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

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

Reflect assumptions about lock and interrupt state in functions themselves.

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