Subversion Repositories HelenOS

Rev

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

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