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.1 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/>.
19 #include "qemu/osdep.h"
22 #include "exec/exec-all.h"
24 #include "sysemu/sysemu.h"
26 #if !defined(CONFIG_USER_ONLY)
27 #include "hw/sh4/sh_intc.h"
30 #if defined(CONFIG_USER_ONLY)
32 void superh_cpu_do_interrupt(CPUState *cs)
34 cs->exception_index = -1;
37 int superh_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size, int rw,
40 SuperHCPU *cpu = SUPERH_CPU(cs);
41 CPUSH4State *env = &cpu->env;
44 cs->exception_index = -1;
47 cs->exception_index = 0x0a0;
50 cs->exception_index = 0x0c0;
53 cs->exception_index = 0x0a0;
59 int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
61 /* For user mode, only U0 area is cacheable. */
62 return !(addr & 0x80000000);
65 #else /* !CONFIG_USER_ONLY */
68 #define MMU_ITLB_MISS (-1)
69 #define MMU_ITLB_MULTIPLE (-2)
70 #define MMU_ITLB_VIOLATION (-3)
71 #define MMU_DTLB_MISS_READ (-4)
72 #define MMU_DTLB_MISS_WRITE (-5)
73 #define MMU_DTLB_INITIAL_WRITE (-6)
74 #define MMU_DTLB_VIOLATION_READ (-7)
75 #define MMU_DTLB_VIOLATION_WRITE (-8)
76 #define MMU_DTLB_MULTIPLE (-9)
77 #define MMU_DTLB_MISS (-10)
78 #define MMU_IADDR_ERROR (-11)
79 #define MMU_DADDR_ERROR_READ (-12)
80 #define MMU_DADDR_ERROR_WRITE (-13)
82 void superh_cpu_do_interrupt(CPUState *cs)
84 SuperHCPU *cpu = SUPERH_CPU(cs);
85 CPUSH4State *env = &cpu->env;
86 int do_irq = cs->interrupt_request & CPU_INTERRUPT_HARD;
87 int do_exp, irq_vector = cs->exception_index;
89 /* prioritize exceptions over interrupts */
91 do_exp = cs->exception_index != -1;
92 do_irq = do_irq && (cs->exception_index == -1);
94 if (env->sr & (1u << SR_BL)) {
95 if (do_exp && cs->exception_index != 0x1e0) {
96 /* In theory a masked exception generates a reset exception,
97 which in turn jumps to the reset vector. However this only
98 works when using a bootloader. When using a kernel and an
99 initrd, they need to be reloaded and the program counter
100 should be loaded with the kernel entry point.
101 qemu_system_reset_request takes care of that. */
102 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
105 if (do_irq && !env->in_sleep) {
112 irq_vector = sh_intc_get_pending_vector(env->intc_handle,
113 (env->sr >> 4) & 0xf);
114 if (irq_vector == -1) {
119 if (qemu_loglevel_mask(CPU_LOG_INT)) {
121 switch (cs->exception_index) {
123 expname = "addr_error";
126 expname = "tlb_miss";
129 expname = "tlb_violation";
132 expname = "illegal_instruction";
135 expname = "slot_illegal_instruction";
138 expname = "fpu_disable";
141 expname = "slot_fpu";
144 expname = "data_write";
147 expname = "dtlb_miss_write";
150 expname = "dtlb_violation_write";
153 expname = "fpu_exception";
156 expname = "initial_page_write";
162 expname = do_irq ? "interrupt" : "???";
165 qemu_log("exception 0x%03x [%s] raised\n",
166 irq_vector, expname);
167 log_cpu_state(cs, 0);
170 env->ssr = cpu_read_sr(env);
172 env->sgr = env->gregs[15];
173 env->sr |= (1u << SR_BL) | (1u << SR_MD) | (1u << SR_RB);
176 if (env->flags & DELAY_SLOT_MASK) {
177 /* Branch instruction should be executed again before delay slot. */
179 /* Clear flags for exception/interrupt routine. */
180 env->flags &= ~DELAY_SLOT_MASK;
184 env->expevt = cs->exception_index;
185 switch (cs->exception_index) {
189 env->sr &= ~(1u << SR_FD);
190 env->sr |= 0xf << 4; /* IMASK */
191 env->pc = 0xa0000000;
195 env->pc = env->vbr + 0x400;
198 env->spc += 2; /* special case for TRAPA */
201 env->pc = env->vbr + 0x100;
208 env->intevt = irq_vector;
209 env->pc = env->vbr + 0x600;
214 static void update_itlb_use(CPUSH4State * env, int itlbnb)
216 uint8_t or_mask = 0, and_mask = (uint8_t) - 1;
235 env->mmucr &= (and_mask << 24) | 0x00ffffff;
236 env->mmucr |= (or_mask << 24);
239 static int itlb_replacement(CPUSH4State * env)
241 SuperHCPU *cpu = sh_env_get_cpu(env);
243 if ((env->mmucr & 0xe0000000) == 0xe0000000) {
246 if ((env->mmucr & 0x98000000) == 0x18000000) {
249 if ((env->mmucr & 0x54000000) == 0x04000000) {
252 if ((env->mmucr & 0x2c000000) == 0x00000000) {
255 cpu_abort(CPU(cpu), "Unhandled itlb_replacement");
258 /* Find the corresponding entry in the right TLB
259 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
261 static int find_tlb_entry(CPUSH4State * env, target_ulong address,
262 tlb_t * entries, uint8_t nbtlb, int use_asid)
264 int match = MMU_DTLB_MISS;
269 asid = env->pteh & 0xff;
271 for (i = 0; i < nbtlb; i++) {
273 continue; /* Invalid entry */
274 if (!entries[i].sh && use_asid && entries[i].asid != asid)
275 continue; /* Bad ASID */
276 start = (entries[i].vpn << 10) & ~(entries[i].size - 1);
277 end = start + entries[i].size - 1;
278 if (address >= start && address <= end) { /* Match */
279 if (match != MMU_DTLB_MISS)
280 return MMU_DTLB_MULTIPLE; /* Multiple match */
287 static void increment_urc(CPUSH4State * env)
292 urb = ((env->mmucr) >> 18) & 0x3f;
293 urc = ((env->mmucr) >> 10) & 0x3f;
295 if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1))
297 env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
300 /* Copy and utlb entry into itlb
303 static int copy_utlb_entry_itlb(CPUSH4State *env, int utlb)
308 itlb = itlb_replacement(env);
309 ientry = &env->itlb[itlb];
311 tlb_flush_page(CPU(sh_env_get_cpu(env)), ientry->vpn << 10);
313 *ientry = env->utlb[utlb];
314 update_itlb_use(env, itlb);
319 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
321 static int find_itlb_entry(CPUSH4State * env, target_ulong address,
326 e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid);
327 if (e == MMU_DTLB_MULTIPLE) {
328 e = MMU_ITLB_MULTIPLE;
329 } else if (e == MMU_DTLB_MISS) {
332 update_itlb_use(env, e);
338 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
339 static int find_utlb_entry(CPUSH4State * env, target_ulong address, int use_asid)
341 /* per utlb access */
345 return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
348 /* Match address against MMU
349 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
350 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
351 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
352 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
353 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
355 static int get_mmu_address(CPUSH4State * env, target_ulong * physical,
356 int *prot, target_ulong address,
357 int rw, int access_type)
360 tlb_t *matching = NULL;
362 use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD));
365 n = find_itlb_entry(env, address, use_asid);
367 matching = &env->itlb[n];
368 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
369 n = MMU_ITLB_VIOLATION;
374 n = find_utlb_entry(env, address, use_asid);
376 n = copy_utlb_entry_itlb(env, n);
377 matching = &env->itlb[n];
378 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
379 n = MMU_ITLB_VIOLATION;
381 *prot = PAGE_READ | PAGE_EXEC;
382 if ((matching->pr & 1) && matching->d) {
386 } else if (n == MMU_DTLB_MULTIPLE) {
387 n = MMU_ITLB_MULTIPLE;
388 } else if (n == MMU_DTLB_MISS) {
393 n = find_utlb_entry(env, address, use_asid);
395 matching = &env->utlb[n];
396 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
397 n = (rw == 1) ? MMU_DTLB_VIOLATION_WRITE :
398 MMU_DTLB_VIOLATION_READ;
399 } else if ((rw == 1) && !(matching->pr & 1)) {
400 n = MMU_DTLB_VIOLATION_WRITE;
401 } else if ((rw == 1) && !matching->d) {
402 n = MMU_DTLB_INITIAL_WRITE;
405 if ((matching->pr & 1) && matching->d) {
409 } else if (n == MMU_DTLB_MISS) {
410 n = (rw == 1) ? MMU_DTLB_MISS_WRITE :
416 *physical = ((matching->ppn << 10) & ~(matching->size - 1)) |
417 (address & (matching->size - 1));
422 static int get_physical_address(CPUSH4State * env, target_ulong * physical,
423 int *prot, target_ulong address,
424 int rw, int access_type)
426 /* P1, P2 and P4 areas do not use translation */
427 if ((address >= 0x80000000 && address < 0xc0000000) ||
428 address >= 0xe0000000) {
429 if (!(env->sr & (1u << SR_MD))
430 && (address < 0xe0000000 || address >= 0xe4000000)) {
431 /* Unauthorized access in user mode (only store queues are available) */
432 qemu_log_mask(LOG_GUEST_ERROR, "Unauthorized access\n");
434 return MMU_DADDR_ERROR_READ;
436 return MMU_DADDR_ERROR_WRITE;
438 return MMU_IADDR_ERROR;
440 if (address >= 0x80000000 && address < 0xc0000000) {
441 /* Mask upper 3 bits for P1 and P2 areas */
442 *physical = address & 0x1fffffff;
446 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
450 /* If MMU is disabled, return the corresponding physical page */
451 if (!(env->mmucr & MMUCR_AT)) {
452 *physical = address & 0x1FFFFFFF;
453 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
457 /* We need to resort to the MMU */
458 return get_mmu_address(env, physical, prot, address, rw, access_type);
461 int superh_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size, int rw,
464 SuperHCPU *cpu = SUPERH_CPU(cs);
465 CPUSH4State *env = &cpu->env;
466 target_ulong physical;
467 int prot, ret, access_type;
469 access_type = ACCESS_INT;
471 get_physical_address(env, &physical, &prot, address, rw,
476 if (ret != MMU_DTLB_MULTIPLE && ret != MMU_ITLB_MULTIPLE) {
477 env->pteh = (env->pteh & PTEH_ASID_MASK) |
478 (address & PTEH_VPN_MASK);
482 case MMU_DTLB_MISS_READ:
483 cs->exception_index = 0x040;
485 case MMU_DTLB_MULTIPLE:
486 case MMU_ITLB_MULTIPLE:
487 cs->exception_index = 0x140;
489 case MMU_ITLB_VIOLATION:
490 cs->exception_index = 0x0a0;
492 case MMU_DTLB_MISS_WRITE:
493 cs->exception_index = 0x060;
495 case MMU_DTLB_INITIAL_WRITE:
496 cs->exception_index = 0x080;
498 case MMU_DTLB_VIOLATION_READ:
499 cs->exception_index = 0x0a0;
501 case MMU_DTLB_VIOLATION_WRITE:
502 cs->exception_index = 0x0c0;
504 case MMU_IADDR_ERROR:
505 case MMU_DADDR_ERROR_READ:
506 cs->exception_index = 0x0e0;
508 case MMU_DADDR_ERROR_WRITE:
509 cs->exception_index = 0x100;
512 cpu_abort(cs, "Unhandled MMU fault");
517 address &= TARGET_PAGE_MASK;
518 physical &= TARGET_PAGE_MASK;
520 tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE);
524 hwaddr superh_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
526 SuperHCPU *cpu = SUPERH_CPU(cs);
527 target_ulong physical;
530 get_physical_address(&cpu->env, &physical, &prot, addr, 0, 0);
534 void cpu_load_tlb(CPUSH4State * env)
536 SuperHCPU *cpu = sh_env_get_cpu(env);
537 int n = cpu_mmucr_urc(env->mmucr);
538 tlb_t * entry = &env->utlb[n];
541 /* Overwriting valid entry in utlb. */
542 target_ulong address = entry->vpn << 10;
543 tlb_flush_page(CPU(cpu), address);
546 /* Take values into cpu status from registers. */
547 entry->asid = (uint8_t)cpu_pteh_asid(env->pteh);
548 entry->vpn = cpu_pteh_vpn(env->pteh);
549 entry->v = (uint8_t)cpu_ptel_v(env->ptel);
550 entry->ppn = cpu_ptel_ppn(env->ptel);
551 entry->sz = (uint8_t)cpu_ptel_sz(env->ptel);
554 entry->size = 1024; /* 1K */
557 entry->size = 1024 * 4; /* 4K */
560 entry->size = 1024 * 64; /* 64K */
563 entry->size = 1024 * 1024; /* 1M */
566 cpu_abort(CPU(cpu), "Unhandled load_tlb");
569 entry->sh = (uint8_t)cpu_ptel_sh(env->ptel);
570 entry->c = (uint8_t)cpu_ptel_c(env->ptel);
571 entry->pr = (uint8_t)cpu_ptel_pr(env->ptel);
572 entry->d = (uint8_t)cpu_ptel_d(env->ptel);
573 entry->wt = (uint8_t)cpu_ptel_wt(env->ptel);
574 entry->sa = (uint8_t)cpu_ptea_sa(env->ptea);
575 entry->tc = (uint8_t)cpu_ptea_tc(env->ptea);
578 void cpu_sh4_invalidate_tlb(CPUSH4State *s)
583 for (i = 0; i < UTLB_SIZE; i++) {
584 tlb_t * entry = &s->utlb[i];
588 for (i = 0; i < ITLB_SIZE; i++) {
589 tlb_t * entry = &s->itlb[i];
593 tlb_flush(CPU(sh_env_get_cpu(s)));
596 uint32_t cpu_sh4_read_mmaped_itlb_addr(CPUSH4State *s,
599 int index = (addr & 0x00000300) >> 8;
600 tlb_t * entry = &s->itlb[index];
602 return (entry->vpn << 10) |
607 void cpu_sh4_write_mmaped_itlb_addr(CPUSH4State *s, hwaddr addr,
610 uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
611 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
612 uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
614 int index = (addr & 0x00000300) >> 8;
615 tlb_t * entry = &s->itlb[index];
617 /* Overwriting valid entry in itlb. */
618 target_ulong address = entry->vpn << 10;
619 tlb_flush_page(CPU(sh_env_get_cpu(s)), address);
626 uint32_t cpu_sh4_read_mmaped_itlb_data(CPUSH4State *s,
629 int array = (addr & 0x00800000) >> 23;
630 int index = (addr & 0x00000300) >> 8;
631 tlb_t * entry = &s->itlb[index];
634 /* ITLB Data Array 1 */
635 return (entry->ppn << 10) |
638 ((entry->sz & 1) << 6) |
639 ((entry->sz & 2) << 4) |
643 /* ITLB Data Array 2 */
644 return (entry->tc << 1) |
649 void cpu_sh4_write_mmaped_itlb_data(CPUSH4State *s, hwaddr addr,
652 int array = (addr & 0x00800000) >> 23;
653 int index = (addr & 0x00000300) >> 8;
654 tlb_t * entry = &s->itlb[index];
657 /* ITLB Data Array 1 */
659 /* Overwriting valid entry in utlb. */
660 target_ulong address = entry->vpn << 10;
661 tlb_flush_page(CPU(sh_env_get_cpu(s)), address);
663 entry->ppn = (mem_value & 0x1ffffc00) >> 10;
664 entry->v = (mem_value & 0x00000100) >> 8;
665 entry->sz = (mem_value & 0x00000080) >> 6 |
666 (mem_value & 0x00000010) >> 4;
667 entry->pr = (mem_value & 0x00000040) >> 5;
668 entry->c = (mem_value & 0x00000008) >> 3;
669 entry->sh = (mem_value & 0x00000002) >> 1;
671 /* ITLB Data Array 2 */
672 entry->tc = (mem_value & 0x00000008) >> 3;
673 entry->sa = (mem_value & 0x00000007);
677 uint32_t cpu_sh4_read_mmaped_utlb_addr(CPUSH4State *s,
680 int index = (addr & 0x00003f00) >> 8;
681 tlb_t * entry = &s->utlb[index];
683 increment_urc(s); /* per utlb access */
685 return (entry->vpn << 10) |
690 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State *s, hwaddr addr,
693 int associate = addr & 0x0000080;
694 uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
695 uint8_t d = (uint8_t)((mem_value & 0x00000200) >> 9);
696 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
697 uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
698 int use_asid = !(s->mmucr & MMUCR_SV) || !(s->sr & (1u << SR_MD));
702 tlb_t * utlb_match_entry = NULL;
703 int needs_tlb_flush = 0;
706 for (i = 0; i < UTLB_SIZE; i++) {
707 tlb_t * entry = &s->utlb[i];
711 if (entry->vpn == vpn
712 && (!use_asid || entry->asid == asid || entry->sh)) {
713 if (utlb_match_entry) {
714 CPUState *cs = CPU(sh_env_get_cpu(s));
716 /* Multiple TLB Exception */
717 cs->exception_index = 0x140;
725 utlb_match_entry = entry;
727 increment_urc(s); /* per utlb access */
731 for (i = 0; i < ITLB_SIZE; i++) {
732 tlb_t * entry = &s->itlb[i];
733 if (entry->vpn == vpn
734 && (!use_asid || entry->asid == asid || entry->sh)) {
737 if (utlb_match_entry)
738 *entry = *utlb_match_entry;
745 if (needs_tlb_flush) {
746 tlb_flush_page(CPU(sh_env_get_cpu(s)), vpn << 10);
750 int index = (addr & 0x00003f00) >> 8;
751 tlb_t * entry = &s->utlb[index];
753 CPUState *cs = CPU(sh_env_get_cpu(s));
755 /* Overwriting valid entry in utlb. */
756 target_ulong address = entry->vpn << 10;
757 tlb_flush_page(cs, address);
767 uint32_t cpu_sh4_read_mmaped_utlb_data(CPUSH4State *s,
770 int array = (addr & 0x00800000) >> 23;
771 int index = (addr & 0x00003f00) >> 8;
772 tlb_t * entry = &s->utlb[index];
774 increment_urc(s); /* per utlb access */
777 /* ITLB Data Array 1 */
778 return (entry->ppn << 10) |
781 ((entry->sz & 1) << 6) |
782 ((entry->sz & 2) << 4) |
788 /* ITLB Data Array 2 */
789 return (entry->tc << 1) |
794 void cpu_sh4_write_mmaped_utlb_data(CPUSH4State *s, hwaddr addr,
797 int array = (addr & 0x00800000) >> 23;
798 int index = (addr & 0x00003f00) >> 8;
799 tlb_t * entry = &s->utlb[index];
801 increment_urc(s); /* per utlb access */
804 /* UTLB Data Array 1 */
806 /* Overwriting valid entry in utlb. */
807 target_ulong address = entry->vpn << 10;
808 tlb_flush_page(CPU(sh_env_get_cpu(s)), address);
810 entry->ppn = (mem_value & 0x1ffffc00) >> 10;
811 entry->v = (mem_value & 0x00000100) >> 8;
812 entry->sz = (mem_value & 0x00000080) >> 6 |
813 (mem_value & 0x00000010) >> 4;
814 entry->pr = (mem_value & 0x00000060) >> 5;
815 entry->c = (mem_value & 0x00000008) >> 3;
816 entry->d = (mem_value & 0x00000004) >> 2;
817 entry->sh = (mem_value & 0x00000002) >> 1;
818 entry->wt = (mem_value & 0x00000001);
820 /* UTLB Data Array 2 */
821 entry->tc = (mem_value & 0x00000008) >> 3;
822 entry->sa = (mem_value & 0x00000007);
826 int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
829 int use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD));
832 if (env->sr & (1u << SR_MD)) {
833 /* For privileged mode, P2 and P4 area is not cacheable. */
834 if ((0xA0000000 <= addr && addr < 0xC0000000) || 0xE0000000 <= addr)
837 /* For user mode, only U0 area is cacheable. */
838 if (0x80000000 <= addr)
843 * TODO : Evaluate CCR and check if the cache is on or off.
844 * Now CCR is not in CPUSH4State, but in SH7750State.
845 * When you move the ccr into CPUSH4State, the code will be
849 /* check if operand cache is enabled or not. */
854 /* if MMU is off, no check for TLB. */
855 if (env->mmucr & MMUCR_AT)
859 n = find_tlb_entry(env, addr, env->itlb, ITLB_SIZE, use_asid);
861 return env->itlb[n].c;
863 n = find_tlb_entry(env, addr, env->utlb, UTLB_SIZE, use_asid);
865 return env->utlb[n].c;
872 bool superh_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
874 if (interrupt_request & CPU_INTERRUPT_HARD) {
875 SuperHCPU *cpu = SUPERH_CPU(cs);
876 CPUSH4State *env = &cpu->env;
878 /* Delay slots are indivisible, ignore interrupts */
879 if (env->flags & DELAY_SLOT_MASK) {
882 superh_cpu_do_interrupt(cs);