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 #include "hw/sh_intc.h"
30 #if defined(CONFIG_USER_ONLY)
32 void do_interrupt (CPUState
*env
)
34 env
->exception_index
= -1;
37 int cpu_sh4_handle_mmu_fault(CPUState
* env
, target_ulong address
, int rw
,
38 int mmu_idx
, int is_softmmu
)
41 env
->exception_index
= -1;
44 env
->exception_index
= 0x0a0;
47 env
->exception_index
= 0x0c0;
50 env
->exception_index
= 0x0a0;
56 int cpu_sh4_is_cached(CPUSH4State
* env
, target_ulong addr
)
58 /* For user mode, only U0 area is cachable. */
59 return !(addr
& 0x80000000);
62 #else /* !CONFIG_USER_ONLY */
65 #define MMU_ITLB_MISS (-1)
66 #define MMU_ITLB_MULTIPLE (-2)
67 #define MMU_ITLB_VIOLATION (-3)
68 #define MMU_DTLB_MISS_READ (-4)
69 #define MMU_DTLB_MISS_WRITE (-5)
70 #define MMU_DTLB_INITIAL_WRITE (-6)
71 #define MMU_DTLB_VIOLATION_READ (-7)
72 #define MMU_DTLB_VIOLATION_WRITE (-8)
73 #define MMU_DTLB_MULTIPLE (-9)
74 #define MMU_DTLB_MISS (-10)
75 #define MMU_IADDR_ERROR (-11)
76 #define MMU_DADDR_ERROR_READ (-12)
77 #define MMU_DADDR_ERROR_WRITE (-13)
79 void do_interrupt(CPUState
* env
)
81 int do_irq
= env
->interrupt_request
& CPU_INTERRUPT_HARD
;
82 int do_exp
, irq_vector
= env
->exception_index
;
84 /* prioritize exceptions over interrupts */
86 do_exp
= env
->exception_index
!= -1;
87 do_irq
= do_irq
&& (env
->exception_index
== -1);
89 if (env
->sr
& SR_BL
) {
90 if (do_exp
&& env
->exception_index
!= 0x1e0) {
91 env
->exception_index
= 0x000; /* masked exception -> reset */
93 if (do_irq
&& !env
->intr_at_halt
) {
96 env
->intr_at_halt
= 0;
100 irq_vector
= sh_intc_get_pending_vector(env
->intc_handle
,
101 (env
->sr
>> 4) & 0xf);
102 if (irq_vector
== -1) {
107 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
109 switch (env
->exception_index
) {
111 expname
= "addr_error";
114 expname
= "tlb_miss";
117 expname
= "tlb_violation";
120 expname
= "illegal_instruction";
123 expname
= "slot_illegal_instruction";
126 expname
= "fpu_disable";
129 expname
= "slot_fpu";
132 expname
= "data_write";
135 expname
= "dtlb_miss_write";
138 expname
= "dtlb_violation_write";
141 expname
= "fpu_exception";
144 expname
= "initial_page_write";
150 expname
= do_irq
? "interrupt" : "???";
153 qemu_log("exception 0x%03x [%s] raised\n",
154 irq_vector
, expname
);
155 log_cpu_state(env
, 0);
160 env
->sgr
= env
->gregs
[15];
161 env
->sr
|= SR_BL
| SR_MD
| SR_RB
;
163 if (env
->flags
& (DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
)) {
164 /* Branch instruction should be executed again before delay slot. */
166 /* Clear flags for exception/interrupt routine. */
167 env
->flags
&= ~(DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
| DELAY_SLOT_TRUE
);
169 if (env
->flags
& DELAY_SLOT_CLEARME
)
173 env
->expevt
= env
->exception_index
;
174 switch (env
->exception_index
) {
179 env
->sr
|= 0xf << 4; /* IMASK */
180 env
->pc
= 0xa0000000;
184 env
->pc
= env
->vbr
+ 0x400;
187 env
->spc
+= 2; /* special case for TRAPA */
190 env
->pc
= env
->vbr
+ 0x100;
197 env
->intevt
= irq_vector
;
198 env
->pc
= env
->vbr
+ 0x600;
203 static void update_itlb_use(CPUState
* env
, int itlbnb
)
205 uint8_t or_mask
= 0, and_mask
= (uint8_t) - 1;
224 env
->mmucr
&= (and_mask
<< 24) | 0x00ffffff;
225 env
->mmucr
|= (or_mask
<< 24);
228 static int itlb_replacement(CPUState
* env
)
230 if ((env
->mmucr
& 0xe0000000) == 0xe0000000)
232 if ((env
->mmucr
& 0x98000000) == 0x18000000)
234 if ((env
->mmucr
& 0x54000000) == 0x04000000)
236 if ((env
->mmucr
& 0x2c000000) == 0x00000000)
238 cpu_abort(env
, "Unhandled itlb_replacement");
241 /* Find the corresponding entry in the right TLB
242 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
244 static int find_tlb_entry(CPUState
* env
, target_ulong address
,
245 tlb_t
* entries
, uint8_t nbtlb
, int use_asid
)
247 int match
= MMU_DTLB_MISS
;
252 asid
= env
->pteh
& 0xff;
254 for (i
= 0; i
< nbtlb
; i
++) {
256 continue; /* Invalid entry */
257 if (!entries
[i
].sh
&& use_asid
&& entries
[i
].asid
!= asid
)
258 continue; /* Bad ASID */
259 start
= (entries
[i
].vpn
<< 10) & ~(entries
[i
].size
- 1);
260 end
= start
+ entries
[i
].size
- 1;
261 if (address
>= start
&& address
<= end
) { /* Match */
262 if (match
!= MMU_DTLB_MISS
)
263 return MMU_DTLB_MULTIPLE
; /* Multiple match */
270 static void increment_urc(CPUState
* env
)
275 urb
= ((env
->mmucr
) >> 18) & 0x3f;
276 urc
= ((env
->mmucr
) >> 10) & 0x3f;
278 if ((urb
> 0 && urc
> urb
) || urc
> (UTLB_SIZE
- 1))
280 env
->mmucr
= (env
->mmucr
& 0xffff03ff) | (urc
<< 10);
283 /* Find itlb entry - update itlb from utlb if necessary and asked for
284 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
285 Update the itlb from utlb if update is not 0
287 static int find_itlb_entry(CPUState
* env
, target_ulong address
,
288 int use_asid
, int update
)
292 e
= find_tlb_entry(env
, address
, env
->itlb
, ITLB_SIZE
, use_asid
);
293 if (e
== MMU_DTLB_MULTIPLE
)
294 e
= MMU_ITLB_MULTIPLE
;
295 else if (e
== MMU_DTLB_MISS
&& update
) {
296 e
= find_tlb_entry(env
, address
, env
->utlb
, UTLB_SIZE
, use_asid
);
299 n
= itlb_replacement(env
);
300 ientry
= &env
->itlb
[n
];
302 tlb_flush_page(env
, ientry
->vpn
<< 10);
304 *ientry
= env
->utlb
[e
];
306 } else if (e
== MMU_DTLB_MISS
)
308 } else if (e
== MMU_DTLB_MISS
)
311 update_itlb_use(env
, e
);
316 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
317 static int find_utlb_entry(CPUState
* env
, target_ulong address
, int use_asid
)
319 /* per utlb access */
323 return find_tlb_entry(env
, address
, env
->utlb
, UTLB_SIZE
, use_asid
);
326 /* Match address against MMU
327 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
328 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
329 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
330 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
331 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
333 static int get_mmu_address(CPUState
* env
, target_ulong
* physical
,
334 int *prot
, target_ulong address
,
335 int rw
, int access_type
)
338 tlb_t
*matching
= NULL
;
340 use_asid
= (env
->mmucr
& MMUCR_SV
) == 0 || (env
->sr
& SR_MD
) == 0;
343 n
= find_itlb_entry(env
, address
, use_asid
, 1);
345 matching
= &env
->itlb
[n
];
346 if (!(env
->sr
& SR_MD
) && !(matching
->pr
& 2))
347 n
= MMU_ITLB_VIOLATION
;
352 n
= find_utlb_entry(env
, address
, use_asid
);
354 matching
= &env
->utlb
[n
];
355 if (!(env
->sr
& SR_MD
) && !(matching
->pr
& 2)) {
356 n
= (rw
== 1) ? MMU_DTLB_VIOLATION_WRITE
:
357 MMU_DTLB_VIOLATION_READ
;
358 } else if ((rw
== 1) && !(matching
->pr
& 1)) {
359 n
= MMU_DTLB_VIOLATION_WRITE
;
360 } else if ((rw
== 1) & !matching
->d
) {
361 n
= MMU_DTLB_INITIAL_WRITE
;
364 if ((matching
->pr
& 1) && matching
->d
) {
368 } else if (n
== MMU_DTLB_MISS
) {
369 n
= (rw
== 1) ? MMU_DTLB_MISS_WRITE
:
375 *physical
= ((matching
->ppn
<< 10) & ~(matching
->size
- 1)) |
376 (address
& (matching
->size
- 1));
381 static int get_physical_address(CPUState
* env
, target_ulong
* physical
,
382 int *prot
, target_ulong address
,
383 int rw
, int access_type
)
385 /* P1, P2 and P4 areas do not use translation */
386 if ((address
>= 0x80000000 && address
< 0xc0000000) ||
387 address
>= 0xe0000000) {
388 if (!(env
->sr
& SR_MD
)
389 && (address
< 0xe0000000 || address
>= 0xe4000000)) {
390 /* Unauthorized access in user mode (only store queues are available) */
391 fprintf(stderr
, "Unauthorized access\n");
393 return MMU_DADDR_ERROR_READ
;
395 return MMU_DADDR_ERROR_WRITE
;
397 return MMU_IADDR_ERROR
;
399 if (address
>= 0x80000000 && address
< 0xc0000000) {
400 /* Mask upper 3 bits for P1 and P2 areas */
401 *physical
= address
& 0x1fffffff;
405 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
409 /* If MMU is disabled, return the corresponding physical page */
410 if (!env
->mmucr
& MMUCR_AT
) {
411 *physical
= address
& 0x1FFFFFFF;
412 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
416 /* We need to resort to the MMU */
417 return get_mmu_address(env
, physical
, prot
, address
, rw
, access_type
);
420 int cpu_sh4_handle_mmu_fault(CPUState
* env
, target_ulong address
, int rw
,
421 int mmu_idx
, int is_softmmu
)
423 target_ulong physical
;
424 int prot
, ret
, access_type
;
426 access_type
= ACCESS_INT
;
428 get_physical_address(env
, &physical
, &prot
, address
, rw
,
435 case MMU_DTLB_MISS_READ
:
436 env
->exception_index
= 0x040;
438 case MMU_DTLB_MULTIPLE
:
439 case MMU_ITLB_MULTIPLE
:
440 env
->exception_index
= 0x140;
442 case MMU_ITLB_VIOLATION
:
443 env
->exception_index
= 0x0a0;
445 case MMU_DTLB_MISS_WRITE
:
446 env
->exception_index
= 0x060;
448 case MMU_DTLB_INITIAL_WRITE
:
449 env
->exception_index
= 0x080;
451 case MMU_DTLB_VIOLATION_READ
:
452 env
->exception_index
= 0x0a0;
454 case MMU_DTLB_VIOLATION_WRITE
:
455 env
->exception_index
= 0x0c0;
457 case MMU_IADDR_ERROR
:
458 case MMU_DADDR_ERROR_READ
:
459 env
->exception_index
= 0x0c0;
461 case MMU_DADDR_ERROR_WRITE
:
462 env
->exception_index
= 0x100;
465 cpu_abort(env
, "Unhandled MMU fault");
470 address
&= TARGET_PAGE_MASK
;
471 physical
&= TARGET_PAGE_MASK
;
473 tlb_set_page(env
, address
, physical
, prot
, mmu_idx
, TARGET_PAGE_SIZE
);
477 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
* env
, target_ulong addr
)
479 target_ulong physical
;
482 get_physical_address(env
, &physical
, &prot
, addr
, 0, 0);
486 void cpu_load_tlb(CPUSH4State
* env
)
488 int n
= cpu_mmucr_urc(env
->mmucr
);
489 tlb_t
* entry
= &env
->utlb
[n
];
492 /* Overwriting valid entry in utlb. */
493 target_ulong address
= entry
->vpn
<< 10;
494 tlb_flush_page(env
, address
);
497 /* Take values into cpu status from registers. */
498 entry
->asid
= (uint8_t)cpu_pteh_asid(env
->pteh
);
499 entry
->vpn
= cpu_pteh_vpn(env
->pteh
);
500 entry
->v
= (uint8_t)cpu_ptel_v(env
->ptel
);
501 entry
->ppn
= cpu_ptel_ppn(env
->ptel
);
502 entry
->sz
= (uint8_t)cpu_ptel_sz(env
->ptel
);
505 entry
->size
= 1024; /* 1K */
508 entry
->size
= 1024 * 4; /* 4K */
511 entry
->size
= 1024 * 64; /* 64K */
514 entry
->size
= 1024 * 1024; /* 1M */
517 cpu_abort(env
, "Unhandled load_tlb");
520 entry
->sh
= (uint8_t)cpu_ptel_sh(env
->ptel
);
521 entry
->c
= (uint8_t)cpu_ptel_c(env
->ptel
);
522 entry
->pr
= (uint8_t)cpu_ptel_pr(env
->ptel
);
523 entry
->d
= (uint8_t)cpu_ptel_d(env
->ptel
);
524 entry
->wt
= (uint8_t)cpu_ptel_wt(env
->ptel
);
525 entry
->sa
= (uint8_t)cpu_ptea_sa(env
->ptea
);
526 entry
->tc
= (uint8_t)cpu_ptea_tc(env
->ptea
);
529 void cpu_sh4_invalidate_tlb(CPUSH4State
*s
)
534 for (i
= 0; i
< UTLB_SIZE
; i
++) {
535 tlb_t
* entry
= &s
->utlb
[i
];
539 for (i
= 0; i
< UTLB_SIZE
; i
++) {
540 tlb_t
* entry
= &s
->utlb
[i
];
547 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State
*s
, target_phys_addr_t addr
,
550 int associate
= addr
& 0x0000080;
551 uint32_t vpn
= (mem_value
& 0xfffffc00) >> 10;
552 uint8_t d
= (uint8_t)((mem_value
& 0x00000200) >> 9);
553 uint8_t v
= (uint8_t)((mem_value
& 0x00000100) >> 8);
554 uint8_t asid
= (uint8_t)(mem_value
& 0x000000ff);
555 int use_asid
= (s
->mmucr
& MMUCR_SV
) == 0 || (s
->sr
& SR_MD
) == 0;
559 tlb_t
* utlb_match_entry
= NULL
;
560 int needs_tlb_flush
= 0;
563 for (i
= 0; i
< UTLB_SIZE
; i
++) {
564 tlb_t
* entry
= &s
->utlb
[i
];
568 if (entry
->vpn
== vpn
569 && (!use_asid
|| entry
->asid
== asid
|| entry
->sh
)) {
570 if (utlb_match_entry
) {
571 /* Multiple TLB Exception */
572 s
->exception_index
= 0x140;
580 utlb_match_entry
= entry
;
582 increment_urc(s
); /* per utlb access */
586 for (i
= 0; i
< ITLB_SIZE
; i
++) {
587 tlb_t
* entry
= &s
->itlb
[i
];
588 if (entry
->vpn
== vpn
589 && (!use_asid
|| entry
->asid
== asid
|| entry
->sh
)) {
592 if (utlb_match_entry
)
593 *entry
= *utlb_match_entry
;
601 tlb_flush_page(s
, vpn
<< 10);
604 int index
= (addr
& 0x00003f00) >> 8;
605 tlb_t
* entry
= &s
->utlb
[index
];
607 /* Overwriting valid entry in utlb. */
608 target_ulong address
= entry
->vpn
<< 10;
609 tlb_flush_page(s
, address
);
619 int cpu_sh4_is_cached(CPUSH4State
* env
, target_ulong addr
)
622 int use_asid
= (env
->mmucr
& MMUCR_SV
) == 0 || (env
->sr
& SR_MD
) == 0;
625 if (env
->sr
& SR_MD
) {
626 /* For previledged mode, P2 and P4 area is not cachable. */
627 if ((0xA0000000 <= addr
&& addr
< 0xC0000000) || 0xE0000000 <= addr
)
630 /* For user mode, only U0 area is cachable. */
631 if (0x80000000 <= addr
)
636 * TODO : Evaluate CCR and check if the cache is on or off.
637 * Now CCR is not in CPUSH4State, but in SH7750State.
638 * When you move the ccr inot CPUSH4State, the code will be
642 /* check if operand cache is enabled or not. */
647 /* if MMU is off, no check for TLB. */
648 if (env
->mmucr
& MMUCR_AT
)
652 n
= find_tlb_entry(env
, addr
, env
->itlb
, ITLB_SIZE
, use_asid
);
654 return env
->itlb
[n
].c
;
656 n
= find_tlb_entry(env
, addr
, env
->utlb
, UTLB_SIZE
, use_asid
);
658 return env
->utlb
[n
].c
;