One of the missed opportunities that makes me sad in both AArch64 and RISC-V (I ...
David Chisnall (*Now with 50% more sarcasm!*) @david_chisnall@infosec.exchange
One of the missed opportunities that makes me sad in both AArch64 and RISC-V (I advocated for it when both were still new) is not moving to 64-bit ASIDs.
On a system that supports virtual memory, there's a small (or, on modern chips, large!) cache of address translations so that you don't need to walk the page tables[1] on every load or store (or instruction fetch). This cache, the Translation Look-aside Buffer (TLB) is fast, but filling it when you miss is slow (with virtualisation extensions, you may need over 20 memory accesses to fill it and each of those might miss in the cache).
In early systems with TLBs, every time you did a context switch, you had to flush the TLB. This meant that a process that you context switched to would be slow until it had filled the TLB. Back then, TLBs were pretty small, so this wasn't a huge penalty, but it was nice to avoid. The solution was a tagged TLB.
In a tagged TLB, each entry contains an additional address-space identifier (ASID). When you context switch, you write to a special register to say 'the new process is ASID 42' (or whatever) and now any TLB lookup that returns a valid translation that is not for address space 42 is ignored. New fills are associated with address space 42, so the next context switch doesn't blow away all of your translations.
Early systems with ASIDs typically used an 8-bit ASID. This worked for a lot of hot paths (especially if you managed them per CPU / core) but it added some kernel complexity. My laptop currently reports that I have 1,050 processes running. With an 8-bit ASID, I can't represent them all and so the kernel needs to periodically note that it's run out and then do a full TLB invalidate (or invalidate one ASID) and recycle it. This introduces some annoying performance cliffs.
AArch64 went to 16-bit ASIDs (I think AArch32 had them as an option?), which makes this a lot better. With a 16-bit ASID, have 64 Ki ASIDs available and so the kernel doesn't have to invalidate ASIDs very often. But it can still run out, because you will almost certainly launch more than 216 processes in between booting a general-purpose OS and shutting it down. For some workloads with a lot of short-lived processes, you need to recycle ASIDs fairly rapidly.
With a 64-bit ASID, you don't. You can use a monotonic counter (even doing this the naïve way, one atomic fetch and increment doesn't add a measurable overhead to process creation, even on a 128-core system).
From a software perspective, moving to a 64-bit ASID just eliminates a bunch of code in your kernel. It is strictly better. But what about hardware?
Well, it turns out that some implementations don't actually use a 16-bit ASID in their TLB even when they expose one architecturally. When you set the ASID for the current process, it hashes that ASID to a smaller number and then checks to see whether that number is either unused or used for the architectural ASID. If not, it flushes all TLB entries with that ASID (something you need to be able to do with an ASID anyway). This requires a small amount of hardware but none on any critical paths. Internally, you may still have any sized ASID you want. A tiny core may have a 4-bit microarchitectural ASID and still get some benefit, a large one may go to 12 bits or even large. As the number of address space IDs approaches the number of TLB entries, the benefits rapidly drop off, so there's a limit to the number that it's useful to support and that is typically less than 216.
Is this fast? Well, it turns out AMD processors have been doing this for ages. AMD had a patent on using the page-table base register (which is either 32 or 64 bits) as the ASID on x86. And they do exactly this trick: hash the address and use it as an ASID. They avoid needing an explicit ASID by using the page-table base and tracking some writes for implicit invalidation, but the hashing a 64-bit ASID to a smaller microarchitectural one is something that they've been doing for a very long time.
And that's why you should co-design your ISA with your software model. 64-bit ASIDs simplify software and add almost no complexity to hardware.
[1] Most modern CPUs use radix trees for the canonical address space mappings. Radix trees are not the optimal data structure for these, especially for modern workloads, but both the Linux and NT kernels (though not *BSD / XNU) are full of assumptions that the hardware wants a radix tree so we're stuck with them.