RISC-V/Paging

From FriendOS Wiki
Revision as of 02:53, 24 November 2022 by ThePuzzlemaker (talk | contribs) (Describe PPNs and VPNs)
Jump to navigation Jump to search

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.

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

TODO

Sv48

TODO

Sv57

TODO

Enabling Paging

TODO