source: mainline/kernel/generic/src/mm/as.c@ 1dbc43f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1dbc43f was 1dbc43f, checked in by Jakub Jermar <jakub@…>, 13 years ago

Unify user page fault handling in as_page_fault().

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