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