source: mainline/generic/src/mm/as.c@ 152b2b0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 152b2b0 was 152b2b0, checked in by Jakub Jermar <jakub@…>, 19 years ago

Implement recursive function for deallocating the whole B+tree.
Make use of this function in address space management.

  • Property mode set to 100644
File size: 41.0 KB
RevLine 
[20d50a1]1/*
2 * Copyright (C) 2001-2006 Jakub Jermar
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
[9179d0a]29/**
30 * @file as.c
31 * @brief Address space related functions.
32 *
[20d50a1]33 * This file contains address space manipulation functions.
34 * Roughly speaking, this is a higher-level client of
35 * Virtual Address Translation (VAT) subsystem.
[9179d0a]36 *
37 * Functionality provided by this file allows one to
38 * create address space and create, resize and share
39 * address space areas.
40 *
41 * @see page.c
42 *
[20d50a1]43 */
44
45#include <mm/as.h>
[ef67bab]46#include <arch/mm/as.h>
[20d50a1]47#include <mm/page.h>
48#include <mm/frame.h>
[085d973]49#include <mm/slab.h>
[20d50a1]50#include <mm/tlb.h>
51#include <arch/mm/page.h>
52#include <genarch/mm/page_pt.h>
[2802767]53#include <genarch/mm/page_ht.h>
[4512d7e]54#include <mm/asid.h>
[20d50a1]55#include <arch/mm/asid.h>
56#include <synch/spinlock.h>
[1068f6a]57#include <synch/mutex.h>
[5c9a08b]58#include <adt/list.h>
[252127e]59#include <adt/btree.h>
[df0103f7]60#include <proc/task.h>
[e3c762cd]61#include <proc/thread.h>
[20d50a1]62#include <arch/asm.h>
[df0103f7]63#include <panic.h>
[20d50a1]64#include <debug.h>
[df0103f7]65#include <print.h>
[20d50a1]66#include <memstr.h>
[5a7d9d1]67#include <macros.h>
[20d50a1]68#include <arch.h>
[df0103f7]69#include <errno.h>
70#include <config.h>
[25bf215]71#include <align.h>
[df0103f7]72#include <arch/types.h>
73#include <typedefs.h>
[e3c762cd]74#include <syscall/copy.h>
75#include <arch/interrupt.h>
[20d50a1]76
[ef67bab]77as_operations_t *as_operations = NULL;
[20d50a1]78
[47800e0]79/** This lock protects inactive_as_with_asid_head list. It must be acquired before as_t mutex. */
80SPINLOCK_INITIALIZE(inactive_as_with_asid_lock);
[7e4e532]81
82/**
83 * This list contains address spaces that are not active on any
84 * processor and that have valid ASID.
85 */
86LIST_INITIALIZE(inactive_as_with_asid_head);
87
[071a8ae6]88/** Kernel address space. */
89as_t *AS_KERNEL = NULL;
90
[df0103f7]91static int area_flags_to_page_flags(int aflags);
[d3e7ff4]92static as_area_t *find_area_and_lock(as_t *as, __address va);
[37e7d2b9]93static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area);
[8182031]94static void sh_info_remove_reference(share_info_t *sh_info);
[20d50a1]95
[ef67bab]96/** Initialize address space subsystem. */
97void as_init(void)
98{
99 as_arch_init();
[8e1ea655]100 AS_KERNEL = as_create(FLAG_AS_KERNEL);
[125e944]101 if (!AS_KERNEL)
102 panic("can't create kernel address space\n");
103
[ef67bab]104}
105
[071a8ae6]106/** Create address space.
107 *
108 * @param flags Flags that influence way in wich the address space is created.
109 */
[ef67bab]110as_t *as_create(int flags)
[20d50a1]111{
112 as_t *as;
113
[bb68433]114 as = (as_t *) malloc(sizeof(as_t), 0);
[7e4e532]115 link_initialize(&as->inactive_as_with_asid_link);
[1068f6a]116 mutex_initialize(&as->lock);
[252127e]117 btree_create(&as->as_area_btree);
[bb68433]118
119 if (flags & FLAG_AS_KERNEL)
120 as->asid = ASID_KERNEL;
121 else
122 as->asid = ASID_INVALID;
123
[482826d]124 as->refcount = 0;
[47800e0]125 as->cpu_refcount = 0;
[bb68433]126 as->page_table = page_table_create(flags);
[20d50a1]127
128 return as;
129}
130
[482826d]131/** Destroy adress space.
132 *
133 * When there are no tasks referencing this address space (i.e. its refcount is zero),
134 * the address space can be destroyed.
135 */
136void as_destroy(as_t *as)
[5be1923]137{
[482826d]138 ipl_t ipl;
139 bool cond;
140
141 ASSERT(as->refcount == 0);
142
143 /*
144 * Since there is no reference to this area,
145 * it is safe not to lock its mutex.
146 */
147
148 ipl = interrupts_disable();
149 spinlock_lock(&inactive_as_with_asid_lock);
150 if (as->asid != ASID_INVALID && as->asid != ASID_KERNEL) {
151 list_remove(&as->inactive_as_with_asid_link);
152 asid_put(as->asid);
153 }
154 spinlock_unlock(&inactive_as_with_asid_lock);
155
156 /*
157 * Destroy address space areas of the address space.
158 */
159 for (cond = true; cond; ) {
160 btree_node_t *node;
161
162 ASSERT(!list_empty(&as->as_area_btree.leaf_head));
163 node = list_get_instance(&as->as_area_btree.leaf_head.next, btree_node_t, leaf_link);
[152b2b0]164 if ((cond = node->keys))
[482826d]165 as_area_destroy(as, node->key[0]);
166 }
167
[152b2b0]168 btree_destroy(&as->as_area_btree);
[482826d]169 page_table_destroy(as->page_table);
[5be1923]170
[482826d]171 interrupts_restore(ipl);
172
[5be1923]173 free(as);
174}
175
[20d50a1]176/** Create address space area of common attributes.
177 *
178 * The created address space area is added to the target address space.
179 *
180 * @param as Target address space.
[a9e8b39]181 * @param flags Flags of the area memory.
[37e7d2b9]182 * @param size Size of area.
[20d50a1]183 * @param base Base address of area.
[a9e8b39]184 * @param attrs Attributes of the area.
[8182031]185 * @param backend Address space area backend. NULL if no backend is used.
186 * @param backend_data NULL or a pointer to an array holding two void *.
[20d50a1]187 *
188 * @return Address space area on success or NULL on failure.
189 */
[8182031]190as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base, int attrs,
[0ee077ee]191 mem_backend_t *backend, mem_backend_data_t *backend_data)
[20d50a1]192{
193 ipl_t ipl;
194 as_area_t *a;
195
196 if (base % PAGE_SIZE)
[37e7d2b9]197 return NULL;
198
[dbbeb26]199 if (!size)
200 return NULL;
201
[37e7d2b9]202 /* Writeable executable areas are not supported. */
203 if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
204 return NULL;
[20d50a1]205
206 ipl = interrupts_disable();
[1068f6a]207 mutex_lock(&as->lock);
[20d50a1]208
[37e7d2b9]209 if (!check_area_conflicts(as, base, size, NULL)) {
[1068f6a]210 mutex_unlock(&as->lock);
[37e7d2b9]211 interrupts_restore(ipl);
212 return NULL;
213 }
[20d50a1]214
[bb68433]215 a = (as_area_t *) malloc(sizeof(as_area_t), 0);
216
[1068f6a]217 mutex_initialize(&a->lock);
[bb68433]218
[0ee077ee]219 a->as = as;
[c23502d]220 a->flags = flags;
[a9e8b39]221 a->attributes = attrs;
[37e7d2b9]222 a->pages = SIZE2FRAMES(size);
[bb68433]223 a->base = base;
[8182031]224 a->sh_info = NULL;
225 a->backend = backend;
[0ee077ee]226 if (backend_data)
227 a->backend_data = *backend_data;
228 else
229 memsetb((__address) &a->backend_data, sizeof(a->backend_data), 0);
230
[25bf215]231 btree_create(&a->used_space);
[bb68433]232
[252127e]233 btree_insert(&as->as_area_btree, base, (void *) a, NULL);
[20d50a1]234
[1068f6a]235 mutex_unlock(&as->lock);
[20d50a1]236 interrupts_restore(ipl);
[f9425006]237
[20d50a1]238 return a;
239}
240
[df0103f7]241/** Find address space area and change it.
242 *
243 * @param as Address space.
244 * @param address Virtual address belonging to the area to be changed. Must be page-aligned.
245 * @param size New size of the virtual memory block starting at address.
246 * @param flags Flags influencing the remap operation. Currently unused.
247 *
[7242a78e]248 * @return Zero on success or a value from @ref errno.h otherwise.
[df0103f7]249 */
[7242a78e]250int as_area_resize(as_t *as, __address address, size_t size, int flags)
[df0103f7]251{
[7242a78e]252 as_area_t *area;
[df0103f7]253 ipl_t ipl;
254 size_t pages;
255
256 ipl = interrupts_disable();
[1068f6a]257 mutex_lock(&as->lock);
[df0103f7]258
259 /*
260 * Locate the area.
261 */
262 area = find_area_and_lock(as, address);
263 if (!area) {
[1068f6a]264 mutex_unlock(&as->lock);
[df0103f7]265 interrupts_restore(ipl);
[7242a78e]266 return ENOENT;
[df0103f7]267 }
268
[0ee077ee]269 if (area->backend == &phys_backend) {
[df0103f7]270 /*
271 * Remapping of address space areas associated
272 * with memory mapped devices is not supported.
273 */
[1068f6a]274 mutex_unlock(&area->lock);
275 mutex_unlock(&as->lock);
[df0103f7]276 interrupts_restore(ipl);
[7242a78e]277 return ENOTSUP;
[df0103f7]278 }
[8182031]279 if (area->sh_info) {
280 /*
281 * Remapping of shared address space areas
282 * is not supported.
283 */
284 mutex_unlock(&area->lock);
285 mutex_unlock(&as->lock);
286 interrupts_restore(ipl);
287 return ENOTSUP;
288 }
[df0103f7]289
290 pages = SIZE2FRAMES((address - area->base) + size);
291 if (!pages) {
292 /*
293 * Zero size address space areas are not allowed.
294 */
[1068f6a]295 mutex_unlock(&area->lock);
296 mutex_unlock(&as->lock);
[df0103f7]297 interrupts_restore(ipl);
[7242a78e]298 return EPERM;
[df0103f7]299 }
300
301 if (pages < area->pages) {
[56789125]302 bool cond;
303 __address start_free = area->base + pages*PAGE_SIZE;
[df0103f7]304
305 /*
306 * Shrinking the area.
307 * No need to check for overlaps.
308 */
309
[5552d60]310 /*
311 * Start TLB shootdown sequence.
312 */
313 tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
314
[56789125]315 /*
316 * Remove frames belonging to used space starting from
317 * the highest addresses downwards until an overlap with
318 * the resized address space area is found. Note that this
319 * is also the right way to remove part of the used_space
320 * B+tree leaf list.
321 */
322 for (cond = true; cond;) {
323 btree_node_t *node;
324
325 ASSERT(!list_empty(&area->used_space.leaf_head));
326 node = list_get_instance(area->used_space.leaf_head.prev, btree_node_t, leaf_link);
327 if ((cond = (bool) node->keys)) {
328 __address b = node->key[node->keys - 1];
329 count_t c = (count_t) node->value[node->keys - 1];
330 int i = 0;
331
332 if (overlaps(b, c*PAGE_SIZE, area->base, pages*PAGE_SIZE)) {
333
334 if (b + c*PAGE_SIZE <= start_free) {
335 /*
336 * The whole interval fits completely
337 * in the resized address space area.
338 */
339 break;
340 }
341
342 /*
343 * Part of the interval corresponding to b and c
344 * overlaps with the resized address space area.
345 */
346
347 cond = false; /* we are almost done */
348 i = (start_free - b) >> PAGE_WIDTH;
349 if (!used_space_remove(area, start_free, c - i))
350 panic("Could not remove used space.");
351 } else {
352 /*
353 * The interval of used space can be completely removed.
354 */
355 if (!used_space_remove(area, b, c))
356 panic("Could not remove used space.\n");
357 }
358
359 for (; i < c; i++) {
360 pte_t *pte;
361
362 page_table_lock(as, false);
363 pte = page_mapping_find(as, b + i*PAGE_SIZE);
364 ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte));
[0ee077ee]365 if (area->backend && area->backend->frame_free) {
366 area->backend->frame_free(area,
[8182031]367 b + i*PAGE_SIZE, PTE_GET_FRAME(pte));
368 }
[56789125]369 page_mapping_remove(as, b + i*PAGE_SIZE);
370 page_table_unlock(as, false);
371 }
[df0103f7]372 }
373 }
[5552d60]374
[df0103f7]375 /*
[5552d60]376 * Finish TLB shootdown sequence.
[df0103f7]377 */
378 tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
379 tlb_shootdown_finalize();
380 } else {
381 /*
382 * Growing the area.
383 * Check for overlaps with other address space areas.
384 */
385 if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) {
[1068f6a]386 mutex_unlock(&area->lock);
387 mutex_unlock(&as->lock);
[df0103f7]388 interrupts_restore(ipl);
[7242a78e]389 return EADDRNOTAVAIL;
[df0103f7]390 }
391 }
392
393 area->pages = pages;
394
[1068f6a]395 mutex_unlock(&area->lock);
396 mutex_unlock(&as->lock);
[df0103f7]397 interrupts_restore(ipl);
398
[7242a78e]399 return 0;
400}
401
402/** Destroy address space area.
403 *
404 * @param as Address space.
405 * @param address Address withing the area to be deleted.
406 *
407 * @return Zero on success or a value from @ref errno.h on failure.
408 */
409int as_area_destroy(as_t *as, __address address)
410{
411 as_area_t *area;
412 __address base;
413 ipl_t ipl;
[567807b1]414 bool cond;
[7242a78e]415
416 ipl = interrupts_disable();
[1068f6a]417 mutex_lock(&as->lock);
[7242a78e]418
419 area = find_area_and_lock(as, address);
420 if (!area) {
[1068f6a]421 mutex_unlock(&as->lock);
[7242a78e]422 interrupts_restore(ipl);
423 return ENOENT;
424 }
425
[56789125]426 base = area->base;
427
[5552d60]428 /*
429 * Start TLB shootdown sequence.
430 */
431 tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base, area->pages);
432
[567807b1]433 /*
434 * Visit only the pages mapped by used_space B+tree.
435 * Note that we must be very careful when walking the tree
436 * leaf list and removing used space as the leaf list changes
437 * unpredictibly after each remove. The solution is to actually
438 * not walk the tree at all, but to remove items from the head
439 * of the leaf list until there are some keys left.
440 */
441 for (cond = true; cond;) {
442 btree_node_t *node;
[56789125]443
[567807b1]444 ASSERT(!list_empty(&area->used_space.leaf_head));
445 node = list_get_instance(area->used_space.leaf_head.next, btree_node_t, leaf_link);
446 if ((cond = (bool) node->keys)) {
447 __address b = node->key[0];
448 count_t i;
449 pte_t *pte;
[56789125]450
[567807b1]451 for (i = 0; i < (count_t) node->value[0]; i++) {
452 page_table_lock(as, false);
453 pte = page_mapping_find(as, b + i*PAGE_SIZE);
454 ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte));
[0ee077ee]455 if (area->backend && area->backend->frame_free) {
456 area->backend->frame_free(area,
[567807b1]457 b + i*PAGE_SIZE, PTE_GET_FRAME(pte));
[56789125]458 }
[567807b1]459 page_mapping_remove(as, b + i*PAGE_SIZE);
460 page_table_unlock(as, false);
[7242a78e]461 }
462 }
463 }
[56789125]464
[7242a78e]465 /*
[5552d60]466 * Finish TLB shootdown sequence.
[7242a78e]467 */
468 tlb_invalidate_pages(AS->asid, area->base, area->pages);
469 tlb_shootdown_finalize();
[5552d60]470
471 btree_destroy(&area->used_space);
[7242a78e]472
[8d4f2ae]473 area->attributes |= AS_AREA_ATTR_PARTIAL;
[8182031]474
475 if (area->sh_info)
476 sh_info_remove_reference(area->sh_info);
477
[1068f6a]478 mutex_unlock(&area->lock);
[7242a78e]479
480 /*
481 * Remove the empty area from address space.
482 */
483 btree_remove(&AS->as_area_btree, base, NULL);
484
[8d4f2ae]485 free(area);
486
[1068f6a]487 mutex_unlock(&AS->lock);
[7242a78e]488 interrupts_restore(ipl);
489 return 0;
[df0103f7]490}
491
[8d6bc2d5]492/** Share address space area with another or the same address space.
[df0103f7]493 *
[0ee077ee]494 * Address space area mapping is shared with a new address space area.
495 * If the source address space area has not been shared so far,
496 * a new sh_info is created. The new address space area simply gets the
497 * sh_info of the source area. The process of duplicating the
498 * mapping is done through the backend share function.
[8d6bc2d5]499 *
[fd4d8c0]500 * @param src_as Pointer to source address space.
[a9e8b39]501 * @param src_base Base address of the source address space area.
[fd4d8c0]502 * @param acc_size Expected size of the source area.
[46fc2f9]503 * @param dst_as Pointer to destination address space.
[fd4d8c0]504 * @param dst_base Target base address.
505 * @param dst_flags_mask Destination address space area flags mask.
[df0103f7]506 *
[7242a78e]507 * @return Zero on success or ENOENT if there is no such task or
[df0103f7]508 * if there is no such address space area,
509 * EPERM if there was a problem in accepting the area or
510 * ENOMEM if there was a problem in allocating destination
[8d6bc2d5]511 * address space area. ENOTSUP is returned if an attempt
512 * to share non-anonymous address space area is detected.
[df0103f7]513 */
[8d6bc2d5]514int as_area_share(as_t *src_as, __address src_base, size_t acc_size,
[46fc2f9]515 as_t *dst_as, __address dst_base, int dst_flags_mask)
[df0103f7]516{
517 ipl_t ipl;
[a9e8b39]518 int src_flags;
519 size_t src_size;
520 as_area_t *src_area, *dst_area;
[8d6bc2d5]521 share_info_t *sh_info;
[0ee077ee]522 mem_backend_t *src_backend;
523 mem_backend_data_t src_backend_data;
[d6e5cbc]524
[7c23af9]525 ipl = interrupts_disable();
[1068f6a]526 mutex_lock(&src_as->lock);
[7c23af9]527 src_area = find_area_and_lock(src_as, src_base);
[a9e8b39]528 if (!src_area) {
[6fa476f7]529 /*
530 * Could not find the source address space area.
531 */
[1068f6a]532 mutex_unlock(&src_as->lock);
[6fa476f7]533 interrupts_restore(ipl);
534 return ENOENT;
535 }
[8d6bc2d5]536
[0ee077ee]537 if (!src_area->backend || !src_area->backend->share) {
[8d6bc2d5]538 /*
[0ee077ee]539 * There is now backend or the backend does not
540 * know how to share the area.
[8d6bc2d5]541 */
542 mutex_unlock(&src_area->lock);
543 mutex_unlock(&src_as->lock);
544 interrupts_restore(ipl);
545 return ENOTSUP;
546 }
547
[a9e8b39]548 src_size = src_area->pages * PAGE_SIZE;
549 src_flags = src_area->flags;
[0ee077ee]550 src_backend = src_area->backend;
551 src_backend_data = src_area->backend_data;
[8d6bc2d5]552
[76d7305]553 if (src_size != acc_size || (src_flags & dst_flags_mask) != dst_flags_mask) {
[8d6bc2d5]554 mutex_unlock(&src_area->lock);
555 mutex_unlock(&src_as->lock);
[df0103f7]556 interrupts_restore(ipl);
557 return EPERM;
558 }
[8d6bc2d5]559
560 /*
561 * Now we are committed to sharing the area.
562 * First prepare the area for sharing.
563 * Then it will be safe to unlock it.
564 */
565 sh_info = src_area->sh_info;
566 if (!sh_info) {
567 sh_info = (share_info_t *) malloc(sizeof(share_info_t), 0);
568 mutex_initialize(&sh_info->lock);
569 sh_info->refcount = 2;
570 btree_create(&sh_info->pagemap);
571 src_area->sh_info = sh_info;
572 } else {
573 mutex_lock(&sh_info->lock);
574 sh_info->refcount++;
575 mutex_unlock(&sh_info->lock);
576 }
577
[0ee077ee]578 src_area->backend->share(src_area);
[8d6bc2d5]579
580 mutex_unlock(&src_area->lock);
581 mutex_unlock(&src_as->lock);
582
[df0103f7]583 /*
[a9e8b39]584 * Create copy of the source address space area.
585 * The destination area is created with AS_AREA_ATTR_PARTIAL
586 * attribute set which prevents race condition with
587 * preliminary as_page_fault() calls.
[fd4d8c0]588 * The flags of the source area are masked against dst_flags_mask
589 * to support sharing in less privileged mode.
[df0103f7]590 */
[76d7305]591 dst_area = as_area_create(dst_as, dst_flags_mask, src_size, dst_base,
[0ee077ee]592 AS_AREA_ATTR_PARTIAL, src_backend, &src_backend_data);
[a9e8b39]593 if (!dst_area) {
[df0103f7]594 /*
595 * Destination address space area could not be created.
596 */
[8d6bc2d5]597 sh_info_remove_reference(sh_info);
598
[df0103f7]599 interrupts_restore(ipl);
600 return ENOMEM;
601 }
602
[a9e8b39]603 /*
604 * Now the destination address space area has been
605 * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
[8d6bc2d5]606 * attribute and set the sh_info.
[a9e8b39]607 */
[1068f6a]608 mutex_lock(&dst_area->lock);
[a9e8b39]609 dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
[8d6bc2d5]610 dst_area->sh_info = sh_info;
[1068f6a]611 mutex_unlock(&dst_area->lock);
[df0103f7]612
613 interrupts_restore(ipl);
614
615 return 0;
616}
617
[fb84455]618/** Check access mode for address space area.
619 *
620 * The address space area must be locked prior to this call.
621 *
622 * @param area Address space area.
623 * @param access Access mode.
624 *
625 * @return False if access violates area's permissions, true otherwise.
626 */
627bool as_area_check_access(as_area_t *area, pf_access_t access)
628{
629 int flagmap[] = {
630 [PF_ACCESS_READ] = AS_AREA_READ,
631 [PF_ACCESS_WRITE] = AS_AREA_WRITE,
632 [PF_ACCESS_EXEC] = AS_AREA_EXEC
633 };
634
635 if (!(area->flags & flagmap[access]))
636 return false;
637
638 return true;
639}
640
[20d50a1]641/** Handle page fault within the current address space.
642 *
[8182031]643 * This is the high-level page fault handler. It decides
644 * whether the page fault can be resolved by any backend
645 * and if so, it invokes the backend to resolve the page
646 * fault.
647 *
[20d50a1]648 * Interrupts are assumed disabled.
649 *
650 * @param page Faulting page.
[567807b1]651 * @param access Access mode that caused the fault (i.e. read/write/exec).
[e3c762cd]652 * @param istate Pointer to interrupted state.
[20d50a1]653 *
[8182031]654 * @return AS_PF_FAULT on page fault, AS_PF_OK on success or AS_PF_DEFER if the
655 * fault was caused by copy_to_uspace() or copy_from_uspace().
[20d50a1]656 */
[567807b1]657int as_page_fault(__address page, pf_access_t access, istate_t *istate)
[20d50a1]658{
[2299914]659 pte_t *pte;
[d3e7ff4]660 as_area_t *area;
[20d50a1]661
[1068f6a]662 if (!THREAD)
[8182031]663 return AS_PF_FAULT;
[1068f6a]664
[20d50a1]665 ASSERT(AS);
[2299914]666
[1068f6a]667 mutex_lock(&AS->lock);
[d3e7ff4]668 area = find_area_and_lock(AS, page);
[20d50a1]669 if (!area) {
670 /*
671 * No area contained mapping for 'page'.
672 * Signal page fault to low-level handler.
673 */
[1068f6a]674 mutex_unlock(&AS->lock);
[e3c762cd]675 goto page_fault;
[20d50a1]676 }
677
[a9e8b39]678 if (area->attributes & AS_AREA_ATTR_PARTIAL) {
679 /*
680 * The address space area is not fully initialized.
681 * Avoid possible race by returning error.
682 */
[1068f6a]683 mutex_unlock(&area->lock);
684 mutex_unlock(&AS->lock);
[e3c762cd]685 goto page_fault;
[a9e8b39]686 }
687
[0ee077ee]688 if (!area->backend || !area->backend->page_fault) {
[8182031]689 /*
690 * The address space area is not backed by any backend
691 * or the backend cannot handle page faults.
692 */
693 mutex_unlock(&area->lock);
694 mutex_unlock(&AS->lock);
695 goto page_fault;
696 }
[1ace9ea]697
[2299914]698 page_table_lock(AS, false);
699
700 /*
701 * To avoid race condition between two page faults
702 * on the same address, we need to make sure
703 * the mapping has not been already inserted.
704 */
705 if ((pte = page_mapping_find(AS, page))) {
706 if (PTE_PRESENT(pte)) {
[fb84455]707 if (((access == PF_ACCESS_READ) && PTE_READABLE(pte)) ||
708 (access == PF_ACCESS_WRITE && PTE_WRITABLE(pte)) ||
709 (access == PF_ACCESS_EXEC && PTE_EXECUTABLE(pte))) {
710 page_table_unlock(AS, false);
711 mutex_unlock(&area->lock);
712 mutex_unlock(&AS->lock);
713 return AS_PF_OK;
714 }
[2299914]715 }
716 }
[20d50a1]717
718 /*
[8182031]719 * Resort to the backend page fault handler.
[20d50a1]720 */
[0ee077ee]721 if (area->backend->page_fault(area, page, access) != AS_PF_OK) {
[8182031]722 page_table_unlock(AS, false);
723 mutex_unlock(&area->lock);
724 mutex_unlock(&AS->lock);
725 goto page_fault;
726 }
[20d50a1]727
[8182031]728 page_table_unlock(AS, false);
[1068f6a]729 mutex_unlock(&area->lock);
730 mutex_unlock(&AS->lock);
[e3c762cd]731 return AS_PF_OK;
732
733page_fault:
734 if (THREAD->in_copy_from_uspace) {
735 THREAD->in_copy_from_uspace = false;
736 istate_set_retaddr(istate, (__address) &memcpy_from_uspace_failover_address);
737 } else if (THREAD->in_copy_to_uspace) {
738 THREAD->in_copy_to_uspace = false;
739 istate_set_retaddr(istate, (__address) &memcpy_to_uspace_failover_address);
740 } else {
741 return AS_PF_FAULT;
742 }
743
744 return AS_PF_DEFER;
[20d50a1]745}
746
[7e4e532]747/** Switch address spaces.
[1068f6a]748 *
749 * Note that this function cannot sleep as it is essentially a part of
[47800e0]750 * scheduling. Sleeping here would lead to deadlock on wakeup.
[20d50a1]751 *
[7e4e532]752 * @param old Old address space or NULL.
753 * @param new New address space.
[20d50a1]754 */
[7e4e532]755void as_switch(as_t *old, as_t *new)
[20d50a1]756{
757 ipl_t ipl;
[7e4e532]758 bool needs_asid = false;
[4512d7e]759
[20d50a1]760 ipl = interrupts_disable();
[47800e0]761 spinlock_lock(&inactive_as_with_asid_lock);
[7e4e532]762
763 /*
764 * First, take care of the old address space.
765 */
766 if (old) {
[1068f6a]767 mutex_lock_active(&old->lock);
[47800e0]768 ASSERT(old->cpu_refcount);
769 if((--old->cpu_refcount == 0) && (old != AS_KERNEL)) {
[7e4e532]770 /*
771 * The old address space is no longer active on
772 * any processor. It can be appended to the
773 * list of inactive address spaces with assigned
774 * ASID.
775 */
776 ASSERT(old->asid != ASID_INVALID);
777 list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
778 }
[1068f6a]779 mutex_unlock(&old->lock);
[7e4e532]780 }
781
782 /*
783 * Second, prepare the new address space.
784 */
[1068f6a]785 mutex_lock_active(&new->lock);
[47800e0]786 if ((new->cpu_refcount++ == 0) && (new != AS_KERNEL)) {
[7e4e532]787 if (new->asid != ASID_INVALID)
788 list_remove(&new->inactive_as_with_asid_link);
789 else
790 needs_asid = true; /* defer call to asid_get() until new->lock is released */
791 }
792 SET_PTL0_ADDRESS(new->page_table);
[1068f6a]793 mutex_unlock(&new->lock);
[20d50a1]794
[7e4e532]795 if (needs_asid) {
796 /*
797 * Allocation of new ASID was deferred
798 * until now in order to avoid deadlock.
799 */
800 asid_t asid;
801
802 asid = asid_get();
[1068f6a]803 mutex_lock_active(&new->lock);
[7e4e532]804 new->asid = asid;
[1068f6a]805 mutex_unlock(&new->lock);
[7e4e532]806 }
[47800e0]807 spinlock_unlock(&inactive_as_with_asid_lock);
[7e4e532]808 interrupts_restore(ipl);
809
[20d50a1]810 /*
811 * Perform architecture-specific steps.
[4512d7e]812 * (e.g. write ASID to hardware register etc.)
[20d50a1]813 */
[7e4e532]814 as_install_arch(new);
[20d50a1]815
[7e4e532]816 AS = new;
[20d50a1]817}
[6a3c9a7]818
[df0103f7]819/** Convert address space area flags to page flags.
[6a3c9a7]820 *
[df0103f7]821 * @param aflags Flags of some address space area.
[6a3c9a7]822 *
[df0103f7]823 * @return Flags to be passed to page_mapping_insert().
[6a3c9a7]824 */
[df0103f7]825int area_flags_to_page_flags(int aflags)
[6a3c9a7]826{
827 int flags;
828
[9a8d91b]829 flags = PAGE_USER | PAGE_PRESENT;
[c23502d]830
[df0103f7]831 if (aflags & AS_AREA_READ)
[c23502d]832 flags |= PAGE_READ;
833
[df0103f7]834 if (aflags & AS_AREA_WRITE)
[c23502d]835 flags |= PAGE_WRITE;
836
[df0103f7]837 if (aflags & AS_AREA_EXEC)
[c23502d]838 flags |= PAGE_EXEC;
[6a3c9a7]839
[0ee077ee]840 if (aflags & AS_AREA_CACHEABLE)
[9a8d91b]841 flags |= PAGE_CACHEABLE;
842
[6a3c9a7]843 return flags;
844}
[ef67bab]845
[df0103f7]846/** Compute flags for virtual address translation subsytem.
847 *
848 * The address space area must be locked.
849 * Interrupts must be disabled.
850 *
851 * @param a Address space area.
852 *
853 * @return Flags to be used in page_mapping_insert().
854 */
[8182031]855int as_area_get_flags(as_area_t *a)
[df0103f7]856{
857 return area_flags_to_page_flags(a->flags);
858}
859
[ef67bab]860/** Create page table.
861 *
862 * Depending on architecture, create either address space
863 * private or global page table.
864 *
865 * @param flags Flags saying whether the page table is for kernel address space.
866 *
867 * @return First entry of the page table.
868 */
869pte_t *page_table_create(int flags)
870{
871 ASSERT(as_operations);
872 ASSERT(as_operations->page_table_create);
873
874 return as_operations->page_table_create(flags);
875}
[d3e7ff4]876
[482826d]877/** Destroy page table.
878 *
879 * Destroy page table in architecture specific way.
880 *
881 * @param page_table Physical address of PTL0.
882 */
883void page_table_destroy(pte_t *page_table)
884{
885 ASSERT(as_operations);
886 ASSERT(as_operations->page_table_destroy);
887
888 as_operations->page_table_destroy(page_table);
889}
890
[2299914]891/** Lock page table.
892 *
893 * This function should be called before any page_mapping_insert(),
894 * page_mapping_remove() and page_mapping_find().
895 *
896 * Locking order is such that address space areas must be locked
897 * prior to this call. Address space can be locked prior to this
898 * call in which case the lock argument is false.
899 *
900 * @param as Address space.
[9179d0a]901 * @param lock If false, do not attempt to lock as->lock.
[2299914]902 */
903void page_table_lock(as_t *as, bool lock)
904{
905 ASSERT(as_operations);
906 ASSERT(as_operations->page_table_lock);
907
908 as_operations->page_table_lock(as, lock);
909}
910
911/** Unlock page table.
912 *
913 * @param as Address space.
[9179d0a]914 * @param unlock If false, do not attempt to unlock as->lock.
[2299914]915 */
916void page_table_unlock(as_t *as, bool unlock)
917{
918 ASSERT(as_operations);
919 ASSERT(as_operations->page_table_unlock);
920
921 as_operations->page_table_unlock(as, unlock);
922}
923
[d3e7ff4]924
925/** Find address space area and lock it.
926 *
927 * The address space must be locked and interrupts must be disabled.
928 *
929 * @param as Address space.
930 * @param va Virtual address.
931 *
932 * @return Locked address space area containing va on success or NULL on failure.
933 */
934as_area_t *find_area_and_lock(as_t *as, __address va)
935{
936 as_area_t *a;
[252127e]937 btree_node_t *leaf, *lnode;
938 int i;
939
940 a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
941 if (a) {
942 /* va is the base address of an address space area */
[1068f6a]943 mutex_lock(&a->lock);
[252127e]944 return a;
945 }
[d3e7ff4]946
[252127e]947 /*
[c47912f]948 * Search the leaf node and the righmost record of its left neighbour
[252127e]949 * to find out whether this is a miss or va belongs to an address
950 * space area found there.
951 */
952
953 /* First, search the leaf node itself. */
954 for (i = 0; i < leaf->keys; i++) {
955 a = (as_area_t *) leaf->value[i];
[1068f6a]956 mutex_lock(&a->lock);
[252127e]957 if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) {
958 return a;
959 }
[1068f6a]960 mutex_unlock(&a->lock);
[252127e]961 }
[d3e7ff4]962
[252127e]963 /*
[c47912f]964 * Second, locate the left neighbour and test its last record.
[b26db0c]965 * Because of its position in the B+tree, it must have base < va.
[252127e]966 */
[c47912f]967 if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
[252127e]968 a = (as_area_t *) lnode->value[lnode->keys - 1];
[1068f6a]969 mutex_lock(&a->lock);
[252127e]970 if (va < a->base + a->pages * PAGE_SIZE) {
[37e7d2b9]971 return a;
[252127e]972 }
[1068f6a]973 mutex_unlock(&a->lock);
[d3e7ff4]974 }
975
976 return NULL;
977}
[37e7d2b9]978
979/** Check area conflicts with other areas.
980 *
981 * The address space must be locked and interrupts must be disabled.
982 *
983 * @param as Address space.
984 * @param va Starting virtual address of the area being tested.
985 * @param size Size of the area being tested.
986 * @param avoid_area Do not touch this area.
987 *
988 * @return True if there is no conflict, false otherwise.
989 */
990bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area)
991{
992 as_area_t *a;
[252127e]993 btree_node_t *leaf, *node;
994 int i;
[37e7d2b9]995
[5a7d9d1]996 /*
997 * We don't want any area to have conflicts with NULL page.
998 */
999 if (overlaps(va, size, NULL, PAGE_SIZE))
1000 return false;
1001
[252127e]1002 /*
1003 * The leaf node is found in O(log n), where n is proportional to
1004 * the number of address space areas belonging to as.
1005 * The check for conflicts is then attempted on the rightmost
[c47912f]1006 * record in the left neighbour, the leftmost record in the right
1007 * neighbour and all records in the leaf node itself.
[252127e]1008 */
1009
1010 if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) {
1011 if (a != avoid_area)
1012 return false;
1013 }
1014
1015 /* First, check the two border cases. */
[c47912f]1016 if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
[252127e]1017 a = (as_area_t *) node->value[node->keys - 1];
[1068f6a]1018 mutex_lock(&a->lock);
[252127e]1019 if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
[1068f6a]1020 mutex_unlock(&a->lock);
[252127e]1021 return false;
1022 }
[1068f6a]1023 mutex_unlock(&a->lock);
[252127e]1024 }
[c47912f]1025 if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) {
[252127e]1026 a = (as_area_t *) node->value[0];
[1068f6a]1027 mutex_lock(&a->lock);
[252127e]1028 if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
[1068f6a]1029 mutex_unlock(&a->lock);
[252127e]1030 return false;
1031 }
[1068f6a]1032 mutex_unlock(&a->lock);
[252127e]1033 }
1034
1035 /* Second, check the leaf node. */
1036 for (i = 0; i < leaf->keys; i++) {
1037 a = (as_area_t *) leaf->value[i];
[37e7d2b9]1038
1039 if (a == avoid_area)
1040 continue;
[252127e]1041
[1068f6a]1042 mutex_lock(&a->lock);
[252127e]1043 if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
[1068f6a]1044 mutex_unlock(&a->lock);
[252127e]1045 return false;
1046 }
[1068f6a]1047 mutex_unlock(&a->lock);
[5a7d9d1]1048 }
[37e7d2b9]1049
[5a7d9d1]1050 /*
1051 * So far, the area does not conflict with other areas.
1052 * Check if it doesn't conflict with kernel address space.
1053 */
1054 if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
1055 return !overlaps(va, size,
1056 KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START);
[37e7d2b9]1057 }
1058
1059 return true;
1060}
[df0103f7]1061
[1068f6a]1062/** Return size of the address space area with given base. */
[7c23af9]1063size_t as_get_size(__address base)
1064{
1065 ipl_t ipl;
1066 as_area_t *src_area;
1067 size_t size;
1068
1069 ipl = interrupts_disable();
1070 src_area = find_area_and_lock(AS, base);
1071 if (src_area){
1072 size = src_area->pages * PAGE_SIZE;
[1068f6a]1073 mutex_unlock(&src_area->lock);
[7c23af9]1074 } else {
1075 size = 0;
1076 }
1077 interrupts_restore(ipl);
1078 return size;
1079}
1080
[25bf215]1081/** Mark portion of address space area as used.
1082 *
1083 * The address space area must be already locked.
1084 *
1085 * @param a Address space area.
1086 * @param page First page to be marked.
1087 * @param count Number of page to be marked.
1088 *
1089 * @return 0 on failure and 1 on success.
1090 */
1091int used_space_insert(as_area_t *a, __address page, count_t count)
1092{
1093 btree_node_t *leaf, *node;
1094 count_t pages;
1095 int i;
1096
1097 ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE));
1098 ASSERT(count);
1099
1100 pages = (count_t) btree_search(&a->used_space, page, &leaf);
1101 if (pages) {
1102 /*
1103 * We hit the beginning of some used space.
1104 */
1105 return 0;
1106 }
1107
[a6cb8cb]1108 if (!leaf->keys) {
1109 btree_insert(&a->used_space, page, (void *) count, leaf);
1110 return 1;
1111 }
1112
[25bf215]1113 node = btree_leaf_node_left_neighbour(&a->used_space, leaf);
1114 if (node) {
1115 __address left_pg = node->key[node->keys - 1], right_pg = leaf->key[0];
1116 count_t left_cnt = (count_t) node->value[node->keys - 1], right_cnt = (count_t) leaf->value[0];
1117
1118 /*
1119 * Examine the possibility that the interval fits
1120 * somewhere between the rightmost interval of
1121 * the left neigbour and the first interval of the leaf.
1122 */
1123
1124 if (page >= right_pg) {
1125 /* Do nothing. */
1126 } else if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
1127 /* The interval intersects with the left interval. */
1128 return 0;
1129 } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
1130 /* The interval intersects with the right interval. */
1131 return 0;
1132 } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
1133 /* The interval can be added by merging the two already present intervals. */
[56789125]1134 node->value[node->keys - 1] += count + right_cnt;
[25bf215]1135 btree_remove(&a->used_space, right_pg, leaf);
1136 return 1;
1137 } else if (page == left_pg + left_cnt*PAGE_SIZE) {
1138 /* The interval can be added by simply growing the left interval. */
[56789125]1139 node->value[node->keys - 1] += count;
[25bf215]1140 return 1;
1141 } else if (page + count*PAGE_SIZE == right_pg) {
1142 /*
1143 * The interval can be addded by simply moving base of the right
1144 * interval down and increasing its size accordingly.
1145 */
[56789125]1146 leaf->value[0] += count;
[25bf215]1147 leaf->key[0] = page;
1148 return 1;
1149 } else {
1150 /*
1151 * The interval is between both neigbouring intervals,
1152 * but cannot be merged with any of them.
1153 */
1154 btree_insert(&a->used_space, page, (void *) count, leaf);
1155 return 1;
1156 }
1157 } else if (page < leaf->key[0]) {
1158 __address right_pg = leaf->key[0];
1159 count_t right_cnt = (count_t) leaf->value[0];
1160
1161 /*
1162 * Investigate the border case in which the left neighbour does not
1163 * exist but the interval fits from the left.
1164 */
1165
1166 if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
1167 /* The interval intersects with the right interval. */
1168 return 0;
1169 } else if (page + count*PAGE_SIZE == right_pg) {
1170 /*
1171 * The interval can be added by moving the base of the right interval down
1172 * and increasing its size accordingly.
1173 */
1174 leaf->key[0] = page;
[56789125]1175 leaf->value[0] += count;
[25bf215]1176 return 1;
1177 } else {
1178 /*
1179 * The interval doesn't adjoin with the right interval.
1180 * It must be added individually.
1181 */
1182 btree_insert(&a->used_space, page, (void *) count, leaf);
1183 return 1;
1184 }
1185 }
1186
1187 node = btree_leaf_node_right_neighbour(&a->used_space, leaf);
1188 if (node) {
1189 __address left_pg = leaf->key[leaf->keys - 1], right_pg = node->key[0];
1190 count_t left_cnt = (count_t) leaf->value[leaf->keys - 1], right_cnt = (count_t) node->value[0];
1191
1192 /*
1193 * Examine the possibility that the interval fits
1194 * somewhere between the leftmost interval of
1195 * the right neigbour and the last interval of the leaf.
1196 */
1197
1198 if (page < left_pg) {
1199 /* Do nothing. */
1200 } else if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
1201 /* The interval intersects with the left interval. */
1202 return 0;
1203 } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
1204 /* The interval intersects with the right interval. */
1205 return 0;
1206 } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
1207 /* The interval can be added by merging the two already present intervals. */
[56789125]1208 leaf->value[leaf->keys - 1] += count + right_cnt;
[25bf215]1209 btree_remove(&a->used_space, right_pg, node);
1210 return 1;
1211 } else if (page == left_pg + left_cnt*PAGE_SIZE) {
1212 /* The interval can be added by simply growing the left interval. */
[56789125]1213 leaf->value[leaf->keys - 1] += count;
[25bf215]1214 return 1;
1215 } else if (page + count*PAGE_SIZE == right_pg) {
1216 /*
1217 * The interval can be addded by simply moving base of the right
1218 * interval down and increasing its size accordingly.
1219 */
[56789125]1220 node->value[0] += count;
[25bf215]1221 node->key[0] = page;
1222 return 1;
1223 } else {
1224 /*
1225 * The interval is between both neigbouring intervals,
1226 * but cannot be merged with any of them.
1227 */
1228 btree_insert(&a->used_space, page, (void *) count, leaf);
1229 return 1;
1230 }
1231 } else if (page >= leaf->key[leaf->keys - 1]) {
1232 __address left_pg = leaf->key[leaf->keys - 1];
1233 count_t left_cnt = (count_t) leaf->value[leaf->keys - 1];
1234
1235 /*
1236 * Investigate the border case in which the right neighbour does not
1237 * exist but the interval fits from the right.
1238 */
1239
1240 if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
[56789125]1241 /* The interval intersects with the left interval. */
[25bf215]1242 return 0;
1243 } else if (left_pg + left_cnt*PAGE_SIZE == page) {
1244 /* The interval can be added by growing the left interval. */
[56789125]1245 leaf->value[leaf->keys - 1] += count;
[25bf215]1246 return 1;
1247 } else {
1248 /*
1249 * The interval doesn't adjoin with the left interval.
1250 * It must be added individually.
1251 */
1252 btree_insert(&a->used_space, page, (void *) count, leaf);
1253 return 1;
1254 }
1255 }
1256
1257 /*
1258 * Note that if the algorithm made it thus far, the interval can fit only
1259 * between two other intervals of the leaf. The two border cases were already
1260 * resolved.
1261 */
1262 for (i = 1; i < leaf->keys; i++) {
1263 if (page < leaf->key[i]) {
1264 __address left_pg = leaf->key[i - 1], right_pg = leaf->key[i];
1265 count_t left_cnt = (count_t) leaf->value[i - 1], right_cnt = (count_t) leaf->value[i];
1266
1267 /*
1268 * The interval fits between left_pg and right_pg.
1269 */
1270
1271 if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
1272 /* The interval intersects with the left interval. */
1273 return 0;
1274 } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
1275 /* The interval intersects with the right interval. */
1276 return 0;
1277 } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
1278 /* The interval can be added by merging the two already present intervals. */
[56789125]1279 leaf->value[i - 1] += count + right_cnt;
[25bf215]1280 btree_remove(&a->used_space, right_pg, leaf);
1281 return 1;
1282 } else if (page == left_pg + left_cnt*PAGE_SIZE) {
1283 /* The interval can be added by simply growing the left interval. */
[56789125]1284 leaf->value[i - 1] += count;
[25bf215]1285 return 1;
1286 } else if (page + count*PAGE_SIZE == right_pg) {
1287 /*
1288 * The interval can be addded by simply moving base of the right
1289 * interval down and increasing its size accordingly.
1290 */
[56789125]1291 leaf->value[i] += count;
[25bf215]1292 leaf->key[i] = page;
1293 return 1;
1294 } else {
1295 /*
1296 * The interval is between both neigbouring intervals,
1297 * but cannot be merged with any of them.
1298 */
1299 btree_insert(&a->used_space, page, (void *) count, leaf);
1300 return 1;
1301 }
1302 }
1303 }
1304
1305 panic("Inconsistency detected while adding %d pages of used space at %P.\n", count, page);
1306}
1307
1308/** Mark portion of address space area as unused.
1309 *
1310 * The address space area must be already locked.
1311 *
1312 * @param a Address space area.
1313 * @param page First page to be marked.
1314 * @param count Number of page to be marked.
1315 *
1316 * @return 0 on failure and 1 on success.
1317 */
1318int used_space_remove(as_area_t *a, __address page, count_t count)
1319{
1320 btree_node_t *leaf, *node;
1321 count_t pages;
1322 int i;
1323
1324 ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE));
1325 ASSERT(count);
1326
1327 pages = (count_t) btree_search(&a->used_space, page, &leaf);
1328 if (pages) {
1329 /*
1330 * We are lucky, page is the beginning of some interval.
1331 */
1332 if (count > pages) {
1333 return 0;
1334 } else if (count == pages) {
1335 btree_remove(&a->used_space, page, leaf);
[56789125]1336 return 1;
[25bf215]1337 } else {
1338 /*
1339 * Find the respective interval.
1340 * Decrease its size and relocate its start address.
1341 */
1342 for (i = 0; i < leaf->keys; i++) {
1343 if (leaf->key[i] == page) {
1344 leaf->key[i] += count*PAGE_SIZE;
[56789125]1345 leaf->value[i] -= count;
[25bf215]1346 return 1;
1347 }
1348 }
1349 goto error;
1350 }
1351 }
1352
1353 node = btree_leaf_node_left_neighbour(&a->used_space, leaf);
1354 if (node && page < leaf->key[0]) {
1355 __address left_pg = node->key[node->keys - 1];
1356 count_t left_cnt = (count_t) node->value[node->keys - 1];
1357
1358 if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
1359 if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
1360 /*
1361 * The interval is contained in the rightmost interval
1362 * of the left neighbour and can be removed by
1363 * updating the size of the bigger interval.
1364 */
[56789125]1365 node->value[node->keys - 1] -= count;
[25bf215]1366 return 1;
1367 } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
[56789125]1368 count_t new_cnt;
[25bf215]1369
1370 /*
1371 * The interval is contained in the rightmost interval
1372 * of the left neighbour but its removal requires
1373 * both updating the size of the original interval and
1374 * also inserting a new interval.
1375 */
[56789125]1376 new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
1377 node->value[node->keys - 1] -= count + new_cnt;
[25bf215]1378 btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
1379 return 1;
1380 }
1381 }
1382 return 0;
1383 } else if (page < leaf->key[0]) {
1384 return 0;
1385 }
1386
1387 if (page > leaf->key[leaf->keys - 1]) {
1388 __address left_pg = leaf->key[leaf->keys - 1];
1389 count_t left_cnt = (count_t) leaf->value[leaf->keys - 1];
1390
1391 if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
1392 if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
1393 /*
1394 * The interval is contained in the rightmost interval
1395 * of the leaf and can be removed by updating the size
1396 * of the bigger interval.
1397 */
[56789125]1398 leaf->value[leaf->keys - 1] -= count;
[25bf215]1399 return 1;
1400 } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
[56789125]1401 count_t new_cnt;
[25bf215]1402
1403 /*
1404 * The interval is contained in the rightmost interval
1405 * of the leaf but its removal requires both updating
1406 * the size of the original interval and
1407 * also inserting a new interval.
1408 */
[56789125]1409 new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
1410 leaf->value[leaf->keys - 1] -= count + new_cnt;
[25bf215]1411 btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
1412 return 1;
1413 }
1414 }
1415 return 0;
1416 }
1417
1418 /*
1419 * The border cases have been already resolved.
1420 * Now the interval can be only between intervals of the leaf.
1421 */
1422 for (i = 1; i < leaf->keys - 1; i++) {
1423 if (page < leaf->key[i]) {
1424 __address left_pg = leaf->key[i - 1];
1425 count_t left_cnt = (count_t) leaf->value[i - 1];
1426
1427 /*
1428 * Now the interval is between intervals corresponding to (i - 1) and i.
1429 */
1430 if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
1431 if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
1432 /*
1433 * The interval is contained in the interval (i - 1)
1434 * of the leaf and can be removed by updating the size
1435 * of the bigger interval.
1436 */
[56789125]1437 leaf->value[i - 1] -= count;
[25bf215]1438 return 1;
1439 } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
[56789125]1440 count_t new_cnt;
[25bf215]1441
1442 /*
1443 * The interval is contained in the interval (i - 1)
1444 * of the leaf but its removal requires both updating
1445 * the size of the original interval and
1446 * also inserting a new interval.
1447 */
[56789125]1448 new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
1449 leaf->value[i - 1] -= count + new_cnt;
[25bf215]1450 btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
1451 return 1;
1452 }
1453 }
1454 return 0;
1455 }
1456 }
1457
1458error:
1459 panic("Inconsistency detected while removing %d pages of used space from %P.\n", count, page);
1460}
1461
[8182031]1462/** Remove reference to address space area share info.
1463 *
1464 * If the reference count drops to 0, the sh_info is deallocated.
1465 *
1466 * @param sh_info Pointer to address space area share info.
1467 */
1468void sh_info_remove_reference(share_info_t *sh_info)
1469{
1470 bool dealloc = false;
1471
1472 mutex_lock(&sh_info->lock);
1473 ASSERT(sh_info->refcount);
1474 if (--sh_info->refcount == 0) {
1475 dealloc = true;
1476 bool cond;
1477
1478 /*
1479 * Now walk carefully the pagemap B+tree and free/remove
1480 * reference from all frames found there.
1481 */
1482 for (cond = true; cond;) {
1483 btree_node_t *node;
1484
1485 ASSERT(!list_empty(&sh_info->pagemap.leaf_head));
1486 node = list_get_instance(sh_info->pagemap.leaf_head.next, btree_node_t, leaf_link);
1487 if ((cond = node->keys)) {
1488 frame_free(ADDR2PFN((__address) node->value[0]));
1489 }
1490 }
1491
1492 }
1493 mutex_unlock(&sh_info->lock);
1494
1495 if (dealloc) {
1496 btree_destroy(&sh_info->pagemap);
1497 free(sh_info);
1498 }
1499}
1500
[df0103f7]1501/*
1502 * Address space related syscalls.
1503 */
1504
1505/** Wrapper for as_area_create(). */
1506__native sys_as_area_create(__address address, size_t size, int flags)
1507{
[0ee077ee]1508 if (as_area_create(AS, flags | AS_AREA_CACHEABLE, size, address, AS_AREA_ATTR_NONE, &anon_backend, NULL))
[df0103f7]1509 return (__native) address;
1510 else
1511 return (__native) -1;
1512}
1513
1514/** Wrapper for as_area_resize. */
1515__native sys_as_area_resize(__address address, size_t size, int flags)
1516{
[7242a78e]1517 return (__native) as_area_resize(AS, address, size, 0);
1518}
1519
1520/** Wrapper for as_area_destroy. */
1521__native sys_as_area_destroy(__address address)
1522{
1523 return (__native) as_area_destroy(AS, address);
[df0103f7]1524}
Note: See TracBrowser for help on using the repository browser.