Re: A basic question question about volatile use



I have a memory-mapped peripheral with a mapping like, say,
struct T {uint8_t read, write, status, forkicks;};

If I slap a volatile on an object of type struct T, does it guarantee
that all accesses to the members are byte-wide, or is the compiler free
to read or read-modify-write in any data width it chooses?

I don't believe that anything in the standard guarantees the right
thing for memory-mapped I/O (the vague promises aren't good enough),
What if it is not an I/O but a normal memory location with intended
byte-size members? (BTW, why the difference?)

Ordinary RAM does not care about the number of accesses or the width
of accesses. What you write can be read back. Ordinary RAM doesn't
usually NEED volatile unless there is some asynchronous code (signal
handler, interrupt routine, etc.) that can change the contents
without the compiler knowing about it.

With memory-mapped I/O, reading or writing a register may trigger
an action (clearing a "data ready" flag, fetching the next set of
data, launching a missile, etc. What you read (the "status" register)
may have nothing to do with what you write (the "command" register)
except they share the same address (so turning a byte write into a
word fetch, replace a byte, write a word may be a disaster, even
though it's not an issue with ordinary RAM). Accessing the register
with the wrong I/O width may cause malfunctions or access something
entirely different. The contents of a memory-mapped I/O device
register may change on its own (e.g. data ready or status flags,
or a clock).

.



Relevant Pages

  • Re: Load ordering by introducing dependencies
    ... So, discuss it with Linus: ... In C, it's "data" that is volatile, but that is insane. ... Accesses aren't volatile. ... data that may be at that address or in that register. ...
    (comp.lang.asm.x86)
  • Re: [PATCH 2/2] Markers Implementation for Preempt RCU Boost Tracing
    ... is for them to have minimal effect! ... marker function/macro name itself. ... the tracepoint accesses). ... Worst-case we'll have some additional register constraints ...
    (Linux-Kernel)
  • Re: i8042 access timings
    ... It made me curious as we only wait between accesses to status ... > register but not data register. ... actually require the delays are the Digital Hinote laptops, ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)