]> git.proxmox.com Git - mirror_qemu.git/blame - target-mips/helper.c
target-mips: get_physical_address: Add defines for segment bases
[mirror_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 */
e37e863f
FB
19#include <stdarg.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <inttypes.h>
24#include <signal.h>
e37e863f
FB
25
26#include "cpu.h"
6af0bf9c 27
43057ab1
FB
28enum {
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 */
f2e9ebef 83 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
6af0bf9c 84 /* TLB match */
f2e9ebef 85 int n = !!(address & mask & ~(mask >> 1));
6af0bf9c 86 /* Check access rights */
f2e9ebef 87 if (!(n ? tlb->V1 : tlb->V0))
43057ab1 88 return TLBRET_INVALID;
f2e9ebef 89 if (rw == 0 || (n ? tlb->D1 : tlb->D0)) {
3b1c8be4 90 *physical = tlb->PFN[n] | (address & (mask >> 1));
9fb63ac2 91 *prot = PAGE_READ;
98c1b82b 92 if (n ? tlb->D1 : tlb->D0)
9fb63ac2 93 *prot |= PAGE_WRITE;
43057ab1 94 return TLBRET_MATCH;
6af0bf9c 95 }
43057ab1 96 return TLBRET_DIRTY;
6af0bf9c
FB
97 }
98 }
43057ab1 99 return TLBRET_NOMATCH;
6af0bf9c 100}
6af0bf9c 101
a8170e5e 102static int get_physical_address (CPUMIPSState *env, hwaddr *physical,
43057ab1
FB
103 int *prot, target_ulong address,
104 int rw, int access_type)
6af0bf9c 105{
b4ab4b4e 106 /* User mode can only access useg/xuseg */
43057ab1 107 int user_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM;
671880e6
TS
108 int supervisor_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_SM;
109 int kernel_mode = !user_mode && !supervisor_mode;
d26bc211 110#if defined(TARGET_MIPS64)
b4ab4b4e
TS
111 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
112 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
113 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
114#endif
43057ab1
FB
115 int ret = TLBRET_MATCH;
116
6af0bf9c 117#if 0
93fcfe39 118 qemu_log("user mode %d h %08x\n", user_mode, env->hflags);
6af0bf9c 119#endif
b4ab4b4e 120
22010ce7
JH
121#define USEG_LIMIT 0x7FFFFFFFUL
122#define KSEG0_BASE 0x80000000UL
123#define KSEG1_BASE 0xA0000000UL
124#define KSEG2_BASE 0xC0000000UL
125#define KSEG3_BASE 0xE0000000UL
126
127 if (address <= USEG_LIMIT) {
b4ab4b4e 128 /* useg */
996ba2cc 129 if (env->CP0_Status & (1 << CP0St_ERL)) {
29929e34 130 *physical = address & 0xFFFFFFFF;
6af0bf9c 131 *prot = PAGE_READ | PAGE_WRITE;
996ba2cc 132 } else {
ead9360e 133 ret = env->tlb->map_address(env, physical, prot, address, rw, access_type);
6af0bf9c 134 }
d26bc211 135#if defined(TARGET_MIPS64)
89fc88da 136 } else if (address < 0x4000000000000000ULL) {
b4ab4b4e 137 /* xuseg */
6958549d 138 if (UX && address <= (0x3FFFFFFFFFFFFFFFULL & env->SEGMask)) {
ead9360e 139 ret = env->tlb->map_address(env, physical, prot, address, rw, access_type);
6958549d
AJ
140 } else {
141 ret = TLBRET_BADADDR;
b4ab4b4e 142 }
89fc88da 143 } else if (address < 0x8000000000000000ULL) {
b4ab4b4e 144 /* xsseg */
6958549d
AJ
145 if ((supervisor_mode || kernel_mode) &&
146 SX && address <= (0x7FFFFFFFFFFFFFFFULL & env->SEGMask)) {
ead9360e 147 ret = env->tlb->map_address(env, physical, prot, address, rw, access_type);
6958549d
AJ
148 } else {
149 ret = TLBRET_BADADDR;
b4ab4b4e 150 }
89fc88da 151 } else if (address < 0xC000000000000000ULL) {
b4ab4b4e 152 /* xkphys */
671880e6 153 if (kernel_mode && KX &&
6d35524c
TS
154 (address & 0x07FFFFFFFFFFFFFFULL) <= env->PAMask) {
155 *physical = address & env->PAMask;
b4ab4b4e 156 *prot = PAGE_READ | PAGE_WRITE;
6958549d
AJ
157 } else {
158 ret = TLBRET_BADADDR;
159 }
89fc88da 160 } else if (address < 0xFFFFFFFF80000000ULL) {
b4ab4b4e 161 /* xkseg */
6958549d
AJ
162 if (kernel_mode && KX &&
163 address <= (0xFFFFFFFF7FFFFFFFULL & env->SEGMask)) {
ead9360e 164 ret = env->tlb->map_address(env, physical, prot, address, rw, access_type);
6958549d
AJ
165 } else {
166 ret = TLBRET_BADADDR;
167 }
b4ab4b4e 168#endif
22010ce7 169 } else if (address < (int32_t)KSEG1_BASE) {
6af0bf9c 170 /* kseg0 */
671880e6 171 if (kernel_mode) {
22010ce7 172 *physical = address - (int32_t)KSEG0_BASE;
671880e6
TS
173 *prot = PAGE_READ | PAGE_WRITE;
174 } else {
175 ret = TLBRET_BADADDR;
176 }
22010ce7 177 } else if (address < (int32_t)KSEG2_BASE) {
6af0bf9c 178 /* kseg1 */
671880e6 179 if (kernel_mode) {
22010ce7 180 *physical = address - (int32_t)KSEG1_BASE;
671880e6
TS
181 *prot = PAGE_READ | PAGE_WRITE;
182 } else {
183 ret = TLBRET_BADADDR;
184 }
22010ce7 185 } else if (address < (int32_t)KSEG3_BASE) {
89fc88da 186 /* sseg (kseg2) */
671880e6
TS
187 if (supervisor_mode || kernel_mode) {
188 ret = env->tlb->map_address(env, physical, prot, address, rw, access_type);
189 } else {
190 ret = TLBRET_BADADDR;
191 }
6af0bf9c
FB
192 } else {
193 /* kseg3 */
6af0bf9c 194 /* XXX: debug segment is not emulated */
671880e6
TS
195 if (kernel_mode) {
196 ret = env->tlb->map_address(env, physical, prot, address, rw, access_type);
197 } else {
198 ret = TLBRET_BADADDR;
199 }
6af0bf9c
FB
200 }
201#if 0
951fab99 202 qemu_log(TARGET_FMT_lx " %d %d => %" HWADDR_PRIx " %d (%d)\n",
93fcfe39 203 address, rw, access_type, *physical, *prot, ret);
6af0bf9c
FB
204#endif
205
206 return ret;
207}
932e71cd 208#endif
6af0bf9c 209
7db13fae 210static void raise_mmu_exception(CPUMIPSState *env, target_ulong address,
1147e189
AJ
211 int rw, int tlb_error)
212{
27103424 213 CPUState *cs = CPU(mips_env_get_cpu(env));
1147e189
AJ
214 int exception = 0, error_code = 0;
215
216 switch (tlb_error) {
217 default:
218 case TLBRET_BADADDR:
219 /* Reference to kernel address from user mode or supervisor mode */
220 /* Reference to supervisor address from user mode */
221 if (rw)
222 exception = EXCP_AdES;
223 else
224 exception = EXCP_AdEL;
225 break;
226 case TLBRET_NOMATCH:
227 /* No TLB match for a mapped address */
228 if (rw)
229 exception = EXCP_TLBS;
230 else
231 exception = EXCP_TLBL;
232 error_code = 1;
233 break;
234 case TLBRET_INVALID:
235 /* TLB match with no valid bit */
236 if (rw)
237 exception = EXCP_TLBS;
238 else
239 exception = EXCP_TLBL;
240 break;
241 case TLBRET_DIRTY:
242 /* TLB match but 'D' bit is cleared */
243 exception = EXCP_LTLBL;
244 break;
245
246 }
247 /* Raise exception */
248 env->CP0_BadVAddr = address;
249 env->CP0_Context = (env->CP0_Context & ~0x007fffff) |
250 ((address >> 9) & 0x007ffff0);
251 env->CP0_EntryHi =
252 (env->CP0_EntryHi & 0xFF) | (address & (TARGET_PAGE_MASK << 1));
253#if defined(TARGET_MIPS64)
254 env->CP0_EntryHi &= env->SEGMask;
255 env->CP0_XContext = (env->CP0_XContext & ((~0ULL) << (env->SEGBITS - 7))) |
256 ((address & 0xC00000000000ULL) >> (55 - env->SEGBITS)) |
257 ((address & ((1ULL << env->SEGBITS) - 1) & 0xFFFFFFFFFFFFE000ULL) >> 9);
258#endif
27103424 259 cs->exception_index = exception;
1147e189
AJ
260 env->error_code = error_code;
261}
262
4fcc562b 263#if !defined(CONFIG_USER_ONLY)
00b941e5 264hwaddr mips_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
6af0bf9c 265{
00b941e5 266 MIPSCPU *cpu = MIPS_CPU(cs);
a8170e5e 267 hwaddr phys_addr;
932e71cd 268 int prot;
6af0bf9c 269
00b941e5
AF
270 if (get_physical_address(&cpu->env, &phys_addr, &prot, addr, 0,
271 ACCESS_INT) != 0) {
932e71cd 272 return -1;
00b941e5 273 }
932e71cd 274 return phys_addr;
6af0bf9c 275}
4fcc562b 276#endif
6af0bf9c 277
7510454e
AF
278int mips_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
279 int mmu_idx)
6af0bf9c 280{
7510454e
AF
281 MIPSCPU *cpu = MIPS_CPU(cs);
282 CPUMIPSState *env = &cpu->env;
932e71cd 283#if !defined(CONFIG_USER_ONLY)
a8170e5e 284 hwaddr physical;
6af0bf9c 285 int prot;
6af0bf9c 286 int access_type;
99e43d36 287#endif
6af0bf9c
FB
288 int ret = 0;
289
4ad40f36 290#if 0
7510454e 291 log_cpu_state(cs, 0);
4ad40f36 292#endif
7510454e 293 qemu_log("%s pc " TARGET_FMT_lx " ad %" VADDR_PRIx " rw %d mmu_idx %d\n",
97b348e7 294 __func__, env->active_tc.PC, address, rw, mmu_idx);
4ad40f36
FB
295
296 rw &= 1;
297
6af0bf9c 298 /* data access */
99e43d36 299#if !defined(CONFIG_USER_ONLY)
6af0bf9c
FB
300 /* XXX: put correct access by using cpu_restore_state()
301 correctly */
302 access_type = ACCESS_INT;
6af0bf9c
FB
303 ret = get_physical_address(env, &physical, &prot,
304 address, rw, access_type);
7510454e
AF
305 qemu_log("%s address=%" VADDR_PRIx " ret %d physical " TARGET_FMT_plx
306 " prot %d\n",
307 __func__, address, ret, physical, prot);
43057ab1 308 if (ret == TLBRET_MATCH) {
0c591eb0 309 tlb_set_page(cs, address & TARGET_PAGE_MASK,
99e43d36
AJ
310 physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
311 mmu_idx, TARGET_PAGE_SIZE);
312 ret = 0;
932e71cd
AJ
313 } else if (ret < 0)
314#endif
315 {
1147e189 316 raise_mmu_exception(env, address, rw, ret);
6af0bf9c
FB
317 ret = 1;
318 }
319
320 return ret;
321}
322
25b91e32 323#if !defined(CONFIG_USER_ONLY)
a8170e5e 324hwaddr cpu_mips_translate_address(CPUMIPSState *env, target_ulong address, int rw)
25b91e32 325{
a8170e5e 326 hwaddr physical;
25b91e32
AJ
327 int prot;
328 int access_type;
329 int ret = 0;
330
331 rw &= 1;
332
333 /* data access */
334 access_type = ACCESS_INT;
335 ret = get_physical_address(env, &physical, &prot,
336 address, rw, access_type);
337 if (ret != TLBRET_MATCH) {
338 raise_mmu_exception(env, address, rw, ret);
c36bbb28
AJ
339 return -1LL;
340 } else {
341 return physical;
25b91e32 342 }
25b91e32
AJ
343}
344#endif
345
9a5d878f
TS
346static const char * const excp_names[EXCP_LAST + 1] = {
347 [EXCP_RESET] = "reset",
348 [EXCP_SRESET] = "soft reset",
349 [EXCP_DSS] = "debug single step",
350 [EXCP_DINT] = "debug interrupt",
351 [EXCP_NMI] = "non-maskable interrupt",
352 [EXCP_MCHECK] = "machine check",
353 [EXCP_EXT_INTERRUPT] = "interrupt",
354 [EXCP_DFWATCH] = "deferred watchpoint",
355 [EXCP_DIB] = "debug instruction breakpoint",
356 [EXCP_IWATCH] = "instruction fetch watchpoint",
357 [EXCP_AdEL] = "address error load",
358 [EXCP_AdES] = "address error store",
359 [EXCP_TLBF] = "TLB refill",
360 [EXCP_IBE] = "instruction bus error",
361 [EXCP_DBp] = "debug breakpoint",
362 [EXCP_SYSCALL] = "syscall",
363 [EXCP_BREAK] = "break",
364 [EXCP_CpU] = "coprocessor unusable",
365 [EXCP_RI] = "reserved instruction",
366 [EXCP_OVERFLOW] = "arithmetic overflow",
367 [EXCP_TRAP] = "trap",
368 [EXCP_FPE] = "floating point",
369 [EXCP_DDBS] = "debug data break store",
370 [EXCP_DWATCH] = "data watchpoint",
371 [EXCP_LTLBL] = "TLB modify",
372 [EXCP_TLBL] = "TLB load",
373 [EXCP_TLBS] = "TLB store",
374 [EXCP_DBE] = "data bus error",
375 [EXCP_DDBL] = "debug data break load",
376 [EXCP_THREAD] = "thread",
377 [EXCP_MDMX] = "MDMX",
378 [EXCP_C2E] = "precise coprocessor 2",
379 [EXCP_CACHE] = "cache error",
14e51cc7 380};
14e51cc7 381
1239b472 382target_ulong exception_resume_pc (CPUMIPSState *env)
32188a03
NF
383{
384 target_ulong bad_pc;
385 target_ulong isa_mode;
386
387 isa_mode = !!(env->hflags & MIPS_HFLAG_M16);
388 bad_pc = env->active_tc.PC | isa_mode;
389 if (env->hflags & MIPS_HFLAG_BMASK) {
390 /* If the exception was raised from a delay slot, come back to
391 the jump. */
392 bad_pc -= (env->hflags & MIPS_HFLAG_B16 ? 2 : 4);
393 }
394
395 return bad_pc;
396}
bbfa8f72 397
1239b472 398#if !defined(CONFIG_USER_ONLY)
7db13fae 399static void set_hflags_for_handler (CPUMIPSState *env)
bbfa8f72
NF
400{
401 /* Exception handlers are entered in 32-bit mode. */
402 env->hflags &= ~(MIPS_HFLAG_M16);
403 /* ...except that microMIPS lets you choose. */
404 if (env->insn_flags & ASE_MICROMIPS) {
405 env->hflags |= (!!(env->CP0_Config3
406 & (1 << CP0C3_ISA_ON_EXC))
407 << MIPS_HFLAG_M16_SHIFT);
408 }
409}
32188a03
NF
410#endif
411
97a8ea5a 412void mips_cpu_do_interrupt(CPUState *cs)
6af0bf9c 413{
27103424 414#if !defined(CONFIG_USER_ONLY)
97a8ea5a
AF
415 MIPSCPU *cpu = MIPS_CPU(cs);
416 CPUMIPSState *env = &cpu->env;
932e71cd
AJ
417 target_ulong offset;
418 int cause = -1;
419 const char *name;
100ce988 420
27103424
AF
421 if (qemu_log_enabled() && cs->exception_index != EXCP_EXT_INTERRUPT) {
422 if (cs->exception_index < 0 || cs->exception_index > EXCP_LAST) {
932e71cd 423 name = "unknown";
27103424
AF
424 } else {
425 name = excp_names[cs->exception_index];
426 }
b67bfe8d 427
93fcfe39
AL
428 qemu_log("%s enter: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " %s exception\n",
429 __func__, env->active_tc.PC, env->CP0_EPC, name);
932e71cd 430 }
27103424
AF
431 if (cs->exception_index == EXCP_EXT_INTERRUPT &&
432 (env->hflags & MIPS_HFLAG_DM)) {
433 cs->exception_index = EXCP_DINT;
434 }
932e71cd 435 offset = 0x180;
27103424 436 switch (cs->exception_index) {
932e71cd
AJ
437 case EXCP_DSS:
438 env->CP0_Debug |= 1 << CP0DB_DSS;
439 /* Debug single step cannot be raised inside a delay slot and
440 resume will always occur on the next instruction
441 (but we assume the pc has always been updated during
442 code translation). */
32188a03 443 env->CP0_DEPC = env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16);
932e71cd
AJ
444 goto enter_debug_mode;
445 case EXCP_DINT:
446 env->CP0_Debug |= 1 << CP0DB_DINT;
447 goto set_DEPC;
448 case EXCP_DIB:
449 env->CP0_Debug |= 1 << CP0DB_DIB;
450 goto set_DEPC;
451 case EXCP_DBp:
452 env->CP0_Debug |= 1 << CP0DB_DBp;
453 goto set_DEPC;
454 case EXCP_DDBS:
455 env->CP0_Debug |= 1 << CP0DB_DDBS;
456 goto set_DEPC;
457 case EXCP_DDBL:
458 env->CP0_Debug |= 1 << CP0DB_DDBL;
459 set_DEPC:
32188a03
NF
460 env->CP0_DEPC = exception_resume_pc(env);
461 env->hflags &= ~MIPS_HFLAG_BMASK;
0eaef5aa 462 enter_debug_mode:
932e71cd
AJ
463 env->hflags |= MIPS_HFLAG_DM | MIPS_HFLAG_64 | MIPS_HFLAG_CP0;
464 env->hflags &= ~(MIPS_HFLAG_KSU);
465 /* EJTAG probe trap enable is not implemented... */
466 if (!(env->CP0_Status & (1 << CP0St_EXL)))
f45cb2f4 467 env->CP0_Cause &= ~(1U << CP0Ca_BD);
932e71cd 468 env->active_tc.PC = (int32_t)0xBFC00480;
bbfa8f72 469 set_hflags_for_handler(env);
932e71cd
AJ
470 break;
471 case EXCP_RESET:
fca1be7c 472 cpu_reset(CPU(cpu));
932e71cd
AJ
473 break;
474 case EXCP_SRESET:
475 env->CP0_Status |= (1 << CP0St_SR);
476 memset(env->CP0_WatchLo, 0, sizeof(*env->CP0_WatchLo));
477 goto set_error_EPC;
478 case EXCP_NMI:
479 env->CP0_Status |= (1 << CP0St_NMI);
0eaef5aa 480 set_error_EPC:
32188a03
NF
481 env->CP0_ErrorEPC = exception_resume_pc(env);
482 env->hflags &= ~MIPS_HFLAG_BMASK;
932e71cd
AJ
483 env->CP0_Status |= (1 << CP0St_ERL) | (1 << CP0St_BEV);
484 env->hflags |= MIPS_HFLAG_64 | MIPS_HFLAG_CP0;
485 env->hflags &= ~(MIPS_HFLAG_KSU);
486 if (!(env->CP0_Status & (1 << CP0St_EXL)))
f45cb2f4 487 env->CP0_Cause &= ~(1U << CP0Ca_BD);
932e71cd 488 env->active_tc.PC = (int32_t)0xBFC00000;
bbfa8f72 489 set_hflags_for_handler(env);
932e71cd
AJ
490 break;
491 case EXCP_EXT_INTERRUPT:
492 cause = 0;
493 if (env->CP0_Cause & (1 << CP0Ca_IV))
494 offset = 0x200;
138afb02
EI
495
496 if (env->CP0_Config3 & ((1 << CP0C3_VInt) | (1 << CP0C3_VEIC))) {
497 /* Vectored Interrupts. */
498 unsigned int spacing;
499 unsigned int vector;
500 unsigned int pending = (env->CP0_Cause & CP0Ca_IP_mask) >> 8;
501
e4280973 502 pending &= env->CP0_Status >> 8;
138afb02
EI
503 /* Compute the Vector Spacing. */
504 spacing = (env->CP0_IntCtl >> CP0IntCtl_VS) & ((1 << 6) - 1);
505 spacing <<= 5;
506
507 if (env->CP0_Config3 & (1 << CP0C3_VInt)) {
508 /* For VInt mode, the MIPS computes the vector internally. */
e4280973
EI
509 for (vector = 7; vector > 0; vector--) {
510 if (pending & (1 << vector)) {
138afb02
EI
511 /* Found it. */
512 break;
513 }
138afb02
EI
514 }
515 } else {
516 /* For VEIC mode, the external interrupt controller feeds the
e7d81004 517 vector through the CP0Cause IP lines. */
138afb02
EI
518 vector = pending;
519 }
520 offset = 0x200 + vector * spacing;
521 }
932e71cd
AJ
522 goto set_EPC;
523 case EXCP_LTLBL:
524 cause = 1;
525 goto set_EPC;
526 case EXCP_TLBL:
527 cause = 2;
528 if (env->error_code == 1 && !(env->CP0_Status & (1 << CP0St_EXL))) {
0eaef5aa 529#if defined(TARGET_MIPS64)
932e71cd
AJ
530 int R = env->CP0_BadVAddr >> 62;
531 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
532 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
533 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
0eaef5aa 534
3fc00a7b
AJ
535 if (((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) &&
536 (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F))))
932e71cd
AJ
537 offset = 0x080;
538 else
0eaef5aa 539#endif
932e71cd
AJ
540 offset = 0x000;
541 }
542 goto set_EPC;
543 case EXCP_TLBS:
544 cause = 3;
545 if (env->error_code == 1 && !(env->CP0_Status & (1 << CP0St_EXL))) {
0eaef5aa 546#if defined(TARGET_MIPS64)
932e71cd
AJ
547 int R = env->CP0_BadVAddr >> 62;
548 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
549 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
550 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
0eaef5aa 551
3fc00a7b
AJ
552 if (((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) &&
553 (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F))))
932e71cd
AJ
554 offset = 0x080;
555 else
0eaef5aa 556#endif
932e71cd
AJ
557 offset = 0x000;
558 }
559 goto set_EPC;
560 case EXCP_AdEL:
561 cause = 4;
562 goto set_EPC;
563 case EXCP_AdES:
564 cause = 5;
565 goto set_EPC;
566 case EXCP_IBE:
567 cause = 6;
568 goto set_EPC;
569 case EXCP_DBE:
570 cause = 7;
571 goto set_EPC;
572 case EXCP_SYSCALL:
573 cause = 8;
574 goto set_EPC;
575 case EXCP_BREAK:
576 cause = 9;
577 goto set_EPC;
578 case EXCP_RI:
579 cause = 10;
580 goto set_EPC;
581 case EXCP_CpU:
582 cause = 11;
583 env->CP0_Cause = (env->CP0_Cause & ~(0x3 << CP0Ca_CE)) |
584 (env->error_code << CP0Ca_CE);
585 goto set_EPC;
586 case EXCP_OVERFLOW:
587 cause = 12;
588 goto set_EPC;
589 case EXCP_TRAP:
590 cause = 13;
591 goto set_EPC;
592 case EXCP_FPE:
593 cause = 15;
594 goto set_EPC;
595 case EXCP_C2E:
596 cause = 18;
597 goto set_EPC;
598 case EXCP_MDMX:
599 cause = 22;
600 goto set_EPC;
601 case EXCP_DWATCH:
602 cause = 23;
603 /* XXX: TODO: manage defered watch exceptions */
604 goto set_EPC;
605 case EXCP_MCHECK:
606 cause = 24;
607 goto set_EPC;
608 case EXCP_THREAD:
609 cause = 25;
610 goto set_EPC;
853c3240
JL
611 case EXCP_DSPDIS:
612 cause = 26;
613 goto set_EPC;
932e71cd
AJ
614 case EXCP_CACHE:
615 cause = 30;
616 if (env->CP0_Status & (1 << CP0St_BEV)) {
617 offset = 0x100;
618 } else {
619 offset = 0x20000100;
620 }
0eaef5aa 621 set_EPC:
932e71cd 622 if (!(env->CP0_Status & (1 << CP0St_EXL))) {
32188a03 623 env->CP0_EPC = exception_resume_pc(env);
932e71cd 624 if (env->hflags & MIPS_HFLAG_BMASK) {
f45cb2f4 625 env->CP0_Cause |= (1U << CP0Ca_BD);
0eaef5aa 626 } else {
f45cb2f4 627 env->CP0_Cause &= ~(1U << CP0Ca_BD);
0eaef5aa 628 }
932e71cd
AJ
629 env->CP0_Status |= (1 << CP0St_EXL);
630 env->hflags |= MIPS_HFLAG_64 | MIPS_HFLAG_CP0;
631 env->hflags &= ~(MIPS_HFLAG_KSU);
6af0bf9c 632 }
932e71cd
AJ
633 env->hflags &= ~MIPS_HFLAG_BMASK;
634 if (env->CP0_Status & (1 << CP0St_BEV)) {
635 env->active_tc.PC = (int32_t)0xBFC00200;
636 } else {
637 env->active_tc.PC = (int32_t)(env->CP0_EBase & ~0x3ff);
6af0bf9c 638 }
932e71cd 639 env->active_tc.PC += offset;
bbfa8f72 640 set_hflags_for_handler(env);
932e71cd
AJ
641 env->CP0_Cause = (env->CP0_Cause & ~(0x1f << CP0Ca_EC)) | (cause << CP0Ca_EC);
642 break;
643 default:
27103424
AF
644 qemu_log("Invalid MIPS exception %d. Exiting\n", cs->exception_index);
645 printf("Invalid MIPS exception %d. Exiting\n", cs->exception_index);
932e71cd
AJ
646 exit(1);
647 }
27103424 648 if (qemu_log_enabled() && cs->exception_index != EXCP_EXT_INTERRUPT) {
93fcfe39 649 qemu_log("%s: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " cause %d\n"
932e71cd
AJ
650 " S %08x C %08x A " TARGET_FMT_lx " D " TARGET_FMT_lx "\n",
651 __func__, env->active_tc.PC, env->CP0_EPC, cause,
652 env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr,
653 env->CP0_DEPC);
6af0bf9c 654 }
932e71cd 655#endif
27103424 656 cs->exception_index = EXCP_NONE;
6af0bf9c 657}
2ee4aed8 658
3c7b48b7 659#if !defined(CONFIG_USER_ONLY)
7db13fae 660void r4k_invalidate_tlb (CPUMIPSState *env, int idx, int use_extra)
2ee4aed8 661{
31b030d4
AF
662 MIPSCPU *cpu = mips_env_get_cpu(env);
663 CPUState *cs;
c227f099 664 r4k_tlb_t *tlb;
3b1c8be4
TS
665 target_ulong addr;
666 target_ulong end;
667 uint8_t ASID = env->CP0_EntryHi & 0xFF;
668 target_ulong mask;
2ee4aed8 669
ead9360e 670 tlb = &env->tlb->mmu.r4k.tlb[idx];
f2e9ebef 671 /* The qemu TLB is flushed when the ASID changes, so no need to
2ee4aed8
FB
672 flush these entries again. */
673 if (tlb->G == 0 && tlb->ASID != ASID) {
674 return;
675 }
676
ead9360e 677 if (use_extra && env->tlb->tlb_in_use < MIPS_TLB_MAX) {
2ee4aed8 678 /* For tlbwr, we can shadow the discarded entry into
6958549d
AJ
679 a new (fake) TLB entry, as long as the guest can not
680 tell that it's there. */
ead9360e
TS
681 env->tlb->mmu.r4k.tlb[env->tlb->tlb_in_use] = *tlb;
682 env->tlb->tlb_in_use++;
2ee4aed8
FB
683 return;
684 }
685
3b1c8be4 686 /* 1k pages are not supported. */
f2e9ebef 687 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
3b1c8be4 688 if (tlb->V0) {
31b030d4 689 cs = CPU(cpu);
f2e9ebef 690 addr = tlb->VPN & ~mask;
d26bc211 691#if defined(TARGET_MIPS64)
e034e2c3 692 if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) {
100ce988
TS
693 addr |= 0x3FFFFF0000000000ULL;
694 }
695#endif
3b1c8be4
TS
696 end = addr | (mask >> 1);
697 while (addr < end) {
31b030d4 698 tlb_flush_page(cs, addr);
3b1c8be4
TS
699 addr += TARGET_PAGE_SIZE;
700 }
701 }
702 if (tlb->V1) {
31b030d4 703 cs = CPU(cpu);
f2e9ebef 704 addr = (tlb->VPN & ~mask) | ((mask >> 1) + 1);
d26bc211 705#if defined(TARGET_MIPS64)
e034e2c3 706 if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) {
100ce988
TS
707 addr |= 0x3FFFFF0000000000ULL;
708 }
709#endif
3b1c8be4 710 end = addr | mask;
53715e48 711 while (addr - 1 < end) {
31b030d4 712 tlb_flush_page(cs, addr);
3b1c8be4
TS
713 addr += TARGET_PAGE_SIZE;
714 }
715 }
2ee4aed8 716}
3c7b48b7 717#endif