Subversion Repositories HelenOS

Rev

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

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