Rev 1383 | Rev 1403 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1383 | Rev 1387 | ||
---|---|---|---|
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 <align.h> |
|
71 | #include <arch/types.h> |
72 | #include <arch/types.h> |
72 | #include <typedefs.h> |
73 | #include <typedefs.h> |
73 | #include <syscall/copy.h> |
74 | #include <syscall/copy.h> |
74 | #include <arch/interrupt.h> |
75 | #include <arch/interrupt.h> |
75 | 76 | ||
76 | as_operations_t *as_operations = NULL; |
77 | as_operations_t *as_operations = NULL; |
77 | 78 | ||
78 | /** Address space lock. It protects inactive_as_with_asid_head. Must be acquired before as_t mutex. */ |
79 | /** Address space lock. It protects inactive_as_with_asid_head. Must be acquired before as_t mutex. */ |
79 | SPINLOCK_INITIALIZE(as_lock); |
80 | SPINLOCK_INITIALIZE(as_lock); |
80 | 81 | ||
81 | /** |
82 | /** |
82 | * This list contains address spaces that are not active on any |
83 | * This list contains address spaces that are not active on any |
83 | * processor and that have valid ASID. |
84 | * processor and that have valid ASID. |
84 | */ |
85 | */ |
85 | LIST_INITIALIZE(inactive_as_with_asid_head); |
86 | LIST_INITIALIZE(inactive_as_with_asid_head); |
86 | 87 | ||
87 | /** Kernel address space. */ |
88 | /** Kernel address space. */ |
88 | as_t *AS_KERNEL = NULL; |
89 | as_t *AS_KERNEL = NULL; |
89 | 90 | ||
90 | static int area_flags_to_page_flags(int aflags); |
91 | static int area_flags_to_page_flags(int aflags); |
91 | static int get_area_flags(as_area_t *a); |
92 | static int get_area_flags(as_area_t *a); |
92 | static as_area_t *find_area_and_lock(as_t *as, __address va); |
93 | 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); |
94 | static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area); |
- | 95 | int used_space_insert(as_area_t *a, __address page, count_t count); |
|
- | 96 | int used_space_remove(as_area_t *a, __address page, count_t count); |
|
94 | 97 | ||
95 | /** Initialize address space subsystem. */ |
98 | /** Initialize address space subsystem. */ |
96 | void as_init(void) |
99 | void as_init(void) |
97 | { |
100 | { |
98 | as_arch_init(); |
101 | as_arch_init(); |
99 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
102 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
100 | if (!AS_KERNEL) |
103 | if (!AS_KERNEL) |
101 | panic("can't create kernel address space\n"); |
104 | panic("can't create kernel address space\n"); |
102 | 105 | ||
103 | } |
106 | } |
104 | 107 | ||
105 | /** Create address space. |
108 | /** Create address space. |
106 | * |
109 | * |
107 | * @param flags Flags that influence way in wich the address space is created. |
110 | * @param flags Flags that influence way in wich the address space is created. |
108 | */ |
111 | */ |
109 | as_t *as_create(int flags) |
112 | as_t *as_create(int flags) |
110 | { |
113 | { |
111 | as_t *as; |
114 | as_t *as; |
112 | 115 | ||
113 | as = (as_t *) malloc(sizeof(as_t), 0); |
116 | as = (as_t *) malloc(sizeof(as_t), 0); |
114 | link_initialize(&as->inactive_as_with_asid_link); |
117 | link_initialize(&as->inactive_as_with_asid_link); |
115 | mutex_initialize(&as->lock); |
118 | mutex_initialize(&as->lock); |
116 | btree_create(&as->as_area_btree); |
119 | btree_create(&as->as_area_btree); |
117 | 120 | ||
118 | if (flags & FLAG_AS_KERNEL) |
121 | if (flags & FLAG_AS_KERNEL) |
119 | as->asid = ASID_KERNEL; |
122 | as->asid = ASID_KERNEL; |
120 | else |
123 | else |
121 | as->asid = ASID_INVALID; |
124 | as->asid = ASID_INVALID; |
122 | 125 | ||
123 | as->refcount = 0; |
126 | as->refcount = 0; |
124 | as->page_table = page_table_create(flags); |
127 | as->page_table = page_table_create(flags); |
125 | 128 | ||
126 | return as; |
129 | return as; |
127 | } |
130 | } |
128 | 131 | ||
129 | /** Free Adress space */ |
132 | /** Free Adress space */ |
130 | void as_free(as_t *as) |
133 | void as_free(as_t *as) |
131 | { |
134 | { |
132 | ASSERT(as->refcount == 0); |
135 | ASSERT(as->refcount == 0); |
133 | 136 | ||
134 | /* TODO: free as_areas and other resources held by as */ |
137 | /* TODO: free as_areas and other resources held by as */ |
135 | /* TODO: free page table */ |
138 | /* TODO: free page table */ |
136 | free(as); |
139 | free(as); |
137 | } |
140 | } |
138 | 141 | ||
139 | /** Create address space area of common attributes. |
142 | /** Create address space area of common attributes. |
140 | * |
143 | * |
141 | * The created address space area is added to the target address space. |
144 | * The created address space area is added to the target address space. |
142 | * |
145 | * |
143 | * @param as Target address space. |
146 | * @param as Target address space. |
144 | * @param flags Flags of the area memory. |
147 | * @param flags Flags of the area memory. |
145 | * @param size Size of area. |
148 | * @param size Size of area. |
146 | * @param base Base address of area. |
149 | * @param base Base address of area. |
147 | * @param attrs Attributes of the area. |
150 | * @param attrs Attributes of the area. |
148 | * |
151 | * |
149 | * @return Address space area on success or NULL on failure. |
152 | * @return Address space area on success or NULL on failure. |
150 | */ |
153 | */ |
151 | as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base, int attrs) |
154 | as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base, int attrs) |
152 | { |
155 | { |
153 | ipl_t ipl; |
156 | ipl_t ipl; |
154 | as_area_t *a; |
157 | as_area_t *a; |
155 | 158 | ||
156 | if (base % PAGE_SIZE) |
159 | if (base % PAGE_SIZE) |
157 | return NULL; |
160 | return NULL; |
158 | 161 | ||
159 | if (!size) |
162 | if (!size) |
160 | return NULL; |
163 | return NULL; |
161 | 164 | ||
162 | /* Writeable executable areas are not supported. */ |
165 | /* Writeable executable areas are not supported. */ |
163 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE)) |
166 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE)) |
164 | return NULL; |
167 | return NULL; |
165 | 168 | ||
166 | ipl = interrupts_disable(); |
169 | ipl = interrupts_disable(); |
167 | mutex_lock(&as->lock); |
170 | mutex_lock(&as->lock); |
168 | 171 | ||
169 | if (!check_area_conflicts(as, base, size, NULL)) { |
172 | if (!check_area_conflicts(as, base, size, NULL)) { |
170 | mutex_unlock(&as->lock); |
173 | mutex_unlock(&as->lock); |
171 | interrupts_restore(ipl); |
174 | interrupts_restore(ipl); |
172 | return NULL; |
175 | return NULL; |
173 | } |
176 | } |
174 | 177 | ||
175 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
178 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
176 | 179 | ||
177 | mutex_initialize(&a->lock); |
180 | mutex_initialize(&a->lock); |
178 | 181 | ||
179 | a->flags = flags; |
182 | a->flags = flags; |
180 | a->attributes = attrs; |
183 | a->attributes = attrs; |
181 | a->pages = SIZE2FRAMES(size); |
184 | a->pages = SIZE2FRAMES(size); |
182 | a->base = base; |
185 | a->base = base; |
- | 186 | btree_create(&a->used_space); |
|
183 | 187 | ||
184 | btree_insert(&as->as_area_btree, base, (void *) a, NULL); |
188 | btree_insert(&as->as_area_btree, base, (void *) a, NULL); |
185 | 189 | ||
186 | mutex_unlock(&as->lock); |
190 | mutex_unlock(&as->lock); |
187 | interrupts_restore(ipl); |
191 | interrupts_restore(ipl); |
188 | 192 | ||
189 | return a; |
193 | return a; |
190 | } |
194 | } |
191 | 195 | ||
192 | /** Find address space area and change it. |
196 | /** Find address space area and change it. |
193 | * |
197 | * |
194 | * @param as Address space. |
198 | * @param as Address space. |
195 | * @param address Virtual address belonging to the area to be changed. Must be page-aligned. |
199 | * @param address Virtual address belonging to the area to be changed. Must be page-aligned. |
196 | * @param size New size of the virtual memory block starting at address. |
200 | * @param size New size of the virtual memory block starting at address. |
197 | * @param flags Flags influencing the remap operation. Currently unused. |
201 | * @param flags Flags influencing the remap operation. Currently unused. |
198 | * |
202 | * |
199 | * @return Zero on success or a value from @ref errno.h otherwise. |
203 | * @return Zero on success or a value from @ref errno.h otherwise. |
200 | */ |
204 | */ |
201 | int as_area_resize(as_t *as, __address address, size_t size, int flags) |
205 | int as_area_resize(as_t *as, __address address, size_t size, int flags) |
202 | { |
206 | { |
203 | as_area_t *area; |
207 | as_area_t *area; |
204 | ipl_t ipl; |
208 | ipl_t ipl; |
205 | size_t pages; |
209 | size_t pages; |
206 | 210 | ||
207 | ipl = interrupts_disable(); |
211 | ipl = interrupts_disable(); |
208 | mutex_lock(&as->lock); |
212 | mutex_lock(&as->lock); |
209 | 213 | ||
210 | /* |
214 | /* |
211 | * Locate the area. |
215 | * Locate the area. |
212 | */ |
216 | */ |
213 | area = find_area_and_lock(as, address); |
217 | area = find_area_and_lock(as, address); |
214 | if (!area) { |
218 | if (!area) { |
215 | mutex_unlock(&as->lock); |
219 | mutex_unlock(&as->lock); |
216 | interrupts_restore(ipl); |
220 | interrupts_restore(ipl); |
217 | return ENOENT; |
221 | return ENOENT; |
218 | } |
222 | } |
219 | 223 | ||
220 | if (area->flags & AS_AREA_DEVICE) { |
224 | if (area->flags & AS_AREA_DEVICE) { |
221 | /* |
225 | /* |
222 | * Remapping of address space areas associated |
226 | * Remapping of address space areas associated |
223 | * with memory mapped devices is not supported. |
227 | * with memory mapped devices is not supported. |
224 | */ |
228 | */ |
225 | mutex_unlock(&area->lock); |
229 | mutex_unlock(&area->lock); |
226 | mutex_unlock(&as->lock); |
230 | mutex_unlock(&as->lock); |
227 | interrupts_restore(ipl); |
231 | interrupts_restore(ipl); |
228 | return ENOTSUP; |
232 | return ENOTSUP; |
229 | } |
233 | } |
230 | 234 | ||
231 | pages = SIZE2FRAMES((address - area->base) + size); |
235 | pages = SIZE2FRAMES((address - area->base) + size); |
232 | if (!pages) { |
236 | if (!pages) { |
233 | /* |
237 | /* |
234 | * Zero size address space areas are not allowed. |
238 | * Zero size address space areas are not allowed. |
235 | */ |
239 | */ |
236 | mutex_unlock(&area->lock); |
240 | mutex_unlock(&area->lock); |
237 | mutex_unlock(&as->lock); |
241 | mutex_unlock(&as->lock); |
238 | interrupts_restore(ipl); |
242 | interrupts_restore(ipl); |
239 | return EPERM; |
243 | return EPERM; |
240 | } |
244 | } |
241 | 245 | ||
242 | if (pages < area->pages) { |
246 | if (pages < area->pages) { |
243 | int i; |
247 | int i; |
244 | 248 | ||
245 | /* |
249 | /* |
246 | * Shrinking the area. |
250 | * Shrinking the area. |
247 | * No need to check for overlaps. |
251 | * No need to check for overlaps. |
248 | */ |
252 | */ |
249 | for (i = pages; i < area->pages; i++) { |
253 | for (i = pages; i < area->pages; i++) { |
250 | pte_t *pte; |
254 | pte_t *pte; |
251 | 255 | ||
252 | /* |
256 | /* |
253 | * Releasing physical memory. |
257 | * Releasing physical memory. |
254 | * This depends on the fact that the memory was allocated using frame_alloc(). |
258 | * This depends on the fact that the memory was allocated using frame_alloc(). |
255 | */ |
259 | */ |
256 | page_table_lock(as, false); |
260 | page_table_lock(as, false); |
257 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
261 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
258 | if (pte && PTE_VALID(pte)) { |
262 | if (pte && PTE_VALID(pte)) { |
259 | __address frame; |
263 | __address frame; |
260 | 264 | ||
261 | ASSERT(PTE_PRESENT(pte)); |
265 | ASSERT(PTE_PRESENT(pte)); |
262 | frame = PTE_GET_FRAME(pte); |
266 | frame = PTE_GET_FRAME(pte); |
263 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
267 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
264 | page_table_unlock(as, false); |
268 | page_table_unlock(as, false); |
265 | 269 | ||
266 | frame_free(ADDR2PFN(frame)); |
270 | frame_free(ADDR2PFN(frame)); |
267 | } else { |
271 | } else { |
268 | page_table_unlock(as, false); |
272 | page_table_unlock(as, false); |
269 | } |
273 | } |
270 | } |
274 | } |
271 | /* |
275 | /* |
272 | * Invalidate TLB's. |
276 | * Invalidate TLB's. |
273 | */ |
277 | */ |
274 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages); |
278 | tlb_shootdown_start(TLB_INVL_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); |
279 | tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages); |
276 | tlb_shootdown_finalize(); |
280 | tlb_shootdown_finalize(); |
277 | } else { |
281 | } else { |
278 | /* |
282 | /* |
279 | * Growing the area. |
283 | * Growing the area. |
280 | * Check for overlaps with other address space areas. |
284 | * Check for overlaps with other address space areas. |
281 | */ |
285 | */ |
282 | if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) { |
286 | if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) { |
283 | mutex_unlock(&area->lock); |
287 | mutex_unlock(&area->lock); |
284 | mutex_unlock(&as->lock); |
288 | mutex_unlock(&as->lock); |
285 | interrupts_restore(ipl); |
289 | interrupts_restore(ipl); |
286 | return EADDRNOTAVAIL; |
290 | return EADDRNOTAVAIL; |
287 | } |
291 | } |
288 | } |
292 | } |
289 | 293 | ||
290 | area->pages = pages; |
294 | area->pages = pages; |
291 | 295 | ||
292 | mutex_unlock(&area->lock); |
296 | mutex_unlock(&area->lock); |
293 | mutex_unlock(&as->lock); |
297 | mutex_unlock(&as->lock); |
294 | interrupts_restore(ipl); |
298 | interrupts_restore(ipl); |
295 | 299 | ||
296 | return 0; |
300 | return 0; |
297 | } |
301 | } |
298 | 302 | ||
299 | /** Destroy address space area. |
303 | /** Destroy address space area. |
300 | * |
304 | * |
301 | * @param as Address space. |
305 | * @param as Address space. |
302 | * @param address Address withing the area to be deleted. |
306 | * @param address Address withing the area to be deleted. |
303 | * |
307 | * |
304 | * @return Zero on success or a value from @ref errno.h on failure. |
308 | * @return Zero on success or a value from @ref errno.h on failure. |
305 | */ |
309 | */ |
306 | int as_area_destroy(as_t *as, __address address) |
310 | int as_area_destroy(as_t *as, __address address) |
307 | { |
311 | { |
308 | as_area_t *area; |
312 | as_area_t *area; |
309 | __address base; |
313 | __address base; |
310 | ipl_t ipl; |
314 | ipl_t ipl; |
311 | int i; |
315 | int i; |
312 | 316 | ||
313 | ipl = interrupts_disable(); |
317 | ipl = interrupts_disable(); |
314 | mutex_lock(&as->lock); |
318 | mutex_lock(&as->lock); |
315 | 319 | ||
316 | area = find_area_and_lock(as, address); |
320 | area = find_area_and_lock(as, address); |
317 | if (!area) { |
321 | if (!area) { |
318 | mutex_unlock(&as->lock); |
322 | mutex_unlock(&as->lock); |
319 | interrupts_restore(ipl); |
323 | interrupts_restore(ipl); |
320 | return ENOENT; |
324 | return ENOENT; |
321 | } |
325 | } |
322 | 326 | ||
323 | base = area->base; |
327 | base = area->base; |
324 | for (i = 0; i < area->pages; i++) { |
328 | for (i = 0; i < area->pages; i++) { |
325 | pte_t *pte; |
329 | pte_t *pte; |
326 | 330 | ||
327 | /* |
331 | /* |
328 | * Releasing physical memory. |
332 | * Releasing physical memory. |
329 | * Areas mapping memory-mapped devices are treated differently than |
333 | * Areas mapping memory-mapped devices are treated differently than |
330 | * areas backing frame_alloc()'ed memory. |
334 | * areas backing frame_alloc()'ed memory. |
331 | */ |
335 | */ |
332 | page_table_lock(as, false); |
336 | page_table_lock(as, false); |
333 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
337 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
334 | if (pte && PTE_VALID(pte)) { |
338 | if (pte && PTE_VALID(pte)) { |
335 | ASSERT(PTE_PRESENT(pte)); |
339 | ASSERT(PTE_PRESENT(pte)); |
336 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
340 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
337 | if (area->flags & AS_AREA_DEVICE) { |
341 | if (area->flags & AS_AREA_DEVICE) { |
338 | __address frame; |
342 | __address frame; |
339 | frame = PTE_GET_FRAME(pte); |
343 | frame = PTE_GET_FRAME(pte); |
340 | frame_free(ADDR2PFN(frame)); |
344 | frame_free(ADDR2PFN(frame)); |
341 | } |
345 | } |
342 | page_table_unlock(as, false); |
346 | page_table_unlock(as, false); |
343 | } else { |
347 | } else { |
344 | page_table_unlock(as, false); |
348 | page_table_unlock(as, false); |
345 | } |
349 | } |
346 | } |
350 | } |
347 | /* |
351 | /* |
348 | * Invalidate TLB's. |
352 | * Invalidate TLB's. |
349 | */ |
353 | */ |
350 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base, area->pages); |
354 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base, area->pages); |
351 | tlb_invalidate_pages(AS->asid, area->base, area->pages); |
355 | tlb_invalidate_pages(AS->asid, area->base, area->pages); |
352 | tlb_shootdown_finalize(); |
356 | tlb_shootdown_finalize(); |
353 | 357 | ||
354 | area->attributes |= AS_AREA_ATTR_PARTIAL; |
358 | area->attributes |= AS_AREA_ATTR_PARTIAL; |
355 | mutex_unlock(&area->lock); |
359 | mutex_unlock(&area->lock); |
356 | 360 | ||
357 | /* |
361 | /* |
358 | * Remove the empty area from address space. |
362 | * Remove the empty area from address space. |
359 | */ |
363 | */ |
360 | btree_remove(&AS->as_area_btree, base, NULL); |
364 | btree_remove(&AS->as_area_btree, base, NULL); |
361 | 365 | ||
362 | free(area); |
366 | free(area); |
363 | 367 | ||
364 | mutex_unlock(&AS->lock); |
368 | mutex_unlock(&AS->lock); |
365 | interrupts_restore(ipl); |
369 | interrupts_restore(ipl); |
366 | return 0; |
370 | return 0; |
367 | } |
371 | } |
368 | 372 | ||
369 | /** Steal address space area from another task. |
373 | /** Steal address space area from another task. |
370 | * |
374 | * |
371 | * Address space area is stolen from another task |
375 | * Address space area is stolen from another task |
372 | * Moreover, any existing mapping |
376 | * Moreover, any existing mapping |
373 | * is copied as well, providing thus a mechanism |
377 | * is copied as well, providing thus a mechanism |
374 | * for sharing group of pages. The source address |
378 | * for sharing group of pages. The source address |
375 | * space area and any associated mapping is preserved. |
379 | * space area and any associated mapping is preserved. |
376 | * |
380 | * |
377 | * @param src_task Pointer of source task |
381 | * @param src_task Pointer of source task |
378 | * @param src_base Base address of the source address space area. |
382 | * @param src_base Base address of the source address space area. |
379 | * @param acc_size Expected size of the source area |
383 | * @param acc_size Expected size of the source area |
380 | * @param dst_base Target base address |
384 | * @param dst_base Target base address |
381 | * |
385 | * |
382 | * @return Zero on success or ENOENT if there is no such task or |
386 | * @return Zero on success or ENOENT if there is no such task or |
383 | * if there is no such address space area, |
387 | * if there is no such address space area, |
384 | * EPERM if there was a problem in accepting the area or |
388 | * EPERM if there was a problem in accepting the area or |
385 | * ENOMEM if there was a problem in allocating destination |
389 | * ENOMEM if there was a problem in allocating destination |
386 | * address space area. |
390 | * address space area. |
387 | */ |
391 | */ |
388 | int as_area_steal(task_t *src_task, __address src_base, size_t acc_size, |
392 | int as_area_steal(task_t *src_task, __address src_base, size_t acc_size, |
389 | __address dst_base) |
393 | __address dst_base) |
390 | { |
394 | { |
391 | ipl_t ipl; |
395 | ipl_t ipl; |
392 | count_t i; |
396 | count_t i; |
393 | as_t *src_as; |
397 | as_t *src_as; |
394 | int src_flags; |
398 | int src_flags; |
395 | size_t src_size; |
399 | size_t src_size; |
396 | as_area_t *src_area, *dst_area; |
400 | as_area_t *src_area, *dst_area; |
397 | 401 | ||
398 | ipl = interrupts_disable(); |
402 | ipl = interrupts_disable(); |
399 | spinlock_lock(&src_task->lock); |
403 | spinlock_lock(&src_task->lock); |
400 | src_as = src_task->as; |
404 | src_as = src_task->as; |
401 | 405 | ||
402 | mutex_lock(&src_as->lock); |
406 | mutex_lock(&src_as->lock); |
403 | src_area = find_area_and_lock(src_as, src_base); |
407 | src_area = find_area_and_lock(src_as, src_base); |
404 | if (!src_area) { |
408 | if (!src_area) { |
405 | /* |
409 | /* |
406 | * Could not find the source address space area. |
410 | * Could not find the source address space area. |
407 | */ |
411 | */ |
408 | spinlock_unlock(&src_task->lock); |
412 | spinlock_unlock(&src_task->lock); |
409 | mutex_unlock(&src_as->lock); |
413 | mutex_unlock(&src_as->lock); |
410 | interrupts_restore(ipl); |
414 | interrupts_restore(ipl); |
411 | return ENOENT; |
415 | return ENOENT; |
412 | } |
416 | } |
413 | src_size = src_area->pages * PAGE_SIZE; |
417 | src_size = src_area->pages * PAGE_SIZE; |
414 | src_flags = src_area->flags; |
418 | src_flags = src_area->flags; |
415 | mutex_unlock(&src_area->lock); |
419 | mutex_unlock(&src_area->lock); |
416 | mutex_unlock(&src_as->lock); |
420 | mutex_unlock(&src_as->lock); |
417 | 421 | ||
418 | 422 | ||
419 | if (src_size != acc_size) { |
423 | if (src_size != acc_size) { |
420 | spinlock_unlock(&src_task->lock); |
424 | spinlock_unlock(&src_task->lock); |
421 | interrupts_restore(ipl); |
425 | interrupts_restore(ipl); |
422 | return EPERM; |
426 | return EPERM; |
423 | } |
427 | } |
424 | /* |
428 | /* |
425 | * Create copy of the source address space area. |
429 | * Create copy of the source address space area. |
426 | * The destination area is created with AS_AREA_ATTR_PARTIAL |
430 | * The destination area is created with AS_AREA_ATTR_PARTIAL |
427 | * attribute set which prevents race condition with |
431 | * attribute set which prevents race condition with |
428 | * preliminary as_page_fault() calls. |
432 | * preliminary as_page_fault() calls. |
429 | */ |
433 | */ |
430 | dst_area = as_area_create(AS, src_flags, src_size, dst_base, AS_AREA_ATTR_PARTIAL); |
434 | dst_area = as_area_create(AS, src_flags, src_size, dst_base, AS_AREA_ATTR_PARTIAL); |
431 | if (!dst_area) { |
435 | if (!dst_area) { |
432 | /* |
436 | /* |
433 | * Destination address space area could not be created. |
437 | * Destination address space area could not be created. |
434 | */ |
438 | */ |
435 | spinlock_unlock(&src_task->lock); |
439 | spinlock_unlock(&src_task->lock); |
436 | interrupts_restore(ipl); |
440 | interrupts_restore(ipl); |
437 | return ENOMEM; |
441 | return ENOMEM; |
438 | } |
442 | } |
439 | 443 | ||
440 | spinlock_unlock(&src_task->lock); |
444 | spinlock_unlock(&src_task->lock); |
441 | 445 | ||
442 | /* |
446 | /* |
443 | * Avoid deadlock by first locking the address space with lower address. |
447 | * Avoid deadlock by first locking the address space with lower address. |
444 | */ |
448 | */ |
445 | if (AS < src_as) { |
449 | if (AS < src_as) { |
446 | mutex_lock(&AS->lock); |
450 | mutex_lock(&AS->lock); |
447 | mutex_lock(&src_as->lock); |
451 | mutex_lock(&src_as->lock); |
448 | } else { |
452 | } else { |
449 | mutex_lock(&AS->lock); |
453 | mutex_lock(&AS->lock); |
450 | mutex_lock(&src_as->lock); |
454 | mutex_lock(&src_as->lock); |
451 | } |
455 | } |
452 | 456 | ||
453 | for (i = 0; i < SIZE2FRAMES(src_size); i++) { |
457 | for (i = 0; i < SIZE2FRAMES(src_size); i++) { |
454 | pte_t *pte; |
458 | pte_t *pte; |
455 | __address frame; |
459 | __address frame; |
456 | 460 | ||
457 | page_table_lock(src_as, false); |
461 | page_table_lock(src_as, false); |
458 | pte = page_mapping_find(src_as, src_base + i*PAGE_SIZE); |
462 | pte = page_mapping_find(src_as, src_base + i*PAGE_SIZE); |
459 | if (pte && PTE_VALID(pte)) { |
463 | if (pte && PTE_VALID(pte)) { |
460 | ASSERT(PTE_PRESENT(pte)); |
464 | ASSERT(PTE_PRESENT(pte)); |
461 | frame = PTE_GET_FRAME(pte); |
465 | frame = PTE_GET_FRAME(pte); |
462 | if (!(src_flags & AS_AREA_DEVICE)) |
466 | if (!(src_flags & AS_AREA_DEVICE)) |
463 | frame_reference_add(ADDR2PFN(frame)); |
467 | frame_reference_add(ADDR2PFN(frame)); |
464 | page_table_unlock(src_as, false); |
468 | page_table_unlock(src_as, false); |
465 | } else { |
469 | } else { |
466 | page_table_unlock(src_as, false); |
470 | page_table_unlock(src_as, false); |
467 | continue; |
471 | continue; |
468 | } |
472 | } |
469 | 473 | ||
470 | page_table_lock(AS, false); |
474 | page_table_lock(AS, false); |
471 | page_mapping_insert(AS, dst_base + i*PAGE_SIZE, frame, area_flags_to_page_flags(src_flags)); |
475 | page_mapping_insert(AS, dst_base + i*PAGE_SIZE, frame, area_flags_to_page_flags(src_flags)); |
472 | page_table_unlock(AS, false); |
476 | page_table_unlock(AS, false); |
473 | } |
477 | } |
474 | 478 | ||
475 | /* |
479 | /* |
476 | * Now the destination address space area has been |
480 | * Now the destination address space area has been |
477 | * fully initialized. Clear the AS_AREA_ATTR_PARTIAL |
481 | * fully initialized. Clear the AS_AREA_ATTR_PARTIAL |
478 | * attribute. |
482 | * attribute. |
479 | */ |
483 | */ |
480 | mutex_lock(&dst_area->lock); |
484 | mutex_lock(&dst_area->lock); |
481 | dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL; |
485 | dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL; |
482 | mutex_unlock(&dst_area->lock); |
486 | mutex_unlock(&dst_area->lock); |
483 | 487 | ||
484 | mutex_unlock(&AS->lock); |
488 | mutex_unlock(&AS->lock); |
485 | mutex_unlock(&src_as->lock); |
489 | mutex_unlock(&src_as->lock); |
486 | interrupts_restore(ipl); |
490 | interrupts_restore(ipl); |
487 | 491 | ||
488 | return 0; |
492 | return 0; |
489 | } |
493 | } |
490 | 494 | ||
491 | /** Initialize mapping for one page of address space. |
495 | /** Initialize mapping for one page of address space. |
492 | * |
496 | * |
493 | * This functions maps 'page' to 'frame' according |
497 | * This functions maps 'page' to 'frame' according |
494 | * to attributes of the address space area to |
498 | * to attributes of the address space area to |
495 | * wich 'page' belongs. |
499 | * wich 'page' belongs. |
496 | * |
500 | * |
497 | * @param as Target address space. |
501 | * @param as Target address space. |
498 | * @param page Virtual page within the area. |
502 | * @param page Virtual page within the area. |
499 | * @param frame Physical frame to which page will be mapped. |
503 | * @param frame Physical frame to which page will be mapped. |
500 | */ |
504 | */ |
501 | void as_set_mapping(as_t *as, __address page, __address frame) |
505 | void as_set_mapping(as_t *as, __address page, __address frame) |
502 | { |
506 | { |
503 | as_area_t *area; |
507 | as_area_t *area; |
504 | ipl_t ipl; |
508 | ipl_t ipl; |
505 | 509 | ||
506 | ipl = interrupts_disable(); |
510 | ipl = interrupts_disable(); |
507 | page_table_lock(as, true); |
511 | page_table_lock(as, true); |
508 | 512 | ||
509 | area = find_area_and_lock(as, page); |
513 | area = find_area_and_lock(as, page); |
510 | if (!area) { |
514 | if (!area) { |
511 | panic("page not part of any as_area\n"); |
515 | panic("page not part of any as_area\n"); |
512 | } |
516 | } |
513 | 517 | ||
514 | page_mapping_insert(as, page, frame, get_area_flags(area)); |
518 | page_mapping_insert(as, page, frame, get_area_flags(area)); |
515 | 519 | ||
516 | mutex_unlock(&area->lock); |
520 | mutex_unlock(&area->lock); |
517 | page_table_unlock(as, true); |
521 | page_table_unlock(as, true); |
518 | interrupts_restore(ipl); |
522 | interrupts_restore(ipl); |
519 | } |
523 | } |
520 | 524 | ||
521 | /** Handle page fault within the current address space. |
525 | /** Handle page fault within the current address space. |
522 | * |
526 | * |
523 | * This is the high-level page fault handler. |
527 | * This is the high-level page fault handler. |
524 | * Interrupts are assumed disabled. |
528 | * Interrupts are assumed disabled. |
525 | * |
529 | * |
526 | * @param page Faulting page. |
530 | * @param page Faulting page. |
527 | * @param istate Pointer to interrupted state. |
531 | * @param istate Pointer to interrupted state. |
528 | * |
532 | * |
529 | * @return 0 on page fault, 1 on success or 2 if the fault was caused by copy_to_uspace() or copy_from_uspace(). |
533 | * @return 0 on page fault, 1 on success or 2 if the fault was caused by copy_to_uspace() or copy_from_uspace(). |
530 | */ |
534 | */ |
531 | int as_page_fault(__address page, istate_t *istate) |
535 | int as_page_fault(__address page, istate_t *istate) |
532 | { |
536 | { |
533 | pte_t *pte; |
537 | pte_t *pte; |
534 | as_area_t *area; |
538 | as_area_t *area; |
535 | __address frame; |
539 | __address frame; |
536 | 540 | ||
537 | if (!THREAD) |
541 | if (!THREAD) |
538 | return 0; |
542 | return 0; |
539 | 543 | ||
540 | ASSERT(AS); |
544 | ASSERT(AS); |
541 | 545 | ||
542 | mutex_lock(&AS->lock); |
546 | mutex_lock(&AS->lock); |
543 | area = find_area_and_lock(AS, page); |
547 | area = find_area_and_lock(AS, page); |
544 | if (!area) { |
548 | if (!area) { |
545 | /* |
549 | /* |
546 | * No area contained mapping for 'page'. |
550 | * No area contained mapping for 'page'. |
547 | * Signal page fault to low-level handler. |
551 | * Signal page fault to low-level handler. |
548 | */ |
552 | */ |
549 | mutex_unlock(&AS->lock); |
553 | mutex_unlock(&AS->lock); |
550 | goto page_fault; |
554 | goto page_fault; |
551 | } |
555 | } |
552 | 556 | ||
553 | if (area->attributes & AS_AREA_ATTR_PARTIAL) { |
557 | if (area->attributes & AS_AREA_ATTR_PARTIAL) { |
554 | /* |
558 | /* |
555 | * The address space area is not fully initialized. |
559 | * The address space area is not fully initialized. |
556 | * Avoid possible race by returning error. |
560 | * Avoid possible race by returning error. |
557 | */ |
561 | */ |
558 | mutex_unlock(&area->lock); |
562 | mutex_unlock(&area->lock); |
559 | mutex_unlock(&AS->lock); |
563 | mutex_unlock(&AS->lock); |
560 | goto page_fault; |
564 | goto page_fault; |
561 | } |
565 | } |
562 | 566 | ||
563 | ASSERT(!(area->flags & AS_AREA_DEVICE)); |
567 | ASSERT(!(area->flags & AS_AREA_DEVICE)); |
564 | 568 | ||
565 | page_table_lock(AS, false); |
569 | page_table_lock(AS, false); |
566 | 570 | ||
567 | /* |
571 | /* |
568 | * To avoid race condition between two page faults |
572 | * To avoid race condition between two page faults |
569 | * on the same address, we need to make sure |
573 | * on the same address, we need to make sure |
570 | * the mapping has not been already inserted. |
574 | * the mapping has not been already inserted. |
571 | */ |
575 | */ |
572 | if ((pte = page_mapping_find(AS, page))) { |
576 | if ((pte = page_mapping_find(AS, page))) { |
573 | if (PTE_PRESENT(pte)) { |
577 | if (PTE_PRESENT(pte)) { |
574 | page_table_unlock(AS, false); |
578 | page_table_unlock(AS, false); |
575 | mutex_unlock(&area->lock); |
579 | mutex_unlock(&area->lock); |
576 | mutex_unlock(&AS->lock); |
580 | mutex_unlock(&AS->lock); |
577 | return 1; |
581 | return 1; |
578 | } |
582 | } |
579 | } |
583 | } |
580 | 584 | ||
581 | /* |
585 | /* |
582 | * In general, there can be several reasons that |
586 | * In general, there can be several reasons that |
583 | * can have caused this fault. |
587 | * can have caused this fault. |
584 | * |
588 | * |
585 | * - non-existent mapping: the area is a scratch |
589 | * - non-existent mapping: the area is a scratch |
586 | * area (e.g. stack) and so far has not been |
590 | * area (e.g. stack) and so far has not been |
587 | * allocated a frame for the faulting page |
591 | * allocated a frame for the faulting page |
588 | * |
592 | * |
589 | * - non-present mapping: another possibility, |
593 | * - non-present mapping: another possibility, |
590 | * currently not implemented, would be frame |
594 | * currently not implemented, would be frame |
591 | * reuse; when this becomes a possibility, |
595 | * reuse; when this becomes a possibility, |
592 | * do not forget to distinguish between |
596 | * do not forget to distinguish between |
593 | * the different causes |
597 | * the different causes |
594 | */ |
598 | */ |
595 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
599 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
596 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
600 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
597 | 601 | ||
598 | /* |
602 | /* |
599 | * Map 'page' to 'frame'. |
603 | * Map 'page' to 'frame'. |
600 | * Note that TLB shootdown is not attempted as only new information is being |
604 | * Note that TLB shootdown is not attempted as only new information is being |
601 | * inserted into page tables. |
605 | * inserted into page tables. |
602 | */ |
606 | */ |
603 | page_mapping_insert(AS, page, frame, get_area_flags(area)); |
607 | page_mapping_insert(AS, page, frame, get_area_flags(area)); |
604 | page_table_unlock(AS, false); |
608 | page_table_unlock(AS, false); |
605 | 609 | ||
606 | mutex_unlock(&area->lock); |
610 | mutex_unlock(&area->lock); |
607 | mutex_unlock(&AS->lock); |
611 | mutex_unlock(&AS->lock); |
608 | return AS_PF_OK; |
612 | return AS_PF_OK; |
609 | 613 | ||
610 | page_fault: |
614 | page_fault: |
611 | if (!THREAD) |
615 | if (!THREAD) |
612 | return AS_PF_FAULT; |
616 | return AS_PF_FAULT; |
613 | 617 | ||
614 | if (THREAD->in_copy_from_uspace) { |
618 | if (THREAD->in_copy_from_uspace) { |
615 | THREAD->in_copy_from_uspace = false; |
619 | THREAD->in_copy_from_uspace = false; |
616 | istate_set_retaddr(istate, (__address) &memcpy_from_uspace_failover_address); |
620 | istate_set_retaddr(istate, (__address) &memcpy_from_uspace_failover_address); |
617 | } else if (THREAD->in_copy_to_uspace) { |
621 | } else if (THREAD->in_copy_to_uspace) { |
618 | THREAD->in_copy_to_uspace = false; |
622 | THREAD->in_copy_to_uspace = false; |
619 | istate_set_retaddr(istate, (__address) &memcpy_to_uspace_failover_address); |
623 | istate_set_retaddr(istate, (__address) &memcpy_to_uspace_failover_address); |
620 | } else { |
624 | } else { |
621 | return AS_PF_FAULT; |
625 | return AS_PF_FAULT; |
622 | } |
626 | } |
623 | 627 | ||
624 | return AS_PF_DEFER; |
628 | return AS_PF_DEFER; |
625 | } |
629 | } |
626 | 630 | ||
627 | /** Switch address spaces. |
631 | /** Switch address spaces. |
628 | * |
632 | * |
629 | * Note that this function cannot sleep as it is essentially a part of |
633 | * Note that this function cannot sleep as it is essentially a part of |
630 | * the scheduling. Sleeping here would lead to deadlock on wakeup. |
634 | * the scheduling. Sleeping here would lead to deadlock on wakeup. |
631 | * |
635 | * |
632 | * @param old Old address space or NULL. |
636 | * @param old Old address space or NULL. |
633 | * @param new New address space. |
637 | * @param new New address space. |
634 | */ |
638 | */ |
635 | void as_switch(as_t *old, as_t *new) |
639 | void as_switch(as_t *old, as_t *new) |
636 | { |
640 | { |
637 | ipl_t ipl; |
641 | ipl_t ipl; |
638 | bool needs_asid = false; |
642 | bool needs_asid = false; |
639 | 643 | ||
640 | ipl = interrupts_disable(); |
644 | ipl = interrupts_disable(); |
641 | spinlock_lock(&as_lock); |
645 | spinlock_lock(&as_lock); |
642 | 646 | ||
643 | /* |
647 | /* |
644 | * First, take care of the old address space. |
648 | * First, take care of the old address space. |
645 | */ |
649 | */ |
646 | if (old) { |
650 | if (old) { |
647 | mutex_lock_active(&old->lock); |
651 | mutex_lock_active(&old->lock); |
648 | ASSERT(old->refcount); |
652 | ASSERT(old->refcount); |
649 | if((--old->refcount == 0) && (old != AS_KERNEL)) { |
653 | if((--old->refcount == 0) && (old != AS_KERNEL)) { |
650 | /* |
654 | /* |
651 | * The old address space is no longer active on |
655 | * The old address space is no longer active on |
652 | * any processor. It can be appended to the |
656 | * any processor. It can be appended to the |
653 | * list of inactive address spaces with assigned |
657 | * list of inactive address spaces with assigned |
654 | * ASID. |
658 | * ASID. |
655 | */ |
659 | */ |
656 | ASSERT(old->asid != ASID_INVALID); |
660 | ASSERT(old->asid != ASID_INVALID); |
657 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head); |
661 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head); |
658 | } |
662 | } |
659 | mutex_unlock(&old->lock); |
663 | mutex_unlock(&old->lock); |
660 | } |
664 | } |
661 | 665 | ||
662 | /* |
666 | /* |
663 | * Second, prepare the new address space. |
667 | * Second, prepare the new address space. |
664 | */ |
668 | */ |
665 | mutex_lock_active(&new->lock); |
669 | mutex_lock_active(&new->lock); |
666 | if ((new->refcount++ == 0) && (new != AS_KERNEL)) { |
670 | if ((new->refcount++ == 0) && (new != AS_KERNEL)) { |
667 | if (new->asid != ASID_INVALID) |
671 | if (new->asid != ASID_INVALID) |
668 | list_remove(&new->inactive_as_with_asid_link); |
672 | list_remove(&new->inactive_as_with_asid_link); |
669 | else |
673 | else |
670 | needs_asid = true; /* defer call to asid_get() until new->lock is released */ |
674 | needs_asid = true; /* defer call to asid_get() until new->lock is released */ |
671 | } |
675 | } |
672 | SET_PTL0_ADDRESS(new->page_table); |
676 | SET_PTL0_ADDRESS(new->page_table); |
673 | mutex_unlock(&new->lock); |
677 | mutex_unlock(&new->lock); |
674 | 678 | ||
675 | if (needs_asid) { |
679 | if (needs_asid) { |
676 | /* |
680 | /* |
677 | * Allocation of new ASID was deferred |
681 | * Allocation of new ASID was deferred |
678 | * until now in order to avoid deadlock. |
682 | * until now in order to avoid deadlock. |
679 | */ |
683 | */ |
680 | asid_t asid; |
684 | asid_t asid; |
681 | 685 | ||
682 | asid = asid_get(); |
686 | asid = asid_get(); |
683 | mutex_lock_active(&new->lock); |
687 | mutex_lock_active(&new->lock); |
684 | new->asid = asid; |
688 | new->asid = asid; |
685 | mutex_unlock(&new->lock); |
689 | mutex_unlock(&new->lock); |
686 | } |
690 | } |
687 | spinlock_unlock(&as_lock); |
691 | spinlock_unlock(&as_lock); |
688 | interrupts_restore(ipl); |
692 | interrupts_restore(ipl); |
689 | 693 | ||
690 | /* |
694 | /* |
691 | * Perform architecture-specific steps. |
695 | * Perform architecture-specific steps. |
692 | * (e.g. write ASID to hardware register etc.) |
696 | * (e.g. write ASID to hardware register etc.) |
693 | */ |
697 | */ |
694 | as_install_arch(new); |
698 | as_install_arch(new); |
695 | 699 | ||
696 | AS = new; |
700 | AS = new; |
697 | } |
701 | } |
698 | 702 | ||
699 | /** Convert address space area flags to page flags. |
703 | /** Convert address space area flags to page flags. |
700 | * |
704 | * |
701 | * @param aflags Flags of some address space area. |
705 | * @param aflags Flags of some address space area. |
702 | * |
706 | * |
703 | * @return Flags to be passed to page_mapping_insert(). |
707 | * @return Flags to be passed to page_mapping_insert(). |
704 | */ |
708 | */ |
705 | int area_flags_to_page_flags(int aflags) |
709 | int area_flags_to_page_flags(int aflags) |
706 | { |
710 | { |
707 | int flags; |
711 | int flags; |
708 | 712 | ||
709 | flags = PAGE_USER | PAGE_PRESENT; |
713 | flags = PAGE_USER | PAGE_PRESENT; |
710 | 714 | ||
711 | if (aflags & AS_AREA_READ) |
715 | if (aflags & AS_AREA_READ) |
712 | flags |= PAGE_READ; |
716 | flags |= PAGE_READ; |
713 | 717 | ||
714 | if (aflags & AS_AREA_WRITE) |
718 | if (aflags & AS_AREA_WRITE) |
715 | flags |= PAGE_WRITE; |
719 | flags |= PAGE_WRITE; |
716 | 720 | ||
717 | if (aflags & AS_AREA_EXEC) |
721 | if (aflags & AS_AREA_EXEC) |
718 | flags |= PAGE_EXEC; |
722 | flags |= PAGE_EXEC; |
719 | 723 | ||
720 | if (!(aflags & AS_AREA_DEVICE)) |
724 | if (!(aflags & AS_AREA_DEVICE)) |
721 | flags |= PAGE_CACHEABLE; |
725 | flags |= PAGE_CACHEABLE; |
722 | 726 | ||
723 | return flags; |
727 | return flags; |
724 | } |
728 | } |
725 | 729 | ||
726 | /** Compute flags for virtual address translation subsytem. |
730 | /** Compute flags for virtual address translation subsytem. |
727 | * |
731 | * |
728 | * The address space area must be locked. |
732 | * The address space area must be locked. |
729 | * Interrupts must be disabled. |
733 | * Interrupts must be disabled. |
730 | * |
734 | * |
731 | * @param a Address space area. |
735 | * @param a Address space area. |
732 | * |
736 | * |
733 | * @return Flags to be used in page_mapping_insert(). |
737 | * @return Flags to be used in page_mapping_insert(). |
734 | */ |
738 | */ |
735 | int get_area_flags(as_area_t *a) |
739 | int get_area_flags(as_area_t *a) |
736 | { |
740 | { |
737 | return area_flags_to_page_flags(a->flags); |
741 | return area_flags_to_page_flags(a->flags); |
738 | } |
742 | } |
739 | 743 | ||
740 | /** Create page table. |
744 | /** Create page table. |
741 | * |
745 | * |
742 | * Depending on architecture, create either address space |
746 | * Depending on architecture, create either address space |
743 | * private or global page table. |
747 | * private or global page table. |
744 | * |
748 | * |
745 | * @param flags Flags saying whether the page table is for kernel address space. |
749 | * @param flags Flags saying whether the page table is for kernel address space. |
746 | * |
750 | * |
747 | * @return First entry of the page table. |
751 | * @return First entry of the page table. |
748 | */ |
752 | */ |
749 | pte_t *page_table_create(int flags) |
753 | pte_t *page_table_create(int flags) |
750 | { |
754 | { |
751 | ASSERT(as_operations); |
755 | ASSERT(as_operations); |
752 | ASSERT(as_operations->page_table_create); |
756 | ASSERT(as_operations->page_table_create); |
753 | 757 | ||
754 | return as_operations->page_table_create(flags); |
758 | return as_operations->page_table_create(flags); |
755 | } |
759 | } |
756 | 760 | ||
757 | /** Lock page table. |
761 | /** Lock page table. |
758 | * |
762 | * |
759 | * This function should be called before any page_mapping_insert(), |
763 | * This function should be called before any page_mapping_insert(), |
760 | * page_mapping_remove() and page_mapping_find(). |
764 | * page_mapping_remove() and page_mapping_find(). |
761 | * |
765 | * |
762 | * Locking order is such that address space areas must be locked |
766 | * Locking order is such that address space areas must be locked |
763 | * prior to this call. Address space can be locked prior to this |
767 | * prior to this call. Address space can be locked prior to this |
764 | * call in which case the lock argument is false. |
768 | * call in which case the lock argument is false. |
765 | * |
769 | * |
766 | * @param as Address space. |
770 | * @param as Address space. |
767 | * @param lock If false, do not attempt to lock as->lock. |
771 | * @param lock If false, do not attempt to lock as->lock. |
768 | */ |
772 | */ |
769 | void page_table_lock(as_t *as, bool lock) |
773 | void page_table_lock(as_t *as, bool lock) |
770 | { |
774 | { |
771 | ASSERT(as_operations); |
775 | ASSERT(as_operations); |
772 | ASSERT(as_operations->page_table_lock); |
776 | ASSERT(as_operations->page_table_lock); |
773 | 777 | ||
774 | as_operations->page_table_lock(as, lock); |
778 | as_operations->page_table_lock(as, lock); |
775 | } |
779 | } |
776 | 780 | ||
777 | /** Unlock page table. |
781 | /** Unlock page table. |
778 | * |
782 | * |
779 | * @param as Address space. |
783 | * @param as Address space. |
780 | * @param unlock If false, do not attempt to unlock as->lock. |
784 | * @param unlock If false, do not attempt to unlock as->lock. |
781 | */ |
785 | */ |
782 | void page_table_unlock(as_t *as, bool unlock) |
786 | void page_table_unlock(as_t *as, bool unlock) |
783 | { |
787 | { |
784 | ASSERT(as_operations); |
788 | ASSERT(as_operations); |
785 | ASSERT(as_operations->page_table_unlock); |
789 | ASSERT(as_operations->page_table_unlock); |
786 | 790 | ||
787 | as_operations->page_table_unlock(as, unlock); |
791 | as_operations->page_table_unlock(as, unlock); |
788 | } |
792 | } |
789 | 793 | ||
790 | 794 | ||
791 | /** Find address space area and lock it. |
795 | /** Find address space area and lock it. |
792 | * |
796 | * |
793 | * The address space must be locked and interrupts must be disabled. |
797 | * The address space must be locked and interrupts must be disabled. |
794 | * |
798 | * |
795 | * @param as Address space. |
799 | * @param as Address space. |
796 | * @param va Virtual address. |
800 | * @param va Virtual address. |
797 | * |
801 | * |
798 | * @return Locked address space area containing va on success or NULL on failure. |
802 | * @return Locked address space area containing va on success or NULL on failure. |
799 | */ |
803 | */ |
800 | as_area_t *find_area_and_lock(as_t *as, __address va) |
804 | as_area_t *find_area_and_lock(as_t *as, __address va) |
801 | { |
805 | { |
802 | as_area_t *a; |
806 | as_area_t *a; |
803 | btree_node_t *leaf, *lnode; |
807 | btree_node_t *leaf, *lnode; |
804 | int i; |
808 | int i; |
805 | 809 | ||
806 | a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf); |
810 | a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf); |
807 | if (a) { |
811 | if (a) { |
808 | /* va is the base address of an address space area */ |
812 | /* va is the base address of an address space area */ |
809 | mutex_lock(&a->lock); |
813 | mutex_lock(&a->lock); |
810 | return a; |
814 | return a; |
811 | } |
815 | } |
812 | 816 | ||
813 | /* |
817 | /* |
814 | * Search the leaf node and the righmost record of its left neighbour |
818 | * Search the leaf node and the righmost record of its left neighbour |
815 | * to find out whether this is a miss or va belongs to an address |
819 | * to find out whether this is a miss or va belongs to an address |
816 | * space area found there. |
820 | * space area found there. |
817 | */ |
821 | */ |
818 | 822 | ||
819 | /* First, search the leaf node itself. */ |
823 | /* First, search the leaf node itself. */ |
820 | for (i = 0; i < leaf->keys; i++) { |
824 | for (i = 0; i < leaf->keys; i++) { |
821 | a = (as_area_t *) leaf->value[i]; |
825 | a = (as_area_t *) leaf->value[i]; |
822 | mutex_lock(&a->lock); |
826 | mutex_lock(&a->lock); |
823 | if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) { |
827 | if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) { |
824 | return a; |
828 | return a; |
825 | } |
829 | } |
826 | mutex_unlock(&a->lock); |
830 | mutex_unlock(&a->lock); |
827 | } |
831 | } |
828 | 832 | ||
829 | /* |
833 | /* |
830 | * Second, locate the left neighbour and test its last record. |
834 | * Second, locate the left neighbour and test its last record. |
831 | * Because of its position in the B+tree, it must have base < va. |
835 | * Because of its position in the B+tree, it must have base < va. |
832 | */ |
836 | */ |
833 | if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
837 | if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
834 | a = (as_area_t *) lnode->value[lnode->keys - 1]; |
838 | a = (as_area_t *) lnode->value[lnode->keys - 1]; |
835 | mutex_lock(&a->lock); |
839 | mutex_lock(&a->lock); |
836 | if (va < a->base + a->pages * PAGE_SIZE) { |
840 | if (va < a->base + a->pages * PAGE_SIZE) { |
837 | return a; |
841 | return a; |
838 | } |
842 | } |
839 | mutex_unlock(&a->lock); |
843 | mutex_unlock(&a->lock); |
840 | } |
844 | } |
841 | 845 | ||
842 | return NULL; |
846 | return NULL; |
843 | } |
847 | } |
844 | 848 | ||
845 | /** Check area conflicts with other areas. |
849 | /** Check area conflicts with other areas. |
846 | * |
850 | * |
847 | * The address space must be locked and interrupts must be disabled. |
851 | * The address space must be locked and interrupts must be disabled. |
848 | * |
852 | * |
849 | * @param as Address space. |
853 | * @param as Address space. |
850 | * @param va Starting virtual address of the area being tested. |
854 | * @param va Starting virtual address of the area being tested. |
851 | * @param size Size of the area being tested. |
855 | * @param size Size of the area being tested. |
852 | * @param avoid_area Do not touch this area. |
856 | * @param avoid_area Do not touch this area. |
853 | * |
857 | * |
854 | * @return True if there is no conflict, false otherwise. |
858 | * @return True if there is no conflict, false otherwise. |
855 | */ |
859 | */ |
856 | bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area) |
860 | bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area) |
857 | { |
861 | { |
858 | as_area_t *a; |
862 | as_area_t *a; |
859 | btree_node_t *leaf, *node; |
863 | btree_node_t *leaf, *node; |
860 | int i; |
864 | int i; |
861 | 865 | ||
862 | /* |
866 | /* |
863 | * We don't want any area to have conflicts with NULL page. |
867 | * We don't want any area to have conflicts with NULL page. |
864 | */ |
868 | */ |
865 | if (overlaps(va, size, NULL, PAGE_SIZE)) |
869 | if (overlaps(va, size, NULL, PAGE_SIZE)) |
866 | return false; |
870 | return false; |
867 | 871 | ||
868 | /* |
872 | /* |
869 | * The leaf node is found in O(log n), where n is proportional to |
873 | * The leaf node is found in O(log n), where n is proportional to |
870 | * the number of address space areas belonging to as. |
874 | * the number of address space areas belonging to as. |
871 | * The check for conflicts is then attempted on the rightmost |
875 | * The check for conflicts is then attempted on the rightmost |
872 | * record in the left neighbour, the leftmost record in the right |
876 | * record in the left neighbour, the leftmost record in the right |
873 | * neighbour and all records in the leaf node itself. |
877 | * neighbour and all records in the leaf node itself. |
874 | */ |
878 | */ |
875 | 879 | ||
876 | if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) { |
880 | if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) { |
877 | if (a != avoid_area) |
881 | if (a != avoid_area) |
878 | return false; |
882 | return false; |
879 | } |
883 | } |
880 | 884 | ||
881 | /* First, check the two border cases. */ |
885 | /* First, check the two border cases. */ |
882 | if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
886 | if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
883 | a = (as_area_t *) node->value[node->keys - 1]; |
887 | a = (as_area_t *) node->value[node->keys - 1]; |
884 | mutex_lock(&a->lock); |
888 | mutex_lock(&a->lock); |
885 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
889 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
886 | mutex_unlock(&a->lock); |
890 | mutex_unlock(&a->lock); |
887 | return false; |
891 | return false; |
888 | } |
892 | } |
889 | mutex_unlock(&a->lock); |
893 | mutex_unlock(&a->lock); |
890 | } |
894 | } |
891 | if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) { |
895 | if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) { |
892 | a = (as_area_t *) node->value[0]; |
896 | a = (as_area_t *) node->value[0]; |
893 | mutex_lock(&a->lock); |
897 | mutex_lock(&a->lock); |
894 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
898 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
895 | mutex_unlock(&a->lock); |
899 | mutex_unlock(&a->lock); |
896 | return false; |
900 | return false; |
897 | } |
901 | } |
898 | mutex_unlock(&a->lock); |
902 | mutex_unlock(&a->lock); |
899 | } |
903 | } |
900 | 904 | ||
901 | /* Second, check the leaf node. */ |
905 | /* Second, check the leaf node. */ |
902 | for (i = 0; i < leaf->keys; i++) { |
906 | for (i = 0; i < leaf->keys; i++) { |
903 | a = (as_area_t *) leaf->value[i]; |
907 | a = (as_area_t *) leaf->value[i]; |
904 | 908 | ||
905 | if (a == avoid_area) |
909 | if (a == avoid_area) |
906 | continue; |
910 | continue; |
907 | 911 | ||
908 | mutex_lock(&a->lock); |
912 | mutex_lock(&a->lock); |
909 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
913 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
910 | mutex_unlock(&a->lock); |
914 | mutex_unlock(&a->lock); |
911 | return false; |
915 | return false; |
912 | } |
916 | } |
913 | mutex_unlock(&a->lock); |
917 | mutex_unlock(&a->lock); |
914 | } |
918 | } |
915 | 919 | ||
916 | /* |
920 | /* |
917 | * So far, the area does not conflict with other areas. |
921 | * So far, the area does not conflict with other areas. |
918 | * Check if it doesn't conflict with kernel address space. |
922 | * Check if it doesn't conflict with kernel address space. |
919 | */ |
923 | */ |
920 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) { |
924 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) { |
921 | return !overlaps(va, size, |
925 | return !overlaps(va, size, |
922 | KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START); |
926 | KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START); |
923 | } |
927 | } |
924 | 928 | ||
925 | return true; |
929 | return true; |
926 | } |
930 | } |
927 | 931 | ||
928 | /** Return size of the address space area with given base. */ |
932 | /** Return size of the address space area with given base. */ |
929 | size_t as_get_size(__address base) |
933 | size_t as_get_size(__address base) |
930 | { |
934 | { |
931 | ipl_t ipl; |
935 | ipl_t ipl; |
932 | as_area_t *src_area; |
936 | as_area_t *src_area; |
933 | size_t size; |
937 | size_t size; |
934 | 938 | ||
935 | ipl = interrupts_disable(); |
939 | ipl = interrupts_disable(); |
936 | src_area = find_area_and_lock(AS, base); |
940 | src_area = find_area_and_lock(AS, base); |
937 | if (src_area){ |
941 | if (src_area){ |
938 | size = src_area->pages * PAGE_SIZE; |
942 | size = src_area->pages * PAGE_SIZE; |
939 | mutex_unlock(&src_area->lock); |
943 | mutex_unlock(&src_area->lock); |
940 | } else { |
944 | } else { |
941 | size = 0; |
945 | size = 0; |
942 | } |
946 | } |
943 | interrupts_restore(ipl); |
947 | interrupts_restore(ipl); |
944 | return size; |
948 | return size; |
945 | } |
949 | } |
- | 950 | ||
- | 951 | /** Mark portion of address space area as used. |
|
- | 952 | * |
|
- | 953 | * The address space area must be already locked. |
|
- | 954 | * |
|
- | 955 | * @param a Address space area. |
|
- | 956 | * @param page First page to be marked. |
|
- | 957 | * @param count Number of page to be marked. |
|
- | 958 | * |
|
- | 959 | * @return 0 on failure and 1 on success. |
|
- | 960 | */ |
|
- | 961 | int used_space_insert(as_area_t *a, __address page, count_t count) |
|
- | 962 | { |
|
- | 963 | btree_node_t *leaf, *node; |
|
- | 964 | count_t pages; |
|
- | 965 | int i; |
|
- | 966 | ||
- | 967 | ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE)); |
|
- | 968 | ASSERT(count); |
|
- | 969 | ||
- | 970 | pages = (count_t) btree_search(&a->used_space, page, &leaf); |
|
- | 971 | if (pages) { |
|
- | 972 | /* |
|
- | 973 | * We hit the beginning of some used space. |
|
- | 974 | */ |
|
- | 975 | return 0; |
|
- | 976 | } |
|
- | 977 | ||
- | 978 | node = btree_leaf_node_left_neighbour(&a->used_space, leaf); |
|
- | 979 | if (node) { |
|
- | 980 | __address left_pg = node->key[node->keys - 1], right_pg = leaf->key[0]; |
|
- | 981 | count_t left_cnt = (count_t) node->value[node->keys - 1], right_cnt = (count_t) leaf->value[0]; |
|
- | 982 | ||
- | 983 | /* |
|
- | 984 | * Examine the possibility that the interval fits |
|
- | 985 | * somewhere between the rightmost interval of |
|
- | 986 | * the left neigbour and the first interval of the leaf. |
|
- | 987 | */ |
|
- | 988 | ||
- | 989 | if (page >= right_pg) { |
|
- | 990 | /* Do nothing. */ |
|
- | 991 | } else if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) { |
|
- | 992 | /* The interval intersects with the left interval. */ |
|
- | 993 | return 0; |
|
- | 994 | } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) { |
|
- | 995 | /* The interval intersects with the right interval. */ |
|
- | 996 | return 0; |
|
- | 997 | } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) { |
|
- | 998 | /* The interval can be added by merging the two already present intervals. */ |
|
- | 999 | node->value[node->keys - 1] += (count_t) count + right_cnt; |
|
- | 1000 | btree_remove(&a->used_space, right_pg, leaf); |
|
- | 1001 | return 1; |
|
- | 1002 | } else if (page == left_pg + left_cnt*PAGE_SIZE) { |
|
- | 1003 | /* The interval can be added by simply growing the left interval. */ |
|
- | 1004 | node->value[node->keys - 1] += (count_t) count; |
|
- | 1005 | return 1; |
|
- | 1006 | } else if (page + count*PAGE_SIZE == right_pg) { |
|
- | 1007 | /* |
|
- | 1008 | * The interval can be addded by simply moving base of the right |
|
- | 1009 | * interval down and increasing its size accordingly. |
|
- | 1010 | */ |
|
- | 1011 | leaf->value[0] += (count_t) count; |
|
- | 1012 | leaf->key[0] = page; |
|
- | 1013 | return 1; |
|
- | 1014 | } else { |
|
- | 1015 | /* |
|
- | 1016 | * The interval is between both neigbouring intervals, |
|
- | 1017 | * but cannot be merged with any of them. |
|
- | 1018 | */ |
|
- | 1019 | btree_insert(&a->used_space, page, (void *) count, leaf); |
|
- | 1020 | return 1; |
|
- | 1021 | } |
|
- | 1022 | } else if (page < leaf->key[0]) { |
|
- | 1023 | __address right_pg = leaf->key[0]; |
|
- | 1024 | count_t right_cnt = (count_t) leaf->value[0]; |
|
- | 1025 | ||
- | 1026 | /* |
|
- | 1027 | * Investigate the border case in which the left neighbour does not |
|
- | 1028 | * exist but the interval fits from the left. |
|
- | 1029 | */ |
|
- | 1030 | ||
- | 1031 | if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) { |
|
- | 1032 | /* The interval intersects with the right interval. */ |
|
- | 1033 | return 0; |
|
- | 1034 | } else if (page + count*PAGE_SIZE == right_pg) { |
|
- | 1035 | /* |
|
- | 1036 | * The interval can be added by moving the base of the right interval down |
|
- | 1037 | * and increasing its size accordingly. |
|
- | 1038 | */ |
|
- | 1039 | leaf->key[0] = page; |
|
- | 1040 | leaf->value[0] += (count_t) count; |
|
- | 1041 | return 1; |
|
- | 1042 | } else { |
|
- | 1043 | /* |
|
- | 1044 | * The interval doesn't adjoin with the right interval. |
|
- | 1045 | * It must be added individually. |
|
- | 1046 | */ |
|
- | 1047 | btree_insert(&a->used_space, page, (void *) count, leaf); |
|
- | 1048 | return 1; |
|
- | 1049 | } |
|
- | 1050 | } |
|
- | 1051 | ||
- | 1052 | node = btree_leaf_node_right_neighbour(&a->used_space, leaf); |
|
- | 1053 | if (node) { |
|
- | 1054 | __address left_pg = leaf->key[leaf->keys - 1], right_pg = node->key[0]; |
|
- | 1055 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1], right_cnt = (count_t) node->value[0]; |
|
- | 1056 | ||
- | 1057 | /* |
|
- | 1058 | * Examine the possibility that the interval fits |
|
- | 1059 | * somewhere between the leftmost interval of |
|
- | 1060 | * the right neigbour and the last interval of the leaf. |
|
- | 1061 | */ |
|
- | 1062 | ||
- | 1063 | if (page < left_pg) { |
|
- | 1064 | /* Do nothing. */ |
|
- | 1065 | } else if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) { |
|
- | 1066 | /* The interval intersects with the left interval. */ |
|
- | 1067 | return 0; |
|
- | 1068 | } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) { |
|
- | 1069 | /* The interval intersects with the right interval. */ |
|
- | 1070 | return 0; |
|
- | 1071 | } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) { |
|
- | 1072 | /* The interval can be added by merging the two already present intervals. */ |
|
- | 1073 | leaf->value[leaf->keys - 1] += (count_t) count + right_cnt; |
|
- | 1074 | btree_remove(&a->used_space, right_pg, node); |
|
- | 1075 | return 1; |
|
- | 1076 | } else if (page == left_pg + left_cnt*PAGE_SIZE) { |
|
- | 1077 | /* The interval can be added by simply growing the left interval. */ |
|
- | 1078 | leaf->value[leaf->keys - 1] += (count_t) count; |
|
- | 1079 | return 1; |
|
- | 1080 | } else if (page + count*PAGE_SIZE == right_pg) { |
|
- | 1081 | /* |
|
- | 1082 | * The interval can be addded by simply moving base of the right |
|
- | 1083 | * interval down and increasing its size accordingly. |
|
- | 1084 | */ |
|
- | 1085 | node->value[0] += (count_t) count; |
|
- | 1086 | node->key[0] = page; |
|
- | 1087 | return 1; |
|
- | 1088 | } else { |
|
- | 1089 | /* |
|
- | 1090 | * The interval is between both neigbouring intervals, |
|
- | 1091 | * but cannot be merged with any of them. |
|
- | 1092 | */ |
|
- | 1093 | btree_insert(&a->used_space, page, (void *) count, leaf); |
|
- | 1094 | return 1; |
|
- | 1095 | } |
|
- | 1096 | } else if (page >= leaf->key[leaf->keys - 1]) { |
|
- | 1097 | __address left_pg = leaf->key[leaf->keys - 1]; |
|
- | 1098 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1]; |
|
- | 1099 | ||
- | 1100 | /* |
|
- | 1101 | * Investigate the border case in which the right neighbour does not |
|
- | 1102 | * exist but the interval fits from the right. |
|
- | 1103 | */ |
|
- | 1104 | ||
- | 1105 | if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) { |
|
- | 1106 | /* The interval intersects with the right interval. */ |
|
- | 1107 | return 0; |
|
- | 1108 | } else if (left_pg + left_cnt*PAGE_SIZE == page) { |
|
- | 1109 | /* The interval can be added by growing the left interval. */ |
|
- | 1110 | leaf->value[leaf->keys - 1] += (count_t) count; |
|
- | 1111 | return 1; |
|
- | 1112 | } else { |
|
- | 1113 | /* |
|
- | 1114 | * The interval doesn't adjoin with the left interval. |
|
- | 1115 | * It must be added individually. |
|
- | 1116 | */ |
|
- | 1117 | btree_insert(&a->used_space, page, (void *) count, leaf); |
|
- | 1118 | return 1; |
|
- | 1119 | } |
|
- | 1120 | } |
|
- | 1121 | ||
- | 1122 | /* |
|
- | 1123 | * Note that if the algorithm made it thus far, the interval can fit only |
|
- | 1124 | * between two other intervals of the leaf. The two border cases were already |
|
- | 1125 | * resolved. |
|
- | 1126 | */ |
|
- | 1127 | for (i = 1; i < leaf->keys; i++) { |
|
- | 1128 | if (page < leaf->key[i]) { |
|
- | 1129 | __address left_pg = leaf->key[i - 1], right_pg = leaf->key[i]; |
|
- | 1130 | count_t left_cnt = (count_t) leaf->value[i - 1], right_cnt = (count_t) leaf->value[i]; |
|
- | 1131 | ||
- | 1132 | /* |
|
- | 1133 | * The interval fits between left_pg and right_pg. |
|
- | 1134 | */ |
|
- | 1135 | ||
- | 1136 | if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) { |
|
- | 1137 | /* The interval intersects with the left interval. */ |
|
- | 1138 | return 0; |
|
- | 1139 | } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) { |
|
- | 1140 | /* The interval intersects with the right interval. */ |
|
- | 1141 | return 0; |
|
- | 1142 | } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) { |
|
- | 1143 | /* The interval can be added by merging the two already present intervals. */ |
|
- | 1144 | leaf->value[i - 1] += (count_t) count + right_cnt; |
|
- | 1145 | btree_remove(&a->used_space, right_pg, leaf); |
|
- | 1146 | return 1; |
|
- | 1147 | } else if (page == left_pg + left_cnt*PAGE_SIZE) { |
|
- | 1148 | /* The interval can be added by simply growing the left interval. */ |
|
- | 1149 | leaf->value[i - 1] += (count_t) count; |
|
- | 1150 | return 1; |
|
- | 1151 | } else if (page + count*PAGE_SIZE == right_pg) { |
|
- | 1152 | /* |
|
- | 1153 | * The interval can be addded by simply moving base of the right |
|
- | 1154 | * interval down and increasing its size accordingly. |
|
- | 1155 | */ |
|
- | 1156 | leaf->value[i] += (count_t) count; |
|
- | 1157 | leaf->key[i] = page; |
|
- | 1158 | return 1; |
|
- | 1159 | } else { |
|
- | 1160 | /* |
|
- | 1161 | * The interval is between both neigbouring intervals, |
|
- | 1162 | * but cannot be merged with any of them. |
|
- | 1163 | */ |
|
- | 1164 | btree_insert(&a->used_space, page, (void *) count, leaf); |
|
- | 1165 | return 1; |
|
- | 1166 | } |
|
- | 1167 | } |
|
- | 1168 | } |
|
- | 1169 | ||
- | 1170 | panic("Inconsistency detected while adding %d pages of used space at %P.\n", count, page); |
|
- | 1171 | } |
|
- | 1172 | ||
- | 1173 | /** Mark portion of address space area as unused. |
|
- | 1174 | * |
|
- | 1175 | * The address space area must be already locked. |
|
- | 1176 | * |
|
- | 1177 | * @param a Address space area. |
|
- | 1178 | * @param page First page to be marked. |
|
- | 1179 | * @param count Number of page to be marked. |
|
- | 1180 | * |
|
- | 1181 | * @return 0 on failure and 1 on success. |
|
- | 1182 | */ |
|
- | 1183 | int used_space_remove(as_area_t *a, __address page, count_t count) |
|
- | 1184 | { |
|
- | 1185 | btree_node_t *leaf, *node; |
|
- | 1186 | count_t pages; |
|
- | 1187 | int i; |
|
- | 1188 | ||
- | 1189 | ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE)); |
|
- | 1190 | ASSERT(count); |
|
- | 1191 | ||
- | 1192 | pages = (count_t) btree_search(&a->used_space, page, &leaf); |
|
- | 1193 | if (pages) { |
|
- | 1194 | /* |
|
- | 1195 | * We are lucky, page is the beginning of some interval. |
|
- | 1196 | */ |
|
- | 1197 | if (count > pages) { |
|
- | 1198 | return 0; |
|
- | 1199 | } else if (count == pages) { |
|
- | 1200 | btree_remove(&a->used_space, page, leaf); |
|
- | 1201 | } else { |
|
- | 1202 | /* |
|
- | 1203 | * Find the respective interval. |
|
- | 1204 | * Decrease its size and relocate its start address. |
|
- | 1205 | */ |
|
- | 1206 | for (i = 0; i < leaf->keys; i++) { |
|
- | 1207 | if (leaf->key[i] == page) { |
|
- | 1208 | leaf->key[i] += count*PAGE_SIZE; |
|
- | 1209 | leaf->value[i] -= (count_t) count; |
|
- | 1210 | return 1; |
|
- | 1211 | } |
|
- | 1212 | } |
|
- | 1213 | goto error; |
|
- | 1214 | } |
|
- | 1215 | } |
|
- | 1216 | ||
- | 1217 | node = btree_leaf_node_left_neighbour(&a->used_space, leaf); |
|
- | 1218 | if (node && page < leaf->key[0]) { |
|
- | 1219 | __address left_pg = node->key[node->keys - 1]; |
|
- | 1220 | count_t left_cnt = (count_t) node->value[node->keys - 1]; |
|
- | 1221 | ||
- | 1222 | if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) { |
|
- | 1223 | if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) { |
|
- | 1224 | /* |
|
- | 1225 | * The interval is contained in the rightmost interval |
|
- | 1226 | * of the left neighbour and can be removed by |
|
- | 1227 | * updating the size of the bigger interval. |
|
- | 1228 | */ |
|
- | 1229 | node->value[node->keys - 1] -= (count_t) count; |
|
- | 1230 | return 1; |
|
- | 1231 | } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) { |
|
- | 1232 | count_t new_cnt = (left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE); |
|
- | 1233 | ||
- | 1234 | /* |
|
- | 1235 | * The interval is contained in the rightmost interval |
|
- | 1236 | * of the left neighbour but its removal requires |
|
- | 1237 | * both updating the size of the original interval and |
|
- | 1238 | * also inserting a new interval. |
|
- | 1239 | */ |
|
- | 1240 | node->value[node->keys - 1] -= (count_t) count + new_cnt; |
|
- | 1241 | btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf); |
|
- | 1242 | return 1; |
|
- | 1243 | } |
|
- | 1244 | } |
|
- | 1245 | return 0; |
|
- | 1246 | } else if (page < leaf->key[0]) { |
|
- | 1247 | return 0; |
|
- | 1248 | } |
|
- | 1249 | ||
- | 1250 | if (page > leaf->key[leaf->keys - 1]) { |
|
- | 1251 | __address left_pg = leaf->key[leaf->keys - 1]; |
|
- | 1252 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1]; |
|
- | 1253 | ||
- | 1254 | if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) { |
|
- | 1255 | if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) { |
|
- | 1256 | /* |
|
- | 1257 | * The interval is contained in the rightmost interval |
|
- | 1258 | * of the leaf and can be removed by updating the size |
|
- | 1259 | * of the bigger interval. |
|
- | 1260 | */ |
|
- | 1261 | leaf->value[leaf->keys - 1] -= (count_t) count; |
|
- | 1262 | return 1; |
|
- | 1263 | } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) { |
|
- | 1264 | count_t new_cnt = (left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE); |
|
- | 1265 | ||
- | 1266 | /* |
|
- | 1267 | * The interval is contained in the rightmost interval |
|
- | 1268 | * of the leaf but its removal requires both updating |
|
- | 1269 | * the size of the original interval and |
|
- | 1270 | * also inserting a new interval. |
|
- | 1271 | */ |
|
- | 1272 | leaf->value[leaf->keys - 1] -= (count_t) count + new_cnt; |
|
- | 1273 | btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf); |
|
- | 1274 | return 1; |
|
- | 1275 | } |
|
- | 1276 | } |
|
- | 1277 | return 0; |
|
- | 1278 | } |
|
- | 1279 | ||
- | 1280 | /* |
|
- | 1281 | * The border cases have been already resolved. |
|
- | 1282 | * Now the interval can be only between intervals of the leaf. |
|
- | 1283 | */ |
|
- | 1284 | for (i = 1; i < leaf->keys - 1; i++) { |
|
- | 1285 | if (page < leaf->key[i]) { |
|
- | 1286 | __address left_pg = leaf->key[i - 1]; |
|
- | 1287 | count_t left_cnt = (count_t) leaf->value[i - 1]; |
|
- | 1288 | ||
- | 1289 | /* |
|
- | 1290 | * Now the interval is between intervals corresponding to (i - 1) and i. |
|
- | 1291 | */ |
|
- | 1292 | if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) { |
|
- | 1293 | if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) { |
|
- | 1294 | /* |
|
- | 1295 | * The interval is contained in the interval (i - 1) |
|
- | 1296 | * of the leaf and can be removed by updating the size |
|
- | 1297 | * of the bigger interval. |
|
- | 1298 | */ |
|
- | 1299 | leaf->value[i - 1] -= (count_t) count; |
|
- | 1300 | return 1; |
|
- | 1301 | } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) { |
|
- | 1302 | count_t new_cnt = (left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE); |
|
- | 1303 | ||
- | 1304 | /* |
|
- | 1305 | * The interval is contained in the interval (i - 1) |
|
- | 1306 | * of the leaf but its removal requires both updating |
|
- | 1307 | * the size of the original interval and |
|
- | 1308 | * also inserting a new interval. |
|
- | 1309 | */ |
|
- | 1310 | leaf->value[i - 1] -= (count_t) count + new_cnt; |
|
- | 1311 | btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf); |
|
- | 1312 | return 1; |
|
- | 1313 | } |
|
- | 1314 | } |
|
- | 1315 | return 0; |
|
- | 1316 | } |
|
- | 1317 | } |
|
- | 1318 | ||
- | 1319 | error: |
|
- | 1320 | panic("Inconsistency detected while removing %d pages of used space from %P.\n", count, page); |
|
- | 1321 | } |
|
946 | 1322 | ||
947 | /* |
1323 | /* |
948 | * Address space related syscalls. |
1324 | * Address space related syscalls. |
949 | */ |
1325 | */ |
950 | 1326 | ||
951 | /** Wrapper for as_area_create(). */ |
1327 | /** Wrapper for as_area_create(). */ |
952 | __native sys_as_area_create(__address address, size_t size, int flags) |
1328 | __native sys_as_area_create(__address address, size_t size, int flags) |
953 | { |
1329 | { |
954 | if (as_area_create(AS, flags, size, address, AS_AREA_ATTR_NONE)) |
1330 | if (as_area_create(AS, flags, size, address, AS_AREA_ATTR_NONE)) |
955 | return (__native) address; |
1331 | return (__native) address; |
956 | else |
1332 | else |
957 | return (__native) -1; |
1333 | return (__native) -1; |
958 | } |
1334 | } |
959 | 1335 | ||
960 | /** Wrapper for as_area_resize. */ |
1336 | /** Wrapper for as_area_resize. */ |
961 | __native sys_as_area_resize(__address address, size_t size, int flags) |
1337 | __native sys_as_area_resize(__address address, size_t size, int flags) |
962 | { |
1338 | { |
963 | return (__native) as_area_resize(AS, address, size, 0); |
1339 | return (__native) as_area_resize(AS, address, size, 0); |
964 | } |
1340 | } |
965 | 1341 | ||
966 | /** Wrapper for as_area_destroy. */ |
1342 | /** Wrapper for as_area_destroy. */ |
967 | __native sys_as_area_destroy(__address address) |
1343 | __native sys_as_area_destroy(__address address) |
968 | { |
1344 | { |
969 | return (__native) as_area_destroy(AS, address); |
1345 | return (__native) as_area_destroy(AS, address); |
970 | } |
1346 | } |
971 | 1347 |