Chris Low-level systems developer
← Back to all posts

The Bit That Decides Who Writes the A and D Bits

ISA

the switch is drawn by the ISA: menvcfg.ADUE is bit 61, a WARL field the privileged architecture reserves for exactly this choice; the fault codes 13 and 15 and the A/D bit positions in the PTE are architected, and the load-needs-A, store-needs-A-and-D split is the contract both sides implement.

MICROARCH

not at Microarch because the post describes the architected choice between a hardware update and a software update, not how any pipeline implements the atomic compare-and-swap; no reordering, prediction, or timing claim appears.

FIRMWARE

M-mode does the setup the machine layer owns: it probes ADUE writability, grants the PMP entry, delegates the faults with medeleg, and flips ADUE on the S-mode ecall, because menvcfg is writable only in M-mode.

KERNEL

the software half of the switch is kernel work: the S-mode handler reads scause and stval, sets A and D in the entry, issues sfence.vma, and retries by returning the unchanged sepc; an OS on a chip without Svadu does exactly this on every first touch.

ALGORITHM

not at Algorithm because the post describes who maintains two bookkeeping bits and how the choice is configured, not a computational method; nothing is stated, timed, or compared as an algorithm.

Take one page, at virtual address 0x80100000, identity mapped so virtual equals physical, its page-table entry starting with the Accessed and Dirty bits clear. Accessed means the page has been touched; Dirty means it has been written and must be written back before the OS reuses the frame. Now store a word to that page. What happens next depends on one bit: ADUE, bit 61 of the M-mode CSR menvcfg, the Svadu extension.

Two hand stamps and ink pads on a table, the kind used to mark documents
The hand stamp marks the paper. The post asks who stamps the A and D bits: the hardware walker or the software handler.

With ADUE=0, the store traps exactly once. scause reads 15, a store page fault, and stval holds the faulting address. The S-mode handler sets A and D in the entry, issues sfence.vma to make the update visible, and the retried store succeeds; the entry reads 0x200400c7. A load traps exactly once too, scause 13, and the handler sets A only: 0x20040047. Loads need A, stores need A and D, and the software side of the switch respects exactly that split.

svadu.c, S-mode trap handler (excerpt)
    if(scause==15){    /* store/AMO page fault: software sets A and D */
        if(stval != TEST_VA){ uputs("S-UNEXPECTED store tval="); uputhex(stval); uputc('\n'); for(;;); }
        *test_pte |= (PTE_A|PTE_D);
        sfence_vma();  /* retry the faulting store (sepc unchanged) */
        return sepc;
    }

With ADUE=1, the same store traps zero times. The page-table walker sets A and D itself with an atomic update; the load leaves D clear with A set. QEMU 8.2.2 implements exactly this switch: ADUE gates an atomic compare-and-swap on the entry, otherwise the walk fails into a page fault. Four checks, four passes, three byte-identical runs.

The obvious alternative would be to fix the behavior in silicon, but simple implementations may not want the atomic walker-update logic, so the architecture makes it a runtime bit and standard supervisor software assumes either scheme may be in effect. One bit of configuration replaces a fixed property of the chip, and the trap count tells you who did the writing.

Stuck on a board or a crash? Fixed-price firmware work: crash triage $250/symptom, bare-metal bring-up from $500, C audit from $350. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app