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