Subversion Repositories HelenOS-historic

Rev

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

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