Subversion Repositories HelenOS-historic

Rev

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

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