Subversion Repositories HelenOS-historic

Rev

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

Rev 778 Rev 786
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/tlb.h>
39
#include <mm/tlb.h>
40
#include <mm/heap.h>
40
#include <mm/heap.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 <mm/asid.h>
43
#include <mm/asid.h>
44
#include <arch/mm/asid.h>
44
#include <arch/mm/asid.h>
45
#include <arch/types.h>
45
#include <arch/types.h>
46
#include <typedefs.h>
46
#include <typedefs.h>
47
#include <synch/spinlock.h>
47
#include <synch/spinlock.h>
48
#include <config.h>
48
#include <config.h>
49
#include <list.h>
49
#include <list.h>
50
#include <panic.h>
50
#include <panic.h>
51
#include <arch/asm.h>
51
#include <arch/asm.h>
52
#include <debug.h>
52
#include <debug.h>
53
#include <memstr.h>
53
#include <memstr.h>
54
#include <arch.h>
54
#include <arch.h>
55
#include <print.h>
55
#include <print.h>
56
 
56
 
57
as_operations_t *as_operations = NULL;
57
as_operations_t *as_operations = NULL;
58
 
58
 
59
/** Kernel address space. */
59
/** Kernel address space. */
60
as_t *AS_KERNEL = NULL;
60
as_t *AS_KERNEL = NULL;
61
 
61
 
62
static int get_area_flags(as_area_t *a);
62
static int get_area_flags(as_area_t *a);
63
 
63
 
64
/** Initialize address space subsystem. */
64
/** Initialize address space subsystem. */
65
void as_init(void)
65
void as_init(void)
66
{
66
{
67
    as_arch_init();
67
    as_arch_init();
68
    AS_KERNEL = as_create(FLAG_AS_KERNEL | FLAG_AS_EARLYMALLOC);
68
    AS_KERNEL = as_create(FLAG_AS_KERNEL | FLAG_AS_EARLYMALLOC);
69
        if (!AS_KERNEL)
69
        if (!AS_KERNEL)
70
                panic("can't create kernel address space\n");
70
                panic("can't create kernel address space\n");
71
}
71
}
72
 
72
 
73
/** Create address space.
73
/** Create address space.
74
 *
74
 *
75
 * @param flags Flags that influence way in wich the address space is created.
75
 * @param flags Flags that influence way in wich the address space is created.
76
 */
76
 */
77
as_t *as_create(int flags)
77
as_t *as_create(int flags)
78
{
78
{
79
    as_t *as;
79
    as_t *as;
80
 
80
 
81
    if (flags & FLAG_AS_EARLYMALLOC)
81
    if (flags & FLAG_AS_EARLYMALLOC)
82
        as = (as_t *) early_malloc(sizeof(as_t));
82
        as = (as_t *) early_malloc(sizeof(as_t));
83
    else
83
    else
84
        as = (as_t *) malloc(sizeof(as_t));
84
        as = (as_t *) malloc(sizeof(as_t));
85
    if (as) {
85
    if (as) {
86
        list_initialize(&as->as_with_asid_link);
86
        list_initialize(&as->as_with_asid_link);
87
        spinlock_initialize(&as->lock, "as_lock");
87
        spinlock_initialize(&as->lock, "as_lock");
88
        list_initialize(&as->as_area_head);
88
        list_initialize(&as->as_area_head);
89
 
89
 
90
        if (flags & FLAG_AS_KERNEL)
90
        if (flags & FLAG_AS_KERNEL)
91
            as->asid = ASID_KERNEL;
91
            as->asid = ASID_KERNEL;
92
        else
92
        else
93
            as->asid = ASID_INVALID;
93
            as->asid = ASID_INVALID;
94
 
94
 
95
        as->page_table = page_table_create(flags);
95
        as->page_table = page_table_create(flags);
96
    }
96
    }
97
 
97
 
98
    return as;
98
    return as;
99
}
99
}
100
 
100
 
101
/** Create address space area of common attributes.
101
/** Create address space area of common attributes.
102
 *
102
 *
103
 * The created address space area is added to the target address space.
103
 * The created address space area is added to the target address space.
104
 *
104
 *
105
 * @param as Target address space.
105
 * @param as Target address space.
106
 * @param type Type of area.
106
 * @param type Type of area.
107
 * @param size Size of area in multiples of PAGE_SIZE.
107
 * @param size Size of area in multiples of PAGE_SIZE.
108
 * @param base Base address of area.
108
 * @param base Base address of area.
109
 *
109
 *
110
 * @return Address space area on success or NULL on failure.
110
 * @return Address space area on success or NULL on failure.
111
 */
111
 */
112
as_area_t *as_area_create(as_t *as, as_area_type_t type, size_t size, __address base)
112
as_area_t *as_area_create(as_t *as, as_area_type_t type, size_t size, __address base)
113
{
113
{
114
    ipl_t ipl;
114
    ipl_t ipl;
115
    as_area_t *a;
115
    as_area_t *a;
116
   
116
   
117
    if (base % PAGE_SIZE)
117
    if (base % PAGE_SIZE)
118
        panic("addr not aligned to a page boundary");
118
        panic("addr not aligned to a page boundary");
119
   
119
   
120
    ipl = interrupts_disable();
120
    ipl = interrupts_disable();
121
    spinlock_lock(&as->lock);
121
    spinlock_lock(&as->lock);
122
   
122
   
123
    /*
123
    /*
124
     * TODO: test as_area which is to be created doesn't overlap with an existing one.
124
     * TODO: test as_area which is to be created doesn't overlap with an existing one.
125
     */
125
     */
126
   
126
   
127
    a = (as_area_t *) malloc(sizeof(as_area_t));
127
    a = (as_area_t *) malloc(sizeof(as_area_t));
128
    if (a) {   
128
    if (a) {   
129
        spinlock_initialize(&a->lock, "as_area_lock");
129
        spinlock_initialize(&a->lock, "as_area_lock");
130
           
130
           
131
        link_initialize(&a->link);         
131
        link_initialize(&a->link);         
132
        a->type = type;
132
        a->type = type;
133
        a->size = size;
133
        a->size = size;
134
        a->base = base;
134
        a->base = base;
135
       
135
       
136
        list_append(&a->link, &as->as_area_head);
136
        list_append(&a->link, &as->as_area_head);
137
    }
137
    }
138
 
138
 
139
    spinlock_unlock(&as->lock);
139
    spinlock_unlock(&as->lock);
140
    interrupts_restore(ipl);
140
    interrupts_restore(ipl);
141
 
141
 
142
    return a;
142
    return a;
143
}
143
}
144
 
144
 
145
/** Initialize mapping for one page of address space.
145
/** Initialize mapping for one page of address space.
146
 *
146
 *
147
 * This functions maps 'page' to 'frame' according
147
 * This functions maps 'page' to 'frame' according
148
 * to attributes of the address space area to
148
 * to attributes of the address space area to
149
 * wich 'page' belongs.
149
 * wich 'page' belongs.
150
 *
150
 *
151
 * @param a Target address space.
151
 * @param a Target address space.
152
 * @param page Virtual page within the area.
152
 * @param page Virtual page within the area.
153
 * @param frame Physical frame to which page will be mapped.
153
 * @param frame Physical frame to which page will be mapped.
154
 */
154
 */
155
void as_set_mapping(as_t *as, __address page, __address frame)
155
void as_set_mapping(as_t *as, __address page, __address frame)
156
{
156
{
157
    as_area_t *a, *area = NULL;
157
    as_area_t *a, *area = NULL;
158
    link_t *cur;
158
    link_t *cur;
159
    ipl_t ipl;
159
    ipl_t ipl;
160
   
160
   
161
    ipl = interrupts_disable();
161
    ipl = interrupts_disable();
162
    spinlock_lock(&as->lock);
162
    spinlock_lock(&as->lock);
163
   
163
   
164
    /*
164
    /*
165
     * First, try locate an area.
165
     * First, try locate an area.
166
     */
166
     */
167
    for (cur = as->as_area_head.next; cur != &as->as_area_head; cur = cur->next) {
167
    for (cur = as->as_area_head.next; cur != &as->as_area_head; cur = cur->next) {
168
        a = list_get_instance(cur, as_area_t, link);
168
        a = list_get_instance(cur, as_area_t, link);
169
        spinlock_lock(&a->lock);
169
        spinlock_lock(&a->lock);
170
 
170
 
171
        if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) {
171
        if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) {
172
            area = a;
172
            area = a;
173
            break;
173
            break;
174
        }
174
        }
175
       
175
       
176
        spinlock_unlock(&a->lock);
176
        spinlock_unlock(&a->lock);
177
    }
177
    }
178
   
178
   
179
    if (!area) {
179
    if (!area) {
180
        panic("page not part of any as_area\n");
180
        panic("page not part of any as_area\n");
181
    }
181
    }
182
 
182
 
183
    /*
183
    /*
184
     * Note: area->lock is held.
184
     * Note: area->lock is held.
185
     */
185
     */
186
   
186
   
187
    page_mapping_insert(as, page, frame, get_area_flags(area));
187
    page_mapping_insert(as, page, frame, get_area_flags(area));
188
   
188
   
189
    spinlock_unlock(&area->lock);
189
    spinlock_unlock(&area->lock);
190
    spinlock_unlock(&as->lock);
190
    spinlock_unlock(&as->lock);
191
    interrupts_restore(ipl);
191
    interrupts_restore(ipl);
192
}
192
}
193
 
193
 
194
/** Handle page fault within the current address space.
194
/** Handle page fault within the current address space.
195
 *
195
 *
196
 * This is the high-level page fault handler.
196
 * This is the high-level page fault handler.
197
 * Interrupts are assumed disabled.
197
 * Interrupts are assumed disabled.
198
 *
198
 *
199
 * @param page Faulting page.
199
 * @param page Faulting page.
200
 *
200
 *
201
 * @return 0 on page fault, 1 on success.
201
 * @return 0 on page fault, 1 on success.
202
 */
202
 */
203
int as_page_fault(__address page)
203
int as_page_fault(__address page)
204
{
204
{
205
    link_t *cur;
205
    link_t *cur;
206
    as_area_t *a, *area = NULL;
206
    as_area_t *a, *area = NULL;
207
    __address frame;
207
    __address frame;
208
   
208
   
209
    ASSERT(AS);
209
    ASSERT(AS);
210
    spinlock_lock(&AS->lock);
210
    spinlock_lock(&AS->lock);
211
   
211
   
212
    /*
212
    /*
213
     * Search this areas of this address space for presence of 'page'.
213
     * Search this areas of this address space for presence of 'page'.
214
     */
214
     */
215
    for (cur = AS->as_area_head.next; cur != &AS->as_area_head; cur = cur->next) {
215
    for (cur = AS->as_area_head.next; cur != &AS->as_area_head; cur = cur->next) {
216
        a = list_get_instance(cur, as_area_t, link);
216
        a = list_get_instance(cur, as_area_t, link);
217
        spinlock_lock(&a->lock);
217
        spinlock_lock(&a->lock);
218
 
218
 
219
        if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) {
219
        if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) {
220
 
220
 
221
            /*
221
            /*
222
             * We found the area containing 'page'.
222
             * We found the area containing 'page'.
223
             * TODO: access checking
223
             * TODO: access checking
224
             */
224
             */
225
            area = a;
225
            area = a;
226
            break;
226
            break;
227
        }
227
        }
228
       
228
       
229
        spinlock_unlock(&a->lock);
229
        spinlock_unlock(&a->lock);
230
    }
230
    }
231
   
231
   
232
    if (!area) {
232
    if (!area) {
233
        /*
233
        /*
234
         * No area contained mapping for 'page'.
234
         * No area contained mapping for 'page'.
235
         * Signal page fault to low-level handler.
235
         * Signal page fault to low-level handler.
236
         */
236
         */
237
        spinlock_unlock(&AS->lock);
237
        spinlock_unlock(&AS->lock);
238
        return 0;
238
        return 0;
239
    }
239
    }
240
 
240
 
241
    /*
241
    /*
242
     * Note: area->lock is held.
242
     * Note: area->lock is held.
243
     */
243
     */
244
   
244
   
245
    /*
245
    /*
246
     * In general, there can be several reasons that
246
     * In general, there can be several reasons that
247
     * can have caused this fault.
247
     * can have caused this fault.
248
     *
248
     *
249
     * - non-existent mapping: the area is a scratch
249
     * - non-existent mapping: the area is a scratch
250
     *   area (e.g. stack) and so far has not been
250
     *   area (e.g. stack) and so far has not been
251
     *   allocated a frame for the faulting page
251
     *   allocated a frame for the faulting page
252
     *
252
     *
253
     * - non-present mapping: another possibility,
253
     * - non-present mapping: another possibility,
254
     *   currently not implemented, would be frame
254
     *   currently not implemented, would be frame
255
     *   reuse; when this becomes a possibility,
255
     *   reuse; when this becomes a possibility,
256
     *   do not forget to distinguish between
256
     *   do not forget to distinguish between
257
     *   the different causes
257
     *   the different causes
258
     */
258
     */
259
    frame = frame_alloc(0, ONE_FRAME, NULL, NULL);
259
    frame = frame_alloc(ONE_FRAME, 0);
260
    memsetb(PA2KA(frame), FRAME_SIZE, 0);
260
    memsetb(PA2KA(frame), FRAME_SIZE, 0);
261
   
261
   
262
    /*
262
    /*
263
     * Map 'page' to 'frame'.
263
     * Map 'page' to 'frame'.
264
     * Note that TLB shootdown is not attempted as only new information is being
264
     * Note that TLB shootdown is not attempted as only new information is being
265
     * inserted into page tables.
265
     * inserted into page tables.
266
     */
266
     */
267
    page_mapping_insert(AS, page, frame, get_area_flags(area));
267
    page_mapping_insert(AS, page, frame, get_area_flags(area));
268
   
268
   
269
    spinlock_unlock(&area->lock);
269
    spinlock_unlock(&area->lock);
270
    spinlock_unlock(&AS->lock);
270
    spinlock_unlock(&AS->lock);
271
 
271
 
272
    return 1;
272
    return 1;
273
}
273
}
274
 
274
 
275
/** Install address space on CPU.
275
/** Install address space on CPU.
276
 *
276
 *
277
 * @param as Address space.
277
 * @param as Address space.
278
 */
278
 */
279
void as_install(as_t *as)
279
void as_install(as_t *as)
280
{
280
{
281
    ipl_t ipl;
281
    ipl_t ipl;
282
   
282
   
283
    asid_install(as);
283
    asid_install(as);
284
   
284
   
285
    ipl = interrupts_disable();
285
    ipl = interrupts_disable();
286
    spinlock_lock(&as->lock);
286
    spinlock_lock(&as->lock);
287
    ASSERT(as->page_table);
287
    ASSERT(as->page_table);
288
    SET_PTL0_ADDRESS(as->page_table);
288
    SET_PTL0_ADDRESS(as->page_table);
289
    spinlock_unlock(&as->lock);
289
    spinlock_unlock(&as->lock);
290
    interrupts_restore(ipl);
290
    interrupts_restore(ipl);
291
 
291
 
292
    /*
292
    /*
293
     * Perform architecture-specific steps.
293
     * Perform architecture-specific steps.
294
     * (e.g. write ASID to hardware register etc.)
294
     * (e.g. write ASID to hardware register etc.)
295
     */
295
     */
296
    as_install_arch(as);
296
    as_install_arch(as);
297
   
297
   
298
    AS = as;
298
    AS = as;
299
}
299
}
300
 
300
 
301
/** Compute flags for virtual address translation subsytem.
301
/** Compute flags for virtual address translation subsytem.
302
 *
302
 *
303
 * The address space area must be locked.
303
 * The address space area must be locked.
304
 * Interrupts must be disabled.
304
 * Interrupts must be disabled.
305
 *
305
 *
306
 * @param a Address space area.
306
 * @param a Address space area.
307
 *
307
 *
308
 * @return Flags to be used in page_mapping_insert().
308
 * @return Flags to be used in page_mapping_insert().
309
 */
309
 */
310
int get_area_flags(as_area_t *a)
310
int get_area_flags(as_area_t *a)
311
{
311
{
312
    int flags;
312
    int flags;
313
 
313
 
314
    switch (a->type) {
314
    switch (a->type) {
315
        case AS_AREA_TEXT:
315
        case AS_AREA_TEXT:
316
            flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
316
            flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
317
            break;
317
            break;
318
        case AS_AREA_DATA:
318
        case AS_AREA_DATA:
319
        case AS_AREA_STACK:
319
        case AS_AREA_STACK:
320
            flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
320
            flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
321
            break;
321
            break;
322
        default:
322
        default:
323
            panic("unexpected as_area_type_t %d", a->type);
323
            panic("unexpected as_area_type_t %d", a->type);
324
    }
324
    }
325
   
325
   
326
    return flags;
326
    return flags;
327
}
327
}
328
 
328
 
329
/** Create page table.
329
/** Create page table.
330
 *
330
 *
331
 * Depending on architecture, create either address space
331
 * Depending on architecture, create either address space
332
 * private or global page table.
332
 * private or global page table.
333
 *
333
 *
334
 * @param flags Flags saying whether the page table is for kernel address space.
334
 * @param flags Flags saying whether the page table is for kernel address space.
335
 *
335
 *
336
 * @return First entry of the page table.
336
 * @return First entry of the page table.
337
 */
337
 */
338
pte_t *page_table_create(int flags)
338
pte_t *page_table_create(int flags)
339
{
339
{
340
        ASSERT(as_operations);
340
        ASSERT(as_operations);
341
        ASSERT(as_operations->page_table_create);
341
        ASSERT(as_operations->page_table_create);
342
 
342
 
343
        return as_operations->page_table_create(flags);
343
        return as_operations->page_table_create(flags);
344
}
344
}
345
 
345