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

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

fix a pretty dumb bug; we must not unmap since we use the area for
cluster0's virtual space

File size: 7.9 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
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>
[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);
70
[116]71        va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE;
[45]72}
73
[116]74/*
75 * Uniformize the PA and VA offsets, and return the value. After this function,
76 * we are guaranteed to have [VA = PA + constant_offset]. And therefore we can
77 * only call hal_gpt_bootstrap_valloc, without entering it in a PA.
78 */
79size_t hal_gpt_bootstrap_uniformize()
80{
81        size_t pa_offset = pa_avail - 0;
82        size_t va_offset = va_avail - CLUSTER_MIN_VA(0);
83
84        if (pa_offset < va_offset)
85                pa_avail += (va_offset - pa_offset);
86        else if (pa_offset > va_offset)
87                va_avail += (pa_offset - va_offset);
88
89        return MAX(pa_offset, va_offset);
90}
91
[83]92void hal_gpt_enter(vaddr_t va, paddr_t pa, pt_entry_t flags)
[35]93{
[45]94        XASSERT(va % PAGE_SIZE == 0);
95        XASSERT(pa % PAGE_SIZE == 0);
[116]96        //XASSERT(va == tmpva || PTE_BASE[pl1_i(va)] == 0);
[83]97        PTE_BASE[pl1_i(va)] = (pa & PG_FRAME) | flags;
[79]98        invlpg(va);
[35]99}
100
[39]101void hal_gpt_enter_range(vaddr_t va, paddr_t pa, size_t n)
102{
[83]103        pt_entry_t flags = PG_V | PG_KW | PG_NX;
[39]104        size_t i;
105        for (i = 0; i < n; i++) {
[83]106                hal_gpt_enter(va + i * PAGE_SIZE, pa + i * PAGE_SIZE, flags);
[39]107        }
108}
109
[45]110void hal_gpt_leave(vaddr_t va)
111{
112        XASSERT(va % PAGE_SIZE == 0);
113        XASSERT(PTE_BASE[pl1_i(va)] != 0);
114        PTE_BASE[pl1_i(va)] = 0;
[79]115        invlpg(va);
[45]116}
117
118void hal_gpt_leave_range(vaddr_t va, size_t n)
119{
120        size_t i;
121        for (i = 0; i < n; i++) {
122                hal_gpt_leave(va + i * PAGE_SIZE);
123        }
124}
125
[35]126/*
127 * Create a page tree that can map va_start->va_end. The caller can then
128 * enter these addresses to physical locations.
129 *
[148]130 * This function is a bit complicated, and may need to be revisited.
[35]131 */
132void hal_gpt_maptree_area(vaddr_t va_start, vaddr_t va_end)
133{
[112]134        pt_entry_t flags = PG_V | PG_KW | PG_NX;
[35]135        size_t L4start, L4end, nL4e;
136        size_t L3start, L3end, nL3e;
137        size_t L2start, L2end, nL2e;
138        paddr_t L3page, L2page, L1page;
139        paddr_t pa;
140        size_t i, npa;
141        pt_entry_t *pde;
142
143        /* Allocate L3 */
144        L4start = pl4_i(va_start);
145        L4end = pl4_i(va_end);
146        nL4e = (L4end - L4start + 1);
147        L3page = hal_gpt_bootstrap_palloc(nL4e);
148
149        /* Allocate L2 */
150        L3start = pl3_i(va_start);
151        L3end = pl3_i(va_end);
152        nL3e = (L3end - L3start + 1);
153        L2page = hal_gpt_bootstrap_palloc(nL3e);
154
155        /* Allocate L1 */
156        L2start = pl2_i(va_start);
157        L2end = pl2_i(va_end);
158        nL2e = (L2end - L2start + 1);
159        L1page = hal_gpt_bootstrap_palloc(nL2e);
160
161        /* Zero out L1 */
162        for (i = 0; i < nL2e; i++) {
163                pa = L1page + i * PAGE_SIZE;
[83]164                hal_gpt_enter(tmpva, pa, flags);
[35]165
166                memset((void *)tmpva, 0, PAGE_SIZE);
167        }
168
169        /* Zero out L2 */
170        for (i = 0; i < nL3e; i++) {
171                pa = L2page + i * PAGE_SIZE;
[83]172                hal_gpt_enter(tmpva, pa, flags);
[35]173
174                memset((void *)tmpva, 0, PAGE_SIZE);
175        }
176
177        /* Zero out L3 */
178        for (i = 0; i < nL4e; i++) {
179                pa = L3page + i * PAGE_SIZE;
[83]180                hal_gpt_enter(tmpva, pa, flags);
[35]181
182                memset((void *)tmpva, 0, PAGE_SIZE);
183        }
184
185        /* Create L2, linked to L1 */
186        npa = (L2start / NPDPG) * PAGE_SIZE;
187        for (i = L2start; i <= L2end; i++) {
188                pa = (paddr_t)&(((pt_entry_t *)L2page)[i]);
189                pa -= npa;      /* shift on the left */
190                pa &= PG_FRAME; /* rounddown to a page boundary */
[83]191                hal_gpt_enter(tmpva, pa, flags);
[35]192
193                pde = (pt_entry_t *)tmpva;
194                pa = L1page + (i - L2start) * PAGE_SIZE;
195                pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW;
196        }
197
198        /* Create L3, linked to L2 */
199        npa = (L3start / NPDPG) * PAGE_SIZE;
200        for (i = L3start; i <= L3end; i++) {
201                pa = (paddr_t)&(((pt_entry_t *)L3page)[i]);
202                pa -= npa;      /* shift on the left */
203                pa &= PG_FRAME; /* rounddown to a page boundary */
[83]204                hal_gpt_enter(tmpva, pa, flags);
[35]205
206                pde = (pt_entry_t *)tmpva;
207                pa = L2page + (i - L3start) * PAGE_SIZE;
208                pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW;
209        }
210
211        /* Link L3 into L4 */
212        for (i = 0; i < nL4e; i++) {
213                pa = L3page + i * PAGE_SIZE;
214                L4_BASE[L4start + i] = (pa & PG_FRAME) | PG_V | PG_KW;
215        }
216}
217
218void hal_gpt_init(paddr_t firstpa)
219{
[112]220        paddr_t kimg_min_pa, kimg_max_pa;
[44]221
222        /* Initialize global values */
[35]223        pa_avail = firstpa;
[47]224        va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE;
[44]225        kimg_size = ((uint64_t)&__kernel_end - KERNBASE);
226        XASSERT(kimg_size % PAGE_SIZE == 0);
227
[112]228        kimg_min_pa = 0;
229        kimg_max_pa = kimg_min_pa + kimg_size;
230
[134]231        /* Create cluster0's page tree */
[148]232        hal_gpt_maptree_area(CLUSTER_MIN_VA(0), CLUSTER_MIN_VA(0) + CLUSTER_PA_SIZE);
[44]233
[148]234        /* Enter cluster0's space */
235        hal_gpt_enter_range(CLUSTER_MIN_VA(0), 0, CLUSTER_PA_SIZE / PAGE_SIZE);
[112]236
[123]237        /* Unmap the area below the kernel */
238        hal_gpt_leave_range(CLUSTER_MIN_VA(0), (KERNTEXTOFF - KERNBASE) / PAGE_SIZE);
[35]239}
240
241/* -------------------------------------------------------------------------- */
242
[25]243/****************************************************************************************
244 * These global variables defines the masks for the Generic Page Table Entry attributes,
245 * and must be defined in all GPT implementation.
246 ***************************************************************************************/
247
248uint32_t  GPT_MAPPED;
249uint32_t  GPT_SMALL;
250uint32_t  GPT_READABLE;
[145]251uint32_t  GPT_WRITABLE;
[25]252uint32_t  GPT_EXECUTABLE;
[145]253uint32_t  GPT_CACHABLE;
254uint32_t  GPT_USER;
[25]255uint32_t  GPT_DIRTY;
256uint32_t  GPT_ACCESSED;
257uint32_t  GPT_GLOBAL;
258uint32_t  GPT_COW;
259uint32_t  GPT_SWAP;
260uint32_t  GPT_LOCKED;
261
262error_t hal_gpt_create( gpt_t * gpt )
263{
[48]264        x86_panic((char *)__func__);
[25]265        return 0;
266}
267
268void hal_gpt_destroy( gpt_t * gpt )
269{
[48]270        x86_panic((char *)__func__);
[25]271}
272
273void hal_gpt_print( gpt_t * gpt )
274{
[48]275        x86_panic((char *)__func__);
[25]276}
277
278error_t hal_gpt_set_pte( gpt_t   * gpt,
279                         vpn_t     vpn,
280                         ppn_t     ppn,
281                         uint32_t  attr )
282{
[48]283        x86_panic((char *)__func__);
[25]284        return 0;
285}
286
287void hal_gpt_get_pte( gpt_t    * gpt,
288                      vpn_t      vpn,
289                      uint32_t * attr,
290                      ppn_t    * ppn )
291{
[48]292        x86_panic((char *)__func__);
[25]293}
294
295void hal_gpt_reset_pte( gpt_t * gpt,
296                        vpn_t   vpn )
297{
[48]298        x86_panic((char *)__func__);
[25]299}
300
301error_t hal_gpt_lock_pte( gpt_t * gpt,
302                          vpn_t   vpn )
303{
[48]304        x86_panic((char *)__func__);
[25]305        return 0;
306}
307
308error_t hal_gpt_unlock_pte( gpt_t * gpt,
309                            vpn_t   vpn )
310{
[48]311        x86_panic((char *)__func__);
[25]312        return 0;
313}
314
315error_t hal_gpt_copy( gpt_t  * dst_gpt,
316                      gpt_t  * src_gpt,
317                      bool_t   cow )
318{
[48]319        x86_panic((char *)__func__);
[25]320    return 0;
321}
322
Note: See TracBrowser for help on using the repository browser.