source: mainline/generic/src/mm/as.c@ f8d069e8

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

Remove three infinite loops introduced yesterday :-)

Make ia64 ready to load 5 ELF images.

  • Property mode set to 100644
File size: 40.7 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;
[f8d069e8]139 link_t *cur;
[482826d]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 */
[f8d069e8]159 for (cur = as->as_area_btree.leaf_head.next; cur != &as->as_area_btree.leaf_head; cur = cur->next) {
[482826d]160 btree_node_t *node;
[f8d069e8]161 int i;
[482826d]162
[f8d069e8]163 node = list_get_instance(cur, btree_node_t, leaf_link);
164 for (i = 0; i < node->keys; i++)
165 as_area_destroy(as, node->key[i]);
[482826d]166 }
[f8d069e8]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;
[f8d069e8]413 link_t *cur;
[7242a78e]414 ipl_t ipl;
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 */
[f8d069e8]436 for (cur = area->used_space.leaf_head.next; cur != &area->used_space.leaf_head; cur = cur->next) {
[567807b1]437 btree_node_t *node;
[f8d069e8]438 int i;
[56789125]439
[f8d069e8]440 node = list_get_instance(cur, btree_node_t, leaf_link);
441 for (i = 0; i < node->keys; i++) {
442 __address b = node->key[i];
443 count_t j;
[567807b1]444 pte_t *pte;
[56789125]445
[f8d069e8]446 for (j = 0; j < (count_t) node->value[i]; j++) {
[567807b1]447 page_table_lock(as, false);
[f8d069e8]448 pte = page_mapping_find(as, b + j*PAGE_SIZE);
[567807b1]449 ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte));
[0ee077ee]450 if (area->backend && area->backend->frame_free) {
451 area->backend->frame_free(area,
[f8d069e8]452 b + j*PAGE_SIZE, PTE_GET_FRAME(pte));
[56789125]453 }
[f8d069e8]454 page_mapping_remove(as, b + j*PAGE_SIZE);
[567807b1]455 page_table_unlock(as, false);
[7242a78e]456 }
457 }
458 }
[56789125]459
[7242a78e]460 /*
[5552d60]461 * Finish TLB shootdown sequence.
[7242a78e]462 */
463 tlb_invalidate_pages(AS->asid, area->base, area->pages);
464 tlb_shootdown_finalize();
[5552d60]465
466 btree_destroy(&area->used_space);
[7242a78e]467
[8d4f2ae]468 area->attributes |= AS_AREA_ATTR_PARTIAL;
[8182031]469
470 if (area->sh_info)
471 sh_info_remove_reference(area->sh_info);
472
[1068f6a]473 mutex_unlock(&area->lock);
[7242a78e]474
475 /*
476 * Remove the empty area from address space.
477 */
478 btree_remove(&AS->as_area_btree, base, NULL);
479
[8d4f2ae]480 free(area);
481
[1068f6a]482 mutex_unlock(&AS->lock);
[7242a78e]483 interrupts_restore(ipl);
484 return 0;
[df0103f7]485}
486
[8d6bc2d5]487/** Share address space area with another or the same address space.
[df0103f7]488 *
[0ee077ee]489 * Address space area mapping is shared with a new address space area.
490 * If the source address space area has not been shared so far,
491 * a new sh_info is created. The new address space area simply gets the
492 * sh_info of the source area. The process of duplicating the
493 * mapping is done through the backend share function.
[8d6bc2d5]494 *
[fd4d8c0]495 * @param src_as Pointer to source address space.
[a9e8b39]496 * @param src_base Base address of the source address space area.
[fd4d8c0]497 * @param acc_size Expected size of the source area.
[46fc2f9]498 * @param dst_as Pointer to destination address space.
[fd4d8c0]499 * @param dst_base Target base address.
500 * @param dst_flags_mask Destination address space area flags mask.
[df0103f7]501 *
[7242a78e]502 * @return Zero on success or ENOENT if there is no such task or
[df0103f7]503 * if there is no such address space area,
504 * EPERM if there was a problem in accepting the area or
505 * ENOMEM if there was a problem in allocating destination
[8d6bc2d5]506 * address space area. ENOTSUP is returned if an attempt
507 * to share non-anonymous address space area is detected.
[df0103f7]508 */
[8d6bc2d5]509int as_area_share(as_t *src_as, __address src_base, size_t acc_size,
[46fc2f9]510 as_t *dst_as, __address dst_base, int dst_flags_mask)
[df0103f7]511{
512 ipl_t ipl;
[a9e8b39]513 int src_flags;
514 size_t src_size;
515 as_area_t *src_area, *dst_area;
[8d6bc2d5]516 share_info_t *sh_info;
[0ee077ee]517 mem_backend_t *src_backend;
518 mem_backend_data_t src_backend_data;
[d6e5cbc]519
[7c23af9]520 ipl = interrupts_disable();
[1068f6a]521 mutex_lock(&src_as->lock);
[7c23af9]522 src_area = find_area_and_lock(src_as, src_base);
[a9e8b39]523 if (!src_area) {
[6fa476f7]524 /*
525 * Could not find the source address space area.
526 */
[1068f6a]527 mutex_unlock(&src_as->lock);
[6fa476f7]528 interrupts_restore(ipl);
529 return ENOENT;
530 }
[8d6bc2d5]531
[0ee077ee]532 if (!src_area->backend || !src_area->backend->share) {
[8d6bc2d5]533 /*
[0ee077ee]534 * There is now backend or the backend does not
535 * know how to share the area.
[8d6bc2d5]536 */
537 mutex_unlock(&src_area->lock);
538 mutex_unlock(&src_as->lock);
539 interrupts_restore(ipl);
540 return ENOTSUP;
541 }
542
[a9e8b39]543 src_size = src_area->pages * PAGE_SIZE;
544 src_flags = src_area->flags;
[0ee077ee]545 src_backend = src_area->backend;
546 src_backend_data = src_area->backend_data;
[8d6bc2d5]547
[76d7305]548 if (src_size != acc_size || (src_flags & dst_flags_mask) != dst_flags_mask) {
[8d6bc2d5]549 mutex_unlock(&src_area->lock);
550 mutex_unlock(&src_as->lock);
[df0103f7]551 interrupts_restore(ipl);
552 return EPERM;
553 }
[8d6bc2d5]554
555 /*
556 * Now we are committed to sharing the area.
557 * First prepare the area for sharing.
558 * Then it will be safe to unlock it.
559 */
560 sh_info = src_area->sh_info;
561 if (!sh_info) {
562 sh_info = (share_info_t *) malloc(sizeof(share_info_t), 0);
563 mutex_initialize(&sh_info->lock);
564 sh_info->refcount = 2;
565 btree_create(&sh_info->pagemap);
566 src_area->sh_info = sh_info;
567 } else {
568 mutex_lock(&sh_info->lock);
569 sh_info->refcount++;
570 mutex_unlock(&sh_info->lock);
571 }
572
[0ee077ee]573 src_area->backend->share(src_area);
[8d6bc2d5]574
575 mutex_unlock(&src_area->lock);
576 mutex_unlock(&src_as->lock);
577
[df0103f7]578 /*
[a9e8b39]579 * Create copy of the source address space area.
580 * The destination area is created with AS_AREA_ATTR_PARTIAL
581 * attribute set which prevents race condition with
582 * preliminary as_page_fault() calls.
[fd4d8c0]583 * The flags of the source area are masked against dst_flags_mask
584 * to support sharing in less privileged mode.
[df0103f7]585 */
[76d7305]586 dst_area = as_area_create(dst_as, dst_flags_mask, src_size, dst_base,
[0ee077ee]587 AS_AREA_ATTR_PARTIAL, src_backend, &src_backend_data);
[a9e8b39]588 if (!dst_area) {
[df0103f7]589 /*
590 * Destination address space area could not be created.
591 */
[8d6bc2d5]592 sh_info_remove_reference(sh_info);
593
[df0103f7]594 interrupts_restore(ipl);
595 return ENOMEM;
596 }
597
[a9e8b39]598 /*
599 * Now the destination address space area has been
600 * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
[8d6bc2d5]601 * attribute and set the sh_info.
[a9e8b39]602 */
[1068f6a]603 mutex_lock(&dst_area->lock);
[a9e8b39]604 dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
[8d6bc2d5]605 dst_area->sh_info = sh_info;
[1068f6a]606 mutex_unlock(&dst_area->lock);
[df0103f7]607
608 interrupts_restore(ipl);
609
610 return 0;
611}
612
[fb84455]613/** Check access mode for address space area.
614 *
615 * The address space area must be locked prior to this call.
616 *
617 * @param area Address space area.
618 * @param access Access mode.
619 *
620 * @return False if access violates area's permissions, true otherwise.
621 */
622bool as_area_check_access(as_area_t *area, pf_access_t access)
623{
624 int flagmap[] = {
625 [PF_ACCESS_READ] = AS_AREA_READ,
626 [PF_ACCESS_WRITE] = AS_AREA_WRITE,
627 [PF_ACCESS_EXEC] = AS_AREA_EXEC
628 };
629
630 if (!(area->flags & flagmap[access]))
631 return false;
632
633 return true;
634}
635
[20d50a1]636/** Handle page fault within the current address space.
637 *
[8182031]638 * This is the high-level page fault handler. It decides
639 * whether the page fault can be resolved by any backend
640 * and if so, it invokes the backend to resolve the page
641 * fault.
642 *
[20d50a1]643 * Interrupts are assumed disabled.
644 *
645 * @param page Faulting page.
[567807b1]646 * @param access Access mode that caused the fault (i.e. read/write/exec).
[e3c762cd]647 * @param istate Pointer to interrupted state.
[20d50a1]648 *
[8182031]649 * @return AS_PF_FAULT on page fault, AS_PF_OK on success or AS_PF_DEFER if the
650 * fault was caused by copy_to_uspace() or copy_from_uspace().
[20d50a1]651 */
[567807b1]652int as_page_fault(__address page, pf_access_t access, istate_t *istate)
[20d50a1]653{
[2299914]654 pte_t *pte;
[d3e7ff4]655 as_area_t *area;
[20d50a1]656
[1068f6a]657 if (!THREAD)
[8182031]658 return AS_PF_FAULT;
[1068f6a]659
[20d50a1]660 ASSERT(AS);
[2299914]661
[1068f6a]662 mutex_lock(&AS->lock);
[d3e7ff4]663 area = find_area_and_lock(AS, page);
[20d50a1]664 if (!area) {
665 /*
666 * No area contained mapping for 'page'.
667 * Signal page fault to low-level handler.
668 */
[1068f6a]669 mutex_unlock(&AS->lock);
[e3c762cd]670 goto page_fault;
[20d50a1]671 }
672
[a9e8b39]673 if (area->attributes & AS_AREA_ATTR_PARTIAL) {
674 /*
675 * The address space area is not fully initialized.
676 * Avoid possible race by returning error.
677 */
[1068f6a]678 mutex_unlock(&area->lock);
679 mutex_unlock(&AS->lock);
[e3c762cd]680 goto page_fault;
[a9e8b39]681 }
682
[0ee077ee]683 if (!area->backend || !area->backend->page_fault) {
[8182031]684 /*
685 * The address space area is not backed by any backend
686 * or the backend cannot handle page faults.
687 */
688 mutex_unlock(&area->lock);
689 mutex_unlock(&AS->lock);
690 goto page_fault;
691 }
[1ace9ea]692
[2299914]693 page_table_lock(AS, false);
694
695 /*
696 * To avoid race condition between two page faults
697 * on the same address, we need to make sure
698 * the mapping has not been already inserted.
699 */
700 if ((pte = page_mapping_find(AS, page))) {
701 if (PTE_PRESENT(pte)) {
[fb84455]702 if (((access == PF_ACCESS_READ) && PTE_READABLE(pte)) ||
703 (access == PF_ACCESS_WRITE && PTE_WRITABLE(pte)) ||
704 (access == PF_ACCESS_EXEC && PTE_EXECUTABLE(pte))) {
705 page_table_unlock(AS, false);
706 mutex_unlock(&area->lock);
707 mutex_unlock(&AS->lock);
708 return AS_PF_OK;
709 }
[2299914]710 }
711 }
[20d50a1]712
713 /*
[8182031]714 * Resort to the backend page fault handler.
[20d50a1]715 */
[0ee077ee]716 if (area->backend->page_fault(area, page, access) != AS_PF_OK) {
[8182031]717 page_table_unlock(AS, false);
718 mutex_unlock(&area->lock);
719 mutex_unlock(&AS->lock);
720 goto page_fault;
721 }
[20d50a1]722
[8182031]723 page_table_unlock(AS, false);
[1068f6a]724 mutex_unlock(&area->lock);
725 mutex_unlock(&AS->lock);
[e3c762cd]726 return AS_PF_OK;
727
728page_fault:
729 if (THREAD->in_copy_from_uspace) {
730 THREAD->in_copy_from_uspace = false;
731 istate_set_retaddr(istate, (__address) &memcpy_from_uspace_failover_address);
732 } else if (THREAD->in_copy_to_uspace) {
733 THREAD->in_copy_to_uspace = false;
734 istate_set_retaddr(istate, (__address) &memcpy_to_uspace_failover_address);
735 } else {
736 return AS_PF_FAULT;
737 }
738
739 return AS_PF_DEFER;
[20d50a1]740}
741
[7e4e532]742/** Switch address spaces.
[1068f6a]743 *
744 * Note that this function cannot sleep as it is essentially a part of
[47800e0]745 * scheduling. Sleeping here would lead to deadlock on wakeup.
[20d50a1]746 *
[7e4e532]747 * @param old Old address space or NULL.
748 * @param new New address space.
[20d50a1]749 */
[7e4e532]750void as_switch(as_t *old, as_t *new)
[20d50a1]751{
752 ipl_t ipl;
[7e4e532]753 bool needs_asid = false;
[4512d7e]754
[20d50a1]755 ipl = interrupts_disable();
[47800e0]756 spinlock_lock(&inactive_as_with_asid_lock);
[7e4e532]757
758 /*
759 * First, take care of the old address space.
760 */
761 if (old) {
[1068f6a]762 mutex_lock_active(&old->lock);
[47800e0]763 ASSERT(old->cpu_refcount);
764 if((--old->cpu_refcount == 0) && (old != AS_KERNEL)) {
[7e4e532]765 /*
766 * The old address space is no longer active on
767 * any processor. It can be appended to the
768 * list of inactive address spaces with assigned
769 * ASID.
770 */
771 ASSERT(old->asid != ASID_INVALID);
772 list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
773 }
[1068f6a]774 mutex_unlock(&old->lock);
[7e4e532]775 }
776
777 /*
778 * Second, prepare the new address space.
779 */
[1068f6a]780 mutex_lock_active(&new->lock);
[47800e0]781 if ((new->cpu_refcount++ == 0) && (new != AS_KERNEL)) {
[7e4e532]782 if (new->asid != ASID_INVALID)
783 list_remove(&new->inactive_as_with_asid_link);
784 else
785 needs_asid = true; /* defer call to asid_get() until new->lock is released */
786 }
787 SET_PTL0_ADDRESS(new->page_table);
[1068f6a]788 mutex_unlock(&new->lock);
[20d50a1]789
[7e4e532]790 if (needs_asid) {
791 /*
792 * Allocation of new ASID was deferred
793 * until now in order to avoid deadlock.
794 */
795 asid_t asid;
796
797 asid = asid_get();
[1068f6a]798 mutex_lock_active(&new->lock);
[7e4e532]799 new->asid = asid;
[1068f6a]800 mutex_unlock(&new->lock);
[7e4e532]801 }
[47800e0]802 spinlock_unlock(&inactive_as_with_asid_lock);
[7e4e532]803 interrupts_restore(ipl);
804
[20d50a1]805 /*
806 * Perform architecture-specific steps.
[4512d7e]807 * (e.g. write ASID to hardware register etc.)
[20d50a1]808 */
[7e4e532]809 as_install_arch(new);
[20d50a1]810
[7e4e532]811 AS = new;
[20d50a1]812}
[6a3c9a7]813
[df0103f7]814/** Convert address space area flags to page flags.
[6a3c9a7]815 *
[df0103f7]816 * @param aflags Flags of some address space area.
[6a3c9a7]817 *
[df0103f7]818 * @return Flags to be passed to page_mapping_insert().
[6a3c9a7]819 */
[df0103f7]820int area_flags_to_page_flags(int aflags)
[6a3c9a7]821{
822 int flags;
823
[9a8d91b]824 flags = PAGE_USER | PAGE_PRESENT;
[c23502d]825
[df0103f7]826 if (aflags & AS_AREA_READ)
[c23502d]827 flags |= PAGE_READ;
828
[df0103f7]829 if (aflags & AS_AREA_WRITE)
[c23502d]830 flags |= PAGE_WRITE;
831
[df0103f7]832 if (aflags & AS_AREA_EXEC)
[c23502d]833 flags |= PAGE_EXEC;
[6a3c9a7]834
[0ee077ee]835 if (aflags & AS_AREA_CACHEABLE)
[9a8d91b]836 flags |= PAGE_CACHEABLE;
837
[6a3c9a7]838 return flags;
839}
[ef67bab]840
[df0103f7]841/** Compute flags for virtual address translation subsytem.
842 *
843 * The address space area must be locked.
844 * Interrupts must be disabled.
845 *
846 * @param a Address space area.
847 *
848 * @return Flags to be used in page_mapping_insert().
849 */
[8182031]850int as_area_get_flags(as_area_t *a)
[df0103f7]851{
852 return area_flags_to_page_flags(a->flags);
853}
854
[ef67bab]855/** Create page table.
856 *
857 * Depending on architecture, create either address space
858 * private or global page table.
859 *
860 * @param flags Flags saying whether the page table is for kernel address space.
861 *
862 * @return First entry of the page table.
863 */
864pte_t *page_table_create(int flags)
865{
866 ASSERT(as_operations);
867 ASSERT(as_operations->page_table_create);
868
869 return as_operations->page_table_create(flags);
870}
[d3e7ff4]871
[482826d]872/** Destroy page table.
873 *
874 * Destroy page table in architecture specific way.
875 *
876 * @param page_table Physical address of PTL0.
877 */
878void page_table_destroy(pte_t *page_table)
879{
880 ASSERT(as_operations);
881 ASSERT(as_operations->page_table_destroy);
882
883 as_operations->page_table_destroy(page_table);
884}
885
[2299914]886/** Lock page table.
887 *
888 * This function should be called before any page_mapping_insert(),
889 * page_mapping_remove() and page_mapping_find().
890 *
891 * Locking order is such that address space areas must be locked
892 * prior to this call. Address space can be locked prior to this
893 * call in which case the lock argument is false.
894 *
895 * @param as Address space.
[9179d0a]896 * @param lock If false, do not attempt to lock as->lock.
[2299914]897 */
898void page_table_lock(as_t *as, bool lock)
899{
900 ASSERT(as_operations);
901 ASSERT(as_operations->page_table_lock);
902
903 as_operations->page_table_lock(as, lock);
904}
905
906/** Unlock page table.
907 *
908 * @param as Address space.
[9179d0a]909 * @param unlock If false, do not attempt to unlock as->lock.
[2299914]910 */
911void page_table_unlock(as_t *as, bool unlock)
912{
913 ASSERT(as_operations);
914 ASSERT(as_operations->page_table_unlock);
915
916 as_operations->page_table_unlock(as, unlock);
917}
918
[d3e7ff4]919
920/** Find address space area and lock it.
921 *
922 * The address space must be locked and interrupts must be disabled.
923 *
924 * @param as Address space.
925 * @param va Virtual address.
926 *
927 * @return Locked address space area containing va on success or NULL on failure.
928 */
929as_area_t *find_area_and_lock(as_t *as, __address va)
930{
931 as_area_t *a;
[252127e]932 btree_node_t *leaf, *lnode;
933 int i;
934
935 a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
936 if (a) {
937 /* va is the base address of an address space area */
[1068f6a]938 mutex_lock(&a->lock);
[252127e]939 return a;
940 }
[d3e7ff4]941
[252127e]942 /*
[c47912f]943 * Search the leaf node and the righmost record of its left neighbour
[252127e]944 * to find out whether this is a miss or va belongs to an address
945 * space area found there.
946 */
947
948 /* First, search the leaf node itself. */
949 for (i = 0; i < leaf->keys; i++) {
950 a = (as_area_t *) leaf->value[i];
[1068f6a]951 mutex_lock(&a->lock);
[252127e]952 if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) {
953 return a;
954 }
[1068f6a]955 mutex_unlock(&a->lock);
[252127e]956 }
[d3e7ff4]957
[252127e]958 /*
[c47912f]959 * Second, locate the left neighbour and test its last record.
[b26db0c]960 * Because of its position in the B+tree, it must have base < va.
[252127e]961 */
[c47912f]962 if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
[252127e]963 a = (as_area_t *) lnode->value[lnode->keys - 1];
[1068f6a]964 mutex_lock(&a->lock);
[252127e]965 if (va < a->base + a->pages * PAGE_SIZE) {
[37e7d2b9]966 return a;
[252127e]967 }
[1068f6a]968 mutex_unlock(&a->lock);
[d3e7ff4]969 }
970
971 return NULL;
972}
[37e7d2b9]973
974/** Check area conflicts with other areas.
975 *
976 * The address space must be locked and interrupts must be disabled.
977 *
978 * @param as Address space.
979 * @param va Starting virtual address of the area being tested.
980 * @param size Size of the area being tested.
981 * @param avoid_area Do not touch this area.
982 *
983 * @return True if there is no conflict, false otherwise.
984 */
985bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area)
986{
987 as_area_t *a;
[252127e]988 btree_node_t *leaf, *node;
989 int i;
[37e7d2b9]990
[5a7d9d1]991 /*
992 * We don't want any area to have conflicts with NULL page.
993 */
994 if (overlaps(va, size, NULL, PAGE_SIZE))
995 return false;
996
[252127e]997 /*
998 * The leaf node is found in O(log n), where n is proportional to
999 * the number of address space areas belonging to as.
1000 * The check for conflicts is then attempted on the rightmost
[c47912f]1001 * record in the left neighbour, the leftmost record in the right
1002 * neighbour and all records in the leaf node itself.
[252127e]1003 */
1004
1005 if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) {
1006 if (a != avoid_area)
1007 return false;
1008 }
1009
1010 /* First, check the two border cases. */
[c47912f]1011 if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
[252127e]1012 a = (as_area_t *) node->value[node->keys - 1];
[1068f6a]1013 mutex_lock(&a->lock);
[252127e]1014 if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
[1068f6a]1015 mutex_unlock(&a->lock);
[252127e]1016 return false;
1017 }
[1068f6a]1018 mutex_unlock(&a->lock);
[252127e]1019 }
[c47912f]1020 if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) {
[252127e]1021 a = (as_area_t *) node->value[0];
[1068f6a]1022 mutex_lock(&a->lock);
[252127e]1023 if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
[1068f6a]1024 mutex_unlock(&a->lock);
[252127e]1025 return false;
1026 }
[1068f6a]1027 mutex_unlock(&a->lock);
[252127e]1028 }
1029
1030 /* Second, check the leaf node. */
1031 for (i = 0; i < leaf->keys; i++) {
1032 a = (as_area_t *) leaf->value[i];
[37e7d2b9]1033
1034 if (a == avoid_area)
1035 continue;
[252127e]1036
[1068f6a]1037 mutex_lock(&a->lock);
[252127e]1038 if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
[1068f6a]1039 mutex_unlock(&a->lock);
[252127e]1040 return false;
1041 }
[1068f6a]1042 mutex_unlock(&a->lock);
[5a7d9d1]1043 }
[37e7d2b9]1044
[5a7d9d1]1045 /*
1046 * So far, the area does not conflict with other areas.
1047 * Check if it doesn't conflict with kernel address space.
1048 */
1049 if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
1050 return !overlaps(va, size,
1051 KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START);
[37e7d2b9]1052 }
1053
1054 return true;
1055}
[df0103f7]1056
[1068f6a]1057/** Return size of the address space area with given base. */
[7c23af9]1058size_t as_get_size(__address base)
1059{
1060 ipl_t ipl;
1061 as_area_t *src_area;
1062 size_t size;
1063
1064 ipl = interrupts_disable();
1065 src_area = find_area_and_lock(AS, base);
1066 if (src_area){
1067 size = src_area->pages * PAGE_SIZE;
[1068f6a]1068 mutex_unlock(&src_area->lock);
[7c23af9]1069 } else {
1070 size = 0;
1071 }
1072 interrupts_restore(ipl);
1073 return size;
1074}
1075
[25bf215]1076/** Mark portion of address space area as used.
1077 *
1078 * The address space area must be already locked.
1079 *
1080 * @param a Address space area.
1081 * @param page First page to be marked.
1082 * @param count Number of page to be marked.
1083 *
1084 * @return 0 on failure and 1 on success.
1085 */
1086int used_space_insert(as_area_t *a, __address page, count_t count)
1087{
1088 btree_node_t *leaf, *node;
1089 count_t pages;
1090 int i;
1091
1092 ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE));
1093 ASSERT(count);
1094
1095 pages = (count_t) btree_search(&a->used_space, page, &leaf);
1096 if (pages) {
1097 /*
1098 * We hit the beginning of some used space.
1099 */
1100 return 0;
1101 }
1102
[a6cb8cb]1103 if (!leaf->keys) {
1104 btree_insert(&a->used_space, page, (void *) count, leaf);
1105 return 1;
1106 }
1107
[25bf215]1108 node = btree_leaf_node_left_neighbour(&a->used_space, leaf);
1109 if (node) {
1110 __address left_pg = node->key[node->keys - 1], right_pg = leaf->key[0];
1111 count_t left_cnt = (count_t) node->value[node->keys - 1], right_cnt = (count_t) leaf->value[0];
1112
1113 /*
1114 * Examine the possibility that the interval fits
1115 * somewhere between the rightmost interval of
1116 * the left neigbour and the first interval of the leaf.
1117 */
1118
1119 if (page >= right_pg) {
1120 /* Do nothing. */
1121 } else if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
1122 /* The interval intersects with the left interval. */
1123 return 0;
1124 } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
1125 /* The interval intersects with the right interval. */
1126 return 0;
1127 } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
1128 /* The interval can be added by merging the two already present intervals. */
[56789125]1129 node->value[node->keys - 1] += count + right_cnt;
[25bf215]1130 btree_remove(&a->used_space, right_pg, leaf);
1131 return 1;
1132 } else if (page == left_pg + left_cnt*PAGE_SIZE) {
1133 /* The interval can be added by simply growing the left interval. */
[56789125]1134 node->value[node->keys - 1] += count;
[25bf215]1135 return 1;
1136 } else if (page + count*PAGE_SIZE == right_pg) {
1137 /*
1138 * The interval can be addded by simply moving base of the right
1139 * interval down and increasing its size accordingly.
1140 */
[56789125]1141 leaf->value[0] += count;
[25bf215]1142 leaf->key[0] = page;
1143 return 1;
1144 } else {
1145 /*
1146 * The interval is between both neigbouring intervals,
1147 * but cannot be merged with any of them.
1148 */
1149 btree_insert(&a->used_space, page, (void *) count, leaf);
1150 return 1;
1151 }
1152 } else if (page < leaf->key[0]) {
1153 __address right_pg = leaf->key[0];
1154 count_t right_cnt = (count_t) leaf->value[0];
1155
1156 /*
1157 * Investigate the border case in which the left neighbour does not
1158 * exist but the interval fits from the left.
1159 */
1160
1161 if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
1162 /* The interval intersects with the right interval. */
1163 return 0;
1164 } else if (page + count*PAGE_SIZE == right_pg) {
1165 /*
1166 * The interval can be added by moving the base of the right interval down
1167 * and increasing its size accordingly.
1168 */
1169 leaf->key[0] = page;
[56789125]1170 leaf->value[0] += count;
[25bf215]1171 return 1;
1172 } else {
1173 /*
1174 * The interval doesn't adjoin with the right interval.
1175 * It must be added individually.
1176 */
1177 btree_insert(&a->used_space, page, (void *) count, leaf);
1178 return 1;
1179 }
1180 }
1181
1182 node = btree_leaf_node_right_neighbour(&a->used_space, leaf);
1183 if (node) {
1184 __address left_pg = leaf->key[leaf->keys - 1], right_pg = node->key[0];
1185 count_t left_cnt = (count_t) leaf->value[leaf->keys - 1], right_cnt = (count_t) node->value[0];
1186
1187 /*
1188 * Examine the possibility that the interval fits
1189 * somewhere between the leftmost interval of
1190 * the right neigbour and the last interval of the leaf.
1191 */
1192
1193 if (page < left_pg) {
1194 /* Do nothing. */
1195 } else if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
1196 /* The interval intersects with the left interval. */
1197 return 0;
1198 } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
1199 /* The interval intersects with the right interval. */
1200 return 0;
1201 } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
1202 /* The interval can be added by merging the two already present intervals. */
[56789125]1203 leaf->value[leaf->keys - 1] += count + right_cnt;
[25bf215]1204 btree_remove(&a->used_space, right_pg, node);
1205 return 1;
1206 } else if (page == left_pg + left_cnt*PAGE_SIZE) {
1207 /* The interval can be added by simply growing the left interval. */
[56789125]1208 leaf->value[leaf->keys - 1] += count;
[25bf215]1209 return 1;
1210 } else if (page + count*PAGE_SIZE == right_pg) {
1211 /*
1212 * The interval can be addded by simply moving base of the right
1213 * interval down and increasing its size accordingly.
1214 */
[56789125]1215 node->value[0] += count;
[25bf215]1216 node->key[0] = page;
1217 return 1;
1218 } else {
1219 /*
1220 * The interval is between both neigbouring intervals,
1221 * but cannot be merged with any of them.
1222 */
1223 btree_insert(&a->used_space, page, (void *) count, leaf);
1224 return 1;
1225 }
1226 } else if (page >= leaf->key[leaf->keys - 1]) {
1227 __address left_pg = leaf->key[leaf->keys - 1];
1228 count_t left_cnt = (count_t) leaf->value[leaf->keys - 1];
1229
1230 /*
1231 * Investigate the border case in which the right neighbour does not
1232 * exist but the interval fits from the right.
1233 */
1234
1235 if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
[56789125]1236 /* The interval intersects with the left interval. */
[25bf215]1237 return 0;
1238 } else if (left_pg + left_cnt*PAGE_SIZE == page) {
1239 /* The interval can be added by growing the left interval. */
[56789125]1240 leaf->value[leaf->keys - 1] += count;
[25bf215]1241 return 1;
1242 } else {
1243 /*
1244 * The interval doesn't adjoin with the left interval.
1245 * It must be added individually.
1246 */
1247 btree_insert(&a->used_space, page, (void *) count, leaf);
1248 return 1;
1249 }
1250 }
1251
1252 /*
1253 * Note that if the algorithm made it thus far, the interval can fit only
1254 * between two other intervals of the leaf. The two border cases were already
1255 * resolved.
1256 */
1257 for (i = 1; i < leaf->keys; i++) {
1258 if (page < leaf->key[i]) {
1259 __address left_pg = leaf->key[i - 1], right_pg = leaf->key[i];
1260 count_t left_cnt = (count_t) leaf->value[i - 1], right_cnt = (count_t) leaf->value[i];
1261
1262 /*
1263 * The interval fits between left_pg and right_pg.
1264 */
1265
1266 if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
1267 /* The interval intersects with the left interval. */
1268 return 0;
1269 } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
1270 /* The interval intersects with the right interval. */
1271 return 0;
1272 } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
1273 /* The interval can be added by merging the two already present intervals. */
[56789125]1274 leaf->value[i - 1] += count + right_cnt;
[25bf215]1275 btree_remove(&a->used_space, right_pg, leaf);
1276 return 1;
1277 } else if (page == left_pg + left_cnt*PAGE_SIZE) {
1278 /* The interval can be added by simply growing the left interval. */
[56789125]1279 leaf->value[i - 1] += count;
[25bf215]1280 return 1;
1281 } else if (page + count*PAGE_SIZE == right_pg) {
1282 /*
1283 * The interval can be addded by simply moving base of the right
1284 * interval down and increasing its size accordingly.
1285 */
[56789125]1286 leaf->value[i] += count;
[25bf215]1287 leaf->key[i] = page;
1288 return 1;
1289 } else {
1290 /*
1291 * The interval is between both neigbouring intervals,
1292 * but cannot be merged with any of them.
1293 */
1294 btree_insert(&a->used_space, page, (void *) count, leaf);
1295 return 1;
1296 }
1297 }
1298 }
1299
1300 panic("Inconsistency detected while adding %d pages of used space at %P.\n", count, page);
1301}
1302
1303/** Mark portion of address space area as unused.
1304 *
1305 * The address space area must be already locked.
1306 *
1307 * @param a Address space area.
1308 * @param page First page to be marked.
1309 * @param count Number of page to be marked.
1310 *
1311 * @return 0 on failure and 1 on success.
1312 */
1313int used_space_remove(as_area_t *a, __address page, count_t count)
1314{
1315 btree_node_t *leaf, *node;
1316 count_t pages;
1317 int i;
1318
1319 ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE));
1320 ASSERT(count);
1321
1322 pages = (count_t) btree_search(&a->used_space, page, &leaf);
1323 if (pages) {
1324 /*
1325 * We are lucky, page is the beginning of some interval.
1326 */
1327 if (count > pages) {
1328 return 0;
1329 } else if (count == pages) {
1330 btree_remove(&a->used_space, page, leaf);
[56789125]1331 return 1;
[25bf215]1332 } else {
1333 /*
1334 * Find the respective interval.
1335 * Decrease its size and relocate its start address.
1336 */
1337 for (i = 0; i < leaf->keys; i++) {
1338 if (leaf->key[i] == page) {
1339 leaf->key[i] += count*PAGE_SIZE;
[56789125]1340 leaf->value[i] -= count;
[25bf215]1341 return 1;
1342 }
1343 }
1344 goto error;
1345 }
1346 }
1347
1348 node = btree_leaf_node_left_neighbour(&a->used_space, leaf);
1349 if (node && page < leaf->key[0]) {
1350 __address left_pg = node->key[node->keys - 1];
1351 count_t left_cnt = (count_t) node->value[node->keys - 1];
1352
1353 if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
1354 if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
1355 /*
1356 * The interval is contained in the rightmost interval
1357 * of the left neighbour and can be removed by
1358 * updating the size of the bigger interval.
1359 */
[56789125]1360 node->value[node->keys - 1] -= count;
[25bf215]1361 return 1;
1362 } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
[56789125]1363 count_t new_cnt;
[25bf215]1364
1365 /*
1366 * The interval is contained in the rightmost interval
1367 * of the left neighbour but its removal requires
1368 * both updating the size of the original interval and
1369 * also inserting a new interval.
1370 */
[56789125]1371 new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
1372 node->value[node->keys - 1] -= count + new_cnt;
[25bf215]1373 btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
1374 return 1;
1375 }
1376 }
1377 return 0;
1378 } else if (page < leaf->key[0]) {
1379 return 0;
1380 }
1381
1382 if (page > leaf->key[leaf->keys - 1]) {
1383 __address left_pg = leaf->key[leaf->keys - 1];
1384 count_t left_cnt = (count_t) leaf->value[leaf->keys - 1];
1385
1386 if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
1387 if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
1388 /*
1389 * The interval is contained in the rightmost interval
1390 * of the leaf and can be removed by updating the size
1391 * of the bigger interval.
1392 */
[56789125]1393 leaf->value[leaf->keys - 1] -= count;
[25bf215]1394 return 1;
1395 } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
[56789125]1396 count_t new_cnt;
[25bf215]1397
1398 /*
1399 * The interval is contained in the rightmost interval
1400 * of the leaf but its removal requires both updating
1401 * the size of the original interval and
1402 * also inserting a new interval.
1403 */
[56789125]1404 new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
1405 leaf->value[leaf->keys - 1] -= count + new_cnt;
[25bf215]1406 btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
1407 return 1;
1408 }
1409 }
1410 return 0;
1411 }
1412
1413 /*
1414 * The border cases have been already resolved.
1415 * Now the interval can be only between intervals of the leaf.
1416 */
1417 for (i = 1; i < leaf->keys - 1; i++) {
1418 if (page < leaf->key[i]) {
1419 __address left_pg = leaf->key[i - 1];
1420 count_t left_cnt = (count_t) leaf->value[i - 1];
1421
1422 /*
1423 * Now the interval is between intervals corresponding to (i - 1) and i.
1424 */
1425 if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
1426 if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
1427 /*
1428 * The interval is contained in the interval (i - 1)
1429 * of the leaf and can be removed by updating the size
1430 * of the bigger interval.
1431 */
[56789125]1432 leaf->value[i - 1] -= count;
[25bf215]1433 return 1;
1434 } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
[56789125]1435 count_t new_cnt;
[25bf215]1436
1437 /*
1438 * The interval is contained in the interval (i - 1)
1439 * of the leaf but its removal requires both updating
1440 * the size of the original interval and
1441 * also inserting a new interval.
1442 */
[56789125]1443 new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
1444 leaf->value[i - 1] -= count + new_cnt;
[25bf215]1445 btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
1446 return 1;
1447 }
1448 }
1449 return 0;
1450 }
1451 }
1452
1453error:
1454 panic("Inconsistency detected while removing %d pages of used space from %P.\n", count, page);
1455}
1456
[8182031]1457/** Remove reference to address space area share info.
1458 *
1459 * If the reference count drops to 0, the sh_info is deallocated.
1460 *
1461 * @param sh_info Pointer to address space area share info.
1462 */
1463void sh_info_remove_reference(share_info_t *sh_info)
1464{
1465 bool dealloc = false;
1466
1467 mutex_lock(&sh_info->lock);
1468 ASSERT(sh_info->refcount);
1469 if (--sh_info->refcount == 0) {
1470 dealloc = true;
[f8d069e8]1471 link_t *cur;
[8182031]1472
1473 /*
1474 * Now walk carefully the pagemap B+tree and free/remove
1475 * reference from all frames found there.
1476 */
[f8d069e8]1477 for (cur = sh_info->pagemap.leaf_head.next; cur != &sh_info->pagemap.leaf_head; cur = cur->next) {
[8182031]1478 btree_node_t *node;
[f8d069e8]1479 int i;
[8182031]1480
[f8d069e8]1481 node = list_get_instance(cur, btree_node_t, leaf_link);
1482 for (i = 0; i < node->keys; i++)
1483 frame_free(ADDR2PFN((__address) node->value[i]));
[8182031]1484 }
1485
1486 }
1487 mutex_unlock(&sh_info->lock);
1488
1489 if (dealloc) {
1490 btree_destroy(&sh_info->pagemap);
1491 free(sh_info);
1492 }
1493}
1494
[df0103f7]1495/*
1496 * Address space related syscalls.
1497 */
1498
1499/** Wrapper for as_area_create(). */
1500__native sys_as_area_create(__address address, size_t size, int flags)
1501{
[0ee077ee]1502 if (as_area_create(AS, flags | AS_AREA_CACHEABLE, size, address, AS_AREA_ATTR_NONE, &anon_backend, NULL))
[df0103f7]1503 return (__native) address;
1504 else
1505 return (__native) -1;
1506}
1507
1508/** Wrapper for as_area_resize. */
1509__native sys_as_area_resize(__address address, size_t size, int flags)
1510{
[7242a78e]1511 return (__native) as_area_resize(AS, address, size, 0);
1512}
1513
1514/** Wrapper for as_area_destroy. */
1515__native sys_as_area_destroy(__address address)
1516{
1517 return (__native) as_area_destroy(AS, address);
[df0103f7]1518}
Note: See TracBrowser for help on using the repository browser.