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

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

major code revision

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