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