|
15 | 15 | // This crate also provides a basic physical memory frame |
16 | 16 | // allocator, which is used in the allocator module to |
17 | 17 | // 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. |
181 | 18 |
|
182 | 19 | use bootloader::BootInfo; |
183 | 20 | use x86_64::registers::control::Cr3; |
|
0 commit comments