ACPI: Add definitions for the SPCR table
[qemu/ar7.git] / target-mips / helper.c
blob8e3204a3a0bbaea6c8e607a17b61f352d7c25a11
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 <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <inttypes.h>
24 #include <signal.h>
26 #include "cpu.h"
27 #include "sysemu/kvm.h"
28 #include "exec/cpu_ldst.h"
30 enum {
31 TLBRET_XI = -6,
32 TLBRET_RI = -5,
33 TLBRET_DIRTY = -4,
34 TLBRET_INVALID = -3,
35 TLBRET_NOMATCH = -2,
36 TLBRET_BADADDR = -1,
37 TLBRET_MATCH = 0
40 #if !defined(CONFIG_USER_ONLY)
42 /* no MMU emulation */
43 int no_mmu_map_address (CPUMIPSState *env, hwaddr *physical, int *prot,
44 target_ulong address, int rw, int access_type)
46 *physical = address;
47 *prot = PAGE_READ | PAGE_WRITE;
48 return TLBRET_MATCH;
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;
58 else
59 *physical = address;
60 } else if (address <= (int32_t)0xBFFFFFFFUL)
61 *physical = address & 0x1FFFFFFF;
62 else
63 *physical = address;
65 *prot = PAGE_READ | PAGE_WRITE;
66 return TLBRET_MATCH;
69 /* MIPS32/MIPS64 R4000-style MMU emulation */
70 int r4k_map_address (CPUMIPSState *env, hwaddr *physical, int *prot,
71 target_ulong address, int rw, int access_type)
73 uint8_t ASID = env->CP0_EntryHi & 0xFF;
74 int i;
76 for (i = 0; i < env->tlb->tlb_in_use; i++) {
77 r4k_tlb_t *tlb = &env->tlb->mmu.r4k.tlb[i];
78 /* 1k pages are not supported. */
79 target_ulong mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
80 target_ulong tag = address & ~mask;
81 target_ulong VPN = tlb->VPN & ~mask;
82 #if defined(TARGET_MIPS64)
83 tag &= env->SEGMask;
84 #endif
86 /* Check ASID, virtual page number & size */
87 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag && !tlb->EHINV) {
88 /* TLB match */
89 int n = !!(address & mask & ~(mask >> 1));
90 /* Check access rights */
91 if (!(n ? tlb->V1 : tlb->V0)) {
92 return TLBRET_INVALID;
94 if (rw == MMU_INST_FETCH && (n ? tlb->XI1 : tlb->XI0)) {
95 return TLBRET_XI;
97 if (rw == MMU_DATA_LOAD && (n ? tlb->RI1 : tlb->RI0)) {
98 return TLBRET_RI;
100 if (rw != MMU_DATA_STORE || (n ? tlb->D1 : tlb->D0)) {
101 *physical = tlb->PFN[n] | (address & (mask >> 1));
102 *prot = PAGE_READ;
103 if (n ? tlb->D1 : tlb->D0)
104 *prot |= PAGE_WRITE;
105 return TLBRET_MATCH;
107 return TLBRET_DIRTY;
110 return TLBRET_NOMATCH;
113 static int get_physical_address (CPUMIPSState *env, hwaddr *physical,
114 int *prot, target_ulong real_address,
115 int rw, int access_type)
117 /* User mode can only access useg/xuseg */
118 int user_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM;
119 int supervisor_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_SM;
120 int kernel_mode = !user_mode && !supervisor_mode;
121 #if defined(TARGET_MIPS64)
122 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
123 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
124 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
125 #endif
126 int ret = TLBRET_MATCH;
127 /* effective address (modified for KVM T&E kernel segments) */
128 target_ulong address = real_address;
130 #if 0
131 qemu_log("user mode %d h %08x\n", user_mode, env->hflags);
132 #endif
134 #define USEG_LIMIT 0x7FFFFFFFUL
135 #define KSEG0_BASE 0x80000000UL
136 #define KSEG1_BASE 0xA0000000UL
137 #define KSEG2_BASE 0xC0000000UL
138 #define KSEG3_BASE 0xE0000000UL
140 #define KVM_KSEG0_BASE 0x40000000UL
141 #define KVM_KSEG2_BASE 0x60000000UL
143 if (kvm_enabled()) {
144 /* KVM T&E adds guest kernel segments in useg */
145 if (real_address >= KVM_KSEG0_BASE) {
146 if (real_address < KVM_KSEG2_BASE) {
147 /* kseg0 */
148 address += KSEG0_BASE - KVM_KSEG0_BASE;
149 } else if (real_address <= USEG_LIMIT) {
150 /* kseg2/3 */
151 address += KSEG2_BASE - KVM_KSEG2_BASE;
156 if (address <= USEG_LIMIT) {
157 /* useg */
158 if (env->CP0_Status & (1 << CP0St_ERL)) {
159 *physical = address & 0xFFFFFFFF;
160 *prot = PAGE_READ | PAGE_WRITE;
161 } else {
162 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
164 #if defined(TARGET_MIPS64)
165 } else if (address < 0x4000000000000000ULL) {
166 /* xuseg */
167 if (UX && address <= (0x3FFFFFFFFFFFFFFFULL & env->SEGMask)) {
168 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
169 } else {
170 ret = TLBRET_BADADDR;
172 } else if (address < 0x8000000000000000ULL) {
173 /* xsseg */
174 if ((supervisor_mode || kernel_mode) &&
175 SX && address <= (0x7FFFFFFFFFFFFFFFULL & env->SEGMask)) {
176 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
177 } else {
178 ret = TLBRET_BADADDR;
180 } else if (address < 0xC000000000000000ULL) {
181 /* xkphys */
182 if (kernel_mode && KX &&
183 (address & 0x07FFFFFFFFFFFFFFULL) <= env->PAMask) {
184 *physical = address & env->PAMask;
185 *prot = PAGE_READ | PAGE_WRITE;
186 } else {
187 ret = TLBRET_BADADDR;
189 } else if (address < 0xFFFFFFFF80000000ULL) {
190 /* xkseg */
191 if (kernel_mode && KX &&
192 address <= (0xFFFFFFFF7FFFFFFFULL & env->SEGMask)) {
193 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
194 } else {
195 ret = TLBRET_BADADDR;
197 #endif
198 } else if (address < (int32_t)KSEG1_BASE) {
199 /* kseg0 */
200 if (kernel_mode) {
201 *physical = address - (int32_t)KSEG0_BASE;
202 *prot = PAGE_READ | PAGE_WRITE;
203 } else {
204 ret = TLBRET_BADADDR;
206 } else if (address < (int32_t)KSEG2_BASE) {
207 /* kseg1 */
208 if (kernel_mode) {
209 *physical = address - (int32_t)KSEG1_BASE;
210 *prot = PAGE_READ | PAGE_WRITE;
211 } else {
212 ret = TLBRET_BADADDR;
214 } else if (address < (int32_t)KSEG3_BASE) {
215 /* sseg (kseg2) */
216 if (supervisor_mode || kernel_mode) {
217 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
218 } else {
219 ret = TLBRET_BADADDR;
221 } else {
222 /* kseg3 */
223 /* XXX: debug segment is not emulated */
224 if (kernel_mode) {
225 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
226 } else {
227 ret = TLBRET_BADADDR;
230 #if 0
231 qemu_log(TARGET_FMT_lx " %d %d => %" HWADDR_PRIx " %d (%d)\n",
232 address, rw, access_type, *physical, *prot, ret);
233 #endif
235 return ret;
237 #endif
239 static void raise_mmu_exception(CPUMIPSState *env, target_ulong address,
240 int rw, int tlb_error)
242 CPUState *cs = CPU(mips_env_get_cpu(env));
243 int exception = 0, error_code = 0;
245 if (rw == MMU_INST_FETCH) {
246 error_code |= EXCP_INST_NOTAVAIL;
249 switch (tlb_error) {
250 default:
251 case TLBRET_BADADDR:
252 /* Reference to kernel address from user mode or supervisor mode */
253 /* Reference to supervisor address from user mode */
254 if (rw == MMU_DATA_STORE) {
255 exception = EXCP_AdES;
256 } else {
257 exception = EXCP_AdEL;
259 break;
260 case TLBRET_NOMATCH:
261 /* No TLB match for a mapped address */
262 if (rw == MMU_DATA_STORE) {
263 exception = EXCP_TLBS;
264 } else {
265 exception = EXCP_TLBL;
267 error_code |= EXCP_TLB_NOMATCH;
268 break;
269 case TLBRET_INVALID:
270 /* TLB match with no valid bit */
271 if (rw == MMU_DATA_STORE) {
272 exception = EXCP_TLBS;
273 } else {
274 exception = EXCP_TLBL;
276 break;
277 case TLBRET_DIRTY:
278 /* TLB match but 'D' bit is cleared */
279 exception = EXCP_LTLBL;
280 break;
281 case TLBRET_XI:
282 /* Execute-Inhibit Exception */
283 if (env->CP0_PageGrain & (1 << CP0PG_IEC)) {
284 exception = EXCP_TLBXI;
285 } else {
286 exception = EXCP_TLBL;
288 break;
289 case TLBRET_RI:
290 /* Read-Inhibit Exception */
291 if (env->CP0_PageGrain & (1 << CP0PG_IEC)) {
292 exception = EXCP_TLBRI;
293 } else {
294 exception = EXCP_TLBL;
296 break;
298 /* Raise exception */
299 env->CP0_BadVAddr = address;
300 env->CP0_Context = (env->CP0_Context & ~0x007fffff) |
301 ((address >> 9) & 0x007ffff0);
302 env->CP0_EntryHi =
303 (env->CP0_EntryHi & 0xFF) | (address & (TARGET_PAGE_MASK << 1));
304 #if defined(TARGET_MIPS64)
305 env->CP0_EntryHi &= env->SEGMask;
306 env->CP0_XContext = (env->CP0_XContext & ((~0ULL) << (env->SEGBITS - 7))) |
307 ((address & 0xC00000000000ULL) >> (55 - env->SEGBITS)) |
308 ((address & ((1ULL << env->SEGBITS) - 1) & 0xFFFFFFFFFFFFE000ULL) >> 9);
309 #endif
310 cs->exception_index = exception;
311 env->error_code = error_code;
314 #if !defined(CONFIG_USER_ONLY)
315 hwaddr mips_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
317 MIPSCPU *cpu = MIPS_CPU(cs);
318 hwaddr phys_addr;
319 int prot;
321 if (get_physical_address(&cpu->env, &phys_addr, &prot, addr, 0,
322 ACCESS_INT) != 0) {
323 return -1;
325 return phys_addr;
327 #endif
329 int mips_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
330 int mmu_idx)
332 MIPSCPU *cpu = MIPS_CPU(cs);
333 CPUMIPSState *env = &cpu->env;
334 #if !defined(CONFIG_USER_ONLY)
335 hwaddr physical;
336 int prot;
337 int access_type;
338 #endif
339 int ret = 0;
341 #if 0
342 log_cpu_state(cs, 0);
343 #endif
344 qemu_log_mask(CPU_LOG_MMU,
345 "%s pc " TARGET_FMT_lx " ad %" VADDR_PRIx " rw %d mmu_idx %d\n",
346 __func__, env->active_tc.PC, address, rw, mmu_idx);
348 /* data access */
349 #if !defined(CONFIG_USER_ONLY)
350 /* XXX: put correct access by using cpu_restore_state()
351 correctly */
352 access_type = ACCESS_INT;
353 ret = get_physical_address(env, &physical, &prot,
354 address, rw, access_type);
355 qemu_log_mask(CPU_LOG_MMU,
356 "%s address=%" VADDR_PRIx " ret %d physical " TARGET_FMT_plx
357 " prot %d\n",
358 __func__, address, ret, physical, prot);
359 if (ret == TLBRET_MATCH) {
360 tlb_set_page(cs, address & TARGET_PAGE_MASK,
361 physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
362 mmu_idx, TARGET_PAGE_SIZE);
363 ret = 0;
364 } else if (ret < 0)
365 #endif
367 raise_mmu_exception(env, address, rw, ret);
368 ret = 1;
371 return ret;
374 #if !defined(CONFIG_USER_ONLY)
375 hwaddr cpu_mips_translate_address(CPUMIPSState *env, target_ulong address, int rw)
377 hwaddr physical;
378 int prot;
379 int access_type;
380 int ret = 0;
382 /* data access */
383 access_type = ACCESS_INT;
384 ret = get_physical_address(env, &physical, &prot,
385 address, rw, access_type);
386 if (ret != TLBRET_MATCH) {
387 raise_mmu_exception(env, address, rw, ret);
388 return -1LL;
389 } else {
390 return physical;
394 static const char * const excp_names[EXCP_LAST + 1] = {
395 [EXCP_RESET] = "reset",
396 [EXCP_SRESET] = "soft reset",
397 [EXCP_DSS] = "debug single step",
398 [EXCP_DINT] = "debug interrupt",
399 [EXCP_NMI] = "non-maskable interrupt",
400 [EXCP_MCHECK] = "machine check",
401 [EXCP_EXT_INTERRUPT] = "interrupt",
402 [EXCP_DFWATCH] = "deferred watchpoint",
403 [EXCP_DIB] = "debug instruction breakpoint",
404 [EXCP_IWATCH] = "instruction fetch watchpoint",
405 [EXCP_AdEL] = "address error load",
406 [EXCP_AdES] = "address error store",
407 [EXCP_TLBF] = "TLB refill",
408 [EXCP_IBE] = "instruction bus error",
409 [EXCP_DBp] = "debug breakpoint",
410 [EXCP_SYSCALL] = "syscall",
411 [EXCP_BREAK] = "break",
412 [EXCP_CpU] = "coprocessor unusable",
413 [EXCP_RI] = "reserved instruction",
414 [EXCP_OVERFLOW] = "arithmetic overflow",
415 [EXCP_TRAP] = "trap",
416 [EXCP_FPE] = "floating point",
417 [EXCP_DDBS] = "debug data break store",
418 [EXCP_DWATCH] = "data watchpoint",
419 [EXCP_LTLBL] = "TLB modify",
420 [EXCP_TLBL] = "TLB load",
421 [EXCP_TLBS] = "TLB store",
422 [EXCP_DBE] = "data bus error",
423 [EXCP_DDBL] = "debug data break load",
424 [EXCP_THREAD] = "thread",
425 [EXCP_MDMX] = "MDMX",
426 [EXCP_C2E] = "precise coprocessor 2",
427 [EXCP_CACHE] = "cache error",
428 [EXCP_TLBXI] = "TLB execute-inhibit",
429 [EXCP_TLBRI] = "TLB read-inhibit",
430 [EXCP_MSADIS] = "MSA disabled",
431 [EXCP_MSAFPE] = "MSA floating point",
433 #endif
435 target_ulong exception_resume_pc (CPUMIPSState *env)
437 target_ulong bad_pc;
438 target_ulong isa_mode;
440 isa_mode = !!(env->hflags & MIPS_HFLAG_M16);
441 bad_pc = env->active_tc.PC | isa_mode;
442 if (env->hflags & MIPS_HFLAG_BMASK) {
443 /* If the exception was raised from a delay slot, come back to
444 the jump. */
445 bad_pc -= (env->hflags & MIPS_HFLAG_B16 ? 2 : 4);
448 return bad_pc;
451 #if !defined(CONFIG_USER_ONLY)
452 static void set_hflags_for_handler (CPUMIPSState *env)
454 /* Exception handlers are entered in 32-bit mode. */
455 env->hflags &= ~(MIPS_HFLAG_M16);
456 /* ...except that microMIPS lets you choose. */
457 if (env->insn_flags & ASE_MICROMIPS) {
458 env->hflags |= (!!(env->CP0_Config3
459 & (1 << CP0C3_ISA_ON_EXC))
460 << MIPS_HFLAG_M16_SHIFT);
464 static inline void set_badinstr_registers(CPUMIPSState *env)
466 if (env->hflags & MIPS_HFLAG_M16) {
467 /* TODO: add BadInstr support for microMIPS */
468 return;
470 if (env->CP0_Config3 & (1 << CP0C3_BI)) {
471 env->CP0_BadInstr = cpu_ldl_code(env, env->active_tc.PC);
473 if ((env->CP0_Config3 & (1 << CP0C3_BP)) &&
474 (env->hflags & MIPS_HFLAG_BMASK)) {
475 env->CP0_BadInstrP = cpu_ldl_code(env, env->active_tc.PC - 4);
478 #endif
480 void mips_cpu_do_interrupt(CPUState *cs)
482 #if !defined(CONFIG_USER_ONLY)
483 MIPSCPU *cpu = MIPS_CPU(cs);
484 CPUMIPSState *env = &cpu->env;
485 bool update_badinstr = 0;
486 target_ulong offset;
487 int cause = -1;
488 const char *name;
490 if (qemu_log_enabled() && cs->exception_index != EXCP_EXT_INTERRUPT) {
491 if (cs->exception_index < 0 || cs->exception_index > EXCP_LAST) {
492 name = "unknown";
493 } else {
494 name = excp_names[cs->exception_index];
497 qemu_log("%s enter: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " %s exception\n",
498 __func__, env->active_tc.PC, env->CP0_EPC, name);
500 if (cs->exception_index == EXCP_EXT_INTERRUPT &&
501 (env->hflags & MIPS_HFLAG_DM)) {
502 cs->exception_index = EXCP_DINT;
504 offset = 0x180;
505 switch (cs->exception_index) {
506 case EXCP_DSS:
507 env->CP0_Debug |= 1 << CP0DB_DSS;
508 /* Debug single step cannot be raised inside a delay slot and
509 resume will always occur on the next instruction
510 (but we assume the pc has always been updated during
511 code translation). */
512 env->CP0_DEPC = env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16);
513 goto enter_debug_mode;
514 case EXCP_DINT:
515 env->CP0_Debug |= 1 << CP0DB_DINT;
516 goto set_DEPC;
517 case EXCP_DIB:
518 env->CP0_Debug |= 1 << CP0DB_DIB;
519 goto set_DEPC;
520 case EXCP_DBp:
521 env->CP0_Debug |= 1 << CP0DB_DBp;
522 goto set_DEPC;
523 case EXCP_DDBS:
524 env->CP0_Debug |= 1 << CP0DB_DDBS;
525 goto set_DEPC;
526 case EXCP_DDBL:
527 env->CP0_Debug |= 1 << CP0DB_DDBL;
528 set_DEPC:
529 env->CP0_DEPC = exception_resume_pc(env);
530 env->hflags &= ~MIPS_HFLAG_BMASK;
531 enter_debug_mode:
532 if (env->insn_flags & ISA_MIPS3) {
533 env->hflags |= MIPS_HFLAG_64;
535 env->hflags |= MIPS_HFLAG_DM | MIPS_HFLAG_CP0;
536 env->hflags &= ~(MIPS_HFLAG_KSU);
537 /* EJTAG probe trap enable is not implemented... */
538 if (!(env->CP0_Status & (1 << CP0St_EXL)))
539 env->CP0_Cause &= ~(1U << CP0Ca_BD);
540 env->active_tc.PC = (int32_t)0xBFC00480;
541 set_hflags_for_handler(env);
542 break;
543 case EXCP_RESET:
544 cpu_reset(CPU(cpu));
545 break;
546 case EXCP_SRESET:
547 env->CP0_Status |= (1 << CP0St_SR);
548 memset(env->CP0_WatchLo, 0, sizeof(*env->CP0_WatchLo));
549 goto set_error_EPC;
550 case EXCP_NMI:
551 env->CP0_Status |= (1 << CP0St_NMI);
552 set_error_EPC:
553 env->CP0_ErrorEPC = exception_resume_pc(env);
554 env->hflags &= ~MIPS_HFLAG_BMASK;
555 env->CP0_Status |= (1 << CP0St_ERL) | (1 << CP0St_BEV);
556 if (env->insn_flags & ISA_MIPS3) {
557 env->hflags |= MIPS_HFLAG_64;
559 env->hflags |= MIPS_HFLAG_CP0;
560 env->hflags &= ~(MIPS_HFLAG_KSU);
561 if (!(env->CP0_Status & (1 << CP0St_EXL)))
562 env->CP0_Cause &= ~(1U << CP0Ca_BD);
563 env->active_tc.PC = (int32_t)0xBFC00000;
564 set_hflags_for_handler(env);
565 break;
566 case EXCP_EXT_INTERRUPT:
567 cause = 0;
568 if (env->CP0_Cause & (1 << CP0Ca_IV))
569 offset = 0x200;
571 if (env->CP0_Config3 & ((1 << CP0C3_VInt) | (1 << CP0C3_VEIC))) {
572 /* Vectored Interrupts. */
573 unsigned int spacing;
574 unsigned int vector;
575 unsigned int pending = (env->CP0_Cause & CP0Ca_IP_mask) >> 8;
577 pending &= env->CP0_Status >> 8;
578 /* Compute the Vector Spacing. */
579 spacing = (env->CP0_IntCtl >> CP0IntCtl_VS) & ((1 << 6) - 1);
580 spacing <<= 5;
582 if (env->CP0_Config3 & (1 << CP0C3_VInt)) {
583 /* For VInt mode, the MIPS computes the vector internally. */
584 for (vector = 7; vector > 0; vector--) {
585 if (pending & (1 << vector)) {
586 /* Found it. */
587 break;
590 } else {
591 /* For VEIC mode, the external interrupt controller feeds the
592 vector through the CP0Cause IP lines. */
593 vector = pending;
595 offset = 0x200 + vector * spacing;
597 goto set_EPC;
598 case EXCP_LTLBL:
599 cause = 1;
600 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
601 goto set_EPC;
602 case EXCP_TLBL:
603 cause = 2;
604 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
605 if ((env->error_code & EXCP_TLB_NOMATCH) &&
606 !(env->CP0_Status & (1 << CP0St_EXL))) {
607 #if defined(TARGET_MIPS64)
608 int R = env->CP0_BadVAddr >> 62;
609 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
610 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
611 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
613 if (((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) &&
614 (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F))))
615 offset = 0x080;
616 else
617 #endif
618 offset = 0x000;
620 goto set_EPC;
621 case EXCP_TLBS:
622 cause = 3;
623 update_badinstr = 1;
624 if ((env->error_code & EXCP_TLB_NOMATCH) &&
625 !(env->CP0_Status & (1 << CP0St_EXL))) {
626 #if defined(TARGET_MIPS64)
627 int R = env->CP0_BadVAddr >> 62;
628 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
629 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
630 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
632 if (((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) &&
633 (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F))))
634 offset = 0x080;
635 else
636 #endif
637 offset = 0x000;
639 goto set_EPC;
640 case EXCP_AdEL:
641 cause = 4;
642 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
643 goto set_EPC;
644 case EXCP_AdES:
645 cause = 5;
646 update_badinstr = 1;
647 goto set_EPC;
648 case EXCP_IBE:
649 cause = 6;
650 goto set_EPC;
651 case EXCP_DBE:
652 cause = 7;
653 goto set_EPC;
654 case EXCP_SYSCALL:
655 cause = 8;
656 update_badinstr = 1;
657 goto set_EPC;
658 case EXCP_BREAK:
659 cause = 9;
660 update_badinstr = 1;
661 goto set_EPC;
662 case EXCP_RI:
663 cause = 10;
664 update_badinstr = 1;
665 goto set_EPC;
666 case EXCP_CpU:
667 cause = 11;
668 update_badinstr = 1;
669 env->CP0_Cause = (env->CP0_Cause & ~(0x3 << CP0Ca_CE)) |
670 (env->error_code << CP0Ca_CE);
671 goto set_EPC;
672 case EXCP_OVERFLOW:
673 cause = 12;
674 update_badinstr = 1;
675 goto set_EPC;
676 case EXCP_TRAP:
677 cause = 13;
678 update_badinstr = 1;
679 goto set_EPC;
680 case EXCP_MSAFPE:
681 cause = 14;
682 update_badinstr = 1;
683 goto set_EPC;
684 case EXCP_FPE:
685 cause = 15;
686 update_badinstr = 1;
687 goto set_EPC;
688 case EXCP_C2E:
689 cause = 18;
690 goto set_EPC;
691 case EXCP_TLBRI:
692 cause = 19;
693 update_badinstr = 1;
694 goto set_EPC;
695 case EXCP_TLBXI:
696 cause = 20;
697 goto set_EPC;
698 case EXCP_MSADIS:
699 cause = 21;
700 update_badinstr = 1;
701 goto set_EPC;
702 case EXCP_MDMX:
703 cause = 22;
704 goto set_EPC;
705 case EXCP_DWATCH:
706 cause = 23;
707 /* XXX: TODO: manage defered watch exceptions */
708 goto set_EPC;
709 case EXCP_MCHECK:
710 cause = 24;
711 goto set_EPC;
712 case EXCP_THREAD:
713 cause = 25;
714 goto set_EPC;
715 case EXCP_DSPDIS:
716 cause = 26;
717 goto set_EPC;
718 case EXCP_CACHE:
719 cause = 30;
720 if (env->CP0_Status & (1 << CP0St_BEV)) {
721 offset = 0x100;
722 } else {
723 offset = 0x20000100;
725 set_EPC:
726 if (!(env->CP0_Status & (1 << CP0St_EXL))) {
727 env->CP0_EPC = exception_resume_pc(env);
728 if (update_badinstr) {
729 set_badinstr_registers(env);
731 if (env->hflags & MIPS_HFLAG_BMASK) {
732 env->CP0_Cause |= (1U << CP0Ca_BD);
733 } else {
734 env->CP0_Cause &= ~(1U << CP0Ca_BD);
736 env->CP0_Status |= (1 << CP0St_EXL);
737 if (env->insn_flags & ISA_MIPS3) {
738 env->hflags |= MIPS_HFLAG_64;
740 env->hflags |= MIPS_HFLAG_CP0;
741 env->hflags &= ~(MIPS_HFLAG_KSU);
743 env->hflags &= ~MIPS_HFLAG_BMASK;
744 if (env->CP0_Status & (1 << CP0St_BEV)) {
745 env->active_tc.PC = (int32_t)0xBFC00200;
746 } else {
747 env->active_tc.PC = (int32_t)(env->CP0_EBase & ~0x3ff);
749 env->active_tc.PC += offset;
750 set_hflags_for_handler(env);
751 env->CP0_Cause = (env->CP0_Cause & ~(0x1f << CP0Ca_EC)) | (cause << CP0Ca_EC);
752 break;
753 default:
754 qemu_log("Invalid MIPS exception %d. Exiting\n", cs->exception_index);
755 printf("Invalid MIPS exception %d. Exiting\n", cs->exception_index);
756 exit(1);
758 if (qemu_log_enabled() && cs->exception_index != EXCP_EXT_INTERRUPT) {
759 qemu_log("%s: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " cause %d\n"
760 " S %08x C %08x A " TARGET_FMT_lx " D " TARGET_FMT_lx "\n",
761 __func__, env->active_tc.PC, env->CP0_EPC, cause,
762 env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr,
763 env->CP0_DEPC);
765 #endif
766 cs->exception_index = EXCP_NONE;
769 bool mips_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
771 if (interrupt_request & CPU_INTERRUPT_HARD) {
772 MIPSCPU *cpu = MIPS_CPU(cs);
773 CPUMIPSState *env = &cpu->env;
775 if (cpu_mips_hw_interrupts_pending(env)) {
776 /* Raise it */
777 cs->exception_index = EXCP_EXT_INTERRUPT;
778 env->error_code = 0;
779 mips_cpu_do_interrupt(cs);
780 return true;
783 return false;
786 #if !defined(CONFIG_USER_ONLY)
787 void r4k_invalidate_tlb (CPUMIPSState *env, int idx, int use_extra)
789 MIPSCPU *cpu = mips_env_get_cpu(env);
790 CPUState *cs;
791 r4k_tlb_t *tlb;
792 target_ulong addr;
793 target_ulong end;
794 uint8_t ASID = env->CP0_EntryHi & 0xFF;
795 target_ulong mask;
797 tlb = &env->tlb->mmu.r4k.tlb[idx];
798 /* The qemu TLB is flushed when the ASID changes, so no need to
799 flush these entries again. */
800 if (tlb->G == 0 && tlb->ASID != ASID) {
801 return;
804 if (use_extra && env->tlb->tlb_in_use < MIPS_TLB_MAX) {
805 /* For tlbwr, we can shadow the discarded entry into
806 a new (fake) TLB entry, as long as the guest can not
807 tell that it's there. */
808 env->tlb->mmu.r4k.tlb[env->tlb->tlb_in_use] = *tlb;
809 env->tlb->tlb_in_use++;
810 return;
813 /* 1k pages are not supported. */
814 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
815 if (tlb->V0) {
816 cs = CPU(cpu);
817 addr = tlb->VPN & ~mask;
818 #if defined(TARGET_MIPS64)
819 if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) {
820 addr |= 0x3FFFFF0000000000ULL;
822 #endif
823 end = addr | (mask >> 1);
824 while (addr < end) {
825 tlb_flush_page(cs, addr);
826 addr += TARGET_PAGE_SIZE;
829 if (tlb->V1) {
830 cs = CPU(cpu);
831 addr = (tlb->VPN & ~mask) | ((mask >> 1) + 1);
832 #if defined(TARGET_MIPS64)
833 if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) {
834 addr |= 0x3FFFFF0000000000ULL;
836 #endif
837 end = addr | mask;
838 while (addr - 1 < end) {
839 tlb_flush_page(cs, addr);
840 addr += TARGET_PAGE_SIZE;
844 #endif