Skip to content

Commit e93db68

Browse files
committed
Moved the paging documentation to the README
Signed-off-by: SlyMarbo <[email protected]>
1 parent cbeb767 commit e93db68

File tree

2 files changed

+173
-163
lines changed

2 files changed

+173
-163
lines changed

kernel/src/memory/README.md

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,176 @@ Firefly uses the following layout of virtual memory:
1414
| Kernel stacks 1+ | `0xffff_8000_555d_0000` | `0xffff_8000_5d5c_ffff` | 32,768x 4 KiB page | 128 MiB |
1515
| MMIO address space | `0xffff_8000_6666_0000` | `0xffff_8000_6675_ffff` | 256x 4 KiB page | 1 MiB |
1616
| Physical memory map | `0xffff_8000_8000_0000` | `0xffff_ffff_ffff_ffff` | rest of memory | < 128 TiB |
17+
18+
## Paging
19+
20+
The following high-level discussion of 4-level paging may be helpful:
21+
22+
Paging maps a virtual address (referred to in the Intel manuals as a 'linear address')
23+
to a physical address, through a series of page tables. Different parts of the virtual
24+
address reference different tables, as shown below:
25+
26+
```
27+
6 5 4
28+
3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
29+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30+
| Ignored | PML4 | PDPT ~
31+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32+
33+
3 2 1
34+
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
35+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36+
~ | PDT | Table | Offset |
37+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38+
39+
Ignored: Not used during address translation.
40+
PML4: Used as an index into the Page Map Level 4 table (9 bits, 0-511).
41+
PDPT: Used as an index into the Page Directory Pointer table (9 bits, 0-511).
42+
PDT: Used as an index into the Page Directory table (9 bits, 0-511).
43+
PT: Used as an index into the Page table (9 bits, 0-511).
44+
Offset: Used as an index into the page (12 bits, 4kB).
45+
```
46+
47+
A PML4 table comprises 512 64-bit entries (PML4Es)
48+
49+
PML4 entry referencing a PDP entry:
50+
51+
```
52+
6 5 4
53+
3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
54+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55+
|X| - | PDPT Address ~
56+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57+
58+
3 2 1
59+
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
60+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61+
~ PDPT Address | - |S|-|A|C|W|U|R|P|
62+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63+
64+
X (Execute disable): Whether the memory is executable (0) or not (1).
65+
- (Ignored)
66+
PDPT Address: The address of the entry in the Page Directory Pointer Table.
67+
- (Ignored)
68+
S (Page size): Must be 0.
69+
- (Ignored)
70+
A (Accessed): Whether the memory has been accessed (1) or not (0).
71+
C (Cache disable): Whether the memory has caching enabled (0) or disabled (1).
72+
W (Write-through): Whether the memory has write-through caching (1) or write-back (0).
73+
U (User): Whether the memory is accessible to userspace.
74+
R (Read-only): Whether the memory is read/write (1) or read-only (0).
75+
P (Present): Whether this entry is active (1) or absent (0).
76+
```
77+
78+
A 4-KByte naturally aligned page-directory-pointer table is located at the
79+
physical address specified in bits 51:12 of the PML4E. A page-directory-pointer
80+
table comprises 512 64-bit entries (PDPTEs).
81+
82+
PDPT entry referencing a PD entry:
83+
84+
```
85+
6 5 4
86+
3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
87+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88+
|X| - | PD Address ~
89+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90+
91+
3 2 1
92+
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
93+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94+
~ PD Address | - |S|-|A|C|W|U|R|P|
95+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96+
97+
X (Execute disable): Whether the memory is executable (0) or not (1).
98+
- (Ignored)
99+
PD Address: The address of the entry in the Page Directory table.
100+
- (Ignored)
101+
S (Page size): Whether the address is for a PD entry (0) or a physical address (1).
102+
- (Ignored)
103+
A (Accessed): Whether the memory has been accessed (1) or not (0).
104+
C (Cache disable): Whether the memory has caching enabled (0) or disabled (1).
105+
W (Write-through): Whether the memory has write-through caching (1) or write-back (0).
106+
U (User): Whether the memory is accessible to userspace.
107+
R (Read-only): Whether the memory is read/write (1) or read-only (0).
108+
P (Present): Whether this entry is active (1) or absent (0).
109+
```
110+
111+
Because a PDPTE is identified using bits 47:30 of the linear address, it controls
112+
access to a 1-GByte region of the linear-address space. Use of the PDPTE depends
113+
on its PS flag:
114+
115+
- If the PDPTE’s PS flag is 1, the PDPTE maps a 1-GByte page.
116+
- If the PDPTE’s PS flag is 0, a 4-KByte naturally aligned page directory is
117+
located at the physical address specified in bits 51:12 of the PDPTE. A page
118+
directory comprises 512 64-bit entries.
119+
120+
PD entry referencing a 2MB page:
121+
122+
```
123+
6 5 4
124+
3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
125+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126+
|X| - | PT Address ~
127+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128+
129+
3 2 1
130+
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
131+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132+
~ PT Address | - |S|-|A|C|W|U|R|P|
133+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134+
135+
X (Execute disable): Whether the memory is executable (0) or not (1).
136+
- (Ignored)
137+
PT Address: The address of the page table.
138+
- (Ignored)
139+
S (Page size): Whether the address is for a PT entry (0) or a physical address (1).
140+
- (Ignored)
141+
A (Accessed): Whether the memory has been accessed (1) or not (0).
142+
C (Cache disable): Whether the memory has caching enabled (0) or disabled (1).
143+
W (Write-through): Whether the memory has write-through caching (1) or write-back (0).
144+
U (User): Whether the memory is accessible to userspace.
145+
R (Read-only): Whether the memory is read/write (1) or read-only (0).
146+
P (Present): Whether this entry is active (1) or absent (0).
147+
```
148+
149+
Because a PDE is identified using bits 47:21 of the linear address, it
150+
controls access to a 2-MByte region of the linear-address space. Use of
151+
the PDE depends on its PS flag:
152+
153+
- If the PDE's PS flag is 1, the PDE maps a 2-MByte page.
154+
- If the PDE’s PS flag is 0, a 4-KByte naturally aligned page table is
155+
located at the physical address specified in bits 51:12 of the PDE.
156+
A page table comprises 512 64-bit entries.
157+
158+
PT entry referencing a 4kB page:
159+
160+
```
161+
6 5 4
162+
3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
163+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
164+
|X| - | Page Address ~
165+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
166+
167+
3 2 1
168+
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
169+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
170+
~ Page Address | - |G|S|-|A|C|W|U|R|P|
171+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
172+
173+
X (Execute disable): Whether the memory is executable (0) or not (1).
174+
- (Ignored)
175+
PT Address: The address of the page table.
176+
- (Ignored)
177+
G (Global): Whether to flush the TLB cache when changing mappings.
178+
S (Page size): Whether the address is for a PT entry (0) or a physical address (1).
179+
D (Dirty): Whether the memory has been written (1) or not (0).
180+
A (Accessed): Whether the memory has been accessed (1) or not (0).
181+
C (Cache disable): Whether the memory has caching enabled (0) or disabled (1).
182+
W (Write-through): Whether the memory has write-through caching (1) or write-back (0).
183+
U (User): Whether the memory is accessible to userspace.
184+
R (Read-only): Whether the memory is read/write (1) or read-only (0).
185+
P (Present): Whether this entry is active (1) or absent (0).
186+
```
187+
188+
Because a PTE is identified using bits 47:21 of the linear address, it
189+
controls access to a 4-kByte region of the linear-address space.

kernel/src/memory/mod.rs

Lines changed: 0 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -15,169 +15,6 @@
1515
// This crate also provides a basic physical memory frame
1616
// allocator, which is used in the allocator module to
1717
// build the memory manager.
18-
//
19-
// Although paging is covered by the x86_64 crate, the
20-
// following high-level discussion of 4-level paging may
21-
// be helpful:
22-
//
23-
// Paging maps a virtual address (referred to in the Intel manuals as a 'linear address')
24-
// to a physical address, through a series of page tables. Different parts of the virtual
25-
// address reference different tables, as shown below:
26-
//
27-
// 6 5 4
28-
// 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
29-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30-
// | Ignored | PML4 | PDPT ~
31-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32-
//
33-
// 3 2 1
34-
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
35-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36-
// ~ | PDT | Table | Offset |
37-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38-
//
39-
// Ignored: Not used during address translation.
40-
// PML4: Used as an index into the Page Map Level 4 table (9 bits, 0-511).
41-
// PDPT: Used as an index into the Page Directory Pointer table (9 bits, 0-511).
42-
// PDT: Used as an index into the Page Directory table (9 bits, 0-511).
43-
// PT: Used as an index into the Page table (9 bits, 0-511).
44-
// Offset: Used as an index into the page (12 bits, 4kB).
45-
//
46-
// A PML4 table comprises 512 64-bit entries (PML4Es)
47-
//
48-
// PML4 entry referencing a PDP entry:
49-
//
50-
// 6 5 4
51-
// 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
52-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53-
// |X| - | PDPT Address ~
54-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55-
//
56-
// 3 2 1
57-
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
58-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59-
// ~ PDPT Address | - |S|-|A|C|W|U|R|P|
60-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61-
//
62-
// X (Execute disable): Whether the memory is executable (0) or not (1).
63-
// - (Ignored)
64-
// PDPT Address: The address of the entry in the Page Directory Pointer Table.
65-
// - (Ignored)
66-
// S (Page size): Must be 0.
67-
// - (Ignored)
68-
// A (Accessed): Whether the memory has been accessed (1) or not (0).
69-
// C (Cache disable): Whether the memory has caching enabled (0) or disabled (1).
70-
// W (Write-through): Whether the memory has write-through caching (1) or write-back (0).
71-
// U (User): Whether the memory is accessible to userspace.
72-
// R (Read-only): Whether the memory is read/write (1) or read-only (0).
73-
// P (Present): Whether this entry is active (1) or absent (0).
74-
//
75-
// A 4-KByte naturally aligned page-directory-pointer table is located at the
76-
// physical address specified in bits 51:12 of the PML4E. A page-directory-pointer
77-
// table comprises 512 64-bit entries (PDPTEs).
78-
//
79-
// PDPT entry referencing a PD entry:
80-
//
81-
// 6 5 4
82-
// 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
83-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
84-
// |X| - | PD Address ~
85-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86-
//
87-
// 3 2 1
88-
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
89-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90-
// ~ PD Address | - |S|-|A|C|W|U|R|P|
91-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92-
//
93-
// X (Execute disable): Whether the memory is executable (0) or not (1).
94-
// - (Ignored)
95-
// PD Address: The address of the entry in the Page Directory table.
96-
// - (Ignored)
97-
// S (Page size): Whether the address is for a PD entry (0) or a physical address (1).
98-
// - (Ignored)
99-
// A (Accessed): Whether the memory has been accessed (1) or not (0).
100-
// C (Cache disable): Whether the memory has caching enabled (0) or disabled (1).
101-
// W (Write-through): Whether the memory has write-through caching (1) or write-back (0).
102-
// U (User): Whether the memory is accessible to userspace.
103-
// R (Read-only): Whether the memory is read/write (1) or read-only (0).
104-
// P (Present): Whether this entry is active (1) or absent (0).
105-
//
106-
// Because a PDPTE is identified using bits 47:30 of the linear address, it controls
107-
// access to a 1-GByte region of the linear-address space. Use of the PDPTE depends
108-
// on its PS flag:
109-
//
110-
// - If the PDPTE’s PS flag is 1, the PDPTE maps a 1-GByte page.
111-
// - If the PDPTE’s PS flag is 0, a 4-KByte naturally aligned page directory is
112-
// located at the physical address specified in bits 51:12 of the PDPTE. A page
113-
// directory comprises 512 64-bit entries.
114-
//
115-
// PD entry referencing a 2MB page:
116-
//
117-
// 6 5 4
118-
// 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
119-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120-
// |X| - | PT Address ~
121-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122-
//
123-
// 3 2 1
124-
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
125-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126-
// ~ PT Address | - |S|-|A|C|W|U|R|P|
127-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128-
//
129-
// X (Execute disable): Whether the memory is executable (0) or not (1).
130-
// - (Ignored)
131-
// PT Address: The address of the page table.
132-
// - (Ignored)
133-
// S (Page size): Whether the address is for a PT entry (0) or a physical address (1).
134-
// - (Ignored)
135-
// A (Accessed): Whether the memory has been accessed (1) or not (0).
136-
// C (Cache disable): Whether the memory has caching enabled (0) or disabled (1).
137-
// W (Write-through): Whether the memory has write-through caching (1) or write-back (0).
138-
// U (User): Whether the memory is accessible to userspace.
139-
// R (Read-only): Whether the memory is read/write (1) or read-only (0).
140-
// P (Present): Whether this entry is active (1) or absent (0).
141-
//
142-
// Because a PDE is identified using bits 47:21 of the linear address, it
143-
// controls access to a 2-MByte region of the linear-address space. Use of
144-
// the PDE depends on its PS flag:
145-
//
146-
// - If the PDE's PS flag is 1, the PDE maps a 2-MByte page.
147-
// - If the PDE’s PS flag is 0, a 4-KByte naturally aligned page table is
148-
// located at the physical address specified in bits 51:12 of the PDE.
149-
// A page table comprises 512 64-bit entries.
150-
//
151-
// PT entry referencing a 4kB page:
152-
//
153-
// 6 5 4
154-
// 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
155-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
156-
// |X| - | Page Address ~
157-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158-
//
159-
// 3 2 1
160-
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
161-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
162-
// ~ Page Address | - |G|S|-|A|C|W|U|R|P|
163-
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
164-
//
165-
// X (Execute disable): Whether the memory is executable (0) or not (1).
166-
// - (Ignored)
167-
// PT Address: The address of the page table.
168-
// - (Ignored)
169-
// G (Global): Whether to flush the TLB cache when changing mappings.
170-
// S (Page size): Whether the address is for a PT entry (0) or a physical address (1).
171-
// D (Dirty): Whether the memory has been written (1) or not (0).
172-
// A (Accessed): Whether the memory has been accessed (1) or not (0).
173-
// C (Cache disable): Whether the memory has caching enabled (0) or disabled (1).
174-
// W (Write-through): Whether the memory has write-through caching (1) or write-back (0).
175-
// U (User): Whether the memory is accessible to userspace.
176-
// R (Read-only): Whether the memory is read/write (1) or read-only (0).
177-
// P (Present): Whether this entry is active (1) or absent (0).
178-
//
179-
// Because a PTE is identified using bits 47:21 of the linear address, it
180-
// controls access to a 4-kByte region of the linear-address space.
18118

18219
use bootloader::BootInfo;
18320
use x86_64::registers::control::Cr3;

0 commit comments

Comments
 (0)