Subversion Repositories HelenOS-historic

Rev

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

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