ASIDs: tagged address spaces
(Milestone 15. The mechanism behind crates/address_space_identifier and the disappearance of the context-switch
TLB flush.)
The problem
The TLB caches translations. Two processes both have a 0x40_0000, mapping different frames.
Until this milestone the TLB could not tell them apart, because every user mapping was global:
its entries matched under any address space. The only safe switch was tlbi vmalle1is, discard
every EL1 translation on every core, the kernel's included, and then re-walk everything from
cold. Correct, and the code said so honestly, and slow in exactly the way that matters on the
hot path of a microkernel: switches are what an IPC-heavy system does all day.
The mechanism, in three parts
- The
nGbit (descriptor bit 11, "not-global"): a TLB entry made from annGmapping is tagged with the ASID that was live during the walk, and matches lookups only under that same ASID. Every user constructor inpage_tablesnow sets it. Kernel mappings stay global on purpose: the high half is identical for everyone, and tagging it would waste TLB entries re-caching the same kernel translation per process. - The ASID rides in
TTBR0_EL1's top bits, written together with the table root as one composed value (mmu::ttbr0_value). Installing an address space is installing its tag. - Each address space owns one ASID for life (
crates/address_space_identifier): allocated at creation, freed at teardown aftertlbi aside1ishas destroyed every entry wearing it. That flush-then-free order is the whole reuse contract, stated at the drop site.
The switch itself now flushes nothing. The old space's entries stop matching instead of being destroyed; come back to that process later and its translations may still be warm. Invalidation survives at exactly two targeted points: revocation flushes by VA across all ASIDs (a shared page leaving every space), and teardown flushes by ASID (a space leaving the world).
Why allocation is a bitmap, not Linux's generation scheme
The roadmap sketched "generation/rollover", the Linux design: processes outnumber ASIDs there,
so exhaustion bumps a generation, mass-flushes once, and reassigns lazily. Milestone 14 changed
the arithmetic under that sketch: concurrent address spaces are bounded (MAXIMUM_SPACES,
enforced by the revocation registry), and a bitmap answers "none free" cleanly. So: one bitmap,
ASID 0 born reserved for the kernel (the "nobody is home" reserved table runs as ASID 0 forever),
255 numbers. MAXIMUM_SPACES is 1,056, so the 255 numbers, not the registry, bound the live
address spaces, and an allocation past them is refused as a full registry is. The first answer to
that is 16-bit ASIDs (one TCR bit plus an ID-register check), not a new algorithm.
The same three parts on RISC-V, and what they cost there instead
Milestone 58 brought this to the second ISA, and every part of the mechanism above has a twin whose shape differs enough to be worth naming.
- Non-global is the default, not an opt-in. Sv39 has a
Gbit meaning global, where aarch64 hasnGmeaning the opposite, so a user mapping is tagged unless somebody says otherwise. The kernel half isG, for the same reason it is global on aarch64. - The tag rides in
satp[59:44], above the root PPN and below MODE, packed with no slack. One register holds mode, tag and root, which is why installing an address space is a singlecsrw. - Teardown is not one instruction.
sfence.vmais local. Discharging an ASID machine-wide means an IPI to every other hart through SBI RFENCE and an acknowledgement, which is the whole of milestone 58 and is written up in notes/riscv-tlb-shootdown.md.
And one thing that is not a twin: RISC-V permits satp.ASID to be zero bits wide where aarch64
mandates eight. The paragraph above about 255 numbers being safely below the smallest hardware ASID
space is an aarch64 fact. RISC-V probes the width at boot and keeps flushing on every switch if the
machine cannot tell the numbers apart.
What is proved, and what is witnessed
Three Kani harnesses in crates/address_space_identifier (script/verify), the frontier crate
notes/verification.md predicted:
| Harness | Property |
|---|---|
the_kernel_asid_is_never_allocated |
no reachable state hands out ASID 0: a user space can never share the kernel's tag |
two_live_asids_are_distinct |
two live allocations never alias, from any state (sharing a tag would let the TLB serve one process the other's memory) |
free_releases_exactly_its_own_asid |
free clears its own bit and nothing else |
The flush-before-reuse half of the contract is kernel-side and runs on hardware, so it is
witnessed rather than proved. asid_tagging_keeps_address_spaces_apart_without_flushes maps the
same VA to different bytes in two spaces, switches between them with no flush, and demands each
space read its own byte. If nG were missing, the tag were not in TTBR0, or two spaces shared an
ASID, that test reads the wrong byte. It runs on both ISAs since milestone 58 (RISC-V TLB shootdown). Before that it could
not be ported honestly, because RISC-V's context switch flushed everything and the twin would have
passed for the wrong reason.
The machine-wide half has its own witness since then too. an_asid_flush_reaches_the_other_cores
caches a translation on another core, changes the mapping with no per-address invalidation at all,
calls flush_asid, and demands the other core see the change. It fails if the sweep stays local,
which on RISC-V it does without the SBI RFENCE, and it is the only test of aarch64's tlbi aside1is
broadcast.