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

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

Lock the page tables before initiating the TLB shootdown, avoiding thus possible
blocking while holding a spinlock.

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