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

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

Use constant flags.

File size: 8.3 KB
RevLine 
[25]1/*
[35]2 * hal_gpt.c - implementation of the Generic Page Table API for x86_64
[25]3 *
[35]4 * Copyright (c) 2017 Maxime Villard
[25]5 *
6 * This file is part of ALMOS-MKH.
7 *
[35]8 * ALMOS-MKH is free software; you can redistribute it and/or modify it
[25]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 *
[35]12 * ALMOS-MKH is distributed in the hope that it will be useful, but
[25]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
[234]18 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
[25]19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <hal_types.h>
[35]23#include <hal_boot.h> /* XXX */
[25]24#include <hal_gpt.h>
25#include <hal_special.h>
[35]26#include <hal_internal.h>
27
[25]28#include <printk.h>
29#include <bits.h>
[35]30#include <string.h>
[25]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
[44]38extern vaddr_t __kernel_end;
39size_t kimg_size __in_kdata = 0;
40
[35]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
[45]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{
[148]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);
[319]70        hal_gpt_leave_range(CLUSTER_MIN_VA(0), (KERNTEXTOFF - KERNBASE) / PAGE_SIZE);
[148]71
[116]72        va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE;
[45]73}
74
[116]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
[83]93void hal_gpt_enter(vaddr_t va, paddr_t pa, pt_entry_t flags)
[35]94{
[45]95        XASSERT(va % PAGE_SIZE == 0);
96        XASSERT(pa % PAGE_SIZE == 0);
[116]97        //XASSERT(va == tmpva || PTE_BASE[pl1_i(va)] == 0);
[83]98        PTE_BASE[pl1_i(va)] = (pa & PG_FRAME) | flags;
[79]99        invlpg(va);
[35]100}
101
[39]102void hal_gpt_enter_range(vaddr_t va, paddr_t pa, size_t n)
103{
[83]104        pt_entry_t flags = PG_V | PG_KW | PG_NX;
[39]105        size_t i;
106        for (i = 0; i < n; i++) {
[83]107                hal_gpt_enter(va + i * PAGE_SIZE, pa + i * PAGE_SIZE, flags);
[39]108        }
109}
110
[45]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;
[79]116        invlpg(va);
[45]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
[35]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 *
[148]131 * This function is a bit complicated, and may need to be revisited.
[35]132 */
133void hal_gpt_maptree_area(vaddr_t va_start, vaddr_t va_end)
134{
[112]135        pt_entry_t flags = PG_V | PG_KW | PG_NX;
[35]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;
[83]165                hal_gpt_enter(tmpva, pa, flags);
[35]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;
[83]173                hal_gpt_enter(tmpva, pa, flags);
[35]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;
[83]181                hal_gpt_enter(tmpva, pa, flags);
[35]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 */
[83]192                hal_gpt_enter(tmpva, pa, flags);
[35]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 */
[83]205                hal_gpt_enter(tmpva, pa, flags);
[35]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{
[44]221        /* Initialize global values */
[35]222        pa_avail = firstpa;
[47]223        va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE;
[44]224        kimg_size = ((uint64_t)&__kernel_end - KERNBASE);
225        XASSERT(kimg_size % PAGE_SIZE == 0);
226
[224]227        /*
228         * Create cluster0's page tree, enter the space, and unmap the area
229         * below the kernel.
230         */
[148]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);
[224]233        hal_gpt_leave_range(CLUSTER_MIN_VA(0), (KERNTEXTOFF - KERNBASE) / PAGE_SIZE);
[35]234}
235
236/* -------------------------------------------------------------------------- */
237
[225]238error_t hal_gpt_create(gpt_t *gpt)
[25]239{
[225]240        page_t *page;
[316]241        xptr_t page_xp;
[225]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
[316]261        /*
262         * XXX XXX XXX: can kmem_alloc allocate the page in a remote cluster??
263         */
264        page_xp = XPTR(local_cxy, page);
265
[225]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;
[316]271        L4dst = (pt_entry_t *)ppm_page2base(page_xp);
[225]272        memcpy(&L4dst[256], &L4src[256], 256 * sizeof(pt_entry_t));
[368]273        L4dst[L4_SLOT_PTE] = (ppm_page2ppn(page_xp) << CONFIG_PPM_PAGE_SHIFT) |
[225]274            PG_V | PG_KW | PG_NX;
275
276        /* initialize generic page table descriptor */
[316]277        gpt->ptr  = GET_PTR(ppm_page2base(page_xp));
278        gpt->ppn  = ppm_page2ppn(page_xp);
279        gpt->page = GET_PTR(page_xp);
[225]280
281
[25]282        return 0;
283}
284
285void hal_gpt_destroy( gpt_t * gpt )
286{
[48]287        x86_panic((char *)__func__);
[25]288}
289
290void hal_gpt_print( gpt_t * gpt )
291{
[48]292        x86_panic((char *)__func__);
[25]293}
294
295error_t hal_gpt_set_pte( gpt_t   * gpt,
296                         vpn_t     vpn,
297                         ppn_t     ppn,
298                         uint32_t  attr )
299{
[48]300        x86_panic((char *)__func__);
[25]301        return 0;
302}
303
304void hal_gpt_get_pte( gpt_t    * gpt,
305                      vpn_t      vpn,
306                      uint32_t * attr,
307                      ppn_t    * ppn )
308{
[48]309        x86_panic((char *)__func__);
[25]310}
311
312void hal_gpt_reset_pte( gpt_t * gpt,
313                        vpn_t   vpn )
314{
[48]315        x86_panic((char *)__func__);
[25]316}
317
318error_t hal_gpt_lock_pte( gpt_t * gpt,
319                          vpn_t   vpn )
320{
[48]321        x86_panic((char *)__func__);
[25]322        return 0;
323}
324
325error_t hal_gpt_unlock_pte( gpt_t * gpt,
326                            vpn_t   vpn )
327{
[48]328        x86_panic((char *)__func__);
[25]329        return 0;
330}
331
332error_t hal_gpt_copy( gpt_t  * dst_gpt,
333                      gpt_t  * src_gpt,
334                      bool_t   cow )
335{
[48]336        x86_panic((char *)__func__);
[25]337    return 0;
338}
339
Note: See TracBrowser for help on using the repository browser.