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
Line 
1/*
2 * Copyright (c) 2010 Jakub Jermar
3 * Copyright (c) 2018 Jiri Svoboda
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
30/** @addtogroup kernel_generic_mm
31 * @{
32 */
33
34/**
35 * @file
36 * @brief Address space related functions.
37 *
38 * This file contains address space manipulation functions.
39 * Roughly speaking, this is a higher-level client of
40 * Virtual Address Translation (VAT) subsystem.
41 *
42 * Functionality provided by this file allows one to
43 * create address spaces and create, resize and share
44 * address space areas.
45 *
46 * @see page.c
47 *
48 */
49
50#include <mm/as.h>
51#include <arch/mm/as.h>
52#include <mm/page.h>
53#include <mm/frame.h>
54#include <mm/slab.h>
55#include <mm/tlb.h>
56#include <arch/mm/page.h>
57#include <genarch/mm/page_pt.h>
58#include <genarch/mm/page_ht.h>
59#include <mm/asid.h>
60#include <arch/mm/asid.h>
61#include <preemption.h>
62#include <synch/spinlock.h>
63#include <synch/mutex.h>
64#include <adt/list.h>
65#include <adt/btree.h>
66#include <proc/task.h>
67#include <proc/thread.h>
68#include <arch/asm.h>
69#include <panic.h>
70#include <assert.h>
71#include <stdio.h>
72#include <mem.h>
73#include <macros.h>
74#include <bitops.h>
75#include <arch.h>
76#include <errno.h>
77#include <config.h>
78#include <align.h>
79#include <typedefs.h>
80#include <syscall/copy.h>
81#include <arch/interrupt.h>
82#include <interrupt.h>
83#include <stdlib.h>
84
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 */
89as_operations_t *as_operations = NULL;
90
91/** Cache for as_t objects */
92static slab_cache_t *as_cache;
93
94/** Cache for as_page_mapping_t objects */
95static slab_cache_t *as_page_mapping_cache;
96
97/** ASID subsystem lock.
98 *
99 * This lock protects:
100 * - inactive_as_with_asid_list
101 * - as->asid for each as of the as_t type
102 * - asids_allocated counter
103 *
104 */
105SPINLOCK_INITIALIZE(asidlock);
106
107/**
108 * Inactive address spaces (on all processors)
109 * that have valid ASID.
110 */
111LIST_INITIALIZE(inactive_as_with_asid_list);
112
113/** Kernel address space. */
114as_t *AS_KERNEL = NULL;
115
116static void *as_areas_getkey(odlink_t *);
117static int as_areas_cmp(void *, void *);
118
119NO_TRACE static errno_t as_constructor(void *obj, unsigned int flags)
120{
121 as_t *as = (as_t *) obj;
122
123 link_initialize(&as->inactive_as_with_asid_link);
124 mutex_initialize(&as->lock, MUTEX_PASSIVE);
125
126 return as_constructor_arch(as, flags);
127}
128
129NO_TRACE static size_t as_destructor(void *obj)
130{
131 return as_destructor_arch((as_t *) obj);
132}
133
134/** Initialize address space subsystem. */
135void as_init(void)
136{
137 as_arch_init();
138
139 as_cache = slab_cache_create("as_t", sizeof(as_t), 0,
140 as_constructor, as_destructor, SLAB_CACHE_MAGDEFERRED);
141
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
145 AS_KERNEL = as_create(FLAG_AS_KERNEL);
146 if (!AS_KERNEL)
147 panic("Cannot create kernel address space.");
148}
149
150/** Create address space.
151 *
152 * @param flags Flags that influence the way in wich the address
153 * space is created.
154 *
155 */
156as_t *as_create(unsigned int flags)
157{
158 as_t *as = (as_t *) slab_alloc(as_cache, FRAME_ATOMIC);
159 if (!as)
160 return NULL;
161
162 (void) as_create_arch(as, 0);
163
164 odict_initialize(&as->as_areas, as_areas_getkey, as_areas_cmp);
165
166 if (flags & FLAG_AS_KERNEL)
167 as->asid = ASID_KERNEL;
168 else
169 as->asid = ASID_INVALID;
170
171 refcount_init(&as->refcount);
172 as->cpu_refcount = 0;
173
174#ifdef AS_PAGE_TABLE
175 as->genarch.page_table = page_table_create(flags);
176#else
177 page_table_create(flags);
178#endif
179
180 return as;
181}
182
183/** Destroy adress space.
184 *
185 * When there are no tasks referencing this address space (i.e. its refcount is
186 * zero), the address space can be destroyed.
187 *
188 * We know that we don't hold any spinlock.
189 *
190 * @param as Address space to be destroyed.
191 *
192 */
193static void as_destroy(as_t *as)
194{
195 DEADLOCK_PROBE_INIT(p_asidlock);
196
197 assert(as != AS);
198 assert(refcount_unique(&as->refcount));
199
200 /*
201 * Since there is no reference to this address space, it is safe not to
202 * lock its mutex.
203 */
204
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();
213 ipl_t ipl = interrupts_read();
214
215retry:
216 interrupts_disable();
217 if (!spinlock_trylock(&asidlock)) {
218 interrupts_enable();
219 DEADLOCK_PROBE(p_asidlock, DEADLOCK_THRESHOLD);
220 goto retry;
221 }
222
223 /* Interrupts disabled, enable preemption */
224 preemption_enable();
225
226 if ((as->asid != ASID_INVALID) && (as != AS_KERNEL)) {
227 if (as->cpu_refcount == 0)
228 list_remove(&as->inactive_as_with_asid_link);
229
230 asid_put(as->asid);
231 }
232
233 spinlock_unlock(&asidlock);
234 interrupts_restore(ipl);
235
236 /*
237 * Destroy address space areas of the address space.
238 * Need to start from the beginning each time since we are destroying
239 * the areas.
240 */
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);
249 }
250
251 odict_finalize(&as->as_areas);
252
253#ifdef AS_PAGE_TABLE
254 page_table_destroy(as->genarch.page_table);
255#else
256 page_table_destroy(NULL);
257#endif
258
259 slab_free(as_cache, as);
260}
261
262/** Hold a reference to an address space.
263 *
264 * Holding a reference to an address space prevents destruction
265 * of that address space.
266 *
267 * @param as Address space to be held.
268 *
269 */
270NO_TRACE void as_hold(as_t *as)
271{
272 refcount_up(&as->refcount);
273}
274
275/** Release a reference to an address space.
276 *
277 * The last one to release a reference to an address space
278 * destroys the address space.
279 *
280 * @param as Address space to be released.
281 *
282 */
283NO_TRACE void as_release(as_t *as)
284{
285 if (refcount_down(&as->refcount))
286 as_destroy(as);
287}
288
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);
335 size_t agsize = P2SZ(area->pages);
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 }
352
353 return overlaps(addr, gsize, area->base, agsize);
354
355}
356
357/** Check area conflicts with other areas.
358 *
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.
363 * @param avoid Do not touch this area. I.e. this area is not considered
364 * as presenting a conflict.
365 *
366 * @return True if there is no conflict, false otherwise.
367 *
368 */
369NO_TRACE static bool check_area_conflicts(as_t *as, uintptr_t addr,
370 size_t count, bool guarded, as_area_t *avoid)
371{
372 assert((addr % PAGE_SIZE) == 0);
373 assert(mutex_locked(&as->lock));
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;
381
382 /*
383 * We don't want any area to have conflicts with NULL page.
384 */
385 if (overlaps(addr, P2SZ(count), (uintptr_t) NULL, PAGE_SIZE))
386 return false;
387
388 /*
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.
394 */
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);
399
400 if (area != avoid) {
401 mutex_lock(&area->lock);
402 if (area_is_conflicting(addr, count, guarded, area)) {
403 mutex_unlock(&area->lock);
404 return false;
405 }
406
407 mutex_unlock(&area->lock);
408 }
409
410 /* Next area */
411 odlink = odict_next(odlink, &as->as_areas);
412 }
413
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
421 if (odlink != NULL) {
422 as_area_t *area = odict_get_instance(odlink, as_area_t,
423 las_areas);
424
425 if (area != avoid) {
426 mutex_lock(&area->lock);
427 if (area_is_conflicting(addr, count, guarded, area)) {
428 mutex_unlock(&area->lock);
429 return false;
430 }
431
432 mutex_unlock(&area->lock);
433 }
434 }
435
436 /*
437 * So far, the area does not conflict with other areas.
438 * Check if it is contained in the user address space.
439 */
440 if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
441 return iswithin(USER_ADDRESS_SPACE_START,
442 (USER_ADDRESS_SPACE_END - USER_ADDRESS_SPACE_START) + 1,
443 addr, P2SZ(count));
444 }
445
446 return true;
447}
448
449/** Return pointer to unmapped address space area
450 *
451 * The address space must be already locked when calling
452 * this function.
453 *
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.
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,
464 size_t size, bool guarded)
465{
466 assert(mutex_locked(&as->lock));
467
468 if (size == 0)
469 return (uintptr_t) -1;
470
471 /*
472 * Make sure we allocate from page-aligned
473 * address. Check for possible overflow in
474 * each step.
475 */
476
477 size_t pages = SIZE2FRAMES(size);
478
479 /*
480 * Find the lowest unmapped address aligned on the size
481 * boundary, not smaller than bound and of the required size.
482 */
483
484 /* First check the bound address itself */
485 uintptr_t addr = ALIGN_UP(bound, PAGE_SIZE);
486 if (addr >= bound) {
487 if (guarded) {
488 /*
489 * Leave an unmapped page between the lower
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 }
498
499 /* Eventually check the addresses behind each area */
500 as_area_t *area = as_area_first(as);
501 while (area != NULL) {
502 mutex_lock(&area->lock);
503
504 addr = area->base + P2SZ(area->pages);
505
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 }
513
514 bool avail =
515 ((addr >= bound) && (addr >= area->base) &&
516 (check_area_conflicts(as, addr, pages, guarded, area)));
517
518 mutex_unlock(&area->lock);
519
520 if (avail)
521 return addr;
522
523 area = as_area_next(area);
524 }
525
526 /* No suitable address space area found */
527 return (uintptr_t) -1;
528}
529
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
557 if (va < vb)
558 return -1;
559 else if (va == vb)
560 return 0;
561 else
562 return +1;
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
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;
685
686 mutex_lock(&sh_info->lock);
687 assert(sh_info->refcount);
688
689 if (--sh_info->refcount == 0) {
690 dealloc = true;
691
692 /*
693 * Now walk carefully the pagemap B+tree and free/remove
694 * reference from all frames found there.
695 */
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);
700 }
701
702 }
703 mutex_unlock(&sh_info->lock);
704
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 }
710 as_pagemap_finalize(&sh_info->pagemap);
711 free(sh_info);
712 }
713}
714
715/** Create address space area of common attributes.
716 *
717 * The created address space area is added to the target address space.
718 *
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.
724 * @param backend_data NULL or a pointer to custom backend data.
725 * @param base Starting virtual address of the area.
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.
729 * Otherwise ignored.
730 *
731 * @return Address space area on success or NULL on failure.
732 *
733 */
734as_area_t *as_area_create(as_t *as, unsigned int flags, size_t size,
735 unsigned int attrs, mem_backend_t *backend,
736 mem_backend_data_t *backend_data, uintptr_t *base, uintptr_t bound)
737{
738 if ((*base != (uintptr_t) AS_AREA_ANY) && !IS_ALIGNED(*base, PAGE_SIZE))
739 return NULL;
740
741 if (size == 0)
742 return NULL;
743
744 size_t pages = SIZE2FRAMES(size);
745
746 /* Writeable executable areas are not supported. */
747 if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
748 return NULL;
749
750 bool const guarded = flags & AS_AREA_GUARD;
751
752 mutex_lock(&as->lock);
753
754 if (*base == (uintptr_t) AS_AREA_ANY) {
755 *base = as_get_unmapped_area(as, bound, size, guarded);
756 if (*base == (uintptr_t) -1) {
757 mutex_unlock(&as->lock);
758 return NULL;
759 }
760 }
761
762 if (overflows_into_positive(*base, size)) {
763 mutex_unlock(&as->lock);
764 return NULL;
765 }
766
767 if (!check_area_conflicts(as, *base, pages, guarded, NULL)) {
768 mutex_unlock(&as->lock);
769 return NULL;
770 }
771
772 as_area_t *area = (as_area_t *) malloc(sizeof(as_area_t));
773 if (!area) {
774 mutex_unlock(&as->lock);
775 return NULL;
776 }
777
778 mutex_initialize(&area->lock, MUTEX_PASSIVE);
779
780 area->as = as;
781 odlink_initialize(&area->las_areas);
782 area->flags = flags;
783 area->attributes = attrs;
784 area->pages = pages;
785 area->resident = 0;
786 area->base = *base;
787 area->backend = backend;
788 area->sh_info = NULL;
789
790 if (backend_data)
791 area->backend_data = *backend_data;
792 else
793 memsetb(&area->backend_data, sizeof(area->backend_data), 0);
794
795 share_info_t *si = NULL;
796
797 /*
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 */
802 if (!(attrs & AS_AREA_ATTR_PARTIAL)) {
803 si = (share_info_t *) malloc(sizeof(share_info_t));
804 if (!si) {
805 free(area);
806 mutex_unlock(&as->lock);
807 return NULL;
808 }
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;
814 as_pagemap_initialize(&si->pagemap);
815
816 area->sh_info = si;
817
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
828 if (area->backend && area->backend->create) {
829 if (!area->backend->create(area)) {
830 free(area);
831 mutex_unlock(&as->lock);
832 if (!(attrs & AS_AREA_ATTR_PARTIAL))
833 sh_info_remove_reference(si);
834 return NULL;
835 }
836 }
837
838 btree_create(&area->used_space);
839 odict_insert(&area->las_areas, &as->as_areas, NULL);
840
841 mutex_unlock(&as->lock);
842
843 return area;
844}
845
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 */
855NO_TRACE static as_area_t *find_area_and_lock(as_t *as, uintptr_t va)
856{
857 assert(mutex_locked(&as->lock));
858
859 odlink_t *odlink = odict_find_leq(&as->as_areas, &va, NULL);
860 if (odlink == NULL)
861 return NULL;
862
863 as_area_t *area = odict_get_instance(odlink, as_area_t, las_areas);
864 mutex_lock(&area->lock);
865
866 assert(area->base <= va);
867
868 if (va <= area->base + (P2SZ(area->pages) - 1))
869 return area;
870
871 mutex_unlock(&area->lock);
872 return NULL;
873}
874
875/** Find address space area and change it.
876 *
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.
885 *
886 */
887errno_t as_area_resize(as_t *as, uintptr_t address, size_t size, unsigned int flags)
888{
889 if (!IS_ALIGNED(address, PAGE_SIZE))
890 return EINVAL;
891
892 mutex_lock(&as->lock);
893
894 /*
895 * Locate the area.
896 */
897 as_area_t *area = find_area_and_lock(as, address);
898 if (!area) {
899 mutex_unlock(&as->lock);
900 return ENOENT;
901 }
902
903 if (!area->backend->is_resizable(area)) {
904 /*
905 * The backend does not support resizing for this area.
906 */
907 mutex_unlock(&area->lock);
908 mutex_unlock(&as->lock);
909 return ENOTSUP;
910 }
911
912 mutex_lock(&area->sh_info->lock);
913 if (area->sh_info->shared) {
914 /*
915 * Remapping of shared address space areas
916 * is not supported.
917 */
918 mutex_unlock(&area->sh_info->lock);
919 mutex_unlock(&area->lock);
920 mutex_unlock(&as->lock);
921 return ENOTSUP;
922 }
923 mutex_unlock(&area->sh_info->lock);
924
925 size_t pages = SIZE2FRAMES((address - area->base) + size);
926 if (!pages) {
927 /*
928 * Zero size address space areas are not allowed.
929 */
930 mutex_unlock(&area->lock);
931 mutex_unlock(&as->lock);
932 return EPERM;
933 }
934
935 if (pages < area->pages) {
936 uintptr_t start_free = area->base + P2SZ(pages);
937
938 /*
939 * Shrinking the area.
940 * No need to check for overlaps.
941 */
942
943 page_table_lock(as, false);
944
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.
951 */
952 bool cond = true;
953 while (cond) {
954 assert(!list_empty(&area->used_space.leaf_list));
955
956 btree_node_t *node =
957 list_get_instance(list_last(&area->used_space.leaf_list),
958 btree_node_t, leaf_link);
959
960 if ((cond = (node->keys != 0))) {
961 uintptr_t ptr = node->key[node->keys - 1];
962 size_t node_size =
963 (size_t) node->value[node->keys - 1];
964 size_t i = 0;
965
966 if (overlaps(ptr, P2SZ(node_size), area->base,
967 P2SZ(pages))) {
968
969 if (ptr + P2SZ(node_size) <= start_free) {
970 /*
971 * The whole interval fits
972 * completely in the resized
973 * address space area.
974 */
975 break;
976 }
977
978 /*
979 * Part of the interval corresponding
980 * to b and c overlaps with the resized
981 * address space area.
982 */
983
984 /* We are almost done */
985 cond = false;
986 i = (start_free - ptr) >> PAGE_WIDTH;
987 if (!used_space_remove(area, start_free,
988 node_size - i))
989 panic("Cannot remove used space.");
990 } else {
991 /*
992 * The interval of used space can be
993 * completely removed.
994 */
995 if (!used_space_remove(area, ptr, node_size))
996 panic("Cannot remove used space.");
997 }
998
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);
1014
1015 for (; i < node_size; i++) {
1016 pte_t pte;
1017 bool found = page_mapping_find(as,
1018 ptr + P2SZ(i), false, &pte);
1019
1020 (void) found;
1021 assert(found);
1022 assert(PTE_VALID(&pte));
1023 assert(PTE_PRESENT(&pte));
1024
1025 if ((area->backend) &&
1026 (area->backend->frame_free)) {
1027 area->backend->frame_free(area,
1028 ptr + P2SZ(i),
1029 PTE_GET_FRAME(&pte));
1030 }
1031
1032 page_mapping_remove(as, ptr + P2SZ(i));
1033 }
1034
1035 /*
1036 * Finish TLB shootdown sequence.
1037 */
1038
1039 tlb_invalidate_pages(as->asid,
1040 area->base + P2SZ(pages),
1041 area->pages - pages);
1042
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 }
1053 page_table_unlock(as, false);
1054 } else {
1055 /*
1056 * Growing the area.
1057 */
1058
1059 if (overflows_into_positive(address, P2SZ(pages)))
1060 return EINVAL;
1061
1062 /*
1063 * Check for overlaps with other address space areas.
1064 */
1065 bool const guarded = area->flags & AS_AREA_GUARD;
1066 if (!check_area_conflicts(as, address, pages, guarded, area)) {
1067 mutex_unlock(&area->lock);
1068 mutex_unlock(&as->lock);
1069 return EADDRNOTAVAIL;
1070 }
1071 }
1072
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 }
1080
1081 area->pages = pages;
1082
1083 mutex_unlock(&area->lock);
1084 mutex_unlock(&as->lock);
1085
1086 return 0;
1087}
1088
1089/** Destroy address space area.
1090 *
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.
1095 *
1096 */
1097errno_t as_area_destroy(as_t *as, uintptr_t address)
1098{
1099 mutex_lock(&as->lock);
1100
1101 as_area_t *area = find_area_and_lock(as, address);
1102 if (!area) {
1103 mutex_unlock(&as->lock);
1104 return ENOENT;
1105 }
1106
1107 if (area->backend && area->backend->destroy)
1108 area->backend->destroy(area);
1109
1110 page_table_lock(as, false);
1111
1112 /*
1113 * Start TLB shootdown sequence.
1114 */
1115 ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base,
1116 area->pages);
1117
1118 /*
1119 * Visit only the pages mapped by used_space B+tree.
1120 */
1121 list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
1122 node) {
1123 btree_key_t i;
1124
1125 for (i = 0; i < node->keys; i++) {
1126 uintptr_t ptr = node->key[i];
1127 size_t size;
1128
1129 for (size = 0; size < (size_t) node->value[i]; size++) {
1130 pte_t pte;
1131 bool found = page_mapping_find(as,
1132 ptr + P2SZ(size), false, &pte);
1133
1134 (void) found;
1135 assert(found);
1136 assert(PTE_VALID(&pte));
1137 assert(PTE_PRESENT(&pte));
1138
1139 if ((area->backend) &&
1140 (area->backend->frame_free)) {
1141 area->backend->frame_free(area,
1142 ptr + P2SZ(size),
1143 PTE_GET_FRAME(&pte));
1144 }
1145
1146 page_mapping_remove(as, ptr + P2SZ(size));
1147 }
1148 }
1149 }
1150
1151 /*
1152 * Finish TLB shootdown sequence.
1153 */
1154
1155 tlb_invalidate_pages(as->asid, area->base, area->pages);
1156
1157 /*
1158 * Invalidate potential software translation caches
1159 * (e.g. TSB on sparc64, PHT on ppc32).
1160 */
1161 as_invalidate_translation_cache(as, area->base, area->pages);
1162 tlb_shootdown_finalize(ipl);
1163
1164 page_table_unlock(as, false);
1165
1166 btree_destroy(&area->used_space);
1167
1168 area->attributes |= AS_AREA_ATTR_PARTIAL;
1169
1170 sh_info_remove_reference(area->sh_info);
1171
1172 mutex_unlock(&area->lock);
1173
1174 /*
1175 * Remove the empty area from address space.
1176 */
1177 odict_remove(&area->las_areas);
1178
1179 free(area);
1180
1181 mutex_unlock(&as->lock);
1182 return 0;
1183}
1184
1185/** Share address space area with another or the same address space.
1186 *
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.
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.
1197 * @param dst_flags_mask Destination address space area flags mask.
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.
1202 *
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 *
1211 */
1212errno_t as_area_share(as_t *src_as, uintptr_t src_base, size_t acc_size,
1213 as_t *dst_as, unsigned int dst_flags_mask, uintptr_t *dst_base,
1214 uintptr_t bound)
1215{
1216 mutex_lock(&src_as->lock);
1217 as_area_t *src_area = find_area_and_lock(src_as, src_base);
1218 if (!src_area) {
1219 /*
1220 * Could not find the source address space area.
1221 */
1222 mutex_unlock(&src_as->lock);
1223 return ENOENT;
1224 }
1225
1226 if (!src_area->backend->is_shareable(src_area)) {
1227 /*
1228 * The backend does not permit sharing of this area.
1229 */
1230 mutex_unlock(&src_area->lock);
1231 mutex_unlock(&src_as->lock);
1232 return ENOTSUP;
1233 }
1234
1235 size_t src_size = P2SZ(src_area->pages);
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;
1239
1240 /* Share the cacheable flag from the original mapping */
1241 if (src_flags & AS_AREA_CACHEABLE)
1242 dst_flags_mask |= AS_AREA_CACHEABLE;
1243
1244 if ((src_size != acc_size) ||
1245 ((src_flags & dst_flags_mask) != dst_flags_mask)) {
1246 mutex_unlock(&src_area->lock);
1247 mutex_unlock(&src_as->lock);
1248 return EPERM;
1249 }
1250
1251 /*
1252 * Now we are committed to sharing the area.
1253 * First, prepare the area for sharing.
1254 * Then it will be safe to unlock it.
1255 */
1256 share_info_t *sh_info = src_area->sh_info;
1257
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) {
1265 /*
1266 * Call the backend to setup sharing.
1267 * This only happens once for each sh_info.
1268 */
1269 src_area->backend->share(src_area);
1270 }
1271
1272 mutex_unlock(&src_area->lock);
1273 mutex_unlock(&src_as->lock);
1274
1275 /*
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.
1280 * The flags of the source area are masked against dst_flags_mask
1281 * to support sharing in less privileged mode.
1282 */
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);
1286 if (!dst_area) {
1287 /*
1288 * Destination address space area could not be created.
1289 */
1290 sh_info_remove_reference(sh_info);
1291
1292 return ENOMEM;
1293 }
1294
1295 /*
1296 * Now the destination address space area has been
1297 * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
1298 * attribute and set the sh_info.
1299 */
1300 mutex_lock(&dst_as->lock);
1301 mutex_lock(&dst_area->lock);
1302 dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
1303 dst_area->sh_info = sh_info;
1304 mutex_unlock(&dst_area->lock);
1305 mutex_unlock(&dst_as->lock);
1306
1307 return 0;
1308}
1309
1310/** Check access mode for address space area.
1311 *
1312 * @param area Address space area.
1313 * @param access Access mode.
1314 *
1315 * @return False if access violates area's permissions, true
1316 * otherwise.
1317 *
1318 */
1319NO_TRACE bool as_area_check_access(as_area_t *area, pf_access_t access)
1320{
1321 assert(mutex_locked(&area->lock));
1322
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 };
1328
1329 if (!(area->flags & flagmap[access]))
1330 return false;
1331
1332 return true;
1333}
1334
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 */
1342NO_TRACE static unsigned int area_flags_to_page_flags(unsigned int aflags)
1343{
1344 unsigned int flags = PAGE_USER | PAGE_PRESENT;
1345
1346 if (aflags & AS_AREA_READ)
1347 flags |= PAGE_READ;
1348
1349 if (aflags & AS_AREA_WRITE)
1350 flags |= PAGE_WRITE;
1351
1352 if (aflags & AS_AREA_EXEC)
1353 flags |= PAGE_EXEC;
1354
1355 if (aflags & AS_AREA_CACHEABLE)
1356 flags |= PAGE_CACHEABLE;
1357
1358 return flags;
1359}
1360
1361/** Change adress space area flags.
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 *
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.
1373 *
1374 */
1375errno_t as_area_change_flags(as_t *as, unsigned int flags, uintptr_t address)
1376{
1377 /* Flags for the new memory mapping */
1378 unsigned int page_flags = area_flags_to_page_flags(flags);
1379
1380 mutex_lock(&as->lock);
1381
1382 as_area_t *area = find_area_and_lock(as, address);
1383 if (!area) {
1384 mutex_unlock(&as->lock);
1385 return ENOENT;
1386 }
1387
1388 if (area->backend != &anon_backend) {
1389 /* Copying non-anonymous memory not supported yet */
1390 mutex_unlock(&area->lock);
1391 mutex_unlock(&as->lock);
1392 return ENOTSUP;
1393 }
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);
1404
1405 /*
1406 * Compute total number of used pages in the used_space B+tree
1407 */
1408 size_t used_pages = 0;
1409
1410 list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
1411 node) {
1412 btree_key_t i;
1413
1414 for (i = 0; i < node->keys; i++)
1415 used_pages += (size_t) node->value[i];
1416 }
1417
1418 /* An array for storing frame numbers */
1419 uintptr_t *old_frame = malloc(used_pages * sizeof(uintptr_t));
1420 if (!old_frame) {
1421 mutex_unlock(&area->lock);
1422 mutex_unlock(&as->lock);
1423 return ENOMEM;
1424 }
1425
1426 page_table_lock(as, false);
1427
1428 /*
1429 * Start TLB shootdown sequence.
1430 */
1431 ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base,
1432 area->pages);
1433
1434 /*
1435 * Remove used pages from page tables and remember their frame
1436 * numbers.
1437 */
1438 size_t frame_idx = 0;
1439
1440 list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
1441 node) {
1442 btree_key_t i;
1443
1444 for (i = 0; i < node->keys; i++) {
1445 uintptr_t ptr = node->key[i];
1446 size_t size;
1447
1448 for (size = 0; size < (size_t) node->value[i]; size++) {
1449 pte_t pte;
1450 bool found = page_mapping_find(as,
1451 ptr + P2SZ(size), false, &pte);
1452
1453 (void) found;
1454 assert(found);
1455 assert(PTE_VALID(&pte));
1456 assert(PTE_PRESENT(&pte));
1457
1458 old_frame[frame_idx++] = PTE_GET_FRAME(&pte);
1459
1460 /* Remove old mapping */
1461 page_mapping_remove(as, ptr + P2SZ(size));
1462 }
1463 }
1464 }
1465
1466 /*
1467 * Finish TLB shootdown sequence.
1468 */
1469
1470 tlb_invalidate_pages(as->asid, area->base, area->pages);
1471
1472 /*
1473 * Invalidate potential software translation caches
1474 * (e.g. TSB on sparc64, PHT on ppc32).
1475 */
1476 as_invalidate_translation_cache(as, area->base, area->pages);
1477 tlb_shootdown_finalize(ipl);
1478
1479 page_table_unlock(as, false);
1480
1481 /*
1482 * Set the new flags.
1483 */
1484 area->flags = flags;
1485
1486 /*
1487 * Map pages back in with new flags. This step is kept separate
1488 * so that the memory area could not be accesed with both the old and
1489 * the new flags at once.
1490 */
1491 frame_idx = 0;
1492
1493 list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
1494 node) {
1495 btree_key_t i;
1496
1497 for (i = 0; i < node->keys; i++) {
1498 uintptr_t ptr = node->key[i];
1499 size_t size;
1500
1501 for (size = 0; size < (size_t) node->value[i]; size++) {
1502 page_table_lock(as, false);
1503
1504 /* Insert the new mapping */
1505 page_mapping_insert(as, ptr + P2SZ(size),
1506 old_frame[frame_idx++], page_flags);
1507
1508 page_table_unlock(as, false);
1509 }
1510 }
1511 }
1512
1513 free(old_frame);
1514
1515 mutex_unlock(&area->lock);
1516 mutex_unlock(&as->lock);
1517
1518 return 0;
1519}
1520
1521/** Handle page fault within the current address space.
1522 *
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.
1526 *
1527 * Interrupts are assumed disabled.
1528 *
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.
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().
1538 *
1539 */
1540int as_page_fault(uintptr_t address, pf_access_t access, istate_t *istate)
1541{
1542 uintptr_t page = ALIGN_DOWN(address, PAGE_SIZE);
1543 int rc = AS_PF_FAULT;
1544
1545 if (!THREAD)
1546 goto page_fault;
1547
1548 if (!AS)
1549 goto page_fault;
1550
1551 mutex_lock(&AS->lock);
1552 as_area_t *area = find_area_and_lock(AS, page);
1553 if (!area) {
1554 /*
1555 * No area contained mapping for 'page'.
1556 * Signal page fault to low-level handler.
1557 */
1558 mutex_unlock(&AS->lock);
1559 goto page_fault;
1560 }
1561
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 */
1567 mutex_unlock(&area->lock);
1568 mutex_unlock(&AS->lock);
1569 goto page_fault;
1570 }
1571
1572 if ((!area->backend) || (!area->backend->page_fault)) {
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);
1579 goto page_fault;
1580 }
1581
1582 page_table_lock(AS, false);
1583
1584 /*
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.
1587 */
1588 pte_t pte;
1589 bool found = page_mapping_find(AS, page, false, &pte);
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;
1598 }
1599 }
1600
1601 /*
1602 * Resort to the backend page fault handler.
1603 */
1604 rc = area->backend->page_fault(area, page, access);
1605 if (rc != AS_PF_OK) {
1606 page_table_unlock(AS, false);
1607 mutex_unlock(&area->lock);
1608 mutex_unlock(&AS->lock);
1609 goto page_fault;
1610 }
1611
1612 page_table_unlock(AS, false);
1613 mutex_unlock(&area->lock);
1614 mutex_unlock(&AS->lock);
1615 return AS_PF_OK;
1616
1617page_fault:
1618 if (THREAD && THREAD->in_copy_from_uspace) {
1619 THREAD->in_copy_from_uspace = false;
1620 istate_set_retaddr(istate,
1621 (uintptr_t) &memcpy_from_uspace_failover_address);
1622 } else if (THREAD && THREAD->in_copy_to_uspace) {
1623 THREAD->in_copy_to_uspace = false;
1624 istate_set_retaddr(istate,
1625 (uintptr_t) &memcpy_to_uspace_failover_address);
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);
1630 } else {
1631 fault_if_from_uspace(istate, "Page fault: %p.", (void *) address);
1632 panic_memtrap(istate, access, address, NULL);
1633 }
1634
1635 return AS_PF_DEFER;
1636}
1637
1638/** Switch address spaces.
1639 *
1640 * Note that this function cannot sleep as it is essentially a part of
1641 * scheduling. Sleeping here would lead to deadlock on wakeup. Another
1642 * thing which is forbidden in this context is locking the address space.
1643 *
1644 * When this function is entered, no spinlocks may be held.
1645 *
1646 * @param old Old address space or NULL.
1647 * @param new New address space.
1648 *
1649 */
1650void as_switch(as_t *old_as, as_t *new_as)
1651{
1652 DEADLOCK_PROBE_INIT(p_asidlock);
1653 preemption_disable();
1654
1655retry:
1656 (void) interrupts_disable();
1657 if (!spinlock_trylock(&asidlock)) {
1658 /*
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();
1669
1670 /*
1671 * First, take care of the old address space.
1672 */
1673 if (old_as) {
1674 assert(old_as->cpu_refcount);
1675
1676 if ((--old_as->cpu_refcount == 0) && (old_as != AS_KERNEL)) {
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 */
1683 assert(old_as->asid != ASID_INVALID);
1684
1685 list_append(&old_as->inactive_as_with_asid_link,
1686 &inactive_as_with_asid_list);
1687 }
1688
1689 /*
1690 * Perform architecture-specific tasks when the address space
1691 * is being removed from the CPU.
1692 */
1693 as_deinstall_arch(old_as);
1694 }
1695
1696 /*
1697 * Second, prepare the new address space.
1698 */
1699 if ((new_as->cpu_refcount++ == 0) && (new_as != AS_KERNEL)) {
1700 if (new_as->asid != ASID_INVALID)
1701 list_remove(&new_as->inactive_as_with_asid_link);
1702 else
1703 new_as->asid = asid_get();
1704 }
1705
1706#ifdef AS_PAGE_TABLE
1707 SET_PTL0_ADDRESS(new_as->genarch.page_table);
1708#endif
1709
1710 /*
1711 * Perform architecture-specific steps.
1712 * (e.g. write ASID to hardware register etc.)
1713 */
1714 as_install_arch(new_as);
1715
1716 spinlock_unlock(&asidlock);
1717
1718 AS = new_as;
1719}
1720
1721/** Compute flags for virtual address translation subsytem.
1722 *
1723 * @param area Address space area.
1724 *
1725 * @return Flags to be used in page_mapping_insert().
1726 *
1727 */
1728NO_TRACE unsigned int as_area_get_flags(as_area_t *area)
1729{
1730 assert(mutex_locked(&area->lock));
1731
1732 return area_flags_to_page_flags(area->flags);
1733}
1734
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
1765/** Create page table.
1766 *
1767 * Depending on architecture, create either address space private or global page
1768 * table.
1769 *
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.
1774 *
1775 */
1776NO_TRACE pte_t *page_table_create(unsigned int flags)
1777{
1778 assert(as_operations);
1779 assert(as_operations->page_table_create);
1780
1781 return as_operations->page_table_create(flags);
1782}
1783
1784/** Destroy page table.
1785 *
1786 * Destroy page table in architecture specific way.
1787 *
1788 * @param page_table Physical address of PTL0.
1789 *
1790 */
1791NO_TRACE void page_table_destroy(pte_t *page_table)
1792{
1793 assert(as_operations);
1794 assert(as_operations->page_table_destroy);
1795
1796 as_operations->page_table_destroy(page_table);
1797}
1798
1799/** Lock page table.
1800 *
1801 * This function should be called before any page_mapping_insert(),
1802 * page_mapping_remove() and page_mapping_find().
1803 *
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 *
1808 * @param as Address space.
1809 * @param lock If false, do not attempt to lock as->lock.
1810 *
1811 */
1812NO_TRACE void page_table_lock(as_t *as, bool lock)
1813{
1814 assert(as_operations);
1815 assert(as_operations->page_table_lock);
1816
1817 as_operations->page_table_lock(as, lock);
1818}
1819
1820/** Unlock page table.
1821 *
1822 * @param as Address space.
1823 * @param unlock If false, do not attempt to unlock as->lock.
1824 *
1825 */
1826NO_TRACE void page_table_unlock(as_t *as, bool unlock)
1827{
1828 assert(as_operations);
1829 assert(as_operations->page_table_unlock);
1830
1831 as_operations->page_table_unlock(as, unlock);
1832}
1833
1834/** Test whether page tables are locked.
1835 *
1836 * @param as Address space where the page tables belong.
1837 *
1838 * @return True if the page tables belonging to the address soace
1839 * are locked, otherwise false.
1840 */
1841NO_TRACE bool page_table_locked(as_t *as)
1842{
1843 assert(as_operations);
1844 assert(as_operations->page_table_locked);
1845
1846 return as_operations->page_table_locked(as);
1847}
1848
1849/** Return size of the address space area with given base.
1850 *
1851 * @param base Arbitrary address inside the address space area.
1852 *
1853 * @return Size of the address space area in bytes or zero if it
1854 * does not exist.
1855 *
1856 */
1857size_t as_area_get_size(uintptr_t base)
1858{
1859 size_t size;
1860
1861 page_table_lock(AS, true);
1862 as_area_t *src_area = find_area_and_lock(AS, base);
1863
1864 if (src_area) {
1865 size = P2SZ(src_area->pages);
1866 mutex_unlock(&src_area->lock);
1867 } else
1868 size = 0;
1869
1870 page_table_unlock(AS, true);
1871 return size;
1872}
1873
1874/** Mark portion of address space area as used.
1875 *
1876 * The address space area must be already locked.
1877 *
1878 * @param area Address space area.
1879 * @param page First page to be marked.
1880 * @param count Number of page to be marked.
1881 *
1882 * @return False on failure or true on success.
1883 *
1884 */
1885bool used_space_insert(as_area_t *area, uintptr_t page, size_t count)
1886{
1887 assert(mutex_locked(&area->lock));
1888 assert(IS_ALIGNED(page, PAGE_SIZE));
1889 assert(count);
1890
1891 btree_node_t *leaf = NULL;
1892 size_t pages = (size_t) btree_search(&area->used_space, page, &leaf);
1893 if (pages) {
1894 /*
1895 * We hit the beginning of some used space.
1896 */
1897 return false;
1898 }
1899
1900 assert(leaf != NULL);
1901
1902 if (!leaf->keys) {
1903 btree_insert(&area->used_space, page, (void *) count, leaf);
1904 goto success;
1905 }
1906
1907 btree_node_t *node = btree_leaf_node_left_neighbour(&area->used_space, leaf);
1908 if (node) {
1909 uintptr_t left_pg = node->key[node->keys - 1];
1910 uintptr_t right_pg = leaf->key[0];
1911 size_t left_cnt = (size_t) node->value[node->keys - 1];
1912 size_t right_cnt = (size_t) leaf->value[0];
1913
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 */
1919
1920 if (page >= right_pg) {
1921 /* Do nothing. */
1922 } else if (overlaps(page, P2SZ(count), left_pg,
1923 P2SZ(left_cnt))) {
1924 /* The interval intersects with the left interval. */
1925 return false;
1926 } else if (overlaps(page, P2SZ(count), right_pg,
1927 P2SZ(right_cnt))) {
1928 /* The interval intersects with the right interval. */
1929 return false;
1930 } else if ((page == left_pg + P2SZ(left_cnt)) &&
1931 (page + P2SZ(count) == right_pg)) {
1932 /*
1933 * The interval can be added by merging the two already
1934 * present intervals.
1935 */
1936 node->value[node->keys - 1] += count + right_cnt;
1937 btree_remove(&area->used_space, right_pg, leaf);
1938 goto success;
1939 } else if (page == left_pg + P2SZ(left_cnt)) {
1940 /*
1941 * The interval can be added by simply growing the left
1942 * interval.
1943 */
1944 node->value[node->keys - 1] += count;
1945 goto success;
1946 } else if (page + P2SZ(count) == right_pg) {
1947 /*
1948 * The interval can be addded by simply moving base of
1949 * the right interval down and increasing its size
1950 * accordingly.
1951 */
1952 leaf->value[0] += count;
1953 leaf->key[0] = page;
1954 goto success;
1955 } else {
1956 /*
1957 * The interval is between both neigbouring intervals,
1958 * but cannot be merged with any of them.
1959 */
1960 btree_insert(&area->used_space, page, (void *) count,
1961 leaf);
1962 goto success;
1963 }
1964 } else if (page < leaf->key[0]) {
1965 uintptr_t right_pg = leaf->key[0];
1966 size_t right_cnt = (size_t) leaf->value[0];
1967
1968 /*
1969 * Investigate the border case in which the left neighbour does
1970 * not exist but the interval fits from the left.
1971 */
1972
1973 if (overlaps(page, P2SZ(count), right_pg, P2SZ(right_cnt))) {
1974 /* The interval intersects with the right interval. */
1975 return false;
1976 } else if (page + P2SZ(count) == right_pg) {
1977 /*
1978 * The interval can be added by moving the base of the
1979 * right interval down and increasing its size
1980 * accordingly.
1981 */
1982 leaf->key[0] = page;
1983 leaf->value[0] += count;
1984 goto success;
1985 } else {
1986 /*
1987 * The interval doesn't adjoin with the right interval.
1988 * It must be added individually.
1989 */
1990 btree_insert(&area->used_space, page, (void *) count,
1991 leaf);
1992 goto success;
1993 }
1994 }
1995
1996 node = btree_leaf_node_right_neighbour(&area->used_space, leaf);
1997 if (node) {
1998 uintptr_t left_pg = leaf->key[leaf->keys - 1];
1999 uintptr_t right_pg = node->key[0];
2000 size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
2001 size_t right_cnt = (size_t) node->value[0];
2002
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 */
2008
2009 if (page < left_pg) {
2010 /* Do nothing. */
2011 } else if (overlaps(page, P2SZ(count), left_pg,
2012 P2SZ(left_cnt))) {
2013 /* The interval intersects with the left interval. */
2014 return false;
2015 } else if (overlaps(page, P2SZ(count), right_pg,
2016 P2SZ(right_cnt))) {
2017 /* The interval intersects with the right interval. */
2018 return false;
2019 } else if ((page == left_pg + P2SZ(left_cnt)) &&
2020 (page + P2SZ(count) == right_pg)) {
2021 /*
2022 * The interval can be added by merging the two already
2023 * present intervals.
2024 */
2025 leaf->value[leaf->keys - 1] += count + right_cnt;
2026 btree_remove(&area->used_space, right_pg, node);
2027 goto success;
2028 } else if (page == left_pg + P2SZ(left_cnt)) {
2029 /*
2030 * The interval can be added by simply growing the left
2031 * interval.
2032 */
2033 leaf->value[leaf->keys - 1] += count;
2034 goto success;
2035 } else if (page + P2SZ(count) == right_pg) {
2036 /*
2037 * The interval can be addded by simply moving base of
2038 * the right interval down and increasing its size
2039 * accordingly.
2040 */
2041 node->value[0] += count;
2042 node->key[0] = page;
2043 goto success;
2044 } else {
2045 /*
2046 * The interval is between both neigbouring intervals,
2047 * but cannot be merged with any of them.
2048 */
2049 btree_insert(&area->used_space, page, (void *) count,
2050 leaf);
2051 goto success;
2052 }
2053 } else if (page >= leaf->key[leaf->keys - 1]) {
2054 uintptr_t left_pg = leaf->key[leaf->keys - 1];
2055 size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
2056
2057 /*
2058 * Investigate the border case in which the right neighbour
2059 * does not exist but the interval fits from the right.
2060 */
2061
2062 if (overlaps(page, P2SZ(count), left_pg, P2SZ(left_cnt))) {
2063 /* The interval intersects with the left interval. */
2064 return false;
2065 } else if (left_pg + P2SZ(left_cnt) == page) {
2066 /*
2067 * The interval can be added by growing the left
2068 * interval.
2069 */
2070 leaf->value[leaf->keys - 1] += count;
2071 goto success;
2072 } else {
2073 /*
2074 * The interval doesn't adjoin with the left interval.
2075 * It must be added individually.
2076 */
2077 btree_insert(&area->used_space, page, (void *) count,
2078 leaf);
2079 goto success;
2080 }
2081 }
2082
2083 /*
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.
2087 */
2088 btree_key_t i;
2089 for (i = 1; i < leaf->keys; i++) {
2090 if (page < leaf->key[i]) {
2091 uintptr_t left_pg = leaf->key[i - 1];
2092 uintptr_t right_pg = leaf->key[i];
2093 size_t left_cnt = (size_t) leaf->value[i - 1];
2094 size_t right_cnt = (size_t) leaf->value[i];
2095
2096 /*
2097 * The interval fits between left_pg and right_pg.
2098 */
2099
2100 if (overlaps(page, P2SZ(count), left_pg,
2101 P2SZ(left_cnt))) {
2102 /*
2103 * The interval intersects with the left
2104 * interval.
2105 */
2106 return false;
2107 } else if (overlaps(page, P2SZ(count), right_pg,
2108 P2SZ(right_cnt))) {
2109 /*
2110 * The interval intersects with the right
2111 * interval.
2112 */
2113 return false;
2114 } else if ((page == left_pg + P2SZ(left_cnt)) &&
2115 (page + P2SZ(count) == right_pg)) {
2116 /*
2117 * The interval can be added by merging the two
2118 * already present intervals.
2119 */
2120 leaf->value[i - 1] += count + right_cnt;
2121 btree_remove(&area->used_space, right_pg, leaf);
2122 goto success;
2123 } else if (page == left_pg + P2SZ(left_cnt)) {
2124 /*
2125 * The interval can be added by simply growing
2126 * the left interval.
2127 */
2128 leaf->value[i - 1] += count;
2129 goto success;
2130 } else if (page + P2SZ(count) == right_pg) {
2131 /*
2132 * The interval can be addded by simply moving
2133 * base of the right interval down and
2134 * increasing its size accordingly.
2135 */
2136 leaf->value[i] += count;
2137 leaf->key[i] = page;
2138 goto success;
2139 } else {
2140 /*
2141 * The interval is between both neigbouring
2142 * intervals, but cannot be merged with any of
2143 * them.
2144 */
2145 btree_insert(&area->used_space, page,
2146 (void *) count, leaf);
2147 goto success;
2148 }
2149 }
2150 }
2151
2152 panic("Inconsistency detected while adding %zu pages of used "
2153 "space at %p.", count, (void *) page);
2154
2155success:
2156 area->resident += count;
2157 return true;
2158}
2159
2160/** Mark portion of address space area as unused.
2161 *
2162 * The address space area must be already locked.
2163 *
2164 * @param area Address space area.
2165 * @param page First page to be marked.
2166 * @param count Number of page to be marked.
2167 *
2168 * @return False on failure or true on success.
2169 *
2170 */
2171bool used_space_remove(as_area_t *area, uintptr_t page, size_t count)
2172{
2173 assert(mutex_locked(&area->lock));
2174 assert(IS_ALIGNED(page, PAGE_SIZE));
2175 assert(count);
2176
2177 btree_node_t *leaf;
2178 size_t pages = (size_t) btree_search(&area->used_space, page, &leaf);
2179 if (pages) {
2180 /*
2181 * We are lucky, page is the beginning of some interval.
2182 */
2183 if (count > pages) {
2184 return false;
2185 } else if (count == pages) {
2186 btree_remove(&area->used_space, page, leaf);
2187 goto success;
2188 } else {
2189 /*
2190 * Find the respective interval.
2191 * Decrease its size and relocate its start address.
2192 */
2193 btree_key_t i;
2194 for (i = 0; i < leaf->keys; i++) {
2195 if (leaf->key[i] == page) {
2196 leaf->key[i] += P2SZ(count);
2197 leaf->value[i] -= count;
2198 goto success;
2199 }
2200 }
2201
2202 goto error;
2203 }
2204 }
2205
2206 btree_node_t *node = btree_leaf_node_left_neighbour(&area->used_space,
2207 leaf);
2208 if ((node) && (page < leaf->key[0])) {
2209 uintptr_t left_pg = node->key[node->keys - 1];
2210 size_t left_cnt = (size_t) node->value[node->keys - 1];
2211
2212 if (overlaps(left_pg, P2SZ(left_cnt), page, P2SZ(count))) {
2213 if (page + P2SZ(count) == left_pg + P2SZ(left_cnt)) {
2214 /*
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.
2219 */
2220 node->value[node->keys - 1] -= count;
2221 goto success;
2222 } else if (page + P2SZ(count) <
2223 left_pg + P2SZ(left_cnt)) {
2224 size_t new_cnt;
2225
2226 /*
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.
2232 */
2233 new_cnt = ((left_pg + P2SZ(left_cnt)) -
2234 (page + P2SZ(count))) >> PAGE_WIDTH;
2235 node->value[node->keys - 1] -= count + new_cnt;
2236 btree_insert(&area->used_space, page +
2237 P2SZ(count), (void *) new_cnt, leaf);
2238 goto success;
2239 }
2240 }
2241
2242 return false;
2243 } else if (page < leaf->key[0])
2244 return false;
2245
2246 if (page > leaf->key[leaf->keys - 1]) {
2247 uintptr_t left_pg = leaf->key[leaf->keys - 1];
2248 size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
2249
2250 if (overlaps(left_pg, P2SZ(left_cnt), page, P2SZ(count))) {
2251 if (page + P2SZ(count) == left_pg + P2SZ(left_cnt)) {
2252 /*
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.
2256 */
2257 leaf->value[leaf->keys - 1] -= count;
2258 goto success;
2259 } else if (page + P2SZ(count) < left_pg +
2260 P2SZ(left_cnt)) {
2261 size_t new_cnt;
2262
2263 /*
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.
2269 */
2270 new_cnt = ((left_pg + P2SZ(left_cnt)) -
2271 (page + P2SZ(count))) >> PAGE_WIDTH;
2272 leaf->value[leaf->keys - 1] -= count + new_cnt;
2273 btree_insert(&area->used_space, page +
2274 P2SZ(count), (void *) new_cnt, leaf);
2275 goto success;
2276 }
2277 }
2278
2279 return false;
2280 }
2281
2282 /*
2283 * The border cases have been already resolved.
2284 * Now the interval can be only between intervals of the leaf.
2285 */
2286 btree_key_t i;
2287 for (i = 1; i < leaf->keys - 1; i++) {
2288 if (page < leaf->key[i]) {
2289 uintptr_t left_pg = leaf->key[i - 1];
2290 size_t left_cnt = (size_t) leaf->value[i - 1];
2291
2292 /*
2293 * Now the interval is between intervals corresponding
2294 * to (i - 1) and i.
2295 */
2296 if (overlaps(left_pg, P2SZ(left_cnt), page,
2297 P2SZ(count))) {
2298 if (page + P2SZ(count) ==
2299 left_pg + P2SZ(left_cnt)) {
2300 /*
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.
2305 */
2306 leaf->value[i - 1] -= count;
2307 goto success;
2308 } else if (page + P2SZ(count) <
2309 left_pg + P2SZ(left_cnt)) {
2310 size_t new_cnt;
2311
2312 /*
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
2317 * also inserting a new interval.
2318 */
2319 new_cnt = ((left_pg + P2SZ(left_cnt)) -
2320 (page + P2SZ(count))) >>
2321 PAGE_WIDTH;
2322 leaf->value[i - 1] -= count + new_cnt;
2323 btree_insert(&area->used_space, page +
2324 P2SZ(count), (void *) new_cnt,
2325 leaf);
2326 goto success;
2327 }
2328 }
2329
2330 return false;
2331 }
2332 }
2333
2334error:
2335 panic("Inconsistency detected while removing %zu pages of used "
2336 "space from %p.", count, (void *) page);
2337
2338success:
2339 area->resident -= count;
2340 return true;
2341}
2342
2343/*
2344 * Address space related syscalls.
2345 */
2346
2347sysarg_t sys_as_area_create(uintptr_t base, size_t size, unsigned int flags,
2348 uintptr_t bound, as_area_pager_info_t *pager_info)
2349{
2350 uintptr_t virt = base;
2351 mem_backend_t *backend;
2352 mem_backend_data_t backend_data;
2353
2354 if (pager_info == AS_AREA_UNPAGED)
2355 backend = &anon_backend;
2356 else {
2357 backend = &user_backend;
2358 if (copy_from_uspace(&backend_data.pager_info, pager_info,
2359 sizeof(as_area_pager_info_t)) != EOK) {
2360 return (sysarg_t) AS_MAP_FAILED;
2361 }
2362 }
2363 as_area_t *area = as_area_create(AS, flags, size,
2364 AS_AREA_ATTR_NONE, backend, &backend_data, &virt, bound);
2365 if (area == NULL)
2366 return (sysarg_t) AS_MAP_FAILED;
2367
2368 return (sysarg_t) virt;
2369}
2370
2371sys_errno_t sys_as_area_resize(uintptr_t address, size_t size, unsigned int flags)
2372{
2373 return (sys_errno_t) as_area_resize(AS, address, size, 0);
2374}
2375
2376sys_errno_t sys_as_area_change_flags(uintptr_t address, unsigned int flags)
2377{
2378 return (sys_errno_t) as_area_change_flags(AS, flags, address);
2379}
2380
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
2401sys_errno_t sys_as_area_destroy(uintptr_t address)
2402{
2403 return (sys_errno_t) as_area_destroy(AS, address);
2404}
2405
2406/** Get list of adress space areas.
2407 *
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 *
2412 */
2413as_area_info_t *as_get_area_info(as_t *as, size_t *osize)
2414{
2415 mutex_lock(&as->lock);
2416
2417 /* Count number of areas. */
2418 size_t area_cnt = odict_count(&as->as_areas);
2419
2420 size_t isize = area_cnt * sizeof(as_area_info_t);
2421 as_area_info_t *info = malloc(isize);
2422 if (!info) {
2423 mutex_unlock(&as->lock);
2424 return NULL;
2425 }
2426
2427 /* Record area data. */
2428
2429 size_t area_idx = 0;
2430
2431 as_area_t *area = as_area_first(as);
2432 while (area != NULL) {
2433 assert(area_idx < area_cnt);
2434 mutex_lock(&area->lock);
2435
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;
2440
2441 mutex_unlock(&area->lock);
2442 area = as_area_next(area);
2443 }
2444
2445 mutex_unlock(&as->lock);
2446
2447 *osize = isize;
2448 return info;
2449}
2450
2451/** Print out information about address space.
2452 *
2453 * @param as Address space.
2454 *
2455 */
2456void as_print(as_t *as)
2457{
2458 mutex_lock(&as->lock);
2459
2460 /* Print out info about address space areas */
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);
2469
2470 area = as_area_next(area);
2471 }
2472
2473 mutex_unlock(&as->lock);
2474}
2475
2476/** @}
2477 */
Note: See TracBrowser for help on using the repository browser.