标签: load-link-store-conditional

ARM Cortex M7 什么时候真正需要 CLREX?

我在网上找到了几个地方,其中指出每当进入中断例程时“必须”调用 CLREX,我不明白。CLREX状态的文档(添加编号以便于参考):

(1) 清除执行处理器的本地记录,该记录表明地址已请求独占访问。

(2) 使用该CLREX指令将一个紧密耦合的独占访问监视器返回到其开放访问状态。这消除了对内存的虚拟存储的要求。

(3) 是否CLREX也清除执行处理器的全局记录,即某个地址已请求独占访问,则由实现定义。

我在这里几乎什么都不懂。

我的印象是,按照文档中示例编写一些内容足以保证原子性:

    MOV r1, #0x1                ; load the ‘lock taken’ value
try:                                                       <---\
    LDREX r0, [LockAddr]        ; load the lock value          |
    CMP r0, #0                  ; is the lock free?            |
    STREXEQ r0, r1, [LockAddr]  ; try and claim the lock       |
    CMPEQ r0, #0                ; did this succeed?            |
    BNE try                     ; no - try again   ------------/
    ....                        ; yes - …
Run Code Online (Sandbox Code Playgroud)

arm atomic cortex-m load-link-store-conditional

9
推荐指数
2
解决办法
1062
查看次数

ldrex / strex如何使ARM中的atomic_add成为原子操作?

根据http://lxr.free-electrons.com/source/arch/arm/include/asm/atomic.h#L31

 static inline void atomic_add(int i, atomic_t *v)
 41 {
 42         unsigned long tmp;
 43         int result;
 44 
 45         prefetchw(&v->counter);
 46         __asm__ __volatile__("@ atomic_add\n"
 47 "1:     ldrex   %0, [%3]\n"
 48 "       add     %0, %0, %4\n"
 49 "       strex   %1, %0, [%3]\n"
 50 "       teq     %1, #0\n"
 51 "       bne     1b"
 52         : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
 53         : "r" (&v->counter), "Ir" (i)
 54         : "cc");
 55 }
Run Code Online (Sandbox Code Playgroud)

可以抢占,怎么能叫“原子”呢?

arm atomic inline-assembly linux-kernel load-link-store-conditional

1
推荐指数
1
解决办法
3384
查看次数