Rev 840 | Rev 977 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 840 | Rev 973 | ||
---|---|---|---|
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 | 72 | ||
73 | /** Initialize address space subsystem. */ |
73 | /** Initialize address space subsystem. */ |
74 | void as_init(void) |
74 | void as_init(void) |
75 | { |
75 | { |
76 | as_arch_init(); |
76 | as_arch_init(); |
77 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
77 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
78 | if (!AS_KERNEL) |
78 | if (!AS_KERNEL) |
79 | panic("can't create kernel address space\n"); |
79 | panic("can't create kernel address space\n"); |
80 | } |
80 | } |
81 | 81 | ||
82 | /** Create address space. |
82 | /** Create address space. |
83 | * |
83 | * |
84 | * @param flags Flags that influence way in wich the address space is created. |
84 | * @param flags Flags that influence way in wich the address space is created. |
85 | */ |
85 | */ |
86 | as_t *as_create(int flags) |
86 | as_t *as_create(int flags) |
87 | { |
87 | { |
88 | as_t *as; |
88 | as_t *as; |
89 | 89 | ||
90 | as = (as_t *) malloc(sizeof(as_t), 0); |
90 | as = (as_t *) malloc(sizeof(as_t), 0); |
91 | link_initialize(&as->inactive_as_with_asid_link); |
91 | link_initialize(&as->inactive_as_with_asid_link); |
92 | spinlock_initialize(&as->lock, "as_lock"); |
92 | spinlock_initialize(&as->lock, "as_lock"); |
93 | list_initialize(&as->as_area_head); |
93 | list_initialize(&as->as_area_head); |
94 | 94 | ||
95 | if (flags & FLAG_AS_KERNEL) |
95 | if (flags & FLAG_AS_KERNEL) |
96 | as->asid = ASID_KERNEL; |
96 | as->asid = ASID_KERNEL; |
97 | else |
97 | else |
98 | as->asid = ASID_INVALID; |
98 | as->asid = ASID_INVALID; |
99 | 99 | ||
100 | as->refcount = 0; |
100 | as->refcount = 0; |
101 | as->page_table = page_table_create(flags); |
101 | as->page_table = page_table_create(flags); |
102 | 102 | ||
103 | return as; |
103 | return as; |
104 | } |
104 | } |
105 | 105 | ||
- | 106 | /** Free Adress space */ |
|
- | 107 | void as_free(as_t *as) |
|
- | 108 | { |
|
- | 109 | ASSERT(as->refcount == 0); |
|
- | 110 | ||
- | 111 | /* TODO: free as_areas and other resources held by as */ |
|
- | 112 | /* TODO: free page table */ |
|
- | 113 | free(as); |
|
- | 114 | } |
|
- | 115 | ||
106 | /** Create address space area of common attributes. |
116 | /** Create address space area of common attributes. |
107 | * |
117 | * |
108 | * The created address space area is added to the target address space. |
118 | * The created address space area is added to the target address space. |
109 | * |
119 | * |
110 | * @param as Target address space. |
120 | * @param as Target address space. |
111 | * @param type Type of area. |
121 | * @param type Type of area. |
112 | * @param size Size of area in multiples of PAGE_SIZE. |
122 | * @param size Size of area in multiples of PAGE_SIZE. |
113 | * @param base Base address of area. |
123 | * @param base Base address of area. |
114 | * |
124 | * |
115 | * @return Address space area on success or NULL on failure. |
125 | * @return Address space area on success or NULL on failure. |
116 | */ |
126 | */ |
117 | as_area_t *as_area_create(as_t *as, as_area_type_t type, size_t size, __address base) |
127 | as_area_t *as_area_create(as_t *as, as_area_type_t type, size_t size, __address base) |
118 | { |
128 | { |
119 | ipl_t ipl; |
129 | ipl_t ipl; |
120 | as_area_t *a; |
130 | as_area_t *a; |
121 | 131 | ||
122 | if (base % PAGE_SIZE) |
132 | if (base % PAGE_SIZE) |
123 | panic("addr not aligned to a page boundary"); |
133 | panic("addr not aligned to a page boundary"); |
124 | 134 | ||
125 | ipl = interrupts_disable(); |
135 | ipl = interrupts_disable(); |
126 | spinlock_lock(&as->lock); |
136 | spinlock_lock(&as->lock); |
127 | 137 | ||
128 | /* |
138 | /* |
129 | * TODO: test as_area which is to be created doesn't overlap with an existing one. |
139 | * TODO: test as_area which is to be created doesn't overlap with an existing one. |
130 | */ |
140 | */ |
131 | 141 | ||
132 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
142 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
133 | 143 | ||
134 | spinlock_initialize(&a->lock, "as_area_lock"); |
144 | spinlock_initialize(&a->lock, "as_area_lock"); |
135 | 145 | ||
136 | link_initialize(&a->link); |
146 | link_initialize(&a->link); |
137 | a->type = type; |
147 | a->type = type; |
138 | a->size = size; |
148 | a->size = size; |
139 | a->base = base; |
149 | a->base = base; |
140 | 150 | ||
141 | list_append(&a->link, &as->as_area_head); |
151 | list_append(&a->link, &as->as_area_head); |
142 | 152 | ||
143 | spinlock_unlock(&as->lock); |
153 | spinlock_unlock(&as->lock); |
144 | interrupts_restore(ipl); |
154 | interrupts_restore(ipl); |
145 | 155 | ||
146 | return a; |
156 | return a; |
147 | } |
157 | } |
148 | 158 | ||
149 | /** Initialize mapping for one page of address space. |
159 | /** Initialize mapping for one page of address space. |
150 | * |
160 | * |
151 | * This functions maps 'page' to 'frame' according |
161 | * This functions maps 'page' to 'frame' according |
152 | * to attributes of the address space area to |
162 | * to attributes of the address space area to |
153 | * wich 'page' belongs. |
163 | * wich 'page' belongs. |
154 | * |
164 | * |
155 | * @param as Target address space. |
165 | * @param as Target address space. |
156 | * @param page Virtual page within the area. |
166 | * @param page Virtual page within the area. |
157 | * @param frame Physical frame to which page will be mapped. |
167 | * @param frame Physical frame to which page will be mapped. |
158 | */ |
168 | */ |
159 | void as_set_mapping(as_t *as, __address page, __address frame) |
169 | void as_set_mapping(as_t *as, __address page, __address frame) |
160 | { |
170 | { |
161 | as_area_t *a, *area = NULL; |
171 | as_area_t *a, *area = NULL; |
162 | link_t *cur; |
172 | link_t *cur; |
163 | ipl_t ipl; |
173 | ipl_t ipl; |
164 | 174 | ||
165 | ipl = interrupts_disable(); |
175 | ipl = interrupts_disable(); |
166 | spinlock_lock(&as->lock); |
176 | spinlock_lock(&as->lock); |
167 | 177 | ||
168 | /* |
178 | /* |
169 | * First, try locate an area. |
179 | * First, try locate an area. |
170 | */ |
180 | */ |
171 | for (cur = as->as_area_head.next; cur != &as->as_area_head; cur = cur->next) { |
181 | for (cur = as->as_area_head.next; cur != &as->as_area_head; cur = cur->next) { |
172 | a = list_get_instance(cur, as_area_t, link); |
182 | a = list_get_instance(cur, as_area_t, link); |
173 | spinlock_lock(&a->lock); |
183 | spinlock_lock(&a->lock); |
174 | 184 | ||
175 | if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) { |
185 | if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) { |
176 | area = a; |
186 | area = a; |
177 | break; |
187 | break; |
178 | } |
188 | } |
179 | 189 | ||
180 | spinlock_unlock(&a->lock); |
190 | spinlock_unlock(&a->lock); |
181 | } |
191 | } |
182 | 192 | ||
183 | if (!area) { |
193 | if (!area) { |
184 | panic("page not part of any as_area\n"); |
194 | panic("page not part of any as_area\n"); |
185 | } |
195 | } |
186 | 196 | ||
187 | /* |
197 | /* |
188 | * Note: area->lock is held. |
198 | * Note: area->lock is held. |
189 | */ |
199 | */ |
190 | 200 | ||
191 | page_mapping_insert(as, page, frame, get_area_flags(area)); |
201 | page_mapping_insert(as, page, frame, get_area_flags(area)); |
192 | 202 | ||
193 | spinlock_unlock(&area->lock); |
203 | spinlock_unlock(&area->lock); |
194 | spinlock_unlock(&as->lock); |
204 | spinlock_unlock(&as->lock); |
195 | interrupts_restore(ipl); |
205 | interrupts_restore(ipl); |
196 | } |
206 | } |
197 | 207 | ||
198 | /** Handle page fault within the current address space. |
208 | /** Handle page fault within the current address space. |
199 | * |
209 | * |
200 | * This is the high-level page fault handler. |
210 | * This is the high-level page fault handler. |
201 | * Interrupts are assumed disabled. |
211 | * Interrupts are assumed disabled. |
202 | * |
212 | * |
203 | * @param page Faulting page. |
213 | * @param page Faulting page. |
204 | * |
214 | * |
205 | * @return 0 on page fault, 1 on success. |
215 | * @return 0 on page fault, 1 on success. |
206 | */ |
216 | */ |
207 | int as_page_fault(__address page) |
217 | int as_page_fault(__address page) |
208 | { |
218 | { |
209 | link_t *cur; |
219 | link_t *cur; |
210 | as_area_t *a, *area = NULL; |
220 | as_area_t *a, *area = NULL; |
211 | __address frame; |
221 | __address frame; |
212 | 222 | ||
213 | ASSERT(AS); |
223 | ASSERT(AS); |
214 | spinlock_lock(&AS->lock); |
224 | spinlock_lock(&AS->lock); |
215 | 225 | ||
216 | /* |
226 | /* |
217 | * Search this areas of this address space for presence of 'page'. |
227 | * Search this areas of this address space for presence of 'page'. |
218 | */ |
228 | */ |
219 | for (cur = AS->as_area_head.next; cur != &AS->as_area_head; cur = cur->next) { |
229 | for (cur = AS->as_area_head.next; cur != &AS->as_area_head; cur = cur->next) { |
220 | a = list_get_instance(cur, as_area_t, link); |
230 | a = list_get_instance(cur, as_area_t, link); |
221 | spinlock_lock(&a->lock); |
231 | spinlock_lock(&a->lock); |
222 | 232 | ||
223 | if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) { |
233 | if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) { |
224 | 234 | ||
225 | /* |
235 | /* |
226 | * We found the area containing 'page'. |
236 | * We found the area containing 'page'. |
227 | * TODO: access checking |
237 | * TODO: access checking |
228 | */ |
238 | */ |
229 | area = a; |
239 | area = a; |
230 | break; |
240 | break; |
231 | } |
241 | } |
232 | 242 | ||
233 | spinlock_unlock(&a->lock); |
243 | spinlock_unlock(&a->lock); |
234 | } |
244 | } |
235 | 245 | ||
236 | if (!area) { |
246 | if (!area) { |
237 | /* |
247 | /* |
238 | * No area contained mapping for 'page'. |
248 | * No area contained mapping for 'page'. |
239 | * Signal page fault to low-level handler. |
249 | * Signal page fault to low-level handler. |
240 | */ |
250 | */ |
241 | spinlock_unlock(&AS->lock); |
251 | spinlock_unlock(&AS->lock); |
242 | return 0; |
252 | return 0; |
243 | } |
253 | } |
244 | 254 | ||
245 | /* |
255 | /* |
246 | * Note: area->lock is held. |
256 | * Note: area->lock is held. |
247 | */ |
257 | */ |
248 | 258 | ||
249 | /* |
259 | /* |
250 | * In general, there can be several reasons that |
260 | * In general, there can be several reasons that |
251 | * can have caused this fault. |
261 | * can have caused this fault. |
252 | * |
262 | * |
253 | * - non-existent mapping: the area is a scratch |
263 | * - non-existent mapping: the area is a scratch |
254 | * area (e.g. stack) and so far has not been |
264 | * area (e.g. stack) and so far has not been |
255 | * allocated a frame for the faulting page |
265 | * allocated a frame for the faulting page |
256 | * |
266 | * |
257 | * - non-present mapping: another possibility, |
267 | * - non-present mapping: another possibility, |
258 | * currently not implemented, would be frame |
268 | * currently not implemented, would be frame |
259 | * reuse; when this becomes a possibility, |
269 | * reuse; when this becomes a possibility, |
260 | * do not forget to distinguish between |
270 | * do not forget to distinguish between |
261 | * the different causes |
271 | * the different causes |
262 | */ |
272 | */ |
263 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
273 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
264 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
274 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
265 | 275 | ||
266 | /* |
276 | /* |
267 | * Map 'page' to 'frame'. |
277 | * Map 'page' to 'frame'. |
268 | * Note that TLB shootdown is not attempted as only new information is being |
278 | * Note that TLB shootdown is not attempted as only new information is being |
269 | * inserted into page tables. |
279 | * inserted into page tables. |
270 | */ |
280 | */ |
271 | page_mapping_insert(AS, page, frame, get_area_flags(area)); |
281 | page_mapping_insert(AS, page, frame, get_area_flags(area)); |
272 | 282 | ||
273 | spinlock_unlock(&area->lock); |
283 | spinlock_unlock(&area->lock); |
274 | spinlock_unlock(&AS->lock); |
284 | spinlock_unlock(&AS->lock); |
275 | 285 | ||
276 | return 1; |
286 | return 1; |
277 | } |
287 | } |
278 | 288 | ||
279 | /** Switch address spaces. |
289 | /** Switch address spaces. |
280 | * |
290 | * |
281 | * @param old Old address space or NULL. |
291 | * @param old Old address space or NULL. |
282 | * @param new New address space. |
292 | * @param new New address space. |
283 | */ |
293 | */ |
284 | void as_switch(as_t *old, as_t *new) |
294 | void as_switch(as_t *old, as_t *new) |
285 | { |
295 | { |
286 | ipl_t ipl; |
296 | ipl_t ipl; |
287 | bool needs_asid = false; |
297 | bool needs_asid = false; |
288 | 298 | ||
289 | ipl = interrupts_disable(); |
299 | ipl = interrupts_disable(); |
290 | spinlock_lock(&as_lock); |
300 | spinlock_lock(&as_lock); |
291 | 301 | ||
292 | /* |
302 | /* |
293 | * First, take care of the old address space. |
303 | * First, take care of the old address space. |
294 | */ |
304 | */ |
295 | if (old) { |
305 | if (old) { |
296 | spinlock_lock(&old->lock); |
306 | spinlock_lock(&old->lock); |
297 | ASSERT(old->refcount); |
307 | ASSERT(old->refcount); |
298 | if((--old->refcount == 0) && (old != AS_KERNEL)) { |
308 | if((--old->refcount == 0) && (old != AS_KERNEL)) { |
299 | /* |
309 | /* |
300 | * The old address space is no longer active on |
310 | * The old address space is no longer active on |
301 | * any processor. It can be appended to the |
311 | * any processor. It can be appended to the |
302 | * list of inactive address spaces with assigned |
312 | * list of inactive address spaces with assigned |
303 | * ASID. |
313 | * ASID. |
304 | */ |
314 | */ |
305 | ASSERT(old->asid != ASID_INVALID); |
315 | ASSERT(old->asid != ASID_INVALID); |
306 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head); |
316 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head); |
307 | } |
317 | } |
308 | spinlock_unlock(&old->lock); |
318 | spinlock_unlock(&old->lock); |
309 | } |
319 | } |
310 | 320 | ||
311 | /* |
321 | /* |
312 | * Second, prepare the new address space. |
322 | * Second, prepare the new address space. |
313 | */ |
323 | */ |
314 | spinlock_lock(&new->lock); |
324 | spinlock_lock(&new->lock); |
315 | if ((new->refcount++ == 0) && (new != AS_KERNEL)) { |
325 | if ((new->refcount++ == 0) && (new != AS_KERNEL)) { |
316 | if (new->asid != ASID_INVALID) |
326 | if (new->asid != ASID_INVALID) |
317 | list_remove(&new->inactive_as_with_asid_link); |
327 | list_remove(&new->inactive_as_with_asid_link); |
318 | else |
328 | else |
319 | needs_asid = true; /* defer call to asid_get() until new->lock is released */ |
329 | needs_asid = true; /* defer call to asid_get() until new->lock is released */ |
320 | } |
330 | } |
321 | SET_PTL0_ADDRESS(new->page_table); |
331 | SET_PTL0_ADDRESS(new->page_table); |
322 | spinlock_unlock(&new->lock); |
332 | spinlock_unlock(&new->lock); |
323 | 333 | ||
324 | if (needs_asid) { |
334 | if (needs_asid) { |
325 | /* |
335 | /* |
326 | * Allocation of new ASID was deferred |
336 | * Allocation of new ASID was deferred |
327 | * until now in order to avoid deadlock. |
337 | * until now in order to avoid deadlock. |
328 | */ |
338 | */ |
329 | asid_t asid; |
339 | asid_t asid; |
330 | 340 | ||
331 | asid = asid_get(); |
341 | asid = asid_get(); |
332 | spinlock_lock(&new->lock); |
342 | spinlock_lock(&new->lock); |
333 | new->asid = asid; |
343 | new->asid = asid; |
334 | spinlock_unlock(&new->lock); |
344 | spinlock_unlock(&new->lock); |
335 | } |
345 | } |
336 | spinlock_unlock(&as_lock); |
346 | spinlock_unlock(&as_lock); |
337 | interrupts_restore(ipl); |
347 | interrupts_restore(ipl); |
338 | 348 | ||
339 | /* |
349 | /* |
340 | * Perform architecture-specific steps. |
350 | * Perform architecture-specific steps. |
341 | * (e.g. write ASID to hardware register etc.) |
351 | * (e.g. write ASID to hardware register etc.) |
342 | */ |
352 | */ |
343 | as_install_arch(new); |
353 | as_install_arch(new); |
344 | 354 | ||
345 | AS = new; |
355 | AS = new; |
346 | } |
356 | } |
347 | 357 | ||
348 | /** Compute flags for virtual address translation subsytem. |
358 | /** Compute flags for virtual address translation subsytem. |
349 | * |
359 | * |
350 | * The address space area must be locked. |
360 | * The address space area must be locked. |
351 | * Interrupts must be disabled. |
361 | * Interrupts must be disabled. |
352 | * |
362 | * |
353 | * @param a Address space area. |
363 | * @param a Address space area. |
354 | * |
364 | * |
355 | * @return Flags to be used in page_mapping_insert(). |
365 | * @return Flags to be used in page_mapping_insert(). |
356 | */ |
366 | */ |
357 | int get_area_flags(as_area_t *a) |
367 | int get_area_flags(as_area_t *a) |
358 | { |
368 | { |
359 | int flags; |
369 | int flags; |
360 | 370 | ||
361 | switch (a->type) { |
371 | switch (a->type) { |
362 | case AS_AREA_TEXT: |
372 | case AS_AREA_TEXT: |
363 | flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
373 | flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
364 | break; |
374 | break; |
365 | case AS_AREA_DATA: |
375 | case AS_AREA_DATA: |
366 | case AS_AREA_STACK: |
376 | case AS_AREA_STACK: |
367 | flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
377 | flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
368 | break; |
378 | break; |
369 | default: |
379 | default: |
370 | panic("unexpected as_area_type_t %d", a->type); |
380 | panic("unexpected as_area_type_t %d", a->type); |
371 | } |
381 | } |
372 | 382 | ||
373 | return flags; |
383 | return flags; |
374 | } |
384 | } |
375 | 385 | ||
376 | /** Create page table. |
386 | /** Create page table. |
377 | * |
387 | * |
378 | * Depending on architecture, create either address space |
388 | * Depending on architecture, create either address space |
379 | * private or global page table. |
389 | * private or global page table. |
380 | * |
390 | * |
381 | * @param flags Flags saying whether the page table is for kernel address space. |
391 | * @param flags Flags saying whether the page table is for kernel address space. |
382 | * |
392 | * |
383 | * @return First entry of the page table. |
393 | * @return First entry of the page table. |
384 | */ |
394 | */ |
385 | pte_t *page_table_create(int flags) |
395 | pte_t *page_table_create(int flags) |
386 | { |
396 | { |
387 | ASSERT(as_operations); |
397 | ASSERT(as_operations); |
388 | ASSERT(as_operations->page_table_create); |
398 | ASSERT(as_operations->page_table_create); |
389 | 399 | ||
390 | return as_operations->page_table_create(flags); |
400 | return as_operations->page_table_create(flags); |
391 | } |
401 | } |
392 | 402 |