Subversion Repositories HelenOS-historic

Rev

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

Rev 1227 Rev 1233
1
/*
1
/*
2
 * Copyright (C) 2001-2006 Jakub Jermar
2
 * Copyright (C) 2001-2006 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/*
29
/*
30
 * This file contains address space manipulation functions.
30
 * This file contains address space manipulation functions.
31
 * Roughly speaking, this is a higher-level client of
31
 * Roughly speaking, this is a higher-level client of
32
 * Virtual Address Translation (VAT) subsystem.
32
 * Virtual Address Translation (VAT) subsystem.
33
 */
33
 */
34
 
34
 
35
#include <mm/as.h>
35
#include <mm/as.h>
36
#include <arch/mm/as.h>
36
#include <arch/mm/as.h>
37
#include <mm/page.h>
37
#include <mm/page.h>
38
#include <mm/frame.h>
38
#include <mm/frame.h>
39
#include <mm/slab.h>
39
#include <mm/slab.h>
40
#include <mm/tlb.h>
40
#include <mm/tlb.h>
41
#include <arch/mm/page.h>
41
#include <arch/mm/page.h>
42
#include <genarch/mm/page_pt.h>
42
#include <genarch/mm/page_pt.h>
43
#include <genarch/mm/page_ht.h>
43
#include <genarch/mm/page_ht.h>
44
#include <mm/asid.h>
44
#include <mm/asid.h>
45
#include <arch/mm/asid.h>
45
#include <arch/mm/asid.h>
46
#include <arch/types.h>
46
#include <arch/types.h>
47
#include <typedefs.h>
47
#include <typedefs.h>
48
#include <synch/spinlock.h>
48
#include <synch/spinlock.h>
49
#include <config.h>
49
#include <config.h>
50
#include <adt/list.h>
50
#include <adt/list.h>
51
#include <adt/btree.h>
51
#include <adt/btree.h>
52
#include <panic.h>
52
#include <panic.h>
53
#include <arch/asm.h>
53
#include <arch/asm.h>
54
#include <debug.h>
54
#include <debug.h>
55
#include <memstr.h>
55
#include <memstr.h>
56
#include <macros.h>
56
#include <macros.h>
57
#include <arch.h>
57
#include <arch.h>
58
#include <print.h>
58
#include <print.h>
59
 
59
 
60
as_operations_t *as_operations = NULL;
60
as_operations_t *as_operations = NULL;
61
 
61
 
62
/** Address space lock. It protects inactive_as_with_asid_head. */
62
/** Address space lock. It protects inactive_as_with_asid_head. */
63
SPINLOCK_INITIALIZE(as_lock);
63
SPINLOCK_INITIALIZE(as_lock);
64
 
64
 
65
/**
65
/**
66
 * This list contains address spaces that are not active on any
66
 * This list contains address spaces that are not active on any
67
 * processor and that have valid ASID.
67
 * processor and that have valid ASID.
68
 */
68
 */
69
LIST_INITIALIZE(inactive_as_with_asid_head);
69
LIST_INITIALIZE(inactive_as_with_asid_head);
70
 
70
 
71
/** Kernel address space. */
71
/** Kernel address space. */
72
as_t *AS_KERNEL = NULL;
72
as_t *AS_KERNEL = NULL;
73
 
73
 
74
static int get_area_flags(as_area_t *a);
74
static int get_area_flags(as_area_t *a);
75
static as_area_t *find_area_and_lock(as_t *as, __address va);
75
static as_area_t *find_area_and_lock(as_t *as, __address va);
76
static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area);
76
static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area);
77
 
77
 
78
/** Initialize address space subsystem. */
78
/** Initialize address space subsystem. */
79
void as_init(void)
79
void as_init(void)
80
{
80
{
81
    as_arch_init();
81
    as_arch_init();
82
    AS_KERNEL = as_create(FLAG_AS_KERNEL);
82
    AS_KERNEL = as_create(FLAG_AS_KERNEL);
83
        if (!AS_KERNEL)
83
        if (!AS_KERNEL)
84
                panic("can't create kernel address space\n");
84
                panic("can't create kernel address space\n");
85
}
85
}
86
 
86
 
87
/** Create address space.
87
/** Create address space.
88
 *
88
 *
89
 * @param flags Flags that influence way in wich the address space is created.
89
 * @param flags Flags that influence way in wich the address space is created.
90
 */
90
 */
91
as_t *as_create(int flags)
91
as_t *as_create(int flags)
92
{
92
{
93
    as_t *as;
93
    as_t *as;
94
 
94
 
95
    as = (as_t *) malloc(sizeof(as_t), 0);
95
    as = (as_t *) malloc(sizeof(as_t), 0);
96
    link_initialize(&as->inactive_as_with_asid_link);
96
    link_initialize(&as->inactive_as_with_asid_link);
97
    spinlock_initialize(&as->lock, "as_lock");
97
    spinlock_initialize(&as->lock, "as_lock");
98
    btree_create(&as->as_area_btree);
98
    btree_create(&as->as_area_btree);
99
   
99
   
100
    if (flags & FLAG_AS_KERNEL)
100
    if (flags & FLAG_AS_KERNEL)
101
        as->asid = ASID_KERNEL;
101
        as->asid = ASID_KERNEL;
102
    else
102
    else
103
        as->asid = ASID_INVALID;
103
        as->asid = ASID_INVALID;
104
   
104
   
105
    as->refcount = 0;
105
    as->refcount = 0;
106
    as->page_table = page_table_create(flags);
106
    as->page_table = page_table_create(flags);
107
 
107
 
108
    return as;
108
    return as;
109
}
109
}
110
 
110
 
111
/** Free Adress space */
111
/** Free Adress space */
112
void as_free(as_t *as)
112
void as_free(as_t *as)
113
{
113
{
114
    ASSERT(as->refcount == 0);
114
    ASSERT(as->refcount == 0);
115
 
115
 
116
    /* TODO: free as_areas and other resources held by as */
116
    /* TODO: free as_areas and other resources held by as */
117
    /* TODO: free page table */
117
    /* TODO: free page table */
118
    free(as);
118
    free(as);
119
}
119
}
120
 
120
 
121
/** Create address space area of common attributes.
121
/** Create address space area of common attributes.
122
 *
122
 *
123
 * The created address space area is added to the target address space.
123
 * The created address space area is added to the target address space.
124
 *
124
 *
125
 * @param as Target address space.
125
 * @param as Target address space.
126
 * @param flags Flags of the area.
126
 * @param flags Flags of the area.
127
 * @param size Size of area.
127
 * @param size Size of area.
128
 * @param base Base address of area.
128
 * @param base Base address of area.
129
 *
129
 *
130
 * @return Address space area on success or NULL on failure.
130
 * @return Address space area on success or NULL on failure.
131
 */
131
 */
132
as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base)
132
as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base)
133
{
133
{
134
    ipl_t ipl;
134
    ipl_t ipl;
135
    as_area_t *a;
135
    as_area_t *a;
136
   
136
   
137
    if (base % PAGE_SIZE)
137
    if (base % PAGE_SIZE)
138
        return NULL;
138
        return NULL;
139
 
139
 
-
 
140
    if (!size)
-
 
141
        return NULL;
-
 
142
 
140
    /* Writeable executable areas are not supported. */
143
    /* Writeable executable areas are not supported. */
141
    if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
144
    if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
142
        return NULL;
145
        return NULL;
143
   
146
   
144
    ipl = interrupts_disable();
147
    ipl = interrupts_disable();
145
    spinlock_lock(&as->lock);
148
    spinlock_lock(&as->lock);
146
   
149
   
147
    if (!check_area_conflicts(as, base, size, NULL)) {
150
    if (!check_area_conflicts(as, base, size, NULL)) {
148
        spinlock_unlock(&as->lock);
151
        spinlock_unlock(&as->lock);
149
        interrupts_restore(ipl);
152
        interrupts_restore(ipl);
150
        return NULL;
153
        return NULL;
151
    }
154
    }
152
   
155
   
153
    a = (as_area_t *) malloc(sizeof(as_area_t), 0);
156
    a = (as_area_t *) malloc(sizeof(as_area_t), 0);
154
 
157
 
155
    spinlock_initialize(&a->lock, "as_area_lock");
158
    spinlock_initialize(&a->lock, "as_area_lock");
156
   
159
   
157
    a->flags = flags;
160
    a->flags = flags;
158
    a->pages = SIZE2FRAMES(size);
161
    a->pages = SIZE2FRAMES(size);
159
    a->base = base;
162
    a->base = base;
160
   
163
   
161
    btree_insert(&as->as_area_btree, base, (void *) a, NULL);
164
    btree_insert(&as->as_area_btree, base, (void *) a, NULL);
162
 
165
 
163
    spinlock_unlock(&as->lock);
166
    spinlock_unlock(&as->lock);
164
    interrupts_restore(ipl);
167
    interrupts_restore(ipl);
165
 
168
 
166
    return a;
169
    return a;
167
}
170
}
168
 
171
 
169
/** Initialize mapping for one page of address space.
172
/** Initialize mapping for one page of address space.
170
 *
173
 *
171
 * This functions maps 'page' to 'frame' according
174
 * This functions maps 'page' to 'frame' according
172
 * to attributes of the address space area to
175
 * to attributes of the address space area to
173
 * wich 'page' belongs.
176
 * wich 'page' belongs.
174
 *
177
 *
175
 * @param as Target address space.
178
 * @param as Target address space.
176
 * @param page Virtual page within the area.
179
 * @param page Virtual page within the area.
177
 * @param frame Physical frame to which page will be mapped.
180
 * @param frame Physical frame to which page will be mapped.
178
 */
181
 */
179
void as_set_mapping(as_t *as, __address page, __address frame)
182
void as_set_mapping(as_t *as, __address page, __address frame)
180
{
183
{
181
    as_area_t *area;
184
    as_area_t *area;
182
    ipl_t ipl;
185
    ipl_t ipl;
183
   
186
   
184
    ipl = interrupts_disable();
187
    ipl = interrupts_disable();
185
    page_table_lock(as, true);
188
    page_table_lock(as, true);
186
   
189
   
187
    area = find_area_and_lock(as, page);
190
    area = find_area_and_lock(as, page);
188
    if (!area) {
191
    if (!area) {
189
        panic("page not part of any as_area\n");
192
        panic("page not part of any as_area\n");
190
    }
193
    }
191
 
194
 
192
    page_mapping_insert(as, page, frame, get_area_flags(area));
195
    page_mapping_insert(as, page, frame, get_area_flags(area));
193
   
196
   
194
    spinlock_unlock(&area->lock);
197
    spinlock_unlock(&area->lock);
195
    page_table_unlock(as, true);
198
    page_table_unlock(as, true);
196
    interrupts_restore(ipl);
199
    interrupts_restore(ipl);
197
}
200
}
198
 
201
 
199
/** Handle page fault within the current address space.
202
/** Handle page fault within the current address space.
200
 *
203
 *
201
 * This is the high-level page fault handler.
204
 * This is the high-level page fault handler.
202
 * Interrupts are assumed disabled.
205
 * Interrupts are assumed disabled.
203
 *
206
 *
204
 * @param page Faulting page.
207
 * @param page Faulting page.
205
 *
208
 *
206
 * @return 0 on page fault, 1 on success.
209
 * @return 0 on page fault, 1 on success.
207
 */
210
 */
208
int as_page_fault(__address page)
211
int as_page_fault(__address page)
209
{
212
{
210
    pte_t *pte;
213
    pte_t *pte;
211
    as_area_t *area;
214
    as_area_t *area;
212
    __address frame;
215
    __address frame;
213
   
216
   
214
    ASSERT(AS);
217
    ASSERT(AS);
215
 
218
 
216
    spinlock_lock(&AS->lock);
219
    spinlock_lock(&AS->lock);
217
    area = find_area_and_lock(AS, page);   
220
    area = find_area_and_lock(AS, page);   
218
    if (!area) {
221
    if (!area) {
219
        /*
222
        /*
220
         * No area contained mapping for 'page'.
223
         * No area contained mapping for 'page'.
221
         * Signal page fault to low-level handler.
224
         * Signal page fault to low-level handler.
222
         */
225
         */
223
        spinlock_unlock(&AS->lock);
226
        spinlock_unlock(&AS->lock);
224
        return 0;
227
        return 0;
225
    }
228
    }
226
 
229
 
227
    ASSERT(!(area->flags & AS_AREA_DEVICE));
230
    ASSERT(!(area->flags & AS_AREA_DEVICE));
228
 
231
 
229
    page_table_lock(AS, false);
232
    page_table_lock(AS, false);
230
   
233
   
231
    /*
234
    /*
232
     * To avoid race condition between two page faults
235
     * To avoid race condition between two page faults
233
     * on the same address, we need to make sure
236
     * on the same address, we need to make sure
234
     * the mapping has not been already inserted.
237
     * the mapping has not been already inserted.
235
     */
238
     */
236
    if ((pte = page_mapping_find(AS, page))) {
239
    if ((pte = page_mapping_find(AS, page))) {
237
        if (PTE_PRESENT(pte)) {
240
        if (PTE_PRESENT(pte)) {
238
            page_table_unlock(AS, false);
241
            page_table_unlock(AS, false);
239
            spinlock_unlock(&area->lock);
242
            spinlock_unlock(&area->lock);
240
            spinlock_unlock(&AS->lock);
243
            spinlock_unlock(&AS->lock);
241
            return 1;
244
            return 1;
242
        }
245
        }
243
    }
246
    }
244
 
247
 
245
    /*
248
    /*
246
     * In general, there can be several reasons that
249
     * In general, there can be several reasons that
247
     * can have caused this fault.
250
     * can have caused this fault.
248
     *
251
     *
249
     * - non-existent mapping: the area is a scratch
252
     * - non-existent mapping: the area is a scratch
250
     *   area (e.g. stack) and so far has not been
253
     *   area (e.g. stack) and so far has not been
251
     *   allocated a frame for the faulting page
254
     *   allocated a frame for the faulting page
252
     *
255
     *
253
     * - non-present mapping: another possibility,
256
     * - non-present mapping: another possibility,
254
     *   currently not implemented, would be frame
257
     *   currently not implemented, would be frame
255
     *   reuse; when this becomes a possibility,
258
     *   reuse; when this becomes a possibility,
256
     *   do not forget to distinguish between
259
     *   do not forget to distinguish between
257
     *   the different causes
260
     *   the different causes
258
     */
261
     */
259
    frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
262
    frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
260
    memsetb(PA2KA(frame), FRAME_SIZE, 0);
263
    memsetb(PA2KA(frame), FRAME_SIZE, 0);
261
   
264
   
262
    /*
265
    /*
263
     * Map 'page' to 'frame'.
266
     * Map 'page' to 'frame'.
264
     * Note that TLB shootdown is not attempted as only new information is being
267
     * Note that TLB shootdown is not attempted as only new information is being
265
     * inserted into page tables.
268
     * inserted into page tables.
266
     */
269
     */
267
    page_mapping_insert(AS, page, frame, get_area_flags(area));
270
    page_mapping_insert(AS, page, frame, get_area_flags(area));
268
    page_table_unlock(AS, false);
271
    page_table_unlock(AS, false);
269
   
272
   
270
    spinlock_unlock(&area->lock);
273
    spinlock_unlock(&area->lock);
271
    spinlock_unlock(&AS->lock);
274
    spinlock_unlock(&AS->lock);
272
    return 1;
275
    return 1;
273
}
276
}
274
 
277
 
275
/** Switch address spaces.
278
/** Switch address spaces.
276
 *
279
 *
277
 * @param old Old address space or NULL.
280
 * @param old Old address space or NULL.
278
 * @param new New address space.
281
 * @param new New address space.
279
 */
282
 */
280
void as_switch(as_t *old, as_t *new)
283
void as_switch(as_t *old, as_t *new)
281
{
284
{
282
    ipl_t ipl;
285
    ipl_t ipl;
283
    bool needs_asid = false;
286
    bool needs_asid = false;
284
   
287
   
285
    ipl = interrupts_disable();
288
    ipl = interrupts_disable();
286
    spinlock_lock(&as_lock);
289
    spinlock_lock(&as_lock);
287
 
290
 
288
    /*
291
    /*
289
     * First, take care of the old address space.
292
     * First, take care of the old address space.
290
     */
293
     */
291
    if (old) {
294
    if (old) {
292
        spinlock_lock(&old->lock);
295
        spinlock_lock(&old->lock);
293
        ASSERT(old->refcount);
296
        ASSERT(old->refcount);
294
        if((--old->refcount == 0) && (old != AS_KERNEL)) {
297
        if((--old->refcount == 0) && (old != AS_KERNEL)) {
295
            /*
298
            /*
296
             * The old address space is no longer active on
299
             * The old address space is no longer active on
297
             * any processor. It can be appended to the
300
             * any processor. It can be appended to the
298
             * list of inactive address spaces with assigned
301
             * list of inactive address spaces with assigned
299
             * ASID.
302
             * ASID.
300
             */
303
             */
301
             ASSERT(old->asid != ASID_INVALID);
304
             ASSERT(old->asid != ASID_INVALID);
302
             list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
305
             list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
303
        }
306
        }
304
        spinlock_unlock(&old->lock);
307
        spinlock_unlock(&old->lock);
305
    }
308
    }
306
 
309
 
307
    /*
310
    /*
308
     * Second, prepare the new address space.
311
     * Second, prepare the new address space.
309
     */
312
     */
310
    spinlock_lock(&new->lock);
313
    spinlock_lock(&new->lock);
311
    if ((new->refcount++ == 0) && (new != AS_KERNEL)) {
314
    if ((new->refcount++ == 0) && (new != AS_KERNEL)) {
312
        if (new->asid != ASID_INVALID)
315
        if (new->asid != ASID_INVALID)
313
            list_remove(&new->inactive_as_with_asid_link);
316
            list_remove(&new->inactive_as_with_asid_link);
314
        else
317
        else
315
            needs_asid = true;  /* defer call to asid_get() until new->lock is released */
318
            needs_asid = true;  /* defer call to asid_get() until new->lock is released */
316
    }
319
    }
317
    SET_PTL0_ADDRESS(new->page_table);
320
    SET_PTL0_ADDRESS(new->page_table);
318
    spinlock_unlock(&new->lock);
321
    spinlock_unlock(&new->lock);
319
 
322
 
320
    if (needs_asid) {
323
    if (needs_asid) {
321
        /*
324
        /*
322
         * Allocation of new ASID was deferred
325
         * Allocation of new ASID was deferred
323
         * until now in order to avoid deadlock.
326
         * until now in order to avoid deadlock.
324
         */
327
         */
325
        asid_t asid;
328
        asid_t asid;
326
       
329
       
327
        asid = asid_get();
330
        asid = asid_get();
328
        spinlock_lock(&new->lock);
331
        spinlock_lock(&new->lock);
329
        new->asid = asid;
332
        new->asid = asid;
330
        spinlock_unlock(&new->lock);
333
        spinlock_unlock(&new->lock);
331
    }
334
    }
332
    spinlock_unlock(&as_lock);
335
    spinlock_unlock(&as_lock);
333
    interrupts_restore(ipl);
336
    interrupts_restore(ipl);
334
   
337
   
335
    /*
338
    /*
336
     * Perform architecture-specific steps.
339
     * Perform architecture-specific steps.
337
     * (e.g. write ASID to hardware register etc.)
340
     * (e.g. write ASID to hardware register etc.)
338
     */
341
     */
339
    as_install_arch(new);
342
    as_install_arch(new);
340
   
343
   
341
    AS = new;
344
    AS = new;
342
}
345
}
343
 
346
 
344
/** Compute flags for virtual address translation subsytem.
347
/** Compute flags for virtual address translation subsytem.
345
 *
348
 *
346
 * The address space area must be locked.
349
 * The address space area must be locked.
347
 * Interrupts must be disabled.
350
 * Interrupts must be disabled.
348
 *
351
 *
349
 * @param a Address space area.
352
 * @param a Address space area.
350
 *
353
 *
351
 * @return Flags to be used in page_mapping_insert().
354
 * @return Flags to be used in page_mapping_insert().
352
 */
355
 */
353
int get_area_flags(as_area_t *a)
356
int get_area_flags(as_area_t *a)
354
{
357
{
355
    int flags;
358
    int flags;
356
 
359
 
357
    flags = PAGE_USER | PAGE_PRESENT;
360
    flags = PAGE_USER | PAGE_PRESENT;
358
   
361
   
359
    if (a->flags & AS_AREA_READ)
362
    if (a->flags & AS_AREA_READ)
360
        flags |= PAGE_READ;
363
        flags |= PAGE_READ;
361
       
364
       
362
    if (a->flags & AS_AREA_WRITE)
365
    if (a->flags & AS_AREA_WRITE)
363
        flags |= PAGE_WRITE;
366
        flags |= PAGE_WRITE;
364
   
367
   
365
    if (a->flags & AS_AREA_EXEC)
368
    if (a->flags & AS_AREA_EXEC)
366
        flags |= PAGE_EXEC;
369
        flags |= PAGE_EXEC;
367
   
370
   
368
    if (!(a->flags & AS_AREA_DEVICE))
371
    if (!(a->flags & AS_AREA_DEVICE))
369
        flags |= PAGE_CACHEABLE;
372
        flags |= PAGE_CACHEABLE;
370
       
373
       
371
    return flags;
374
    return flags;
372
}
375
}
373
 
376
 
374
/** Create page table.
377
/** Create page table.
375
 *
378
 *
376
 * Depending on architecture, create either address space
379
 * Depending on architecture, create either address space
377
 * private or global page table.
380
 * private or global page table.
378
 *
381
 *
379
 * @param flags Flags saying whether the page table is for kernel address space.
382
 * @param flags Flags saying whether the page table is for kernel address space.
380
 *
383
 *
381
 * @return First entry of the page table.
384
 * @return First entry of the page table.
382
 */
385
 */
383
pte_t *page_table_create(int flags)
386
pte_t *page_table_create(int flags)
384
{
387
{
385
        ASSERT(as_operations);
388
        ASSERT(as_operations);
386
        ASSERT(as_operations->page_table_create);
389
        ASSERT(as_operations->page_table_create);
387
 
390
 
388
        return as_operations->page_table_create(flags);
391
        return as_operations->page_table_create(flags);
389
}
392
}
390
 
393
 
391
/** Lock page table.
394
/** Lock page table.
392
 *
395
 *
393
 * This function should be called before any page_mapping_insert(),
396
 * This function should be called before any page_mapping_insert(),
394
 * page_mapping_remove() and page_mapping_find().
397
 * page_mapping_remove() and page_mapping_find().
395
 *
398
 *
396
 * Locking order is such that address space areas must be locked
399
 * Locking order is such that address space areas must be locked
397
 * prior to this call. Address space can be locked prior to this
400
 * prior to this call. Address space can be locked prior to this
398
 * call in which case the lock argument is false.
401
 * call in which case the lock argument is false.
399
 *
402
 *
400
 * @param as Address space.
403
 * @param as Address space.
401
 * @param as_locked If false, do not attempt to lock as->lock.
404
 * @param as_locked If false, do not attempt to lock as->lock.
402
 */
405
 */
403
void page_table_lock(as_t *as, bool lock)
406
void page_table_lock(as_t *as, bool lock)
404
{
407
{
405
    ASSERT(as_operations);
408
    ASSERT(as_operations);
406
    ASSERT(as_operations->page_table_lock);
409
    ASSERT(as_operations->page_table_lock);
407
 
410
 
408
    as_operations->page_table_lock(as, lock);
411
    as_operations->page_table_lock(as, lock);
409
}
412
}
410
 
413
 
411
/** Unlock page table.
414
/** Unlock page table.
412
 *
415
 *
413
 * @param as Address space.
416
 * @param as Address space.
414
 * @param as_locked If false, do not attempt to unlock as->lock.
417
 * @param as_locked If false, do not attempt to unlock as->lock.
415
 */
418
 */
416
void page_table_unlock(as_t *as, bool unlock)
419
void page_table_unlock(as_t *as, bool unlock)
417
{
420
{
418
    ASSERT(as_operations);
421
    ASSERT(as_operations);
419
    ASSERT(as_operations->page_table_unlock);
422
    ASSERT(as_operations->page_table_unlock);
420
 
423
 
421
    as_operations->page_table_unlock(as, unlock);
424
    as_operations->page_table_unlock(as, unlock);
422
}
425
}
423
 
426
 
424
/** Find address space area and change it.
427
/** Find address space area and change it.
425
 *
428
 *
426
 * @param as Address space.
429
 * @param as Address space.
427
 * @param address Virtual address belonging to the area to be changed. Must be page-aligned.
430
 * @param address Virtual address belonging to the area to be changed. Must be page-aligned.
428
 * @param size New size of the virtual memory block starting at address.
431
 * @param size New size of the virtual memory block starting at address.
429
 * @param flags Flags influencing the remap operation. Currently unused.
432
 * @param flags Flags influencing the remap operation. Currently unused.
430
 *
433
 *
431
 * @return address on success, (__address) -1 otherwise.
434
 * @return address on success, (__address) -1 otherwise.
432
 */
435
 */
433
__address as_area_resize(as_t *as, __address address, size_t size, int flags)
436
__address as_area_resize(as_t *as, __address address, size_t size, int flags)
434
{
437
{
435
    as_area_t *area = NULL;
438
    as_area_t *area = NULL;
436
    ipl_t ipl;
439
    ipl_t ipl;
437
    size_t pages;
440
    size_t pages;
438
   
441
   
439
    ipl = interrupts_disable();
442
    ipl = interrupts_disable();
440
    spinlock_lock(&as->lock);
443
    spinlock_lock(&as->lock);
441
   
444
   
442
    /*
445
    /*
443
     * Locate the area.
446
     * Locate the area.
444
     */
447
     */
445
    area = find_area_and_lock(as, address);
448
    area = find_area_and_lock(as, address);
446
    if (!area) {
449
    if (!area) {
447
        spinlock_unlock(&as->lock);
450
        spinlock_unlock(&as->lock);
448
        interrupts_restore(ipl);
451
        interrupts_restore(ipl);
449
        return (__address) -1;
452
        return (__address) -1;
450
    }
453
    }
451
 
454
 
452
    if (area->flags & AS_AREA_DEVICE) {
455
    if (area->flags & AS_AREA_DEVICE) {
453
        /*
456
        /*
454
         * Remapping of address space areas associated
457
         * Remapping of address space areas associated
455
         * with memory mapped devices is not supported.
458
         * with memory mapped devices is not supported.
456
         */
459
         */
457
        spinlock_unlock(&area->lock);
460
        spinlock_unlock(&area->lock);
458
        spinlock_unlock(&as->lock);
461
        spinlock_unlock(&as->lock);
459
        interrupts_restore(ipl);
462
        interrupts_restore(ipl);
460
        return (__address) -1;
463
        return (__address) -1;
461
    }
464
    }
462
 
465
 
463
    pages = SIZE2FRAMES((address - area->base) + size);
466
    pages = SIZE2FRAMES((address - area->base) + size);
-
 
467
    if (!pages) {
-
 
468
        /*
-
 
469
         * Zero size address space areas are not allowed.
-
 
470
         */
-
 
471
        spinlock_unlock(&area->lock);
-
 
472
        spinlock_unlock(&as->lock);
-
 
473
        interrupts_restore(ipl);
-
 
474
        return (__address) -1;
-
 
475
    }
-
 
476
   
464
    if (pages < area->pages) {
477
    if (pages < area->pages) {
465
        int i;
478
        int i;
466
 
479
 
467
        /*
480
        /*
468
         * Shrinking the area.
481
         * Shrinking the area.
469
         * No need to check for overlaps.
482
         * No need to check for overlaps.
470
         */
483
         */
471
        for (i = pages; i < area->pages; i++) {
484
        for (i = pages; i < area->pages; i++) {
472
            pte_t *pte;
485
            pte_t *pte;
473
           
486
           
474
            /*
487
            /*
475
             * Releasing physical memory.
488
             * Releasing physical memory.
476
             * This depends on the fact that the memory was allocated using frame_alloc().
489
             * This depends on the fact that the memory was allocated using frame_alloc().
477
             */
490
             */
478
            page_table_lock(as, false);
491
            page_table_lock(as, false);
479
            pte = page_mapping_find(as, area->base + i*PAGE_SIZE);
492
            pte = page_mapping_find(as, area->base + i*PAGE_SIZE);
480
            if (pte && PTE_VALID(pte)) {
493
            if (pte && PTE_VALID(pte)) {
481
                __address frame;
494
                __address frame;
482
 
495
 
483
                ASSERT(PTE_PRESENT(pte));
496
                ASSERT(PTE_PRESENT(pte));
484
                frame = PTE_GET_FRAME(pte);
497
                frame = PTE_GET_FRAME(pte);
485
                page_mapping_remove(as, area->base + i*PAGE_SIZE);
498
                page_mapping_remove(as, area->base + i*PAGE_SIZE);
486
                page_table_unlock(as, false);
499
                page_table_unlock(as, false);
487
 
500
 
488
                frame_free(ADDR2PFN(frame));
501
                frame_free(ADDR2PFN(frame));
489
            } else {
502
            } else {
490
                page_table_unlock(as, false);
503
                page_table_unlock(as, false);
491
            }
504
            }
492
        }
505
        }
493
        /*
506
        /*
494
         * Invalidate TLB's.
507
         * Invalidate TLB's.
495
         */
508
         */
496
        tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
509
        tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
497
        tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
510
        tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
498
        tlb_shootdown_finalize();
511
        tlb_shootdown_finalize();
499
    } else {
512
    } else {
500
        /*
513
        /*
501
         * Growing the area.
514
         * Growing the area.
502
         * Check for overlaps with other address space areas.
515
         * Check for overlaps with other address space areas.
503
         */
516
         */
504
        if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) {
517
        if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) {
505
            spinlock_unlock(&area->lock);
518
            spinlock_unlock(&area->lock);
506
            spinlock_unlock(&as->lock);    
519
            spinlock_unlock(&as->lock);    
507
            interrupts_restore(ipl);
520
            interrupts_restore(ipl);
508
            return (__address) -1;
521
            return (__address) -1;
509
        }
522
        }
510
    }
523
    }
511
 
524
 
512
    area->pages = pages;
525
    area->pages = pages;
513
   
526
   
514
    spinlock_unlock(&area->lock);
527
    spinlock_unlock(&area->lock);
515
    spinlock_unlock(&as->lock);
528
    spinlock_unlock(&as->lock);
516
    interrupts_restore(ipl);
529
    interrupts_restore(ipl);
517
 
530
 
518
    return address;
531
    return address;
519
}
532
}
520
 
533
 
521
/** Find address space area and lock it.
534
/** Find address space area and lock it.
522
 *
535
 *
523
 * The address space must be locked and interrupts must be disabled.
536
 * The address space must be locked and interrupts must be disabled.
524
 *
537
 *
525
 * @param as Address space.
538
 * @param as Address space.
526
 * @param va Virtual address.
539
 * @param va Virtual address.
527
 *
540
 *
528
 * @return Locked address space area containing va on success or NULL on failure.
541
 * @return Locked address space area containing va on success or NULL on failure.
529
 */
542
 */
530
as_area_t *find_area_and_lock(as_t *as, __address va)
543
as_area_t *find_area_and_lock(as_t *as, __address va)
531
{
544
{
532
    as_area_t *a;
545
    as_area_t *a;
533
    btree_node_t *leaf, *lnode;
546
    btree_node_t *leaf, *lnode;
534
    int i;
547
    int i;
535
   
548
   
536
    a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
549
    a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
537
    if (a) {
550
    if (a) {
538
        /* va is the base address of an address space area */
551
        /* va is the base address of an address space area */
539
        spinlock_lock(&a->lock);
552
        spinlock_lock(&a->lock);
540
        return a;
553
        return a;
541
    }
554
    }
542
   
555
   
543
    /*
556
    /*
544
     * Search the leaf node and the righmost record of its left neighbour
557
     * Search the leaf node and the righmost record of its left neighbour
545
     * to find out whether this is a miss or va belongs to an address
558
     * to find out whether this is a miss or va belongs to an address
546
     * space area found there.
559
     * space area found there.
547
     */
560
     */
548
   
561
   
549
    /* First, search the leaf node itself. */
562
    /* First, search the leaf node itself. */
550
    for (i = 0; i < leaf->keys; i++) {
563
    for (i = 0; i < leaf->keys; i++) {
551
        a = (as_area_t *) leaf->value[i];
564
        a = (as_area_t *) leaf->value[i];
552
        spinlock_lock(&a->lock);
565
        spinlock_lock(&a->lock);
553
        if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) {
566
        if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) {
554
            return a;
567
            return a;
555
        }
568
        }
556
        spinlock_unlock(&a->lock);
569
        spinlock_unlock(&a->lock);
557
    }
570
    }
558
 
571
 
559
    /*
572
    /*
560
     * Second, locate the left neighbour and test its last record.
573
     * Second, locate the left neighbour and test its last record.
561
     * Because of its position in the B+tree, it must have base < va.
574
     * Because of its position in the B+tree, it must have base < va.
562
     */
575
     */
563
    if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
576
    if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
564
        a = (as_area_t *) lnode->value[lnode->keys - 1];
577
        a = (as_area_t *) lnode->value[lnode->keys - 1];
565
        spinlock_lock(&a->lock);
578
        spinlock_lock(&a->lock);
566
        if (va < a->base + a->pages * PAGE_SIZE) {
579
        if (va < a->base + a->pages * PAGE_SIZE) {
567
            return a;
580
            return a;
568
        }
581
        }
569
        spinlock_unlock(&a->lock);
582
        spinlock_unlock(&a->lock);
570
    }
583
    }
571
 
584
 
572
    return NULL;
585
    return NULL;
573
}
586
}
574
 
587
 
575
/** Check area conflicts with other areas.
588
/** Check area conflicts with other areas.
576
 *
589
 *
577
 * The address space must be locked and interrupts must be disabled.
590
 * The address space must be locked and interrupts must be disabled.
578
 *
591
 *
579
 * @param as Address space.
592
 * @param as Address space.
580
 * @param va Starting virtual address of the area being tested.
593
 * @param va Starting virtual address of the area being tested.
581
 * @param size Size of the area being tested.
594
 * @param size Size of the area being tested.
582
 * @param avoid_area Do not touch this area.
595
 * @param avoid_area Do not touch this area.
583
 *
596
 *
584
 * @return True if there is no conflict, false otherwise.
597
 * @return True if there is no conflict, false otherwise.
585
 */
598
 */
586
bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area)
599
bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area)
587
{
600
{
588
    as_area_t *a;
601
    as_area_t *a;
589
    btree_node_t *leaf, *node;
602
    btree_node_t *leaf, *node;
590
    int i;
603
    int i;
591
   
604
   
592
    /*
605
    /*
593
     * We don't want any area to have conflicts with NULL page.
606
     * We don't want any area to have conflicts with NULL page.
594
     */
607
     */
595
    if (overlaps(va, size, NULL, PAGE_SIZE))
608
    if (overlaps(va, size, NULL, PAGE_SIZE))
596
        return false;
609
        return false;
597
   
610
   
598
    /*
611
    /*
599
     * The leaf node is found in O(log n), where n is proportional to
612
     * The leaf node is found in O(log n), where n is proportional to
600
     * the number of address space areas belonging to as.
613
     * the number of address space areas belonging to as.
601
     * The check for conflicts is then attempted on the rightmost
614
     * The check for conflicts is then attempted on the rightmost
602
     * record in the left neighbour, the leftmost record in the right
615
     * record in the left neighbour, the leftmost record in the right
603
     * neighbour and all records in the leaf node itself.
616
     * neighbour and all records in the leaf node itself.
604
     */
617
     */
605
   
618
   
606
    if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) {
619
    if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) {
607
        if (a != avoid_area)
620
        if (a != avoid_area)
608
            return false;
621
            return false;
609
    }
622
    }
610
   
623
   
611
    /* First, check the two border cases. */
624
    /* First, check the two border cases. */
612
    if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
625
    if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
613
        a = (as_area_t *) node->value[node->keys - 1];
626
        a = (as_area_t *) node->value[node->keys - 1];
614
        spinlock_lock(&a->lock);
627
        spinlock_lock(&a->lock);
615
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
628
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
616
            spinlock_unlock(&a->lock);
629
            spinlock_unlock(&a->lock);
617
            return false;
630
            return false;
618
        }
631
        }
619
        spinlock_unlock(&a->lock);
632
        spinlock_unlock(&a->lock);
620
    }
633
    }
621
    if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) {
634
    if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) {
622
        a = (as_area_t *) node->value[0];
635
        a = (as_area_t *) node->value[0];
623
        spinlock_lock(&a->lock);
636
        spinlock_lock(&a->lock);
624
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
637
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
625
            spinlock_unlock(&a->lock);
638
            spinlock_unlock(&a->lock);
626
            return false;
639
            return false;
627
        }
640
        }
628
        spinlock_unlock(&a->lock);
641
        spinlock_unlock(&a->lock);
629
    }
642
    }
630
   
643
   
631
    /* Second, check the leaf node. */
644
    /* Second, check the leaf node. */
632
    for (i = 0; i < leaf->keys; i++) {
645
    for (i = 0; i < leaf->keys; i++) {
633
        a = (as_area_t *) leaf->value[i];
646
        a = (as_area_t *) leaf->value[i];
634
   
647
   
635
        if (a == avoid_area)
648
        if (a == avoid_area)
636
            continue;
649
            continue;
637
   
650
   
638
        spinlock_lock(&a->lock);
651
        spinlock_lock(&a->lock);
639
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
652
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
640
            spinlock_unlock(&a->lock);
653
            spinlock_unlock(&a->lock);
641
            return false;
654
            return false;
642
        }
655
        }
643
        spinlock_unlock(&a->lock);
656
        spinlock_unlock(&a->lock);
644
    }
657
    }
645
 
658
 
646
    /*
659
    /*
647
     * So far, the area does not conflict with other areas.
660
     * So far, the area does not conflict with other areas.
648
     * Check if it doesn't conflict with kernel address space.
661
     * Check if it doesn't conflict with kernel address space.
649
     */  
662
     */  
650
    if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
663
    if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
651
        return !overlaps(va, size,
664
        return !overlaps(va, size,
652
            KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START);
665
            KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START);
653
    }
666
    }
654
 
667
 
655
    return true;
668
    return true;
656
}
669
}
657
 
670