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