2 * MIPS emulation helpers for qemu.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
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 "sysemu/kvm.h"
23 #include "exec/exec-all.h"
24 #include "exec/cpu_ldst.h"
37 #if !defined(CONFIG_USER_ONLY)
39 /* no MMU emulation */
40 int no_mmu_map_address (CPUMIPSState
*env
, hwaddr
*physical
, int *prot
,
41 target_ulong address
, int rw
, int access_type
)
51 /* fixed mapping MMU emulation */
52 int fixed_mmu_map_address (CPUMIPSState
*env
, hwaddr
*physical
, int *prot
,
53 target_ulong address
, int rw
, int access_type
)
55 if (address
<= (int32_t)0x7FFFFFFFUL
) {
56 if (!(env
->CP0_Status
& (1 << CP0St_ERL
)))
57 *physical
= address
+ 0x40000000UL
;
60 } else if (address
<= (int32_t)0xBFFFFFFFUL
)
61 *physical
= address
& 0x1FFFFFFF;
72 /* MIPS32/MIPS64 R4000-style MMU emulation */
73 int r4k_map_address (CPUMIPSState
*env
, hwaddr
*physical
, int *prot
,
74 target_ulong address
, int rw
, int access_type
)
76 uint16_t ASID
= env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
;
79 for (i
= 0; i
< env
->tlb
->tlb_in_use
; i
++) {
80 r4k_tlb_t
*tlb
= &env
->tlb
->mmu
.r4k
.tlb
[i
];
81 /* 1k pages are not supported. */
82 target_ulong mask
= tlb
->PageMask
| ~(TARGET_PAGE_MASK
<< 1);
83 target_ulong tag
= address
& ~mask
;
84 target_ulong VPN
= tlb
->VPN
& ~mask
;
85 #if defined(TARGET_MIPS64)
89 /* Check ASID, virtual page number & size */
90 if ((tlb
->G
== 1 || tlb
->ASID
== ASID
) && VPN
== tag
&& !tlb
->EHINV
) {
92 int n
= !!(address
& mask
& ~(mask
>> 1));
93 /* Check access rights */
94 if (!(n
? tlb
->V1
: tlb
->V0
)) {
95 return TLBRET_INVALID
;
97 if (rw
== MMU_INST_FETCH
&& (n
? tlb
->XI1
: tlb
->XI0
)) {
100 if (rw
== MMU_DATA_LOAD
&& (n
? tlb
->RI1
: tlb
->RI0
)) {
103 if (rw
!= MMU_DATA_STORE
|| (n
? tlb
->D1
: tlb
->D0
)) {
104 *physical
= tlb
->PFN
[n
] | (address
& (mask
>> 1));
106 if (n
? tlb
->D1
: tlb
->D0
)
113 return TLBRET_NOMATCH
;
116 static int get_physical_address (CPUMIPSState
*env
, hwaddr
*physical
,
117 int *prot
, target_ulong real_address
,
118 int rw
, int access_type
)
120 /* User mode can only access useg/xuseg */
121 int user_mode
= (env
->hflags
& MIPS_HFLAG_MODE
) == MIPS_HFLAG_UM
;
122 int supervisor_mode
= (env
->hflags
& MIPS_HFLAG_MODE
) == MIPS_HFLAG_SM
;
123 int kernel_mode
= !user_mode
&& !supervisor_mode
;
124 #if defined(TARGET_MIPS64)
125 int UX
= (env
->CP0_Status
& (1 << CP0St_UX
)) != 0;
126 int SX
= (env
->CP0_Status
& (1 << CP0St_SX
)) != 0;
127 int KX
= (env
->CP0_Status
& (1 << CP0St_KX
)) != 0;
129 int ret
= TLBRET_MATCH
;
130 /* effective address (modified for KVM T&E kernel segments) */
131 target_ulong address
= real_address
;
133 #define USEG_LIMIT 0x7FFFFFFFUL
134 #define KSEG0_BASE 0x80000000UL
135 #define KSEG1_BASE 0xA0000000UL
136 #define KSEG2_BASE 0xC0000000UL
137 #define KSEG3_BASE 0xE0000000UL
139 #define KVM_KSEG0_BASE 0x40000000UL
140 #define KVM_KSEG2_BASE 0x60000000UL
143 /* KVM T&E adds guest kernel segments in useg */
144 if (real_address
>= KVM_KSEG0_BASE
) {
145 if (real_address
< KVM_KSEG2_BASE
) {
147 address
+= KSEG0_BASE
- KVM_KSEG0_BASE
;
148 } else if (real_address
<= USEG_LIMIT
) {
150 address
+= KSEG2_BASE
- KVM_KSEG2_BASE
;
155 if (address
<= USEG_LIMIT
) {
157 if (env
->CP0_Status
& (1 << CP0St_ERL
)) {
158 *physical
= address
& 0xFFFFFFFF;
164 ret
= env
->tlb
->map_address(env
, physical
, prot
, real_address
, rw
, access_type
);
166 #if defined(TARGET_MIPS64)
167 } else if (address
< 0x4000000000000000ULL
) {
169 if (UX
&& address
<= (0x3FFFFFFFFFFFFFFFULL
& env
->SEGMask
)) {
170 ret
= env
->tlb
->map_address(env
, physical
, prot
, real_address
, rw
, access_type
);
172 ret
= TLBRET_BADADDR
;
174 } else if (address
< 0x8000000000000000ULL
) {
176 if ((supervisor_mode
|| kernel_mode
) &&
177 SX
&& address
<= (0x7FFFFFFFFFFFFFFFULL
& env
->SEGMask
)) {
178 ret
= env
->tlb
->map_address(env
, physical
, prot
, real_address
, rw
, access_type
);
180 ret
= TLBRET_BADADDR
;
182 } else if (address
< 0xC000000000000000ULL
) {
184 if (kernel_mode
&& KX
&&
185 (address
& 0x07FFFFFFFFFFFFFFULL
) <= env
->PAMask
) {
186 *physical
= address
& env
->PAMask
;
192 ret
= TLBRET_BADADDR
;
194 } else if (address
< 0xFFFFFFFF80000000ULL
) {
196 if (kernel_mode
&& KX
&&
197 address
<= (0xFFFFFFFF7FFFFFFFULL
& env
->SEGMask
)) {
198 ret
= env
->tlb
->map_address(env
, physical
, prot
, real_address
, rw
, access_type
);
200 ret
= TLBRET_BADADDR
;
203 } else if (address
< (int32_t)KSEG1_BASE
) {
206 *physical
= address
- (int32_t)KSEG0_BASE
;
212 ret
= TLBRET_BADADDR
;
214 } else if (address
< (int32_t)KSEG2_BASE
) {
217 *physical
= address
- (int32_t)KSEG1_BASE
;
223 ret
= TLBRET_BADADDR
;
225 } else if (address
< (int32_t)KSEG3_BASE
) {
227 if (supervisor_mode
|| kernel_mode
) {
228 ret
= env
->tlb
->map_address(env
, physical
, prot
, real_address
, rw
, access_type
);
230 ret
= TLBRET_BADADDR
;
234 /* XXX: debug segment is not emulated */
236 ret
= env
->tlb
->map_address(env
, physical
, prot
, real_address
, rw
, access_type
);
238 ret
= TLBRET_BADADDR
;
244 void cpu_mips_tlb_flush(CPUMIPSState
*env
, int flush_global
)
246 MIPSCPU
*cpu
= mips_env_get_cpu(env
);
248 /* Flush qemu's TLB and discard all shadowed entries. */
249 tlb_flush(CPU(cpu
), flush_global
);
250 env
->tlb
->tlb_in_use
= env
->tlb
->nb_tlb
;
253 /* Called for updates to CP0_Status. */
254 void sync_c0_status(CPUMIPSState
*env
, CPUMIPSState
*cpu
, int tc
)
256 int32_t tcstatus
, *tcst
;
257 uint32_t v
= cpu
->CP0_Status
;
258 uint32_t cu
, mx
, asid
, ksu
;
259 uint32_t mask
= ((1 << CP0TCSt_TCU3
)
260 | (1 << CP0TCSt_TCU2
)
261 | (1 << CP0TCSt_TCU1
)
262 | (1 << CP0TCSt_TCU0
)
264 | (3 << CP0TCSt_TKSU
)
265 | (0xff << CP0TCSt_TASID
));
267 cu
= (v
>> CP0St_CU0
) & 0xf;
268 mx
= (v
>> CP0St_MX
) & 0x1;
269 ksu
= (v
>> CP0St_KSU
) & 0x3;
270 asid
= env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
;
272 tcstatus
= cu
<< CP0TCSt_TCU0
;
273 tcstatus
|= mx
<< CP0TCSt_TMX
;
274 tcstatus
|= ksu
<< CP0TCSt_TKSU
;
277 if (tc
== cpu
->current_tc
) {
278 tcst
= &cpu
->active_tc
.CP0_TCStatus
;
280 tcst
= &cpu
->tcs
[tc
].CP0_TCStatus
;
288 void cpu_mips_store_status(CPUMIPSState
*env
, target_ulong val
)
290 uint32_t mask
= env
->CP0_Status_rw_bitmask
;
291 target_ulong old
= env
->CP0_Status
;
293 if (env
->insn_flags
& ISA_MIPS32R6
) {
294 bool has_supervisor
= extract32(mask
, CP0St_KSU
, 2) == 0x3;
295 #if defined(TARGET_MIPS64)
296 uint32_t ksux
= (1 << CP0St_KX
) & val
;
297 ksux
|= (ksux
>> 1) & val
; /* KX = 0 forces SX to be 0 */
298 ksux
|= (ksux
>> 1) & val
; /* SX = 0 forces UX to be 0 */
299 val
= (val
& ~(7 << CP0St_UX
)) | ksux
;
301 if (has_supervisor
&& extract32(val
, CP0St_KSU
, 2) == 0x3) {
302 mask
&= ~(3 << CP0St_KSU
);
304 mask
&= ~(((1 << CP0St_SR
) | (1 << CP0St_NMI
)) & val
);
307 env
->CP0_Status
= (old
& ~mask
) | (val
& mask
);
308 #if defined(TARGET_MIPS64)
309 if ((env
->CP0_Status
^ old
) & (old
& (7 << CP0St_UX
))) {
310 /* Access to at least one of the 64-bit segments has been disabled */
311 cpu_mips_tlb_flush(env
, 1);
314 if (env
->CP0_Config3
& (1 << CP0C3_MT
)) {
315 sync_c0_status(env
, env
, env
->current_tc
);
321 void cpu_mips_store_cause(CPUMIPSState
*env
, target_ulong val
)
323 uint32_t mask
= 0x00C00300;
324 uint32_t old
= env
->CP0_Cause
;
327 if (env
->insn_flags
& ISA_MIPS32R2
) {
328 mask
|= 1 << CP0Ca_DC
;
330 if (env
->insn_flags
& ISA_MIPS32R6
) {
331 mask
&= ~((1 << CP0Ca_WP
) & val
);
334 env
->CP0_Cause
= (env
->CP0_Cause
& ~mask
) | (val
& mask
);
336 if ((old
^ env
->CP0_Cause
) & (1 << CP0Ca_DC
)) {
337 if (env
->CP0_Cause
& (1 << CP0Ca_DC
)) {
338 cpu_mips_stop_count(env
);
340 cpu_mips_start_count(env
);
344 /* Set/reset software interrupts */
345 for (i
= 0 ; i
< 2 ; i
++) {
346 if ((old
^ env
->CP0_Cause
) & (1 << (CP0Ca_IP
+ i
))) {
347 cpu_mips_soft_irq(env
, i
, env
->CP0_Cause
& (1 << (CP0Ca_IP
+ i
)));
353 static void raise_mmu_exception(CPUMIPSState
*env
, target_ulong address
,
354 int rw
, int tlb_error
)
356 CPUState
*cs
= CPU(mips_env_get_cpu(env
));
357 int exception
= 0, error_code
= 0;
359 if (rw
== MMU_INST_FETCH
) {
360 error_code
|= EXCP_INST_NOTAVAIL
;
366 /* Reference to kernel address from user mode or supervisor mode */
367 /* Reference to supervisor address from user mode */
368 if (rw
== MMU_DATA_STORE
) {
369 exception
= EXCP_AdES
;
371 exception
= EXCP_AdEL
;
375 /* No TLB match for a mapped address */
376 if (rw
== MMU_DATA_STORE
) {
377 exception
= EXCP_TLBS
;
379 exception
= EXCP_TLBL
;
381 error_code
|= EXCP_TLB_NOMATCH
;
384 /* TLB match with no valid bit */
385 if (rw
== MMU_DATA_STORE
) {
386 exception
= EXCP_TLBS
;
388 exception
= EXCP_TLBL
;
392 /* TLB match but 'D' bit is cleared */
393 exception
= EXCP_LTLBL
;
396 /* Execute-Inhibit Exception */
397 if (env
->CP0_PageGrain
& (1 << CP0PG_IEC
)) {
398 exception
= EXCP_TLBXI
;
400 exception
= EXCP_TLBL
;
404 /* Read-Inhibit Exception */
405 if (env
->CP0_PageGrain
& (1 << CP0PG_IEC
)) {
406 exception
= EXCP_TLBRI
;
408 exception
= EXCP_TLBL
;
412 /* Raise exception */
413 env
->CP0_BadVAddr
= address
;
414 env
->CP0_Context
= (env
->CP0_Context
& ~0x007fffff) |
415 ((address
>> 9) & 0x007ffff0);
416 env
->CP0_EntryHi
= (env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
) |
417 (address
& (TARGET_PAGE_MASK
<< 1));
418 #if defined(TARGET_MIPS64)
419 env
->CP0_EntryHi
&= env
->SEGMask
;
421 /* PTEBase */ (env
->CP0_XContext
& ((~0ULL) << (env
->SEGBITS
- 7))) |
422 /* R */ (extract64(address
, 62, 2) << (env
->SEGBITS
- 9)) |
423 /* BadVPN2 */ (extract64(address
, 13, env
->SEGBITS
- 13) << 4);
425 cs
->exception_index
= exception
;
426 env
->error_code
= error_code
;
429 #if !defined(CONFIG_USER_ONLY)
430 hwaddr
mips_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
432 MIPSCPU
*cpu
= MIPS_CPU(cs
);
436 if (get_physical_address(&cpu
->env
, &phys_addr
, &prot
, addr
, 0,
444 int mips_cpu_handle_mmu_fault(CPUState
*cs
, vaddr address
, int rw
,
447 MIPSCPU
*cpu
= MIPS_CPU(cs
);
448 CPUMIPSState
*env
= &cpu
->env
;
449 #if !defined(CONFIG_USER_ONLY)
457 log_cpu_state(cs
, 0);
459 qemu_log_mask(CPU_LOG_MMU
,
460 "%s pc " TARGET_FMT_lx
" ad %" VADDR_PRIx
" rw %d mmu_idx %d\n",
461 __func__
, env
->active_tc
.PC
, address
, rw
, mmu_idx
);
464 #if !defined(CONFIG_USER_ONLY)
465 /* XXX: put correct access by using cpu_restore_state()
467 access_type
= ACCESS_INT
;
468 ret
= get_physical_address(env
, &physical
, &prot
,
469 address
, rw
, access_type
);
470 qemu_log_mask(CPU_LOG_MMU
,
471 "%s address=%" VADDR_PRIx
" ret %d physical " TARGET_FMT_plx
473 __func__
, address
, ret
, physical
, prot
);
474 if (ret
== TLBRET_MATCH
) {
475 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
,
476 physical
& TARGET_PAGE_MASK
, prot
| PAGE_EXEC
,
477 mmu_idx
, TARGET_PAGE_SIZE
);
482 raise_mmu_exception(env
, address
, rw
, ret
);
489 #if !defined(CONFIG_USER_ONLY)
490 hwaddr
cpu_mips_translate_address(CPUMIPSState
*env
, target_ulong address
, int rw
)
498 access_type
= ACCESS_INT
;
499 ret
= get_physical_address(env
, &physical
, &prot
,
500 address
, rw
, access_type
);
501 if (ret
!= TLBRET_MATCH
) {
502 raise_mmu_exception(env
, address
, rw
, ret
);
509 static const char * const excp_names
[EXCP_LAST
+ 1] = {
510 [EXCP_RESET
] = "reset",
511 [EXCP_SRESET
] = "soft reset",
512 [EXCP_DSS
] = "debug single step",
513 [EXCP_DINT
] = "debug interrupt",
514 [EXCP_NMI
] = "non-maskable interrupt",
515 [EXCP_MCHECK
] = "machine check",
516 [EXCP_EXT_INTERRUPT
] = "interrupt",
517 [EXCP_DFWATCH
] = "deferred watchpoint",
518 [EXCP_DIB
] = "debug instruction breakpoint",
519 [EXCP_IWATCH
] = "instruction fetch watchpoint",
520 [EXCP_AdEL
] = "address error load",
521 [EXCP_AdES
] = "address error store",
522 [EXCP_TLBF
] = "TLB refill",
523 [EXCP_IBE
] = "instruction bus error",
524 [EXCP_DBp
] = "debug breakpoint",
525 [EXCP_SYSCALL
] = "syscall",
526 [EXCP_BREAK
] = "break",
527 [EXCP_CpU
] = "coprocessor unusable",
528 [EXCP_RI
] = "reserved instruction",
529 [EXCP_OVERFLOW
] = "arithmetic overflow",
530 [EXCP_TRAP
] = "trap",
531 [EXCP_FPE
] = "floating point",
532 [EXCP_DDBS
] = "debug data break store",
533 [EXCP_DWATCH
] = "data watchpoint",
534 [EXCP_LTLBL
] = "TLB modify",
535 [EXCP_TLBL
] = "TLB load",
536 [EXCP_TLBS
] = "TLB store",
537 [EXCP_DBE
] = "data bus error",
538 [EXCP_DDBL
] = "debug data break load",
539 [EXCP_THREAD
] = "thread",
540 [EXCP_MDMX
] = "MDMX",
541 [EXCP_C2E
] = "precise coprocessor 2",
542 [EXCP_CACHE
] = "cache error",
543 [EXCP_TLBXI
] = "TLB execute-inhibit",
544 [EXCP_TLBRI
] = "TLB read-inhibit",
545 [EXCP_MSADIS
] = "MSA disabled",
546 [EXCP_MSAFPE
] = "MSA floating point",
550 target_ulong
exception_resume_pc (CPUMIPSState
*env
)
553 target_ulong isa_mode
;
555 isa_mode
= !!(env
->hflags
& MIPS_HFLAG_M16
);
556 bad_pc
= env
->active_tc
.PC
| isa_mode
;
557 if (env
->hflags
& MIPS_HFLAG_BMASK
) {
558 /* If the exception was raised from a delay slot, come back to
560 bad_pc
-= (env
->hflags
& MIPS_HFLAG_B16
? 2 : 4);
566 #if !defined(CONFIG_USER_ONLY)
567 static void set_hflags_for_handler (CPUMIPSState
*env
)
569 /* Exception handlers are entered in 32-bit mode. */
570 env
->hflags
&= ~(MIPS_HFLAG_M16
);
571 /* ...except that microMIPS lets you choose. */
572 if (env
->insn_flags
& ASE_MICROMIPS
) {
573 env
->hflags
|= (!!(env
->CP0_Config3
574 & (1 << CP0C3_ISA_ON_EXC
))
575 << MIPS_HFLAG_M16_SHIFT
);
579 static inline void set_badinstr_registers(CPUMIPSState
*env
)
581 if (env
->hflags
& MIPS_HFLAG_M16
) {
582 /* TODO: add BadInstr support for microMIPS */
585 if (env
->CP0_Config3
& (1 << CP0C3_BI
)) {
586 env
->CP0_BadInstr
= cpu_ldl_code(env
, env
->active_tc
.PC
);
588 if ((env
->CP0_Config3
& (1 << CP0C3_BP
)) &&
589 (env
->hflags
& MIPS_HFLAG_BMASK
)) {
590 env
->CP0_BadInstrP
= cpu_ldl_code(env
, env
->active_tc
.PC
- 4);
595 void mips_cpu_do_interrupt(CPUState
*cs
)
597 #if !defined(CONFIG_USER_ONLY)
598 MIPSCPU
*cpu
= MIPS_CPU(cs
);
599 CPUMIPSState
*env
= &cpu
->env
;
600 bool update_badinstr
= 0;
605 if (qemu_loglevel_mask(CPU_LOG_INT
)
606 && cs
->exception_index
!= EXCP_EXT_INTERRUPT
) {
607 if (cs
->exception_index
< 0 || cs
->exception_index
> EXCP_LAST
) {
610 name
= excp_names
[cs
->exception_index
];
613 qemu_log("%s enter: PC " TARGET_FMT_lx
" EPC " TARGET_FMT_lx
615 __func__
, env
->active_tc
.PC
, env
->CP0_EPC
, name
);
617 if (cs
->exception_index
== EXCP_EXT_INTERRUPT
&&
618 (env
->hflags
& MIPS_HFLAG_DM
)) {
619 cs
->exception_index
= EXCP_DINT
;
622 switch (cs
->exception_index
) {
624 env
->CP0_Debug
|= 1 << CP0DB_DSS
;
625 /* Debug single step cannot be raised inside a delay slot and
626 resume will always occur on the next instruction
627 (but we assume the pc has always been updated during
628 code translation). */
629 env
->CP0_DEPC
= env
->active_tc
.PC
| !!(env
->hflags
& MIPS_HFLAG_M16
);
630 goto enter_debug_mode
;
632 env
->CP0_Debug
|= 1 << CP0DB_DINT
;
635 env
->CP0_Debug
|= 1 << CP0DB_DIB
;
638 env
->CP0_Debug
|= 1 << CP0DB_DBp
;
641 env
->CP0_Debug
|= 1 << CP0DB_DDBS
;
644 env
->CP0_Debug
|= 1 << CP0DB_DDBL
;
646 env
->CP0_DEPC
= exception_resume_pc(env
);
647 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
649 if (env
->insn_flags
& ISA_MIPS3
) {
650 env
->hflags
|= MIPS_HFLAG_64
;
651 if (!(env
->insn_flags
& ISA_MIPS64R6
) ||
652 env
->CP0_Status
& (1 << CP0St_KX
)) {
653 env
->hflags
&= ~MIPS_HFLAG_AWRAP
;
656 env
->hflags
|= MIPS_HFLAG_DM
| MIPS_HFLAG_CP0
;
657 env
->hflags
&= ~(MIPS_HFLAG_KSU
);
658 /* EJTAG probe trap enable is not implemented... */
659 if (!(env
->CP0_Status
& (1 << CP0St_EXL
)))
660 env
->CP0_Cause
&= ~(1U << CP0Ca_BD
);
661 env
->active_tc
.PC
= env
->exception_base
+ 0x480;
662 set_hflags_for_handler(env
);
668 env
->CP0_Status
|= (1 << CP0St_SR
);
669 memset(env
->CP0_WatchLo
, 0, sizeof(env
->CP0_WatchLo
));
672 env
->CP0_Status
|= (1 << CP0St_NMI
);
674 env
->CP0_ErrorEPC
= exception_resume_pc(env
);
675 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
676 env
->CP0_Status
|= (1 << CP0St_ERL
) | (1 << CP0St_BEV
);
677 if (env
->insn_flags
& ISA_MIPS3
) {
678 env
->hflags
|= MIPS_HFLAG_64
;
679 if (!(env
->insn_flags
& ISA_MIPS64R6
) ||
680 env
->CP0_Status
& (1 << CP0St_KX
)) {
681 env
->hflags
&= ~MIPS_HFLAG_AWRAP
;
684 env
->hflags
|= MIPS_HFLAG_CP0
;
685 env
->hflags
&= ~(MIPS_HFLAG_KSU
);
686 if (!(env
->CP0_Status
& (1 << CP0St_EXL
)))
687 env
->CP0_Cause
&= ~(1U << CP0Ca_BD
);
688 env
->active_tc
.PC
= env
->exception_base
;
689 set_hflags_for_handler(env
);
691 case EXCP_EXT_INTERRUPT
:
693 if (env
->CP0_Cause
& (1 << CP0Ca_IV
)) {
694 uint32_t spacing
= (env
->CP0_IntCtl
>> CP0IntCtl_VS
) & 0x1f;
696 if ((env
->CP0_Status
& (1 << CP0St_BEV
)) || spacing
== 0) {
700 uint32_t pending
= (env
->CP0_Cause
& CP0Ca_IP_mask
) >> CP0Ca_IP
;
702 if (env
->CP0_Config3
& (1 << CP0C3_VEIC
)) {
703 /* For VEIC mode, the external interrupt controller feeds
704 * the vector through the CP0Cause IP lines. */
707 /* Vectored Interrupts
708 * Mask with Status.IM7-IM0 to get enabled interrupts. */
709 pending
&= (env
->CP0_Status
>> CP0St_IM
) & 0xff;
710 /* Find the highest-priority interrupt. */
711 while (pending
>>= 1) {
715 offset
= 0x200 + (vector
* (spacing
<< 5));
721 update_badinstr
= !(env
->error_code
& EXCP_INST_NOTAVAIL
);
725 update_badinstr
= !(env
->error_code
& EXCP_INST_NOTAVAIL
);
726 if ((env
->error_code
& EXCP_TLB_NOMATCH
) &&
727 !(env
->CP0_Status
& (1 << CP0St_EXL
))) {
728 #if defined(TARGET_MIPS64)
729 int R
= env
->CP0_BadVAddr
>> 62;
730 int UX
= (env
->CP0_Status
& (1 << CP0St_UX
)) != 0;
731 int SX
= (env
->CP0_Status
& (1 << CP0St_SX
)) != 0;
732 int KX
= (env
->CP0_Status
& (1 << CP0St_KX
)) != 0;
734 if (((R
== 0 && UX
) || (R
== 1 && SX
) || (R
== 3 && KX
)) &&
735 (!(env
->insn_flags
& (INSN_LOONGSON2E
| INSN_LOONGSON2F
))))
745 if ((env
->error_code
& EXCP_TLB_NOMATCH
) &&
746 !(env
->CP0_Status
& (1 << CP0St_EXL
))) {
747 #if defined(TARGET_MIPS64)
748 int R
= env
->CP0_BadVAddr
>> 62;
749 int UX
= (env
->CP0_Status
& (1 << CP0St_UX
)) != 0;
750 int SX
= (env
->CP0_Status
& (1 << CP0St_SX
)) != 0;
751 int KX
= (env
->CP0_Status
& (1 << CP0St_KX
)) != 0;
753 if (((R
== 0 && UX
) || (R
== 1 && SX
) || (R
== 3 && KX
)) &&
754 (!(env
->insn_flags
& (INSN_LOONGSON2E
| INSN_LOONGSON2F
))))
763 update_badinstr
= !(env
->error_code
& EXCP_INST_NOTAVAIL
);
790 env
->CP0_Cause
= (env
->CP0_Cause
& ~(0x3 << CP0Ca_CE
)) |
791 (env
->error_code
<< CP0Ca_CE
);
828 /* XXX: TODO: manage deferred watch exceptions */
841 if (env
->CP0_Status
& (1 << CP0St_BEV
)) {
847 if (!(env
->CP0_Status
& (1 << CP0St_EXL
))) {
848 env
->CP0_EPC
= exception_resume_pc(env
);
849 if (update_badinstr
) {
850 set_badinstr_registers(env
);
852 if (env
->hflags
& MIPS_HFLAG_BMASK
) {
853 env
->CP0_Cause
|= (1U << CP0Ca_BD
);
855 env
->CP0_Cause
&= ~(1U << CP0Ca_BD
);
857 env
->CP0_Status
|= (1 << CP0St_EXL
);
858 if (env
->insn_flags
& ISA_MIPS3
) {
859 env
->hflags
|= MIPS_HFLAG_64
;
860 if (!(env
->insn_flags
& ISA_MIPS64R6
) ||
861 env
->CP0_Status
& (1 << CP0St_KX
)) {
862 env
->hflags
&= ~MIPS_HFLAG_AWRAP
;
865 env
->hflags
|= MIPS_HFLAG_CP0
;
866 env
->hflags
&= ~(MIPS_HFLAG_KSU
);
868 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
869 if (env
->CP0_Status
& (1 << CP0St_BEV
)) {
870 env
->active_tc
.PC
= env
->exception_base
+ 0x200;
872 env
->active_tc
.PC
= (int32_t)(env
->CP0_EBase
& ~0x3ff);
874 env
->active_tc
.PC
+= offset
;
875 set_hflags_for_handler(env
);
876 env
->CP0_Cause
= (env
->CP0_Cause
& ~(0x1f << CP0Ca_EC
)) | (cause
<< CP0Ca_EC
);
881 if (qemu_loglevel_mask(CPU_LOG_INT
)
882 && cs
->exception_index
!= EXCP_EXT_INTERRUPT
) {
883 qemu_log("%s: PC " TARGET_FMT_lx
" EPC " TARGET_FMT_lx
" cause %d\n"
884 " S %08x C %08x A " TARGET_FMT_lx
" D " TARGET_FMT_lx
"\n",
885 __func__
, env
->active_tc
.PC
, env
->CP0_EPC
, cause
,
886 env
->CP0_Status
, env
->CP0_Cause
, env
->CP0_BadVAddr
,
890 cs
->exception_index
= EXCP_NONE
;
893 bool mips_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
895 if (interrupt_request
& CPU_INTERRUPT_HARD
) {
896 MIPSCPU
*cpu
= MIPS_CPU(cs
);
897 CPUMIPSState
*env
= &cpu
->env
;
899 if (cpu_mips_hw_interrupts_enabled(env
) &&
900 cpu_mips_hw_interrupts_pending(env
)) {
902 cs
->exception_index
= EXCP_EXT_INTERRUPT
;
904 mips_cpu_do_interrupt(cs
);
911 #if !defined(CONFIG_USER_ONLY)
912 void r4k_invalidate_tlb (CPUMIPSState
*env
, int idx
, int use_extra
)
914 MIPSCPU
*cpu
= mips_env_get_cpu(env
);
919 uint16_t ASID
= env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
;
922 tlb
= &env
->tlb
->mmu
.r4k
.tlb
[idx
];
923 /* The qemu TLB is flushed when the ASID changes, so no need to
924 flush these entries again. */
925 if (tlb
->G
== 0 && tlb
->ASID
!= ASID
) {
929 if (use_extra
&& env
->tlb
->tlb_in_use
< MIPS_TLB_MAX
) {
930 /* For tlbwr, we can shadow the discarded entry into
931 a new (fake) TLB entry, as long as the guest can not
932 tell that it's there. */
933 env
->tlb
->mmu
.r4k
.tlb
[env
->tlb
->tlb_in_use
] = *tlb
;
934 env
->tlb
->tlb_in_use
++;
938 /* 1k pages are not supported. */
939 mask
= tlb
->PageMask
| ~(TARGET_PAGE_MASK
<< 1);
942 addr
= tlb
->VPN
& ~mask
;
943 #if defined(TARGET_MIPS64)
944 if (addr
>= (0xFFFFFFFF80000000ULL
& env
->SEGMask
)) {
945 addr
|= 0x3FFFFF0000000000ULL
;
948 end
= addr
| (mask
>> 1);
950 // optimize memset in tlb_flush_page!!!
951 tlb_flush_page(cs
, addr
);
952 addr
+= TARGET_PAGE_SIZE
;
957 addr
= (tlb
->VPN
& ~mask
) | ((mask
>> 1) + 1);
958 #if defined(TARGET_MIPS64)
959 if (addr
>= (0xFFFFFFFF80000000ULL
& env
->SEGMask
)) {
960 addr
|= 0x3FFFFF0000000000ULL
;
964 while (addr
- 1 < end
) {
965 // optimize memset in tlb_flush_page!!!
966 tlb_flush_page(cs
, addr
);
967 addr
+= TARGET_PAGE_SIZE
;
973 void QEMU_NORETURN
do_raise_exception_err(CPUMIPSState
*env
,
978 CPUState
*cs
= CPU(mips_env_get_cpu(env
));
980 if (exception
< EXCP_SC
) {
981 qemu_log_mask(CPU_LOG_INT
, "%s: %d %d\n",
982 __func__
, exception
, error_code
);
984 cs
->exception_index
= exception
;
985 env
->error_code
= error_code
;
987 cpu_loop_exit_restore(cs
, pc
);