Rev 1026 | Rev 1048 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1026 | Rev 1044 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2006 Jakub Jermar |
2 | * Copyright (C) 2001-2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /* |
29 | /* |
30 | * This file contains address space manipulation functions. |
30 | * This file contains address space manipulation functions. |
31 | * Roughly speaking, this is a higher-level client of |
31 | * Roughly speaking, this is a higher-level client of |
32 | * Virtual Address Translation (VAT) subsystem. |
32 | * Virtual Address Translation (VAT) subsystem. |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #include <mm/as.h> |
35 | #include <mm/as.h> |
36 | #include <arch/mm/as.h> |
36 | #include <arch/mm/as.h> |
37 | #include <mm/page.h> |
37 | #include <mm/page.h> |
38 | #include <mm/frame.h> |
38 | #include <mm/frame.h> |
39 | #include <mm/slab.h> |
39 | #include <mm/slab.h> |
40 | #include <mm/tlb.h> |
40 | #include <mm/tlb.h> |
41 | #include <arch/mm/page.h> |
41 | #include <arch/mm/page.h> |
42 | #include <genarch/mm/page_pt.h> |
42 | #include <genarch/mm/page_pt.h> |
43 | #include <mm/asid.h> |
43 | #include <mm/asid.h> |
44 | #include <arch/mm/asid.h> |
44 | #include <arch/mm/asid.h> |
45 | #include <arch/types.h> |
45 | #include <arch/types.h> |
46 | #include <typedefs.h> |
46 | #include <typedefs.h> |
47 | #include <synch/spinlock.h> |
47 | #include <synch/spinlock.h> |
48 | #include <config.h> |
48 | #include <config.h> |
49 | #include <adt/list.h> |
49 | #include <adt/list.h> |
50 | #include <panic.h> |
50 | #include <panic.h> |
51 | #include <arch/asm.h> |
51 | #include <arch/asm.h> |
52 | #include <debug.h> |
52 | #include <debug.h> |
53 | #include <memstr.h> |
53 | #include <memstr.h> |
54 | #include <arch.h> |
54 | #include <arch.h> |
55 | #include <print.h> |
55 | #include <print.h> |
56 | 56 | ||
57 | as_operations_t *as_operations = NULL; |
57 | as_operations_t *as_operations = NULL; |
58 | 58 | ||
59 | /** Address space lock. It protects inactive_as_with_asid_head. */ |
59 | /** Address space lock. It protects inactive_as_with_asid_head. */ |
60 | SPINLOCK_INITIALIZE(as_lock); |
60 | SPINLOCK_INITIALIZE(as_lock); |
61 | 61 | ||
62 | /** |
62 | /** |
63 | * This list contains address spaces that are not active on any |
63 | * This list contains address spaces that are not active on any |
64 | * processor and that have valid ASID. |
64 | * processor and that have valid ASID. |
65 | */ |
65 | */ |
66 | LIST_INITIALIZE(inactive_as_with_asid_head); |
66 | LIST_INITIALIZE(inactive_as_with_asid_head); |
67 | 67 | ||
68 | /** Kernel address space. */ |
68 | /** Kernel address space. */ |
69 | as_t *AS_KERNEL = NULL; |
69 | as_t *AS_KERNEL = NULL; |
70 | 70 | ||
71 | static int get_area_flags(as_area_t *a); |
71 | static int get_area_flags(as_area_t *a); |
72 | static as_area_t *find_area_and_lock(as_t *as, __address va); |
72 | static as_area_t *find_area_and_lock(as_t *as, __address va); |
73 | 73 | ||
74 | /** Initialize address space subsystem. */ |
74 | /** Initialize address space subsystem. */ |
75 | void as_init(void) |
75 | void as_init(void) |
76 | { |
76 | { |
77 | as_arch_init(); |
77 | as_arch_init(); |
78 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
78 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
79 | if (!AS_KERNEL) |
79 | if (!AS_KERNEL) |
80 | panic("can't create kernel address space\n"); |
80 | panic("can't create kernel address space\n"); |
81 | } |
81 | } |
82 | 82 | ||
83 | /** Create address space. |
83 | /** Create address space. |
84 | * |
84 | * |
85 | * @param flags Flags that influence way in wich the address space is created. |
85 | * @param flags Flags that influence way in wich the address space is created. |
86 | */ |
86 | */ |
87 | as_t *as_create(int flags) |
87 | as_t *as_create(int flags) |
88 | { |
88 | { |
89 | as_t *as; |
89 | as_t *as; |
90 | 90 | ||
91 | as = (as_t *) malloc(sizeof(as_t), 0); |
91 | as = (as_t *) malloc(sizeof(as_t), 0); |
92 | link_initialize(&as->inactive_as_with_asid_link); |
92 | link_initialize(&as->inactive_as_with_asid_link); |
93 | spinlock_initialize(&as->lock, "as_lock"); |
93 | spinlock_initialize(&as->lock, "as_lock"); |
94 | list_initialize(&as->as_area_head); |
94 | list_initialize(&as->as_area_head); |
95 | 95 | ||
96 | if (flags & FLAG_AS_KERNEL) |
96 | if (flags & FLAG_AS_KERNEL) |
97 | as->asid = ASID_KERNEL; |
97 | as->asid = ASID_KERNEL; |
98 | else |
98 | else |
99 | as->asid = ASID_INVALID; |
99 | as->asid = ASID_INVALID; |
100 | 100 | ||
101 | as->refcount = 0; |
101 | as->refcount = 0; |
102 | as->page_table = page_table_create(flags); |
102 | as->page_table = page_table_create(flags); |
103 | 103 | ||
104 | return as; |
104 | return as; |
105 | } |
105 | } |
106 | 106 | ||
107 | /** Free Adress space */ |
107 | /** Free Adress space */ |
108 | void as_free(as_t *as) |
108 | void as_free(as_t *as) |
109 | { |
109 | { |
110 | ASSERT(as->refcount == 0); |
110 | ASSERT(as->refcount == 0); |
111 | 111 | ||
112 | /* TODO: free as_areas and other resources held by as */ |
112 | /* TODO: free as_areas and other resources held by as */ |
113 | /* TODO: free page table */ |
113 | /* TODO: free page table */ |
114 | free(as); |
114 | free(as); |
115 | } |
115 | } |
116 | 116 | ||
117 | /** Create address space area of common attributes. |
117 | /** Create address space area of common attributes. |
118 | * |
118 | * |
119 | * The created address space area is added to the target address space. |
119 | * The created address space area is added to the target address space. |
120 | * |
120 | * |
121 | * @param as Target address space. |
121 | * @param as Target address space. |
122 | * @param flags Flags of the area. |
122 | * @param flags Flags of the area. |
123 | * @param size Size of area in multiples of PAGE_SIZE. |
123 | * @param size Size of area in multiples of PAGE_SIZE. |
124 | * @param base Base address of area. |
124 | * @param base Base address of area. |
125 | * |
125 | * |
126 | * @return Address space area on success or NULL on failure. |
126 | * @return Address space area on success or NULL on failure. |
127 | */ |
127 | */ |
128 | as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base) |
128 | as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base) |
129 | { |
129 | { |
130 | ipl_t ipl; |
130 | ipl_t ipl; |
131 | as_area_t *a; |
131 | as_area_t *a; |
132 | 132 | ||
133 | if (base % PAGE_SIZE) |
133 | if (base % PAGE_SIZE) |
134 | panic("addr not aligned to a page boundary"); |
134 | panic("addr not aligned to a page boundary"); |
135 | 135 | ||
136 | ipl = interrupts_disable(); |
136 | ipl = interrupts_disable(); |
137 | spinlock_lock(&as->lock); |
137 | spinlock_lock(&as->lock); |
138 | 138 | ||
139 | /* |
139 | /* |
140 | * TODO: test as_area which is to be created doesn't overlap with an existing one. |
140 | * TODO: test as_area which is to be created doesn't overlap with an existing one. |
141 | */ |
141 | */ |
142 | 142 | ||
143 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
143 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
144 | 144 | ||
145 | spinlock_initialize(&a->lock, "as_area_lock"); |
145 | spinlock_initialize(&a->lock, "as_area_lock"); |
146 | 146 | ||
147 | link_initialize(&a->link); |
147 | link_initialize(&a->link); |
148 | a->flags = flags; |
148 | a->flags = flags; |
149 | a->size = size; |
149 | a->size = size; |
150 | a->base = base; |
150 | a->base = base; |
151 | 151 | ||
152 | list_append(&a->link, &as->as_area_head); |
152 | list_append(&a->link, &as->as_area_head); |
153 | 153 | ||
154 | spinlock_unlock(&as->lock); |
154 | spinlock_unlock(&as->lock); |
155 | interrupts_restore(ipl); |
155 | interrupts_restore(ipl); |
156 | 156 | ||
157 | return a; |
157 | return a; |
158 | } |
158 | } |
159 | 159 | ||
160 | /** Initialize mapping for one page of address space. |
160 | /** Initialize mapping for one page of address space. |
161 | * |
161 | * |
162 | * This functions maps 'page' to 'frame' according |
162 | * This functions maps 'page' to 'frame' according |
163 | * to attributes of the address space area to |
163 | * to attributes of the address space area to |
164 | * wich 'page' belongs. |
164 | * wich 'page' belongs. |
165 | * |
165 | * |
166 | * @param as Target address space. |
166 | * @param as Target address space. |
167 | * @param page Virtual page within the area. |
167 | * @param page Virtual page within the area. |
168 | * @param frame Physical frame to which page will be mapped. |
168 | * @param frame Physical frame to which page will be mapped. |
169 | */ |
169 | */ |
170 | void as_set_mapping(as_t *as, __address page, __address frame) |
170 | void as_set_mapping(as_t *as, __address page, __address frame) |
171 | { |
171 | { |
172 | as_area_t *area; |
172 | as_area_t *area; |
173 | ipl_t ipl; |
173 | ipl_t ipl; |
174 | 174 | ||
175 | ipl = interrupts_disable(); |
175 | ipl = interrupts_disable(); |
176 | spinlock_lock(&as->lock); |
176 | page_table_lock(as, true); |
177 | 177 | ||
178 | area = find_area_and_lock(as, page); |
178 | area = find_area_and_lock(as, page); |
179 | if (!area) { |
179 | if (!area) { |
180 | panic("page not part of any as_area\n"); |
180 | panic("page not part of any as_area\n"); |
181 | } |
181 | } |
182 | 182 | ||
183 | page_mapping_insert(as, page, frame, get_area_flags(area)); |
183 | page_mapping_insert(as, page, frame, get_area_flags(area)); |
184 | 184 | ||
185 | spinlock_unlock(&area->lock); |
185 | spinlock_unlock(&area->lock); |
186 | spinlock_unlock(&as->lock); |
186 | page_table_unlock(as, true); |
187 | interrupts_restore(ipl); |
187 | interrupts_restore(ipl); |
188 | } |
188 | } |
189 | 189 | ||
190 | /** Handle page fault within the current address space. |
190 | /** Handle page fault within the current address space. |
191 | * |
191 | * |
192 | * This is the high-level page fault handler. |
192 | * This is the high-level page fault handler. |
193 | * Interrupts are assumed disabled. |
193 | * Interrupts are assumed disabled. |
194 | * |
194 | * |
195 | * @param page Faulting page. |
195 | * @param page Faulting page. |
196 | * |
196 | * |
197 | * @return 0 on page fault, 1 on success. |
197 | * @return 0 on page fault, 1 on success. |
198 | */ |
198 | */ |
199 | int as_page_fault(__address page) |
199 | int as_page_fault(__address page) |
200 | { |
200 | { |
- | 201 | pte_t *pte; |
|
201 | as_area_t *area; |
202 | as_area_t *area; |
202 | __address frame; |
203 | __address frame; |
203 | 204 | ||
204 | ASSERT(AS); |
205 | ASSERT(AS); |
- | 206 | ||
205 | spinlock_lock(&AS->lock); |
207 | spinlock_lock(&AS->lock); |
206 | - | ||
207 | area = find_area_and_lock(AS, page); |
208 | area = find_area_and_lock(AS, page); |
208 | if (!area) { |
209 | if (!area) { |
209 | /* |
210 | /* |
210 | * No area contained mapping for 'page'. |
211 | * No area contained mapping for 'page'. |
211 | * Signal page fault to low-level handler. |
212 | * Signal page fault to low-level handler. |
212 | */ |
213 | */ |
213 | spinlock_unlock(&AS->lock); |
214 | spinlock_unlock(&AS->lock); |
214 | return 0; |
215 | return 0; |
215 | } |
216 | } |
216 | 217 | ||
- | 218 | page_table_lock(AS, false); |
|
- | 219 | ||
- | 220 | /* |
|
- | 221 | * To avoid race condition between two page faults |
|
- | 222 | * on the same address, we need to make sure |
|
- | 223 | * the mapping has not been already inserted. |
|
- | 224 | */ |
|
- | 225 | if ((pte = page_mapping_find(AS, page))) { |
|
- | 226 | if (PTE_PRESENT(pte)) { |
|
- | 227 | page_table_unlock(AS, false); |
|
- | 228 | spinlock_unlock(&area->lock); |
|
- | 229 | spinlock_unlock(&AS->lock); |
|
- | 230 | return 1; |
|
- | 231 | } |
|
- | 232 | } |
|
- | 233 | ||
217 | /* |
234 | /* |
218 | * In general, there can be several reasons that |
235 | * In general, there can be several reasons that |
219 | * can have caused this fault. |
236 | * can have caused this fault. |
220 | * |
237 | * |
221 | * - non-existent mapping: the area is a scratch |
238 | * - non-existent mapping: the area is a scratch |
222 | * area (e.g. stack) and so far has not been |
239 | * area (e.g. stack) and so far has not been |
223 | * allocated a frame for the faulting page |
240 | * allocated a frame for the faulting page |
224 | * |
241 | * |
225 | * - non-present mapping: another possibility, |
242 | * - non-present mapping: another possibility, |
226 | * currently not implemented, would be frame |
243 | * currently not implemented, would be frame |
227 | * reuse; when this becomes a possibility, |
244 | * reuse; when this becomes a possibility, |
228 | * do not forget to distinguish between |
245 | * do not forget to distinguish between |
229 | * the different causes |
246 | * the different causes |
230 | */ |
247 | */ |
231 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
248 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
232 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
249 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
233 | 250 | ||
234 | /* |
251 | /* |
235 | * Map 'page' to 'frame'. |
252 | * Map 'page' to 'frame'. |
236 | * Note that TLB shootdown is not attempted as only new information is being |
253 | * Note that TLB shootdown is not attempted as only new information is being |
237 | * inserted into page tables. |
254 | * inserted into page tables. |
238 | */ |
255 | */ |
239 | page_mapping_insert(AS, page, frame, get_area_flags(area)); |
256 | page_mapping_insert(AS, page, frame, get_area_flags(area)); |
- | 257 | page_table_unlock(AS, false); |
|
240 | 258 | ||
241 | spinlock_unlock(&area->lock); |
259 | spinlock_unlock(&area->lock); |
242 | spinlock_unlock(&AS->lock); |
260 | spinlock_unlock(&AS->lock); |
243 | - | ||
244 | return 1; |
261 | return 1; |
245 | } |
262 | } |
246 | 263 | ||
247 | /** Switch address spaces. |
264 | /** Switch address spaces. |
248 | * |
265 | * |
249 | * @param old Old address space or NULL. |
266 | * @param old Old address space or NULL. |
250 | * @param new New address space. |
267 | * @param new New address space. |
251 | */ |
268 | */ |
252 | void as_switch(as_t *old, as_t *new) |
269 | void as_switch(as_t *old, as_t *new) |
253 | { |
270 | { |
254 | ipl_t ipl; |
271 | ipl_t ipl; |
255 | bool needs_asid = false; |
272 | bool needs_asid = false; |
256 | 273 | ||
257 | ipl = interrupts_disable(); |
274 | ipl = interrupts_disable(); |
258 | spinlock_lock(&as_lock); |
275 | spinlock_lock(&as_lock); |
259 | 276 | ||
260 | /* |
277 | /* |
261 | * First, take care of the old address space. |
278 | * First, take care of the old address space. |
262 | */ |
279 | */ |
263 | if (old) { |
280 | if (old) { |
264 | spinlock_lock(&old->lock); |
281 | spinlock_lock(&old->lock); |
265 | ASSERT(old->refcount); |
282 | ASSERT(old->refcount); |
266 | if((--old->refcount == 0) && (old != AS_KERNEL)) { |
283 | if((--old->refcount == 0) && (old != AS_KERNEL)) { |
267 | /* |
284 | /* |
268 | * The old address space is no longer active on |
285 | * The old address space is no longer active on |
269 | * any processor. It can be appended to the |
286 | * any processor. It can be appended to the |
270 | * list of inactive address spaces with assigned |
287 | * list of inactive address spaces with assigned |
271 | * ASID. |
288 | * ASID. |
272 | */ |
289 | */ |
273 | ASSERT(old->asid != ASID_INVALID); |
290 | ASSERT(old->asid != ASID_INVALID); |
274 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head); |
291 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head); |
275 | } |
292 | } |
276 | spinlock_unlock(&old->lock); |
293 | spinlock_unlock(&old->lock); |
277 | } |
294 | } |
278 | 295 | ||
279 | /* |
296 | /* |
280 | * Second, prepare the new address space. |
297 | * Second, prepare the new address space. |
281 | */ |
298 | */ |
282 | spinlock_lock(&new->lock); |
299 | spinlock_lock(&new->lock); |
283 | if ((new->refcount++ == 0) && (new != AS_KERNEL)) { |
300 | if ((new->refcount++ == 0) && (new != AS_KERNEL)) { |
284 | if (new->asid != ASID_INVALID) |
301 | if (new->asid != ASID_INVALID) |
285 | list_remove(&new->inactive_as_with_asid_link); |
302 | list_remove(&new->inactive_as_with_asid_link); |
286 | else |
303 | else |
287 | needs_asid = true; /* defer call to asid_get() until new->lock is released */ |
304 | needs_asid = true; /* defer call to asid_get() until new->lock is released */ |
288 | } |
305 | } |
289 | SET_PTL0_ADDRESS(new->page_table); |
306 | SET_PTL0_ADDRESS(new->page_table); |
290 | spinlock_unlock(&new->lock); |
307 | spinlock_unlock(&new->lock); |
291 | 308 | ||
292 | if (needs_asid) { |
309 | if (needs_asid) { |
293 | /* |
310 | /* |
294 | * Allocation of new ASID was deferred |
311 | * Allocation of new ASID was deferred |
295 | * until now in order to avoid deadlock. |
312 | * until now in order to avoid deadlock. |
296 | */ |
313 | */ |
297 | asid_t asid; |
314 | asid_t asid; |
298 | 315 | ||
299 | asid = asid_get(); |
316 | asid = asid_get(); |
300 | spinlock_lock(&new->lock); |
317 | spinlock_lock(&new->lock); |
301 | new->asid = asid; |
318 | new->asid = asid; |
302 | spinlock_unlock(&new->lock); |
319 | spinlock_unlock(&new->lock); |
303 | } |
320 | } |
304 | spinlock_unlock(&as_lock); |
321 | spinlock_unlock(&as_lock); |
305 | interrupts_restore(ipl); |
322 | interrupts_restore(ipl); |
306 | 323 | ||
307 | /* |
324 | /* |
308 | * Perform architecture-specific steps. |
325 | * Perform architecture-specific steps. |
309 | * (e.g. write ASID to hardware register etc.) |
326 | * (e.g. write ASID to hardware register etc.) |
310 | */ |
327 | */ |
311 | as_install_arch(new); |
328 | as_install_arch(new); |
312 | 329 | ||
313 | AS = new; |
330 | AS = new; |
314 | } |
331 | } |
315 | 332 | ||
316 | /** Compute flags for virtual address translation subsytem. |
333 | /** Compute flags for virtual address translation subsytem. |
317 | * |
334 | * |
318 | * The address space area must be locked. |
335 | * The address space area must be locked. |
319 | * Interrupts must be disabled. |
336 | * Interrupts must be disabled. |
320 | * |
337 | * |
321 | * @param a Address space area. |
338 | * @param a Address space area. |
322 | * |
339 | * |
323 | * @return Flags to be used in page_mapping_insert(). |
340 | * @return Flags to be used in page_mapping_insert(). |
324 | */ |
341 | */ |
325 | int get_area_flags(as_area_t *a) |
342 | int get_area_flags(as_area_t *a) |
326 | { |
343 | { |
327 | int flags; |
344 | int flags; |
328 | 345 | ||
329 | flags = PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
346 | flags = PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
330 | 347 | ||
331 | if (a->flags & AS_AREA_READ) |
348 | if (a->flags & AS_AREA_READ) |
332 | flags |= PAGE_READ; |
349 | flags |= PAGE_READ; |
333 | 350 | ||
334 | if (a->flags & AS_AREA_WRITE) |
351 | if (a->flags & AS_AREA_WRITE) |
335 | flags |= PAGE_WRITE; |
352 | flags |= PAGE_WRITE; |
336 | 353 | ||
337 | if (a->flags & AS_AREA_EXEC) |
354 | if (a->flags & AS_AREA_EXEC) |
338 | flags |= PAGE_EXEC; |
355 | flags |= PAGE_EXEC; |
339 | 356 | ||
340 | return flags; |
357 | return flags; |
341 | } |
358 | } |
342 | 359 | ||
343 | /** Create page table. |
360 | /** Create page table. |
344 | * |
361 | * |
345 | * Depending on architecture, create either address space |
362 | * Depending on architecture, create either address space |
346 | * private or global page table. |
363 | * private or global page table. |
347 | * |
364 | * |
348 | * @param flags Flags saying whether the page table is for kernel address space. |
365 | * @param flags Flags saying whether the page table is for kernel address space. |
349 | * |
366 | * |
350 | * @return First entry of the page table. |
367 | * @return First entry of the page table. |
351 | */ |
368 | */ |
352 | pte_t *page_table_create(int flags) |
369 | pte_t *page_table_create(int flags) |
353 | { |
370 | { |
354 | ASSERT(as_operations); |
371 | ASSERT(as_operations); |
355 | ASSERT(as_operations->page_table_create); |
372 | ASSERT(as_operations->page_table_create); |
356 | 373 | ||
357 | return as_operations->page_table_create(flags); |
374 | return as_operations->page_table_create(flags); |
358 | } |
375 | } |
359 | 376 | ||
- | 377 | /** Lock page table. |
|
- | 378 | * |
|
- | 379 | * This function should be called before any page_mapping_insert(), |
|
- | 380 | * page_mapping_remove() and page_mapping_find(). |
|
- | 381 | * |
|
- | 382 | * Locking order is such that address space areas must be locked |
|
- | 383 | * prior to this call. Address space can be locked prior to this |
|
- | 384 | * call in which case the lock argument is false. |
|
- | 385 | * |
|
- | 386 | * @param as Address space. |
|
- | 387 | * @param as_locked If false, do not attempt to lock as->lock. |
|
- | 388 | */ |
|
- | 389 | void page_table_lock(as_t *as, bool lock) |
|
- | 390 | { |
|
- | 391 | ASSERT(as_operations); |
|
- | 392 | ASSERT(as_operations->page_table_lock); |
|
- | 393 | ||
- | 394 | as_operations->page_table_lock(as, lock); |
|
- | 395 | } |
|
- | 396 | ||
- | 397 | /** Unlock page table. |
|
- | 398 | * |
|
- | 399 | * @param as Address space. |
|
- | 400 | * @param as_locked If false, do not attempt to unlock as->lock. |
|
- | 401 | */ |
|
- | 402 | void page_table_unlock(as_t *as, bool unlock) |
|
- | 403 | { |
|
- | 404 | ASSERT(as_operations); |
|
- | 405 | ASSERT(as_operations->page_table_unlock); |
|
- | 406 | ||
- | 407 | as_operations->page_table_unlock(as, unlock); |
|
- | 408 | } |
|
- | 409 | ||
360 | /** Find address space area and change it. |
410 | /** Find address space area and change it. |
361 | * |
411 | * |
362 | * @param as Address space. |
412 | * @param as Address space. |
363 | * @param address Virtual address belonging to the area to be changed. Must be page-aligned. |
413 | * @param address Virtual address belonging to the area to be changed. Must be page-aligned. |
364 | * @param size New size of the virtual memory block starting at address. |
414 | * @param size New size of the virtual memory block starting at address. |
365 | * @param flags Flags influencing the remap operation. Currently unused. |
415 | * @param flags Flags influencing the remap operation. Currently unused. |
366 | * |
416 | * |
367 | * @return address on success, (__address) -1 otherwise. |
417 | * @return address on success, (__address) -1 otherwise. |
368 | */ |
418 | */ |
369 | __address as_remap(as_t *as, __address address, size_t size, int flags) |
419 | __address as_remap(as_t *as, __address address, size_t size, int flags) |
370 | { |
420 | { |
371 | as_area_t *area = NULL; |
421 | as_area_t *area = NULL; |
372 | ipl_t ipl; |
422 | ipl_t ipl; |
373 | size_t pages; |
423 | size_t pages; |
374 | 424 | ||
375 | ipl = interrupts_disable(); |
425 | ipl = interrupts_disable(); |
376 | spinlock_lock(&as->lock); |
426 | spinlock_lock(&as->lock); |
377 | 427 | ||
378 | /* |
428 | /* |
379 | * Locate the area. |
429 | * Locate the area. |
380 | */ |
430 | */ |
381 | area = find_area_and_lock(as, address); |
431 | area = find_area_and_lock(as, address); |
382 | if (!area) { |
432 | if (!area) { |
383 | spinlock_unlock(&as->lock); |
433 | spinlock_unlock(&as->lock); |
384 | return (__address) -1; |
434 | return (__address) -1; |
385 | } |
435 | } |
386 | 436 | ||
387 | pages = SIZE2FRAMES((address - area->base) + size); |
437 | pages = SIZE2FRAMES((address - area->base) + size); |
388 | if (pages < area->size) { |
438 | if (pages < area->size) { |
389 | int i; |
439 | int i; |
390 | 440 | ||
391 | /* |
441 | /* |
392 | * Shrinking the area. |
442 | * Shrinking the area. |
393 | */ |
443 | */ |
394 | for (i = pages; i < area->size; i++) { |
444 | for (i = pages; i < area->size; i++) { |
395 | pte_t *pte; |
445 | pte_t *pte; |
396 | 446 | ||
397 | /* |
447 | /* |
398 | * Releasing physical memory. |
448 | * Releasing physical memory. |
399 | * This depends on the fact that the memory was allocated using frame_alloc(). |
449 | * This depends on the fact that the memory was allocated using frame_alloc(). |
400 | */ |
450 | */ |
- | 451 | page_table_lock(as, false); |
|
401 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
452 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
402 | if (pte && PTE_VALID(pte)) { |
453 | if (pte && PTE_VALID(pte)) { |
- | 454 | __address frame; |
|
- | 455 | ||
403 | ASSERT(PTE_PRESENT(pte)); |
456 | ASSERT(PTE_PRESENT(pte)); |
404 | frame_free(ADDR2PFN(PTE_GET_FRAME(pte))); |
457 | frame = PTE_GET_FRAME(pte); |
405 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
458 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
- | 459 | page_table_unlock(as, false); |
|
- | 460 | ||
- | 461 | frame_free(ADDR2PFN(frame)); |
|
- | 462 | } else { |
|
- | 463 | page_table_unlock(as, false); |
|
406 | } |
464 | } |
407 | } |
465 | } |
408 | /* |
466 | /* |
409 | * Invalidate TLB's. |
467 | * Invalidate TLB's. |
410 | */ |
468 | */ |
411 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->size - pages); |
469 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->size - pages); |
412 | tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->size - pages); |
470 | tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->size - pages); |
413 | tlb_shootdown_finalize(); |
471 | tlb_shootdown_finalize(); |
414 | } |
472 | } |
415 | 473 | ||
416 | area->size = pages; |
474 | area->size = pages; |
417 | 475 | ||
418 | spinlock_unlock(&area->lock); |
476 | spinlock_unlock(&area->lock); |
419 | spinlock_unlock(&as->lock); |
477 | spinlock_unlock(&as->lock); |
420 | interrupts_restore(ipl); |
478 | interrupts_restore(ipl); |
421 | 479 | ||
422 | return address; |
480 | return address; |
423 | } |
481 | } |
424 | 482 | ||
425 | /** Find address space area and lock it. |
483 | /** Find address space area and lock it. |
426 | * |
484 | * |
427 | * The address space must be locked and interrupts must be disabled. |
485 | * The address space must be locked and interrupts must be disabled. |
428 | * |
486 | * |
429 | * @param as Address space. |
487 | * @param as Address space. |
430 | * @param va Virtual address. |
488 | * @param va Virtual address. |
431 | * |
489 | * |
432 | * @return Locked address space area containing va on success or NULL on failure. |
490 | * @return Locked address space area containing va on success or NULL on failure. |
433 | */ |
491 | */ |
434 | as_area_t *find_area_and_lock(as_t *as, __address va) |
492 | as_area_t *find_area_and_lock(as_t *as, __address va) |
435 | { |
493 | { |
436 | link_t *cur; |
494 | link_t *cur; |
437 | as_area_t *a; |
495 | as_area_t *a; |
438 | 496 | ||
439 | for (cur = as->as_area_head.next; cur != &as->as_area_head; cur = cur->next) { |
497 | for (cur = as->as_area_head.next; cur != &as->as_area_head; cur = cur->next) { |
440 | a = list_get_instance(cur, as_area_t, link); |
498 | a = list_get_instance(cur, as_area_t, link); |
441 | spinlock_lock(&a->lock); |
499 | spinlock_lock(&a->lock); |
442 | 500 | ||
443 | if ((va >= a->base) && (va < a->base + a->size * PAGE_SIZE)) |
501 | if ((va >= a->base) && (va < a->base + a->size * PAGE_SIZE)) |
444 | return a; |
502 | return a; |
445 | 503 | ||
446 | spinlock_unlock(&a->lock); |
504 | spinlock_unlock(&a->lock); |
447 | } |
505 | } |
448 | 506 | ||
449 | return NULL; |
507 | return NULL; |
450 | } |
508 | } |
451 | 509 |