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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d5f774f6 was f18d01b6, checked in by Martin Decky <martin@…>, 12 years ago

allocate frames for the page tables on naturally aligned addresses

  • Property mode set to 100644
File size: 11.2 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{
[da1bafb]79 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
[1d432f9]80
81 ASSERT(page_table_locked(as));
[da1bafb]82
[6d7ffa65]83 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
[b0c2075]84 pte_t *newpt = (pte_t *)
[f18d01b6]85 PA2KA(frame_alloc(PTL1_FRAMES, FRAME_LOWMEM, PTL1_SIZE - 1));
86 memsetb(newpt, PTL1_SIZE, 0);
[6d7ffa65]87 SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
[6b326ea1]88 SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page),
[609a417]89 PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
[6b326ea1]90 PAGE_WRITE);
[de73242]91 /*
92 * Make sure that a concurrent hardware page table walk or
93 * pt_mapping_find() will see the new PTL1 only after it is
94 * fully initialized.
95 */
[609a417]96 write_barrier();
97 SET_PTL1_PRESENT(ptl0, PTL0_INDEX(page));
[6d7ffa65]98 }
[da1bafb]99
100 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
101
[6d7ffa65]102 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
[b0c2075]103 pte_t *newpt = (pte_t *)
[f18d01b6]104 PA2KA(frame_alloc(PTL2_FRAMES, FRAME_LOWMEM, PTL2_SIZE - 1));
105 memsetb(newpt, PTL2_SIZE, 0);
[6d7ffa65]106 SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
[6b326ea1]107 SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page),
[609a417]108 PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
[6b326ea1]109 PAGE_WRITE);
[de73242]110 /*
111 * Make the new PTL2 visible only after it is fully initialized.
112 */
[609a417]113 write_barrier();
[e40b8066]114 SET_PTL2_PRESENT(ptl1, PTL1_INDEX(page));
[6d7ffa65]115 }
[da1bafb]116
117 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
118
[6d7ffa65]119 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
[b0c2075]120 pte_t *newpt = (pte_t *)
[f18d01b6]121 PA2KA(frame_alloc(PTL3_FRAMES, FRAME_LOWMEM, PTL2_SIZE - 1));
122 memsetb(newpt, PTL2_SIZE, 0);
[6d7ffa65]123 SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
[6b326ea1]124 SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page),
[609a417]125 PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
[6b326ea1]126 PAGE_WRITE);
[de73242]127 /*
128 * Make the new PTL3 visible only after it is fully initialized.
129 */
[609a417]130 write_barrier();
131 SET_PTL3_PRESENT(ptl2, PTL2_INDEX(page));
[6d7ffa65]132 }
[da1bafb]133
134 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
135
[6d7ffa65]136 SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame);
[609a417]137 SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags | PAGE_NOT_PRESENT);
[de73242]138 /*
139 * Make the new mapping visible only after it is fully initialized.
140 */
[609a417]141 write_barrier();
142 SET_FRAME_PRESENT(ptl3, PTL3_INDEX(page));
[6d7ffa65]143}
144
[8f00329]145/** Remove mapping of page from hierarchical page tables.
146 *
[9179d0a]147 * Remove any mapping of page within address space as.
[8f00329]148 * TLB shootdown should follow in order to make effects of
149 * this call visible.
150 *
[ecbdc724]151 * Empty page tables except PTL0 are freed.
152 *
[da1bafb]153 * @param as Address space to wich page belongs.
[8f00329]154 * @param page Virtual address of the page to be demapped.
[da1bafb]155 *
[8f00329]156 */
[7f1c620]157void pt_mapping_remove(as_t *as, uintptr_t page)
[8f00329]158{
[1d432f9]159 ASSERT(page_table_locked(as));
160
[ecbdc724]161 /*
162 * First, remove the mapping, if it exists.
163 */
[da1bafb]164
165 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
[8f00329]166 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
167 return;
[da1bafb]168
169 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
[8f00329]170 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
171 return;
[da1bafb]172
173 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
[8f00329]174 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
175 return;
[da1bafb]176
177 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
178
[c868e2d]179 /*
180 * Destroy the mapping.
181 * Setting to PAGE_NOT_PRESENT is not sufficient.
[15187c3]182 * But we need SET_FRAME for possible PT coherence maintenance.
183 * At least on ARM.
[c868e2d]184 */
[15187c3]185 //TODO: Fix this inconsistency
186 SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), PAGE_NOT_PRESENT);
[e32e092]187 memsetb(&ptl3[PTL3_INDEX(page)], sizeof(pte_t), 0);
[da1bafb]188
[ecbdc724]189 /*
[c72dc15]190 * Second, free all empty tables along the way from PTL3 down to PTL0
191 * except those needed for sharing the kernel non-identity mappings.
[ecbdc724]192 */
193
[da1bafb]194 /* Check PTL3 */
195 bool empty = true;
196
197 unsigned int i;
[ecbdc724]198 for (i = 0; i < PTL3_ENTRIES; i++) {
199 if (PTE_VALID(&ptl3[i])) {
200 empty = false;
201 break;
202 }
203 }
[da1bafb]204
[ecbdc724]205 if (empty) {
206 /*
207 * PTL3 is empty.
[c72dc15]208 * Release the frame and remove PTL3 pointer from the parent
209 * table.
[ecbdc724]210 */
[da1bafb]211#if (PTL2_ENTRIES != 0)
212 memsetb(&ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
213#elif (PTL1_ENTRIES != 0)
214 memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
215#else
[c72dc15]216 if (km_is_non_identity(page))
217 return;
218
[da1bafb]219 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
220#endif
[5df1963]221 frame_free(KA2PA((uintptr_t) ptl3), PTL3_FRAMES);
[ecbdc724]222 } else {
223 /*
224 * PTL3 is not empty.
225 * Therefore, there must be a path from PTL0 to PTL3 and
226 * thus nothing to free in higher levels.
[da1bafb]227 *
[ecbdc724]228 */
229 return;
230 }
231
[da1bafb]232 /* Check PTL2, empty is still true */
233#if (PTL2_ENTRIES != 0)
234 for (i = 0; i < PTL2_ENTRIES; i++) {
235 if (PTE_VALID(&ptl2[i])) {
236 empty = false;
237 break;
[ecbdc724]238 }
239 }
[da1bafb]240
241 if (empty) {
242 /*
243 * PTL2 is empty.
[c72dc15]244 * Release the frame and remove PTL2 pointer from the parent
245 * table.
[da1bafb]246 */
247#if (PTL1_ENTRIES != 0)
248 memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
249#else
[c72dc15]250 if (km_is_non_identity(page))
251 return;
252
[da1bafb]253 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
254#endif
[5df1963]255 frame_free(KA2PA((uintptr_t) ptl2), PTL2_FRAMES);
[da1bafb]256 } else {
257 /*
258 * PTL2 is not empty.
259 * Therefore, there must be a path from PTL0 to PTL2 and
260 * thus nothing to free in higher levels.
261 *
262 */
263 return;
264 }
265#endif /* PTL2_ENTRIES != 0 */
266
[ecbdc724]267 /* check PTL1, empty is still true */
[da1bafb]268#if (PTL1_ENTRIES != 0)
269 for (i = 0; i < PTL1_ENTRIES; i++) {
270 if (PTE_VALID(&ptl1[i])) {
271 empty = false;
272 break;
[ecbdc724]273 }
274 }
[da1bafb]275
276 if (empty) {
277 /*
278 * PTL1 is empty.
[c72dc15]279 * Release the frame and remove PTL1 pointer from the parent
280 * table.
[da1bafb]281 */
[c72dc15]282 if (km_is_non_identity(page))
283 return;
284
[da1bafb]285 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
[5df1963]286 frame_free(KA2PA((uintptr_t) ptl1), PTL1_FRAMES);
[da1bafb]287 }
288#endif /* PTL1_ENTRIES != 0 */
[8f00329]289}
290
[6d7ffa65]291/** Find mapping for virtual page in hierarchical page tables.
292 *
[235e6c7]293 * @param as Address space to which page belongs.
294 * @param page Virtual page.
295 * @param nolock True if the page tables need not be locked.
[6d7ffa65]296 *
[da1bafb]297 * @return NULL if there is no such mapping; entry from PTL3 describing
298 * the mapping otherwise.
299 *
[6d7ffa65]300 */
[235e6c7]301pte_t *pt_mapping_find(as_t *as, uintptr_t page, bool nolock)
[6d7ffa65]302{
[235e6c7]303 ASSERT(nolock || page_table_locked(as));
[1d432f9]304
[da1bafb]305 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
[6d7ffa65]306 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
307 return NULL;
[e943ecf]308
309 read_barrier();
[da1bafb]310
311 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
[6d7ffa65]312 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
313 return NULL;
[e943ecf]314
315#if (PTL1_ENTRIES != 0)
[de73242]316 /*
317 * Always read ptl2 only after we are sure it is present.
318 */
[e943ecf]319 read_barrier();
320#endif
[da1bafb]321
322 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
[6d7ffa65]323 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
324 return NULL;
[e943ecf]325
326#if (PTL2_ENTRIES != 0)
[de73242]327 /*
328 * Always read ptl3 only after we are sure it is present.
329 */
[e943ecf]330 read_barrier();
331#endif
[da1bafb]332
333 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
334
[6d7ffa65]335 return &ptl3[PTL3_INDEX(page)];
336}
[b45c443]337
[caed0279]338/** Return the size of the region mapped by a single PTL0 entry.
339 *
340 * @return Size of the region mapped by a single PTL0 entry.
341 */
342static uintptr_t ptl0_step_get(void)
343{
344 size_t va_bits;
345
346 va_bits = fnzb(PTL0_ENTRIES) + fnzb(PTL1_ENTRIES) + fnzb(PTL2_ENTRIES) +
347 fnzb(PTL3_ENTRIES) + PAGE_WIDTH;
348
349 return 1UL << (va_bits - fnzb(PTL0_ENTRIES));
350}
351
[c868e2d]352/** Make the mappings in the given range global accross all address spaces.
353 *
354 * All PTL0 entries in the given range will be mapped to a next level page
355 * table. The next level page table will be allocated and cleared.
356 *
357 * pt_mapping_remove() will never deallocate these page tables even when there
358 * are no PTEs in them.
359 *
360 * @param as Address space.
361 * @param base Base address corresponding to the first PTL0 entry that will be
362 * altered by this function.
363 * @param size Size in bytes defining the range of PTL0 entries that will be
364 * altered by this function.
[e2a0d76]365 *
[c868e2d]366 */
367void pt_mapping_make_global(uintptr_t base, size_t size)
368{
[e2a0d76]369 ASSERT(size > 0);
370
[c868e2d]371 uintptr_t ptl0 = PA2KA((uintptr_t) AS_KERNEL->genarch.page_table);
[caed0279]372 uintptr_t ptl0_step = ptl0_step_get();
[b0c2075]373 size_t frames;
[e2a0d76]374
[c868e2d]375#if (PTL1_ENTRIES != 0)
[b0c2075]376 frames = PTL1_FRAMES;
[c868e2d]377#elif (PTL2_ENTRIES != 0)
[b0c2075]378 frames = PTL2_FRAMES;
[c868e2d]379#else
[b0c2075]380 frames = PTL3_FRAMES;
[c868e2d]381#endif
[e2a0d76]382
383 for (uintptr_t addr = ALIGN_DOWN(base, ptl0_step);
384 addr - 1 < base + size - 1;
[caed0279]385 addr += ptl0_step) {
[b0c2075]386 uintptr_t l1 = PA2KA(frame_alloc(frames, FRAME_LOWMEM, 0));
387 memsetb((void *) l1, FRAMES2SIZE(frames), 0);
[c868e2d]388 SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(addr), KA2PA(l1));
389 SET_PTL1_FLAGS(ptl0, PTL0_INDEX(addr),
[34ab31c0]390 PAGE_PRESENT | PAGE_USER | PAGE_CACHEABLE |
391 PAGE_EXEC | PAGE_WRITE | PAGE_READ);
[c868e2d]392 }
393}
394
[f47fd19]395/** @}
[b45c443]396 */
Note: See TracBrowser for help on using the repository browser.