ARM 中的原子操作 [英] Atomic operations in ARM

查看:40
本文介绍了ARM 中的原子操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为 ARM 开发嵌入式操作系统,但是即使在参考了 ARMARM 和 linux 源代码之后,我也有一些关于架构的事情我不明白.

I've been working on an embedded OS for ARM, However there are a few things i didn't understand about the architecture even after referring to ARMARM and linux source.

原子操作.

ARM ARM 说 Load 和 Store 指令是原子的,它的执行保证在中断处理程序执行之前完成.验证通过查看

ARM ARM says that Load and Store instructions are atomic and it's execution is guaranteed to be complete before interrupt handler executes. Verified by looking at

arch/arm/include/asm/atomic.h :
    #define atomic_read(v)  (*(volatile int *)&(v)->counter)
    #define atomic_set(v,i) (((v)->counter) = (i))

但是,当我想使用 cpu 指令(atomic_inc、atomic_dec、atomic_cmpxchg 等)以原子方式操作此值时,问题就出现了,这些指令将 LDREX 和 STREX 用于 ARMv7(我的目标).

However, the problem comes in when i want to manipulate this value atomically using the cpu instructions (atomic_inc, atomic_dec, atomic_cmpxchg etc..) which use LDREX and STREX for ARMv7 (my target).

ARMARM 没有说明本节中中断被阻塞的任何内容,因此我假设中断可能发生在 LDREX 和 STREX 之间.它确实提到的是关于锁定内存总线,我猜这仅对 MP 系统有帮助,因为 MP 系统可能有更多 CPU 试图同时访问同一位置.但是对于 UP(也可能是 MP),如果在 LDREX 和 STREX 的这个小窗口中触发了定时器中断(或 SMP 的 IPI),则异常处理程序执行可能会更改 cpu 上下文并返回到新任务,但是现在令人震惊的部分来了,它执行 'CLREX' 并因此删除前一个线程持有的任何排他锁.那么,在 UP 系统上,使用 LDREX 和 STREX 比使用 LDR 和 STR 更好地实现原子性如何?

ARMARM doesn't say anything about interrupts being blocked in this section so i assume an interrupt can occur in between the LDREX and STREX. The thing it does mention is about locking the memory bus which i guess is only helpful for MP systems where there can be more CPUs trying to access same location at same time. But for UP (and possibly MP), If a timer interrupt (or IPI for SMP) fires in this small window of LDREX and STREX, Exception handler executes possibly changes cpu context and returns to the new task, however the shocking part comes in now, it executes 'CLREX' and hence removing any exclusive lock held by previous thread. So how better is using LDREX and STREX than LDR and STR for atomicity on a UP system ?

我确实读过一些关于独占锁监视器的内容,所以我有一个可能的理论,即当线程恢复并执行 STREX 时,操作系统监视器会导致此调用失败,这可以被检测到并且可以重新执行循环在进程中使用新值(分支回到 LDREX),我在这里吗?

I did read something about an Exclusive lock monitor, so I've a possible theory that when the thread resumes and executes the STREX, the os monitor causes this call to fail which can be detected and the loop can be re-executed using the new value in the process (branch back to LDREX), Am i right here ?

推荐答案

好的,从他们的 网站.

如果上下文切换在进程执行了 Load-Exclusive 之后但在它执行 Store-Exclusive 之前调度了一个进程,当进程恢复时,Store-Exclusive 返回一个假阴性结果,并且内存不会更新.这不会影响程序功能,因为进程可以立即重试操作.

If a context switch schedules out a process after the process has performed a Load-Exclusive but before it performs the Store-Exclusive, the Store-Exclusive returns a false negative result when the process resumes, and memory is not updated. This does not affect program functionality, because the process can retry the operation immediately.

这篇关于ARM 中的原子操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆