Subversion Repositories HelenOS-historic

Rev

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

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