source: mainline/kernel/genarch/src/mm/page_pt.c@ 6ac3d27

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6ac3d27 was f7bb6d1, checked in by Jakub Klama <jakub.klama@…>, 12 years ago

Merge from launchpad branch.

  • Property mode set to 100644
File size: 11.7 KB
RevLine 
[6d7ffa65]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[6d7ffa65]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[f47fd19]29/** @addtogroup genarchmm
[b45c443]30 * @{
31 */
32
[0f27b4c]33/**
[b45c443]34 * @file
[da1bafb]35 * @brief Virtual Address Translation for hierarchical 4-level page tables.
[0f27b4c]36 */
37
[6d7ffa65]38#include <genarch/mm/page_pt.h>
39#include <mm/page.h>
40#include <mm/frame.h>
[c72dc15]41#include <mm/km.h>
[ef67bab]42#include <mm/as.h>
[6d7ffa65]43#include <arch/mm/page.h>
[fc1e4f6]44#include <arch/mm/as.h>
[609a417]45#include <arch/barrier.h>
[d99c1d2]46#include <typedefs.h>
[6d7ffa65]47#include <arch/asm.h>
48#include <memstr.h>
[c868e2d]49#include <align.h>
50#include <macros.h>
[caed0279]51#include <bitops.h>
[6d7ffa65]52
[da1bafb]53static void pt_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
54static void pt_mapping_remove(as_t *, uintptr_t);
[235e6c7]55static pte_t *pt_mapping_find(as_t *, uintptr_t, bool);
[c868e2d]56static void pt_mapping_make_global(uintptr_t, size_t);
[6d7ffa65]57
[f5935ed]58page_mapping_operations_t pt_mapping_operations = {
[6d7ffa65]59 .mapping_insert = pt_mapping_insert,
[8f00329]60 .mapping_remove = pt_mapping_remove,
[c868e2d]61 .mapping_find = pt_mapping_find,
62 .mapping_make_global = pt_mapping_make_global
[6d7ffa65]63};
64
65/** Map page to frame using hierarchical page tables.
66 *
[9179d0a]67 * Map virtual address page to physical address frame
68 * using flags.
[6d7ffa65]69 *
[da1bafb]70 * @param as Address space to wich page belongs.
71 * @param page Virtual address of the page to be mapped.
[6d7ffa65]72 * @param frame Physical address of memory frame to which the mapping is done.
73 * @param flags Flags to be used for mapping.
[da1bafb]74 *
[6d7ffa65]75 */
[da1bafb]76void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
77 unsigned int flags)
[6d7ffa65]78{
[ef9a2a8]79 //printf("pt_mapping_insert: as=%p, page=0x%08x, frame=0x%08x\n", as, page, frame);
80
[da1bafb]81 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
[1d432f9]82
[ef9a2a8]83 //printf("ptl0 = %p\n", ptl0);
84
[1d432f9]85 ASSERT(page_table_locked(as));
[ef9a2a8]86
[6d7ffa65]87 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
[ef9a2a8]88 // printf("allocating ptl1\n");
89
[6b326ea1]90 pte_t *newpt = (pte_t *) frame_alloc(PTL1_SIZE,
91 FRAME_LOWMEM | FRAME_KA);
[ef9a2a8]92
93 // printf("newpt = %p, index = %d\n", newpt, PTL0_INDEX(page));
94
[e32e092]95 memsetb(newpt, FRAME_SIZE << PTL1_SIZE, 0);
[6d7ffa65]96 SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
[6b326ea1]97 SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page),
[609a417]98 PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
[6b326ea1]99 PAGE_WRITE);
[de73242]100 /*
101 * Make sure that a concurrent hardware page table walk or
102 * pt_mapping_find() will see the new PTL1 only after it is
103 * fully initialized.
104 */
[609a417]105 write_barrier();
106 SET_PTL1_PRESENT(ptl0, PTL0_INDEX(page));
[6d7ffa65]107 }
[da1bafb]108
109 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
110
[ef9a2a8]111// printf("ptl1 = %p\n", ptl1);
112
[6d7ffa65]113 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
[ef9a2a8]114// printf("allocating ptl2\n");
115
[6b326ea1]116 pte_t *newpt = (pte_t *) frame_alloc(PTL2_SIZE,
117 FRAME_LOWMEM | FRAME_KA);
[ef9a2a8]118
119// printf("newpt = %p, index = %d\n", newpt, PTL1_INDEX(page));
120
[e32e092]121 memsetb(newpt, FRAME_SIZE << PTL2_SIZE, 0);
[6d7ffa65]122 SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
[6b326ea1]123 SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page),
[609a417]124 PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
[6b326ea1]125 PAGE_WRITE);
[de73242]126 /*
127 * Make the new PTL2 visible only after it is fully initialized.
128 */
[609a417]129 write_barrier();
[e40b8066]130 SET_PTL2_PRESENT(ptl1, PTL1_INDEX(page));
[6d7ffa65]131 }
[da1bafb]132
133 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
134
[ef9a2a8]135// printf("ptl2 = %p\n", ptl2);
136
[6d7ffa65]137 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
[ef9a2a8]138// printf("allocating ptl3\n");
139
[6b326ea1]140 pte_t *newpt = (pte_t *) frame_alloc(PTL3_SIZE,
141 FRAME_LOWMEM | FRAME_KA);
[ef9a2a8]142
143// printf("newpt = %p, index = %d\n", newpt, PTL2_INDEX(page));
144
[e32e092]145 memsetb(newpt, FRAME_SIZE << PTL3_SIZE, 0);
[6d7ffa65]146 SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
[6b326ea1]147 SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page),
[609a417]148 PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
[6b326ea1]149 PAGE_WRITE);
[de73242]150 /*
151 * Make the new PTL3 visible only after it is fully initialized.
152 */
[609a417]153 write_barrier();
154 SET_PTL3_PRESENT(ptl2, PTL2_INDEX(page));
[6d7ffa65]155 }
[da1bafb]156
157 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
158
[ef9a2a8]159// printf("ptl3 = %p\n", ptl3);
160
[6d7ffa65]161 SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame);
[609a417]162 SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags | PAGE_NOT_PRESENT);
[de73242]163 /*
164 * Make the new mapping visible only after it is fully initialized.
165 */
[609a417]166 write_barrier();
167 SET_FRAME_PRESENT(ptl3, PTL3_INDEX(page));
[6d7ffa65]168}
169
[8f00329]170/** Remove mapping of page from hierarchical page tables.
171 *
[9179d0a]172 * Remove any mapping of page within address space as.
[8f00329]173 * TLB shootdown should follow in order to make effects of
174 * this call visible.
175 *
[ecbdc724]176 * Empty page tables except PTL0 are freed.
177 *
[da1bafb]178 * @param as Address space to wich page belongs.
[8f00329]179 * @param page Virtual address of the page to be demapped.
[da1bafb]180 *
[8f00329]181 */
[7f1c620]182void pt_mapping_remove(as_t *as, uintptr_t page)
[8f00329]183{
[1d432f9]184 ASSERT(page_table_locked(as));
185
[ecbdc724]186 /*
187 * First, remove the mapping, if it exists.
188 */
[da1bafb]189
190 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
[8f00329]191 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
192 return;
[da1bafb]193
194 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
[8f00329]195 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
196 return;
[da1bafb]197
198 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
[8f00329]199 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
200 return;
[da1bafb]201
202 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
203
[c868e2d]204 /*
205 * Destroy the mapping.
206 * Setting to PAGE_NOT_PRESENT is not sufficient.
[15187c3]207 * But we need SET_FRAME for possible PT coherence maintenance.
208 * At least on ARM.
[c868e2d]209 */
[15187c3]210 //TODO: Fix this inconsistency
211 SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), PAGE_NOT_PRESENT);
[e32e092]212 memsetb(&ptl3[PTL3_INDEX(page)], sizeof(pte_t), 0);
[da1bafb]213
[ecbdc724]214 /*
[c72dc15]215 * Second, free all empty tables along the way from PTL3 down to PTL0
216 * except those needed for sharing the kernel non-identity mappings.
[ecbdc724]217 */
218
[da1bafb]219 /* Check PTL3 */
220 bool empty = true;
221
222 unsigned int i;
[ecbdc724]223 for (i = 0; i < PTL3_ENTRIES; i++) {
224 if (PTE_VALID(&ptl3[i])) {
225 empty = false;
226 break;
227 }
228 }
[da1bafb]229
[ecbdc724]230 if (empty) {
231 /*
232 * PTL3 is empty.
[c72dc15]233 * Release the frame and remove PTL3 pointer from the parent
234 * table.
[ecbdc724]235 */
[da1bafb]236#if (PTL2_ENTRIES != 0)
237 memsetb(&ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
238#elif (PTL1_ENTRIES != 0)
239 memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
240#else
[c72dc15]241 if (km_is_non_identity(page))
242 return;
243
[da1bafb]244 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
245#endif
[c72dc15]246 frame_free(KA2PA((uintptr_t) ptl3));
[ecbdc724]247 } else {
248 /*
249 * PTL3 is not empty.
250 * Therefore, there must be a path from PTL0 to PTL3 and
251 * thus nothing to free in higher levels.
[da1bafb]252 *
[ecbdc724]253 */
254 return;
255 }
256
[da1bafb]257 /* Check PTL2, empty is still true */
258#if (PTL2_ENTRIES != 0)
259 for (i = 0; i < PTL2_ENTRIES; i++) {
260 if (PTE_VALID(&ptl2[i])) {
261 empty = false;
262 break;
[ecbdc724]263 }
264 }
[da1bafb]265
266 if (empty) {
267 /*
268 * PTL2 is empty.
[c72dc15]269 * Release the frame and remove PTL2 pointer from the parent
270 * table.
[da1bafb]271 */
272#if (PTL1_ENTRIES != 0)
273 memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
274#else
[c72dc15]275 if (km_is_non_identity(page))
276 return;
277
[da1bafb]278 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
279#endif
[c72dc15]280 frame_free(KA2PA((uintptr_t) ptl2));
[da1bafb]281 } else {
282 /*
283 * PTL2 is not empty.
284 * Therefore, there must be a path from PTL0 to PTL2 and
285 * thus nothing to free in higher levels.
286 *
287 */
288 return;
289 }
290#endif /* PTL2_ENTRIES != 0 */
291
[ecbdc724]292 /* check PTL1, empty is still true */
[da1bafb]293#if (PTL1_ENTRIES != 0)
294 for (i = 0; i < PTL1_ENTRIES; i++) {
295 if (PTE_VALID(&ptl1[i])) {
296 empty = false;
297 break;
[ecbdc724]298 }
299 }
[da1bafb]300
301 if (empty) {
302 /*
303 * PTL1 is empty.
[c72dc15]304 * Release the frame and remove PTL1 pointer from the parent
305 * table.
[da1bafb]306 */
[c72dc15]307 if (km_is_non_identity(page))
308 return;
309
[da1bafb]310 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
[c72dc15]311 frame_free(KA2PA((uintptr_t) ptl1));
[da1bafb]312 }
313#endif /* PTL1_ENTRIES != 0 */
[8f00329]314}
315
[6d7ffa65]316/** Find mapping for virtual page in hierarchical page tables.
317 *
[235e6c7]318 * @param as Address space to which page belongs.
319 * @param page Virtual page.
320 * @param nolock True if the page tables need not be locked.
[6d7ffa65]321 *
[da1bafb]322 * @return NULL if there is no such mapping; entry from PTL3 describing
323 * the mapping otherwise.
324 *
[6d7ffa65]325 */
[235e6c7]326pte_t *pt_mapping_find(as_t *as, uintptr_t page, bool nolock)
[6d7ffa65]327{
[235e6c7]328 ASSERT(nolock || page_table_locked(as));
[1d432f9]329
[da1bafb]330 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
[6d7ffa65]331 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
332 return NULL;
[e943ecf]333
334 read_barrier();
[da1bafb]335
336 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
[6d7ffa65]337 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
338 return NULL;
[e943ecf]339
340#if (PTL1_ENTRIES != 0)
[de73242]341 /*
342 * Always read ptl2 only after we are sure it is present.
343 */
[e943ecf]344 read_barrier();
345#endif
[da1bafb]346
347 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
[6d7ffa65]348 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
349 return NULL;
[e943ecf]350
351#if (PTL2_ENTRIES != 0)
[de73242]352 /*
353 * Always read ptl3 only after we are sure it is present.
354 */
[e943ecf]355 read_barrier();
356#endif
[da1bafb]357
358 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
359
[6d7ffa65]360 return &ptl3[PTL3_INDEX(page)];
361}
[b45c443]362
[caed0279]363/** Return the size of the region mapped by a single PTL0 entry.
364 *
365 * @return Size of the region mapped by a single PTL0 entry.
366 */
367static uintptr_t ptl0_step_get(void)
368{
369 size_t va_bits;
370
371 va_bits = fnzb(PTL0_ENTRIES) + fnzb(PTL1_ENTRIES) + fnzb(PTL2_ENTRIES) +
372 fnzb(PTL3_ENTRIES) + PAGE_WIDTH;
373
374 return 1UL << (va_bits - fnzb(PTL0_ENTRIES));
375}
376
[c868e2d]377/** Make the mappings in the given range global accross all address spaces.
378 *
379 * All PTL0 entries in the given range will be mapped to a next level page
380 * table. The next level page table will be allocated and cleared.
381 *
382 * pt_mapping_remove() will never deallocate these page tables even when there
383 * are no PTEs in them.
384 *
385 * @param as Address space.
386 * @param base Base address corresponding to the first PTL0 entry that will be
387 * altered by this function.
388 * @param size Size in bytes defining the range of PTL0 entries that will be
389 * altered by this function.
390 */
391void pt_mapping_make_global(uintptr_t base, size_t size)
392{
393 uintptr_t ptl0 = PA2KA((uintptr_t) AS_KERNEL->genarch.page_table);
[caed0279]394 uintptr_t ptl0_step = ptl0_step_get();
[c868e2d]395 size_t order;
396 uintptr_t addr;
397
398#if (PTL1_ENTRIES != 0)
399 order = PTL1_SIZE;
400#elif (PTL2_ENTRIES != 0)
401 order = PTL2_SIZE;
402#else
403 order = PTL3_SIZE;
404#endif
405
[a2789d2]406 ASSERT(size > 0);
[c868e2d]407
[caed0279]408 for (addr = ALIGN_DOWN(base, ptl0_step); addr - 1 < base + size - 1;
409 addr += ptl0_step) {
[c868e2d]410 uintptr_t l1;
411
412 l1 = (uintptr_t) frame_alloc(order, FRAME_KA | FRAME_LOWMEM);
413 memsetb((void *) l1, FRAME_SIZE << order, 0);
414 SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(addr), KA2PA(l1));
415 SET_PTL1_FLAGS(ptl0, PTL0_INDEX(addr),
[34ab31c0]416 PAGE_PRESENT | PAGE_USER | PAGE_CACHEABLE |
417 PAGE_EXEC | PAGE_WRITE | PAGE_READ);
[c868e2d]418 }
419}
420
[f47fd19]421/** @}
[b45c443]422 */
Note: See TracBrowser for help on using the repository browser.