Subversion Repositories HelenOS

Rev

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

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