Rev 2141 | Rev 2183 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2141 | Rev 2170 | ||
---|---|---|---|
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 | /** @addtogroup genericmm |
29 | /** @addtogroup genericmm |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Address space related functions. |
35 | * @brief Address space related functions. |
36 | * |
36 | * |
37 | * This file contains address space manipulation functions. |
37 | * This file contains address space manipulation functions. |
38 | * Roughly speaking, this is a higher-level client of |
38 | * Roughly speaking, this is a higher-level client of |
39 | * Virtual Address Translation (VAT) subsystem. |
39 | * Virtual Address Translation (VAT) subsystem. |
40 | * |
40 | * |
41 | * Functionality provided by this file allows one to |
41 | * Functionality provided by this file allows one to |
42 | * create address spaces and create, resize and share |
42 | * create address spaces and create, resize and share |
43 | * address space areas. |
43 | * address space areas. |
44 | * |
44 | * |
45 | * @see page.c |
45 | * @see page.c |
46 | * |
46 | * |
47 | */ |
47 | */ |
48 | 48 | ||
49 | #include <mm/as.h> |
49 | #include <mm/as.h> |
50 | #include <arch/mm/as.h> |
50 | #include <arch/mm/as.h> |
51 | #include <mm/page.h> |
51 | #include <mm/page.h> |
52 | #include <mm/frame.h> |
52 | #include <mm/frame.h> |
53 | #include <mm/slab.h> |
53 | #include <mm/slab.h> |
54 | #include <mm/tlb.h> |
54 | #include <mm/tlb.h> |
55 | #include <arch/mm/page.h> |
55 | #include <arch/mm/page.h> |
56 | #include <genarch/mm/page_pt.h> |
56 | #include <genarch/mm/page_pt.h> |
57 | #include <genarch/mm/page_ht.h> |
57 | #include <genarch/mm/page_ht.h> |
58 | #include <mm/asid.h> |
58 | #include <mm/asid.h> |
59 | #include <arch/mm/asid.h> |
59 | #include <arch/mm/asid.h> |
60 | #include <synch/spinlock.h> |
60 | #include <synch/spinlock.h> |
61 | #include <synch/mutex.h> |
61 | #include <synch/mutex.h> |
62 | #include <adt/list.h> |
62 | #include <adt/list.h> |
63 | #include <adt/btree.h> |
63 | #include <adt/btree.h> |
64 | #include <proc/task.h> |
64 | #include <proc/task.h> |
65 | #include <proc/thread.h> |
65 | #include <proc/thread.h> |
66 | #include <arch/asm.h> |
66 | #include <arch/asm.h> |
67 | #include <panic.h> |
67 | #include <panic.h> |
68 | #include <debug.h> |
68 | #include <debug.h> |
69 | #include <print.h> |
69 | #include <print.h> |
70 | #include <memstr.h> |
70 | #include <memstr.h> |
71 | #include <macros.h> |
71 | #include <macros.h> |
72 | #include <arch.h> |
72 | #include <arch.h> |
73 | #include <errno.h> |
73 | #include <errno.h> |
74 | #include <config.h> |
74 | #include <config.h> |
75 | #include <align.h> |
75 | #include <align.h> |
76 | #include <arch/types.h> |
76 | #include <arch/types.h> |
77 | #include <syscall/copy.h> |
77 | #include <syscall/copy.h> |
78 | #include <arch/interrupt.h> |
78 | #include <arch/interrupt.h> |
79 | 79 | ||
80 | #ifdef CONFIG_VIRT_IDX_DCACHE |
80 | #ifdef CONFIG_VIRT_IDX_DCACHE |
81 | #include <arch/mm/cache.h> |
81 | #include <arch/mm/cache.h> |
82 | #endif /* CONFIG_VIRT_IDX_DCACHE */ |
82 | #endif /* CONFIG_VIRT_IDX_DCACHE */ |
83 | 83 | ||
84 | #ifndef __OBJC__ |
84 | #ifndef __OBJC__ |
85 | /** |
85 | /** |
86 | * Each architecture decides what functions will be used to carry out |
86 | * Each architecture decides what functions will be used to carry out |
87 | * address space operations such as creating or locking page tables. |
87 | * address space operations such as creating or locking page tables. |
88 | */ |
88 | */ |
89 | as_operations_t *as_operations = NULL; |
89 | as_operations_t *as_operations = NULL; |
90 | 90 | ||
91 | /** |
91 | /** |
92 | * Slab for as_t objects. |
92 | * Slab for as_t objects. |
93 | */ |
93 | */ |
94 | static slab_cache_t *as_slab; |
94 | static slab_cache_t *as_slab; |
95 | #endif |
95 | #endif |
96 | 96 | ||
97 | /** |
97 | /** |
- | 98 | * This lock serializes access to the ASID subsystem. |
|
- | 99 | * It protects: |
|
98 | * This lock protects inactive_as_with_asid_head list. It must be acquired |
100 | * - inactive_as_with_asid_head list |
- | 101 | * - as->asid for each as of the as_t type |
|
99 | * before as_t mutex. |
102 | * - asids_allocated counter |
100 | */ |
103 | */ |
101 | SPINLOCK_INITIALIZE(inactive_as_with_asid_lock); |
104 | SPINLOCK_INITIALIZE(asidlock); |
102 | 105 | ||
103 | /** |
106 | /** |
104 | * This list contains address spaces that are not active on any |
107 | * This list contains address spaces that are not active on any |
105 | * processor and that have valid ASID. |
108 | * processor and that have valid ASID. |
106 | */ |
109 | */ |
107 | LIST_INITIALIZE(inactive_as_with_asid_head); |
110 | LIST_INITIALIZE(inactive_as_with_asid_head); |
108 | 111 | ||
109 | /** Kernel address space. */ |
112 | /** Kernel address space. */ |
110 | as_t *AS_KERNEL = NULL; |
113 | as_t *AS_KERNEL = NULL; |
111 | 114 | ||
112 | static int area_flags_to_page_flags(int aflags); |
115 | static int area_flags_to_page_flags(int aflags); |
113 | static as_area_t *find_area_and_lock(as_t *as, uintptr_t va); |
116 | static as_area_t *find_area_and_lock(as_t *as, uintptr_t va); |
114 | static bool check_area_conflicts(as_t *as, uintptr_t va, size_t size, |
117 | static bool check_area_conflicts(as_t *as, uintptr_t va, size_t size, |
115 | as_area_t *avoid_area); |
118 | as_area_t *avoid_area); |
116 | static void sh_info_remove_reference(share_info_t *sh_info); |
119 | static void sh_info_remove_reference(share_info_t *sh_info); |
117 | 120 | ||
118 | #ifndef __OBJC__ |
121 | #ifndef __OBJC__ |
119 | static int as_constructor(void *obj, int flags) |
122 | static int as_constructor(void *obj, int flags) |
120 | { |
123 | { |
121 | as_t *as = (as_t *) obj; |
124 | as_t *as = (as_t *) obj; |
122 | int rc; |
125 | int rc; |
123 | 126 | ||
124 | link_initialize(&as->inactive_as_with_asid_link); |
127 | link_initialize(&as->inactive_as_with_asid_link); |
125 | mutex_initialize(&as->lock); |
128 | mutex_initialize(&as->lock); |
126 | 129 | ||
127 | rc = as_constructor_arch(as, flags); |
130 | rc = as_constructor_arch(as, flags); |
128 | 131 | ||
129 | return rc; |
132 | return rc; |
130 | } |
133 | } |
131 | 134 | ||
132 | static int as_destructor(void *obj) |
135 | static int as_destructor(void *obj) |
133 | { |
136 | { |
134 | as_t *as = (as_t *) obj; |
137 | as_t *as = (as_t *) obj; |
135 | 138 | ||
136 | return as_destructor_arch(as); |
139 | return as_destructor_arch(as); |
137 | } |
140 | } |
138 | #endif |
141 | #endif |
139 | 142 | ||
140 | /** Initialize address space subsystem. */ |
143 | /** Initialize address space subsystem. */ |
141 | void as_init(void) |
144 | void as_init(void) |
142 | { |
145 | { |
143 | as_arch_init(); |
146 | as_arch_init(); |
144 | 147 | ||
145 | #ifndef __OBJC__ |
148 | #ifndef __OBJC__ |
146 | as_slab = slab_cache_create("as_slab", sizeof(as_t), 0, |
149 | as_slab = slab_cache_create("as_slab", sizeof(as_t), 0, |
147 | as_constructor, as_destructor, SLAB_CACHE_MAGDEFERRED); |
150 | as_constructor, as_destructor, SLAB_CACHE_MAGDEFERRED); |
148 | #endif |
151 | #endif |
149 | 152 | ||
150 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
153 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
151 | if (!AS_KERNEL) |
154 | if (!AS_KERNEL) |
152 | panic("can't create kernel address space\n"); |
155 | panic("can't create kernel address space\n"); |
153 | 156 | ||
154 | } |
157 | } |
155 | 158 | ||
156 | /** Create address space. |
159 | /** Create address space. |
157 | * |
160 | * |
158 | * @param flags Flags that influence way in wich the address space is created. |
161 | * @param flags Flags that influence way in wich the address space is created. |
159 | */ |
162 | */ |
160 | as_t *as_create(int flags) |
163 | as_t *as_create(int flags) |
161 | { |
164 | { |
162 | as_t *as; |
165 | as_t *as; |
163 | 166 | ||
164 | #ifdef __OBJC__ |
167 | #ifdef __OBJC__ |
165 | as = [as_t new]; |
168 | as = [as_t new]; |
166 | link_initialize(&as->inactive_as_with_asid_link); |
169 | link_initialize(&as->inactive_as_with_asid_link); |
167 | mutex_initialize(&as->lock); |
170 | mutex_initialize(&as->lock); |
168 | (void) as_constructor_arch(as, flags); |
171 | (void) as_constructor_arch(as, flags); |
169 | #else |
172 | #else |
170 | as = (as_t *) slab_alloc(as_slab, 0); |
173 | as = (as_t *) slab_alloc(as_slab, 0); |
171 | #endif |
174 | #endif |
172 | (void) as_create_arch(as, 0); |
175 | (void) as_create_arch(as, 0); |
173 | 176 | ||
174 | btree_create(&as->as_area_btree); |
177 | btree_create(&as->as_area_btree); |
175 | 178 | ||
176 | if (flags & FLAG_AS_KERNEL) |
179 | if (flags & FLAG_AS_KERNEL) |
177 | as->asid = ASID_KERNEL; |
180 | as->asid = ASID_KERNEL; |
178 | else |
181 | else |
179 | as->asid = ASID_INVALID; |
182 | as->asid = ASID_INVALID; |
180 | 183 | ||
181 | as->refcount = 0; |
184 | as->refcount = 0; |
182 | as->cpu_refcount = 0; |
185 | as->cpu_refcount = 0; |
183 | #ifdef AS_PAGE_TABLE |
186 | #ifdef AS_PAGE_TABLE |
184 | as->genarch.page_table = page_table_create(flags); |
187 | as->genarch.page_table = page_table_create(flags); |
185 | #else |
188 | #else |
186 | page_table_create(flags); |
189 | page_table_create(flags); |
187 | #endif |
190 | #endif |
188 | 191 | ||
189 | return as; |
192 | return as; |
190 | } |
193 | } |
191 | 194 | ||
192 | /** Destroy adress space. |
195 | /** Destroy adress space. |
193 | * |
196 | * |
194 | * When there are no tasks referencing this address space (i.e. its refcount is |
197 | * When there are no tasks referencing this address space (i.e. its refcount is |
195 | * zero), the address space can be destroyed. |
198 | * zero), the address space can be destroyed. |
196 | */ |
199 | */ |
197 | void as_destroy(as_t *as) |
200 | void as_destroy(as_t *as) |
198 | { |
201 | { |
199 | ipl_t ipl; |
202 | ipl_t ipl; |
200 | bool cond; |
203 | bool cond; |
201 | 204 | ||
202 | ASSERT(as->refcount == 0); |
205 | ASSERT(as->refcount == 0); |
203 | 206 | ||
204 | /* |
207 | /* |
205 | * Since there is no reference to this area, |
208 | * Since there is no reference to this area, |
206 | * it is safe not to lock its mutex. |
209 | * it is safe not to lock its mutex. |
207 | */ |
210 | */ |
- | 211 | ||
208 | ipl = interrupts_disable(); |
212 | ipl = interrupts_disable(); |
209 | spinlock_lock(&inactive_as_with_asid_lock); |
213 | spinlock_lock(&asidlock); |
210 | if (as->asid != ASID_INVALID && as != AS_KERNEL) { |
214 | if (as->asid != ASID_INVALID && as != AS_KERNEL) { |
211 | if (as != AS && as->cpu_refcount == 0) |
215 | if (as != AS && as->cpu_refcount == 0) |
212 | list_remove(&as->inactive_as_with_asid_link); |
216 | list_remove(&as->inactive_as_with_asid_link); |
213 | asid_put(as->asid); |
217 | asid_put(as->asid); |
214 | } |
218 | } |
215 | spinlock_unlock(&inactive_as_with_asid_lock); |
219 | spinlock_unlock(&asidlock); |
216 | 220 | ||
217 | /* |
221 | /* |
218 | * Destroy address space areas of the address space. |
222 | * Destroy address space areas of the address space. |
219 | * The B+tree must be walked carefully because it is |
223 | * The B+tree must be walked carefully because it is |
220 | * also being destroyed. |
224 | * also being destroyed. |
221 | */ |
225 | */ |
222 | for (cond = true; cond; ) { |
226 | for (cond = true; cond; ) { |
223 | btree_node_t *node; |
227 | btree_node_t *node; |
224 | 228 | ||
225 | ASSERT(!list_empty(&as->as_area_btree.leaf_head)); |
229 | ASSERT(!list_empty(&as->as_area_btree.leaf_head)); |
226 | node = list_get_instance(as->as_area_btree.leaf_head.next, |
230 | node = list_get_instance(as->as_area_btree.leaf_head.next, |
227 | btree_node_t, leaf_link); |
231 | btree_node_t, leaf_link); |
228 | 232 | ||
229 | if ((cond = node->keys)) { |
233 | if ((cond = node->keys)) { |
230 | as_area_destroy(as, node->key[0]); |
234 | as_area_destroy(as, node->key[0]); |
231 | } |
235 | } |
232 | } |
236 | } |
233 | 237 | ||
234 | btree_destroy(&as->as_area_btree); |
238 | btree_destroy(&as->as_area_btree); |
235 | #ifdef AS_PAGE_TABLE |
239 | #ifdef AS_PAGE_TABLE |
236 | page_table_destroy(as->genarch.page_table); |
240 | page_table_destroy(as->genarch.page_table); |
237 | #else |
241 | #else |
238 | page_table_destroy(NULL); |
242 | page_table_destroy(NULL); |
239 | #endif |
243 | #endif |
240 | 244 | ||
241 | interrupts_restore(ipl); |
245 | interrupts_restore(ipl); |
242 | 246 | ||
243 | #ifdef __OBJC__ |
247 | #ifdef __OBJC__ |
244 | [as free]; |
248 | [as free]; |
245 | #else |
249 | #else |
246 | slab_free(as_slab, as); |
250 | slab_free(as_slab, as); |
247 | #endif |
251 | #endif |
248 | } |
252 | } |
249 | 253 | ||
250 | /** Create address space area of common attributes. |
254 | /** Create address space area of common attributes. |
251 | * |
255 | * |
252 | * The created address space area is added to the target address space. |
256 | * The created address space area is added to the target address space. |
253 | * |
257 | * |
254 | * @param as Target address space. |
258 | * @param as Target address space. |
255 | * @param flags Flags of the area memory. |
259 | * @param flags Flags of the area memory. |
256 | * @param size Size of area. |
260 | * @param size Size of area. |
257 | * @param base Base address of area. |
261 | * @param base Base address of area. |
258 | * @param attrs Attributes of the area. |
262 | * @param attrs Attributes of the area. |
259 | * @param backend Address space area backend. NULL if no backend is used. |
263 | * @param backend Address space area backend. NULL if no backend is used. |
260 | * @param backend_data NULL or a pointer to an array holding two void *. |
264 | * @param backend_data NULL or a pointer to an array holding two void *. |
261 | * |
265 | * |
262 | * @return Address space area on success or NULL on failure. |
266 | * @return Address space area on success or NULL on failure. |
263 | */ |
267 | */ |
264 | as_area_t * |
268 | as_area_t * |
265 | as_area_create(as_t *as, int flags, size_t size, uintptr_t base, int attrs, |
269 | as_area_create(as_t *as, int flags, size_t size, uintptr_t base, int attrs, |
266 | mem_backend_t *backend, mem_backend_data_t *backend_data) |
270 | mem_backend_t *backend, mem_backend_data_t *backend_data) |
267 | { |
271 | { |
268 | ipl_t ipl; |
272 | ipl_t ipl; |
269 | as_area_t *a; |
273 | as_area_t *a; |
270 | 274 | ||
271 | if (base % PAGE_SIZE) |
275 | if (base % PAGE_SIZE) |
272 | return NULL; |
276 | return NULL; |
273 | 277 | ||
274 | if (!size) |
278 | if (!size) |
275 | return NULL; |
279 | return NULL; |
276 | 280 | ||
277 | /* Writeable executable areas are not supported. */ |
281 | /* Writeable executable areas are not supported. */ |
278 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE)) |
282 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE)) |
279 | return NULL; |
283 | return NULL; |
280 | 284 | ||
281 | ipl = interrupts_disable(); |
285 | ipl = interrupts_disable(); |
282 | mutex_lock(&as->lock); |
286 | mutex_lock(&as->lock); |
283 | 287 | ||
284 | if (!check_area_conflicts(as, base, size, NULL)) { |
288 | if (!check_area_conflicts(as, base, size, NULL)) { |
285 | mutex_unlock(&as->lock); |
289 | mutex_unlock(&as->lock); |
286 | interrupts_restore(ipl); |
290 | interrupts_restore(ipl); |
287 | return NULL; |
291 | return NULL; |
288 | } |
292 | } |
289 | 293 | ||
290 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
294 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
291 | 295 | ||
292 | mutex_initialize(&a->lock); |
296 | mutex_initialize(&a->lock); |
293 | 297 | ||
294 | a->as = as; |
298 | a->as = as; |
295 | a->flags = flags; |
299 | a->flags = flags; |
296 | a->attributes = attrs; |
300 | a->attributes = attrs; |
297 | a->pages = SIZE2FRAMES(size); |
301 | a->pages = SIZE2FRAMES(size); |
298 | a->base = base; |
302 | a->base = base; |
299 | a->sh_info = NULL; |
303 | a->sh_info = NULL; |
300 | a->backend = backend; |
304 | a->backend = backend; |
301 | if (backend_data) |
305 | if (backend_data) |
302 | a->backend_data = *backend_data; |
306 | a->backend_data = *backend_data; |
303 | else |
307 | else |
304 | memsetb((uintptr_t) &a->backend_data, sizeof(a->backend_data), |
308 | memsetb((uintptr_t) &a->backend_data, sizeof(a->backend_data), |
305 | 0); |
309 | 0); |
306 | 310 | ||
307 | btree_create(&a->used_space); |
311 | btree_create(&a->used_space); |
308 | 312 | ||
309 | btree_insert(&as->as_area_btree, base, (void *) a, NULL); |
313 | btree_insert(&as->as_area_btree, base, (void *) a, NULL); |
310 | 314 | ||
311 | mutex_unlock(&as->lock); |
315 | mutex_unlock(&as->lock); |
312 | interrupts_restore(ipl); |
316 | interrupts_restore(ipl); |
313 | 317 | ||
314 | return a; |
318 | return a; |
315 | } |
319 | } |
316 | 320 | ||
317 | /** Find address space area and change it. |
321 | /** Find address space area and change it. |
318 | * |
322 | * |
319 | * @param as Address space. |
323 | * @param as Address space. |
320 | * @param address Virtual address belonging to the area to be changed. Must be |
324 | * @param address Virtual address belonging to the area to be changed. Must be |
321 | * page-aligned. |
325 | * page-aligned. |
322 | * @param size New size of the virtual memory block starting at address. |
326 | * @param size New size of the virtual memory block starting at address. |
323 | * @param flags Flags influencing the remap operation. Currently unused. |
327 | * @param flags Flags influencing the remap operation. Currently unused. |
324 | * |
328 | * |
325 | * @return Zero on success or a value from @ref errno.h otherwise. |
329 | * @return Zero on success or a value from @ref errno.h otherwise. |
326 | */ |
330 | */ |
327 | int as_area_resize(as_t *as, uintptr_t address, size_t size, int flags) |
331 | int as_area_resize(as_t *as, uintptr_t address, size_t size, int flags) |
328 | { |
332 | { |
329 | as_area_t *area; |
333 | as_area_t *area; |
330 | ipl_t ipl; |
334 | ipl_t ipl; |
331 | size_t pages; |
335 | size_t pages; |
332 | 336 | ||
333 | ipl = interrupts_disable(); |
337 | ipl = interrupts_disable(); |
334 | mutex_lock(&as->lock); |
338 | mutex_lock(&as->lock); |
335 | 339 | ||
336 | /* |
340 | /* |
337 | * Locate the area. |
341 | * Locate the area. |
338 | */ |
342 | */ |
339 | area = find_area_and_lock(as, address); |
343 | area = find_area_and_lock(as, address); |
340 | if (!area) { |
344 | if (!area) { |
341 | mutex_unlock(&as->lock); |
345 | mutex_unlock(&as->lock); |
342 | interrupts_restore(ipl); |
346 | interrupts_restore(ipl); |
343 | return ENOENT; |
347 | return ENOENT; |
344 | } |
348 | } |
345 | 349 | ||
346 | if (area->backend == &phys_backend) { |
350 | if (area->backend == &phys_backend) { |
347 | /* |
351 | /* |
348 | * Remapping of address space areas associated |
352 | * Remapping of address space areas associated |
349 | * with memory mapped devices is not supported. |
353 | * with memory mapped devices is not supported. |
350 | */ |
354 | */ |
351 | mutex_unlock(&area->lock); |
355 | mutex_unlock(&area->lock); |
352 | mutex_unlock(&as->lock); |
356 | mutex_unlock(&as->lock); |
353 | interrupts_restore(ipl); |
357 | interrupts_restore(ipl); |
354 | return ENOTSUP; |
358 | return ENOTSUP; |
355 | } |
359 | } |
356 | if (area->sh_info) { |
360 | if (area->sh_info) { |
357 | /* |
361 | /* |
358 | * Remapping of shared address space areas |
362 | * Remapping of shared address space areas |
359 | * is not supported. |
363 | * is not supported. |
360 | */ |
364 | */ |
361 | mutex_unlock(&area->lock); |
365 | mutex_unlock(&area->lock); |
362 | mutex_unlock(&as->lock); |
366 | mutex_unlock(&as->lock); |
363 | interrupts_restore(ipl); |
367 | interrupts_restore(ipl); |
364 | return ENOTSUP; |
368 | return ENOTSUP; |
365 | } |
369 | } |
366 | 370 | ||
367 | pages = SIZE2FRAMES((address - area->base) + size); |
371 | pages = SIZE2FRAMES((address - area->base) + size); |
368 | if (!pages) { |
372 | if (!pages) { |
369 | /* |
373 | /* |
370 | * Zero size address space areas are not allowed. |
374 | * Zero size address space areas are not allowed. |
371 | */ |
375 | */ |
372 | mutex_unlock(&area->lock); |
376 | mutex_unlock(&area->lock); |
373 | mutex_unlock(&as->lock); |
377 | mutex_unlock(&as->lock); |
374 | interrupts_restore(ipl); |
378 | interrupts_restore(ipl); |
375 | return EPERM; |
379 | return EPERM; |
376 | } |
380 | } |
377 | 381 | ||
378 | if (pages < area->pages) { |
382 | if (pages < area->pages) { |
379 | bool cond; |
383 | bool cond; |
380 | uintptr_t start_free = area->base + pages*PAGE_SIZE; |
384 | uintptr_t start_free = area->base + pages*PAGE_SIZE; |
381 | 385 | ||
382 | /* |
386 | /* |
383 | * Shrinking the area. |
387 | * Shrinking the area. |
384 | * No need to check for overlaps. |
388 | * No need to check for overlaps. |
385 | */ |
389 | */ |
386 | 390 | ||
387 | /* |
391 | /* |
388 | * Start TLB shootdown sequence. |
392 | * Start TLB shootdown sequence. |
389 | */ |
393 | */ |
390 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + |
394 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + |
391 | pages * PAGE_SIZE, area->pages - pages); |
395 | pages * PAGE_SIZE, area->pages - pages); |
392 | 396 | ||
393 | /* |
397 | /* |
394 | * Remove frames belonging to used space starting from |
398 | * Remove frames belonging to used space starting from |
395 | * the highest addresses downwards until an overlap with |
399 | * the highest addresses downwards until an overlap with |
396 | * the resized address space area is found. Note that this |
400 | * the resized address space area is found. Note that this |
397 | * is also the right way to remove part of the used_space |
401 | * is also the right way to remove part of the used_space |
398 | * B+tree leaf list. |
402 | * B+tree leaf list. |
399 | */ |
403 | */ |
400 | for (cond = true; cond;) { |
404 | for (cond = true; cond;) { |
401 | btree_node_t *node; |
405 | btree_node_t *node; |
402 | 406 | ||
403 | ASSERT(!list_empty(&area->used_space.leaf_head)); |
407 | ASSERT(!list_empty(&area->used_space.leaf_head)); |
404 | node = |
408 | node = |
405 | list_get_instance(area->used_space.leaf_head.prev, |
409 | list_get_instance(area->used_space.leaf_head.prev, |
406 | btree_node_t, leaf_link); |
410 | btree_node_t, leaf_link); |
407 | if ((cond = (bool) node->keys)) { |
411 | if ((cond = (bool) node->keys)) { |
408 | uintptr_t b = node->key[node->keys - 1]; |
412 | uintptr_t b = node->key[node->keys - 1]; |
409 | count_t c = |
413 | count_t c = |
410 | (count_t) node->value[node->keys - 1]; |
414 | (count_t) node->value[node->keys - 1]; |
411 | int i = 0; |
415 | int i = 0; |
412 | 416 | ||
413 | if (overlaps(b, c * PAGE_SIZE, area->base, |
417 | if (overlaps(b, c * PAGE_SIZE, area->base, |
414 | pages * PAGE_SIZE)) { |
418 | pages * PAGE_SIZE)) { |
415 | 419 | ||
416 | if (b + c * PAGE_SIZE <= start_free) { |
420 | if (b + c * PAGE_SIZE <= start_free) { |
417 | /* |
421 | /* |
418 | * The whole interval fits |
422 | * The whole interval fits |
419 | * completely in the resized |
423 | * completely in the resized |
420 | * address space area. |
424 | * address space area. |
421 | */ |
425 | */ |
422 | break; |
426 | break; |
423 | } |
427 | } |
424 | 428 | ||
425 | /* |
429 | /* |
426 | * Part of the interval corresponding |
430 | * Part of the interval corresponding |
427 | * to b and c overlaps with the resized |
431 | * to b and c overlaps with the resized |
428 | * address space area. |
432 | * address space area. |
429 | */ |
433 | */ |
430 | 434 | ||
431 | cond = false; /* we are almost done */ |
435 | cond = false; /* we are almost done */ |
432 | i = (start_free - b) >> PAGE_WIDTH; |
436 | i = (start_free - b) >> PAGE_WIDTH; |
433 | if (!used_space_remove(area, start_free, |
437 | if (!used_space_remove(area, start_free, |
434 | c - i)) |
438 | c - i)) |
435 | panic("Could not remove used " |
439 | panic("Could not remove used " |
436 | "space.\n"); |
440 | "space.\n"); |
437 | } else { |
441 | } else { |
438 | /* |
442 | /* |
439 | * The interval of used space can be |
443 | * The interval of used space can be |
440 | * completely removed. |
444 | * completely removed. |
441 | */ |
445 | */ |
442 | if (!used_space_remove(area, b, c)) |
446 | if (!used_space_remove(area, b, c)) |
443 | panic("Could not remove used " |
447 | panic("Could not remove used " |
444 | "space.\n"); |
448 | "space.\n"); |
445 | } |
449 | } |
446 | 450 | ||
447 | for (; i < c; i++) { |
451 | for (; i < c; i++) { |
448 | pte_t *pte; |
452 | pte_t *pte; |
449 | 453 | ||
450 | page_table_lock(as, false); |
454 | page_table_lock(as, false); |
451 | pte = page_mapping_find(as, b + |
455 | pte = page_mapping_find(as, b + |
452 | i * PAGE_SIZE); |
456 | i * PAGE_SIZE); |
453 | ASSERT(pte && PTE_VALID(pte) && |
457 | ASSERT(pte && PTE_VALID(pte) && |
454 | PTE_PRESENT(pte)); |
458 | PTE_PRESENT(pte)); |
455 | if (area->backend && |
459 | if (area->backend && |
456 | area->backend->frame_free) { |
460 | area->backend->frame_free) { |
457 | area->backend->frame_free(area, |
461 | area->backend->frame_free(area, |
458 | b + i * PAGE_SIZE, |
462 | b + i * PAGE_SIZE, |
459 | PTE_GET_FRAME(pte)); |
463 | PTE_GET_FRAME(pte)); |
460 | } |
464 | } |
461 | page_mapping_remove(as, b + |
465 | page_mapping_remove(as, b + |
462 | i * PAGE_SIZE); |
466 | i * PAGE_SIZE); |
463 | page_table_unlock(as, false); |
467 | page_table_unlock(as, false); |
464 | } |
468 | } |
465 | } |
469 | } |
466 | } |
470 | } |
467 | 471 | ||
468 | /* |
472 | /* |
469 | * Finish TLB shootdown sequence. |
473 | * Finish TLB shootdown sequence. |
470 | */ |
474 | */ |
471 | tlb_invalidate_pages(as->asid, area->base + pages * PAGE_SIZE, |
475 | tlb_invalidate_pages(as->asid, area->base + pages * PAGE_SIZE, |
472 | area->pages - pages); |
476 | area->pages - pages); |
473 | tlb_shootdown_finalize(); |
477 | tlb_shootdown_finalize(); |
474 | 478 | ||
475 | /* |
479 | /* |
476 | * Invalidate software translation caches (e.g. TSB on sparc64). |
480 | * Invalidate software translation caches (e.g. TSB on sparc64). |
477 | */ |
481 | */ |
478 | as_invalidate_translation_cache(as, area->base + |
482 | as_invalidate_translation_cache(as, area->base + |
479 | pages * PAGE_SIZE, area->pages - pages); |
483 | pages * PAGE_SIZE, area->pages - pages); |
480 | } else { |
484 | } else { |
481 | /* |
485 | /* |
482 | * Growing the area. |
486 | * Growing the area. |
483 | * Check for overlaps with other address space areas. |
487 | * Check for overlaps with other address space areas. |
484 | */ |
488 | */ |
485 | if (!check_area_conflicts(as, address, pages * PAGE_SIZE, |
489 | if (!check_area_conflicts(as, address, pages * PAGE_SIZE, |
486 | area)) { |
490 | area)) { |
487 | mutex_unlock(&area->lock); |
491 | mutex_unlock(&area->lock); |
488 | mutex_unlock(&as->lock); |
492 | mutex_unlock(&as->lock); |
489 | interrupts_restore(ipl); |
493 | interrupts_restore(ipl); |
490 | return EADDRNOTAVAIL; |
494 | return EADDRNOTAVAIL; |
491 | } |
495 | } |
492 | } |
496 | } |
493 | 497 | ||
494 | area->pages = pages; |
498 | area->pages = pages; |
495 | 499 | ||
496 | mutex_unlock(&area->lock); |
500 | mutex_unlock(&area->lock); |
497 | mutex_unlock(&as->lock); |
501 | mutex_unlock(&as->lock); |
498 | interrupts_restore(ipl); |
502 | interrupts_restore(ipl); |
499 | 503 | ||
500 | return 0; |
504 | return 0; |
501 | } |
505 | } |
502 | 506 | ||
503 | /** Destroy address space area. |
507 | /** Destroy address space area. |
504 | * |
508 | * |
505 | * @param as Address space. |
509 | * @param as Address space. |
506 | * @param address Address withing the area to be deleted. |
510 | * @param address Address withing the area to be deleted. |
507 | * |
511 | * |
508 | * @return Zero on success or a value from @ref errno.h on failure. |
512 | * @return Zero on success or a value from @ref errno.h on failure. |
509 | */ |
513 | */ |
510 | int as_area_destroy(as_t *as, uintptr_t address) |
514 | int as_area_destroy(as_t *as, uintptr_t address) |
511 | { |
515 | { |
512 | as_area_t *area; |
516 | as_area_t *area; |
513 | uintptr_t base; |
517 | uintptr_t base; |
514 | link_t *cur; |
518 | link_t *cur; |
515 | ipl_t ipl; |
519 | ipl_t ipl; |
516 | 520 | ||
517 | ipl = interrupts_disable(); |
521 | ipl = interrupts_disable(); |
518 | mutex_lock(&as->lock); |
522 | mutex_lock(&as->lock); |
519 | 523 | ||
520 | area = find_area_and_lock(as, address); |
524 | area = find_area_and_lock(as, address); |
521 | if (!area) { |
525 | if (!area) { |
522 | mutex_unlock(&as->lock); |
526 | mutex_unlock(&as->lock); |
523 | interrupts_restore(ipl); |
527 | interrupts_restore(ipl); |
524 | return ENOENT; |
528 | return ENOENT; |
525 | } |
529 | } |
526 | 530 | ||
527 | base = area->base; |
531 | base = area->base; |
528 | 532 | ||
529 | /* |
533 | /* |
530 | * Start TLB shootdown sequence. |
534 | * Start TLB shootdown sequence. |
531 | */ |
535 | */ |
532 | tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base, area->pages); |
536 | tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base, area->pages); |
533 | 537 | ||
534 | /* |
538 | /* |
535 | * Visit only the pages mapped by used_space B+tree. |
539 | * Visit only the pages mapped by used_space B+tree. |
536 | */ |
540 | */ |
537 | for (cur = area->used_space.leaf_head.next; |
541 | for (cur = area->used_space.leaf_head.next; |
538 | cur != &area->used_space.leaf_head; cur = cur->next) { |
542 | cur != &area->used_space.leaf_head; cur = cur->next) { |
539 | btree_node_t *node; |
543 | btree_node_t *node; |
540 | int i; |
544 | int i; |
541 | 545 | ||
542 | node = list_get_instance(cur, btree_node_t, leaf_link); |
546 | node = list_get_instance(cur, btree_node_t, leaf_link); |
543 | for (i = 0; i < node->keys; i++) { |
547 | for (i = 0; i < node->keys; i++) { |
544 | uintptr_t b = node->key[i]; |
548 | uintptr_t b = node->key[i]; |
545 | count_t j; |
549 | count_t j; |
546 | pte_t *pte; |
550 | pte_t *pte; |
547 | 551 | ||
548 | for (j = 0; j < (count_t) node->value[i]; j++) { |
552 | for (j = 0; j < (count_t) node->value[i]; j++) { |
549 | page_table_lock(as, false); |
553 | page_table_lock(as, false); |
550 | pte = page_mapping_find(as, b + j * PAGE_SIZE); |
554 | pte = page_mapping_find(as, b + j * PAGE_SIZE); |
551 | ASSERT(pte && PTE_VALID(pte) && |
555 | ASSERT(pte && PTE_VALID(pte) && |
552 | PTE_PRESENT(pte)); |
556 | PTE_PRESENT(pte)); |
553 | if (area->backend && |
557 | if (area->backend && |
554 | area->backend->frame_free) { |
558 | area->backend->frame_free) { |
555 | area->backend->frame_free(area, b + |
559 | area->backend->frame_free(area, b + |
556 | j * PAGE_SIZE, PTE_GET_FRAME(pte)); |
560 | j * PAGE_SIZE, PTE_GET_FRAME(pte)); |
557 | } |
561 | } |
558 | page_mapping_remove(as, b + j * PAGE_SIZE); |
562 | page_mapping_remove(as, b + j * PAGE_SIZE); |
559 | page_table_unlock(as, false); |
563 | page_table_unlock(as, false); |
560 | } |
564 | } |
561 | } |
565 | } |
562 | } |
566 | } |
563 | 567 | ||
564 | /* |
568 | /* |
565 | * Finish TLB shootdown sequence. |
569 | * Finish TLB shootdown sequence. |
566 | */ |
570 | */ |
567 | tlb_invalidate_pages(as->asid, area->base, area->pages); |
571 | tlb_invalidate_pages(as->asid, area->base, area->pages); |
568 | tlb_shootdown_finalize(); |
572 | tlb_shootdown_finalize(); |
569 | 573 | ||
570 | /* |
574 | /* |
571 | * Invalidate potential software translation caches (e.g. TSB on |
575 | * Invalidate potential software translation caches (e.g. TSB on |
572 | * sparc64). |
576 | * sparc64). |
573 | */ |
577 | */ |
574 | as_invalidate_translation_cache(as, area->base, area->pages); |
578 | as_invalidate_translation_cache(as, area->base, area->pages); |
575 | 579 | ||
576 | btree_destroy(&area->used_space); |
580 | btree_destroy(&area->used_space); |
577 | 581 | ||
578 | area->attributes |= AS_AREA_ATTR_PARTIAL; |
582 | area->attributes |= AS_AREA_ATTR_PARTIAL; |
579 | 583 | ||
580 | if (area->sh_info) |
584 | if (area->sh_info) |
581 | sh_info_remove_reference(area->sh_info); |
585 | sh_info_remove_reference(area->sh_info); |
582 | 586 | ||
583 | mutex_unlock(&area->lock); |
587 | mutex_unlock(&area->lock); |
584 | 588 | ||
585 | /* |
589 | /* |
586 | * Remove the empty area from address space. |
590 | * Remove the empty area from address space. |
587 | */ |
591 | */ |
588 | btree_remove(&as->as_area_btree, base, NULL); |
592 | btree_remove(&as->as_area_btree, base, NULL); |
589 | 593 | ||
590 | free(area); |
594 | free(area); |
591 | 595 | ||
592 | mutex_unlock(&as->lock); |
596 | mutex_unlock(&as->lock); |
593 | interrupts_restore(ipl); |
597 | interrupts_restore(ipl); |
594 | return 0; |
598 | return 0; |
595 | } |
599 | } |
596 | 600 | ||
597 | /** Share address space area with another or the same address space. |
601 | /** Share address space area with another or the same address space. |
598 | * |
602 | * |
599 | * Address space area mapping is shared with a new address space area. |
603 | * Address space area mapping is shared with a new address space area. |
600 | * If the source address space area has not been shared so far, |
604 | * If the source address space area has not been shared so far, |
601 | * a new sh_info is created. The new address space area simply gets the |
605 | * a new sh_info is created. The new address space area simply gets the |
602 | * sh_info of the source area. The process of duplicating the |
606 | * sh_info of the source area. The process of duplicating the |
603 | * mapping is done through the backend share function. |
607 | * mapping is done through the backend share function. |
604 | * |
608 | * |
605 | * @param src_as Pointer to source address space. |
609 | * @param src_as Pointer to source address space. |
606 | * @param src_base Base address of the source address space area. |
610 | * @param src_base Base address of the source address space area. |
607 | * @param acc_size Expected size of the source area. |
611 | * @param acc_size Expected size of the source area. |
608 | * @param dst_as Pointer to destination address space. |
612 | * @param dst_as Pointer to destination address space. |
609 | * @param dst_base Target base address. |
613 | * @param dst_base Target base address. |
610 | * @param dst_flags_mask Destination address space area flags mask. |
614 | * @param dst_flags_mask Destination address space area flags mask. |
611 | * |
615 | * |
612 | * @return Zero on success or ENOENT if there is no such task or if there is no |
616 | * @return Zero on success or ENOENT if there is no such task or if there is no |
613 | * such address space area, EPERM if there was a problem in accepting the area |
617 | * such address space area, EPERM if there was a problem in accepting the area |
614 | * or ENOMEM if there was a problem in allocating destination address space |
618 | * or ENOMEM if there was a problem in allocating destination address space |
615 | * area. ENOTSUP is returned if the address space area backend does not support |
619 | * area. ENOTSUP is returned if the address space area backend does not support |
616 | * sharing. |
620 | * sharing. |
617 | */ |
621 | */ |
618 | int as_area_share(as_t *src_as, uintptr_t src_base, size_t acc_size, |
622 | int as_area_share(as_t *src_as, uintptr_t src_base, size_t acc_size, |
619 | as_t *dst_as, uintptr_t dst_base, int dst_flags_mask) |
623 | as_t *dst_as, uintptr_t dst_base, int dst_flags_mask) |
620 | { |
624 | { |
621 | ipl_t ipl; |
625 | ipl_t ipl; |
622 | int src_flags; |
626 | int src_flags; |
623 | size_t src_size; |
627 | size_t src_size; |
624 | as_area_t *src_area, *dst_area; |
628 | as_area_t *src_area, *dst_area; |
625 | share_info_t *sh_info; |
629 | share_info_t *sh_info; |
626 | mem_backend_t *src_backend; |
630 | mem_backend_t *src_backend; |
627 | mem_backend_data_t src_backend_data; |
631 | mem_backend_data_t src_backend_data; |
628 | 632 | ||
629 | ipl = interrupts_disable(); |
633 | ipl = interrupts_disable(); |
630 | mutex_lock(&src_as->lock); |
634 | mutex_lock(&src_as->lock); |
631 | src_area = find_area_and_lock(src_as, src_base); |
635 | src_area = find_area_and_lock(src_as, src_base); |
632 | if (!src_area) { |
636 | if (!src_area) { |
633 | /* |
637 | /* |
634 | * Could not find the source address space area. |
638 | * Could not find the source address space area. |
635 | */ |
639 | */ |
636 | mutex_unlock(&src_as->lock); |
640 | mutex_unlock(&src_as->lock); |
637 | interrupts_restore(ipl); |
641 | interrupts_restore(ipl); |
638 | return ENOENT; |
642 | return ENOENT; |
639 | } |
643 | } |
640 | 644 | ||
641 | if (!src_area->backend || !src_area->backend->share) { |
645 | if (!src_area->backend || !src_area->backend->share) { |
642 | /* |
646 | /* |
643 | * There is no backend or the backend does not |
647 | * There is no backend or the backend does not |
644 | * know how to share the area. |
648 | * know how to share the area. |
645 | */ |
649 | */ |
646 | mutex_unlock(&src_area->lock); |
650 | mutex_unlock(&src_area->lock); |
647 | mutex_unlock(&src_as->lock); |
651 | mutex_unlock(&src_as->lock); |
648 | interrupts_restore(ipl); |
652 | interrupts_restore(ipl); |
649 | return ENOTSUP; |
653 | return ENOTSUP; |
650 | } |
654 | } |
651 | 655 | ||
652 | src_size = src_area->pages * PAGE_SIZE; |
656 | src_size = src_area->pages * PAGE_SIZE; |
653 | src_flags = src_area->flags; |
657 | src_flags = src_area->flags; |
654 | src_backend = src_area->backend; |
658 | src_backend = src_area->backend; |
655 | src_backend_data = src_area->backend_data; |
659 | src_backend_data = src_area->backend_data; |
656 | 660 | ||
657 | /* Share the cacheable flag from the original mapping */ |
661 | /* Share the cacheable flag from the original mapping */ |
658 | if (src_flags & AS_AREA_CACHEABLE) |
662 | if (src_flags & AS_AREA_CACHEABLE) |
659 | dst_flags_mask |= AS_AREA_CACHEABLE; |
663 | dst_flags_mask |= AS_AREA_CACHEABLE; |
660 | 664 | ||
661 | if (src_size != acc_size || |
665 | if (src_size != acc_size || |
662 | (src_flags & dst_flags_mask) != dst_flags_mask) { |
666 | (src_flags & dst_flags_mask) != dst_flags_mask) { |
663 | mutex_unlock(&src_area->lock); |
667 | mutex_unlock(&src_area->lock); |
664 | mutex_unlock(&src_as->lock); |
668 | mutex_unlock(&src_as->lock); |
665 | interrupts_restore(ipl); |
669 | interrupts_restore(ipl); |
666 | return EPERM; |
670 | return EPERM; |
667 | } |
671 | } |
668 | 672 | ||
669 | /* |
673 | /* |
670 | * Now we are committed to sharing the area. |
674 | * Now we are committed to sharing the area. |
671 | * First, prepare the area for sharing. |
675 | * First, prepare the area for sharing. |
672 | * Then it will be safe to unlock it. |
676 | * Then it will be safe to unlock it. |
673 | */ |
677 | */ |
674 | sh_info = src_area->sh_info; |
678 | sh_info = src_area->sh_info; |
675 | if (!sh_info) { |
679 | if (!sh_info) { |
676 | sh_info = (share_info_t *) malloc(sizeof(share_info_t), 0); |
680 | sh_info = (share_info_t *) malloc(sizeof(share_info_t), 0); |
677 | mutex_initialize(&sh_info->lock); |
681 | mutex_initialize(&sh_info->lock); |
678 | sh_info->refcount = 2; |
682 | sh_info->refcount = 2; |
679 | btree_create(&sh_info->pagemap); |
683 | btree_create(&sh_info->pagemap); |
680 | src_area->sh_info = sh_info; |
684 | src_area->sh_info = sh_info; |
681 | } else { |
685 | } else { |
682 | mutex_lock(&sh_info->lock); |
686 | mutex_lock(&sh_info->lock); |
683 | sh_info->refcount++; |
687 | sh_info->refcount++; |
684 | mutex_unlock(&sh_info->lock); |
688 | mutex_unlock(&sh_info->lock); |
685 | } |
689 | } |
686 | 690 | ||
687 | src_area->backend->share(src_area); |
691 | src_area->backend->share(src_area); |
688 | 692 | ||
689 | mutex_unlock(&src_area->lock); |
693 | mutex_unlock(&src_area->lock); |
690 | mutex_unlock(&src_as->lock); |
694 | mutex_unlock(&src_as->lock); |
691 | 695 | ||
692 | /* |
696 | /* |
693 | * Create copy of the source address space area. |
697 | * Create copy of the source address space area. |
694 | * The destination area is created with AS_AREA_ATTR_PARTIAL |
698 | * The destination area is created with AS_AREA_ATTR_PARTIAL |
695 | * attribute set which prevents race condition with |
699 | * attribute set which prevents race condition with |
696 | * preliminary as_page_fault() calls. |
700 | * preliminary as_page_fault() calls. |
697 | * The flags of the source area are masked against dst_flags_mask |
701 | * The flags of the source area are masked against dst_flags_mask |
698 | * to support sharing in less privileged mode. |
702 | * to support sharing in less privileged mode. |
699 | */ |
703 | */ |
700 | dst_area = as_area_create(dst_as, dst_flags_mask, src_size, dst_base, |
704 | dst_area = as_area_create(dst_as, dst_flags_mask, src_size, dst_base, |
701 | AS_AREA_ATTR_PARTIAL, src_backend, &src_backend_data); |
705 | AS_AREA_ATTR_PARTIAL, src_backend, &src_backend_data); |
702 | if (!dst_area) { |
706 | if (!dst_area) { |
703 | /* |
707 | /* |
704 | * Destination address space area could not be created. |
708 | * Destination address space area could not be created. |
705 | */ |
709 | */ |
706 | sh_info_remove_reference(sh_info); |
710 | sh_info_remove_reference(sh_info); |
707 | 711 | ||
708 | interrupts_restore(ipl); |
712 | interrupts_restore(ipl); |
709 | return ENOMEM; |
713 | return ENOMEM; |
710 | } |
714 | } |
711 | 715 | ||
712 | /* |
716 | /* |
713 | * Now the destination address space area has been |
717 | * Now the destination address space area has been |
714 | * fully initialized. Clear the AS_AREA_ATTR_PARTIAL |
718 | * fully initialized. Clear the AS_AREA_ATTR_PARTIAL |
715 | * attribute and set the sh_info. |
719 | * attribute and set the sh_info. |
716 | */ |
720 | */ |
717 | mutex_lock(&dst_as->lock); |
721 | mutex_lock(&dst_as->lock); |
718 | mutex_lock(&dst_area->lock); |
722 | mutex_lock(&dst_area->lock); |
719 | dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL; |
723 | dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL; |
720 | dst_area->sh_info = sh_info; |
724 | dst_area->sh_info = sh_info; |
721 | mutex_unlock(&dst_area->lock); |
725 | mutex_unlock(&dst_area->lock); |
722 | mutex_unlock(&dst_as->lock); |
726 | mutex_unlock(&dst_as->lock); |
723 | 727 | ||
724 | interrupts_restore(ipl); |
728 | interrupts_restore(ipl); |
725 | 729 | ||
726 | return 0; |
730 | return 0; |
727 | } |
731 | } |
728 | 732 | ||
729 | /** Check access mode for address space area. |
733 | /** Check access mode for address space area. |
730 | * |
734 | * |
731 | * The address space area must be locked prior to this call. |
735 | * The address space area must be locked prior to this call. |
732 | * |
736 | * |
733 | * @param area Address space area. |
737 | * @param area Address space area. |
734 | * @param access Access mode. |
738 | * @param access Access mode. |
735 | * |
739 | * |
736 | * @return False if access violates area's permissions, true otherwise. |
740 | * @return False if access violates area's permissions, true otherwise. |
737 | */ |
741 | */ |
738 | bool as_area_check_access(as_area_t *area, pf_access_t access) |
742 | bool as_area_check_access(as_area_t *area, pf_access_t access) |
739 | { |
743 | { |
740 | int flagmap[] = { |
744 | int flagmap[] = { |
741 | [PF_ACCESS_READ] = AS_AREA_READ, |
745 | [PF_ACCESS_READ] = AS_AREA_READ, |
742 | [PF_ACCESS_WRITE] = AS_AREA_WRITE, |
746 | [PF_ACCESS_WRITE] = AS_AREA_WRITE, |
743 | [PF_ACCESS_EXEC] = AS_AREA_EXEC |
747 | [PF_ACCESS_EXEC] = AS_AREA_EXEC |
744 | }; |
748 | }; |
745 | 749 | ||
746 | if (!(area->flags & flagmap[access])) |
750 | if (!(area->flags & flagmap[access])) |
747 | return false; |
751 | return false; |
748 | 752 | ||
749 | return true; |
753 | return true; |
750 | } |
754 | } |
751 | 755 | ||
752 | /** Handle page fault within the current address space. |
756 | /** Handle page fault within the current address space. |
753 | * |
757 | * |
754 | * This is the high-level page fault handler. It decides |
758 | * This is the high-level page fault handler. It decides |
755 | * whether the page fault can be resolved by any backend |
759 | * whether the page fault can be resolved by any backend |
756 | * and if so, it invokes the backend to resolve the page |
760 | * and if so, it invokes the backend to resolve the page |
757 | * fault. |
761 | * fault. |
758 | * |
762 | * |
759 | * Interrupts are assumed disabled. |
763 | * Interrupts are assumed disabled. |
760 | * |
764 | * |
761 | * @param page Faulting page. |
765 | * @param page Faulting page. |
762 | * @param access Access mode that caused the fault (i.e. read/write/exec). |
766 | * @param access Access mode that caused the fault (i.e. read/write/exec). |
763 | * @param istate Pointer to interrupted state. |
767 | * @param istate Pointer to interrupted state. |
764 | * |
768 | * |
765 | * @return AS_PF_FAULT on page fault, AS_PF_OK on success or AS_PF_DEFER if the |
769 | * @return AS_PF_FAULT on page fault, AS_PF_OK on success or AS_PF_DEFER if the |
766 | * fault was caused by copy_to_uspace() or copy_from_uspace(). |
770 | * fault was caused by copy_to_uspace() or copy_from_uspace(). |
767 | */ |
771 | */ |
768 | int as_page_fault(uintptr_t page, pf_access_t access, istate_t *istate) |
772 | int as_page_fault(uintptr_t page, pf_access_t access, istate_t *istate) |
769 | { |
773 | { |
770 | pte_t *pte; |
774 | pte_t *pte; |
771 | as_area_t *area; |
775 | as_area_t *area; |
772 | 776 | ||
773 | if (!THREAD) |
777 | if (!THREAD) |
774 | return AS_PF_FAULT; |
778 | return AS_PF_FAULT; |
775 | 779 | ||
776 | ASSERT(AS); |
780 | ASSERT(AS); |
777 | 781 | ||
778 | mutex_lock(&AS->lock); |
782 | mutex_lock(&AS->lock); |
779 | area = find_area_and_lock(AS, page); |
783 | area = find_area_and_lock(AS, page); |
780 | if (!area) { |
784 | if (!area) { |
781 | /* |
785 | /* |
782 | * No area contained mapping for 'page'. |
786 | * No area contained mapping for 'page'. |
783 | * Signal page fault to low-level handler. |
787 | * Signal page fault to low-level handler. |
784 | */ |
788 | */ |
785 | mutex_unlock(&AS->lock); |
789 | mutex_unlock(&AS->lock); |
786 | goto page_fault; |
790 | goto page_fault; |
787 | } |
791 | } |
788 | 792 | ||
789 | if (area->attributes & AS_AREA_ATTR_PARTIAL) { |
793 | if (area->attributes & AS_AREA_ATTR_PARTIAL) { |
790 | /* |
794 | /* |
791 | * The address space area is not fully initialized. |
795 | * The address space area is not fully initialized. |
792 | * Avoid possible race by returning error. |
796 | * Avoid possible race by returning error. |
793 | */ |
797 | */ |
794 | mutex_unlock(&area->lock); |
798 | mutex_unlock(&area->lock); |
795 | mutex_unlock(&AS->lock); |
799 | mutex_unlock(&AS->lock); |
796 | goto page_fault; |
800 | goto page_fault; |
797 | } |
801 | } |
798 | 802 | ||
799 | if (!area->backend || !area->backend->page_fault) { |
803 | if (!area->backend || !area->backend->page_fault) { |
800 | /* |
804 | /* |
801 | * The address space area is not backed by any backend |
805 | * The address space area is not backed by any backend |
802 | * or the backend cannot handle page faults. |
806 | * or the backend cannot handle page faults. |
803 | */ |
807 | */ |
804 | mutex_unlock(&area->lock); |
808 | mutex_unlock(&area->lock); |
805 | mutex_unlock(&AS->lock); |
809 | mutex_unlock(&AS->lock); |
806 | goto page_fault; |
810 | goto page_fault; |
807 | } |
811 | } |
808 | 812 | ||
809 | page_table_lock(AS, false); |
813 | page_table_lock(AS, false); |
810 | 814 | ||
811 | /* |
815 | /* |
812 | * To avoid race condition between two page faults |
816 | * To avoid race condition between two page faults |
813 | * on the same address, we need to make sure |
817 | * on the same address, we need to make sure |
814 | * the mapping has not been already inserted. |
818 | * the mapping has not been already inserted. |
815 | */ |
819 | */ |
816 | if ((pte = page_mapping_find(AS, page))) { |
820 | if ((pte = page_mapping_find(AS, page))) { |
817 | if (PTE_PRESENT(pte)) { |
821 | if (PTE_PRESENT(pte)) { |
818 | if (((access == PF_ACCESS_READ) && PTE_READABLE(pte)) || |
822 | if (((access == PF_ACCESS_READ) && PTE_READABLE(pte)) || |
819 | (access == PF_ACCESS_WRITE && PTE_WRITABLE(pte)) || |
823 | (access == PF_ACCESS_WRITE && PTE_WRITABLE(pte)) || |
820 | (access == PF_ACCESS_EXEC && PTE_EXECUTABLE(pte))) { |
824 | (access == PF_ACCESS_EXEC && PTE_EXECUTABLE(pte))) { |
821 | page_table_unlock(AS, false); |
825 | page_table_unlock(AS, false); |
822 | mutex_unlock(&area->lock); |
826 | mutex_unlock(&area->lock); |
823 | mutex_unlock(&AS->lock); |
827 | mutex_unlock(&AS->lock); |
824 | return AS_PF_OK; |
828 | return AS_PF_OK; |
825 | } |
829 | } |
826 | } |
830 | } |
827 | } |
831 | } |
828 | 832 | ||
829 | /* |
833 | /* |
830 | * Resort to the backend page fault handler. |
834 | * Resort to the backend page fault handler. |
831 | */ |
835 | */ |
832 | if (area->backend->page_fault(area, page, access) != AS_PF_OK) { |
836 | if (area->backend->page_fault(area, page, access) != AS_PF_OK) { |
833 | page_table_unlock(AS, false); |
837 | page_table_unlock(AS, false); |
834 | mutex_unlock(&area->lock); |
838 | mutex_unlock(&area->lock); |
835 | mutex_unlock(&AS->lock); |
839 | mutex_unlock(&AS->lock); |
836 | goto page_fault; |
840 | goto page_fault; |
837 | } |
841 | } |
838 | 842 | ||
839 | page_table_unlock(AS, false); |
843 | page_table_unlock(AS, false); |
840 | mutex_unlock(&area->lock); |
844 | mutex_unlock(&area->lock); |
841 | mutex_unlock(&AS->lock); |
845 | mutex_unlock(&AS->lock); |
842 | return AS_PF_OK; |
846 | return AS_PF_OK; |
843 | 847 | ||
844 | page_fault: |
848 | page_fault: |
845 | if (THREAD->in_copy_from_uspace) { |
849 | if (THREAD->in_copy_from_uspace) { |
846 | THREAD->in_copy_from_uspace = false; |
850 | THREAD->in_copy_from_uspace = false; |
847 | istate_set_retaddr(istate, |
851 | istate_set_retaddr(istate, |
848 | (uintptr_t) &memcpy_from_uspace_failover_address); |
852 | (uintptr_t) &memcpy_from_uspace_failover_address); |
849 | } else if (THREAD->in_copy_to_uspace) { |
853 | } else if (THREAD->in_copy_to_uspace) { |
850 | THREAD->in_copy_to_uspace = false; |
854 | THREAD->in_copy_to_uspace = false; |
851 | istate_set_retaddr(istate, |
855 | istate_set_retaddr(istate, |
852 | (uintptr_t) &memcpy_to_uspace_failover_address); |
856 | (uintptr_t) &memcpy_to_uspace_failover_address); |
853 | } else { |
857 | } else { |
854 | return AS_PF_FAULT; |
858 | return AS_PF_FAULT; |
855 | } |
859 | } |
856 | 860 | ||
857 | return AS_PF_DEFER; |
861 | return AS_PF_DEFER; |
858 | } |
862 | } |
859 | 863 | ||
860 | /** Switch address spaces. |
864 | /** Switch address spaces. |
861 | * |
865 | * |
862 | * Note that this function cannot sleep as it is essentially a part of |
866 | * Note that this function cannot sleep as it is essentially a part of |
863 | * scheduling. Sleeping here would lead to deadlock on wakeup. |
867 | * scheduling. Sleeping here would lead to deadlock on wakeup. Another |
- | 868 | * thing which is forbidden in this context is locking the address space. |
|
864 | * |
869 | * |
865 | * @param old Old address space or NULL. |
870 | * @param old Old address space or NULL. |
866 | * @param new New address space. |
871 | * @param new New address space. |
867 | */ |
872 | */ |
868 | void as_switch(as_t *old_as, as_t *new_as) |
873 | void as_switch(as_t *old_as, as_t *new_as) |
869 | { |
874 | { |
870 | ipl_t ipl; |
- | |
871 | bool needs_asid = false; |
- | |
872 | - | ||
873 | ipl = interrupts_disable(); |
- | |
874 | spinlock_lock(&inactive_as_with_asid_lock); |
875 | spinlock_lock(&asidlock); |
875 | 876 | ||
876 | /* |
877 | /* |
877 | * First, take care of the old address space. |
878 | * First, take care of the old address space. |
878 | */ |
879 | */ |
879 | if (old_as) { |
880 | if (old_as) { |
880 | mutex_lock_active(&old_as->lock); |
- | |
881 | ASSERT(old_as->cpu_refcount); |
881 | ASSERT(old_as->cpu_refcount); |
882 | if((--old_as->cpu_refcount == 0) && (old_as != AS_KERNEL)) { |
882 | if((--old_as->cpu_refcount == 0) && (old_as != AS_KERNEL)) { |
883 | /* |
883 | /* |
884 | * The old address space is no longer active on |
884 | * The old address space is no longer active on |
885 | * any processor. It can be appended to the |
885 | * any processor. It can be appended to the |
886 | * list of inactive address spaces with assigned |
886 | * list of inactive address spaces with assigned |
887 | * ASID. |
887 | * ASID. |
888 | */ |
888 | */ |
889 | ASSERT(old_as->asid != ASID_INVALID); |
889 | ASSERT(old_as->asid != ASID_INVALID); |
890 | list_append(&old_as->inactive_as_with_asid_link, |
890 | list_append(&old_as->inactive_as_with_asid_link, |
891 | &inactive_as_with_asid_head); |
891 | &inactive_as_with_asid_head); |
892 | } |
892 | } |
893 | mutex_unlock(&old_as->lock); |
- | |
894 | 893 | ||
895 | /* |
894 | /* |
896 | * Perform architecture-specific tasks when the address space |
895 | * Perform architecture-specific tasks when the address space |
897 | * is being removed from the CPU. |
896 | * is being removed from the CPU. |
898 | */ |
897 | */ |
899 | as_deinstall_arch(old_as); |
898 | as_deinstall_arch(old_as); |
900 | } |
899 | } |
901 | 900 | ||
902 | /* |
901 | /* |
903 | * Second, prepare the new address space. |
902 | * Second, prepare the new address space. |
904 | */ |
903 | */ |
905 | mutex_lock_active(&new_as->lock); |
- | |
906 | if ((new_as->cpu_refcount++ == 0) && (new_as != AS_KERNEL)) { |
904 | if ((new_as->cpu_refcount++ == 0) && (new_as != AS_KERNEL)) { |
907 | if (new_as->asid != ASID_INVALID) { |
905 | if (new_as->asid != ASID_INVALID) |
908 | list_remove(&new_as->inactive_as_with_asid_link); |
906 | list_remove(&new_as->inactive_as_with_asid_link); |
909 | } else { |
907 | else |
910 | /* |
- | |
911 | * Defer call to asid_get() until new_as->lock is released. |
- | |
912 | */ |
- | |
913 | needs_asid = true; |
908 | new_as->asid = asid_get(); |
914 | } |
- | |
915 | } |
909 | } |
916 | #ifdef AS_PAGE_TABLE |
910 | #ifdef AS_PAGE_TABLE |
917 | SET_PTL0_ADDRESS(new_as->genarch.page_table); |
911 | SET_PTL0_ADDRESS(new_as->genarch.page_table); |
918 | #endif |
912 | #endif |
919 | mutex_unlock(&new_as->lock); |
- | |
920 | - | ||
921 | if (needs_asid) { |
- | |
922 | /* |
- | |
923 | * Allocation of new ASID was deferred |
- | |
924 | * until now in order to avoid deadlock. |
- | |
925 | */ |
- | |
926 | asid_t asid; |
- | |
927 | - | ||
928 | asid = asid_get(); |
- | |
929 | mutex_lock_active(&new_as->lock); |
- | |
930 | new_as->asid = asid; |
- | |
931 | mutex_unlock(&new_as->lock); |
- | |
932 | } |
- | |
933 | spinlock_unlock(&inactive_as_with_asid_lock); |
- | |
934 | interrupts_restore(ipl); |
- | |
935 | 913 | ||
936 | /* |
914 | /* |
937 | * Perform architecture-specific steps. |
915 | * Perform architecture-specific steps. |
938 | * (e.g. write ASID to hardware register etc.) |
916 | * (e.g. write ASID to hardware register etc.) |
939 | */ |
917 | */ |
940 | as_install_arch(new_as); |
918 | as_install_arch(new_as); |
- | 919 | ||
- | 920 | spinlock_unlock(&asidlock); |
|
941 | 921 | ||
942 | AS = new_as; |
922 | AS = new_as; |
943 | } |
923 | } |
944 | 924 | ||
945 | /** Convert address space area flags to page flags. |
925 | /** Convert address space area flags to page flags. |
946 | * |
926 | * |
947 | * @param aflags Flags of some address space area. |
927 | * @param aflags Flags of some address space area. |
948 | * |
928 | * |
949 | * @return Flags to be passed to page_mapping_insert(). |
929 | * @return Flags to be passed to page_mapping_insert(). |
950 | */ |
930 | */ |
951 | int area_flags_to_page_flags(int aflags) |
931 | int area_flags_to_page_flags(int aflags) |
952 | { |
932 | { |
953 | int flags; |
933 | int flags; |
954 | 934 | ||
955 | flags = PAGE_USER | PAGE_PRESENT; |
935 | flags = PAGE_USER | PAGE_PRESENT; |
956 | 936 | ||
957 | if (aflags & AS_AREA_READ) |
937 | if (aflags & AS_AREA_READ) |
958 | flags |= PAGE_READ; |
938 | flags |= PAGE_READ; |
959 | 939 | ||
960 | if (aflags & AS_AREA_WRITE) |
940 | if (aflags & AS_AREA_WRITE) |
961 | flags |= PAGE_WRITE; |
941 | flags |= PAGE_WRITE; |
962 | 942 | ||
963 | if (aflags & AS_AREA_EXEC) |
943 | if (aflags & AS_AREA_EXEC) |
964 | flags |= PAGE_EXEC; |
944 | flags |= PAGE_EXEC; |
965 | 945 | ||
966 | if (aflags & AS_AREA_CACHEABLE) |
946 | if (aflags & AS_AREA_CACHEABLE) |
967 | flags |= PAGE_CACHEABLE; |
947 | flags |= PAGE_CACHEABLE; |
968 | 948 | ||
969 | return flags; |
949 | return flags; |
970 | } |
950 | } |
971 | 951 | ||
972 | /** Compute flags for virtual address translation subsytem. |
952 | /** Compute flags for virtual address translation subsytem. |
973 | * |
953 | * |
974 | * The address space area must be locked. |
954 | * The address space area must be locked. |
975 | * Interrupts must be disabled. |
955 | * Interrupts must be disabled. |
976 | * |
956 | * |
977 | * @param a Address space area. |
957 | * @param a Address space area. |
978 | * |
958 | * |
979 | * @return Flags to be used in page_mapping_insert(). |
959 | * @return Flags to be used in page_mapping_insert(). |
980 | */ |
960 | */ |
981 | int as_area_get_flags(as_area_t *a) |
961 | int as_area_get_flags(as_area_t *a) |
982 | { |
962 | { |
983 | return area_flags_to_page_flags(a->flags); |
963 | return area_flags_to_page_flags(a->flags); |
984 | } |
964 | } |
985 | 965 | ||
986 | /** Create page table. |
966 | /** Create page table. |
987 | * |
967 | * |
988 | * Depending on architecture, create either address space |
968 | * Depending on architecture, create either address space |
989 | * private or global page table. |
969 | * private or global page table. |
990 | * |
970 | * |
991 | * @param flags Flags saying whether the page table is for kernel address space. |
971 | * @param flags Flags saying whether the page table is for kernel address space. |
992 | * |
972 | * |
993 | * @return First entry of the page table. |
973 | * @return First entry of the page table. |
994 | */ |
974 | */ |
995 | pte_t *page_table_create(int flags) |
975 | pte_t *page_table_create(int flags) |
996 | { |
976 | { |
997 | #ifdef __OBJC__ |
977 | #ifdef __OBJC__ |
998 | return [as_t page_table_create: flags]; |
978 | return [as_t page_table_create: flags]; |
999 | #else |
979 | #else |
1000 | ASSERT(as_operations); |
980 | ASSERT(as_operations); |
1001 | ASSERT(as_operations->page_table_create); |
981 | ASSERT(as_operations->page_table_create); |
1002 | 982 | ||
1003 | return as_operations->page_table_create(flags); |
983 | return as_operations->page_table_create(flags); |
1004 | #endif |
984 | #endif |
1005 | } |
985 | } |
1006 | 986 | ||
1007 | /** Destroy page table. |
987 | /** Destroy page table. |
1008 | * |
988 | * |
1009 | * Destroy page table in architecture specific way. |
989 | * Destroy page table in architecture specific way. |
1010 | * |
990 | * |
1011 | * @param page_table Physical address of PTL0. |
991 | * @param page_table Physical address of PTL0. |
1012 | */ |
992 | */ |
1013 | void page_table_destroy(pte_t *page_table) |
993 | void page_table_destroy(pte_t *page_table) |
1014 | { |
994 | { |
1015 | #ifdef __OBJC__ |
995 | #ifdef __OBJC__ |
1016 | return [as_t page_table_destroy: page_table]; |
996 | return [as_t page_table_destroy: page_table]; |
1017 | #else |
997 | #else |
1018 | ASSERT(as_operations); |
998 | ASSERT(as_operations); |
1019 | ASSERT(as_operations->page_table_destroy); |
999 | ASSERT(as_operations->page_table_destroy); |
1020 | 1000 | ||
1021 | as_operations->page_table_destroy(page_table); |
1001 | as_operations->page_table_destroy(page_table); |
1022 | #endif |
1002 | #endif |
1023 | } |
1003 | } |
1024 | 1004 | ||
1025 | /** Lock page table. |
1005 | /** Lock page table. |
1026 | * |
1006 | * |
1027 | * This function should be called before any page_mapping_insert(), |
1007 | * This function should be called before any page_mapping_insert(), |
1028 | * page_mapping_remove() and page_mapping_find(). |
1008 | * page_mapping_remove() and page_mapping_find(). |
1029 | * |
1009 | * |
1030 | * Locking order is such that address space areas must be locked |
1010 | * Locking order is such that address space areas must be locked |
1031 | * prior to this call. Address space can be locked prior to this |
1011 | * prior to this call. Address space can be locked prior to this |
1032 | * call in which case the lock argument is false. |
1012 | * call in which case the lock argument is false. |
1033 | * |
1013 | * |
1034 | * @param as Address space. |
1014 | * @param as Address space. |
1035 | * @param lock If false, do not attempt to lock as->lock. |
1015 | * @param lock If false, do not attempt to lock as->lock. |
1036 | */ |
1016 | */ |
1037 | void page_table_lock(as_t *as, bool lock) |
1017 | void page_table_lock(as_t *as, bool lock) |
1038 | { |
1018 | { |
1039 | #ifdef __OBJC__ |
1019 | #ifdef __OBJC__ |
1040 | [as page_table_lock: lock]; |
1020 | [as page_table_lock: lock]; |
1041 | #else |
1021 | #else |
1042 | ASSERT(as_operations); |
1022 | ASSERT(as_operations); |
1043 | ASSERT(as_operations->page_table_lock); |
1023 | ASSERT(as_operations->page_table_lock); |
1044 | 1024 | ||
1045 | as_operations->page_table_lock(as, lock); |
1025 | as_operations->page_table_lock(as, lock); |
1046 | #endif |
1026 | #endif |
1047 | } |
1027 | } |
1048 | 1028 | ||
1049 | /** Unlock page table. |
1029 | /** Unlock page table. |
1050 | * |
1030 | * |
1051 | * @param as Address space. |
1031 | * @param as Address space. |
1052 | * @param unlock If false, do not attempt to unlock as->lock. |
1032 | * @param unlock If false, do not attempt to unlock as->lock. |
1053 | */ |
1033 | */ |
1054 | void page_table_unlock(as_t *as, bool unlock) |
1034 | void page_table_unlock(as_t *as, bool unlock) |
1055 | { |
1035 | { |
1056 | #ifdef __OBJC__ |
1036 | #ifdef __OBJC__ |
1057 | [as page_table_unlock: unlock]; |
1037 | [as page_table_unlock: unlock]; |
1058 | #else |
1038 | #else |
1059 | ASSERT(as_operations); |
1039 | ASSERT(as_operations); |
1060 | ASSERT(as_operations->page_table_unlock); |
1040 | ASSERT(as_operations->page_table_unlock); |
1061 | 1041 | ||
1062 | as_operations->page_table_unlock(as, unlock); |
1042 | as_operations->page_table_unlock(as, unlock); |
1063 | #endif |
1043 | #endif |
1064 | } |
1044 | } |
1065 | 1045 | ||
1066 | 1046 | ||
1067 | /** Find address space area and lock it. |
1047 | /** Find address space area and lock it. |
1068 | * |
1048 | * |
1069 | * The address space must be locked and interrupts must be disabled. |
1049 | * The address space must be locked and interrupts must be disabled. |
1070 | * |
1050 | * |
1071 | * @param as Address space. |
1051 | * @param as Address space. |
1072 | * @param va Virtual address. |
1052 | * @param va Virtual address. |
1073 | * |
1053 | * |
1074 | * @return Locked address space area containing va on success or NULL on |
1054 | * @return Locked address space area containing va on success or NULL on |
1075 | * failure. |
1055 | * failure. |
1076 | */ |
1056 | */ |
1077 | as_area_t *find_area_and_lock(as_t *as, uintptr_t va) |
1057 | as_area_t *find_area_and_lock(as_t *as, uintptr_t va) |
1078 | { |
1058 | { |
1079 | as_area_t *a; |
1059 | as_area_t *a; |
1080 | btree_node_t *leaf, *lnode; |
1060 | btree_node_t *leaf, *lnode; |
1081 | int i; |
1061 | int i; |
1082 | 1062 | ||
1083 | a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf); |
1063 | a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf); |
1084 | if (a) { |
1064 | if (a) { |
1085 | /* va is the base address of an address space area */ |
1065 | /* va is the base address of an address space area */ |
1086 | mutex_lock(&a->lock); |
1066 | mutex_lock(&a->lock); |
1087 | return a; |
1067 | return a; |
1088 | } |
1068 | } |
1089 | 1069 | ||
1090 | /* |
1070 | /* |
1091 | * Search the leaf node and the righmost record of its left neighbour |
1071 | * Search the leaf node and the righmost record of its left neighbour |
1092 | * to find out whether this is a miss or va belongs to an address |
1072 | * to find out whether this is a miss or va belongs to an address |
1093 | * space area found there. |
1073 | * space area found there. |
1094 | */ |
1074 | */ |
1095 | 1075 | ||
1096 | /* First, search the leaf node itself. */ |
1076 | /* First, search the leaf node itself. */ |
1097 | for (i = 0; i < leaf->keys; i++) { |
1077 | for (i = 0; i < leaf->keys; i++) { |
1098 | a = (as_area_t *) leaf->value[i]; |
1078 | a = (as_area_t *) leaf->value[i]; |
1099 | mutex_lock(&a->lock); |
1079 | mutex_lock(&a->lock); |
1100 | if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) { |
1080 | if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) { |
1101 | return a; |
1081 | return a; |
1102 | } |
1082 | } |
1103 | mutex_unlock(&a->lock); |
1083 | mutex_unlock(&a->lock); |
1104 | } |
1084 | } |
1105 | 1085 | ||
1106 | /* |
1086 | /* |
1107 | * Second, locate the left neighbour and test its last record. |
1087 | * Second, locate the left neighbour and test its last record. |
1108 | * Because of its position in the B+tree, it must have base < va. |
1088 | * Because of its position in the B+tree, it must have base < va. |
1109 | */ |
1089 | */ |
1110 | lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf); |
1090 | lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf); |
1111 | if (lnode) { |
1091 | if (lnode) { |
1112 | a = (as_area_t *) lnode->value[lnode->keys - 1]; |
1092 | a = (as_area_t *) lnode->value[lnode->keys - 1]; |
1113 | mutex_lock(&a->lock); |
1093 | mutex_lock(&a->lock); |
1114 | if (va < a->base + a->pages * PAGE_SIZE) { |
1094 | if (va < a->base + a->pages * PAGE_SIZE) { |
1115 | return a; |
1095 | return a; |
1116 | } |
1096 | } |
1117 | mutex_unlock(&a->lock); |
1097 | mutex_unlock(&a->lock); |
1118 | } |
1098 | } |
1119 | 1099 | ||
1120 | return NULL; |
1100 | return NULL; |
1121 | } |
1101 | } |
1122 | 1102 | ||
1123 | /** Check area conflicts with other areas. |
1103 | /** Check area conflicts with other areas. |
1124 | * |
1104 | * |
1125 | * The address space must be locked and interrupts must be disabled. |
1105 | * The address space must be locked and interrupts must be disabled. |
1126 | * |
1106 | * |
1127 | * @param as Address space. |
1107 | * @param as Address space. |
1128 | * @param va Starting virtual address of the area being tested. |
1108 | * @param va Starting virtual address of the area being tested. |
1129 | * @param size Size of the area being tested. |
1109 | * @param size Size of the area being tested. |
1130 | * @param avoid_area Do not touch this area. |
1110 | * @param avoid_area Do not touch this area. |
1131 | * |
1111 | * |
1132 | * @return True if there is no conflict, false otherwise. |
1112 | * @return True if there is no conflict, false otherwise. |
1133 | */ |
1113 | */ |
1134 | bool check_area_conflicts(as_t *as, uintptr_t va, size_t size, |
1114 | bool check_area_conflicts(as_t *as, uintptr_t va, size_t size, |
1135 | as_area_t *avoid_area) |
1115 | as_area_t *avoid_area) |
1136 | { |
1116 | { |
1137 | as_area_t *a; |
1117 | as_area_t *a; |
1138 | btree_node_t *leaf, *node; |
1118 | btree_node_t *leaf, *node; |
1139 | int i; |
1119 | int i; |
1140 | 1120 | ||
1141 | /* |
1121 | /* |
1142 | * We don't want any area to have conflicts with NULL page. |
1122 | * We don't want any area to have conflicts with NULL page. |
1143 | */ |
1123 | */ |
1144 | if (overlaps(va, size, NULL, PAGE_SIZE)) |
1124 | if (overlaps(va, size, NULL, PAGE_SIZE)) |
1145 | return false; |
1125 | return false; |
1146 | 1126 | ||
1147 | /* |
1127 | /* |
1148 | * The leaf node is found in O(log n), where n is proportional to |
1128 | * The leaf node is found in O(log n), where n is proportional to |
1149 | * the number of address space areas belonging to as. |
1129 | * the number of address space areas belonging to as. |
1150 | * The check for conflicts is then attempted on the rightmost |
1130 | * The check for conflicts is then attempted on the rightmost |
1151 | * record in the left neighbour, the leftmost record in the right |
1131 | * record in the left neighbour, the leftmost record in the right |
1152 | * neighbour and all records in the leaf node itself. |
1132 | * neighbour and all records in the leaf node itself. |
1153 | */ |
1133 | */ |
1154 | 1134 | ||
1155 | if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) { |
1135 | if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) { |
1156 | if (a != avoid_area) |
1136 | if (a != avoid_area) |
1157 | return false; |
1137 | return false; |
1158 | } |
1138 | } |
1159 | 1139 | ||
1160 | /* First, check the two border cases. */ |
1140 | /* First, check the two border cases. */ |
1161 | if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
1141 | if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
1162 | a = (as_area_t *) node->value[node->keys - 1]; |
1142 | a = (as_area_t *) node->value[node->keys - 1]; |
1163 | mutex_lock(&a->lock); |
1143 | mutex_lock(&a->lock); |
1164 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
1144 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
1165 | mutex_unlock(&a->lock); |
1145 | mutex_unlock(&a->lock); |
1166 | return false; |
1146 | return false; |
1167 | } |
1147 | } |
1168 | mutex_unlock(&a->lock); |
1148 | mutex_unlock(&a->lock); |
1169 | } |
1149 | } |
1170 | node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf); |
1150 | node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf); |
1171 | if (node) { |
1151 | if (node) { |
1172 | a = (as_area_t *) node->value[0]; |
1152 | a = (as_area_t *) node->value[0]; |
1173 | mutex_lock(&a->lock); |
1153 | mutex_lock(&a->lock); |
1174 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
1154 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
1175 | mutex_unlock(&a->lock); |
1155 | mutex_unlock(&a->lock); |
1176 | return false; |
1156 | return false; |
1177 | } |
1157 | } |
1178 | mutex_unlock(&a->lock); |
1158 | mutex_unlock(&a->lock); |
1179 | } |
1159 | } |
1180 | 1160 | ||
1181 | /* Second, check the leaf node. */ |
1161 | /* Second, check the leaf node. */ |
1182 | for (i = 0; i < leaf->keys; i++) { |
1162 | for (i = 0; i < leaf->keys; i++) { |
1183 | a = (as_area_t *) leaf->value[i]; |
1163 | a = (as_area_t *) leaf->value[i]; |
1184 | 1164 | ||
1185 | if (a == avoid_area) |
1165 | if (a == avoid_area) |
1186 | continue; |
1166 | continue; |
1187 | 1167 | ||
1188 | mutex_lock(&a->lock); |
1168 | mutex_lock(&a->lock); |
1189 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
1169 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
1190 | mutex_unlock(&a->lock); |
1170 | mutex_unlock(&a->lock); |
1191 | return false; |
1171 | return false; |
1192 | } |
1172 | } |
1193 | mutex_unlock(&a->lock); |
1173 | mutex_unlock(&a->lock); |
1194 | } |
1174 | } |
1195 | 1175 | ||
1196 | /* |
1176 | /* |
1197 | * So far, the area does not conflict with other areas. |
1177 | * So far, the area does not conflict with other areas. |
1198 | * Check if it doesn't conflict with kernel address space. |
1178 | * Check if it doesn't conflict with kernel address space. |
1199 | */ |
1179 | */ |
1200 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) { |
1180 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) { |
1201 | return !overlaps(va, size, |
1181 | return !overlaps(va, size, |
1202 | KERNEL_ADDRESS_SPACE_START, |
1182 | KERNEL_ADDRESS_SPACE_START, |
1203 | KERNEL_ADDRESS_SPACE_END - KERNEL_ADDRESS_SPACE_START); |
1183 | KERNEL_ADDRESS_SPACE_END - KERNEL_ADDRESS_SPACE_START); |
1204 | } |
1184 | } |
1205 | 1185 | ||
1206 | return true; |
1186 | return true; |
1207 | } |
1187 | } |
1208 | 1188 | ||
1209 | /** Return size of the address space area with given base. */ |
1189 | /** Return size of the address space area with given base. */ |
1210 | size_t as_get_size(uintptr_t base) |
1190 | size_t as_get_size(uintptr_t base) |
1211 | { |
1191 | { |
1212 | ipl_t ipl; |
1192 | ipl_t ipl; |
1213 | as_area_t *src_area; |
1193 | as_area_t *src_area; |
1214 | size_t size; |
1194 | size_t size; |
1215 | 1195 | ||
1216 | ipl = interrupts_disable(); |
1196 | ipl = interrupts_disable(); |
1217 | src_area = find_area_and_lock(AS, base); |
1197 | src_area = find_area_and_lock(AS, base); |
1218 | if (src_area){ |
1198 | if (src_area){ |
1219 | size = src_area->pages * PAGE_SIZE; |
1199 | size = src_area->pages * PAGE_SIZE; |
1220 | mutex_unlock(&src_area->lock); |
1200 | mutex_unlock(&src_area->lock); |
1221 | } else { |
1201 | } else { |
1222 | size = 0; |
1202 | size = 0; |
1223 | } |
1203 | } |
1224 | interrupts_restore(ipl); |
1204 | interrupts_restore(ipl); |
1225 | return size; |
1205 | return size; |
1226 | } |
1206 | } |
1227 | 1207 | ||
1228 | /** Mark portion of address space area as used. |
1208 | /** Mark portion of address space area as used. |
1229 | * |
1209 | * |
1230 | * The address space area must be already locked. |
1210 | * The address space area must be already locked. |
1231 | * |
1211 | * |
1232 | * @param a Address space area. |
1212 | * @param a Address space area. |
1233 | * @param page First page to be marked. |
1213 | * @param page First page to be marked. |
1234 | * @param count Number of page to be marked. |
1214 | * @param count Number of page to be marked. |
1235 | * |
1215 | * |
1236 | * @return 0 on failure and 1 on success. |
1216 | * @return 0 on failure and 1 on success. |
1237 | */ |
1217 | */ |
1238 | int used_space_insert(as_area_t *a, uintptr_t page, count_t count) |
1218 | int used_space_insert(as_area_t *a, uintptr_t page, count_t count) |
1239 | { |
1219 | { |
1240 | btree_node_t *leaf, *node; |
1220 | btree_node_t *leaf, *node; |
1241 | count_t pages; |
1221 | count_t pages; |
1242 | int i; |
1222 | int i; |
1243 | 1223 | ||
1244 | ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE)); |
1224 | ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE)); |
1245 | ASSERT(count); |
1225 | ASSERT(count); |
1246 | 1226 | ||
1247 | pages = (count_t) btree_search(&a->used_space, page, &leaf); |
1227 | pages = (count_t) btree_search(&a->used_space, page, &leaf); |
1248 | if (pages) { |
1228 | if (pages) { |
1249 | /* |
1229 | /* |
1250 | * We hit the beginning of some used space. |
1230 | * We hit the beginning of some used space. |
1251 | */ |
1231 | */ |
1252 | return 0; |
1232 | return 0; |
1253 | } |
1233 | } |
1254 | 1234 | ||
1255 | if (!leaf->keys) { |
1235 | if (!leaf->keys) { |
1256 | btree_insert(&a->used_space, page, (void *) count, leaf); |
1236 | btree_insert(&a->used_space, page, (void *) count, leaf); |
1257 | return 1; |
1237 | return 1; |
1258 | } |
1238 | } |
1259 | 1239 | ||
1260 | node = btree_leaf_node_left_neighbour(&a->used_space, leaf); |
1240 | node = btree_leaf_node_left_neighbour(&a->used_space, leaf); |
1261 | if (node) { |
1241 | if (node) { |
1262 | uintptr_t left_pg = node->key[node->keys - 1]; |
1242 | uintptr_t left_pg = node->key[node->keys - 1]; |
1263 | uintptr_t right_pg = leaf->key[0]; |
1243 | uintptr_t right_pg = leaf->key[0]; |
1264 | count_t left_cnt = (count_t) node->value[node->keys - 1]; |
1244 | count_t left_cnt = (count_t) node->value[node->keys - 1]; |
1265 | count_t right_cnt = (count_t) leaf->value[0]; |
1245 | count_t right_cnt = (count_t) leaf->value[0]; |
1266 | 1246 | ||
1267 | /* |
1247 | /* |
1268 | * Examine the possibility that the interval fits |
1248 | * Examine the possibility that the interval fits |
1269 | * somewhere between the rightmost interval of |
1249 | * somewhere between the rightmost interval of |
1270 | * the left neigbour and the first interval of the leaf. |
1250 | * the left neigbour and the first interval of the leaf. |
1271 | */ |
1251 | */ |
1272 | 1252 | ||
1273 | if (page >= right_pg) { |
1253 | if (page >= right_pg) { |
1274 | /* Do nothing. */ |
1254 | /* Do nothing. */ |
1275 | } else if (overlaps(page, count * PAGE_SIZE, left_pg, |
1255 | } else if (overlaps(page, count * PAGE_SIZE, left_pg, |
1276 | left_cnt * PAGE_SIZE)) { |
1256 | left_cnt * PAGE_SIZE)) { |
1277 | /* The interval intersects with the left interval. */ |
1257 | /* The interval intersects with the left interval. */ |
1278 | return 0; |
1258 | return 0; |
1279 | } else if (overlaps(page, count * PAGE_SIZE, right_pg, |
1259 | } else if (overlaps(page, count * PAGE_SIZE, right_pg, |
1280 | right_cnt * PAGE_SIZE)) { |
1260 | right_cnt * PAGE_SIZE)) { |
1281 | /* The interval intersects with the right interval. */ |
1261 | /* The interval intersects with the right interval. */ |
1282 | return 0; |
1262 | return 0; |
1283 | } else if ((page == left_pg + left_cnt * PAGE_SIZE) && |
1263 | } else if ((page == left_pg + left_cnt * PAGE_SIZE) && |
1284 | (page + count * PAGE_SIZE == right_pg)) { |
1264 | (page + count * PAGE_SIZE == right_pg)) { |
1285 | /* |
1265 | /* |
1286 | * The interval can be added by merging the two already |
1266 | * The interval can be added by merging the two already |
1287 | * present intervals. |
1267 | * present intervals. |
1288 | */ |
1268 | */ |
1289 | node->value[node->keys - 1] += count + right_cnt; |
1269 | node->value[node->keys - 1] += count + right_cnt; |
1290 | btree_remove(&a->used_space, right_pg, leaf); |
1270 | btree_remove(&a->used_space, right_pg, leaf); |
1291 | return 1; |
1271 | return 1; |
1292 | } else if (page == left_pg + left_cnt * PAGE_SIZE) { |
1272 | } else if (page == left_pg + left_cnt * PAGE_SIZE) { |
1293 | /* |
1273 | /* |
1294 | * The interval can be added by simply growing the left |
1274 | * The interval can be added by simply growing the left |
1295 | * interval. |
1275 | * interval. |
1296 | */ |
1276 | */ |
1297 | node->value[node->keys - 1] += count; |
1277 | node->value[node->keys - 1] += count; |
1298 | return 1; |
1278 | return 1; |
1299 | } else if (page + count * PAGE_SIZE == right_pg) { |
1279 | } else if (page + count * PAGE_SIZE == right_pg) { |
1300 | /* |
1280 | /* |
1301 | * The interval can be addded by simply moving base of |
1281 | * The interval can be addded by simply moving base of |
1302 | * the right interval down and increasing its size |
1282 | * the right interval down and increasing its size |
1303 | * accordingly. |
1283 | * accordingly. |
1304 | */ |
1284 | */ |
1305 | leaf->value[0] += count; |
1285 | leaf->value[0] += count; |
1306 | leaf->key[0] = page; |
1286 | leaf->key[0] = page; |
1307 | return 1; |
1287 | return 1; |
1308 | } else { |
1288 | } else { |
1309 | /* |
1289 | /* |
1310 | * The interval is between both neigbouring intervals, |
1290 | * The interval is between both neigbouring intervals, |
1311 | * but cannot be merged with any of them. |
1291 | * but cannot be merged with any of them. |
1312 | */ |
1292 | */ |
1313 | btree_insert(&a->used_space, page, (void *) count, |
1293 | btree_insert(&a->used_space, page, (void *) count, |
1314 | leaf); |
1294 | leaf); |
1315 | return 1; |
1295 | return 1; |
1316 | } |
1296 | } |
1317 | } else if (page < leaf->key[0]) { |
1297 | } else if (page < leaf->key[0]) { |
1318 | uintptr_t right_pg = leaf->key[0]; |
1298 | uintptr_t right_pg = leaf->key[0]; |
1319 | count_t right_cnt = (count_t) leaf->value[0]; |
1299 | count_t right_cnt = (count_t) leaf->value[0]; |
1320 | 1300 | ||
1321 | /* |
1301 | /* |
1322 | * Investigate the border case in which the left neighbour does |
1302 | * Investigate the border case in which the left neighbour does |
1323 | * not exist but the interval fits from the left. |
1303 | * not exist but the interval fits from the left. |
1324 | */ |
1304 | */ |
1325 | 1305 | ||
1326 | if (overlaps(page, count * PAGE_SIZE, right_pg, |
1306 | if (overlaps(page, count * PAGE_SIZE, right_pg, |
1327 | right_cnt * PAGE_SIZE)) { |
1307 | right_cnt * PAGE_SIZE)) { |
1328 | /* The interval intersects with the right interval. */ |
1308 | /* The interval intersects with the right interval. */ |
1329 | return 0; |
1309 | return 0; |
1330 | } else if (page + count * PAGE_SIZE == right_pg) { |
1310 | } else if (page + count * PAGE_SIZE == right_pg) { |
1331 | /* |
1311 | /* |
1332 | * The interval can be added by moving the base of the |
1312 | * The interval can be added by moving the base of the |
1333 | * right interval down and increasing its size |
1313 | * right interval down and increasing its size |
1334 | * accordingly. |
1314 | * accordingly. |
1335 | */ |
1315 | */ |
1336 | leaf->key[0] = page; |
1316 | leaf->key[0] = page; |
1337 | leaf->value[0] += count; |
1317 | leaf->value[0] += count; |
1338 | return 1; |
1318 | return 1; |
1339 | } else { |
1319 | } else { |
1340 | /* |
1320 | /* |
1341 | * The interval doesn't adjoin with the right interval. |
1321 | * The interval doesn't adjoin with the right interval. |
1342 | * It must be added individually. |
1322 | * It must be added individually. |
1343 | */ |
1323 | */ |
1344 | btree_insert(&a->used_space, page, (void *) count, |
1324 | btree_insert(&a->used_space, page, (void *) count, |
1345 | leaf); |
1325 | leaf); |
1346 | return 1; |
1326 | return 1; |
1347 | } |
1327 | } |
1348 | } |
1328 | } |
1349 | 1329 | ||
1350 | node = btree_leaf_node_right_neighbour(&a->used_space, leaf); |
1330 | node = btree_leaf_node_right_neighbour(&a->used_space, leaf); |
1351 | if (node) { |
1331 | if (node) { |
1352 | uintptr_t left_pg = leaf->key[leaf->keys - 1]; |
1332 | uintptr_t left_pg = leaf->key[leaf->keys - 1]; |
1353 | uintptr_t right_pg = node->key[0]; |
1333 | uintptr_t right_pg = node->key[0]; |
1354 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1]; |
1334 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1]; |
1355 | count_t right_cnt = (count_t) node->value[0]; |
1335 | count_t right_cnt = (count_t) node->value[0]; |
1356 | 1336 | ||
1357 | /* |
1337 | /* |
1358 | * Examine the possibility that the interval fits |
1338 | * Examine the possibility that the interval fits |
1359 | * somewhere between the leftmost interval of |
1339 | * somewhere between the leftmost interval of |
1360 | * the right neigbour and the last interval of the leaf. |
1340 | * the right neigbour and the last interval of the leaf. |
1361 | */ |
1341 | */ |
1362 | 1342 | ||
1363 | if (page < left_pg) { |
1343 | if (page < left_pg) { |
1364 | /* Do nothing. */ |
1344 | /* Do nothing. */ |
1365 | } else if (overlaps(page, count * PAGE_SIZE, left_pg, |
1345 | } else if (overlaps(page, count * PAGE_SIZE, left_pg, |
1366 | left_cnt * PAGE_SIZE)) { |
1346 | left_cnt * PAGE_SIZE)) { |
1367 | /* The interval intersects with the left interval. */ |
1347 | /* The interval intersects with the left interval. */ |
1368 | return 0; |
1348 | return 0; |
1369 | } else if (overlaps(page, count * PAGE_SIZE, right_pg, |
1349 | } else if (overlaps(page, count * PAGE_SIZE, right_pg, |
1370 | right_cnt * PAGE_SIZE)) { |
1350 | right_cnt * PAGE_SIZE)) { |
1371 | /* The interval intersects with the right interval. */ |
1351 | /* The interval intersects with the right interval. */ |
1372 | return 0; |
1352 | return 0; |
1373 | } else if ((page == left_pg + left_cnt * PAGE_SIZE) && |
1353 | } else if ((page == left_pg + left_cnt * PAGE_SIZE) && |
1374 | (page + count * PAGE_SIZE == right_pg)) { |
1354 | (page + count * PAGE_SIZE == right_pg)) { |
1375 | /* |
1355 | /* |
1376 | * The interval can be added by merging the two already |
1356 | * The interval can be added by merging the two already |
1377 | * present intervals. |
1357 | * present intervals. |
1378 | * */ |
1358 | * */ |
1379 | leaf->value[leaf->keys - 1] += count + right_cnt; |
1359 | leaf->value[leaf->keys - 1] += count + right_cnt; |
1380 | btree_remove(&a->used_space, right_pg, node); |
1360 | btree_remove(&a->used_space, right_pg, node); |
1381 | return 1; |
1361 | return 1; |
1382 | } else if (page == left_pg + left_cnt * PAGE_SIZE) { |
1362 | } else if (page == left_pg + left_cnt * PAGE_SIZE) { |
1383 | /* |
1363 | /* |
1384 | * The interval can be added by simply growing the left |
1364 | * The interval can be added by simply growing the left |
1385 | * interval. |
1365 | * interval. |
1386 | * */ |
1366 | * */ |
1387 | leaf->value[leaf->keys - 1] += count; |
1367 | leaf->value[leaf->keys - 1] += count; |
1388 | return 1; |
1368 | return 1; |
1389 | } else if (page + count * PAGE_SIZE == right_pg) { |
1369 | } else if (page + count * PAGE_SIZE == right_pg) { |
1390 | /* |
1370 | /* |
1391 | * The interval can be addded by simply moving base of |
1371 | * The interval can be addded by simply moving base of |
1392 | * the right interval down and increasing its size |
1372 | * the right interval down and increasing its size |
1393 | * accordingly. |
1373 | * accordingly. |
1394 | */ |
1374 | */ |
1395 | node->value[0] += count; |
1375 | node->value[0] += count; |
1396 | node->key[0] = page; |
1376 | node->key[0] = page; |
1397 | return 1; |
1377 | return 1; |
1398 | } else { |
1378 | } else { |
1399 | /* |
1379 | /* |
1400 | * The interval is between both neigbouring intervals, |
1380 | * The interval is between both neigbouring intervals, |
1401 | * but cannot be merged with any of them. |
1381 | * but cannot be merged with any of them. |
1402 | */ |
1382 | */ |
1403 | btree_insert(&a->used_space, page, (void *) count, |
1383 | btree_insert(&a->used_space, page, (void *) count, |
1404 | leaf); |
1384 | leaf); |
1405 | return 1; |
1385 | return 1; |
1406 | } |
1386 | } |
1407 | } else if (page >= leaf->key[leaf->keys - 1]) { |
1387 | } else if (page >= leaf->key[leaf->keys - 1]) { |
1408 | uintptr_t left_pg = leaf->key[leaf->keys - 1]; |
1388 | uintptr_t left_pg = leaf->key[leaf->keys - 1]; |
1409 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1]; |
1389 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1]; |
1410 | 1390 | ||
1411 | /* |
1391 | /* |
1412 | * Investigate the border case in which the right neighbour |
1392 | * Investigate the border case in which the right neighbour |
1413 | * does not exist but the interval fits from the right. |
1393 | * does not exist but the interval fits from the right. |
1414 | */ |
1394 | */ |
1415 | 1395 | ||
1416 | if (overlaps(page, count * PAGE_SIZE, left_pg, |
1396 | if (overlaps(page, count * PAGE_SIZE, left_pg, |
1417 | left_cnt * PAGE_SIZE)) { |
1397 | left_cnt * PAGE_SIZE)) { |
1418 | /* The interval intersects with the left interval. */ |
1398 | /* The interval intersects with the left interval. */ |
1419 | return 0; |
1399 | return 0; |
1420 | } else if (left_pg + left_cnt * PAGE_SIZE == page) { |
1400 | } else if (left_pg + left_cnt * PAGE_SIZE == page) { |
1421 | /* |
1401 | /* |
1422 | * The interval can be added by growing the left |
1402 | * The interval can be added by growing the left |
1423 | * interval. |
1403 | * interval. |
1424 | */ |
1404 | */ |
1425 | leaf->value[leaf->keys - 1] += count; |
1405 | leaf->value[leaf->keys - 1] += count; |
1426 | return 1; |
1406 | return 1; |
1427 | } else { |
1407 | } else { |
1428 | /* |
1408 | /* |
1429 | * The interval doesn't adjoin with the left interval. |
1409 | * The interval doesn't adjoin with the left interval. |
1430 | * It must be added individually. |
1410 | * It must be added individually. |
1431 | */ |
1411 | */ |
1432 | btree_insert(&a->used_space, page, (void *) count, |
1412 | btree_insert(&a->used_space, page, (void *) count, |
1433 | leaf); |
1413 | leaf); |
1434 | return 1; |
1414 | return 1; |
1435 | } |
1415 | } |
1436 | } |
1416 | } |
1437 | 1417 | ||
1438 | /* |
1418 | /* |
1439 | * Note that if the algorithm made it thus far, the interval can fit |
1419 | * Note that if the algorithm made it thus far, the interval can fit |
1440 | * only between two other intervals of the leaf. The two border cases |
1420 | * only between two other intervals of the leaf. The two border cases |
1441 | * were already resolved. |
1421 | * were already resolved. |
1442 | */ |
1422 | */ |
1443 | for (i = 1; i < leaf->keys; i++) { |
1423 | for (i = 1; i < leaf->keys; i++) { |
1444 | if (page < leaf->key[i]) { |
1424 | if (page < leaf->key[i]) { |
1445 | uintptr_t left_pg = leaf->key[i - 1]; |
1425 | uintptr_t left_pg = leaf->key[i - 1]; |
1446 | uintptr_t right_pg = leaf->key[i]; |
1426 | uintptr_t right_pg = leaf->key[i]; |
1447 | count_t left_cnt = (count_t) leaf->value[i - 1]; |
1427 | count_t left_cnt = (count_t) leaf->value[i - 1]; |
1448 | count_t right_cnt = (count_t) leaf->value[i]; |
1428 | count_t right_cnt = (count_t) leaf->value[i]; |
1449 | 1429 | ||
1450 | /* |
1430 | /* |
1451 | * The interval fits between left_pg and right_pg. |
1431 | * The interval fits between left_pg and right_pg. |
1452 | */ |
1432 | */ |
1453 | 1433 | ||
1454 | if (overlaps(page, count * PAGE_SIZE, left_pg, |
1434 | if (overlaps(page, count * PAGE_SIZE, left_pg, |
1455 | left_cnt * PAGE_SIZE)) { |
1435 | left_cnt * PAGE_SIZE)) { |
1456 | /* |
1436 | /* |
1457 | * The interval intersects with the left |
1437 | * The interval intersects with the left |
1458 | * interval. |
1438 | * interval. |
1459 | */ |
1439 | */ |
1460 | return 0; |
1440 | return 0; |
1461 | } else if (overlaps(page, count * PAGE_SIZE, right_pg, |
1441 | } else if (overlaps(page, count * PAGE_SIZE, right_pg, |
1462 | right_cnt * PAGE_SIZE)) { |
1442 | right_cnt * PAGE_SIZE)) { |
1463 | /* |
1443 | /* |
1464 | * The interval intersects with the right |
1444 | * The interval intersects with the right |
1465 | * interval. |
1445 | * interval. |
1466 | */ |
1446 | */ |
1467 | return 0; |
1447 | return 0; |
1468 | } else if ((page == left_pg + left_cnt * PAGE_SIZE) && |
1448 | } else if ((page == left_pg + left_cnt * PAGE_SIZE) && |
1469 | (page + count * PAGE_SIZE == right_pg)) { |
1449 | (page + count * PAGE_SIZE == right_pg)) { |
1470 | /* |
1450 | /* |
1471 | * The interval can be added by merging the two |
1451 | * The interval can be added by merging the two |
1472 | * already present intervals. |
1452 | * already present intervals. |
1473 | */ |
1453 | */ |
1474 | leaf->value[i - 1] += count + right_cnt; |
1454 | leaf->value[i - 1] += count + right_cnt; |
1475 | btree_remove(&a->used_space, right_pg, leaf); |
1455 | btree_remove(&a->used_space, right_pg, leaf); |
1476 | return 1; |
1456 | return 1; |
1477 | } else if (page == left_pg + left_cnt * PAGE_SIZE) { |
1457 | } else if (page == left_pg + left_cnt * PAGE_SIZE) { |
1478 | /* |
1458 | /* |
1479 | * The interval can be added by simply growing |
1459 | * The interval can be added by simply growing |
1480 | * the left interval. |
1460 | * the left interval. |
1481 | */ |
1461 | */ |
1482 | leaf->value[i - 1] += count; |
1462 | leaf->value[i - 1] += count; |
1483 | return 1; |
1463 | return 1; |
1484 | } else if (page + count * PAGE_SIZE == right_pg) { |
1464 | } else if (page + count * PAGE_SIZE == right_pg) { |
1485 | /* |
1465 | /* |
1486 | * The interval can be addded by simply moving |
1466 | * The interval can be addded by simply moving |
1487 | * base of the right interval down and |
1467 | * base of the right interval down and |
1488 | * increasing its size accordingly. |
1468 | * increasing its size accordingly. |
1489 | */ |
1469 | */ |
1490 | leaf->value[i] += count; |
1470 | leaf->value[i] += count; |
1491 | leaf->key[i] = page; |
1471 | leaf->key[i] = page; |
1492 | return 1; |
1472 | return 1; |
1493 | } else { |
1473 | } else { |
1494 | /* |
1474 | /* |
1495 | * The interval is between both neigbouring |
1475 | * The interval is between both neigbouring |
1496 | * intervals, but cannot be merged with any of |
1476 | * intervals, but cannot be merged with any of |
1497 | * them. |
1477 | * them. |
1498 | */ |
1478 | */ |
1499 | btree_insert(&a->used_space, page, |
1479 | btree_insert(&a->used_space, page, |
1500 | (void *) count, leaf); |
1480 | (void *) count, leaf); |
1501 | return 1; |
1481 | return 1; |
1502 | } |
1482 | } |
1503 | } |
1483 | } |
1504 | } |
1484 | } |
1505 | 1485 | ||
1506 | panic("Inconsistency detected while adding %d pages of used space at " |
1486 | panic("Inconsistency detected while adding %d pages of used space at " |
1507 | "%p.\n", count, page); |
1487 | "%p.\n", count, page); |
1508 | } |
1488 | } |
1509 | 1489 | ||
1510 | /** Mark portion of address space area as unused. |
1490 | /** Mark portion of address space area as unused. |
1511 | * |
1491 | * |
1512 | * The address space area must be already locked. |
1492 | * The address space area must be already locked. |
1513 | * |
1493 | * |
1514 | * @param a Address space area. |
1494 | * @param a Address space area. |
1515 | * @param page First page to be marked. |
1495 | * @param page First page to be marked. |
1516 | * @param count Number of page to be marked. |
1496 | * @param count Number of page to be marked. |
1517 | * |
1497 | * |
1518 | * @return 0 on failure and 1 on success. |
1498 | * @return 0 on failure and 1 on success. |
1519 | */ |
1499 | */ |
1520 | int used_space_remove(as_area_t *a, uintptr_t page, count_t count) |
1500 | int used_space_remove(as_area_t *a, uintptr_t page, count_t count) |
1521 | { |
1501 | { |
1522 | btree_node_t *leaf, *node; |
1502 | btree_node_t *leaf, *node; |
1523 | count_t pages; |
1503 | count_t pages; |
1524 | int i; |
1504 | int i; |
1525 | 1505 | ||
1526 | ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE)); |
1506 | ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE)); |
1527 | ASSERT(count); |
1507 | ASSERT(count); |
1528 | 1508 | ||
1529 | pages = (count_t) btree_search(&a->used_space, page, &leaf); |
1509 | pages = (count_t) btree_search(&a->used_space, page, &leaf); |
1530 | if (pages) { |
1510 | if (pages) { |
1531 | /* |
1511 | /* |
1532 | * We are lucky, page is the beginning of some interval. |
1512 | * We are lucky, page is the beginning of some interval. |
1533 | */ |
1513 | */ |
1534 | if (count > pages) { |
1514 | if (count > pages) { |
1535 | return 0; |
1515 | return 0; |
1536 | } else if (count == pages) { |
1516 | } else if (count == pages) { |
1537 | btree_remove(&a->used_space, page, leaf); |
1517 | btree_remove(&a->used_space, page, leaf); |
1538 | return 1; |
1518 | return 1; |
1539 | } else { |
1519 | } else { |
1540 | /* |
1520 | /* |
1541 | * Find the respective interval. |
1521 | * Find the respective interval. |
1542 | * Decrease its size and relocate its start address. |
1522 | * Decrease its size and relocate its start address. |
1543 | */ |
1523 | */ |
1544 | for (i = 0; i < leaf->keys; i++) { |
1524 | for (i = 0; i < leaf->keys; i++) { |
1545 | if (leaf->key[i] == page) { |
1525 | if (leaf->key[i] == page) { |
1546 | leaf->key[i] += count * PAGE_SIZE; |
1526 | leaf->key[i] += count * PAGE_SIZE; |
1547 | leaf->value[i] -= count; |
1527 | leaf->value[i] -= count; |
1548 | return 1; |
1528 | return 1; |
1549 | } |
1529 | } |
1550 | } |
1530 | } |
1551 | goto error; |
1531 | goto error; |
1552 | } |
1532 | } |
1553 | } |
1533 | } |
1554 | 1534 | ||
1555 | node = btree_leaf_node_left_neighbour(&a->used_space, leaf); |
1535 | node = btree_leaf_node_left_neighbour(&a->used_space, leaf); |
1556 | if (node && page < leaf->key[0]) { |
1536 | if (node && page < leaf->key[0]) { |
1557 | uintptr_t left_pg = node->key[node->keys - 1]; |
1537 | uintptr_t left_pg = node->key[node->keys - 1]; |
1558 | count_t left_cnt = (count_t) node->value[node->keys - 1]; |
1538 | count_t left_cnt = (count_t) node->value[node->keys - 1]; |
1559 | 1539 | ||
1560 | if (overlaps(left_pg, left_cnt * PAGE_SIZE, page, |
1540 | if (overlaps(left_pg, left_cnt * PAGE_SIZE, page, |
1561 | count * PAGE_SIZE)) { |
1541 | count * PAGE_SIZE)) { |
1562 | if (page + count * PAGE_SIZE == |
1542 | if (page + count * PAGE_SIZE == |
1563 | left_pg + left_cnt * PAGE_SIZE) { |
1543 | left_pg + left_cnt * PAGE_SIZE) { |
1564 | /* |
1544 | /* |
1565 | * The interval is contained in the rightmost |
1545 | * The interval is contained in the rightmost |
1566 | * interval of the left neighbour and can be |
1546 | * interval of the left neighbour and can be |
1567 | * removed by updating the size of the bigger |
1547 | * removed by updating the size of the bigger |
1568 | * interval. |
1548 | * interval. |
1569 | */ |
1549 | */ |
1570 | node->value[node->keys - 1] -= count; |
1550 | node->value[node->keys - 1] -= count; |
1571 | return 1; |
1551 | return 1; |
1572 | } else if (page + count * PAGE_SIZE < |
1552 | } else if (page + count * PAGE_SIZE < |
1573 | left_pg + left_cnt*PAGE_SIZE) { |
1553 | left_pg + left_cnt*PAGE_SIZE) { |
1574 | count_t new_cnt; |
1554 | count_t new_cnt; |
1575 | 1555 | ||
1576 | /* |
1556 | /* |
1577 | * The interval is contained in the rightmost |
1557 | * The interval is contained in the rightmost |
1578 | * interval of the left neighbour but its |
1558 | * interval of the left neighbour but its |
1579 | * removal requires both updating the size of |
1559 | * removal requires both updating the size of |
1580 | * the original interval and also inserting a |
1560 | * the original interval and also inserting a |
1581 | * new interval. |
1561 | * new interval. |
1582 | */ |
1562 | */ |
1583 | new_cnt = ((left_pg + left_cnt * PAGE_SIZE) - |
1563 | new_cnt = ((left_pg + left_cnt * PAGE_SIZE) - |
1584 | (page + count*PAGE_SIZE)) >> PAGE_WIDTH; |
1564 | (page + count*PAGE_SIZE)) >> PAGE_WIDTH; |
1585 | node->value[node->keys - 1] -= count + new_cnt; |
1565 | node->value[node->keys - 1] -= count + new_cnt; |
1586 | btree_insert(&a->used_space, page + |
1566 | btree_insert(&a->used_space, page + |
1587 | count * PAGE_SIZE, (void *) new_cnt, leaf); |
1567 | count * PAGE_SIZE, (void *) new_cnt, leaf); |
1588 | return 1; |
1568 | return 1; |
1589 | } |
1569 | } |
1590 | } |
1570 | } |
1591 | return 0; |
1571 | return 0; |
1592 | } else if (page < leaf->key[0]) { |
1572 | } else if (page < leaf->key[0]) { |
1593 | return 0; |
1573 | return 0; |
1594 | } |
1574 | } |
1595 | 1575 | ||
1596 | if (page > leaf->key[leaf->keys - 1]) { |
1576 | if (page > leaf->key[leaf->keys - 1]) { |
1597 | uintptr_t left_pg = leaf->key[leaf->keys - 1]; |
1577 | uintptr_t left_pg = leaf->key[leaf->keys - 1]; |
1598 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1]; |
1578 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1]; |
1599 | 1579 | ||
1600 | if (overlaps(left_pg, left_cnt * PAGE_SIZE, page, |
1580 | if (overlaps(left_pg, left_cnt * PAGE_SIZE, page, |
1601 | count * PAGE_SIZE)) { |
1581 | count * PAGE_SIZE)) { |
1602 | if (page + count * PAGE_SIZE == |
1582 | if (page + count * PAGE_SIZE == |
1603 | left_pg + left_cnt * PAGE_SIZE) { |
1583 | left_pg + left_cnt * PAGE_SIZE) { |
1604 | /* |
1584 | /* |
1605 | * The interval is contained in the rightmost |
1585 | * The interval is contained in the rightmost |
1606 | * interval of the leaf and can be removed by |
1586 | * interval of the leaf and can be removed by |
1607 | * updating the size of the bigger interval. |
1587 | * updating the size of the bigger interval. |
1608 | */ |
1588 | */ |
1609 | leaf->value[leaf->keys - 1] -= count; |
1589 | leaf->value[leaf->keys - 1] -= count; |
1610 | return 1; |
1590 | return 1; |
1611 | } else if (page + count * PAGE_SIZE < left_pg + |
1591 | } else if (page + count * PAGE_SIZE < left_pg + |
1612 | left_cnt * PAGE_SIZE) { |
1592 | left_cnt * PAGE_SIZE) { |
1613 | count_t new_cnt; |
1593 | count_t new_cnt; |
1614 | 1594 | ||
1615 | /* |
1595 | /* |
1616 | * The interval is contained in the rightmost |
1596 | * The interval is contained in the rightmost |
1617 | * interval of the leaf but its removal |
1597 | * interval of the leaf but its removal |
1618 | * requires both updating the size of the |
1598 | * requires both updating the size of the |
1619 | * original interval and also inserting a new |
1599 | * original interval and also inserting a new |
1620 | * interval. |
1600 | * interval. |
1621 | */ |
1601 | */ |
1622 | new_cnt = ((left_pg + left_cnt * PAGE_SIZE) - |
1602 | new_cnt = ((left_pg + left_cnt * PAGE_SIZE) - |
1623 | (page + count * PAGE_SIZE)) >> PAGE_WIDTH; |
1603 | (page + count * PAGE_SIZE)) >> PAGE_WIDTH; |
1624 | leaf->value[leaf->keys - 1] -= count + new_cnt; |
1604 | leaf->value[leaf->keys - 1] -= count + new_cnt; |
1625 | btree_insert(&a->used_space, page + |
1605 | btree_insert(&a->used_space, page + |
1626 | count * PAGE_SIZE, (void *) new_cnt, leaf); |
1606 | count * PAGE_SIZE, (void *) new_cnt, leaf); |
1627 | return 1; |
1607 | return 1; |
1628 | } |
1608 | } |
1629 | } |
1609 | } |
1630 | return 0; |
1610 | return 0; |
1631 | } |
1611 | } |
1632 | 1612 | ||
1633 | /* |
1613 | /* |
1634 | * The border cases have been already resolved. |
1614 | * The border cases have been already resolved. |
1635 | * Now the interval can be only between intervals of the leaf. |
1615 | * Now the interval can be only between intervals of the leaf. |
1636 | */ |
1616 | */ |
1637 | for (i = 1; i < leaf->keys - 1; i++) { |
1617 | for (i = 1; i < leaf->keys - 1; i++) { |
1638 | if (page < leaf->key[i]) { |
1618 | if (page < leaf->key[i]) { |
1639 | uintptr_t left_pg = leaf->key[i - 1]; |
1619 | uintptr_t left_pg = leaf->key[i - 1]; |
1640 | count_t left_cnt = (count_t) leaf->value[i - 1]; |
1620 | count_t left_cnt = (count_t) leaf->value[i - 1]; |
1641 | 1621 | ||
1642 | /* |
1622 | /* |
1643 | * Now the interval is between intervals corresponding |
1623 | * Now the interval is between intervals corresponding |
1644 | * to (i - 1) and i. |
1624 | * to (i - 1) and i. |
1645 | */ |
1625 | */ |
1646 | if (overlaps(left_pg, left_cnt * PAGE_SIZE, page, |
1626 | if (overlaps(left_pg, left_cnt * PAGE_SIZE, page, |
1647 | count * PAGE_SIZE)) { |
1627 | count * PAGE_SIZE)) { |
1648 | if (page + count * PAGE_SIZE == |
1628 | if (page + count * PAGE_SIZE == |
1649 | left_pg + left_cnt*PAGE_SIZE) { |
1629 | left_pg + left_cnt*PAGE_SIZE) { |
1650 | /* |
1630 | /* |
1651 | * The interval is contained in the |
1631 | * The interval is contained in the |
1652 | * interval (i - 1) of the leaf and can |
1632 | * interval (i - 1) of the leaf and can |
1653 | * be removed by updating the size of |
1633 | * be removed by updating the size of |
1654 | * the bigger interval. |
1634 | * the bigger interval. |
1655 | */ |
1635 | */ |
1656 | leaf->value[i - 1] -= count; |
1636 | leaf->value[i - 1] -= count; |
1657 | return 1; |
1637 | return 1; |
1658 | } else if (page + count * PAGE_SIZE < |
1638 | } else if (page + count * PAGE_SIZE < |
1659 | left_pg + left_cnt * PAGE_SIZE) { |
1639 | left_pg + left_cnt * PAGE_SIZE) { |
1660 | count_t new_cnt; |
1640 | count_t new_cnt; |
1661 | 1641 | ||
1662 | /* |
1642 | /* |
1663 | * The interval is contained in the |
1643 | * The interval is contained in the |
1664 | * interval (i - 1) of the leaf but its |
1644 | * interval (i - 1) of the leaf but its |
1665 | * removal requires both updating the |
1645 | * removal requires both updating the |
1666 | * size of the original interval and |
1646 | * size of the original interval and |
1667 | * also inserting a new interval. |
1647 | * also inserting a new interval. |
1668 | */ |
1648 | */ |
1669 | new_cnt = ((left_pg + |
1649 | new_cnt = ((left_pg + |
1670 | left_cnt * PAGE_SIZE) - |
1650 | left_cnt * PAGE_SIZE) - |
1671 | (page + count * PAGE_SIZE)) >> |
1651 | (page + count * PAGE_SIZE)) >> |
1672 | PAGE_WIDTH; |
1652 | PAGE_WIDTH; |
1673 | leaf->value[i - 1] -= count + new_cnt; |
1653 | leaf->value[i - 1] -= count + new_cnt; |
1674 | btree_insert(&a->used_space, page + |
1654 | btree_insert(&a->used_space, page + |
1675 | count * PAGE_SIZE, (void *) new_cnt, |
1655 | count * PAGE_SIZE, (void *) new_cnt, |
1676 | leaf); |
1656 | leaf); |
1677 | return 1; |
1657 | return 1; |
1678 | } |
1658 | } |
1679 | } |
1659 | } |
1680 | return 0; |
1660 | return 0; |
1681 | } |
1661 | } |
1682 | } |
1662 | } |
1683 | 1663 | ||
1684 | error: |
1664 | error: |
1685 | panic("Inconsistency detected while removing %d pages of used space " |
1665 | panic("Inconsistency detected while removing %d pages of used space " |
1686 | "from %p.\n", count, page); |
1666 | "from %p.\n", count, page); |
1687 | } |
1667 | } |
1688 | 1668 | ||
1689 | /** Remove reference to address space area share info. |
1669 | /** Remove reference to address space area share info. |
1690 | * |
1670 | * |
1691 | * If the reference count drops to 0, the sh_info is deallocated. |
1671 | * If the reference count drops to 0, the sh_info is deallocated. |
1692 | * |
1672 | * |
1693 | * @param sh_info Pointer to address space area share info. |
1673 | * @param sh_info Pointer to address space area share info. |
1694 | */ |
1674 | */ |
1695 | void sh_info_remove_reference(share_info_t *sh_info) |
1675 | void sh_info_remove_reference(share_info_t *sh_info) |
1696 | { |
1676 | { |
1697 | bool dealloc = false; |
1677 | bool dealloc = false; |
1698 | 1678 | ||
1699 | mutex_lock(&sh_info->lock); |
1679 | mutex_lock(&sh_info->lock); |
1700 | ASSERT(sh_info->refcount); |
1680 | ASSERT(sh_info->refcount); |
1701 | if (--sh_info->refcount == 0) { |
1681 | if (--sh_info->refcount == 0) { |
1702 | dealloc = true; |
1682 | dealloc = true; |
1703 | link_t *cur; |
1683 | link_t *cur; |
1704 | 1684 | ||
1705 | /* |
1685 | /* |
1706 | * Now walk carefully the pagemap B+tree and free/remove |
1686 | * Now walk carefully the pagemap B+tree and free/remove |
1707 | * reference from all frames found there. |
1687 | * reference from all frames found there. |
1708 | */ |
1688 | */ |
1709 | for (cur = sh_info->pagemap.leaf_head.next; |
1689 | for (cur = sh_info->pagemap.leaf_head.next; |
1710 | cur != &sh_info->pagemap.leaf_head; cur = cur->next) { |
1690 | cur != &sh_info->pagemap.leaf_head; cur = cur->next) { |
1711 | btree_node_t *node; |
1691 | btree_node_t *node; |
1712 | int i; |
1692 | int i; |
1713 | 1693 | ||
1714 | node = list_get_instance(cur, btree_node_t, leaf_link); |
1694 | node = list_get_instance(cur, btree_node_t, leaf_link); |
1715 | for (i = 0; i < node->keys; i++) |
1695 | for (i = 0; i < node->keys; i++) |
1716 | frame_free((uintptr_t) node->value[i]); |
1696 | frame_free((uintptr_t) node->value[i]); |
1717 | } |
1697 | } |
1718 | 1698 | ||
1719 | } |
1699 | } |
1720 | mutex_unlock(&sh_info->lock); |
1700 | mutex_unlock(&sh_info->lock); |
1721 | 1701 | ||
1722 | if (dealloc) { |
1702 | if (dealloc) { |
1723 | btree_destroy(&sh_info->pagemap); |
1703 | btree_destroy(&sh_info->pagemap); |
1724 | free(sh_info); |
1704 | free(sh_info); |
1725 | } |
1705 | } |
1726 | } |
1706 | } |
1727 | 1707 | ||
1728 | /* |
1708 | /* |
1729 | * Address space related syscalls. |
1709 | * Address space related syscalls. |
1730 | */ |
1710 | */ |
1731 | 1711 | ||
1732 | /** Wrapper for as_area_create(). */ |
1712 | /** Wrapper for as_area_create(). */ |
1733 | unative_t sys_as_area_create(uintptr_t address, size_t size, int flags) |
1713 | unative_t sys_as_area_create(uintptr_t address, size_t size, int flags) |
1734 | { |
1714 | { |
1735 | if (as_area_create(AS, flags | AS_AREA_CACHEABLE, size, address, |
1715 | if (as_area_create(AS, flags | AS_AREA_CACHEABLE, size, address, |
1736 | AS_AREA_ATTR_NONE, &anon_backend, NULL)) |
1716 | AS_AREA_ATTR_NONE, &anon_backend, NULL)) |
1737 | return (unative_t) address; |
1717 | return (unative_t) address; |
1738 | else |
1718 | else |
1739 | return (unative_t) -1; |
1719 | return (unative_t) -1; |
1740 | } |
1720 | } |
1741 | 1721 | ||
1742 | /** Wrapper for as_area_resize(). */ |
1722 | /** Wrapper for as_area_resize(). */ |
1743 | unative_t sys_as_area_resize(uintptr_t address, size_t size, int flags) |
1723 | unative_t sys_as_area_resize(uintptr_t address, size_t size, int flags) |
1744 | { |
1724 | { |
1745 | return (unative_t) as_area_resize(AS, address, size, 0); |
1725 | return (unative_t) as_area_resize(AS, address, size, 0); |
1746 | } |
1726 | } |
1747 | 1727 | ||
1748 | /** Wrapper for as_area_destroy(). */ |
1728 | /** Wrapper for as_area_destroy(). */ |
1749 | unative_t sys_as_area_destroy(uintptr_t address) |
1729 | unative_t sys_as_area_destroy(uintptr_t address) |
1750 | { |
1730 | { |
1751 | return (unative_t) as_area_destroy(AS, address); |
1731 | return (unative_t) as_area_destroy(AS, address); |
1752 | } |
1732 | } |
1753 | 1733 | ||
1754 | /** Print out information about address space. |
1734 | /** Print out information about address space. |
1755 | * |
1735 | * |
1756 | * @param as Address space. |
1736 | * @param as Address space. |
1757 | */ |
1737 | */ |
1758 | void as_print(as_t *as) |
1738 | void as_print(as_t *as) |
1759 | { |
1739 | { |
1760 | ipl_t ipl; |
1740 | ipl_t ipl; |
1761 | 1741 | ||
1762 | ipl = interrupts_disable(); |
1742 | ipl = interrupts_disable(); |
1763 | mutex_lock(&as->lock); |
1743 | mutex_lock(&as->lock); |
1764 | 1744 | ||
1765 | /* print out info about address space areas */ |
1745 | /* print out info about address space areas */ |
1766 | link_t *cur; |
1746 | link_t *cur; |
1767 | for (cur = as->as_area_btree.leaf_head.next; |
1747 | for (cur = as->as_area_btree.leaf_head.next; |
1768 | cur != &as->as_area_btree.leaf_head; cur = cur->next) { |
1748 | cur != &as->as_area_btree.leaf_head; cur = cur->next) { |
1769 | btree_node_t *node; |
1749 | btree_node_t *node; |
1770 | 1750 | ||
1771 | node = list_get_instance(cur, btree_node_t, leaf_link); |
1751 | node = list_get_instance(cur, btree_node_t, leaf_link); |
1772 | 1752 | ||
1773 | int i; |
1753 | int i; |
1774 | for (i = 0; i < node->keys; i++) { |
1754 | for (i = 0; i < node->keys; i++) { |
1775 | as_area_t *area = node->value[i]; |
1755 | as_area_t *area = node->value[i]; |
1776 | 1756 | ||
1777 | mutex_lock(&area->lock); |
1757 | mutex_lock(&area->lock); |
1778 | printf("as_area: %p, base=%p, pages=%d (%p - %p)\n", |
1758 | printf("as_area: %p, base=%p, pages=%d (%p - %p)\n", |
1779 | area, area->base, area->pages, area->base, |
1759 | area, area->base, area->pages, area->base, |
1780 | area->base + area->pages*PAGE_SIZE); |
1760 | area->base + area->pages*PAGE_SIZE); |
1781 | mutex_unlock(&area->lock); |
1761 | mutex_unlock(&area->lock); |
1782 | } |
1762 | } |
1783 | } |
1763 | } |
1784 | 1764 | ||
1785 | mutex_unlock(&as->lock); |
1765 | mutex_unlock(&as->lock); |
1786 | interrupts_restore(ipl); |
1766 | interrupts_restore(ipl); |
1787 | } |
1767 | } |
1788 | 1768 | ||
1789 | /** @} |
1769 | /** @} |
1790 | */ |
1770 | */ |
1791 | 1771 |