Subversion Repositories HelenOS-historic

Rev

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

Rev 1374 Rev 1378
1
/*
1
/*
2
 * Copyright (C) 2005 Martin Decky
2
 * Copyright (C) 2005 Martin Decky
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
#include <arch/mm/page.h>
29
#include <arch/mm/page.h>
30
#include <genarch/mm/page_pt.h>
30
#include <genarch/mm/page_pt.h>
31
#include <arch/mm/frame.h>
31
#include <arch/mm/frame.h>
32
#include <arch/asm.h>
32
#include <arch/asm.h>
33
#include <mm/frame.h>
33
#include <mm/frame.h>
34
#include <mm/page.h>
34
#include <mm/page.h>
35
#include <mm/as.h>
35
#include <mm/as.h>
36
#include <arch.h>
36
#include <arch.h>
37
#include <arch/types.h>
37
#include <arch/types.h>
38
#include <arch/exception.h>
38
#include <arch/exception.h>
39
#include <config.h>
39
#include <config.h>
40
#include <print.h>
40
#include <print.h>
41
#include <symtab.h>
41
#include <symtab.h>
42
 
42
 
43
static phte_t *phte;
43
static phte_t *phte;
44
 
44
 
45
 
45
 
46
/** Try to find PTE for faulting address
46
/** Try to find PTE for faulting address
47
 *
47
 *
48
 * Try to find PTE for faulting address.
48
 * Try to find PTE for faulting address.
49
 * The AS->lock must be held on entry to this function.
49
 * The AS->lock must be held on entry to this function.
50
 *
50
 *
51
 * @param badvaddr Faulting virtual address.
51
 * @param badvaddr Faulting virtual address.
52
 * @param istate Pointer to interrupted state.
52
 * @param istate   Pointer to interrupted state.
53
 * @param pfrc Pointer to variable where as_page_fault() return code will be stored.
53
 * @param pfrc     Pointer to variable where as_page_fault() return code will be stored.
54
 * @return         PTE on success, NULL otherwise.
54
 * @return         PTE on success, NULL otherwise.
55
 *
55
 *
56
 */
56
 */
57
static pte_t *find_mapping_and_check(__address badvaddr, istate_t *istate, int *pfcr)
57
static pte_t *find_mapping_and_check(__address badvaddr, istate_t *istate, int *pfcr)
58
{
58
{
59
    /*
59
    /*
60
     * Check if the mapping exists in page tables.
60
     * Check if the mapping exists in page tables.
61
     */
61
     */
62
    pte_t *pte = page_mapping_find(AS, badvaddr);
62
    pte_t *pte = page_mapping_find(AS, badvaddr);
63
    if ((pte) && (pte->p)) {
63
    if ((pte) && (pte->p)) {
64
        /*
64
        /*
65
         * Mapping found in page tables.
65
         * Mapping found in page tables.
66
         * Immediately succeed.
66
         * Immediately succeed.
67
         */
67
         */
68
        return pte;
68
        return pte;
69
    } else {
69
    } else {
70
        int rc;
70
        int rc;
71
   
71
   
72
        /*
72
        /*
73
         * Mapping not found in page tables.
73
         * Mapping not found in page tables.
74
         * Resort to higher-level page fault handler.
74
         * Resort to higher-level page fault handler.
75
         */
75
         */
76
        page_table_unlock(AS, true);
76
        page_table_unlock(AS, true);
77
        switch (rc = as_page_fault(badvaddr, istate)) {
77
        switch (rc = as_page_fault(badvaddr, istate)) {
78
            case AS_PF_OK:
78
            case AS_PF_OK:
79
                /*
79
                /*
80
                 * The higher-level page fault handler succeeded,
80
                 * The higher-level page fault handler succeeded,
81
                 * The mapping ought to be in place.
81
                 * The mapping ought to be in place.
82
                 */
82
                 */
83
                page_table_lock(AS, true);
83
                page_table_lock(AS, true);
84
                pte = page_mapping_find(AS, badvaddr);
84
                pte = page_mapping_find(AS, badvaddr);
85
                ASSERT((pte) && (pte->p));
85
                ASSERT((pte) && (pte->p));
86
                return pte;
86
                return pte;
87
            case AS_PF_DEFER:
87
            case AS_PF_DEFER:
88
                page_table_lock(AS, true);
88
                page_table_lock(AS, true);
89
                *pfcr = rc;
89
                *pfcr = rc;
90
                return NULL;
90
                return NULL;
91
            case AS_PF_FAULT:
91
            case AS_PF_FAULT:
92
                page_table_lock(AS, true);
92
                page_table_lock(AS, true);
93
                printf("Page fault.\n");
93
                printf("Page fault.\n");
94
                *pfcr = rc;
94
                *pfcr = rc;
95
                return NULL;
95
                return NULL;
96
            default:
96
            default:
97
                panic("unexpected rc (%d)\n", rc);
97
                panic("unexpected rc (%d)\n", rc);
98
        }  
98
        }  
99
    }
99
    }
100
}
100
}
101
 
101
 
102
 
102
 
103
static void pht_refill_fail(__address badvaddr, istate_t *istate)
103
static void pht_refill_fail(__address badvaddr, istate_t *istate)
104
{
104
{
105
    char *symbol = "";
105
    char *symbol = "";
106
    char *sym2 = "";
106
    char *sym2 = "";
107
 
107
 
108
    char *s = get_symtab_entry(istate->pc);
108
    char *s = get_symtab_entry(istate->pc);
109
    if (s)
109
    if (s)
110
        symbol = s;
110
        symbol = s;
111
    s = get_symtab_entry(istate->lr);
111
    s = get_symtab_entry(istate->lr);
112
    if (s)
112
    if (s)
113
        sym2 = s;
113
        sym2 = s;
114
    panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr, istate->pc, symbol, sym2);
114
    panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr, istate->pc, symbol, sym2);
115
}
115
}
116
 
116
 
-
 
117
 
117
static void pht_insert(const __address vaddr, const pfn_t pfn)
118
static void pht_insert(const __address vaddr, const pfn_t pfn)
118
{
119
{
119
    __u32 page = (vaddr >> 12) & 0xffff;
120
    __u32 page = (vaddr >> 12) & 0xffff;
120
    __u32 api = (vaddr >> 22) & 0x3f;
121
    __u32 api = (vaddr >> 22) & 0x3f;
121
    __u32 vsid;
122
    __u32 vsid;
122
   
123
   
123
    asm volatile (
124
    asm volatile (
124
        "mfsrin %0, %1\n"
125
        "mfsrin %0, %1\n"
125
        : "=r" (vsid)
126
        : "=r" (vsid)
126
        : "r" (vaddr)
127
        : "r" (vaddr)
127
    );
128
    );
128
   
129
   
129
    /* Primary hash (xor) */
130
    /* Primary hash (xor) */
130
    __u32 hash = ((vsid ^ page) & 0x3ff) << 3;
131
    __u32 hash = ((vsid ^ page) & 0x3ff) << 3;
131
   
132
   
132
    __u32 i;
133
    __u32 i;
-
 
134
    bool found = false;
133
    /* Find unused PTE in PTEG */
135
    /* Find unused PTE in PTEG */
134
    for (i = 0; i < 8; i++) {
136
    for (i = 0; i < 8; i++) {
135
        if (!phte[hash + i].v)
137
        if (!phte[hash + i].v) {
-
 
138
            found = true;
136
            break;
139
            break;
-
 
140
        }
137
    }
141
    }
138
   
142
   
-
 
143
    if (!found) {
139
    // TODO: Check access/change bits, secondary hash
144
        /* Secondary hash (not) */
-
 
145
        hash = ~hash;
-
 
146
       
-
 
147
        /* Find unused PTE in PTEG */
-
 
148
        for (i = 0; i < 8; i++) {
-
 
149
            if (!phte[hash + i].v) {
-
 
150
                found = true;
-
 
151
                break;
-
 
152
            }
-
 
153
        }
140
   
154
       
141
    if (i == 8)
155
        if (!found) {
-
 
156
            // TODO: A/C precedence groups
142
        i = page % 8;
157
            i = page % 8;
-
 
158
        }
-
 
159
    }
143
   
160
   
144
    phte[hash + i].v = 1;
161
    phte[hash + i].v = 1;
145
    phte[hash + i].vsid = vsid;
162
    phte[hash + i].vsid = vsid;
146
    phte[hash + i].h = 0;
163
    phte[hash + i].h = 0;
147
    phte[hash + i].api = api;
164
    phte[hash + i].api = api;
148
    phte[hash + i].rpn = pfn;
165
    phte[hash + i].rpn = pfn;
149
    phte[hash + i].r = 0;
166
    phte[hash + i].r = 0;
150
    phte[hash + i].c = 0;
167
    phte[hash + i].c = 0;
151
    phte[hash + i].pp = 2; // FIXME
168
    phte[hash + i].pp = 2; // FIXME
152
}
169
}
153
 
170
 
154
 
171
 
155
/** Process Instruction/Data Storage Interrupt
172
/** Process Instruction/Data Storage Interrupt
156
 *
173
 *
157
 * @param data   True if Data Storage Interrupt.
174
 * @param data   True if Data Storage Interrupt.
158
 * @param istate Interrupted register context.
175
 * @param istate Interrupted register context.
159
 *
176
 *
160
 */
177
 */
161
void pht_refill(bool data, istate_t *istate)
178
void pht_refill(bool data, istate_t *istate)
162
{
179
{
163
    asid_t asid;
180
    asid_t asid;
164
    __address badvaddr;
181
    __address badvaddr;
165
    pte_t *pte;
182
    pte_t *pte;
166
   
183
   
167
    int pfcr;
184
    int pfcr;
168
   
185
   
169
    if (data) {
186
    if (data) {
170
        asm volatile (
187
        asm volatile (
171
            "mfdar %0\n"
188
            "mfdar %0\n"
172
            : "=r" (badvaddr)
189
            : "=r" (badvaddr)
173
        );
190
        );
174
    } else
191
    } else
175
        badvaddr = istate->pc;
192
        badvaddr = istate->pc;
176
       
193
       
177
    spinlock_lock(&AS->lock);
194
    spinlock_lock(&AS->lock);
178
    asid = AS->asid;
195
    asid = AS->asid;
179
    spinlock_unlock(&AS->lock);
196
    spinlock_unlock(&AS->lock);
180
   
197
   
181
    page_table_lock(AS, true);
198
    page_table_lock(AS, true);
182
   
199
   
183
    pte = find_mapping_and_check(badvaddr, istate, &pfcr);
200
    pte = find_mapping_and_check(badvaddr, istate, &pfcr);
184
    if (!pte) {
201
    if (!pte) {
185
        switch (pfcr) {
202
        switch (pfcr) {
186
        case AS_PF_FAULT:
203
            case AS_PF_FAULT:
187
            goto fail;
204
                goto fail;
188
            break;
205
                break;
189
        case AS_PF_DEFER:
206
            case AS_PF_DEFER:
190
            /*
207
                /*
191
             * The page fault came during copy_from_uspace()
208
                 * The page fault came during copy_from_uspace()
192
             * or copy_to_uspace().
209
                 * or copy_to_uspace().
193
             */
210
                 */
194
            page_table_unlock(AS, true);
211
                page_table_unlock(AS, true);
195
            return;
212
                return;
196
        default:
213
            default:
197
            panic("Unexpected pfrc (%d)\n", pfcr);
214
                panic("Unexpected pfrc (%d)\n", pfcr);
198
            break;
-
 
199
        }
215
        }
200
    }
216
    }
201
   
217
   
202
    pte->a = 1; /* Record access to PTE */
218
    pte->a = 1; /* Record access to PTE */
203
    pht_insert(badvaddr, pte->pfn);
219
    pht_insert(badvaddr, pte->pfn);
204
   
220
   
205
    page_table_unlock(AS, true);
221
    page_table_unlock(AS, true);
206
    return;
222
    return;
207
   
223
   
208
fail:
224
fail:
209
    page_table_unlock(AS, true);
225
    page_table_unlock(AS, true);
210
    pht_refill_fail(badvaddr, istate);
226
    pht_refill_fail(badvaddr, istate);
211
}
227
}
212
 
228
 
213
 
229
 
214
void pht_init(void)
230
void pht_init(void)
215
{
231
{
216
    memsetb((__address) phte, 1 << PHT_BITS, 0);
232
    memsetb((__address) phte, 1 << PHT_BITS, 0);
217
   
-
 
218
    /* Insert global kernel mapping */
-
 
219
   
-
 
220
    __address cur;
-
 
221
    for (cur = 0; cur < last_frame; cur += FRAME_SIZE) {
-
 
222
        pte_t *pte = page_mapping_find(AS_KERNEL, PA2KA(cur));
-
 
223
        if ((pte) && (pte->p) && (pte->g))
-
 
224
            pht_insert(PA2KA(cur), pte->pfn);
-
 
225
    }
-
 
226
}
233
}
227
 
234
 
228
 
235
 
229
void page_arch_init(void)
236
void page_arch_init(void)
230
{
237
{
231
    if (config.cpu_active == 1) {
238
    if (config.cpu_active == 1) {
232
        page_mapping_operations = &pt_mapping_operations;
239
        page_mapping_operations = &pt_mapping_operations;
233
       
240
       
234
        /*
241
        /*
235
         * PA2KA(identity) mapping for all frames until last_frame.
242
         * PA2KA(identity) mapping for all frames until last_frame.
236
         */
243
         */
237
        __address cur;
244
        __address cur;
238
        int flags;
245
        int flags;
239
       
246
       
240
        for (cur = 0; cur < last_frame; cur += FRAME_SIZE) {
247
        for (cur = 0; cur < last_frame; cur += FRAME_SIZE) {
241
            flags = PAGE_CACHEABLE;
248
            flags = PAGE_CACHEABLE;
242
            if ((PA2KA(cur) >= config.base) && (PA2KA(cur) < config.base + config.kernel_size))
249
            if ((PA2KA(cur) >= config.base) && (PA2KA(cur) < config.base + config.kernel_size))
243
                flags |= PAGE_GLOBAL;
250
                flags |= PAGE_GLOBAL;
244
            page_mapping_insert(AS_KERNEL, PA2KA(cur), cur, flags);
251
            page_mapping_insert(AS_KERNEL, PA2KA(cur), cur, flags);
245
        }
252
        }
246
       
253
       
247
        /* Allocate page hash table */
254
        /* Allocate page hash table */
248
        phte_t *physical_phte = (phte_t *) PFN2ADDR(frame_alloc(PHT_ORDER, FRAME_KA | FRAME_PANIC));
255
        phte_t *physical_phte = (phte_t *) PFN2ADDR(frame_alloc(PHT_ORDER, FRAME_KA | FRAME_PANIC));
249
        phte = (phte_t *) PA2KA((__address) physical_phte);
256
        phte = (phte_t *) PA2KA((__address) physical_phte);
250
       
257
       
251
        ASSERT((__address) physical_phte % (1 << PHT_BITS) == 0);
258
        ASSERT((__address) physical_phte % (1 << PHT_BITS) == 0);
252
        pht_init();
259
        pht_init();
253
       
260
       
254
        asm volatile (
261
        asm volatile (
255
            "mtsdr1 %0\n"
262
            "mtsdr1 %0\n"
256
            :
263
            :
257
            : "r" ((__address) physical_phte)
264
            : "r" ((__address) physical_phte)
258
        );
265
        );
259
       
-
 
260
        /* Invalidate block address translation registers,
-
 
261
           thus remove the temporary mapping */
-
 
262
//      invalidate_bat();
-
 
263
    }
266
    }
264
}
267
}
265
 
268