Subversion Repositories HelenOS-historic

Rev

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

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