The Store That Isn't
matters at ISA because the verified properties are the concrete rv64 instruction sequences read off the disassembly this run: the volatile poll loop reloads LSR with lbu every iteration and transmits with one sb to 0x10000000, while the plain-pointer loop hoists the load out and spins on a jump to itself, both produced by gcc 13.2.0 at -O2.
not at Microarch because nothing was timed and no pipeline, cache, or predictor behavior enters the argument: the UART's 38.4K baud timing is the peripheral's own clock, and the compiled sequences would carry the same argument on any RV64 implementation.
not at Firmware because no firmware contract enters the argument: the driver programs the UART's registers directly with loads and stores, and no SBI call, trap delegation, or machine-mode handoff is quoted or needed.
matters at Kernel because the claims are about kernel execution context: xv6's UART driver builds register access from the Reg, ReadReg, and WriteReg macros and polls the Line Status Register's transmitter-idle bit in uartputc_sync, each read off xv6's kernel source.
matters at Algorithm because the volatile rule is the portable contract every optimizing C compiler honors: accesses through volatile-qualified lvalues are visible side effects that cannot be hoisted or deleted, which is why the poll loop survives -O2 on any target.
I compiled two one-byte functions, gcc 13.2.0 at -O2 for rv64, and read the disassembly. Both poll bit 5 of the byte at 0x10000005, then store a byte to 0x10000000. Polling means asking the device, over and over, "ready yet?": the loop reads the status register on every iteration until the bit says yes. The source differs by one keyword.
putc_nv: ; plain pointer
lbu a5,5(a4) ; ask once
andi a5,a5,32
bnez a5,.write
.spin:
j .spin ; never asks again
.write:
sb a0,0(a5) ; 0x10000000: the store that sends
putc_v: ; volatile pointer
.poll:
lbu a5,0(a4) ; ask every iteration
andi a5,a5,32
beqz a5,.poll
sb a0,0(a5) ; 0x10000000: the store that sendsputc_nv takes a plain pointer. a4 arrives holding 0x10000000 (the excerpt trims the lui), so lbu a5,5(a4) loads the Line Status Register at 0x10000005, once, before the loop. The compiler hoists it: in its model of memory, a load from an address nothing in the program writes cannot change between iterations. andi a5,a5,32 keeps only bit 5, the transmitter-idle bit. bnez a5,.write skips the spin if the UART was already ready. Otherwise the loop body is empty, and an empty loop is j .spin, a jump to itself. The device can raise the bit a microsecond later; this code will never ask again. sb a0,0(a5) stores the argument byte to 0x10000000; the excerpt trims a second lui that reloads a5 with the base, the polled value being dead after the branch.
putc_v takes a volatile pointer, and the load stays inside the loop. lbu a5,0(a4) reads the register every iteration (a4 holds 0x10000005 here, setup trimmed the same way). andi isolates the bit; beqz a5,.poll goes back while it is clear. A volatile access is a visible side effect: the optimizer may not hoist it, delete it, or merge two of them. The loop survives -O2 because the compiler is forbidden from assuming what the device does between two reads.
Those two addresses are the whole story. This is xv6's UART driver turning an integer into a pointer [1]:
/* the UART control registers are memory-mapped
at address UART0. this macro returns the
address of one of the registers. */
#define Reg(reg) ((volatile unsigned char *)(UART0 + (reg)))
#define ReadReg(reg) (*(Reg(reg)))
#define WriteReg(reg, v) (*(Reg(reg)) = (v))Reg casts UART0 + reg to volatile unsigned char *. The arithmetic is in bytes, so Reg(5) is exactly 0x10000005; there is no scaling to get wrong. The volatile is the entire contract: every dereference is one bus transaction, emitted exactly as written, in order. ReadReg is one load, WriteReg one store. That cuts both ways: an extra ReadReg dropped in for debugging is not a free peek, it is another transaction on the wire. Count your accesses.
The CPU runs the same sb whether the target is DRAM or a device register; the instruction itself cannot tell the two apart. The address decides: the hardware sends different address ranges to different destinations, and 0x10000000 goes to the UART, not to RAM. So the same instruction does two different jobs. To DRAM, sb saves a byte a later load can read back. To 0x10000000, sb drops the byte into the transmit holding register, and the UART clocks it out over the serial line. That is what memory-mapped I/O means: the address selects the destination, not just a cell. DRAM is storage: a store saves a value, a load brings it back. A UART register is a hardware interface: a load returns bits the hardware computed, for example bit 5, which the UART sets itself when its transmitter goes idle and no store from the CPU can set; the store itself is what sends the byte out the serial line.
The compiler treats memory as a closed system. A load from an address nothing in the program wrote is assumed to return the same value every time, so the compiler keeps the first load and deletes the rest. A store to an address nothing in the program reads is assumed to change nothing anyone can see, so the compiler deletes the store. Those are two different failures on a UART. The status register changes on its own schedule, so when the compiler deletes the repeated loads, the poll loop spins forever on the first reading and never sees the device become ready. The transmit register's whole job happens off-chip, so when the compiler deletes the store, the byte is simply never sent. volatile tells the compiler an address is not ordinary memory: every load and store through it must be emitted exactly as written, in order [2]. That is why the volatile poll loop survives -O2, and why the plain-pointer version degrades into a jump to itself.
xv6's driver is the same loop as a function. uartputc_sync spins on bit 5 of the Line Status Register, then calls WriteReg, which is one sb through a volatile pointer to 0x10000000 [1]. Ask the device if it is ready, then store the byte. The store is not a write to memory; it is a message to a second computer, delivered by an instruction that thinks it is writing RAM.
Sources
- 01xv6-riscv kernel/uart.cMIT PDOS
The literal driver this post reads: the Reg, ReadReg, and WriteReg macros that turn integers into pointers, and uartputc_sync's poll on LSR_TX_IDLE.
- 02volatile semanticscppreference.com
The portable contract behind the experiment: every access through a glvalue of volatile-qualified type is treated as a visible side effect for the purposes of optimization, so the poll loop's load cannot be hoisted and the transmit store cannot be deleted.