Subversion Repositories HelenOS-historic

Rev

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

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