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