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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d6e5cbc was d6e5cbc, checked in by Ondrej Palkovsky <ondrap@…>, 20 years ago

Added 'realtime' clock interface.
Added some asm macros as memory barriers.
Added drift computing for mips platform.

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