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

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

When unmapping pages, do not cleanup the first level page tables
in the subtree which corresponds to the kernel non-identity mapping.

  • Property mode set to 100644
File size: 8.3 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>
[d99c1d2]45#include <typedefs.h>
[6d7ffa65]46#include <arch/asm.h>
47#include <memstr.h>
48
[da1bafb]49static void pt_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
50static void pt_mapping_remove(as_t *, uintptr_t);
[235e6c7]51static pte_t *pt_mapping_find(as_t *, uintptr_t, bool);
[6d7ffa65]52
[f5935ed]53page_mapping_operations_t pt_mapping_operations = {
[6d7ffa65]54 .mapping_insert = pt_mapping_insert,
[8f00329]55 .mapping_remove = pt_mapping_remove,
[6d7ffa65]56 .mapping_find = pt_mapping_find
57};
58
59/** Map page to frame using hierarchical page tables.
60 *
[9179d0a]61 * Map virtual address page to physical address frame
62 * using flags.
[6d7ffa65]63 *
[da1bafb]64 * @param as Address space to wich page belongs.
65 * @param page Virtual address of the page to be mapped.
[6d7ffa65]66 * @param frame Physical address of memory frame to which the mapping is done.
67 * @param flags Flags to be used for mapping.
[da1bafb]68 *
[6d7ffa65]69 */
[da1bafb]70void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
71 unsigned int flags)
[6d7ffa65]72{
[da1bafb]73 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
[1d432f9]74
75 ASSERT(page_table_locked(as));
[da1bafb]76
[6d7ffa65]77 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
[6b326ea1]78 pte_t *newpt = (pte_t *) frame_alloc(PTL1_SIZE,
79 FRAME_LOWMEM | FRAME_KA);
[e32e092]80 memsetb(newpt, FRAME_SIZE << PTL1_SIZE, 0);
[6d7ffa65]81 SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
[6b326ea1]82 SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page),
83 PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
84 PAGE_WRITE);
[6d7ffa65]85 }
[da1bafb]86
87 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
88
[6d7ffa65]89 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
[6b326ea1]90 pte_t *newpt = (pte_t *) frame_alloc(PTL2_SIZE,
91 FRAME_LOWMEM | FRAME_KA);
[e32e092]92 memsetb(newpt, FRAME_SIZE << PTL2_SIZE, 0);
[6d7ffa65]93 SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
[6b326ea1]94 SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page),
95 PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
96 PAGE_WRITE);
[6d7ffa65]97 }
[da1bafb]98
99 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
100
[6d7ffa65]101 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
[6b326ea1]102 pte_t *newpt = (pte_t *) frame_alloc(PTL3_SIZE,
103 FRAME_LOWMEM | FRAME_KA);
[e32e092]104 memsetb(newpt, FRAME_SIZE << PTL3_SIZE, 0);
[6d7ffa65]105 SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
[6b326ea1]106 SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page),
107 PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
108 PAGE_WRITE);
[6d7ffa65]109 }
[da1bafb]110
111 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
112
[6d7ffa65]113 SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame);
114 SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags);
115}
116
[8f00329]117/** Remove mapping of page from hierarchical page tables.
118 *
[9179d0a]119 * Remove any mapping of page within address space as.
[8f00329]120 * TLB shootdown should follow in order to make effects of
121 * this call visible.
122 *
[ecbdc724]123 * Empty page tables except PTL0 are freed.
124 *
[da1bafb]125 * @param as Address space to wich page belongs.
[8f00329]126 * @param page Virtual address of the page to be demapped.
[da1bafb]127 *
[8f00329]128 */
[7f1c620]129void pt_mapping_remove(as_t *as, uintptr_t page)
[8f00329]130{
[1d432f9]131 ASSERT(page_table_locked(as));
132
[ecbdc724]133 /*
134 * First, remove the mapping, if it exists.
[da1bafb]135 *
[ecbdc724]136 */
[da1bafb]137
138 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
[8f00329]139 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
140 return;
[da1bafb]141
142 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
[8f00329]143 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
144 return;
[da1bafb]145
146 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
[8f00329]147 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
148 return;
[da1bafb]149
150 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
151
[8f00329]152 /* Destroy the mapping. Setting to PAGE_NOT_PRESENT is not sufficient. */
[e32e092]153 memsetb(&ptl3[PTL3_INDEX(page)], sizeof(pte_t), 0);
[da1bafb]154
[ecbdc724]155 /*
[c72dc15]156 * Second, free all empty tables along the way from PTL3 down to PTL0
157 * except those needed for sharing the kernel non-identity mappings.
[ecbdc724]158 */
159
[da1bafb]160 /* Check PTL3 */
161 bool empty = true;
162
163 unsigned int i;
[ecbdc724]164 for (i = 0; i < PTL3_ENTRIES; i++) {
165 if (PTE_VALID(&ptl3[i])) {
166 empty = false;
167 break;
168 }
169 }
[da1bafb]170
[ecbdc724]171 if (empty) {
172 /*
173 * PTL3 is empty.
[c72dc15]174 * Release the frame and remove PTL3 pointer from the parent
175 * table.
[ecbdc724]176 */
[da1bafb]177#if (PTL2_ENTRIES != 0)
178 memsetb(&ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
179#elif (PTL1_ENTRIES != 0)
180 memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
181#else
[c72dc15]182 if (km_is_non_identity(page))
183 return;
184
[da1bafb]185 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
186#endif
[c72dc15]187 frame_free(KA2PA((uintptr_t) ptl3));
[ecbdc724]188 } else {
189 /*
190 * PTL3 is not empty.
191 * Therefore, there must be a path from PTL0 to PTL3 and
192 * thus nothing to free in higher levels.
[da1bafb]193 *
[ecbdc724]194 */
195 return;
196 }
197
[da1bafb]198 /* Check PTL2, empty is still true */
199#if (PTL2_ENTRIES != 0)
200 for (i = 0; i < PTL2_ENTRIES; i++) {
201 if (PTE_VALID(&ptl2[i])) {
202 empty = false;
203 break;
[ecbdc724]204 }
205 }
[da1bafb]206
207 if (empty) {
208 /*
209 * PTL2 is empty.
[c72dc15]210 * Release the frame and remove PTL2 pointer from the parent
211 * table.
[da1bafb]212 */
213#if (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
[c72dc15]221 frame_free(KA2PA((uintptr_t) ptl2));
[da1bafb]222 } else {
223 /*
224 * PTL2 is not empty.
225 * Therefore, there must be a path from PTL0 to PTL2 and
226 * thus nothing to free in higher levels.
227 *
228 */
229 return;
230 }
231#endif /* PTL2_ENTRIES != 0 */
232
[ecbdc724]233 /* check PTL1, empty is still true */
[da1bafb]234#if (PTL1_ENTRIES != 0)
235 for (i = 0; i < PTL1_ENTRIES; i++) {
236 if (PTE_VALID(&ptl1[i])) {
237 empty = false;
238 break;
[ecbdc724]239 }
240 }
[da1bafb]241
242 if (empty) {
243 /*
244 * PTL1 is empty.
[c72dc15]245 * Release the frame and remove PTL1 pointer from the parent
246 * table.
[da1bafb]247 */
[c72dc15]248 if (km_is_non_identity(page))
249 return;
250
[da1bafb]251 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
[c72dc15]252 frame_free(KA2PA((uintptr_t) ptl1));
[da1bafb]253 }
254#endif /* PTL1_ENTRIES != 0 */
[8f00329]255}
256
[6d7ffa65]257/** Find mapping for virtual page in hierarchical page tables.
258 *
[235e6c7]259 * @param as Address space to which page belongs.
260 * @param page Virtual page.
261 * @param nolock True if the page tables need not be locked.
[6d7ffa65]262 *
[da1bafb]263 * @return NULL if there is no such mapping; entry from PTL3 describing
264 * the mapping otherwise.
265 *
[6d7ffa65]266 */
[235e6c7]267pte_t *pt_mapping_find(as_t *as, uintptr_t page, bool nolock)
[6d7ffa65]268{
[235e6c7]269 ASSERT(nolock || page_table_locked(as));
[1d432f9]270
[da1bafb]271 pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
[6d7ffa65]272 if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
273 return NULL;
[da1bafb]274
275 pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
[6d7ffa65]276 if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
277 return NULL;
[da1bafb]278
279 pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
[6d7ffa65]280 if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
281 return NULL;
[da1bafb]282
283 pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
284
[6d7ffa65]285 return &ptl3[PTL3_INDEX(page)];
286}
[b45c443]287
[f47fd19]288/** @}
[b45c443]289 */
Note: See TracBrowser for help on using the repository browser.