Subversion Repositories HelenOS-historic

Rev

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

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