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

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

Comment the purpose of the recently added barriers.

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