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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6a75c134 was 315b7e3, checked in by Martin Decky <martin@…>, 10 years ago

avoid the local variable 'size' hiding function argument 'size'
(reported by Coverity as CID 10364)

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