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

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

Merge from lp:~jakub/helenos/mm.

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