4 * Copyright (c) 2005 Samuel Tardieu
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #if !defined(CONFIG_USER_ONLY)
29 #include "hw/sh_intc.h"
32 #if defined(CONFIG_USER_ONLY)
34 void do_interrupt (CPUSH4State *env)
36 env->exception_index = -1;
39 int cpu_sh4_handle_mmu_fault(CPUSH4State * env, target_ulong address, int rw,
43 env->exception_index = -1;
46 env->exception_index = 0x0a0;
49 env->exception_index = 0x0c0;
52 env->exception_index = 0x0a0;
58 int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
60 /* For user mode, only U0 area is cachable. */
61 return !(addr & 0x80000000);
64 #else /* !CONFIG_USER_ONLY */
67 #define MMU_ITLB_MISS (-1)
68 #define MMU_ITLB_MULTIPLE (-2)
69 #define MMU_ITLB_VIOLATION (-3)
70 #define MMU_DTLB_MISS_READ (-4)
71 #define MMU_DTLB_MISS_WRITE (-5)
72 #define MMU_DTLB_INITIAL_WRITE (-6)
73 #define MMU_DTLB_VIOLATION_READ (-7)
74 #define MMU_DTLB_VIOLATION_WRITE (-8)
75 #define MMU_DTLB_MULTIPLE (-9)
76 #define MMU_DTLB_MISS (-10)
77 #define MMU_IADDR_ERROR (-11)
78 #define MMU_DADDR_ERROR_READ (-12)
79 #define MMU_DADDR_ERROR_WRITE (-13)
81 void do_interrupt(CPUSH4State * env)
83 int do_irq = env->interrupt_request & CPU_INTERRUPT_HARD;
84 int do_exp, irq_vector = env->exception_index;
86 /* prioritize exceptions over interrupts */
88 do_exp = env->exception_index != -1;
89 do_irq = do_irq && (env->exception_index == -1);
91 if (env->sr & SR_BL) {
92 if (do_exp && env->exception_index != 0x1e0) {
93 env->exception_index = 0x000; /* masked exception -> reset */
95 if (do_irq && !env->in_sleep) {
102 irq_vector = sh_intc_get_pending_vector(env->intc_handle,
103 (env->sr >> 4) & 0xf);
104 if (irq_vector == -1) {
109 if (qemu_loglevel_mask(CPU_LOG_INT)) {
111 switch (env->exception_index) {
113 expname = "addr_error";
116 expname = "tlb_miss";
119 expname = "tlb_violation";
122 expname = "illegal_instruction";
125 expname = "slot_illegal_instruction";
128 expname = "fpu_disable";
131 expname = "slot_fpu";
134 expname = "data_write";
137 expname = "dtlb_miss_write";
140 expname = "dtlb_violation_write";
143 expname = "fpu_exception";
146 expname = "initial_page_write";
152 expname = do_irq ? "interrupt" : "???";
155 qemu_log("exception 0x%03x [%s] raised\n",
156 irq_vector, expname);
157 log_cpu_state(env, 0);
162 env->sgr = env->gregs[15];
163 env->sr |= SR_BL | SR_MD | SR_RB;
165 if (env->flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL)) {
166 /* Branch instruction should be executed again before delay slot. */
168 /* Clear flags for exception/interrupt routine. */
169 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL | DELAY_SLOT_TRUE);
171 if (env->flags & DELAY_SLOT_CLEARME)
175 env->expevt = env->exception_index;
176 switch (env->exception_index) {
181 env->sr |= 0xf << 4; /* IMASK */
182 env->pc = 0xa0000000;
186 env->pc = env->vbr + 0x400;
189 env->spc += 2; /* special case for TRAPA */
192 env->pc = env->vbr + 0x100;
199 env->intevt = irq_vector;
200 env->pc = env->vbr + 0x600;
205 static void update_itlb_use(CPUSH4State * env, int itlbnb)
207 uint8_t or_mask = 0, and_mask = (uint8_t) - 1;
226 env->mmucr &= (and_mask << 24) | 0x00ffffff;
227 env->mmucr |= (or_mask << 24);
230 static int itlb_replacement(CPUSH4State * env)
232 if ((env->mmucr & 0xe0000000) == 0xe0000000)
234 if ((env->mmucr & 0x98000000) == 0x18000000)
236 if ((env->mmucr & 0x54000000) == 0x04000000)
238 if ((env->mmucr & 0x2c000000) == 0x00000000)
240 cpu_abort(env, "Unhandled itlb_replacement");
243 /* Find the corresponding entry in the right TLB
244 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
246 static int find_tlb_entry(CPUSH4State * env, target_ulong address,
247 tlb_t * entries, uint8_t nbtlb, int use_asid)
249 int match = MMU_DTLB_MISS;
254 asid = env->pteh & 0xff;
256 for (i = 0; i < nbtlb; i++) {
258 continue; /* Invalid entry */
259 if (!entries[i].sh && use_asid && entries[i].asid != asid)
260 continue; /* Bad ASID */
261 start = (entries[i].vpn << 10) & ~(entries[i].size - 1);
262 end = start + entries[i].size - 1;
263 if (address >= start && address <= end) { /* Match */
264 if (match != MMU_DTLB_MISS)
265 return MMU_DTLB_MULTIPLE; /* Multiple match */
272 static void increment_urc(CPUSH4State * env)
277 urb = ((env->mmucr) >> 18) & 0x3f;
278 urc = ((env->mmucr) >> 10) & 0x3f;
280 if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1))
282 env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
285 /* Copy and utlb entry into itlb
288 static int copy_utlb_entry_itlb(CPUSH4State *env, int utlb)
293 itlb = itlb_replacement(env);
294 ientry = &env->itlb[itlb];
296 tlb_flush_page(env, ientry->vpn << 10);
298 *ientry = env->utlb[utlb];
299 update_itlb_use(env, itlb);
304 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
306 static int find_itlb_entry(CPUSH4State * env, target_ulong address,
311 e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid);
312 if (e == MMU_DTLB_MULTIPLE) {
313 e = MMU_ITLB_MULTIPLE;
314 } else if (e == MMU_DTLB_MISS) {
317 update_itlb_use(env, e);
323 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
324 static int find_utlb_entry(CPUSH4State * env, target_ulong address, int use_asid)
326 /* per utlb access */
330 return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
333 /* Match address against MMU
334 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
335 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
336 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
337 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
338 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
340 static int get_mmu_address(CPUSH4State * env, target_ulong * physical,
341 int *prot, target_ulong address,
342 int rw, int access_type)
345 tlb_t *matching = NULL;
347 use_asid = (env->mmucr & MMUCR_SV) == 0 || (env->sr & SR_MD) == 0;
350 n = find_itlb_entry(env, address, use_asid);
352 matching = &env->itlb[n];
353 if (!(env->sr & SR_MD) && !(matching->pr & 2))
354 n = MMU_ITLB_VIOLATION;
358 n = find_utlb_entry(env, address, use_asid);
360 n = copy_utlb_entry_itlb(env, n);
361 matching = &env->itlb[n];
362 if (!(env->sr & SR_MD) && !(matching->pr & 2)) {
363 n = MMU_ITLB_VIOLATION;
365 *prot = PAGE_READ | PAGE_EXEC;
366 if ((matching->pr & 1) && matching->d) {
370 } else if (n == MMU_DTLB_MULTIPLE) {
371 n = MMU_ITLB_MULTIPLE;
372 } else if (n == MMU_DTLB_MISS) {
377 n = find_utlb_entry(env, address, use_asid);
379 matching = &env->utlb[n];
380 if (!(env->sr & SR_MD) && !(matching->pr & 2)) {
381 n = (rw == 1) ? MMU_DTLB_VIOLATION_WRITE :
382 MMU_DTLB_VIOLATION_READ;
383 } else if ((rw == 1) && !(matching->pr & 1)) {
384 n = MMU_DTLB_VIOLATION_WRITE;
385 } else if ((rw == 1) && !matching->d) {
386 n = MMU_DTLB_INITIAL_WRITE;
389 if ((matching->pr & 1) && matching->d) {
393 } else if (n == MMU_DTLB_MISS) {
394 n = (rw == 1) ? MMU_DTLB_MISS_WRITE :
400 *physical = ((matching->ppn << 10) & ~(matching->size - 1)) |
401 (address & (matching->size - 1));
406 static int get_physical_address(CPUSH4State * env, target_ulong * physical,
407 int *prot, target_ulong address,
408 int rw, int access_type)
410 /* P1, P2 and P4 areas do not use translation */
411 if ((address >= 0x80000000 && address < 0xc0000000) ||
412 address >= 0xe0000000) {
413 if (!(env->sr & SR_MD)
414 && (address < 0xe0000000 || address >= 0xe4000000)) {
415 /* Unauthorized access in user mode (only store queues are available) */
416 fprintf(stderr, "Unauthorized access\n");
418 return MMU_DADDR_ERROR_READ;
420 return MMU_DADDR_ERROR_WRITE;
422 return MMU_IADDR_ERROR;
424 if (address >= 0x80000000 && address < 0xc0000000) {
425 /* Mask upper 3 bits for P1 and P2 areas */
426 *physical = address & 0x1fffffff;
430 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
434 /* If MMU is disabled, return the corresponding physical page */
435 if (!(env->mmucr & MMUCR_AT)) {
436 *physical = address & 0x1FFFFFFF;
437 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
441 /* We need to resort to the MMU */
442 return get_mmu_address(env, physical, prot, address, rw, access_type);
445 int cpu_sh4_handle_mmu_fault(CPUSH4State * env, target_ulong address, int rw,
448 target_ulong physical;
449 int prot, ret, access_type;
451 access_type = ACCESS_INT;
453 get_physical_address(env, &physical, &prot, address, rw,
458 if (ret != MMU_DTLB_MULTIPLE && ret != MMU_ITLB_MULTIPLE) {
459 env->pteh = (env->pteh & PTEH_ASID_MASK) |
460 (address & PTEH_VPN_MASK);
464 case MMU_DTLB_MISS_READ:
465 env->exception_index = 0x040;
467 case MMU_DTLB_MULTIPLE:
468 case MMU_ITLB_MULTIPLE:
469 env->exception_index = 0x140;
471 case MMU_ITLB_VIOLATION:
472 env->exception_index = 0x0a0;
474 case MMU_DTLB_MISS_WRITE:
475 env->exception_index = 0x060;
477 case MMU_DTLB_INITIAL_WRITE:
478 env->exception_index = 0x080;
480 case MMU_DTLB_VIOLATION_READ:
481 env->exception_index = 0x0a0;
483 case MMU_DTLB_VIOLATION_WRITE:
484 env->exception_index = 0x0c0;
486 case MMU_IADDR_ERROR:
487 case MMU_DADDR_ERROR_READ:
488 env->exception_index = 0x0e0;
490 case MMU_DADDR_ERROR_WRITE:
491 env->exception_index = 0x100;
494 cpu_abort(env, "Unhandled MMU fault");
499 address &= TARGET_PAGE_MASK;
500 physical &= TARGET_PAGE_MASK;
502 tlb_set_page(env, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE);
506 hwaddr cpu_get_phys_page_debug(CPUSH4State * env, target_ulong addr)
508 target_ulong physical;
511 get_physical_address(env, &physical, &prot, addr, 0, 0);
515 void cpu_load_tlb(CPUSH4State * env)
517 int n = cpu_mmucr_urc(env->mmucr);
518 tlb_t * entry = &env->utlb[n];
521 /* Overwriting valid entry in utlb. */
522 target_ulong address = entry->vpn << 10;
523 tlb_flush_page(env, address);
526 /* Take values into cpu status from registers. */
527 entry->asid = (uint8_t)cpu_pteh_asid(env->pteh);
528 entry->vpn = cpu_pteh_vpn(env->pteh);
529 entry->v = (uint8_t)cpu_ptel_v(env->ptel);
530 entry->ppn = cpu_ptel_ppn(env->ptel);
531 entry->sz = (uint8_t)cpu_ptel_sz(env->ptel);
534 entry->size = 1024; /* 1K */
537 entry->size = 1024 * 4; /* 4K */
540 entry->size = 1024 * 64; /* 64K */
543 entry->size = 1024 * 1024; /* 1M */
546 cpu_abort(env, "Unhandled load_tlb");
549 entry->sh = (uint8_t)cpu_ptel_sh(env->ptel);
550 entry->c = (uint8_t)cpu_ptel_c(env->ptel);
551 entry->pr = (uint8_t)cpu_ptel_pr(env->ptel);
552 entry->d = (uint8_t)cpu_ptel_d(env->ptel);
553 entry->wt = (uint8_t)cpu_ptel_wt(env->ptel);
554 entry->sa = (uint8_t)cpu_ptea_sa(env->ptea);
555 entry->tc = (uint8_t)cpu_ptea_tc(env->ptea);
558 void cpu_sh4_invalidate_tlb(CPUSH4State *s)
563 for (i = 0; i < UTLB_SIZE; i++) {
564 tlb_t * entry = &s->utlb[i];
568 for (i = 0; i < ITLB_SIZE; i++) {
569 tlb_t * entry = &s->itlb[i];
576 uint32_t cpu_sh4_read_mmaped_itlb_addr(CPUSH4State *s,
579 int index = (addr & 0x00000300) >> 8;
580 tlb_t * entry = &s->itlb[index];
582 return (entry->vpn << 10) |
587 void cpu_sh4_write_mmaped_itlb_addr(CPUSH4State *s, hwaddr addr,
590 uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
591 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
592 uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
594 int index = (addr & 0x00000300) >> 8;
595 tlb_t * entry = &s->itlb[index];
597 /* Overwriting valid entry in itlb. */
598 target_ulong address = entry->vpn << 10;
599 tlb_flush_page(s, address);
606 uint32_t cpu_sh4_read_mmaped_itlb_data(CPUSH4State *s,
609 int array = (addr & 0x00800000) >> 23;
610 int index = (addr & 0x00000300) >> 8;
611 tlb_t * entry = &s->itlb[index];
614 /* ITLB Data Array 1 */
615 return (entry->ppn << 10) |
618 ((entry->sz & 1) << 6) |
619 ((entry->sz & 2) << 4) |
623 /* ITLB Data Array 2 */
624 return (entry->tc << 1) |
629 void cpu_sh4_write_mmaped_itlb_data(CPUSH4State *s, hwaddr addr,
632 int array = (addr & 0x00800000) >> 23;
633 int index = (addr & 0x00000300) >> 8;
634 tlb_t * entry = &s->itlb[index];
637 /* ITLB Data Array 1 */
639 /* Overwriting valid entry in utlb. */
640 target_ulong address = entry->vpn << 10;
641 tlb_flush_page(s, address);
643 entry->ppn = (mem_value & 0x1ffffc00) >> 10;
644 entry->v = (mem_value & 0x00000100) >> 8;
645 entry->sz = (mem_value & 0x00000080) >> 6 |
646 (mem_value & 0x00000010) >> 4;
647 entry->pr = (mem_value & 0x00000040) >> 5;
648 entry->c = (mem_value & 0x00000008) >> 3;
649 entry->sh = (mem_value & 0x00000002) >> 1;
651 /* ITLB Data Array 2 */
652 entry->tc = (mem_value & 0x00000008) >> 3;
653 entry->sa = (mem_value & 0x00000007);
657 uint32_t cpu_sh4_read_mmaped_utlb_addr(CPUSH4State *s,
660 int index = (addr & 0x00003f00) >> 8;
661 tlb_t * entry = &s->utlb[index];
663 increment_urc(s); /* per utlb access */
665 return (entry->vpn << 10) |
670 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State *s, hwaddr addr,
673 int associate = addr & 0x0000080;
674 uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
675 uint8_t d = (uint8_t)((mem_value & 0x00000200) >> 9);
676 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
677 uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
678 int use_asid = (s->mmucr & MMUCR_SV) == 0 || (s->sr & SR_MD) == 0;
682 tlb_t * utlb_match_entry = NULL;
683 int needs_tlb_flush = 0;
686 for (i = 0; i < UTLB_SIZE; i++) {
687 tlb_t * entry = &s->utlb[i];
691 if (entry->vpn == vpn
692 && (!use_asid || entry->asid == asid || entry->sh)) {
693 if (utlb_match_entry) {
694 /* Multiple TLB Exception */
695 s->exception_index = 0x140;
703 utlb_match_entry = entry;
705 increment_urc(s); /* per utlb access */
709 for (i = 0; i < ITLB_SIZE; i++) {
710 tlb_t * entry = &s->itlb[i];
711 if (entry->vpn == vpn
712 && (!use_asid || entry->asid == asid || entry->sh)) {
715 if (utlb_match_entry)
716 *entry = *utlb_match_entry;
724 tlb_flush_page(s, vpn << 10);
727 int index = (addr & 0x00003f00) >> 8;
728 tlb_t * entry = &s->utlb[index];
730 /* Overwriting valid entry in utlb. */
731 target_ulong address = entry->vpn << 10;
732 tlb_flush_page(s, address);
742 uint32_t cpu_sh4_read_mmaped_utlb_data(CPUSH4State *s,
745 int array = (addr & 0x00800000) >> 23;
746 int index = (addr & 0x00003f00) >> 8;
747 tlb_t * entry = &s->utlb[index];
749 increment_urc(s); /* per utlb access */
752 /* ITLB Data Array 1 */
753 return (entry->ppn << 10) |
756 ((entry->sz & 1) << 6) |
757 ((entry->sz & 2) << 4) |
763 /* ITLB Data Array 2 */
764 return (entry->tc << 1) |
769 void cpu_sh4_write_mmaped_utlb_data(CPUSH4State *s, hwaddr addr,
772 int array = (addr & 0x00800000) >> 23;
773 int index = (addr & 0x00003f00) >> 8;
774 tlb_t * entry = &s->utlb[index];
776 increment_urc(s); /* per utlb access */
779 /* UTLB Data Array 1 */
781 /* Overwriting valid entry in utlb. */
782 target_ulong address = entry->vpn << 10;
783 tlb_flush_page(s, address);
785 entry->ppn = (mem_value & 0x1ffffc00) >> 10;
786 entry->v = (mem_value & 0x00000100) >> 8;
787 entry->sz = (mem_value & 0x00000080) >> 6 |
788 (mem_value & 0x00000010) >> 4;
789 entry->pr = (mem_value & 0x00000060) >> 5;
790 entry->c = (mem_value & 0x00000008) >> 3;
791 entry->d = (mem_value & 0x00000004) >> 2;
792 entry->sh = (mem_value & 0x00000002) >> 1;
793 entry->wt = (mem_value & 0x00000001);
795 /* UTLB Data Array 2 */
796 entry->tc = (mem_value & 0x00000008) >> 3;
797 entry->sa = (mem_value & 0x00000007);
801 int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
804 int use_asid = (env->mmucr & MMUCR_SV) == 0 || (env->sr & SR_MD) == 0;
807 if (env->sr & SR_MD) {
808 /* For previledged mode, P2 and P4 area is not cachable. */
809 if ((0xA0000000 <= addr && addr < 0xC0000000) || 0xE0000000 <= addr)
812 /* For user mode, only U0 area is cachable. */
813 if (0x80000000 <= addr)
818 * TODO : Evaluate CCR and check if the cache is on or off.
819 * Now CCR is not in CPUSH4State, but in SH7750State.
820 * When you move the ccr into CPUSH4State, the code will be
824 /* check if operand cache is enabled or not. */
829 /* if MMU is off, no check for TLB. */
830 if (env->mmucr & MMUCR_AT)
834 n = find_tlb_entry(env, addr, env->itlb, ITLB_SIZE, use_asid);
836 return env->itlb[n].c;
838 n = find_tlb_entry(env, addr, env->utlb, UTLB_SIZE, use_asid);
840 return env->utlb[n].c;