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

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

Assert that as_destroy() is not being called on AS.

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