[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 | */
|
---|
[b6f3e7e] | 304 | if (overlaps(addr, P2SZ(count), (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 |
|
---|
[b6f3e7e] | 331 | if (overlaps(addr, P2SZ(count), area->base,
|
---|
| 332 | P2SZ(area->pages))) {
|
---|
[0b37882] | 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 |
|
---|
[b6f3e7e] | 348 | if (overlaps(addr, P2SZ(count), area->base,
|
---|
| 349 | P2SZ(area->pages))) {
|
---|
[0b37882] | 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 |
|
---|
[b6f3e7e] | 368 | if (overlaps(addr, P2SZ(count), area->base,
|
---|
| 369 | P2SZ(area->pages))) {
|
---|
[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) {
|
---|
[b6f3e7e] | 382 | return !overlaps(addr, P2SZ(count), KERNEL_ADDRESS_SPACE_START,
|
---|
[e3ee9b9] | 383 | KERNEL_ADDRESS_SPACE_END - KERNEL_ADDRESS_SPACE_START);
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | return true;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[20d50a1] | 389 | /** Create address space area of common attributes.
|
---|
| 390 | *
|
---|
| 391 | * The created address space area is added to the target address space.
|
---|
| 392 | *
|
---|
[da1bafb] | 393 | * @param as Target address space.
|
---|
| 394 | * @param flags Flags of the area memory.
|
---|
| 395 | * @param size Size of area.
|
---|
| 396 | * @param base Base address of area.
|
---|
| 397 | * @param attrs Attributes of the area.
|
---|
| 398 | * @param backend Address space area backend. NULL if no backend is used.
|
---|
| 399 | * @param backend_data NULL or a pointer to an array holding two void *.
|
---|
| 400 | *
|
---|
| 401 | * @return Address space area on success or NULL on failure.
|
---|
[20d50a1] | 402 | *
|
---|
| 403 | */
|
---|
[da1bafb] | 404 | as_area_t *as_area_create(as_t *as, unsigned int flags, size_t size,
|
---|
| 405 | uintptr_t base, unsigned int attrs, mem_backend_t *backend,
|
---|
| 406 | mem_backend_data_t *backend_data)
|
---|
[20d50a1] | 407 | {
|
---|
[0b37882] | 408 | if ((base % PAGE_SIZE) != 0)
|
---|
[37e7d2b9] | 409 | return NULL;
|
---|
[da1bafb] | 410 |
|
---|
[0b37882] | 411 | if (size == 0)
|
---|
[dbbeb26] | 412 | return NULL;
|
---|
[da1bafb] | 413 |
|
---|
[0b37882] | 414 | size_t pages = SIZE2FRAMES(size);
|
---|
| 415 |
|
---|
[37e7d2b9] | 416 | /* Writeable executable areas are not supported. */
|
---|
| 417 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
|
---|
| 418 | return NULL;
|
---|
[20d50a1] | 419 |
|
---|
[1068f6a] | 420 | mutex_lock(&as->lock);
|
---|
[20d50a1] | 421 |
|
---|
[0b37882] | 422 | if (!check_area_conflicts(as, base, pages, NULL)) {
|
---|
[1068f6a] | 423 | mutex_unlock(&as->lock);
|
---|
[37e7d2b9] | 424 | return NULL;
|
---|
| 425 | }
|
---|
[20d50a1] | 426 |
|
---|
[da1bafb] | 427 | as_area_t *area = (as_area_t *) malloc(sizeof(as_area_t), 0);
|
---|
| 428 |
|
---|
| 429 | mutex_initialize(&area->lock, MUTEX_PASSIVE);
|
---|
| 430 |
|
---|
| 431 | area->as = as;
|
---|
| 432 | area->flags = flags;
|
---|
| 433 | area->attributes = attrs;
|
---|
[0b37882] | 434 | area->pages = pages;
|
---|
[fc47885] | 435 | area->resident = 0;
|
---|
[da1bafb] | 436 | area->base = base;
|
---|
| 437 | area->sh_info = NULL;
|
---|
| 438 | area->backend = backend;
|
---|
| 439 |
|
---|
[0ee077ee] | 440 | if (backend_data)
|
---|
[da1bafb] | 441 | area->backend_data = *backend_data;
|
---|
[0ee077ee] | 442 | else
|
---|
[da1bafb] | 443 | memsetb(&area->backend_data, sizeof(area->backend_data), 0);
|
---|
| 444 |
|
---|
[e394b736] | 445 | if (area->backend && area->backend->create) {
|
---|
| 446 | if (!area->backend->create(area)) {
|
---|
| 447 | free(area);
|
---|
| 448 | mutex_unlock(&as->lock);
|
---|
| 449 | return NULL;
|
---|
| 450 | }
|
---|
| 451 | }
|
---|
| 452 |
|
---|
[da1bafb] | 453 | btree_create(&area->used_space);
|
---|
| 454 | btree_insert(&as->as_area_btree, base, (void *) area, NULL);
|
---|
[bb68433] | 455 |
|
---|
[1068f6a] | 456 | mutex_unlock(&as->lock);
|
---|
[da1bafb] | 457 |
|
---|
| 458 | return area;
|
---|
[20d50a1] | 459 | }
|
---|
| 460 |
|
---|
[e3ee9b9] | 461 | /** Find address space area and lock it.
|
---|
| 462 | *
|
---|
| 463 | * @param as Address space.
|
---|
| 464 | * @param va Virtual address.
|
---|
| 465 | *
|
---|
| 466 | * @return Locked address space area containing va on success or
|
---|
| 467 | * NULL on failure.
|
---|
| 468 | *
|
---|
| 469 | */
|
---|
[7a0359b] | 470 | NO_TRACE static as_area_t *find_area_and_lock(as_t *as, uintptr_t va)
|
---|
[e3ee9b9] | 471 | {
|
---|
| 472 | ASSERT(mutex_locked(&as->lock));
|
---|
| 473 |
|
---|
| 474 | btree_node_t *leaf;
|
---|
[b6f3e7e] | 475 | as_area_t *area = (as_area_t *) btree_search(&as->as_area_btree, va,
|
---|
| 476 | &leaf);
|
---|
[e3ee9b9] | 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 | /*
|
---|
[326bf65] | 484 | * Search the leaf node and the rightmost record of its left neighbour
|
---|
[e3ee9b9] | 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);
|
---|
[326bf65] | 496 |
|
---|
[b6f3e7e] | 497 | if ((area->base <= va) &&
|
---|
| 498 | (va <= area->base + (P2SZ(area->pages) - 1)))
|
---|
[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 | */
|
---|
[b6f3e7e] | 508 | btree_node_t *lnode = btree_leaf_node_left_neighbour(&as->as_area_btree,
|
---|
| 509 | leaf);
|
---|
[e3ee9b9] | 510 | if (lnode) {
|
---|
| 511 | area = (as_area_t *) lnode->value[lnode->keys - 1];
|
---|
| 512 |
|
---|
| 513 | mutex_lock(&area->lock);
|
---|
| 514 |
|
---|
[b6f3e7e] | 515 | if (va <= area->base + (P2SZ(area->pages) - 1))
|
---|
[e3ee9b9] | 516 | return area;
|
---|
| 517 |
|
---|
| 518 | mutex_unlock(&area->lock);
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | return NULL;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
[df0103f7] | 524 | /** Find address space area and change it.
|
---|
| 525 | *
|
---|
[da1bafb] | 526 | * @param as Address space.
|
---|
| 527 | * @param address Virtual address belonging to the area to be changed.
|
---|
| 528 | * Must be page-aligned.
|
---|
| 529 | * @param size New size of the virtual memory block starting at
|
---|
| 530 | * address.
|
---|
| 531 | * @param flags Flags influencing the remap operation. Currently unused.
|
---|
| 532 | *
|
---|
| 533 | * @return Zero on success or a value from @ref errno.h otherwise.
|
---|
[df0103f7] | 534 | *
|
---|
[da1bafb] | 535 | */
|
---|
| 536 | int as_area_resize(as_t *as, uintptr_t address, size_t size, unsigned int flags)
|
---|
[df0103f7] | 537 | {
|
---|
[1068f6a] | 538 | mutex_lock(&as->lock);
|
---|
[df0103f7] | 539 |
|
---|
| 540 | /*
|
---|
| 541 | * Locate the area.
|
---|
| 542 | */
|
---|
[da1bafb] | 543 | as_area_t *area = find_area_and_lock(as, address);
|
---|
[df0103f7] | 544 | if (!area) {
|
---|
[1068f6a] | 545 | mutex_unlock(&as->lock);
|
---|
[7242a78e] | 546 | return ENOENT;
|
---|
[df0103f7] | 547 | }
|
---|
[da1bafb] | 548 |
|
---|
[0ee077ee] | 549 | if (area->backend == &phys_backend) {
|
---|
[df0103f7] | 550 | /*
|
---|
| 551 | * Remapping of address space areas associated
|
---|
| 552 | * with memory mapped devices is not supported.
|
---|
| 553 | */
|
---|
[1068f6a] | 554 | mutex_unlock(&area->lock);
|
---|
| 555 | mutex_unlock(&as->lock);
|
---|
[7242a78e] | 556 | return ENOTSUP;
|
---|
[df0103f7] | 557 | }
|
---|
[da1bafb] | 558 |
|
---|
[8182031] | 559 | if (area->sh_info) {
|
---|
| 560 | /*
|
---|
[da1bafb] | 561 | * Remapping of shared address space areas
|
---|
[8182031] | 562 | * is not supported.
|
---|
| 563 | */
|
---|
| 564 | mutex_unlock(&area->lock);
|
---|
| 565 | mutex_unlock(&as->lock);
|
---|
| 566 | return ENOTSUP;
|
---|
| 567 | }
|
---|
[da1bafb] | 568 |
|
---|
| 569 | size_t pages = SIZE2FRAMES((address - area->base) + size);
|
---|
[df0103f7] | 570 | if (!pages) {
|
---|
| 571 | /*
|
---|
| 572 | * Zero size address space areas are not allowed.
|
---|
| 573 | */
|
---|
[1068f6a] | 574 | mutex_unlock(&area->lock);
|
---|
| 575 | mutex_unlock(&as->lock);
|
---|
[7242a78e] | 576 | return EPERM;
|
---|
[df0103f7] | 577 | }
|
---|
| 578 |
|
---|
| 579 | if (pages < area->pages) {
|
---|
[b6f3e7e] | 580 | uintptr_t start_free = area->base + P2SZ(pages);
|
---|
[da1bafb] | 581 |
|
---|
[df0103f7] | 582 | /*
|
---|
| 583 | * Shrinking the area.
|
---|
| 584 | * No need to check for overlaps.
|
---|
| 585 | */
|
---|
[da1bafb] | 586 |
|
---|
[c964521] | 587 | page_table_lock(as, false);
|
---|
[da1bafb] | 588 |
|
---|
[5552d60] | 589 | /*
|
---|
| 590 | * Start TLB shootdown sequence.
|
---|
| 591 | */
|
---|
[402eda5] | 592 | ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid,
|
---|
[b6f3e7e] | 593 | area->base + P2SZ(pages), area->pages - pages);
|
---|
[da1bafb] | 594 |
|
---|
[56789125] | 595 | /*
|
---|
| 596 | * Remove frames belonging to used space starting from
|
---|
| 597 | * the highest addresses downwards until an overlap with
|
---|
| 598 | * the resized address space area is found. Note that this
|
---|
| 599 | * is also the right way to remove part of the used_space
|
---|
| 600 | * B+tree leaf list.
|
---|
[da1bafb] | 601 | */
|
---|
| 602 | bool cond = true;
|
---|
| 603 | while (cond) {
|
---|
[56789125] | 604 | ASSERT(!list_empty(&area->used_space.leaf_head));
|
---|
[da1bafb] | 605 |
|
---|
| 606 | btree_node_t *node =
|
---|
[6f4495f5] | 607 | list_get_instance(area->used_space.leaf_head.prev,
|
---|
| 608 | btree_node_t, leaf_link);
|
---|
[da1bafb] | 609 |
|
---|
[56789125] | 610 | if ((cond = (bool) node->keys)) {
|
---|
[da1bafb] | 611 | uintptr_t ptr = node->key[node->keys - 1];
|
---|
| 612 | size_t size =
|
---|
[98000fb] | 613 | (size_t) node->value[node->keys - 1];
|
---|
[da1bafb] | 614 | size_t i = 0;
|
---|
| 615 |
|
---|
[b6f3e7e] | 616 | if (overlaps(ptr, P2SZ(size), area->base,
|
---|
| 617 | P2SZ(pages))) {
|
---|
[56789125] | 618 |
|
---|
[b6f3e7e] | 619 | if (ptr + P2SZ(size) <= start_free) {
|
---|
[56789125] | 620 | /*
|
---|
[6f4495f5] | 621 | * The whole interval fits
|
---|
| 622 | * completely in the resized
|
---|
| 623 | * address space area.
|
---|
[56789125] | 624 | */
|
---|
| 625 | break;
|
---|
| 626 | }
|
---|
[da1bafb] | 627 |
|
---|
[56789125] | 628 | /*
|
---|
[6f4495f5] | 629 | * Part of the interval corresponding
|
---|
| 630 | * to b and c overlaps with the resized
|
---|
| 631 | * address space area.
|
---|
[56789125] | 632 | */
|
---|
[da1bafb] | 633 |
|
---|
| 634 | /* We are almost done */
|
---|
| 635 | cond = false;
|
---|
| 636 | i = (start_free - ptr) >> PAGE_WIDTH;
|
---|
[6745592] | 637 | if (!used_space_remove(area, start_free,
|
---|
[da1bafb] | 638 | size - i))
|
---|
| 639 | panic("Cannot remove used space.");
|
---|
[56789125] | 640 | } else {
|
---|
| 641 | /*
|
---|
[6f4495f5] | 642 | * The interval of used space can be
|
---|
| 643 | * completely removed.
|
---|
[56789125] | 644 | */
|
---|
[da1bafb] | 645 | if (!used_space_remove(area, ptr, size))
|
---|
| 646 | panic("Cannot remove used space.");
|
---|
[56789125] | 647 | }
|
---|
[da1bafb] | 648 |
|
---|
| 649 | for (; i < size; i++) {
|
---|
[b6f3e7e] | 650 | pte_t *pte = page_mapping_find(as,
|
---|
[0ff03f3] | 651 | ptr + P2SZ(i), false);
|
---|
[da1bafb] | 652 |
|
---|
| 653 | ASSERT(pte);
|
---|
| 654 | ASSERT(PTE_VALID(pte));
|
---|
| 655 | ASSERT(PTE_PRESENT(pte));
|
---|
| 656 |
|
---|
| 657 | if ((area->backend) &&
|
---|
| 658 | (area->backend->frame_free)) {
|
---|
[0ee077ee] | 659 | area->backend->frame_free(area,
|
---|
[b6f3e7e] | 660 | ptr + P2SZ(i),
|
---|
[6f4495f5] | 661 | PTE_GET_FRAME(pte));
|
---|
[8182031] | 662 | }
|
---|
[da1bafb] | 663 |
|
---|
[b6f3e7e] | 664 | page_mapping_remove(as, ptr + P2SZ(i));
|
---|
[56789125] | 665 | }
|
---|
[df0103f7] | 666 | }
|
---|
| 667 | }
|
---|
[da1bafb] | 668 |
|
---|
[df0103f7] | 669 | /*
|
---|
[5552d60] | 670 | * Finish TLB shootdown sequence.
|
---|
[df0103f7] | 671 | */
|
---|
[da1bafb] | 672 |
|
---|
[b6f3e7e] | 673 | tlb_invalidate_pages(as->asid, area->base + P2SZ(pages),
|
---|
[6f4495f5] | 674 | area->pages - pages);
|
---|
[da1bafb] | 675 |
|
---|
[f1d1f5d3] | 676 | /*
|
---|
[eef1b031] | 677 | * Invalidate software translation caches
|
---|
| 678 | * (e.g. TSB on sparc64, PHT on ppc32).
|
---|
[f1d1f5d3] | 679 | */
|
---|
[b6f3e7e] | 680 | as_invalidate_translation_cache(as, area->base + P2SZ(pages),
|
---|
| 681 | area->pages - pages);
|
---|
[402eda5] | 682 | tlb_shootdown_finalize(ipl);
|
---|
[31d8e10] | 683 |
|
---|
[da1bafb] | 684 | page_table_unlock(as, false);
|
---|
[df0103f7] | 685 | } else {
|
---|
| 686 | /*
|
---|
| 687 | * Growing the area.
|
---|
| 688 | * Check for overlaps with other address space areas.
|
---|
| 689 | */
|
---|
[0b37882] | 690 | if (!check_area_conflicts(as, address, pages, area)) {
|
---|
[1068f6a] | 691 | mutex_unlock(&area->lock);
|
---|
[da1bafb] | 692 | mutex_unlock(&as->lock);
|
---|
[7242a78e] | 693 | return EADDRNOTAVAIL;
|
---|
[df0103f7] | 694 | }
|
---|
[da1bafb] | 695 | }
|
---|
| 696 |
|
---|
[e394b736] | 697 | if (area->backend && area->backend->resize) {
|
---|
| 698 | if (!area->backend->resize(area, pages)) {
|
---|
| 699 | mutex_unlock(&area->lock);
|
---|
| 700 | mutex_unlock(&as->lock);
|
---|
| 701 | return ENOMEM;
|
---|
| 702 | }
|
---|
| 703 | }
|
---|
| 704 |
|
---|
[df0103f7] | 705 | area->pages = pages;
|
---|
| 706 |
|
---|
[1068f6a] | 707 | mutex_unlock(&area->lock);
|
---|
| 708 | mutex_unlock(&as->lock);
|
---|
[da1bafb] | 709 |
|
---|
[7242a78e] | 710 | return 0;
|
---|
| 711 | }
|
---|
| 712 |
|
---|
[e3ee9b9] | 713 | /** Remove reference to address space area share info.
|
---|
| 714 | *
|
---|
| 715 | * If the reference count drops to 0, the sh_info is deallocated.
|
---|
| 716 | *
|
---|
| 717 | * @param sh_info Pointer to address space area share info.
|
---|
| 718 | *
|
---|
| 719 | */
|
---|
[7a0359b] | 720 | NO_TRACE static void sh_info_remove_reference(share_info_t *sh_info)
|
---|
[e3ee9b9] | 721 | {
|
---|
| 722 | bool dealloc = false;
|
---|
| 723 |
|
---|
| 724 | mutex_lock(&sh_info->lock);
|
---|
| 725 | ASSERT(sh_info->refcount);
|
---|
| 726 |
|
---|
| 727 | if (--sh_info->refcount == 0) {
|
---|
| 728 | dealloc = true;
|
---|
| 729 | link_t *cur;
|
---|
| 730 |
|
---|
| 731 | /*
|
---|
| 732 | * Now walk carefully the pagemap B+tree and free/remove
|
---|
| 733 | * reference from all frames found there.
|
---|
| 734 | */
|
---|
| 735 | for (cur = sh_info->pagemap.leaf_head.next;
|
---|
| 736 | cur != &sh_info->pagemap.leaf_head; cur = cur->next) {
|
---|
| 737 | btree_node_t *node
|
---|
| 738 | = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 739 | btree_key_t i;
|
---|
| 740 |
|
---|
| 741 | for (i = 0; i < node->keys; i++)
|
---|
| 742 | frame_free((uintptr_t) node->value[i]);
|
---|
| 743 | }
|
---|
| 744 |
|
---|
| 745 | }
|
---|
| 746 | mutex_unlock(&sh_info->lock);
|
---|
| 747 |
|
---|
| 748 | if (dealloc) {
|
---|
| 749 | btree_destroy(&sh_info->pagemap);
|
---|
| 750 | free(sh_info);
|
---|
| 751 | }
|
---|
| 752 | }
|
---|
| 753 |
|
---|
[7242a78e] | 754 | /** Destroy address space area.
|
---|
| 755 | *
|
---|
[da1bafb] | 756 | * @param as Address space.
|
---|
| 757 | * @param address Address within the area to be deleted.
|
---|
| 758 | *
|
---|
| 759 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[7242a78e] | 760 | *
|
---|
| 761 | */
|
---|
[7f1c620] | 762 | int as_area_destroy(as_t *as, uintptr_t address)
|
---|
[7242a78e] | 763 | {
|
---|
[1068f6a] | 764 | mutex_lock(&as->lock);
|
---|
[da1bafb] | 765 |
|
---|
| 766 | as_area_t *area = find_area_and_lock(as, address);
|
---|
[7242a78e] | 767 | if (!area) {
|
---|
[1068f6a] | 768 | mutex_unlock(&as->lock);
|
---|
[7242a78e] | 769 | return ENOENT;
|
---|
| 770 | }
|
---|
[e394b736] | 771 |
|
---|
| 772 | if (area->backend && area->backend->destroy)
|
---|
| 773 | area->backend->destroy(area);
|
---|
[da1bafb] | 774 |
|
---|
| 775 | uintptr_t base = area->base;
|
---|
| 776 |
|
---|
[c964521] | 777 | page_table_lock(as, false);
|
---|
[da1bafb] | 778 |
|
---|
[5552d60] | 779 | /*
|
---|
| 780 | * Start TLB shootdown sequence.
|
---|
| 781 | */
|
---|
[402eda5] | 782 | ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base,
|
---|
| 783 | area->pages);
|
---|
[da1bafb] | 784 |
|
---|
[567807b1] | 785 | /*
|
---|
| 786 | * Visit only the pages mapped by used_space B+tree.
|
---|
| 787 | */
|
---|
[da1bafb] | 788 | link_t *cur;
|
---|
[6f4495f5] | 789 | for (cur = area->used_space.leaf_head.next;
|
---|
| 790 | cur != &area->used_space.leaf_head; cur = cur->next) {
|
---|
[567807b1] | 791 | btree_node_t *node;
|
---|
[da1bafb] | 792 | btree_key_t i;
|
---|
[56789125] | 793 |
|
---|
[f8d069e8] | 794 | node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 795 | for (i = 0; i < node->keys; i++) {
|
---|
[da1bafb] | 796 | uintptr_t ptr = node->key[i];
|
---|
| 797 | size_t size;
|
---|
[56789125] | 798 |
|
---|
[da1bafb] | 799 | for (size = 0; size < (size_t) node->value[i]; size++) {
|
---|
[b6f3e7e] | 800 | pte_t *pte = page_mapping_find(as,
|
---|
[0ff03f3] | 801 | ptr + P2SZ(size), false);
|
---|
[da1bafb] | 802 |
|
---|
| 803 | ASSERT(pte);
|
---|
| 804 | ASSERT(PTE_VALID(pte));
|
---|
| 805 | ASSERT(PTE_PRESENT(pte));
|
---|
| 806 |
|
---|
| 807 | if ((area->backend) &&
|
---|
| 808 | (area->backend->frame_free)) {
|
---|
| 809 | area->backend->frame_free(area,
|
---|
[b6f3e7e] | 810 | ptr + P2SZ(size),
|
---|
| 811 | PTE_GET_FRAME(pte));
|
---|
[56789125] | 812 | }
|
---|
[da1bafb] | 813 |
|
---|
[b6f3e7e] | 814 | page_mapping_remove(as, ptr + P2SZ(size));
|
---|
[7242a78e] | 815 | }
|
---|
| 816 | }
|
---|
| 817 | }
|
---|
[da1bafb] | 818 |
|
---|
[7242a78e] | 819 | /*
|
---|
[5552d60] | 820 | * Finish TLB shootdown sequence.
|
---|
[7242a78e] | 821 | */
|
---|
[da1bafb] | 822 |
|
---|
[f1d1f5d3] | 823 | tlb_invalidate_pages(as->asid, area->base, area->pages);
|
---|
[da1bafb] | 824 |
|
---|
[f1d1f5d3] | 825 | /*
|
---|
[eef1b031] | 826 | * Invalidate potential software translation caches
|
---|
| 827 | * (e.g. TSB on sparc64, PHT on ppc32).
|
---|
[f1d1f5d3] | 828 | */
|
---|
| 829 | as_invalidate_translation_cache(as, area->base, area->pages);
|
---|
[402eda5] | 830 | tlb_shootdown_finalize(ipl);
|
---|
[da1bafb] | 831 |
|
---|
[c964521] | 832 | page_table_unlock(as, false);
|
---|
[f1d1f5d3] | 833 |
|
---|
[5552d60] | 834 | btree_destroy(&area->used_space);
|
---|
[da1bafb] | 835 |
|
---|
[8d4f2ae] | 836 | area->attributes |= AS_AREA_ATTR_PARTIAL;
|
---|
[8182031] | 837 |
|
---|
| 838 | if (area->sh_info)
|
---|
| 839 | sh_info_remove_reference(area->sh_info);
|
---|
[da1bafb] | 840 |
|
---|
[1068f6a] | 841 | mutex_unlock(&area->lock);
|
---|
[da1bafb] | 842 |
|
---|
[7242a78e] | 843 | /*
|
---|
| 844 | * Remove the empty area from address space.
|
---|
| 845 | */
|
---|
[f1d1f5d3] | 846 | btree_remove(&as->as_area_btree, base, NULL);
|
---|
[7242a78e] | 847 |
|
---|
[8d4f2ae] | 848 | free(area);
|
---|
| 849 |
|
---|
[f1d1f5d3] | 850 | mutex_unlock(&as->lock);
|
---|
[7242a78e] | 851 | return 0;
|
---|
[df0103f7] | 852 | }
|
---|
| 853 |
|
---|
[8d6bc2d5] | 854 | /** Share address space area with another or the same address space.
|
---|
[df0103f7] | 855 | *
|
---|
[0ee077ee] | 856 | * Address space area mapping is shared with a new address space area.
|
---|
| 857 | * If the source address space area has not been shared so far,
|
---|
| 858 | * a new sh_info is created. The new address space area simply gets the
|
---|
| 859 | * sh_info of the source area. The process of duplicating the
|
---|
| 860 | * mapping is done through the backend share function.
|
---|
[da1bafb] | 861 | *
|
---|
| 862 | * @param src_as Pointer to source address space.
|
---|
| 863 | * @param src_base Base address of the source address space area.
|
---|
| 864 | * @param acc_size Expected size of the source area.
|
---|
| 865 | * @param dst_as Pointer to destination address space.
|
---|
| 866 | * @param dst_base Target base address.
|
---|
[fd4d8c0] | 867 | * @param dst_flags_mask Destination address space area flags mask.
|
---|
[df0103f7] | 868 | *
|
---|
[da1bafb] | 869 | * @return Zero on success.
|
---|
| 870 | * @return ENOENT if there is no such task or such address space.
|
---|
| 871 | * @return EPERM if there was a problem in accepting the area.
|
---|
| 872 | * @return ENOMEM if there was a problem in allocating destination
|
---|
| 873 | * address space area.
|
---|
| 874 | * @return ENOTSUP if the address space area backend does not support
|
---|
| 875 | * sharing.
|
---|
| 876 | *
|
---|
[df0103f7] | 877 | */
|
---|
[7f1c620] | 878 | int as_area_share(as_t *src_as, uintptr_t src_base, size_t acc_size,
|
---|
[da1bafb] | 879 | as_t *dst_as, uintptr_t dst_base, unsigned int dst_flags_mask)
|
---|
[df0103f7] | 880 | {
|
---|
[1068f6a] | 881 | mutex_lock(&src_as->lock);
|
---|
[da1bafb] | 882 | as_area_t *src_area = find_area_and_lock(src_as, src_base);
|
---|
[a9e8b39] | 883 | if (!src_area) {
|
---|
[6fa476f7] | 884 | /*
|
---|
| 885 | * Could not find the source address space area.
|
---|
| 886 | */
|
---|
[1068f6a] | 887 | mutex_unlock(&src_as->lock);
|
---|
[6fa476f7] | 888 | return ENOENT;
|
---|
| 889 | }
|
---|
[da1bafb] | 890 |
|
---|
| 891 | if ((!src_area->backend) || (!src_area->backend->share)) {
|
---|
[8d6bc2d5] | 892 | /*
|
---|
[f47fd19] | 893 | * There is no backend or the backend does not
|
---|
[0ee077ee] | 894 | * know how to share the area.
|
---|
[8d6bc2d5] | 895 | */
|
---|
| 896 | mutex_unlock(&src_area->lock);
|
---|
| 897 | mutex_unlock(&src_as->lock);
|
---|
| 898 | return ENOTSUP;
|
---|
| 899 | }
|
---|
| 900 |
|
---|
[b6f3e7e] | 901 | size_t src_size = P2SZ(src_area->pages);
|
---|
[da1bafb] | 902 | unsigned int src_flags = src_area->flags;
|
---|
| 903 | mem_backend_t *src_backend = src_area->backend;
|
---|
| 904 | mem_backend_data_t src_backend_data = src_area->backend_data;
|
---|
| 905 |
|
---|
[1ec1fd8] | 906 | /* Share the cacheable flag from the original mapping */
|
---|
| 907 | if (src_flags & AS_AREA_CACHEABLE)
|
---|
| 908 | dst_flags_mask |= AS_AREA_CACHEABLE;
|
---|
[da1bafb] | 909 |
|
---|
| 910 | if ((src_size != acc_size) ||
|
---|
| 911 | ((src_flags & dst_flags_mask) != dst_flags_mask)) {
|
---|
[8d6bc2d5] | 912 | mutex_unlock(&src_area->lock);
|
---|
| 913 | mutex_unlock(&src_as->lock);
|
---|
[df0103f7] | 914 | return EPERM;
|
---|
| 915 | }
|
---|
[da1bafb] | 916 |
|
---|
[8d6bc2d5] | 917 | /*
|
---|
| 918 | * Now we are committed to sharing the area.
|
---|
[8440473] | 919 | * First, prepare the area for sharing.
|
---|
[8d6bc2d5] | 920 | * Then it will be safe to unlock it.
|
---|
| 921 | */
|
---|
[da1bafb] | 922 | share_info_t *sh_info = src_area->sh_info;
|
---|
[8d6bc2d5] | 923 | if (!sh_info) {
|
---|
| 924 | sh_info = (share_info_t *) malloc(sizeof(share_info_t), 0);
|
---|
[08a19ba] | 925 | mutex_initialize(&sh_info->lock, MUTEX_PASSIVE);
|
---|
[8d6bc2d5] | 926 | sh_info->refcount = 2;
|
---|
| 927 | btree_create(&sh_info->pagemap);
|
---|
| 928 | src_area->sh_info = sh_info;
|
---|
[da1bafb] | 929 |
|
---|
[c0697c4c] | 930 | /*
|
---|
| 931 | * Call the backend to setup sharing.
|
---|
| 932 | */
|
---|
| 933 | src_area->backend->share(src_area);
|
---|
[8d6bc2d5] | 934 | } else {
|
---|
| 935 | mutex_lock(&sh_info->lock);
|
---|
| 936 | sh_info->refcount++;
|
---|
| 937 | mutex_unlock(&sh_info->lock);
|
---|
| 938 | }
|
---|
[da1bafb] | 939 |
|
---|
[8d6bc2d5] | 940 | mutex_unlock(&src_area->lock);
|
---|
| 941 | mutex_unlock(&src_as->lock);
|
---|
[da1bafb] | 942 |
|
---|
[df0103f7] | 943 | /*
|
---|
[a9e8b39] | 944 | * Create copy of the source address space area.
|
---|
| 945 | * The destination area is created with AS_AREA_ATTR_PARTIAL
|
---|
| 946 | * attribute set which prevents race condition with
|
---|
| 947 | * preliminary as_page_fault() calls.
|
---|
[fd4d8c0] | 948 | * The flags of the source area are masked against dst_flags_mask
|
---|
| 949 | * to support sharing in less privileged mode.
|
---|
[df0103f7] | 950 | */
|
---|
[da1bafb] | 951 | as_area_t *dst_area = as_area_create(dst_as, dst_flags_mask, src_size,
|
---|
| 952 | dst_base, AS_AREA_ATTR_PARTIAL, src_backend, &src_backend_data);
|
---|
[a9e8b39] | 953 | if (!dst_area) {
|
---|
[df0103f7] | 954 | /*
|
---|
| 955 | * Destination address space area could not be created.
|
---|
| 956 | */
|
---|
[8d6bc2d5] | 957 | sh_info_remove_reference(sh_info);
|
---|
| 958 |
|
---|
[df0103f7] | 959 | return ENOMEM;
|
---|
| 960 | }
|
---|
[da1bafb] | 961 |
|
---|
[a9e8b39] | 962 | /*
|
---|
| 963 | * Now the destination address space area has been
|
---|
| 964 | * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
|
---|
[8d6bc2d5] | 965 | * attribute and set the sh_info.
|
---|
[da1bafb] | 966 | */
|
---|
| 967 | mutex_lock(&dst_as->lock);
|
---|
[1068f6a] | 968 | mutex_lock(&dst_area->lock);
|
---|
[a9e8b39] | 969 | dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
|
---|
[8d6bc2d5] | 970 | dst_area->sh_info = sh_info;
|
---|
[1068f6a] | 971 | mutex_unlock(&dst_area->lock);
|
---|
[da1bafb] | 972 | mutex_unlock(&dst_as->lock);
|
---|
| 973 |
|
---|
[df0103f7] | 974 | return 0;
|
---|
| 975 | }
|
---|
| 976 |
|
---|
[fb84455] | 977 | /** Check access mode for address space area.
|
---|
| 978 | *
|
---|
[da1bafb] | 979 | * @param area Address space area.
|
---|
| 980 | * @param access Access mode.
|
---|
| 981 | *
|
---|
| 982 | * @return False if access violates area's permissions, true
|
---|
| 983 | * otherwise.
|
---|
[fb84455] | 984 | *
|
---|
| 985 | */
|
---|
[97bdb4a] | 986 | NO_TRACE bool as_area_check_access(as_area_t *area, pf_access_t access)
|
---|
[fb84455] | 987 | {
|
---|
[fc47885] | 988 | ASSERT(mutex_locked(&area->lock));
|
---|
| 989 |
|
---|
[fb84455] | 990 | int flagmap[] = {
|
---|
| 991 | [PF_ACCESS_READ] = AS_AREA_READ,
|
---|
| 992 | [PF_ACCESS_WRITE] = AS_AREA_WRITE,
|
---|
| 993 | [PF_ACCESS_EXEC] = AS_AREA_EXEC
|
---|
| 994 | };
|
---|
[da1bafb] | 995 |
|
---|
[fb84455] | 996 | if (!(area->flags & flagmap[access]))
|
---|
| 997 | return false;
|
---|
| 998 |
|
---|
| 999 | return true;
|
---|
| 1000 | }
|
---|
| 1001 |
|
---|
[e3ee9b9] | 1002 | /** Convert address space area flags to page flags.
|
---|
| 1003 | *
|
---|
| 1004 | * @param aflags Flags of some address space area.
|
---|
| 1005 | *
|
---|
| 1006 | * @return Flags to be passed to page_mapping_insert().
|
---|
| 1007 | *
|
---|
| 1008 | */
|
---|
[7a0359b] | 1009 | NO_TRACE static unsigned int area_flags_to_page_flags(unsigned int aflags)
|
---|
[e3ee9b9] | 1010 | {
|
---|
| 1011 | unsigned int flags = PAGE_USER | PAGE_PRESENT;
|
---|
| 1012 |
|
---|
| 1013 | if (aflags & AS_AREA_READ)
|
---|
| 1014 | flags |= PAGE_READ;
|
---|
| 1015 |
|
---|
| 1016 | if (aflags & AS_AREA_WRITE)
|
---|
| 1017 | flags |= PAGE_WRITE;
|
---|
| 1018 |
|
---|
| 1019 | if (aflags & AS_AREA_EXEC)
|
---|
| 1020 | flags |= PAGE_EXEC;
|
---|
| 1021 |
|
---|
| 1022 | if (aflags & AS_AREA_CACHEABLE)
|
---|
| 1023 | flags |= PAGE_CACHEABLE;
|
---|
| 1024 |
|
---|
| 1025 | return flags;
|
---|
| 1026 | }
|
---|
| 1027 |
|
---|
[6745592] | 1028 | /** Change adress space area flags.
|
---|
[c98e6ee] | 1029 | *
|
---|
| 1030 | * The idea is to have the same data, but with a different access mode.
|
---|
| 1031 | * This is needed e.g. for writing code into memory and then executing it.
|
---|
| 1032 | * In order for this to work properly, this may copy the data
|
---|
| 1033 | * into private anonymous memory (unless it's already there).
|
---|
| 1034 | *
|
---|
[76fca31] | 1035 | * @param as Address space.
|
---|
| 1036 | * @param flags Flags of the area memory.
|
---|
| 1037 | * @param address Address within the area to be changed.
|
---|
| 1038 | *
|
---|
| 1039 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[c98e6ee] | 1040 | *
|
---|
| 1041 | */
|
---|
[da1bafb] | 1042 | int as_area_change_flags(as_t *as, unsigned int flags, uintptr_t address)
|
---|
[c98e6ee] | 1043 | {
|
---|
| 1044 | /* Flags for the new memory mapping */
|
---|
[da1bafb] | 1045 | unsigned int page_flags = area_flags_to_page_flags(flags);
|
---|
| 1046 |
|
---|
[c98e6ee] | 1047 | mutex_lock(&as->lock);
|
---|
[da1bafb] | 1048 |
|
---|
| 1049 | as_area_t *area = find_area_and_lock(as, address);
|
---|
[c98e6ee] | 1050 | if (!area) {
|
---|
| 1051 | mutex_unlock(&as->lock);
|
---|
| 1052 | return ENOENT;
|
---|
| 1053 | }
|
---|
[da1bafb] | 1054 |
|
---|
[76fca31] | 1055 | if ((area->sh_info) || (area->backend != &anon_backend)) {
|
---|
[c98e6ee] | 1056 | /* Copying shared areas not supported yet */
|
---|
| 1057 | /* Copying non-anonymous memory not supported yet */
|
---|
| 1058 | mutex_unlock(&area->lock);
|
---|
| 1059 | mutex_unlock(&as->lock);
|
---|
| 1060 | return ENOTSUP;
|
---|
| 1061 | }
|
---|
[da1bafb] | 1062 |
|
---|
[c98e6ee] | 1063 | /*
|
---|
| 1064 | * Compute total number of used pages in the used_space B+tree
|
---|
| 1065 | */
|
---|
[da1bafb] | 1066 | size_t used_pages = 0;
|
---|
| 1067 | link_t *cur;
|
---|
| 1068 |
|
---|
[c98e6ee] | 1069 | for (cur = area->used_space.leaf_head.next;
|
---|
| 1070 | cur != &area->used_space.leaf_head; cur = cur->next) {
|
---|
[da1bafb] | 1071 | btree_node_t *node
|
---|
| 1072 | = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 1073 | btree_key_t i;
|
---|
[c98e6ee] | 1074 |
|
---|
[da1bafb] | 1075 | for (i = 0; i < node->keys; i++)
|
---|
[98000fb] | 1076 | used_pages += (size_t) node->value[i];
|
---|
[c98e6ee] | 1077 | }
|
---|
[da1bafb] | 1078 |
|
---|
[c98e6ee] | 1079 | /* An array for storing frame numbers */
|
---|
[da1bafb] | 1080 | uintptr_t *old_frame = malloc(used_pages * sizeof(uintptr_t), 0);
|
---|
| 1081 |
|
---|
[c964521] | 1082 | page_table_lock(as, false);
|
---|
[da1bafb] | 1083 |
|
---|
[c98e6ee] | 1084 | /*
|
---|
| 1085 | * Start TLB shootdown sequence.
|
---|
| 1086 | */
|
---|
[402eda5] | 1087 | ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base,
|
---|
| 1088 | area->pages);
|
---|
[da1bafb] | 1089 |
|
---|
[c98e6ee] | 1090 | /*
|
---|
| 1091 | * Remove used pages from page tables and remember their frame
|
---|
| 1092 | * numbers.
|
---|
| 1093 | */
|
---|
[da1bafb] | 1094 | size_t frame_idx = 0;
|
---|
| 1095 |
|
---|
[c98e6ee] | 1096 | for (cur = area->used_space.leaf_head.next;
|
---|
| 1097 | cur != &area->used_space.leaf_head; cur = cur->next) {
|
---|
[b6f3e7e] | 1098 | btree_node_t *node = list_get_instance(cur, btree_node_t,
|
---|
| 1099 | leaf_link);
|
---|
[da1bafb] | 1100 | btree_key_t i;
|
---|
[c98e6ee] | 1101 |
|
---|
| 1102 | for (i = 0; i < node->keys; i++) {
|
---|
[da1bafb] | 1103 | uintptr_t ptr = node->key[i];
|
---|
| 1104 | size_t size;
|
---|
[c98e6ee] | 1105 |
|
---|
[da1bafb] | 1106 | for (size = 0; size < (size_t) node->value[i]; size++) {
|
---|
[b6f3e7e] | 1107 | pte_t *pte = page_mapping_find(as,
|
---|
[0ff03f3] | 1108 | ptr + P2SZ(size), false);
|
---|
[da1bafb] | 1109 |
|
---|
| 1110 | ASSERT(pte);
|
---|
| 1111 | ASSERT(PTE_VALID(pte));
|
---|
| 1112 | ASSERT(PTE_PRESENT(pte));
|
---|
| 1113 |
|
---|
[c98e6ee] | 1114 | old_frame[frame_idx++] = PTE_GET_FRAME(pte);
|
---|
[da1bafb] | 1115 |
|
---|
[c98e6ee] | 1116 | /* Remove old mapping */
|
---|
[b6f3e7e] | 1117 | page_mapping_remove(as, ptr + P2SZ(size));
|
---|
[c98e6ee] | 1118 | }
|
---|
| 1119 | }
|
---|
| 1120 | }
|
---|
[da1bafb] | 1121 |
|
---|
[c98e6ee] | 1122 | /*
|
---|
| 1123 | * Finish TLB shootdown sequence.
|
---|
| 1124 | */
|
---|
[da1bafb] | 1125 |
|
---|
[c98e6ee] | 1126 | tlb_invalidate_pages(as->asid, area->base, area->pages);
|
---|
[76fca31] | 1127 |
|
---|
[c98e6ee] | 1128 | /*
|
---|
[eef1b031] | 1129 | * Invalidate potential software translation caches
|
---|
| 1130 | * (e.g. TSB on sparc64, PHT on ppc32).
|
---|
[c98e6ee] | 1131 | */
|
---|
| 1132 | as_invalidate_translation_cache(as, area->base, area->pages);
|
---|
[402eda5] | 1133 | tlb_shootdown_finalize(ipl);
|
---|
[da1bafb] | 1134 |
|
---|
[c964521] | 1135 | page_table_unlock(as, false);
|
---|
[da1bafb] | 1136 |
|
---|
[ae7f6fb] | 1137 | /*
|
---|
| 1138 | * Set the new flags.
|
---|
| 1139 | */
|
---|
| 1140 | area->flags = flags;
|
---|
[da1bafb] | 1141 |
|
---|
[c98e6ee] | 1142 | /*
|
---|
| 1143 | * Map pages back in with new flags. This step is kept separate
|
---|
[6745592] | 1144 | * so that the memory area could not be accesed with both the old and
|
---|
| 1145 | * the new flags at once.
|
---|
[c98e6ee] | 1146 | */
|
---|
| 1147 | frame_idx = 0;
|
---|
[da1bafb] | 1148 |
|
---|
[c98e6ee] | 1149 | for (cur = area->used_space.leaf_head.next;
|
---|
| 1150 | cur != &area->used_space.leaf_head; cur = cur->next) {
|
---|
[da1bafb] | 1151 | btree_node_t *node
|
---|
| 1152 | = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 1153 | btree_key_t i;
|
---|
[c98e6ee] | 1154 |
|
---|
| 1155 | for (i = 0; i < node->keys; i++) {
|
---|
[da1bafb] | 1156 | uintptr_t ptr = node->key[i];
|
---|
| 1157 | size_t size;
|
---|
[c98e6ee] | 1158 |
|
---|
[da1bafb] | 1159 | for (size = 0; size < (size_t) node->value[i]; size++) {
|
---|
[c98e6ee] | 1160 | page_table_lock(as, false);
|
---|
[da1bafb] | 1161 |
|
---|
[c98e6ee] | 1162 | /* Insert the new mapping */
|
---|
[b6f3e7e] | 1163 | page_mapping_insert(as, ptr + P2SZ(size),
|
---|
[c98e6ee] | 1164 | old_frame[frame_idx++], page_flags);
|
---|
[da1bafb] | 1165 |
|
---|
[c98e6ee] | 1166 | page_table_unlock(as, false);
|
---|
| 1167 | }
|
---|
| 1168 | }
|
---|
| 1169 | }
|
---|
[da1bafb] | 1170 |
|
---|
[c98e6ee] | 1171 | free(old_frame);
|
---|
[da1bafb] | 1172 |
|
---|
[c98e6ee] | 1173 | mutex_unlock(&area->lock);
|
---|
| 1174 | mutex_unlock(&as->lock);
|
---|
[da1bafb] | 1175 |
|
---|
[c98e6ee] | 1176 | return 0;
|
---|
| 1177 | }
|
---|
| 1178 |
|
---|
[20d50a1] | 1179 | /** Handle page fault within the current address space.
|
---|
| 1180 | *
|
---|
[6745592] | 1181 | * This is the high-level page fault handler. It decides whether the page fault
|
---|
| 1182 | * can be resolved by any backend and if so, it invokes the backend to resolve
|
---|
| 1183 | * the page fault.
|
---|
[8182031] | 1184 | *
|
---|
[20d50a1] | 1185 | * Interrupts are assumed disabled.
|
---|
| 1186 | *
|
---|
[da1bafb] | 1187 | * @param page Faulting page.
|
---|
| 1188 | * @param access Access mode that caused the page fault (i.e.
|
---|
| 1189 | * read/write/exec).
|
---|
| 1190 | * @param istate Pointer to the interrupted state.
|
---|
| 1191 | *
|
---|
| 1192 | * @return AS_PF_FAULT on page fault.
|
---|
| 1193 | * @return AS_PF_OK on success.
|
---|
| 1194 | * @return AS_PF_DEFER if the fault was caused by copy_to_uspace()
|
---|
| 1195 | * or copy_from_uspace().
|
---|
[20d50a1] | 1196 | *
|
---|
| 1197 | */
|
---|
[7f1c620] | 1198 | int as_page_fault(uintptr_t page, pf_access_t access, istate_t *istate)
|
---|
[20d50a1] | 1199 | {
|
---|
[1068f6a] | 1200 | if (!THREAD)
|
---|
[8182031] | 1201 | return AS_PF_FAULT;
|
---|
[7af8c0e] | 1202 |
|
---|
| 1203 | if (!AS)
|
---|
| 1204 | return AS_PF_FAULT;
|
---|
| 1205 |
|
---|
[1068f6a] | 1206 | mutex_lock(&AS->lock);
|
---|
[da1bafb] | 1207 | as_area_t *area = find_area_and_lock(AS, page);
|
---|
[20d50a1] | 1208 | if (!area) {
|
---|
| 1209 | /*
|
---|
| 1210 | * No area contained mapping for 'page'.
|
---|
| 1211 | * Signal page fault to low-level handler.
|
---|
| 1212 | */
|
---|
[1068f6a] | 1213 | mutex_unlock(&AS->lock);
|
---|
[e3c762cd] | 1214 | goto page_fault;
|
---|
[20d50a1] | 1215 | }
|
---|
[da1bafb] | 1216 |
|
---|
[a9e8b39] | 1217 | if (area->attributes & AS_AREA_ATTR_PARTIAL) {
|
---|
| 1218 | /*
|
---|
| 1219 | * The address space area is not fully initialized.
|
---|
| 1220 | * Avoid possible race by returning error.
|
---|
| 1221 | */
|
---|
[1068f6a] | 1222 | mutex_unlock(&area->lock);
|
---|
| 1223 | mutex_unlock(&AS->lock);
|
---|
[da1bafb] | 1224 | goto page_fault;
|
---|
[a9e8b39] | 1225 | }
|
---|
[da1bafb] | 1226 |
|
---|
| 1227 | if ((!area->backend) || (!area->backend->page_fault)) {
|
---|
[8182031] | 1228 | /*
|
---|
| 1229 | * The address space area is not backed by any backend
|
---|
| 1230 | * or the backend cannot handle page faults.
|
---|
| 1231 | */
|
---|
| 1232 | mutex_unlock(&area->lock);
|
---|
| 1233 | mutex_unlock(&AS->lock);
|
---|
[da1bafb] | 1234 | goto page_fault;
|
---|
[8182031] | 1235 | }
|
---|
[da1bafb] | 1236 |
|
---|
[2299914] | 1237 | page_table_lock(AS, false);
|
---|
| 1238 |
|
---|
| 1239 | /*
|
---|
[6745592] | 1240 | * To avoid race condition between two page faults on the same address,
|
---|
| 1241 | * we need to make sure the mapping has not been already inserted.
|
---|
[2299914] | 1242 | */
|
---|
[da1bafb] | 1243 | pte_t *pte;
|
---|
[0ff03f3] | 1244 | if ((pte = page_mapping_find(AS, page, false))) {
|
---|
[2299914] | 1245 | if (PTE_PRESENT(pte)) {
|
---|
[fb84455] | 1246 | if (((access == PF_ACCESS_READ) && PTE_READABLE(pte)) ||
|
---|
[6f4495f5] | 1247 | (access == PF_ACCESS_WRITE && PTE_WRITABLE(pte)) ||
|
---|
| 1248 | (access == PF_ACCESS_EXEC && PTE_EXECUTABLE(pte))) {
|
---|
[fb84455] | 1249 | page_table_unlock(AS, false);
|
---|
| 1250 | mutex_unlock(&area->lock);
|
---|
| 1251 | mutex_unlock(&AS->lock);
|
---|
| 1252 | return AS_PF_OK;
|
---|
| 1253 | }
|
---|
[2299914] | 1254 | }
|
---|
| 1255 | }
|
---|
[20d50a1] | 1256 |
|
---|
| 1257 | /*
|
---|
[8182031] | 1258 | * Resort to the backend page fault handler.
|
---|
[20d50a1] | 1259 | */
|
---|
[0ee077ee] | 1260 | if (area->backend->page_fault(area, page, access) != AS_PF_OK) {
|
---|
[8182031] | 1261 | page_table_unlock(AS, false);
|
---|
| 1262 | mutex_unlock(&area->lock);
|
---|
| 1263 | mutex_unlock(&AS->lock);
|
---|
| 1264 | goto page_fault;
|
---|
| 1265 | }
|
---|
[20d50a1] | 1266 |
|
---|
[8182031] | 1267 | page_table_unlock(AS, false);
|
---|
[1068f6a] | 1268 | mutex_unlock(&area->lock);
|
---|
| 1269 | mutex_unlock(&AS->lock);
|
---|
[e3c762cd] | 1270 | return AS_PF_OK;
|
---|
[da1bafb] | 1271 |
|
---|
[e3c762cd] | 1272 | page_fault:
|
---|
| 1273 | if (THREAD->in_copy_from_uspace) {
|
---|
| 1274 | THREAD->in_copy_from_uspace = false;
|
---|
[6f4495f5] | 1275 | istate_set_retaddr(istate,
|
---|
| 1276 | (uintptr_t) &memcpy_from_uspace_failover_address);
|
---|
[e3c762cd] | 1277 | } else if (THREAD->in_copy_to_uspace) {
|
---|
| 1278 | THREAD->in_copy_to_uspace = false;
|
---|
[6f4495f5] | 1279 | istate_set_retaddr(istate,
|
---|
| 1280 | (uintptr_t) &memcpy_to_uspace_failover_address);
|
---|
[e3c762cd] | 1281 | } else {
|
---|
| 1282 | return AS_PF_FAULT;
|
---|
| 1283 | }
|
---|
[da1bafb] | 1284 |
|
---|
[e3c762cd] | 1285 | return AS_PF_DEFER;
|
---|
[20d50a1] | 1286 | }
|
---|
| 1287 |
|
---|
[7e4e532] | 1288 | /** Switch address spaces.
|
---|
[1068f6a] | 1289 | *
|
---|
| 1290 | * Note that this function cannot sleep as it is essentially a part of
|
---|
[879585a3] | 1291 | * scheduling. Sleeping here would lead to deadlock on wakeup. Another
|
---|
| 1292 | * thing which is forbidden in this context is locking the address space.
|
---|
[20d50a1] | 1293 | *
|
---|
[31d8e10] | 1294 | * When this function is enetered, no spinlocks may be held.
|
---|
| 1295 | *
|
---|
[da1bafb] | 1296 | * @param old Old address space or NULL.
|
---|
| 1297 | * @param new New address space.
|
---|
| 1298 | *
|
---|
[20d50a1] | 1299 | */
|
---|
[80bcaed] | 1300 | void as_switch(as_t *old_as, as_t *new_as)
|
---|
[20d50a1] | 1301 | {
|
---|
[31d8e10] | 1302 | DEADLOCK_PROBE_INIT(p_asidlock);
|
---|
| 1303 | preemption_disable();
|
---|
[da1bafb] | 1304 |
|
---|
[31d8e10] | 1305 | retry:
|
---|
| 1306 | (void) interrupts_disable();
|
---|
| 1307 | if (!spinlock_trylock(&asidlock)) {
|
---|
[da1bafb] | 1308 | /*
|
---|
[31d8e10] | 1309 | * Avoid deadlock with TLB shootdown.
|
---|
| 1310 | * We can enable interrupts here because
|
---|
| 1311 | * preemption is disabled. We should not be
|
---|
| 1312 | * holding any other lock.
|
---|
| 1313 | */
|
---|
| 1314 | (void) interrupts_enable();
|
---|
| 1315 | DEADLOCK_PROBE(p_asidlock, DEADLOCK_THRESHOLD);
|
---|
| 1316 | goto retry;
|
---|
| 1317 | }
|
---|
| 1318 | preemption_enable();
|
---|
[da1bafb] | 1319 |
|
---|
[7e4e532] | 1320 | /*
|
---|
| 1321 | * First, take care of the old address space.
|
---|
[da1bafb] | 1322 | */
|
---|
[80bcaed] | 1323 | if (old_as) {
|
---|
| 1324 | ASSERT(old_as->cpu_refcount);
|
---|
[da1bafb] | 1325 |
|
---|
| 1326 | if ((--old_as->cpu_refcount == 0) && (old_as != AS_KERNEL)) {
|
---|
[7e4e532] | 1327 | /*
|
---|
| 1328 | * The old address space is no longer active on
|
---|
| 1329 | * any processor. It can be appended to the
|
---|
| 1330 | * list of inactive address spaces with assigned
|
---|
| 1331 | * ASID.
|
---|
| 1332 | */
|
---|
[2057572] | 1333 | ASSERT(old_as->asid != ASID_INVALID);
|
---|
[da1bafb] | 1334 |
|
---|
[2057572] | 1335 | list_append(&old_as->inactive_as_with_asid_link,
|
---|
| 1336 | &inactive_as_with_asid_head);
|
---|
[7e4e532] | 1337 | }
|
---|
[da1bafb] | 1338 |
|
---|
[57da95c] | 1339 | /*
|
---|
| 1340 | * Perform architecture-specific tasks when the address space
|
---|
| 1341 | * is being removed from the CPU.
|
---|
| 1342 | */
|
---|
[80bcaed] | 1343 | as_deinstall_arch(old_as);
|
---|
[7e4e532] | 1344 | }
|
---|
[da1bafb] | 1345 |
|
---|
[7e4e532] | 1346 | /*
|
---|
| 1347 | * Second, prepare the new address space.
|
---|
| 1348 | */
|
---|
[80bcaed] | 1349 | if ((new_as->cpu_refcount++ == 0) && (new_as != AS_KERNEL)) {
|
---|
[879585a3] | 1350 | if (new_as->asid != ASID_INVALID)
|
---|
[80bcaed] | 1351 | list_remove(&new_as->inactive_as_with_asid_link);
|
---|
[879585a3] | 1352 | else
|
---|
| 1353 | new_as->asid = asid_get();
|
---|
[7e4e532] | 1354 | }
|
---|
[da1bafb] | 1355 |
|
---|
[80bcaed] | 1356 | #ifdef AS_PAGE_TABLE
|
---|
| 1357 | SET_PTL0_ADDRESS(new_as->genarch.page_table);
|
---|
| 1358 | #endif
|
---|
[7e4e532] | 1359 |
|
---|
[20d50a1] | 1360 | /*
|
---|
| 1361 | * Perform architecture-specific steps.
|
---|
[4512d7e] | 1362 | * (e.g. write ASID to hardware register etc.)
|
---|
[20d50a1] | 1363 | */
|
---|
[80bcaed] | 1364 | as_install_arch(new_as);
|
---|
[da1bafb] | 1365 |
|
---|
[879585a3] | 1366 | spinlock_unlock(&asidlock);
|
---|
[20d50a1] | 1367 |
|
---|
[80bcaed] | 1368 | AS = new_as;
|
---|
[20d50a1] | 1369 | }
|
---|
[6a3c9a7] | 1370 |
|
---|
[df0103f7] | 1371 | /** Compute flags for virtual address translation subsytem.
|
---|
| 1372 | *
|
---|
[da1bafb] | 1373 | * @param area Address space area.
|
---|
| 1374 | *
|
---|
| 1375 | * @return Flags to be used in page_mapping_insert().
|
---|
[df0103f7] | 1376 | *
|
---|
| 1377 | */
|
---|
[97bdb4a] | 1378 | NO_TRACE unsigned int as_area_get_flags(as_area_t *area)
|
---|
[df0103f7] | 1379 | {
|
---|
[1d432f9] | 1380 | ASSERT(mutex_locked(&area->lock));
|
---|
[fc47885] | 1381 |
|
---|
[da1bafb] | 1382 | return area_flags_to_page_flags(area->flags);
|
---|
[df0103f7] | 1383 | }
|
---|
| 1384 |
|
---|
[ef67bab] | 1385 | /** Create page table.
|
---|
| 1386 | *
|
---|
[6745592] | 1387 | * Depending on architecture, create either address space private or global page
|
---|
| 1388 | * table.
|
---|
[ef67bab] | 1389 | *
|
---|
[da1bafb] | 1390 | * @param flags Flags saying whether the page table is for the kernel
|
---|
| 1391 | * address space.
|
---|
| 1392 | *
|
---|
| 1393 | * @return First entry of the page table.
|
---|
[ef67bab] | 1394 | *
|
---|
| 1395 | */
|
---|
[97bdb4a] | 1396 | NO_TRACE pte_t *page_table_create(unsigned int flags)
|
---|
[ef67bab] | 1397 | {
|
---|
[bd1deed] | 1398 | ASSERT(as_operations);
|
---|
| 1399 | ASSERT(as_operations->page_table_create);
|
---|
| 1400 |
|
---|
| 1401 | return as_operations->page_table_create(flags);
|
---|
[ef67bab] | 1402 | }
|
---|
[d3e7ff4] | 1403 |
|
---|
[482826d] | 1404 | /** Destroy page table.
|
---|
| 1405 | *
|
---|
| 1406 | * Destroy page table in architecture specific way.
|
---|
| 1407 | *
|
---|
[da1bafb] | 1408 | * @param page_table Physical address of PTL0.
|
---|
| 1409 | *
|
---|
[482826d] | 1410 | */
|
---|
[97bdb4a] | 1411 | NO_TRACE void page_table_destroy(pte_t *page_table)
|
---|
[482826d] | 1412 | {
|
---|
[bd1deed] | 1413 | ASSERT(as_operations);
|
---|
| 1414 | ASSERT(as_operations->page_table_destroy);
|
---|
| 1415 |
|
---|
| 1416 | as_operations->page_table_destroy(page_table);
|
---|
[482826d] | 1417 | }
|
---|
| 1418 |
|
---|
[2299914] | 1419 | /** Lock page table.
|
---|
| 1420 | *
|
---|
| 1421 | * This function should be called before any page_mapping_insert(),
|
---|
| 1422 | * page_mapping_remove() and page_mapping_find().
|
---|
[da1bafb] | 1423 | *
|
---|
[2299914] | 1424 | * Locking order is such that address space areas must be locked
|
---|
| 1425 | * prior to this call. Address space can be locked prior to this
|
---|
| 1426 | * call in which case the lock argument is false.
|
---|
| 1427 | *
|
---|
[da1bafb] | 1428 | * @param as Address space.
|
---|
| 1429 | * @param lock If false, do not attempt to lock as->lock.
|
---|
| 1430 | *
|
---|
[2299914] | 1431 | */
|
---|
[97bdb4a] | 1432 | NO_TRACE void page_table_lock(as_t *as, bool lock)
|
---|
[2299914] | 1433 | {
|
---|
| 1434 | ASSERT(as_operations);
|
---|
| 1435 | ASSERT(as_operations->page_table_lock);
|
---|
[bd1deed] | 1436 |
|
---|
[2299914] | 1437 | as_operations->page_table_lock(as, lock);
|
---|
| 1438 | }
|
---|
| 1439 |
|
---|
| 1440 | /** Unlock page table.
|
---|
| 1441 | *
|
---|
[da1bafb] | 1442 | * @param as Address space.
|
---|
| 1443 | * @param unlock If false, do not attempt to unlock as->lock.
|
---|
| 1444 | *
|
---|
[2299914] | 1445 | */
|
---|
[97bdb4a] | 1446 | NO_TRACE void page_table_unlock(as_t *as, bool unlock)
|
---|
[2299914] | 1447 | {
|
---|
| 1448 | ASSERT(as_operations);
|
---|
| 1449 | ASSERT(as_operations->page_table_unlock);
|
---|
[bd1deed] | 1450 |
|
---|
[2299914] | 1451 | as_operations->page_table_unlock(as, unlock);
|
---|
| 1452 | }
|
---|
| 1453 |
|
---|
[ada559c] | 1454 | /** Test whether page tables are locked.
|
---|
| 1455 | *
|
---|
[e3ee9b9] | 1456 | * @param as Address space where the page tables belong.
|
---|
[ada559c] | 1457 | *
|
---|
[e3ee9b9] | 1458 | * @return True if the page tables belonging to the address soace
|
---|
| 1459 | * are locked, otherwise false.
|
---|
[ada559c] | 1460 | */
|
---|
[97bdb4a] | 1461 | NO_TRACE bool page_table_locked(as_t *as)
|
---|
[ada559c] | 1462 | {
|
---|
| 1463 | ASSERT(as_operations);
|
---|
| 1464 | ASSERT(as_operations->page_table_locked);
|
---|
| 1465 |
|
---|
| 1466 | return as_operations->page_table_locked(as);
|
---|
| 1467 | }
|
---|
| 1468 |
|
---|
[b878df3] | 1469 | /** Return size of the address space area with given base.
|
---|
| 1470 | *
|
---|
[1d432f9] | 1471 | * @param base Arbitrary address inside the address space area.
|
---|
[da1bafb] | 1472 | *
|
---|
| 1473 | * @return Size of the address space area in bytes or zero if it
|
---|
| 1474 | * does not exist.
|
---|
[b878df3] | 1475 | *
|
---|
| 1476 | */
|
---|
| 1477 | size_t as_area_get_size(uintptr_t base)
|
---|
[7c23af9] | 1478 | {
|
---|
| 1479 | size_t size;
|
---|
[da1bafb] | 1480 |
|
---|
[1d432f9] | 1481 | page_table_lock(AS, true);
|
---|
[da1bafb] | 1482 | as_area_t *src_area = find_area_and_lock(AS, base);
|
---|
| 1483 |
|
---|
[6745592] | 1484 | if (src_area) {
|
---|
[b6f3e7e] | 1485 | size = P2SZ(src_area->pages);
|
---|
[1068f6a] | 1486 | mutex_unlock(&src_area->lock);
|
---|
[da1bafb] | 1487 | } else
|
---|
[7c23af9] | 1488 | size = 0;
|
---|
[da1bafb] | 1489 |
|
---|
[1d432f9] | 1490 | page_table_unlock(AS, true);
|
---|
[7c23af9] | 1491 | return size;
|
---|
| 1492 | }
|
---|
| 1493 |
|
---|
[25bf215] | 1494 | /** Mark portion of address space area as used.
|
---|
| 1495 | *
|
---|
| 1496 | * The address space area must be already locked.
|
---|
| 1497 | *
|
---|
[da1bafb] | 1498 | * @param area Address space area.
|
---|
| 1499 | * @param page First page to be marked.
|
---|
| 1500 | * @param count Number of page to be marked.
|
---|
| 1501 | *
|
---|
[fc47885] | 1502 | * @return False on failure or true on success.
|
---|
[25bf215] | 1503 | *
|
---|
| 1504 | */
|
---|
[fc47885] | 1505 | bool used_space_insert(as_area_t *area, uintptr_t page, size_t count)
|
---|
[25bf215] | 1506 | {
|
---|
[1d432f9] | 1507 | ASSERT(mutex_locked(&area->lock));
|
---|
[25bf215] | 1508 | ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE));
|
---|
| 1509 | ASSERT(count);
|
---|
[da1bafb] | 1510 |
|
---|
| 1511 | btree_node_t *leaf;
|
---|
| 1512 | size_t pages = (size_t) btree_search(&area->used_space, page, &leaf);
|
---|
[25bf215] | 1513 | if (pages) {
|
---|
| 1514 | /*
|
---|
| 1515 | * We hit the beginning of some used space.
|
---|
| 1516 | */
|
---|
[fc47885] | 1517 | return false;
|
---|
[25bf215] | 1518 | }
|
---|
[da1bafb] | 1519 |
|
---|
[a6cb8cb] | 1520 | if (!leaf->keys) {
|
---|
[da1bafb] | 1521 | btree_insert(&area->used_space, page, (void *) count, leaf);
|
---|
[fc47885] | 1522 | goto success;
|
---|
[a6cb8cb] | 1523 | }
|
---|
[da1bafb] | 1524 |
|
---|
| 1525 | btree_node_t *node = btree_leaf_node_left_neighbour(&area->used_space, leaf);
|
---|
[25bf215] | 1526 | if (node) {
|
---|
[6f4495f5] | 1527 | uintptr_t left_pg = node->key[node->keys - 1];
|
---|
| 1528 | uintptr_t right_pg = leaf->key[0];
|
---|
[98000fb] | 1529 | size_t left_cnt = (size_t) node->value[node->keys - 1];
|
---|
| 1530 | size_t right_cnt = (size_t) leaf->value[0];
|
---|
[25bf215] | 1531 |
|
---|
| 1532 | /*
|
---|
| 1533 | * Examine the possibility that the interval fits
|
---|
| 1534 | * somewhere between the rightmost interval of
|
---|
| 1535 | * the left neigbour and the first interval of the leaf.
|
---|
| 1536 | */
|
---|
[da1bafb] | 1537 |
|
---|
[25bf215] | 1538 | if (page >= right_pg) {
|
---|
| 1539 | /* Do nothing. */
|
---|
[b6f3e7e] | 1540 | } else if (overlaps(page, P2SZ(count), left_pg,
|
---|
| 1541 | P2SZ(left_cnt))) {
|
---|
[25bf215] | 1542 | /* The interval intersects with the left interval. */
|
---|
[fc47885] | 1543 | return false;
|
---|
[b6f3e7e] | 1544 | } else if (overlaps(page, P2SZ(count), right_pg,
|
---|
| 1545 | P2SZ(right_cnt))) {
|
---|
[25bf215] | 1546 | /* The interval intersects with the right interval. */
|
---|
[fc47885] | 1547 | return false;
|
---|
[b6f3e7e] | 1548 | } else if ((page == left_pg + P2SZ(left_cnt)) &&
|
---|
| 1549 | (page + P2SZ(count) == right_pg)) {
|
---|
[6f4495f5] | 1550 | /*
|
---|
| 1551 | * The interval can be added by merging the two already
|
---|
| 1552 | * present intervals.
|
---|
| 1553 | */
|
---|
[56789125] | 1554 | node->value[node->keys - 1] += count + right_cnt;
|
---|
[da1bafb] | 1555 | btree_remove(&area->used_space, right_pg, leaf);
|
---|
[fc47885] | 1556 | goto success;
|
---|
[b6f3e7e] | 1557 | } else if (page == left_pg + P2SZ(left_cnt)) {
|
---|
[da1bafb] | 1558 | /*
|
---|
[6f4495f5] | 1559 | * The interval can be added by simply growing the left
|
---|
| 1560 | * interval.
|
---|
| 1561 | */
|
---|
[56789125] | 1562 | node->value[node->keys - 1] += count;
|
---|
[fc47885] | 1563 | goto success;
|
---|
[b6f3e7e] | 1564 | } else if (page + P2SZ(count) == right_pg) {
|
---|
[25bf215] | 1565 | /*
|
---|
[6f4495f5] | 1566 | * The interval can be addded by simply moving base of
|
---|
| 1567 | * the right interval down and increasing its size
|
---|
| 1568 | * accordingly.
|
---|
[25bf215] | 1569 | */
|
---|
[56789125] | 1570 | leaf->value[0] += count;
|
---|
[25bf215] | 1571 | leaf->key[0] = page;
|
---|
[fc47885] | 1572 | goto success;
|
---|
[25bf215] | 1573 | } else {
|
---|
| 1574 | /*
|
---|
| 1575 | * The interval is between both neigbouring intervals,
|
---|
| 1576 | * but cannot be merged with any of them.
|
---|
| 1577 | */
|
---|
[da1bafb] | 1578 | btree_insert(&area->used_space, page, (void *) count,
|
---|
[6f4495f5] | 1579 | leaf);
|
---|
[fc47885] | 1580 | goto success;
|
---|
[25bf215] | 1581 | }
|
---|
| 1582 | } else if (page < leaf->key[0]) {
|
---|
[7f1c620] | 1583 | uintptr_t right_pg = leaf->key[0];
|
---|
[98000fb] | 1584 | size_t right_cnt = (size_t) leaf->value[0];
|
---|
[da1bafb] | 1585 |
|
---|
[25bf215] | 1586 | /*
|
---|
[6f4495f5] | 1587 | * Investigate the border case in which the left neighbour does
|
---|
| 1588 | * not exist but the interval fits from the left.
|
---|
[25bf215] | 1589 | */
|
---|
[da1bafb] | 1590 |
|
---|
[b6f3e7e] | 1591 | if (overlaps(page, P2SZ(count), right_pg, P2SZ(right_cnt))) {
|
---|
[25bf215] | 1592 | /* The interval intersects with the right interval. */
|
---|
[fc47885] | 1593 | return false;
|
---|
[b6f3e7e] | 1594 | } else if (page + P2SZ(count) == right_pg) {
|
---|
[25bf215] | 1595 | /*
|
---|
[6f4495f5] | 1596 | * The interval can be added by moving the base of the
|
---|
| 1597 | * right interval down and increasing its size
|
---|
| 1598 | * accordingly.
|
---|
[25bf215] | 1599 | */
|
---|
| 1600 | leaf->key[0] = page;
|
---|
[56789125] | 1601 | leaf->value[0] += count;
|
---|
[fc47885] | 1602 | goto success;
|
---|
[25bf215] | 1603 | } else {
|
---|
| 1604 | /*
|
---|
| 1605 | * The interval doesn't adjoin with the right interval.
|
---|
| 1606 | * It must be added individually.
|
---|
| 1607 | */
|
---|
[da1bafb] | 1608 | btree_insert(&area->used_space, page, (void *) count,
|
---|
[6f4495f5] | 1609 | leaf);
|
---|
[fc47885] | 1610 | goto success;
|
---|
[25bf215] | 1611 | }
|
---|
| 1612 | }
|
---|
[da1bafb] | 1613 |
|
---|
| 1614 | node = btree_leaf_node_right_neighbour(&area->used_space, leaf);
|
---|
[25bf215] | 1615 | if (node) {
|
---|
[6f4495f5] | 1616 | uintptr_t left_pg = leaf->key[leaf->keys - 1];
|
---|
| 1617 | uintptr_t right_pg = node->key[0];
|
---|
[98000fb] | 1618 | size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
|
---|
| 1619 | size_t right_cnt = (size_t) node->value[0];
|
---|
[25bf215] | 1620 |
|
---|
| 1621 | /*
|
---|
| 1622 | * Examine the possibility that the interval fits
|
---|
| 1623 | * somewhere between the leftmost interval of
|
---|
| 1624 | * the right neigbour and the last interval of the leaf.
|
---|
| 1625 | */
|
---|
[da1bafb] | 1626 |
|
---|
[25bf215] | 1627 | if (page < left_pg) {
|
---|
| 1628 | /* Do nothing. */
|
---|
[b6f3e7e] | 1629 | } else if (overlaps(page, P2SZ(count), left_pg,
|
---|
| 1630 | P2SZ(left_cnt))) {
|
---|
[25bf215] | 1631 | /* The interval intersects with the left interval. */
|
---|
[fc47885] | 1632 | return false;
|
---|
[b6f3e7e] | 1633 | } else if (overlaps(page, P2SZ(count), right_pg,
|
---|
| 1634 | P2SZ(right_cnt))) {
|
---|
[25bf215] | 1635 | /* The interval intersects with the right interval. */
|
---|
[fc47885] | 1636 | return false;
|
---|
[b6f3e7e] | 1637 | } else if ((page == left_pg + P2SZ(left_cnt)) &&
|
---|
| 1638 | (page + P2SZ(count) == right_pg)) {
|
---|
[6f4495f5] | 1639 | /*
|
---|
| 1640 | * The interval can be added by merging the two already
|
---|
| 1641 | * present intervals.
|
---|
[da1bafb] | 1642 | */
|
---|
[56789125] | 1643 | leaf->value[leaf->keys - 1] += count + right_cnt;
|
---|
[da1bafb] | 1644 | btree_remove(&area->used_space, right_pg, node);
|
---|
[fc47885] | 1645 | goto success;
|
---|
[b6f3e7e] | 1646 | } else if (page == left_pg + P2SZ(left_cnt)) {
|
---|
[6f4495f5] | 1647 | /*
|
---|
| 1648 | * The interval can be added by simply growing the left
|
---|
| 1649 | * interval.
|
---|
[da1bafb] | 1650 | */
|
---|
[fc47885] | 1651 | leaf->value[leaf->keys - 1] += count;
|
---|
| 1652 | goto success;
|
---|
[b6f3e7e] | 1653 | } else if (page + P2SZ(count) == right_pg) {
|
---|
[25bf215] | 1654 | /*
|
---|
[6f4495f5] | 1655 | * The interval can be addded by simply moving base of
|
---|
| 1656 | * the right interval down and increasing its size
|
---|
| 1657 | * accordingly.
|
---|
[25bf215] | 1658 | */
|
---|
[56789125] | 1659 | node->value[0] += count;
|
---|
[25bf215] | 1660 | node->key[0] = page;
|
---|
[fc47885] | 1661 | goto success;
|
---|
[25bf215] | 1662 | } else {
|
---|
| 1663 | /*
|
---|
| 1664 | * The interval is between both neigbouring intervals,
|
---|
| 1665 | * but cannot be merged with any of them.
|
---|
| 1666 | */
|
---|
[da1bafb] | 1667 | btree_insert(&area->used_space, page, (void *) count,
|
---|
[6f4495f5] | 1668 | leaf);
|
---|
[fc47885] | 1669 | goto success;
|
---|
[25bf215] | 1670 | }
|
---|
| 1671 | } else if (page >= leaf->key[leaf->keys - 1]) {
|
---|
[7f1c620] | 1672 | uintptr_t left_pg = leaf->key[leaf->keys - 1];
|
---|
[98000fb] | 1673 | size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
|
---|
[da1bafb] | 1674 |
|
---|
[25bf215] | 1675 | /*
|
---|
[6f4495f5] | 1676 | * Investigate the border case in which the right neighbour
|
---|
| 1677 | * does not exist but the interval fits from the right.
|
---|
[25bf215] | 1678 | */
|
---|
[da1bafb] | 1679 |
|
---|
[b6f3e7e] | 1680 | if (overlaps(page, P2SZ(count), left_pg, P2SZ(left_cnt))) {
|
---|
[56789125] | 1681 | /* The interval intersects with the left interval. */
|
---|
[fc47885] | 1682 | return false;
|
---|
[b6f3e7e] | 1683 | } else if (left_pg + P2SZ(left_cnt) == 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 |
|
---|
[b6f3e7e] | 1718 | if (overlaps(page, P2SZ(count), left_pg,
|
---|
| 1719 | P2SZ(left_cnt))) {
|
---|
[6f4495f5] | 1720 | /*
|
---|
| 1721 | * The interval intersects with the left
|
---|
| 1722 | * interval.
|
---|
| 1723 | */
|
---|
[fc47885] | 1724 | return false;
|
---|
[b6f3e7e] | 1725 | } else if (overlaps(page, P2SZ(count), right_pg,
|
---|
| 1726 | P2SZ(right_cnt))) {
|
---|
[6f4495f5] | 1727 | /*
|
---|
| 1728 | * The interval intersects with the right
|
---|
| 1729 | * interval.
|
---|
| 1730 | */
|
---|
[fc47885] | 1731 | return false;
|
---|
[b6f3e7e] | 1732 | } else if ((page == left_pg + P2SZ(left_cnt)) &&
|
---|
| 1733 | (page + P2SZ(count) == 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;
|
---|
[b6f3e7e] | 1741 | } else if (page == left_pg + P2SZ(left_cnt)) {
|
---|
[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;
|
---|
[b6f3e7e] | 1748 | } else if (page + P2SZ(count) == 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) {
|
---|
[b6f3e7e] | 1814 | leaf->key[i] += P2SZ(count);
|
---|
[56789125] | 1815 | leaf->value[i] -= count;
|
---|
[fc47885] | 1816 | goto success;
|
---|
[25bf215] | 1817 | }
|
---|
| 1818 | }
|
---|
[fc47885] | 1819 |
|
---|
[25bf215] | 1820 | goto error;
|
---|
| 1821 | }
|
---|
| 1822 | }
|
---|
[da1bafb] | 1823 |
|
---|
[b6f3e7e] | 1824 | btree_node_t *node = btree_leaf_node_left_neighbour(&area->used_space,
|
---|
| 1825 | leaf);
|
---|
[da1bafb] | 1826 | if ((node) && (page < leaf->key[0])) {
|
---|
[7f1c620] | 1827 | uintptr_t left_pg = node->key[node->keys - 1];
|
---|
[98000fb] | 1828 | size_t left_cnt = (size_t) node->value[node->keys - 1];
|
---|
[da1bafb] | 1829 |
|
---|
[b6f3e7e] | 1830 | if (overlaps(left_pg, P2SZ(left_cnt), page, P2SZ(count))) {
|
---|
| 1831 | if (page + P2SZ(count) == left_pg + P2SZ(left_cnt)) {
|
---|
[25bf215] | 1832 | /*
|
---|
[6f4495f5] | 1833 | * The interval is contained in the rightmost
|
---|
| 1834 | * interval of the left neighbour and can be
|
---|
| 1835 | * removed by updating the size of the bigger
|
---|
| 1836 | * interval.
|
---|
[25bf215] | 1837 | */
|
---|
[56789125] | 1838 | node->value[node->keys - 1] -= count;
|
---|
[fc47885] | 1839 | goto success;
|
---|
[b6f3e7e] | 1840 | } else if (page + P2SZ(count) <
|
---|
| 1841 | left_pg + P2SZ(left_cnt)) {
|
---|
| 1842 | size_t new_cnt;
|
---|
| 1843 |
|
---|
[25bf215] | 1844 | /*
|
---|
[6f4495f5] | 1845 | * The interval is contained in the rightmost
|
---|
| 1846 | * interval of the left neighbour but its
|
---|
| 1847 | * removal requires both updating the size of
|
---|
| 1848 | * the original interval and also inserting a
|
---|
| 1849 | * new interval.
|
---|
[25bf215] | 1850 | */
|
---|
[b6f3e7e] | 1851 | new_cnt = ((left_pg + P2SZ(left_cnt)) -
|
---|
| 1852 | (page + P2SZ(count))) >> PAGE_WIDTH;
|
---|
[56789125] | 1853 | node->value[node->keys - 1] -= count + new_cnt;
|
---|
[da1bafb] | 1854 | btree_insert(&area->used_space, page +
|
---|
[b6f3e7e] | 1855 | P2SZ(count), (void *) new_cnt, leaf);
|
---|
[fc47885] | 1856 | goto success;
|
---|
[25bf215] | 1857 | }
|
---|
| 1858 | }
|
---|
[fc47885] | 1859 |
|
---|
| 1860 | return false;
|
---|
[da1bafb] | 1861 | } else if (page < leaf->key[0])
|
---|
[fc47885] | 1862 | return false;
|
---|
[25bf215] | 1863 |
|
---|
| 1864 | if (page > leaf->key[leaf->keys - 1]) {
|
---|
[7f1c620] | 1865 | uintptr_t left_pg = leaf->key[leaf->keys - 1];
|
---|
[98000fb] | 1866 | size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
|
---|
[da1bafb] | 1867 |
|
---|
[b6f3e7e] | 1868 | if (overlaps(left_pg, P2SZ(left_cnt), page, P2SZ(count))) {
|
---|
| 1869 | if (page + P2SZ(count) == left_pg + P2SZ(left_cnt)) {
|
---|
[25bf215] | 1870 | /*
|
---|
[6f4495f5] | 1871 | * The interval is contained in the rightmost
|
---|
| 1872 | * interval of the leaf and can be removed by
|
---|
| 1873 | * updating the size of the bigger interval.
|
---|
[25bf215] | 1874 | */
|
---|
[56789125] | 1875 | leaf->value[leaf->keys - 1] -= count;
|
---|
[fc47885] | 1876 | goto success;
|
---|
[b6f3e7e] | 1877 | } else if (page + P2SZ(count) < left_pg +
|
---|
| 1878 | P2SZ(left_cnt)) {
|
---|
| 1879 | size_t new_cnt;
|
---|
| 1880 |
|
---|
[25bf215] | 1881 | /*
|
---|
[6f4495f5] | 1882 | * The interval is contained in the rightmost
|
---|
| 1883 | * interval of the leaf but its removal
|
---|
| 1884 | * requires both updating the size of the
|
---|
| 1885 | * original interval and also inserting a new
|
---|
| 1886 | * interval.
|
---|
[25bf215] | 1887 | */
|
---|
[b6f3e7e] | 1888 | new_cnt = ((left_pg + P2SZ(left_cnt)) -
|
---|
| 1889 | (page + P2SZ(count))) >> PAGE_WIDTH;
|
---|
[56789125] | 1890 | leaf->value[leaf->keys - 1] -= count + new_cnt;
|
---|
[da1bafb] | 1891 | btree_insert(&area->used_space, page +
|
---|
[b6f3e7e] | 1892 | P2SZ(count), (void *) new_cnt, leaf);
|
---|
[fc47885] | 1893 | goto success;
|
---|
[25bf215] | 1894 | }
|
---|
| 1895 | }
|
---|
[fc47885] | 1896 |
|
---|
| 1897 | return false;
|
---|
[da1bafb] | 1898 | }
|
---|
[25bf215] | 1899 |
|
---|
| 1900 | /*
|
---|
| 1901 | * The border cases have been already resolved.
|
---|
[fc47885] | 1902 | * Now the interval can be only between intervals of the leaf.
|
---|
[25bf215] | 1903 | */
|
---|
[da1bafb] | 1904 | btree_key_t i;
|
---|
[25bf215] | 1905 | for (i = 1; i < leaf->keys - 1; i++) {
|
---|
| 1906 | if (page < leaf->key[i]) {
|
---|
[7f1c620] | 1907 | uintptr_t left_pg = leaf->key[i - 1];
|
---|
[98000fb] | 1908 | size_t left_cnt = (size_t) leaf->value[i - 1];
|
---|
[da1bafb] | 1909 |
|
---|
[25bf215] | 1910 | /*
|
---|
[6f4495f5] | 1911 | * Now the interval is between intervals corresponding
|
---|
| 1912 | * to (i - 1) and i.
|
---|
[25bf215] | 1913 | */
|
---|
[b6f3e7e] | 1914 | if (overlaps(left_pg, P2SZ(left_cnt), page,
|
---|
| 1915 | P2SZ(count))) {
|
---|
| 1916 | if (page + P2SZ(count) ==
|
---|
| 1917 | left_pg + P2SZ(left_cnt)) {
|
---|
[25bf215] | 1918 | /*
|
---|
[6f4495f5] | 1919 | * The interval is contained in the
|
---|
| 1920 | * interval (i - 1) of the leaf and can
|
---|
| 1921 | * be removed by updating the size of
|
---|
| 1922 | * the bigger interval.
|
---|
[25bf215] | 1923 | */
|
---|
[56789125] | 1924 | leaf->value[i - 1] -= count;
|
---|
[fc47885] | 1925 | goto success;
|
---|
[b6f3e7e] | 1926 | } else if (page + P2SZ(count) <
|
---|
| 1927 | left_pg + P2SZ(left_cnt)) {
|
---|
| 1928 | size_t new_cnt;
|
---|
| 1929 |
|
---|
[25bf215] | 1930 | /*
|
---|
[6f4495f5] | 1931 | * The interval is contained in the
|
---|
| 1932 | * interval (i - 1) of the leaf but its
|
---|
| 1933 | * removal requires both updating the
|
---|
| 1934 | * size of the original interval and
|
---|
[25bf215] | 1935 | * also inserting a new interval.
|
---|
| 1936 | */
|
---|
[b6f3e7e] | 1937 | new_cnt = ((left_pg + P2SZ(left_cnt)) -
|
---|
| 1938 | (page + P2SZ(count))) >>
|
---|
[6f4495f5] | 1939 | PAGE_WIDTH;
|
---|
[56789125] | 1940 | leaf->value[i - 1] -= count + new_cnt;
|
---|
[da1bafb] | 1941 | btree_insert(&area->used_space, page +
|
---|
[b6f3e7e] | 1942 | P2SZ(count), (void *) new_cnt,
|
---|
[6f4495f5] | 1943 | leaf);
|
---|
[fc47885] | 1944 | goto success;
|
---|
[25bf215] | 1945 | }
|
---|
| 1946 | }
|
---|
[fc47885] | 1947 |
|
---|
| 1948 | return false;
|
---|
[25bf215] | 1949 | }
|
---|
| 1950 | }
|
---|
[da1bafb] | 1951 |
|
---|
[25bf215] | 1952 | error:
|
---|
[7e752b2] | 1953 | panic("Inconsistency detected while removing %zu pages of used "
|
---|
| 1954 | "space from %p.", count, (void *) page);
|
---|
[fc47885] | 1955 |
|
---|
| 1956 | success:
|
---|
| 1957 | area->resident -= count;
|
---|
| 1958 | return true;
|
---|
[25bf215] | 1959 | }
|
---|
| 1960 |
|
---|
[df0103f7] | 1961 | /*
|
---|
| 1962 | * Address space related syscalls.
|
---|
| 1963 | */
|
---|
| 1964 |
|
---|
| 1965 | /** Wrapper for as_area_create(). */
|
---|
[96b02eb9] | 1966 | sysarg_t sys_as_area_create(uintptr_t address, size_t size, unsigned int flags)
|
---|
[df0103f7] | 1967 | {
|
---|
[6f4495f5] | 1968 | if (as_area_create(AS, flags | AS_AREA_CACHEABLE, size, address,
|
---|
| 1969 | AS_AREA_ATTR_NONE, &anon_backend, NULL))
|
---|
[96b02eb9] | 1970 | return (sysarg_t) address;
|
---|
[df0103f7] | 1971 | else
|
---|
[96b02eb9] | 1972 | return (sysarg_t) -1;
|
---|
[df0103f7] | 1973 | }
|
---|
| 1974 |
|
---|
[c6e314a] | 1975 | /** Wrapper for as_area_resize(). */
|
---|
[96b02eb9] | 1976 | sysarg_t sys_as_area_resize(uintptr_t address, size_t size, unsigned int flags)
|
---|
[df0103f7] | 1977 | {
|
---|
[96b02eb9] | 1978 | return (sysarg_t) as_area_resize(AS, address, size, 0);
|
---|
[7242a78e] | 1979 | }
|
---|
| 1980 |
|
---|
[c98e6ee] | 1981 | /** Wrapper for as_area_change_flags(). */
|
---|
[96b02eb9] | 1982 | sysarg_t sys_as_area_change_flags(uintptr_t address, unsigned int flags)
|
---|
[c98e6ee] | 1983 | {
|
---|
[96b02eb9] | 1984 | return (sysarg_t) as_area_change_flags(AS, flags, address);
|
---|
[c98e6ee] | 1985 | }
|
---|
| 1986 |
|
---|
[c6e314a] | 1987 | /** Wrapper for as_area_destroy(). */
|
---|
[96b02eb9] | 1988 | sysarg_t sys_as_area_destroy(uintptr_t address)
|
---|
[7242a78e] | 1989 | {
|
---|
[96b02eb9] | 1990 | return (sysarg_t) as_area_destroy(AS, address);
|
---|
[df0103f7] | 1991 | }
|
---|
[b45c443] | 1992 |
|
---|
[0b37882] | 1993 | /** Return pointer to unmapped address space area
|
---|
| 1994 | *
|
---|
| 1995 | * @param base Lowest address bound.
|
---|
| 1996 | * @param size Requested size of the allocation.
|
---|
| 1997 | *
|
---|
| 1998 | * @return Pointer to the beginning of unmapped address space area.
|
---|
| 1999 | *
|
---|
| 2000 | */
|
---|
| 2001 | sysarg_t sys_as_get_unmapped_area(uintptr_t base, size_t size)
|
---|
| 2002 | {
|
---|
| 2003 | if (size == 0)
|
---|
| 2004 | return 0;
|
---|
| 2005 |
|
---|
| 2006 | /*
|
---|
| 2007 | * Make sure we allocate from page-aligned
|
---|
| 2008 | * address. Check for possible overflow in
|
---|
| 2009 | * each step.
|
---|
| 2010 | */
|
---|
| 2011 |
|
---|
| 2012 | size_t pages = SIZE2FRAMES(size);
|
---|
| 2013 | uintptr_t ret = 0;
|
---|
| 2014 |
|
---|
| 2015 | /*
|
---|
| 2016 | * Find the lowest unmapped address aligned on the sz
|
---|
| 2017 | * boundary, not smaller than base and of the required size.
|
---|
| 2018 | */
|
---|
| 2019 |
|
---|
| 2020 | mutex_lock(&AS->lock);
|
---|
| 2021 |
|
---|
| 2022 | /* First check the base address itself */
|
---|
| 2023 | uintptr_t addr = ALIGN_UP(base, PAGE_SIZE);
|
---|
| 2024 | if ((addr >= base) &&
|
---|
| 2025 | (check_area_conflicts(AS, addr, pages, NULL)))
|
---|
| 2026 | ret = addr;
|
---|
| 2027 |
|
---|
| 2028 | /* Eventually check the addresses behind each area */
|
---|
| 2029 | link_t *cur;
|
---|
| 2030 | for (cur = AS->as_area_btree.leaf_head.next;
|
---|
| 2031 | (ret == 0) && (cur != &AS->as_area_btree.leaf_head);
|
---|
| 2032 | cur = cur->next) {
|
---|
| 2033 | btree_node_t *node =
|
---|
| 2034 | list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 2035 |
|
---|
| 2036 | btree_key_t i;
|
---|
| 2037 | for (i = 0; (ret == 0) && (i < node->keys); i++) {
|
---|
[b6f3e7e] | 2038 | uintptr_t addr;
|
---|
| 2039 |
|
---|
[0b37882] | 2040 | as_area_t *area = (as_area_t *) node->value[i];
|
---|
| 2041 |
|
---|
| 2042 | mutex_lock(&area->lock);
|
---|
| 2043 |
|
---|
[b6f3e7e] | 2044 | addr = ALIGN_UP(area->base + P2SZ(area->pages),
|
---|
[0b37882] | 2045 | PAGE_SIZE);
|
---|
| 2046 |
|
---|
| 2047 | if ((addr >= base) && (addr >= area->base) &&
|
---|
| 2048 | (check_area_conflicts(AS, addr, pages, area)))
|
---|
| 2049 | ret = addr;
|
---|
| 2050 |
|
---|
| 2051 | mutex_unlock(&area->lock);
|
---|
| 2052 | }
|
---|
| 2053 | }
|
---|
| 2054 |
|
---|
| 2055 | mutex_unlock(&AS->lock);
|
---|
| 2056 |
|
---|
| 2057 | return (sysarg_t) ret;
|
---|
| 2058 | }
|
---|
| 2059 |
|
---|
[336db295] | 2060 | /** Get list of adress space areas.
|
---|
| 2061 | *
|
---|
[da1bafb] | 2062 | * @param as Address space.
|
---|
| 2063 | * @param obuf Place to save pointer to returned buffer.
|
---|
| 2064 | * @param osize Place to save size of returned buffer.
|
---|
| 2065 | *
|
---|
[336db295] | 2066 | */
|
---|
| 2067 | void as_get_area_info(as_t *as, as_area_info_t **obuf, size_t *osize)
|
---|
| 2068 | {
|
---|
| 2069 | mutex_lock(&as->lock);
|
---|
[da1bafb] | 2070 |
|
---|
[336db295] | 2071 | /* First pass, count number of areas. */
|
---|
[da1bafb] | 2072 |
|
---|
| 2073 | size_t area_cnt = 0;
|
---|
| 2074 | link_t *cur;
|
---|
| 2075 |
|
---|
[336db295] | 2076 | for (cur = as->as_area_btree.leaf_head.next;
|
---|
| 2077 | cur != &as->as_area_btree.leaf_head; cur = cur->next) {
|
---|
[da1bafb] | 2078 | btree_node_t *node =
|
---|
| 2079 | list_get_instance(cur, btree_node_t, leaf_link);
|
---|
[336db295] | 2080 | area_cnt += node->keys;
|
---|
| 2081 | }
|
---|
[da1bafb] | 2082 |
|
---|
| 2083 | size_t isize = area_cnt * sizeof(as_area_info_t);
|
---|
| 2084 | as_area_info_t *info = malloc(isize, 0);
|
---|
| 2085 |
|
---|
[336db295] | 2086 | /* Second pass, record data. */
|
---|
[da1bafb] | 2087 |
|
---|
| 2088 | size_t area_idx = 0;
|
---|
| 2089 |
|
---|
[336db295] | 2090 | for (cur = as->as_area_btree.leaf_head.next;
|
---|
| 2091 | cur != &as->as_area_btree.leaf_head; cur = cur->next) {
|
---|
[da1bafb] | 2092 | btree_node_t *node =
|
---|
| 2093 | list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 2094 | btree_key_t i;
|
---|
| 2095 |
|
---|
[336db295] | 2096 | for (i = 0; i < node->keys; i++) {
|
---|
| 2097 | as_area_t *area = node->value[i];
|
---|
[da1bafb] | 2098 |
|
---|
[336db295] | 2099 | ASSERT(area_idx < area_cnt);
|
---|
| 2100 | mutex_lock(&area->lock);
|
---|
[da1bafb] | 2101 |
|
---|
[336db295] | 2102 | info[area_idx].start_addr = area->base;
|
---|
[b6f3e7e] | 2103 | info[area_idx].size = P2SZ(area->pages);
|
---|
[336db295] | 2104 | info[area_idx].flags = area->flags;
|
---|
| 2105 | ++area_idx;
|
---|
[da1bafb] | 2106 |
|
---|
[336db295] | 2107 | mutex_unlock(&area->lock);
|
---|
| 2108 | }
|
---|
| 2109 | }
|
---|
[da1bafb] | 2110 |
|
---|
[336db295] | 2111 | mutex_unlock(&as->lock);
|
---|
[da1bafb] | 2112 |
|
---|
[336db295] | 2113 | *obuf = info;
|
---|
| 2114 | *osize = isize;
|
---|
| 2115 | }
|
---|
| 2116 |
|
---|
[64c2ad5] | 2117 | /** Print out information about address space.
|
---|
| 2118 | *
|
---|
[da1bafb] | 2119 | * @param as Address space.
|
---|
| 2120 | *
|
---|
[64c2ad5] | 2121 | */
|
---|
| 2122 | void as_print(as_t *as)
|
---|
| 2123 | {
|
---|
| 2124 | mutex_lock(&as->lock);
|
---|
| 2125 |
|
---|
[0b37882] | 2126 | /* Print out info about address space areas */
|
---|
[64c2ad5] | 2127 | link_t *cur;
|
---|
[6f4495f5] | 2128 | for (cur = as->as_area_btree.leaf_head.next;
|
---|
| 2129 | cur != &as->as_area_btree.leaf_head; cur = cur->next) {
|
---|
[da1bafb] | 2130 | btree_node_t *node
|
---|
| 2131 | = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 2132 | btree_key_t i;
|
---|
[64c2ad5] | 2133 |
|
---|
| 2134 | for (i = 0; i < node->keys; i++) {
|
---|
[7ba7c6d] | 2135 | as_area_t *area = node->value[i];
|
---|
[da1bafb] | 2136 |
|
---|
[64c2ad5] | 2137 | mutex_lock(&area->lock);
|
---|
[7e752b2] | 2138 | printf("as_area: %p, base=%p, pages=%zu"
|
---|
| 2139 | " (%p - %p)\n", area, (void *) area->base,
|
---|
| 2140 | area->pages, (void *) area->base,
|
---|
[b6f3e7e] | 2141 | (void *) (area->base + P2SZ(area->pages)));
|
---|
[64c2ad5] | 2142 | mutex_unlock(&area->lock);
|
---|
| 2143 | }
|
---|
| 2144 | }
|
---|
| 2145 |
|
---|
| 2146 | mutex_unlock(&as->lock);
|
---|
| 2147 | }
|
---|
| 2148 |
|
---|
[cc73a8a1] | 2149 | /** @}
|
---|
[b45c443] | 2150 | */
|
---|