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/sh4/sh_intc.h"
32 #if defined(CONFIG_USER_ONLY)
34 void superh_cpu_do_interrupt(CPUState
*cs
)
36 SuperHCPU
*cpu
= SUPERH_CPU(cs
);
37 CPUSH4State
*env
= &cpu
->env
;
39 env
->exception_index
= -1;
42 int cpu_sh4_handle_mmu_fault(CPUSH4State
* env
, target_ulong address
, int rw
,
46 env
->exception_index
= -1;
49 env
->exception_index
= 0x0a0;
52 env
->exception_index
= 0x0c0;
55 env
->exception_index
= 0x0a0;
61 int cpu_sh4_is_cached(CPUSH4State
* env
, target_ulong addr
)
63 /* For user mode, only U0 area is cachable. */
64 return !(addr
& 0x80000000);
67 #else /* !CONFIG_USER_ONLY */
70 #define MMU_ITLB_MISS (-1)
71 #define MMU_ITLB_MULTIPLE (-2)
72 #define MMU_ITLB_VIOLATION (-3)
73 #define MMU_DTLB_MISS_READ (-4)
74 #define MMU_DTLB_MISS_WRITE (-5)
75 #define MMU_DTLB_INITIAL_WRITE (-6)
76 #define MMU_DTLB_VIOLATION_READ (-7)
77 #define MMU_DTLB_VIOLATION_WRITE (-8)
78 #define MMU_DTLB_MULTIPLE (-9)
79 #define MMU_DTLB_MISS (-10)
80 #define MMU_IADDR_ERROR (-11)
81 #define MMU_DADDR_ERROR_READ (-12)
82 #define MMU_DADDR_ERROR_WRITE (-13)
84 void superh_cpu_do_interrupt(CPUState
*cs
)
86 SuperHCPU
*cpu
= SUPERH_CPU(cs
);
87 CPUSH4State
*env
= &cpu
->env
;
88 int do_irq
= cs
->interrupt_request
& CPU_INTERRUPT_HARD
;
89 int do_exp
, irq_vector
= env
->exception_index
;
91 /* prioritize exceptions over interrupts */
93 do_exp
= env
->exception_index
!= -1;
94 do_irq
= do_irq
&& (env
->exception_index
== -1);
96 if (env
->sr
& SR_BL
) {
97 if (do_exp
&& env
->exception_index
!= 0x1e0) {
98 env
->exception_index
= 0x000; /* masked exception -> reset */
100 if (do_irq
&& !env
->in_sleep
) {
107 irq_vector
= sh_intc_get_pending_vector(env
->intc_handle
,
108 (env
->sr
>> 4) & 0xf);
109 if (irq_vector
== -1) {
114 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
116 switch (env
->exception_index
) {
118 expname
= "addr_error";
121 expname
= "tlb_miss";
124 expname
= "tlb_violation";
127 expname
= "illegal_instruction";
130 expname
= "slot_illegal_instruction";
133 expname
= "fpu_disable";
136 expname
= "slot_fpu";
139 expname
= "data_write";
142 expname
= "dtlb_miss_write";
145 expname
= "dtlb_violation_write";
148 expname
= "fpu_exception";
151 expname
= "initial_page_write";
157 expname
= do_irq
? "interrupt" : "???";
160 qemu_log("exception 0x%03x [%s] raised\n",
161 irq_vector
, expname
);
162 log_cpu_state(env
, 0);
167 env
->sgr
= env
->gregs
[15];
168 env
->sr
|= SR_BL
| SR_MD
| SR_RB
;
170 if (env
->flags
& (DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
)) {
171 /* Branch instruction should be executed again before delay slot. */
173 /* Clear flags for exception/interrupt routine. */
174 env
->flags
&= ~(DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
| DELAY_SLOT_TRUE
);
176 if (env
->flags
& DELAY_SLOT_CLEARME
)
180 env
->expevt
= env
->exception_index
;
181 switch (env
->exception_index
) {
186 env
->sr
|= 0xf << 4; /* IMASK */
187 env
->pc
= 0xa0000000;
191 env
->pc
= env
->vbr
+ 0x400;
194 env
->spc
+= 2; /* special case for TRAPA */
197 env
->pc
= env
->vbr
+ 0x100;
204 env
->intevt
= irq_vector
;
205 env
->pc
= env
->vbr
+ 0x600;
210 static void update_itlb_use(CPUSH4State
* env
, int itlbnb
)
212 uint8_t or_mask
= 0, and_mask
= (uint8_t) - 1;
231 env
->mmucr
&= (and_mask
<< 24) | 0x00ffffff;
232 env
->mmucr
|= (or_mask
<< 24);
235 static int itlb_replacement(CPUSH4State
* env
)
237 if ((env
->mmucr
& 0xe0000000) == 0xe0000000)
239 if ((env
->mmucr
& 0x98000000) == 0x18000000)
241 if ((env
->mmucr
& 0x54000000) == 0x04000000)
243 if ((env
->mmucr
& 0x2c000000) == 0x00000000)
245 cpu_abort(env
, "Unhandled itlb_replacement");
248 /* Find the corresponding entry in the right TLB
249 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
251 static int find_tlb_entry(CPUSH4State
* env
, target_ulong address
,
252 tlb_t
* entries
, uint8_t nbtlb
, int use_asid
)
254 int match
= MMU_DTLB_MISS
;
259 asid
= env
->pteh
& 0xff;
261 for (i
= 0; i
< nbtlb
; i
++) {
263 continue; /* Invalid entry */
264 if (!entries
[i
].sh
&& use_asid
&& entries
[i
].asid
!= asid
)
265 continue; /* Bad ASID */
266 start
= (entries
[i
].vpn
<< 10) & ~(entries
[i
].size
- 1);
267 end
= start
+ entries
[i
].size
- 1;
268 if (address
>= start
&& address
<= end
) { /* Match */
269 if (match
!= MMU_DTLB_MISS
)
270 return MMU_DTLB_MULTIPLE
; /* Multiple match */
277 static void increment_urc(CPUSH4State
* env
)
282 urb
= ((env
->mmucr
) >> 18) & 0x3f;
283 urc
= ((env
->mmucr
) >> 10) & 0x3f;
285 if ((urb
> 0 && urc
> urb
) || urc
> (UTLB_SIZE
- 1))
287 env
->mmucr
= (env
->mmucr
& 0xffff03ff) | (urc
<< 10);
290 /* Copy and utlb entry into itlb
293 static int copy_utlb_entry_itlb(CPUSH4State
*env
, int utlb
)
298 itlb
= itlb_replacement(env
);
299 ientry
= &env
->itlb
[itlb
];
301 tlb_flush_page(env
, ientry
->vpn
<< 10);
303 *ientry
= env
->utlb
[utlb
];
304 update_itlb_use(env
, itlb
);
309 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
311 static int find_itlb_entry(CPUSH4State
* env
, target_ulong address
,
316 e
= find_tlb_entry(env
, address
, env
->itlb
, ITLB_SIZE
, use_asid
);
317 if (e
== MMU_DTLB_MULTIPLE
) {
318 e
= MMU_ITLB_MULTIPLE
;
319 } else if (e
== MMU_DTLB_MISS
) {
322 update_itlb_use(env
, e
);
328 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
329 static int find_utlb_entry(CPUSH4State
* env
, target_ulong address
, int use_asid
)
331 /* per utlb access */
335 return find_tlb_entry(env
, address
, env
->utlb
, UTLB_SIZE
, use_asid
);
338 /* Match address against MMU
339 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
340 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
341 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
342 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
343 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
345 static int get_mmu_address(CPUSH4State
* env
, target_ulong
* physical
,
346 int *prot
, target_ulong address
,
347 int rw
, int access_type
)
350 tlb_t
*matching
= NULL
;
352 use_asid
= (env
->mmucr
& MMUCR_SV
) == 0 || (env
->sr
& SR_MD
) == 0;
355 n
= find_itlb_entry(env
, address
, use_asid
);
357 matching
= &env
->itlb
[n
];
358 if (!(env
->sr
& SR_MD
) && !(matching
->pr
& 2))
359 n
= MMU_ITLB_VIOLATION
;
363 n
= find_utlb_entry(env
, address
, use_asid
);
365 n
= copy_utlb_entry_itlb(env
, n
);
366 matching
= &env
->itlb
[n
];
367 if (!(env
->sr
& SR_MD
) && !(matching
->pr
& 2)) {
368 n
= MMU_ITLB_VIOLATION
;
370 *prot
= PAGE_READ
| PAGE_EXEC
;
371 if ((matching
->pr
& 1) && matching
->d
) {
375 } else if (n
== MMU_DTLB_MULTIPLE
) {
376 n
= MMU_ITLB_MULTIPLE
;
377 } else if (n
== MMU_DTLB_MISS
) {
382 n
= find_utlb_entry(env
, address
, use_asid
);
384 matching
= &env
->utlb
[n
];
385 if (!(env
->sr
& SR_MD
) && !(matching
->pr
& 2)) {
386 n
= (rw
== 1) ? MMU_DTLB_VIOLATION_WRITE
:
387 MMU_DTLB_VIOLATION_READ
;
388 } else if ((rw
== 1) && !(matching
->pr
& 1)) {
389 n
= MMU_DTLB_VIOLATION_WRITE
;
390 } else if ((rw
== 1) && !matching
->d
) {
391 n
= MMU_DTLB_INITIAL_WRITE
;
394 if ((matching
->pr
& 1) && matching
->d
) {
398 } else if (n
== MMU_DTLB_MISS
) {
399 n
= (rw
== 1) ? MMU_DTLB_MISS_WRITE
:
405 *physical
= ((matching
->ppn
<< 10) & ~(matching
->size
- 1)) |
406 (address
& (matching
->size
- 1));
411 static int get_physical_address(CPUSH4State
* env
, target_ulong
* physical
,
412 int *prot
, target_ulong address
,
413 int rw
, int access_type
)
415 /* P1, P2 and P4 areas do not use translation */
416 if ((address
>= 0x80000000 && address
< 0xc0000000) ||
417 address
>= 0xe0000000) {
418 if (!(env
->sr
& SR_MD
)
419 && (address
< 0xe0000000 || address
>= 0xe4000000)) {
420 /* Unauthorized access in user mode (only store queues are available) */
421 fprintf(stderr
, "Unauthorized access\n");
423 return MMU_DADDR_ERROR_READ
;
425 return MMU_DADDR_ERROR_WRITE
;
427 return MMU_IADDR_ERROR
;
429 if (address
>= 0x80000000 && address
< 0xc0000000) {
430 /* Mask upper 3 bits for P1 and P2 areas */
431 *physical
= address
& 0x1fffffff;
435 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
439 /* If MMU is disabled, return the corresponding physical page */
440 if (!(env
->mmucr
& MMUCR_AT
)) {
441 *physical
= address
& 0x1FFFFFFF;
442 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
446 /* We need to resort to the MMU */
447 return get_mmu_address(env
, physical
, prot
, address
, rw
, access_type
);
450 int cpu_sh4_handle_mmu_fault(CPUSH4State
* env
, target_ulong address
, int rw
,
453 target_ulong physical
;
454 int prot
, ret
, access_type
;
456 access_type
= ACCESS_INT
;
458 get_physical_address(env
, &physical
, &prot
, address
, rw
,
463 if (ret
!= MMU_DTLB_MULTIPLE
&& ret
!= MMU_ITLB_MULTIPLE
) {
464 env
->pteh
= (env
->pteh
& PTEH_ASID_MASK
) |
465 (address
& PTEH_VPN_MASK
);
469 case MMU_DTLB_MISS_READ
:
470 env
->exception_index
= 0x040;
472 case MMU_DTLB_MULTIPLE
:
473 case MMU_ITLB_MULTIPLE
:
474 env
->exception_index
= 0x140;
476 case MMU_ITLB_VIOLATION
:
477 env
->exception_index
= 0x0a0;
479 case MMU_DTLB_MISS_WRITE
:
480 env
->exception_index
= 0x060;
482 case MMU_DTLB_INITIAL_WRITE
:
483 env
->exception_index
= 0x080;
485 case MMU_DTLB_VIOLATION_READ
:
486 env
->exception_index
= 0x0a0;
488 case MMU_DTLB_VIOLATION_WRITE
:
489 env
->exception_index
= 0x0c0;
491 case MMU_IADDR_ERROR
:
492 case MMU_DADDR_ERROR_READ
:
493 env
->exception_index
= 0x0e0;
495 case MMU_DADDR_ERROR_WRITE
:
496 env
->exception_index
= 0x100;
499 cpu_abort(env
, "Unhandled MMU fault");
504 address
&= TARGET_PAGE_MASK
;
505 physical
&= TARGET_PAGE_MASK
;
507 tlb_set_page(env
, address
, physical
, prot
, mmu_idx
, TARGET_PAGE_SIZE
);
511 hwaddr
cpu_get_phys_page_debug(CPUSH4State
* env
, target_ulong addr
)
513 target_ulong physical
;
516 get_physical_address(env
, &physical
, &prot
, addr
, 0, 0);
520 void cpu_load_tlb(CPUSH4State
* env
)
522 int n
= cpu_mmucr_urc(env
->mmucr
);
523 tlb_t
* entry
= &env
->utlb
[n
];
526 /* Overwriting valid entry in utlb. */
527 target_ulong address
= entry
->vpn
<< 10;
528 tlb_flush_page(env
, address
);
531 /* Take values into cpu status from registers. */
532 entry
->asid
= (uint8_t)cpu_pteh_asid(env
->pteh
);
533 entry
->vpn
= cpu_pteh_vpn(env
->pteh
);
534 entry
->v
= (uint8_t)cpu_ptel_v(env
->ptel
);
535 entry
->ppn
= cpu_ptel_ppn(env
->ptel
);
536 entry
->sz
= (uint8_t)cpu_ptel_sz(env
->ptel
);
539 entry
->size
= 1024; /* 1K */
542 entry
->size
= 1024 * 4; /* 4K */
545 entry
->size
= 1024 * 64; /* 64K */
548 entry
->size
= 1024 * 1024; /* 1M */
551 cpu_abort(env
, "Unhandled load_tlb");
554 entry
->sh
= (uint8_t)cpu_ptel_sh(env
->ptel
);
555 entry
->c
= (uint8_t)cpu_ptel_c(env
->ptel
);
556 entry
->pr
= (uint8_t)cpu_ptel_pr(env
->ptel
);
557 entry
->d
= (uint8_t)cpu_ptel_d(env
->ptel
);
558 entry
->wt
= (uint8_t)cpu_ptel_wt(env
->ptel
);
559 entry
->sa
= (uint8_t)cpu_ptea_sa(env
->ptea
);
560 entry
->tc
= (uint8_t)cpu_ptea_tc(env
->ptea
);
563 void cpu_sh4_invalidate_tlb(CPUSH4State
*s
)
568 for (i
= 0; i
< UTLB_SIZE
; i
++) {
569 tlb_t
* entry
= &s
->utlb
[i
];
573 for (i
= 0; i
< ITLB_SIZE
; i
++) {
574 tlb_t
* entry
= &s
->itlb
[i
];
581 uint32_t cpu_sh4_read_mmaped_itlb_addr(CPUSH4State
*s
,
584 int index
= (addr
& 0x00000300) >> 8;
585 tlb_t
* entry
= &s
->itlb
[index
];
587 return (entry
->vpn
<< 10) |
592 void cpu_sh4_write_mmaped_itlb_addr(CPUSH4State
*s
, hwaddr addr
,
595 uint32_t vpn
= (mem_value
& 0xfffffc00) >> 10;
596 uint8_t v
= (uint8_t)((mem_value
& 0x00000100) >> 8);
597 uint8_t asid
= (uint8_t)(mem_value
& 0x000000ff);
599 int index
= (addr
& 0x00000300) >> 8;
600 tlb_t
* entry
= &s
->itlb
[index
];
602 /* Overwriting valid entry in itlb. */
603 target_ulong address
= entry
->vpn
<< 10;
604 tlb_flush_page(s
, address
);
611 uint32_t cpu_sh4_read_mmaped_itlb_data(CPUSH4State
*s
,
614 int array
= (addr
& 0x00800000) >> 23;
615 int index
= (addr
& 0x00000300) >> 8;
616 tlb_t
* entry
= &s
->itlb
[index
];
619 /* ITLB Data Array 1 */
620 return (entry
->ppn
<< 10) |
623 ((entry
->sz
& 1) << 6) |
624 ((entry
->sz
& 2) << 4) |
628 /* ITLB Data Array 2 */
629 return (entry
->tc
<< 1) |
634 void cpu_sh4_write_mmaped_itlb_data(CPUSH4State
*s
, hwaddr addr
,
637 int array
= (addr
& 0x00800000) >> 23;
638 int index
= (addr
& 0x00000300) >> 8;
639 tlb_t
* entry
= &s
->itlb
[index
];
642 /* ITLB Data Array 1 */
644 /* Overwriting valid entry in utlb. */
645 target_ulong address
= entry
->vpn
<< 10;
646 tlb_flush_page(s
, address
);
648 entry
->ppn
= (mem_value
& 0x1ffffc00) >> 10;
649 entry
->v
= (mem_value
& 0x00000100) >> 8;
650 entry
->sz
= (mem_value
& 0x00000080) >> 6 |
651 (mem_value
& 0x00000010) >> 4;
652 entry
->pr
= (mem_value
& 0x00000040) >> 5;
653 entry
->c
= (mem_value
& 0x00000008) >> 3;
654 entry
->sh
= (mem_value
& 0x00000002) >> 1;
656 /* ITLB Data Array 2 */
657 entry
->tc
= (mem_value
& 0x00000008) >> 3;
658 entry
->sa
= (mem_value
& 0x00000007);
662 uint32_t cpu_sh4_read_mmaped_utlb_addr(CPUSH4State
*s
,
665 int index
= (addr
& 0x00003f00) >> 8;
666 tlb_t
* entry
= &s
->utlb
[index
];
668 increment_urc(s
); /* per utlb access */
670 return (entry
->vpn
<< 10) |
675 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State
*s
, hwaddr addr
,
678 int associate
= addr
& 0x0000080;
679 uint32_t vpn
= (mem_value
& 0xfffffc00) >> 10;
680 uint8_t d
= (uint8_t)((mem_value
& 0x00000200) >> 9);
681 uint8_t v
= (uint8_t)((mem_value
& 0x00000100) >> 8);
682 uint8_t asid
= (uint8_t)(mem_value
& 0x000000ff);
683 int use_asid
= (s
->mmucr
& MMUCR_SV
) == 0 || (s
->sr
& SR_MD
) == 0;
687 tlb_t
* utlb_match_entry
= NULL
;
688 int needs_tlb_flush
= 0;
691 for (i
= 0; i
< UTLB_SIZE
; i
++) {
692 tlb_t
* entry
= &s
->utlb
[i
];
696 if (entry
->vpn
== vpn
697 && (!use_asid
|| entry
->asid
== asid
|| entry
->sh
)) {
698 if (utlb_match_entry
) {
699 /* Multiple TLB Exception */
700 s
->exception_index
= 0x140;
708 utlb_match_entry
= entry
;
710 increment_urc(s
); /* per utlb access */
714 for (i
= 0; i
< ITLB_SIZE
; i
++) {
715 tlb_t
* entry
= &s
->itlb
[i
];
716 if (entry
->vpn
== vpn
717 && (!use_asid
|| entry
->asid
== asid
|| entry
->sh
)) {
720 if (utlb_match_entry
)
721 *entry
= *utlb_match_entry
;
729 tlb_flush_page(s
, vpn
<< 10);
732 int index
= (addr
& 0x00003f00) >> 8;
733 tlb_t
* entry
= &s
->utlb
[index
];
735 /* Overwriting valid entry in utlb. */
736 target_ulong address
= entry
->vpn
<< 10;
737 tlb_flush_page(s
, address
);
747 uint32_t cpu_sh4_read_mmaped_utlb_data(CPUSH4State
*s
,
750 int array
= (addr
& 0x00800000) >> 23;
751 int index
= (addr
& 0x00003f00) >> 8;
752 tlb_t
* entry
= &s
->utlb
[index
];
754 increment_urc(s
); /* per utlb access */
757 /* ITLB Data Array 1 */
758 return (entry
->ppn
<< 10) |
761 ((entry
->sz
& 1) << 6) |
762 ((entry
->sz
& 2) << 4) |
768 /* ITLB Data Array 2 */
769 return (entry
->tc
<< 1) |
774 void cpu_sh4_write_mmaped_utlb_data(CPUSH4State
*s
, hwaddr addr
,
777 int array
= (addr
& 0x00800000) >> 23;
778 int index
= (addr
& 0x00003f00) >> 8;
779 tlb_t
* entry
= &s
->utlb
[index
];
781 increment_urc(s
); /* per utlb access */
784 /* UTLB Data Array 1 */
786 /* Overwriting valid entry in utlb. */
787 target_ulong address
= entry
->vpn
<< 10;
788 tlb_flush_page(s
, address
);
790 entry
->ppn
= (mem_value
& 0x1ffffc00) >> 10;
791 entry
->v
= (mem_value
& 0x00000100) >> 8;
792 entry
->sz
= (mem_value
& 0x00000080) >> 6 |
793 (mem_value
& 0x00000010) >> 4;
794 entry
->pr
= (mem_value
& 0x00000060) >> 5;
795 entry
->c
= (mem_value
& 0x00000008) >> 3;
796 entry
->d
= (mem_value
& 0x00000004) >> 2;
797 entry
->sh
= (mem_value
& 0x00000002) >> 1;
798 entry
->wt
= (mem_value
& 0x00000001);
800 /* UTLB Data Array 2 */
801 entry
->tc
= (mem_value
& 0x00000008) >> 3;
802 entry
->sa
= (mem_value
& 0x00000007);
806 int cpu_sh4_is_cached(CPUSH4State
* env
, target_ulong addr
)
809 int use_asid
= (env
->mmucr
& MMUCR_SV
) == 0 || (env
->sr
& SR_MD
) == 0;
812 if (env
->sr
& SR_MD
) {
813 /* For previledged mode, P2 and P4 area is not cachable. */
814 if ((0xA0000000 <= addr
&& addr
< 0xC0000000) || 0xE0000000 <= addr
)
817 /* For user mode, only U0 area is cachable. */
818 if (0x80000000 <= addr
)
823 * TODO : Evaluate CCR and check if the cache is on or off.
824 * Now CCR is not in CPUSH4State, but in SH7750State.
825 * When you move the ccr into CPUSH4State, the code will be
829 /* check if operand cache is enabled or not. */
834 /* if MMU is off, no check for TLB. */
835 if (env
->mmucr
& MMUCR_AT
)
839 n
= find_tlb_entry(env
, addr
, env
->itlb
, ITLB_SIZE
, use_asid
);
841 return env
->itlb
[n
].c
;
843 n
= find_tlb_entry(env
, addr
, env
->utlb
, UTLB_SIZE
, use_asid
);
845 return env
->utlb
[n
].c
;