]> git.ipfire.org Git - thirdparty/qemu.git/blame - target-mips/helper.c
log: do not unnecessarily include qom/cpu.h
[thirdparty/qemu.git] / target-mips / helper.c
CommitLineData
6af0bf9c
FB
1/*
2 * MIPS emulation helpers for qemu.
5fafdf24 3 *
6af0bf9c
FB
4 * Copyright (c) 2004-2005 Jocelyn Mayer
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
6af0bf9c 18 */
c684822a 19#include "qemu/osdep.h"
e37e863f
FB
20
21#include "cpu.h"
4ef37e69 22#include "sysemu/kvm.h"
aea14095 23#include "exec/cpu_ldst.h"
508127e2 24#include "exec/log.h"
6af0bf9c 25
43057ab1 26enum {
2fb58b73
LA
27 TLBRET_XI = -6,
28 TLBRET_RI = -5,
43057ab1
FB
29 TLBRET_DIRTY = -4,
30 TLBRET_INVALID = -3,
31 TLBRET_NOMATCH = -2,
32 TLBRET_BADADDR = -1,
33 TLBRET_MATCH = 0
34};
35
3c7b48b7
PB
36#if !defined(CONFIG_USER_ONLY)
37
29929e34 38/* no MMU emulation */
a8170e5e 39int no_mmu_map_address (CPUMIPSState *env, hwaddr *physical, int *prot,
6af0bf9c 40 target_ulong address, int rw, int access_type)
29929e34
TS
41{
42 *physical = address;
43 *prot = PAGE_READ | PAGE_WRITE;
44 return TLBRET_MATCH;
45}
46
47/* fixed mapping MMU emulation */
a8170e5e 48int fixed_mmu_map_address (CPUMIPSState *env, hwaddr *physical, int *prot,
29929e34
TS
49 target_ulong address, int rw, int access_type)
50{
51 if (address <= (int32_t)0x7FFFFFFFUL) {
52 if (!(env->CP0_Status & (1 << CP0St_ERL)))
53 *physical = address + 0x40000000UL;
54 else
55 *physical = address;
56 } else if (address <= (int32_t)0xBFFFFFFFUL)
57 *physical = address & 0x1FFFFFFF;
58 else
59 *physical = address;
60
61 *prot = PAGE_READ | PAGE_WRITE;
62 return TLBRET_MATCH;
63}
64
65/* MIPS32/MIPS64 R4000-style MMU emulation */
a8170e5e 66int r4k_map_address (CPUMIPSState *env, hwaddr *physical, int *prot,
29929e34 67 target_ulong address, int rw, int access_type)
6af0bf9c 68{
925fd0f2 69 uint8_t ASID = env->CP0_EntryHi & 0xFF;
3b1c8be4 70 int i;
6af0bf9c 71
ead9360e 72 for (i = 0; i < env->tlb->tlb_in_use; i++) {
c227f099 73 r4k_tlb_t *tlb = &env->tlb->mmu.r4k.tlb[i];
3b1c8be4 74 /* 1k pages are not supported. */
f2e9ebef 75 target_ulong mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
3b1c8be4 76 target_ulong tag = address & ~mask;
f2e9ebef 77 target_ulong VPN = tlb->VPN & ~mask;
d26bc211 78#if defined(TARGET_MIPS64)
e034e2c3 79 tag &= env->SEGMask;
100ce988 80#endif
3b1c8be4 81
6af0bf9c 82 /* Check ASID, virtual page number & size */
9456c2fb 83 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag && !tlb->EHINV) {
6af0bf9c 84 /* TLB match */
f2e9ebef 85 int n = !!(address & mask & ~(mask >> 1));
6af0bf9c 86 /* Check access rights */
2fb58b73 87 if (!(n ? tlb->V1 : tlb->V0)) {
43057ab1 88 return TLBRET_INVALID;
2fb58b73
LA
89 }
90 if (rw == MMU_INST_FETCH && (n ? tlb->XI1 : tlb->XI0)) {
91 return TLBRET_XI;
92 }
93 if (rw == MMU_DATA_LOAD && (n ? tlb->RI1 : tlb->RI0)) {
94 return TLBRET_RI;
95 }
9f6bcedb 96 if (rw != MMU_DATA_STORE || (n ? tlb->D1 : tlb->D0)) {
3b1c8be4 97 *physical = tlb->PFN[n] | (address & (mask >> 1));
9fb63ac2 98 *prot = PAGE_READ;
98c1b82b 99 if (n ? tlb->D1 : tlb->D0)
9fb63ac2 100 *prot |= PAGE_WRITE;
43057ab1 101 return TLBRET_MATCH;
6af0bf9c 102 }
43057ab1 103 return TLBRET_DIRTY;
6af0bf9c
FB
104 }
105 }
43057ab1 106 return TLBRET_NOMATCH;
6af0bf9c 107}
6af0bf9c 108
a8170e5e 109static int get_physical_address (CPUMIPSState *env, hwaddr *physical,
4ef37e69 110 int *prot, target_ulong real_address,
43057ab1 111 int rw, int access_type)
6af0bf9c 112{
b4ab4b4e 113 /* User mode can only access useg/xuseg */
43057ab1 114 int user_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM;
671880e6
TS
115 int supervisor_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_SM;
116 int kernel_mode = !user_mode && !supervisor_mode;
d26bc211 117#if defined(TARGET_MIPS64)
b4ab4b4e
TS
118 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
119 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
120 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
121#endif
43057ab1 122 int ret = TLBRET_MATCH;
4ef37e69
JH
123 /* effective address (modified for KVM T&E kernel segments) */
124 target_ulong address = real_address;
43057ab1 125
22010ce7
JH
126#define USEG_LIMIT 0x7FFFFFFFUL
127#define KSEG0_BASE 0x80000000UL
128#define KSEG1_BASE 0xA0000000UL
129#define KSEG2_BASE 0xC0000000UL
130#define KSEG3_BASE 0xE0000000UL
131
4ef37e69
JH
132#define KVM_KSEG0_BASE 0x40000000UL
133#define KVM_KSEG2_BASE 0x60000000UL
134
135 if (kvm_enabled()) {
136 /* KVM T&E adds guest kernel segments in useg */
137 if (real_address >= KVM_KSEG0_BASE) {
138 if (real_address < KVM_KSEG2_BASE) {
139 /* kseg0 */
140 address += KSEG0_BASE - KVM_KSEG0_BASE;
141 } else if (real_address <= USEG_LIMIT) {
142 /* kseg2/3 */
143 address += KSEG2_BASE - KVM_KSEG2_BASE;
144 }
145 }
146 }
147
22010ce7 148 if (address <= USEG_LIMIT) {
b4ab4b4e 149 /* useg */
996ba2cc 150 if (env->CP0_Status & (1 << CP0St_ERL)) {
29929e34 151 *physical = address & 0xFFFFFFFF;
6af0bf9c 152 *prot = PAGE_READ | PAGE_WRITE;
996ba2cc 153 } else {
4ef37e69 154 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
6af0bf9c 155 }
d26bc211 156#if defined(TARGET_MIPS64)
89fc88da 157 } else if (address < 0x4000000000000000ULL) {
b4ab4b4e 158 /* xuseg */
6958549d 159 if (UX && address <= (0x3FFFFFFFFFFFFFFFULL & env->SEGMask)) {
4ef37e69 160 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
6958549d
AJ
161 } else {
162 ret = TLBRET_BADADDR;
b4ab4b4e 163 }
89fc88da 164 } else if (address < 0x8000000000000000ULL) {
b4ab4b4e 165 /* xsseg */
6958549d
AJ
166 if ((supervisor_mode || kernel_mode) &&
167 SX && address <= (0x7FFFFFFFFFFFFFFFULL & env->SEGMask)) {
4ef37e69 168 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
6958549d
AJ
169 } else {
170 ret = TLBRET_BADADDR;
b4ab4b4e 171 }
89fc88da 172 } else if (address < 0xC000000000000000ULL) {
b4ab4b4e 173 /* xkphys */
671880e6 174 if (kernel_mode && KX &&
6d35524c
TS
175 (address & 0x07FFFFFFFFFFFFFFULL) <= env->PAMask) {
176 *physical = address & env->PAMask;
b4ab4b4e 177 *prot = PAGE_READ | PAGE_WRITE;
6958549d
AJ
178 } else {
179 ret = TLBRET_BADADDR;
180 }
89fc88da 181 } else if (address < 0xFFFFFFFF80000000ULL) {
b4ab4b4e 182 /* xkseg */
6958549d
AJ
183 if (kernel_mode && KX &&
184 address <= (0xFFFFFFFF7FFFFFFFULL & env->SEGMask)) {
4ef37e69 185 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
6958549d
AJ
186 } else {
187 ret = TLBRET_BADADDR;
188 }
b4ab4b4e 189#endif
22010ce7 190 } else if (address < (int32_t)KSEG1_BASE) {
6af0bf9c 191 /* kseg0 */
671880e6 192 if (kernel_mode) {
22010ce7 193 *physical = address - (int32_t)KSEG0_BASE;
671880e6
TS
194 *prot = PAGE_READ | PAGE_WRITE;
195 } else {
196 ret = TLBRET_BADADDR;
197 }
22010ce7 198 } else if (address < (int32_t)KSEG2_BASE) {
6af0bf9c 199 /* kseg1 */
671880e6 200 if (kernel_mode) {
22010ce7 201 *physical = address - (int32_t)KSEG1_BASE;
671880e6
TS
202 *prot = PAGE_READ | PAGE_WRITE;
203 } else {
204 ret = TLBRET_BADADDR;
205 }
22010ce7 206 } else if (address < (int32_t)KSEG3_BASE) {
89fc88da 207 /* sseg (kseg2) */
671880e6 208 if (supervisor_mode || kernel_mode) {
4ef37e69 209 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
671880e6
TS
210 } else {
211 ret = TLBRET_BADADDR;
212 }
6af0bf9c
FB
213 } else {
214 /* kseg3 */
6af0bf9c 215 /* XXX: debug segment is not emulated */
671880e6 216 if (kernel_mode) {
4ef37e69 217 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
671880e6
TS
218 } else {
219 ret = TLBRET_BADADDR;
220 }
6af0bf9c 221 }
6af0bf9c
FB
222 return ret;
223}
932e71cd 224#endif
6af0bf9c 225
7db13fae 226static void raise_mmu_exception(CPUMIPSState *env, target_ulong address,
1147e189
AJ
227 int rw, int tlb_error)
228{
27103424 229 CPUState *cs = CPU(mips_env_get_cpu(env));
1147e189
AJ
230 int exception = 0, error_code = 0;
231
aea14095
LA
232 if (rw == MMU_INST_FETCH) {
233 error_code |= EXCP_INST_NOTAVAIL;
234 }
235
1147e189
AJ
236 switch (tlb_error) {
237 default:
238 case TLBRET_BADADDR:
239 /* Reference to kernel address from user mode or supervisor mode */
240 /* Reference to supervisor address from user mode */
9f6bcedb 241 if (rw == MMU_DATA_STORE) {
1147e189 242 exception = EXCP_AdES;
9f6bcedb 243 } else {
1147e189 244 exception = EXCP_AdEL;
9f6bcedb 245 }
1147e189
AJ
246 break;
247 case TLBRET_NOMATCH:
248 /* No TLB match for a mapped address */
9f6bcedb 249 if (rw == MMU_DATA_STORE) {
1147e189 250 exception = EXCP_TLBS;
9f6bcedb 251 } else {
1147e189 252 exception = EXCP_TLBL;
9f6bcedb 253 }
aea14095 254 error_code |= EXCP_TLB_NOMATCH;
1147e189
AJ
255 break;
256 case TLBRET_INVALID:
257 /* TLB match with no valid bit */
9f6bcedb 258 if (rw == MMU_DATA_STORE) {
1147e189 259 exception = EXCP_TLBS;
9f6bcedb 260 } else {
1147e189 261 exception = EXCP_TLBL;
9f6bcedb 262 }
1147e189
AJ
263 break;
264 case TLBRET_DIRTY:
265 /* TLB match but 'D' bit is cleared */
266 exception = EXCP_LTLBL;
267 break;
92ceb440
LA
268 case TLBRET_XI:
269 /* Execute-Inhibit Exception */
270 if (env->CP0_PageGrain & (1 << CP0PG_IEC)) {
271 exception = EXCP_TLBXI;
272 } else {
273 exception = EXCP_TLBL;
274 }
275 break;
276 case TLBRET_RI:
277 /* Read-Inhibit Exception */
278 if (env->CP0_PageGrain & (1 << CP0PG_IEC)) {
279 exception = EXCP_TLBRI;
280 } else {
281 exception = EXCP_TLBL;
282 }
283 break;
1147e189
AJ
284 }
285 /* Raise exception */
286 env->CP0_BadVAddr = address;
287 env->CP0_Context = (env->CP0_Context & ~0x007fffff) |
288 ((address >> 9) & 0x007ffff0);
289 env->CP0_EntryHi =
290 (env->CP0_EntryHi & 0xFF) | (address & (TARGET_PAGE_MASK << 1));
291#if defined(TARGET_MIPS64)
292 env->CP0_EntryHi &= env->SEGMask;
60270f85
YK
293 env->CP0_XContext =
294 /* PTEBase */ (env->CP0_XContext & ((~0ULL) << (env->SEGBITS - 7))) |
295 /* R */ (extract64(address, 62, 2) << (env->SEGBITS - 9)) |
296 /* BadVPN2 */ (extract64(address, 13, env->SEGBITS - 13) << 4);
1147e189 297#endif
27103424 298 cs->exception_index = exception;
1147e189
AJ
299 env->error_code = error_code;
300}
301
4fcc562b 302#if !defined(CONFIG_USER_ONLY)
00b941e5 303hwaddr mips_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
6af0bf9c 304{
00b941e5 305 MIPSCPU *cpu = MIPS_CPU(cs);
a8170e5e 306 hwaddr phys_addr;
932e71cd 307 int prot;
6af0bf9c 308
00b941e5
AF
309 if (get_physical_address(&cpu->env, &phys_addr, &prot, addr, 0,
310 ACCESS_INT) != 0) {
932e71cd 311 return -1;
00b941e5 312 }
932e71cd 313 return phys_addr;
6af0bf9c 314}
4fcc562b 315#endif
6af0bf9c 316
7510454e
AF
317int mips_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
318 int mmu_idx)
6af0bf9c 319{
7510454e
AF
320 MIPSCPU *cpu = MIPS_CPU(cs);
321 CPUMIPSState *env = &cpu->env;
932e71cd 322#if !defined(CONFIG_USER_ONLY)
a8170e5e 323 hwaddr physical;
6af0bf9c 324 int prot;
6af0bf9c 325 int access_type;
99e43d36 326#endif
6af0bf9c
FB
327 int ret = 0;
328
4ad40f36 329#if 0
7510454e 330 log_cpu_state(cs, 0);
4ad40f36 331#endif
339aaf5b
AP
332 qemu_log_mask(CPU_LOG_MMU,
333 "%s pc " TARGET_FMT_lx " ad %" VADDR_PRIx " rw %d mmu_idx %d\n",
97b348e7 334 __func__, env->active_tc.PC, address, rw, mmu_idx);
4ad40f36 335
6af0bf9c 336 /* data access */
99e43d36 337#if !defined(CONFIG_USER_ONLY)
6af0bf9c
FB
338 /* XXX: put correct access by using cpu_restore_state()
339 correctly */
340 access_type = ACCESS_INT;
6af0bf9c
FB
341 ret = get_physical_address(env, &physical, &prot,
342 address, rw, access_type);
339aaf5b
AP
343 qemu_log_mask(CPU_LOG_MMU,
344 "%s address=%" VADDR_PRIx " ret %d physical " TARGET_FMT_plx
7510454e
AF
345 " prot %d\n",
346 __func__, address, ret, physical, prot);
43057ab1 347 if (ret == TLBRET_MATCH) {
0c591eb0 348 tlb_set_page(cs, address & TARGET_PAGE_MASK,
99e43d36
AJ
349 physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
350 mmu_idx, TARGET_PAGE_SIZE);
351 ret = 0;
932e71cd
AJ
352 } else if (ret < 0)
353#endif
354 {
1147e189 355 raise_mmu_exception(env, address, rw, ret);
6af0bf9c
FB
356 ret = 1;
357 }
358
359 return ret;
360}
361
25b91e32 362#if !defined(CONFIG_USER_ONLY)
a8170e5e 363hwaddr cpu_mips_translate_address(CPUMIPSState *env, target_ulong address, int rw)
25b91e32 364{
a8170e5e 365 hwaddr physical;
25b91e32
AJ
366 int prot;
367 int access_type;
368 int ret = 0;
369
25b91e32
AJ
370 /* data access */
371 access_type = ACCESS_INT;
372 ret = get_physical_address(env, &physical, &prot,
373 address, rw, access_type);
374 if (ret != TLBRET_MATCH) {
375 raise_mmu_exception(env, address, rw, ret);
c36bbb28
AJ
376 return -1LL;
377 } else {
378 return physical;
25b91e32 379 }
25b91e32 380}
25b91e32 381
9a5d878f
TS
382static const char * const excp_names[EXCP_LAST + 1] = {
383 [EXCP_RESET] = "reset",
384 [EXCP_SRESET] = "soft reset",
385 [EXCP_DSS] = "debug single step",
386 [EXCP_DINT] = "debug interrupt",
387 [EXCP_NMI] = "non-maskable interrupt",
388 [EXCP_MCHECK] = "machine check",
389 [EXCP_EXT_INTERRUPT] = "interrupt",
390 [EXCP_DFWATCH] = "deferred watchpoint",
391 [EXCP_DIB] = "debug instruction breakpoint",
392 [EXCP_IWATCH] = "instruction fetch watchpoint",
393 [EXCP_AdEL] = "address error load",
394 [EXCP_AdES] = "address error store",
395 [EXCP_TLBF] = "TLB refill",
396 [EXCP_IBE] = "instruction bus error",
397 [EXCP_DBp] = "debug breakpoint",
398 [EXCP_SYSCALL] = "syscall",
399 [EXCP_BREAK] = "break",
400 [EXCP_CpU] = "coprocessor unusable",
401 [EXCP_RI] = "reserved instruction",
402 [EXCP_OVERFLOW] = "arithmetic overflow",
403 [EXCP_TRAP] = "trap",
404 [EXCP_FPE] = "floating point",
405 [EXCP_DDBS] = "debug data break store",
406 [EXCP_DWATCH] = "data watchpoint",
407 [EXCP_LTLBL] = "TLB modify",
408 [EXCP_TLBL] = "TLB load",
409 [EXCP_TLBS] = "TLB store",
410 [EXCP_DBE] = "data bus error",
411 [EXCP_DDBL] = "debug data break load",
412 [EXCP_THREAD] = "thread",
413 [EXCP_MDMX] = "MDMX",
414 [EXCP_C2E] = "precise coprocessor 2",
415 [EXCP_CACHE] = "cache error",
92ceb440
LA
416 [EXCP_TLBXI] = "TLB execute-inhibit",
417 [EXCP_TLBRI] = "TLB read-inhibit",
b10ac204
YK
418 [EXCP_MSADIS] = "MSA disabled",
419 [EXCP_MSAFPE] = "MSA floating point",
14e51cc7 420};
d4fa5354 421#endif
14e51cc7 422
1239b472 423target_ulong exception_resume_pc (CPUMIPSState *env)
32188a03
NF
424{
425 target_ulong bad_pc;
426 target_ulong isa_mode;
427
428 isa_mode = !!(env->hflags & MIPS_HFLAG_M16);
429 bad_pc = env->active_tc.PC | isa_mode;
430 if (env->hflags & MIPS_HFLAG_BMASK) {
431 /* If the exception was raised from a delay slot, come back to
432 the jump. */
433 bad_pc -= (env->hflags & MIPS_HFLAG_B16 ? 2 : 4);
434 }
435
436 return bad_pc;
437}
bbfa8f72 438
1239b472 439#if !defined(CONFIG_USER_ONLY)
7db13fae 440static void set_hflags_for_handler (CPUMIPSState *env)
bbfa8f72
NF
441{
442 /* Exception handlers are entered in 32-bit mode. */
443 env->hflags &= ~(MIPS_HFLAG_M16);
444 /* ...except that microMIPS lets you choose. */
445 if (env->insn_flags & ASE_MICROMIPS) {
446 env->hflags |= (!!(env->CP0_Config3
447 & (1 << CP0C3_ISA_ON_EXC))
448 << MIPS_HFLAG_M16_SHIFT);
449 }
450}
aea14095
LA
451
452static inline void set_badinstr_registers(CPUMIPSState *env)
453{
454 if (env->hflags & MIPS_HFLAG_M16) {
455 /* TODO: add BadInstr support for microMIPS */
456 return;
457 }
458 if (env->CP0_Config3 & (1 << CP0C3_BI)) {
459 env->CP0_BadInstr = cpu_ldl_code(env, env->active_tc.PC);
460 }
461 if ((env->CP0_Config3 & (1 << CP0C3_BP)) &&
462 (env->hflags & MIPS_HFLAG_BMASK)) {
463 env->CP0_BadInstrP = cpu_ldl_code(env, env->active_tc.PC - 4);
464 }
465}
32188a03
NF
466#endif
467
97a8ea5a 468void mips_cpu_do_interrupt(CPUState *cs)
6af0bf9c 469{
27103424 470#if !defined(CONFIG_USER_ONLY)
97a8ea5a
AF
471 MIPSCPU *cpu = MIPS_CPU(cs);
472 CPUMIPSState *env = &cpu->env;
aea14095 473 bool update_badinstr = 0;
932e71cd
AJ
474 target_ulong offset;
475 int cause = -1;
476 const char *name;
100ce988 477
c8557016
RH
478 if (qemu_loglevel_mask(CPU_LOG_INT)
479 && cs->exception_index != EXCP_EXT_INTERRUPT) {
27103424 480 if (cs->exception_index < 0 || cs->exception_index > EXCP_LAST) {
932e71cd 481 name = "unknown";
27103424
AF
482 } else {
483 name = excp_names[cs->exception_index];
484 }
b67bfe8d 485
c8557016
RH
486 qemu_log("%s enter: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx
487 " %s exception\n",
93fcfe39 488 __func__, env->active_tc.PC, env->CP0_EPC, name);
932e71cd 489 }
27103424
AF
490 if (cs->exception_index == EXCP_EXT_INTERRUPT &&
491 (env->hflags & MIPS_HFLAG_DM)) {
492 cs->exception_index = EXCP_DINT;
493 }
932e71cd 494 offset = 0x180;
27103424 495 switch (cs->exception_index) {
932e71cd
AJ
496 case EXCP_DSS:
497 env->CP0_Debug |= 1 << CP0DB_DSS;
498 /* Debug single step cannot be raised inside a delay slot and
499 resume will always occur on the next instruction
500 (but we assume the pc has always been updated during
501 code translation). */
32188a03 502 env->CP0_DEPC = env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16);
932e71cd
AJ
503 goto enter_debug_mode;
504 case EXCP_DINT:
505 env->CP0_Debug |= 1 << CP0DB_DINT;
506 goto set_DEPC;
507 case EXCP_DIB:
508 env->CP0_Debug |= 1 << CP0DB_DIB;
509 goto set_DEPC;
510 case EXCP_DBp:
511 env->CP0_Debug |= 1 << CP0DB_DBp;
512 goto set_DEPC;
513 case EXCP_DDBS:
514 env->CP0_Debug |= 1 << CP0DB_DDBS;
515 goto set_DEPC;
516 case EXCP_DDBL:
517 env->CP0_Debug |= 1 << CP0DB_DDBL;
518 set_DEPC:
32188a03
NF
519 env->CP0_DEPC = exception_resume_pc(env);
520 env->hflags &= ~MIPS_HFLAG_BMASK;
0eaef5aa 521 enter_debug_mode:
d9224450
MR
522 if (env->insn_flags & ISA_MIPS3) {
523 env->hflags |= MIPS_HFLAG_64;
7871abb9
JH
524 if (!(env->insn_flags & ISA_MIPS64R6) ||
525 env->CP0_Status & (1 << CP0St_KX)) {
526 env->hflags &= ~MIPS_HFLAG_AWRAP;
527 }
d9224450
MR
528 }
529 env->hflags |= MIPS_HFLAG_DM | MIPS_HFLAG_CP0;
932e71cd
AJ
530 env->hflags &= ~(MIPS_HFLAG_KSU);
531 /* EJTAG probe trap enable is not implemented... */
532 if (!(env->CP0_Status & (1 << CP0St_EXL)))
f45cb2f4 533 env->CP0_Cause &= ~(1U << CP0Ca_BD);
932e71cd 534 env->active_tc.PC = (int32_t)0xBFC00480;
bbfa8f72 535 set_hflags_for_handler(env);
932e71cd
AJ
536 break;
537 case EXCP_RESET:
fca1be7c 538 cpu_reset(CPU(cpu));
932e71cd
AJ
539 break;
540 case EXCP_SRESET:
541 env->CP0_Status |= (1 << CP0St_SR);
542 memset(env->CP0_WatchLo, 0, sizeof(*env->CP0_WatchLo));
543 goto set_error_EPC;
544 case EXCP_NMI:
545 env->CP0_Status |= (1 << CP0St_NMI);
0eaef5aa 546 set_error_EPC:
32188a03
NF
547 env->CP0_ErrorEPC = exception_resume_pc(env);
548 env->hflags &= ~MIPS_HFLAG_BMASK;
932e71cd 549 env->CP0_Status |= (1 << CP0St_ERL) | (1 << CP0St_BEV);
d9224450
MR
550 if (env->insn_flags & ISA_MIPS3) {
551 env->hflags |= MIPS_HFLAG_64;
7871abb9
JH
552 if (!(env->insn_flags & ISA_MIPS64R6) ||
553 env->CP0_Status & (1 << CP0St_KX)) {
554 env->hflags &= ~MIPS_HFLAG_AWRAP;
555 }
d9224450
MR
556 }
557 env->hflags |= MIPS_HFLAG_CP0;
932e71cd
AJ
558 env->hflags &= ~(MIPS_HFLAG_KSU);
559 if (!(env->CP0_Status & (1 << CP0St_EXL)))
f45cb2f4 560 env->CP0_Cause &= ~(1U << CP0Ca_BD);
932e71cd 561 env->active_tc.PC = (int32_t)0xBFC00000;
bbfa8f72 562 set_hflags_for_handler(env);
932e71cd
AJ
563 break;
564 case EXCP_EXT_INTERRUPT:
565 cause = 0;
da52a4df
YK
566 if (env->CP0_Cause & (1 << CP0Ca_IV)) {
567 uint32_t spacing = (env->CP0_IntCtl >> CP0IntCtl_VS) & 0x1f;
568
569 if ((env->CP0_Status & (1 << CP0St_BEV)) || spacing == 0) {
570 offset = 0x200;
571 } else {
572 uint32_t vector = 0;
573 uint32_t pending = (env->CP0_Cause & CP0Ca_IP_mask) >> CP0Ca_IP;
574
575 if (env->CP0_Config3 & (1 << CP0C3_VEIC)) {
576 /* For VEIC mode, the external interrupt controller feeds
577 * the vector through the CP0Cause IP lines. */
578 vector = pending;
579 } else {
580 /* Vectored Interrupts
581 * Mask with Status.IM7-IM0 to get enabled interrupts. */
582 pending &= (env->CP0_Status >> CP0St_IM) & 0xff;
583 /* Find the highest-priority interrupt. */
584 while (pending >>= 1) {
585 vector++;
138afb02 586 }
138afb02 587 }
da52a4df 588 offset = 0x200 + (vector * (spacing << 5));
138afb02 589 }
138afb02 590 }
932e71cd
AJ
591 goto set_EPC;
592 case EXCP_LTLBL:
593 cause = 1;
aea14095 594 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
932e71cd
AJ
595 goto set_EPC;
596 case EXCP_TLBL:
597 cause = 2;
aea14095
LA
598 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
599 if ((env->error_code & EXCP_TLB_NOMATCH) &&
600 !(env->CP0_Status & (1 << CP0St_EXL))) {
0eaef5aa 601#if defined(TARGET_MIPS64)
932e71cd
AJ
602 int R = env->CP0_BadVAddr >> 62;
603 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
604 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
605 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
0eaef5aa 606
3fc00a7b
AJ
607 if (((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) &&
608 (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F))))
932e71cd
AJ
609 offset = 0x080;
610 else
0eaef5aa 611#endif
932e71cd
AJ
612 offset = 0x000;
613 }
614 goto set_EPC;
615 case EXCP_TLBS:
616 cause = 3;
aea14095
LA
617 update_badinstr = 1;
618 if ((env->error_code & EXCP_TLB_NOMATCH) &&
619 !(env->CP0_Status & (1 << CP0St_EXL))) {
0eaef5aa 620#if defined(TARGET_MIPS64)
932e71cd
AJ
621 int R = env->CP0_BadVAddr >> 62;
622 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
623 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
624 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
0eaef5aa 625
3fc00a7b
AJ
626 if (((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) &&
627 (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F))))
932e71cd
AJ
628 offset = 0x080;
629 else
0eaef5aa 630#endif
932e71cd
AJ
631 offset = 0x000;
632 }
633 goto set_EPC;
634 case EXCP_AdEL:
635 cause = 4;
aea14095 636 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
932e71cd
AJ
637 goto set_EPC;
638 case EXCP_AdES:
639 cause = 5;
aea14095 640 update_badinstr = 1;
932e71cd
AJ
641 goto set_EPC;
642 case EXCP_IBE:
643 cause = 6;
644 goto set_EPC;
645 case EXCP_DBE:
646 cause = 7;
647 goto set_EPC;
648 case EXCP_SYSCALL:
649 cause = 8;
aea14095 650 update_badinstr = 1;
932e71cd
AJ
651 goto set_EPC;
652 case EXCP_BREAK:
653 cause = 9;
aea14095 654 update_badinstr = 1;
932e71cd
AJ
655 goto set_EPC;
656 case EXCP_RI:
657 cause = 10;
aea14095 658 update_badinstr = 1;
932e71cd
AJ
659 goto set_EPC;
660 case EXCP_CpU:
661 cause = 11;
aea14095 662 update_badinstr = 1;
932e71cd
AJ
663 env->CP0_Cause = (env->CP0_Cause & ~(0x3 << CP0Ca_CE)) |
664 (env->error_code << CP0Ca_CE);
665 goto set_EPC;
666 case EXCP_OVERFLOW:
667 cause = 12;
aea14095 668 update_badinstr = 1;
932e71cd
AJ
669 goto set_EPC;
670 case EXCP_TRAP:
671 cause = 13;
aea14095 672 update_badinstr = 1;
932e71cd 673 goto set_EPC;
b10ac204
YK
674 case EXCP_MSAFPE:
675 cause = 14;
676 update_badinstr = 1;
677 goto set_EPC;
932e71cd
AJ
678 case EXCP_FPE:
679 cause = 15;
aea14095 680 update_badinstr = 1;
932e71cd
AJ
681 goto set_EPC;
682 case EXCP_C2E:
683 cause = 18;
684 goto set_EPC;
92ceb440
LA
685 case EXCP_TLBRI:
686 cause = 19;
aea14095 687 update_badinstr = 1;
92ceb440
LA
688 goto set_EPC;
689 case EXCP_TLBXI:
690 cause = 20;
691 goto set_EPC;
b10ac204
YK
692 case EXCP_MSADIS:
693 cause = 21;
694 update_badinstr = 1;
695 goto set_EPC;
932e71cd
AJ
696 case EXCP_MDMX:
697 cause = 22;
698 goto set_EPC;
699 case EXCP_DWATCH:
700 cause = 23;
67cc32eb 701 /* XXX: TODO: manage deferred watch exceptions */
932e71cd
AJ
702 goto set_EPC;
703 case EXCP_MCHECK:
704 cause = 24;
705 goto set_EPC;
706 case EXCP_THREAD:
707 cause = 25;
708 goto set_EPC;
853c3240
JL
709 case EXCP_DSPDIS:
710 cause = 26;
711 goto set_EPC;
932e71cd
AJ
712 case EXCP_CACHE:
713 cause = 30;
714 if (env->CP0_Status & (1 << CP0St_BEV)) {
715 offset = 0x100;
716 } else {
717 offset = 0x20000100;
718 }
0eaef5aa 719 set_EPC:
932e71cd 720 if (!(env->CP0_Status & (1 << CP0St_EXL))) {
32188a03 721 env->CP0_EPC = exception_resume_pc(env);
aea14095
LA
722 if (update_badinstr) {
723 set_badinstr_registers(env);
724 }
932e71cd 725 if (env->hflags & MIPS_HFLAG_BMASK) {
f45cb2f4 726 env->CP0_Cause |= (1U << CP0Ca_BD);
0eaef5aa 727 } else {
f45cb2f4 728 env->CP0_Cause &= ~(1U << CP0Ca_BD);
0eaef5aa 729 }
932e71cd 730 env->CP0_Status |= (1 << CP0St_EXL);
d9224450
MR
731 if (env->insn_flags & ISA_MIPS3) {
732 env->hflags |= MIPS_HFLAG_64;
7871abb9
JH
733 if (!(env->insn_flags & ISA_MIPS64R6) ||
734 env->CP0_Status & (1 << CP0St_KX)) {
735 env->hflags &= ~MIPS_HFLAG_AWRAP;
736 }
d9224450
MR
737 }
738 env->hflags |= MIPS_HFLAG_CP0;
932e71cd 739 env->hflags &= ~(MIPS_HFLAG_KSU);
6af0bf9c 740 }
932e71cd
AJ
741 env->hflags &= ~MIPS_HFLAG_BMASK;
742 if (env->CP0_Status & (1 << CP0St_BEV)) {
743 env->active_tc.PC = (int32_t)0xBFC00200;
744 } else {
745 env->active_tc.PC = (int32_t)(env->CP0_EBase & ~0x3ff);
6af0bf9c 746 }
932e71cd 747 env->active_tc.PC += offset;
bbfa8f72 748 set_hflags_for_handler(env);
932e71cd
AJ
749 env->CP0_Cause = (env->CP0_Cause & ~(0x1f << CP0Ca_EC)) | (cause << CP0Ca_EC);
750 break;
751 default:
c8557016 752 abort();
932e71cd 753 }
c8557016
RH
754 if (qemu_loglevel_mask(CPU_LOG_INT)
755 && cs->exception_index != EXCP_EXT_INTERRUPT) {
93fcfe39 756 qemu_log("%s: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " cause %d\n"
c8557016
RH
757 " S %08x C %08x A " TARGET_FMT_lx " D " TARGET_FMT_lx "\n",
758 __func__, env->active_tc.PC, env->CP0_EPC, cause,
759 env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr,
760 env->CP0_DEPC);
6af0bf9c 761 }
932e71cd 762#endif
27103424 763 cs->exception_index = EXCP_NONE;
6af0bf9c 764}
2ee4aed8 765
fa4faba4
RH
766bool mips_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
767{
768 if (interrupt_request & CPU_INTERRUPT_HARD) {
769 MIPSCPU *cpu = MIPS_CPU(cs);
770 CPUMIPSState *env = &cpu->env;
771
71ca034a
LA
772 if (cpu_mips_hw_interrupts_enabled(env) &&
773 cpu_mips_hw_interrupts_pending(env)) {
fa4faba4
RH
774 /* Raise it */
775 cs->exception_index = EXCP_EXT_INTERRUPT;
776 env->error_code = 0;
777 mips_cpu_do_interrupt(cs);
778 return true;
779 }
780 }
781 return false;
782}
783
3c7b48b7 784#if !defined(CONFIG_USER_ONLY)
7db13fae 785void r4k_invalidate_tlb (CPUMIPSState *env, int idx, int use_extra)
2ee4aed8 786{
31b030d4
AF
787 MIPSCPU *cpu = mips_env_get_cpu(env);
788 CPUState *cs;
c227f099 789 r4k_tlb_t *tlb;
3b1c8be4
TS
790 target_ulong addr;
791 target_ulong end;
792 uint8_t ASID = env->CP0_EntryHi & 0xFF;
793 target_ulong mask;
2ee4aed8 794
ead9360e 795 tlb = &env->tlb->mmu.r4k.tlb[idx];
f2e9ebef 796 /* The qemu TLB is flushed when the ASID changes, so no need to
2ee4aed8
FB
797 flush these entries again. */
798 if (tlb->G == 0 && tlb->ASID != ASID) {
799 return;
800 }
801
ead9360e 802 if (use_extra && env->tlb->tlb_in_use < MIPS_TLB_MAX) {
2ee4aed8 803 /* For tlbwr, we can shadow the discarded entry into
6958549d
AJ
804 a new (fake) TLB entry, as long as the guest can not
805 tell that it's there. */
ead9360e
TS
806 env->tlb->mmu.r4k.tlb[env->tlb->tlb_in_use] = *tlb;
807 env->tlb->tlb_in_use++;
2ee4aed8
FB
808 return;
809 }
810
3b1c8be4 811 /* 1k pages are not supported. */
f2e9ebef 812 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
3b1c8be4 813 if (tlb->V0) {
31b030d4 814 cs = CPU(cpu);
f2e9ebef 815 addr = tlb->VPN & ~mask;
d26bc211 816#if defined(TARGET_MIPS64)
e034e2c3 817 if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) {
100ce988
TS
818 addr |= 0x3FFFFF0000000000ULL;
819 }
820#endif
3b1c8be4
TS
821 end = addr | (mask >> 1);
822 while (addr < end) {
31b030d4 823 tlb_flush_page(cs, addr);
3b1c8be4
TS
824 addr += TARGET_PAGE_SIZE;
825 }
826 }
827 if (tlb->V1) {
31b030d4 828 cs = CPU(cpu);
f2e9ebef 829 addr = (tlb->VPN & ~mask) | ((mask >> 1) + 1);
d26bc211 830#if defined(TARGET_MIPS64)
e034e2c3 831 if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) {
100ce988
TS
832 addr |= 0x3FFFFF0000000000ULL;
833 }
834#endif
3b1c8be4 835 end = addr | mask;
53715e48 836 while (addr - 1 < end) {
31b030d4 837 tlb_flush_page(cs, addr);
3b1c8be4
TS
838 addr += TARGET_PAGE_SIZE;
839 }
840 }
2ee4aed8 841}
3c7b48b7 842#endif