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

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

Merge mainline changes.

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