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