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/>.
19 #include "qemu/osdep.h"
22 #include "exec/exec-all.h"
25 #if !defined(CONFIG_USER_ONLY)
26 #include "hw/sh4/sh_intc.h"
29 #if defined(CONFIG_USER_ONLY)
31 void superh_cpu_do_interrupt(CPUState *cs)
33 cs->exception_index = -1;
36 int superh_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
39 SuperHCPU *cpu = SUPERH_CPU(cs);
40 CPUSH4State *env = &cpu->env;
43 cs->exception_index = -1;
46 cs->exception_index = 0x0a0;
49 cs->exception_index = 0x0c0;
52 cs->exception_index = 0x0a0;
58 int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
60 /* For user mode, only U0 area is cacheable. */
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 superh_cpu_do_interrupt(CPUState *cs)
83 SuperHCPU *cpu = SUPERH_CPU(cs);
84 CPUSH4State *env = &cpu->env;
85 int do_irq = cs->interrupt_request & CPU_INTERRUPT_HARD;
86 int do_exp, irq_vector = cs->exception_index;
88 /* prioritize exceptions over interrupts */
90 do_exp = cs->exception_index != -1;
91 do_irq = do_irq && (cs->exception_index == -1);
93 if (env->sr & (1u << SR_BL)) {
94 if (do_exp && cs->exception_index != 0x1e0) {
95 cs->exception_index = 0x000; /* masked exception -> reset */
97 if (do_irq && !env->in_sleep) {
104 irq_vector = sh_intc_get_pending_vector(env->intc_handle,
105 (env->sr >> 4) & 0xf);
106 if (irq_vector == -1) {
111 if (qemu_loglevel_mask(CPU_LOG_INT)) {
113 switch (cs->exception_index) {
115 expname = "addr_error";
118 expname = "tlb_miss";
121 expname = "tlb_violation";
124 expname = "illegal_instruction";
127 expname = "slot_illegal_instruction";
130 expname = "fpu_disable";
133 expname = "slot_fpu";
136 expname = "data_write";
139 expname = "dtlb_miss_write";
142 expname = "dtlb_violation_write";
145 expname = "fpu_exception";
148 expname = "initial_page_write";
154 expname = do_irq ? "interrupt" : "???";
157 qemu_log("exception 0x%03x [%s] raised\n",
158 irq_vector, expname);
159 log_cpu_state(cs, 0);
162 env->ssr = cpu_read_sr(env);
164 env->sgr = env->gregs[15];
165 env->sr |= (1u << SR_BL) | (1u << SR_MD) | (1u << SR_RB);
167 if (env->flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL)) {
168 /* Branch instruction should be executed again before delay slot. */
170 /* Clear flags for exception/interrupt routine. */
171 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
175 env->expevt = cs->exception_index;
176 switch (cs->exception_index) {
180 env->sr &= ~(1u << SR_FD);
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 SuperHCPU *cpu = sh_env_get_cpu(env);
234 if ((env->mmucr & 0xe0000000) == 0xe0000000) {
237 if ((env->mmucr & 0x98000000) == 0x18000000) {
240 if ((env->mmucr & 0x54000000) == 0x04000000) {
243 if ((env->mmucr & 0x2c000000) == 0x00000000) {
246 cpu_abort(CPU(cpu), "Unhandled itlb_replacement");
249 /* Find the corresponding entry in the right TLB
250 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
252 static int find_tlb_entry(CPUSH4State * env, target_ulong address,
253 tlb_t * entries, uint8_t nbtlb, int use_asid)
255 int match = MMU_DTLB_MISS;
260 asid = env->pteh & 0xff;
262 for (i = 0; i < nbtlb; i++) {
264 continue; /* Invalid entry */
265 if (!entries[i].sh && use_asid && entries[i].asid != asid)
266 continue; /* Bad ASID */
267 start = (entries[i].vpn << 10) & ~(entries[i].size - 1);
268 end = start + entries[i].size - 1;
269 if (address >= start && address <= end) { /* Match */
270 if (match != MMU_DTLB_MISS)
271 return MMU_DTLB_MULTIPLE; /* Multiple match */
278 static void increment_urc(CPUSH4State * env)
283 urb = ((env->mmucr) >> 18) & 0x3f;
284 urc = ((env->mmucr) >> 10) & 0x3f;
286 if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1))
288 env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
291 /* Copy and utlb entry into itlb
294 static int copy_utlb_entry_itlb(CPUSH4State *env, int utlb)
299 itlb = itlb_replacement(env);
300 ientry = &env->itlb[itlb];
302 tlb_flush_page(CPU(sh_env_get_cpu(env)), ientry->vpn << 10);
304 *ientry = env->utlb[utlb];
305 update_itlb_use(env, itlb);
310 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
312 static int find_itlb_entry(CPUSH4State * env, target_ulong address,
317 e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid);
318 if (e == MMU_DTLB_MULTIPLE) {
319 e = MMU_ITLB_MULTIPLE;
320 } else if (e == MMU_DTLB_MISS) {
323 update_itlb_use(env, e);
329 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
330 static int find_utlb_entry(CPUSH4State * env, target_ulong address, int use_asid)
332 /* per utlb access */
336 return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
339 /* Match address against MMU
340 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
341 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
342 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
343 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
344 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
346 static int get_mmu_address(CPUSH4State * env, target_ulong * physical,
347 int *prot, target_ulong address,
348 int rw, int access_type)
351 tlb_t *matching = NULL;
353 use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD));
356 n = find_itlb_entry(env, address, use_asid);
358 matching = &env->itlb[n];
359 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
360 n = MMU_ITLB_VIOLATION;
365 n = find_utlb_entry(env, address, use_asid);
367 n = copy_utlb_entry_itlb(env, n);
368 matching = &env->itlb[n];
369 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
370 n = MMU_ITLB_VIOLATION;
372 *prot = PAGE_READ | PAGE_EXEC;
373 if ((matching->pr & 1) && matching->d) {
377 } else if (n == MMU_DTLB_MULTIPLE) {
378 n = MMU_ITLB_MULTIPLE;
379 } else if (n == MMU_DTLB_MISS) {
384 n = find_utlb_entry(env, address, use_asid);
386 matching = &env->utlb[n];
387 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
388 n = (rw == 1) ? MMU_DTLB_VIOLATION_WRITE :
389 MMU_DTLB_VIOLATION_READ;
390 } else if ((rw == 1) && !(matching->pr & 1)) {
391 n = MMU_DTLB_VIOLATION_WRITE;
392 } else if ((rw == 1) && !matching->d) {
393 n = MMU_DTLB_INITIAL_WRITE;
396 if ((matching->pr & 1) && matching->d) {
400 } else if (n == MMU_DTLB_MISS) {
401 n = (rw == 1) ? MMU_DTLB_MISS_WRITE :
407 *physical = ((matching->ppn << 10) & ~(matching->size - 1)) |
408 (address & (matching->size - 1));
413 static int get_physical_address(CPUSH4State * env, target_ulong * physical,
414 int *prot, target_ulong address,
415 int rw, int access_type)
417 /* P1, P2 and P4 areas do not use translation */
418 if ((address >= 0x80000000 && address < 0xc0000000) ||
419 address >= 0xe0000000) {
420 if (!(env->sr & (1u << SR_MD))
421 && (address < 0xe0000000 || address >= 0xe4000000)) {
422 /* Unauthorized access in user mode (only store queues are available) */
423 fprintf(stderr, "Unauthorized access\n");
425 return MMU_DADDR_ERROR_READ;
427 return MMU_DADDR_ERROR_WRITE;
429 return MMU_IADDR_ERROR;
431 if (address >= 0x80000000 && address < 0xc0000000) {
432 /* Mask upper 3 bits for P1 and P2 areas */
433 *physical = address & 0x1fffffff;
437 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
441 /* If MMU is disabled, return the corresponding physical page */
442 if (!(env->mmucr & MMUCR_AT)) {
443 *physical = address & 0x1FFFFFFF;
444 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
448 /* We need to resort to the MMU */
449 return get_mmu_address(env, physical, prot, address, rw, access_type);
452 int superh_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
455 SuperHCPU *cpu = SUPERH_CPU(cs);
456 CPUSH4State *env = &cpu->env;
457 target_ulong physical;
458 int prot, ret, access_type;
460 access_type = ACCESS_INT;
462 get_physical_address(env, &physical, &prot, address, rw,
467 if (ret != MMU_DTLB_MULTIPLE && ret != MMU_ITLB_MULTIPLE) {
468 env->pteh = (env->pteh & PTEH_ASID_MASK) |
469 (address & PTEH_VPN_MASK);
473 case MMU_DTLB_MISS_READ:
474 cs->exception_index = 0x040;
476 case MMU_DTLB_MULTIPLE:
477 case MMU_ITLB_MULTIPLE:
478 cs->exception_index = 0x140;
480 case MMU_ITLB_VIOLATION:
481 cs->exception_index = 0x0a0;
483 case MMU_DTLB_MISS_WRITE:
484 cs->exception_index = 0x060;
486 case MMU_DTLB_INITIAL_WRITE:
487 cs->exception_index = 0x080;
489 case MMU_DTLB_VIOLATION_READ:
490 cs->exception_index = 0x0a0;
492 case MMU_DTLB_VIOLATION_WRITE:
493 cs->exception_index = 0x0c0;
495 case MMU_IADDR_ERROR:
496 case MMU_DADDR_ERROR_READ:
497 cs->exception_index = 0x0e0;
499 case MMU_DADDR_ERROR_WRITE:
500 cs->exception_index = 0x100;
503 cpu_abort(cs, "Unhandled MMU fault");
508 address &= TARGET_PAGE_MASK;
509 physical &= TARGET_PAGE_MASK;
511 tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE);
515 hwaddr superh_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
517 SuperHCPU *cpu = SUPERH_CPU(cs);
518 target_ulong physical;
521 get_physical_address(&cpu->env, &physical, &prot, addr, 0, 0);
525 void cpu_load_tlb(CPUSH4State * env)
527 SuperHCPU *cpu = sh_env_get_cpu(env);
528 int n = cpu_mmucr_urc(env->mmucr);
529 tlb_t * entry = &env->utlb[n];
532 /* Overwriting valid entry in utlb. */
533 target_ulong address = entry->vpn << 10;
534 tlb_flush_page(CPU(cpu), address);
537 /* Take values into cpu status from registers. */
538 entry->asid = (uint8_t)cpu_pteh_asid(env->pteh);
539 entry->vpn = cpu_pteh_vpn(env->pteh);
540 entry->v = (uint8_t)cpu_ptel_v(env->ptel);
541 entry->ppn = cpu_ptel_ppn(env->ptel);
542 entry->sz = (uint8_t)cpu_ptel_sz(env->ptel);
545 entry->size = 1024; /* 1K */
548 entry->size = 1024 * 4; /* 4K */
551 entry->size = 1024 * 64; /* 64K */
554 entry->size = 1024 * 1024; /* 1M */
557 cpu_abort(CPU(cpu), "Unhandled load_tlb");
560 entry->sh = (uint8_t)cpu_ptel_sh(env->ptel);
561 entry->c = (uint8_t)cpu_ptel_c(env->ptel);
562 entry->pr = (uint8_t)cpu_ptel_pr(env->ptel);
563 entry->d = (uint8_t)cpu_ptel_d(env->ptel);
564 entry->wt = (uint8_t)cpu_ptel_wt(env->ptel);
565 entry->sa = (uint8_t)cpu_ptea_sa(env->ptea);
566 entry->tc = (uint8_t)cpu_ptea_tc(env->ptea);
569 void cpu_sh4_invalidate_tlb(CPUSH4State *s)
574 for (i = 0; i < UTLB_SIZE; i++) {
575 tlb_t * entry = &s->utlb[i];
579 for (i = 0; i < ITLB_SIZE; i++) {
580 tlb_t * entry = &s->itlb[i];
584 tlb_flush(CPU(sh_env_get_cpu(s)));
587 uint32_t cpu_sh4_read_mmaped_itlb_addr(CPUSH4State *s,
590 int index = (addr & 0x00000300) >> 8;
591 tlb_t * entry = &s->itlb[index];
593 return (entry->vpn << 10) |
598 void cpu_sh4_write_mmaped_itlb_addr(CPUSH4State *s, hwaddr addr,
601 uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
602 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
603 uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
605 int index = (addr & 0x00000300) >> 8;
606 tlb_t * entry = &s->itlb[index];
608 /* Overwriting valid entry in itlb. */
609 target_ulong address = entry->vpn << 10;
610 tlb_flush_page(CPU(sh_env_get_cpu(s)), address);
617 uint32_t cpu_sh4_read_mmaped_itlb_data(CPUSH4State *s,
620 int array = (addr & 0x00800000) >> 23;
621 int index = (addr & 0x00000300) >> 8;
622 tlb_t * entry = &s->itlb[index];
625 /* ITLB Data Array 1 */
626 return (entry->ppn << 10) |
629 ((entry->sz & 1) << 6) |
630 ((entry->sz & 2) << 4) |
634 /* ITLB Data Array 2 */
635 return (entry->tc << 1) |
640 void cpu_sh4_write_mmaped_itlb_data(CPUSH4State *s, hwaddr addr,
643 int array = (addr & 0x00800000) >> 23;
644 int index = (addr & 0x00000300) >> 8;
645 tlb_t * entry = &s->itlb[index];
648 /* ITLB Data Array 1 */
650 /* Overwriting valid entry in utlb. */
651 target_ulong address = entry->vpn << 10;
652 tlb_flush_page(CPU(sh_env_get_cpu(s)), address);
654 entry->ppn = (mem_value & 0x1ffffc00) >> 10;
655 entry->v = (mem_value & 0x00000100) >> 8;
656 entry->sz = (mem_value & 0x00000080) >> 6 |
657 (mem_value & 0x00000010) >> 4;
658 entry->pr = (mem_value & 0x00000040) >> 5;
659 entry->c = (mem_value & 0x00000008) >> 3;
660 entry->sh = (mem_value & 0x00000002) >> 1;
662 /* ITLB Data Array 2 */
663 entry->tc = (mem_value & 0x00000008) >> 3;
664 entry->sa = (mem_value & 0x00000007);
668 uint32_t cpu_sh4_read_mmaped_utlb_addr(CPUSH4State *s,
671 int index = (addr & 0x00003f00) >> 8;
672 tlb_t * entry = &s->utlb[index];
674 increment_urc(s); /* per utlb access */
676 return (entry->vpn << 10) |
681 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State *s, hwaddr addr,
684 int associate = addr & 0x0000080;
685 uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
686 uint8_t d = (uint8_t)((mem_value & 0x00000200) >> 9);
687 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
688 uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
689 int use_asid = !(s->mmucr & MMUCR_SV) || !(s->sr & (1u << SR_MD));
693 tlb_t * utlb_match_entry = NULL;
694 int needs_tlb_flush = 0;
697 for (i = 0; i < UTLB_SIZE; i++) {
698 tlb_t * entry = &s->utlb[i];
702 if (entry->vpn == vpn
703 && (!use_asid || entry->asid == asid || entry->sh)) {
704 if (utlb_match_entry) {
705 CPUState *cs = CPU(sh_env_get_cpu(s));
707 /* Multiple TLB Exception */
708 cs->exception_index = 0x140;
716 utlb_match_entry = entry;
718 increment_urc(s); /* per utlb access */
722 for (i = 0; i < ITLB_SIZE; i++) {
723 tlb_t * entry = &s->itlb[i];
724 if (entry->vpn == vpn
725 && (!use_asid || entry->asid == asid || entry->sh)) {
728 if (utlb_match_entry)
729 *entry = *utlb_match_entry;
736 if (needs_tlb_flush) {
737 tlb_flush_page(CPU(sh_env_get_cpu(s)), vpn << 10);
741 int index = (addr & 0x00003f00) >> 8;
742 tlb_t * entry = &s->utlb[index];
744 CPUState *cs = CPU(sh_env_get_cpu(s));
746 /* Overwriting valid entry in utlb. */
747 target_ulong address = entry->vpn << 10;
748 tlb_flush_page(cs, address);
758 uint32_t cpu_sh4_read_mmaped_utlb_data(CPUSH4State *s,
761 int array = (addr & 0x00800000) >> 23;
762 int index = (addr & 0x00003f00) >> 8;
763 tlb_t * entry = &s->utlb[index];
765 increment_urc(s); /* per utlb access */
768 /* ITLB Data Array 1 */
769 return (entry->ppn << 10) |
772 ((entry->sz & 1) << 6) |
773 ((entry->sz & 2) << 4) |
779 /* ITLB Data Array 2 */
780 return (entry->tc << 1) |
785 void cpu_sh4_write_mmaped_utlb_data(CPUSH4State *s, hwaddr addr,
788 int array = (addr & 0x00800000) >> 23;
789 int index = (addr & 0x00003f00) >> 8;
790 tlb_t * entry = &s->utlb[index];
792 increment_urc(s); /* per utlb access */
795 /* UTLB Data Array 1 */
797 /* Overwriting valid entry in utlb. */
798 target_ulong address = entry->vpn << 10;
799 tlb_flush_page(CPU(sh_env_get_cpu(s)), address);
801 entry->ppn = (mem_value & 0x1ffffc00) >> 10;
802 entry->v = (mem_value & 0x00000100) >> 8;
803 entry->sz = (mem_value & 0x00000080) >> 6 |
804 (mem_value & 0x00000010) >> 4;
805 entry->pr = (mem_value & 0x00000060) >> 5;
806 entry->c = (mem_value & 0x00000008) >> 3;
807 entry->d = (mem_value & 0x00000004) >> 2;
808 entry->sh = (mem_value & 0x00000002) >> 1;
809 entry->wt = (mem_value & 0x00000001);
811 /* UTLB Data Array 2 */
812 entry->tc = (mem_value & 0x00000008) >> 3;
813 entry->sa = (mem_value & 0x00000007);
817 int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
820 int use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD));
823 if (env->sr & (1u << SR_MD)) {
824 /* For privileged mode, P2 and P4 area is not cacheable. */
825 if ((0xA0000000 <= addr && addr < 0xC0000000) || 0xE0000000 <= addr)
828 /* For user mode, only U0 area is cacheable. */
829 if (0x80000000 <= addr)
834 * TODO : Evaluate CCR and check if the cache is on or off.
835 * Now CCR is not in CPUSH4State, but in SH7750State.
836 * When you move the ccr into CPUSH4State, the code will be
840 /* check if operand cache is enabled or not. */
845 /* if MMU is off, no check for TLB. */
846 if (env->mmucr & MMUCR_AT)
850 n = find_tlb_entry(env, addr, env->itlb, ITLB_SIZE, use_asid);
852 return env->itlb[n].c;
854 n = find_tlb_entry(env, addr, env->utlb, UTLB_SIZE, use_asid);
856 return env->utlb[n].c;
863 bool superh_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
865 if (interrupt_request & CPU_INTERRUPT_HARD) {
866 superh_cpu_do_interrupt(cs);