1 | /* |
---|
2 | * hal_gpt.c - implementation of the Generic Page Table API for x86_64 |
---|
3 | * |
---|
4 | * Copyright (c) 2017 Maxime Villard |
---|
5 | * |
---|
6 | * This file is part of ALMOS-MKH. |
---|
7 | * |
---|
8 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
9 | * under the terms of the GNU General Public License as published by |
---|
10 | * the Free Software Foundation; version 2.0 of the License. |
---|
11 | * |
---|
12 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | * General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
20 | */ |
---|
21 | |
---|
22 | #include <hal_types.h> |
---|
23 | #include <hal_boot.h> /* XXX */ |
---|
24 | #include <hal_gpt.h> |
---|
25 | #include <hal_special.h> |
---|
26 | #include <hal_internal.h> |
---|
27 | |
---|
28 | #include <printk.h> |
---|
29 | #include <bits.h> |
---|
30 | #include <string.h> |
---|
31 | #include <process.h> |
---|
32 | #include <kmem.h> |
---|
33 | #include <thread.h> |
---|
34 | #include <cluster.h> |
---|
35 | #include <ppm.h> |
---|
36 | #include <page.h> |
---|
37 | |
---|
38 | extern vaddr_t __kernel_end; |
---|
39 | size_t kimg_size __in_kdata = 0; |
---|
40 | |
---|
41 | paddr_t pa_avail __in_kdata = 0; |
---|
42 | vaddr_t va_avail __in_kdata = 0; |
---|
43 | vaddr_t tmpva __in_kdata = (KERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2); |
---|
44 | |
---|
45 | paddr_t hal_gpt_bootstrap_palloc(size_t npages) |
---|
46 | { |
---|
47 | paddr_t pa = pa_avail; |
---|
48 | pa_avail += npages * PAGE_SIZE; |
---|
49 | return pa; |
---|
50 | } |
---|
51 | |
---|
52 | vaddr_t hal_gpt_bootstrap_valloc(size_t npages) |
---|
53 | { |
---|
54 | vaddr_t va = va_avail; |
---|
55 | va_avail += npages * PAGE_SIZE; |
---|
56 | return va; |
---|
57 | } |
---|
58 | |
---|
59 | /* |
---|
60 | * Reset the bootstrap VA we've used in cluster0 so far. After this |
---|
61 | * function, cluster0's heap is empty. |
---|
62 | */ |
---|
63 | void hal_gpt_bootstrap_reset() |
---|
64 | { |
---|
65 | /* |
---|
66 | * Re-enter cluster0's space, because we altered it when mapping the ACPI |
---|
67 | * tables. |
---|
68 | */ |
---|
69 | hal_gpt_enter_range(CLUSTER_MIN_VA(0), 0, CLUSTER_PA_SIZE / PAGE_SIZE); |
---|
70 | hal_gpt_leave_range(CLUSTER_MIN_VA(0), (KERNTEXTOFF - KERNBASE) / PAGE_SIZE); |
---|
71 | |
---|
72 | va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE; |
---|
73 | } |
---|
74 | |
---|
75 | /* |
---|
76 | * Uniformize the PA and VA offsets, and return the value. After this function, |
---|
77 | * we are guaranteed to have [VA = PA + constant_offset]. And therefore we can |
---|
78 | * only call hal_gpt_bootstrap_valloc, without entering it in a PA. |
---|
79 | */ |
---|
80 | size_t hal_gpt_bootstrap_uniformize() |
---|
81 | { |
---|
82 | size_t pa_offset = pa_avail - 0; |
---|
83 | size_t va_offset = va_avail - CLUSTER_MIN_VA(0); |
---|
84 | |
---|
85 | if (pa_offset < va_offset) |
---|
86 | pa_avail += (va_offset - pa_offset); |
---|
87 | else if (pa_offset > va_offset) |
---|
88 | va_avail += (pa_offset - va_offset); |
---|
89 | |
---|
90 | return MAX(pa_offset, va_offset); |
---|
91 | } |
---|
92 | |
---|
93 | void hal_gpt_enter(vaddr_t va, paddr_t pa, pt_entry_t flags) |
---|
94 | { |
---|
95 | XASSERT(va % PAGE_SIZE == 0); |
---|
96 | XASSERT(pa % PAGE_SIZE == 0); |
---|
97 | //XASSERT(va == tmpva || PTE_BASE[pl1_i(va)] == 0); |
---|
98 | PTE_BASE[pl1_i(va)] = (pa & PG_FRAME) | flags; |
---|
99 | invlpg(va); |
---|
100 | } |
---|
101 | |
---|
102 | void hal_gpt_enter_range(vaddr_t va, paddr_t pa, size_t n) |
---|
103 | { |
---|
104 | pt_entry_t flags = PG_V | PG_KW | PG_NX; |
---|
105 | size_t i; |
---|
106 | for (i = 0; i < n; i++) { |
---|
107 | hal_gpt_enter(va + i * PAGE_SIZE, pa + i * PAGE_SIZE, flags); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | void hal_gpt_leave(vaddr_t va) |
---|
112 | { |
---|
113 | XASSERT(va % PAGE_SIZE == 0); |
---|
114 | XASSERT(PTE_BASE[pl1_i(va)] != 0); |
---|
115 | PTE_BASE[pl1_i(va)] = 0; |
---|
116 | invlpg(va); |
---|
117 | } |
---|
118 | |
---|
119 | void hal_gpt_leave_range(vaddr_t va, size_t n) |
---|
120 | { |
---|
121 | size_t i; |
---|
122 | for (i = 0; i < n; i++) { |
---|
123 | hal_gpt_leave(va + i * PAGE_SIZE); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | /* |
---|
128 | * Create a page tree that can map va_start->va_end. The caller can then |
---|
129 | * enter these addresses to physical locations. |
---|
130 | * |
---|
131 | * This function is a bit complicated, and may need to be revisited. |
---|
132 | */ |
---|
133 | void hal_gpt_maptree_area(vaddr_t va_start, vaddr_t va_end) |
---|
134 | { |
---|
135 | pt_entry_t flags = PG_V | PG_KW | PG_NX; |
---|
136 | size_t L4start, L4end, nL4e; |
---|
137 | size_t L3start, L3end, nL3e; |
---|
138 | size_t L2start, L2end, nL2e; |
---|
139 | paddr_t L3page, L2page, L1page; |
---|
140 | paddr_t pa; |
---|
141 | size_t i, npa; |
---|
142 | pt_entry_t *pde; |
---|
143 | |
---|
144 | /* Allocate L3 */ |
---|
145 | L4start = pl4_i(va_start); |
---|
146 | L4end = pl4_i(va_end); |
---|
147 | nL4e = (L4end - L4start + 1); |
---|
148 | L3page = hal_gpt_bootstrap_palloc(nL4e); |
---|
149 | |
---|
150 | /* Allocate L2 */ |
---|
151 | L3start = pl3_i(va_start); |
---|
152 | L3end = pl3_i(va_end); |
---|
153 | nL3e = (L3end - L3start + 1); |
---|
154 | L2page = hal_gpt_bootstrap_palloc(nL3e); |
---|
155 | |
---|
156 | /* Allocate L1 */ |
---|
157 | L2start = pl2_i(va_start); |
---|
158 | L2end = pl2_i(va_end); |
---|
159 | nL2e = (L2end - L2start + 1); |
---|
160 | L1page = hal_gpt_bootstrap_palloc(nL2e); |
---|
161 | |
---|
162 | /* Zero out L1 */ |
---|
163 | for (i = 0; i < nL2e; i++) { |
---|
164 | pa = L1page + i * PAGE_SIZE; |
---|
165 | hal_gpt_enter(tmpva, pa, flags); |
---|
166 | |
---|
167 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
168 | } |
---|
169 | |
---|
170 | /* Zero out L2 */ |
---|
171 | for (i = 0; i < nL3e; i++) { |
---|
172 | pa = L2page + i * PAGE_SIZE; |
---|
173 | hal_gpt_enter(tmpva, pa, flags); |
---|
174 | |
---|
175 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
176 | } |
---|
177 | |
---|
178 | /* Zero out L3 */ |
---|
179 | for (i = 0; i < nL4e; i++) { |
---|
180 | pa = L3page + i * PAGE_SIZE; |
---|
181 | hal_gpt_enter(tmpva, pa, flags); |
---|
182 | |
---|
183 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
184 | } |
---|
185 | |
---|
186 | /* Create L2, linked to L1 */ |
---|
187 | npa = (L2start / NPDPG) * PAGE_SIZE; |
---|
188 | for (i = L2start; i <= L2end; i++) { |
---|
189 | pa = (paddr_t)&(((pt_entry_t *)L2page)[i]); |
---|
190 | pa -= npa; /* shift on the left */ |
---|
191 | pa &= PG_FRAME; /* rounddown to a page boundary */ |
---|
192 | hal_gpt_enter(tmpva, pa, flags); |
---|
193 | |
---|
194 | pde = (pt_entry_t *)tmpva; |
---|
195 | pa = L1page + (i - L2start) * PAGE_SIZE; |
---|
196 | pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
197 | } |
---|
198 | |
---|
199 | /* Create L3, linked to L2 */ |
---|
200 | npa = (L3start / NPDPG) * PAGE_SIZE; |
---|
201 | for (i = L3start; i <= L3end; i++) { |
---|
202 | pa = (paddr_t)&(((pt_entry_t *)L3page)[i]); |
---|
203 | pa -= npa; /* shift on the left */ |
---|
204 | pa &= PG_FRAME; /* rounddown to a page boundary */ |
---|
205 | hal_gpt_enter(tmpva, pa, flags); |
---|
206 | |
---|
207 | pde = (pt_entry_t *)tmpva; |
---|
208 | pa = L2page + (i - L3start) * PAGE_SIZE; |
---|
209 | pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
210 | } |
---|
211 | |
---|
212 | /* Link L3 into L4 */ |
---|
213 | for (i = 0; i < nL4e; i++) { |
---|
214 | pa = L3page + i * PAGE_SIZE; |
---|
215 | L4_BASE[L4start + i] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | void hal_gpt_init(paddr_t firstpa) |
---|
220 | { |
---|
221 | /* Initialize global values */ |
---|
222 | pa_avail = firstpa; |
---|
223 | va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE; |
---|
224 | kimg_size = ((uint64_t)&__kernel_end - KERNBASE); |
---|
225 | XASSERT(kimg_size % PAGE_SIZE == 0); |
---|
226 | |
---|
227 | /* |
---|
228 | * Create cluster0's page tree, enter the space, and unmap the area |
---|
229 | * below the kernel. |
---|
230 | */ |
---|
231 | hal_gpt_maptree_area(CLUSTER_MIN_VA(0), CLUSTER_MIN_VA(0) + CLUSTER_PA_SIZE); |
---|
232 | hal_gpt_enter_range(CLUSTER_MIN_VA(0), 0, CLUSTER_PA_SIZE / PAGE_SIZE); |
---|
233 | hal_gpt_leave_range(CLUSTER_MIN_VA(0), (KERNTEXTOFF - KERNBASE) / PAGE_SIZE); |
---|
234 | } |
---|
235 | |
---|
236 | /* -------------------------------------------------------------------------- */ |
---|
237 | |
---|
238 | error_t hal_gpt_create(gpt_t *gpt) |
---|
239 | { |
---|
240 | page_t *page; |
---|
241 | xptr_t page_xp; |
---|
242 | |
---|
243 | /* check page size */ |
---|
244 | if (CONFIG_PPM_PAGE_SIZE != 4096) { |
---|
245 | printk("\n[PANIC] in %s : For x86, the page must be 4 Kbytes\n", __FUNCTION__); |
---|
246 | hal_core_sleep(); |
---|
247 | } |
---|
248 | |
---|
249 | /* allocate a physical page for L4 */ |
---|
250 | kmem_req_t req; |
---|
251 | req.type = KMEM_PAGE; |
---|
252 | req.size = 1; |
---|
253 | req.flags = AF_KERNEL | AF_ZERO; |
---|
254 | page = (page_t *)kmem_alloc(&req); |
---|
255 | |
---|
256 | if (page == NULL) { |
---|
257 | printk("\n[ERROR] in %s : cannot allocate physical memory for PT1\n", __FUNCTION__); |
---|
258 | return ENOMEM; |
---|
259 | } |
---|
260 | |
---|
261 | /* |
---|
262 | * XXX XXX XXX: can kmem_alloc allocate the page in a remote cluster?? |
---|
263 | */ |
---|
264 | page_xp = XPTR(local_cxy, page); |
---|
265 | |
---|
266 | /* populate the kernel entries */ |
---|
267 | pt_entry_t *L4src, *L4dst; |
---|
268 | extern paddr_t L4paddr; // XXX XXX smp |
---|
269 | vaddr_t L4vaddr = L4paddr + KERNBASE; // XXX |
---|
270 | L4src = (pt_entry_t *)L4vaddr; |
---|
271 | L4dst = (pt_entry_t *)ppm_page2base(page_xp); |
---|
272 | memcpy(&L4dst[256], &L4src[256], 256 * sizeof(pt_entry_t)); |
---|
273 | L4dst[L4_SLOT_PTE] = (ppm_page2ppn(page_xp) << CONFIG_PPM_PAGE_SHIFT) | |
---|
274 | PG_V | PG_KW | PG_NX; |
---|
275 | |
---|
276 | /* initialize generic page table descriptor */ |
---|
277 | gpt->ptr = GET_PTR(ppm_page2base(page_xp)); |
---|
278 | gpt->ppn = ppm_page2ppn(page_xp); |
---|
279 | gpt->page = GET_PTR(page_xp); |
---|
280 | |
---|
281 | |
---|
282 | return 0; |
---|
283 | } |
---|
284 | |
---|
285 | void hal_gpt_destroy( gpt_t * gpt ) |
---|
286 | { |
---|
287 | x86_panic((char *)__func__); |
---|
288 | } |
---|
289 | |
---|
290 | void hal_gpt_print( gpt_t * gpt ) |
---|
291 | { |
---|
292 | x86_panic((char *)__func__); |
---|
293 | } |
---|
294 | |
---|
295 | error_t hal_gpt_set_pte( gpt_t * gpt, |
---|
296 | vpn_t vpn, |
---|
297 | ppn_t ppn, |
---|
298 | uint32_t attr ) |
---|
299 | { |
---|
300 | x86_panic((char *)__func__); |
---|
301 | return 0; |
---|
302 | } |
---|
303 | |
---|
304 | void hal_gpt_get_pte( gpt_t * gpt, |
---|
305 | vpn_t vpn, |
---|
306 | uint32_t * attr, |
---|
307 | ppn_t * ppn ) |
---|
308 | { |
---|
309 | x86_panic((char *)__func__); |
---|
310 | } |
---|
311 | |
---|
312 | void hal_gpt_reset_pte( gpt_t * gpt, |
---|
313 | vpn_t vpn ) |
---|
314 | { |
---|
315 | x86_panic((char *)__func__); |
---|
316 | } |
---|
317 | |
---|
318 | error_t hal_gpt_lock_pte( gpt_t * gpt, |
---|
319 | vpn_t vpn ) |
---|
320 | { |
---|
321 | x86_panic((char *)__func__); |
---|
322 | return 0; |
---|
323 | } |
---|
324 | |
---|
325 | error_t hal_gpt_unlock_pte( gpt_t * gpt, |
---|
326 | vpn_t vpn ) |
---|
327 | { |
---|
328 | x86_panic((char *)__func__); |
---|
329 | return 0; |
---|
330 | } |
---|
331 | |
---|
332 | error_t hal_gpt_copy( gpt_t * dst_gpt, |
---|
333 | gpt_t * src_gpt, |
---|
334 | bool_t cow ) |
---|
335 | { |
---|
336 | x86_panic((char *)__func__); |
---|
337 | return 0; |
---|
338 | } |
---|
339 | |
---|