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