Rev 1380 | Rev 1387 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1380 | Rev 1383 | ||
---|---|---|---|
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 | * @file as.c |
30 | * @file as.c |
31 | * @brief Address space related functions. |
31 | * @brief Address space related functions. |
32 | * |
32 | * |
33 | * This file contains address space manipulation functions. |
33 | * This file contains address space manipulation functions. |
34 | * Roughly speaking, this is a higher-level client of |
34 | * Roughly speaking, this is a higher-level client of |
35 | * Virtual Address Translation (VAT) subsystem. |
35 | * Virtual Address Translation (VAT) subsystem. |
36 | * |
36 | * |
37 | * Functionality provided by this file allows one to |
37 | * Functionality provided by this file allows one to |
38 | * create address space and create, resize and share |
38 | * create address space and create, resize and share |
39 | * address space areas. |
39 | * address space areas. |
40 | * |
40 | * |
41 | * @see page.c |
41 | * @see page.c |
42 | * |
42 | * |
43 | */ |
43 | */ |
44 | 44 | ||
45 | #include <mm/as.h> |
45 | #include <mm/as.h> |
46 | #include <arch/mm/as.h> |
46 | #include <arch/mm/as.h> |
47 | #include <mm/page.h> |
47 | #include <mm/page.h> |
48 | #include <mm/frame.h> |
48 | #include <mm/frame.h> |
49 | #include <mm/slab.h> |
49 | #include <mm/slab.h> |
50 | #include <mm/tlb.h> |
50 | #include <mm/tlb.h> |
51 | #include <arch/mm/page.h> |
51 | #include <arch/mm/page.h> |
52 | #include <genarch/mm/page_pt.h> |
52 | #include <genarch/mm/page_pt.h> |
53 | #include <genarch/mm/page_ht.h> |
53 | #include <genarch/mm/page_ht.h> |
54 | #include <mm/asid.h> |
54 | #include <mm/asid.h> |
55 | #include <arch/mm/asid.h> |
55 | #include <arch/mm/asid.h> |
56 | #include <synch/spinlock.h> |
56 | #include <synch/spinlock.h> |
57 | #include <synch/mutex.h> |
57 | #include <synch/mutex.h> |
58 | #include <adt/list.h> |
58 | #include <adt/list.h> |
59 | #include <adt/btree.h> |
59 | #include <adt/btree.h> |
60 | #include <proc/task.h> |
60 | #include <proc/task.h> |
61 | #include <proc/thread.h> |
61 | #include <proc/thread.h> |
62 | #include <arch/asm.h> |
62 | #include <arch/asm.h> |
63 | #include <panic.h> |
63 | #include <panic.h> |
64 | #include <debug.h> |
64 | #include <debug.h> |
65 | #include <print.h> |
65 | #include <print.h> |
66 | #include <memstr.h> |
66 | #include <memstr.h> |
67 | #include <macros.h> |
67 | #include <macros.h> |
68 | #include <arch.h> |
68 | #include <arch.h> |
69 | #include <errno.h> |
69 | #include <errno.h> |
70 | #include <config.h> |
70 | #include <config.h> |
71 | #include <arch/types.h> |
71 | #include <arch/types.h> |
72 | #include <typedefs.h> |
72 | #include <typedefs.h> |
73 | #include <syscall/copy.h> |
73 | #include <syscall/copy.h> |
74 | #include <arch/interrupt.h> |
74 | #include <arch/interrupt.h> |
75 | 75 | ||
76 | as_operations_t *as_operations = NULL; |
76 | as_operations_t *as_operations = NULL; |
77 | 77 | ||
78 | /** Address space lock. It protects inactive_as_with_asid_head. Must be acquired before as_t mutex. */ |
78 | /** Address space lock. It protects inactive_as_with_asid_head. Must be acquired before as_t mutex. */ |
79 | SPINLOCK_INITIALIZE(as_lock); |
79 | SPINLOCK_INITIALIZE(as_lock); |
80 | 80 | ||
81 | /** |
81 | /** |
82 | * This list contains address spaces that are not active on any |
82 | * This list contains address spaces that are not active on any |
83 | * processor and that have valid ASID. |
83 | * processor and that have valid ASID. |
84 | */ |
84 | */ |
85 | LIST_INITIALIZE(inactive_as_with_asid_head); |
85 | LIST_INITIALIZE(inactive_as_with_asid_head); |
86 | 86 | ||
87 | /** Kernel address space. */ |
87 | /** Kernel address space. */ |
88 | as_t *AS_KERNEL = NULL; |
88 | as_t *AS_KERNEL = NULL; |
89 | 89 | ||
90 | static int area_flags_to_page_flags(int aflags); |
90 | static int area_flags_to_page_flags(int aflags); |
91 | static int get_area_flags(as_area_t *a); |
91 | static int get_area_flags(as_area_t *a); |
92 | static as_area_t *find_area_and_lock(as_t *as, __address va); |
92 | static as_area_t *find_area_and_lock(as_t *as, __address va); |
93 | static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area); |
93 | static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area); |
94 | 94 | ||
95 | /** Initialize address space subsystem. */ |
95 | /** Initialize address space subsystem. */ |
96 | void as_init(void) |
96 | void as_init(void) |
97 | { |
97 | { |
98 | as_arch_init(); |
98 | as_arch_init(); |
99 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
99 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
100 | if (!AS_KERNEL) |
100 | if (!AS_KERNEL) |
101 | panic("can't create kernel address space\n"); |
101 | panic("can't create kernel address space\n"); |
- | 102 | ||
102 | } |
103 | } |
103 | 104 | ||
104 | /** Create address space. |
105 | /** Create address space. |
105 | * |
106 | * |
106 | * @param flags Flags that influence way in wich the address space is created. |
107 | * @param flags Flags that influence way in wich the address space is created. |
107 | */ |
108 | */ |
108 | as_t *as_create(int flags) |
109 | as_t *as_create(int flags) |
109 | { |
110 | { |
110 | as_t *as; |
111 | as_t *as; |
111 | 112 | ||
112 | as = (as_t *) malloc(sizeof(as_t), 0); |
113 | as = (as_t *) malloc(sizeof(as_t), 0); |
113 | link_initialize(&as->inactive_as_with_asid_link); |
114 | link_initialize(&as->inactive_as_with_asid_link); |
114 | mutex_initialize(&as->lock); |
115 | mutex_initialize(&as->lock); |
115 | btree_create(&as->as_area_btree); |
116 | btree_create(&as->as_area_btree); |
116 | 117 | ||
117 | if (flags & FLAG_AS_KERNEL) |
118 | if (flags & FLAG_AS_KERNEL) |
118 | as->asid = ASID_KERNEL; |
119 | as->asid = ASID_KERNEL; |
119 | else |
120 | else |
120 | as->asid = ASID_INVALID; |
121 | as->asid = ASID_INVALID; |
121 | 122 | ||
122 | as->refcount = 0; |
123 | as->refcount = 0; |
123 | as->page_table = page_table_create(flags); |
124 | as->page_table = page_table_create(flags); |
124 | 125 | ||
125 | return as; |
126 | return as; |
126 | } |
127 | } |
127 | 128 | ||
128 | /** Free Adress space */ |
129 | /** Free Adress space */ |
129 | void as_free(as_t *as) |
130 | void as_free(as_t *as) |
130 | { |
131 | { |
131 | ASSERT(as->refcount == 0); |
132 | ASSERT(as->refcount == 0); |
132 | 133 | ||
133 | /* TODO: free as_areas and other resources held by as */ |
134 | /* TODO: free as_areas and other resources held by as */ |
134 | /* TODO: free page table */ |
135 | /* TODO: free page table */ |
135 | free(as); |
136 | free(as); |
136 | } |
137 | } |
137 | 138 | ||
138 | /** Create address space area of common attributes. |
139 | /** Create address space area of common attributes. |
139 | * |
140 | * |
140 | * The created address space area is added to the target address space. |
141 | * The created address space area is added to the target address space. |
141 | * |
142 | * |
142 | * @param as Target address space. |
143 | * @param as Target address space. |
143 | * @param flags Flags of the area memory. |
144 | * @param flags Flags of the area memory. |
144 | * @param size Size of area. |
145 | * @param size Size of area. |
145 | * @param base Base address of area. |
146 | * @param base Base address of area. |
146 | * @param attrs Attributes of the area. |
147 | * @param attrs Attributes of the area. |
147 | * |
148 | * |
148 | * @return Address space area on success or NULL on failure. |
149 | * @return Address space area on success or NULL on failure. |
149 | */ |
150 | */ |
150 | as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base, int attrs) |
151 | as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base, int attrs) |
151 | { |
152 | { |
152 | ipl_t ipl; |
153 | ipl_t ipl; |
153 | as_area_t *a; |
154 | as_area_t *a; |
154 | 155 | ||
155 | if (base % PAGE_SIZE) |
156 | if (base % PAGE_SIZE) |
156 | return NULL; |
157 | return NULL; |
157 | 158 | ||
158 | if (!size) |
159 | if (!size) |
159 | return NULL; |
160 | return NULL; |
160 | 161 | ||
161 | /* Writeable executable areas are not supported. */ |
162 | /* Writeable executable areas are not supported. */ |
162 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE)) |
163 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE)) |
163 | return NULL; |
164 | return NULL; |
164 | 165 | ||
165 | ipl = interrupts_disable(); |
166 | ipl = interrupts_disable(); |
166 | mutex_lock(&as->lock); |
167 | mutex_lock(&as->lock); |
167 | 168 | ||
168 | if (!check_area_conflicts(as, base, size, NULL)) { |
169 | if (!check_area_conflicts(as, base, size, NULL)) { |
169 | mutex_unlock(&as->lock); |
170 | mutex_unlock(&as->lock); |
170 | interrupts_restore(ipl); |
171 | interrupts_restore(ipl); |
171 | return NULL; |
172 | return NULL; |
172 | } |
173 | } |
173 | 174 | ||
174 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
175 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
175 | 176 | ||
176 | mutex_initialize(&a->lock); |
177 | mutex_initialize(&a->lock); |
177 | 178 | ||
178 | a->flags = flags; |
179 | a->flags = flags; |
179 | a->attributes = attrs; |
180 | a->attributes = attrs; |
180 | a->pages = SIZE2FRAMES(size); |
181 | a->pages = SIZE2FRAMES(size); |
181 | a->base = base; |
182 | a->base = base; |
182 | 183 | ||
183 | btree_insert(&as->as_area_btree, base, (void *) a, NULL); |
184 | btree_insert(&as->as_area_btree, base, (void *) a, NULL); |
184 | 185 | ||
185 | mutex_unlock(&as->lock); |
186 | mutex_unlock(&as->lock); |
186 | interrupts_restore(ipl); |
187 | interrupts_restore(ipl); |
187 | 188 | ||
188 | return a; |
189 | return a; |
189 | } |
190 | } |
190 | 191 | ||
191 | /** Find address space area and change it. |
192 | /** Find address space area and change it. |
192 | * |
193 | * |
193 | * @param as Address space. |
194 | * @param as Address space. |
194 | * @param address Virtual address belonging to the area to be changed. Must be page-aligned. |
195 | * @param address Virtual address belonging to the area to be changed. Must be page-aligned. |
195 | * @param size New size of the virtual memory block starting at address. |
196 | * @param size New size of the virtual memory block starting at address. |
196 | * @param flags Flags influencing the remap operation. Currently unused. |
197 | * @param flags Flags influencing the remap operation. Currently unused. |
197 | * |
198 | * |
198 | * @return Zero on success or a value from @ref errno.h otherwise. |
199 | * @return Zero on success or a value from @ref errno.h otherwise. |
199 | */ |
200 | */ |
200 | int as_area_resize(as_t *as, __address address, size_t size, int flags) |
201 | int as_area_resize(as_t *as, __address address, size_t size, int flags) |
201 | { |
202 | { |
202 | as_area_t *area; |
203 | as_area_t *area; |
203 | ipl_t ipl; |
204 | ipl_t ipl; |
204 | size_t pages; |
205 | size_t pages; |
205 | 206 | ||
206 | ipl = interrupts_disable(); |
207 | ipl = interrupts_disable(); |
207 | mutex_lock(&as->lock); |
208 | mutex_lock(&as->lock); |
208 | 209 | ||
209 | /* |
210 | /* |
210 | * Locate the area. |
211 | * Locate the area. |
211 | */ |
212 | */ |
212 | area = find_area_and_lock(as, address); |
213 | area = find_area_and_lock(as, address); |
213 | if (!area) { |
214 | if (!area) { |
214 | mutex_unlock(&as->lock); |
215 | mutex_unlock(&as->lock); |
215 | interrupts_restore(ipl); |
216 | interrupts_restore(ipl); |
216 | return ENOENT; |
217 | return ENOENT; |
217 | } |
218 | } |
218 | 219 | ||
219 | if (area->flags & AS_AREA_DEVICE) { |
220 | if (area->flags & AS_AREA_DEVICE) { |
220 | /* |
221 | /* |
221 | * Remapping of address space areas associated |
222 | * Remapping of address space areas associated |
222 | * with memory mapped devices is not supported. |
223 | * with memory mapped devices is not supported. |
223 | */ |
224 | */ |
224 | mutex_unlock(&area->lock); |
225 | mutex_unlock(&area->lock); |
225 | mutex_unlock(&as->lock); |
226 | mutex_unlock(&as->lock); |
226 | interrupts_restore(ipl); |
227 | interrupts_restore(ipl); |
227 | return ENOTSUP; |
228 | return ENOTSUP; |
228 | } |
229 | } |
229 | 230 | ||
230 | pages = SIZE2FRAMES((address - area->base) + size); |
231 | pages = SIZE2FRAMES((address - area->base) + size); |
231 | if (!pages) { |
232 | if (!pages) { |
232 | /* |
233 | /* |
233 | * Zero size address space areas are not allowed. |
234 | * Zero size address space areas are not allowed. |
234 | */ |
235 | */ |
235 | mutex_unlock(&area->lock); |
236 | mutex_unlock(&area->lock); |
236 | mutex_unlock(&as->lock); |
237 | mutex_unlock(&as->lock); |
237 | interrupts_restore(ipl); |
238 | interrupts_restore(ipl); |
238 | return EPERM; |
239 | return EPERM; |
239 | } |
240 | } |
240 | 241 | ||
241 | if (pages < area->pages) { |
242 | if (pages < area->pages) { |
242 | int i; |
243 | int i; |
243 | 244 | ||
244 | /* |
245 | /* |
245 | * Shrinking the area. |
246 | * Shrinking the area. |
246 | * No need to check for overlaps. |
247 | * No need to check for overlaps. |
247 | */ |
248 | */ |
248 | for (i = pages; i < area->pages; i++) { |
249 | for (i = pages; i < area->pages; i++) { |
249 | pte_t *pte; |
250 | pte_t *pte; |
250 | 251 | ||
251 | /* |
252 | /* |
252 | * Releasing physical memory. |
253 | * Releasing physical memory. |
253 | * This depends on the fact that the memory was allocated using frame_alloc(). |
254 | * This depends on the fact that the memory was allocated using frame_alloc(). |
254 | */ |
255 | */ |
255 | page_table_lock(as, false); |
256 | page_table_lock(as, false); |
256 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
257 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
257 | if (pte && PTE_VALID(pte)) { |
258 | if (pte && PTE_VALID(pte)) { |
258 | __address frame; |
259 | __address frame; |
259 | 260 | ||
260 | ASSERT(PTE_PRESENT(pte)); |
261 | ASSERT(PTE_PRESENT(pte)); |
261 | frame = PTE_GET_FRAME(pte); |
262 | frame = PTE_GET_FRAME(pte); |
262 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
263 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
263 | page_table_unlock(as, false); |
264 | page_table_unlock(as, false); |
264 | 265 | ||
265 | frame_free(ADDR2PFN(frame)); |
266 | frame_free(ADDR2PFN(frame)); |
266 | } else { |
267 | } else { |
267 | page_table_unlock(as, false); |
268 | page_table_unlock(as, false); |
268 | } |
269 | } |
269 | } |
270 | } |
270 | /* |
271 | /* |
271 | * Invalidate TLB's. |
272 | * Invalidate TLB's. |
272 | */ |
273 | */ |
273 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages); |
274 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages); |
274 | tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages); |
275 | tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages); |
275 | tlb_shootdown_finalize(); |
276 | tlb_shootdown_finalize(); |
276 | } else { |
277 | } else { |
277 | /* |
278 | /* |
278 | * Growing the area. |
279 | * Growing the area. |
279 | * Check for overlaps with other address space areas. |
280 | * Check for overlaps with other address space areas. |
280 | */ |
281 | */ |
281 | if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) { |
282 | if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) { |
282 | mutex_unlock(&area->lock); |
283 | mutex_unlock(&area->lock); |
283 | mutex_unlock(&as->lock); |
284 | mutex_unlock(&as->lock); |
284 | interrupts_restore(ipl); |
285 | interrupts_restore(ipl); |
285 | return EADDRNOTAVAIL; |
286 | return EADDRNOTAVAIL; |
286 | } |
287 | } |
287 | } |
288 | } |
288 | 289 | ||
289 | area->pages = pages; |
290 | area->pages = pages; |
290 | 291 | ||
291 | mutex_unlock(&area->lock); |
292 | mutex_unlock(&area->lock); |
292 | mutex_unlock(&as->lock); |
293 | mutex_unlock(&as->lock); |
293 | interrupts_restore(ipl); |
294 | interrupts_restore(ipl); |
294 | 295 | ||
295 | return 0; |
296 | return 0; |
296 | } |
297 | } |
297 | 298 | ||
298 | /** Destroy address space area. |
299 | /** Destroy address space area. |
299 | * |
300 | * |
300 | * @param as Address space. |
301 | * @param as Address space. |
301 | * @param address Address withing the area to be deleted. |
302 | * @param address Address withing the area to be deleted. |
302 | * |
303 | * |
303 | * @return Zero on success or a value from @ref errno.h on failure. |
304 | * @return Zero on success or a value from @ref errno.h on failure. |
304 | */ |
305 | */ |
305 | int as_area_destroy(as_t *as, __address address) |
306 | int as_area_destroy(as_t *as, __address address) |
306 | { |
307 | { |
307 | as_area_t *area; |
308 | as_area_t *area; |
308 | __address base; |
309 | __address base; |
309 | ipl_t ipl; |
310 | ipl_t ipl; |
310 | int i; |
311 | int i; |
311 | 312 | ||
312 | ipl = interrupts_disable(); |
313 | ipl = interrupts_disable(); |
313 | mutex_lock(&as->lock); |
314 | mutex_lock(&as->lock); |
314 | 315 | ||
315 | area = find_area_and_lock(as, address); |
316 | area = find_area_and_lock(as, address); |
316 | if (!area) { |
317 | if (!area) { |
317 | mutex_unlock(&as->lock); |
318 | mutex_unlock(&as->lock); |
318 | interrupts_restore(ipl); |
319 | interrupts_restore(ipl); |
319 | return ENOENT; |
320 | return ENOENT; |
320 | } |
321 | } |
321 | 322 | ||
322 | base = area->base; |
323 | base = area->base; |
323 | for (i = 0; i < area->pages; i++) { |
324 | for (i = 0; i < area->pages; i++) { |
324 | pte_t *pte; |
325 | pte_t *pte; |
325 | 326 | ||
326 | /* |
327 | /* |
327 | * Releasing physical memory. |
328 | * Releasing physical memory. |
328 | * Areas mapping memory-mapped devices are treated differently than |
329 | * Areas mapping memory-mapped devices are treated differently than |
329 | * areas backing frame_alloc()'ed memory. |
330 | * areas backing frame_alloc()'ed memory. |
330 | */ |
331 | */ |
331 | page_table_lock(as, false); |
332 | page_table_lock(as, false); |
332 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
333 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
333 | if (pte && PTE_VALID(pte)) { |
334 | if (pte && PTE_VALID(pte)) { |
334 | ASSERT(PTE_PRESENT(pte)); |
335 | ASSERT(PTE_PRESENT(pte)); |
335 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
336 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
336 | if (area->flags & AS_AREA_DEVICE) { |
337 | if (area->flags & AS_AREA_DEVICE) { |
337 | __address frame; |
338 | __address frame; |
338 | frame = PTE_GET_FRAME(pte); |
339 | frame = PTE_GET_FRAME(pte); |
339 | frame_free(ADDR2PFN(frame)); |
340 | frame_free(ADDR2PFN(frame)); |
340 | } |
341 | } |
341 | page_table_unlock(as, false); |
342 | page_table_unlock(as, false); |
342 | } else { |
343 | } else { |
343 | page_table_unlock(as, false); |
344 | page_table_unlock(as, false); |
344 | } |
345 | } |
345 | } |
346 | } |
346 | /* |
347 | /* |
347 | * Invalidate TLB's. |
348 | * Invalidate TLB's. |
348 | */ |
349 | */ |
349 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base, area->pages); |
350 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base, area->pages); |
350 | tlb_invalidate_pages(AS->asid, area->base, area->pages); |
351 | tlb_invalidate_pages(AS->asid, area->base, area->pages); |
351 | tlb_shootdown_finalize(); |
352 | tlb_shootdown_finalize(); |
352 | 353 | ||
353 | area->attributes |= AS_AREA_ATTR_PARTIAL; |
354 | area->attributes |= AS_AREA_ATTR_PARTIAL; |
354 | mutex_unlock(&area->lock); |
355 | mutex_unlock(&area->lock); |
355 | 356 | ||
356 | /* |
357 | /* |
357 | * Remove the empty area from address space. |
358 | * Remove the empty area from address space. |
358 | */ |
359 | */ |
359 | btree_remove(&AS->as_area_btree, base, NULL); |
360 | btree_remove(&AS->as_area_btree, base, NULL); |
360 | 361 | ||
361 | free(area); |
362 | free(area); |
362 | 363 | ||
363 | mutex_unlock(&AS->lock); |
364 | mutex_unlock(&AS->lock); |
364 | interrupts_restore(ipl); |
365 | interrupts_restore(ipl); |
365 | return 0; |
366 | return 0; |
366 | } |
367 | } |
367 | 368 | ||
368 | /** Steal address space area from another task. |
369 | /** Steal address space area from another task. |
369 | * |
370 | * |
370 | * Address space area is stolen from another task |
371 | * Address space area is stolen from another task |
371 | * Moreover, any existing mapping |
372 | * Moreover, any existing mapping |
372 | * is copied as well, providing thus a mechanism |
373 | * is copied as well, providing thus a mechanism |
373 | * for sharing group of pages. The source address |
374 | * for sharing group of pages. The source address |
374 | * space area and any associated mapping is preserved. |
375 | * space area and any associated mapping is preserved. |
375 | * |
376 | * |
376 | * @param src_task Pointer of source task |
377 | * @param src_task Pointer of source task |
377 | * @param src_base Base address of the source address space area. |
378 | * @param src_base Base address of the source address space area. |
378 | * @param acc_size Expected size of the source area |
379 | * @param acc_size Expected size of the source area |
379 | * @param dst_base Target base address |
380 | * @param dst_base Target base address |
380 | * |
381 | * |
381 | * @return Zero on success or ENOENT if there is no such task or |
382 | * @return Zero on success or ENOENT if there is no such task or |
382 | * if there is no such address space area, |
383 | * if there is no such address space area, |
383 | * EPERM if there was a problem in accepting the area or |
384 | * EPERM if there was a problem in accepting the area or |
384 | * ENOMEM if there was a problem in allocating destination |
385 | * ENOMEM if there was a problem in allocating destination |
385 | * address space area. |
386 | * address space area. |
386 | */ |
387 | */ |
387 | int as_area_steal(task_t *src_task, __address src_base, size_t acc_size, |
388 | int as_area_steal(task_t *src_task, __address src_base, size_t acc_size, |
388 | __address dst_base) |
389 | __address dst_base) |
389 | { |
390 | { |
390 | ipl_t ipl; |
391 | ipl_t ipl; |
391 | count_t i; |
392 | count_t i; |
392 | as_t *src_as; |
393 | as_t *src_as; |
393 | int src_flags; |
394 | int src_flags; |
394 | size_t src_size; |
395 | size_t src_size; |
395 | as_area_t *src_area, *dst_area; |
396 | as_area_t *src_area, *dst_area; |
396 | 397 | ||
397 | ipl = interrupts_disable(); |
398 | ipl = interrupts_disable(); |
398 | spinlock_lock(&src_task->lock); |
399 | spinlock_lock(&src_task->lock); |
399 | src_as = src_task->as; |
400 | src_as = src_task->as; |
400 | 401 | ||
401 | mutex_lock(&src_as->lock); |
402 | mutex_lock(&src_as->lock); |
402 | src_area = find_area_and_lock(src_as, src_base); |
403 | src_area = find_area_and_lock(src_as, src_base); |
403 | if (!src_area) { |
404 | if (!src_area) { |
404 | /* |
405 | /* |
405 | * Could not find the source address space area. |
406 | * Could not find the source address space area. |
406 | */ |
407 | */ |
407 | spinlock_unlock(&src_task->lock); |
408 | spinlock_unlock(&src_task->lock); |
408 | mutex_unlock(&src_as->lock); |
409 | mutex_unlock(&src_as->lock); |
409 | interrupts_restore(ipl); |
410 | interrupts_restore(ipl); |
410 | return ENOENT; |
411 | return ENOENT; |
411 | } |
412 | } |
412 | src_size = src_area->pages * PAGE_SIZE; |
413 | src_size = src_area->pages * PAGE_SIZE; |
413 | src_flags = src_area->flags; |
414 | src_flags = src_area->flags; |
414 | mutex_unlock(&src_area->lock); |
415 | mutex_unlock(&src_area->lock); |
415 | mutex_unlock(&src_as->lock); |
416 | mutex_unlock(&src_as->lock); |
416 | 417 | ||
417 | 418 | ||
418 | if (src_size != acc_size) { |
419 | if (src_size != acc_size) { |
419 | spinlock_unlock(&src_task->lock); |
420 | spinlock_unlock(&src_task->lock); |
420 | interrupts_restore(ipl); |
421 | interrupts_restore(ipl); |
421 | return EPERM; |
422 | return EPERM; |
422 | } |
423 | } |
423 | /* |
424 | /* |
424 | * Create copy of the source address space area. |
425 | * Create copy of the source address space area. |
425 | * The destination area is created with AS_AREA_ATTR_PARTIAL |
426 | * The destination area is created with AS_AREA_ATTR_PARTIAL |
426 | * attribute set which prevents race condition with |
427 | * attribute set which prevents race condition with |
427 | * preliminary as_page_fault() calls. |
428 | * preliminary as_page_fault() calls. |
428 | */ |
429 | */ |
429 | dst_area = as_area_create(AS, src_flags, src_size, dst_base, AS_AREA_ATTR_PARTIAL); |
430 | dst_area = as_area_create(AS, src_flags, src_size, dst_base, AS_AREA_ATTR_PARTIAL); |
430 | if (!dst_area) { |
431 | if (!dst_area) { |
431 | /* |
432 | /* |
432 | * Destination address space area could not be created. |
433 | * Destination address space area could not be created. |
433 | */ |
434 | */ |
434 | spinlock_unlock(&src_task->lock); |
435 | spinlock_unlock(&src_task->lock); |
435 | interrupts_restore(ipl); |
436 | interrupts_restore(ipl); |
436 | return ENOMEM; |
437 | return ENOMEM; |
437 | } |
438 | } |
438 | 439 | ||
439 | spinlock_unlock(&src_task->lock); |
440 | spinlock_unlock(&src_task->lock); |
440 | 441 | ||
441 | /* |
442 | /* |
442 | * Avoid deadlock by first locking the address space with lower address. |
443 | * Avoid deadlock by first locking the address space with lower address. |
443 | */ |
444 | */ |
444 | if (AS < src_as) { |
445 | if (AS < src_as) { |
445 | mutex_lock(&AS->lock); |
446 | mutex_lock(&AS->lock); |
446 | mutex_lock(&src_as->lock); |
447 | mutex_lock(&src_as->lock); |
447 | } else { |
448 | } else { |
448 | mutex_lock(&AS->lock); |
449 | mutex_lock(&AS->lock); |
449 | mutex_lock(&src_as->lock); |
450 | mutex_lock(&src_as->lock); |
450 | } |
451 | } |
451 | 452 | ||
452 | for (i = 0; i < SIZE2FRAMES(src_size); i++) { |
453 | for (i = 0; i < SIZE2FRAMES(src_size); i++) { |
453 | pte_t *pte; |
454 | pte_t *pte; |
454 | __address frame; |
455 | __address frame; |
455 | 456 | ||
456 | page_table_lock(src_as, false); |
457 | page_table_lock(src_as, false); |
457 | pte = page_mapping_find(src_as, src_base + i*PAGE_SIZE); |
458 | pte = page_mapping_find(src_as, src_base + i*PAGE_SIZE); |
458 | if (pte && PTE_VALID(pte)) { |
459 | if (pte && PTE_VALID(pte)) { |
459 | ASSERT(PTE_PRESENT(pte)); |
460 | ASSERT(PTE_PRESENT(pte)); |
460 | frame = PTE_GET_FRAME(pte); |
461 | frame = PTE_GET_FRAME(pte); |
461 | if (!(src_flags & AS_AREA_DEVICE)) |
462 | if (!(src_flags & AS_AREA_DEVICE)) |
462 | frame_reference_add(ADDR2PFN(frame)); |
463 | frame_reference_add(ADDR2PFN(frame)); |
463 | page_table_unlock(src_as, false); |
464 | page_table_unlock(src_as, false); |
464 | } else { |
465 | } else { |
465 | page_table_unlock(src_as, false); |
466 | page_table_unlock(src_as, false); |
466 | continue; |
467 | continue; |
467 | } |
468 | } |
468 | 469 | ||
469 | page_table_lock(AS, false); |
470 | page_table_lock(AS, false); |
470 | page_mapping_insert(AS, dst_base + i*PAGE_SIZE, frame, area_flags_to_page_flags(src_flags)); |
471 | page_mapping_insert(AS, dst_base + i*PAGE_SIZE, frame, area_flags_to_page_flags(src_flags)); |
471 | page_table_unlock(AS, false); |
472 | page_table_unlock(AS, false); |
472 | } |
473 | } |
473 | 474 | ||
474 | /* |
475 | /* |
475 | * Now the destination address space area has been |
476 | * Now the destination address space area has been |
476 | * fully initialized. Clear the AS_AREA_ATTR_PARTIAL |
477 | * fully initialized. Clear the AS_AREA_ATTR_PARTIAL |
477 | * attribute. |
478 | * attribute. |
478 | */ |
479 | */ |
479 | mutex_lock(&dst_area->lock); |
480 | mutex_lock(&dst_area->lock); |
480 | dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL; |
481 | dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL; |
481 | mutex_unlock(&dst_area->lock); |
482 | mutex_unlock(&dst_area->lock); |
482 | 483 | ||
483 | mutex_unlock(&AS->lock); |
484 | mutex_unlock(&AS->lock); |
484 | mutex_unlock(&src_as->lock); |
485 | mutex_unlock(&src_as->lock); |
485 | interrupts_restore(ipl); |
486 | interrupts_restore(ipl); |
486 | 487 | ||
487 | return 0; |
488 | return 0; |
488 | } |
489 | } |
489 | 490 | ||
490 | /** Initialize mapping for one page of address space. |
491 | /** Initialize mapping for one page of address space. |
491 | * |
492 | * |
492 | * This functions maps 'page' to 'frame' according |
493 | * This functions maps 'page' to 'frame' according |
493 | * to attributes of the address space area to |
494 | * to attributes of the address space area to |
494 | * wich 'page' belongs. |
495 | * wich 'page' belongs. |
495 | * |
496 | * |
496 | * @param as Target address space. |
497 | * @param as Target address space. |
497 | * @param page Virtual page within the area. |
498 | * @param page Virtual page within the area. |
498 | * @param frame Physical frame to which page will be mapped. |
499 | * @param frame Physical frame to which page will be mapped. |
499 | */ |
500 | */ |
500 | void as_set_mapping(as_t *as, __address page, __address frame) |
501 | void as_set_mapping(as_t *as, __address page, __address frame) |
501 | { |
502 | { |
502 | as_area_t *area; |
503 | as_area_t *area; |
503 | ipl_t ipl; |
504 | ipl_t ipl; |
504 | 505 | ||
505 | ipl = interrupts_disable(); |
506 | ipl = interrupts_disable(); |
506 | page_table_lock(as, true); |
507 | page_table_lock(as, true); |
507 | 508 | ||
508 | area = find_area_and_lock(as, page); |
509 | area = find_area_and_lock(as, page); |
509 | if (!area) { |
510 | if (!area) { |
510 | panic("page not part of any as_area\n"); |
511 | panic("page not part of any as_area\n"); |
511 | } |
512 | } |
512 | 513 | ||
513 | page_mapping_insert(as, page, frame, get_area_flags(area)); |
514 | page_mapping_insert(as, page, frame, get_area_flags(area)); |
514 | 515 | ||
515 | mutex_unlock(&area->lock); |
516 | mutex_unlock(&area->lock); |
516 | page_table_unlock(as, true); |
517 | page_table_unlock(as, true); |
517 | interrupts_restore(ipl); |
518 | interrupts_restore(ipl); |
518 | } |
519 | } |
519 | 520 | ||
520 | /** Handle page fault within the current address space. |
521 | /** Handle page fault within the current address space. |
521 | * |
522 | * |
522 | * This is the high-level page fault handler. |
523 | * This is the high-level page fault handler. |
523 | * Interrupts are assumed disabled. |
524 | * Interrupts are assumed disabled. |
524 | * |
525 | * |
525 | * @param page Faulting page. |
526 | * @param page Faulting page. |
526 | * @param istate Pointer to interrupted state. |
527 | * @param istate Pointer to interrupted state. |
527 | * |
528 | * |
528 | * @return 0 on page fault, 1 on success or 2 if the fault was caused by copy_to_uspace() or copy_from_uspace(). |
529 | * @return 0 on page fault, 1 on success or 2 if the fault was caused by copy_to_uspace() or copy_from_uspace(). |
529 | */ |
530 | */ |
530 | int as_page_fault(__address page, istate_t *istate) |
531 | int as_page_fault(__address page, istate_t *istate) |
531 | { |
532 | { |
532 | pte_t *pte; |
533 | pte_t *pte; |
533 | as_area_t *area; |
534 | as_area_t *area; |
534 | __address frame; |
535 | __address frame; |
535 | 536 | ||
536 | if (!THREAD) |
537 | if (!THREAD) |
537 | return 0; |
538 | return 0; |
538 | 539 | ||
539 | ASSERT(AS); |
540 | ASSERT(AS); |
540 | 541 | ||
541 | mutex_lock(&AS->lock); |
542 | mutex_lock(&AS->lock); |
542 | area = find_area_and_lock(AS, page); |
543 | area = find_area_and_lock(AS, page); |
543 | if (!area) { |
544 | if (!area) { |
544 | /* |
545 | /* |
545 | * No area contained mapping for 'page'. |
546 | * No area contained mapping for 'page'. |
546 | * Signal page fault to low-level handler. |
547 | * Signal page fault to low-level handler. |
547 | */ |
548 | */ |
548 | mutex_unlock(&AS->lock); |
549 | mutex_unlock(&AS->lock); |
549 | goto page_fault; |
550 | goto page_fault; |
550 | } |
551 | } |
551 | 552 | ||
552 | if (area->attributes & AS_AREA_ATTR_PARTIAL) { |
553 | if (area->attributes & AS_AREA_ATTR_PARTIAL) { |
553 | /* |
554 | /* |
554 | * The address space area is not fully initialized. |
555 | * The address space area is not fully initialized. |
555 | * Avoid possible race by returning error. |
556 | * Avoid possible race by returning error. |
556 | */ |
557 | */ |
557 | mutex_unlock(&area->lock); |
558 | mutex_unlock(&area->lock); |
558 | mutex_unlock(&AS->lock); |
559 | mutex_unlock(&AS->lock); |
559 | goto page_fault; |
560 | goto page_fault; |
560 | } |
561 | } |
561 | 562 | ||
562 | ASSERT(!(area->flags & AS_AREA_DEVICE)); |
563 | ASSERT(!(area->flags & AS_AREA_DEVICE)); |
563 | 564 | ||
564 | page_table_lock(AS, false); |
565 | page_table_lock(AS, false); |
565 | 566 | ||
566 | /* |
567 | /* |
567 | * To avoid race condition between two page faults |
568 | * To avoid race condition between two page faults |
568 | * on the same address, we need to make sure |
569 | * on the same address, we need to make sure |
569 | * the mapping has not been already inserted. |
570 | * the mapping has not been already inserted. |
570 | */ |
571 | */ |
571 | if ((pte = page_mapping_find(AS, page))) { |
572 | if ((pte = page_mapping_find(AS, page))) { |
572 | if (PTE_PRESENT(pte)) { |
573 | if (PTE_PRESENT(pte)) { |
573 | page_table_unlock(AS, false); |
574 | page_table_unlock(AS, false); |
574 | mutex_unlock(&area->lock); |
575 | mutex_unlock(&area->lock); |
575 | mutex_unlock(&AS->lock); |
576 | mutex_unlock(&AS->lock); |
576 | return 1; |
577 | return 1; |
577 | } |
578 | } |
578 | } |
579 | } |
579 | 580 | ||
580 | /* |
581 | /* |
581 | * In general, there can be several reasons that |
582 | * In general, there can be several reasons that |
582 | * can have caused this fault. |
583 | * can have caused this fault. |
583 | * |
584 | * |
584 | * - non-existent mapping: the area is a scratch |
585 | * - non-existent mapping: the area is a scratch |
585 | * area (e.g. stack) and so far has not been |
586 | * area (e.g. stack) and so far has not been |
586 | * allocated a frame for the faulting page |
587 | * allocated a frame for the faulting page |
587 | * |
588 | * |
588 | * - non-present mapping: another possibility, |
589 | * - non-present mapping: another possibility, |
589 | * currently not implemented, would be frame |
590 | * currently not implemented, would be frame |
590 | * reuse; when this becomes a possibility, |
591 | * reuse; when this becomes a possibility, |
591 | * do not forget to distinguish between |
592 | * do not forget to distinguish between |
592 | * the different causes |
593 | * the different causes |
593 | */ |
594 | */ |
594 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
595 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
595 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
596 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
596 | 597 | ||
597 | /* |
598 | /* |
598 | * Map 'page' to 'frame'. |
599 | * Map 'page' to 'frame'. |
599 | * Note that TLB shootdown is not attempted as only new information is being |
600 | * Note that TLB shootdown is not attempted as only new information is being |
600 | * inserted into page tables. |
601 | * inserted into page tables. |
601 | */ |
602 | */ |
602 | page_mapping_insert(AS, page, frame, get_area_flags(area)); |
603 | page_mapping_insert(AS, page, frame, get_area_flags(area)); |
603 | page_table_unlock(AS, false); |
604 | page_table_unlock(AS, false); |
604 | 605 | ||
605 | mutex_unlock(&area->lock); |
606 | mutex_unlock(&area->lock); |
606 | mutex_unlock(&AS->lock); |
607 | mutex_unlock(&AS->lock); |
607 | return AS_PF_OK; |
608 | return AS_PF_OK; |
608 | 609 | ||
609 | page_fault: |
610 | page_fault: |
610 | if (!THREAD) |
611 | if (!THREAD) |
611 | return AS_PF_FAULT; |
612 | return AS_PF_FAULT; |
612 | 613 | ||
613 | if (THREAD->in_copy_from_uspace) { |
614 | if (THREAD->in_copy_from_uspace) { |
614 | THREAD->in_copy_from_uspace = false; |
615 | THREAD->in_copy_from_uspace = false; |
615 | istate_set_retaddr(istate, (__address) &memcpy_from_uspace_failover_address); |
616 | istate_set_retaddr(istate, (__address) &memcpy_from_uspace_failover_address); |
616 | } else if (THREAD->in_copy_to_uspace) { |
617 | } else if (THREAD->in_copy_to_uspace) { |
617 | THREAD->in_copy_to_uspace = false; |
618 | THREAD->in_copy_to_uspace = false; |
618 | istate_set_retaddr(istate, (__address) &memcpy_to_uspace_failover_address); |
619 | istate_set_retaddr(istate, (__address) &memcpy_to_uspace_failover_address); |
619 | } else { |
620 | } else { |
620 | return AS_PF_FAULT; |
621 | return AS_PF_FAULT; |
621 | } |
622 | } |
622 | 623 | ||
623 | return AS_PF_DEFER; |
624 | return AS_PF_DEFER; |
624 | } |
625 | } |
625 | 626 | ||
626 | /** Switch address spaces. |
627 | /** Switch address spaces. |
627 | * |
628 | * |
628 | * Note that this function cannot sleep as it is essentially a part of |
629 | * Note that this function cannot sleep as it is essentially a part of |
629 | * the scheduling. Sleeping here would lead to deadlock on wakeup. |
630 | * the scheduling. Sleeping here would lead to deadlock on wakeup. |
630 | * |
631 | * |
631 | * @param old Old address space or NULL. |
632 | * @param old Old address space or NULL. |
632 | * @param new New address space. |
633 | * @param new New address space. |
633 | */ |
634 | */ |
634 | void as_switch(as_t *old, as_t *new) |
635 | void as_switch(as_t *old, as_t *new) |
635 | { |
636 | { |
636 | ipl_t ipl; |
637 | ipl_t ipl; |
637 | bool needs_asid = false; |
638 | bool needs_asid = false; |
638 | 639 | ||
639 | ipl = interrupts_disable(); |
640 | ipl = interrupts_disable(); |
640 | spinlock_lock(&as_lock); |
641 | spinlock_lock(&as_lock); |
641 | 642 | ||
642 | /* |
643 | /* |
643 | * First, take care of the old address space. |
644 | * First, take care of the old address space. |
644 | */ |
645 | */ |
645 | if (old) { |
646 | if (old) { |
646 | mutex_lock_active(&old->lock); |
647 | mutex_lock_active(&old->lock); |
647 | ASSERT(old->refcount); |
648 | ASSERT(old->refcount); |
648 | if((--old->refcount == 0) && (old != AS_KERNEL)) { |
649 | if((--old->refcount == 0) && (old != AS_KERNEL)) { |
649 | /* |
650 | /* |
650 | * The old address space is no longer active on |
651 | * The old address space is no longer active on |
651 | * any processor. It can be appended to the |
652 | * any processor. It can be appended to the |
652 | * list of inactive address spaces with assigned |
653 | * list of inactive address spaces with assigned |
653 | * ASID. |
654 | * ASID. |
654 | */ |
655 | */ |
655 | ASSERT(old->asid != ASID_INVALID); |
656 | ASSERT(old->asid != ASID_INVALID); |
656 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head); |
657 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head); |
657 | } |
658 | } |
658 | mutex_unlock(&old->lock); |
659 | mutex_unlock(&old->lock); |
659 | } |
660 | } |
660 | 661 | ||
661 | /* |
662 | /* |
662 | * Second, prepare the new address space. |
663 | * Second, prepare the new address space. |
663 | */ |
664 | */ |
664 | mutex_lock_active(&new->lock); |
665 | mutex_lock_active(&new->lock); |
665 | if ((new->refcount++ == 0) && (new != AS_KERNEL)) { |
666 | if ((new->refcount++ == 0) && (new != AS_KERNEL)) { |
666 | if (new->asid != ASID_INVALID) |
667 | if (new->asid != ASID_INVALID) |
667 | list_remove(&new->inactive_as_with_asid_link); |
668 | list_remove(&new->inactive_as_with_asid_link); |
668 | else |
669 | else |
669 | needs_asid = true; /* defer call to asid_get() until new->lock is released */ |
670 | needs_asid = true; /* defer call to asid_get() until new->lock is released */ |
670 | } |
671 | } |
671 | SET_PTL0_ADDRESS(new->page_table); |
672 | SET_PTL0_ADDRESS(new->page_table); |
672 | mutex_unlock(&new->lock); |
673 | mutex_unlock(&new->lock); |
673 | 674 | ||
674 | if (needs_asid) { |
675 | if (needs_asid) { |
675 | /* |
676 | /* |
676 | * Allocation of new ASID was deferred |
677 | * Allocation of new ASID was deferred |
677 | * until now in order to avoid deadlock. |
678 | * until now in order to avoid deadlock. |
678 | */ |
679 | */ |
679 | asid_t asid; |
680 | asid_t asid; |
680 | 681 | ||
681 | asid = asid_get(); |
682 | asid = asid_get(); |
682 | mutex_lock_active(&new->lock); |
683 | mutex_lock_active(&new->lock); |
683 | new->asid = asid; |
684 | new->asid = asid; |
684 | mutex_unlock(&new->lock); |
685 | mutex_unlock(&new->lock); |
685 | } |
686 | } |
686 | spinlock_unlock(&as_lock); |
687 | spinlock_unlock(&as_lock); |
687 | interrupts_restore(ipl); |
688 | interrupts_restore(ipl); |
688 | 689 | ||
689 | /* |
690 | /* |
690 | * Perform architecture-specific steps. |
691 | * Perform architecture-specific steps. |
691 | * (e.g. write ASID to hardware register etc.) |
692 | * (e.g. write ASID to hardware register etc.) |
692 | */ |
693 | */ |
693 | as_install_arch(new); |
694 | as_install_arch(new); |
694 | 695 | ||
695 | AS = new; |
696 | AS = new; |
696 | } |
697 | } |
697 | 698 | ||
698 | /** Convert address space area flags to page flags. |
699 | /** Convert address space area flags to page flags. |
699 | * |
700 | * |
700 | * @param aflags Flags of some address space area. |
701 | * @param aflags Flags of some address space area. |
701 | * |
702 | * |
702 | * @return Flags to be passed to page_mapping_insert(). |
703 | * @return Flags to be passed to page_mapping_insert(). |
703 | */ |
704 | */ |
704 | int area_flags_to_page_flags(int aflags) |
705 | int area_flags_to_page_flags(int aflags) |
705 | { |
706 | { |
706 | int flags; |
707 | int flags; |
707 | 708 | ||
708 | flags = PAGE_USER | PAGE_PRESENT; |
709 | flags = PAGE_USER | PAGE_PRESENT; |
709 | 710 | ||
710 | if (aflags & AS_AREA_READ) |
711 | if (aflags & AS_AREA_READ) |
711 | flags |= PAGE_READ; |
712 | flags |= PAGE_READ; |
712 | 713 | ||
713 | if (aflags & AS_AREA_WRITE) |
714 | if (aflags & AS_AREA_WRITE) |
714 | flags |= PAGE_WRITE; |
715 | flags |= PAGE_WRITE; |
715 | 716 | ||
716 | if (aflags & AS_AREA_EXEC) |
717 | if (aflags & AS_AREA_EXEC) |
717 | flags |= PAGE_EXEC; |
718 | flags |= PAGE_EXEC; |
718 | 719 | ||
719 | if (!(aflags & AS_AREA_DEVICE)) |
720 | if (!(aflags & AS_AREA_DEVICE)) |
720 | flags |= PAGE_CACHEABLE; |
721 | flags |= PAGE_CACHEABLE; |
721 | 722 | ||
722 | return flags; |
723 | return flags; |
723 | } |
724 | } |
724 | 725 | ||
725 | /** Compute flags for virtual address translation subsytem. |
726 | /** Compute flags for virtual address translation subsytem. |
726 | * |
727 | * |
727 | * The address space area must be locked. |
728 | * The address space area must be locked. |
728 | * Interrupts must be disabled. |
729 | * Interrupts must be disabled. |
729 | * |
730 | * |
730 | * @param a Address space area. |
731 | * @param a Address space area. |
731 | * |
732 | * |
732 | * @return Flags to be used in page_mapping_insert(). |
733 | * @return Flags to be used in page_mapping_insert(). |
733 | */ |
734 | */ |
734 | int get_area_flags(as_area_t *a) |
735 | int get_area_flags(as_area_t *a) |
735 | { |
736 | { |
736 | return area_flags_to_page_flags(a->flags); |
737 | return area_flags_to_page_flags(a->flags); |
737 | } |
738 | } |
738 | 739 | ||
739 | /** Create page table. |
740 | /** Create page table. |
740 | * |
741 | * |
741 | * Depending on architecture, create either address space |
742 | * Depending on architecture, create either address space |
742 | * private or global page table. |
743 | * private or global page table. |
743 | * |
744 | * |
744 | * @param flags Flags saying whether the page table is for kernel address space. |
745 | * @param flags Flags saying whether the page table is for kernel address space. |
745 | * |
746 | * |
746 | * @return First entry of the page table. |
747 | * @return First entry of the page table. |
747 | */ |
748 | */ |
748 | pte_t *page_table_create(int flags) |
749 | pte_t *page_table_create(int flags) |
749 | { |
750 | { |
750 | ASSERT(as_operations); |
751 | ASSERT(as_operations); |
751 | ASSERT(as_operations->page_table_create); |
752 | ASSERT(as_operations->page_table_create); |
752 | 753 | ||
753 | return as_operations->page_table_create(flags); |
754 | return as_operations->page_table_create(flags); |
754 | } |
755 | } |
755 | 756 | ||
756 | /** Lock page table. |
757 | /** Lock page table. |
757 | * |
758 | * |
758 | * This function should be called before any page_mapping_insert(), |
759 | * This function should be called before any page_mapping_insert(), |
759 | * page_mapping_remove() and page_mapping_find(). |
760 | * page_mapping_remove() and page_mapping_find(). |
760 | * |
761 | * |
761 | * Locking order is such that address space areas must be locked |
762 | * Locking order is such that address space areas must be locked |
762 | * prior to this call. Address space can be locked prior to this |
763 | * prior to this call. Address space can be locked prior to this |
763 | * call in which case the lock argument is false. |
764 | * call in which case the lock argument is false. |
764 | * |
765 | * |
765 | * @param as Address space. |
766 | * @param as Address space. |
766 | * @param lock If false, do not attempt to lock as->lock. |
767 | * @param lock If false, do not attempt to lock as->lock. |
767 | */ |
768 | */ |
768 | void page_table_lock(as_t *as, bool lock) |
769 | void page_table_lock(as_t *as, bool lock) |
769 | { |
770 | { |
770 | ASSERT(as_operations); |
771 | ASSERT(as_operations); |
771 | ASSERT(as_operations->page_table_lock); |
772 | ASSERT(as_operations->page_table_lock); |
772 | 773 | ||
773 | as_operations->page_table_lock(as, lock); |
774 | as_operations->page_table_lock(as, lock); |
774 | } |
775 | } |
775 | 776 | ||
776 | /** Unlock page table. |
777 | /** Unlock page table. |
777 | * |
778 | * |
778 | * @param as Address space. |
779 | * @param as Address space. |
779 | * @param unlock If false, do not attempt to unlock as->lock. |
780 | * @param unlock If false, do not attempt to unlock as->lock. |
780 | */ |
781 | */ |
781 | void page_table_unlock(as_t *as, bool unlock) |
782 | void page_table_unlock(as_t *as, bool unlock) |
782 | { |
783 | { |
783 | ASSERT(as_operations); |
784 | ASSERT(as_operations); |
784 | ASSERT(as_operations->page_table_unlock); |
785 | ASSERT(as_operations->page_table_unlock); |
785 | 786 | ||
786 | as_operations->page_table_unlock(as, unlock); |
787 | as_operations->page_table_unlock(as, unlock); |
787 | } |
788 | } |
788 | 789 | ||
789 | 790 | ||
790 | /** Find address space area and lock it. |
791 | /** Find address space area and lock it. |
791 | * |
792 | * |
792 | * The address space must be locked and interrupts must be disabled. |
793 | * The address space must be locked and interrupts must be disabled. |
793 | * |
794 | * |
794 | * @param as Address space. |
795 | * @param as Address space. |
795 | * @param va Virtual address. |
796 | * @param va Virtual address. |
796 | * |
797 | * |
797 | * @return Locked address space area containing va on success or NULL on failure. |
798 | * @return Locked address space area containing va on success or NULL on failure. |
798 | */ |
799 | */ |
799 | as_area_t *find_area_and_lock(as_t *as, __address va) |
800 | as_area_t *find_area_and_lock(as_t *as, __address va) |
800 | { |
801 | { |
801 | as_area_t *a; |
802 | as_area_t *a; |
802 | btree_node_t *leaf, *lnode; |
803 | btree_node_t *leaf, *lnode; |
803 | int i; |
804 | int i; |
804 | 805 | ||
805 | a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf); |
806 | a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf); |
806 | if (a) { |
807 | if (a) { |
807 | /* va is the base address of an address space area */ |
808 | /* va is the base address of an address space area */ |
808 | mutex_lock(&a->lock); |
809 | mutex_lock(&a->lock); |
809 | return a; |
810 | return a; |
810 | } |
811 | } |
811 | 812 | ||
812 | /* |
813 | /* |
813 | * Search the leaf node and the righmost record of its left neighbour |
814 | * Search the leaf node and the righmost record of its left neighbour |
814 | * to find out whether this is a miss or va belongs to an address |
815 | * to find out whether this is a miss or va belongs to an address |
815 | * space area found there. |
816 | * space area found there. |
816 | */ |
817 | */ |
817 | 818 | ||
818 | /* First, search the leaf node itself. */ |
819 | /* First, search the leaf node itself. */ |
819 | for (i = 0; i < leaf->keys; i++) { |
820 | for (i = 0; i < leaf->keys; i++) { |
820 | a = (as_area_t *) leaf->value[i]; |
821 | a = (as_area_t *) leaf->value[i]; |
821 | mutex_lock(&a->lock); |
822 | mutex_lock(&a->lock); |
822 | if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) { |
823 | if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) { |
823 | return a; |
824 | return a; |
824 | } |
825 | } |
825 | mutex_unlock(&a->lock); |
826 | mutex_unlock(&a->lock); |
826 | } |
827 | } |
827 | 828 | ||
828 | /* |
829 | /* |
829 | * Second, locate the left neighbour and test its last record. |
830 | * Second, locate the left neighbour and test its last record. |
830 | * Because of its position in the B+tree, it must have base < va. |
831 | * Because of its position in the B+tree, it must have base < va. |
831 | */ |
832 | */ |
832 | if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
833 | if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
833 | a = (as_area_t *) lnode->value[lnode->keys - 1]; |
834 | a = (as_area_t *) lnode->value[lnode->keys - 1]; |
834 | mutex_lock(&a->lock); |
835 | mutex_lock(&a->lock); |
835 | if (va < a->base + a->pages * PAGE_SIZE) { |
836 | if (va < a->base + a->pages * PAGE_SIZE) { |
836 | return a; |
837 | return a; |
837 | } |
838 | } |
838 | mutex_unlock(&a->lock); |
839 | mutex_unlock(&a->lock); |
839 | } |
840 | } |
840 | 841 | ||
841 | return NULL; |
842 | return NULL; |
842 | } |
843 | } |
843 | 844 | ||
844 | /** Check area conflicts with other areas. |
845 | /** Check area conflicts with other areas. |
845 | * |
846 | * |
846 | * The address space must be locked and interrupts must be disabled. |
847 | * The address space must be locked and interrupts must be disabled. |
847 | * |
848 | * |
848 | * @param as Address space. |
849 | * @param as Address space. |
849 | * @param va Starting virtual address of the area being tested. |
850 | * @param va Starting virtual address of the area being tested. |
850 | * @param size Size of the area being tested. |
851 | * @param size Size of the area being tested. |
851 | * @param avoid_area Do not touch this area. |
852 | * @param avoid_area Do not touch this area. |
852 | * |
853 | * |
853 | * @return True if there is no conflict, false otherwise. |
854 | * @return True if there is no conflict, false otherwise. |
854 | */ |
855 | */ |
855 | bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area) |
856 | bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area) |
856 | { |
857 | { |
857 | as_area_t *a; |
858 | as_area_t *a; |
858 | btree_node_t *leaf, *node; |
859 | btree_node_t *leaf, *node; |
859 | int i; |
860 | int i; |
860 | 861 | ||
861 | /* |
862 | /* |
862 | * We don't want any area to have conflicts with NULL page. |
863 | * We don't want any area to have conflicts with NULL page. |
863 | */ |
864 | */ |
864 | if (overlaps(va, size, NULL, PAGE_SIZE)) |
865 | if (overlaps(va, size, NULL, PAGE_SIZE)) |
865 | return false; |
866 | return false; |
866 | 867 | ||
867 | /* |
868 | /* |
868 | * The leaf node is found in O(log n), where n is proportional to |
869 | * The leaf node is found in O(log n), where n is proportional to |
869 | * the number of address space areas belonging to as. |
870 | * the number of address space areas belonging to as. |
870 | * The check for conflicts is then attempted on the rightmost |
871 | * The check for conflicts is then attempted on the rightmost |
871 | * record in the left neighbour, the leftmost record in the right |
872 | * record in the left neighbour, the leftmost record in the right |
872 | * neighbour and all records in the leaf node itself. |
873 | * neighbour and all records in the leaf node itself. |
873 | */ |
874 | */ |
874 | 875 | ||
875 | if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) { |
876 | if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) { |
876 | if (a != avoid_area) |
877 | if (a != avoid_area) |
877 | return false; |
878 | return false; |
878 | } |
879 | } |
879 | 880 | ||
880 | /* First, check the two border cases. */ |
881 | /* First, check the two border cases. */ |
881 | if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
882 | if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
882 | a = (as_area_t *) node->value[node->keys - 1]; |
883 | a = (as_area_t *) node->value[node->keys - 1]; |
883 | mutex_lock(&a->lock); |
884 | mutex_lock(&a->lock); |
884 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
885 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
885 | mutex_unlock(&a->lock); |
886 | mutex_unlock(&a->lock); |
886 | return false; |
887 | return false; |
887 | } |
888 | } |
888 | mutex_unlock(&a->lock); |
889 | mutex_unlock(&a->lock); |
889 | } |
890 | } |
890 | if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) { |
891 | if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) { |
891 | a = (as_area_t *) node->value[0]; |
892 | a = (as_area_t *) node->value[0]; |
892 | mutex_lock(&a->lock); |
893 | mutex_lock(&a->lock); |
893 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
894 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
894 | mutex_unlock(&a->lock); |
895 | mutex_unlock(&a->lock); |
895 | return false; |
896 | return false; |
896 | } |
897 | } |
897 | mutex_unlock(&a->lock); |
898 | mutex_unlock(&a->lock); |
898 | } |
899 | } |
899 | 900 | ||
900 | /* Second, check the leaf node. */ |
901 | /* Second, check the leaf node. */ |
901 | for (i = 0; i < leaf->keys; i++) { |
902 | for (i = 0; i < leaf->keys; i++) { |
902 | a = (as_area_t *) leaf->value[i]; |
903 | a = (as_area_t *) leaf->value[i]; |
903 | 904 | ||
904 | if (a == avoid_area) |
905 | if (a == avoid_area) |
905 | continue; |
906 | continue; |
906 | 907 | ||
907 | mutex_lock(&a->lock); |
908 | mutex_lock(&a->lock); |
908 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
909 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
909 | mutex_unlock(&a->lock); |
910 | mutex_unlock(&a->lock); |
910 | return false; |
911 | return false; |
911 | } |
912 | } |
912 | mutex_unlock(&a->lock); |
913 | mutex_unlock(&a->lock); |
913 | } |
914 | } |
914 | 915 | ||
915 | /* |
916 | /* |
916 | * So far, the area does not conflict with other areas. |
917 | * So far, the area does not conflict with other areas. |
917 | * Check if it doesn't conflict with kernel address space. |
918 | * Check if it doesn't conflict with kernel address space. |
918 | */ |
919 | */ |
919 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) { |
920 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) { |
920 | return !overlaps(va, size, |
921 | return !overlaps(va, size, |
921 | KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START); |
922 | KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START); |
922 | } |
923 | } |
923 | 924 | ||
924 | return true; |
925 | return true; |
925 | } |
926 | } |
926 | 927 | ||
927 | /** Return size of the address space area with given base. */ |
928 | /** Return size of the address space area with given base. */ |
928 | size_t as_get_size(__address base) |
929 | size_t as_get_size(__address base) |
929 | { |
930 | { |
930 | ipl_t ipl; |
931 | ipl_t ipl; |
931 | as_area_t *src_area; |
932 | as_area_t *src_area; |
932 | size_t size; |
933 | size_t size; |
933 | 934 | ||
934 | ipl = interrupts_disable(); |
935 | ipl = interrupts_disable(); |
935 | src_area = find_area_and_lock(AS, base); |
936 | src_area = find_area_and_lock(AS, base); |
936 | if (src_area){ |
937 | if (src_area){ |
937 | size = src_area->pages * PAGE_SIZE; |
938 | size = src_area->pages * PAGE_SIZE; |
938 | mutex_unlock(&src_area->lock); |
939 | mutex_unlock(&src_area->lock); |
939 | } else { |
940 | } else { |
940 | size = 0; |
941 | size = 0; |
941 | } |
942 | } |
942 | interrupts_restore(ipl); |
943 | interrupts_restore(ipl); |
943 | return size; |
944 | return size; |
944 | } |
945 | } |
945 | 946 | ||
946 | /* |
947 | /* |
947 | * Address space related syscalls. |
948 | * Address space related syscalls. |
948 | */ |
949 | */ |
949 | 950 | ||
950 | /** Wrapper for as_area_create(). */ |
951 | /** Wrapper for as_area_create(). */ |
951 | __native sys_as_area_create(__address address, size_t size, int flags) |
952 | __native sys_as_area_create(__address address, size_t size, int flags) |
952 | { |
953 | { |
953 | if (as_area_create(AS, flags, size, address, AS_AREA_ATTR_NONE)) |
954 | if (as_area_create(AS, flags, size, address, AS_AREA_ATTR_NONE)) |
954 | return (__native) address; |
955 | return (__native) address; |
955 | else |
956 | else |
956 | return (__native) -1; |
957 | return (__native) -1; |
957 | } |
958 | } |
958 | 959 | ||
959 | /** Wrapper for as_area_resize. */ |
960 | /** Wrapper for as_area_resize. */ |
960 | __native sys_as_area_resize(__address address, size_t size, int flags) |
961 | __native sys_as_area_resize(__address address, size_t size, int flags) |
961 | { |
962 | { |
962 | return (__native) as_area_resize(AS, address, size, 0); |
963 | return (__native) as_area_resize(AS, address, size, 0); |
963 | } |
964 | } |
964 | 965 | ||
965 | /** Wrapper for as_area_destroy. */ |
966 | /** Wrapper for as_area_destroy. */ |
966 | __native sys_as_area_destroy(__address address) |
967 | __native sys_as_area_destroy(__address address) |
967 | { |
968 | { |
968 | return (__native) as_area_destroy(AS, address); |
969 | return (__native) as_area_destroy(AS, address); |
969 | } |
970 | } |
970 | 971 |