Re:Memory-mapped IO registers in Zig. (2021)
by Joker_vD on Tuesday April 23, 2024 @09:02AM
(#40131484)
Wait, is it even possible to do an atomic volatile modify of a MMIO register? Sure, x86 has LOCK prefix but would it work with memory-mapped stuff?
[ Parent
| on HN ]
Re:Memory-mapped IO registers in Zig. (2021)
by auxym on Tuesday April 23, 2024 @09:11AM
(#40131567)
From my understanding, the only thing volatile does (IIRC this is implementation specified as the standard is vague) is prevent the compiler from optimizing out reads or writes when it thinks they are "useless". That is, is tells the compiler "this memory location may change independently of that the code is doing, so you always need to read or write it when I say so".
Atomicity is sort of a different thing. You might need things like critical sections (disable interrupts) or memory fencing for the read-modify-write cycle.
Re:Memory-mapped IO registers in Zig. (2021)
by matt-noonan on Tuesday April 23, 2024 @09:33AM
(#40131788)
There are some interesting solutions out there, such as bit-banding used in some ARM Cortex CPUs. This maps entire bytes in the high part of the address space to single bits in the low part of the address space, so that you can make an atomic set or clear of a single physical bit by writing to a byte of memory. https://spin.atomicobject.com/bit-banding/