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

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

remove obsolete net-qe.bat
accept optional arguments from the user

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