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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 346b12a2 was 346b12a2, checked in by Jakub Jermar <jakub@…>, 9 years ago

Add page_mapping_update()

page_mapping_update() can be used to safely update the accessed and dirty
bits of a PTE in the actual page tables.

  • Property mode set to 100644
File size: 12.5 KB
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
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
29/** @addtogroup genarchmm
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Virtual Address Translation for hierarchical 4-level page tables.
36 */
37
38#include <genarch/mm/page_pt.h>
39#include <mm/page.h>
40#include <mm/frame.h>
41#include <mm/km.h>
42#include <mm/as.h>
43#include <arch/mm/page.h>
44#include <arch/mm/as.h>
45#include <arch/barrier.h>
46#include <typedefs.h>
47#include <arch/asm.h>
48#include <memstr.h>
49#include <align.h>
50#include <macros.h>
51#include <bitops.h>
52
53static void pt_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
54static void pt_mapping_remove(as_t *, uintptr_t);
55static bool pt_mapping_find(as_t *, uintptr_t, bool, pte_t *pte);
56static void pt_mapping_update(as_t *, uintptr_t, bool, pte_t *pte);
57static void pt_mapping_make_global(uintptr_t, size_t);
58
59page_mapping_operations_t pt_mapping_operations = {
60 .mapping_insert = pt_mapping_insert,
61 .mapping_remove = pt_mapping_remove,
62 .mapping_find = pt_mapping_find,
63 .mapping_update = pt_mapping_update,
64 .mapping_make_global = pt_mapping_make_global
65};
66
67/** Map page to frame using hierarchical page tables.
68 *
69 * Map virtual address page to physical address frame
70 * using flags.
71 *
72 * @param as Address space to wich page belongs.
73 * @param page Virtual address of the page to be mapped.
74 * @param frame Physical address of memory frame to which the mapping is done.
75 * @param flags Flags to be used for mapping.
76 *
77 */
78void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
79 unsigned int flags)
80{
81 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
82
83 ASSERT(page_table_locked(as));
84
85 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
86 pte_t *newpt = (pte_t *)
87 PA2KA(frame_alloc(PTL1_FRAMES, FRAME_LOWMEM, PTL1_SIZE - 1));
88 memsetb(newpt, PTL1_SIZE, 0);
89 SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
90 SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page),
91 PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
92 PAGE_WRITE);
93 /*
94 * Make sure that a concurrent hardware page table walk or
95 * pt_mapping_find() will see the new PTL1 only after it is
96 * fully initialized.
97 */
98 write_barrier();
99 SET_PTL1_PRESENT(ptl0, PTL0_INDEX(page));
100 }
101
102 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
103
104 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
105 pte_t *newpt = (pte_t *)
106 PA2KA(frame_alloc(PTL2_FRAMES, FRAME_LOWMEM, PTL2_SIZE - 1));
107 memsetb(newpt, PTL2_SIZE, 0);
108 SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
109 SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page),
110 PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
111 PAGE_WRITE);
112 /*
113 * Make the new PTL2 visible only after it is fully initialized.
114 */
115 write_barrier();
116 SET_PTL2_PRESENT(ptl1, PTL1_INDEX(page));
117 }
118
119 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
120
121 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
122 pte_t *newpt = (pte_t *)
123 PA2KA(frame_alloc(PTL3_FRAMES, FRAME_LOWMEM, PTL2_SIZE - 1));
124 memsetb(newpt, PTL2_SIZE, 0);
125 SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
126 SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page),
127 PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
128 PAGE_WRITE);
129 /*
130 * Make the new PTL3 visible only after it is fully initialized.
131 */
132 write_barrier();
133 SET_PTL3_PRESENT(ptl2, PTL2_INDEX(page));
134 }
135
136 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
137
138 SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame);
139 SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags | PAGE_NOT_PRESENT);
140 /*
141 * Make the new mapping visible only after it is fully initialized.
142 */
143 write_barrier();
144 SET_FRAME_PRESENT(ptl3, PTL3_INDEX(page));
145}
146
147/** Remove mapping of page from hierarchical page tables.
148 *
149 * Remove any mapping of page within address space as.
150 * TLB shootdown should follow in order to make effects of
151 * this call visible.
152 *
153 * Empty page tables except PTL0 are freed.
154 *
155 * @param as Address space to wich page belongs.
156 * @param page Virtual address of the page to be demapped.
157 *
158 */
159void pt_mapping_remove(as_t *as, uintptr_t page)
160{
161 ASSERT(page_table_locked(as));
162
163 /*
164 * First, remove the mapping, if it exists.
165 */
166
167 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
168 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
169 return;
170
171 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
172 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
173 return;
174
175 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
176 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
177 return;
178
179 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
180
181 /*
182 * Destroy the mapping.
183 * Setting to PAGE_NOT_PRESENT is not sufficient.
184 * But we need SET_FRAME for possible PT coherence maintenance.
185 * At least on ARM.
186 */
187 //TODO: Fix this inconsistency
188 SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), PAGE_NOT_PRESENT);
189 memsetb(&ptl3[PTL3_INDEX(page)], sizeof(pte_t), 0);
190
191 /*
192 * Second, free all empty tables along the way from PTL3 down to PTL0
193 * except those needed for sharing the kernel non-identity mappings.
194 */
195
196 /* Check PTL3 */
197 bool empty = true;
198
199 unsigned int i;
200 for (i = 0; i < PTL3_ENTRIES; i++) {
201 if (PTE_VALID(&ptl3[i])) {
202 empty = false;
203 break;
204 }
205 }
206
207 if (empty) {
208 /*
209 * PTL3 is empty.
210 * Release the frame and remove PTL3 pointer from the parent
211 * table.
212 */
213#if (PTL2_ENTRIES != 0)
214 memsetb(&ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
215#elif (PTL1_ENTRIES != 0)
216 memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
217#else
218 if (km_is_non_identity(page))
219 return;
220
221 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
222#endif
223 frame_free(KA2PA((uintptr_t) ptl3), PTL3_FRAMES);
224 } else {
225 /*
226 * PTL3 is not empty.
227 * Therefore, there must be a path from PTL0 to PTL3 and
228 * thus nothing to free in higher levels.
229 *
230 */
231 return;
232 }
233
234 /* Check PTL2, empty is still true */
235#if (PTL2_ENTRIES != 0)
236 for (i = 0; i < PTL2_ENTRIES; i++) {
237 if (PTE_VALID(&ptl2[i])) {
238 empty = false;
239 break;
240 }
241 }
242
243 if (empty) {
244 /*
245 * PTL2 is empty.
246 * Release the frame and remove PTL2 pointer from the parent
247 * table.
248 */
249#if (PTL1_ENTRIES != 0)
250 memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
251#else
252 if (km_is_non_identity(page))
253 return;
254
255 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
256#endif
257 frame_free(KA2PA((uintptr_t) ptl2), PTL2_FRAMES);
258 } else {
259 /*
260 * PTL2 is not empty.
261 * Therefore, there must be a path from PTL0 to PTL2 and
262 * thus nothing to free in higher levels.
263 *
264 */
265 return;
266 }
267#endif /* PTL2_ENTRIES != 0 */
268
269 /* check PTL1, empty is still true */
270#if (PTL1_ENTRIES != 0)
271 for (i = 0; i < PTL1_ENTRIES; i++) {
272 if (PTE_VALID(&ptl1[i])) {
273 empty = false;
274 break;
275 }
276 }
277
278 if (empty) {
279 /*
280 * PTL1 is empty.
281 * Release the frame and remove PTL1 pointer from the parent
282 * table.
283 */
284 if (km_is_non_identity(page))
285 return;
286
287 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
288 frame_free(KA2PA((uintptr_t) ptl1), PTL1_FRAMES);
289 }
290#endif /* PTL1_ENTRIES != 0 */
291}
292
293static pte_t *pt_mapping_find_internal(as_t *as, uintptr_t page, bool nolock)
294{
295 ASSERT(nolock || page_table_locked(as));
296
297 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
298 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
299 return NULL;
300
301 read_barrier();
302
303 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
304 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
305 return NULL;
306
307#if (PTL1_ENTRIES != 0)
308 /*
309 * Always read ptl2 only after we are sure it is present.
310 */
311 read_barrier();
312#endif
313
314 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
315 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
316 return NULL;
317
318#if (PTL2_ENTRIES != 0)
319 /*
320 * Always read ptl3 only after we are sure it is present.
321 */
322 read_barrier();
323#endif
324
325 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
326
327 return &ptl3[PTL3_INDEX(page)];
328}
329
330/** Find mapping for virtual page in hierarchical page tables.
331 *
332 * @param as Address space to which page belongs.
333 * @param page Virtual page.
334 * @param nolock True if the page tables need not be locked.
335 * @param[out] pte Structure that will receive a copy of the found PTE.
336 *
337 * @return True if the mapping was found, false otherwise.
338 */
339bool pt_mapping_find(as_t *as, uintptr_t page, bool nolock, pte_t *pte)
340{
341 pte_t *t = pt_mapping_find_internal(as, page, nolock);
342 if (t)
343 *pte = *t;
344 return t != NULL;
345}
346
347/** Update mapping for virtual page in hierarchical page tables.
348 *
349 * @param as Address space to which page belongs.
350 * @param page Virtual page.
351 * @param nolock True if the page tables need not be locked.
352 * @param[in] pte New PTE.
353 */
354void pt_mapping_update(as_t *as, uintptr_t page, bool nolock, pte_t *pte)
355{
356 pte_t *t = pt_mapping_find_internal(as, page, nolock);
357 if (!t)
358 panic("Updating non-existent PTE");
359
360 ASSERT(PTE_VALID(t) == PTE_VALID(pte));
361 ASSERT(PTE_PRESENT(t) == PTE_PRESENT(pte));
362 ASSERT(PTE_GET_FRAME(t) == PTE_GET_FRAME(pte));
363 ASSERT(PTE_WRITABLE(t) == PTE_WRITABLE(pte));
364 ASSERT(PTE_EXECUTABLE(t) == PTE_EXECUTABLE(pte));
365
366 *t = *pte;
367}
368
369/** Return the size of the region mapped by a single PTL0 entry.
370 *
371 * @return Size of the region mapped by a single PTL0 entry.
372 */
373static uintptr_t ptl0_step_get(void)
374{
375 size_t va_bits;
376
377 va_bits = fnzb(PTL0_ENTRIES) + fnzb(PTL1_ENTRIES) + fnzb(PTL2_ENTRIES) +
378 fnzb(PTL3_ENTRIES) + PAGE_WIDTH;
379
380 return 1UL << (va_bits - fnzb(PTL0_ENTRIES));
381}
382
383/** Make the mappings in the given range global accross all address spaces.
384 *
385 * All PTL0 entries in the given range will be mapped to a next level page
386 * table. The next level page table will be allocated and cleared.
387 *
388 * pt_mapping_remove() will never deallocate these page tables even when there
389 * are no PTEs in them.
390 *
391 * @param as Address space.
392 * @param base Base address corresponding to the first PTL0 entry that will be
393 * altered by this function.
394 * @param size Size in bytes defining the range of PTL0 entries that will be
395 * altered by this function.
396 *
397 */
398void pt_mapping_make_global(uintptr_t base, size_t size)
399{
400 ASSERT(size > 0);
401
402 uintptr_t ptl0 = PA2KA((uintptr_t) AS_KERNEL->genarch.page_table);
403 uintptr_t ptl0_step = ptl0_step_get();
404 size_t frames;
405
406#if (PTL1_ENTRIES != 0)
407 frames = PTL1_FRAMES;
408#elif (PTL2_ENTRIES != 0)
409 frames = PTL2_FRAMES;
410#else
411 frames = PTL3_FRAMES;
412#endif
413
414 for (uintptr_t addr = ALIGN_DOWN(base, ptl0_step);
415 addr - 1 < base + size - 1;
416 addr += ptl0_step) {
417 if (GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(addr))) {
418 ASSERT(overlaps(addr, ptl0_step,
419 config.identity_base, config.identity_size));
420
421 /*
422 * This PTL0 entry also maps the kernel identity region,
423 * so it is already global and initialized.
424 */
425 continue;
426 }
427
428 uintptr_t l1 = PA2KA(frame_alloc(frames, FRAME_LOWMEM, 0));
429 memsetb((void *) l1, FRAMES2SIZE(frames), 0);
430 SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(addr), KA2PA(l1));
431 SET_PTL1_FLAGS(ptl0, PTL0_INDEX(addr),
432 PAGE_PRESENT | PAGE_USER | PAGE_CACHEABLE |
433 PAGE_EXEC | PAGE_WRITE | PAGE_READ);
434 }
435}
436
437/** @}
438 */
Note: See TracBrowser for help on using the repository browser.