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