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