Subversion Repositories HelenOS

Rev

Rev 1965 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1965 Rev 1968
1
/*
1
/*
2
 * Copyright (C) 2003-2004 Jakub Jermar
2
 * Copyright (C) 2003-2004 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
#include <arch/mm/tlb.h>
29
#include <arch/mm/tlb.h>
30
#include <mm/asid.h>
30
#include <mm/asid.h>
31
#include <mm/tlb.h>
31
#include <mm/tlb.h>
32
#include <mm/page.h>
32
#include <mm/page.h>
33
#include <mm/as.h>
33
#include <mm/as.h>
34
#include <arch/cp0.h>
34
#include <arch/cp0.h>
35
#include <panic.h>
35
#include <panic.h>
36
#include <arch.h>
36
#include <arch.h>
37
#include <symtab.h>
37
#include <symtab.h>
38
#include <synch/spinlock.h>
38
#include <synch/spinlock.h>
39
#include <print.h>
39
#include <print.h>
40
#include <debug.h>
40
#include <debug.h>
41
#include <align.h>
41
#include <align.h>
42
#include <interrupt.h>
42
#include <interrupt.h>
43
 
43
 
44
static void tlb_refill_fail(istate_t *istate);
44
static void tlb_refill_fail(istate_t *istate);
45
static void tlb_invalid_fail(istate_t *istate);
45
static void tlb_invalid_fail(istate_t *istate);
46
static void tlb_modified_fail(istate_t *istate);
46
static void tlb_modified_fail(istate_t *istate);
47
 
47
 
48
static pte_t *find_mapping_and_check(__address badvaddr, int access, istate_t *istate, int *pfrc);
48
static pte_t *find_mapping_and_check(__address badvaddr, int access, istate_t *istate, int *pfrc);
49
 
49
 
50
static void prepare_entry_lo(entry_lo_t *lo, bool g, bool v, bool d, bool cacheable, __address pfn);
50
static void prepare_entry_lo(entry_lo_t *lo, bool g, bool v, bool d, bool cacheable, __address pfn);
51
static void prepare_entry_hi(entry_hi_t *hi, asid_t asid, __address addr);
51
static void prepare_entry_hi(entry_hi_t *hi, asid_t asid, __address addr);
52
 
52
 
53
/** Initialize TLB
53
/** Initialize TLB
54
 *
54
 *
55
 * Initialize TLB.
55
 * Initialize TLB.
56
 * Invalidate all entries and mark wired entries.
56
 * Invalidate all entries and mark wired entries.
57
 */
57
 */
58
void tlb_arch_init(void)
58
void tlb_arch_init(void)
59
{
59
{
60
    int i;
60
    int i;
61
 
61
 
62
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
62
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
63
    cp0_entry_hi_write(0);
63
    cp0_entry_hi_write(0);
64
    cp0_entry_lo0_write(0);
64
    cp0_entry_lo0_write(0);
65
    cp0_entry_lo1_write(0);
65
    cp0_entry_lo1_write(0);
66
 
66
 
67
    /* Clear and initialize TLB. */
67
    /* Clear and initialize TLB. */
68
   
68
   
69
    for (i = 0; i < TLB_ENTRY_COUNT; i++) {
69
    for (i = 0; i < TLB_ENTRY_COUNT; i++) {
70
        cp0_index_write(i);
70
        cp0_index_write(i);
71
        tlbwi();
71
        tlbwi();
72
    }
72
    }
73
 
73
 
74
       
74
       
75
    /*
75
    /*
76
     * The kernel is going to make use of some wired
76
     * The kernel is going to make use of some wired
77
     * entries (e.g. mapping kernel stacks in kseg3).
77
     * entries (e.g. mapping kernel stacks in kseg3).
78
     */
78
     */
79
    cp0_wired_write(TLB_WIRED);
79
    cp0_wired_write(TLB_WIRED);
80
}
80
}
81
 
81
 
82
/** Process TLB Refill Exception
82
/** Process TLB Refill Exception
83
 *
83
 *
84
 * Process TLB Refill Exception.
84
 * Process TLB Refill Exception.
85
 *
85
 *
86
 * @param istate Interrupted register context.
86
 * @param istate Interrupted register context.
87
 */
87
 */
88
void tlb_refill(istate_t *istate)
88
void tlb_refill(istate_t *istate)
89
{
89
{
90
    entry_lo_t lo;
90
    entry_lo_t lo;
91
    entry_hi_t hi;
91
    entry_hi_t hi;
92
    asid_t asid;
92
    asid_t asid;
93
    __address badvaddr;
93
    __address badvaddr;
94
    pte_t *pte;
94
    pte_t *pte;
95
    int pfrc;
95
    int pfrc;
96
 
96
 
97
    badvaddr = cp0_badvaddr_read();
97
    badvaddr = cp0_badvaddr_read();
98
 
98
 
99
    spinlock_lock(&AS->lock);
99
    spinlock_lock(&AS->lock);
100
    asid = AS->asid;
100
    asid = AS->asid;
101
    spinlock_unlock(&AS->lock);
101
    spinlock_unlock(&AS->lock);
102
 
102
 
103
    page_table_lock(AS, true);
103
    page_table_lock(AS, true);
104
 
104
 
105
    pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate, &pfrc);
105
    pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate, &pfrc);
106
    if (!pte) {
106
    if (!pte) {
107
        switch (pfrc) {
107
        switch (pfrc) {
108
        case AS_PF_FAULT:
108
        case AS_PF_FAULT:
109
            goto fail;
109
            goto fail;
110
            break;
110
            break;
111
        case AS_PF_DEFER:
111
        case AS_PF_DEFER:
112
            /*
112
            /*
113
             * The page fault came during copy_from_uspace()
113
             * The page fault came during copy_from_uspace()
114
             * or copy_to_uspace().
114
             * or copy_to_uspace().
115
             */
115
             */
116
            page_table_unlock(AS, true);
116
            page_table_unlock(AS, true);
117
            return;
117
            return;
118
        default:
118
        default:
119
            panic("unexpected pfrc (%d)\n", pfrc);
119
            panic("unexpected pfrc (%d)\n", pfrc);
120
        }
120
        }
121
    }
121
    }
122
 
122
 
123
    /*
123
    /*
124
     * Record access to PTE.
124
     * Record access to PTE.
125
     */
125
     */
126
    pte->a = 1;
126
    pte->a = 1;
127
 
127
 
128
    prepare_entry_hi(&hi, asid, badvaddr);
128
    prepare_entry_hi(&hi, asid, badvaddr);
129
    prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable, pte->pfn);
129
    prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable, pte->pfn);
130
 
130
 
131
    /*
131
    /*
132
     * New entry is to be inserted into TLB
132
     * New entry is to be inserted into TLB
133
     */
133
     */
134
    cp0_entry_hi_write(hi.value);
134
    cp0_entry_hi_write(hi.value);
135
    if ((badvaddr/PAGE_SIZE) % 2 == 0) {
135
    if ((badvaddr/PAGE_SIZE) % 2 == 0) {
136
        cp0_entry_lo0_write(lo.value);
136
        cp0_entry_lo0_write(lo.value);
137
        cp0_entry_lo1_write(0);
137
        cp0_entry_lo1_write(0);
138
    }
138
    }
139
    else {
139
    else {
140
        cp0_entry_lo0_write(0);
140
        cp0_entry_lo0_write(0);
141
        cp0_entry_lo1_write(lo.value);
141
        cp0_entry_lo1_write(lo.value);
142
    }
142
    }
143
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
143
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
144
    tlbwr();
144
    tlbwr();
145
 
145
 
146
    page_table_unlock(AS, true);
146
    page_table_unlock(AS, true);
147
    return;
147
    return;
148
   
148
   
149
fail:
149
fail:
150
    page_table_unlock(AS, true);
150
    page_table_unlock(AS, true);
151
    tlb_refill_fail(istate);
151
    tlb_refill_fail(istate);
152
}
152
}
153
 
153
 
154
/** Process TLB Invalid Exception
154
/** Process TLB Invalid Exception
155
 *
155
 *
156
 * Process TLB Invalid Exception.
156
 * Process TLB Invalid Exception.
157
 *
157
 *
158
 * @param istate Interrupted register context.
158
 * @param istate Interrupted register context.
159
 */
159
 */
160
void tlb_invalid(istate_t *istate)
160
void tlb_invalid(istate_t *istate)
161
{
161
{
162
    tlb_index_t index;
162
    tlb_index_t index;
163
    __address badvaddr;
163
    __address badvaddr;
164
    entry_lo_t lo;
164
    entry_lo_t lo;
165
    entry_hi_t hi;
165
    entry_hi_t hi;
166
    pte_t *pte;
166
    pte_t *pte;
167
    int pfrc;
167
    int pfrc;
168
 
168
 
169
    badvaddr = cp0_badvaddr_read();
169
    badvaddr = cp0_badvaddr_read();
170
 
170
 
171
    /*
171
    /*
172
     * Locate the faulting entry in TLB.
172
     * Locate the faulting entry in TLB.
173
     */
173
     */
174
    hi.value = cp0_entry_hi_read();
174
    hi.value = cp0_entry_hi_read();
175
    prepare_entry_hi(&hi, hi.asid, badvaddr);
175
    prepare_entry_hi(&hi, hi.asid, badvaddr);
176
    cp0_entry_hi_write(hi.value);
176
    cp0_entry_hi_write(hi.value);
177
    tlbp();
177
    tlbp();
178
    index.value = cp0_index_read();
178
    index.value = cp0_index_read();
179
 
179
 
180
    page_table_lock(AS, true); 
180
    page_table_lock(AS, true); 
181
   
181
   
182
    /*
182
    /*
183
     * Fail if the entry is not in TLB.
183
     * Fail if the entry is not in TLB.
184
     */
184
     */
185
    if (index.p) {
185
    if (index.p) {
186
        printf("TLB entry not found.\n");
186
        printf("TLB entry not found.\n");
187
        goto fail;
187
        goto fail;
188
    }
188
    }
189
 
189
 
190
    pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate, &pfrc);
190
    pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate, &pfrc);
191
    if (!pte) {
191
    if (!pte) {
192
        switch (pfrc) {
192
        switch (pfrc) {
193
        case AS_PF_FAULT:
193
        case AS_PF_FAULT:
194
            goto fail;
194
            goto fail;
195
            break;
195
            break;
196
        case AS_PF_DEFER:
196
        case AS_PF_DEFER:
197
            /*
197
            /*
198
             * The page fault came during copy_from_uspace()
198
             * The page fault came during copy_from_uspace()
199
             * or copy_to_uspace().
199
             * or copy_to_uspace().
200
             */
200
             */
201
            page_table_unlock(AS, true);             
201
            page_table_unlock(AS, true);             
202
            return;
202
            return;
203
        default:
203
        default:
204
            panic("unexpected pfrc (%d)\n", pfrc);
204
            panic("unexpected pfrc (%d)\n", pfrc);
205
        }
205
        }
206
    }
206
    }
207
 
207
 
208
    /*
208
    /*
209
     * Read the faulting TLB entry.
209
     * Read the faulting TLB entry.
210
     */
210
     */
211
    tlbr();
211
    tlbr();
212
 
212
 
213
    /*
213
    /*
214
     * Record access to PTE.
214
     * Record access to PTE.
215
     */
215
     */
216
    pte->a = 1;
216
    pte->a = 1;
217
 
217
 
218
    prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable, pte->pfn);
218
    prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable, pte->pfn);
219
 
219
 
220
    /*
220
    /*
221
     * The entry is to be updated in TLB.
221
     * The entry is to be updated in TLB.
222
     */
222
     */
223
    if ((badvaddr/PAGE_SIZE) % 2 == 0)
223
    if ((badvaddr/PAGE_SIZE) % 2 == 0)
224
        cp0_entry_lo0_write(lo.value);
224
        cp0_entry_lo0_write(lo.value);
225
    else
225
    else
226
        cp0_entry_lo1_write(lo.value);
226
        cp0_entry_lo1_write(lo.value);
227
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
227
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
228
    tlbwi();
228
    tlbwi();
229
 
229
 
230
    page_table_unlock(AS, true);
230
    page_table_unlock(AS, true);
231
    return;
231
    return;
232
   
232
   
233
fail:
233
fail:
234
    page_table_unlock(AS, true);
234
    page_table_unlock(AS, true);
235
    tlb_invalid_fail(istate);
235
    tlb_invalid_fail(istate);
236
}
236
}
237
 
237
 
238
/** Process TLB Modified Exception
238
/** Process TLB Modified Exception
239
 *
239
 *
240
 * Process TLB Modified Exception.
240
 * Process TLB Modified Exception.
241
 *
241
 *
242
 * @param istate Interrupted register context.
242
 * @param istate Interrupted register context.
243
 */
243
 */
244
void tlb_modified(istate_t *istate)
244
void tlb_modified(istate_t *istate)
245
{
245
{
246
    tlb_index_t index;
246
    tlb_index_t index;
247
    __address badvaddr;
247
    __address badvaddr;
248
    entry_lo_t lo;
248
    entry_lo_t lo;
249
    entry_hi_t hi;
249
    entry_hi_t hi;
250
    pte_t *pte;
250
    pte_t *pte;
251
    int pfrc;
251
    int pfrc;
252
 
252
 
253
    badvaddr = cp0_badvaddr_read();
253
    badvaddr = cp0_badvaddr_read();
254
 
254
 
255
    /*
255
    /*
256
     * Locate the faulting entry in TLB.
256
     * Locate the faulting entry in TLB.
257
     */
257
     */
258
    hi.value = cp0_entry_hi_read();
258
    hi.value = cp0_entry_hi_read();
259
    prepare_entry_hi(&hi, hi.asid, badvaddr);
259
    prepare_entry_hi(&hi, hi.asid, badvaddr);
260
    cp0_entry_hi_write(hi.value);
260
    cp0_entry_hi_write(hi.value);
261
    tlbp();
261
    tlbp();
262
    index.value = cp0_index_read();
262
    index.value = cp0_index_read();
263
 
263
 
264
    page_table_lock(AS, true); 
264
    page_table_lock(AS, true); 
265
   
265
   
266
    /*
266
    /*
267
     * Fail if the entry is not in TLB.
267
     * Fail if the entry is not in TLB.
268
     */
268
     */
269
    if (index.p) {
269
    if (index.p) {
270
        printf("TLB entry not found.\n");
270
        printf("TLB entry not found.\n");
271
        goto fail;
271
        goto fail;
272
    }
272
    }
273
 
273
 
274
    pte = find_mapping_and_check(badvaddr, PF_ACCESS_WRITE, istate, &pfrc);
274
    pte = find_mapping_and_check(badvaddr, PF_ACCESS_WRITE, istate, &pfrc);
275
    if (!pte) {
275
    if (!pte) {
276
        switch (pfrc) {
276
        switch (pfrc) {
277
        case AS_PF_FAULT:
277
        case AS_PF_FAULT:
278
            goto fail;
278
            goto fail;
279
            break;
279
            break;
280
        case AS_PF_DEFER:
280
        case AS_PF_DEFER:
281
            /*
281
            /*
282
             * The page fault came during copy_from_uspace()
282
             * The page fault came during copy_from_uspace()
283
             * or copy_to_uspace().
283
             * or copy_to_uspace().
284
             */
284
             */
285
            page_table_unlock(AS, true);             
285
            page_table_unlock(AS, true);             
286
            return;
286
            return;
287
        default:
287
        default:
288
            panic("unexpected pfrc (%d)\n", pfrc);
288
            panic("unexpected pfrc (%d)\n", pfrc);
289
        }
289
        }
290
    }
290
    }
291
 
291
 
292
    /*
292
    /*
293
     * Fail if the page is not writable.
293
     * Fail if the page is not writable.
294
     */
294
     */
295
    if (!pte->w)
295
    if (!pte->w)
296
        goto fail;
296
        goto fail;
297
 
297
 
298
    /*
298
    /*
299
     * Read the faulting TLB entry.
299
     * Read the faulting TLB entry.
300
     */
300
     */
301
    tlbr();
301
    tlbr();
302
 
302
 
303
    /*
303
    /*
304
     * Record access and write to PTE.
304
     * Record access and write to PTE.
305
     */
305
     */
306
    pte->a = 1;
306
    pte->a = 1;
307
    pte->d = 1;
307
    pte->d = 1;
308
 
308
 
309
    prepare_entry_lo(&lo, pte->g, pte->p, pte->w, pte->cacheable, pte->pfn);
309
    prepare_entry_lo(&lo, pte->g, pte->p, pte->w, pte->cacheable, pte->pfn);
310
 
310
 
311
    /*
311
    /*
312
     * The entry is to be updated in TLB.
312
     * The entry is to be updated in TLB.
313
     */
313
     */
314
    if ((badvaddr/PAGE_SIZE) % 2 == 0)
314
    if ((badvaddr/PAGE_SIZE) % 2 == 0)
315
        cp0_entry_lo0_write(lo.value);
315
        cp0_entry_lo0_write(lo.value);
316
    else
316
    else
317
        cp0_entry_lo1_write(lo.value);
317
        cp0_entry_lo1_write(lo.value);
318
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
318
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
319
    tlbwi();
319
    tlbwi();
320
 
320
 
321
    page_table_unlock(AS, true);
321
    page_table_unlock(AS, true);
322
    return;
322
    return;
323
   
323
   
324
fail:
324
fail:
325
    page_table_unlock(AS, true);
325
    page_table_unlock(AS, true);
326
    tlb_modified_fail(istate);
326
    tlb_modified_fail(istate);
327
}
327
}
328
 
328
 
329
void tlb_refill_fail(istate_t *istate)
329
void tlb_refill_fail(istate_t *istate)
330
{
330
{
331
    char *symbol = "";
331
    char *symbol = "";
332
    char *sym2 = "";
332
    char *sym2 = "";
333
 
333
 
334
    char *s = get_symtab_entry(istate->epc);
334
    char *s = get_symtab_entry(istate->epc);
335
    if (s)
335
    if (s)
336
        symbol = s;
336
        symbol = s;
337
    s = get_symtab_entry(istate->ra);
337
    s = get_symtab_entry(istate->ra);
338
    if (s)
338
    if (s)
339
        sym2 = s;
339
        sym2 = s;
340
 
340
 
341
    fault_if_from_uspace(istate, "TLB Refill Exception on %P", cp0_badvaddr_read());
341
    fault_if_from_uspace(istate, "TLB Refill Exception on %P", cp0_badvaddr_read());
342
    panic("%X: TLB Refill Exception at %X(%s<-%s)\n", cp0_badvaddr_read(), istate->epc, symbol, sym2);
342
    panic("%X: TLB Refill Exception at %X(%s<-%s)\n", cp0_badvaddr_read(), istate->epc, symbol, sym2);
343
}
343
}
344
 
344
 
345
 
345
 
346
void tlb_invalid_fail(istate_t *istate)
346
void tlb_invalid_fail(istate_t *istate)
347
{
347
{
348
    char *symbol = "";
348
    char *symbol = "";
349
 
349
 
350
    char *s = get_symtab_entry(istate->epc);
350
    char *s = get_symtab_entry(istate->epc);
351
    if (s)
351
    if (s)
352
        symbol = s;
352
        symbol = s;
353
    fault_if_from_uspace(istate, "TLB Invalid Exception on %P", cp0_badvaddr_read());
353
    fault_if_from_uspace(istate, "TLB Invalid Exception on %P", cp0_badvaddr_read());
354
    panic("%X: TLB Invalid Exception at %X(%s)\n", cp0_badvaddr_read(), istate->epc, symbol);
354
    panic("%X: TLB Invalid Exception at %X(%s)\n", cp0_badvaddr_read(), istate->epc, symbol);
355
}
355
}
356
 
356
 
357
void tlb_modified_fail(istate_t *istate)
357
void tlb_modified_fail(istate_t *istate)
358
{
358
{
359
    char *symbol = "";
359
    char *symbol = "";
360
 
360
 
361
    char *s = get_symtab_entry(istate->epc);
361
    char *s = get_symtab_entry(istate->epc);
362
    if (s)
362
    if (s)
363
        symbol = s;
363
        symbol = s;
364
    fault_if_from_uspace(istate, "TLB Modified Exception on %P", cp0_badvaddr_read());
364
    fault_if_from_uspace(istate, "TLB Modified Exception on %P", cp0_badvaddr_read());
365
    panic("%X: TLB Modified Exception at %X(%s)\n", cp0_badvaddr_read(), istate->epc, symbol);
365
    panic("%X: TLB Modified Exception at %X(%s)\n", cp0_badvaddr_read(), istate->epc, symbol);
366
}
366
}
367
 
367
 
368
/** Try to find PTE for faulting address
368
/** Try to find PTE for faulting address
369
 *
369
 *
370
 * Try to find PTE for faulting address.
370
 * Try to find PTE for faulting address.
371
 * The AS->lock must be held on entry to this function.
371
 * The AS->lock must be held on entry to this function.
372
 *
372
 *
373
 * @param badvaddr Faulting virtual address.
373
 * @param badvaddr Faulting virtual address.
374
 * @param access Access mode that caused the fault.
374
 * @param access Access mode that caused the fault.
375
 * @param istate Pointer to interrupted state.
375
 * @param istate Pointer to interrupted state.
376
 * @param pfrc Pointer to variable where as_page_fault() return code will be stored.
376
 * @param pfrc Pointer to variable where as_page_fault() return code will be stored.
377
 *
377
 *
378
 * @return PTE on success, NULL otherwise.
378
 * @return PTE on success, NULL otherwise.
379
 */
379
 */
380
pte_t *find_mapping_and_check(__address badvaddr, int access, istate_t *istate, int *pfrc)
380
pte_t *find_mapping_and_check(__address badvaddr, int access, istate_t *istate, int *pfrc)
381
{
381
{
382
    entry_hi_t hi;
382
    entry_hi_t hi;
383
    pte_t *pte;
383
    pte_t *pte;
384
 
384
 
385
    hi.value = cp0_entry_hi_read();
385
    hi.value = cp0_entry_hi_read();
386
 
386
 
387
    /*
387
    /*
388
     * Handler cannot succeed if the ASIDs don't match.
388
     * Handler cannot succeed if the ASIDs don't match.
389
     */
389
     */
390
    if (hi.asid != AS->asid) {
390
    if (hi.asid != AS->asid) {
391
        printf("EntryHi.asid=%d, AS->asid=%d\n", hi.asid, AS->asid);
391
        printf("EntryHi.asid=%d, AS->asid=%d\n", hi.asid, AS->asid);
392
        return NULL;
392
        return NULL;
393
    }
393
    }
394
 
394
 
395
    /*
395
    /*
396
     * Check if the mapping exists in page tables.
396
     * Check if the mapping exists in page tables.
397
     */
397
     */
398
    pte = page_mapping_find(AS, badvaddr);
398
    pte = page_mapping_find(AS, badvaddr);
399
    if (pte && pte->p) {
399
    if (pte && pte->p) {
400
        /*
400
        /*
401
         * Mapping found in page tables.
401
         * Mapping found in page tables.
402
         * Immediately succeed.
402
         * Immediately succeed.
403
         */
403
         */
404
        return pte;
404
        return pte;
405
    } else {
405
    } else {
406
        int rc;
406
        int rc;
407
       
407
       
408
        /*
408
        /*
409
         * Mapping not found in page tables.
409
         * Mapping not found in page tables.
410
         * Resort to higher-level page fault handler.
410
         * Resort to higher-level page fault handler.
411
         */
411
         */
412
        page_table_unlock(AS, true);
412
        page_table_unlock(AS, true);
413
        switch (rc = as_page_fault(badvaddr, access, istate)) {
413
        switch (rc = as_page_fault(badvaddr, access, istate)) {
414
        case AS_PF_OK:
414
        case AS_PF_OK:
415
            /*
415
            /*
416
             * The higher-level page fault handler succeeded,
416
             * The higher-level page fault handler succeeded,
417
             * The mapping ought to be in place.
417
             * The mapping ought to be in place.
418
             */
418
             */
419
            page_table_lock(AS, true);
419
            page_table_lock(AS, true);
420
            pte = page_mapping_find(AS, badvaddr);
420
            pte = page_mapping_find(AS, badvaddr);
421
            ASSERT(pte && pte->p);
421
            ASSERT(pte && pte->p);
422
            return pte;
422
            return pte;
423
            break;
423
            break;
424
        case AS_PF_DEFER:
424
        case AS_PF_DEFER:
425
            page_table_lock(AS, true);
425
            page_table_lock(AS, true);
426
            *pfrc = AS_PF_DEFER;
426
            *pfrc = AS_PF_DEFER;
427
            return NULL;
427
            return NULL;
428
            break;
428
            break;
429
        case AS_PF_FAULT:
429
        case AS_PF_FAULT:
430
            page_table_lock(AS, true);
430
            page_table_lock(AS, true);
431
            printf("Page fault.\n");
431
            printf("Page fault.\n");
432
            *pfrc = AS_PF_FAULT;
432
            *pfrc = AS_PF_FAULT;
433
            return NULL;
433
            return NULL;
434
            break;
434
            break;
435
        default:
435
        default:
436
            panic("unexpected rc (%d)\n", rc);
436
            panic("unexpected rc (%d)\n", rc);
437
        }
437
        }
438
       
438
       
439
    }
439
    }
440
}
440
}
441
 
441
 
442
void prepare_entry_lo(entry_lo_t *lo, bool g, bool v, bool d, bool cacheable, __address pfn)
442
void prepare_entry_lo(entry_lo_t *lo, bool g, bool v, bool d, bool cacheable, __address pfn)
443
{
443
{
444
    lo->value = 0;
444
    lo->value = 0;
445
    lo->g = g;
445
    lo->g = g;
446
    lo->v = v;
446
    lo->v = v;
447
    lo->d = d;
447
    lo->d = d;
448
    lo->c = cacheable ? PAGE_CACHEABLE_EXC_WRITE : PAGE_UNCACHED;
448
    lo->c = cacheable ? PAGE_CACHEABLE_EXC_WRITE : PAGE_UNCACHED;
449
    lo->pfn = pfn;
449
    lo->pfn = pfn;
450
}
450
}
451
 
451
 
452
void prepare_entry_hi(entry_hi_t *hi, asid_t asid, __address addr)
452
void prepare_entry_hi(entry_hi_t *hi, asid_t asid, __address addr)
453
{
453
{
454
    hi->value = ALIGN_DOWN(addr, PAGE_SIZE * 2);
454
    hi->value = ALIGN_DOWN(addr, PAGE_SIZE * 2);
455
    hi->asid = asid;
455
    hi->asid = asid;
456
}
456
}
457
 
457
 
458
/** Print contents of TLB. */
458
/** Print contents of TLB. */
459
void tlb_print(void)
459
void tlb_print(void)
460
{
460
{
461
    page_mask_t mask;
461
    page_mask_t mask;
462
    entry_lo_t lo0, lo1;
462
    entry_lo_t lo0, lo1;
463
    entry_hi_t hi, hi_save;
463
    entry_hi_t hi, hi_save;
464
    int i;
464
    int i;
465
 
465
 
466
    hi_save.value = cp0_entry_hi_read();
466
    hi_save.value = cp0_entry_hi_read();
467
 
467
 
468
    printf("TLB:\n");
468
    printf("TLB:\n");
469
    for (i = 0; i < TLB_ENTRY_COUNT; i++) {
469
    for (i = 0; i < TLB_ENTRY_COUNT; i++) {
470
        cp0_index_write(i);
470
        cp0_index_write(i);
471
        tlbr();
471
        tlbr();
472
       
472
       
473
        mask.value = cp0_pagemask_read();
473
        mask.value = cp0_pagemask_read();
474
        hi.value = cp0_entry_hi_read();
474
        hi.value = cp0_entry_hi_read();
475
        lo0.value = cp0_entry_lo0_read();
475
        lo0.value = cp0_entry_lo0_read();
476
        lo1.value = cp0_entry_lo1_read();
476
        lo1.value = cp0_entry_lo1_read();
477
       
477
       
478
        printf("%d: asid=%d, vpn2=%d, mask=%d\tg[0]=%d, v[0]=%d, d[0]=%d, c[0]=%hhd, pfn[0]=%d\n"
478
        printf("%d: asid=%d, vpn2=%d, mask=%d\tg[0]=%d, v[0]=%d, d[0]=%d, c[0]=%hhd, pfn[0]=%d\n"
479
               "\t\t\t\tg[1]=%d, v[1]=%d, d[1]=%d, c[1]=%hhd, pfn[1]=%d\n",
479
               "\t\t\t\tg[1]=%d, v[1]=%d, d[1]=%d, c[1]=%hhd, pfn[1]=%d\n",
480
               i, hi.asid, hi.vpn2, mask.mask, lo0.g, lo0.v, lo0.d, lo0.c, lo0.pfn,
480
               i, hi.asid, hi.vpn2, mask.mask, lo0.g, lo0.v, lo0.d, lo0.c, lo0.pfn,
481
               lo1.g, lo1.v, lo1.d, lo1.c, lo1.pfn);
481
               lo1.g, lo1.v, lo1.d, lo1.c, lo1.pfn);
482
    }
482
    }
483
   
483
   
484
    cp0_entry_hi_write(hi_save.value);
484
    cp0_entry_hi_write(hi_save.value);
485
}
485
}
486
 
486
 
487
/** Invalidate all not wired TLB entries. */
487
/** Invalidate all not wired TLB entries. */
488
void tlb_invalidate_all(void)
488
void tlb_invalidate_all(void)
489
{
489
{
490
    ipl_t ipl;
490
    ipl_t ipl;
491
    entry_lo_t lo0, lo1;
491
    entry_lo_t lo0, lo1;
492
    entry_hi_t hi_save;
492
    entry_hi_t hi_save;
493
    int i;
493
    int i;
494
 
494
 
495
    hi_save.value = cp0_entry_hi_read();
495
    hi_save.value = cp0_entry_hi_read();
496
    ipl = interrupts_disable();
496
    ipl = interrupts_disable();
497
 
497
 
498
    for (i = TLB_WIRED; i < TLB_ENTRY_COUNT; i++) {
498
    for (i = TLB_WIRED; i < TLB_ENTRY_COUNT; i++) {
499
        cp0_index_write(i);
499
        cp0_index_write(i);
500
        tlbr();
500
        tlbr();
501
 
501
 
502
        lo0.value = cp0_entry_lo0_read();
502
        lo0.value = cp0_entry_lo0_read();
503
        lo1.value = cp0_entry_lo1_read();
503
        lo1.value = cp0_entry_lo1_read();
504
 
504
 
505
        lo0.v = 0;
505
        lo0.v = 0;
506
        lo1.v = 0;
506
        lo1.v = 0;
507
 
507
 
508
        cp0_entry_lo0_write(lo0.value);
508
        cp0_entry_lo0_write(lo0.value);
509
        cp0_entry_lo1_write(lo1.value);
509
        cp0_entry_lo1_write(lo1.value);
510
               
510
               
511
        tlbwi();
511
        tlbwi();
512
    }
512
    }
513
   
513
   
514
    interrupts_restore(ipl);
514
    interrupts_restore(ipl);
515
    cp0_entry_hi_write(hi_save.value);
515
    cp0_entry_hi_write(hi_save.value);
516
}
516
}
517
 
517
 
518
/** Invalidate all TLB entries belonging to specified address space.
518
/** Invalidate all TLB entries belonging to specified address space.
519
 *
519
 *
520
 * @param asid Address space identifier.
520
 * @param asid Address space identifier.
521
 */
521
 */
522
void tlb_invalidate_asid(asid_t asid)
522
void tlb_invalidate_asid(asid_t asid)
523
{
523
{
524
    ipl_t ipl;
524
    ipl_t ipl;
525
    entry_lo_t lo0, lo1;
525
    entry_lo_t lo0, lo1;
526
    entry_hi_t hi, hi_save;
526
    entry_hi_t hi, hi_save;
527
    int i;
527
    int i;
528
 
528
 
529
    ASSERT(asid != ASID_INVALID);
529
    ASSERT(asid != ASID_INVALID);
530
 
530
 
531
    hi_save.value = cp0_entry_hi_read();
531
    hi_save.value = cp0_entry_hi_read();
532
    ipl = interrupts_disable();
532
    ipl = interrupts_disable();
533
   
533
   
534
    for (i = 0; i < TLB_ENTRY_COUNT; i++) {
534
    for (i = 0; i < TLB_ENTRY_COUNT; i++) {
535
        cp0_index_write(i);
535
        cp0_index_write(i);
536
        tlbr();
536
        tlbr();
537
       
537
       
538
        hi.value = cp0_entry_hi_read();
538
        hi.value = cp0_entry_hi_read();
539
       
539
       
540
        if (hi.asid == asid) {
540
        if (hi.asid == asid) {
541
            lo0.value = cp0_entry_lo0_read();
541
            lo0.value = cp0_entry_lo0_read();
542
            lo1.value = cp0_entry_lo1_read();
542
            lo1.value = cp0_entry_lo1_read();
543
 
543
 
544
            lo0.v = 0;
544
            lo0.v = 0;
545
            lo1.v = 0;
545
            lo1.v = 0;
546
 
546
 
547
            cp0_entry_lo0_write(lo0.value);
547
            cp0_entry_lo0_write(lo0.value);
548
            cp0_entry_lo1_write(lo1.value);
548
            cp0_entry_lo1_write(lo1.value);
549
 
549
 
550
            tlbwi();
550
            tlbwi();
551
        }
551
        }
552
    }
552
    }
553
   
553
   
554
    interrupts_restore(ipl);
554
    interrupts_restore(ipl);
555
    cp0_entry_hi_write(hi_save.value);
555
    cp0_entry_hi_write(hi_save.value);
556
}
556
}
557
 
557
 
558
/** Invalidate TLB entries for specified page range belonging to specified address space.
558
/** Invalidate TLB entries for specified page range belonging to specified address space.
559
 *
559
 *
560
 * @param asid Address space identifier.
560
 * @param asid Address space identifier.
561
 * @param page First page whose TLB entry is to be invalidated.
561
 * @param page First page whose TLB entry is to be invalidated.
562
 * @param cnt Number of entries to invalidate.
562
 * @param cnt Number of entries to invalidate.
563
 */
563
 */
564
void tlb_invalidate_pages(asid_t asid, __address page, count_t cnt)
564
void tlb_invalidate_pages(asid_t asid, __address page, count_t cnt)
565
{
565
{
566
    int i;
566
    int i;
567
    ipl_t ipl;
567
    ipl_t ipl;
568
    entry_lo_t lo0, lo1;
568
    entry_lo_t lo0, lo1;
569
    entry_hi_t hi, hi_save;
569
    entry_hi_t hi, hi_save;
570
    tlb_index_t index;
570
    tlb_index_t index;
571
 
571
 
572
    ASSERT(asid != ASID_INVALID);
572
    ASSERT(asid != ASID_INVALID);
573
 
573
 
574
    hi_save.value = cp0_entry_hi_read();
574
    hi_save.value = cp0_entry_hi_read();
575
    ipl = interrupts_disable();
575
    ipl = interrupts_disable();
576
 
576
 
577
    for (i = 0; i < cnt+1; i+=2) {
577
    for (i = 0; i < cnt+1; i+=2) {
578
        hi.value = 0;
578
        hi.value = 0;
579
        prepare_entry_hi(&hi, asid, page + i * PAGE_SIZE);
579
        prepare_entry_hi(&hi, asid, page + i * PAGE_SIZE);
580
        cp0_entry_hi_write(hi.value);
580
        cp0_entry_hi_write(hi.value);
581
 
581
 
582
        tlbp();
582
        tlbp();
583
        index.value = cp0_index_read();
583
        index.value = cp0_index_read();
584
 
584
 
585
        if (!index.p) {
585
        if (!index.p) {
586
            /* Entry was found, index register contains valid index. */
586
            /* Entry was found, index register contains valid index. */
587
            tlbr();
587
            tlbr();
588
 
588
 
589
            lo0.value = cp0_entry_lo0_read();
589
            lo0.value = cp0_entry_lo0_read();
590
            lo1.value = cp0_entry_lo1_read();
590
            lo1.value = cp0_entry_lo1_read();
591
 
591
 
592
            lo0.v = 0;
592
            lo0.v = 0;
593
            lo1.v = 0;
593
            lo1.v = 0;
594
 
594
 
595
            cp0_entry_lo0_write(lo0.value);
595
            cp0_entry_lo0_write(lo0.value);
596
            cp0_entry_lo1_write(lo1.value);
596
            cp0_entry_lo1_write(lo1.value);
597
 
597
 
598
            tlbwi();
598
            tlbwi();
599
        }
599
        }
600
    }
600
    }
601
   
601
   
602
    interrupts_restore(ipl);
602
    interrupts_restore(ipl);
603
    cp0_entry_hi_write(hi_save.value);
603
    cp0_entry_hi_write(hi_save.value);
604
}
604
}
605
 
605