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

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

Unify the use of virtual addresses and virtual page addresses in mm code.

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