Subversion Repositories HelenOS-historic

Rev

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

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