source: trunk/hal/x86_64/core/hal_gpt.c @ 368

Last change on this file since 368 was 368, checked in by max@…, 7 years ago

Switch the VM space, and explicitly disable the FPU.

File size: 9.3 KB
Line 
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
38extern vaddr_t __kernel_end;
39size_t kimg_size __in_kdata = 0;
40
41paddr_t pa_avail __in_kdata = 0;
42vaddr_t va_avail __in_kdata = 0;
43vaddr_t tmpva __in_kdata = (KERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2);
44
45paddr_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
52vaddr_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 */
63void 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 */
80size_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
93void 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
102void 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
111void 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
119void 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 */
133void 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
219void 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/****************************************************************************************
239 * These global variables defines the masks for the Generic Page Table Entry attributes,
240 * and must be defined in all GPT implementation.
241 ***************************************************************************************/
242
243uint32_t GPT_MAPPED;
244uint32_t GPT_SMALL;
245uint32_t GPT_READABLE;
246uint32_t GPT_WRITABLE;
247uint32_t GPT_EXECUTABLE;
248uint32_t GPT_CACHABLE;
249uint32_t GPT_USER;
250uint32_t GPT_DIRTY;
251uint32_t GPT_ACCESSED;
252uint32_t GPT_GLOBAL;
253uint32_t GPT_COW;
254uint32_t GPT_SWAP;
255uint32_t GPT_LOCKED;
256
257error_t hal_gpt_create(gpt_t *gpt)
258{
259        page_t *page;
260        xptr_t page_xp;
261
262        /* check page size */
263        if (CONFIG_PPM_PAGE_SIZE != 4096) {
264                printk("\n[PANIC] in %s : For x86, the page must be 4 Kbytes\n", __FUNCTION__);
265                hal_core_sleep();
266        }
267
268        /* allocate a physical page for L4 */
269        kmem_req_t req;
270        req.type  = KMEM_PAGE;
271        req.size  = 1;
272        req.flags = AF_KERNEL | AF_ZERO;
273        page = (page_t *)kmem_alloc(&req);
274
275        if (page == NULL) {
276                printk("\n[ERROR] in %s : cannot allocate physical memory for PT1\n", __FUNCTION__);
277                return ENOMEM;
278        }
279
280        /*
281         * XXX XXX XXX: can kmem_alloc allocate the page in a remote cluster??
282         */
283        page_xp = XPTR(local_cxy, page);
284
285        /* populate the kernel entries */
286        pt_entry_t *L4src, *L4dst;
287        extern paddr_t L4paddr; // XXX XXX smp
288        vaddr_t L4vaddr = L4paddr + KERNBASE; // XXX
289        L4src = (pt_entry_t *)L4vaddr;
290        L4dst = (pt_entry_t *)ppm_page2base(page_xp);
291        memcpy(&L4dst[256], &L4src[256], 256 * sizeof(pt_entry_t));
292        L4dst[L4_SLOT_PTE] = (ppm_page2ppn(page_xp) << CONFIG_PPM_PAGE_SHIFT) |
293            PG_V | PG_KW | PG_NX;
294
295        /* initialize generic page table descriptor */
296        gpt->ptr  = GET_PTR(ppm_page2base(page_xp));
297        gpt->ppn  = ppm_page2ppn(page_xp);
298        gpt->page = GET_PTR(page_xp);
299
300        /* initialize PTE entries attributes masks */
301        GPT_MAPPED     = PG_V;
302        GPT_SMALL      = 0;
303        GPT_READABLE   = PG_V;
304        GPT_WRITABLE   = PG_RW;
305        GPT_EXECUTABLE = 0;
306        GPT_CACHABLE   = 0;
307        GPT_USER       = PG_u;
308        GPT_DIRTY      = 0;
309        GPT_ACCESSED   = 0;
310        GPT_GLOBAL     = PG_G;
311        GPT_COW        = 0;
312        GPT_SWAP       = 0;
313        GPT_LOCKED     = 0;
314
315        return 0;
316}
317
318void hal_gpt_destroy( gpt_t * gpt )
319{
320        x86_panic((char *)__func__);
321}
322
323void hal_gpt_print( gpt_t * gpt )
324{
325        x86_panic((char *)__func__);
326}
327
328error_t hal_gpt_set_pte( gpt_t   * gpt,
329                         vpn_t     vpn,
330                         ppn_t     ppn,
331                         uint32_t  attr )
332{
333        x86_panic((char *)__func__);
334        return 0;
335}
336
337void hal_gpt_get_pte( gpt_t    * gpt,
338                      vpn_t      vpn,
339                      uint32_t * attr,
340                      ppn_t    * ppn )
341{
342        x86_panic((char *)__func__);
343}
344
345void hal_gpt_reset_pte( gpt_t * gpt,
346                        vpn_t   vpn )
347{
348        x86_panic((char *)__func__);
349}
350
351error_t hal_gpt_lock_pte( gpt_t * gpt,
352                          vpn_t   vpn )
353{
354        x86_panic((char *)__func__);
355        return 0;
356}
357
358error_t hal_gpt_unlock_pte( gpt_t * gpt,
359                            vpn_t   vpn )
360{
361        x86_panic((char *)__func__);
362        return 0;
363}
364
365error_t hal_gpt_copy( gpt_t  * dst_gpt,
366                      gpt_t  * src_gpt,
367                      bool_t   cow )
368{
369        x86_panic((char *)__func__);
370    return 0;
371}
372
Note: See TracBrowser for help on using the repository browser.