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