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

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

Lock/interrupt assertions in the code are self-documenting. No need to have that information duplicated in the comments.

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