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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
30 #include "hw/sh_intc.h"
32 #if defined(CONFIG_USER_ONLY)
34 void do_interrupt (CPUState
*env
)
36 env
->exception_index
= -1;
39 int cpu_sh4_handle_mmu_fault(CPUState
* env
, target_ulong address
, int rw
,
40 int mmu_idx
, int is_softmmu
)
43 env
->exception_index
= 0;
46 env
->exception_index
= 0x0a0;
49 env
->exception_index
= 0x0c0;
52 env
->exception_index
= 0x0a0;
58 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
* env
, target_ulong addr
)
63 #else /* !CONFIG_USER_ONLY */
66 #define MMU_ITLB_MISS (-1)
67 #define MMU_ITLB_MULTIPLE (-2)
68 #define MMU_ITLB_VIOLATION (-3)
69 #define MMU_DTLB_MISS_READ (-4)
70 #define MMU_DTLB_MISS_WRITE (-5)
71 #define MMU_DTLB_INITIAL_WRITE (-6)
72 #define MMU_DTLB_VIOLATION_READ (-7)
73 #define MMU_DTLB_VIOLATION_WRITE (-8)
74 #define MMU_DTLB_MULTIPLE (-9)
75 #define MMU_DTLB_MISS (-10)
76 #define MMU_IADDR_ERROR (-11)
77 #define MMU_DADDR_ERROR_READ (-12)
78 #define MMU_DADDR_ERROR_WRITE (-13)
80 void do_interrupt(CPUState
* env
)
82 int do_irq
= env
->interrupt_request
& CPU_INTERRUPT_HARD
;
83 int do_exp
, irq_vector
= env
->exception_index
;
85 /* prioritize exceptions over interrupts */
87 do_exp
= env
->exception_index
!= -1;
88 do_irq
= do_irq
&& (env
->exception_index
== -1);
90 if (env
->sr
& SR_BL
) {
91 if (do_exp
&& env
->exception_index
!= 0x1e0) {
92 env
->exception_index
= 0x000; /* masked exception -> reset */
94 if (do_irq
&& !env
->intr_at_halt
) {
97 env
->intr_at_halt
= 0;
101 irq_vector
= sh_intc_get_pending_vector(env
->intc_handle
,
102 (env
->sr
>> 4) & 0xf);
103 if (irq_vector
== -1) {
108 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
110 switch (env
->exception_index
) {
112 expname
= "addr_error";
115 expname
= "tlb_miss";
118 expname
= "tlb_violation";
121 expname
= "illegal_instruction";
124 expname
= "slot_illegal_instruction";
127 expname
= "fpu_disable";
130 expname
= "slot_fpu";
133 expname
= "data_write";
136 expname
= "dtlb_miss_write";
139 expname
= "dtlb_violation_write";
142 expname
= "fpu_exception";
145 expname
= "initial_page_write";
151 expname
= do_irq
? "interrupt" : "???";
154 qemu_log("exception 0x%03x [%s] raised\n",
155 irq_vector
, expname
);
156 log_cpu_state(env
, 0);
161 env
->sgr
= env
->gregs
[15];
162 env
->sr
|= SR_BL
| SR_MD
| SR_RB
;
164 if (env
->flags
& (DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
)) {
165 /* Branch instruction should be executed again before delay slot. */
167 /* Clear flags for exception/interrupt routine. */
168 env
->flags
&= ~(DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
| DELAY_SLOT_TRUE
);
170 if (env
->flags
& DELAY_SLOT_CLEARME
)
174 env
->expevt
= env
->exception_index
;
175 switch (env
->exception_index
) {
180 env
->sr
|= 0xf << 4; /* IMASK */
181 env
->pc
= 0xa0000000;
185 env
->pc
= env
->vbr
+ 0x400;
188 env
->spc
+= 2; /* special case for TRAPA */
191 env
->pc
= env
->vbr
+ 0x100;
198 env
->intevt
= irq_vector
;
199 env
->pc
= env
->vbr
+ 0x600;
204 static void update_itlb_use(CPUState
* env
, int itlbnb
)
206 uint8_t or_mask
= 0, and_mask
= (uint8_t) - 1;
225 env
->mmucr
&= (and_mask
<< 24) | 0x00ffffff;
226 env
->mmucr
|= (or_mask
<< 24);
229 static int itlb_replacement(CPUState
* env
)
231 if ((env
->mmucr
& 0xe0000000) == 0xe0000000)
233 if ((env
->mmucr
& 0x98000000) == 0x18000000)
235 if ((env
->mmucr
& 0x54000000) == 0x04000000)
237 if ((env
->mmucr
& 0x2c000000) == 0x00000000)
242 /* Find the corresponding entry in the right TLB
243 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
245 static int find_tlb_entry(CPUState
* env
, target_ulong address
,
246 tlb_t
* entries
, uint8_t nbtlb
, int use_asid
)
248 int match
= MMU_DTLB_MISS
;
253 asid
= env
->pteh
& 0xff;
255 for (i
= 0; i
< nbtlb
; i
++) {
257 continue; /* Invalid entry */
258 if (!entries
[i
].sh
&& use_asid
&& entries
[i
].asid
!= asid
)
259 continue; /* Bad ASID */
261 switch (entries
[i
].sz
) {
263 size
= 1024; /* 1kB */
266 size
= 4 * 1024; /* 4kB */
269 size
= 64 * 1024; /* 64kB */
272 size
= 1024 * 1024; /* 1MB */
278 start
= (entries
[i
].vpn
<< 10) & ~(entries
[i
].size
- 1);
279 end
= start
+ entries
[i
].size
- 1;
280 if (address
>= start
&& address
<= end
) { /* Match */
281 if (match
!= MMU_DTLB_MISS
)
282 return MMU_DTLB_MULTIPLE
; /* Multiple match */
289 static int same_tlb_entry_exists(const tlb_t
* haystack
, uint8_t nbtlb
,
290 const tlb_t
* needle
)
293 for (i
= 0; i
< nbtlb
; i
++)
294 if (!memcmp(&haystack
[i
], needle
, sizeof(tlb_t
)))
299 static void increment_urc(CPUState
* env
)
304 urb
= ((env
->mmucr
) >> 18) & 0x3f;
305 urc
= ((env
->mmucr
) >> 10) & 0x3f;
307 if ((urb
> 0 && urc
> urb
) || urc
> (UTLB_SIZE
- 1))
309 env
->mmucr
= (env
->mmucr
& 0xffff03ff) | (urc
<< 10);
312 /* Find itlb entry - update itlb from utlb if necessary and asked for
313 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
314 Update the itlb from utlb if update is not 0
316 static int find_itlb_entry(CPUState
* env
, target_ulong address
,
317 int use_asid
, int update
)
321 e
= find_tlb_entry(env
, address
, env
->itlb
, ITLB_SIZE
, use_asid
);
322 if (e
== MMU_DTLB_MULTIPLE
)
323 e
= MMU_ITLB_MULTIPLE
;
324 else if (e
== MMU_DTLB_MISS
&& update
) {
325 e
= find_tlb_entry(env
, address
, env
->utlb
, UTLB_SIZE
, use_asid
);
328 n
= itlb_replacement(env
);
329 ientry
= &env
->itlb
[n
];
331 if (!same_tlb_entry_exists(env
->utlb
, UTLB_SIZE
, ientry
))
332 tlb_flush_page(env
, ientry
->vpn
<< 10);
334 *ientry
= env
->utlb
[e
];
336 } else if (e
== MMU_DTLB_MISS
)
338 } else if (e
== MMU_DTLB_MISS
)
341 update_itlb_use(env
, e
);
346 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
347 static int find_utlb_entry(CPUState
* env
, target_ulong address
, int use_asid
)
349 /* per utlb access */
353 return find_tlb_entry(env
, address
, env
->utlb
, UTLB_SIZE
, use_asid
);
356 /* Match address against MMU
357 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
358 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
359 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
360 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
361 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
363 static int get_mmu_address(CPUState
* env
, target_ulong
* physical
,
364 int *prot
, target_ulong address
,
365 int rw
, int access_type
)
368 tlb_t
*matching
= NULL
;
370 use_asid
= (env
->mmucr
& MMUCR_SV
) == 0 || (env
->sr
& SR_MD
) == 0;
373 n
= find_itlb_entry(env
, address
, use_asid
, 1);
375 matching
= &env
->itlb
[n
];
376 if ((env
->sr
& SR_MD
) & !(matching
->pr
& 2))
377 n
= MMU_ITLB_VIOLATION
;
382 n
= find_utlb_entry(env
, address
, use_asid
);
384 matching
= &env
->utlb
[n
];
385 switch ((matching
->pr
<< 1) | ((env
->sr
& SR_MD
) ? 1 : 0)) {
388 n
= (rw
== 1) ? MMU_DTLB_VIOLATION_WRITE
:
389 MMU_DTLB_VIOLATION_READ
;
395 n
= MMU_DTLB_VIOLATION_WRITE
;
402 *prot
= (rw
== 1)? PAGE_WRITE
: PAGE_READ
;
405 } else if (n
== MMU_DTLB_MISS
) {
406 n
= (rw
== 1) ? MMU_DTLB_MISS_WRITE
:
411 *physical
= ((matching
->ppn
<< 10) & ~(matching
->size
- 1)) |
412 (address
& (matching
->size
- 1));
413 if ((rw
== 1) & !matching
->d
)
414 n
= MMU_DTLB_INITIAL_WRITE
;
421 static int get_physical_address(CPUState
* env
, target_ulong
* physical
,
422 int *prot
, target_ulong address
,
423 int rw
, int access_type
)
425 /* P1, P2 and P4 areas do not use translation */
426 if ((address
>= 0x80000000 && address
< 0xc0000000) ||
427 address
>= 0xe0000000) {
428 if (!(env
->sr
& SR_MD
)
429 && (address
< 0xe0000000 || address
> 0xe4000000)) {
430 /* Unauthorized access in user mode (only store queues are available) */
431 fprintf(stderr
, "Unauthorized access\n");
433 return MMU_DADDR_ERROR_READ
;
435 return MMU_DADDR_ERROR_WRITE
;
437 return MMU_IADDR_ERROR
;
439 if (address
>= 0x80000000 && address
< 0xc0000000) {
440 /* Mask upper 3 bits for P1 and P2 areas */
441 *physical
= address
& 0x1fffffff;
445 *prot
= PAGE_READ
| PAGE_WRITE
;
449 /* If MMU is disabled, return the corresponding physical page */
450 if (!env
->mmucr
& MMUCR_AT
) {
451 *physical
= address
& 0x1FFFFFFF;
452 *prot
= PAGE_READ
| PAGE_WRITE
;
456 /* We need to resort to the MMU */
457 return get_mmu_address(env
, physical
, prot
, address
, rw
, access_type
);
460 int cpu_sh4_handle_mmu_fault(CPUState
* env
, target_ulong address
, int rw
,
461 int mmu_idx
, int is_softmmu
)
463 target_ulong physical
, page_offset
, page_size
;
464 int prot
, ret
, access_type
;
466 access_type
= ACCESS_INT
;
468 get_physical_address(env
, &physical
, &prot
, address
, rw
,
475 case MMU_DTLB_MISS_READ
:
476 env
->exception_index
= 0x040;
478 case MMU_DTLB_MULTIPLE
:
479 case MMU_ITLB_MULTIPLE
:
480 env
->exception_index
= 0x140;
482 case MMU_ITLB_VIOLATION
:
483 env
->exception_index
= 0x0a0;
485 case MMU_DTLB_MISS_WRITE
:
486 env
->exception_index
= 0x060;
488 case MMU_DTLB_INITIAL_WRITE
:
489 env
->exception_index
= 0x080;
491 case MMU_DTLB_VIOLATION_READ
:
492 env
->exception_index
= 0x0a0;
494 case MMU_DTLB_VIOLATION_WRITE
:
495 env
->exception_index
= 0x0c0;
497 case MMU_IADDR_ERROR
:
498 case MMU_DADDR_ERROR_READ
:
499 env
->exception_index
= 0x0c0;
501 case MMU_DADDR_ERROR_WRITE
:
502 env
->exception_index
= 0x100;
510 page_size
= TARGET_PAGE_SIZE
;
512 (address
- (address
& TARGET_PAGE_MASK
)) & ~(page_size
- 1);
513 address
= (address
& TARGET_PAGE_MASK
) + page_offset
;
514 physical
= (physical
& TARGET_PAGE_MASK
) + page_offset
;
516 return tlb_set_page(env
, address
, physical
, prot
, mmu_idx
, is_softmmu
);
519 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
* env
, target_ulong addr
)
521 target_ulong physical
;
524 get_physical_address(env
, &physical
, &prot
, addr
, 0, 0);
528 void cpu_load_tlb(CPUSH4State
* env
)
530 int n
= cpu_mmucr_urc(env
->mmucr
);
531 tlb_t
* entry
= &env
->utlb
[n
];
534 /* Overwriting valid entry in utlb. */
535 target_ulong address
= entry
->vpn
<< 10;
536 if (!same_tlb_entry_exists(env
->itlb
, ITLB_SIZE
, entry
)) {
537 tlb_flush_page(env
, address
);
541 /* Take values into cpu status from registers. */
542 entry
->asid
= (uint8_t)cpu_pteh_asid(env
->pteh
);
543 entry
->vpn
= cpu_pteh_vpn(env
->pteh
);
544 entry
->v
= (uint8_t)cpu_ptel_v(env
->ptel
);
545 entry
->ppn
= cpu_ptel_ppn(env
->ptel
);
546 entry
->sz
= (uint8_t)cpu_ptel_sz(env
->ptel
);
549 entry
->size
= 1024; /* 1K */
552 entry
->size
= 1024 * 4; /* 4K */
555 entry
->size
= 1024 * 64; /* 64K */
558 entry
->size
= 1024 * 1024; /* 1M */
564 entry
->sh
= (uint8_t)cpu_ptel_sh(env
->ptel
);
565 entry
->c
= (uint8_t)cpu_ptel_c(env
->ptel
);
566 entry
->pr
= (uint8_t)cpu_ptel_pr(env
->ptel
);
567 entry
->d
= (uint8_t)cpu_ptel_d(env
->ptel
);
568 entry
->wt
= (uint8_t)cpu_ptel_wt(env
->ptel
);
569 entry
->sa
= (uint8_t)cpu_ptea_sa(env
->ptea
);
570 entry
->tc
= (uint8_t)cpu_ptea_tc(env
->ptea
);
573 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State
*s
, target_phys_addr_t addr
,
576 int associate
= addr
& 0x0000080;
577 uint32_t vpn
= (mem_value
& 0xfffffc00) >> 10;
578 uint8_t d
= (uint8_t)((mem_value
& 0x00000200) >> 9);
579 uint8_t v
= (uint8_t)((mem_value
& 0x00000100) >> 8);
580 uint8_t asid
= (uint8_t)(mem_value
& 0x000000ff);
581 int use_asid
= (s
->mmucr
& MMUCR_SV
) == 0 || (s
->sr
& SR_MD
) == 0;
585 tlb_t
* utlb_match_entry
= NULL
;
586 int needs_tlb_flush
= 0;
589 for (i
= 0; i
< UTLB_SIZE
; i
++) {
590 tlb_t
* entry
= &s
->utlb
[i
];
594 if (entry
->vpn
== vpn
595 && (!use_asid
|| entry
->asid
== asid
|| entry
->sh
)) {
596 if (utlb_match_entry
) {
597 /* Multiple TLB Exception */
598 s
->exception_index
= 0x140;
606 utlb_match_entry
= entry
;
608 increment_urc(s
); /* per utlb access */
612 for (i
= 0; i
< ITLB_SIZE
; i
++) {
613 tlb_t
* entry
= &s
->itlb
[i
];
614 if (entry
->vpn
== vpn
615 && (!use_asid
|| entry
->asid
== asid
|| entry
->sh
)) {
618 if (utlb_match_entry
)
619 *entry
= *utlb_match_entry
;
627 tlb_flush_page(s
, vpn
<< 10);
630 int index
= (addr
& 0x00003f00) >> 8;
631 tlb_t
* entry
= &s
->utlb
[index
];
633 /* Overwriting valid entry in utlb. */
634 target_ulong address
= entry
->vpn
<< 10;
635 if (!same_tlb_entry_exists(s
->itlb
, ITLB_SIZE
, entry
)) {
636 tlb_flush_page(s
, address
);