Subversion Repositories HelenOS

Rev

Rev 2745 | Rev 3104 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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