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