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

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

Make page_mapping_find() return a copy rather than the actual PTE

This makes page_mapping_find() more suitable for use with lock-free data
structures such as CHT that guarantee existence of the data only for
some limited time while a condition holds (e.g. inside of a RCU-protected
critical section that must be around all CHT lookups).

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