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