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

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

Call the address space area create, resize and destroy hooks.

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