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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 69114714 was fbcdeb8, checked in by Martin Decky <martin@…>, 14 years ago

Remove the two-phase way of creating virtual memory areas (first asking for a mappable address and then mapping it) which was prone to race conditions when two or more calls to as_get_mappable_page() and as_area_create() were interleaved. This for example caused the e1k driver to randomly fail.

The memory area related syscalls and IPC calls have all been altered to accept a special value (void *) -1, representing a demand to atomically search for a mappable address space "hole" and map to it.

Individual changes:

  • IPC_M_SHARE_OUT: the destination address space area is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the callee via a pointer in an IPC reply argument)

  • IPC_M_SHARE_IN: the destination address space ares is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the caller as usual via an IPC argument)

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