Subversion Repositories HelenOS

Rev

Rev 2126 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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