Subversion Repositories HelenOS

Rev

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

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