Subversion Repositories HelenOS

Rev

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

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