Subversion Repositories HelenOS-historic

Rev

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

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