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

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

Avoid blocking callpaths in TLB shootdown sequences.

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