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

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