简体   繁体   中英

Raspberry Pi ldrex causes data abort

I have a simple bare metal Raspberry Pi project in which I'm trying to implement spin locks. This is my code:

spinlock_lock:
    push {r4, r5, lr} 

    mov r5, #0x1
1:
    ldrex r4, [r0]
    teq r4, #0
    strexeq r4, r5, [r0]
    teqeq r4, #0
    bne 1b

    pop {r4, r5, pc} 

The problem is that the ldrex causes a data abort. The pointer I'm passing is page aligned, and the ARM runs in system mode. The strange thing is, replacing this code with a version that uses non exclusive loads/stores, it works. Is there anything I need to keep in mind when using exclusive loads and stores?

[ignoring non-MMU architectures for clarity here]

The exclusive-access instructions are only guaranteed to work on Normal memory. In ARMv7-A, it is implementation defined whether they will work on Strongly-ordered or Device memory - unless the system documentation explicitly says it supports this, expect exclusives to Strongly-ordered or Device memory to be unpredictable. ARMv6, which applies here, is even more strict:

LDREX and STREX operations shall only be performed on memory supporting the Normal memory attribute.

When the MMU is turned off, instruction accesses are treated as Normal, but data accesses are treated as Strongly-ordered. Thus attempting to use exclusives with the MMU off is likely to go bang - in order to make use of them, I think there's no alternative but to set up some minimal page tables for an identity mapping with the right attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM