Subversion Repositories HelenOS-historic

Rev

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

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