Subversion Repositories HelenOS

Rev

Rev 3386 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3386 Rev 4153
Line 24... Line 24...
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 mips32mm   
29
/** @addtogroup mips32mm
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
Line 38... Line 38...
38
#include <mm/page.h>
38
#include <mm/page.h>
39
#include <mm/as.h>
39
#include <mm/as.h>
40
#include <arch/cp0.h>
40
#include <arch/cp0.h>
41
#include <panic.h>
41
#include <panic.h>
42
#include <arch.h>
42
#include <arch.h>
43
#include <symtab.h>
-
 
44
#include <synch/spinlock.h>
43
#include <synch/mutex.h>
45
#include <print.h>
44
#include <print.h>
46
#include <debug.h>
45
#include <debug.h>
47
#include <align.h>
46
#include <align.h>
48
#include <interrupt.h>
47
#include <interrupt.h>
-
 
48
#include <symtab.h>
49
 
49
 
50
static void tlb_refill_fail(istate_t *istate);
50
static void tlb_refill_fail(istate_t *);
51
static void tlb_invalid_fail(istate_t *istate);
51
static void tlb_invalid_fail(istate_t *);
52
static void tlb_modified_fail(istate_t *istate);
52
static void tlb_modified_fail(istate_t *);
53
 
53
 
54
static pte_t *find_mapping_and_check(uintptr_t badvaddr, int access, istate_t *istate, int *pfrc);
54
static pte_t *find_mapping_and_check(uintptr_t, int, istate_t *, int *);
55
 
55
 
56
/** Initialize TLB
56
/** Initialize TLB.
57
 *
57
 *
58
 * Initialize TLB.
-
 
59
 * Invalidate all entries and mark wired entries.
58
 * Invalidate all entries and mark wired entries.
60
 */
59
 */
61
void tlb_arch_init(void)
60
void tlb_arch_init(void)
62
{
61
{
63
    int i;
62
    int i;
Line 79... Line 78...
79
     * entries (e.g. mapping kernel stacks in kseg3).
78
     * entries (e.g. mapping kernel stacks in kseg3).
80
     */
79
     */
81
    cp0_wired_write(TLB_WIRED);
80
    cp0_wired_write(TLB_WIRED);
82
}
81
}
83
 
82
 
84
/** Process TLB Refill Exception
83
/** Process TLB Refill Exception.
85
 *
-
 
86
 * Process TLB Refill Exception.
-
 
87
 *
84
 *
88
 * @param istate Interrupted register context.
85
 * @param istate    Interrupted register context.
89
 */
86
 */
90
void tlb_refill(istate_t *istate)
87
void tlb_refill(istate_t *istate)
91
{
88
{
92
    entry_lo_t lo;
89
    entry_lo_t lo;
93
    entry_hi_t hi;
90
    entry_hi_t hi;
94
    asid_t asid;
91
    asid_t asid;
95
    uintptr_t badvaddr;
92
    uintptr_t badvaddr;
96
    pte_t *pte;
93
    pte_t *pte;
97
    int pfrc;
94
    int pfrc;
98
 
95
   
99
    badvaddr = cp0_badvaddr_read();
96
    badvaddr = cp0_badvaddr_read();
100
 
97
   
101
    spinlock_lock(&AS->lock);
98
    mutex_lock(&AS->lock);
102
    asid = AS->asid;
99
    asid = AS->asid;
103
    spinlock_unlock(&AS->lock);
100
    mutex_unlock(&AS->lock);
104
 
101
   
105
    page_table_lock(AS, true);
102
    page_table_lock(AS, true);
106
 
103
   
107
    pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate, &pfrc);
104
    pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate, &pfrc);
108
    if (!pte) {
105
    if (!pte) {
109
        switch (pfrc) {
106
        switch (pfrc) {
110
        case AS_PF_FAULT:
107
        case AS_PF_FAULT:
111
            goto fail;
108
            goto fail;
Line 116... Line 113...
116
             * or copy_to_uspace().
113
             * or copy_to_uspace().
117
             */
114
             */
118
            page_table_unlock(AS, true);
115
            page_table_unlock(AS, true);
119
            return;
116
            return;
120
        default:
117
        default:
121
            panic("unexpected pfrc (%d)\n", pfrc);
118
            panic("Unexpected pfrc (%d).", pfrc);
122
        }
119
        }
123
    }
120
    }
124
 
121
 
125
    /*
122
    /*
126
     * Record access to PTE.
123
     * Record access to PTE.
127
     */
124
     */
128
    pte->a = 1;
125
    pte->a = 1;
129
 
126
 
130
    tlb_prepare_entry_hi(&hi, asid, badvaddr);
127
    tlb_prepare_entry_hi(&hi, asid, badvaddr);
131
    tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable, pte->pfn);
128
    tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable,
-
 
129
        pte->pfn);
132
 
130
 
133
    /*
131
    /*
134
     * New entry is to be inserted into TLB
132
     * New entry is to be inserted into TLB
135
     */
133
     */
136
    cp0_entry_hi_write(hi.value);
134
    cp0_entry_hi_write(hi.value);
137
    if ((badvaddr/PAGE_SIZE) % 2 == 0) {
135
    if ((badvaddr / PAGE_SIZE) % 2 == 0) {
138
        cp0_entry_lo0_write(lo.value);
136
        cp0_entry_lo0_write(lo.value);
139
        cp0_entry_lo1_write(0);
137
        cp0_entry_lo1_write(0);
140
    }
138
    }
141
    else {
139
    else {
142
        cp0_entry_lo0_write(0);
140
        cp0_entry_lo0_write(0);
Line 151... Line 149...
151
fail:
149
fail:
152
    page_table_unlock(AS, true);
150
    page_table_unlock(AS, true);
153
    tlb_refill_fail(istate);
151
    tlb_refill_fail(istate);
154
}
152
}
155
 
153
 
156
/** Process TLB Invalid Exception
154
/** Process TLB Invalid Exception.
157
 *
-
 
158
 * Process TLB Invalid Exception.
-
 
159
 *
155
 *
160
 * @param istate Interrupted register context.
156
 * @param istate    Interrupted register context.
161
 */
157
 */
162
void tlb_invalid(istate_t *istate)
158
void tlb_invalid(istate_t *istate)
163
{
159
{
164
    tlb_index_t index;
160
    tlb_index_t index;
165
    uintptr_t badvaddr;
161
    uintptr_t badvaddr;
Line 201... Line 197...
201
             * or copy_to_uspace().
197
             * or copy_to_uspace().
202
             */
198
             */
203
            page_table_unlock(AS, true);             
199
            page_table_unlock(AS, true);             
204
            return;
200
            return;
205
        default:
201
        default:
206
            panic("unexpected pfrc (%d)\n", pfrc);
202
            panic("Unexpected pfrc (%d).", pfrc);
207
        }
203
        }
208
    }
204
    }
209
 
205
 
210
    /*
206
    /*
211
     * Read the faulting TLB entry.
207
     * Read the faulting TLB entry.
Line 215... Line 211...
215
    /*
211
    /*
216
     * Record access to PTE.
212
     * Record access to PTE.
217
     */
213
     */
218
    pte->a = 1;
214
    pte->a = 1;
219
 
215
 
220
    tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable, pte->pfn);
216
    tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable,
-
 
217
        pte->pfn);
221
 
218
 
222
    /*
219
    /*
223
     * The entry is to be updated in TLB.
220
     * The entry is to be updated in TLB.
224
     */
221
     */
225
    if ((badvaddr/PAGE_SIZE) % 2 == 0)
222
    if ((badvaddr / PAGE_SIZE) % 2 == 0)
226
        cp0_entry_lo0_write(lo.value);
223
        cp0_entry_lo0_write(lo.value);
227
    else
224
    else
228
        cp0_entry_lo1_write(lo.value);
225
        cp0_entry_lo1_write(lo.value);
229
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
226
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
230
    tlbwi();
227
    tlbwi();
Line 235... Line 232...
235
fail:
232
fail:
236
    page_table_unlock(AS, true);
233
    page_table_unlock(AS, true);
237
    tlb_invalid_fail(istate);
234
    tlb_invalid_fail(istate);
238
}
235
}
239
 
236
 
240
/** Process TLB Modified Exception
237
/** Process TLB Modified Exception.
241
 *
238
 *
242
 * Process TLB Modified Exception.
-
 
243
 *
-
 
244
 * @param istate Interrupted register context.
239
 * @param istate    Interrupted register context.
245
 */
240
 */
246
void tlb_modified(istate_t *istate)
241
void tlb_modified(istate_t *istate)
247
{
242
{
248
    tlb_index_t index;
243
    tlb_index_t index;
249
    uintptr_t badvaddr;
244
    uintptr_t badvaddr;
Line 285... Line 280...
285
             * or copy_to_uspace().
280
             * or copy_to_uspace().
286
             */
281
             */
287
            page_table_unlock(AS, true);             
282
            page_table_unlock(AS, true);             
288
            return;
283
            return;
289
        default:
284
        default:
290
            panic("unexpected pfrc (%d)\n", pfrc);
285
            panic("Unexpected pfrc (%d).", pfrc);
291
        }
286
        }
292
    }
287
    }
293
 
288
 
294
    /*
289
    /*
295
     * Fail if the page is not writable.
-
 
296
     */
-
 
297
    if (!pte->w)
-
 
298
        goto fail;
-
 
299
 
-
 
300
    /*
-
 
301
     * Read the faulting TLB entry.
290
     * Read the faulting TLB entry.
302
     */
291
     */
303
    tlbr();
292
    tlbr();
304
 
293
 
305
    /*
294
    /*
306
     * Record access and write to PTE.
295
     * Record access and write to PTE.
307
     */
296
     */
308
    pte->a = 1;
297
    pte->a = 1;
309
    pte->d = 1;
298
    pte->d = 1;
310
 
299
 
311
    tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->w, pte->cacheable, pte->pfn);
300
    tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->w, pte->cacheable,
-
 
301
        pte->pfn);
312
 
302
 
313
    /*
303
    /*
314
     * The entry is to be updated in TLB.
304
     * The entry is to be updated in TLB.
315
     */
305
     */
316
    if ((badvaddr/PAGE_SIZE) % 2 == 0)
306
    if ((badvaddr / PAGE_SIZE) % 2 == 0)
317
        cp0_entry_lo0_write(lo.value);
307
        cp0_entry_lo0_write(lo.value);
318
    else
308
    else
319
        cp0_entry_lo1_write(lo.value);
309
        cp0_entry_lo1_write(lo.value);
320
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
310
    cp0_pagemask_write(TLB_PAGE_MASK_16K);
321
    tlbwi();
311
    tlbwi();
Line 328... Line 318...
328
    tlb_modified_fail(istate);
318
    tlb_modified_fail(istate);
329
}
319
}
330
 
320
 
331
void tlb_refill_fail(istate_t *istate)
321
void tlb_refill_fail(istate_t *istate)
332
{
322
{
333
    char *symbol = "";
323
    char *symbol, *sym2;
334
    char *sym2 = "";
-
 
335
 
-
 
336
    char *s = get_symtab_entry(istate->epc);
-
 
337
    if (s)
-
 
338
        symbol = s;
-
 
339
    s = get_symtab_entry(istate->ra);
-
 
340
    if (s)
-
 
341
        sym2 = s;
-
 
342
 
324
 
-
 
325
    symbol = symtab_fmt_name_lookup(istate->epc);
-
 
326
    sym2 = symtab_fmt_name_lookup(istate->ra);
-
 
327
   
343
    fault_if_from_uspace(istate, "TLB Refill Exception on %p", cp0_badvaddr_read());
328
    fault_if_from_uspace(istate, "TLB Refill Exception on %p.",
-
 
329
        cp0_badvaddr_read());
344
    panic("%x: TLB Refill Exception at %x(%s<-%s)\n", cp0_badvaddr_read(), istate->epc, symbol, sym2);
330
    panic("%x: TLB Refill Exception at %x (%s<-%s).", cp0_badvaddr_read(),
-
 
331
        istate->epc, symbol, sym2);
345
}
332
}
346
 
333
 
347
 
334
 
348
void tlb_invalid_fail(istate_t *istate)
335
void tlb_invalid_fail(istate_t *istate)
349
{
336
{
350
    char *symbol = "";
337
    char *symbol;
-
 
338
 
-
 
339
    symbol = symtab_fmt_name_lookup(istate->epc);
351
 
340
 
352
    char *s = get_symtab_entry(istate->epc);
341
    fault_if_from_uspace(istate, "TLB Invalid Exception on %p.",
353
    if (s)
-
 
354
        symbol = s;
342
        cp0_badvaddr_read());
355
    fault_if_from_uspace(istate, "TLB Invalid Exception on %p", cp0_badvaddr_read());
343
    panic("%x: TLB Invalid Exception at %x (%s).", cp0_badvaddr_read(),
356
    panic("%x: TLB Invalid Exception at %x(%s)\n", cp0_badvaddr_read(), istate->epc, symbol);
344
        istate->epc, symbol);
357
}
345
}
358
 
346
 
359
void tlb_modified_fail(istate_t *istate)
347
void tlb_modified_fail(istate_t *istate)
360
{
348
{
361
    char *symbol = "";
349
    char *symbol;
-
 
350
 
-
 
351
    symbol = symtab_fmt_name_lookup(istate->epc);
362
 
352
 
363
    char *s = get_symtab_entry(istate->epc);
353
    fault_if_from_uspace(istate, "TLB Modified Exception on %p.",
364
    if (s)
-
 
365
        symbol = s;
354
        cp0_badvaddr_read());
366
    fault_if_from_uspace(istate, "TLB Modified Exception on %p", cp0_badvaddr_read());
355
    panic("%x: TLB Modified Exception at %x (%s).", cp0_badvaddr_read(),
367
    panic("%x: TLB Modified Exception at %x(%s)\n", cp0_badvaddr_read(), istate->epc, symbol);
356
        istate->epc, symbol);
368
}
357
}
369
 
358
 
370
/** Try to find PTE for faulting address
359
/** Try to find PTE for faulting address.
371
 *
360
 *
372
 * Try to find PTE for faulting address.
-
 
373
 * The AS->lock must be held on entry to this function.
361
 * The AS->lock must be held on entry to this function.
374
 *
362
 *
375
 * @param badvaddr Faulting virtual address.
363
 * @param badvaddr  Faulting virtual address.
376
 * @param access Access mode that caused the fault.
364
 * @param access    Access mode that caused the fault.
377
 * @param istate Pointer to interrupted state.
365
 * @param istate    Pointer to interrupted state.
378
 * @param pfrc Pointer to variable where as_page_fault() return code will be stored.
366
 * @param pfrc      Pointer to variable where as_page_fault() return code
-
 
367
 *          will be stored.
379
 *
368
 *
380
 * @return PTE on success, NULL otherwise.
369
 * @return      PTE on success, NULL otherwise.
381
 */
370
 */
-
 
371
pte_t *
382
pte_t *find_mapping_and_check(uintptr_t badvaddr, int access, istate_t *istate, int *pfrc)
372
find_mapping_and_check(uintptr_t badvaddr, int access, istate_t *istate,
-
 
373
    int *pfrc)
383
{
374
{
384
    entry_hi_t hi;
375
    entry_hi_t hi;
385
    pte_t *pte;
376
    pte_t *pte;
386
 
377
 
387
    hi.value = cp0_entry_hi_read();
378
    hi.value = cp0_entry_hi_read();
Line 396... Line 387...
396
 
387
 
397
    /*
388
    /*
398
     * Check if the mapping exists in page tables.
389
     * Check if the mapping exists in page tables.
399
     */
390
     */
400
    pte = page_mapping_find(AS, badvaddr);
391
    pte = page_mapping_find(AS, badvaddr);
401
    if (pte && pte->p) {
392
    if (pte && pte->p && (pte->w || access != PF_ACCESS_WRITE)) {
402
        /*
393
        /*
403
         * Mapping found in page tables.
394
         * Mapping found in page tables.
404
         * Immediately succeed.
395
         * Immediately succeed.
405
         */
396
         */
406
        return pte;
397
        return pte;
Line 419... Line 410...
419
             * The mapping ought to be in place.
410
             * The mapping ought to be in place.
420
             */
411
             */
421
            page_table_lock(AS, true);
412
            page_table_lock(AS, true);
422
            pte = page_mapping_find(AS, badvaddr);
413
            pte = page_mapping_find(AS, badvaddr);
423
            ASSERT(pte && pte->p);
414
            ASSERT(pte && pte->p);
-
 
415
            ASSERT(pte->w || access != PF_ACCESS_WRITE);
424
            return pte;
416
            return pte;
425
            break;
417
            break;
426
        case AS_PF_DEFER:
418
        case AS_PF_DEFER:
427
            page_table_lock(AS, true);
419
            page_table_lock(AS, true);
428
            *pfrc = AS_PF_DEFER;
420
            *pfrc = AS_PF_DEFER;
429
            return NULL;
421
            return NULL;
430
            break;
422
            break;
431
        case AS_PF_FAULT:
423
        case AS_PF_FAULT:
432
            page_table_lock(AS, true);
424
            page_table_lock(AS, true);
433
            printf("Page fault.\n");
-
 
434
            *pfrc = AS_PF_FAULT;
425
            *pfrc = AS_PF_FAULT;
435
            return NULL;
426
            return NULL;
436
            break;
427
            break;
437
        default:
428
        default:
438
            panic("unexpected rc (%d)\n", rc);
429
            panic("Unexpected rc (%d).", rc);
439
        }
430
        }
440
       
431
       
441
    }
432
    }
442
}
433
}
443
 
434
 
-
 
435
void
444
void tlb_prepare_entry_lo(entry_lo_t *lo, bool g, bool v, bool d, bool cacheable, uintptr_t pfn)
436
tlb_prepare_entry_lo(entry_lo_t *lo, bool g, bool v, bool d, bool cacheable,
-
 
437
    uintptr_t pfn)
445
{
438
{
446
    lo->value = 0;
439
    lo->value = 0;
447
    lo->g = g;
440
    lo->g = g;
448
    lo->v = v;
441
    lo->v = v;
449
    lo->d = d;
442
    lo->d = d;
Line 478... Line 471...
478
        hi.value = cp0_entry_hi_read();
471
        hi.value = cp0_entry_hi_read();
479
        lo0.value = cp0_entry_lo0_read();
472
        lo0.value = cp0_entry_lo0_read();
480
        lo1.value = cp0_entry_lo1_read();
473
        lo1.value = cp0_entry_lo1_read();
481
       
474
       
482
        printf("%-2u %-4u %#6x %#4x %1u %1u %1u %1u %#6x\n",
475
        printf("%-2u %-4u %#6x %#4x %1u %1u %1u %1u %#6x\n",
483
            i, hi.asid, hi.vpn2, mask.mask,
476
            i, hi.asid, hi.vpn2, mask.mask,
484
            lo0.g, lo0.v, lo0.d, lo0.c, lo0.pfn);
477
            lo0.g, lo0.v, lo0.d, lo0.c, lo0.pfn);
485
        printf("                    %1u %1u %1u %1u %#6x\n",
478
        printf("                    %1u %1u %1u %1u %#6x\n",
486
            lo1.g, lo1.v, lo1.d, lo1.c, lo1.pfn);
479
            lo1.g, lo1.v, lo1.d, lo1.c, lo1.pfn);
487
    }
480
    }
488
   
481
   
489
    cp0_entry_hi_write(hi_save.value);
482
    cp0_entry_hi_write(hi_save.value);
490
}
483
}
491
 
484
 
Line 558... Line 551...
558
   
551
   
559
    interrupts_restore(ipl);
552
    interrupts_restore(ipl);
560
    cp0_entry_hi_write(hi_save.value);
553
    cp0_entry_hi_write(hi_save.value);
561
}
554
}
562
 
555
 
563
/** Invalidate TLB entries for specified page range belonging to specified address space.
556
/** Invalidate TLB entries for specified page range belonging to specified
-
 
557
 * address space.
564
 *
558
 *
565
 * @param asid Address space identifier.
559
 * @param asid      Address space identifier.
566
 * @param page First page whose TLB entry is to be invalidated.
560
 * @param page      First page whose TLB entry is to be invalidated.
567
 * @param cnt Number of entries to invalidate.
561
 * @param cnt       Number of entries to invalidate.
568
 */
562
 */
569
void tlb_invalidate_pages(asid_t asid, uintptr_t page, count_t cnt)
563
void tlb_invalidate_pages(asid_t asid, uintptr_t page, count_t cnt)
570
{
564
{
571
    unsigned int i;
565
    unsigned int i;
572
    ipl_t ipl;
566
    ipl_t ipl;
Line 586... Line 580...
586
 
580
 
587
        tlbp();
581
        tlbp();
588
        index.value = cp0_index_read();
582
        index.value = cp0_index_read();
589
 
583
 
590
        if (!index.p) {
584
        if (!index.p) {
-
 
585
            /*
591
            /* Entry was found, index register contains valid index. */
586
             * Entry was found, index register contains valid
-
 
587
             * index.
-
 
588
             */
592
            tlbr();
589
            tlbr();
593
 
590
 
594
            lo0.value = cp0_entry_lo0_read();
591
            lo0.value = cp0_entry_lo0_read();
595
            lo1.value = cp0_entry_lo1_read();
592
            lo1.value = cp0_entry_lo1_read();
596
 
593