| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 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 |
|
|---|
| 29 | /** @addtogroup genericmm
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * @brief Address space related functions.
|
|---|
| 36 | *
|
|---|
| 37 | * This file contains address space manipulation functions.
|
|---|
| 38 | * Roughly speaking, this is a higher-level client of
|
|---|
| 39 | * Virtual Address Translation (VAT) subsystem.
|
|---|
| 40 | *
|
|---|
| 41 | * Functionality provided by this file allows one to
|
|---|
| 42 | * create address spaces and create, resize and share
|
|---|
| 43 | * address space areas.
|
|---|
| 44 | *
|
|---|
| 45 | * @see page.c
|
|---|
| 46 | *
|
|---|
| 47 | */
|
|---|
| 48 |
|
|---|
| 49 | #include <mm/as.h>
|
|---|
| 50 | #include <arch/mm/as.h>
|
|---|
| 51 | #include <mm/page.h>
|
|---|
| 52 | #include <mm/frame.h>
|
|---|
| 53 | #include <mm/slab.h>
|
|---|
| 54 | #include <mm/tlb.h>
|
|---|
| 55 | #include <arch/mm/page.h>
|
|---|
| 56 | #include <genarch/mm/page_pt.h>
|
|---|
| 57 | #include <genarch/mm/page_ht.h>
|
|---|
| 58 | #include <mm/asid.h>
|
|---|
| 59 | #include <arch/mm/asid.h>
|
|---|
| 60 | #include <preemption.h>
|
|---|
| 61 | #include <synch/spinlock.h>
|
|---|
| 62 | #include <synch/mutex.h>
|
|---|
| 63 | #include <adt/list.h>
|
|---|
| 64 | #include <adt/btree.h>
|
|---|
| 65 | #include <proc/task.h>
|
|---|
| 66 | #include <proc/thread.h>
|
|---|
| 67 | #include <arch/asm.h>
|
|---|
| 68 | #include <panic.h>
|
|---|
| 69 | #include <assert.h>
|
|---|
| 70 | #include <print.h>
|
|---|
| 71 | #include <mem.h>
|
|---|
| 72 | #include <macros.h>
|
|---|
| 73 | #include <bitops.h>
|
|---|
| 74 | #include <arch.h>
|
|---|
| 75 | #include <errno.h>
|
|---|
| 76 | #include <config.h>
|
|---|
| 77 | #include <align.h>
|
|---|
| 78 | #include <typedefs.h>
|
|---|
| 79 | #include <syscall/copy.h>
|
|---|
| 80 | #include <arch/interrupt.h>
|
|---|
| 81 | #include <interrupt.h>
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * Each architecture decides what functions will be used to carry out
|
|---|
| 85 | * address space operations such as creating or locking page tables.
|
|---|
| 86 | */
|
|---|
| 87 | as_operations_t *as_operations = NULL;
|
|---|
| 88 |
|
|---|
| 89 | /** Slab for as_t objects.
|
|---|
| 90 | *
|
|---|
| 91 | */
|
|---|
| 92 | static slab_cache_t *as_cache;
|
|---|
| 93 |
|
|---|
| 94 | /** ASID subsystem lock.
|
|---|
| 95 | *
|
|---|
| 96 | * This lock protects:
|
|---|
| 97 | * - inactive_as_with_asid_list
|
|---|
| 98 | * - as->asid for each as of the as_t type
|
|---|
| 99 | * - asids_allocated counter
|
|---|
| 100 | *
|
|---|
| 101 | */
|
|---|
| 102 | SPINLOCK_INITIALIZE(asidlock);
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | * Inactive address spaces (on all processors)
|
|---|
| 106 | * that have valid ASID.
|
|---|
| 107 | */
|
|---|
| 108 | LIST_INITIALIZE(inactive_as_with_asid_list);
|
|---|
| 109 |
|
|---|
| 110 | /** Kernel address space. */
|
|---|
| 111 | as_t *AS_KERNEL = NULL;
|
|---|
| 112 |
|
|---|
| 113 | NO_TRACE static errno_t as_constructor(void *obj, unsigned int flags)
|
|---|
| 114 | {
|
|---|
| 115 | as_t *as = (as_t *) obj;
|
|---|
| 116 |
|
|---|
| 117 | link_initialize(&as->inactive_as_with_asid_link);
|
|---|
| 118 | mutex_initialize(&as->lock, MUTEX_PASSIVE);
|
|---|
| 119 |
|
|---|
| 120 | return as_constructor_arch(as, flags);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | NO_TRACE static size_t as_destructor(void *obj)
|
|---|
| 124 | {
|
|---|
| 125 | return as_destructor_arch((as_t *) obj);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /** Initialize address space subsystem. */
|
|---|
| 129 | void as_init(void)
|
|---|
| 130 | {
|
|---|
| 131 | as_arch_init();
|
|---|
| 132 |
|
|---|
| 133 | as_cache = slab_cache_create("as_t", sizeof(as_t), 0,
|
|---|
| 134 | as_constructor, as_destructor, SLAB_CACHE_MAGDEFERRED);
|
|---|
| 135 |
|
|---|
| 136 | AS_KERNEL = as_create(FLAG_AS_KERNEL);
|
|---|
| 137 | if (!AS_KERNEL)
|
|---|
| 138 | panic("Cannot create kernel address space.");
|
|---|
| 139 |
|
|---|
| 140 | /*
|
|---|
| 141 | * Make sure the kernel address space
|
|---|
| 142 | * reference count never drops to zero.
|
|---|
| 143 | */
|
|---|
| 144 | as_hold(AS_KERNEL);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /** Create address space.
|
|---|
| 148 | *
|
|---|
| 149 | * @param flags Flags that influence the way in wich the address
|
|---|
| 150 | * space is created.
|
|---|
| 151 | *
|
|---|
| 152 | */
|
|---|
| 153 | as_t *as_create(unsigned int flags)
|
|---|
| 154 | {
|
|---|
| 155 | as_t *as = (as_t *) slab_alloc(as_cache, 0);
|
|---|
| 156 | (void) as_create_arch(as, 0);
|
|---|
| 157 |
|
|---|
| 158 | btree_create(&as->as_area_btree);
|
|---|
| 159 |
|
|---|
| 160 | if (flags & FLAG_AS_KERNEL)
|
|---|
| 161 | as->asid = ASID_KERNEL;
|
|---|
| 162 | else
|
|---|
| 163 | as->asid = ASID_INVALID;
|
|---|
| 164 |
|
|---|
| 165 | refcount_init(&as->refcount);
|
|---|
| 166 | as->cpu_refcount = 0;
|
|---|
| 167 |
|
|---|
| 168 | #ifdef AS_PAGE_TABLE
|
|---|
| 169 | as->genarch.page_table = page_table_create(flags);
|
|---|
| 170 | #else
|
|---|
| 171 | page_table_create(flags);
|
|---|
| 172 | #endif
|
|---|
| 173 |
|
|---|
| 174 | return as;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /** Destroy adress space.
|
|---|
| 178 | *
|
|---|
| 179 | * When there are no tasks referencing this address space (i.e. its refcount is
|
|---|
| 180 | * zero), the address space can be destroyed.
|
|---|
| 181 | *
|
|---|
| 182 | * We know that we don't hold any spinlock.
|
|---|
| 183 | *
|
|---|
| 184 | * @param as Address space to be destroyed.
|
|---|
| 185 | *
|
|---|
| 186 | */
|
|---|
| 187 | void as_destroy(as_t *as)
|
|---|
| 188 | {
|
|---|
| 189 | DEADLOCK_PROBE_INIT(p_asidlock);
|
|---|
| 190 |
|
|---|
| 191 | assert(as != AS);
|
|---|
| 192 | assert(refcount_unique(&as->refcount));
|
|---|
| 193 |
|
|---|
| 194 | /*
|
|---|
| 195 | * Since there is no reference to this address space, it is safe not to
|
|---|
| 196 | * lock its mutex.
|
|---|
| 197 | */
|
|---|
| 198 |
|
|---|
| 199 | /*
|
|---|
| 200 | * We need to avoid deadlock between TLB shootdown and asidlock.
|
|---|
| 201 | * We therefore try to take asid conditionally and if we don't succeed,
|
|---|
| 202 | * we enable interrupts and try again. This is done while preemption is
|
|---|
| 203 | * disabled to prevent nested context switches. We also depend on the
|
|---|
| 204 | * fact that so far no spinlocks are held.
|
|---|
| 205 | */
|
|---|
| 206 | preemption_disable();
|
|---|
| 207 | ipl_t ipl = interrupts_read();
|
|---|
| 208 |
|
|---|
| 209 | retry:
|
|---|
| 210 | interrupts_disable();
|
|---|
| 211 | if (!spinlock_trylock(&asidlock)) {
|
|---|
| 212 | interrupts_enable();
|
|---|
| 213 | DEADLOCK_PROBE(p_asidlock, DEADLOCK_THRESHOLD);
|
|---|
| 214 | goto retry;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /* Interrupts disabled, enable preemption */
|
|---|
| 218 | preemption_enable();
|
|---|
| 219 |
|
|---|
| 220 | if ((as->asid != ASID_INVALID) && (as != AS_KERNEL)) {
|
|---|
| 221 | if (as->cpu_refcount == 0)
|
|---|
| 222 | list_remove(&as->inactive_as_with_asid_link);
|
|---|
| 223 |
|
|---|
| 224 | asid_put(as->asid);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | spinlock_unlock(&asidlock);
|
|---|
| 228 | interrupts_restore(ipl);
|
|---|
| 229 |
|
|---|
| 230 | /*
|
|---|
| 231 | * Destroy address space areas of the address space.
|
|---|
| 232 | * The B+tree must be walked carefully because it is
|
|---|
| 233 | * also being destroyed.
|
|---|
| 234 | */
|
|---|
| 235 | bool cond = true;
|
|---|
| 236 | while (cond) {
|
|---|
| 237 | assert(!list_empty(&as->as_area_btree.leaf_list));
|
|---|
| 238 |
|
|---|
| 239 | btree_node_t *node =
|
|---|
| 240 | list_get_instance(list_first(&as->as_area_btree.leaf_list),
|
|---|
| 241 | btree_node_t, leaf_link);
|
|---|
| 242 |
|
|---|
| 243 | if ((cond = node->keys))
|
|---|
| 244 | as_area_destroy(as, node->key[0]);
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | btree_destroy(&as->as_area_btree);
|
|---|
| 248 |
|
|---|
| 249 | #ifdef AS_PAGE_TABLE
|
|---|
| 250 | page_table_destroy(as->genarch.page_table);
|
|---|
| 251 | #else
|
|---|
| 252 | page_table_destroy(NULL);
|
|---|
| 253 | #endif
|
|---|
| 254 |
|
|---|
| 255 | slab_free(as_cache, as);
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /** Hold a reference to an address space.
|
|---|
| 259 | *
|
|---|
| 260 | * Holding a reference to an address space prevents destruction
|
|---|
| 261 | * of that address space.
|
|---|
| 262 | *
|
|---|
| 263 | * @param as Address space to be held.
|
|---|
| 264 | *
|
|---|
| 265 | */
|
|---|
| 266 | NO_TRACE void as_hold(as_t *as)
|
|---|
| 267 | {
|
|---|
| 268 | refcount_up(&as->refcount);
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /** Release a reference to an address space.
|
|---|
| 272 | *
|
|---|
| 273 | * The last one to release a reference to an address space
|
|---|
| 274 | * destroys the address space.
|
|---|
| 275 | *
|
|---|
| 276 | * @param as Address space to be released.
|
|---|
| 277 | *
|
|---|
| 278 | */
|
|---|
| 279 | NO_TRACE void as_release(as_t *as)
|
|---|
| 280 | {
|
|---|
| 281 | if (refcount_down(&as->refcount))
|
|---|
| 282 | as_destroy(as);
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /** Check area conflicts with other areas.
|
|---|
| 286 | *
|
|---|
| 287 | * @param as Address space.
|
|---|
| 288 | * @param addr Starting virtual address of the area being tested.
|
|---|
| 289 | * @param count Number of pages in the area being tested.
|
|---|
| 290 | * @param guarded True if the area being tested is protected by guard pages.
|
|---|
| 291 | * @param avoid Do not touch this area.
|
|---|
| 292 | *
|
|---|
| 293 | * @return True if there is no conflict, false otherwise.
|
|---|
| 294 | *
|
|---|
| 295 | */
|
|---|
| 296 | NO_TRACE static bool check_area_conflicts(as_t *as, uintptr_t addr,
|
|---|
| 297 | size_t count, bool guarded, as_area_t *avoid)
|
|---|
| 298 | {
|
|---|
| 299 | assert((addr % PAGE_SIZE) == 0);
|
|---|
| 300 | assert(mutex_locked(&as->lock));
|
|---|
| 301 |
|
|---|
| 302 | /*
|
|---|
| 303 | * If the addition of the supposed area address and size overflows,
|
|---|
| 304 | * report conflict.
|
|---|
| 305 | */
|
|---|
| 306 | if (overflows_into_positive(addr, P2SZ(count)))
|
|---|
| 307 | return false;
|
|---|
| 308 |
|
|---|
| 309 | /*
|
|---|
| 310 | * We don't want any area to have conflicts with NULL page.
|
|---|
| 311 | */
|
|---|
| 312 | if (overlaps(addr, P2SZ(count), (uintptr_t) NULL, PAGE_SIZE))
|
|---|
| 313 | return false;
|
|---|
| 314 |
|
|---|
| 315 | /*
|
|---|
| 316 | * The leaf node is found in O(log n), where n is proportional to
|
|---|
| 317 | * the number of address space areas belonging to as.
|
|---|
| 318 | * The check for conflicts is then attempted on the rightmost
|
|---|
| 319 | * record in the left neighbour, the leftmost record in the right
|
|---|
| 320 | * neighbour and all records in the leaf node itself.
|
|---|
| 321 | */
|
|---|
| 322 | btree_node_t *leaf;
|
|---|
| 323 | as_area_t *area =
|
|---|
| 324 | (as_area_t *) btree_search(&as->as_area_btree, addr, &leaf);
|
|---|
| 325 | if (area) {
|
|---|
| 326 | if (area != avoid)
|
|---|
| 327 | return false;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | /* First, check the two border cases. */
|
|---|
| 331 | btree_node_t *node =
|
|---|
| 332 | btree_leaf_node_left_neighbour(&as->as_area_btree, leaf);
|
|---|
| 333 | if (node) {
|
|---|
| 334 | area = (as_area_t *) node->value[node->keys - 1];
|
|---|
| 335 |
|
|---|
| 336 | if (area != avoid) {
|
|---|
| 337 | mutex_lock(&area->lock);
|
|---|
| 338 |
|
|---|
| 339 | /*
|
|---|
| 340 | * If at least one of the two areas are protected
|
|---|
| 341 | * by the AS_AREA_GUARD flag then we must be sure
|
|---|
| 342 | * that they are separated by at least one unmapped
|
|---|
| 343 | * page.
|
|---|
| 344 | */
|
|---|
| 345 | int const gp = (guarded ||
|
|---|
| 346 | (area->flags & AS_AREA_GUARD)) ? 1 : 0;
|
|---|
| 347 |
|
|---|
| 348 | /*
|
|---|
| 349 | * The area comes from the left neighbour node, which
|
|---|
| 350 | * means that there already are some areas in the leaf
|
|---|
| 351 | * node, which in turn means that adding gp is safe and
|
|---|
| 352 | * will not cause an integer overflow.
|
|---|
| 353 | */
|
|---|
| 354 | if (overlaps(addr, P2SZ(count), area->base,
|
|---|
| 355 | P2SZ(area->pages + gp))) {
|
|---|
| 356 | mutex_unlock(&area->lock);
|
|---|
| 357 | return false;
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | mutex_unlock(&area->lock);
|
|---|
| 361 | }
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf);
|
|---|
| 365 | if (node) {
|
|---|
| 366 | area = (as_area_t *) node->value[0];
|
|---|
| 367 |
|
|---|
| 368 | if (area != avoid) {
|
|---|
| 369 | int gp;
|
|---|
| 370 |
|
|---|
| 371 | mutex_lock(&area->lock);
|
|---|
| 372 |
|
|---|
| 373 | gp = (guarded || (area->flags & AS_AREA_GUARD)) ? 1 : 0;
|
|---|
| 374 | if (gp && overflows(addr, P2SZ(count))) {
|
|---|
| 375 | /*
|
|---|
| 376 | * Guard page not needed if the supposed area
|
|---|
| 377 | * is adjacent to the end of the address space.
|
|---|
| 378 | * We already know that the following test is
|
|---|
| 379 | * going to fail...
|
|---|
| 380 | */
|
|---|
| 381 | gp--;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | if (overlaps(addr, P2SZ(count + gp), area->base,
|
|---|
| 385 | P2SZ(area->pages))) {
|
|---|
| 386 | mutex_unlock(&area->lock);
|
|---|
| 387 | return false;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | mutex_unlock(&area->lock);
|
|---|
| 391 | }
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | /* Second, check the leaf node. */
|
|---|
| 395 | btree_key_t i;
|
|---|
| 396 | for (i = 0; i < leaf->keys; i++) {
|
|---|
| 397 | area = (as_area_t *) leaf->value[i];
|
|---|
| 398 | int agp;
|
|---|
| 399 | int gp;
|
|---|
| 400 |
|
|---|
| 401 | if (area == avoid)
|
|---|
| 402 | continue;
|
|---|
| 403 |
|
|---|
| 404 | mutex_lock(&area->lock);
|
|---|
| 405 |
|
|---|
| 406 | gp = (guarded || (area->flags & AS_AREA_GUARD)) ? 1 : 0;
|
|---|
| 407 | agp = gp;
|
|---|
| 408 |
|
|---|
| 409 | /*
|
|---|
| 410 | * Sanitize the two possible unsigned integer overflows.
|
|---|
| 411 | */
|
|---|
| 412 | if (gp && overflows(addr, P2SZ(count)))
|
|---|
| 413 | gp--;
|
|---|
| 414 | if (agp && overflows(area->base, P2SZ(area->pages)))
|
|---|
| 415 | agp--;
|
|---|
| 416 |
|
|---|
| 417 | if (overlaps(addr, P2SZ(count + gp), area->base,
|
|---|
| 418 | P2SZ(area->pages + agp))) {
|
|---|
| 419 | mutex_unlock(&area->lock);
|
|---|
| 420 | return false;
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | mutex_unlock(&area->lock);
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | /*
|
|---|
| 427 | * So far, the area does not conflict with other areas.
|
|---|
| 428 | * Check if it is contained in the user address space.
|
|---|
| 429 | */
|
|---|
| 430 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
|
|---|
| 431 | return iswithin(USER_ADDRESS_SPACE_START,
|
|---|
| 432 | (USER_ADDRESS_SPACE_END - USER_ADDRESS_SPACE_START) + 1,
|
|---|
| 433 | addr, P2SZ(count));
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | return true;
|
|---|
| 437 | }
|
|---|
| 438 |
|
|---|
| 439 | /** Return pointer to unmapped address space area
|
|---|
| 440 | *
|
|---|
| 441 | * The address space must be already locked when calling
|
|---|
| 442 | * this function.
|
|---|
| 443 | *
|
|---|
| 444 | * @param as Address space.
|
|---|
| 445 | * @param bound Lowest address bound.
|
|---|
| 446 | * @param size Requested size of the allocation.
|
|---|
| 447 | * @param guarded True if the allocation must be protected by guard pages.
|
|---|
| 448 | *
|
|---|
| 449 | * @return Address of the beginning of unmapped address space area.
|
|---|
| 450 | * @return -1 if no suitable address space area was found.
|
|---|
| 451 | *
|
|---|
| 452 | */
|
|---|
| 453 | NO_TRACE static uintptr_t as_get_unmapped_area(as_t *as, uintptr_t bound,
|
|---|
| 454 | size_t size, bool guarded)
|
|---|
| 455 | {
|
|---|
| 456 | assert(mutex_locked(&as->lock));
|
|---|
| 457 |
|
|---|
| 458 | if (size == 0)
|
|---|
| 459 | return (uintptr_t) -1;
|
|---|
| 460 |
|
|---|
| 461 | /*
|
|---|
| 462 | * Make sure we allocate from page-aligned
|
|---|
| 463 | * address. Check for possible overflow in
|
|---|
| 464 | * each step.
|
|---|
| 465 | */
|
|---|
| 466 |
|
|---|
| 467 | size_t pages = SIZE2FRAMES(size);
|
|---|
| 468 |
|
|---|
| 469 | /*
|
|---|
| 470 | * Find the lowest unmapped address aligned on the size
|
|---|
| 471 | * boundary, not smaller than bound and of the required size.
|
|---|
| 472 | */
|
|---|
| 473 |
|
|---|
| 474 | /* First check the bound address itself */
|
|---|
| 475 | uintptr_t addr = ALIGN_UP(bound, PAGE_SIZE);
|
|---|
| 476 | if (addr >= bound) {
|
|---|
| 477 | if (guarded) {
|
|---|
| 478 | /*
|
|---|
| 479 | * Leave an unmapped page between the lower
|
|---|
| 480 | * bound and the area's start address.
|
|---|
| 481 | */
|
|---|
| 482 | addr += P2SZ(1);
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| 485 | if (check_area_conflicts(as, addr, pages, guarded, NULL))
|
|---|
| 486 | return addr;
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | /* Eventually check the addresses behind each area */
|
|---|
| 490 | list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t, node) {
|
|---|
| 491 |
|
|---|
| 492 | for (btree_key_t i = 0; i < node->keys; i++) {
|
|---|
| 493 | as_area_t *area = (as_area_t *) node->value[i];
|
|---|
| 494 |
|
|---|
| 495 | mutex_lock(&area->lock);
|
|---|
| 496 |
|
|---|
| 497 | addr =
|
|---|
| 498 | ALIGN_UP(area->base + P2SZ(area->pages), PAGE_SIZE);
|
|---|
| 499 |
|
|---|
| 500 | if (guarded || area->flags & AS_AREA_GUARD) {
|
|---|
| 501 | /*
|
|---|
| 502 | * We must leave an unmapped page
|
|---|
| 503 | * between the two areas.
|
|---|
| 504 | */
|
|---|
| 505 | addr += P2SZ(1);
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | bool avail =
|
|---|
| 509 | ((addr >= bound) && (addr >= area->base) &&
|
|---|
| 510 | (check_area_conflicts(as, addr, pages, guarded, area)));
|
|---|
| 511 |
|
|---|
| 512 | mutex_unlock(&area->lock);
|
|---|
| 513 |
|
|---|
| 514 | if (avail)
|
|---|
| 515 | return addr;
|
|---|
| 516 | }
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | /* No suitable address space area found */
|
|---|
| 520 | return (uintptr_t) -1;
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | /** Remove reference to address space area share info.
|
|---|
| 524 | *
|
|---|
| 525 | * If the reference count drops to 0, the sh_info is deallocated.
|
|---|
| 526 | *
|
|---|
| 527 | * @param sh_info Pointer to address space area share info.
|
|---|
| 528 | *
|
|---|
| 529 | */
|
|---|
| 530 | NO_TRACE static void sh_info_remove_reference(share_info_t *sh_info)
|
|---|
| 531 | {
|
|---|
| 532 | bool dealloc = false;
|
|---|
| 533 |
|
|---|
| 534 | mutex_lock(&sh_info->lock);
|
|---|
| 535 | assert(sh_info->refcount);
|
|---|
| 536 |
|
|---|
| 537 | if (--sh_info->refcount == 0) {
|
|---|
| 538 | dealloc = true;
|
|---|
| 539 |
|
|---|
| 540 | /*
|
|---|
| 541 | * Now walk carefully the pagemap B+tree and free/remove
|
|---|
| 542 | * reference from all frames found there.
|
|---|
| 543 | */
|
|---|
| 544 | list_foreach(sh_info->pagemap.leaf_list, leaf_link,
|
|---|
| 545 | btree_node_t, node) {
|
|---|
| 546 | btree_key_t i;
|
|---|
| 547 |
|
|---|
| 548 | for (i = 0; i < node->keys; i++)
|
|---|
| 549 | frame_free((uintptr_t) node->value[i], 1);
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | }
|
|---|
| 553 | mutex_unlock(&sh_info->lock);
|
|---|
| 554 |
|
|---|
| 555 | if (dealloc) {
|
|---|
| 556 | if (sh_info->backend && sh_info->backend->destroy_shared_data) {
|
|---|
| 557 | sh_info->backend->destroy_shared_data(
|
|---|
| 558 | sh_info->backend_shared_data);
|
|---|
| 559 | }
|
|---|
| 560 | btree_destroy(&sh_info->pagemap);
|
|---|
| 561 | free(sh_info);
|
|---|
| 562 | }
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 | /** Create address space area of common attributes.
|
|---|
| 566 | *
|
|---|
| 567 | * The created address space area is added to the target address space.
|
|---|
| 568 | *
|
|---|
| 569 | * @param as Target address space.
|
|---|
| 570 | * @param flags Flags of the area memory.
|
|---|
| 571 | * @param size Size of area.
|
|---|
| 572 | * @param attrs Attributes of the area.
|
|---|
| 573 | * @param backend Address space area backend. NULL if no backend is used.
|
|---|
| 574 | * @param backend_data NULL or a pointer to custom backend data.
|
|---|
| 575 | * @param base Starting virtual address of the area.
|
|---|
| 576 | * If set to AS_AREA_ANY, a suitable mappable area is
|
|---|
| 577 | * found.
|
|---|
| 578 | * @param bound Lowest address bound if base is set to AS_AREA_ANY.
|
|---|
| 579 | * Otherwise ignored.
|
|---|
| 580 | *
|
|---|
| 581 | * @return Address space area on success or NULL on failure.
|
|---|
| 582 | *
|
|---|
| 583 | */
|
|---|
| 584 | as_area_t *as_area_create(as_t *as, unsigned int flags, size_t size,
|
|---|
| 585 | unsigned int attrs, mem_backend_t *backend,
|
|---|
| 586 | mem_backend_data_t *backend_data, uintptr_t *base, uintptr_t bound)
|
|---|
| 587 | {
|
|---|
| 588 | if ((*base != (uintptr_t) AS_AREA_ANY) && !IS_ALIGNED(*base, PAGE_SIZE))
|
|---|
| 589 | return NULL;
|
|---|
| 590 |
|
|---|
| 591 | if (size == 0)
|
|---|
| 592 | return NULL;
|
|---|
| 593 |
|
|---|
| 594 | size_t pages = SIZE2FRAMES(size);
|
|---|
| 595 |
|
|---|
| 596 | /* Writeable executable areas are not supported. */
|
|---|
| 597 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
|
|---|
| 598 | return NULL;
|
|---|
| 599 |
|
|---|
| 600 | bool const guarded = flags & AS_AREA_GUARD;
|
|---|
| 601 |
|
|---|
| 602 | mutex_lock(&as->lock);
|
|---|
| 603 |
|
|---|
| 604 | if (*base == (uintptr_t) AS_AREA_ANY) {
|
|---|
| 605 | *base = as_get_unmapped_area(as, bound, size, guarded);
|
|---|
| 606 | if (*base == (uintptr_t) -1) {
|
|---|
| 607 | mutex_unlock(&as->lock);
|
|---|
| 608 | return NULL;
|
|---|
| 609 | }
|
|---|
| 610 | }
|
|---|
| 611 |
|
|---|
| 612 | if (overflows_into_positive(*base, size)) {
|
|---|
| 613 | mutex_unlock(&as->lock);
|
|---|
| 614 | return NULL;
|
|---|
| 615 | }
|
|---|
| 616 |
|
|---|
| 617 | if (!check_area_conflicts(as, *base, pages, guarded, NULL)) {
|
|---|
| 618 | mutex_unlock(&as->lock);
|
|---|
| 619 | return NULL;
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | as_area_t *area = (as_area_t *) malloc(sizeof(as_area_t));
|
|---|
| 623 | if (!area) {
|
|---|
| 624 | mutex_unlock(&as->lock);
|
|---|
| 625 | return NULL;
|
|---|
| 626 | }
|
|---|
| 627 |
|
|---|
| 628 | mutex_initialize(&area->lock, MUTEX_PASSIVE);
|
|---|
| 629 |
|
|---|
| 630 | area->as = as;
|
|---|
| 631 | area->flags = flags;
|
|---|
| 632 | area->attributes = attrs;
|
|---|
| 633 | area->pages = pages;
|
|---|
| 634 | area->resident = 0;
|
|---|
| 635 | area->base = *base;
|
|---|
| 636 | area->backend = backend;
|
|---|
| 637 | area->sh_info = NULL;
|
|---|
| 638 |
|
|---|
| 639 | if (backend_data)
|
|---|
| 640 | area->backend_data = *backend_data;
|
|---|
| 641 | else
|
|---|
| 642 | memsetb(&area->backend_data, sizeof(area->backend_data), 0);
|
|---|
| 643 |
|
|---|
| 644 | share_info_t *si = NULL;
|
|---|
| 645 |
|
|---|
| 646 | /*
|
|---|
| 647 | * Create the sharing info structure.
|
|---|
| 648 | * We do this in advance for every new area, even if it is not going
|
|---|
| 649 | * to be shared.
|
|---|
| 650 | */
|
|---|
| 651 | if (!(attrs & AS_AREA_ATTR_PARTIAL)) {
|
|---|
| 652 | si = (share_info_t *) malloc(sizeof(share_info_t));
|
|---|
| 653 | if (!si) {
|
|---|
| 654 | free(area);
|
|---|
| 655 | mutex_unlock(&as->lock);
|
|---|
| 656 | return NULL;
|
|---|
| 657 | }
|
|---|
| 658 | mutex_initialize(&si->lock, MUTEX_PASSIVE);
|
|---|
| 659 | si->refcount = 1;
|
|---|
| 660 | si->shared = false;
|
|---|
| 661 | si->backend_shared_data = NULL;
|
|---|
| 662 | si->backend = backend;
|
|---|
| 663 | btree_create(&si->pagemap);
|
|---|
| 664 |
|
|---|
| 665 | area->sh_info = si;
|
|---|
| 666 |
|
|---|
| 667 | if (area->backend && area->backend->create_shared_data) {
|
|---|
| 668 | if (!area->backend->create_shared_data(area)) {
|
|---|
| 669 | free(area);
|
|---|
| 670 | mutex_unlock(&as->lock);
|
|---|
| 671 | sh_info_remove_reference(si);
|
|---|
| 672 | return NULL;
|
|---|
| 673 | }
|
|---|
| 674 | }
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | if (area->backend && area->backend->create) {
|
|---|
| 678 | if (!area->backend->create(area)) {
|
|---|
| 679 | free(area);
|
|---|
| 680 | mutex_unlock(&as->lock);
|
|---|
| 681 | if (!(attrs & AS_AREA_ATTR_PARTIAL))
|
|---|
| 682 | sh_info_remove_reference(si);
|
|---|
| 683 | return NULL;
|
|---|
| 684 | }
|
|---|
| 685 | }
|
|---|
| 686 |
|
|---|
| 687 | btree_create(&area->used_space);
|
|---|
| 688 | btree_insert(&as->as_area_btree, *base, (void *) area,
|
|---|
| 689 | NULL);
|
|---|
| 690 |
|
|---|
| 691 | mutex_unlock(&as->lock);
|
|---|
| 692 |
|
|---|
| 693 | return area;
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | /** Find address space area and lock it.
|
|---|
| 697 | *
|
|---|
| 698 | * @param as Address space.
|
|---|
| 699 | * @param va Virtual address.
|
|---|
| 700 | *
|
|---|
| 701 | * @return Locked address space area containing va on success or
|
|---|
| 702 | * NULL on failure.
|
|---|
| 703 | *
|
|---|
| 704 | */
|
|---|
| 705 | NO_TRACE static as_area_t *find_area_and_lock(as_t *as, uintptr_t va)
|
|---|
| 706 | {
|
|---|
| 707 | assert(mutex_locked(&as->lock));
|
|---|
| 708 |
|
|---|
| 709 | btree_node_t *leaf;
|
|---|
| 710 | as_area_t *area = (as_area_t *) btree_search(&as->as_area_btree, va,
|
|---|
| 711 | &leaf);
|
|---|
| 712 | if (area) {
|
|---|
| 713 | /* va is the base address of an address space area */
|
|---|
| 714 | mutex_lock(&area->lock);
|
|---|
| 715 | return area;
|
|---|
| 716 | }
|
|---|
| 717 |
|
|---|
| 718 | /*
|
|---|
| 719 | * Search the leaf node and the rightmost record of its left neighbour
|
|---|
| 720 | * to find out whether this is a miss or va belongs to an address
|
|---|
| 721 | * space area found there.
|
|---|
| 722 | */
|
|---|
| 723 |
|
|---|
| 724 | /* First, search the leaf node itself. */
|
|---|
| 725 | btree_key_t i;
|
|---|
| 726 |
|
|---|
| 727 | for (i = 0; i < leaf->keys; i++) {
|
|---|
| 728 | area = (as_area_t *) leaf->value[i];
|
|---|
| 729 |
|
|---|
| 730 | mutex_lock(&area->lock);
|
|---|
| 731 |
|
|---|
| 732 | if ((area->base <= va) &&
|
|---|
| 733 | (va <= area->base + (P2SZ(area->pages) - 1)))
|
|---|
| 734 | return area;
|
|---|
| 735 |
|
|---|
| 736 | mutex_unlock(&area->lock);
|
|---|
| 737 | }
|
|---|
| 738 |
|
|---|
| 739 | /*
|
|---|
| 740 | * Second, locate the left neighbour and test its last record.
|
|---|
| 741 | * Because of its position in the B+tree, it must have base < va.
|
|---|
| 742 | */
|
|---|
| 743 | btree_node_t *lnode = btree_leaf_node_left_neighbour(&as->as_area_btree,
|
|---|
| 744 | leaf);
|
|---|
| 745 | if (lnode) {
|
|---|
| 746 | area = (as_area_t *) lnode->value[lnode->keys - 1];
|
|---|
| 747 |
|
|---|
| 748 | mutex_lock(&area->lock);
|
|---|
| 749 |
|
|---|
| 750 | if (va <= area->base + (P2SZ(area->pages) - 1))
|
|---|
| 751 | return area;
|
|---|
| 752 |
|
|---|
| 753 | mutex_unlock(&area->lock);
|
|---|
| 754 | }
|
|---|
| 755 |
|
|---|
| 756 | return NULL;
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | /** Find address space area and change it.
|
|---|
| 760 | *
|
|---|
| 761 | * @param as Address space.
|
|---|
| 762 | * @param address Virtual address belonging to the area to be changed.
|
|---|
| 763 | * Must be page-aligned.
|
|---|
| 764 | * @param size New size of the virtual memory block starting at
|
|---|
| 765 | * address.
|
|---|
| 766 | * @param flags Flags influencing the remap operation. Currently unused.
|
|---|
| 767 | *
|
|---|
| 768 | * @return Zero on success or a value from @ref errno.h otherwise.
|
|---|
| 769 | *
|
|---|
| 770 | */
|
|---|
| 771 | errno_t as_area_resize(as_t *as, uintptr_t address, size_t size, unsigned int flags)
|
|---|
| 772 | {
|
|---|
| 773 | if (!IS_ALIGNED(address, PAGE_SIZE))
|
|---|
| 774 | return EINVAL;
|
|---|
| 775 |
|
|---|
| 776 | mutex_lock(&as->lock);
|
|---|
| 777 |
|
|---|
| 778 | /*
|
|---|
| 779 | * Locate the area.
|
|---|
| 780 | */
|
|---|
| 781 | as_area_t *area = find_area_and_lock(as, address);
|
|---|
| 782 | if (!area) {
|
|---|
| 783 | mutex_unlock(&as->lock);
|
|---|
| 784 | return ENOENT;
|
|---|
| 785 | }
|
|---|
| 786 |
|
|---|
| 787 | if (!area->backend->is_resizable(area)) {
|
|---|
| 788 | /*
|
|---|
| 789 | * The backend does not support resizing for this area.
|
|---|
| 790 | */
|
|---|
| 791 | mutex_unlock(&area->lock);
|
|---|
| 792 | mutex_unlock(&as->lock);
|
|---|
| 793 | return ENOTSUP;
|
|---|
| 794 | }
|
|---|
| 795 |
|
|---|
| 796 | mutex_lock(&area->sh_info->lock);
|
|---|
| 797 | if (area->sh_info->shared) {
|
|---|
| 798 | /*
|
|---|
| 799 | * Remapping of shared address space areas
|
|---|
| 800 | * is not supported.
|
|---|
| 801 | */
|
|---|
| 802 | mutex_unlock(&area->sh_info->lock);
|
|---|
| 803 | mutex_unlock(&area->lock);
|
|---|
| 804 | mutex_unlock(&as->lock);
|
|---|
| 805 | return ENOTSUP;
|
|---|
| 806 | }
|
|---|
| 807 | mutex_unlock(&area->sh_info->lock);
|
|---|
| 808 |
|
|---|
| 809 | size_t pages = SIZE2FRAMES((address - area->base) + size);
|
|---|
| 810 | if (!pages) {
|
|---|
| 811 | /*
|
|---|
| 812 | * Zero size address space areas are not allowed.
|
|---|
| 813 | */
|
|---|
| 814 | mutex_unlock(&area->lock);
|
|---|
| 815 | mutex_unlock(&as->lock);
|
|---|
| 816 | return EPERM;
|
|---|
| 817 | }
|
|---|
| 818 |
|
|---|
| 819 | if (pages < area->pages) {
|
|---|
| 820 | uintptr_t start_free = area->base + P2SZ(pages);
|
|---|
| 821 |
|
|---|
| 822 | /*
|
|---|
| 823 | * Shrinking the area.
|
|---|
| 824 | * No need to check for overlaps.
|
|---|
| 825 | */
|
|---|
| 826 |
|
|---|
| 827 | page_table_lock(as, false);
|
|---|
| 828 |
|
|---|
| 829 | /*
|
|---|
| 830 | * Remove frames belonging to used space starting from
|
|---|
| 831 | * the highest addresses downwards until an overlap with
|
|---|
| 832 | * the resized address space area is found. Note that this
|
|---|
| 833 | * is also the right way to remove part of the used_space
|
|---|
| 834 | * B+tree leaf list.
|
|---|
| 835 | */
|
|---|
| 836 | bool cond = true;
|
|---|
| 837 | while (cond) {
|
|---|
| 838 | assert(!list_empty(&area->used_space.leaf_list));
|
|---|
| 839 |
|
|---|
| 840 | btree_node_t *node =
|
|---|
| 841 | list_get_instance(list_last(&area->used_space.leaf_list),
|
|---|
| 842 | btree_node_t, leaf_link);
|
|---|
| 843 |
|
|---|
| 844 | if ((cond = (node->keys != 0))) {
|
|---|
| 845 | uintptr_t ptr = node->key[node->keys - 1];
|
|---|
| 846 | size_t node_size =
|
|---|
| 847 | (size_t) node->value[node->keys - 1];
|
|---|
| 848 | size_t i = 0;
|
|---|
| 849 |
|
|---|
| 850 | if (overlaps(ptr, P2SZ(node_size), area->base,
|
|---|
| 851 | P2SZ(pages))) {
|
|---|
| 852 |
|
|---|
| 853 | if (ptr + P2SZ(node_size) <= start_free) {
|
|---|
| 854 | /*
|
|---|
| 855 | * The whole interval fits
|
|---|
| 856 | * completely in the resized
|
|---|
| 857 | * address space area.
|
|---|
| 858 | */
|
|---|
| 859 | break;
|
|---|
| 860 | }
|
|---|
| 861 |
|
|---|
| 862 | /*
|
|---|
| 863 | * Part of the interval corresponding
|
|---|
| 864 | * to b and c overlaps with the resized
|
|---|
| 865 | * address space area.
|
|---|
| 866 | */
|
|---|
| 867 |
|
|---|
| 868 | /* We are almost done */
|
|---|
| 869 | cond = false;
|
|---|
| 870 | i = (start_free - ptr) >> PAGE_WIDTH;
|
|---|
| 871 | if (!used_space_remove(area, start_free,
|
|---|
| 872 | node_size - i))
|
|---|
| 873 | panic("Cannot remove used space.");
|
|---|
| 874 | } else {
|
|---|
| 875 | /*
|
|---|
| 876 | * The interval of used space can be
|
|---|
| 877 | * completely removed.
|
|---|
| 878 | */
|
|---|
| 879 | if (!used_space_remove(area, ptr, node_size))
|
|---|
| 880 | panic("Cannot remove used space.");
|
|---|
| 881 | }
|
|---|
| 882 |
|
|---|
| 883 | /*
|
|---|
| 884 | * Start TLB shootdown sequence.
|
|---|
| 885 | *
|
|---|
| 886 | * The sequence is rather short and can be
|
|---|
| 887 | * repeated multiple times. The reason is that
|
|---|
| 888 | * we don't want to have used_space_remove()
|
|---|
| 889 | * inside the sequence as it may use a blocking
|
|---|
| 890 | * memory allocation for its B+tree. Blocking
|
|---|
| 891 | * while holding the tlblock spinlock is
|
|---|
| 892 | * forbidden and would hit a kernel assertion.
|
|---|
| 893 | */
|
|---|
| 894 |
|
|---|
| 895 | ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES,
|
|---|
| 896 | as->asid, area->base + P2SZ(pages),
|
|---|
| 897 | area->pages - pages);
|
|---|
| 898 |
|
|---|
| 899 | for (; i < node_size; i++) {
|
|---|
| 900 | pte_t pte;
|
|---|
| 901 | bool found = page_mapping_find(as,
|
|---|
| 902 | ptr + P2SZ(i), false, &pte);
|
|---|
| 903 |
|
|---|
| 904 | assert(found);
|
|---|
| 905 | assert(PTE_VALID(&pte));
|
|---|
| 906 | assert(PTE_PRESENT(&pte));
|
|---|
| 907 |
|
|---|
| 908 | if ((area->backend) &&
|
|---|
| 909 | (area->backend->frame_free)) {
|
|---|
| 910 | area->backend->frame_free(area,
|
|---|
| 911 | ptr + P2SZ(i),
|
|---|
| 912 | PTE_GET_FRAME(&pte));
|
|---|
| 913 | }
|
|---|
| 914 |
|
|---|
| 915 | page_mapping_remove(as, ptr + P2SZ(i));
|
|---|
| 916 | }
|
|---|
| 917 |
|
|---|
| 918 | /*
|
|---|
| 919 | * Finish TLB shootdown sequence.
|
|---|
| 920 | */
|
|---|
| 921 |
|
|---|
| 922 | tlb_invalidate_pages(as->asid,
|
|---|
| 923 | area->base + P2SZ(pages),
|
|---|
| 924 | area->pages - pages);
|
|---|
| 925 |
|
|---|
| 926 | /*
|
|---|
| 927 | * Invalidate software translation caches
|
|---|
| 928 | * (e.g. TSB on sparc64, PHT on ppc32).
|
|---|
| 929 | */
|
|---|
| 930 | as_invalidate_translation_cache(as,
|
|---|
| 931 | area->base + P2SZ(pages),
|
|---|
| 932 | area->pages - pages);
|
|---|
| 933 | tlb_shootdown_finalize(ipl);
|
|---|
| 934 | }
|
|---|
| 935 | }
|
|---|
| 936 | page_table_unlock(as, false);
|
|---|
| 937 | } else {
|
|---|
| 938 | /*
|
|---|
| 939 | * Growing the area.
|
|---|
| 940 | */
|
|---|
| 941 |
|
|---|
| 942 | if (overflows_into_positive(address, P2SZ(pages)))
|
|---|
| 943 | return EINVAL;
|
|---|
| 944 |
|
|---|
| 945 | /*
|
|---|
| 946 | * Check for overlaps with other address space areas.
|
|---|
| 947 | */
|
|---|
| 948 | bool const guarded = area->flags & AS_AREA_GUARD;
|
|---|
| 949 | if (!check_area_conflicts(as, address, pages, guarded, area)) {
|
|---|
| 950 | mutex_unlock(&area->lock);
|
|---|
| 951 | mutex_unlock(&as->lock);
|
|---|
| 952 | return EADDRNOTAVAIL;
|
|---|
| 953 | }
|
|---|
| 954 | }
|
|---|
| 955 |
|
|---|
| 956 | if (area->backend && area->backend->resize) {
|
|---|
| 957 | if (!area->backend->resize(area, pages)) {
|
|---|
| 958 | mutex_unlock(&area->lock);
|
|---|
| 959 | mutex_unlock(&as->lock);
|
|---|
| 960 | return ENOMEM;
|
|---|
| 961 | }
|
|---|
| 962 | }
|
|---|
| 963 |
|
|---|
| 964 | area->pages = pages;
|
|---|
| 965 |
|
|---|
| 966 | mutex_unlock(&area->lock);
|
|---|
| 967 | mutex_unlock(&as->lock);
|
|---|
| 968 |
|
|---|
| 969 | return 0;
|
|---|
| 970 | }
|
|---|
| 971 |
|
|---|
| 972 | /** Destroy address space area.
|
|---|
| 973 | *
|
|---|
| 974 | * @param as Address space.
|
|---|
| 975 | * @param address Address within the area to be deleted.
|
|---|
| 976 | *
|
|---|
| 977 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 978 | *
|
|---|
| 979 | */
|
|---|
| 980 | errno_t as_area_destroy(as_t *as, uintptr_t address)
|
|---|
| 981 | {
|
|---|
| 982 | mutex_lock(&as->lock);
|
|---|
| 983 |
|
|---|
| 984 | as_area_t *area = find_area_and_lock(as, address);
|
|---|
| 985 | if (!area) {
|
|---|
| 986 | mutex_unlock(&as->lock);
|
|---|
| 987 | return ENOENT;
|
|---|
| 988 | }
|
|---|
| 989 |
|
|---|
| 990 | if (area->backend && area->backend->destroy)
|
|---|
| 991 | area->backend->destroy(area);
|
|---|
| 992 |
|
|---|
| 993 | uintptr_t base = area->base;
|
|---|
| 994 |
|
|---|
| 995 | page_table_lock(as, false);
|
|---|
| 996 |
|
|---|
| 997 | /*
|
|---|
| 998 | * Start TLB shootdown sequence.
|
|---|
| 999 | */
|
|---|
| 1000 | ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base,
|
|---|
| 1001 | area->pages);
|
|---|
| 1002 |
|
|---|
| 1003 | /*
|
|---|
| 1004 | * Visit only the pages mapped by used_space B+tree.
|
|---|
| 1005 | */
|
|---|
| 1006 | list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
|
|---|
| 1007 | node) {
|
|---|
| 1008 | btree_key_t i;
|
|---|
| 1009 |
|
|---|
| 1010 | for (i = 0; i < node->keys; i++) {
|
|---|
| 1011 | uintptr_t ptr = node->key[i];
|
|---|
| 1012 | size_t size;
|
|---|
| 1013 |
|
|---|
| 1014 | for (size = 0; size < (size_t) node->value[i]; size++) {
|
|---|
| 1015 | pte_t pte;
|
|---|
| 1016 | bool found = page_mapping_find(as,
|
|---|
| 1017 | ptr + P2SZ(size), false, &pte);
|
|---|
| 1018 |
|
|---|
| 1019 | assert(found);
|
|---|
| 1020 | assert(PTE_VALID(&pte));
|
|---|
| 1021 | assert(PTE_PRESENT(&pte));
|
|---|
| 1022 |
|
|---|
| 1023 | if ((area->backend) &&
|
|---|
| 1024 | (area->backend->frame_free)) {
|
|---|
| 1025 | area->backend->frame_free(area,
|
|---|
| 1026 | ptr + P2SZ(size),
|
|---|
| 1027 | PTE_GET_FRAME(&pte));
|
|---|
| 1028 | }
|
|---|
| 1029 |
|
|---|
| 1030 | page_mapping_remove(as, ptr + P2SZ(size));
|
|---|
| 1031 | }
|
|---|
| 1032 | }
|
|---|
| 1033 | }
|
|---|
| 1034 |
|
|---|
| 1035 | /*
|
|---|
| 1036 | * Finish TLB shootdown sequence.
|
|---|
| 1037 | */
|
|---|
| 1038 |
|
|---|
| 1039 | tlb_invalidate_pages(as->asid, area->base, area->pages);
|
|---|
| 1040 |
|
|---|
| 1041 | /*
|
|---|
| 1042 | * Invalidate potential software translation caches
|
|---|
| 1043 | * (e.g. TSB on sparc64, PHT on ppc32).
|
|---|
| 1044 | */
|
|---|
| 1045 | as_invalidate_translation_cache(as, area->base, area->pages);
|
|---|
| 1046 | tlb_shootdown_finalize(ipl);
|
|---|
| 1047 |
|
|---|
| 1048 | page_table_unlock(as, false);
|
|---|
| 1049 |
|
|---|
| 1050 | btree_destroy(&area->used_space);
|
|---|
| 1051 |
|
|---|
| 1052 | area->attributes |= AS_AREA_ATTR_PARTIAL;
|
|---|
| 1053 |
|
|---|
| 1054 | sh_info_remove_reference(area->sh_info);
|
|---|
| 1055 |
|
|---|
| 1056 | mutex_unlock(&area->lock);
|
|---|
| 1057 |
|
|---|
| 1058 | /*
|
|---|
| 1059 | * Remove the empty area from address space.
|
|---|
| 1060 | */
|
|---|
| 1061 | btree_remove(&as->as_area_btree, base, NULL);
|
|---|
| 1062 |
|
|---|
| 1063 | free(area);
|
|---|
| 1064 |
|
|---|
| 1065 | mutex_unlock(&as->lock);
|
|---|
| 1066 | return 0;
|
|---|
| 1067 | }
|
|---|
| 1068 |
|
|---|
| 1069 | /** Share address space area with another or the same address space.
|
|---|
| 1070 | *
|
|---|
| 1071 | * Address space area mapping is shared with a new address space area.
|
|---|
| 1072 | * If the source address space area has not been shared so far,
|
|---|
| 1073 | * a new sh_info is created. The new address space area simply gets the
|
|---|
| 1074 | * sh_info of the source area. The process of duplicating the
|
|---|
| 1075 | * mapping is done through the backend share function.
|
|---|
| 1076 | *
|
|---|
| 1077 | * @param src_as Pointer to source address space.
|
|---|
| 1078 | * @param src_base Base address of the source address space area.
|
|---|
| 1079 | * @param acc_size Expected size of the source area.
|
|---|
| 1080 | * @param dst_as Pointer to destination address space.
|
|---|
| 1081 | * @param dst_flags_mask Destination address space area flags mask.
|
|---|
| 1082 | * @param dst_base Target base address. If set to -1,
|
|---|
| 1083 | * a suitable mappable area is found.
|
|---|
| 1084 | * @param bound Lowest address bound if dst_base is set to -1.
|
|---|
| 1085 | * Otherwise ignored.
|
|---|
| 1086 | *
|
|---|
| 1087 | * @return Zero on success.
|
|---|
| 1088 | * @return ENOENT if there is no such task or such address space.
|
|---|
| 1089 | * @return EPERM if there was a problem in accepting the area.
|
|---|
| 1090 | * @return ENOMEM if there was a problem in allocating destination
|
|---|
| 1091 | * address space area.
|
|---|
| 1092 | * @return ENOTSUP if the address space area backend does not support
|
|---|
| 1093 | * sharing.
|
|---|
| 1094 | *
|
|---|
| 1095 | */
|
|---|
| 1096 | errno_t as_area_share(as_t *src_as, uintptr_t src_base, size_t acc_size,
|
|---|
| 1097 | as_t *dst_as, unsigned int dst_flags_mask, uintptr_t *dst_base,
|
|---|
| 1098 | uintptr_t bound)
|
|---|
| 1099 | {
|
|---|
| 1100 | mutex_lock(&src_as->lock);
|
|---|
| 1101 | as_area_t *src_area = find_area_and_lock(src_as, src_base);
|
|---|
| 1102 | if (!src_area) {
|
|---|
| 1103 | /*
|
|---|
| 1104 | * Could not find the source address space area.
|
|---|
| 1105 | */
|
|---|
| 1106 | mutex_unlock(&src_as->lock);
|
|---|
| 1107 | return ENOENT;
|
|---|
| 1108 | }
|
|---|
| 1109 |
|
|---|
| 1110 | if (!src_area->backend->is_shareable(src_area)) {
|
|---|
| 1111 | /*
|
|---|
| 1112 | * The backend does not permit sharing of this area.
|
|---|
| 1113 | */
|
|---|
| 1114 | mutex_unlock(&src_area->lock);
|
|---|
| 1115 | mutex_unlock(&src_as->lock);
|
|---|
| 1116 | return ENOTSUP;
|
|---|
| 1117 | }
|
|---|
| 1118 |
|
|---|
| 1119 | size_t src_size = P2SZ(src_area->pages);
|
|---|
| 1120 | unsigned int src_flags = src_area->flags;
|
|---|
| 1121 | mem_backend_t *src_backend = src_area->backend;
|
|---|
| 1122 | mem_backend_data_t src_backend_data = src_area->backend_data;
|
|---|
| 1123 |
|
|---|
| 1124 | /* Share the cacheable flag from the original mapping */
|
|---|
| 1125 | if (src_flags & AS_AREA_CACHEABLE)
|
|---|
| 1126 | dst_flags_mask |= AS_AREA_CACHEABLE;
|
|---|
| 1127 |
|
|---|
| 1128 | if ((src_size != acc_size) ||
|
|---|
| 1129 | ((src_flags & dst_flags_mask) != dst_flags_mask)) {
|
|---|
| 1130 | mutex_unlock(&src_area->lock);
|
|---|
| 1131 | mutex_unlock(&src_as->lock);
|
|---|
| 1132 | return EPERM;
|
|---|
| 1133 | }
|
|---|
| 1134 |
|
|---|
| 1135 | /*
|
|---|
| 1136 | * Now we are committed to sharing the area.
|
|---|
| 1137 | * First, prepare the area for sharing.
|
|---|
| 1138 | * Then it will be safe to unlock it.
|
|---|
| 1139 | */
|
|---|
| 1140 | share_info_t *sh_info = src_area->sh_info;
|
|---|
| 1141 |
|
|---|
| 1142 | mutex_lock(&sh_info->lock);
|
|---|
| 1143 | sh_info->refcount++;
|
|---|
| 1144 | bool shared = sh_info->shared;
|
|---|
| 1145 | sh_info->shared = true;
|
|---|
| 1146 | mutex_unlock(&sh_info->lock);
|
|---|
| 1147 |
|
|---|
| 1148 | if (!shared) {
|
|---|
| 1149 | /*
|
|---|
| 1150 | * Call the backend to setup sharing.
|
|---|
| 1151 | * This only happens once for each sh_info.
|
|---|
| 1152 | */
|
|---|
| 1153 | src_area->backend->share(src_area);
|
|---|
| 1154 | }
|
|---|
| 1155 |
|
|---|
| 1156 | mutex_unlock(&src_area->lock);
|
|---|
| 1157 | mutex_unlock(&src_as->lock);
|
|---|
| 1158 |
|
|---|
| 1159 | /*
|
|---|
| 1160 | * Create copy of the source address space area.
|
|---|
| 1161 | * The destination area is created with AS_AREA_ATTR_PARTIAL
|
|---|
| 1162 | * attribute set which prevents race condition with
|
|---|
| 1163 | * preliminary as_page_fault() calls.
|
|---|
| 1164 | * The flags of the source area are masked against dst_flags_mask
|
|---|
| 1165 | * to support sharing in less privileged mode.
|
|---|
| 1166 | */
|
|---|
| 1167 | as_area_t *dst_area = as_area_create(dst_as, dst_flags_mask,
|
|---|
| 1168 | src_size, AS_AREA_ATTR_PARTIAL, src_backend,
|
|---|
| 1169 | &src_backend_data, dst_base, bound);
|
|---|
| 1170 | if (!dst_area) {
|
|---|
| 1171 | /*
|
|---|
| 1172 | * Destination address space area could not be created.
|
|---|
| 1173 | */
|
|---|
| 1174 | sh_info_remove_reference(sh_info);
|
|---|
| 1175 |
|
|---|
| 1176 | return ENOMEM;
|
|---|
| 1177 | }
|
|---|
| 1178 |
|
|---|
| 1179 | /*
|
|---|
| 1180 | * Now the destination address space area has been
|
|---|
| 1181 | * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
|
|---|
| 1182 | * attribute and set the sh_info.
|
|---|
| 1183 | */
|
|---|
| 1184 | mutex_lock(&dst_as->lock);
|
|---|
| 1185 | mutex_lock(&dst_area->lock);
|
|---|
| 1186 | dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
|
|---|
| 1187 | dst_area->sh_info = sh_info;
|
|---|
| 1188 | mutex_unlock(&dst_area->lock);
|
|---|
| 1189 | mutex_unlock(&dst_as->lock);
|
|---|
| 1190 |
|
|---|
| 1191 | return 0;
|
|---|
| 1192 | }
|
|---|
| 1193 |
|
|---|
| 1194 | /** Check access mode for address space area.
|
|---|
| 1195 | *
|
|---|
| 1196 | * @param area Address space area.
|
|---|
| 1197 | * @param access Access mode.
|
|---|
| 1198 | *
|
|---|
| 1199 | * @return False if access violates area's permissions, true
|
|---|
| 1200 | * otherwise.
|
|---|
| 1201 | *
|
|---|
| 1202 | */
|
|---|
| 1203 | NO_TRACE bool as_area_check_access(as_area_t *area, pf_access_t access)
|
|---|
| 1204 | {
|
|---|
| 1205 | assert(mutex_locked(&area->lock));
|
|---|
| 1206 |
|
|---|
| 1207 | int flagmap[] = {
|
|---|
| 1208 | [PF_ACCESS_READ] = AS_AREA_READ,
|
|---|
| 1209 | [PF_ACCESS_WRITE] = AS_AREA_WRITE,
|
|---|
| 1210 | [PF_ACCESS_EXEC] = AS_AREA_EXEC
|
|---|
| 1211 | };
|
|---|
| 1212 |
|
|---|
| 1213 | if (!(area->flags & flagmap[access]))
|
|---|
| 1214 | return false;
|
|---|
| 1215 |
|
|---|
| 1216 | return true;
|
|---|
| 1217 | }
|
|---|
| 1218 |
|
|---|
| 1219 | /** Convert address space area flags to page flags.
|
|---|
| 1220 | *
|
|---|
| 1221 | * @param aflags Flags of some address space area.
|
|---|
| 1222 | *
|
|---|
| 1223 | * @return Flags to be passed to page_mapping_insert().
|
|---|
| 1224 | *
|
|---|
| 1225 | */
|
|---|
| 1226 | NO_TRACE static unsigned int area_flags_to_page_flags(unsigned int aflags)
|
|---|
| 1227 | {
|
|---|
| 1228 | unsigned int flags = PAGE_USER | PAGE_PRESENT;
|
|---|
| 1229 |
|
|---|
| 1230 | if (aflags & AS_AREA_READ)
|
|---|
| 1231 | flags |= PAGE_READ;
|
|---|
| 1232 |
|
|---|
| 1233 | if (aflags & AS_AREA_WRITE)
|
|---|
| 1234 | flags |= PAGE_WRITE;
|
|---|
| 1235 |
|
|---|
| 1236 | if (aflags & AS_AREA_EXEC)
|
|---|
| 1237 | flags |= PAGE_EXEC;
|
|---|
| 1238 |
|
|---|
| 1239 | if (aflags & AS_AREA_CACHEABLE)
|
|---|
| 1240 | flags |= PAGE_CACHEABLE;
|
|---|
| 1241 |
|
|---|
| 1242 | return flags;
|
|---|
| 1243 | }
|
|---|
| 1244 |
|
|---|
| 1245 | /** Change adress space area flags.
|
|---|
| 1246 | *
|
|---|
| 1247 | * The idea is to have the same data, but with a different access mode.
|
|---|
| 1248 | * This is needed e.g. for writing code into memory and then executing it.
|
|---|
| 1249 | * In order for this to work properly, this may copy the data
|
|---|
| 1250 | * into private anonymous memory (unless it's already there).
|
|---|
| 1251 | *
|
|---|
| 1252 | * @param as Address space.
|
|---|
| 1253 | * @param flags Flags of the area memory.
|
|---|
| 1254 | * @param address Address within the area to be changed.
|
|---|
| 1255 | *
|
|---|
| 1256 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 1257 | *
|
|---|
| 1258 | */
|
|---|
| 1259 | errno_t as_area_change_flags(as_t *as, unsigned int flags, uintptr_t address)
|
|---|
| 1260 | {
|
|---|
| 1261 | /* Flags for the new memory mapping */
|
|---|
| 1262 | unsigned int page_flags = area_flags_to_page_flags(flags);
|
|---|
| 1263 |
|
|---|
| 1264 | mutex_lock(&as->lock);
|
|---|
| 1265 |
|
|---|
| 1266 | as_area_t *area = find_area_and_lock(as, address);
|
|---|
| 1267 | if (!area) {
|
|---|
| 1268 | mutex_unlock(&as->lock);
|
|---|
| 1269 | return ENOENT;
|
|---|
| 1270 | }
|
|---|
| 1271 |
|
|---|
| 1272 | if (area->backend != &anon_backend) {
|
|---|
| 1273 | /* Copying non-anonymous memory not supported yet */
|
|---|
| 1274 | mutex_unlock(&area->lock);
|
|---|
| 1275 | mutex_unlock(&as->lock);
|
|---|
| 1276 | return ENOTSUP;
|
|---|
| 1277 | }
|
|---|
| 1278 |
|
|---|
| 1279 | mutex_lock(&area->sh_info->lock);
|
|---|
| 1280 | if (area->sh_info->shared) {
|
|---|
| 1281 | /* Copying shared areas not supported yet */
|
|---|
| 1282 | mutex_unlock(&area->sh_info->lock);
|
|---|
| 1283 | mutex_unlock(&area->lock);
|
|---|
| 1284 | mutex_unlock(&as->lock);
|
|---|
| 1285 | return ENOTSUP;
|
|---|
| 1286 | }
|
|---|
| 1287 | mutex_unlock(&area->sh_info->lock);
|
|---|
| 1288 |
|
|---|
| 1289 | /*
|
|---|
| 1290 | * Compute total number of used pages in the used_space B+tree
|
|---|
| 1291 | */
|
|---|
| 1292 | size_t used_pages = 0;
|
|---|
| 1293 |
|
|---|
| 1294 | list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
|
|---|
| 1295 | node) {
|
|---|
| 1296 | btree_key_t i;
|
|---|
| 1297 |
|
|---|
| 1298 | for (i = 0; i < node->keys; i++)
|
|---|
| 1299 | used_pages += (size_t) node->value[i];
|
|---|
| 1300 | }
|
|---|
| 1301 |
|
|---|
| 1302 | /* An array for storing frame numbers */
|
|---|
| 1303 | uintptr_t *old_frame = malloc(used_pages * sizeof(uintptr_t));
|
|---|
| 1304 | if (!old_frame) {
|
|---|
| 1305 | mutex_unlock(&area->lock);
|
|---|
| 1306 | mutex_unlock(&as->lock);
|
|---|
| 1307 | return ENOMEM;
|
|---|
| 1308 | }
|
|---|
| 1309 |
|
|---|
| 1310 | page_table_lock(as, false);
|
|---|
| 1311 |
|
|---|
| 1312 | /*
|
|---|
| 1313 | * Start TLB shootdown sequence.
|
|---|
| 1314 | */
|
|---|
| 1315 | ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base,
|
|---|
| 1316 | area->pages);
|
|---|
| 1317 |
|
|---|
| 1318 | /*
|
|---|
| 1319 | * Remove used pages from page tables and remember their frame
|
|---|
| 1320 | * numbers.
|
|---|
| 1321 | */
|
|---|
| 1322 | size_t frame_idx = 0;
|
|---|
| 1323 |
|
|---|
| 1324 | list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
|
|---|
| 1325 | node) {
|
|---|
| 1326 | btree_key_t i;
|
|---|
| 1327 |
|
|---|
| 1328 | for (i = 0; i < node->keys; i++) {
|
|---|
| 1329 | uintptr_t ptr = node->key[i];
|
|---|
| 1330 | size_t size;
|
|---|
| 1331 |
|
|---|
| 1332 | for (size = 0; size < (size_t) node->value[i]; size++) {
|
|---|
| 1333 | pte_t pte;
|
|---|
| 1334 | bool found = page_mapping_find(as,
|
|---|
| 1335 | ptr + P2SZ(size), false, &pte);
|
|---|
| 1336 |
|
|---|
| 1337 | assert(found);
|
|---|
| 1338 | assert(PTE_VALID(&pte));
|
|---|
| 1339 | assert(PTE_PRESENT(&pte));
|
|---|
| 1340 |
|
|---|
| 1341 | old_frame[frame_idx++] = PTE_GET_FRAME(&pte);
|
|---|
| 1342 |
|
|---|
| 1343 | /* Remove old mapping */
|
|---|
| 1344 | page_mapping_remove(as, ptr + P2SZ(size));
|
|---|
| 1345 | }
|
|---|
| 1346 | }
|
|---|
| 1347 | }
|
|---|
| 1348 |
|
|---|
| 1349 | /*
|
|---|
| 1350 | * Finish TLB shootdown sequence.
|
|---|
| 1351 | */
|
|---|
| 1352 |
|
|---|
| 1353 | tlb_invalidate_pages(as->asid, area->base, area->pages);
|
|---|
| 1354 |
|
|---|
| 1355 | /*
|
|---|
| 1356 | * Invalidate potential software translation caches
|
|---|
| 1357 | * (e.g. TSB on sparc64, PHT on ppc32).
|
|---|
| 1358 | */
|
|---|
| 1359 | as_invalidate_translation_cache(as, area->base, area->pages);
|
|---|
| 1360 | tlb_shootdown_finalize(ipl);
|
|---|
| 1361 |
|
|---|
| 1362 | page_table_unlock(as, false);
|
|---|
| 1363 |
|
|---|
| 1364 | /*
|
|---|
| 1365 | * Set the new flags.
|
|---|
| 1366 | */
|
|---|
| 1367 | area->flags = flags;
|
|---|
| 1368 |
|
|---|
| 1369 | /*
|
|---|
| 1370 | * Map pages back in with new flags. This step is kept separate
|
|---|
| 1371 | * so that the memory area could not be accesed with both the old and
|
|---|
| 1372 | * the new flags at once.
|
|---|
| 1373 | */
|
|---|
| 1374 | frame_idx = 0;
|
|---|
| 1375 |
|
|---|
| 1376 | list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
|
|---|
| 1377 | node) {
|
|---|
| 1378 | btree_key_t i;
|
|---|
| 1379 |
|
|---|
| 1380 | for (i = 0; i < node->keys; i++) {
|
|---|
| 1381 | uintptr_t ptr = node->key[i];
|
|---|
| 1382 | size_t size;
|
|---|
| 1383 |
|
|---|
| 1384 | for (size = 0; size < (size_t) node->value[i]; size++) {
|
|---|
| 1385 | page_table_lock(as, false);
|
|---|
| 1386 |
|
|---|
| 1387 | /* Insert the new mapping */
|
|---|
| 1388 | page_mapping_insert(as, ptr + P2SZ(size),
|
|---|
| 1389 | old_frame[frame_idx++], page_flags);
|
|---|
| 1390 |
|
|---|
| 1391 | page_table_unlock(as, false);
|
|---|
| 1392 | }
|
|---|
| 1393 | }
|
|---|
| 1394 | }
|
|---|
| 1395 |
|
|---|
| 1396 | free(old_frame);
|
|---|
| 1397 |
|
|---|
| 1398 | mutex_unlock(&area->lock);
|
|---|
| 1399 | mutex_unlock(&as->lock);
|
|---|
| 1400 |
|
|---|
| 1401 | return 0;
|
|---|
| 1402 | }
|
|---|
| 1403 |
|
|---|
| 1404 | /** Handle page fault within the current address space.
|
|---|
| 1405 | *
|
|---|
| 1406 | * This is the high-level page fault handler. It decides whether the page fault
|
|---|
| 1407 | * can be resolved by any backend and if so, it invokes the backend to resolve
|
|---|
| 1408 | * the page fault.
|
|---|
| 1409 | *
|
|---|
| 1410 | * Interrupts are assumed disabled.
|
|---|
| 1411 | *
|
|---|
| 1412 | * @param address Faulting address.
|
|---|
| 1413 | * @param access Access mode that caused the page fault (i.e.
|
|---|
| 1414 | * read/write/exec).
|
|---|
| 1415 | * @param istate Pointer to the interrupted state.
|
|---|
| 1416 | *
|
|---|
| 1417 | * @return AS_PF_FAULT on page fault.
|
|---|
| 1418 | * @return AS_PF_OK on success.
|
|---|
| 1419 | * @return AS_PF_DEFER if the fault was caused by copy_to_uspace()
|
|---|
| 1420 | * or copy_from_uspace().
|
|---|
| 1421 | *
|
|---|
| 1422 | */
|
|---|
| 1423 | int as_page_fault(uintptr_t address, pf_access_t access, istate_t *istate)
|
|---|
| 1424 | {
|
|---|
| 1425 | uintptr_t page = ALIGN_DOWN(address, PAGE_SIZE);
|
|---|
| 1426 | int rc = AS_PF_FAULT;
|
|---|
| 1427 |
|
|---|
| 1428 | if (!THREAD)
|
|---|
| 1429 | goto page_fault;
|
|---|
| 1430 |
|
|---|
| 1431 | if (!AS)
|
|---|
| 1432 | goto page_fault;
|
|---|
| 1433 |
|
|---|
| 1434 | mutex_lock(&AS->lock);
|
|---|
| 1435 | as_area_t *area = find_area_and_lock(AS, page);
|
|---|
| 1436 | if (!area) {
|
|---|
| 1437 | /*
|
|---|
| 1438 | * No area contained mapping for 'page'.
|
|---|
| 1439 | * Signal page fault to low-level handler.
|
|---|
| 1440 | */
|
|---|
| 1441 | mutex_unlock(&AS->lock);
|
|---|
| 1442 | goto page_fault;
|
|---|
| 1443 | }
|
|---|
| 1444 |
|
|---|
| 1445 | if (area->attributes & AS_AREA_ATTR_PARTIAL) {
|
|---|
| 1446 | /*
|
|---|
| 1447 | * The address space area is not fully initialized.
|
|---|
| 1448 | * Avoid possible race by returning error.
|
|---|
| 1449 | */
|
|---|
| 1450 | mutex_unlock(&area->lock);
|
|---|
| 1451 | mutex_unlock(&AS->lock);
|
|---|
| 1452 | goto page_fault;
|
|---|
| 1453 | }
|
|---|
| 1454 |
|
|---|
| 1455 | if ((!area->backend) || (!area->backend->page_fault)) {
|
|---|
| 1456 | /*
|
|---|
| 1457 | * The address space area is not backed by any backend
|
|---|
| 1458 | * or the backend cannot handle page faults.
|
|---|
| 1459 | */
|
|---|
| 1460 | mutex_unlock(&area->lock);
|
|---|
| 1461 | mutex_unlock(&AS->lock);
|
|---|
| 1462 | goto page_fault;
|
|---|
| 1463 | }
|
|---|
| 1464 |
|
|---|
| 1465 | page_table_lock(AS, false);
|
|---|
| 1466 |
|
|---|
| 1467 | /*
|
|---|
| 1468 | * To avoid race condition between two page faults on the same address,
|
|---|
| 1469 | * we need to make sure the mapping has not been already inserted.
|
|---|
| 1470 | */
|
|---|
| 1471 | pte_t pte;
|
|---|
| 1472 | bool found = page_mapping_find(AS, page, false, &pte);
|
|---|
| 1473 | if (found && PTE_PRESENT(&pte)) {
|
|---|
| 1474 | if (((access == PF_ACCESS_READ) && PTE_READABLE(&pte)) ||
|
|---|
| 1475 | (access == PF_ACCESS_WRITE && PTE_WRITABLE(&pte)) ||
|
|---|
| 1476 | (access == PF_ACCESS_EXEC && PTE_EXECUTABLE(&pte))) {
|
|---|
| 1477 | page_table_unlock(AS, false);
|
|---|
| 1478 | mutex_unlock(&area->lock);
|
|---|
| 1479 | mutex_unlock(&AS->lock);
|
|---|
| 1480 | return AS_PF_OK;
|
|---|
| 1481 | }
|
|---|
| 1482 | }
|
|---|
| 1483 |
|
|---|
| 1484 | /*
|
|---|
| 1485 | * Resort to the backend page fault handler.
|
|---|
| 1486 | */
|
|---|
| 1487 | rc = area->backend->page_fault(area, page, access);
|
|---|
| 1488 | if (rc != AS_PF_OK) {
|
|---|
| 1489 | page_table_unlock(AS, false);
|
|---|
| 1490 | mutex_unlock(&area->lock);
|
|---|
| 1491 | mutex_unlock(&AS->lock);
|
|---|
| 1492 | goto page_fault;
|
|---|
| 1493 | }
|
|---|
| 1494 |
|
|---|
| 1495 | page_table_unlock(AS, false);
|
|---|
| 1496 | mutex_unlock(&area->lock);
|
|---|
| 1497 | mutex_unlock(&AS->lock);
|
|---|
| 1498 | return AS_PF_OK;
|
|---|
| 1499 |
|
|---|
| 1500 | page_fault:
|
|---|
| 1501 | if (THREAD->in_copy_from_uspace) {
|
|---|
| 1502 | THREAD->in_copy_from_uspace = false;
|
|---|
| 1503 | istate_set_retaddr(istate,
|
|---|
| 1504 | (uintptr_t) &memcpy_from_uspace_failover_address);
|
|---|
| 1505 | } else if (THREAD->in_copy_to_uspace) {
|
|---|
| 1506 | THREAD->in_copy_to_uspace = false;
|
|---|
| 1507 | istate_set_retaddr(istate,
|
|---|
| 1508 | (uintptr_t) &memcpy_to_uspace_failover_address);
|
|---|
| 1509 | } else if (rc == AS_PF_SILENT) {
|
|---|
| 1510 | printf("Killing task %" PRIu64 " due to a "
|
|---|
| 1511 | "failed late reservation request.\n", TASK->taskid);
|
|---|
| 1512 | task_kill_self(true);
|
|---|
| 1513 | } else {
|
|---|
| 1514 | fault_if_from_uspace(istate, "Page fault: %p.", (void *) address);
|
|---|
| 1515 | panic_memtrap(istate, access, address, NULL);
|
|---|
| 1516 | }
|
|---|
| 1517 |
|
|---|
| 1518 | return AS_PF_DEFER;
|
|---|
| 1519 | }
|
|---|
| 1520 |
|
|---|
| 1521 | /** Switch address spaces.
|
|---|
| 1522 | *
|
|---|
| 1523 | * Note that this function cannot sleep as it is essentially a part of
|
|---|
| 1524 | * scheduling. Sleeping here would lead to deadlock on wakeup. Another
|
|---|
| 1525 | * thing which is forbidden in this context is locking the address space.
|
|---|
| 1526 | *
|
|---|
| 1527 | * When this function is entered, no spinlocks may be held.
|
|---|
| 1528 | *
|
|---|
| 1529 | * @param old Old address space or NULL.
|
|---|
| 1530 | * @param new New address space.
|
|---|
| 1531 | *
|
|---|
| 1532 | */
|
|---|
| 1533 | void as_switch(as_t *old_as, as_t *new_as)
|
|---|
| 1534 | {
|
|---|
| 1535 | DEADLOCK_PROBE_INIT(p_asidlock);
|
|---|
| 1536 | preemption_disable();
|
|---|
| 1537 |
|
|---|
| 1538 | retry:
|
|---|
| 1539 | (void) interrupts_disable();
|
|---|
| 1540 | if (!spinlock_trylock(&asidlock)) {
|
|---|
| 1541 | /*
|
|---|
| 1542 | * Avoid deadlock with TLB shootdown.
|
|---|
| 1543 | * We can enable interrupts here because
|
|---|
| 1544 | * preemption is disabled. We should not be
|
|---|
| 1545 | * holding any other lock.
|
|---|
| 1546 | */
|
|---|
| 1547 | (void) interrupts_enable();
|
|---|
| 1548 | DEADLOCK_PROBE(p_asidlock, DEADLOCK_THRESHOLD);
|
|---|
| 1549 | goto retry;
|
|---|
| 1550 | }
|
|---|
| 1551 | preemption_enable();
|
|---|
| 1552 |
|
|---|
| 1553 | /*
|
|---|
| 1554 | * First, take care of the old address space.
|
|---|
| 1555 | */
|
|---|
| 1556 | if (old_as) {
|
|---|
| 1557 | assert(old_as->cpu_refcount);
|
|---|
| 1558 |
|
|---|
| 1559 | if ((--old_as->cpu_refcount == 0) && (old_as != AS_KERNEL)) {
|
|---|
| 1560 | /*
|
|---|
| 1561 | * The old address space is no longer active on
|
|---|
| 1562 | * any processor. It can be appended to the
|
|---|
| 1563 | * list of inactive address spaces with assigned
|
|---|
| 1564 | * ASID.
|
|---|
| 1565 | */
|
|---|
| 1566 | assert(old_as->asid != ASID_INVALID);
|
|---|
| 1567 |
|
|---|
| 1568 | list_append(&old_as->inactive_as_with_asid_link,
|
|---|
| 1569 | &inactive_as_with_asid_list);
|
|---|
| 1570 | }
|
|---|
| 1571 |
|
|---|
| 1572 | /*
|
|---|
| 1573 | * Perform architecture-specific tasks when the address space
|
|---|
| 1574 | * is being removed from the CPU.
|
|---|
| 1575 | */
|
|---|
| 1576 | as_deinstall_arch(old_as);
|
|---|
| 1577 | }
|
|---|
| 1578 |
|
|---|
| 1579 | /*
|
|---|
| 1580 | * Second, prepare the new address space.
|
|---|
| 1581 | */
|
|---|
| 1582 | if ((new_as->cpu_refcount++ == 0) && (new_as != AS_KERNEL)) {
|
|---|
| 1583 | if (new_as->asid != ASID_INVALID)
|
|---|
| 1584 | list_remove(&new_as->inactive_as_with_asid_link);
|
|---|
| 1585 | else
|
|---|
| 1586 | new_as->asid = asid_get();
|
|---|
| 1587 | }
|
|---|
| 1588 |
|
|---|
| 1589 | #ifdef AS_PAGE_TABLE
|
|---|
| 1590 | SET_PTL0_ADDRESS(new_as->genarch.page_table);
|
|---|
| 1591 | #endif
|
|---|
| 1592 |
|
|---|
| 1593 | /*
|
|---|
| 1594 | * Perform architecture-specific steps.
|
|---|
| 1595 | * (e.g. write ASID to hardware register etc.)
|
|---|
| 1596 | */
|
|---|
| 1597 | as_install_arch(new_as);
|
|---|
| 1598 |
|
|---|
| 1599 | spinlock_unlock(&asidlock);
|
|---|
| 1600 |
|
|---|
| 1601 | AS = new_as;
|
|---|
| 1602 | }
|
|---|
| 1603 |
|
|---|
| 1604 | /** Compute flags for virtual address translation subsytem.
|
|---|
| 1605 | *
|
|---|
| 1606 | * @param area Address space area.
|
|---|
| 1607 | *
|
|---|
| 1608 | * @return Flags to be used in page_mapping_insert().
|
|---|
| 1609 | *
|
|---|
| 1610 | */
|
|---|
| 1611 | NO_TRACE unsigned int as_area_get_flags(as_area_t *area)
|
|---|
| 1612 | {
|
|---|
| 1613 | assert(mutex_locked(&area->lock));
|
|---|
| 1614 |
|
|---|
| 1615 | return area_flags_to_page_flags(area->flags);
|
|---|
| 1616 | }
|
|---|
| 1617 |
|
|---|
| 1618 | /** Create page table.
|
|---|
| 1619 | *
|
|---|
| 1620 | * Depending on architecture, create either address space private or global page
|
|---|
| 1621 | * table.
|
|---|
| 1622 | *
|
|---|
| 1623 | * @param flags Flags saying whether the page table is for the kernel
|
|---|
| 1624 | * address space.
|
|---|
| 1625 | *
|
|---|
| 1626 | * @return First entry of the page table.
|
|---|
| 1627 | *
|
|---|
| 1628 | */
|
|---|
| 1629 | NO_TRACE pte_t *page_table_create(unsigned int flags)
|
|---|
| 1630 | {
|
|---|
| 1631 | assert(as_operations);
|
|---|
| 1632 | assert(as_operations->page_table_create);
|
|---|
| 1633 |
|
|---|
| 1634 | return as_operations->page_table_create(flags);
|
|---|
| 1635 | }
|
|---|
| 1636 |
|
|---|
| 1637 | /** Destroy page table.
|
|---|
| 1638 | *
|
|---|
| 1639 | * Destroy page table in architecture specific way.
|
|---|
| 1640 | *
|
|---|
| 1641 | * @param page_table Physical address of PTL0.
|
|---|
| 1642 | *
|
|---|
| 1643 | */
|
|---|
| 1644 | NO_TRACE void page_table_destroy(pte_t *page_table)
|
|---|
| 1645 | {
|
|---|
| 1646 | assert(as_operations);
|
|---|
| 1647 | assert(as_operations->page_table_destroy);
|
|---|
| 1648 |
|
|---|
| 1649 | as_operations->page_table_destroy(page_table);
|
|---|
| 1650 | }
|
|---|
| 1651 |
|
|---|
| 1652 | /** Lock page table.
|
|---|
| 1653 | *
|
|---|
| 1654 | * This function should be called before any page_mapping_insert(),
|
|---|
| 1655 | * page_mapping_remove() and page_mapping_find().
|
|---|
| 1656 | *
|
|---|
| 1657 | * Locking order is such that address space areas must be locked
|
|---|
| 1658 | * prior to this call. Address space can be locked prior to this
|
|---|
| 1659 | * call in which case the lock argument is false.
|
|---|
| 1660 | *
|
|---|
| 1661 | * @param as Address space.
|
|---|
| 1662 | * @param lock If false, do not attempt to lock as->lock.
|
|---|
| 1663 | *
|
|---|
| 1664 | */
|
|---|
| 1665 | NO_TRACE void page_table_lock(as_t *as, bool lock)
|
|---|
| 1666 | {
|
|---|
| 1667 | assert(as_operations);
|
|---|
| 1668 | assert(as_operations->page_table_lock);
|
|---|
| 1669 |
|
|---|
| 1670 | as_operations->page_table_lock(as, lock);
|
|---|
| 1671 | }
|
|---|
| 1672 |
|
|---|
| 1673 | /** Unlock page table.
|
|---|
| 1674 | *
|
|---|
| 1675 | * @param as Address space.
|
|---|
| 1676 | * @param unlock If false, do not attempt to unlock as->lock.
|
|---|
| 1677 | *
|
|---|
| 1678 | */
|
|---|
| 1679 | NO_TRACE void page_table_unlock(as_t *as, bool unlock)
|
|---|
| 1680 | {
|
|---|
| 1681 | assert(as_operations);
|
|---|
| 1682 | assert(as_operations->page_table_unlock);
|
|---|
| 1683 |
|
|---|
| 1684 | as_operations->page_table_unlock(as, unlock);
|
|---|
| 1685 | }
|
|---|
| 1686 |
|
|---|
| 1687 | /** Test whether page tables are locked.
|
|---|
| 1688 | *
|
|---|
| 1689 | * @param as Address space where the page tables belong.
|
|---|
| 1690 | *
|
|---|
| 1691 | * @return True if the page tables belonging to the address soace
|
|---|
| 1692 | * are locked, otherwise false.
|
|---|
| 1693 | */
|
|---|
| 1694 | NO_TRACE bool page_table_locked(as_t *as)
|
|---|
| 1695 | {
|
|---|
| 1696 | assert(as_operations);
|
|---|
| 1697 | assert(as_operations->page_table_locked);
|
|---|
| 1698 |
|
|---|
| 1699 | return as_operations->page_table_locked(as);
|
|---|
| 1700 | }
|
|---|
| 1701 |
|
|---|
| 1702 | /** Return size of the address space area with given base.
|
|---|
| 1703 | *
|
|---|
| 1704 | * @param base Arbitrary address inside the address space area.
|
|---|
| 1705 | *
|
|---|
| 1706 | * @return Size of the address space area in bytes or zero if it
|
|---|
| 1707 | * does not exist.
|
|---|
| 1708 | *
|
|---|
| 1709 | */
|
|---|
| 1710 | size_t as_area_get_size(uintptr_t base)
|
|---|
| 1711 | {
|
|---|
| 1712 | size_t size;
|
|---|
| 1713 |
|
|---|
| 1714 | page_table_lock(AS, true);
|
|---|
| 1715 | as_area_t *src_area = find_area_and_lock(AS, base);
|
|---|
| 1716 |
|
|---|
| 1717 | if (src_area) {
|
|---|
| 1718 | size = P2SZ(src_area->pages);
|
|---|
| 1719 | mutex_unlock(&src_area->lock);
|
|---|
| 1720 | } else
|
|---|
| 1721 | size = 0;
|
|---|
| 1722 |
|
|---|
| 1723 | page_table_unlock(AS, true);
|
|---|
| 1724 | return size;
|
|---|
| 1725 | }
|
|---|
| 1726 |
|
|---|
| 1727 | /** Mark portion of address space area as used.
|
|---|
| 1728 | *
|
|---|
| 1729 | * The address space area must be already locked.
|
|---|
| 1730 | *
|
|---|
| 1731 | * @param area Address space area.
|
|---|
| 1732 | * @param page First page to be marked.
|
|---|
| 1733 | * @param count Number of page to be marked.
|
|---|
| 1734 | *
|
|---|
| 1735 | * @return False on failure or true on success.
|
|---|
| 1736 | *
|
|---|
| 1737 | */
|
|---|
| 1738 | bool used_space_insert(as_area_t *area, uintptr_t page, size_t count)
|
|---|
| 1739 | {
|
|---|
| 1740 | assert(mutex_locked(&area->lock));
|
|---|
| 1741 | assert(IS_ALIGNED(page, PAGE_SIZE));
|
|---|
| 1742 | assert(count);
|
|---|
| 1743 |
|
|---|
| 1744 | btree_node_t *leaf = NULL;
|
|---|
| 1745 | size_t pages = (size_t) btree_search(&area->used_space, page, &leaf);
|
|---|
| 1746 | if (pages) {
|
|---|
| 1747 | /*
|
|---|
| 1748 | * We hit the beginning of some used space.
|
|---|
| 1749 | */
|
|---|
| 1750 | return false;
|
|---|
| 1751 | }
|
|---|
| 1752 |
|
|---|
| 1753 | assert(leaf != NULL);
|
|---|
| 1754 |
|
|---|
| 1755 | if (!leaf->keys) {
|
|---|
| 1756 | btree_insert(&area->used_space, page, (void *) count, leaf);
|
|---|
| 1757 | goto success;
|
|---|
| 1758 | }
|
|---|
| 1759 |
|
|---|
| 1760 | btree_node_t *node = btree_leaf_node_left_neighbour(&area->used_space, leaf);
|
|---|
| 1761 | if (node) {
|
|---|
| 1762 | uintptr_t left_pg = node->key[node->keys - 1];
|
|---|
| 1763 | uintptr_t right_pg = leaf->key[0];
|
|---|
| 1764 | size_t left_cnt = (size_t) node->value[node->keys - 1];
|
|---|
| 1765 | size_t right_cnt = (size_t) leaf->value[0];
|
|---|
| 1766 |
|
|---|
| 1767 | /*
|
|---|
| 1768 | * Examine the possibility that the interval fits
|
|---|
| 1769 | * somewhere between the rightmost interval of
|
|---|
| 1770 | * the left neigbour and the first interval of the leaf.
|
|---|
| 1771 | */
|
|---|
| 1772 |
|
|---|
| 1773 | if (page >= right_pg) {
|
|---|
| 1774 | /* Do nothing. */
|
|---|
| 1775 | } else if (overlaps(page, P2SZ(count), left_pg,
|
|---|
| 1776 | P2SZ(left_cnt))) {
|
|---|
| 1777 | /* The interval intersects with the left interval. */
|
|---|
| 1778 | return false;
|
|---|
| 1779 | } else if (overlaps(page, P2SZ(count), right_pg,
|
|---|
| 1780 | P2SZ(right_cnt))) {
|
|---|
| 1781 | /* The interval intersects with the right interval. */
|
|---|
| 1782 | return false;
|
|---|
| 1783 | } else if ((page == left_pg + P2SZ(left_cnt)) &&
|
|---|
| 1784 | (page + P2SZ(count) == right_pg)) {
|
|---|
| 1785 | /*
|
|---|
| 1786 | * The interval can be added by merging the two already
|
|---|
| 1787 | * present intervals.
|
|---|
| 1788 | */
|
|---|
| 1789 | node->value[node->keys - 1] += count + right_cnt;
|
|---|
| 1790 | btree_remove(&area->used_space, right_pg, leaf);
|
|---|
| 1791 | goto success;
|
|---|
| 1792 | } else if (page == left_pg + P2SZ(left_cnt)) {
|
|---|
| 1793 | /*
|
|---|
| 1794 | * The interval can be added by simply growing the left
|
|---|
| 1795 | * interval.
|
|---|
| 1796 | */
|
|---|
| 1797 | node->value[node->keys - 1] += count;
|
|---|
| 1798 | goto success;
|
|---|
| 1799 | } else if (page + P2SZ(count) == right_pg) {
|
|---|
| 1800 | /*
|
|---|
| 1801 | * The interval can be addded by simply moving base of
|
|---|
| 1802 | * the right interval down and increasing its size
|
|---|
| 1803 | * accordingly.
|
|---|
| 1804 | */
|
|---|
| 1805 | leaf->value[0] += count;
|
|---|
| 1806 | leaf->key[0] = page;
|
|---|
| 1807 | goto success;
|
|---|
| 1808 | } else {
|
|---|
| 1809 | /*
|
|---|
| 1810 | * The interval is between both neigbouring intervals,
|
|---|
| 1811 | * but cannot be merged with any of them.
|
|---|
| 1812 | */
|
|---|
| 1813 | btree_insert(&area->used_space, page, (void *) count,
|
|---|
| 1814 | leaf);
|
|---|
| 1815 | goto success;
|
|---|
| 1816 | }
|
|---|
| 1817 | } else if (page < leaf->key[0]) {
|
|---|
| 1818 | uintptr_t right_pg = leaf->key[0];
|
|---|
| 1819 | size_t right_cnt = (size_t) leaf->value[0];
|
|---|
| 1820 |
|
|---|
| 1821 | /*
|
|---|
| 1822 | * Investigate the border case in which the left neighbour does
|
|---|
| 1823 | * not exist but the interval fits from the left.
|
|---|
| 1824 | */
|
|---|
| 1825 |
|
|---|
| 1826 | if (overlaps(page, P2SZ(count), right_pg, P2SZ(right_cnt))) {
|
|---|
| 1827 | /* The interval intersects with the right interval. */
|
|---|
| 1828 | return false;
|
|---|
| 1829 | } else if (page + P2SZ(count) == right_pg) {
|
|---|
| 1830 | /*
|
|---|
| 1831 | * The interval can be added by moving the base of the
|
|---|
| 1832 | * right interval down and increasing its size
|
|---|
| 1833 | * accordingly.
|
|---|
| 1834 | */
|
|---|
| 1835 | leaf->key[0] = page;
|
|---|
| 1836 | leaf->value[0] += count;
|
|---|
| 1837 | goto success;
|
|---|
| 1838 | } else {
|
|---|
| 1839 | /*
|
|---|
| 1840 | * The interval doesn't adjoin with the right interval.
|
|---|
| 1841 | * It must be added individually.
|
|---|
| 1842 | */
|
|---|
| 1843 | btree_insert(&area->used_space, page, (void *) count,
|
|---|
| 1844 | leaf);
|
|---|
| 1845 | goto success;
|
|---|
| 1846 | }
|
|---|
| 1847 | }
|
|---|
| 1848 |
|
|---|
| 1849 | node = btree_leaf_node_right_neighbour(&area->used_space, leaf);
|
|---|
| 1850 | if (node) {
|
|---|
| 1851 | uintptr_t left_pg = leaf->key[leaf->keys - 1];
|
|---|
| 1852 | uintptr_t right_pg = node->key[0];
|
|---|
| 1853 | size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
|
|---|
| 1854 | size_t right_cnt = (size_t) node->value[0];
|
|---|
| 1855 |
|
|---|
| 1856 | /*
|
|---|
| 1857 | * Examine the possibility that the interval fits
|
|---|
| 1858 | * somewhere between the leftmost interval of
|
|---|
| 1859 | * the right neigbour and the last interval of the leaf.
|
|---|
| 1860 | */
|
|---|
| 1861 |
|
|---|
| 1862 | if (page < left_pg) {
|
|---|
| 1863 | /* Do nothing. */
|
|---|
| 1864 | } else if (overlaps(page, P2SZ(count), left_pg,
|
|---|
| 1865 | P2SZ(left_cnt))) {
|
|---|
| 1866 | /* The interval intersects with the left interval. */
|
|---|
| 1867 | return false;
|
|---|
| 1868 | } else if (overlaps(page, P2SZ(count), right_pg,
|
|---|
| 1869 | P2SZ(right_cnt))) {
|
|---|
| 1870 | /* The interval intersects with the right interval. */
|
|---|
| 1871 | return false;
|
|---|
| 1872 | } else if ((page == left_pg + P2SZ(left_cnt)) &&
|
|---|
| 1873 | (page + P2SZ(count) == right_pg)) {
|
|---|
| 1874 | /*
|
|---|
| 1875 | * The interval can be added by merging the two already
|
|---|
| 1876 | * present intervals.
|
|---|
| 1877 | */
|
|---|
| 1878 | leaf->value[leaf->keys - 1] += count + right_cnt;
|
|---|
| 1879 | btree_remove(&area->used_space, right_pg, node);
|
|---|
| 1880 | goto success;
|
|---|
| 1881 | } else if (page == left_pg + P2SZ(left_cnt)) {
|
|---|
| 1882 | /*
|
|---|
| 1883 | * The interval can be added by simply growing the left
|
|---|
| 1884 | * interval.
|
|---|
| 1885 | */
|
|---|
| 1886 | leaf->value[leaf->keys - 1] += count;
|
|---|
| 1887 | goto success;
|
|---|
| 1888 | } else if (page + P2SZ(count) == right_pg) {
|
|---|
| 1889 | /*
|
|---|
| 1890 | * The interval can be addded by simply moving base of
|
|---|
| 1891 | * the right interval down and increasing its size
|
|---|
| 1892 | * accordingly.
|
|---|
| 1893 | */
|
|---|
| 1894 | node->value[0] += count;
|
|---|
| 1895 | node->key[0] = page;
|
|---|
| 1896 | goto success;
|
|---|
| 1897 | } else {
|
|---|
| 1898 | /*
|
|---|
| 1899 | * The interval is between both neigbouring intervals,
|
|---|
| 1900 | * but cannot be merged with any of them.
|
|---|
| 1901 | */
|
|---|
| 1902 | btree_insert(&area->used_space, page, (void *) count,
|
|---|
| 1903 | leaf);
|
|---|
| 1904 | goto success;
|
|---|
| 1905 | }
|
|---|
| 1906 | } else if (page >= leaf->key[leaf->keys - 1]) {
|
|---|
| 1907 | uintptr_t left_pg = leaf->key[leaf->keys - 1];
|
|---|
| 1908 | size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
|
|---|
| 1909 |
|
|---|
| 1910 | /*
|
|---|
| 1911 | * Investigate the border case in which the right neighbour
|
|---|
| 1912 | * does not exist but the interval fits from the right.
|
|---|
| 1913 | */
|
|---|
| 1914 |
|
|---|
| 1915 | if (overlaps(page, P2SZ(count), left_pg, P2SZ(left_cnt))) {
|
|---|
| 1916 | /* The interval intersects with the left interval. */
|
|---|
| 1917 | return false;
|
|---|
| 1918 | } else if (left_pg + P2SZ(left_cnt) == page) {
|
|---|
| 1919 | /*
|
|---|
| 1920 | * The interval can be added by growing the left
|
|---|
| 1921 | * interval.
|
|---|
| 1922 | */
|
|---|
| 1923 | leaf->value[leaf->keys - 1] += count;
|
|---|
| 1924 | goto success;
|
|---|
| 1925 | } else {
|
|---|
| 1926 | /*
|
|---|
| 1927 | * The interval doesn't adjoin with the left interval.
|
|---|
| 1928 | * It must be added individually.
|
|---|
| 1929 | */
|
|---|
| 1930 | btree_insert(&area->used_space, page, (void *) count,
|
|---|
| 1931 | leaf);
|
|---|
| 1932 | goto success;
|
|---|
| 1933 | }
|
|---|
| 1934 | }
|
|---|
| 1935 |
|
|---|
| 1936 | /*
|
|---|
| 1937 | * Note that if the algorithm made it thus far, the interval can fit
|
|---|
| 1938 | * only between two other intervals of the leaf. The two border cases
|
|---|
| 1939 | * were already resolved.
|
|---|
| 1940 | */
|
|---|
| 1941 | btree_key_t i;
|
|---|
| 1942 | for (i = 1; i < leaf->keys; i++) {
|
|---|
| 1943 | if (page < leaf->key[i]) {
|
|---|
| 1944 | uintptr_t left_pg = leaf->key[i - 1];
|
|---|
| 1945 | uintptr_t right_pg = leaf->key[i];
|
|---|
| 1946 | size_t left_cnt = (size_t) leaf->value[i - 1];
|
|---|
| 1947 | size_t right_cnt = (size_t) leaf->value[i];
|
|---|
| 1948 |
|
|---|
| 1949 | /*
|
|---|
| 1950 | * The interval fits between left_pg and right_pg.
|
|---|
| 1951 | */
|
|---|
| 1952 |
|
|---|
| 1953 | if (overlaps(page, P2SZ(count), left_pg,
|
|---|
| 1954 | P2SZ(left_cnt))) {
|
|---|
| 1955 | /*
|
|---|
| 1956 | * The interval intersects with the left
|
|---|
| 1957 | * interval.
|
|---|
| 1958 | */
|
|---|
| 1959 | return false;
|
|---|
| 1960 | } else if (overlaps(page, P2SZ(count), right_pg,
|
|---|
| 1961 | P2SZ(right_cnt))) {
|
|---|
| 1962 | /*
|
|---|
| 1963 | * The interval intersects with the right
|
|---|
| 1964 | * interval.
|
|---|
| 1965 | */
|
|---|
| 1966 | return false;
|
|---|
| 1967 | } else if ((page == left_pg + P2SZ(left_cnt)) &&
|
|---|
| 1968 | (page + P2SZ(count) == right_pg)) {
|
|---|
| 1969 | /*
|
|---|
| 1970 | * The interval can be added by merging the two
|
|---|
| 1971 | * already present intervals.
|
|---|
| 1972 | */
|
|---|
| 1973 | leaf->value[i - 1] += count + right_cnt;
|
|---|
| 1974 | btree_remove(&area->used_space, right_pg, leaf);
|
|---|
| 1975 | goto success;
|
|---|
| 1976 | } else if (page == left_pg + P2SZ(left_cnt)) {
|
|---|
| 1977 | /*
|
|---|
| 1978 | * The interval can be added by simply growing
|
|---|
| 1979 | * the left interval.
|
|---|
| 1980 | */
|
|---|
| 1981 | leaf->value[i - 1] += count;
|
|---|
| 1982 | goto success;
|
|---|
| 1983 | } else if (page + P2SZ(count) == right_pg) {
|
|---|
| 1984 | /*
|
|---|
| 1985 | * The interval can be addded by simply moving
|
|---|
| 1986 | * base of the right interval down and
|
|---|
| 1987 | * increasing its size accordingly.
|
|---|
| 1988 | */
|
|---|
| 1989 | leaf->value[i] += count;
|
|---|
| 1990 | leaf->key[i] = page;
|
|---|
| 1991 | goto success;
|
|---|
| 1992 | } else {
|
|---|
| 1993 | /*
|
|---|
| 1994 | * The interval is between both neigbouring
|
|---|
| 1995 | * intervals, but cannot be merged with any of
|
|---|
| 1996 | * them.
|
|---|
| 1997 | */
|
|---|
| 1998 | btree_insert(&area->used_space, page,
|
|---|
| 1999 | (void *) count, leaf);
|
|---|
| 2000 | goto success;
|
|---|
| 2001 | }
|
|---|
| 2002 | }
|
|---|
| 2003 | }
|
|---|
| 2004 |
|
|---|
| 2005 | panic("Inconsistency detected while adding %zu pages of used "
|
|---|
| 2006 | "space at %p.", count, (void *) page);
|
|---|
| 2007 |
|
|---|
| 2008 | success:
|
|---|
| 2009 | area->resident += count;
|
|---|
| 2010 | return true;
|
|---|
| 2011 | }
|
|---|
| 2012 |
|
|---|
| 2013 | /** Mark portion of address space area as unused.
|
|---|
| 2014 | *
|
|---|
| 2015 | * The address space area must be already locked.
|
|---|
| 2016 | *
|
|---|
| 2017 | * @param area Address space area.
|
|---|
| 2018 | * @param page First page to be marked.
|
|---|
| 2019 | * @param count Number of page to be marked.
|
|---|
| 2020 | *
|
|---|
| 2021 | * @return False on failure or true on success.
|
|---|
| 2022 | *
|
|---|
| 2023 | */
|
|---|
| 2024 | bool used_space_remove(as_area_t *area, uintptr_t page, size_t count)
|
|---|
| 2025 | {
|
|---|
| 2026 | assert(mutex_locked(&area->lock));
|
|---|
| 2027 | assert(IS_ALIGNED(page, PAGE_SIZE));
|
|---|
| 2028 | assert(count);
|
|---|
| 2029 |
|
|---|
| 2030 | btree_node_t *leaf;
|
|---|
| 2031 | size_t pages = (size_t) btree_search(&area->used_space, page, &leaf);
|
|---|
| 2032 | if (pages) {
|
|---|
| 2033 | /*
|
|---|
| 2034 | * We are lucky, page is the beginning of some interval.
|
|---|
| 2035 | */
|
|---|
| 2036 | if (count > pages) {
|
|---|
| 2037 | return false;
|
|---|
| 2038 | } else if (count == pages) {
|
|---|
| 2039 | btree_remove(&area->used_space, page, leaf);
|
|---|
| 2040 | goto success;
|
|---|
| 2041 | } else {
|
|---|
| 2042 | /*
|
|---|
| 2043 | * Find the respective interval.
|
|---|
| 2044 | * Decrease its size and relocate its start address.
|
|---|
| 2045 | */
|
|---|
| 2046 | btree_key_t i;
|
|---|
| 2047 | for (i = 0; i < leaf->keys; i++) {
|
|---|
| 2048 | if (leaf->key[i] == page) {
|
|---|
| 2049 | leaf->key[i] += P2SZ(count);
|
|---|
| 2050 | leaf->value[i] -= count;
|
|---|
| 2051 | goto success;
|
|---|
| 2052 | }
|
|---|
| 2053 | }
|
|---|
| 2054 |
|
|---|
| 2055 | goto error;
|
|---|
| 2056 | }
|
|---|
| 2057 | }
|
|---|
| 2058 |
|
|---|
| 2059 | btree_node_t *node = btree_leaf_node_left_neighbour(&area->used_space,
|
|---|
| 2060 | leaf);
|
|---|
| 2061 | if ((node) && (page < leaf->key[0])) {
|
|---|
| 2062 | uintptr_t left_pg = node->key[node->keys - 1];
|
|---|
| 2063 | size_t left_cnt = (size_t) node->value[node->keys - 1];
|
|---|
| 2064 |
|
|---|
| 2065 | if (overlaps(left_pg, P2SZ(left_cnt), page, P2SZ(count))) {
|
|---|
| 2066 | if (page + P2SZ(count) == left_pg + P2SZ(left_cnt)) {
|
|---|
| 2067 | /*
|
|---|
| 2068 | * The interval is contained in the rightmost
|
|---|
| 2069 | * interval of the left neighbour and can be
|
|---|
| 2070 | * removed by updating the size of the bigger
|
|---|
| 2071 | * interval.
|
|---|
| 2072 | */
|
|---|
| 2073 | node->value[node->keys - 1] -= count;
|
|---|
| 2074 | goto success;
|
|---|
| 2075 | } else if (page + P2SZ(count) <
|
|---|
| 2076 | left_pg + P2SZ(left_cnt)) {
|
|---|
| 2077 | size_t new_cnt;
|
|---|
| 2078 |
|
|---|
| 2079 | /*
|
|---|
| 2080 | * The interval is contained in the rightmost
|
|---|
| 2081 | * interval of the left neighbour but its
|
|---|
| 2082 | * removal requires both updating the size of
|
|---|
| 2083 | * the original interval and also inserting a
|
|---|
| 2084 | * new interval.
|
|---|
| 2085 | */
|
|---|
| 2086 | new_cnt = ((left_pg + P2SZ(left_cnt)) -
|
|---|
| 2087 | (page + P2SZ(count))) >> PAGE_WIDTH;
|
|---|
| 2088 | node->value[node->keys - 1] -= count + new_cnt;
|
|---|
| 2089 | btree_insert(&area->used_space, page +
|
|---|
| 2090 | P2SZ(count), (void *) new_cnt, leaf);
|
|---|
| 2091 | goto success;
|
|---|
| 2092 | }
|
|---|
| 2093 | }
|
|---|
| 2094 |
|
|---|
| 2095 | return false;
|
|---|
| 2096 | } else if (page < leaf->key[0])
|
|---|
| 2097 | return false;
|
|---|
| 2098 |
|
|---|
| 2099 | if (page > leaf->key[leaf->keys - 1]) {
|
|---|
| 2100 | uintptr_t left_pg = leaf->key[leaf->keys - 1];
|
|---|
| 2101 | size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
|
|---|
| 2102 |
|
|---|
| 2103 | if (overlaps(left_pg, P2SZ(left_cnt), page, P2SZ(count))) {
|
|---|
| 2104 | if (page + P2SZ(count) == left_pg + P2SZ(left_cnt)) {
|
|---|
| 2105 | /*
|
|---|
| 2106 | * The interval is contained in the rightmost
|
|---|
| 2107 | * interval of the leaf and can be removed by
|
|---|
| 2108 | * updating the size of the bigger interval.
|
|---|
| 2109 | */
|
|---|
| 2110 | leaf->value[leaf->keys - 1] -= count;
|
|---|
| 2111 | goto success;
|
|---|
| 2112 | } else if (page + P2SZ(count) < left_pg +
|
|---|
| 2113 | P2SZ(left_cnt)) {
|
|---|
| 2114 | size_t new_cnt;
|
|---|
| 2115 |
|
|---|
| 2116 | /*
|
|---|
| 2117 | * The interval is contained in the rightmost
|
|---|
| 2118 | * interval of the leaf but its removal
|
|---|
| 2119 | * requires both updating the size of the
|
|---|
| 2120 | * original interval and also inserting a new
|
|---|
| 2121 | * interval.
|
|---|
| 2122 | */
|
|---|
| 2123 | new_cnt = ((left_pg + P2SZ(left_cnt)) -
|
|---|
| 2124 | (page + P2SZ(count))) >> PAGE_WIDTH;
|
|---|
| 2125 | leaf->value[leaf->keys - 1] -= count + new_cnt;
|
|---|
| 2126 | btree_insert(&area->used_space, page +
|
|---|
| 2127 | P2SZ(count), (void *) new_cnt, leaf);
|
|---|
| 2128 | goto success;
|
|---|
| 2129 | }
|
|---|
| 2130 | }
|
|---|
| 2131 |
|
|---|
| 2132 | return false;
|
|---|
| 2133 | }
|
|---|
| 2134 |
|
|---|
| 2135 | /*
|
|---|
| 2136 | * The border cases have been already resolved.
|
|---|
| 2137 | * Now the interval can be only between intervals of the leaf.
|
|---|
| 2138 | */
|
|---|
| 2139 | btree_key_t i;
|
|---|
| 2140 | for (i = 1; i < leaf->keys - 1; i++) {
|
|---|
| 2141 | if (page < leaf->key[i]) {
|
|---|
| 2142 | uintptr_t left_pg = leaf->key[i - 1];
|
|---|
| 2143 | size_t left_cnt = (size_t) leaf->value[i - 1];
|
|---|
| 2144 |
|
|---|
| 2145 | /*
|
|---|
| 2146 | * Now the interval is between intervals corresponding
|
|---|
| 2147 | * to (i - 1) and i.
|
|---|
| 2148 | */
|
|---|
| 2149 | if (overlaps(left_pg, P2SZ(left_cnt), page,
|
|---|
| 2150 | P2SZ(count))) {
|
|---|
| 2151 | if (page + P2SZ(count) ==
|
|---|
| 2152 | left_pg + P2SZ(left_cnt)) {
|
|---|
| 2153 | /*
|
|---|
| 2154 | * The interval is contained in the
|
|---|
| 2155 | * interval (i - 1) of the leaf and can
|
|---|
| 2156 | * be removed by updating the size of
|
|---|
| 2157 | * the bigger interval.
|
|---|
| 2158 | */
|
|---|
| 2159 | leaf->value[i - 1] -= count;
|
|---|
| 2160 | goto success;
|
|---|
| 2161 | } else if (page + P2SZ(count) <
|
|---|
| 2162 | left_pg + P2SZ(left_cnt)) {
|
|---|
| 2163 | size_t new_cnt;
|
|---|
| 2164 |
|
|---|
| 2165 | /*
|
|---|
| 2166 | * The interval is contained in the
|
|---|
| 2167 | * interval (i - 1) of the leaf but its
|
|---|
| 2168 | * removal requires both updating the
|
|---|
| 2169 | * size of the original interval and
|
|---|
| 2170 | * also inserting a new interval.
|
|---|
| 2171 | */
|
|---|
| 2172 | new_cnt = ((left_pg + P2SZ(left_cnt)) -
|
|---|
| 2173 | (page + P2SZ(count))) >>
|
|---|
| 2174 | PAGE_WIDTH;
|
|---|
| 2175 | leaf->value[i - 1] -= count + new_cnt;
|
|---|
| 2176 | btree_insert(&area->used_space, page +
|
|---|
| 2177 | P2SZ(count), (void *) new_cnt,
|
|---|
| 2178 | leaf);
|
|---|
| 2179 | goto success;
|
|---|
| 2180 | }
|
|---|
| 2181 | }
|
|---|
| 2182 |
|
|---|
| 2183 | return false;
|
|---|
| 2184 | }
|
|---|
| 2185 | }
|
|---|
| 2186 |
|
|---|
| 2187 | error:
|
|---|
| 2188 | panic("Inconsistency detected while removing %zu pages of used "
|
|---|
| 2189 | "space from %p.", count, (void *) page);
|
|---|
| 2190 |
|
|---|
| 2191 | success:
|
|---|
| 2192 | area->resident -= count;
|
|---|
| 2193 | return true;
|
|---|
| 2194 | }
|
|---|
| 2195 |
|
|---|
| 2196 | /*
|
|---|
| 2197 | * Address space related syscalls.
|
|---|
| 2198 | */
|
|---|
| 2199 |
|
|---|
| 2200 | sysarg_t sys_as_area_create(uintptr_t base, size_t size, unsigned int flags,
|
|---|
| 2201 | uintptr_t bound, as_area_pager_info_t *pager_info)
|
|---|
| 2202 | {
|
|---|
| 2203 | uintptr_t virt = base;
|
|---|
| 2204 | mem_backend_t *backend;
|
|---|
| 2205 | mem_backend_data_t backend_data;
|
|---|
| 2206 |
|
|---|
| 2207 | if (pager_info == AS_AREA_UNPAGED)
|
|---|
| 2208 | backend = &anon_backend;
|
|---|
| 2209 | else {
|
|---|
| 2210 | backend = &user_backend;
|
|---|
| 2211 | if (copy_from_uspace(&backend_data.pager_info, pager_info,
|
|---|
| 2212 | sizeof(as_area_pager_info_t)) != EOK) {
|
|---|
| 2213 | return (sysarg_t) AS_MAP_FAILED;
|
|---|
| 2214 | }
|
|---|
| 2215 | }
|
|---|
| 2216 | as_area_t *area = as_area_create(AS, flags, size,
|
|---|
| 2217 | AS_AREA_ATTR_NONE, backend, &backend_data, &virt, bound);
|
|---|
| 2218 | if (area == NULL)
|
|---|
| 2219 | return (sysarg_t) AS_MAP_FAILED;
|
|---|
| 2220 |
|
|---|
| 2221 | return (sysarg_t) virt;
|
|---|
| 2222 | }
|
|---|
| 2223 |
|
|---|
| 2224 | sys_errno_t sys_as_area_resize(uintptr_t address, size_t size, unsigned int flags)
|
|---|
| 2225 | {
|
|---|
| 2226 | return (sys_errno_t) as_area_resize(AS, address, size, 0);
|
|---|
| 2227 | }
|
|---|
| 2228 |
|
|---|
| 2229 | sys_errno_t sys_as_area_change_flags(uintptr_t address, unsigned int flags)
|
|---|
| 2230 | {
|
|---|
| 2231 | return (sys_errno_t) as_area_change_flags(AS, flags, address);
|
|---|
| 2232 | }
|
|---|
| 2233 |
|
|---|
| 2234 | sys_errno_t sys_as_area_destroy(uintptr_t address)
|
|---|
| 2235 | {
|
|---|
| 2236 | return (sys_errno_t) as_area_destroy(AS, address);
|
|---|
| 2237 | }
|
|---|
| 2238 |
|
|---|
| 2239 | /** Get list of adress space areas.
|
|---|
| 2240 | *
|
|---|
| 2241 | * @param as Address space.
|
|---|
| 2242 | * @param obuf Place to save pointer to returned buffer.
|
|---|
| 2243 | * @param osize Place to save size of returned buffer.
|
|---|
| 2244 | *
|
|---|
| 2245 | */
|
|---|
| 2246 | void as_get_area_info(as_t *as, as_area_info_t **obuf, size_t *osize)
|
|---|
| 2247 | {
|
|---|
| 2248 | mutex_lock(&as->lock);
|
|---|
| 2249 |
|
|---|
| 2250 | /* First pass, count number of areas. */
|
|---|
| 2251 |
|
|---|
| 2252 | size_t area_cnt = 0;
|
|---|
| 2253 |
|
|---|
| 2254 | list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t,
|
|---|
| 2255 | node) {
|
|---|
| 2256 | area_cnt += node->keys;
|
|---|
| 2257 | }
|
|---|
| 2258 |
|
|---|
| 2259 | size_t isize = area_cnt * sizeof(as_area_info_t);
|
|---|
| 2260 | as_area_info_t *info = nfmalloc(isize);
|
|---|
| 2261 |
|
|---|
| 2262 | /* Second pass, record data. */
|
|---|
| 2263 |
|
|---|
| 2264 | size_t area_idx = 0;
|
|---|
| 2265 |
|
|---|
| 2266 | list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t,
|
|---|
| 2267 | node) {
|
|---|
| 2268 | btree_key_t i;
|
|---|
| 2269 |
|
|---|
| 2270 | for (i = 0; i < node->keys; i++) {
|
|---|
| 2271 | as_area_t *area = node->value[i];
|
|---|
| 2272 |
|
|---|
| 2273 | assert(area_idx < area_cnt);
|
|---|
| 2274 | mutex_lock(&area->lock);
|
|---|
| 2275 |
|
|---|
| 2276 | info[area_idx].start_addr = area->base;
|
|---|
| 2277 | info[area_idx].size = P2SZ(area->pages);
|
|---|
| 2278 | info[area_idx].flags = area->flags;
|
|---|
| 2279 | ++area_idx;
|
|---|
| 2280 |
|
|---|
| 2281 | mutex_unlock(&area->lock);
|
|---|
| 2282 | }
|
|---|
| 2283 | }
|
|---|
| 2284 |
|
|---|
| 2285 | mutex_unlock(&as->lock);
|
|---|
| 2286 |
|
|---|
| 2287 | *obuf = info;
|
|---|
| 2288 | *osize = isize;
|
|---|
| 2289 | }
|
|---|
| 2290 |
|
|---|
| 2291 | /** Print out information about address space.
|
|---|
| 2292 | *
|
|---|
| 2293 | * @param as Address space.
|
|---|
| 2294 | *
|
|---|
| 2295 | */
|
|---|
| 2296 | void as_print(as_t *as)
|
|---|
| 2297 | {
|
|---|
| 2298 | mutex_lock(&as->lock);
|
|---|
| 2299 |
|
|---|
| 2300 | /* Print out info about address space areas */
|
|---|
| 2301 | list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t,
|
|---|
| 2302 | node) {
|
|---|
| 2303 | btree_key_t i;
|
|---|
| 2304 |
|
|---|
| 2305 | for (i = 0; i < node->keys; i++) {
|
|---|
| 2306 | as_area_t *area = node->value[i];
|
|---|
| 2307 |
|
|---|
| 2308 | mutex_lock(&area->lock);
|
|---|
| 2309 | printf("as_area: %p, base=%p, pages=%zu"
|
|---|
| 2310 | " (%p - %p)\n", area, (void *) area->base,
|
|---|
| 2311 | area->pages, (void *) area->base,
|
|---|
| 2312 | (void *) (area->base + P2SZ(area->pages)));
|
|---|
| 2313 | mutex_unlock(&area->lock);
|
|---|
| 2314 | }
|
|---|
| 2315 | }
|
|---|
| 2316 |
|
|---|
| 2317 | mutex_unlock(&as->lock);
|
|---|
| 2318 | }
|
|---|
| 2319 |
|
|---|
| 2320 | /** @}
|
|---|
| 2321 | */
|
|---|