[20d50a1] | 1 | /*
|
---|
| 2 | * Copyright (C) 2001-2006 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[cc73a8a1] | 29 | /** @addtogroup genericmm
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[9179d0a] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[9179d0a] | 35 | * @brief Address space related functions.
|
---|
| 36 | *
|
---|
[20d50a1] | 37 | * This file contains address space manipulation functions.
|
---|
| 38 | * Roughly speaking, this is a higher-level client of
|
---|
| 39 | * Virtual Address Translation (VAT) subsystem.
|
---|
[9179d0a] | 40 | *
|
---|
| 41 | * Functionality provided by this file allows one to
|
---|
[cc73a8a1] | 42 | * create address spaces and create, resize and share
|
---|
[9179d0a] | 43 | * address space areas.
|
---|
| 44 | *
|
---|
| 45 | * @see page.c
|
---|
| 46 | *
|
---|
[20d50a1] | 47 | */
|
---|
| 48 |
|
---|
| 49 | #include <mm/as.h>
|
---|
[ef67bab] | 50 | #include <arch/mm/as.h>
|
---|
[20d50a1] | 51 | #include <mm/page.h>
|
---|
| 52 | #include <mm/frame.h>
|
---|
[085d973] | 53 | #include <mm/slab.h>
|
---|
[20d50a1] | 54 | #include <mm/tlb.h>
|
---|
| 55 | #include <arch/mm/page.h>
|
---|
| 56 | #include <genarch/mm/page_pt.h>
|
---|
[2802767] | 57 | #include <genarch/mm/page_ht.h>
|
---|
[4512d7e] | 58 | #include <mm/asid.h>
|
---|
[20d50a1] | 59 | #include <arch/mm/asid.h>
|
---|
| 60 | #include <synch/spinlock.h>
|
---|
[1068f6a] | 61 | #include <synch/mutex.h>
|
---|
[5c9a08b] | 62 | #include <adt/list.h>
|
---|
[252127e] | 63 | #include <adt/btree.h>
|
---|
[df0103f7] | 64 | #include <proc/task.h>
|
---|
[e3c762cd] | 65 | #include <proc/thread.h>
|
---|
[20d50a1] | 66 | #include <arch/asm.h>
|
---|
[df0103f7] | 67 | #include <panic.h>
|
---|
[20d50a1] | 68 | #include <debug.h>
|
---|
[df0103f7] | 69 | #include <print.h>
|
---|
[20d50a1] | 70 | #include <memstr.h>
|
---|
[5a7d9d1] | 71 | #include <macros.h>
|
---|
[20d50a1] | 72 | #include <arch.h>
|
---|
[df0103f7] | 73 | #include <errno.h>
|
---|
| 74 | #include <config.h>
|
---|
[25bf215] | 75 | #include <align.h>
|
---|
[df0103f7] | 76 | #include <arch/types.h>
|
---|
| 77 | #include <typedefs.h>
|
---|
[e3c762cd] | 78 | #include <syscall/copy.h>
|
---|
| 79 | #include <arch/interrupt.h>
|
---|
[20d50a1] | 80 |
|
---|
[cc73a8a1] | 81 | /**
|
---|
| 82 | * Each architecture decides what functions will be used to carry out
|
---|
| 83 | * address space operations such as creating or locking page tables.
|
---|
| 84 | */
|
---|
[ef67bab] | 85 | as_operations_t *as_operations = NULL;
|
---|
[20d50a1] | 86 |
|
---|
[57da95c] | 87 | /**
|
---|
| 88 | * Slab for as_t objects.
|
---|
| 89 | */
|
---|
| 90 | static slab_cache_t *as_slab;
|
---|
| 91 |
|
---|
[47800e0] | 92 | /** This lock protects inactive_as_with_asid_head list. It must be acquired before as_t mutex. */
|
---|
| 93 | SPINLOCK_INITIALIZE(inactive_as_with_asid_lock);
|
---|
[7e4e532] | 94 |
|
---|
| 95 | /**
|
---|
| 96 | * This list contains address spaces that are not active on any
|
---|
| 97 | * processor and that have valid ASID.
|
---|
| 98 | */
|
---|
| 99 | LIST_INITIALIZE(inactive_as_with_asid_head);
|
---|
| 100 |
|
---|
[071a8ae6] | 101 | /** Kernel address space. */
|
---|
| 102 | as_t *AS_KERNEL = NULL;
|
---|
| 103 |
|
---|
[df0103f7] | 104 | static int area_flags_to_page_flags(int aflags);
|
---|
[7f1c620] | 105 | static as_area_t *find_area_and_lock(as_t *as, uintptr_t va);
|
---|
| 106 | static bool check_area_conflicts(as_t *as, uintptr_t va, size_t size, as_area_t *avoid_area);
|
---|
[8182031] | 107 | static void sh_info_remove_reference(share_info_t *sh_info);
|
---|
[20d50a1] | 108 |
|
---|
[29b2bbf] | 109 | static int as_constructor(void *obj, int flags)
|
---|
| 110 | {
|
---|
| 111 | as_t *as = (as_t *) obj;
|
---|
| 112 | int rc;
|
---|
| 113 |
|
---|
| 114 | link_initialize(&as->inactive_as_with_asid_link);
|
---|
| 115 | mutex_initialize(&as->lock);
|
---|
| 116 |
|
---|
| 117 | rc = as_constructor_arch(as, flags);
|
---|
| 118 |
|
---|
| 119 | return rc;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | static int as_destructor(void *obj)
|
---|
| 123 | {
|
---|
| 124 | as_t *as = (as_t *) obj;
|
---|
| 125 |
|
---|
| 126 | return as_destructor_arch(as);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[ef67bab] | 129 | /** Initialize address space subsystem. */
|
---|
| 130 | void as_init(void)
|
---|
| 131 | {
|
---|
| 132 | as_arch_init();
|
---|
[57da95c] | 133 |
|
---|
[29b2bbf] | 134 | as_slab = slab_cache_create("as_slab", sizeof(as_t), 0,
|
---|
| 135 | as_constructor, as_destructor, SLAB_CACHE_MAGDEFERRED);
|
---|
[57da95c] | 136 |
|
---|
[8e1ea655] | 137 | AS_KERNEL = as_create(FLAG_AS_KERNEL);
|
---|
[125e944] | 138 | if (!AS_KERNEL)
|
---|
| 139 | panic("can't create kernel address space\n");
|
---|
| 140 |
|
---|
[ef67bab] | 141 | }
|
---|
| 142 |
|
---|
[071a8ae6] | 143 | /** Create address space.
|
---|
| 144 | *
|
---|
| 145 | * @param flags Flags that influence way in wich the address space is created.
|
---|
| 146 | */
|
---|
[ef67bab] | 147 | as_t *as_create(int flags)
|
---|
[20d50a1] | 148 | {
|
---|
| 149 | as_t *as;
|
---|
| 150 |
|
---|
[57da95c] | 151 | as = (as_t *) slab_alloc(as_slab, 0);
|
---|
[29b2bbf] | 152 | (void) as_create_arch(as, 0);
|
---|
| 153 |
|
---|
[252127e] | 154 | btree_create(&as->as_area_btree);
|
---|
[bb68433] | 155 |
|
---|
| 156 | if (flags & FLAG_AS_KERNEL)
|
---|
| 157 | as->asid = ASID_KERNEL;
|
---|
| 158 | else
|
---|
| 159 | as->asid = ASID_INVALID;
|
---|
| 160 |
|
---|
[482826d] | 161 | as->refcount = 0;
|
---|
[47800e0] | 162 | as->cpu_refcount = 0;
|
---|
[bb68433] | 163 | as->page_table = page_table_create(flags);
|
---|
[20d50a1] | 164 |
|
---|
| 165 | return as;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[482826d] | 168 | /** Destroy adress space.
|
---|
| 169 | *
|
---|
| 170 | * When there are no tasks referencing this address space (i.e. its refcount is zero),
|
---|
| 171 | * the address space can be destroyed.
|
---|
| 172 | */
|
---|
| 173 | void as_destroy(as_t *as)
|
---|
[5be1923] | 174 | {
|
---|
[482826d] | 175 | ipl_t ipl;
|
---|
[6f9a9bc] | 176 | bool cond;
|
---|
[482826d] | 177 |
|
---|
| 178 | ASSERT(as->refcount == 0);
|
---|
| 179 |
|
---|
| 180 | /*
|
---|
| 181 | * Since there is no reference to this area,
|
---|
| 182 | * it is safe not to lock its mutex.
|
---|
| 183 | */
|
---|
| 184 | ipl = interrupts_disable();
|
---|
| 185 | spinlock_lock(&inactive_as_with_asid_lock);
|
---|
[31e8ddd] | 186 | if (as->asid != ASID_INVALID && as != AS_KERNEL) {
|
---|
[6f9a9bc] | 187 | if (as != AS && as->cpu_refcount == 0)
|
---|
[31e8ddd] | 188 | list_remove(&as->inactive_as_with_asid_link);
|
---|
[482826d] | 189 | asid_put(as->asid);
|
---|
| 190 | }
|
---|
| 191 | spinlock_unlock(&inactive_as_with_asid_lock);
|
---|
| 192 |
|
---|
| 193 | /*
|
---|
| 194 | * Destroy address space areas of the address space.
|
---|
[8440473] | 195 | * The B+tree must be walked carefully because it is
|
---|
[6f9a9bc] | 196 | * also being destroyed.
|
---|
[482826d] | 197 | */
|
---|
[6f9a9bc] | 198 | for (cond = true; cond; ) {
|
---|
[482826d] | 199 | btree_node_t *node;
|
---|
[6f9a9bc] | 200 |
|
---|
| 201 | ASSERT(!list_empty(&as->as_area_btree.leaf_head));
|
---|
| 202 | node = list_get_instance(as->as_area_btree.leaf_head.next, btree_node_t, leaf_link);
|
---|
| 203 |
|
---|
| 204 | if ((cond = node->keys)) {
|
---|
| 205 | as_area_destroy(as, node->key[0]);
|
---|
| 206 | }
|
---|
[482826d] | 207 | }
|
---|
[f8d069e8] | 208 |
|
---|
[152b2b0] | 209 | btree_destroy(&as->as_area_btree);
|
---|
[482826d] | 210 | page_table_destroy(as->page_table);
|
---|
[5be1923] | 211 |
|
---|
[482826d] | 212 | interrupts_restore(ipl);
|
---|
| 213 |
|
---|
[57da95c] | 214 | slab_free(as_slab, as);
|
---|
[5be1923] | 215 | }
|
---|
| 216 |
|
---|
[20d50a1] | 217 | /** Create address space area of common attributes.
|
---|
| 218 | *
|
---|
| 219 | * The created address space area is added to the target address space.
|
---|
| 220 | *
|
---|
| 221 | * @param as Target address space.
|
---|
[a9e8b39] | 222 | * @param flags Flags of the area memory.
|
---|
[37e7d2b9] | 223 | * @param size Size of area.
|
---|
[20d50a1] | 224 | * @param base Base address of area.
|
---|
[a9e8b39] | 225 | * @param attrs Attributes of the area.
|
---|
[8182031] | 226 | * @param backend Address space area backend. NULL if no backend is used.
|
---|
| 227 | * @param backend_data NULL or a pointer to an array holding two void *.
|
---|
[20d50a1] | 228 | *
|
---|
| 229 | * @return Address space area on success or NULL on failure.
|
---|
| 230 | */
|
---|
[7f1c620] | 231 | as_area_t *as_area_create(as_t *as, int flags, size_t size, uintptr_t base, int attrs,
|
---|
[0ee077ee] | 232 | mem_backend_t *backend, mem_backend_data_t *backend_data)
|
---|
[20d50a1] | 233 | {
|
---|
| 234 | ipl_t ipl;
|
---|
| 235 | as_area_t *a;
|
---|
| 236 |
|
---|
| 237 | if (base % PAGE_SIZE)
|
---|
[37e7d2b9] | 238 | return NULL;
|
---|
| 239 |
|
---|
[dbbeb26] | 240 | if (!size)
|
---|
| 241 | return NULL;
|
---|
| 242 |
|
---|
[37e7d2b9] | 243 | /* Writeable executable areas are not supported. */
|
---|
| 244 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
|
---|
| 245 | return NULL;
|
---|
[20d50a1] | 246 |
|
---|
| 247 | ipl = interrupts_disable();
|
---|
[1068f6a] | 248 | mutex_lock(&as->lock);
|
---|
[20d50a1] | 249 |
|
---|
[37e7d2b9] | 250 | if (!check_area_conflicts(as, base, size, NULL)) {
|
---|
[1068f6a] | 251 | mutex_unlock(&as->lock);
|
---|
[37e7d2b9] | 252 | interrupts_restore(ipl);
|
---|
| 253 | return NULL;
|
---|
| 254 | }
|
---|
[20d50a1] | 255 |
|
---|
[bb68433] | 256 | a = (as_area_t *) malloc(sizeof(as_area_t), 0);
|
---|
| 257 |
|
---|
[1068f6a] | 258 | mutex_initialize(&a->lock);
|
---|
[bb68433] | 259 |
|
---|
[0ee077ee] | 260 | a->as = as;
|
---|
[c23502d] | 261 | a->flags = flags;
|
---|
[a9e8b39] | 262 | a->attributes = attrs;
|
---|
[37e7d2b9] | 263 | a->pages = SIZE2FRAMES(size);
|
---|
[bb68433] | 264 | a->base = base;
|
---|
[8182031] | 265 | a->sh_info = NULL;
|
---|
| 266 | a->backend = backend;
|
---|
[0ee077ee] | 267 | if (backend_data)
|
---|
| 268 | a->backend_data = *backend_data;
|
---|
| 269 | else
|
---|
[7f1c620] | 270 | memsetb((uintptr_t) &a->backend_data, sizeof(a->backend_data), 0);
|
---|
[0ee077ee] | 271 |
|
---|
[25bf215] | 272 | btree_create(&a->used_space);
|
---|
[bb68433] | 273 |
|
---|
[252127e] | 274 | btree_insert(&as->as_area_btree, base, (void *) a, NULL);
|
---|
[20d50a1] | 275 |
|
---|
[1068f6a] | 276 | mutex_unlock(&as->lock);
|
---|
[20d50a1] | 277 | interrupts_restore(ipl);
|
---|
[f9425006] | 278 |
|
---|
[20d50a1] | 279 | return a;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[df0103f7] | 282 | /** Find address space area and change it.
|
---|
| 283 | *
|
---|
| 284 | * @param as Address space.
|
---|
| 285 | * @param address Virtual address belonging to the area to be changed. Must be page-aligned.
|
---|
| 286 | * @param size New size of the virtual memory block starting at address.
|
---|
| 287 | * @param flags Flags influencing the remap operation. Currently unused.
|
---|
| 288 | *
|
---|
[7242a78e] | 289 | * @return Zero on success or a value from @ref errno.h otherwise.
|
---|
[df0103f7] | 290 | */
|
---|
[7f1c620] | 291 | int as_area_resize(as_t *as, uintptr_t address, size_t size, int flags)
|
---|
[df0103f7] | 292 | {
|
---|
[7242a78e] | 293 | as_area_t *area;
|
---|
[df0103f7] | 294 | ipl_t ipl;
|
---|
| 295 | size_t pages;
|
---|
| 296 |
|
---|
| 297 | ipl = interrupts_disable();
|
---|
[1068f6a] | 298 | mutex_lock(&as->lock);
|
---|
[df0103f7] | 299 |
|
---|
| 300 | /*
|
---|
| 301 | * Locate the area.
|
---|
| 302 | */
|
---|
| 303 | area = find_area_and_lock(as, address);
|
---|
| 304 | if (!area) {
|
---|
[1068f6a] | 305 | mutex_unlock(&as->lock);
|
---|
[df0103f7] | 306 | interrupts_restore(ipl);
|
---|
[7242a78e] | 307 | return ENOENT;
|
---|
[df0103f7] | 308 | }
|
---|
| 309 |
|
---|
[0ee077ee] | 310 | if (area->backend == &phys_backend) {
|
---|
[df0103f7] | 311 | /*
|
---|
| 312 | * Remapping of address space areas associated
|
---|
| 313 | * with memory mapped devices is not supported.
|
---|
| 314 | */
|
---|
[1068f6a] | 315 | mutex_unlock(&area->lock);
|
---|
| 316 | mutex_unlock(&as->lock);
|
---|
[df0103f7] | 317 | interrupts_restore(ipl);
|
---|
[7242a78e] | 318 | return ENOTSUP;
|
---|
[df0103f7] | 319 | }
|
---|
[8182031] | 320 | if (area->sh_info) {
|
---|
| 321 | /*
|
---|
| 322 | * Remapping of shared address space areas
|
---|
| 323 | * is not supported.
|
---|
| 324 | */
|
---|
| 325 | mutex_unlock(&area->lock);
|
---|
| 326 | mutex_unlock(&as->lock);
|
---|
| 327 | interrupts_restore(ipl);
|
---|
| 328 | return ENOTSUP;
|
---|
| 329 | }
|
---|
[df0103f7] | 330 |
|
---|
| 331 | pages = SIZE2FRAMES((address - area->base) + size);
|
---|
| 332 | if (!pages) {
|
---|
| 333 | /*
|
---|
| 334 | * Zero size address space areas are not allowed.
|
---|
| 335 | */
|
---|
[1068f6a] | 336 | mutex_unlock(&area->lock);
|
---|
| 337 | mutex_unlock(&as->lock);
|
---|
[df0103f7] | 338 | interrupts_restore(ipl);
|
---|
[7242a78e] | 339 | return EPERM;
|
---|
[df0103f7] | 340 | }
|
---|
| 341 |
|
---|
| 342 | if (pages < area->pages) {
|
---|
[56789125] | 343 | bool cond;
|
---|
[7f1c620] | 344 | uintptr_t start_free = area->base + pages*PAGE_SIZE;
|
---|
[df0103f7] | 345 |
|
---|
| 346 | /*
|
---|
| 347 | * Shrinking the area.
|
---|
| 348 | * No need to check for overlaps.
|
---|
| 349 | */
|
---|
| 350 |
|
---|
[5552d60] | 351 | /*
|
---|
| 352 | * Start TLB shootdown sequence.
|
---|
| 353 | */
|
---|
| 354 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
|
---|
| 355 |
|
---|
[56789125] | 356 | /*
|
---|
| 357 | * Remove frames belonging to used space starting from
|
---|
| 358 | * the highest addresses downwards until an overlap with
|
---|
| 359 | * the resized address space area is found. Note that this
|
---|
| 360 | * is also the right way to remove part of the used_space
|
---|
| 361 | * B+tree leaf list.
|
---|
| 362 | */
|
---|
| 363 | for (cond = true; cond;) {
|
---|
| 364 | btree_node_t *node;
|
---|
| 365 |
|
---|
| 366 | ASSERT(!list_empty(&area->used_space.leaf_head));
|
---|
| 367 | node = list_get_instance(area->used_space.leaf_head.prev, btree_node_t, leaf_link);
|
---|
| 368 | if ((cond = (bool) node->keys)) {
|
---|
[7f1c620] | 369 | uintptr_t b = node->key[node->keys - 1];
|
---|
[56789125] | 370 | count_t c = (count_t) node->value[node->keys - 1];
|
---|
| 371 | int i = 0;
|
---|
| 372 |
|
---|
| 373 | if (overlaps(b, c*PAGE_SIZE, area->base, pages*PAGE_SIZE)) {
|
---|
| 374 |
|
---|
| 375 | if (b + c*PAGE_SIZE <= start_free) {
|
---|
| 376 | /*
|
---|
| 377 | * The whole interval fits completely
|
---|
| 378 | * in the resized address space area.
|
---|
| 379 | */
|
---|
| 380 | break;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | /*
|
---|
| 384 | * Part of the interval corresponding to b and c
|
---|
| 385 | * overlaps with the resized address space area.
|
---|
| 386 | */
|
---|
| 387 |
|
---|
| 388 | cond = false; /* we are almost done */
|
---|
| 389 | i = (start_free - b) >> PAGE_WIDTH;
|
---|
| 390 | if (!used_space_remove(area, start_free, c - i))
|
---|
[f1d1f5d3] | 391 | panic("Could not remove used space.\n");
|
---|
[56789125] | 392 | } else {
|
---|
| 393 | /*
|
---|
| 394 | * The interval of used space can be completely removed.
|
---|
| 395 | */
|
---|
| 396 | if (!used_space_remove(area, b, c))
|
---|
| 397 | panic("Could not remove used space.\n");
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | for (; i < c; i++) {
|
---|
| 401 | pte_t *pte;
|
---|
| 402 |
|
---|
| 403 | page_table_lock(as, false);
|
---|
| 404 | pte = page_mapping_find(as, b + i*PAGE_SIZE);
|
---|
| 405 | ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte));
|
---|
[0ee077ee] | 406 | if (area->backend && area->backend->frame_free) {
|
---|
| 407 | area->backend->frame_free(area,
|
---|
[8182031] | 408 | b + i*PAGE_SIZE, PTE_GET_FRAME(pte));
|
---|
| 409 | }
|
---|
[56789125] | 410 | page_mapping_remove(as, b + i*PAGE_SIZE);
|
---|
| 411 | page_table_unlock(as, false);
|
---|
| 412 | }
|
---|
[df0103f7] | 413 | }
|
---|
| 414 | }
|
---|
[5552d60] | 415 |
|
---|
[df0103f7] | 416 | /*
|
---|
[5552d60] | 417 | * Finish TLB shootdown sequence.
|
---|
[df0103f7] | 418 | */
|
---|
[8440473] | 419 | tlb_invalidate_pages(as->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
|
---|
[df0103f7] | 420 | tlb_shootdown_finalize();
|
---|
[f1d1f5d3] | 421 |
|
---|
| 422 | /*
|
---|
| 423 | * Invalidate software translation caches (e.g. TSB on sparc64).
|
---|
| 424 | */
|
---|
| 425 | as_invalidate_translation_cache(as, area->base + pages*PAGE_SIZE, area->pages - pages);
|
---|
[df0103f7] | 426 | } else {
|
---|
| 427 | /*
|
---|
| 428 | * Growing the area.
|
---|
| 429 | * Check for overlaps with other address space areas.
|
---|
| 430 | */
|
---|
| 431 | if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) {
|
---|
[1068f6a] | 432 | mutex_unlock(&area->lock);
|
---|
| 433 | mutex_unlock(&as->lock);
|
---|
[df0103f7] | 434 | interrupts_restore(ipl);
|
---|
[7242a78e] | 435 | return EADDRNOTAVAIL;
|
---|
[df0103f7] | 436 | }
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | area->pages = pages;
|
---|
| 440 |
|
---|
[1068f6a] | 441 | mutex_unlock(&area->lock);
|
---|
| 442 | mutex_unlock(&as->lock);
|
---|
[df0103f7] | 443 | interrupts_restore(ipl);
|
---|
| 444 |
|
---|
[7242a78e] | 445 | return 0;
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | /** Destroy address space area.
|
---|
| 449 | *
|
---|
| 450 | * @param as Address space.
|
---|
| 451 | * @param address Address withing the area to be deleted.
|
---|
| 452 | *
|
---|
| 453 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
| 454 | */
|
---|
[7f1c620] | 455 | int as_area_destroy(as_t *as, uintptr_t address)
|
---|
[7242a78e] | 456 | {
|
---|
| 457 | as_area_t *area;
|
---|
[7f1c620] | 458 | uintptr_t base;
|
---|
[f8d069e8] | 459 | link_t *cur;
|
---|
[7242a78e] | 460 | ipl_t ipl;
|
---|
| 461 |
|
---|
| 462 | ipl = interrupts_disable();
|
---|
[1068f6a] | 463 | mutex_lock(&as->lock);
|
---|
[7242a78e] | 464 |
|
---|
| 465 | area = find_area_and_lock(as, address);
|
---|
| 466 | if (!area) {
|
---|
[1068f6a] | 467 | mutex_unlock(&as->lock);
|
---|
[7242a78e] | 468 | interrupts_restore(ipl);
|
---|
| 469 | return ENOENT;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
[56789125] | 472 | base = area->base;
|
---|
| 473 |
|
---|
[5552d60] | 474 | /*
|
---|
| 475 | * Start TLB shootdown sequence.
|
---|
| 476 | */
|
---|
[f1d1f5d3] | 477 | tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base, area->pages);
|
---|
[5552d60] | 478 |
|
---|
[567807b1] | 479 | /*
|
---|
| 480 | * Visit only the pages mapped by used_space B+tree.
|
---|
| 481 | */
|
---|
[f8d069e8] | 482 | for (cur = area->used_space.leaf_head.next; cur != &area->used_space.leaf_head; cur = cur->next) {
|
---|
[567807b1] | 483 | btree_node_t *node;
|
---|
[f8d069e8] | 484 | int i;
|
---|
[56789125] | 485 |
|
---|
[f8d069e8] | 486 | node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 487 | for (i = 0; i < node->keys; i++) {
|
---|
[7f1c620] | 488 | uintptr_t b = node->key[i];
|
---|
[f8d069e8] | 489 | count_t j;
|
---|
[567807b1] | 490 | pte_t *pte;
|
---|
[56789125] | 491 |
|
---|
[f8d069e8] | 492 | for (j = 0; j < (count_t) node->value[i]; j++) {
|
---|
[567807b1] | 493 | page_table_lock(as, false);
|
---|
[f8d069e8] | 494 | pte = page_mapping_find(as, b + j*PAGE_SIZE);
|
---|
[567807b1] | 495 | ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte));
|
---|
[0ee077ee] | 496 | if (area->backend && area->backend->frame_free) {
|
---|
| 497 | area->backend->frame_free(area,
|
---|
[f8d069e8] | 498 | b + j*PAGE_SIZE, PTE_GET_FRAME(pte));
|
---|
[56789125] | 499 | }
|
---|
[f1d1f5d3] | 500 | page_mapping_remove(as, b + j*PAGE_SIZE);
|
---|
[567807b1] | 501 | page_table_unlock(as, false);
|
---|
[7242a78e] | 502 | }
|
---|
| 503 | }
|
---|
| 504 | }
|
---|
[56789125] | 505 |
|
---|
[7242a78e] | 506 | /*
|
---|
[5552d60] | 507 | * Finish TLB shootdown sequence.
|
---|
[7242a78e] | 508 | */
|
---|
[f1d1f5d3] | 509 | tlb_invalidate_pages(as->asid, area->base, area->pages);
|
---|
[7242a78e] | 510 | tlb_shootdown_finalize();
|
---|
[5552d60] | 511 |
|
---|
[f1d1f5d3] | 512 | /*
|
---|
| 513 | * Invalidate potential software translation caches (e.g. TSB on sparc64).
|
---|
| 514 | */
|
---|
| 515 | as_invalidate_translation_cache(as, area->base, area->pages);
|
---|
| 516 |
|
---|
[5552d60] | 517 | btree_destroy(&area->used_space);
|
---|
[7242a78e] | 518 |
|
---|
[8d4f2ae] | 519 | area->attributes |= AS_AREA_ATTR_PARTIAL;
|
---|
[8182031] | 520 |
|
---|
| 521 | if (area->sh_info)
|
---|
| 522 | sh_info_remove_reference(area->sh_info);
|
---|
| 523 |
|
---|
[1068f6a] | 524 | mutex_unlock(&area->lock);
|
---|
[7242a78e] | 525 |
|
---|
| 526 | /*
|
---|
| 527 | * Remove the empty area from address space.
|
---|
| 528 | */
|
---|
[f1d1f5d3] | 529 | btree_remove(&as->as_area_btree, base, NULL);
|
---|
[7242a78e] | 530 |
|
---|
[8d4f2ae] | 531 | free(area);
|
---|
| 532 |
|
---|
[f1d1f5d3] | 533 | mutex_unlock(&as->lock);
|
---|
[7242a78e] | 534 | interrupts_restore(ipl);
|
---|
| 535 | return 0;
|
---|
[df0103f7] | 536 | }
|
---|
| 537 |
|
---|
[8d6bc2d5] | 538 | /** Share address space area with another or the same address space.
|
---|
[df0103f7] | 539 | *
|
---|
[0ee077ee] | 540 | * Address space area mapping is shared with a new address space area.
|
---|
| 541 | * If the source address space area has not been shared so far,
|
---|
| 542 | * a new sh_info is created. The new address space area simply gets the
|
---|
| 543 | * sh_info of the source area. The process of duplicating the
|
---|
| 544 | * mapping is done through the backend share function.
|
---|
[8d6bc2d5] | 545 | *
|
---|
[fd4d8c0] | 546 | * @param src_as Pointer to source address space.
|
---|
[a9e8b39] | 547 | * @param src_base Base address of the source address space area.
|
---|
[fd4d8c0] | 548 | * @param acc_size Expected size of the source area.
|
---|
[46fc2f9] | 549 | * @param dst_as Pointer to destination address space.
|
---|
[fd4d8c0] | 550 | * @param dst_base Target base address.
|
---|
| 551 | * @param dst_flags_mask Destination address space area flags mask.
|
---|
[df0103f7] | 552 | *
|
---|
[d0485c6] | 553 | * @return Zero on success or ENOENT if there is no such task or if there is no
|
---|
| 554 | * such address space area, EPERM if there was a problem in accepting the area
|
---|
| 555 | * or ENOMEM if there was a problem in allocating destination address space
|
---|
| 556 | * area. ENOTSUP is returned if the address space area backend does not support
|
---|
| 557 | * sharing. It can be also returned if the architecture uses virtually indexed
|
---|
| 558 | * caches and the source and destination areas start at pages with different
|
---|
| 559 | * page colors.
|
---|
[df0103f7] | 560 | */
|
---|
[7f1c620] | 561 | int as_area_share(as_t *src_as, uintptr_t src_base, size_t acc_size,
|
---|
| 562 | as_t *dst_as, uintptr_t dst_base, int dst_flags_mask)
|
---|
[df0103f7] | 563 | {
|
---|
| 564 | ipl_t ipl;
|
---|
[a9e8b39] | 565 | int src_flags;
|
---|
| 566 | size_t src_size;
|
---|
| 567 | as_area_t *src_area, *dst_area;
|
---|
[8d6bc2d5] | 568 | share_info_t *sh_info;
|
---|
[0ee077ee] | 569 | mem_backend_t *src_backend;
|
---|
| 570 | mem_backend_data_t src_backend_data;
|
---|
[d6e5cbc] | 571 |
|
---|
[7c23af9] | 572 | ipl = interrupts_disable();
|
---|
[1068f6a] | 573 | mutex_lock(&src_as->lock);
|
---|
[7c23af9] | 574 | src_area = find_area_and_lock(src_as, src_base);
|
---|
[a9e8b39] | 575 | if (!src_area) {
|
---|
[6fa476f7] | 576 | /*
|
---|
| 577 | * Could not find the source address space area.
|
---|
| 578 | */
|
---|
[1068f6a] | 579 | mutex_unlock(&src_as->lock);
|
---|
[6fa476f7] | 580 | interrupts_restore(ipl);
|
---|
| 581 | return ENOENT;
|
---|
| 582 | }
|
---|
[8d6bc2d5] | 583 |
|
---|
[d0485c6] | 584 | #if 0 /* disable the check for now */
|
---|
| 585 | #ifdef CONFIG_VIRT_IDX_CACHE
|
---|
| 586 | if (PAGE_COLOR(src_area->base) != PAGE_COLOR(dst_base)) {
|
---|
| 587 | /*
|
---|
| 588 | * Refuse to create illegal address alias.
|
---|
| 589 | */
|
---|
| 590 | mutex_unlock(&src_area->lock);
|
---|
| 591 | mutex_unlock(&src_as->lock);
|
---|
| 592 | interrupts_restore(ipl);
|
---|
| 593 | return ENOTSUP;
|
---|
| 594 | }
|
---|
| 595 | #endif /* CONFIG_VIRT_IDX_CACHE */
|
---|
| 596 | #endif
|
---|
| 597 |
|
---|
[0ee077ee] | 598 | if (!src_area->backend || !src_area->backend->share) {
|
---|
[8d6bc2d5] | 599 | /*
|
---|
[f47fd19] | 600 | * There is no backend or the backend does not
|
---|
[0ee077ee] | 601 | * know how to share the area.
|
---|
[8d6bc2d5] | 602 | */
|
---|
| 603 | mutex_unlock(&src_area->lock);
|
---|
| 604 | mutex_unlock(&src_as->lock);
|
---|
| 605 | interrupts_restore(ipl);
|
---|
| 606 | return ENOTSUP;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[a9e8b39] | 609 | src_size = src_area->pages * PAGE_SIZE;
|
---|
| 610 | src_flags = src_area->flags;
|
---|
[0ee077ee] | 611 | src_backend = src_area->backend;
|
---|
| 612 | src_backend_data = src_area->backend_data;
|
---|
[1ec1fd8] | 613 |
|
---|
| 614 | /* Share the cacheable flag from the original mapping */
|
---|
| 615 | if (src_flags & AS_AREA_CACHEABLE)
|
---|
| 616 | dst_flags_mask |= AS_AREA_CACHEABLE;
|
---|
| 617 |
|
---|
[76d7305] | 618 | if (src_size != acc_size || (src_flags & dst_flags_mask) != dst_flags_mask) {
|
---|
[8d6bc2d5] | 619 | mutex_unlock(&src_area->lock);
|
---|
| 620 | mutex_unlock(&src_as->lock);
|
---|
[df0103f7] | 621 | interrupts_restore(ipl);
|
---|
| 622 | return EPERM;
|
---|
| 623 | }
|
---|
[8d6bc2d5] | 624 |
|
---|
| 625 | /*
|
---|
| 626 | * Now we are committed to sharing the area.
|
---|
[8440473] | 627 | * First, prepare the area for sharing.
|
---|
[8d6bc2d5] | 628 | * Then it will be safe to unlock it.
|
---|
| 629 | */
|
---|
| 630 | sh_info = src_area->sh_info;
|
---|
| 631 | if (!sh_info) {
|
---|
| 632 | sh_info = (share_info_t *) malloc(sizeof(share_info_t), 0);
|
---|
| 633 | mutex_initialize(&sh_info->lock);
|
---|
| 634 | sh_info->refcount = 2;
|
---|
| 635 | btree_create(&sh_info->pagemap);
|
---|
| 636 | src_area->sh_info = sh_info;
|
---|
| 637 | } else {
|
---|
| 638 | mutex_lock(&sh_info->lock);
|
---|
| 639 | sh_info->refcount++;
|
---|
| 640 | mutex_unlock(&sh_info->lock);
|
---|
| 641 | }
|
---|
| 642 |
|
---|
[0ee077ee] | 643 | src_area->backend->share(src_area);
|
---|
[8d6bc2d5] | 644 |
|
---|
| 645 | mutex_unlock(&src_area->lock);
|
---|
| 646 | mutex_unlock(&src_as->lock);
|
---|
| 647 |
|
---|
[df0103f7] | 648 | /*
|
---|
[a9e8b39] | 649 | * Create copy of the source address space area.
|
---|
| 650 | * The destination area is created with AS_AREA_ATTR_PARTIAL
|
---|
| 651 | * attribute set which prevents race condition with
|
---|
| 652 | * preliminary as_page_fault() calls.
|
---|
[fd4d8c0] | 653 | * The flags of the source area are masked against dst_flags_mask
|
---|
| 654 | * to support sharing in less privileged mode.
|
---|
[df0103f7] | 655 | */
|
---|
[76d7305] | 656 | dst_area = as_area_create(dst_as, dst_flags_mask, src_size, dst_base,
|
---|
[0ee077ee] | 657 | AS_AREA_ATTR_PARTIAL, src_backend, &src_backend_data);
|
---|
[a9e8b39] | 658 | if (!dst_area) {
|
---|
[df0103f7] | 659 | /*
|
---|
| 660 | * Destination address space area could not be created.
|
---|
| 661 | */
|
---|
[8d6bc2d5] | 662 | sh_info_remove_reference(sh_info);
|
---|
| 663 |
|
---|
[df0103f7] | 664 | interrupts_restore(ipl);
|
---|
| 665 | return ENOMEM;
|
---|
| 666 | }
|
---|
| 667 |
|
---|
[a9e8b39] | 668 | /*
|
---|
| 669 | * Now the destination address space area has been
|
---|
| 670 | * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
|
---|
[8d6bc2d5] | 671 | * attribute and set the sh_info.
|
---|
[a9e8b39] | 672 | */
|
---|
[1068f6a] | 673 | mutex_lock(&dst_area->lock);
|
---|
[a9e8b39] | 674 | dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
|
---|
[8d6bc2d5] | 675 | dst_area->sh_info = sh_info;
|
---|
[1068f6a] | 676 | mutex_unlock(&dst_area->lock);
|
---|
[df0103f7] | 677 |
|
---|
| 678 | interrupts_restore(ipl);
|
---|
| 679 |
|
---|
| 680 | return 0;
|
---|
| 681 | }
|
---|
| 682 |
|
---|
[fb84455] | 683 | /** Check access mode for address space area.
|
---|
| 684 | *
|
---|
| 685 | * The address space area must be locked prior to this call.
|
---|
| 686 | *
|
---|
| 687 | * @param area Address space area.
|
---|
| 688 | * @param access Access mode.
|
---|
| 689 | *
|
---|
| 690 | * @return False if access violates area's permissions, true otherwise.
|
---|
| 691 | */
|
---|
| 692 | bool as_area_check_access(as_area_t *area, pf_access_t access)
|
---|
| 693 | {
|
---|
| 694 | int flagmap[] = {
|
---|
| 695 | [PF_ACCESS_READ] = AS_AREA_READ,
|
---|
| 696 | [PF_ACCESS_WRITE] = AS_AREA_WRITE,
|
---|
| 697 | [PF_ACCESS_EXEC] = AS_AREA_EXEC
|
---|
| 698 | };
|
---|
| 699 |
|
---|
| 700 | if (!(area->flags & flagmap[access]))
|
---|
| 701 | return false;
|
---|
| 702 |
|
---|
| 703 | return true;
|
---|
| 704 | }
|
---|
| 705 |
|
---|
[20d50a1] | 706 | /** Handle page fault within the current address space.
|
---|
| 707 | *
|
---|
[8182031] | 708 | * This is the high-level page fault handler. It decides
|
---|
| 709 | * whether the page fault can be resolved by any backend
|
---|
| 710 | * and if so, it invokes the backend to resolve the page
|
---|
| 711 | * fault.
|
---|
| 712 | *
|
---|
[20d50a1] | 713 | * Interrupts are assumed disabled.
|
---|
| 714 | *
|
---|
| 715 | * @param page Faulting page.
|
---|
[567807b1] | 716 | * @param access Access mode that caused the fault (i.e. read/write/exec).
|
---|
[e3c762cd] | 717 | * @param istate Pointer to interrupted state.
|
---|
[20d50a1] | 718 | *
|
---|
[8182031] | 719 | * @return AS_PF_FAULT on page fault, AS_PF_OK on success or AS_PF_DEFER if the
|
---|
| 720 | * fault was caused by copy_to_uspace() or copy_from_uspace().
|
---|
[20d50a1] | 721 | */
|
---|
[7f1c620] | 722 | int as_page_fault(uintptr_t page, pf_access_t access, istate_t *istate)
|
---|
[20d50a1] | 723 | {
|
---|
[2299914] | 724 | pte_t *pte;
|
---|
[d3e7ff4] | 725 | as_area_t *area;
|
---|
[20d50a1] | 726 |
|
---|
[1068f6a] | 727 | if (!THREAD)
|
---|
[8182031] | 728 | return AS_PF_FAULT;
|
---|
[1068f6a] | 729 |
|
---|
[20d50a1] | 730 | ASSERT(AS);
|
---|
[2299914] | 731 |
|
---|
[1068f6a] | 732 | mutex_lock(&AS->lock);
|
---|
[d3e7ff4] | 733 | area = find_area_and_lock(AS, page);
|
---|
[20d50a1] | 734 | if (!area) {
|
---|
| 735 | /*
|
---|
| 736 | * No area contained mapping for 'page'.
|
---|
| 737 | * Signal page fault to low-level handler.
|
---|
| 738 | */
|
---|
[1068f6a] | 739 | mutex_unlock(&AS->lock);
|
---|
[e3c762cd] | 740 | goto page_fault;
|
---|
[20d50a1] | 741 | }
|
---|
| 742 |
|
---|
[a9e8b39] | 743 | if (area->attributes & AS_AREA_ATTR_PARTIAL) {
|
---|
| 744 | /*
|
---|
| 745 | * The address space area is not fully initialized.
|
---|
| 746 | * Avoid possible race by returning error.
|
---|
| 747 | */
|
---|
[1068f6a] | 748 | mutex_unlock(&area->lock);
|
---|
| 749 | mutex_unlock(&AS->lock);
|
---|
[e3c762cd] | 750 | goto page_fault;
|
---|
[a9e8b39] | 751 | }
|
---|
| 752 |
|
---|
[0ee077ee] | 753 | if (!area->backend || !area->backend->page_fault) {
|
---|
[8182031] | 754 | /*
|
---|
| 755 | * The address space area is not backed by any backend
|
---|
| 756 | * or the backend cannot handle page faults.
|
---|
| 757 | */
|
---|
| 758 | mutex_unlock(&area->lock);
|
---|
| 759 | mutex_unlock(&AS->lock);
|
---|
| 760 | goto page_fault;
|
---|
| 761 | }
|
---|
[1ace9ea] | 762 |
|
---|
[2299914] | 763 | page_table_lock(AS, false);
|
---|
| 764 |
|
---|
| 765 | /*
|
---|
| 766 | * To avoid race condition between two page faults
|
---|
| 767 | * on the same address, we need to make sure
|
---|
| 768 | * the mapping has not been already inserted.
|
---|
| 769 | */
|
---|
| 770 | if ((pte = page_mapping_find(AS, page))) {
|
---|
| 771 | if (PTE_PRESENT(pte)) {
|
---|
[fb84455] | 772 | if (((access == PF_ACCESS_READ) && PTE_READABLE(pte)) ||
|
---|
| 773 | (access == PF_ACCESS_WRITE && PTE_WRITABLE(pte)) ||
|
---|
| 774 | (access == PF_ACCESS_EXEC && PTE_EXECUTABLE(pte))) {
|
---|
| 775 | page_table_unlock(AS, false);
|
---|
| 776 | mutex_unlock(&area->lock);
|
---|
| 777 | mutex_unlock(&AS->lock);
|
---|
| 778 | return AS_PF_OK;
|
---|
| 779 | }
|
---|
[2299914] | 780 | }
|
---|
| 781 | }
|
---|
[20d50a1] | 782 |
|
---|
| 783 | /*
|
---|
[8182031] | 784 | * Resort to the backend page fault handler.
|
---|
[20d50a1] | 785 | */
|
---|
[0ee077ee] | 786 | if (area->backend->page_fault(area, page, access) != AS_PF_OK) {
|
---|
[8182031] | 787 | page_table_unlock(AS, false);
|
---|
| 788 | mutex_unlock(&area->lock);
|
---|
| 789 | mutex_unlock(&AS->lock);
|
---|
| 790 | goto page_fault;
|
---|
| 791 | }
|
---|
[20d50a1] | 792 |
|
---|
[8182031] | 793 | page_table_unlock(AS, false);
|
---|
[1068f6a] | 794 | mutex_unlock(&area->lock);
|
---|
| 795 | mutex_unlock(&AS->lock);
|
---|
[e3c762cd] | 796 | return AS_PF_OK;
|
---|
| 797 |
|
---|
| 798 | page_fault:
|
---|
| 799 | if (THREAD->in_copy_from_uspace) {
|
---|
| 800 | THREAD->in_copy_from_uspace = false;
|
---|
[7f1c620] | 801 | istate_set_retaddr(istate, (uintptr_t) &memcpy_from_uspace_failover_address);
|
---|
[e3c762cd] | 802 | } else if (THREAD->in_copy_to_uspace) {
|
---|
| 803 | THREAD->in_copy_to_uspace = false;
|
---|
[7f1c620] | 804 | istate_set_retaddr(istate, (uintptr_t) &memcpy_to_uspace_failover_address);
|
---|
[e3c762cd] | 805 | } else {
|
---|
| 806 | return AS_PF_FAULT;
|
---|
| 807 | }
|
---|
| 808 |
|
---|
| 809 | return AS_PF_DEFER;
|
---|
[20d50a1] | 810 | }
|
---|
| 811 |
|
---|
[7e4e532] | 812 | /** Switch address spaces.
|
---|
[1068f6a] | 813 | *
|
---|
| 814 | * Note that this function cannot sleep as it is essentially a part of
|
---|
[47800e0] | 815 | * scheduling. Sleeping here would lead to deadlock on wakeup.
|
---|
[20d50a1] | 816 | *
|
---|
[7e4e532] | 817 | * @param old Old address space or NULL.
|
---|
| 818 | * @param new New address space.
|
---|
[20d50a1] | 819 | */
|
---|
[7e4e532] | 820 | void as_switch(as_t *old, as_t *new)
|
---|
[20d50a1] | 821 | {
|
---|
| 822 | ipl_t ipl;
|
---|
[7e4e532] | 823 | bool needs_asid = false;
|
---|
[4512d7e] | 824 |
|
---|
[20d50a1] | 825 | ipl = interrupts_disable();
|
---|
[47800e0] | 826 | spinlock_lock(&inactive_as_with_asid_lock);
|
---|
[7e4e532] | 827 |
|
---|
| 828 | /*
|
---|
| 829 | * First, take care of the old address space.
|
---|
| 830 | */
|
---|
| 831 | if (old) {
|
---|
[1068f6a] | 832 | mutex_lock_active(&old->lock);
|
---|
[47800e0] | 833 | ASSERT(old->cpu_refcount);
|
---|
| 834 | if((--old->cpu_refcount == 0) && (old != AS_KERNEL)) {
|
---|
[7e4e532] | 835 | /*
|
---|
| 836 | * The old address space is no longer active on
|
---|
| 837 | * any processor. It can be appended to the
|
---|
| 838 | * list of inactive address spaces with assigned
|
---|
| 839 | * ASID.
|
---|
| 840 | */
|
---|
| 841 | ASSERT(old->asid != ASID_INVALID);
|
---|
| 842 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
|
---|
| 843 | }
|
---|
[1068f6a] | 844 | mutex_unlock(&old->lock);
|
---|
[57da95c] | 845 |
|
---|
| 846 | /*
|
---|
| 847 | * Perform architecture-specific tasks when the address space
|
---|
| 848 | * is being removed from the CPU.
|
---|
| 849 | */
|
---|
| 850 | as_deinstall_arch(old);
|
---|
[7e4e532] | 851 | }
|
---|
| 852 |
|
---|
| 853 | /*
|
---|
| 854 | * Second, prepare the new address space.
|
---|
| 855 | */
|
---|
[1068f6a] | 856 | mutex_lock_active(&new->lock);
|
---|
[47800e0] | 857 | if ((new->cpu_refcount++ == 0) && (new != AS_KERNEL)) {
|
---|
[7e4e532] | 858 | if (new->asid != ASID_INVALID)
|
---|
| 859 | list_remove(&new->inactive_as_with_asid_link);
|
---|
| 860 | else
|
---|
| 861 | needs_asid = true; /* defer call to asid_get() until new->lock is released */
|
---|
| 862 | }
|
---|
| 863 | SET_PTL0_ADDRESS(new->page_table);
|
---|
[1068f6a] | 864 | mutex_unlock(&new->lock);
|
---|
[20d50a1] | 865 |
|
---|
[7e4e532] | 866 | if (needs_asid) {
|
---|
| 867 | /*
|
---|
| 868 | * Allocation of new ASID was deferred
|
---|
| 869 | * until now in order to avoid deadlock.
|
---|
| 870 | */
|
---|
| 871 | asid_t asid;
|
---|
| 872 |
|
---|
| 873 | asid = asid_get();
|
---|
[1068f6a] | 874 | mutex_lock_active(&new->lock);
|
---|
[7e4e532] | 875 | new->asid = asid;
|
---|
[1068f6a] | 876 | mutex_unlock(&new->lock);
|
---|
[7e4e532] | 877 | }
|
---|
[47800e0] | 878 | spinlock_unlock(&inactive_as_with_asid_lock);
|
---|
[7e4e532] | 879 | interrupts_restore(ipl);
|
---|
| 880 |
|
---|
[20d50a1] | 881 | /*
|
---|
| 882 | * Perform architecture-specific steps.
|
---|
[4512d7e] | 883 | * (e.g. write ASID to hardware register etc.)
|
---|
[20d50a1] | 884 | */
|
---|
[7e4e532] | 885 | as_install_arch(new);
|
---|
[20d50a1] | 886 |
|
---|
[7e4e532] | 887 | AS = new;
|
---|
[20d50a1] | 888 | }
|
---|
[6a3c9a7] | 889 |
|
---|
[df0103f7] | 890 | /** Convert address space area flags to page flags.
|
---|
[6a3c9a7] | 891 | *
|
---|
[df0103f7] | 892 | * @param aflags Flags of some address space area.
|
---|
[6a3c9a7] | 893 | *
|
---|
[df0103f7] | 894 | * @return Flags to be passed to page_mapping_insert().
|
---|
[6a3c9a7] | 895 | */
|
---|
[df0103f7] | 896 | int area_flags_to_page_flags(int aflags)
|
---|
[6a3c9a7] | 897 | {
|
---|
| 898 | int flags;
|
---|
| 899 |
|
---|
[9a8d91b] | 900 | flags = PAGE_USER | PAGE_PRESENT;
|
---|
[c23502d] | 901 |
|
---|
[df0103f7] | 902 | if (aflags & AS_AREA_READ)
|
---|
[c23502d] | 903 | flags |= PAGE_READ;
|
---|
| 904 |
|
---|
[df0103f7] | 905 | if (aflags & AS_AREA_WRITE)
|
---|
[c23502d] | 906 | flags |= PAGE_WRITE;
|
---|
| 907 |
|
---|
[df0103f7] | 908 | if (aflags & AS_AREA_EXEC)
|
---|
[c23502d] | 909 | flags |= PAGE_EXEC;
|
---|
[6a3c9a7] | 910 |
|
---|
[0ee077ee] | 911 | if (aflags & AS_AREA_CACHEABLE)
|
---|
[9a8d91b] | 912 | flags |= PAGE_CACHEABLE;
|
---|
| 913 |
|
---|
[6a3c9a7] | 914 | return flags;
|
---|
| 915 | }
|
---|
[ef67bab] | 916 |
|
---|
[df0103f7] | 917 | /** Compute flags for virtual address translation subsytem.
|
---|
| 918 | *
|
---|
| 919 | * The address space area must be locked.
|
---|
| 920 | * Interrupts must be disabled.
|
---|
| 921 | *
|
---|
| 922 | * @param a Address space area.
|
---|
| 923 | *
|
---|
| 924 | * @return Flags to be used in page_mapping_insert().
|
---|
| 925 | */
|
---|
[8182031] | 926 | int as_area_get_flags(as_area_t *a)
|
---|
[df0103f7] | 927 | {
|
---|
| 928 | return area_flags_to_page_flags(a->flags);
|
---|
| 929 | }
|
---|
| 930 |
|
---|
[ef67bab] | 931 | /** Create page table.
|
---|
| 932 | *
|
---|
| 933 | * Depending on architecture, create either address space
|
---|
| 934 | * private or global page table.
|
---|
| 935 | *
|
---|
| 936 | * @param flags Flags saying whether the page table is for kernel address space.
|
---|
| 937 | *
|
---|
| 938 | * @return First entry of the page table.
|
---|
| 939 | */
|
---|
| 940 | pte_t *page_table_create(int flags)
|
---|
| 941 | {
|
---|
| 942 | ASSERT(as_operations);
|
---|
| 943 | ASSERT(as_operations->page_table_create);
|
---|
| 944 |
|
---|
| 945 | return as_operations->page_table_create(flags);
|
---|
| 946 | }
|
---|
[d3e7ff4] | 947 |
|
---|
[482826d] | 948 | /** Destroy page table.
|
---|
| 949 | *
|
---|
| 950 | * Destroy page table in architecture specific way.
|
---|
| 951 | *
|
---|
| 952 | * @param page_table Physical address of PTL0.
|
---|
| 953 | */
|
---|
| 954 | void page_table_destroy(pte_t *page_table)
|
---|
| 955 | {
|
---|
| 956 | ASSERT(as_operations);
|
---|
| 957 | ASSERT(as_operations->page_table_destroy);
|
---|
| 958 |
|
---|
| 959 | as_operations->page_table_destroy(page_table);
|
---|
| 960 | }
|
---|
| 961 |
|
---|
[2299914] | 962 | /** Lock page table.
|
---|
| 963 | *
|
---|
| 964 | * This function should be called before any page_mapping_insert(),
|
---|
| 965 | * page_mapping_remove() and page_mapping_find().
|
---|
| 966 | *
|
---|
| 967 | * Locking order is such that address space areas must be locked
|
---|
| 968 | * prior to this call. Address space can be locked prior to this
|
---|
| 969 | * call in which case the lock argument is false.
|
---|
| 970 | *
|
---|
| 971 | * @param as Address space.
|
---|
[9179d0a] | 972 | * @param lock If false, do not attempt to lock as->lock.
|
---|
[2299914] | 973 | */
|
---|
| 974 | void page_table_lock(as_t *as, bool lock)
|
---|
| 975 | {
|
---|
| 976 | ASSERT(as_operations);
|
---|
| 977 | ASSERT(as_operations->page_table_lock);
|
---|
| 978 |
|
---|
| 979 | as_operations->page_table_lock(as, lock);
|
---|
| 980 | }
|
---|
| 981 |
|
---|
| 982 | /** Unlock page table.
|
---|
| 983 | *
|
---|
| 984 | * @param as Address space.
|
---|
[9179d0a] | 985 | * @param unlock If false, do not attempt to unlock as->lock.
|
---|
[2299914] | 986 | */
|
---|
| 987 | void page_table_unlock(as_t *as, bool unlock)
|
---|
| 988 | {
|
---|
| 989 | ASSERT(as_operations);
|
---|
| 990 | ASSERT(as_operations->page_table_unlock);
|
---|
| 991 |
|
---|
| 992 | as_operations->page_table_unlock(as, unlock);
|
---|
| 993 | }
|
---|
| 994 |
|
---|
[d3e7ff4] | 995 |
|
---|
| 996 | /** Find address space area and lock it.
|
---|
| 997 | *
|
---|
| 998 | * The address space must be locked and interrupts must be disabled.
|
---|
| 999 | *
|
---|
| 1000 | * @param as Address space.
|
---|
| 1001 | * @param va Virtual address.
|
---|
| 1002 | *
|
---|
| 1003 | * @return Locked address space area containing va on success or NULL on failure.
|
---|
| 1004 | */
|
---|
[7f1c620] | 1005 | as_area_t *find_area_and_lock(as_t *as, uintptr_t va)
|
---|
[d3e7ff4] | 1006 | {
|
---|
| 1007 | as_area_t *a;
|
---|
[252127e] | 1008 | btree_node_t *leaf, *lnode;
|
---|
| 1009 | int i;
|
---|
| 1010 |
|
---|
| 1011 | a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
|
---|
| 1012 | if (a) {
|
---|
| 1013 | /* va is the base address of an address space area */
|
---|
[1068f6a] | 1014 | mutex_lock(&a->lock);
|
---|
[252127e] | 1015 | return a;
|
---|
| 1016 | }
|
---|
[d3e7ff4] | 1017 |
|
---|
[252127e] | 1018 | /*
|
---|
[c47912f] | 1019 | * Search the leaf node and the righmost record of its left neighbour
|
---|
[252127e] | 1020 | * to find out whether this is a miss or va belongs to an address
|
---|
| 1021 | * space area found there.
|
---|
| 1022 | */
|
---|
| 1023 |
|
---|
| 1024 | /* First, search the leaf node itself. */
|
---|
| 1025 | for (i = 0; i < leaf->keys; i++) {
|
---|
| 1026 | a = (as_area_t *) leaf->value[i];
|
---|
[1068f6a] | 1027 | mutex_lock(&a->lock);
|
---|
[252127e] | 1028 | if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) {
|
---|
| 1029 | return a;
|
---|
| 1030 | }
|
---|
[1068f6a] | 1031 | mutex_unlock(&a->lock);
|
---|
[252127e] | 1032 | }
|
---|
[d3e7ff4] | 1033 |
|
---|
[252127e] | 1034 | /*
|
---|
[c47912f] | 1035 | * Second, locate the left neighbour and test its last record.
|
---|
[b26db0c] | 1036 | * Because of its position in the B+tree, it must have base < va.
|
---|
[252127e] | 1037 | */
|
---|
[c47912f] | 1038 | if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
|
---|
[252127e] | 1039 | a = (as_area_t *) lnode->value[lnode->keys - 1];
|
---|
[1068f6a] | 1040 | mutex_lock(&a->lock);
|
---|
[252127e] | 1041 | if (va < a->base + a->pages * PAGE_SIZE) {
|
---|
[37e7d2b9] | 1042 | return a;
|
---|
[252127e] | 1043 | }
|
---|
[1068f6a] | 1044 | mutex_unlock(&a->lock);
|
---|
[d3e7ff4] | 1045 | }
|
---|
| 1046 |
|
---|
| 1047 | return NULL;
|
---|
| 1048 | }
|
---|
[37e7d2b9] | 1049 |
|
---|
| 1050 | /** Check area conflicts with other areas.
|
---|
| 1051 | *
|
---|
| 1052 | * The address space must be locked and interrupts must be disabled.
|
---|
| 1053 | *
|
---|
| 1054 | * @param as Address space.
|
---|
| 1055 | * @param va Starting virtual address of the area being tested.
|
---|
| 1056 | * @param size Size of the area being tested.
|
---|
| 1057 | * @param avoid_area Do not touch this area.
|
---|
| 1058 | *
|
---|
| 1059 | * @return True if there is no conflict, false otherwise.
|
---|
| 1060 | */
|
---|
[7f1c620] | 1061 | bool check_area_conflicts(as_t *as, uintptr_t va, size_t size, as_area_t *avoid_area)
|
---|
[37e7d2b9] | 1062 | {
|
---|
| 1063 | as_area_t *a;
|
---|
[252127e] | 1064 | btree_node_t *leaf, *node;
|
---|
| 1065 | int i;
|
---|
[37e7d2b9] | 1066 |
|
---|
[5a7d9d1] | 1067 | /*
|
---|
| 1068 | * We don't want any area to have conflicts with NULL page.
|
---|
| 1069 | */
|
---|
| 1070 | if (overlaps(va, size, NULL, PAGE_SIZE))
|
---|
| 1071 | return false;
|
---|
| 1072 |
|
---|
[252127e] | 1073 | /*
|
---|
| 1074 | * The leaf node is found in O(log n), where n is proportional to
|
---|
| 1075 | * the number of address space areas belonging to as.
|
---|
| 1076 | * The check for conflicts is then attempted on the rightmost
|
---|
[c47912f] | 1077 | * record in the left neighbour, the leftmost record in the right
|
---|
| 1078 | * neighbour and all records in the leaf node itself.
|
---|
[252127e] | 1079 | */
|
---|
| 1080 |
|
---|
| 1081 | if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) {
|
---|
| 1082 | if (a != avoid_area)
|
---|
| 1083 | return false;
|
---|
| 1084 | }
|
---|
| 1085 |
|
---|
| 1086 | /* First, check the two border cases. */
|
---|
[c47912f] | 1087 | if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
|
---|
[252127e] | 1088 | a = (as_area_t *) node->value[node->keys - 1];
|
---|
[1068f6a] | 1089 | mutex_lock(&a->lock);
|
---|
[252127e] | 1090 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
|
---|
[1068f6a] | 1091 | mutex_unlock(&a->lock);
|
---|
[252127e] | 1092 | return false;
|
---|
| 1093 | }
|
---|
[1068f6a] | 1094 | mutex_unlock(&a->lock);
|
---|
[252127e] | 1095 | }
|
---|
[c47912f] | 1096 | if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) {
|
---|
[252127e] | 1097 | a = (as_area_t *) node->value[0];
|
---|
[1068f6a] | 1098 | mutex_lock(&a->lock);
|
---|
[252127e] | 1099 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
|
---|
[1068f6a] | 1100 | mutex_unlock(&a->lock);
|
---|
[252127e] | 1101 | return false;
|
---|
| 1102 | }
|
---|
[1068f6a] | 1103 | mutex_unlock(&a->lock);
|
---|
[252127e] | 1104 | }
|
---|
| 1105 |
|
---|
| 1106 | /* Second, check the leaf node. */
|
---|
| 1107 | for (i = 0; i < leaf->keys; i++) {
|
---|
| 1108 | a = (as_area_t *) leaf->value[i];
|
---|
[37e7d2b9] | 1109 |
|
---|
| 1110 | if (a == avoid_area)
|
---|
| 1111 | continue;
|
---|
[252127e] | 1112 |
|
---|
[1068f6a] | 1113 | mutex_lock(&a->lock);
|
---|
[252127e] | 1114 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
|
---|
[1068f6a] | 1115 | mutex_unlock(&a->lock);
|
---|
[252127e] | 1116 | return false;
|
---|
| 1117 | }
|
---|
[1068f6a] | 1118 | mutex_unlock(&a->lock);
|
---|
[5a7d9d1] | 1119 | }
|
---|
[37e7d2b9] | 1120 |
|
---|
[5a7d9d1] | 1121 | /*
|
---|
| 1122 | * So far, the area does not conflict with other areas.
|
---|
| 1123 | * Check if it doesn't conflict with kernel address space.
|
---|
| 1124 | */
|
---|
| 1125 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
|
---|
| 1126 | return !overlaps(va, size,
|
---|
| 1127 | KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START);
|
---|
[37e7d2b9] | 1128 | }
|
---|
| 1129 |
|
---|
| 1130 | return true;
|
---|
| 1131 | }
|
---|
[df0103f7] | 1132 |
|
---|
[1068f6a] | 1133 | /** Return size of the address space area with given base. */
|
---|
[7f1c620] | 1134 | size_t as_get_size(uintptr_t base)
|
---|
[7c23af9] | 1135 | {
|
---|
| 1136 | ipl_t ipl;
|
---|
| 1137 | as_area_t *src_area;
|
---|
| 1138 | size_t size;
|
---|
| 1139 |
|
---|
| 1140 | ipl = interrupts_disable();
|
---|
| 1141 | src_area = find_area_and_lock(AS, base);
|
---|
| 1142 | if (src_area){
|
---|
| 1143 | size = src_area->pages * PAGE_SIZE;
|
---|
[1068f6a] | 1144 | mutex_unlock(&src_area->lock);
|
---|
[7c23af9] | 1145 | } else {
|
---|
| 1146 | size = 0;
|
---|
| 1147 | }
|
---|
| 1148 | interrupts_restore(ipl);
|
---|
| 1149 | return size;
|
---|
| 1150 | }
|
---|
| 1151 |
|
---|
[25bf215] | 1152 | /** Mark portion of address space area as used.
|
---|
| 1153 | *
|
---|
| 1154 | * The address space area must be already locked.
|
---|
| 1155 | *
|
---|
| 1156 | * @param a Address space area.
|
---|
| 1157 | * @param page First page to be marked.
|
---|
| 1158 | * @param count Number of page to be marked.
|
---|
| 1159 | *
|
---|
| 1160 | * @return 0 on failure and 1 on success.
|
---|
| 1161 | */
|
---|
[7f1c620] | 1162 | int used_space_insert(as_area_t *a, uintptr_t page, count_t count)
|
---|
[25bf215] | 1163 | {
|
---|
| 1164 | btree_node_t *leaf, *node;
|
---|
| 1165 | count_t pages;
|
---|
| 1166 | int i;
|
---|
| 1167 |
|
---|
| 1168 | ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE));
|
---|
| 1169 | ASSERT(count);
|
---|
| 1170 |
|
---|
| 1171 | pages = (count_t) btree_search(&a->used_space, page, &leaf);
|
---|
| 1172 | if (pages) {
|
---|
| 1173 | /*
|
---|
| 1174 | * We hit the beginning of some used space.
|
---|
| 1175 | */
|
---|
| 1176 | return 0;
|
---|
| 1177 | }
|
---|
| 1178 |
|
---|
[a6cb8cb] | 1179 | if (!leaf->keys) {
|
---|
| 1180 | btree_insert(&a->used_space, page, (void *) count, leaf);
|
---|
| 1181 | return 1;
|
---|
| 1182 | }
|
---|
| 1183 |
|
---|
[25bf215] | 1184 | node = btree_leaf_node_left_neighbour(&a->used_space, leaf);
|
---|
| 1185 | if (node) {
|
---|
[7f1c620] | 1186 | uintptr_t left_pg = node->key[node->keys - 1], right_pg = leaf->key[0];
|
---|
[25bf215] | 1187 | count_t left_cnt = (count_t) node->value[node->keys - 1], right_cnt = (count_t) leaf->value[0];
|
---|
| 1188 |
|
---|
| 1189 | /*
|
---|
| 1190 | * Examine the possibility that the interval fits
|
---|
| 1191 | * somewhere between the rightmost interval of
|
---|
| 1192 | * the left neigbour and the first interval of the leaf.
|
---|
| 1193 | */
|
---|
| 1194 |
|
---|
| 1195 | if (page >= right_pg) {
|
---|
| 1196 | /* Do nothing. */
|
---|
| 1197 | } else if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
|
---|
| 1198 | /* The interval intersects with the left interval. */
|
---|
| 1199 | return 0;
|
---|
| 1200 | } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
|
---|
| 1201 | /* The interval intersects with the right interval. */
|
---|
| 1202 | return 0;
|
---|
| 1203 | } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
|
---|
| 1204 | /* The interval can be added by merging the two already present intervals. */
|
---|
[56789125] | 1205 | node->value[node->keys - 1] += count + right_cnt;
|
---|
[25bf215] | 1206 | btree_remove(&a->used_space, right_pg, leaf);
|
---|
| 1207 | return 1;
|
---|
| 1208 | } else if (page == left_pg + left_cnt*PAGE_SIZE) {
|
---|
| 1209 | /* The interval can be added by simply growing the left interval. */
|
---|
[56789125] | 1210 | node->value[node->keys - 1] += count;
|
---|
[25bf215] | 1211 | return 1;
|
---|
| 1212 | } else if (page + count*PAGE_SIZE == right_pg) {
|
---|
| 1213 | /*
|
---|
| 1214 | * The interval can be addded by simply moving base of the right
|
---|
| 1215 | * interval down and increasing its size accordingly.
|
---|
| 1216 | */
|
---|
[56789125] | 1217 | leaf->value[0] += count;
|
---|
[25bf215] | 1218 | leaf->key[0] = page;
|
---|
| 1219 | return 1;
|
---|
| 1220 | } else {
|
---|
| 1221 | /*
|
---|
| 1222 | * The interval is between both neigbouring intervals,
|
---|
| 1223 | * but cannot be merged with any of them.
|
---|
| 1224 | */
|
---|
| 1225 | btree_insert(&a->used_space, page, (void *) count, leaf);
|
---|
| 1226 | return 1;
|
---|
| 1227 | }
|
---|
| 1228 | } else if (page < leaf->key[0]) {
|
---|
[7f1c620] | 1229 | uintptr_t right_pg = leaf->key[0];
|
---|
[25bf215] | 1230 | count_t right_cnt = (count_t) leaf->value[0];
|
---|
| 1231 |
|
---|
| 1232 | /*
|
---|
| 1233 | * Investigate the border case in which the left neighbour does not
|
---|
| 1234 | * exist but the interval fits from the left.
|
---|
| 1235 | */
|
---|
| 1236 |
|
---|
| 1237 | if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
|
---|
| 1238 | /* The interval intersects with the right interval. */
|
---|
| 1239 | return 0;
|
---|
| 1240 | } else if (page + count*PAGE_SIZE == right_pg) {
|
---|
| 1241 | /*
|
---|
| 1242 | * The interval can be added by moving the base of the right interval down
|
---|
| 1243 | * and increasing its size accordingly.
|
---|
| 1244 | */
|
---|
| 1245 | leaf->key[0] = page;
|
---|
[56789125] | 1246 | leaf->value[0] += count;
|
---|
[25bf215] | 1247 | return 1;
|
---|
| 1248 | } else {
|
---|
| 1249 | /*
|
---|
| 1250 | * The interval doesn't adjoin with the right interval.
|
---|
| 1251 | * It must be added individually.
|
---|
| 1252 | */
|
---|
| 1253 | btree_insert(&a->used_space, page, (void *) count, leaf);
|
---|
| 1254 | return 1;
|
---|
| 1255 | }
|
---|
| 1256 | }
|
---|
| 1257 |
|
---|
| 1258 | node = btree_leaf_node_right_neighbour(&a->used_space, leaf);
|
---|
| 1259 | if (node) {
|
---|
[7f1c620] | 1260 | uintptr_t left_pg = leaf->key[leaf->keys - 1], right_pg = node->key[0];
|
---|
[25bf215] | 1261 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1], right_cnt = (count_t) node->value[0];
|
---|
| 1262 |
|
---|
| 1263 | /*
|
---|
| 1264 | * Examine the possibility that the interval fits
|
---|
| 1265 | * somewhere between the leftmost interval of
|
---|
| 1266 | * the right neigbour and the last interval of the leaf.
|
---|
| 1267 | */
|
---|
| 1268 |
|
---|
| 1269 | if (page < left_pg) {
|
---|
| 1270 | /* Do nothing. */
|
---|
| 1271 | } else if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
|
---|
| 1272 | /* The interval intersects with the left interval. */
|
---|
| 1273 | return 0;
|
---|
| 1274 | } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
|
---|
| 1275 | /* The interval intersects with the right interval. */
|
---|
| 1276 | return 0;
|
---|
| 1277 | } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
|
---|
| 1278 | /* The interval can be added by merging the two already present intervals. */
|
---|
[56789125] | 1279 | leaf->value[leaf->keys - 1] += count + right_cnt;
|
---|
[25bf215] | 1280 | btree_remove(&a->used_space, right_pg, node);
|
---|
| 1281 | return 1;
|
---|
| 1282 | } else if (page == left_pg + left_cnt*PAGE_SIZE) {
|
---|
| 1283 | /* The interval can be added by simply growing the left interval. */
|
---|
[56789125] | 1284 | leaf->value[leaf->keys - 1] += count;
|
---|
[25bf215] | 1285 | return 1;
|
---|
| 1286 | } else if (page + count*PAGE_SIZE == right_pg) {
|
---|
| 1287 | /*
|
---|
| 1288 | * The interval can be addded by simply moving base of the right
|
---|
| 1289 | * interval down and increasing its size accordingly.
|
---|
| 1290 | */
|
---|
[56789125] | 1291 | node->value[0] += count;
|
---|
[25bf215] | 1292 | node->key[0] = page;
|
---|
| 1293 | return 1;
|
---|
| 1294 | } else {
|
---|
| 1295 | /*
|
---|
| 1296 | * The interval is between both neigbouring intervals,
|
---|
| 1297 | * but cannot be merged with any of them.
|
---|
| 1298 | */
|
---|
| 1299 | btree_insert(&a->used_space, page, (void *) count, leaf);
|
---|
| 1300 | return 1;
|
---|
| 1301 | }
|
---|
| 1302 | } else if (page >= leaf->key[leaf->keys - 1]) {
|
---|
[7f1c620] | 1303 | uintptr_t left_pg = leaf->key[leaf->keys - 1];
|
---|
[25bf215] | 1304 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1];
|
---|
| 1305 |
|
---|
| 1306 | /*
|
---|
| 1307 | * Investigate the border case in which the right neighbour does not
|
---|
| 1308 | * exist but the interval fits from the right.
|
---|
| 1309 | */
|
---|
| 1310 |
|
---|
| 1311 | if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
|
---|
[56789125] | 1312 | /* The interval intersects with the left interval. */
|
---|
[25bf215] | 1313 | return 0;
|
---|
| 1314 | } else if (left_pg + left_cnt*PAGE_SIZE == page) {
|
---|
| 1315 | /* The interval can be added by growing the left interval. */
|
---|
[56789125] | 1316 | leaf->value[leaf->keys - 1] += count;
|
---|
[25bf215] | 1317 | return 1;
|
---|
| 1318 | } else {
|
---|
| 1319 | /*
|
---|
| 1320 | * The interval doesn't adjoin with the left interval.
|
---|
| 1321 | * It must be added individually.
|
---|
| 1322 | */
|
---|
| 1323 | btree_insert(&a->used_space, page, (void *) count, leaf);
|
---|
| 1324 | return 1;
|
---|
| 1325 | }
|
---|
| 1326 | }
|
---|
| 1327 |
|
---|
| 1328 | /*
|
---|
| 1329 | * Note that if the algorithm made it thus far, the interval can fit only
|
---|
| 1330 | * between two other intervals of the leaf. The two border cases were already
|
---|
| 1331 | * resolved.
|
---|
| 1332 | */
|
---|
| 1333 | for (i = 1; i < leaf->keys; i++) {
|
---|
| 1334 | if (page < leaf->key[i]) {
|
---|
[7f1c620] | 1335 | uintptr_t left_pg = leaf->key[i - 1], right_pg = leaf->key[i];
|
---|
[25bf215] | 1336 | count_t left_cnt = (count_t) leaf->value[i - 1], right_cnt = (count_t) leaf->value[i];
|
---|
| 1337 |
|
---|
| 1338 | /*
|
---|
| 1339 | * The interval fits between left_pg and right_pg.
|
---|
| 1340 | */
|
---|
| 1341 |
|
---|
| 1342 | if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
|
---|
| 1343 | /* The interval intersects with the left interval. */
|
---|
| 1344 | return 0;
|
---|
| 1345 | } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
|
---|
| 1346 | /* The interval intersects with the right interval. */
|
---|
| 1347 | return 0;
|
---|
| 1348 | } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
|
---|
| 1349 | /* The interval can be added by merging the two already present intervals. */
|
---|
[56789125] | 1350 | leaf->value[i - 1] += count + right_cnt;
|
---|
[25bf215] | 1351 | btree_remove(&a->used_space, right_pg, leaf);
|
---|
| 1352 | return 1;
|
---|
| 1353 | } else if (page == left_pg + left_cnt*PAGE_SIZE) {
|
---|
| 1354 | /* The interval can be added by simply growing the left interval. */
|
---|
[56789125] | 1355 | leaf->value[i - 1] += count;
|
---|
[25bf215] | 1356 | return 1;
|
---|
| 1357 | } else if (page + count*PAGE_SIZE == right_pg) {
|
---|
| 1358 | /*
|
---|
| 1359 | * The interval can be addded by simply moving base of the right
|
---|
| 1360 | * interval down and increasing its size accordingly.
|
---|
| 1361 | */
|
---|
[56789125] | 1362 | leaf->value[i] += count;
|
---|
[25bf215] | 1363 | leaf->key[i] = page;
|
---|
| 1364 | return 1;
|
---|
| 1365 | } else {
|
---|
| 1366 | /*
|
---|
| 1367 | * The interval is between both neigbouring intervals,
|
---|
| 1368 | * but cannot be merged with any of them.
|
---|
| 1369 | */
|
---|
| 1370 | btree_insert(&a->used_space, page, (void *) count, leaf);
|
---|
| 1371 | return 1;
|
---|
| 1372 | }
|
---|
| 1373 | }
|
---|
| 1374 | }
|
---|
| 1375 |
|
---|
[fbf7b4c] | 1376 | panic("Inconsistency detected while adding %d pages of used space at %p.\n", count, page);
|
---|
[25bf215] | 1377 | }
|
---|
| 1378 |
|
---|
| 1379 | /** Mark portion of address space area as unused.
|
---|
| 1380 | *
|
---|
| 1381 | * The address space area must be already locked.
|
---|
| 1382 | *
|
---|
| 1383 | * @param a Address space area.
|
---|
| 1384 | * @param page First page to be marked.
|
---|
| 1385 | * @param count Number of page to be marked.
|
---|
| 1386 | *
|
---|
| 1387 | * @return 0 on failure and 1 on success.
|
---|
| 1388 | */
|
---|
[7f1c620] | 1389 | int used_space_remove(as_area_t *a, uintptr_t page, count_t count)
|
---|
[25bf215] | 1390 | {
|
---|
| 1391 | btree_node_t *leaf, *node;
|
---|
| 1392 | count_t pages;
|
---|
| 1393 | int i;
|
---|
| 1394 |
|
---|
| 1395 | ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE));
|
---|
| 1396 | ASSERT(count);
|
---|
| 1397 |
|
---|
| 1398 | pages = (count_t) btree_search(&a->used_space, page, &leaf);
|
---|
| 1399 | if (pages) {
|
---|
| 1400 | /*
|
---|
| 1401 | * We are lucky, page is the beginning of some interval.
|
---|
| 1402 | */
|
---|
| 1403 | if (count > pages) {
|
---|
| 1404 | return 0;
|
---|
| 1405 | } else if (count == pages) {
|
---|
| 1406 | btree_remove(&a->used_space, page, leaf);
|
---|
[56789125] | 1407 | return 1;
|
---|
[25bf215] | 1408 | } else {
|
---|
| 1409 | /*
|
---|
| 1410 | * Find the respective interval.
|
---|
| 1411 | * Decrease its size and relocate its start address.
|
---|
| 1412 | */
|
---|
| 1413 | for (i = 0; i < leaf->keys; i++) {
|
---|
| 1414 | if (leaf->key[i] == page) {
|
---|
| 1415 | leaf->key[i] += count*PAGE_SIZE;
|
---|
[56789125] | 1416 | leaf->value[i] -= count;
|
---|
[25bf215] | 1417 | return 1;
|
---|
| 1418 | }
|
---|
| 1419 | }
|
---|
| 1420 | goto error;
|
---|
| 1421 | }
|
---|
| 1422 | }
|
---|
| 1423 |
|
---|
| 1424 | node = btree_leaf_node_left_neighbour(&a->used_space, leaf);
|
---|
| 1425 | if (node && page < leaf->key[0]) {
|
---|
[7f1c620] | 1426 | uintptr_t left_pg = node->key[node->keys - 1];
|
---|
[25bf215] | 1427 | count_t left_cnt = (count_t) node->value[node->keys - 1];
|
---|
| 1428 |
|
---|
| 1429 | if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
|
---|
| 1430 | if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
|
---|
| 1431 | /*
|
---|
| 1432 | * The interval is contained in the rightmost interval
|
---|
| 1433 | * of the left neighbour and can be removed by
|
---|
| 1434 | * updating the size of the bigger interval.
|
---|
| 1435 | */
|
---|
[56789125] | 1436 | node->value[node->keys - 1] -= count;
|
---|
[25bf215] | 1437 | return 1;
|
---|
| 1438 | } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
|
---|
[56789125] | 1439 | count_t new_cnt;
|
---|
[25bf215] | 1440 |
|
---|
| 1441 | /*
|
---|
| 1442 | * The interval is contained in the rightmost interval
|
---|
| 1443 | * of the left neighbour but its removal requires
|
---|
| 1444 | * both updating the size of the original interval and
|
---|
| 1445 | * also inserting a new interval.
|
---|
| 1446 | */
|
---|
[56789125] | 1447 | new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
|
---|
| 1448 | node->value[node->keys - 1] -= count + new_cnt;
|
---|
[25bf215] | 1449 | btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
|
---|
| 1450 | return 1;
|
---|
| 1451 | }
|
---|
| 1452 | }
|
---|
| 1453 | return 0;
|
---|
| 1454 | } else if (page < leaf->key[0]) {
|
---|
| 1455 | return 0;
|
---|
| 1456 | }
|
---|
| 1457 |
|
---|
| 1458 | if (page > leaf->key[leaf->keys - 1]) {
|
---|
[7f1c620] | 1459 | uintptr_t left_pg = leaf->key[leaf->keys - 1];
|
---|
[25bf215] | 1460 | count_t left_cnt = (count_t) leaf->value[leaf->keys - 1];
|
---|
| 1461 |
|
---|
| 1462 | if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
|
---|
| 1463 | if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
|
---|
| 1464 | /*
|
---|
| 1465 | * The interval is contained in the rightmost interval
|
---|
| 1466 | * of the leaf and can be removed by updating the size
|
---|
| 1467 | * of the bigger interval.
|
---|
| 1468 | */
|
---|
[56789125] | 1469 | leaf->value[leaf->keys - 1] -= count;
|
---|
[25bf215] | 1470 | return 1;
|
---|
| 1471 | } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
|
---|
[56789125] | 1472 | count_t new_cnt;
|
---|
[25bf215] | 1473 |
|
---|
| 1474 | /*
|
---|
| 1475 | * The interval is contained in the rightmost interval
|
---|
| 1476 | * of the leaf but its removal requires both updating
|
---|
| 1477 | * the size of the original interval and
|
---|
| 1478 | * also inserting a new interval.
|
---|
| 1479 | */
|
---|
[56789125] | 1480 | new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
|
---|
| 1481 | leaf->value[leaf->keys - 1] -= count + new_cnt;
|
---|
[25bf215] | 1482 | btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
|
---|
| 1483 | return 1;
|
---|
| 1484 | }
|
---|
| 1485 | }
|
---|
| 1486 | return 0;
|
---|
| 1487 | }
|
---|
| 1488 |
|
---|
| 1489 | /*
|
---|
| 1490 | * The border cases have been already resolved.
|
---|
| 1491 | * Now the interval can be only between intervals of the leaf.
|
---|
| 1492 | */
|
---|
| 1493 | for (i = 1; i < leaf->keys - 1; i++) {
|
---|
| 1494 | if (page < leaf->key[i]) {
|
---|
[7f1c620] | 1495 | uintptr_t left_pg = leaf->key[i - 1];
|
---|
[25bf215] | 1496 | count_t left_cnt = (count_t) leaf->value[i - 1];
|
---|
| 1497 |
|
---|
| 1498 | /*
|
---|
| 1499 | * Now the interval is between intervals corresponding to (i - 1) and i.
|
---|
| 1500 | */
|
---|
| 1501 | if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
|
---|
| 1502 | if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
|
---|
| 1503 | /*
|
---|
| 1504 | * The interval is contained in the interval (i - 1)
|
---|
| 1505 | * of the leaf and can be removed by updating the size
|
---|
| 1506 | * of the bigger interval.
|
---|
| 1507 | */
|
---|
[56789125] | 1508 | leaf->value[i - 1] -= count;
|
---|
[25bf215] | 1509 | return 1;
|
---|
| 1510 | } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
|
---|
[56789125] | 1511 | count_t new_cnt;
|
---|
[25bf215] | 1512 |
|
---|
| 1513 | /*
|
---|
| 1514 | * The interval is contained in the interval (i - 1)
|
---|
| 1515 | * of the leaf but its removal requires both updating
|
---|
| 1516 | * the size of the original interval and
|
---|
| 1517 | * also inserting a new interval.
|
---|
| 1518 | */
|
---|
[56789125] | 1519 | new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
|
---|
| 1520 | leaf->value[i - 1] -= count + new_cnt;
|
---|
[25bf215] | 1521 | btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
|
---|
| 1522 | return 1;
|
---|
| 1523 | }
|
---|
| 1524 | }
|
---|
| 1525 | return 0;
|
---|
| 1526 | }
|
---|
| 1527 | }
|
---|
| 1528 |
|
---|
| 1529 | error:
|
---|
[fbf7b4c] | 1530 | panic("Inconsistency detected while removing %d pages of used space from %p.\n", count, page);
|
---|
[25bf215] | 1531 | }
|
---|
| 1532 |
|
---|
[8182031] | 1533 | /** Remove reference to address space area share info.
|
---|
| 1534 | *
|
---|
| 1535 | * If the reference count drops to 0, the sh_info is deallocated.
|
---|
| 1536 | *
|
---|
| 1537 | * @param sh_info Pointer to address space area share info.
|
---|
| 1538 | */
|
---|
| 1539 | void sh_info_remove_reference(share_info_t *sh_info)
|
---|
| 1540 | {
|
---|
| 1541 | bool dealloc = false;
|
---|
| 1542 |
|
---|
| 1543 | mutex_lock(&sh_info->lock);
|
---|
| 1544 | ASSERT(sh_info->refcount);
|
---|
| 1545 | if (--sh_info->refcount == 0) {
|
---|
| 1546 | dealloc = true;
|
---|
[f8d069e8] | 1547 | link_t *cur;
|
---|
[8182031] | 1548 |
|
---|
| 1549 | /*
|
---|
| 1550 | * Now walk carefully the pagemap B+tree and free/remove
|
---|
| 1551 | * reference from all frames found there.
|
---|
| 1552 | */
|
---|
[f8d069e8] | 1553 | for (cur = sh_info->pagemap.leaf_head.next; cur != &sh_info->pagemap.leaf_head; cur = cur->next) {
|
---|
[8182031] | 1554 | btree_node_t *node;
|
---|
[f8d069e8] | 1555 | int i;
|
---|
[8182031] | 1556 |
|
---|
[f8d069e8] | 1557 | node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 1558 | for (i = 0; i < node->keys; i++)
|
---|
[7f1c620] | 1559 | frame_free((uintptr_t) node->value[i]);
|
---|
[8182031] | 1560 | }
|
---|
| 1561 |
|
---|
| 1562 | }
|
---|
| 1563 | mutex_unlock(&sh_info->lock);
|
---|
| 1564 |
|
---|
| 1565 | if (dealloc) {
|
---|
| 1566 | btree_destroy(&sh_info->pagemap);
|
---|
| 1567 | free(sh_info);
|
---|
| 1568 | }
|
---|
| 1569 | }
|
---|
| 1570 |
|
---|
[df0103f7] | 1571 | /*
|
---|
| 1572 | * Address space related syscalls.
|
---|
| 1573 | */
|
---|
| 1574 |
|
---|
| 1575 | /** Wrapper for as_area_create(). */
|
---|
[7f1c620] | 1576 | unative_t sys_as_area_create(uintptr_t address, size_t size, int flags)
|
---|
[df0103f7] | 1577 | {
|
---|
[0ee077ee] | 1578 | if (as_area_create(AS, flags | AS_AREA_CACHEABLE, size, address, AS_AREA_ATTR_NONE, &anon_backend, NULL))
|
---|
[7f1c620] | 1579 | return (unative_t) address;
|
---|
[df0103f7] | 1580 | else
|
---|
[7f1c620] | 1581 | return (unative_t) -1;
|
---|
[df0103f7] | 1582 | }
|
---|
| 1583 |
|
---|
[c6e314a] | 1584 | /** Wrapper for as_area_resize(). */
|
---|
[7f1c620] | 1585 | unative_t sys_as_area_resize(uintptr_t address, size_t size, int flags)
|
---|
[df0103f7] | 1586 | {
|
---|
[7f1c620] | 1587 | return (unative_t) as_area_resize(AS, address, size, 0);
|
---|
[7242a78e] | 1588 | }
|
---|
| 1589 |
|
---|
[c6e314a] | 1590 | /** Wrapper for as_area_destroy(). */
|
---|
[7f1c620] | 1591 | unative_t sys_as_area_destroy(uintptr_t address)
|
---|
[7242a78e] | 1592 | {
|
---|
[7f1c620] | 1593 | return (unative_t) as_area_destroy(AS, address);
|
---|
[df0103f7] | 1594 | }
|
---|
[b45c443] | 1595 |
|
---|
[64c2ad5] | 1596 | /** Print out information about address space.
|
---|
| 1597 | *
|
---|
| 1598 | * @param as Address space.
|
---|
| 1599 | */
|
---|
| 1600 | void as_print(as_t *as)
|
---|
| 1601 | {
|
---|
| 1602 | ipl_t ipl;
|
---|
| 1603 |
|
---|
| 1604 | ipl = interrupts_disable();
|
---|
| 1605 | mutex_lock(&as->lock);
|
---|
| 1606 |
|
---|
| 1607 | /* print out info about address space areas */
|
---|
| 1608 | link_t *cur;
|
---|
| 1609 | for (cur = as->as_area_btree.leaf_head.next; cur != &as->as_area_btree.leaf_head; cur = cur->next) {
|
---|
[7ba7c6d] | 1610 | btree_node_t *node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
[64c2ad5] | 1611 |
|
---|
| 1612 | int i;
|
---|
| 1613 | for (i = 0; i < node->keys; i++) {
|
---|
[7ba7c6d] | 1614 | as_area_t *area = node->value[i];
|
---|
[64c2ad5] | 1615 |
|
---|
| 1616 | mutex_lock(&area->lock);
|
---|
| 1617 | printf("as_area: %p, base=%p, pages=%d (%p - %p)\n",
|
---|
| 1618 | area, area->base, area->pages, area->base, area->base + area->pages*PAGE_SIZE);
|
---|
| 1619 | mutex_unlock(&area->lock);
|
---|
| 1620 | }
|
---|
| 1621 | }
|
---|
| 1622 |
|
---|
| 1623 | mutex_unlock(&as->lock);
|
---|
| 1624 | interrupts_restore(ipl);
|
---|
| 1625 | }
|
---|
| 1626 |
|
---|
[cc73a8a1] | 1627 | /** @}
|
---|
[b45c443] | 1628 | */
|
---|