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

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

Fix a newly introduced deadlock in the TLB shootdown algorithm.

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