[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 |
|
---|
[9179d0a] | 29 | /**
|
---|
| 30 | * @file as.c
|
---|
| 31 | * @brief Address space related functions.
|
---|
| 32 | *
|
---|
[20d50a1] | 33 | * This file contains address space manipulation functions.
|
---|
| 34 | * Roughly speaking, this is a higher-level client of
|
---|
| 35 | * Virtual Address Translation (VAT) subsystem.
|
---|
[9179d0a] | 36 | *
|
---|
| 37 | * Functionality provided by this file allows one to
|
---|
| 38 | * create address space and create, resize and share
|
---|
| 39 | * address space areas.
|
---|
| 40 | *
|
---|
| 41 | * @see page.c
|
---|
| 42 | *
|
---|
[20d50a1] | 43 | */
|
---|
| 44 |
|
---|
| 45 | #include <mm/as.h>
|
---|
[ef67bab] | 46 | #include <arch/mm/as.h>
|
---|
[20d50a1] | 47 | #include <mm/page.h>
|
---|
| 48 | #include <mm/frame.h>
|
---|
[085d973] | 49 | #include <mm/slab.h>
|
---|
[20d50a1] | 50 | #include <mm/tlb.h>
|
---|
| 51 | #include <arch/mm/page.h>
|
---|
| 52 | #include <genarch/mm/page_pt.h>
|
---|
[2802767] | 53 | #include <genarch/mm/page_ht.h>
|
---|
[4512d7e] | 54 | #include <mm/asid.h>
|
---|
[20d50a1] | 55 | #include <arch/mm/asid.h>
|
---|
| 56 | #include <synch/spinlock.h>
|
---|
[5c9a08b] | 57 | #include <adt/list.h>
|
---|
[252127e] | 58 | #include <adt/btree.h>
|
---|
[df0103f7] | 59 | #include <proc/task.h>
|
---|
[e3c762cd] | 60 | #include <proc/thread.h>
|
---|
[20d50a1] | 61 | #include <arch/asm.h>
|
---|
[df0103f7] | 62 | #include <panic.h>
|
---|
[20d50a1] | 63 | #include <debug.h>
|
---|
[df0103f7] | 64 | #include <print.h>
|
---|
[20d50a1] | 65 | #include <memstr.h>
|
---|
[5a7d9d1] | 66 | #include <macros.h>
|
---|
[20d50a1] | 67 | #include <arch.h>
|
---|
[df0103f7] | 68 | #include <errno.h>
|
---|
| 69 | #include <config.h>
|
---|
| 70 | #include <arch/types.h>
|
---|
| 71 | #include <typedefs.h>
|
---|
[e3c762cd] | 72 | #include <syscall/copy.h>
|
---|
| 73 | #include <arch/interrupt.h>
|
---|
[20d50a1] | 74 |
|
---|
[ef67bab] | 75 | as_operations_t *as_operations = NULL;
|
---|
[20d50a1] | 76 |
|
---|
[7e4e532] | 77 | /** Address space lock. It protects inactive_as_with_asid_head. */
|
---|
| 78 | SPINLOCK_INITIALIZE(as_lock);
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * This list contains address spaces that are not active on any
|
---|
| 82 | * processor and that have valid ASID.
|
---|
| 83 | */
|
---|
| 84 | LIST_INITIALIZE(inactive_as_with_asid_head);
|
---|
| 85 |
|
---|
[071a8ae6] | 86 | /** Kernel address space. */
|
---|
| 87 | as_t *AS_KERNEL = NULL;
|
---|
| 88 |
|
---|
[df0103f7] | 89 | static int area_flags_to_page_flags(int aflags);
|
---|
[6a3c9a7] | 90 | static int get_area_flags(as_area_t *a);
|
---|
[d3e7ff4] | 91 | static as_area_t *find_area_and_lock(as_t *as, __address va);
|
---|
[37e7d2b9] | 92 | static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area);
|
---|
[20d50a1] | 93 |
|
---|
[ef67bab] | 94 | /** Initialize address space subsystem. */
|
---|
| 95 | void as_init(void)
|
---|
| 96 | {
|
---|
| 97 | as_arch_init();
|
---|
[8e1ea655] | 98 | AS_KERNEL = as_create(FLAG_AS_KERNEL);
|
---|
[ef67bab] | 99 | if (!AS_KERNEL)
|
---|
| 100 | panic("can't create kernel address space\n");
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[071a8ae6] | 103 | /** Create address space.
|
---|
| 104 | *
|
---|
| 105 | * @param flags Flags that influence way in wich the address space is created.
|
---|
| 106 | */
|
---|
[ef67bab] | 107 | as_t *as_create(int flags)
|
---|
[20d50a1] | 108 | {
|
---|
| 109 | as_t *as;
|
---|
| 110 |
|
---|
[bb68433] | 111 | as = (as_t *) malloc(sizeof(as_t), 0);
|
---|
[7e4e532] | 112 | link_initialize(&as->inactive_as_with_asid_link);
|
---|
[bb68433] | 113 | spinlock_initialize(&as->lock, "as_lock");
|
---|
[252127e] | 114 | btree_create(&as->as_area_btree);
|
---|
[bb68433] | 115 |
|
---|
| 116 | if (flags & FLAG_AS_KERNEL)
|
---|
| 117 | as->asid = ASID_KERNEL;
|
---|
| 118 | else
|
---|
| 119 | as->asid = ASID_INVALID;
|
---|
| 120 |
|
---|
[7e4e532] | 121 | as->refcount = 0;
|
---|
[bb68433] | 122 | as->page_table = page_table_create(flags);
|
---|
[20d50a1] | 123 |
|
---|
| 124 | return as;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[5be1923] | 127 | /** Free Adress space */
|
---|
| 128 | void as_free(as_t *as)
|
---|
| 129 | {
|
---|
| 130 | ASSERT(as->refcount == 0);
|
---|
| 131 |
|
---|
| 132 | /* TODO: free as_areas and other resources held by as */
|
---|
| 133 | /* TODO: free page table */
|
---|
| 134 | free(as);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[20d50a1] | 137 | /** Create address space area of common attributes.
|
---|
| 138 | *
|
---|
| 139 | * The created address space area is added to the target address space.
|
---|
| 140 | *
|
---|
| 141 | * @param as Target address space.
|
---|
[a9e8b39] | 142 | * @param flags Flags of the area memory.
|
---|
[37e7d2b9] | 143 | * @param size Size of area.
|
---|
[20d50a1] | 144 | * @param base Base address of area.
|
---|
[a9e8b39] | 145 | * @param attrs Attributes of the area.
|
---|
[20d50a1] | 146 | *
|
---|
| 147 | * @return Address space area on success or NULL on failure.
|
---|
| 148 | */
|
---|
[a9e8b39] | 149 | as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base, int attrs)
|
---|
[20d50a1] | 150 | {
|
---|
| 151 | ipl_t ipl;
|
---|
| 152 | as_area_t *a;
|
---|
| 153 |
|
---|
| 154 | if (base % PAGE_SIZE)
|
---|
[37e7d2b9] | 155 | return NULL;
|
---|
| 156 |
|
---|
[dbbeb26] | 157 | if (!size)
|
---|
| 158 | return NULL;
|
---|
| 159 |
|
---|
[37e7d2b9] | 160 | /* Writeable executable areas are not supported. */
|
---|
| 161 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
|
---|
| 162 | return NULL;
|
---|
[20d50a1] | 163 |
|
---|
| 164 | ipl = interrupts_disable();
|
---|
| 165 | spinlock_lock(&as->lock);
|
---|
| 166 |
|
---|
[37e7d2b9] | 167 | if (!check_area_conflicts(as, base, size, NULL)) {
|
---|
| 168 | spinlock_unlock(&as->lock);
|
---|
| 169 | interrupts_restore(ipl);
|
---|
| 170 | return NULL;
|
---|
| 171 | }
|
---|
[20d50a1] | 172 |
|
---|
[bb68433] | 173 | a = (as_area_t *) malloc(sizeof(as_area_t), 0);
|
---|
| 174 |
|
---|
| 175 | spinlock_initialize(&a->lock, "as_area_lock");
|
---|
| 176 |
|
---|
[c23502d] | 177 | a->flags = flags;
|
---|
[a9e8b39] | 178 | a->attributes = attrs;
|
---|
[37e7d2b9] | 179 | a->pages = SIZE2FRAMES(size);
|
---|
[bb68433] | 180 | a->base = base;
|
---|
| 181 |
|
---|
[252127e] | 182 | btree_insert(&as->as_area_btree, base, (void *) a, NULL);
|
---|
[20d50a1] | 183 |
|
---|
| 184 | spinlock_unlock(&as->lock);
|
---|
| 185 | interrupts_restore(ipl);
|
---|
[f9425006] | 186 |
|
---|
[20d50a1] | 187 | return a;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[df0103f7] | 190 | /** Find address space area and change it.
|
---|
| 191 | *
|
---|
| 192 | * @param as Address space.
|
---|
| 193 | * @param address Virtual address belonging to the area to be changed. Must be page-aligned.
|
---|
| 194 | * @param size New size of the virtual memory block starting at address.
|
---|
| 195 | * @param flags Flags influencing the remap operation. Currently unused.
|
---|
| 196 | *
|
---|
[7242a78e] | 197 | * @return Zero on success or a value from @ref errno.h otherwise.
|
---|
[df0103f7] | 198 | */
|
---|
[7242a78e] | 199 | int as_area_resize(as_t *as, __address address, size_t size, int flags)
|
---|
[df0103f7] | 200 | {
|
---|
[7242a78e] | 201 | as_area_t *area;
|
---|
[df0103f7] | 202 | ipl_t ipl;
|
---|
| 203 | size_t pages;
|
---|
| 204 |
|
---|
| 205 | ipl = interrupts_disable();
|
---|
| 206 | spinlock_lock(&as->lock);
|
---|
| 207 |
|
---|
| 208 | /*
|
---|
| 209 | * Locate the area.
|
---|
| 210 | */
|
---|
| 211 | area = find_area_and_lock(as, address);
|
---|
| 212 | if (!area) {
|
---|
| 213 | spinlock_unlock(&as->lock);
|
---|
| 214 | interrupts_restore(ipl);
|
---|
[7242a78e] | 215 | return ENOENT;
|
---|
[df0103f7] | 216 | }
|
---|
| 217 |
|
---|
| 218 | if (area->flags & AS_AREA_DEVICE) {
|
---|
| 219 | /*
|
---|
| 220 | * Remapping of address space areas associated
|
---|
| 221 | * with memory mapped devices is not supported.
|
---|
| 222 | */
|
---|
| 223 | spinlock_unlock(&area->lock);
|
---|
| 224 | spinlock_unlock(&as->lock);
|
---|
| 225 | interrupts_restore(ipl);
|
---|
[7242a78e] | 226 | return ENOTSUP;
|
---|
[df0103f7] | 227 | }
|
---|
| 228 |
|
---|
| 229 | pages = SIZE2FRAMES((address - area->base) + size);
|
---|
| 230 | if (!pages) {
|
---|
| 231 | /*
|
---|
| 232 | * Zero size address space areas are not allowed.
|
---|
| 233 | */
|
---|
| 234 | spinlock_unlock(&area->lock);
|
---|
| 235 | spinlock_unlock(&as->lock);
|
---|
| 236 | interrupts_restore(ipl);
|
---|
[7242a78e] | 237 | return EPERM;
|
---|
[df0103f7] | 238 | }
|
---|
| 239 |
|
---|
| 240 | if (pages < area->pages) {
|
---|
| 241 | int i;
|
---|
| 242 |
|
---|
| 243 | /*
|
---|
| 244 | * Shrinking the area.
|
---|
| 245 | * No need to check for overlaps.
|
---|
| 246 | */
|
---|
| 247 | for (i = pages; i < area->pages; i++) {
|
---|
| 248 | pte_t *pte;
|
---|
| 249 |
|
---|
| 250 | /*
|
---|
| 251 | * Releasing physical memory.
|
---|
| 252 | * This depends on the fact that the memory was allocated using frame_alloc().
|
---|
| 253 | */
|
---|
| 254 | page_table_lock(as, false);
|
---|
| 255 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE);
|
---|
| 256 | if (pte && PTE_VALID(pte)) {
|
---|
| 257 | __address frame;
|
---|
| 258 |
|
---|
| 259 | ASSERT(PTE_PRESENT(pte));
|
---|
| 260 | frame = PTE_GET_FRAME(pte);
|
---|
| 261 | page_mapping_remove(as, area->base + i*PAGE_SIZE);
|
---|
| 262 | page_table_unlock(as, false);
|
---|
| 263 |
|
---|
| 264 | frame_free(ADDR2PFN(frame));
|
---|
| 265 | } else {
|
---|
| 266 | page_table_unlock(as, false);
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 | /*
|
---|
| 270 | * Invalidate TLB's.
|
---|
| 271 | */
|
---|
| 272 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
|
---|
| 273 | tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
|
---|
| 274 | tlb_shootdown_finalize();
|
---|
| 275 | } else {
|
---|
| 276 | /*
|
---|
| 277 | * Growing the area.
|
---|
| 278 | * Check for overlaps with other address space areas.
|
---|
| 279 | */
|
---|
| 280 | if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) {
|
---|
| 281 | spinlock_unlock(&area->lock);
|
---|
| 282 | spinlock_unlock(&as->lock);
|
---|
| 283 | interrupts_restore(ipl);
|
---|
[7242a78e] | 284 | return EADDRNOTAVAIL;
|
---|
[df0103f7] | 285 | }
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | area->pages = pages;
|
---|
| 289 |
|
---|
| 290 | spinlock_unlock(&area->lock);
|
---|
| 291 | spinlock_unlock(&as->lock);
|
---|
| 292 | interrupts_restore(ipl);
|
---|
| 293 |
|
---|
[7242a78e] | 294 | return 0;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | /** Destroy address space area.
|
---|
| 298 | *
|
---|
| 299 | * @param as Address space.
|
---|
| 300 | * @param address Address withing the area to be deleted.
|
---|
| 301 | *
|
---|
| 302 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
| 303 | */
|
---|
| 304 | int as_area_destroy(as_t *as, __address address)
|
---|
| 305 | {
|
---|
| 306 | as_area_t *area;
|
---|
| 307 | __address base;
|
---|
| 308 | ipl_t ipl;
|
---|
| 309 | int i;
|
---|
| 310 |
|
---|
| 311 | ipl = interrupts_disable();
|
---|
| 312 | spinlock_lock(&as->lock);
|
---|
| 313 |
|
---|
| 314 | area = find_area_and_lock(as, address);
|
---|
| 315 | if (!area) {
|
---|
| 316 | spinlock_unlock(&as->lock);
|
---|
| 317 | interrupts_restore(ipl);
|
---|
| 318 | return ENOENT;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | base = area->base;
|
---|
| 322 | for (i = 0; i < area->pages; i++) {
|
---|
| 323 | pte_t *pte;
|
---|
| 324 |
|
---|
| 325 | /*
|
---|
| 326 | * Releasing physical memory.
|
---|
| 327 | * Areas mapping memory-mapped devices are treated differently than
|
---|
| 328 | * areas backing frame_alloc()'ed memory.
|
---|
| 329 | */
|
---|
| 330 | page_table_lock(as, false);
|
---|
| 331 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE);
|
---|
| 332 | if (pte && PTE_VALID(pte)) {
|
---|
| 333 | ASSERT(PTE_PRESENT(pte));
|
---|
| 334 | page_mapping_remove(as, area->base + i*PAGE_SIZE);
|
---|
| 335 | if (area->flags & AS_AREA_DEVICE) {
|
---|
| 336 | __address frame;
|
---|
| 337 | frame = PTE_GET_FRAME(pte);
|
---|
| 338 | frame_free(ADDR2PFN(frame));
|
---|
| 339 | }
|
---|
| 340 | page_table_unlock(as, false);
|
---|
| 341 | } else {
|
---|
| 342 | page_table_unlock(as, false);
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
| 345 | /*
|
---|
| 346 | * Invalidate TLB's.
|
---|
| 347 | */
|
---|
| 348 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base, area->pages);
|
---|
| 349 | tlb_invalidate_pages(AS->asid, area->base, area->pages);
|
---|
| 350 | tlb_shootdown_finalize();
|
---|
| 351 |
|
---|
| 352 | spinlock_unlock(&area->lock);
|
---|
| 353 |
|
---|
| 354 | /*
|
---|
| 355 | * Remove the empty area from address space.
|
---|
| 356 | */
|
---|
| 357 | btree_remove(&AS->as_area_btree, base, NULL);
|
---|
| 358 |
|
---|
| 359 | spinlock_unlock(&AS->lock);
|
---|
| 360 | interrupts_restore(ipl);
|
---|
| 361 | return 0;
|
---|
[df0103f7] | 362 | }
|
---|
| 363 |
|
---|
| 364 | /** Send address space area to another task.
|
---|
| 365 | *
|
---|
| 366 | * Address space area is sent to the specified task.
|
---|
| 367 | * If the destination task is willing to accept the
|
---|
| 368 | * area, a new area is created according to the
|
---|
| 369 | * source area. Moreover, any existing mapping
|
---|
| 370 | * is copied as well, providing thus a mechanism
|
---|
| 371 | * for sharing group of pages. The source address
|
---|
| 372 | * space area and any associated mapping is preserved.
|
---|
| 373 | *
|
---|
[a9e8b39] | 374 | * @param dst_id Task ID of the accepting task.
|
---|
| 375 | * @param src_base Base address of the source address space area.
|
---|
[df0103f7] | 376 | *
|
---|
[7242a78e] | 377 | * @return Zero on success or ENOENT if there is no such task or
|
---|
[df0103f7] | 378 | * if there is no such address space area,
|
---|
| 379 | * EPERM if there was a problem in accepting the area or
|
---|
| 380 | * ENOMEM if there was a problem in allocating destination
|
---|
| 381 | * address space area.
|
---|
| 382 | */
|
---|
[a9e8b39] | 383 | int as_area_send(task_id_t dst_id, __address src_base)
|
---|
[df0103f7] | 384 | {
|
---|
| 385 | ipl_t ipl;
|
---|
| 386 | task_t *t;
|
---|
| 387 | count_t i;
|
---|
[a9e8b39] | 388 | as_t *dst_as;
|
---|
[df0103f7] | 389 | __address dst_base;
|
---|
[a9e8b39] | 390 | int src_flags;
|
---|
| 391 | size_t src_size;
|
---|
| 392 | as_area_t *src_area, *dst_area;
|
---|
[df0103f7] | 393 |
|
---|
| 394 | ipl = interrupts_disable();
|
---|
| 395 | spinlock_lock(&tasks_lock);
|
---|
| 396 |
|
---|
[a9e8b39] | 397 | t = task_find_by_id(dst_id);
|
---|
[df0103f7] | 398 | if (!NULL) {
|
---|
| 399 | spinlock_unlock(&tasks_lock);
|
---|
| 400 | interrupts_restore(ipl);
|
---|
| 401 | return ENOENT;
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | spinlock_lock(&t->lock);
|
---|
| 405 | spinlock_unlock(&tasks_lock);
|
---|
| 406 |
|
---|
[a9e8b39] | 407 | dst_as = t->as;
|
---|
[df0103f7] | 408 | dst_base = (__address) t->accept_arg.base;
|
---|
| 409 |
|
---|
[a9e8b39] | 410 | if (dst_as == AS) {
|
---|
[df0103f7] | 411 | /*
|
---|
| 412 | * The two tasks share the entire address space.
|
---|
| 413 | * Return error since there is no point in continuing.
|
---|
| 414 | */
|
---|
| 415 | spinlock_unlock(&t->lock);
|
---|
| 416 | interrupts_restore(ipl);
|
---|
| 417 | return EPERM;
|
---|
| 418 | }
|
---|
[6fa476f7] | 419 |
|
---|
| 420 | spinlock_lock(&AS->lock);
|
---|
[a9e8b39] | 421 | src_area = find_area_and_lock(AS, src_base);
|
---|
| 422 | if (!src_area) {
|
---|
[6fa476f7] | 423 | /*
|
---|
| 424 | * Could not find the source address space area.
|
---|
| 425 | */
|
---|
| 426 | spinlock_unlock(&t->lock);
|
---|
| 427 | spinlock_unlock(&AS->lock);
|
---|
| 428 | interrupts_restore(ipl);
|
---|
| 429 | return ENOENT;
|
---|
| 430 | }
|
---|
[a9e8b39] | 431 | src_size = src_area->pages * PAGE_SIZE;
|
---|
| 432 | src_flags = src_area->flags;
|
---|
| 433 | spinlock_unlock(&src_area->lock);
|
---|
[6fa476f7] | 434 | spinlock_unlock(&AS->lock);
|
---|
[df0103f7] | 435 |
|
---|
[a9e8b39] | 436 | if ((t->accept_arg.task_id != TASK->taskid) || (t->accept_arg.size != src_size) ||
|
---|
| 437 | (t->accept_arg.flags != src_flags)) {
|
---|
[df0103f7] | 438 | /*
|
---|
| 439 | * Discrepancy in either task ID, size or flags.
|
---|
| 440 | */
|
---|
| 441 | spinlock_unlock(&t->lock);
|
---|
| 442 | interrupts_restore(ipl);
|
---|
| 443 | return EPERM;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | /*
|
---|
[a9e8b39] | 447 | * Create copy of the source address space area.
|
---|
| 448 | * The destination area is created with AS_AREA_ATTR_PARTIAL
|
---|
| 449 | * attribute set which prevents race condition with
|
---|
| 450 | * preliminary as_page_fault() calls.
|
---|
[df0103f7] | 451 | */
|
---|
[a9e8b39] | 452 | dst_area = as_area_create(dst_as, src_flags, src_size, dst_base, AS_AREA_ATTR_PARTIAL);
|
---|
| 453 | if (!dst_area) {
|
---|
[df0103f7] | 454 | /*
|
---|
| 455 | * Destination address space area could not be created.
|
---|
| 456 | */
|
---|
| 457 | spinlock_unlock(&t->lock);
|
---|
| 458 | interrupts_restore(ipl);
|
---|
| 459 | return ENOMEM;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | memsetb((__address) &t->accept_arg, sizeof(as_area_acptsnd_arg_t), 0);
|
---|
| 463 | spinlock_unlock(&t->lock);
|
---|
| 464 |
|
---|
| 465 | /*
|
---|
| 466 | * Avoid deadlock by first locking the address space with lower address.
|
---|
| 467 | */
|
---|
[a9e8b39] | 468 | if (dst_as < AS) {
|
---|
| 469 | spinlock_lock(&dst_as->lock);
|
---|
[df0103f7] | 470 | spinlock_lock(&AS->lock);
|
---|
| 471 | } else {
|
---|
| 472 | spinlock_lock(&AS->lock);
|
---|
[a9e8b39] | 473 | spinlock_lock(&dst_as->lock);
|
---|
[df0103f7] | 474 | }
|
---|
| 475 |
|
---|
[a9e8b39] | 476 | for (i = 0; i < SIZE2FRAMES(src_size); i++) {
|
---|
[df0103f7] | 477 | pte_t *pte;
|
---|
| 478 | __address frame;
|
---|
| 479 |
|
---|
| 480 | page_table_lock(AS, false);
|
---|
[a9e8b39] | 481 | pte = page_mapping_find(AS, src_base + i*PAGE_SIZE);
|
---|
[df0103f7] | 482 | if (pte && PTE_VALID(pte)) {
|
---|
| 483 | ASSERT(PTE_PRESENT(pte));
|
---|
| 484 | frame = PTE_GET_FRAME(pte);
|
---|
[a9e8b39] | 485 | if (!(src_flags & AS_AREA_DEVICE))
|
---|
[f3ac636] | 486 | frame_reference_add(ADDR2PFN(frame));
|
---|
[df0103f7] | 487 | page_table_unlock(AS, false);
|
---|
| 488 | } else {
|
---|
| 489 | page_table_unlock(AS, false);
|
---|
| 490 | continue;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
[a9e8b39] | 493 | page_table_lock(dst_as, false);
|
---|
| 494 | page_mapping_insert(dst_as, dst_base + i*PAGE_SIZE, frame, area_flags_to_page_flags(src_flags));
|
---|
| 495 | page_table_unlock(dst_as, false);
|
---|
[df0103f7] | 496 | }
|
---|
[a9e8b39] | 497 |
|
---|
| 498 | /*
|
---|
| 499 | * Now the destination address space area has been
|
---|
| 500 | * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
|
---|
| 501 | * attribute.
|
---|
| 502 | */
|
---|
| 503 | spinlock_lock(&dst_area->lock);
|
---|
| 504 | dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
|
---|
| 505 | spinlock_unlock(&dst_area->lock);
|
---|
[df0103f7] | 506 |
|
---|
| 507 | spinlock_unlock(&AS->lock);
|
---|
[a9e8b39] | 508 | spinlock_unlock(&dst_as->lock);
|
---|
[df0103f7] | 509 | interrupts_restore(ipl);
|
---|
| 510 |
|
---|
| 511 | return 0;
|
---|
| 512 | }
|
---|
| 513 |
|
---|
[6a3c9a7] | 514 | /** Initialize mapping for one page of address space.
|
---|
[20d50a1] | 515 | *
|
---|
[6a3c9a7] | 516 | * This functions maps 'page' to 'frame' according
|
---|
| 517 | * to attributes of the address space area to
|
---|
| 518 | * wich 'page' belongs.
|
---|
[20d50a1] | 519 | *
|
---|
[23230aa] | 520 | * @param as Target address space.
|
---|
[6a3c9a7] | 521 | * @param page Virtual page within the area.
|
---|
| 522 | * @param frame Physical frame to which page will be mapped.
|
---|
[20d50a1] | 523 | */
|
---|
[6a3c9a7] | 524 | void as_set_mapping(as_t *as, __address page, __address frame)
|
---|
[20d50a1] | 525 | {
|
---|
[d3e7ff4] | 526 | as_area_t *area;
|
---|
[20d50a1] | 527 | ipl_t ipl;
|
---|
| 528 |
|
---|
| 529 | ipl = interrupts_disable();
|
---|
[2299914] | 530 | page_table_lock(as, true);
|
---|
[6a3c9a7] | 531 |
|
---|
[d3e7ff4] | 532 | area = find_area_and_lock(as, page);
|
---|
[6a3c9a7] | 533 | if (!area) {
|
---|
| 534 | panic("page not part of any as_area\n");
|
---|
| 535 | }
|
---|
| 536 |
|
---|
[ef67bab] | 537 | page_mapping_insert(as, page, frame, get_area_flags(area));
|
---|
[20d50a1] | 538 |
|
---|
[6a3c9a7] | 539 | spinlock_unlock(&area->lock);
|
---|
[2299914] | 540 | page_table_unlock(as, true);
|
---|
[20d50a1] | 541 | interrupts_restore(ipl);
|
---|
| 542 | }
|
---|
| 543 |
|
---|
| 544 | /** Handle page fault within the current address space.
|
---|
| 545 | *
|
---|
| 546 | * This is the high-level page fault handler.
|
---|
| 547 | * Interrupts are assumed disabled.
|
---|
| 548 | *
|
---|
| 549 | * @param page Faulting page.
|
---|
[e3c762cd] | 550 | * @param istate Pointer to interrupted state.
|
---|
[20d50a1] | 551 | *
|
---|
[e3c762cd] | 552 | * @return 0 on page fault, 1 on success or 2 if the fault was caused by copy_to_uspace() or copy_from_uspace().
|
---|
[20d50a1] | 553 | */
|
---|
[e3c762cd] | 554 | int as_page_fault(__address page, istate_t *istate)
|
---|
[20d50a1] | 555 | {
|
---|
[2299914] | 556 | pte_t *pte;
|
---|
[d3e7ff4] | 557 | as_area_t *area;
|
---|
[20d50a1] | 558 | __address frame;
|
---|
| 559 |
|
---|
| 560 | ASSERT(AS);
|
---|
[2299914] | 561 |
|
---|
[20d50a1] | 562 | spinlock_lock(&AS->lock);
|
---|
[d3e7ff4] | 563 | area = find_area_and_lock(AS, page);
|
---|
[20d50a1] | 564 | if (!area) {
|
---|
| 565 | /*
|
---|
| 566 | * No area contained mapping for 'page'.
|
---|
| 567 | * Signal page fault to low-level handler.
|
---|
| 568 | */
|
---|
| 569 | spinlock_unlock(&AS->lock);
|
---|
[e3c762cd] | 570 | goto page_fault;
|
---|
[20d50a1] | 571 | }
|
---|
| 572 |
|
---|
[a9e8b39] | 573 | if (area->attributes & AS_AREA_ATTR_PARTIAL) {
|
---|
| 574 | /*
|
---|
| 575 | * The address space area is not fully initialized.
|
---|
| 576 | * Avoid possible race by returning error.
|
---|
| 577 | */
|
---|
| 578 | spinlock_unlock(&area->lock);
|
---|
| 579 | spinlock_unlock(&AS->lock);
|
---|
[e3c762cd] | 580 | goto page_fault;
|
---|
[a9e8b39] | 581 | }
|
---|
| 582 |
|
---|
[1ace9ea] | 583 | ASSERT(!(area->flags & AS_AREA_DEVICE));
|
---|
| 584 |
|
---|
[2299914] | 585 | page_table_lock(AS, false);
|
---|
| 586 |
|
---|
| 587 | /*
|
---|
| 588 | * To avoid race condition between two page faults
|
---|
| 589 | * on the same address, we need to make sure
|
---|
| 590 | * the mapping has not been already inserted.
|
---|
| 591 | */
|
---|
| 592 | if ((pte = page_mapping_find(AS, page))) {
|
---|
| 593 | if (PTE_PRESENT(pte)) {
|
---|
| 594 | page_table_unlock(AS, false);
|
---|
| 595 | spinlock_unlock(&area->lock);
|
---|
| 596 | spinlock_unlock(&AS->lock);
|
---|
| 597 | return 1;
|
---|
| 598 | }
|
---|
| 599 | }
|
---|
| 600 |
|
---|
[20d50a1] | 601 | /*
|
---|
[6a3c9a7] | 602 | * In general, there can be several reasons that
|
---|
| 603 | * can have caused this fault.
|
---|
| 604 | *
|
---|
| 605 | * - non-existent mapping: the area is a scratch
|
---|
| 606 | * area (e.g. stack) and so far has not been
|
---|
| 607 | * allocated a frame for the faulting page
|
---|
| 608 | *
|
---|
| 609 | * - non-present mapping: another possibility,
|
---|
| 610 | * currently not implemented, would be frame
|
---|
| 611 | * reuse; when this becomes a possibility,
|
---|
| 612 | * do not forget to distinguish between
|
---|
| 613 | * the different causes
|
---|
[20d50a1] | 614 | */
|
---|
[085d973] | 615 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
|
---|
[6a3c9a7] | 616 | memsetb(PA2KA(frame), FRAME_SIZE, 0);
|
---|
[20d50a1] | 617 |
|
---|
| 618 | /*
|
---|
| 619 | * Map 'page' to 'frame'.
|
---|
| 620 | * Note that TLB shootdown is not attempted as only new information is being
|
---|
| 621 | * inserted into page tables.
|
---|
| 622 | */
|
---|
[ef67bab] | 623 | page_mapping_insert(AS, page, frame, get_area_flags(area));
|
---|
[2299914] | 624 | page_table_unlock(AS, false);
|
---|
[20d50a1] | 625 |
|
---|
| 626 | spinlock_unlock(&area->lock);
|
---|
| 627 | spinlock_unlock(&AS->lock);
|
---|
[e3c762cd] | 628 | return AS_PF_OK;
|
---|
| 629 |
|
---|
| 630 | page_fault:
|
---|
| 631 | if (!THREAD)
|
---|
| 632 | return AS_PF_FAULT;
|
---|
| 633 |
|
---|
| 634 | if (THREAD->in_copy_from_uspace) {
|
---|
| 635 | THREAD->in_copy_from_uspace = false;
|
---|
| 636 | istate_set_retaddr(istate, (__address) &memcpy_from_uspace_failover_address);
|
---|
| 637 | } else if (THREAD->in_copy_to_uspace) {
|
---|
| 638 | THREAD->in_copy_to_uspace = false;
|
---|
| 639 | istate_set_retaddr(istate, (__address) &memcpy_to_uspace_failover_address);
|
---|
| 640 | } else {
|
---|
| 641 | return AS_PF_FAULT;
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | return AS_PF_DEFER;
|
---|
[20d50a1] | 645 | }
|
---|
| 646 |
|
---|
[7e4e532] | 647 | /** Switch address spaces.
|
---|
[20d50a1] | 648 | *
|
---|
[7e4e532] | 649 | * @param old Old address space or NULL.
|
---|
| 650 | * @param new New address space.
|
---|
[20d50a1] | 651 | */
|
---|
[7e4e532] | 652 | void as_switch(as_t *old, as_t *new)
|
---|
[20d50a1] | 653 | {
|
---|
| 654 | ipl_t ipl;
|
---|
[7e4e532] | 655 | bool needs_asid = false;
|
---|
[4512d7e] | 656 |
|
---|
[20d50a1] | 657 | ipl = interrupts_disable();
|
---|
[7e4e532] | 658 | spinlock_lock(&as_lock);
|
---|
| 659 |
|
---|
| 660 | /*
|
---|
| 661 | * First, take care of the old address space.
|
---|
| 662 | */
|
---|
| 663 | if (old) {
|
---|
| 664 | spinlock_lock(&old->lock);
|
---|
| 665 | ASSERT(old->refcount);
|
---|
| 666 | if((--old->refcount == 0) && (old != AS_KERNEL)) {
|
---|
| 667 | /*
|
---|
| 668 | * The old address space is no longer active on
|
---|
| 669 | * any processor. It can be appended to the
|
---|
| 670 | * list of inactive address spaces with assigned
|
---|
| 671 | * ASID.
|
---|
| 672 | */
|
---|
| 673 | ASSERT(old->asid != ASID_INVALID);
|
---|
| 674 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
|
---|
| 675 | }
|
---|
| 676 | spinlock_unlock(&old->lock);
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | /*
|
---|
| 680 | * Second, prepare the new address space.
|
---|
| 681 | */
|
---|
| 682 | spinlock_lock(&new->lock);
|
---|
| 683 | if ((new->refcount++ == 0) && (new != AS_KERNEL)) {
|
---|
| 684 | if (new->asid != ASID_INVALID)
|
---|
| 685 | list_remove(&new->inactive_as_with_asid_link);
|
---|
| 686 | else
|
---|
| 687 | needs_asid = true; /* defer call to asid_get() until new->lock is released */
|
---|
| 688 | }
|
---|
| 689 | SET_PTL0_ADDRESS(new->page_table);
|
---|
| 690 | spinlock_unlock(&new->lock);
|
---|
[20d50a1] | 691 |
|
---|
[7e4e532] | 692 | if (needs_asid) {
|
---|
| 693 | /*
|
---|
| 694 | * Allocation of new ASID was deferred
|
---|
| 695 | * until now in order to avoid deadlock.
|
---|
| 696 | */
|
---|
| 697 | asid_t asid;
|
---|
| 698 |
|
---|
| 699 | asid = asid_get();
|
---|
| 700 | spinlock_lock(&new->lock);
|
---|
| 701 | new->asid = asid;
|
---|
| 702 | spinlock_unlock(&new->lock);
|
---|
| 703 | }
|
---|
| 704 | spinlock_unlock(&as_lock);
|
---|
| 705 | interrupts_restore(ipl);
|
---|
| 706 |
|
---|
[20d50a1] | 707 | /*
|
---|
| 708 | * Perform architecture-specific steps.
|
---|
[4512d7e] | 709 | * (e.g. write ASID to hardware register etc.)
|
---|
[20d50a1] | 710 | */
|
---|
[7e4e532] | 711 | as_install_arch(new);
|
---|
[20d50a1] | 712 |
|
---|
[7e4e532] | 713 | AS = new;
|
---|
[20d50a1] | 714 | }
|
---|
[6a3c9a7] | 715 |
|
---|
[df0103f7] | 716 | /** Convert address space area flags to page flags.
|
---|
[6a3c9a7] | 717 | *
|
---|
[df0103f7] | 718 | * @param aflags Flags of some address space area.
|
---|
[6a3c9a7] | 719 | *
|
---|
[df0103f7] | 720 | * @return Flags to be passed to page_mapping_insert().
|
---|
[6a3c9a7] | 721 | */
|
---|
[df0103f7] | 722 | int area_flags_to_page_flags(int aflags)
|
---|
[6a3c9a7] | 723 | {
|
---|
| 724 | int flags;
|
---|
| 725 |
|
---|
[9a8d91b] | 726 | flags = PAGE_USER | PAGE_PRESENT;
|
---|
[c23502d] | 727 |
|
---|
[df0103f7] | 728 | if (aflags & AS_AREA_READ)
|
---|
[c23502d] | 729 | flags |= PAGE_READ;
|
---|
| 730 |
|
---|
[df0103f7] | 731 | if (aflags & AS_AREA_WRITE)
|
---|
[c23502d] | 732 | flags |= PAGE_WRITE;
|
---|
| 733 |
|
---|
[df0103f7] | 734 | if (aflags & AS_AREA_EXEC)
|
---|
[c23502d] | 735 | flags |= PAGE_EXEC;
|
---|
[6a3c9a7] | 736 |
|
---|
[df0103f7] | 737 | if (!(aflags & AS_AREA_DEVICE))
|
---|
[9a8d91b] | 738 | flags |= PAGE_CACHEABLE;
|
---|
| 739 |
|
---|
[6a3c9a7] | 740 | return flags;
|
---|
| 741 | }
|
---|
[ef67bab] | 742 |
|
---|
[df0103f7] | 743 | /** Compute flags for virtual address translation subsytem.
|
---|
| 744 | *
|
---|
| 745 | * The address space area must be locked.
|
---|
| 746 | * Interrupts must be disabled.
|
---|
| 747 | *
|
---|
| 748 | * @param a Address space area.
|
---|
| 749 | *
|
---|
| 750 | * @return Flags to be used in page_mapping_insert().
|
---|
| 751 | */
|
---|
| 752 | int get_area_flags(as_area_t *a)
|
---|
| 753 | {
|
---|
| 754 | return area_flags_to_page_flags(a->flags);
|
---|
| 755 | }
|
---|
| 756 |
|
---|
[ef67bab] | 757 | /** Create page table.
|
---|
| 758 | *
|
---|
| 759 | * Depending on architecture, create either address space
|
---|
| 760 | * private or global page table.
|
---|
| 761 | *
|
---|
| 762 | * @param flags Flags saying whether the page table is for kernel address space.
|
---|
| 763 | *
|
---|
| 764 | * @return First entry of the page table.
|
---|
| 765 | */
|
---|
| 766 | pte_t *page_table_create(int flags)
|
---|
| 767 | {
|
---|
| 768 | ASSERT(as_operations);
|
---|
| 769 | ASSERT(as_operations->page_table_create);
|
---|
| 770 |
|
---|
| 771 | return as_operations->page_table_create(flags);
|
---|
| 772 | }
|
---|
[d3e7ff4] | 773 |
|
---|
[2299914] | 774 | /** Lock page table.
|
---|
| 775 | *
|
---|
| 776 | * This function should be called before any page_mapping_insert(),
|
---|
| 777 | * page_mapping_remove() and page_mapping_find().
|
---|
| 778 | *
|
---|
| 779 | * Locking order is such that address space areas must be locked
|
---|
| 780 | * prior to this call. Address space can be locked prior to this
|
---|
| 781 | * call in which case the lock argument is false.
|
---|
| 782 | *
|
---|
| 783 | * @param as Address space.
|
---|
[9179d0a] | 784 | * @param lock If false, do not attempt to lock as->lock.
|
---|
[2299914] | 785 | */
|
---|
| 786 | void page_table_lock(as_t *as, bool lock)
|
---|
| 787 | {
|
---|
| 788 | ASSERT(as_operations);
|
---|
| 789 | ASSERT(as_operations->page_table_lock);
|
---|
| 790 |
|
---|
| 791 | as_operations->page_table_lock(as, lock);
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | /** Unlock page table.
|
---|
| 795 | *
|
---|
| 796 | * @param as Address space.
|
---|
[9179d0a] | 797 | * @param unlock If false, do not attempt to unlock as->lock.
|
---|
[2299914] | 798 | */
|
---|
| 799 | void page_table_unlock(as_t *as, bool unlock)
|
---|
| 800 | {
|
---|
| 801 | ASSERT(as_operations);
|
---|
| 802 | ASSERT(as_operations->page_table_unlock);
|
---|
| 803 |
|
---|
| 804 | as_operations->page_table_unlock(as, unlock);
|
---|
| 805 | }
|
---|
| 806 |
|
---|
[d3e7ff4] | 807 |
|
---|
| 808 | /** Find address space area and lock it.
|
---|
| 809 | *
|
---|
| 810 | * The address space must be locked and interrupts must be disabled.
|
---|
| 811 | *
|
---|
| 812 | * @param as Address space.
|
---|
| 813 | * @param va Virtual address.
|
---|
| 814 | *
|
---|
| 815 | * @return Locked address space area containing va on success or NULL on failure.
|
---|
| 816 | */
|
---|
| 817 | as_area_t *find_area_and_lock(as_t *as, __address va)
|
---|
| 818 | {
|
---|
| 819 | as_area_t *a;
|
---|
[252127e] | 820 | btree_node_t *leaf, *lnode;
|
---|
| 821 | int i;
|
---|
| 822 |
|
---|
| 823 | a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
|
---|
| 824 | if (a) {
|
---|
| 825 | /* va is the base address of an address space area */
|
---|
| 826 | spinlock_lock(&a->lock);
|
---|
| 827 | return a;
|
---|
| 828 | }
|
---|
[d3e7ff4] | 829 |
|
---|
[252127e] | 830 | /*
|
---|
[c47912f] | 831 | * Search the leaf node and the righmost record of its left neighbour
|
---|
[252127e] | 832 | * to find out whether this is a miss or va belongs to an address
|
---|
| 833 | * space area found there.
|
---|
| 834 | */
|
---|
| 835 |
|
---|
| 836 | /* First, search the leaf node itself. */
|
---|
| 837 | for (i = 0; i < leaf->keys; i++) {
|
---|
| 838 | a = (as_area_t *) leaf->value[i];
|
---|
[d3e7ff4] | 839 | spinlock_lock(&a->lock);
|
---|
[252127e] | 840 | if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) {
|
---|
| 841 | return a;
|
---|
| 842 | }
|
---|
| 843 | spinlock_unlock(&a->lock);
|
---|
| 844 | }
|
---|
[d3e7ff4] | 845 |
|
---|
[252127e] | 846 | /*
|
---|
[c47912f] | 847 | * Second, locate the left neighbour and test its last record.
|
---|
[b26db0c] | 848 | * Because of its position in the B+tree, it must have base < va.
|
---|
[252127e] | 849 | */
|
---|
[c47912f] | 850 | if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
|
---|
[252127e] | 851 | a = (as_area_t *) lnode->value[lnode->keys - 1];
|
---|
| 852 | spinlock_lock(&a->lock);
|
---|
| 853 | if (va < a->base + a->pages * PAGE_SIZE) {
|
---|
[37e7d2b9] | 854 | return a;
|
---|
[252127e] | 855 | }
|
---|
[d3e7ff4] | 856 | spinlock_unlock(&a->lock);
|
---|
| 857 | }
|
---|
| 858 |
|
---|
| 859 | return NULL;
|
---|
| 860 | }
|
---|
[37e7d2b9] | 861 |
|
---|
| 862 | /** Check area conflicts with other areas.
|
---|
| 863 | *
|
---|
| 864 | * The address space must be locked and interrupts must be disabled.
|
---|
| 865 | *
|
---|
| 866 | * @param as Address space.
|
---|
| 867 | * @param va Starting virtual address of the area being tested.
|
---|
| 868 | * @param size Size of the area being tested.
|
---|
| 869 | * @param avoid_area Do not touch this area.
|
---|
| 870 | *
|
---|
| 871 | * @return True if there is no conflict, false otherwise.
|
---|
| 872 | */
|
---|
| 873 | bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area)
|
---|
| 874 | {
|
---|
| 875 | as_area_t *a;
|
---|
[252127e] | 876 | btree_node_t *leaf, *node;
|
---|
| 877 | int i;
|
---|
[37e7d2b9] | 878 |
|
---|
[5a7d9d1] | 879 | /*
|
---|
| 880 | * We don't want any area to have conflicts with NULL page.
|
---|
| 881 | */
|
---|
| 882 | if (overlaps(va, size, NULL, PAGE_SIZE))
|
---|
| 883 | return false;
|
---|
| 884 |
|
---|
[252127e] | 885 | /*
|
---|
| 886 | * The leaf node is found in O(log n), where n is proportional to
|
---|
| 887 | * the number of address space areas belonging to as.
|
---|
| 888 | * The check for conflicts is then attempted on the rightmost
|
---|
[c47912f] | 889 | * record in the left neighbour, the leftmost record in the right
|
---|
| 890 | * neighbour and all records in the leaf node itself.
|
---|
[252127e] | 891 | */
|
---|
| 892 |
|
---|
| 893 | if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) {
|
---|
| 894 | if (a != avoid_area)
|
---|
| 895 | return false;
|
---|
| 896 | }
|
---|
| 897 |
|
---|
| 898 | /* First, check the two border cases. */
|
---|
[c47912f] | 899 | if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
|
---|
[252127e] | 900 | a = (as_area_t *) node->value[node->keys - 1];
|
---|
| 901 | spinlock_lock(&a->lock);
|
---|
| 902 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
|
---|
| 903 | spinlock_unlock(&a->lock);
|
---|
| 904 | return false;
|
---|
| 905 | }
|
---|
| 906 | spinlock_unlock(&a->lock);
|
---|
| 907 | }
|
---|
[c47912f] | 908 | if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) {
|
---|
[252127e] | 909 | a = (as_area_t *) node->value[0];
|
---|
| 910 | spinlock_lock(&a->lock);
|
---|
| 911 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
|
---|
| 912 | spinlock_unlock(&a->lock);
|
---|
| 913 | return false;
|
---|
| 914 | }
|
---|
| 915 | spinlock_unlock(&a->lock);
|
---|
| 916 | }
|
---|
| 917 |
|
---|
| 918 | /* Second, check the leaf node. */
|
---|
| 919 | for (i = 0; i < leaf->keys; i++) {
|
---|
| 920 | a = (as_area_t *) leaf->value[i];
|
---|
[37e7d2b9] | 921 |
|
---|
| 922 | if (a == avoid_area)
|
---|
| 923 | continue;
|
---|
[252127e] | 924 |
|
---|
[37e7d2b9] | 925 | spinlock_lock(&a->lock);
|
---|
[252127e] | 926 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
|
---|
| 927 | spinlock_unlock(&a->lock);
|
---|
| 928 | return false;
|
---|
| 929 | }
|
---|
[37e7d2b9] | 930 | spinlock_unlock(&a->lock);
|
---|
[5a7d9d1] | 931 | }
|
---|
[37e7d2b9] | 932 |
|
---|
[5a7d9d1] | 933 | /*
|
---|
| 934 | * So far, the area does not conflict with other areas.
|
---|
| 935 | * Check if it doesn't conflict with kernel address space.
|
---|
| 936 | */
|
---|
| 937 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
|
---|
| 938 | return !overlaps(va, size,
|
---|
| 939 | KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START);
|
---|
[37e7d2b9] | 940 | }
|
---|
| 941 |
|
---|
| 942 | return true;
|
---|
| 943 | }
|
---|
[df0103f7] | 944 |
|
---|
| 945 | /*
|
---|
| 946 | * Address space related syscalls.
|
---|
| 947 | */
|
---|
| 948 |
|
---|
| 949 | /** Wrapper for as_area_create(). */
|
---|
| 950 | __native sys_as_area_create(__address address, size_t size, int flags)
|
---|
| 951 | {
|
---|
[a9e8b39] | 952 | if (as_area_create(AS, flags, size, address, AS_AREA_ATTR_NONE))
|
---|
[df0103f7] | 953 | return (__native) address;
|
---|
| 954 | else
|
---|
| 955 | return (__native) -1;
|
---|
| 956 | }
|
---|
| 957 |
|
---|
| 958 | /** Wrapper for as_area_resize. */
|
---|
| 959 | __native sys_as_area_resize(__address address, size_t size, int flags)
|
---|
| 960 | {
|
---|
[7242a78e] | 961 | return (__native) as_area_resize(AS, address, size, 0);
|
---|
| 962 | }
|
---|
| 963 |
|
---|
| 964 | /** Wrapper for as_area_destroy. */
|
---|
| 965 | __native sys_as_area_destroy(__address address)
|
---|
| 966 | {
|
---|
| 967 | return (__native) as_area_destroy(AS, address);
|
---|
[df0103f7] | 968 | }
|
---|
| 969 |
|
---|
| 970 | /** Prepare task for accepting address space area from another task.
|
---|
| 971 | *
|
---|
| 972 | * @param uspace_accept_arg Accept structure passed from userspace.
|
---|
| 973 | *
|
---|
| 974 | * @return EPERM if the task ID encapsulated in @uspace_accept_arg references
|
---|
| 975 | * TASK. Otherwise zero is returned.
|
---|
| 976 | */
|
---|
| 977 | __native sys_as_area_accept(as_area_acptsnd_arg_t *uspace_accept_arg)
|
---|
| 978 | {
|
---|
| 979 | as_area_acptsnd_arg_t arg;
|
---|
[e3c762cd] | 980 | int rc;
|
---|
[df0103f7] | 981 |
|
---|
[e3c762cd] | 982 | rc = copy_from_uspace(&arg, uspace_accept_arg, sizeof(as_area_acptsnd_arg_t));
|
---|
| 983 | if (rc != 0)
|
---|
| 984 | return rc;
|
---|
[df0103f7] | 985 |
|
---|
| 986 | if (!arg.size)
|
---|
| 987 | return (__native) EPERM;
|
---|
| 988 |
|
---|
| 989 | if (arg.task_id == TASK->taskid) {
|
---|
| 990 | /*
|
---|
| 991 | * Accepting from itself is not allowed.
|
---|
| 992 | */
|
---|
| 993 | return (__native) EPERM;
|
---|
| 994 | }
|
---|
| 995 |
|
---|
| 996 | memcpy(&TASK->accept_arg, &arg, sizeof(as_area_acptsnd_arg_t));
|
---|
| 997 |
|
---|
| 998 | return 0;
|
---|
| 999 | }
|
---|
| 1000 |
|
---|
| 1001 | /** Wrapper for as_area_send. */
|
---|
| 1002 | __native sys_as_area_send(as_area_acptsnd_arg_t *uspace_send_arg)
|
---|
| 1003 | {
|
---|
| 1004 | as_area_acptsnd_arg_t arg;
|
---|
[e3c762cd] | 1005 | int rc;
|
---|
[df0103f7] | 1006 |
|
---|
[e3c762cd] | 1007 | rc = copy_from_uspace(&arg, uspace_send_arg, sizeof(as_area_acptsnd_arg_t));
|
---|
| 1008 | if (rc != 0)
|
---|
| 1009 | return rc;
|
---|
[df0103f7] | 1010 |
|
---|
| 1011 | if (!arg.size)
|
---|
| 1012 | return (__native) EPERM;
|
---|
| 1013 |
|
---|
| 1014 | if (arg.task_id == TASK->taskid) {
|
---|
| 1015 | /*
|
---|
| 1016 | * Sending to itself is not allowed.
|
---|
| 1017 | */
|
---|
| 1018 | return (__native) EPERM;
|
---|
| 1019 | }
|
---|
| 1020 |
|
---|
[6fa476f7] | 1021 | return (__native) as_area_send(arg.task_id, (__address) arg.base);
|
---|
[df0103f7] | 1022 | }
|
---|