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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 78de83de was 78de83de, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Improve kernel spinlock and AS refcount.

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