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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d9d0088 was d9d0088, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix case where no area has lower or equal base. Remove duplicate alignment.

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