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