Subversion Repositories HelenOS-historic

Rev

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

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