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

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

Do not disable interrupts unnecessarily.

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