diff --git a/posts/DRAFT-paging.md b/posts/DRAFT-paging.md index 2ded8b37..259c6fe8 100644 --- a/posts/DRAFT-paging.md +++ b/posts/DRAFT-paging.md @@ -6,11 +6,29 @@ title: 'A Paging Module' TODO ## Paging +_Paging_ is a memory management scheme that separates virtual and physical memory. The address space is split into equal sized _pages_ and _page tables_ specify which virtual page points to which physical page. For an extensive paging introduction take a look at the paging chapter ([PDF][paging chapter]) of the [Three Easy Pieces] OS book. -TODO +[paging chapter]: http://pages.cs.wisc.edu/~remzi/OSTEP/vm-paging.pdf +[Three Easy Pieces]: http://pages.cs.wisc.edu/~remzi/OSTEP/ + +The x86 architecture uses a 4-level page table in 64-bit mode. A virtual address has the following structure: + +![structure of a virtual address on x86](/images/x86_address_structure.svg) + +The bits 48–63 are so-called _sign extension_ bits and must be copies of bit 47. The following 36 bits define the page table indexes (9 bits per table) and the last 12 bits specify the offset in the 4KiB page. + +Each table has 2^9 = 512 entries and each entry is 8 byte. Thus a page table fits exactly in one page (4 KiB). + +To translate an address, the CPU reads the P4 address from the CR3 register. Then it uses the indexes to walk the tables: + +![translation of virtual to physical addresses in 64 bit mode](/images/X86_Paging_64bit.svg) + +The P4 entry points to a P3 table, where the next 9 bits of the address are used to select an entry. The P3 entry then points to a P2 table and the P2 entry points to a P1 table. The P1 entry, which is specified through bits 12–20, finally points to the physical page. + +So let's create a Rust module for it! ## A Basic Paging Module -Let's create a basic `memory/paging/mod.rs` module: +We start by creating a basic `memory/paging/mod.rs` module: ```rust use memory::PAGE_SIZE;