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