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

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

Propagate the nolock flag to individual mapping_find() implementations.

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