Subversion Repositories HelenOS

Rev

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

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