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

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

Do not disable interrupts unnecessarily.

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