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

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

Create the core/ sub-directory for x86_64.

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