Subversion Repositories HelenOS-historic

Rev

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

Rev 1238 Rev 1239
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
 * This file contains address space manipulation functions.
30
 * This file contains address space manipulation functions.
31
 * Roughly speaking, this is a higher-level client of
31
 * Roughly speaking, this is a higher-level client of
32
 * Virtual Address Translation (VAT) subsystem.
32
 * Virtual Address Translation (VAT) subsystem.
33
 */
33
 */
34
 
34
 
35
#include <mm/as.h>
35
#include <mm/as.h>
36
#include <arch/mm/as.h>
36
#include <arch/mm/as.h>
37
#include <mm/page.h>
37
#include <mm/page.h>
38
#include <mm/frame.h>
38
#include <mm/frame.h>
39
#include <mm/slab.h>
39
#include <mm/slab.h>
40
#include <mm/tlb.h>
40
#include <mm/tlb.h>
41
#include <arch/mm/page.h>
41
#include <arch/mm/page.h>
42
#include <genarch/mm/page_pt.h>
42
#include <genarch/mm/page_pt.h>
43
#include <genarch/mm/page_ht.h>
43
#include <genarch/mm/page_ht.h>
44
#include <mm/asid.h>
44
#include <mm/asid.h>
45
#include <arch/mm/asid.h>
45
#include <arch/mm/asid.h>
46
#include <synch/spinlock.h>
46
#include <synch/spinlock.h>
47
#include <adt/list.h>
47
#include <adt/list.h>
48
#include <adt/btree.h>
48
#include <adt/btree.h>
49
#include <proc/task.h>
49
#include <proc/task.h>
50
#include <arch/asm.h>
50
#include <arch/asm.h>
51
#include <panic.h>
51
#include <panic.h>
52
#include <debug.h>
52
#include <debug.h>
53
#include <print.h>
53
#include <print.h>
54
#include <memstr.h>
54
#include <memstr.h>
55
#include <macros.h>
55
#include <macros.h>
56
#include <arch.h>
56
#include <arch.h>
57
#include <errno.h>
57
#include <errno.h>
58
#include <config.h>
58
#include <config.h>
59
#include <arch/types.h>
59
#include <arch/types.h>
60
#include <typedefs.h>
60
#include <typedefs.h>
61
 
61
 
62
as_operations_t *as_operations = NULL;
62
as_operations_t *as_operations = NULL;
63
 
63
 
64
/** Address space lock. It protects inactive_as_with_asid_head. */
64
/** Address space lock. It protects inactive_as_with_asid_head. */
65
SPINLOCK_INITIALIZE(as_lock);
65
SPINLOCK_INITIALIZE(as_lock);
66
 
66
 
67
/**
67
/**
68
 * This list contains address spaces that are not active on any
68
 * This list contains address spaces that are not active on any
69
 * processor and that have valid ASID.
69
 * processor and that have valid ASID.
70
 */
70
 */
71
LIST_INITIALIZE(inactive_as_with_asid_head);
71
LIST_INITIALIZE(inactive_as_with_asid_head);
72
 
72
 
73
/** Kernel address space. */
73
/** Kernel address space. */
74
as_t *AS_KERNEL = NULL;
74
as_t *AS_KERNEL = NULL;
75
 
75
 
76
static int area_flags_to_page_flags(int aflags);
76
static int area_flags_to_page_flags(int aflags);
77
static int get_area_flags(as_area_t *a);
77
static int get_area_flags(as_area_t *a);
78
static as_area_t *find_area_and_lock(as_t *as, __address va);
78
static as_area_t *find_area_and_lock(as_t *as, __address va);
79
static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area);
79
static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area);
80
 
80
 
81
/** Initialize address space subsystem. */
81
/** Initialize address space subsystem. */
82
void as_init(void)
82
void as_init(void)
83
{
83
{
84
    as_arch_init();
84
    as_arch_init();
85
    AS_KERNEL = as_create(FLAG_AS_KERNEL);
85
    AS_KERNEL = as_create(FLAG_AS_KERNEL);
86
        if (!AS_KERNEL)
86
        if (!AS_KERNEL)
87
                panic("can't create kernel address space\n");
87
                panic("can't create kernel address space\n");
88
}
88
}
89
 
89
 
90
/** Create address space.
90
/** Create address space.
91
 *
91
 *
92
 * @param flags Flags that influence way in wich the address space is created.
92
 * @param flags Flags that influence way in wich the address space is created.
93
 */
93
 */
94
as_t *as_create(int flags)
94
as_t *as_create(int flags)
95
{
95
{
96
    as_t *as;
96
    as_t *as;
97
 
97
 
98
    as = (as_t *) malloc(sizeof(as_t), 0);
98
    as = (as_t *) malloc(sizeof(as_t), 0);
99
    link_initialize(&as->inactive_as_with_asid_link);
99
    link_initialize(&as->inactive_as_with_asid_link);
100
    spinlock_initialize(&as->lock, "as_lock");
100
    spinlock_initialize(&as->lock, "as_lock");
101
    btree_create(&as->as_area_btree);
101
    btree_create(&as->as_area_btree);
102
   
102
   
103
    if (flags & FLAG_AS_KERNEL)
103
    if (flags & FLAG_AS_KERNEL)
104
        as->asid = ASID_KERNEL;
104
        as->asid = ASID_KERNEL;
105
    else
105
    else
106
        as->asid = ASID_INVALID;
106
        as->asid = ASID_INVALID;
107
   
107
   
108
    as->refcount = 0;
108
    as->refcount = 0;
109
    as->page_table = page_table_create(flags);
109
    as->page_table = page_table_create(flags);
110
 
110
 
111
    return as;
111
    return as;
112
}
112
}
113
 
113
 
114
/** Free Adress space */
114
/** Free Adress space */
115
void as_free(as_t *as)
115
void as_free(as_t *as)
116
{
116
{
117
    ASSERT(as->refcount == 0);
117
    ASSERT(as->refcount == 0);
118
 
118
 
119
    /* TODO: free as_areas and other resources held by as */
119
    /* TODO: free as_areas and other resources held by as */
120
    /* TODO: free page table */
120
    /* TODO: free page table */
121
    free(as);
121
    free(as);
122
}
122
}
123
 
123
 
124
/** Create address space area of common attributes.
124
/** Create address space area of common attributes.
125
 *
125
 *
126
 * The created address space area is added to the target address space.
126
 * The created address space area is added to the target address space.
127
 *
127
 *
128
 * @param as Target address space.
128
 * @param as Target address space.
129
 * @param flags Flags of the area.
129
 * @param flags Flags of the area memory.
130
 * @param size Size of area.
130
 * @param size Size of area.
131
 * @param base Base address of area.
131
 * @param base Base address of area.
-
 
132
 * @param attrs Attributes of the area.
132
 *
133
 *
133
 * @return Address space area on success or NULL on failure.
134
 * @return Address space area on success or NULL on failure.
134
 */
135
 */
135
as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base)
136
as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base, int attrs)
136
{
137
{
137
    ipl_t ipl;
138
    ipl_t ipl;
138
    as_area_t *a;
139
    as_area_t *a;
139
   
140
   
140
    if (base % PAGE_SIZE)
141
    if (base % PAGE_SIZE)
141
        return NULL;
142
        return NULL;
142
 
143
 
143
    if (!size)
144
    if (!size)
144
        return NULL;
145
        return NULL;
145
 
146
 
146
    /* Writeable executable areas are not supported. */
147
    /* Writeable executable areas are not supported. */
147
    if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
148
    if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
148
        return NULL;
149
        return NULL;
149
   
150
   
150
    ipl = interrupts_disable();
151
    ipl = interrupts_disable();
151
    spinlock_lock(&as->lock);
152
    spinlock_lock(&as->lock);
152
   
153
   
153
    if (!check_area_conflicts(as, base, size, NULL)) {
154
    if (!check_area_conflicts(as, base, size, NULL)) {
154
        spinlock_unlock(&as->lock);
155
        spinlock_unlock(&as->lock);
155
        interrupts_restore(ipl);
156
        interrupts_restore(ipl);
156
        return NULL;
157
        return NULL;
157
    }
158
    }
158
   
159
   
159
    a = (as_area_t *) malloc(sizeof(as_area_t), 0);
160
    a = (as_area_t *) malloc(sizeof(as_area_t), 0);
160
 
161
 
161
    spinlock_initialize(&a->lock, "as_area_lock");
162
    spinlock_initialize(&a->lock, "as_area_lock");
162
   
163
   
163
    a->flags = flags;
164
    a->flags = flags;
-
 
165
    a->attributes = attrs;
164
    a->pages = SIZE2FRAMES(size);
166
    a->pages = SIZE2FRAMES(size);
165
    a->base = base;
167
    a->base = base;
166
   
168
   
167
    btree_insert(&as->as_area_btree, base, (void *) a, NULL);
169
    btree_insert(&as->as_area_btree, base, (void *) a, NULL);
168
 
170
 
169
    spinlock_unlock(&as->lock);
171
    spinlock_unlock(&as->lock);
170
    interrupts_restore(ipl);
172
    interrupts_restore(ipl);
171
 
173
 
172
    return a;
174
    return a;
173
}
175
}
174
 
176
 
175
/** Find address space area and change it.
177
/** Find address space area and change it.
176
 *
178
 *
177
 * @param as Address space.
179
 * @param as Address space.
178
 * @param address Virtual address belonging to the area to be changed. Must be page-aligned.
180
 * @param address Virtual address belonging to the area to be changed. Must be page-aligned.
179
 * @param size New size of the virtual memory block starting at address.
181
 * @param size New size of the virtual memory block starting at address.
180
 * @param flags Flags influencing the remap operation. Currently unused.
182
 * @param flags Flags influencing the remap operation. Currently unused.
181
 *
183
 *
182
 * @return address on success, (__address) -1 otherwise.
184
 * @return address on success, (__address) -1 otherwise.
183
 */
185
 */
184
__address as_area_resize(as_t *as, __address address, size_t size, int flags)
186
__address as_area_resize(as_t *as, __address address, size_t size, int flags)
185
{
187
{
186
    as_area_t *area = NULL;
188
    as_area_t *area = NULL;
187
    ipl_t ipl;
189
    ipl_t ipl;
188
    size_t pages;
190
    size_t pages;
189
   
191
   
190
    ipl = interrupts_disable();
192
    ipl = interrupts_disable();
191
    spinlock_lock(&as->lock);
193
    spinlock_lock(&as->lock);
192
   
194
   
193
    /*
195
    /*
194
     * Locate the area.
196
     * Locate the area.
195
     */
197
     */
196
    area = find_area_and_lock(as, address);
198
    area = find_area_and_lock(as, address);
197
    if (!area) {
199
    if (!area) {
198
        spinlock_unlock(&as->lock);
200
        spinlock_unlock(&as->lock);
199
        interrupts_restore(ipl);
201
        interrupts_restore(ipl);
200
        return (__address) -1;
202
        return (__address) -1;
201
    }
203
    }
202
 
204
 
203
    if (area->flags & AS_AREA_DEVICE) {
205
    if (area->flags & AS_AREA_DEVICE) {
204
        /*
206
        /*
205
         * Remapping of address space areas associated
207
         * Remapping of address space areas associated
206
         * with memory mapped devices is not supported.
208
         * with memory mapped devices is not supported.
207
         */
209
         */
208
        spinlock_unlock(&area->lock);
210
        spinlock_unlock(&area->lock);
209
        spinlock_unlock(&as->lock);
211
        spinlock_unlock(&as->lock);
210
        interrupts_restore(ipl);
212
        interrupts_restore(ipl);
211
        return (__address) -1;
213
        return (__address) -1;
212
    }
214
    }
213
 
215
 
214
    pages = SIZE2FRAMES((address - area->base) + size);
216
    pages = SIZE2FRAMES((address - area->base) + size);
215
    if (!pages) {
217
    if (!pages) {
216
        /*
218
        /*
217
         * Zero size address space areas are not allowed.
219
         * Zero size address space areas are not allowed.
218
         */
220
         */
219
        spinlock_unlock(&area->lock);
221
        spinlock_unlock(&area->lock);
220
        spinlock_unlock(&as->lock);
222
        spinlock_unlock(&as->lock);
221
        interrupts_restore(ipl);
223
        interrupts_restore(ipl);
222
        return (__address) -1;
224
        return (__address) -1;
223
    }
225
    }
224
   
226
   
225
    if (pages < area->pages) {
227
    if (pages < area->pages) {
226
        int i;
228
        int i;
227
 
229
 
228
        /*
230
        /*
229
         * Shrinking the area.
231
         * Shrinking the area.
230
         * No need to check for overlaps.
232
         * No need to check for overlaps.
231
         */
233
         */
232
        for (i = pages; i < area->pages; i++) {
234
        for (i = pages; i < area->pages; i++) {
233
            pte_t *pte;
235
            pte_t *pte;
234
           
236
           
235
            /*
237
            /*
236
             * Releasing physical memory.
238
             * Releasing physical memory.
237
             * This depends on the fact that the memory was allocated using frame_alloc().
239
             * This depends on the fact that the memory was allocated using frame_alloc().
238
             */
240
             */
239
            page_table_lock(as, false);
241
            page_table_lock(as, false);
240
            pte = page_mapping_find(as, area->base + i*PAGE_SIZE);
242
            pte = page_mapping_find(as, area->base + i*PAGE_SIZE);
241
            if (pte && PTE_VALID(pte)) {
243
            if (pte && PTE_VALID(pte)) {
242
                __address frame;
244
                __address frame;
243
 
245
 
244
                ASSERT(PTE_PRESENT(pte));
246
                ASSERT(PTE_PRESENT(pte));
245
                frame = PTE_GET_FRAME(pte);
247
                frame = PTE_GET_FRAME(pte);
246
                page_mapping_remove(as, area->base + i*PAGE_SIZE);
248
                page_mapping_remove(as, area->base + i*PAGE_SIZE);
247
                page_table_unlock(as, false);
249
                page_table_unlock(as, false);
248
 
250
 
249
                frame_free(ADDR2PFN(frame));
251
                frame_free(ADDR2PFN(frame));
250
            } else {
252
            } else {
251
                page_table_unlock(as, false);
253
                page_table_unlock(as, false);
252
            }
254
            }
253
        }
255
        }
254
        /*
256
        /*
255
         * Invalidate TLB's.
257
         * Invalidate TLB's.
256
         */
258
         */
257
        tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
259
        tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
258
        tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
260
        tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
259
        tlb_shootdown_finalize();
261
        tlb_shootdown_finalize();
260
    } else {
262
    } else {
261
        /*
263
        /*
262
         * Growing the area.
264
         * Growing the area.
263
         * Check for overlaps with other address space areas.
265
         * Check for overlaps with other address space areas.
264
         */
266
         */
265
        if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) {
267
        if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) {
266
            spinlock_unlock(&area->lock);
268
            spinlock_unlock(&area->lock);
267
            spinlock_unlock(&as->lock);    
269
            spinlock_unlock(&as->lock);    
268
            interrupts_restore(ipl);
270
            interrupts_restore(ipl);
269
            return (__address) -1;
271
            return (__address) -1;
270
        }
272
        }
271
    }
273
    }
272
 
274
 
273
    area->pages = pages;
275
    area->pages = pages;
274
   
276
   
275
    spinlock_unlock(&area->lock);
277
    spinlock_unlock(&area->lock);
276
    spinlock_unlock(&as->lock);
278
    spinlock_unlock(&as->lock);
277
    interrupts_restore(ipl);
279
    interrupts_restore(ipl);
278
 
280
 
279
    return address;
281
    return address;
280
}
282
}
281
 
283
 
282
/** Send address space area to another task.
284
/** Send address space area to another task.
283
 *
285
 *
284
 * Address space area is sent to the specified task.
286
 * Address space area is sent to the specified task.
285
 * If the destination task is willing to accept the
287
 * If the destination task is willing to accept the
286
 * area, a new area is created according to the
288
 * area, a new area is created according to the
287
 * source area. Moreover, any existing mapping
289
 * source area. Moreover, any existing mapping
288
 * is copied as well, providing thus a mechanism
290
 * is copied as well, providing thus a mechanism
289
 * for sharing group of pages. The source address
291
 * for sharing group of pages. The source address
290
 * space area and any associated mapping is preserved.
292
 * space area and any associated mapping is preserved.
291
 *
293
 *
292
 * @param id Task ID of the accepting task.
294
 * @param dst_id Task ID of the accepting task.
293
 * @param base Base address of the source address space area.
295
 * @param src_base Base address of the source address space area.
294
 *
296
 *
295
 * @return 0 on success or ENOENT if there is no such task or
297
 * @return 0 on success or ENOENT if there is no such task or
296
 *     if there is no such address space area,
298
 *     if there is no such address space area,
297
 *     EPERM if there was a problem in accepting the area or
299
 *     EPERM if there was a problem in accepting the area or
298
 *     ENOMEM if there was a problem in allocating destination
300
 *     ENOMEM if there was a problem in allocating destination
299
 *     address space area.
301
 *     address space area.
300
 */
302
 */
301
int as_area_send(task_id_t id, __address base)
303
int as_area_send(task_id_t dst_id, __address src_base)
302
{
304
{
303
    ipl_t ipl;
305
    ipl_t ipl;
304
    task_t *t;
306
    task_t *t;
305
    count_t i;
307
    count_t i;
306
    as_t *as;
308
    as_t *dst_as;
307
    __address dst_base;
309
    __address dst_base;
308
    int flags;
310
    int src_flags;
309
    size_t size;
311
    size_t src_size;
310
    as_area_t *area;
312
    as_area_t *src_area, *dst_area;
311
   
313
   
312
    ipl = interrupts_disable();
314
    ipl = interrupts_disable();
313
    spinlock_lock(&tasks_lock);
315
    spinlock_lock(&tasks_lock);
314
   
316
   
315
    t = task_find_by_id(id);
317
    t = task_find_by_id(dst_id);
316
    if (!NULL) {
318
    if (!NULL) {
317
        spinlock_unlock(&tasks_lock);
319
        spinlock_unlock(&tasks_lock);
318
        interrupts_restore(ipl);
320
        interrupts_restore(ipl);
319
        return ENOENT;
321
        return ENOENT;
320
    }
322
    }
321
 
323
 
322
    spinlock_lock(&t->lock);
324
    spinlock_lock(&t->lock);
323
    spinlock_unlock(&tasks_lock);
325
    spinlock_unlock(&tasks_lock);
324
 
326
 
325
    as = t->as;
327
    dst_as = t->as;
326
    dst_base = (__address) t->accept_arg.base;
328
    dst_base = (__address) t->accept_arg.base;
327
   
329
   
328
    if (as == AS) {
330
    if (dst_as == AS) {
329
        /*
331
        /*
330
         * The two tasks share the entire address space.
332
         * The two tasks share the entire address space.
331
         * Return error since there is no point in continuing.
333
         * Return error since there is no point in continuing.
332
         */
334
         */
333
        spinlock_unlock(&t->lock);
335
        spinlock_unlock(&t->lock);
334
        interrupts_restore(ipl);
336
        interrupts_restore(ipl);
335
        return EPERM;
337
        return EPERM;
336
    }
338
    }
337
   
339
   
338
    spinlock_lock(&AS->lock);
340
    spinlock_lock(&AS->lock);
339
    area = find_area_and_lock(AS, base);
341
    src_area = find_area_and_lock(AS, src_base);
340
    if (!area) {
342
    if (!src_area) {
341
        /*
343
        /*
342
         * Could not find the source address space area.
344
         * Could not find the source address space area.
343
         */
345
         */
344
        spinlock_unlock(&t->lock);
346
        spinlock_unlock(&t->lock);
345
        spinlock_unlock(&AS->lock);
347
        spinlock_unlock(&AS->lock);
346
        interrupts_restore(ipl);
348
        interrupts_restore(ipl);
347
        return ENOENT;
349
        return ENOENT;
348
    }
350
    }
349
    size = area->pages * PAGE_SIZE;
351
    src_size = src_area->pages * PAGE_SIZE;
350
    flags = area->flags;
352
    src_flags = src_area->flags;
351
    spinlock_unlock(&area->lock);
353
    spinlock_unlock(&src_area->lock);
352
    spinlock_unlock(&AS->lock);
354
    spinlock_unlock(&AS->lock);
353
 
355
 
354
    if ((t->accept_arg.task_id != TASK->taskid) || (t->accept_arg.size != size) ||
356
    if ((t->accept_arg.task_id != TASK->taskid) || (t->accept_arg.size != src_size) ||
355
        (t->accept_arg.flags != flags)) {
357
        (t->accept_arg.flags != src_flags)) {
356
        /*
358
        /*
357
         * Discrepancy in either task ID, size or flags.
359
         * Discrepancy in either task ID, size or flags.
358
         */
360
         */
359
        spinlock_unlock(&t->lock);
361
        spinlock_unlock(&t->lock);
360
        interrupts_restore(ipl);
362
        interrupts_restore(ipl);
361
        return EPERM;
363
        return EPERM;
362
    }
364
    }
363
   
365
   
364
    /*
366
    /*
365
     * Create copy of the address space area.
367
     * Create copy of the source address space area.
-
 
368
     * The destination area is created with AS_AREA_ATTR_PARTIAL
-
 
369
     * attribute set which prevents race condition with
-
 
370
     * preliminary as_page_fault() calls.
366
     */
371
     */
367
    if (!as_area_create(as, flags, size, dst_base)) {
372
    dst_area = as_area_create(dst_as, src_flags, src_size, dst_base, AS_AREA_ATTR_PARTIAL);
-
 
373
    if (!dst_area) {
368
        /*
374
        /*
369
         * Destination address space area could not be created.
375
         * Destination address space area could not be created.
370
         */
376
         */
371
        spinlock_unlock(&t->lock);
377
        spinlock_unlock(&t->lock);
372
        interrupts_restore(ipl);
378
        interrupts_restore(ipl);
373
        return ENOMEM;
379
        return ENOMEM;
374
    }
380
    }
375
   
381
   
376
    /*
-
 
377
     * NOTE: we have just introduced a race condition.
-
 
378
     * The destination task can try to attempt the newly
-
 
379
     * created area before its mapping is copied from
-
 
380
     * the source address space area. In result, frames
-
 
381
     * can get lost.
-
 
382
     *
-
 
383
     * Currently, this race is not solved, but one of the
-
 
384
     * possible solutions would be to sleep in as_page_fault()
-
 
385
     * when this situation is detected.
-
 
386
     */
-
 
387
 
-
 
388
    memsetb((__address) &t->accept_arg, sizeof(as_area_acptsnd_arg_t), 0);
382
    memsetb((__address) &t->accept_arg, sizeof(as_area_acptsnd_arg_t), 0);
389
    spinlock_unlock(&t->lock);
383
    spinlock_unlock(&t->lock);
390
   
384
   
391
    /*
385
    /*
392
     * Avoid deadlock by first locking the address space with lower address.
386
     * Avoid deadlock by first locking the address space with lower address.
393
     */
387
     */
394
    if (as < AS) {
388
    if (dst_as < AS) {
395
        spinlock_lock(&as->lock);
389
        spinlock_lock(&dst_as->lock);
396
        spinlock_lock(&AS->lock);
390
        spinlock_lock(&AS->lock);
397
    } else {
391
    } else {
398
        spinlock_lock(&AS->lock);
392
        spinlock_lock(&AS->lock);
399
        spinlock_lock(&as->lock);
393
        spinlock_lock(&dst_as->lock);
400
    }
394
    }
401
   
395
   
402
    for (i = 0; i < SIZE2FRAMES(size); i++) {
396
    for (i = 0; i < SIZE2FRAMES(src_size); i++) {
403
        pte_t *pte;
397
        pte_t *pte;
404
        __address frame;
398
        __address frame;
405
           
399
           
406
        page_table_lock(AS, false);
400
        page_table_lock(AS, false);
407
        pte = page_mapping_find(AS, base + i*PAGE_SIZE);
401
        pte = page_mapping_find(AS, src_base + i*PAGE_SIZE);
408
        if (pte && PTE_VALID(pte)) {
402
        if (pte && PTE_VALID(pte)) {
409
            ASSERT(PTE_PRESENT(pte));
403
            ASSERT(PTE_PRESENT(pte));
410
            frame = PTE_GET_FRAME(pte);
404
            frame = PTE_GET_FRAME(pte);
411
            if (!(flags & AS_AREA_DEVICE))
405
            if (!(src_flags & AS_AREA_DEVICE))
412
                frame_reference_add(ADDR2PFN(frame));
406
                frame_reference_add(ADDR2PFN(frame));
413
            page_table_unlock(AS, false);
407
            page_table_unlock(AS, false);
414
        } else {
408
        } else {
415
            page_table_unlock(AS, false);
409
            page_table_unlock(AS, false);
416
            continue;
410
            continue;
417
        }
411
        }
418
       
412
       
419
        page_table_lock(as, false);
413
        page_table_lock(dst_as, false);
420
        page_mapping_insert(as, dst_base + i*PAGE_SIZE, frame, area_flags_to_page_flags(flags));
414
        page_mapping_insert(dst_as, dst_base + i*PAGE_SIZE, frame, area_flags_to_page_flags(src_flags));
421
        page_table_unlock(as, false);
415
        page_table_unlock(dst_as, false);
422
    }
416
    }
-
 
417
 
-
 
418
    /*
-
 
419
     * Now the destination address space area has been
-
 
420
     * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
-
 
421
     * attribute.
-
 
422
     */
-
 
423
    spinlock_lock(&dst_area->lock);
-
 
424
    dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
-
 
425
    spinlock_unlock(&dst_area->lock);
423
   
426
   
424
    spinlock_unlock(&AS->lock);
427
    spinlock_unlock(&AS->lock);
425
    spinlock_unlock(&as->lock);
428
    spinlock_unlock(&dst_as->lock);
426
    interrupts_restore(ipl);
429
    interrupts_restore(ipl);
427
   
430
   
428
    return 0;
431
    return 0;
429
}
432
}
430
 
433
 
431
/** Initialize mapping for one page of address space.
434
/** Initialize mapping for one page of address space.
432
 *
435
 *
433
 * This functions maps 'page' to 'frame' according
436
 * This functions maps 'page' to 'frame' according
434
 * to attributes of the address space area to
437
 * to attributes of the address space area to
435
 * wich 'page' belongs.
438
 * wich 'page' belongs.
436
 *
439
 *
437
 * @param as Target address space.
440
 * @param as Target address space.
438
 * @param page Virtual page within the area.
441
 * @param page Virtual page within the area.
439
 * @param frame Physical frame to which page will be mapped.
442
 * @param frame Physical frame to which page will be mapped.
440
 */
443
 */
441
void as_set_mapping(as_t *as, __address page, __address frame)
444
void as_set_mapping(as_t *as, __address page, __address frame)
442
{
445
{
443
    as_area_t *area;
446
    as_area_t *area;
444
    ipl_t ipl;
447
    ipl_t ipl;
445
   
448
   
446
    ipl = interrupts_disable();
449
    ipl = interrupts_disable();
447
    page_table_lock(as, true);
450
    page_table_lock(as, true);
448
   
451
   
449
    area = find_area_and_lock(as, page);
452
    area = find_area_and_lock(as, page);
450
    if (!area) {
453
    if (!area) {
451
        panic("page not part of any as_area\n");
454
        panic("page not part of any as_area\n");
452
    }
455
    }
453
 
456
 
454
    page_mapping_insert(as, page, frame, get_area_flags(area));
457
    page_mapping_insert(as, page, frame, get_area_flags(area));
455
   
458
   
456
    spinlock_unlock(&area->lock);
459
    spinlock_unlock(&area->lock);
457
    page_table_unlock(as, true);
460
    page_table_unlock(as, true);
458
    interrupts_restore(ipl);
461
    interrupts_restore(ipl);
459
}
462
}
460
 
463
 
461
/** Handle page fault within the current address space.
464
/** Handle page fault within the current address space.
462
 *
465
 *
463
 * This is the high-level page fault handler.
466
 * This is the high-level page fault handler.
464
 * Interrupts are assumed disabled.
467
 * Interrupts are assumed disabled.
465
 *
468
 *
466
 * @param page Faulting page.
469
 * @param page Faulting page.
467
 *
470
 *
468
 * @return 0 on page fault, 1 on success.
471
 * @return 0 on page fault, 1 on success.
469
 */
472
 */
470
int as_page_fault(__address page)
473
int as_page_fault(__address page)
471
{
474
{
472
    pte_t *pte;
475
    pte_t *pte;
473
    as_area_t *area;
476
    as_area_t *area;
474
    __address frame;
477
    __address frame;
475
   
478
   
476
    ASSERT(AS);
479
    ASSERT(AS);
477
 
480
 
478
    spinlock_lock(&AS->lock);
481
    spinlock_lock(&AS->lock);
479
    area = find_area_and_lock(AS, page);   
482
    area = find_area_and_lock(AS, page);   
480
    if (!area) {
483
    if (!area) {
481
        /*
484
        /*
482
         * No area contained mapping for 'page'.
485
         * No area contained mapping for 'page'.
483
         * Signal page fault to low-level handler.
486
         * Signal page fault to low-level handler.
484
         */
487
         */
485
        spinlock_unlock(&AS->lock);
488
        spinlock_unlock(&AS->lock);
486
        return 0;
489
        return 0;
487
    }
490
    }
488
 
491
 
-
 
492
    if (area->attributes & AS_AREA_ATTR_PARTIAL) {
-
 
493
        /*
-
 
494
         * The address space area is not fully initialized.
-
 
495
         * Avoid possible race by returning error.
-
 
496
         */
-
 
497
        spinlock_unlock(&area->lock);
-
 
498
        spinlock_unlock(&AS->lock);
-
 
499
        return 0;      
-
 
500
    }
-
 
501
 
489
    ASSERT(!(area->flags & AS_AREA_DEVICE));
502
    ASSERT(!(area->flags & AS_AREA_DEVICE));
490
 
503
 
491
    page_table_lock(AS, false);
504
    page_table_lock(AS, false);
492
   
505
   
493
    /*
506
    /*
494
     * To avoid race condition between two page faults
507
     * To avoid race condition between two page faults
495
     * on the same address, we need to make sure
508
     * on the same address, we need to make sure
496
     * the mapping has not been already inserted.
509
     * the mapping has not been already inserted.
497
     */
510
     */
498
    if ((pte = page_mapping_find(AS, page))) {
511
    if ((pte = page_mapping_find(AS, page))) {
499
        if (PTE_PRESENT(pte)) {
512
        if (PTE_PRESENT(pte)) {
500
            page_table_unlock(AS, false);
513
            page_table_unlock(AS, false);
501
            spinlock_unlock(&area->lock);
514
            spinlock_unlock(&area->lock);
502
            spinlock_unlock(&AS->lock);
515
            spinlock_unlock(&AS->lock);
503
            return 1;
516
            return 1;
504
        }
517
        }
505
    }
518
    }
506
 
519
 
507
    /*
520
    /*
508
     * In general, there can be several reasons that
521
     * In general, there can be several reasons that
509
     * can have caused this fault.
522
     * can have caused this fault.
510
     *
523
     *
511
     * - non-existent mapping: the area is a scratch
524
     * - non-existent mapping: the area is a scratch
512
     *   area (e.g. stack) and so far has not been
525
     *   area (e.g. stack) and so far has not been
513
     *   allocated a frame for the faulting page
526
     *   allocated a frame for the faulting page
514
     *
527
     *
515
     * - non-present mapping: another possibility,
528
     * - non-present mapping: another possibility,
516
     *   currently not implemented, would be frame
529
     *   currently not implemented, would be frame
517
     *   reuse; when this becomes a possibility,
530
     *   reuse; when this becomes a possibility,
518
     *   do not forget to distinguish between
531
     *   do not forget to distinguish between
519
     *   the different causes
532
     *   the different causes
520
     */
533
     */
521
    frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
534
    frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
522
    memsetb(PA2KA(frame), FRAME_SIZE, 0);
535
    memsetb(PA2KA(frame), FRAME_SIZE, 0);
523
   
536
   
524
    /*
537
    /*
525
     * Map 'page' to 'frame'.
538
     * Map 'page' to 'frame'.
526
     * Note that TLB shootdown is not attempted as only new information is being
539
     * Note that TLB shootdown is not attempted as only new information is being
527
     * inserted into page tables.
540
     * inserted into page tables.
528
     */
541
     */
529
    page_mapping_insert(AS, page, frame, get_area_flags(area));
542
    page_mapping_insert(AS, page, frame, get_area_flags(area));
530
    page_table_unlock(AS, false);
543
    page_table_unlock(AS, false);
531
   
544
   
532
    spinlock_unlock(&area->lock);
545
    spinlock_unlock(&area->lock);
533
    spinlock_unlock(&AS->lock);
546
    spinlock_unlock(&AS->lock);
534
    return 1;
547
    return 1;
535
}
548
}
536
 
549
 
537
/** Switch address spaces.
550
/** Switch address spaces.
538
 *
551
 *
539
 * @param old Old address space or NULL.
552
 * @param old Old address space or NULL.
540
 * @param new New address space.
553
 * @param new New address space.
541
 */
554
 */
542
void as_switch(as_t *old, as_t *new)
555
void as_switch(as_t *old, as_t *new)
543
{
556
{
544
    ipl_t ipl;
557
    ipl_t ipl;
545
    bool needs_asid = false;
558
    bool needs_asid = false;
546
   
559
   
547
    ipl = interrupts_disable();
560
    ipl = interrupts_disable();
548
    spinlock_lock(&as_lock);
561
    spinlock_lock(&as_lock);
549
 
562
 
550
    /*
563
    /*
551
     * First, take care of the old address space.
564
     * First, take care of the old address space.
552
     */
565
     */
553
    if (old) {
566
    if (old) {
554
        spinlock_lock(&old->lock);
567
        spinlock_lock(&old->lock);
555
        ASSERT(old->refcount);
568
        ASSERT(old->refcount);
556
        if((--old->refcount == 0) && (old != AS_KERNEL)) {
569
        if((--old->refcount == 0) && (old != AS_KERNEL)) {
557
            /*
570
            /*
558
             * The old address space is no longer active on
571
             * The old address space is no longer active on
559
             * any processor. It can be appended to the
572
             * any processor. It can be appended to the
560
             * list of inactive address spaces with assigned
573
             * list of inactive address spaces with assigned
561
             * ASID.
574
             * ASID.
562
             */
575
             */
563
             ASSERT(old->asid != ASID_INVALID);
576
             ASSERT(old->asid != ASID_INVALID);
564
             list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
577
             list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
565
        }
578
        }
566
        spinlock_unlock(&old->lock);
579
        spinlock_unlock(&old->lock);
567
    }
580
    }
568
 
581
 
569
    /*
582
    /*
570
     * Second, prepare the new address space.
583
     * Second, prepare the new address space.
571
     */
584
     */
572
    spinlock_lock(&new->lock);
585
    spinlock_lock(&new->lock);
573
    if ((new->refcount++ == 0) && (new != AS_KERNEL)) {
586
    if ((new->refcount++ == 0) && (new != AS_KERNEL)) {
574
        if (new->asid != ASID_INVALID)
587
        if (new->asid != ASID_INVALID)
575
            list_remove(&new->inactive_as_with_asid_link);
588
            list_remove(&new->inactive_as_with_asid_link);
576
        else
589
        else
577
            needs_asid = true;  /* defer call to asid_get() until new->lock is released */
590
            needs_asid = true;  /* defer call to asid_get() until new->lock is released */
578
    }
591
    }
579
    SET_PTL0_ADDRESS(new->page_table);
592
    SET_PTL0_ADDRESS(new->page_table);
580
    spinlock_unlock(&new->lock);
593
    spinlock_unlock(&new->lock);
581
 
594
 
582
    if (needs_asid) {
595
    if (needs_asid) {
583
        /*
596
        /*
584
         * Allocation of new ASID was deferred
597
         * Allocation of new ASID was deferred
585
         * until now in order to avoid deadlock.
598
         * until now in order to avoid deadlock.
586
         */
599
         */
587
        asid_t asid;
600
        asid_t asid;
588
       
601
       
589
        asid = asid_get();
602
        asid = asid_get();
590
        spinlock_lock(&new->lock);
603
        spinlock_lock(&new->lock);
591
        new->asid = asid;
604
        new->asid = asid;
592
        spinlock_unlock(&new->lock);
605
        spinlock_unlock(&new->lock);
593
    }
606
    }
594
    spinlock_unlock(&as_lock);
607
    spinlock_unlock(&as_lock);
595
    interrupts_restore(ipl);
608
    interrupts_restore(ipl);
596
   
609
   
597
    /*
610
    /*
598
     * Perform architecture-specific steps.
611
     * Perform architecture-specific steps.
599
     * (e.g. write ASID to hardware register etc.)
612
     * (e.g. write ASID to hardware register etc.)
600
     */
613
     */
601
    as_install_arch(new);
614
    as_install_arch(new);
602
   
615
   
603
    AS = new;
616
    AS = new;
604
}
617
}
605
 
618
 
606
/** Convert address space area flags to page flags.
619
/** Convert address space area flags to page flags.
607
 *
620
 *
608
 * @param aflags Flags of some address space area.
621
 * @param aflags Flags of some address space area.
609
 *
622
 *
610
 * @return Flags to be passed to page_mapping_insert().
623
 * @return Flags to be passed to page_mapping_insert().
611
 */
624
 */
612
int area_flags_to_page_flags(int aflags)
625
int area_flags_to_page_flags(int aflags)
613
{
626
{
614
    int flags;
627
    int flags;
615
 
628
 
616
    flags = PAGE_USER | PAGE_PRESENT;
629
    flags = PAGE_USER | PAGE_PRESENT;
617
   
630
   
618
    if (aflags & AS_AREA_READ)
631
    if (aflags & AS_AREA_READ)
619
        flags |= PAGE_READ;
632
        flags |= PAGE_READ;
620
       
633
       
621
    if (aflags & AS_AREA_WRITE)
634
    if (aflags & AS_AREA_WRITE)
622
        flags |= PAGE_WRITE;
635
        flags |= PAGE_WRITE;
623
   
636
   
624
    if (aflags & AS_AREA_EXEC)
637
    if (aflags & AS_AREA_EXEC)
625
        flags |= PAGE_EXEC;
638
        flags |= PAGE_EXEC;
626
   
639
   
627
    if (!(aflags & AS_AREA_DEVICE))
640
    if (!(aflags & AS_AREA_DEVICE))
628
        flags |= PAGE_CACHEABLE;
641
        flags |= PAGE_CACHEABLE;
629
       
642
       
630
    return flags;
643
    return flags;
631
}
644
}
632
 
645
 
633
/** Compute flags for virtual address translation subsytem.
646
/** Compute flags for virtual address translation subsytem.
634
 *
647
 *
635
 * The address space area must be locked.
648
 * The address space area must be locked.
636
 * Interrupts must be disabled.
649
 * Interrupts must be disabled.
637
 *
650
 *
638
 * @param a Address space area.
651
 * @param a Address space area.
639
 *
652
 *
640
 * @return Flags to be used in page_mapping_insert().
653
 * @return Flags to be used in page_mapping_insert().
641
 */
654
 */
642
int get_area_flags(as_area_t *a)
655
int get_area_flags(as_area_t *a)
643
{
656
{
644
    return area_flags_to_page_flags(a->flags);
657
    return area_flags_to_page_flags(a->flags);
645
}
658
}
646
 
659
 
647
/** Create page table.
660
/** Create page table.
648
 *
661
 *
649
 * Depending on architecture, create either address space
662
 * Depending on architecture, create either address space
650
 * private or global page table.
663
 * private or global page table.
651
 *
664
 *
652
 * @param flags Flags saying whether the page table is for kernel address space.
665
 * @param flags Flags saying whether the page table is for kernel address space.
653
 *
666
 *
654
 * @return First entry of the page table.
667
 * @return First entry of the page table.
655
 */
668
 */
656
pte_t *page_table_create(int flags)
669
pte_t *page_table_create(int flags)
657
{
670
{
658
        ASSERT(as_operations);
671
        ASSERT(as_operations);
659
        ASSERT(as_operations->page_table_create);
672
        ASSERT(as_operations->page_table_create);
660
 
673
 
661
        return as_operations->page_table_create(flags);
674
        return as_operations->page_table_create(flags);
662
}
675
}
663
 
676
 
664
/** Lock page table.
677
/** Lock page table.
665
 *
678
 *
666
 * This function should be called before any page_mapping_insert(),
679
 * This function should be called before any page_mapping_insert(),
667
 * page_mapping_remove() and page_mapping_find().
680
 * page_mapping_remove() and page_mapping_find().
668
 *
681
 *
669
 * Locking order is such that address space areas must be locked
682
 * Locking order is such that address space areas must be locked
670
 * prior to this call. Address space can be locked prior to this
683
 * prior to this call. Address space can be locked prior to this
671
 * call in which case the lock argument is false.
684
 * call in which case the lock argument is false.
672
 *
685
 *
673
 * @param as Address space.
686
 * @param as Address space.
674
 * @param as_locked If false, do not attempt to lock as->lock.
687
 * @param as_locked If false, do not attempt to lock as->lock.
675
 */
688
 */
676
void page_table_lock(as_t *as, bool lock)
689
void page_table_lock(as_t *as, bool lock)
677
{
690
{
678
    ASSERT(as_operations);
691
    ASSERT(as_operations);
679
    ASSERT(as_operations->page_table_lock);
692
    ASSERT(as_operations->page_table_lock);
680
 
693
 
681
    as_operations->page_table_lock(as, lock);
694
    as_operations->page_table_lock(as, lock);
682
}
695
}
683
 
696
 
684
/** Unlock page table.
697
/** Unlock page table.
685
 *
698
 *
686
 * @param as Address space.
699
 * @param as Address space.
687
 * @param as_locked If false, do not attempt to unlock as->lock.
700
 * @param as_locked If false, do not attempt to unlock as->lock.
688
 */
701
 */
689
void page_table_unlock(as_t *as, bool unlock)
702
void page_table_unlock(as_t *as, bool unlock)
690
{
703
{
691
    ASSERT(as_operations);
704
    ASSERT(as_operations);
692
    ASSERT(as_operations->page_table_unlock);
705
    ASSERT(as_operations->page_table_unlock);
693
 
706
 
694
    as_operations->page_table_unlock(as, unlock);
707
    as_operations->page_table_unlock(as, unlock);
695
}
708
}
696
 
709
 
697
 
710
 
698
/** Find address space area and lock it.
711
/** Find address space area and lock it.
699
 *
712
 *
700
 * The address space must be locked and interrupts must be disabled.
713
 * The address space must be locked and interrupts must be disabled.
701
 *
714
 *
702
 * @param as Address space.
715
 * @param as Address space.
703
 * @param va Virtual address.
716
 * @param va Virtual address.
704
 *
717
 *
705
 * @return Locked address space area containing va on success or NULL on failure.
718
 * @return Locked address space area containing va on success or NULL on failure.
706
 */
719
 */
707
as_area_t *find_area_and_lock(as_t *as, __address va)
720
as_area_t *find_area_and_lock(as_t *as, __address va)
708
{
721
{
709
    as_area_t *a;
722
    as_area_t *a;
710
    btree_node_t *leaf, *lnode;
723
    btree_node_t *leaf, *lnode;
711
    int i;
724
    int i;
712
   
725
   
713
    a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
726
    a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
714
    if (a) {
727
    if (a) {
715
        /* va is the base address of an address space area */
728
        /* va is the base address of an address space area */
716
        spinlock_lock(&a->lock);
729
        spinlock_lock(&a->lock);
717
        return a;
730
        return a;
718
    }
731
    }
719
   
732
   
720
    /*
733
    /*
721
     * Search the leaf node and the righmost record of its left neighbour
734
     * Search the leaf node and the righmost record of its left neighbour
722
     * to find out whether this is a miss or va belongs to an address
735
     * to find out whether this is a miss or va belongs to an address
723
     * space area found there.
736
     * space area found there.
724
     */
737
     */
725
   
738
   
726
    /* First, search the leaf node itself. */
739
    /* First, search the leaf node itself. */
727
    for (i = 0; i < leaf->keys; i++) {
740
    for (i = 0; i < leaf->keys; i++) {
728
        a = (as_area_t *) leaf->value[i];
741
        a = (as_area_t *) leaf->value[i];
729
        spinlock_lock(&a->lock);
742
        spinlock_lock(&a->lock);
730
        if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) {
743
        if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) {
731
            return a;
744
            return a;
732
        }
745
        }
733
        spinlock_unlock(&a->lock);
746
        spinlock_unlock(&a->lock);
734
    }
747
    }
735
 
748
 
736
    /*
749
    /*
737
     * Second, locate the left neighbour and test its last record.
750
     * Second, locate the left neighbour and test its last record.
738
     * Because of its position in the B+tree, it must have base < va.
751
     * Because of its position in the B+tree, it must have base < va.
739
     */
752
     */
740
    if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
753
    if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
741
        a = (as_area_t *) lnode->value[lnode->keys - 1];
754
        a = (as_area_t *) lnode->value[lnode->keys - 1];
742
        spinlock_lock(&a->lock);
755
        spinlock_lock(&a->lock);
743
        if (va < a->base + a->pages * PAGE_SIZE) {
756
        if (va < a->base + a->pages * PAGE_SIZE) {
744
            return a;
757
            return a;
745
        }
758
        }
746
        spinlock_unlock(&a->lock);
759
        spinlock_unlock(&a->lock);
747
    }
760
    }
748
 
761
 
749
    return NULL;
762
    return NULL;
750
}
763
}
751
 
764
 
752
/** Check area conflicts with other areas.
765
/** Check area conflicts with other areas.
753
 *
766
 *
754
 * The address space must be locked and interrupts must be disabled.
767
 * The address space must be locked and interrupts must be disabled.
755
 *
768
 *
756
 * @param as Address space.
769
 * @param as Address space.
757
 * @param va Starting virtual address of the area being tested.
770
 * @param va Starting virtual address of the area being tested.
758
 * @param size Size of the area being tested.
771
 * @param size Size of the area being tested.
759
 * @param avoid_area Do not touch this area.
772
 * @param avoid_area Do not touch this area.
760
 *
773
 *
761
 * @return True if there is no conflict, false otherwise.
774
 * @return True if there is no conflict, false otherwise.
762
 */
775
 */
763
bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area)
776
bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area)
764
{
777
{
765
    as_area_t *a;
778
    as_area_t *a;
766
    btree_node_t *leaf, *node;
779
    btree_node_t *leaf, *node;
767
    int i;
780
    int i;
768
   
781
   
769
    /*
782
    /*
770
     * We don't want any area to have conflicts with NULL page.
783
     * We don't want any area to have conflicts with NULL page.
771
     */
784
     */
772
    if (overlaps(va, size, NULL, PAGE_SIZE))
785
    if (overlaps(va, size, NULL, PAGE_SIZE))
773
        return false;
786
        return false;
774
   
787
   
775
    /*
788
    /*
776
     * The leaf node is found in O(log n), where n is proportional to
789
     * The leaf node is found in O(log n), where n is proportional to
777
     * the number of address space areas belonging to as.
790
     * the number of address space areas belonging to as.
778
     * The check for conflicts is then attempted on the rightmost
791
     * The check for conflicts is then attempted on the rightmost
779
     * record in the left neighbour, the leftmost record in the right
792
     * record in the left neighbour, the leftmost record in the right
780
     * neighbour and all records in the leaf node itself.
793
     * neighbour and all records in the leaf node itself.
781
     */
794
     */
782
   
795
   
783
    if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) {
796
    if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) {
784
        if (a != avoid_area)
797
        if (a != avoid_area)
785
            return false;
798
            return false;
786
    }
799
    }
787
   
800
   
788
    /* First, check the two border cases. */
801
    /* First, check the two border cases. */
789
    if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
802
    if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
790
        a = (as_area_t *) node->value[node->keys - 1];
803
        a = (as_area_t *) node->value[node->keys - 1];
791
        spinlock_lock(&a->lock);
804
        spinlock_lock(&a->lock);
792
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
805
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
793
            spinlock_unlock(&a->lock);
806
            spinlock_unlock(&a->lock);
794
            return false;
807
            return false;
795
        }
808
        }
796
        spinlock_unlock(&a->lock);
809
        spinlock_unlock(&a->lock);
797
    }
810
    }
798
    if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) {
811
    if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) {
799
        a = (as_area_t *) node->value[0];
812
        a = (as_area_t *) node->value[0];
800
        spinlock_lock(&a->lock);
813
        spinlock_lock(&a->lock);
801
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
814
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
802
            spinlock_unlock(&a->lock);
815
            spinlock_unlock(&a->lock);
803
            return false;
816
            return false;
804
        }
817
        }
805
        spinlock_unlock(&a->lock);
818
        spinlock_unlock(&a->lock);
806
    }
819
    }
807
   
820
   
808
    /* Second, check the leaf node. */
821
    /* Second, check the leaf node. */
809
    for (i = 0; i < leaf->keys; i++) {
822
    for (i = 0; i < leaf->keys; i++) {
810
        a = (as_area_t *) leaf->value[i];
823
        a = (as_area_t *) leaf->value[i];
811
   
824
   
812
        if (a == avoid_area)
825
        if (a == avoid_area)
813
            continue;
826
            continue;
814
   
827
   
815
        spinlock_lock(&a->lock);
828
        spinlock_lock(&a->lock);
816
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
829
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
817
            spinlock_unlock(&a->lock);
830
            spinlock_unlock(&a->lock);
818
            return false;
831
            return false;
819
        }
832
        }
820
        spinlock_unlock(&a->lock);
833
        spinlock_unlock(&a->lock);
821
    }
834
    }
822
 
835
 
823
    /*
836
    /*
824
     * So far, the area does not conflict with other areas.
837
     * So far, the area does not conflict with other areas.
825
     * Check if it doesn't conflict with kernel address space.
838
     * Check if it doesn't conflict with kernel address space.
826
     */  
839
     */  
827
    if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
840
    if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
828
        return !overlaps(va, size,
841
        return !overlaps(va, size,
829
            KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START);
842
            KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START);
830
    }
843
    }
831
 
844
 
832
    return true;
845
    return true;
833
}
846
}
834
 
847
 
835
/*
848
/*
836
 * Address space related syscalls.
849
 * Address space related syscalls.
837
 */
850
 */
838
 
851
 
839
/** Wrapper for as_area_create(). */
852
/** Wrapper for as_area_create(). */
840
__native sys_as_area_create(__address address, size_t size, int flags)
853
__native sys_as_area_create(__address address, size_t size, int flags)
841
{
854
{
842
    if (as_area_create(AS, flags, size, address))
855
    if (as_area_create(AS, flags, size, address, AS_AREA_ATTR_NONE))
843
        return (__native) address;
856
        return (__native) address;
844
    else
857
    else
845
        return (__native) -1;
858
        return (__native) -1;
846
}
859
}
847
 
860
 
848
/** Wrapper for as_area_resize. */
861
/** Wrapper for as_area_resize. */
849
__native sys_as_area_resize(__address address, size_t size, int flags)
862
__native sys_as_area_resize(__address address, size_t size, int flags)
850
{
863
{
851
    return as_area_resize(AS, address, size, 0);
864
    return as_area_resize(AS, address, size, 0);
852
}
865
}
853
 
866
 
854
/** Prepare task for accepting address space area from another task.
867
/** Prepare task for accepting address space area from another task.
855
 *
868
 *
856
 * @param uspace_accept_arg Accept structure passed from userspace.
869
 * @param uspace_accept_arg Accept structure passed from userspace.
857
 *
870
 *
858
 * @return EPERM if the task ID encapsulated in @uspace_accept_arg references
871
 * @return EPERM if the task ID encapsulated in @uspace_accept_arg references
859
 *     TASK. Otherwise zero is returned.
872
 *     TASK. Otherwise zero is returned.
860
 */
873
 */
861
__native sys_as_area_accept(as_area_acptsnd_arg_t *uspace_accept_arg)
874
__native sys_as_area_accept(as_area_acptsnd_arg_t *uspace_accept_arg)
862
{
875
{
863
    as_area_acptsnd_arg_t arg;
876
    as_area_acptsnd_arg_t arg;
864
   
877
   
865
    copy_from_uspace(&arg, uspace_accept_arg, sizeof(as_area_acptsnd_arg_t));
878
    copy_from_uspace(&arg, uspace_accept_arg, sizeof(as_area_acptsnd_arg_t));
866
   
879
   
867
    if (!arg.size)
880
    if (!arg.size)
868
        return (__native) EPERM;
881
        return (__native) EPERM;
869
   
882
   
870
    if (arg.task_id == TASK->taskid) {
883
    if (arg.task_id == TASK->taskid) {
871
        /*
884
        /*
872
         * Accepting from itself is not allowed.
885
         * Accepting from itself is not allowed.
873
         */
886
         */
874
        return (__native) EPERM;
887
        return (__native) EPERM;
875
    }
888
    }
876
   
889
   
877
    memcpy(&TASK->accept_arg, &arg, sizeof(as_area_acptsnd_arg_t));
890
    memcpy(&TASK->accept_arg, &arg, sizeof(as_area_acptsnd_arg_t));
878
   
891
   
879
        return 0;
892
        return 0;
880
}
893
}
881
 
894
 
882
/** Wrapper for as_area_send. */
895
/** Wrapper for as_area_send. */
883
__native sys_as_area_send(as_area_acptsnd_arg_t *uspace_send_arg)
896
__native sys_as_area_send(as_area_acptsnd_arg_t *uspace_send_arg)
884
{
897
{
885
    as_area_acptsnd_arg_t arg;
898
    as_area_acptsnd_arg_t arg;
886
   
899
   
887
    copy_from_uspace(&arg, uspace_send_arg, sizeof(as_area_acptsnd_arg_t));
900
    copy_from_uspace(&arg, uspace_send_arg, sizeof(as_area_acptsnd_arg_t));
888
 
901
 
889
    if (!arg.size)
902
    if (!arg.size)
890
        return (__native) EPERM;
903
        return (__native) EPERM;
891
   
904
   
892
    if (arg.task_id == TASK->taskid) {
905
    if (arg.task_id == TASK->taskid) {
893
        /*
906
        /*
894
         * Sending to itself is not allowed.
907
         * Sending to itself is not allowed.
895
         */
908
         */
896
        return (__native) EPERM;
909
        return (__native) EPERM;
897
    }
910
    }
898
 
911
 
899
    return (__native) as_area_send(arg.task_id, (__address) arg.base);
912
    return (__native) as_area_send(arg.task_id, (__address) arg.base);
900
}
913
}
901
 
914