RISC-V/Paging
Paging in RISC-V can be enabled by first setting up a page table, then pointing the CPU to that page table via the satp
CSR. This article will describe these steps.
Paging type can be determined at runtime using the device tree, its typically located in the CPU specific nodes, such as `cpu@x` where x denotes which number of CPU it is, it will be located under the `mmu-type` property in the form of a string such as `riscv,svY` is either "32", "39", "48", or "57" depending on paging type.
Terminology
There are many acronyms and other terms that are used in the RISC-V specification which, for the purpose of brevity and consistency, will be used in this article as well. I will assume a baseline knowledge of the purpose and basics of paging and virtual addressing.
PPNs and VPNs
A physical page number (or PPN) is a unique, page-aligned identifier for a page in physical memory. The number of PPN segments for an address will depend on the paging mode.
To construct a PPN from a physical address, you can simply (logically, not arithmetically) shift the value right by 12 bits (as 2**12 = 4096). Note that this operation will truncate the offset into the page, if the address was not page-aligned.
A virtual page number (or VPN) is very similar to a PPN, but in the virtual address space. Again, the number of VPN segments for a virtual address will depend on the paging mode.
RISC-V Page Table Layout
There are various paging modes available in the RISC-V ISA, differing by the number of virtual address bits:
Name | Memory Size | SXLEN |
---|---|---|
Sv32 | 4GiB | 32-bit only |
Sv39 | 512GiB | 64-bit only |
Sv48 | 256TiB | 64-bit only |
Sv57 | 128PiB | 64-bit only |
Feel free to choose between these modes as you see fit; this page will describe all of them. Note that in 64-bit systems, Sv39 is more than enough for most hobby operating systems (though it is not much more difficult to go further).
Sv32
TODO
Sv39
Sv39 is the simplest paging mode for 64-bit systems (though larger modes are not too much more difficult to implement). It supports the standard 4KiB pages, as well as 2MiB megapages and 1GiB gigapages.
TODO: actually describe what a page table looks like, and also PTE's RWX encoding
Address and PTE Layouts
Bit Range (inclusive) | Length (bits) | Description |
---|---|---|
0-11 | 12 | Page offset |
12-20 | 9 | Page VPN (VPN[0]) |
21-29 | 9 | Megapage VPN (VPN[1]) |
30-38 | 9 | Gigapage VPN (VPN[2]) |
Bit Range (inclusive) | Length (bits) | Description |
---|---|---|
0-11 | 12 | Page offset |
12-20 | 9 | PPN[0] |
21-29 | 9 | PPN[1] |
30-55 | 26 | PPN[2] |
Bit Range (inclusive) | Length (bits) | Description |
---|---|---|
0 | 1 | Valid flag (V) |
1 | 1 | Readable flag (R) |
2 | 1 | Writable flag (W) |
3 | 1 | Executable flag (X) |
4 | 1 | U-mode accessible flag (U) |
5 | 1 | Global flag (G) |
6 | 1 | Accessed flag (A) |
7 | 1 | Dirty flag (D) |
8-9 | 2 | RSW (TODO) |
10-18 | 9 | PPN[0] |
19-27 | 9 | PPN[1] |
28-53 | 26 | PPN[2] |
54-60 | 7 | Reserved |
61-62 | 2 | PBMT (TODO) |
63 | 1 | N bit (TODO) |
Sv48
TODO: Explain
Address and PTE Layouts
Bit Range (inclusive) | Length (bits) | Description |
---|---|---|
0-11 | 12 | Page offset |
12-20 | 9 | Page VPN (VPN[0]) |
21-29 | 9 | Megapage VPN (VPN[1]) |
30-38 | 9 | Gigapage VPN (VPN[2]) |
39-47 | 9 | (VPN[3]) |
Bit Range (inclusive) | Length (bits) | Description |
---|---|---|
0-11 | 12 | Page offset |
12-20 | 9 | (PPN[0]) |
21-29 | 9 | (PPN[1]) |
30-38 | 9 | (PPN[2]) |
39-47 | 9 | (PPN[3]) |
Bit Range (inclusive) | Length (bits) | Description |
---|---|---|
0 | 1 | Valid flag (V) |
1 | 1 | Readable flag (R) |
2 | 1 | Writable flag (W) |
3 | 1 | Executable flag (X) |
4 | 1 | U-mode accessible flag (U) |
5 | 1 | Global flag (G) |
6 | 1 | Accessed flag (A) |
7 | 1 | Dirty flag (D) |
8-9 | 2 | RSW (TODO) |
10-18 | 9 | PPN[0] |
19-27 | 9 | PPN[1] |
28-36 | 9 | PPN[2] |
37-53 | 17 | PPN[3] |
54-60 | 7 | Reserved |
61-62 | 2 | PBMT (TODO) |
63 | 1 | N bit (TODO) |
Sv57
TODO
Enabling Paging
TODO