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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since de9a18e was 5a2e0dd5, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Be more careful when comparing large numbers

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