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