Subversion Repositories HelenOS

Rev

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

Rev 3425 Rev 3597
1
/*
1
/*
2
 * Copyright (c) 2006 Martin Decky
2
 * Copyright (c) 2006 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
/** @addtogroup ppc32mm
29
/** @addtogroup ppc32mm
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <mm/tlb.h>
35
#include <mm/tlb.h>
36
#include <arch/mm/tlb.h>
36
#include <arch/mm/tlb.h>
37
#include <arch/interrupt.h>
37
#include <arch/interrupt.h>
-
 
38
#include <interrupt.h>
38
#include <mm/as.h>
39
#include <mm/as.h>
39
#include <arch.h>
40
#include <arch.h>
40
#include <print.h>
41
#include <print.h>
41
#include <symtab.h>
42
#include <symtab.h>
42
 
43
 
43
 
44
 
44
/** Try to find PTE for faulting address
45
/** Try to find PTE for faulting address
45
 *
46
 *
46
 * Try to find PTE for faulting address.
47
 * Try to find PTE for faulting address.
47
 * The as->lock must be held on entry to this function
48
 * The as->lock must be held on entry to this function
48
 * if lock is true.
49
 * if lock is true.
49
 *
50
 *
50
 * @param as        Address space.
51
 * @param as        Address space.
51
 * @param lock      Lock/unlock the address space.
52
 * @param lock      Lock/unlock the address space.
52
 * @param badvaddr  Faulting virtual address.
53
 * @param badvaddr  Faulting virtual address.
53
 * @param access    Access mode that caused the fault.
54
 * @param access    Access mode that caused the fault.
54
 * @param istate    Pointer to interrupted state.
55
 * @param istate    Pointer to interrupted state.
55
 * @param pfrc      Pointer to variable where as_page_fault() return code
56
 * @param pfrc      Pointer to variable where as_page_fault() return code
56
 *          will be stored.
57
 *          will be stored.
57
 * @return      PTE on success, NULL otherwise.
58
 * @return      PTE on success, NULL otherwise.
58
 *
59
 *
59
 */
60
 */
60
static pte_t *
61
static pte_t *
61
find_mapping_and_check(as_t *as, bool lock, uintptr_t badvaddr, int access,
62
find_mapping_and_check(as_t *as, bool lock, uintptr_t badvaddr, int access,
62
    istate_t *istate, int *pfrc)
63
    istate_t *istate, int *pfrc)
63
{
64
{
64
    /*
65
    /*
65
     * Check if the mapping exists in page tables.
66
     * Check if the mapping exists in page tables.
66
     */
67
     */
67
    pte_t *pte = page_mapping_find(as, badvaddr);
68
    pte_t *pte = page_mapping_find(as, badvaddr);
68
    if ((pte) && (pte->p)) {
69
    if ((pte) && (pte->p)) {
69
        /*
70
        /*
70
         * Mapping found in page tables.
71
         * Mapping found in page tables.
71
         * Immediately succeed.
72
         * Immediately succeed.
72
         */
73
         */
73
        return pte;
74
        return pte;
74
    } else {
75
    } else {
75
        int rc;
76
        int rc;
76
   
77
   
77
        /*
78
        /*
78
         * Mapping not found in page tables.
79
         * Mapping not found in page tables.
79
         * Resort to higher-level page fault handler.
80
         * Resort to higher-level page fault handler.
80
         */
81
         */
81
        page_table_unlock(as, lock);
82
        page_table_unlock(as, lock);
82
        switch (rc = as_page_fault(badvaddr, access, istate)) {
83
        switch (rc = as_page_fault(badvaddr, access, istate)) {
83
        case AS_PF_OK:
84
        case AS_PF_OK:
84
            /*
85
            /*
85
             * The higher-level page fault handler succeeded,
86
             * The higher-level page fault handler succeeded,
86
             * The mapping ought to be in place.
87
             * The mapping ought to be in place.
87
             */
88
             */
88
            page_table_lock(as, lock);
89
            page_table_lock(as, lock);
89
            pte = page_mapping_find(as, badvaddr);
90
            pte = page_mapping_find(as, badvaddr);
90
            ASSERT((pte) && (pte->p));
91
            ASSERT((pte) && (pte->p));
91
            *pfrc = 0;
92
            *pfrc = 0;
92
            return pte;
93
            return pte;
93
        case AS_PF_DEFER:
94
        case AS_PF_DEFER:
94
            page_table_lock(as, lock);
95
            page_table_lock(as, lock);
95
            *pfrc = rc;
96
            *pfrc = rc;
96
            return NULL;
97
            return NULL;
97
        case AS_PF_FAULT:
98
        case AS_PF_FAULT:
98
            page_table_lock(as, lock);
99
            page_table_lock(as, lock);
99
            printf("Page fault.\n");
-
 
100
            *pfrc = rc;
100
            *pfrc = rc;
101
            return NULL;
101
            return NULL;
102
        default:
102
        default:
103
            panic("unexpected rc (%d)\n", rc);
103
            panic("unexpected rc (%d)\n", rc);
104
        }  
104
        }  
105
    }
105
    }
106
}
106
}
107
 
107
 
108
 
108
 
109
static void pht_refill_fail(uintptr_t badvaddr, istate_t *istate)
109
static void pht_refill_fail(uintptr_t badvaddr, istate_t *istate)
110
{
110
{
111
    char *symbol = "";
111
    char *symbol = "";
112
    char *sym2 = "";
112
    char *sym2 = "";
113
 
113
 
114
    char *s = get_symtab_entry(istate->pc);
114
    char *s = get_symtab_entry(istate->pc);
115
    if (s)
115
    if (s)
116
        symbol = s;
116
        symbol = s;
117
    s = get_symtab_entry(istate->lr);
117
    s = get_symtab_entry(istate->lr);
118
    if (s)
118
    if (s)
119
        sym2 = s;
119
        sym2 = s;
-
 
120
 
-
 
121
    fault_if_from_uspace(istate,
-
 
122
        "%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr,
-
 
123
        istate->pc, symbol, sym2);
120
    panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr,
124
    panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr,
121
        istate->pc, symbol, sym2);
125
        istate->pc, symbol, sym2);
122
}
126
}
123
 
127
 
124
 
128
 
125
static void pht_insert(const uintptr_t vaddr, const pfn_t pfn)
129
static void pht_insert(const uintptr_t vaddr, const pfn_t pfn)
126
{
130
{
127
    uint32_t page = (vaddr >> 12) & 0xffff;
131
    uint32_t page = (vaddr >> 12) & 0xffff;
128
    uint32_t api = (vaddr >> 22) & 0x3f;
132
    uint32_t api = (vaddr >> 22) & 0x3f;
129
   
133
   
130
    uint32_t vsid;
134
    uint32_t vsid;
131
    asm volatile (
135
    asm volatile (
132
        "mfsrin %0, %1\n"
136
        "mfsrin %0, %1\n"
133
        : "=r" (vsid)
137
        : "=r" (vsid)
134
        : "r" (vaddr)
138
        : "r" (vaddr)
135
    );
139
    );
136
   
140
   
137
    uint32_t sdr1;
141
    uint32_t sdr1;
138
    asm volatile (
142
    asm volatile (
139
        "mfsdr1 %0\n"
143
        "mfsdr1 %0\n"
140
        : "=r" (sdr1)
144
        : "=r" (sdr1)
141
    );
145
    );
142
    phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
146
    phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
143
   
147
   
144
    /* Primary hash (xor) */
148
    /* Primary hash (xor) */
145
    uint32_t h = 0;
149
    uint32_t h = 0;
146
    uint32_t hash = vsid ^ page;
150
    uint32_t hash = vsid ^ page;
147
    uint32_t base = (hash & 0x3ff) << 3;
151
    uint32_t base = (hash & 0x3ff) << 3;
148
    uint32_t i;
152
    uint32_t i;
149
    bool found = false;
153
    bool found = false;
150
   
154
   
151
    /* Find unused or colliding
155
    /* Find unused or colliding
152
       PTE in PTEG */
156
       PTE in PTEG */
153
    for (i = 0; i < 8; i++) {
157
    for (i = 0; i < 8; i++) {
154
        if ((!phte[base + i].v) || ((phte[base + i].vsid == vsid) &&
158
        if ((!phte[base + i].v) || ((phte[base + i].vsid == vsid) &&
155
            (phte[base + i].api == api))) {
159
            (phte[base + i].api == api))) {
156
            found = true;
160
            found = true;
157
            break;
161
            break;
158
        }
162
        }
159
    }
163
    }
160
   
164
   
161
    if (!found) {
165
    if (!found) {
162
        /* Secondary hash (not) */
166
        /* Secondary hash (not) */
163
        uint32_t base2 = (~hash & 0x3ff) << 3;
167
        uint32_t base2 = (~hash & 0x3ff) << 3;
164
       
168
       
165
        /* Find unused or colliding
169
        /* Find unused or colliding
166
           PTE in PTEG */
170
           PTE in PTEG */
167
        for (i = 0; i < 8; i++) {
171
        for (i = 0; i < 8; i++) {
168
            if ((!phte[base2 + i].v) ||
172
            if ((!phte[base2 + i].v) ||
169
                ((phte[base2 + i].vsid == vsid) &&
173
                ((phte[base2 + i].vsid == vsid) &&
170
                (phte[base2 + i].api == api))) {
174
                (phte[base2 + i].api == api))) {
171
                found = true;
175
                found = true;
172
                base = base2;
176
                base = base2;
173
                h = 1;
177
                h = 1;
174
                break;
178
                break;
175
            }
179
            }
176
        }
180
        }
177
       
181
       
178
        if (!found) {
182
        if (!found) {
179
            // TODO: A/C precedence groups
183
            // TODO: A/C precedence groups
180
            i = page % 8;
184
            i = page % 8;
181
        }
185
        }
182
    }
186
    }
183
   
187
   
184
    phte[base + i].v = 1;
188
    phte[base + i].v = 1;
185
    phte[base + i].vsid = vsid;
189
    phte[base + i].vsid = vsid;
186
    phte[base + i].h = h;
190
    phte[base + i].h = h;
187
    phte[base + i].api = api;
191
    phte[base + i].api = api;
188
    phte[base + i].rpn = pfn;
192
    phte[base + i].rpn = pfn;
189
    phte[base + i].r = 0;
193
    phte[base + i].r = 0;
190
    phte[base + i].c = 0;
194
    phte[base + i].c = 0;
191
    phte[base + i].pp = 2; // FIXME
195
    phte[base + i].pp = 2; // FIXME
192
}
196
}
193
 
197
 
194
 
198
 
195
static void pht_real_insert(const uintptr_t vaddr, const pfn_t pfn)
199
static void pht_real_insert(const uintptr_t vaddr, const pfn_t pfn)
196
{
200
{
197
    uint32_t page = (vaddr >> 12) & 0xffff;
201
    uint32_t page = (vaddr >> 12) & 0xffff;
198
    uint32_t api = (vaddr >> 22) & 0x3f;
202
    uint32_t api = (vaddr >> 22) & 0x3f;
199
   
203
   
200
    uint32_t vsid;
204
    uint32_t vsid;
201
    asm volatile (
205
    asm volatile (
202
        "mfsrin %0, %1\n"
206
        "mfsrin %0, %1\n"
203
        : "=r" (vsid)
207
        : "=r" (vsid)
204
        : "r" (vaddr)
208
        : "r" (vaddr)
205
    );
209
    );
206
   
210
   
207
    uint32_t sdr1;
211
    uint32_t sdr1;
208
    asm volatile (
212
    asm volatile (
209
        "mfsdr1 %0\n"
213
        "mfsdr1 %0\n"
210
        : "=r" (sdr1)
214
        : "=r" (sdr1)
211
    );
215
    );
212
    phte_t *phte_physical = (phte_t *) (sdr1 & 0xffff0000);
216
    phte_t *phte_physical = (phte_t *) (sdr1 & 0xffff0000);
213
   
217
   
214
    /* Primary hash (xor) */
218
    /* Primary hash (xor) */
215
    uint32_t h = 0;
219
    uint32_t h = 0;
216
    uint32_t hash = vsid ^ page;
220
    uint32_t hash = vsid ^ page;
217
    uint32_t base = (hash & 0x3ff) << 3;
221
    uint32_t base = (hash & 0x3ff) << 3;
218
    uint32_t i;
222
    uint32_t i;
219
    bool found = false;
223
    bool found = false;
220
   
224
   
221
    /* Find unused or colliding
225
    /* Find unused or colliding
222
       PTE in PTEG */
226
       PTE in PTEG */
223
    for (i = 0; i < 8; i++) {
227
    for (i = 0; i < 8; i++) {
224
        if ((!phte_physical[base + i].v) ||
228
        if ((!phte_physical[base + i].v) ||
225
            ((phte_physical[base + i].vsid == vsid) &&
229
            ((phte_physical[base + i].vsid == vsid) &&
226
            (phte_physical[base + i].api == api))) {
230
            (phte_physical[base + i].api == api))) {
227
            found = true;
231
            found = true;
228
            break;
232
            break;
229
        }
233
        }
230
    }
234
    }
231
   
235
   
232
    if (!found) {
236
    if (!found) {
233
        /* Secondary hash (not) */
237
        /* Secondary hash (not) */
234
        uint32_t base2 = (~hash & 0x3ff) << 3;
238
        uint32_t base2 = (~hash & 0x3ff) << 3;
235
       
239
       
236
        /* Find unused or colliding
240
        /* Find unused or colliding
237
           PTE in PTEG */
241
           PTE in PTEG */
238
        for (i = 0; i < 8; i++) {
242
        for (i = 0; i < 8; i++) {
239
            if ((!phte_physical[base2 + i].v) ||
243
            if ((!phte_physical[base2 + i].v) ||
240
                ((phte_physical[base2 + i].vsid == vsid) &&
244
                ((phte_physical[base2 + i].vsid == vsid) &&
241
                (phte_physical[base2 + i].api == api))) {
245
                (phte_physical[base2 + i].api == api))) {
242
                found = true;
246
                found = true;
243
                base = base2;
247
                base = base2;
244
                h = 1;
248
                h = 1;
245
                break;
249
                break;
246
            }
250
            }
247
        }
251
        }
248
       
252
       
249
        if (!found) {
253
        if (!found) {
250
            // TODO: A/C precedence groups
254
            // TODO: A/C precedence groups
251
            i = page % 8;
255
            i = page % 8;
252
        }
256
        }
253
    }
257
    }
254
   
258
   
255
    phte_physical[base + i].v = 1;
259
    phte_physical[base + i].v = 1;
256
    phte_physical[base + i].vsid = vsid;
260
    phte_physical[base + i].vsid = vsid;
257
    phte_physical[base + i].h = h;
261
    phte_physical[base + i].h = h;
258
    phte_physical[base + i].api = api;
262
    phte_physical[base + i].api = api;
259
    phte_physical[base + i].rpn = pfn;
263
    phte_physical[base + i].rpn = pfn;
260
    phte_physical[base + i].r = 0;
264
    phte_physical[base + i].r = 0;
261
    phte_physical[base + i].c = 0;
265
    phte_physical[base + i].c = 0;
262
    phte_physical[base + i].pp = 2; // FIXME
266
    phte_physical[base + i].pp = 2; // FIXME
263
}
267
}
264
 
268
 
265
 
269
 
266
/** Process Instruction/Data Storage Interrupt
270
/** Process Instruction/Data Storage Interrupt
267
 *
271
 *
268
 * @param n     Interrupt vector number.
272
 * @param n     Interrupt vector number.
269
 * @param istate    Interrupted register context.
273
 * @param istate    Interrupted register context.
270
 *
274
 *
271
 */
275
 */
272
void pht_refill(int n, istate_t *istate)
276
void pht_refill(int n, istate_t *istate)
273
{
277
{
274
    uintptr_t badvaddr;
278
    uintptr_t badvaddr;
275
    pte_t *pte;
279
    pte_t *pte;
276
    int pfrc;
280
    int pfrc;
277
    as_t *as;
281
    as_t *as;
278
    bool lock;
282
    bool lock;
279
   
283
   
280
    if (AS == NULL) {
284
    if (AS == NULL) {
281
        as = AS_KERNEL;
285
        as = AS_KERNEL;
282
        lock = false;
286
        lock = false;
283
    } else {
287
    } else {
284
        as = AS;
288
        as = AS;
285
        lock = true;
289
        lock = true;
286
    }
290
    }
287
   
291
   
288
    if (n == VECTOR_DATA_STORAGE) {
292
    if (n == VECTOR_DATA_STORAGE) {
289
        asm volatile (
293
        asm volatile (
290
            "mfdar %0\n"
294
            "mfdar %0\n"
291
            : "=r" (badvaddr)
295
            : "=r" (badvaddr)
292
        );
296
        );
293
    } else
297
    } else
294
        badvaddr = istate->pc;
298
        badvaddr = istate->pc;
295
       
299
       
296
    page_table_lock(as, lock);
300
    page_table_lock(as, lock);
297
   
301
   
298
    pte = find_mapping_and_check(as, lock, badvaddr,
302
    pte = find_mapping_and_check(as, lock, badvaddr,
299
        PF_ACCESS_READ /* FIXME */, istate, &pfrc);
303
        PF_ACCESS_READ /* FIXME */, istate, &pfrc);
300
    if (!pte) {
304
    if (!pte) {
301
        switch (pfrc) {
305
        switch (pfrc) {
302
        case AS_PF_FAULT:
306
        case AS_PF_FAULT:
303
            goto fail;
307
            goto fail;
304
            break;
308
            break;
305
        case AS_PF_DEFER:
309
        case AS_PF_DEFER:
306
            /*
310
            /*
307
             * The page fault came during copy_from_uspace()
311
             * The page fault came during copy_from_uspace()
308
             * or copy_to_uspace().
312
             * or copy_to_uspace().
309
             */
313
             */
310
            page_table_unlock(as, lock);
314
            page_table_unlock(as, lock);
311
            return;
315
            return;
312
        default:
316
        default:
313
            panic("Unexpected pfrc (%d)\n", pfrc);
317
            panic("Unexpected pfrc (%d)\n", pfrc);
314
        }
318
        }
315
    }
319
    }
316
   
320
   
317
    pte->a = 1; /* Record access to PTE */
321
    pte->a = 1; /* Record access to PTE */
318
    pht_insert(badvaddr, pte->pfn);
322
    pht_insert(badvaddr, pte->pfn);
319
   
323
   
320
    page_table_unlock(as, lock);
324
    page_table_unlock(as, lock);
321
    return;
325
    return;
322
   
326
   
323
fail:
327
fail:
324
    page_table_unlock(as, lock);
328
    page_table_unlock(as, lock);
325
    pht_refill_fail(badvaddr, istate);
329
    pht_refill_fail(badvaddr, istate);
326
}
330
}
327
 
331
 
328
 
332
 
329
/** Process Instruction/Data Storage Interrupt in Real Mode
333
/** Process Instruction/Data Storage Interrupt in Real Mode
330
 *
334
 *
331
 * @param n     Interrupt vector number.
335
 * @param n     Interrupt vector number.
332
 * @param istate    Interrupted register context.
336
 * @param istate    Interrupted register context.
333
 *
337
 *
334
 */
338
 */
335
bool pht_real_refill(int n, istate_t *istate)
339
bool pht_real_refill(int n, istate_t *istate)
336
{
340
{
337
    uintptr_t badvaddr;
341
    uintptr_t badvaddr;
338
   
342
   
339
    if (n == VECTOR_DATA_STORAGE) {
343
    if (n == VECTOR_DATA_STORAGE) {
340
        asm volatile (
344
        asm volatile (
341
            "mfdar %0\n"
345
            "mfdar %0\n"
342
            : "=r" (badvaddr)
346
            : "=r" (badvaddr)
343
        );
347
        );
344
    } else
348
    } else
345
        badvaddr = istate->pc;
349
        badvaddr = istate->pc;
346
   
350
   
347
    uint32_t physmem;
351
    uint32_t physmem;
348
    asm volatile (
352
    asm volatile (
349
        "mfsprg3 %0\n"
353
        "mfsprg3 %0\n"
350
        : "=r" (physmem)
354
        : "=r" (physmem)
351
    );
355
    );
352
   
356
   
353
    if ((badvaddr >= PA2KA(0)) && (badvaddr < PA2KA(physmem))) {
357
    if ((badvaddr >= PA2KA(0)) && (badvaddr < PA2KA(physmem))) {
354
        pht_real_insert(badvaddr, KA2PA(badvaddr) >> 12);
358
        pht_real_insert(badvaddr, KA2PA(badvaddr) >> 12);
355
        return true;
359
        return true;
356
    }
360
    }
357
   
361
   
358
    return false;
362
    return false;
359
}
363
}
360
 
364
 
361
 
365
 
362
void tlb_arch_init(void)
366
void tlb_arch_init(void)
363
{
367
{
364
    tlb_invalidate_all();
368
    tlb_invalidate_all();
365
}
369
}
366
 
370
 
367
 
371
 
368
void tlb_invalidate_all(void)
372
void tlb_invalidate_all(void)
369
{
373
{
370
    asm volatile (
374
    asm volatile (
371
        "tlbia\n"
375
        "tlbia\n"
372
        "tlbsync\n"
376
        "tlbsync\n"
373
    );
377
    );
374
}
378
}
375
 
379
 
376
 
380
 
377
void tlb_invalidate_asid(asid_t asid)
381
void tlb_invalidate_asid(asid_t asid)
378
{
382
{
379
    uint32_t sdr1;
383
    uint32_t sdr1;
380
    asm volatile (
384
    asm volatile (
381
        "mfsdr1 %0\n"
385
        "mfsdr1 %0\n"
382
        : "=r" (sdr1)
386
        : "=r" (sdr1)
383
    );
387
    );
384
    phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
388
    phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
385
   
389
   
386
    uint32_t i;
390
    uint32_t i;
387
    for (i = 0; i < 8192; i++) {
391
    for (i = 0; i < 8192; i++) {
388
        if ((phte[i].v) && (phte[i].vsid >= (asid << 4)) &&
392
        if ((phte[i].v) && (phte[i].vsid >= (asid << 4)) &&
389
            (phte[i].vsid < ((asid << 4) + 16)))
393
            (phte[i].vsid < ((asid << 4) + 16)))
390
            phte[i].v = 0;
394
            phte[i].v = 0;
391
    }
395
    }
392
    tlb_invalidate_all();
396
    tlb_invalidate_all();
393
}
397
}
394
 
398
 
395
 
399
 
396
void tlb_invalidate_pages(asid_t asid, uintptr_t page, count_t cnt)
400
void tlb_invalidate_pages(asid_t asid, uintptr_t page, count_t cnt)
397
{
401
{
398
    // TODO
402
    // TODO
399
    tlb_invalidate_all();
403
    tlb_invalidate_all();
400
}
404
}
401
 
405
 
402
 
406
 
403
#define PRINT_BAT(name, ureg, lreg) \
407
#define PRINT_BAT(name, ureg, lreg) \
404
    asm volatile ( \
408
    asm volatile ( \
405
        "mfspr %0," #ureg "\n" \
409
        "mfspr %0," #ureg "\n" \
406
        "mfspr %1," #lreg "\n" \
410
        "mfspr %1," #lreg "\n" \
407
        : "=r" (upper), "=r" (lower) \
411
        : "=r" (upper), "=r" (lower) \
408
    ); \
412
    ); \
409
    mask = (upper & 0x1ffc) >> 2; \
413
    mask = (upper & 0x1ffc) >> 2; \
410
    if (upper & 3) { \
414
    if (upper & 3) { \
411
        uint32_t tmp = mask; \
415
        uint32_t tmp = mask; \
412
        length = 128; \
416
        length = 128; \
413
        while (tmp) { \
417
        while (tmp) { \
414
            if ((tmp & 1) == 0) { \
418
            if ((tmp & 1) == 0) { \
415
                printf("ibat[0]: error in mask\n"); \
419
                printf("ibat[0]: error in mask\n"); \
416
                break; \
420
                break; \
417
            } \
421
            } \
418
            length <<= 1; \
422
            length <<= 1; \
419
            tmp >>= 1; \
423
            tmp >>= 1; \
420
        } \
424
        } \
421
    } else \
425
    } else \
422
        length = 0; \
426
        length = 0; \
423
    printf(name ": page=%.*p frame=%.*p length=%d KB (mask=%#x)%s%s\n", \
427
    printf(name ": page=%.*p frame=%.*p length=%d KB (mask=%#x)%s%s\n", \
424
        sizeof(upper) * 2, upper & 0xffff0000, sizeof(lower) * 2, \
428
        sizeof(upper) * 2, upper & 0xffff0000, sizeof(lower) * 2, \
425
        lower & 0xffff0000, length, mask, \
429
        lower & 0xffff0000, length, mask, \
426
        ((upper >> 1) & 1) ? " supervisor" : "", \
430
        ((upper >> 1) & 1) ? " supervisor" : "", \
427
        (upper & 1) ? " user" : "");
431
        (upper & 1) ? " user" : "");
428
 
432
 
429
 
433
 
430
void tlb_print(void)
434
void tlb_print(void)
431
{
435
{
432
    uint32_t sr;
436
    uint32_t sr;
433
   
437
   
434
    for (sr = 0; sr < 16; sr++) {
438
    for (sr = 0; sr < 16; sr++) {
435
        uint32_t vsid;
439
        uint32_t vsid;
436
        asm volatile (
440
        asm volatile (
437
            "mfsrin %0, %1\n"
441
            "mfsrin %0, %1\n"
438
            : "=r" (vsid)
442
            : "=r" (vsid)
439
            : "r" (sr << 28)
443
            : "r" (sr << 28)
440
        );
444
        );
441
        printf("vsid[%d]: VSID=%.*p (ASID=%d)%s%s\n", sr,
445
        printf("vsid[%d]: VSID=%.*p (ASID=%d)%s%s\n", sr,
442
            sizeof(vsid) * 2, vsid & 0xffffff, (vsid & 0xffffff) >> 4,
446
            sizeof(vsid) * 2, vsid & 0xffffff, (vsid & 0xffffff) >> 4,
443
            ((vsid >> 30) & 1) ? " supervisor" : "",
447
            ((vsid >> 30) & 1) ? " supervisor" : "",
444
            ((vsid >> 29) & 1) ? " user" : "");
448
            ((vsid >> 29) & 1) ? " user" : "");
445
    }
449
    }
446
   
450
   
447
    uint32_t upper;
451
    uint32_t upper;
448
    uint32_t lower;
452
    uint32_t lower;
449
    uint32_t mask;
453
    uint32_t mask;
450
    uint32_t length;
454
    uint32_t length;
451
   
455
   
452
    PRINT_BAT("ibat[0]", 528, 529);
456
    PRINT_BAT("ibat[0]", 528, 529);
453
    PRINT_BAT("ibat[1]", 530, 531);
457
    PRINT_BAT("ibat[1]", 530, 531);
454
    PRINT_BAT("ibat[2]", 532, 533);
458
    PRINT_BAT("ibat[2]", 532, 533);
455
    PRINT_BAT("ibat[3]", 534, 535);
459
    PRINT_BAT("ibat[3]", 534, 535);
456
   
460
   
457
    PRINT_BAT("dbat[0]", 536, 537);
461
    PRINT_BAT("dbat[0]", 536, 537);
458
    PRINT_BAT("dbat[1]", 538, 539);
462
    PRINT_BAT("dbat[1]", 538, 539);
459
    PRINT_BAT("dbat[2]", 540, 541);
463
    PRINT_BAT("dbat[2]", 540, 541);
460
    PRINT_BAT("dbat[3]", 542, 543);
464
    PRINT_BAT("dbat[3]", 542, 543);
461
}
465
}
462
 
466
 
463
/** @}
467
/** @}
464
 */
468
 */
465
 
469