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

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

complete hal_ppm_init, to take into account the reserved areas

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