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 CPUState
*cs
= CPU(sh_env_get_cpu(env
));
84 int do_irq
= cs
->interrupt_request
& CPU_INTERRUPT_HARD
;
85 int do_exp
, irq_vector
= env
->exception_index
;
87 /* prioritize exceptions over interrupts */
89 do_exp
= env
->exception_index
!= -1;
90 do_irq
= do_irq
&& (env
->exception_index
== -1);
92 if (env
->sr
& SR_BL
) {
93 if (do_exp
&& env
->exception_index
!= 0x1e0) {
94 env
->exception_index
= 0x000; /* masked exception -> reset */
96 if (do_irq
&& !env
->in_sleep
) {
103 irq_vector
= sh_intc_get_pending_vector(env
->intc_handle
,
104 (env
->sr
>> 4) & 0xf);
105 if (irq_vector
== -1) {
110 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
112 switch (env
->exception_index
) {
114 expname
= "addr_error";
117 expname
= "tlb_miss";
120 expname
= "tlb_violation";
123 expname
= "illegal_instruction";
126 expname
= "slot_illegal_instruction";
129 expname
= "fpu_disable";
132 expname
= "slot_fpu";
135 expname
= "data_write";
138 expname
= "dtlb_miss_write";
141 expname
= "dtlb_violation_write";
144 expname
= "fpu_exception";
147 expname
= "initial_page_write";
153 expname
= do_irq
? "interrupt" : "???";
156 qemu_log("exception 0x%03x [%s] raised\n",
157 irq_vector
, expname
);
158 log_cpu_state(env
, 0);
163 env
->sgr
= env
->gregs
[15];
164 env
->sr
|= SR_BL
| SR_MD
| SR_RB
;
166 if (env
->flags
& (DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
)) {
167 /* Branch instruction should be executed again before delay slot. */
169 /* Clear flags for exception/interrupt routine. */
170 env
->flags
&= ~(DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
| DELAY_SLOT_TRUE
);
172 if (env
->flags
& DELAY_SLOT_CLEARME
)
176 env
->expevt
= env
->exception_index
;
177 switch (env
->exception_index
) {
182 env
->sr
|= 0xf << 4; /* IMASK */
183 env
->pc
= 0xa0000000;
187 env
->pc
= env
->vbr
+ 0x400;
190 env
->spc
+= 2; /* special case for TRAPA */
193 env
->pc
= env
->vbr
+ 0x100;
200 env
->intevt
= irq_vector
;
201 env
->pc
= env
->vbr
+ 0x600;
206 static void update_itlb_use(CPUSH4State
* env
, int itlbnb
)
208 uint8_t or_mask
= 0, and_mask
= (uint8_t) - 1;
227 env
->mmucr
&= (and_mask
<< 24) | 0x00ffffff;
228 env
->mmucr
|= (or_mask
<< 24);
231 static int itlb_replacement(CPUSH4State
* env
)
233 if ((env
->mmucr
& 0xe0000000) == 0xe0000000)
235 if ((env
->mmucr
& 0x98000000) == 0x18000000)
237 if ((env
->mmucr
& 0x54000000) == 0x04000000)
239 if ((env
->mmucr
& 0x2c000000) == 0x00000000)
241 cpu_abort(env
, "Unhandled itlb_replacement");
244 /* Find the corresponding entry in the right TLB
245 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
247 static int find_tlb_entry(CPUSH4State
* env
, target_ulong address
,
248 tlb_t
* entries
, uint8_t nbtlb
, int use_asid
)
250 int match
= MMU_DTLB_MISS
;
255 asid
= env
->pteh
& 0xff;
257 for (i
= 0; i
< nbtlb
; i
++) {
259 continue; /* Invalid entry */
260 if (!entries
[i
].sh
&& use_asid
&& entries
[i
].asid
!= asid
)
261 continue; /* Bad ASID */
262 start
= (entries
[i
].vpn
<< 10) & ~(entries
[i
].size
- 1);
263 end
= start
+ entries
[i
].size
- 1;
264 if (address
>= start
&& address
<= end
) { /* Match */
265 if (match
!= MMU_DTLB_MISS
)
266 return MMU_DTLB_MULTIPLE
; /* Multiple match */
273 static void increment_urc(CPUSH4State
* env
)
278 urb
= ((env
->mmucr
) >> 18) & 0x3f;
279 urc
= ((env
->mmucr
) >> 10) & 0x3f;
281 if ((urb
> 0 && urc
> urb
) || urc
> (UTLB_SIZE
- 1))
283 env
->mmucr
= (env
->mmucr
& 0xffff03ff) | (urc
<< 10);
286 /* Copy and utlb entry into itlb
289 static int copy_utlb_entry_itlb(CPUSH4State
*env
, int utlb
)
294 itlb
= itlb_replacement(env
);
295 ientry
= &env
->itlb
[itlb
];
297 tlb_flush_page(env
, ientry
->vpn
<< 10);
299 *ientry
= env
->utlb
[utlb
];
300 update_itlb_use(env
, itlb
);
305 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
307 static int find_itlb_entry(CPUSH4State
* env
, target_ulong address
,
312 e
= find_tlb_entry(env
, address
, env
->itlb
, ITLB_SIZE
, use_asid
);
313 if (e
== MMU_DTLB_MULTIPLE
) {
314 e
= MMU_ITLB_MULTIPLE
;
315 } else if (e
== MMU_DTLB_MISS
) {
318 update_itlb_use(env
, e
);
324 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
325 static int find_utlb_entry(CPUSH4State
* env
, target_ulong address
, int use_asid
)
327 /* per utlb access */
331 return find_tlb_entry(env
, address
, env
->utlb
, UTLB_SIZE
, use_asid
);
334 /* Match address against MMU
335 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
336 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
337 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
338 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
339 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
341 static int get_mmu_address(CPUSH4State
* env
, target_ulong
* physical
,
342 int *prot
, target_ulong address
,
343 int rw
, int access_type
)
346 tlb_t
*matching
= NULL
;
348 use_asid
= (env
->mmucr
& MMUCR_SV
) == 0 || (env
->sr
& SR_MD
) == 0;
351 n
= find_itlb_entry(env
, address
, use_asid
);
353 matching
= &env
->itlb
[n
];
354 if (!(env
->sr
& SR_MD
) && !(matching
->pr
& 2))
355 n
= MMU_ITLB_VIOLATION
;
359 n
= find_utlb_entry(env
, address
, use_asid
);
361 n
= copy_utlb_entry_itlb(env
, n
);
362 matching
= &env
->itlb
[n
];
363 if (!(env
->sr
& SR_MD
) && !(matching
->pr
& 2)) {
364 n
= MMU_ITLB_VIOLATION
;
366 *prot
= PAGE_READ
| PAGE_EXEC
;
367 if ((matching
->pr
& 1) && matching
->d
) {
371 } else if (n
== MMU_DTLB_MULTIPLE
) {
372 n
= MMU_ITLB_MULTIPLE
;
373 } else if (n
== MMU_DTLB_MISS
) {
378 n
= find_utlb_entry(env
, address
, use_asid
);
380 matching
= &env
->utlb
[n
];
381 if (!(env
->sr
& SR_MD
) && !(matching
->pr
& 2)) {
382 n
= (rw
== 1) ? MMU_DTLB_VIOLATION_WRITE
:
383 MMU_DTLB_VIOLATION_READ
;
384 } else if ((rw
== 1) && !(matching
->pr
& 1)) {
385 n
= MMU_DTLB_VIOLATION_WRITE
;
386 } else if ((rw
== 1) && !matching
->d
) {
387 n
= MMU_DTLB_INITIAL_WRITE
;
390 if ((matching
->pr
& 1) && matching
->d
) {
394 } else if (n
== MMU_DTLB_MISS
) {
395 n
= (rw
== 1) ? MMU_DTLB_MISS_WRITE
:
401 *physical
= ((matching
->ppn
<< 10) & ~(matching
->size
- 1)) |
402 (address
& (matching
->size
- 1));
407 static int get_physical_address(CPUSH4State
* env
, target_ulong
* physical
,
408 int *prot
, target_ulong address
,
409 int rw
, int access_type
)
411 /* P1, P2 and P4 areas do not use translation */
412 if ((address
>= 0x80000000 && address
< 0xc0000000) ||
413 address
>= 0xe0000000) {
414 if (!(env
->sr
& SR_MD
)
415 && (address
< 0xe0000000 || address
>= 0xe4000000)) {
416 /* Unauthorized access in user mode (only store queues are available) */
417 fprintf(stderr
, "Unauthorized access\n");
419 return MMU_DADDR_ERROR_READ
;
421 return MMU_DADDR_ERROR_WRITE
;
423 return MMU_IADDR_ERROR
;
425 if (address
>= 0x80000000 && address
< 0xc0000000) {
426 /* Mask upper 3 bits for P1 and P2 areas */
427 *physical
= address
& 0x1fffffff;
431 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
435 /* If MMU is disabled, return the corresponding physical page */
436 if (!(env
->mmucr
& MMUCR_AT
)) {
437 *physical
= address
& 0x1FFFFFFF;
438 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
442 /* We need to resort to the MMU */
443 return get_mmu_address(env
, physical
, prot
, address
, rw
, access_type
);
446 int cpu_sh4_handle_mmu_fault(CPUSH4State
* env
, target_ulong address
, int rw
,
449 target_ulong physical
;
450 int prot
, ret
, access_type
;
452 access_type
= ACCESS_INT
;
454 get_physical_address(env
, &physical
, &prot
, address
, rw
,
459 if (ret
!= MMU_DTLB_MULTIPLE
&& ret
!= MMU_ITLB_MULTIPLE
) {
460 env
->pteh
= (env
->pteh
& PTEH_ASID_MASK
) |
461 (address
& PTEH_VPN_MASK
);
465 case MMU_DTLB_MISS_READ
:
466 env
->exception_index
= 0x040;
468 case MMU_DTLB_MULTIPLE
:
469 case MMU_ITLB_MULTIPLE
:
470 env
->exception_index
= 0x140;
472 case MMU_ITLB_VIOLATION
:
473 env
->exception_index
= 0x0a0;
475 case MMU_DTLB_MISS_WRITE
:
476 env
->exception_index
= 0x060;
478 case MMU_DTLB_INITIAL_WRITE
:
479 env
->exception_index
= 0x080;
481 case MMU_DTLB_VIOLATION_READ
:
482 env
->exception_index
= 0x0a0;
484 case MMU_DTLB_VIOLATION_WRITE
:
485 env
->exception_index
= 0x0c0;
487 case MMU_IADDR_ERROR
:
488 case MMU_DADDR_ERROR_READ
:
489 env
->exception_index
= 0x0e0;
491 case MMU_DADDR_ERROR_WRITE
:
492 env
->exception_index
= 0x100;
495 cpu_abort(env
, "Unhandled MMU fault");
500 address
&= TARGET_PAGE_MASK
;
501 physical
&= TARGET_PAGE_MASK
;
503 tlb_set_page(env
, address
, physical
, prot
, mmu_idx
, TARGET_PAGE_SIZE
);
507 hwaddr
cpu_get_phys_page_debug(CPUSH4State
* env
, target_ulong addr
)
509 target_ulong physical
;
512 get_physical_address(env
, &physical
, &prot
, addr
, 0, 0);
516 void cpu_load_tlb(CPUSH4State
* env
)
518 int n
= cpu_mmucr_urc(env
->mmucr
);
519 tlb_t
* entry
= &env
->utlb
[n
];
522 /* Overwriting valid entry in utlb. */
523 target_ulong address
= entry
->vpn
<< 10;
524 tlb_flush_page(env
, address
);
527 /* Take values into cpu status from registers. */
528 entry
->asid
= (uint8_t)cpu_pteh_asid(env
->pteh
);
529 entry
->vpn
= cpu_pteh_vpn(env
->pteh
);
530 entry
->v
= (uint8_t)cpu_ptel_v(env
->ptel
);
531 entry
->ppn
= cpu_ptel_ppn(env
->ptel
);
532 entry
->sz
= (uint8_t)cpu_ptel_sz(env
->ptel
);
535 entry
->size
= 1024; /* 1K */
538 entry
->size
= 1024 * 4; /* 4K */
541 entry
->size
= 1024 * 64; /* 64K */
544 entry
->size
= 1024 * 1024; /* 1M */
547 cpu_abort(env
, "Unhandled load_tlb");
550 entry
->sh
= (uint8_t)cpu_ptel_sh(env
->ptel
);
551 entry
->c
= (uint8_t)cpu_ptel_c(env
->ptel
);
552 entry
->pr
= (uint8_t)cpu_ptel_pr(env
->ptel
);
553 entry
->d
= (uint8_t)cpu_ptel_d(env
->ptel
);
554 entry
->wt
= (uint8_t)cpu_ptel_wt(env
->ptel
);
555 entry
->sa
= (uint8_t)cpu_ptea_sa(env
->ptea
);
556 entry
->tc
= (uint8_t)cpu_ptea_tc(env
->ptea
);
559 void cpu_sh4_invalidate_tlb(CPUSH4State
*s
)
564 for (i
= 0; i
< UTLB_SIZE
; i
++) {
565 tlb_t
* entry
= &s
->utlb
[i
];
569 for (i
= 0; i
< ITLB_SIZE
; i
++) {
570 tlb_t
* entry
= &s
->itlb
[i
];
577 uint32_t cpu_sh4_read_mmaped_itlb_addr(CPUSH4State
*s
,
580 int index
= (addr
& 0x00000300) >> 8;
581 tlb_t
* entry
= &s
->itlb
[index
];
583 return (entry
->vpn
<< 10) |
588 void cpu_sh4_write_mmaped_itlb_addr(CPUSH4State
*s
, hwaddr addr
,
591 uint32_t vpn
= (mem_value
& 0xfffffc00) >> 10;
592 uint8_t v
= (uint8_t)((mem_value
& 0x00000100) >> 8);
593 uint8_t asid
= (uint8_t)(mem_value
& 0x000000ff);
595 int index
= (addr
& 0x00000300) >> 8;
596 tlb_t
* entry
= &s
->itlb
[index
];
598 /* Overwriting valid entry in itlb. */
599 target_ulong address
= entry
->vpn
<< 10;
600 tlb_flush_page(s
, address
);
607 uint32_t cpu_sh4_read_mmaped_itlb_data(CPUSH4State
*s
,
610 int array
= (addr
& 0x00800000) >> 23;
611 int index
= (addr
& 0x00000300) >> 8;
612 tlb_t
* entry
= &s
->itlb
[index
];
615 /* ITLB Data Array 1 */
616 return (entry
->ppn
<< 10) |
619 ((entry
->sz
& 1) << 6) |
620 ((entry
->sz
& 2) << 4) |
624 /* ITLB Data Array 2 */
625 return (entry
->tc
<< 1) |
630 void cpu_sh4_write_mmaped_itlb_data(CPUSH4State
*s
, hwaddr addr
,
633 int array
= (addr
& 0x00800000) >> 23;
634 int index
= (addr
& 0x00000300) >> 8;
635 tlb_t
* entry
= &s
->itlb
[index
];
638 /* ITLB Data Array 1 */
640 /* Overwriting valid entry in utlb. */
641 target_ulong address
= entry
->vpn
<< 10;
642 tlb_flush_page(s
, address
);
644 entry
->ppn
= (mem_value
& 0x1ffffc00) >> 10;
645 entry
->v
= (mem_value
& 0x00000100) >> 8;
646 entry
->sz
= (mem_value
& 0x00000080) >> 6 |
647 (mem_value
& 0x00000010) >> 4;
648 entry
->pr
= (mem_value
& 0x00000040) >> 5;
649 entry
->c
= (mem_value
& 0x00000008) >> 3;
650 entry
->sh
= (mem_value
& 0x00000002) >> 1;
652 /* ITLB Data Array 2 */
653 entry
->tc
= (mem_value
& 0x00000008) >> 3;
654 entry
->sa
= (mem_value
& 0x00000007);
658 uint32_t cpu_sh4_read_mmaped_utlb_addr(CPUSH4State
*s
,
661 int index
= (addr
& 0x00003f00) >> 8;
662 tlb_t
* entry
= &s
->utlb
[index
];
664 increment_urc(s
); /* per utlb access */
666 return (entry
->vpn
<< 10) |
671 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State
*s
, hwaddr addr
,
674 int associate
= addr
& 0x0000080;
675 uint32_t vpn
= (mem_value
& 0xfffffc00) >> 10;
676 uint8_t d
= (uint8_t)((mem_value
& 0x00000200) >> 9);
677 uint8_t v
= (uint8_t)((mem_value
& 0x00000100) >> 8);
678 uint8_t asid
= (uint8_t)(mem_value
& 0x000000ff);
679 int use_asid
= (s
->mmucr
& MMUCR_SV
) == 0 || (s
->sr
& SR_MD
) == 0;
683 tlb_t
* utlb_match_entry
= NULL
;
684 int needs_tlb_flush
= 0;
687 for (i
= 0; i
< UTLB_SIZE
; i
++) {
688 tlb_t
* entry
= &s
->utlb
[i
];
692 if (entry
->vpn
== vpn
693 && (!use_asid
|| entry
->asid
== asid
|| entry
->sh
)) {
694 if (utlb_match_entry
) {
695 /* Multiple TLB Exception */
696 s
->exception_index
= 0x140;
704 utlb_match_entry
= entry
;
706 increment_urc(s
); /* per utlb access */
710 for (i
= 0; i
< ITLB_SIZE
; i
++) {
711 tlb_t
* entry
= &s
->itlb
[i
];
712 if (entry
->vpn
== vpn
713 && (!use_asid
|| entry
->asid
== asid
|| entry
->sh
)) {
716 if (utlb_match_entry
)
717 *entry
= *utlb_match_entry
;
725 tlb_flush_page(s
, vpn
<< 10);
728 int index
= (addr
& 0x00003f00) >> 8;
729 tlb_t
* entry
= &s
->utlb
[index
];
731 /* Overwriting valid entry in utlb. */
732 target_ulong address
= entry
->vpn
<< 10;
733 tlb_flush_page(s
, address
);
743 uint32_t cpu_sh4_read_mmaped_utlb_data(CPUSH4State
*s
,
746 int array
= (addr
& 0x00800000) >> 23;
747 int index
= (addr
& 0x00003f00) >> 8;
748 tlb_t
* entry
= &s
->utlb
[index
];
750 increment_urc(s
); /* per utlb access */
753 /* ITLB Data Array 1 */
754 return (entry
->ppn
<< 10) |
757 ((entry
->sz
& 1) << 6) |
758 ((entry
->sz
& 2) << 4) |
764 /* ITLB Data Array 2 */
765 return (entry
->tc
<< 1) |
770 void cpu_sh4_write_mmaped_utlb_data(CPUSH4State
*s
, hwaddr addr
,
773 int array
= (addr
& 0x00800000) >> 23;
774 int index
= (addr
& 0x00003f00) >> 8;
775 tlb_t
* entry
= &s
->utlb
[index
];
777 increment_urc(s
); /* per utlb access */
780 /* UTLB Data Array 1 */
782 /* Overwriting valid entry in utlb. */
783 target_ulong address
= entry
->vpn
<< 10;
784 tlb_flush_page(s
, address
);
786 entry
->ppn
= (mem_value
& 0x1ffffc00) >> 10;
787 entry
->v
= (mem_value
& 0x00000100) >> 8;
788 entry
->sz
= (mem_value
& 0x00000080) >> 6 |
789 (mem_value
& 0x00000010) >> 4;
790 entry
->pr
= (mem_value
& 0x00000060) >> 5;
791 entry
->c
= (mem_value
& 0x00000008) >> 3;
792 entry
->d
= (mem_value
& 0x00000004) >> 2;
793 entry
->sh
= (mem_value
& 0x00000002) >> 1;
794 entry
->wt
= (mem_value
& 0x00000001);
796 /* UTLB Data Array 2 */
797 entry
->tc
= (mem_value
& 0x00000008) >> 3;
798 entry
->sa
= (mem_value
& 0x00000007);
802 int cpu_sh4_is_cached(CPUSH4State
* env
, target_ulong addr
)
805 int use_asid
= (env
->mmucr
& MMUCR_SV
) == 0 || (env
->sr
& SR_MD
) == 0;
808 if (env
->sr
& SR_MD
) {
809 /* For previledged mode, P2 and P4 area is not cachable. */
810 if ((0xA0000000 <= addr
&& addr
< 0xC0000000) || 0xE0000000 <= addr
)
813 /* For user mode, only U0 area is cachable. */
814 if (0x80000000 <= addr
)
819 * TODO : Evaluate CCR and check if the cache is on or off.
820 * Now CCR is not in CPUSH4State, but in SH7750State.
821 * When you move the ccr into CPUSH4State, the code will be
825 /* check if operand cache is enabled or not. */
830 /* if MMU is off, no check for TLB. */
831 if (env
->mmucr
& MMUCR_AT
)
835 n
= find_tlb_entry(env
, addr
, env
->itlb
, ITLB_SIZE
, use_asid
);
837 return env
->itlb
[n
].c
;
839 n
= find_tlb_entry(env
, addr
, env
->utlb
, UTLB_SIZE
, use_asid
);
841 return env
->utlb
[n
].c
;