Subversion Repositories HelenOS-historic

Rev

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

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