xhci: fix logging
[qemu/ar7.git] / target / mips / helper.c
blobe359ca3b448daa15b3536af3b0c19e77c465dfe5
1 /*
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"
21 #include "cpu.h"
22 #include "sysemu/kvm.h"
23 #include "exec/exec-all.h"
24 #include "exec/cpu_ldst.h"
25 #include "exec/log.h"
27 enum {
28 TLBRET_XI = -6,
29 TLBRET_RI = -5,
30 TLBRET_DIRTY = -4,
31 TLBRET_INVALID = -3,
32 TLBRET_NOMATCH = -2,
33 TLBRET_BADADDR = -1,
34 TLBRET_MATCH = 0
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)
43 *physical = address;
44 *prot = PAGE_READ | PAGE_WRITE;
45 return TLBRET_MATCH;
48 /* fixed mapping MMU emulation */
49 int fixed_mmu_map_address (CPUMIPSState *env, hwaddr *physical, int *prot,
50 target_ulong address, int rw, int access_type)
52 if (address <= (int32_t)0x7FFFFFFFUL) {
53 if (!(env->CP0_Status & (1 << CP0St_ERL)))
54 *physical = address + 0x40000000UL;
55 else
56 *physical = address;
57 } else if (address <= (int32_t)0xBFFFFFFFUL)
58 *physical = address & 0x1FFFFFFF;
59 else
60 *physical = address;
62 *prot = PAGE_READ | PAGE_WRITE;
63 return TLBRET_MATCH;
66 /* MIPS32/MIPS64 R4000-style MMU emulation */
67 int r4k_map_address (CPUMIPSState *env, hwaddr *physical, int *prot,
68 target_ulong address, int rw, int access_type)
70 uint16_t ASID = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask;
71 int i;
73 for (i = 0; i < env->tlb->tlb_in_use; i++) {
74 r4k_tlb_t *tlb = &env->tlb->mmu.r4k.tlb[i];
75 /* 1k pages are not supported. */
76 target_ulong mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
77 target_ulong tag = address & ~mask;
78 target_ulong VPN = tlb->VPN & ~mask;
79 #if defined(TARGET_MIPS64)
80 tag &= env->SEGMask;
81 #endif
83 /* Check ASID, virtual page number & size */
84 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag && !tlb->EHINV) {
85 /* TLB match */
86 int n = !!(address & mask & ~(mask >> 1));
87 /* Check access rights */
88 if (!(n ? tlb->V1 : tlb->V0)) {
89 return TLBRET_INVALID;
91 if (rw == MMU_INST_FETCH && (n ? tlb->XI1 : tlb->XI0)) {
92 return TLBRET_XI;
94 if (rw == MMU_DATA_LOAD && (n ? tlb->RI1 : tlb->RI0)) {
95 return TLBRET_RI;
97 if (rw != MMU_DATA_STORE || (n ? tlb->D1 : tlb->D0)) {
98 *physical = tlb->PFN[n] | (address & (mask >> 1));
99 *prot = PAGE_READ;
100 if (n ? tlb->D1 : tlb->D0)
101 *prot |= PAGE_WRITE;
102 return TLBRET_MATCH;
104 return TLBRET_DIRTY;
107 return TLBRET_NOMATCH;
110 static int get_physical_address (CPUMIPSState *env, hwaddr *physical,
111 int *prot, target_ulong real_address,
112 int rw, int access_type)
114 /* User mode can only access useg/xuseg */
115 int user_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM;
116 int supervisor_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_SM;
117 int kernel_mode = !user_mode && !supervisor_mode;
118 #if defined(TARGET_MIPS64)
119 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
120 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
121 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
122 #endif
123 int ret = TLBRET_MATCH;
124 /* effective address (modified for KVM T&E kernel segments) */
125 target_ulong address = real_address;
127 #define USEG_LIMIT 0x7FFFFFFFUL
128 #define KSEG0_BASE 0x80000000UL
129 #define KSEG1_BASE 0xA0000000UL
130 #define KSEG2_BASE 0xC0000000UL
131 #define KSEG3_BASE 0xE0000000UL
133 #define KVM_KSEG0_BASE 0x40000000UL
134 #define KVM_KSEG2_BASE 0x60000000UL
136 if (kvm_enabled()) {
137 /* KVM T&E adds guest kernel segments in useg */
138 if (real_address >= KVM_KSEG0_BASE) {
139 if (real_address < KVM_KSEG2_BASE) {
140 /* kseg0 */
141 address += KSEG0_BASE - KVM_KSEG0_BASE;
142 } else if (real_address <= USEG_LIMIT) {
143 /* kseg2/3 */
144 address += KSEG2_BASE - KVM_KSEG2_BASE;
149 if (address <= USEG_LIMIT) {
150 /* useg */
151 if (env->CP0_Status & (1 << CP0St_ERL)) {
152 *physical = address & 0xFFFFFFFF;
153 *prot = PAGE_READ | PAGE_WRITE;
154 } else {
155 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
157 #if defined(TARGET_MIPS64)
158 } else if (address < 0x4000000000000000ULL) {
159 /* xuseg */
160 if (UX && address <= (0x3FFFFFFFFFFFFFFFULL & env->SEGMask)) {
161 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
162 } else {
163 ret = TLBRET_BADADDR;
165 } else if (address < 0x8000000000000000ULL) {
166 /* xsseg */
167 if ((supervisor_mode || kernel_mode) &&
168 SX && address <= (0x7FFFFFFFFFFFFFFFULL & env->SEGMask)) {
169 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
170 } else {
171 ret = TLBRET_BADADDR;
173 } else if (address < 0xC000000000000000ULL) {
174 /* xkphys */
175 if (kernel_mode && KX &&
176 (address & 0x07FFFFFFFFFFFFFFULL) <= env->PAMask) {
177 *physical = address & env->PAMask;
178 *prot = PAGE_READ | PAGE_WRITE;
179 } else {
180 ret = TLBRET_BADADDR;
182 } else if (address < 0xFFFFFFFF80000000ULL) {
183 /* xkseg */
184 if (kernel_mode && KX &&
185 address <= (0xFFFFFFFF7FFFFFFFULL & env->SEGMask)) {
186 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
187 } else {
188 ret = TLBRET_BADADDR;
190 #endif
191 } else if (address < (int32_t)KSEG1_BASE) {
192 /* kseg0 */
193 if (kernel_mode) {
194 *physical = address - (int32_t)KSEG0_BASE;
195 *prot = PAGE_READ | PAGE_WRITE;
196 } else {
197 ret = TLBRET_BADADDR;
199 } else if (address < (int32_t)KSEG2_BASE) {
200 /* kseg1 */
201 if (kernel_mode) {
202 *physical = address - (int32_t)KSEG1_BASE;
203 *prot = PAGE_READ | PAGE_WRITE;
204 } else {
205 ret = TLBRET_BADADDR;
207 } else if (address < (int32_t)KSEG3_BASE) {
208 /* sseg (kseg2) */
209 if (supervisor_mode || kernel_mode) {
210 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
211 } else {
212 ret = TLBRET_BADADDR;
214 } else {
215 /* kseg3 */
216 /* XXX: debug segment is not emulated */
217 if (kernel_mode) {
218 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
219 } else {
220 ret = TLBRET_BADADDR;
223 return ret;
226 void cpu_mips_tlb_flush(CPUMIPSState *env)
228 MIPSCPU *cpu = mips_env_get_cpu(env);
230 /* Flush qemu's TLB and discard all shadowed entries. */
231 tlb_flush(CPU(cpu));
232 env->tlb->tlb_in_use = env->tlb->nb_tlb;
235 /* Called for updates to CP0_Status. */
236 void sync_c0_status(CPUMIPSState *env, CPUMIPSState *cpu, int tc)
238 int32_t tcstatus, *tcst;
239 uint32_t v = cpu->CP0_Status;
240 uint32_t cu, mx, asid, ksu;
241 uint32_t mask = ((1 << CP0TCSt_TCU3)
242 | (1 << CP0TCSt_TCU2)
243 | (1 << CP0TCSt_TCU1)
244 | (1 << CP0TCSt_TCU0)
245 | (1 << CP0TCSt_TMX)
246 | (3 << CP0TCSt_TKSU)
247 | (0xff << CP0TCSt_TASID));
249 cu = (v >> CP0St_CU0) & 0xf;
250 mx = (v >> CP0St_MX) & 0x1;
251 ksu = (v >> CP0St_KSU) & 0x3;
252 asid = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask;
254 tcstatus = cu << CP0TCSt_TCU0;
255 tcstatus |= mx << CP0TCSt_TMX;
256 tcstatus |= ksu << CP0TCSt_TKSU;
257 tcstatus |= asid;
259 if (tc == cpu->current_tc) {
260 tcst = &cpu->active_tc.CP0_TCStatus;
261 } else {
262 tcst = &cpu->tcs[tc].CP0_TCStatus;
265 *tcst &= ~mask;
266 *tcst |= tcstatus;
267 compute_hflags(cpu);
270 void cpu_mips_store_status(CPUMIPSState *env, target_ulong val)
272 uint32_t mask = env->CP0_Status_rw_bitmask;
273 target_ulong old = env->CP0_Status;
275 if (env->insn_flags & ISA_MIPS32R6) {
276 bool has_supervisor = extract32(mask, CP0St_KSU, 2) == 0x3;
277 #if defined(TARGET_MIPS64)
278 uint32_t ksux = (1 << CP0St_KX) & val;
279 ksux |= (ksux >> 1) & val; /* KX = 0 forces SX to be 0 */
280 ksux |= (ksux >> 1) & val; /* SX = 0 forces UX to be 0 */
281 val = (val & ~(7 << CP0St_UX)) | ksux;
282 #endif
283 if (has_supervisor && extract32(val, CP0St_KSU, 2) == 0x3) {
284 mask &= ~(3 << CP0St_KSU);
286 mask &= ~(((1 << CP0St_SR) | (1 << CP0St_NMI)) & val);
289 env->CP0_Status = (old & ~mask) | (val & mask);
290 #if defined(TARGET_MIPS64)
291 if ((env->CP0_Status ^ old) & (old & (7 << CP0St_UX))) {
292 /* Access to at least one of the 64-bit segments has been disabled */
293 cpu_mips_tlb_flush(env);
295 #endif
296 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
297 sync_c0_status(env, env, env->current_tc);
298 } else {
299 compute_hflags(env);
303 void cpu_mips_store_cause(CPUMIPSState *env, target_ulong val)
305 uint32_t mask = 0x00C00300;
306 uint32_t old = env->CP0_Cause;
307 int i;
309 if (env->insn_flags & ISA_MIPS32R2) {
310 mask |= 1 << CP0Ca_DC;
312 if (env->insn_flags & ISA_MIPS32R6) {
313 mask &= ~((1 << CP0Ca_WP) & val);
316 env->CP0_Cause = (env->CP0_Cause & ~mask) | (val & mask);
318 if ((old ^ env->CP0_Cause) & (1 << CP0Ca_DC)) {
319 if (env->CP0_Cause & (1 << CP0Ca_DC)) {
320 cpu_mips_stop_count(env);
321 } else {
322 cpu_mips_start_count(env);
326 /* Set/reset software interrupts */
327 for (i = 0 ; i < 2 ; i++) {
328 if ((old ^ env->CP0_Cause) & (1 << (CP0Ca_IP + i))) {
329 cpu_mips_soft_irq(env, i, env->CP0_Cause & (1 << (CP0Ca_IP + i)));
333 #endif
335 static void raise_mmu_exception(CPUMIPSState *env, target_ulong address,
336 int rw, int tlb_error)
338 CPUState *cs = CPU(mips_env_get_cpu(env));
339 int exception = 0, error_code = 0;
341 if (rw == MMU_INST_FETCH) {
342 error_code |= EXCP_INST_NOTAVAIL;
345 switch (tlb_error) {
346 default:
347 case TLBRET_BADADDR:
348 /* Reference to kernel address from user mode or supervisor mode */
349 /* Reference to supervisor address from user mode */
350 if (rw == MMU_DATA_STORE) {
351 exception = EXCP_AdES;
352 } else {
353 exception = EXCP_AdEL;
355 break;
356 case TLBRET_NOMATCH:
357 /* No TLB match for a mapped address */
358 if (rw == MMU_DATA_STORE) {
359 exception = EXCP_TLBS;
360 } else {
361 exception = EXCP_TLBL;
363 error_code |= EXCP_TLB_NOMATCH;
364 break;
365 case TLBRET_INVALID:
366 /* TLB match with no valid bit */
367 if (rw == MMU_DATA_STORE) {
368 exception = EXCP_TLBS;
369 } else {
370 exception = EXCP_TLBL;
372 break;
373 case TLBRET_DIRTY:
374 /* TLB match but 'D' bit is cleared */
375 exception = EXCP_LTLBL;
376 break;
377 case TLBRET_XI:
378 /* Execute-Inhibit Exception */
379 if (env->CP0_PageGrain & (1 << CP0PG_IEC)) {
380 exception = EXCP_TLBXI;
381 } else {
382 exception = EXCP_TLBL;
384 break;
385 case TLBRET_RI:
386 /* Read-Inhibit Exception */
387 if (env->CP0_PageGrain & (1 << CP0PG_IEC)) {
388 exception = EXCP_TLBRI;
389 } else {
390 exception = EXCP_TLBL;
392 break;
394 /* Raise exception */
395 env->CP0_BadVAddr = address;
396 env->CP0_Context = (env->CP0_Context & ~0x007fffff) |
397 ((address >> 9) & 0x007ffff0);
398 env->CP0_EntryHi = (env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask) |
399 (env->CP0_EntryHi & (1 << CP0EnHi_EHINV)) |
400 (address & (TARGET_PAGE_MASK << 1));
401 #if defined(TARGET_MIPS64)
402 env->CP0_EntryHi &= env->SEGMask;
403 env->CP0_XContext =
404 /* PTEBase */ (env->CP0_XContext & ((~0ULL) << (env->SEGBITS - 7))) |
405 /* R */ (extract64(address, 62, 2) << (env->SEGBITS - 9)) |
406 /* BadVPN2 */ (extract64(address, 13, env->SEGBITS - 13) << 4);
407 #endif
408 cs->exception_index = exception;
409 env->error_code = error_code;
412 #if !defined(CONFIG_USER_ONLY)
413 hwaddr mips_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
415 MIPSCPU *cpu = MIPS_CPU(cs);
416 hwaddr phys_addr;
417 int prot;
419 if (get_physical_address(&cpu->env, &phys_addr, &prot, addr, 0,
420 ACCESS_INT) != 0) {
421 return -1;
423 return phys_addr;
425 #endif
427 int mips_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
428 int mmu_idx)
430 MIPSCPU *cpu = MIPS_CPU(cs);
431 CPUMIPSState *env = &cpu->env;
432 #if !defined(CONFIG_USER_ONLY)
433 hwaddr physical;
434 int prot;
435 int access_type;
436 #endif
437 int ret = 0;
439 #if 0
440 log_cpu_state(cs, 0);
441 #endif
442 qemu_log_mask(CPU_LOG_MMU,
443 "%s pc " TARGET_FMT_lx " ad %" VADDR_PRIx " rw %d mmu_idx %d\n",
444 __func__, env->active_tc.PC, address, rw, mmu_idx);
446 /* data access */
447 #if !defined(CONFIG_USER_ONLY)
448 /* XXX: put correct access by using cpu_restore_state()
449 correctly */
450 access_type = ACCESS_INT;
451 ret = get_physical_address(env, &physical, &prot,
452 address, rw, access_type);
453 switch (ret) {
454 case TLBRET_MATCH:
455 qemu_log_mask(CPU_LOG_MMU,
456 "%s address=%" VADDR_PRIx " physical " TARGET_FMT_plx
457 " prot %d\n", __func__, address, physical, prot);
458 break;
459 default:
460 qemu_log_mask(CPU_LOG_MMU,
461 "%s address=%" VADDR_PRIx " ret %d\n", __func__, address,
462 ret);
463 break;
465 if (ret == TLBRET_MATCH) {
466 tlb_set_page(cs, address & TARGET_PAGE_MASK,
467 physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
468 mmu_idx, TARGET_PAGE_SIZE);
469 ret = 0;
470 } else if (ret < 0)
471 #endif
473 raise_mmu_exception(env, address, rw, ret);
474 ret = 1;
477 return ret;
480 #if !defined(CONFIG_USER_ONLY)
481 hwaddr cpu_mips_translate_address(CPUMIPSState *env, target_ulong address, int rw)
483 hwaddr physical;
484 int prot;
485 int access_type;
486 int ret = 0;
488 /* data access */
489 access_type = ACCESS_INT;
490 ret = get_physical_address(env, &physical, &prot,
491 address, rw, access_type);
492 if (ret != TLBRET_MATCH) {
493 raise_mmu_exception(env, address, rw, ret);
494 return -1LL;
495 } else {
496 return physical;
500 static const char * const excp_names[EXCP_LAST + 1] = {
501 [EXCP_RESET] = "reset",
502 [EXCP_SRESET] = "soft reset",
503 [EXCP_DSS] = "debug single step",
504 [EXCP_DINT] = "debug interrupt",
505 [EXCP_NMI] = "non-maskable interrupt",
506 [EXCP_MCHECK] = "machine check",
507 [EXCP_EXT_INTERRUPT] = "interrupt",
508 [EXCP_DFWATCH] = "deferred watchpoint",
509 [EXCP_DIB] = "debug instruction breakpoint",
510 [EXCP_IWATCH] = "instruction fetch watchpoint",
511 [EXCP_AdEL] = "address error load",
512 [EXCP_AdES] = "address error store",
513 [EXCP_TLBF] = "TLB refill",
514 [EXCP_IBE] = "instruction bus error",
515 [EXCP_DBp] = "debug breakpoint",
516 [EXCP_SYSCALL] = "syscall",
517 [EXCP_BREAK] = "break",
518 [EXCP_CpU] = "coprocessor unusable",
519 [EXCP_RI] = "reserved instruction",
520 [EXCP_OVERFLOW] = "arithmetic overflow",
521 [EXCP_TRAP] = "trap",
522 [EXCP_FPE] = "floating point",
523 [EXCP_DDBS] = "debug data break store",
524 [EXCP_DWATCH] = "data watchpoint",
525 [EXCP_LTLBL] = "TLB modify",
526 [EXCP_TLBL] = "TLB load",
527 [EXCP_TLBS] = "TLB store",
528 [EXCP_DBE] = "data bus error",
529 [EXCP_DDBL] = "debug data break load",
530 [EXCP_THREAD] = "thread",
531 [EXCP_MDMX] = "MDMX",
532 [EXCP_C2E] = "precise coprocessor 2",
533 [EXCP_CACHE] = "cache error",
534 [EXCP_TLBXI] = "TLB execute-inhibit",
535 [EXCP_TLBRI] = "TLB read-inhibit",
536 [EXCP_MSADIS] = "MSA disabled",
537 [EXCP_MSAFPE] = "MSA floating point",
539 #endif
541 target_ulong exception_resume_pc (CPUMIPSState *env)
543 target_ulong bad_pc;
544 target_ulong isa_mode;
546 isa_mode = !!(env->hflags & MIPS_HFLAG_M16);
547 bad_pc = env->active_tc.PC | isa_mode;
548 if (env->hflags & MIPS_HFLAG_BMASK) {
549 /* If the exception was raised from a delay slot, come back to
550 the jump. */
551 bad_pc -= (env->hflags & MIPS_HFLAG_B16 ? 2 : 4);
554 return bad_pc;
557 #if !defined(CONFIG_USER_ONLY)
558 static void set_hflags_for_handler (CPUMIPSState *env)
560 /* Exception handlers are entered in 32-bit mode. */
561 env->hflags &= ~(MIPS_HFLAG_M16);
562 /* ...except that microMIPS lets you choose. */
563 if (env->insn_flags & ASE_MICROMIPS) {
564 env->hflags |= (!!(env->CP0_Config3
565 & (1 << CP0C3_ISA_ON_EXC))
566 << MIPS_HFLAG_M16_SHIFT);
570 static inline void set_badinstr_registers(CPUMIPSState *env)
572 if (env->hflags & MIPS_HFLAG_M16) {
573 /* TODO: add BadInstr support for microMIPS */
574 return;
576 if (env->CP0_Config3 & (1 << CP0C3_BI)) {
577 env->CP0_BadInstr = cpu_ldl_code(env, env->active_tc.PC);
579 if ((env->CP0_Config3 & (1 << CP0C3_BP)) &&
580 (env->hflags & MIPS_HFLAG_BMASK)) {
581 env->CP0_BadInstrP = cpu_ldl_code(env, env->active_tc.PC - 4);
584 #endif
586 void mips_cpu_do_interrupt(CPUState *cs)
588 #if !defined(CONFIG_USER_ONLY)
589 MIPSCPU *cpu = MIPS_CPU(cs);
590 CPUMIPSState *env = &cpu->env;
591 bool update_badinstr = 0;
592 target_ulong offset;
593 int cause = -1;
594 const char *name;
596 if (qemu_loglevel_mask(CPU_LOG_INT)
597 && cs->exception_index != EXCP_EXT_INTERRUPT) {
598 if (cs->exception_index < 0 || cs->exception_index > EXCP_LAST) {
599 name = "unknown";
600 } else {
601 name = excp_names[cs->exception_index];
604 qemu_log("%s enter: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx
605 " %s exception\n",
606 __func__, env->active_tc.PC, env->CP0_EPC, name);
608 if (cs->exception_index == EXCP_EXT_INTERRUPT &&
609 (env->hflags & MIPS_HFLAG_DM)) {
610 cs->exception_index = EXCP_DINT;
612 offset = 0x180;
613 switch (cs->exception_index) {
614 case EXCP_DSS:
615 env->CP0_Debug |= 1 << CP0DB_DSS;
616 /* Debug single step cannot be raised inside a delay slot and
617 resume will always occur on the next instruction
618 (but we assume the pc has always been updated during
619 code translation). */
620 env->CP0_DEPC = env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16);
621 goto enter_debug_mode;
622 case EXCP_DINT:
623 env->CP0_Debug |= 1 << CP0DB_DINT;
624 goto set_DEPC;
625 case EXCP_DIB:
626 env->CP0_Debug |= 1 << CP0DB_DIB;
627 goto set_DEPC;
628 case EXCP_DBp:
629 env->CP0_Debug |= 1 << CP0DB_DBp;
630 goto set_DEPC;
631 case EXCP_DDBS:
632 env->CP0_Debug |= 1 << CP0DB_DDBS;
633 goto set_DEPC;
634 case EXCP_DDBL:
635 env->CP0_Debug |= 1 << CP0DB_DDBL;
636 set_DEPC:
637 env->CP0_DEPC = exception_resume_pc(env);
638 env->hflags &= ~MIPS_HFLAG_BMASK;
639 enter_debug_mode:
640 if (env->insn_flags & ISA_MIPS3) {
641 env->hflags |= MIPS_HFLAG_64;
642 if (!(env->insn_flags & ISA_MIPS64R6) ||
643 env->CP0_Status & (1 << CP0St_KX)) {
644 env->hflags &= ~MIPS_HFLAG_AWRAP;
647 env->hflags |= MIPS_HFLAG_DM | MIPS_HFLAG_CP0;
648 env->hflags &= ~(MIPS_HFLAG_KSU);
649 /* EJTAG probe trap enable is not implemented... */
650 if (!(env->CP0_Status & (1 << CP0St_EXL)))
651 env->CP0_Cause &= ~(1U << CP0Ca_BD);
652 env->active_tc.PC = env->exception_base + 0x480;
653 set_hflags_for_handler(env);
654 break;
655 case EXCP_RESET:
656 cpu_reset(CPU(cpu));
657 break;
658 case EXCP_SRESET:
659 env->CP0_Status |= (1 << CP0St_SR);
660 memset(env->CP0_WatchLo, 0, sizeof(env->CP0_WatchLo));
661 goto set_error_EPC;
662 case EXCP_NMI:
663 env->CP0_Status |= (1 << CP0St_NMI);
664 set_error_EPC:
665 env->CP0_ErrorEPC = exception_resume_pc(env);
666 env->hflags &= ~MIPS_HFLAG_BMASK;
667 env->CP0_Status |= (1 << CP0St_ERL) | (1 << CP0St_BEV);
668 if (env->insn_flags & ISA_MIPS3) {
669 env->hflags |= MIPS_HFLAG_64;
670 if (!(env->insn_flags & ISA_MIPS64R6) ||
671 env->CP0_Status & (1 << CP0St_KX)) {
672 env->hflags &= ~MIPS_HFLAG_AWRAP;
675 env->hflags |= MIPS_HFLAG_CP0;
676 env->hflags &= ~(MIPS_HFLAG_KSU);
677 if (!(env->CP0_Status & (1 << CP0St_EXL)))
678 env->CP0_Cause &= ~(1U << CP0Ca_BD);
679 env->active_tc.PC = env->exception_base;
680 set_hflags_for_handler(env);
681 break;
682 case EXCP_EXT_INTERRUPT:
683 cause = 0;
684 if (env->CP0_Cause & (1 << CP0Ca_IV)) {
685 uint32_t spacing = (env->CP0_IntCtl >> CP0IntCtl_VS) & 0x1f;
687 if ((env->CP0_Status & (1 << CP0St_BEV)) || spacing == 0) {
688 offset = 0x200;
689 } else {
690 uint32_t vector = 0;
691 uint32_t pending = (env->CP0_Cause & CP0Ca_IP_mask) >> CP0Ca_IP;
693 if (env->CP0_Config3 & (1 << CP0C3_VEIC)) {
694 /* For VEIC mode, the external interrupt controller feeds
695 * the vector through the CP0Cause IP lines. */
696 vector = pending;
697 } else {
698 /* Vectored Interrupts
699 * Mask with Status.IM7-IM0 to get enabled interrupts. */
700 pending &= (env->CP0_Status >> CP0St_IM) & 0xff;
701 /* Find the highest-priority interrupt. */
702 while (pending >>= 1) {
703 vector++;
706 offset = 0x200 + (vector * (spacing << 5));
709 goto set_EPC;
710 case EXCP_LTLBL:
711 cause = 1;
712 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
713 goto set_EPC;
714 case EXCP_TLBL:
715 cause = 2;
716 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
717 if ((env->error_code & EXCP_TLB_NOMATCH) &&
718 !(env->CP0_Status & (1 << CP0St_EXL))) {
719 #if defined(TARGET_MIPS64)
720 int R = env->CP0_BadVAddr >> 62;
721 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
722 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
723 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
725 if (((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) &&
726 (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F))))
727 offset = 0x080;
728 else
729 #endif
730 offset = 0x000;
732 goto set_EPC;
733 case EXCP_TLBS:
734 cause = 3;
735 update_badinstr = 1;
736 if ((env->error_code & EXCP_TLB_NOMATCH) &&
737 !(env->CP0_Status & (1 << CP0St_EXL))) {
738 #if defined(TARGET_MIPS64)
739 int R = env->CP0_BadVAddr >> 62;
740 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
741 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
742 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
744 if (((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) &&
745 (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F))))
746 offset = 0x080;
747 else
748 #endif
749 offset = 0x000;
751 goto set_EPC;
752 case EXCP_AdEL:
753 cause = 4;
754 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
755 goto set_EPC;
756 case EXCP_AdES:
757 cause = 5;
758 update_badinstr = 1;
759 goto set_EPC;
760 case EXCP_IBE:
761 cause = 6;
762 goto set_EPC;
763 case EXCP_DBE:
764 cause = 7;
765 goto set_EPC;
766 case EXCP_SYSCALL:
767 cause = 8;
768 update_badinstr = 1;
769 goto set_EPC;
770 case EXCP_BREAK:
771 cause = 9;
772 update_badinstr = 1;
773 goto set_EPC;
774 case EXCP_RI:
775 cause = 10;
776 update_badinstr = 1;
777 goto set_EPC;
778 case EXCP_CpU:
779 cause = 11;
780 update_badinstr = 1;
781 env->CP0_Cause = (env->CP0_Cause & ~(0x3 << CP0Ca_CE)) |
782 (env->error_code << CP0Ca_CE);
783 goto set_EPC;
784 case EXCP_OVERFLOW:
785 cause = 12;
786 update_badinstr = 1;
787 goto set_EPC;
788 case EXCP_TRAP:
789 cause = 13;
790 update_badinstr = 1;
791 goto set_EPC;
792 case EXCP_MSAFPE:
793 cause = 14;
794 update_badinstr = 1;
795 goto set_EPC;
796 case EXCP_FPE:
797 cause = 15;
798 update_badinstr = 1;
799 goto set_EPC;
800 case EXCP_C2E:
801 cause = 18;
802 goto set_EPC;
803 case EXCP_TLBRI:
804 cause = 19;
805 update_badinstr = 1;
806 goto set_EPC;
807 case EXCP_TLBXI:
808 cause = 20;
809 goto set_EPC;
810 case EXCP_MSADIS:
811 cause = 21;
812 update_badinstr = 1;
813 goto set_EPC;
814 case EXCP_MDMX:
815 cause = 22;
816 goto set_EPC;
817 case EXCP_DWATCH:
818 cause = 23;
819 /* XXX: TODO: manage deferred watch exceptions */
820 goto set_EPC;
821 case EXCP_MCHECK:
822 cause = 24;
823 goto set_EPC;
824 case EXCP_THREAD:
825 cause = 25;
826 goto set_EPC;
827 case EXCP_DSPDIS:
828 cause = 26;
829 goto set_EPC;
830 case EXCP_CACHE:
831 cause = 30;
832 if (env->CP0_Status & (1 << CP0St_BEV)) {
833 offset = 0x100;
834 } else {
835 offset = 0x20000100;
837 set_EPC:
838 if (!(env->CP0_Status & (1 << CP0St_EXL))) {
839 env->CP0_EPC = exception_resume_pc(env);
840 if (update_badinstr) {
841 set_badinstr_registers(env);
843 if (env->hflags & MIPS_HFLAG_BMASK) {
844 env->CP0_Cause |= (1U << CP0Ca_BD);
845 } else {
846 env->CP0_Cause &= ~(1U << CP0Ca_BD);
848 env->CP0_Status |= (1 << CP0St_EXL);
849 if (env->insn_flags & ISA_MIPS3) {
850 env->hflags |= MIPS_HFLAG_64;
851 if (!(env->insn_flags & ISA_MIPS64R6) ||
852 env->CP0_Status & (1 << CP0St_KX)) {
853 env->hflags &= ~MIPS_HFLAG_AWRAP;
856 env->hflags |= MIPS_HFLAG_CP0;
857 env->hflags &= ~(MIPS_HFLAG_KSU);
859 env->hflags &= ~MIPS_HFLAG_BMASK;
860 if (env->CP0_Status & (1 << CP0St_BEV)) {
861 env->active_tc.PC = env->exception_base + 0x200;
862 } else {
863 env->active_tc.PC = (int32_t)(env->CP0_EBase & ~0x3ff);
865 env->active_tc.PC += offset;
866 set_hflags_for_handler(env);
867 env->CP0_Cause = (env->CP0_Cause & ~(0x1f << CP0Ca_EC)) | (cause << CP0Ca_EC);
868 break;
869 default:
870 abort();
872 if (qemu_loglevel_mask(CPU_LOG_INT)
873 && cs->exception_index != EXCP_EXT_INTERRUPT) {
874 qemu_log("%s: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " cause %d\n"
875 " S %08x C %08x A " TARGET_FMT_lx " D " TARGET_FMT_lx "\n",
876 __func__, env->active_tc.PC, env->CP0_EPC, cause,
877 env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr,
878 env->CP0_DEPC);
880 #endif
881 cs->exception_index = EXCP_NONE;
884 bool mips_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
886 if (interrupt_request & CPU_INTERRUPT_HARD) {
887 MIPSCPU *cpu = MIPS_CPU(cs);
888 CPUMIPSState *env = &cpu->env;
890 if (cpu_mips_hw_interrupts_enabled(env) &&
891 cpu_mips_hw_interrupts_pending(env)) {
892 /* Raise it */
893 cs->exception_index = EXCP_EXT_INTERRUPT;
894 env->error_code = 0;
895 mips_cpu_do_interrupt(cs);
896 return true;
899 return false;
902 #if !defined(CONFIG_USER_ONLY)
903 void r4k_invalidate_tlb (CPUMIPSState *env, int idx, int use_extra)
905 MIPSCPU *cpu = mips_env_get_cpu(env);
906 CPUState *cs;
907 r4k_tlb_t *tlb;
908 target_ulong addr;
909 target_ulong end;
910 uint16_t ASID = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask;
911 target_ulong mask;
913 tlb = &env->tlb->mmu.r4k.tlb[idx];
914 /* The qemu TLB is flushed when the ASID changes, so no need to
915 flush these entries again. */
916 if (tlb->G == 0 && tlb->ASID != ASID) {
917 return;
920 if (use_extra && env->tlb->tlb_in_use < MIPS_TLB_MAX) {
921 /* For tlbwr, we can shadow the discarded entry into
922 a new (fake) TLB entry, as long as the guest can not
923 tell that it's there. */
924 env->tlb->mmu.r4k.tlb[env->tlb->tlb_in_use] = *tlb;
925 env->tlb->tlb_in_use++;
926 return;
929 /* 1k pages are not supported. */
930 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
931 if (tlb->V0) {
932 cs = CPU(cpu);
933 addr = tlb->VPN & ~mask;
934 #if defined(TARGET_MIPS64)
935 if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) {
936 addr |= 0x3FFFFF0000000000ULL;
938 #endif
939 end = addr | (mask >> 1);
940 while (addr < end) {
941 tlb_flush_page(cs, addr);
942 addr += TARGET_PAGE_SIZE;
945 if (tlb->V1) {
946 cs = CPU(cpu);
947 addr = (tlb->VPN & ~mask) | ((mask >> 1) + 1);
948 #if defined(TARGET_MIPS64)
949 if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) {
950 addr |= 0x3FFFFF0000000000ULL;
952 #endif
953 end = addr | mask;
954 while (addr - 1 < end) {
955 tlb_flush_page(cs, addr);
956 addr += TARGET_PAGE_SIZE;
960 #endif
962 void QEMU_NORETURN do_raise_exception_err(CPUMIPSState *env,
963 uint32_t exception,
964 int error_code,
965 uintptr_t pc)
967 CPUState *cs = CPU(mips_env_get_cpu(env));
969 if (exception < EXCP_SC) {
970 qemu_log_mask(CPU_LOG_INT, "%s: %d %d\n",
971 __func__, exception, error_code);
973 cs->exception_index = exception;
974 env->error_code = error_code;
976 cpu_loop_exit_restore(cs, pc);