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

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

Fix a newly introduced deadlock in the TLB shootdown algorithm.

  • Property mode set to 100644
File size: 51.7 KB
Line 
1/*
2 * Copyright (c) 2010 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genericmm
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Address space related functions.
36 *
37 * This file contains address space manipulation functions.
38 * Roughly speaking, this is a higher-level client of
39 * Virtual Address Translation (VAT) subsystem.
40 *
41 * Functionality provided by this file allows one to
42 * create address spaces and create, resize and share
43 * address space areas.
44 *
45 * @see page.c
46 *
47 */
48
49#include <mm/as.h>
50#include <arch/mm/as.h>
51#include <mm/page.h>
52#include <mm/frame.h>
53#include <mm/slab.h>
54#include <mm/tlb.h>
55#include <arch/mm/page.h>
56#include <genarch/mm/page_pt.h>
57#include <genarch/mm/page_ht.h>
58#include <mm/asid.h>
59#include <arch/mm/asid.h>
60#include <preemption.h>
61#include <synch/spinlock.h>
62#include <synch/mutex.h>
63#include <adt/list.h>
64#include <adt/btree.h>
65#include <proc/task.h>
66#include <proc/thread.h>
67#include <arch/asm.h>
68#include <panic.h>
69#include <debug.h>
70#include <print.h>
71#include <memstr.h>
72#include <macros.h>
73#include <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 ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid,
436 area->base + 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(ipl);
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 ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base,
581 area->pages);
582
583 /*
584 * Visit only the pages mapped by used_space B+tree.
585 */
586 link_t *cur;
587 for (cur = area->used_space.leaf_head.next;
588 cur != &area->used_space.leaf_head; cur = cur->next) {
589 btree_node_t *node;
590 btree_key_t i;
591
592 node = list_get_instance(cur, btree_node_t, leaf_link);
593 for (i = 0; i < node->keys; i++) {
594 uintptr_t ptr = node->key[i];
595 size_t size;
596
597 for (size = 0; size < (size_t) node->value[i]; size++) {
598 pte_t *pte = page_mapping_find(as, ptr + size * PAGE_SIZE);
599
600 ASSERT(pte);
601 ASSERT(PTE_VALID(pte));
602 ASSERT(PTE_PRESENT(pte));
603
604 if ((area->backend) &&
605 (area->backend->frame_free)) {
606 area->backend->frame_free(area,
607 ptr + size * PAGE_SIZE, PTE_GET_FRAME(pte));
608 }
609
610 page_mapping_remove(as, ptr + size * PAGE_SIZE);
611 }
612 }
613 }
614
615 /*
616 * Finish TLB shootdown sequence.
617 *
618 */
619
620 tlb_invalidate_pages(as->asid, area->base, area->pages);
621
622 /*
623 * Invalidate potential software translation caches (e.g. TSB on
624 * sparc64).
625 *
626 */
627 as_invalidate_translation_cache(as, area->base, area->pages);
628 tlb_shootdown_finalize(ipl);
629
630 page_table_unlock(as, false);
631
632 btree_destroy(&area->used_space);
633
634 area->attributes |= AS_AREA_ATTR_PARTIAL;
635
636 if (area->sh_info)
637 sh_info_remove_reference(area->sh_info);
638
639 mutex_unlock(&area->lock);
640
641 /*
642 * Remove the empty area from address space.
643 *
644 */
645 btree_remove(&as->as_area_btree, base, NULL);
646
647 free(area);
648
649 mutex_unlock(&as->lock);
650 return 0;
651}
652
653/** Share address space area with another or the same address space.
654 *
655 * Address space area mapping is shared with a new address space area.
656 * If the source address space area has not been shared so far,
657 * a new sh_info is created. The new address space area simply gets the
658 * sh_info of the source area. The process of duplicating the
659 * mapping is done through the backend share function.
660 *
661 * @param src_as Pointer to source address space.
662 * @param src_base Base address of the source address space area.
663 * @param acc_size Expected size of the source area.
664 * @param dst_as Pointer to destination address space.
665 * @param dst_base Target base address.
666 * @param dst_flags_mask Destination address space area flags mask.
667 *
668 * @return Zero on success.
669 * @return ENOENT if there is no such task or such address space.
670 * @return EPERM if there was a problem in accepting the area.
671 * @return ENOMEM if there was a problem in allocating destination
672 * address space area.
673 * @return ENOTSUP if the address space area backend does not support
674 * sharing.
675 *
676 */
677int as_area_share(as_t *src_as, uintptr_t src_base, size_t acc_size,
678 as_t *dst_as, uintptr_t dst_base, unsigned int dst_flags_mask)
679{
680 mutex_lock(&src_as->lock);
681 as_area_t *src_area = find_area_and_lock(src_as, src_base);
682 if (!src_area) {
683 /*
684 * Could not find the source address space area.
685 *
686 */
687 mutex_unlock(&src_as->lock);
688 return ENOENT;
689 }
690
691 if ((!src_area->backend) || (!src_area->backend->share)) {
692 /*
693 * There is no backend or the backend does not
694 * know how to share the area.
695 *
696 */
697 mutex_unlock(&src_area->lock);
698 mutex_unlock(&src_as->lock);
699 return ENOTSUP;
700 }
701
702 size_t src_size = src_area->pages * PAGE_SIZE;
703 unsigned int src_flags = src_area->flags;
704 mem_backend_t *src_backend = src_area->backend;
705 mem_backend_data_t src_backend_data = src_area->backend_data;
706
707 /* Share the cacheable flag from the original mapping */
708 if (src_flags & AS_AREA_CACHEABLE)
709 dst_flags_mask |= AS_AREA_CACHEABLE;
710
711 if ((src_size != acc_size) ||
712 ((src_flags & dst_flags_mask) != dst_flags_mask)) {
713 mutex_unlock(&src_area->lock);
714 mutex_unlock(&src_as->lock);
715 return EPERM;
716 }
717
718 /*
719 * Now we are committed to sharing the area.
720 * First, prepare the area for sharing.
721 * Then it will be safe to unlock it.
722 *
723 */
724 share_info_t *sh_info = src_area->sh_info;
725 if (!sh_info) {
726 sh_info = (share_info_t *) malloc(sizeof(share_info_t), 0);
727 mutex_initialize(&sh_info->lock, MUTEX_PASSIVE);
728 sh_info->refcount = 2;
729 btree_create(&sh_info->pagemap);
730 src_area->sh_info = sh_info;
731
732 /*
733 * Call the backend to setup sharing.
734 *
735 */
736 src_area->backend->share(src_area);
737 } else {
738 mutex_lock(&sh_info->lock);
739 sh_info->refcount++;
740 mutex_unlock(&sh_info->lock);
741 }
742
743 mutex_unlock(&src_area->lock);
744 mutex_unlock(&src_as->lock);
745
746 /*
747 * Create copy of the source address space area.
748 * The destination area is created with AS_AREA_ATTR_PARTIAL
749 * attribute set which prevents race condition with
750 * preliminary as_page_fault() calls.
751 * The flags of the source area are masked against dst_flags_mask
752 * to support sharing in less privileged mode.
753 *
754 */
755 as_area_t *dst_area = as_area_create(dst_as, dst_flags_mask, src_size,
756 dst_base, AS_AREA_ATTR_PARTIAL, src_backend, &src_backend_data);
757 if (!dst_area) {
758 /*
759 * Destination address space area could not be created.
760 */
761 sh_info_remove_reference(sh_info);
762
763 return ENOMEM;
764 }
765
766 /*
767 * Now the destination address space area has been
768 * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
769 * attribute and set the sh_info.
770 *
771 */
772 mutex_lock(&dst_as->lock);
773 mutex_lock(&dst_area->lock);
774 dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
775 dst_area->sh_info = sh_info;
776 mutex_unlock(&dst_area->lock);
777 mutex_unlock(&dst_as->lock);
778
779 return 0;
780}
781
782/** Check access mode for address space area.
783 *
784 * @param area Address space area.
785 * @param access Access mode.
786 *
787 * @return False if access violates area's permissions, true
788 * otherwise.
789 *
790 */
791bool as_area_check_access(as_area_t *area, pf_access_t access)
792{
793 int flagmap[] = {
794 [PF_ACCESS_READ] = AS_AREA_READ,
795 [PF_ACCESS_WRITE] = AS_AREA_WRITE,
796 [PF_ACCESS_EXEC] = AS_AREA_EXEC
797 };
798
799 ASSERT(mutex_locked(&area->lock));
800
801 if (!(area->flags & flagmap[access]))
802 return false;
803
804 return true;
805}
806
807/** Change adress space area flags.
808 *
809 * The idea is to have the same data, but with a different access mode.
810 * This is needed e.g. for writing code into memory and then executing it.
811 * In order for this to work properly, this may copy the data
812 * into private anonymous memory (unless it's already there).
813 *
814 * @param as Address space.
815 * @param flags Flags of the area memory.
816 * @param address Address within the area to be changed.
817 *
818 * @return Zero on success or a value from @ref errno.h on failure.
819 *
820 */
821int as_area_change_flags(as_t *as, unsigned int flags, uintptr_t address)
822{
823 /* Flags for the new memory mapping */
824 unsigned int page_flags = area_flags_to_page_flags(flags);
825
826 mutex_lock(&as->lock);
827
828 as_area_t *area = find_area_and_lock(as, address);
829 if (!area) {
830 mutex_unlock(&as->lock);
831 return ENOENT;
832 }
833
834 if ((area->sh_info) || (area->backend != &anon_backend)) {
835 /* Copying shared areas not supported yet */
836 /* Copying non-anonymous memory not supported yet */
837 mutex_unlock(&area->lock);
838 mutex_unlock(&as->lock);
839 return ENOTSUP;
840 }
841
842 /*
843 * Compute total number of used pages in the used_space B+tree
844 *
845 */
846 size_t used_pages = 0;
847 link_t *cur;
848
849 for (cur = area->used_space.leaf_head.next;
850 cur != &area->used_space.leaf_head; cur = cur->next) {
851 btree_node_t *node
852 = list_get_instance(cur, btree_node_t, leaf_link);
853 btree_key_t i;
854
855 for (i = 0; i < node->keys; i++)
856 used_pages += (size_t) node->value[i];
857 }
858
859 /* An array for storing frame numbers */
860 uintptr_t *old_frame = malloc(used_pages * sizeof(uintptr_t), 0);
861
862 page_table_lock(as, false);
863
864 /*
865 * Start TLB shootdown sequence.
866 *
867 */
868 ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base,
869 area->pages);
870
871 /*
872 * Remove used pages from page tables and remember their frame
873 * numbers.
874 *
875 */
876 size_t frame_idx = 0;
877
878 for (cur = area->used_space.leaf_head.next;
879 cur != &area->used_space.leaf_head; cur = cur->next) {
880 btree_node_t *node
881 = list_get_instance(cur, btree_node_t, leaf_link);
882 btree_key_t i;
883
884 for (i = 0; i < node->keys; i++) {
885 uintptr_t ptr = node->key[i];
886 size_t size;
887
888 for (size = 0; size < (size_t) node->value[i]; size++) {
889 pte_t *pte = page_mapping_find(as, ptr + size * PAGE_SIZE);
890
891 ASSERT(pte);
892 ASSERT(PTE_VALID(pte));
893 ASSERT(PTE_PRESENT(pte));
894
895 old_frame[frame_idx++] = PTE_GET_FRAME(pte);
896
897 /* Remove old mapping */
898 page_mapping_remove(as, ptr + size * PAGE_SIZE);
899 }
900 }
901 }
902
903 /*
904 * Finish TLB shootdown sequence.
905 *
906 */
907
908 tlb_invalidate_pages(as->asid, area->base, area->pages);
909
910 /*
911 * Invalidate potential software translation caches (e.g. TSB on
912 * sparc64).
913 *
914 */
915 as_invalidate_translation_cache(as, area->base, area->pages);
916 tlb_shootdown_finalize(ipl);
917
918 page_table_unlock(as, false);
919
920 /*
921 * Set the new flags.
922 */
923 area->flags = flags;
924
925 /*
926 * Map pages back in with new flags. This step is kept separate
927 * so that the memory area could not be accesed with both the old and
928 * the new flags at once.
929 */
930 frame_idx = 0;
931
932 for (cur = area->used_space.leaf_head.next;
933 cur != &area->used_space.leaf_head; cur = cur->next) {
934 btree_node_t *node
935 = list_get_instance(cur, btree_node_t, leaf_link);
936 btree_key_t i;
937
938 for (i = 0; i < node->keys; i++) {
939 uintptr_t ptr = node->key[i];
940 size_t size;
941
942 for (size = 0; size < (size_t) node->value[i]; size++) {
943 page_table_lock(as, false);
944
945 /* Insert the new mapping */
946 page_mapping_insert(as, ptr + size * PAGE_SIZE,
947 old_frame[frame_idx++], page_flags);
948
949 page_table_unlock(as, false);
950 }
951 }
952 }
953
954 free(old_frame);
955
956 mutex_unlock(&area->lock);
957 mutex_unlock(&as->lock);
958
959 return 0;
960}
961
962/** Handle page fault within the current address space.
963 *
964 * This is the high-level page fault handler. It decides whether the page fault
965 * can be resolved by any backend and if so, it invokes the backend to resolve
966 * the page fault.
967 *
968 * Interrupts are assumed disabled.
969 *
970 * @param page Faulting page.
971 * @param access Access mode that caused the page fault (i.e.
972 * read/write/exec).
973 * @param istate Pointer to the interrupted state.
974 *
975 * @return AS_PF_FAULT on page fault.
976 * @return AS_PF_OK on success.
977 * @return AS_PF_DEFER if the fault was caused by copy_to_uspace()
978 * or copy_from_uspace().
979 *
980 */
981int as_page_fault(uintptr_t page, pf_access_t access, istate_t *istate)
982{
983 if (!THREAD)
984 return AS_PF_FAULT;
985
986 if (!AS)
987 return AS_PF_FAULT;
988
989 mutex_lock(&AS->lock);
990 as_area_t *area = find_area_and_lock(AS, page);
991 if (!area) {
992 /*
993 * No area contained mapping for 'page'.
994 * Signal page fault to low-level handler.
995 *
996 */
997 mutex_unlock(&AS->lock);
998 goto page_fault;
999 }
1000
1001 if (area->attributes & AS_AREA_ATTR_PARTIAL) {
1002 /*
1003 * The address space area is not fully initialized.
1004 * Avoid possible race by returning error.
1005 */
1006 mutex_unlock(&area->lock);
1007 mutex_unlock(&AS->lock);
1008 goto page_fault;
1009 }
1010
1011 if ((!area->backend) || (!area->backend->page_fault)) {
1012 /*
1013 * The address space area is not backed by any backend
1014 * or the backend cannot handle page faults.
1015 *
1016 */
1017 mutex_unlock(&area->lock);
1018 mutex_unlock(&AS->lock);
1019 goto page_fault;
1020 }
1021
1022 page_table_lock(AS, false);
1023
1024 /*
1025 * To avoid race condition between two page faults on the same address,
1026 * we need to make sure the mapping has not been already inserted.
1027 *
1028 */
1029 pte_t *pte;
1030 if ((pte = page_mapping_find(AS, page))) {
1031 if (PTE_PRESENT(pte)) {
1032 if (((access == PF_ACCESS_READ) && PTE_READABLE(pte)) ||
1033 (access == PF_ACCESS_WRITE && PTE_WRITABLE(pte)) ||
1034 (access == PF_ACCESS_EXEC && PTE_EXECUTABLE(pte))) {
1035 page_table_unlock(AS, false);
1036 mutex_unlock(&area->lock);
1037 mutex_unlock(&AS->lock);
1038 return AS_PF_OK;
1039 }
1040 }
1041 }
1042
1043 /*
1044 * Resort to the backend page fault handler.
1045 *
1046 */
1047 if (area->backend->page_fault(area, page, access) != AS_PF_OK) {
1048 page_table_unlock(AS, false);
1049 mutex_unlock(&area->lock);
1050 mutex_unlock(&AS->lock);
1051 goto page_fault;
1052 }
1053
1054 page_table_unlock(AS, false);
1055 mutex_unlock(&area->lock);
1056 mutex_unlock(&AS->lock);
1057 return AS_PF_OK;
1058
1059page_fault:
1060 if (THREAD->in_copy_from_uspace) {
1061 THREAD->in_copy_from_uspace = false;
1062 istate_set_retaddr(istate,
1063 (uintptr_t) &memcpy_from_uspace_failover_address);
1064 } else if (THREAD->in_copy_to_uspace) {
1065 THREAD->in_copy_to_uspace = false;
1066 istate_set_retaddr(istate,
1067 (uintptr_t) &memcpy_to_uspace_failover_address);
1068 } else {
1069 return AS_PF_FAULT;
1070 }
1071
1072 return AS_PF_DEFER;
1073}
1074
1075/** Switch address spaces.
1076 *
1077 * Note that this function cannot sleep as it is essentially a part of
1078 * scheduling. Sleeping here would lead to deadlock on wakeup. Another
1079 * thing which is forbidden in this context is locking the address space.
1080 *
1081 * When this function is enetered, no spinlocks may be held.
1082 *
1083 * @param old Old address space or NULL.
1084 * @param new New address space.
1085 *
1086 */
1087void as_switch(as_t *old_as, as_t *new_as)
1088{
1089 DEADLOCK_PROBE_INIT(p_asidlock);
1090 preemption_disable();
1091
1092retry:
1093 (void) interrupts_disable();
1094 if (!spinlock_trylock(&asidlock)) {
1095 /*
1096 * Avoid deadlock with TLB shootdown.
1097 * We can enable interrupts here because
1098 * preemption is disabled. We should not be
1099 * holding any other lock.
1100 *
1101 */
1102 (void) interrupts_enable();
1103 DEADLOCK_PROBE(p_asidlock, DEADLOCK_THRESHOLD);
1104 goto retry;
1105 }
1106 preemption_enable();
1107
1108 /*
1109 * First, take care of the old address space.
1110 */
1111 if (old_as) {
1112 ASSERT(old_as->cpu_refcount);
1113
1114 if ((--old_as->cpu_refcount == 0) && (old_as != AS_KERNEL)) {
1115 /*
1116 * The old address space is no longer active on
1117 * any processor. It can be appended to the
1118 * list of inactive address spaces with assigned
1119 * ASID.
1120 *
1121 */
1122 ASSERT(old_as->asid != ASID_INVALID);
1123
1124 list_append(&old_as->inactive_as_with_asid_link,
1125 &inactive_as_with_asid_head);
1126 }
1127
1128 /*
1129 * Perform architecture-specific tasks when the address space
1130 * is being removed from the CPU.
1131 *
1132 */
1133 as_deinstall_arch(old_as);
1134 }
1135
1136 /*
1137 * Second, prepare the new address space.
1138 *
1139 */
1140 if ((new_as->cpu_refcount++ == 0) && (new_as != AS_KERNEL)) {
1141 if (new_as->asid != ASID_INVALID)
1142 list_remove(&new_as->inactive_as_with_asid_link);
1143 else
1144 new_as->asid = asid_get();
1145 }
1146
1147#ifdef AS_PAGE_TABLE
1148 SET_PTL0_ADDRESS(new_as->genarch.page_table);
1149#endif
1150
1151 /*
1152 * Perform architecture-specific steps.
1153 * (e.g. write ASID to hardware register etc.)
1154 *
1155 */
1156 as_install_arch(new_as);
1157
1158 spinlock_unlock(&asidlock);
1159
1160 AS = new_as;
1161}
1162
1163/** Convert address space area flags to page flags.
1164 *
1165 * @param aflags Flags of some address space area.
1166 *
1167 * @return Flags to be passed to page_mapping_insert().
1168 *
1169 */
1170unsigned int area_flags_to_page_flags(unsigned int aflags)
1171{
1172 unsigned int flags = PAGE_USER | PAGE_PRESENT;
1173
1174 if (aflags & AS_AREA_READ)
1175 flags |= PAGE_READ;
1176
1177 if (aflags & AS_AREA_WRITE)
1178 flags |= PAGE_WRITE;
1179
1180 if (aflags & AS_AREA_EXEC)
1181 flags |= PAGE_EXEC;
1182
1183 if (aflags & AS_AREA_CACHEABLE)
1184 flags |= PAGE_CACHEABLE;
1185
1186 return flags;
1187}
1188
1189/** Compute flags for virtual address translation subsytem.
1190 *
1191 * @param area Address space area.
1192 *
1193 * @return Flags to be used in page_mapping_insert().
1194 *
1195 */
1196unsigned int as_area_get_flags(as_area_t *area)
1197{
1198 ASSERT(mutex_locked(&area->lock));
1199
1200 return area_flags_to_page_flags(area->flags);
1201}
1202
1203/** Create page table.
1204 *
1205 * Depending on architecture, create either address space private or global page
1206 * table.
1207 *
1208 * @param flags Flags saying whether the page table is for the kernel
1209 * address space.
1210 *
1211 * @return First entry of the page table.
1212 *
1213 */
1214pte_t *page_table_create(unsigned int flags)
1215{
1216 ASSERT(as_operations);
1217 ASSERT(as_operations->page_table_create);
1218
1219 return as_operations->page_table_create(flags);
1220}
1221
1222/** Destroy page table.
1223 *
1224 * Destroy page table in architecture specific way.
1225 *
1226 * @param page_table Physical address of PTL0.
1227 *
1228 */
1229void page_table_destroy(pte_t *page_table)
1230{
1231 ASSERT(as_operations);
1232 ASSERT(as_operations->page_table_destroy);
1233
1234 as_operations->page_table_destroy(page_table);
1235}
1236
1237/** Lock page table.
1238 *
1239 * This function should be called before any page_mapping_insert(),
1240 * page_mapping_remove() and page_mapping_find().
1241 *
1242 * Locking order is such that address space areas must be locked
1243 * prior to this call. Address space can be locked prior to this
1244 * call in which case the lock argument is false.
1245 *
1246 * @param as Address space.
1247 * @param lock If false, do not attempt to lock as->lock.
1248 *
1249 */
1250void page_table_lock(as_t *as, bool lock)
1251{
1252 ASSERT(as_operations);
1253 ASSERT(as_operations->page_table_lock);
1254
1255 as_operations->page_table_lock(as, lock);
1256}
1257
1258/** Unlock page table.
1259 *
1260 * @param as Address space.
1261 * @param unlock If false, do not attempt to unlock as->lock.
1262 *
1263 */
1264void page_table_unlock(as_t *as, bool unlock)
1265{
1266 ASSERT(as_operations);
1267 ASSERT(as_operations->page_table_unlock);
1268
1269 as_operations->page_table_unlock(as, unlock);
1270}
1271
1272/** Test whether page tables are locked.
1273 *
1274 * @param as Address space where the page tables belong.
1275 *
1276 * @return True if the page tables belonging to the address soace
1277 * are locked, otherwise false.
1278 */
1279bool page_table_locked(as_t *as)
1280{
1281 ASSERT(as_operations);
1282 ASSERT(as_operations->page_table_locked);
1283
1284 return as_operations->page_table_locked(as);
1285}
1286
1287
1288/** Find address space area and lock it.
1289 *
1290 * @param as Address space.
1291 * @param va Virtual address.
1292 *
1293 * @return Locked address space area containing va on success or
1294 * NULL on failure.
1295 *
1296 */
1297as_area_t *find_area_and_lock(as_t *as, uintptr_t va)
1298{
1299 ASSERT(mutex_locked(&as->lock));
1300
1301 btree_node_t *leaf;
1302 as_area_t *area = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
1303 if (area) {
1304 /* va is the base address of an address space area */
1305 mutex_lock(&area->lock);
1306 return area;
1307 }
1308
1309 /*
1310 * Search the leaf node and the righmost record of its left neighbour
1311 * to find out whether this is a miss or va belongs to an address
1312 * space area found there.
1313 *
1314 */
1315
1316 /* First, search the leaf node itself. */
1317 btree_key_t i;
1318
1319 for (i = 0; i < leaf->keys; i++) {
1320 area = (as_area_t *) leaf->value[i];
1321
1322 mutex_lock(&area->lock);
1323
1324 if ((area->base <= va) && (va < area->base + area->pages * PAGE_SIZE))
1325 return area;
1326
1327 mutex_unlock(&area->lock);
1328 }
1329
1330 /*
1331 * Second, locate the left neighbour and test its last record.
1332 * Because of its position in the B+tree, it must have base < va.
1333 *
1334 */
1335 btree_node_t *lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf);
1336 if (lnode) {
1337 area = (as_area_t *) lnode->value[lnode->keys - 1];
1338
1339 mutex_lock(&area->lock);
1340
1341 if (va < area->base + area->pages * PAGE_SIZE)
1342 return area;
1343
1344 mutex_unlock(&area->lock);
1345 }
1346
1347 return NULL;
1348}
1349
1350/** Check area conflicts with other areas.
1351 *
1352 * @param as Address space.
1353 * @param va Starting virtual address of the area being tested.
1354 * @param size Size of the area being tested.
1355 * @param avoid_area Do not touch this area.
1356 *
1357 * @return True if there is no conflict, false otherwise.
1358 *
1359 */
1360bool check_area_conflicts(as_t *as, uintptr_t va, size_t size,
1361 as_area_t *avoid_area)
1362{
1363 ASSERT(mutex_locked(&as->lock));
1364
1365 /*
1366 * We don't want any area to have conflicts with NULL page.
1367 *
1368 */
1369 if (overlaps(va, size, NULL, PAGE_SIZE))
1370 return false;
1371
1372 /*
1373 * The leaf node is found in O(log n), where n is proportional to
1374 * the number of address space areas belonging to as.
1375 * The check for conflicts is then attempted on the rightmost
1376 * record in the left neighbour, the leftmost record in the right
1377 * neighbour and all records in the leaf node itself.
1378 *
1379 */
1380 btree_node_t *leaf;
1381 as_area_t *area =
1382 (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
1383 if (area) {
1384 if (area != avoid_area)
1385 return false;
1386 }
1387
1388 /* First, check the two border cases. */
1389 btree_node_t *node =
1390 btree_leaf_node_left_neighbour(&as->as_area_btree, leaf);
1391 if (node) {
1392 area = (as_area_t *) node->value[node->keys - 1];
1393
1394 mutex_lock(&area->lock);
1395
1396 if (overlaps(va, size, area->base, area->pages * PAGE_SIZE)) {
1397 mutex_unlock(&area->lock);
1398 return false;
1399 }
1400
1401 mutex_unlock(&area->lock);
1402 }
1403
1404 node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf);
1405 if (node) {
1406 area = (as_area_t *) node->value[0];
1407
1408 mutex_lock(&area->lock);
1409
1410 if (overlaps(va, size, area->base, area->pages * PAGE_SIZE)) {
1411 mutex_unlock(&area->lock);
1412 return false;
1413 }
1414
1415 mutex_unlock(&area->lock);
1416 }
1417
1418 /* Second, check the leaf node. */
1419 btree_key_t i;
1420 for (i = 0; i < leaf->keys; i++) {
1421 area = (as_area_t *) leaf->value[i];
1422
1423 if (area == avoid_area)
1424 continue;
1425
1426 mutex_lock(&area->lock);
1427
1428 if (overlaps(va, size, area->base, area->pages * PAGE_SIZE)) {
1429 mutex_unlock(&area->lock);
1430 return false;
1431 }
1432
1433 mutex_unlock(&area->lock);
1434 }
1435
1436 /*
1437 * So far, the area does not conflict with other areas.
1438 * Check if it doesn't conflict with kernel address space.
1439 *
1440 */
1441 if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
1442 return !overlaps(va, size,
1443 KERNEL_ADDRESS_SPACE_START,
1444 KERNEL_ADDRESS_SPACE_END - KERNEL_ADDRESS_SPACE_START);
1445 }
1446
1447 return true;
1448}
1449
1450/** Return size of the address space area with given base.
1451 *
1452 * @param base Arbitrary address inside the address space area.
1453 *
1454 * @return Size of the address space area in bytes or zero if it
1455 * does not exist.
1456 *
1457 */
1458size_t as_area_get_size(uintptr_t base)
1459{
1460 size_t size;
1461
1462 page_table_lock(AS, true);
1463 as_area_t *src_area = find_area_and_lock(AS, base);
1464
1465 if (src_area) {
1466 size = src_area->pages * PAGE_SIZE;
1467 mutex_unlock(&src_area->lock);
1468 } else
1469 size = 0;
1470
1471 page_table_unlock(AS, true);
1472 return size;
1473}
1474
1475/** Mark portion of address space area as used.
1476 *
1477 * The address space area must be already locked.
1478 *
1479 * @param area Address space area.
1480 * @param page First page to be marked.
1481 * @param count Number of page to be marked.
1482 *
1483 * @return Zero on failure and non-zero on success.
1484 *
1485 */
1486int used_space_insert(as_area_t *area, uintptr_t page, size_t count)
1487{
1488 ASSERT(mutex_locked(&area->lock));
1489 ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE));
1490 ASSERT(count);
1491
1492 btree_node_t *leaf;
1493 size_t pages = (size_t) btree_search(&area->used_space, page, &leaf);
1494 if (pages) {
1495 /*
1496 * We hit the beginning of some used space.
1497 *
1498 */
1499 return 0;
1500 }
1501
1502 if (!leaf->keys) {
1503 btree_insert(&area->used_space, page, (void *) count, leaf);
1504 return 1;
1505 }
1506
1507 btree_node_t *node = btree_leaf_node_left_neighbour(&area->used_space, leaf);
1508 if (node) {
1509 uintptr_t left_pg = node->key[node->keys - 1];
1510 uintptr_t right_pg = leaf->key[0];
1511 size_t left_cnt = (size_t) node->value[node->keys - 1];
1512 size_t right_cnt = (size_t) leaf->value[0];
1513
1514 /*
1515 * Examine the possibility that the interval fits
1516 * somewhere between the rightmost interval of
1517 * the left neigbour and the first interval of the leaf.
1518 *
1519 */
1520
1521 if (page >= right_pg) {
1522 /* Do nothing. */
1523 } else if (overlaps(page, count * PAGE_SIZE, left_pg,
1524 left_cnt * PAGE_SIZE)) {
1525 /* The interval intersects with the left interval. */
1526 return 0;
1527 } else if (overlaps(page, count * PAGE_SIZE, right_pg,
1528 right_cnt * PAGE_SIZE)) {
1529 /* The interval intersects with the right interval. */
1530 return 0;
1531 } else if ((page == left_pg + left_cnt * PAGE_SIZE) &&
1532 (page + count * PAGE_SIZE == right_pg)) {
1533 /*
1534 * The interval can be added by merging the two already
1535 * present intervals.
1536 *
1537 */
1538 node->value[node->keys - 1] += count + right_cnt;
1539 btree_remove(&area->used_space, right_pg, leaf);
1540 return 1;
1541 } else if (page == left_pg + left_cnt * PAGE_SIZE) {
1542 /*
1543 * The interval can be added by simply growing the left
1544 * interval.
1545 *
1546 */
1547 node->value[node->keys - 1] += count;
1548 return 1;
1549 } else if (page + count * PAGE_SIZE == right_pg) {
1550 /*
1551 * The interval can be addded by simply moving base of
1552 * the right interval down and increasing its size
1553 * accordingly.
1554 *
1555 */
1556 leaf->value[0] += count;
1557 leaf->key[0] = page;
1558 return 1;
1559 } else {
1560 /*
1561 * The interval is between both neigbouring intervals,
1562 * but cannot be merged with any of them.
1563 *
1564 */
1565 btree_insert(&area->used_space, page, (void *) count,
1566 leaf);
1567 return 1;
1568 }
1569 } else if (page < leaf->key[0]) {
1570 uintptr_t right_pg = leaf->key[0];
1571 size_t right_cnt = (size_t) leaf->value[0];
1572
1573 /*
1574 * Investigate the border case in which the left neighbour does
1575 * not exist but the interval fits from the left.
1576 *
1577 */
1578
1579 if (overlaps(page, count * PAGE_SIZE, right_pg,
1580 right_cnt * PAGE_SIZE)) {
1581 /* The interval intersects with the right interval. */
1582 return 0;
1583 } else if (page + count * PAGE_SIZE == right_pg) {
1584 /*
1585 * The interval can be added by moving the base of the
1586 * right interval down and increasing its size
1587 * accordingly.
1588 *
1589 */
1590 leaf->key[0] = page;
1591 leaf->value[0] += count;
1592 return 1;
1593 } else {
1594 /*
1595 * The interval doesn't adjoin with the right interval.
1596 * It must be added individually.
1597 *
1598 */
1599 btree_insert(&area->used_space, page, (void *) count,
1600 leaf);
1601 return 1;
1602 }
1603 }
1604
1605 node = btree_leaf_node_right_neighbour(&area->used_space, leaf);
1606 if (node) {
1607 uintptr_t left_pg = leaf->key[leaf->keys - 1];
1608 uintptr_t right_pg = node->key[0];
1609 size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
1610 size_t right_cnt = (size_t) node->value[0];
1611
1612 /*
1613 * Examine the possibility that the interval fits
1614 * somewhere between the leftmost interval of
1615 * the right neigbour and the last interval of the leaf.
1616 *
1617 */
1618
1619 if (page < left_pg) {
1620 /* Do nothing. */
1621 } else if (overlaps(page, count * PAGE_SIZE, left_pg,
1622 left_cnt * PAGE_SIZE)) {
1623 /* The interval intersects with the left interval. */
1624 return 0;
1625 } else if (overlaps(page, count * PAGE_SIZE, right_pg,
1626 right_cnt * PAGE_SIZE)) {
1627 /* The interval intersects with the right interval. */
1628 return 0;
1629 } else if ((page == left_pg + left_cnt * PAGE_SIZE) &&
1630 (page + count * PAGE_SIZE == right_pg)) {
1631 /*
1632 * The interval can be added by merging the two already
1633 * present intervals.
1634 *
1635 */
1636 leaf->value[leaf->keys - 1] += count + right_cnt;
1637 btree_remove(&area->used_space, right_pg, node);
1638 return 1;
1639 } else if (page == left_pg + left_cnt * PAGE_SIZE) {
1640 /*
1641 * The interval can be added by simply growing the left
1642 * interval.
1643 *
1644 */
1645 leaf->value[leaf->keys - 1] += count;
1646 return 1;
1647 } else if (page + count * PAGE_SIZE == right_pg) {
1648 /*
1649 * The interval can be addded by simply moving base of
1650 * the right interval down and increasing its size
1651 * accordingly.
1652 *
1653 */
1654 node->value[0] += count;
1655 node->key[0] = page;
1656 return 1;
1657 } else {
1658 /*
1659 * The interval is between both neigbouring intervals,
1660 * but cannot be merged with any of them.
1661 *
1662 */
1663 btree_insert(&area->used_space, page, (void *) count,
1664 leaf);
1665 return 1;
1666 }
1667 } else if (page >= leaf->key[leaf->keys - 1]) {
1668 uintptr_t left_pg = leaf->key[leaf->keys - 1];
1669 size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
1670
1671 /*
1672 * Investigate the border case in which the right neighbour
1673 * does not exist but the interval fits from the right.
1674 *
1675 */
1676
1677 if (overlaps(page, count * PAGE_SIZE, left_pg,
1678 left_cnt * PAGE_SIZE)) {
1679 /* The interval intersects with the left interval. */
1680 return 0;
1681 } else if (left_pg + left_cnt * PAGE_SIZE == page) {
1682 /*
1683 * The interval can be added by growing the left
1684 * interval.
1685 *
1686 */
1687 leaf->value[leaf->keys - 1] += count;
1688 return 1;
1689 } else {
1690 /*
1691 * The interval doesn't adjoin with the left interval.
1692 * It must be added individually.
1693 *
1694 */
1695 btree_insert(&area->used_space, page, (void *) count,
1696 leaf);
1697 return 1;
1698 }
1699 }
1700
1701 /*
1702 * Note that if the algorithm made it thus far, the interval can fit
1703 * only between two other intervals of the leaf. The two border cases
1704 * were already resolved.
1705 *
1706 */
1707 btree_key_t i;
1708 for (i = 1; i < leaf->keys; i++) {
1709 if (page < leaf->key[i]) {
1710 uintptr_t left_pg = leaf->key[i - 1];
1711 uintptr_t right_pg = leaf->key[i];
1712 size_t left_cnt = (size_t) leaf->value[i - 1];
1713 size_t right_cnt = (size_t) leaf->value[i];
1714
1715 /*
1716 * The interval fits between left_pg and right_pg.
1717 *
1718 */
1719
1720 if (overlaps(page, count * PAGE_SIZE, left_pg,
1721 left_cnt * PAGE_SIZE)) {
1722 /*
1723 * The interval intersects with the left
1724 * interval.
1725 *
1726 */
1727 return 0;
1728 } else if (overlaps(page, count * PAGE_SIZE, right_pg,
1729 right_cnt * PAGE_SIZE)) {
1730 /*
1731 * The interval intersects with the right
1732 * interval.
1733 *
1734 */
1735 return 0;
1736 } else if ((page == left_pg + left_cnt * PAGE_SIZE) &&
1737 (page + count * PAGE_SIZE == right_pg)) {
1738 /*
1739 * The interval can be added by merging the two
1740 * already present intervals.
1741 *
1742 */
1743 leaf->value[i - 1] += count + right_cnt;
1744 btree_remove(&area->used_space, right_pg, leaf);
1745 return 1;
1746 } else if (page == left_pg + left_cnt * PAGE_SIZE) {
1747 /*
1748 * The interval can be added by simply growing
1749 * the left interval.
1750 *
1751 */
1752 leaf->value[i - 1] += count;
1753 return 1;
1754 } else if (page + count * PAGE_SIZE == right_pg) {
1755 /*
1756 * The interval can be addded by simply moving
1757 * base of the right interval down and
1758 * increasing its size accordingly.
1759 *
1760 */
1761 leaf->value[i] += count;
1762 leaf->key[i] = page;
1763 return 1;
1764 } else {
1765 /*
1766 * The interval is between both neigbouring
1767 * intervals, but cannot be merged with any of
1768 * them.
1769 *
1770 */
1771 btree_insert(&area->used_space, page,
1772 (void *) count, leaf);
1773 return 1;
1774 }
1775 }
1776 }
1777
1778 panic("Inconsistency detected while adding %" PRIs " pages of used "
1779 "space at %p.", count, page);
1780}
1781
1782/** Mark portion of address space area as unused.
1783 *
1784 * The address space area must be already locked.
1785 *
1786 * @param area Address space area.
1787 * @param page First page to be marked.
1788 * @param count Number of page to be marked.
1789 *
1790 * @return Zero on failure and non-zero on success.
1791 *
1792 */
1793int used_space_remove(as_area_t *area, uintptr_t page, size_t count)
1794{
1795 ASSERT(mutex_locked(&area->lock));
1796 ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE));
1797 ASSERT(count);
1798
1799 btree_node_t *leaf;
1800 size_t pages = (size_t) btree_search(&area->used_space, page, &leaf);
1801 if (pages) {
1802 /*
1803 * We are lucky, page is the beginning of some interval.
1804 *
1805 */
1806 if (count > pages) {
1807 return 0;
1808 } else if (count == pages) {
1809 btree_remove(&area->used_space, page, leaf);
1810 return 1;
1811 } else {
1812 /*
1813 * Find the respective interval.
1814 * Decrease its size and relocate its start address.
1815 *
1816 */
1817 btree_key_t i;
1818 for (i = 0; i < leaf->keys; i++) {
1819 if (leaf->key[i] == page) {
1820 leaf->key[i] += count * PAGE_SIZE;
1821 leaf->value[i] -= count;
1822 return 1;
1823 }
1824 }
1825 goto error;
1826 }
1827 }
1828
1829 btree_node_t *node = btree_leaf_node_left_neighbour(&area->used_space, leaf);
1830 if ((node) && (page < leaf->key[0])) {
1831 uintptr_t left_pg = node->key[node->keys - 1];
1832 size_t left_cnt = (size_t) node->value[node->keys - 1];
1833
1834 if (overlaps(left_pg, left_cnt * PAGE_SIZE, page,
1835 count * PAGE_SIZE)) {
1836 if (page + count * PAGE_SIZE ==
1837 left_pg + left_cnt * PAGE_SIZE) {
1838 /*
1839 * The interval is contained in the rightmost
1840 * interval of the left neighbour and can be
1841 * removed by updating the size of the bigger
1842 * interval.
1843 *
1844 */
1845 node->value[node->keys - 1] -= count;
1846 return 1;
1847 } else if (page + count * PAGE_SIZE <
1848 left_pg + left_cnt*PAGE_SIZE) {
1849 /*
1850 * The interval is contained in the rightmost
1851 * interval of the left neighbour but its
1852 * removal requires both updating the size of
1853 * the original interval and also inserting a
1854 * new interval.
1855 *
1856 */
1857 size_t new_cnt = ((left_pg + left_cnt * PAGE_SIZE) -
1858 (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
1859 node->value[node->keys - 1] -= count + new_cnt;
1860 btree_insert(&area->used_space, page +
1861 count * PAGE_SIZE, (void *) new_cnt, leaf);
1862 return 1;
1863 }
1864 }
1865 return 0;
1866 } else if (page < leaf->key[0])
1867 return 0;
1868
1869 if (page > leaf->key[leaf->keys - 1]) {
1870 uintptr_t left_pg = leaf->key[leaf->keys - 1];
1871 size_t left_cnt = (size_t) leaf->value[leaf->keys - 1];
1872
1873 if (overlaps(left_pg, left_cnt * PAGE_SIZE, page,
1874 count * PAGE_SIZE)) {
1875 if (page + count * PAGE_SIZE ==
1876 left_pg + left_cnt * PAGE_SIZE) {
1877 /*
1878 * The interval is contained in the rightmost
1879 * interval of the leaf and can be removed by
1880 * updating the size of the bigger interval.
1881 *
1882 */
1883 leaf->value[leaf->keys - 1] -= count;
1884 return 1;
1885 } else if (page + count * PAGE_SIZE < left_pg +
1886 left_cnt * PAGE_SIZE) {
1887 /*
1888 * The interval is contained in the rightmost
1889 * interval of the leaf but its removal
1890 * requires both updating the size of the
1891 * original interval and also inserting a new
1892 * interval.
1893 *
1894 */
1895 size_t new_cnt = ((left_pg + left_cnt * PAGE_SIZE) -
1896 (page + count * PAGE_SIZE)) >> PAGE_WIDTH;
1897 leaf->value[leaf->keys - 1] -= count + new_cnt;
1898 btree_insert(&area->used_space, page +
1899 count * PAGE_SIZE, (void *) new_cnt, leaf);
1900 return 1;
1901 }
1902 }
1903 return 0;
1904 }
1905
1906 /*
1907 * The border cases have been already resolved.
1908 * Now the interval can be only between intervals of the leaf.
1909 */
1910 btree_key_t i;
1911 for (i = 1; i < leaf->keys - 1; i++) {
1912 if (page < leaf->key[i]) {
1913 uintptr_t left_pg = leaf->key[i - 1];
1914 size_t left_cnt = (size_t) leaf->value[i - 1];
1915
1916 /*
1917 * Now the interval is between intervals corresponding
1918 * to (i - 1) and i.
1919 */
1920 if (overlaps(left_pg, left_cnt * PAGE_SIZE, page,
1921 count * PAGE_SIZE)) {
1922 if (page + count * PAGE_SIZE ==
1923 left_pg + left_cnt*PAGE_SIZE) {
1924 /*
1925 * The interval is contained in the
1926 * interval (i - 1) of the leaf and can
1927 * be removed by updating the size of
1928 * the bigger interval.
1929 *
1930 */
1931 leaf->value[i - 1] -= count;
1932 return 1;
1933 } else if (page + count * PAGE_SIZE <
1934 left_pg + left_cnt * PAGE_SIZE) {
1935 /*
1936 * The interval is contained in the
1937 * interval (i - 1) of the leaf but its
1938 * removal requires both updating the
1939 * size of the original interval and
1940 * also inserting a new interval.
1941 */
1942 size_t new_cnt = ((left_pg +
1943 left_cnt * PAGE_SIZE) -
1944 (page + count * PAGE_SIZE)) >>
1945 PAGE_WIDTH;
1946 leaf->value[i - 1] -= count + new_cnt;
1947 btree_insert(&area->used_space, page +
1948 count * PAGE_SIZE, (void *) new_cnt,
1949 leaf);
1950 return 1;
1951 }
1952 }
1953 return 0;
1954 }
1955 }
1956
1957error:
1958 panic("Inconsistency detected while removing %" PRIs " pages of used "
1959 "space from %p.", count, page);
1960}
1961
1962/** Remove reference to address space area share info.
1963 *
1964 * If the reference count drops to 0, the sh_info is deallocated.
1965 *
1966 * @param sh_info Pointer to address space area share info.
1967 *
1968 */
1969void sh_info_remove_reference(share_info_t *sh_info)
1970{
1971 bool dealloc = false;
1972
1973 mutex_lock(&sh_info->lock);
1974 ASSERT(sh_info->refcount);
1975
1976 if (--sh_info->refcount == 0) {
1977 dealloc = true;
1978 link_t *cur;
1979
1980 /*
1981 * Now walk carefully the pagemap B+tree and free/remove
1982 * reference from all frames found there.
1983 */
1984 for (cur = sh_info->pagemap.leaf_head.next;
1985 cur != &sh_info->pagemap.leaf_head; cur = cur->next) {
1986 btree_node_t *node
1987 = list_get_instance(cur, btree_node_t, leaf_link);
1988 btree_key_t i;
1989
1990 for (i = 0; i < node->keys; i++)
1991 frame_free((uintptr_t) node->value[i]);
1992 }
1993
1994 }
1995 mutex_unlock(&sh_info->lock);
1996
1997 if (dealloc) {
1998 btree_destroy(&sh_info->pagemap);
1999 free(sh_info);
2000 }
2001}
2002
2003/*
2004 * Address space related syscalls.
2005 */
2006
2007/** Wrapper for as_area_create(). */
2008unative_t sys_as_area_create(uintptr_t address, size_t size, unsigned int flags)
2009{
2010 if (as_area_create(AS, flags | AS_AREA_CACHEABLE, size, address,
2011 AS_AREA_ATTR_NONE, &anon_backend, NULL))
2012 return (unative_t) address;
2013 else
2014 return (unative_t) -1;
2015}
2016
2017/** Wrapper for as_area_resize(). */
2018unative_t sys_as_area_resize(uintptr_t address, size_t size, unsigned int flags)
2019{
2020 return (unative_t) as_area_resize(AS, address, size, 0);
2021}
2022
2023/** Wrapper for as_area_change_flags(). */
2024unative_t sys_as_area_change_flags(uintptr_t address, unsigned int flags)
2025{
2026 return (unative_t) as_area_change_flags(AS, flags, address);
2027}
2028
2029/** Wrapper for as_area_destroy(). */
2030unative_t sys_as_area_destroy(uintptr_t address)
2031{
2032 return (unative_t) as_area_destroy(AS, address);
2033}
2034
2035/** Get list of adress space areas.
2036 *
2037 * @param as Address space.
2038 * @param obuf Place to save pointer to returned buffer.
2039 * @param osize Place to save size of returned buffer.
2040 *
2041 */
2042void as_get_area_info(as_t *as, as_area_info_t **obuf, size_t *osize)
2043{
2044 mutex_lock(&as->lock);
2045
2046 /* First pass, count number of areas. */
2047
2048 size_t area_cnt = 0;
2049 link_t *cur;
2050
2051 for (cur = as->as_area_btree.leaf_head.next;
2052 cur != &as->as_area_btree.leaf_head; cur = cur->next) {
2053 btree_node_t *node =
2054 list_get_instance(cur, btree_node_t, leaf_link);
2055 area_cnt += node->keys;
2056 }
2057
2058 size_t isize = area_cnt * sizeof(as_area_info_t);
2059 as_area_info_t *info = malloc(isize, 0);
2060
2061 /* Second pass, record data. */
2062
2063 size_t area_idx = 0;
2064
2065 for (cur = as->as_area_btree.leaf_head.next;
2066 cur != &as->as_area_btree.leaf_head; cur = cur->next) {
2067 btree_node_t *node =
2068 list_get_instance(cur, btree_node_t, leaf_link);
2069 btree_key_t i;
2070
2071 for (i = 0; i < node->keys; i++) {
2072 as_area_t *area = node->value[i];
2073
2074 ASSERT(area_idx < area_cnt);
2075 mutex_lock(&area->lock);
2076
2077 info[area_idx].start_addr = area->base;
2078 info[area_idx].size = FRAMES2SIZE(area->pages);
2079 info[area_idx].flags = area->flags;
2080 ++area_idx;
2081
2082 mutex_unlock(&area->lock);
2083 }
2084 }
2085
2086 mutex_unlock(&as->lock);
2087
2088 *obuf = info;
2089 *osize = isize;
2090}
2091
2092/** Print out information about address space.
2093 *
2094 * @param as Address space.
2095 *
2096 */
2097void as_print(as_t *as)
2098{
2099 mutex_lock(&as->lock);
2100
2101 /* print out info about address space areas */
2102 link_t *cur;
2103 for (cur = as->as_area_btree.leaf_head.next;
2104 cur != &as->as_area_btree.leaf_head; cur = cur->next) {
2105 btree_node_t *node
2106 = list_get_instance(cur, btree_node_t, leaf_link);
2107 btree_key_t i;
2108
2109 for (i = 0; i < node->keys; i++) {
2110 as_area_t *area = node->value[i];
2111
2112 mutex_lock(&area->lock);
2113 printf("as_area: %p, base=%p, pages=%" PRIs
2114 " (%p - %p)\n", area, area->base, area->pages,
2115 area->base, area->base + FRAMES2SIZE(area->pages));
2116 mutex_unlock(&area->lock);
2117 }
2118 }
2119
2120 mutex_unlock(&as->lock);
2121}
2122
2123/** @}
2124 */
Note: See TracBrowser for help on using the repository browser.