Subversion Repositories HelenOS-historic

Rev

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

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