Subversion Repositories HelenOS-historic

Rev

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

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