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