Subversion Repositories HelenOS-historic

Rev

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

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