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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since abfc9f3 was f97f1e51, checked in by Martin Decky <martin@…>, 13 years ago

unify slab cache naming scheme (according to the type name)

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