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