]> git.proxmox.com Git - mirror_qemu.git/blame - target/mips/helper.c
target/mips: compare virtual addresses in LL/SC sequence
[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 */
c684822a 19#include "qemu/osdep.h"
e37e863f
FB
20
21#include "cpu.h"
26aa3d9a 22#include "internal.h"
63c91552 23#include "exec/exec-all.h"
aea14095 24#include "exec/cpu_ldst.h"
508127e2 25#include "exec/log.h"
d3d93c6c 26#include "hw/mips/cpudevs.h"
6af0bf9c 27
43057ab1 28enum {
2fb58b73
LA
29 TLBRET_XI = -6,
30 TLBRET_RI = -5,
43057ab1
FB
31 TLBRET_DIRTY = -4,
32 TLBRET_INVALID = -3,
33 TLBRET_NOMATCH = -2,
34 TLBRET_BADADDR = -1,
35 TLBRET_MATCH = 0
36};
37
3c7b48b7
PB
38#if !defined(CONFIG_USER_ONLY)
39
29929e34 40/* no MMU emulation */
a8170e5e 41int no_mmu_map_address (CPUMIPSState *env, hwaddr *physical, int *prot,
6af0bf9c 42 target_ulong address, int rw, int access_type)
29929e34
TS
43{
44 *physical = address;
45 *prot = PAGE_READ | PAGE_WRITE;
46 return TLBRET_MATCH;
47}
48
49/* fixed mapping MMU emulation */
a8170e5e 50int fixed_mmu_map_address (CPUMIPSState *env, hwaddr *physical, int *prot,
29929e34
TS
51 target_ulong address, int rw, int access_type)
52{
53 if (address <= (int32_t)0x7FFFFFFFUL) {
54 if (!(env->CP0_Status & (1 << CP0St_ERL)))
55 *physical = address + 0x40000000UL;
56 else
57 *physical = address;
58 } else if (address <= (int32_t)0xBFFFFFFFUL)
59 *physical = address & 0x1FFFFFFF;
60 else
61 *physical = address;
62
63 *prot = PAGE_READ | PAGE_WRITE;
64 return TLBRET_MATCH;
65}
66
67/* MIPS32/MIPS64 R4000-style MMU emulation */
a8170e5e 68int r4k_map_address (CPUMIPSState *env, hwaddr *physical, int *prot,
29929e34 69 target_ulong address, int rw, int access_type)
6af0bf9c 70{
2d72e7b0 71 uint16_t ASID = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask;
3b1c8be4 72 int i;
6af0bf9c 73
ead9360e 74 for (i = 0; i < env->tlb->tlb_in_use; i++) {
c227f099 75 r4k_tlb_t *tlb = &env->tlb->mmu.r4k.tlb[i];
3b1c8be4 76 /* 1k pages are not supported. */
f2e9ebef 77 target_ulong mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
3b1c8be4 78 target_ulong tag = address & ~mask;
f2e9ebef 79 target_ulong VPN = tlb->VPN & ~mask;
d26bc211 80#if defined(TARGET_MIPS64)
e034e2c3 81 tag &= env->SEGMask;
100ce988 82#endif
3b1c8be4 83
6af0bf9c 84 /* Check ASID, virtual page number & size */
9456c2fb 85 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag && !tlb->EHINV) {
6af0bf9c 86 /* TLB match */
f2e9ebef 87 int n = !!(address & mask & ~(mask >> 1));
6af0bf9c 88 /* Check access rights */
2fb58b73 89 if (!(n ? tlb->V1 : tlb->V0)) {
43057ab1 90 return TLBRET_INVALID;
2fb58b73
LA
91 }
92 if (rw == MMU_INST_FETCH && (n ? tlb->XI1 : tlb->XI0)) {
93 return TLBRET_XI;
94 }
95 if (rw == MMU_DATA_LOAD && (n ? tlb->RI1 : tlb->RI0)) {
96 return TLBRET_RI;
97 }
9f6bcedb 98 if (rw != MMU_DATA_STORE || (n ? tlb->D1 : tlb->D0)) {
3b1c8be4 99 *physical = tlb->PFN[n] | (address & (mask >> 1));
9fb63ac2 100 *prot = PAGE_READ;
98c1b82b 101 if (n ? tlb->D1 : tlb->D0)
9fb63ac2 102 *prot |= PAGE_WRITE;
43057ab1 103 return TLBRET_MATCH;
6af0bf9c 104 }
43057ab1 105 return TLBRET_DIRTY;
6af0bf9c
FB
106 }
107 }
43057ab1 108 return TLBRET_NOMATCH;
6af0bf9c 109}
6af0bf9c 110
480e79ae
JH
111static int is_seg_am_mapped(unsigned int am, bool eu, int mmu_idx)
112{
113 /*
114 * Interpret access control mode and mmu_idx.
115 * AdE? TLB?
116 * AM K S U E K S U E
117 * UK 0 0 1 1 0 0 - - 0
118 * MK 1 0 1 1 0 1 - - !eu
119 * MSK 2 0 0 1 0 1 1 - !eu
120 * MUSK 3 0 0 0 0 1 1 1 !eu
121 * MUSUK 4 0 0 0 0 0 1 1 0
122 * USK 5 0 0 1 0 0 0 - 0
123 * - 6 - - - - - - - -
124 * UUSK 7 0 0 0 0 0 0 0 0
125 */
126 int32_t adetlb_mask;
127
128 switch (mmu_idx) {
129 case 3 /* ERL */:
130 /* If EU is set, always unmapped */
131 if (eu) {
132 return 0;
133 }
134 /* fall through */
135 case MIPS_HFLAG_KM:
136 /* Never AdE, TLB mapped if AM={1,2,3} */
137 adetlb_mask = 0x70000000;
138 goto check_tlb;
139
140 case MIPS_HFLAG_SM:
141 /* AdE if AM={0,1}, TLB mapped if AM={2,3,4} */
142 adetlb_mask = 0xc0380000;
143 goto check_ade;
144
145 case MIPS_HFLAG_UM:
146 /* AdE if AM={0,1,2,5}, TLB mapped if AM={3,4} */
147 adetlb_mask = 0xe4180000;
148 /* fall through */
149 check_ade:
150 /* does this AM cause AdE in current execution mode */
151 if ((adetlb_mask << am) < 0) {
152 return TLBRET_BADADDR;
153 }
154 adetlb_mask <<= 8;
155 /* fall through */
156 check_tlb:
157 /* is this AM mapped in current execution mode */
158 return ((adetlb_mask << am) < 0);
159 default:
160 assert(0);
161 return TLBRET_BADADDR;
162 };
163}
164
165static int get_seg_physical_address(CPUMIPSState *env, hwaddr *physical,
166 int *prot, target_ulong real_address,
167 int rw, int access_type, int mmu_idx,
168 unsigned int am, bool eu,
169 target_ulong segmask,
170 hwaddr physical_base)
171{
172 int mapped = is_seg_am_mapped(am, eu, mmu_idx);
173
174 if (mapped < 0) {
175 /* is_seg_am_mapped can report TLBRET_BADADDR */
176 return mapped;
177 } else if (mapped) {
178 /* The segment is TLB mapped */
179 return env->tlb->map_address(env, physical, prot, real_address, rw,
180 access_type);
181 } else {
182 /* The segment is unmapped */
183 *physical = physical_base | (real_address & segmask);
184 *prot = PAGE_READ | PAGE_WRITE;
185 return TLBRET_MATCH;
186 }
187}
188
189static int get_segctl_physical_address(CPUMIPSState *env, hwaddr *physical,
190 int *prot, target_ulong real_address,
191 int rw, int access_type, int mmu_idx,
192 uint16_t segctl, target_ulong segmask)
193{
194 unsigned int am = (segctl & CP0SC_AM_MASK) >> CP0SC_AM;
195 bool eu = (segctl >> CP0SC_EU) & 1;
196 hwaddr pa = ((hwaddr)segctl & CP0SC_PA_MASK) << 20;
197
198 return get_seg_physical_address(env, physical, prot, real_address, rw,
199 access_type, mmu_idx, am, eu, segmask,
200 pa & ~(hwaddr)segmask);
201}
202
a8170e5e 203static int get_physical_address (CPUMIPSState *env, hwaddr *physical,
4ef37e69 204 int *prot, target_ulong real_address,
9fbf4a58 205 int rw, int access_type, int mmu_idx)
6af0bf9c 206{
b4ab4b4e 207 /* User mode can only access useg/xuseg */
480e79ae 208#if defined(TARGET_MIPS64)
9fbf4a58
JH
209 int user_mode = mmu_idx == MIPS_HFLAG_UM;
210 int supervisor_mode = mmu_idx == MIPS_HFLAG_SM;
671880e6 211 int kernel_mode = !user_mode && !supervisor_mode;
b4ab4b4e
TS
212 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
213 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
214 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
215#endif
43057ab1 216 int ret = TLBRET_MATCH;
4ef37e69
JH
217 /* effective address (modified for KVM T&E kernel segments) */
218 target_ulong address = real_address;
43057ab1 219
67433345
JH
220#define USEG_LIMIT ((target_ulong)(int32_t)0x7FFFFFFFUL)
221#define KSEG0_BASE ((target_ulong)(int32_t)0x80000000UL)
222#define KSEG1_BASE ((target_ulong)(int32_t)0xA0000000UL)
223#define KSEG2_BASE ((target_ulong)(int32_t)0xC0000000UL)
224#define KSEG3_BASE ((target_ulong)(int32_t)0xE0000000UL)
22010ce7 225
67433345
JH
226#define KVM_KSEG0_BASE ((target_ulong)(int32_t)0x40000000UL)
227#define KVM_KSEG2_BASE ((target_ulong)(int32_t)0x60000000UL)
4ef37e69 228
d3d93c6c 229 if (mips_um_ksegs_enabled()) {
4ef37e69
JH
230 /* KVM T&E adds guest kernel segments in useg */
231 if (real_address >= KVM_KSEG0_BASE) {
232 if (real_address < KVM_KSEG2_BASE) {
233 /* kseg0 */
234 address += KSEG0_BASE - KVM_KSEG0_BASE;
235 } else if (real_address <= USEG_LIMIT) {
236 /* kseg2/3 */
237 address += KSEG2_BASE - KVM_KSEG2_BASE;
238 }
239 }
240 }
241
22010ce7 242 if (address <= USEG_LIMIT) {
b4ab4b4e 243 /* useg */
480e79ae
JH
244 uint16_t segctl;
245
246 if (address >= 0x40000000UL) {
247 segctl = env->CP0_SegCtl2;
996ba2cc 248 } else {
480e79ae 249 segctl = env->CP0_SegCtl2 >> 16;
6af0bf9c 250 }
480e79ae
JH
251 ret = get_segctl_physical_address(env, physical, prot, real_address, rw,
252 access_type, mmu_idx, segctl,
253 0x3FFFFFFF);
d26bc211 254#if defined(TARGET_MIPS64)
89fc88da 255 } else if (address < 0x4000000000000000ULL) {
b4ab4b4e 256 /* xuseg */
6958549d 257 if (UX && address <= (0x3FFFFFFFFFFFFFFFULL & env->SEGMask)) {
4ef37e69 258 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
6958549d
AJ
259 } else {
260 ret = TLBRET_BADADDR;
b4ab4b4e 261 }
89fc88da 262 } else if (address < 0x8000000000000000ULL) {
b4ab4b4e 263 /* xsseg */
6958549d
AJ
264 if ((supervisor_mode || kernel_mode) &&
265 SX && address <= (0x7FFFFFFFFFFFFFFFULL & env->SEGMask)) {
4ef37e69 266 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
6958549d
AJ
267 } else {
268 ret = TLBRET_BADADDR;
b4ab4b4e 269 }
89fc88da 270 } else if (address < 0xC000000000000000ULL) {
b4ab4b4e 271 /* xkphys */
480e79ae
JH
272 if ((address & 0x07FFFFFFFFFFFFFFULL) <= env->PAMask) {
273 /* KX/SX/UX bit to check for each xkphys EVA access mode */
274 static const uint8_t am_ksux[8] = {
275 [CP0SC_AM_UK] = (1u << CP0St_KX),
276 [CP0SC_AM_MK] = (1u << CP0St_KX),
277 [CP0SC_AM_MSK] = (1u << CP0St_SX),
278 [CP0SC_AM_MUSK] = (1u << CP0St_UX),
279 [CP0SC_AM_MUSUK] = (1u << CP0St_UX),
280 [CP0SC_AM_USK] = (1u << CP0St_SX),
281 [6] = (1u << CP0St_KX),
282 [CP0SC_AM_UUSK] = (1u << CP0St_UX),
283 };
284 unsigned int am = CP0SC_AM_UK;
285 unsigned int xr = (env->CP0_SegCtl2 & CP0SC2_XR_MASK) >> CP0SC2_XR;
286
287 if (xr & (1 << ((address >> 59) & 0x7))) {
288 am = (env->CP0_SegCtl1 & CP0SC1_XAM_MASK) >> CP0SC1_XAM;
289 }
290 /* Does CP0_Status.KX/SX/UX permit the access mode (am) */
291 if (env->CP0_Status & am_ksux[am]) {
292 ret = get_seg_physical_address(env, physical, prot,
293 real_address, rw, access_type,
294 mmu_idx, am, false, env->PAMask,
295 0);
296 } else {
297 ret = TLBRET_BADADDR;
298 }
6958549d
AJ
299 } else {
300 ret = TLBRET_BADADDR;
301 }
89fc88da 302 } else if (address < 0xFFFFFFFF80000000ULL) {
b4ab4b4e 303 /* xkseg */
6958549d
AJ
304 if (kernel_mode && KX &&
305 address <= (0xFFFFFFFF7FFFFFFFULL & env->SEGMask)) {
4ef37e69 306 ret = env->tlb->map_address(env, physical, prot, real_address, rw, access_type);
6958549d
AJ
307 } else {
308 ret = TLBRET_BADADDR;
309 }
b4ab4b4e 310#endif
67433345 311 } else if (address < KSEG1_BASE) {
6af0bf9c 312 /* kseg0 */
480e79ae
JH
313 ret = get_segctl_physical_address(env, physical, prot, real_address, rw,
314 access_type, mmu_idx,
315 env->CP0_SegCtl1 >> 16, 0x1FFFFFFF);
67433345 316 } else if (address < KSEG2_BASE) {
6af0bf9c 317 /* kseg1 */
480e79ae
JH
318 ret = get_segctl_physical_address(env, physical, prot, real_address, rw,
319 access_type, mmu_idx,
320 env->CP0_SegCtl1, 0x1FFFFFFF);
67433345 321 } else if (address < KSEG3_BASE) {
89fc88da 322 /* sseg (kseg2) */
480e79ae
JH
323 ret = get_segctl_physical_address(env, physical, prot, real_address, rw,
324 access_type, mmu_idx,
325 env->CP0_SegCtl0 >> 16, 0x1FFFFFFF);
6af0bf9c
FB
326 } else {
327 /* kseg3 */
6af0bf9c 328 /* XXX: debug segment is not emulated */
480e79ae
JH
329 ret = get_segctl_physical_address(env, physical, prot, real_address, rw,
330 access_type, mmu_idx,
331 env->CP0_SegCtl0, 0x1FFFFFFF);
6af0bf9c 332 }
6af0bf9c
FB
333 return ret;
334}
e6623d88 335
d10eb08f 336void cpu_mips_tlb_flush(CPUMIPSState *env)
e6623d88
PB
337{
338 MIPSCPU *cpu = mips_env_get_cpu(env);
339
340 /* Flush qemu's TLB and discard all shadowed entries. */
d10eb08f 341 tlb_flush(CPU(cpu));
e6623d88
PB
342 env->tlb->tlb_in_use = env->tlb->nb_tlb;
343}
344
345/* Called for updates to CP0_Status. */
346void sync_c0_status(CPUMIPSState *env, CPUMIPSState *cpu, int tc)
347{
348 int32_t tcstatus, *tcst;
349 uint32_t v = cpu->CP0_Status;
350 uint32_t cu, mx, asid, ksu;
351 uint32_t mask = ((1 << CP0TCSt_TCU3)
352 | (1 << CP0TCSt_TCU2)
353 | (1 << CP0TCSt_TCU1)
354 | (1 << CP0TCSt_TCU0)
355 | (1 << CP0TCSt_TMX)
356 | (3 << CP0TCSt_TKSU)
357 | (0xff << CP0TCSt_TASID));
358
359 cu = (v >> CP0St_CU0) & 0xf;
360 mx = (v >> CP0St_MX) & 0x1;
361 ksu = (v >> CP0St_KSU) & 0x3;
6ec98bd7 362 asid = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask;
e6623d88
PB
363
364 tcstatus = cu << CP0TCSt_TCU0;
365 tcstatus |= mx << CP0TCSt_TMX;
366 tcstatus |= ksu << CP0TCSt_TKSU;
367 tcstatus |= asid;
368
369 if (tc == cpu->current_tc) {
370 tcst = &cpu->active_tc.CP0_TCStatus;
371 } else {
372 tcst = &cpu->tcs[tc].CP0_TCStatus;
373 }
374
375 *tcst &= ~mask;
376 *tcst |= tcstatus;
377 compute_hflags(cpu);
378}
379
380void cpu_mips_store_status(CPUMIPSState *env, target_ulong val)
381{
382 uint32_t mask = env->CP0_Status_rw_bitmask;
383 target_ulong old = env->CP0_Status;
384
385 if (env->insn_flags & ISA_MIPS32R6) {
386 bool has_supervisor = extract32(mask, CP0St_KSU, 2) == 0x3;
387#if defined(TARGET_MIPS64)
388 uint32_t ksux = (1 << CP0St_KX) & val;
389 ksux |= (ksux >> 1) & val; /* KX = 0 forces SX to be 0 */
390 ksux |= (ksux >> 1) & val; /* SX = 0 forces UX to be 0 */
391 val = (val & ~(7 << CP0St_UX)) | ksux;
392#endif
393 if (has_supervisor && extract32(val, CP0St_KSU, 2) == 0x3) {
394 mask &= ~(3 << CP0St_KSU);
395 }
396 mask &= ~(((1 << CP0St_SR) | (1 << CP0St_NMI)) & val);
397 }
398
399 env->CP0_Status = (old & ~mask) | (val & mask);
400#if defined(TARGET_MIPS64)
401 if ((env->CP0_Status ^ old) & (old & (7 << CP0St_UX))) {
402 /* Access to at least one of the 64-bit segments has been disabled */
9658e4c3 403 tlb_flush(CPU(mips_env_get_cpu(env)));
e6623d88
PB
404 }
405#endif
406 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
407 sync_c0_status(env, env, env->current_tc);
408 } else {
409 compute_hflags(env);
410 }
411}
412
413void cpu_mips_store_cause(CPUMIPSState *env, target_ulong val)
414{
415 uint32_t mask = 0x00C00300;
416 uint32_t old = env->CP0_Cause;
417 int i;
418
419 if (env->insn_flags & ISA_MIPS32R2) {
420 mask |= 1 << CP0Ca_DC;
421 }
422 if (env->insn_flags & ISA_MIPS32R6) {
423 mask &= ~((1 << CP0Ca_WP) & val);
424 }
425
426 env->CP0_Cause = (env->CP0_Cause & ~mask) | (val & mask);
427
428 if ((old ^ env->CP0_Cause) & (1 << CP0Ca_DC)) {
429 if (env->CP0_Cause & (1 << CP0Ca_DC)) {
430 cpu_mips_stop_count(env);
431 } else {
432 cpu_mips_start_count(env);
433 }
434 }
435
436 /* Set/reset software interrupts */
437 for (i = 0 ; i < 2 ; i++) {
438 if ((old ^ env->CP0_Cause) & (1 << (CP0Ca_IP + i))) {
439 cpu_mips_soft_irq(env, i, env->CP0_Cause & (1 << (CP0Ca_IP + i)));
440 }
441 }
442}
932e71cd 443#endif
6af0bf9c 444
7db13fae 445static void raise_mmu_exception(CPUMIPSState *env, target_ulong address,
1147e189
AJ
446 int rw, int tlb_error)
447{
27103424 448 CPUState *cs = CPU(mips_env_get_cpu(env));
1147e189
AJ
449 int exception = 0, error_code = 0;
450
aea14095
LA
451 if (rw == MMU_INST_FETCH) {
452 error_code |= EXCP_INST_NOTAVAIL;
453 }
454
1147e189
AJ
455 switch (tlb_error) {
456 default:
457 case TLBRET_BADADDR:
458 /* Reference to kernel address from user mode or supervisor mode */
459 /* Reference to supervisor address from user mode */
9f6bcedb 460 if (rw == MMU_DATA_STORE) {
1147e189 461 exception = EXCP_AdES;
9f6bcedb 462 } else {
1147e189 463 exception = EXCP_AdEL;
9f6bcedb 464 }
1147e189
AJ
465 break;
466 case TLBRET_NOMATCH:
467 /* No TLB match for a mapped address */
9f6bcedb 468 if (rw == MMU_DATA_STORE) {
1147e189 469 exception = EXCP_TLBS;
9f6bcedb 470 } else {
1147e189 471 exception = EXCP_TLBL;
9f6bcedb 472 }
aea14095 473 error_code |= EXCP_TLB_NOMATCH;
1147e189
AJ
474 break;
475 case TLBRET_INVALID:
476 /* TLB match with no valid bit */
9f6bcedb 477 if (rw == MMU_DATA_STORE) {
1147e189 478 exception = EXCP_TLBS;
9f6bcedb 479 } else {
1147e189 480 exception = EXCP_TLBL;
9f6bcedb 481 }
1147e189
AJ
482 break;
483 case TLBRET_DIRTY:
484 /* TLB match but 'D' bit is cleared */
485 exception = EXCP_LTLBL;
486 break;
92ceb440
LA
487 case TLBRET_XI:
488 /* Execute-Inhibit Exception */
489 if (env->CP0_PageGrain & (1 << CP0PG_IEC)) {
490 exception = EXCP_TLBXI;
491 } else {
492 exception = EXCP_TLBL;
493 }
494 break;
495 case TLBRET_RI:
496 /* Read-Inhibit Exception */
497 if (env->CP0_PageGrain & (1 << CP0PG_IEC)) {
498 exception = EXCP_TLBRI;
499 } else {
500 exception = EXCP_TLBL;
501 }
502 break;
1147e189
AJ
503 }
504 /* Raise exception */
e807bcc1
YK
505 if (!(env->hflags & MIPS_HFLAG_DM)) {
506 env->CP0_BadVAddr = address;
507 }
1147e189
AJ
508 env->CP0_Context = (env->CP0_Context & ~0x007fffff) |
509 ((address >> 9) & 0x007ffff0);
6ec98bd7 510 env->CP0_EntryHi = (env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask) |
701074a6 511 (env->CP0_EntryHi & (1 << CP0EnHi_EHINV)) |
6ec98bd7 512 (address & (TARGET_PAGE_MASK << 1));
1147e189
AJ
513#if defined(TARGET_MIPS64)
514 env->CP0_EntryHi &= env->SEGMask;
60270f85
YK
515 env->CP0_XContext =
516 /* PTEBase */ (env->CP0_XContext & ((~0ULL) << (env->SEGBITS - 7))) |
517 /* R */ (extract64(address, 62, 2) << (env->SEGBITS - 9)) |
518 /* BadVPN2 */ (extract64(address, 13, env->SEGBITS - 13) << 4);
1147e189 519#endif
27103424 520 cs->exception_index = exception;
1147e189
AJ
521 env->error_code = error_code;
522}
523
4fcc562b 524#if !defined(CONFIG_USER_ONLY)
00b941e5 525hwaddr mips_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
6af0bf9c 526{
00b941e5 527 MIPSCPU *cpu = MIPS_CPU(cs);
9fbf4a58 528 CPUMIPSState *env = &cpu->env;
a8170e5e 529 hwaddr phys_addr;
932e71cd 530 int prot;
6af0bf9c 531
9fbf4a58
JH
532 if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT,
533 cpu_mmu_index(env, false)) != 0) {
932e71cd 534 return -1;
00b941e5 535 }
932e71cd 536 return phys_addr;
6af0bf9c 537}
4fcc562b 538#endif
6af0bf9c 539
074cfcb4
YK
540#if !defined(CONFIG_USER_ONLY)
541#if !defined(TARGET_MIPS64)
542
543/*
544 * Perform hardware page table walk
545 *
546 * Memory accesses are performed using the KERNEL privilege level.
547 * Synchronous exceptions detected on memory accesses cause a silent exit
548 * from page table walking, resulting in a TLB or XTLB Refill exception.
549 *
550 * Implementations are not required to support page table walk memory
551 * accesses from mapped memory regions. When an unsupported access is
552 * attempted, a silent exit is taken, resulting in a TLB or XTLB Refill
553 * exception.
554 *
555 * Note that if an exception is caused by AddressTranslation or LoadMemory
556 * functions, the exception is not taken, a silent exit is taken,
557 * resulting in a TLB or XTLB Refill exception.
558 */
559
560static bool get_pte(CPUMIPSState *env, uint64_t vaddr, int entry_size,
561 uint64_t *pte)
562{
563 if ((vaddr & ((entry_size >> 3) - 1)) != 0) {
564 return false;
565 }
566 if (entry_size == 64) {
567 *pte = cpu_ldq_code(env, vaddr);
568 } else {
569 *pte = cpu_ldl_code(env, vaddr);
570 }
571 return true;
572}
573
574static uint64_t get_tlb_entry_layout(CPUMIPSState *env, uint64_t entry,
575 int entry_size, int ptei)
576{
577 uint64_t result = entry;
578 uint64_t rixi;
579 if (ptei > entry_size) {
580 ptei -= 32;
581 }
582 result >>= (ptei - 2);
583 rixi = result & 3;
584 result >>= 2;
585 result |= rixi << CP0EnLo_XI;
586 return result;
587}
588
589static int walk_directory(CPUMIPSState *env, uint64_t *vaddr,
590 int directory_index, bool *huge_page, bool *hgpg_directory_hit,
591 uint64_t *pw_entrylo0, uint64_t *pw_entrylo1)
592{
593 int dph = (env->CP0_PWCtl >> CP0PC_DPH) & 0x1;
594 int psn = (env->CP0_PWCtl >> CP0PC_PSN) & 0x3F;
595 int hugepg = (env->CP0_PWCtl >> CP0PC_HUGEPG) & 0x1;
596 int pf_ptew = (env->CP0_PWField >> CP0PF_PTEW) & 0x3F;
597 int ptew = (env->CP0_PWSize >> CP0PS_PTEW) & 0x3F;
598 int native_shift = (((env->CP0_PWSize >> CP0PS_PS) & 1) == 0) ? 2 : 3;
599 int directory_shift = (ptew > 1) ? -1 :
600 (hugepg && (ptew == 1)) ? native_shift + 1 : native_shift;
601 int leaf_shift = (ptew > 1) ? -1 :
602 (ptew == 1) ? native_shift + 1 : native_shift;
603 uint32_t direntry_size = 1 << (directory_shift + 3);
604 uint32_t leafentry_size = 1 << (leaf_shift + 3);
605 uint64_t entry;
606 uint64_t paddr;
607 int prot;
608 uint64_t lsb = 0;
609 uint64_t w = 0;
610
611 if (get_physical_address(env, &paddr, &prot, *vaddr, MMU_DATA_LOAD,
612 ACCESS_INT, cpu_mmu_index(env, false)) !=
613 TLBRET_MATCH) {
614 /* wrong base address */
615 return 0;
616 }
617 if (!get_pte(env, *vaddr, direntry_size, &entry)) {
618 return 0;
619 }
620
621 if ((entry & (1 << psn)) && hugepg) {
622 *huge_page = true;
623 *hgpg_directory_hit = true;
624 entry = get_tlb_entry_layout(env, entry, leafentry_size, pf_ptew);
625 w = directory_index - 1;
626 if (directory_index & 0x1) {
627 /* Generate adjacent page from same PTE for odd TLB page */
628 lsb = (1 << w) >> 6;
629 *pw_entrylo0 = entry & ~lsb; /* even page */
630 *pw_entrylo1 = entry | lsb; /* odd page */
631 } else if (dph) {
632 int oddpagebit = 1 << leaf_shift;
633 uint64_t vaddr2 = *vaddr ^ oddpagebit;
634 if (*vaddr & oddpagebit) {
635 *pw_entrylo1 = entry;
636 } else {
637 *pw_entrylo0 = entry;
638 }
639 if (get_physical_address(env, &paddr, &prot, vaddr2, MMU_DATA_LOAD,
640 ACCESS_INT, cpu_mmu_index(env, false)) !=
641 TLBRET_MATCH) {
642 return 0;
643 }
644 if (!get_pte(env, vaddr2, leafentry_size, &entry)) {
645 return 0;
646 }
647 entry = get_tlb_entry_layout(env, entry, leafentry_size, pf_ptew);
648 if (*vaddr & oddpagebit) {
649 *pw_entrylo0 = entry;
650 } else {
651 *pw_entrylo1 = entry;
652 }
653 } else {
654 return 0;
655 }
656 return 1;
657 } else {
658 *vaddr = entry;
659 return 2;
660 }
661}
662
663static bool page_table_walk_refill(CPUMIPSState *env, vaddr address, int rw,
664 int mmu_idx)
665{
666 int gdw = (env->CP0_PWSize >> CP0PS_GDW) & 0x3F;
667 int udw = (env->CP0_PWSize >> CP0PS_UDW) & 0x3F;
668 int mdw = (env->CP0_PWSize >> CP0PS_MDW) & 0x3F;
669 int ptw = (env->CP0_PWSize >> CP0PS_PTW) & 0x3F;
670 int ptew = (env->CP0_PWSize >> CP0PS_PTEW) & 0x3F;
671
672 /* Initial values */
673 bool huge_page = false;
674 bool hgpg_bdhit = false;
675 bool hgpg_gdhit = false;
676 bool hgpg_udhit = false;
677 bool hgpg_mdhit = false;
678
679 int32_t pw_pagemask = 0;
680 target_ulong pw_entryhi = 0;
681 uint64_t pw_entrylo0 = 0;
682 uint64_t pw_entrylo1 = 0;
683
684 /* Native pointer size */
685 /*For the 32-bit architectures, this bit is fixed to 0.*/
686 int native_shift = (((env->CP0_PWSize >> CP0PS_PS) & 1) == 0) ? 2 : 3;
687
688 /* Indices from PWField */
689 int pf_gdw = (env->CP0_PWField >> CP0PF_GDW) & 0x3F;
690 int pf_udw = (env->CP0_PWField >> CP0PF_UDW) & 0x3F;
691 int pf_mdw = (env->CP0_PWField >> CP0PF_MDW) & 0x3F;
692 int pf_ptw = (env->CP0_PWField >> CP0PF_PTW) & 0x3F;
693 int pf_ptew = (env->CP0_PWField >> CP0PF_PTEW) & 0x3F;
694
695 /* Indices computed from faulting address */
696 int gindex = (address >> pf_gdw) & ((1 << gdw) - 1);
697 int uindex = (address >> pf_udw) & ((1 << udw) - 1);
698 int mindex = (address >> pf_mdw) & ((1 << mdw) - 1);
699 int ptindex = (address >> pf_ptw) & ((1 << ptw) - 1);
700
701 /* Other HTW configs */
702 int hugepg = (env->CP0_PWCtl >> CP0PC_HUGEPG) & 0x1;
703
704 /* HTW Shift values (depend on entry size) */
705 int directory_shift = (ptew > 1) ? -1 :
706 (hugepg && (ptew == 1)) ? native_shift + 1 : native_shift;
707 int leaf_shift = (ptew > 1) ? -1 :
708 (ptew == 1) ? native_shift + 1 : native_shift;
709
710 /* Offsets into tables */
711 int goffset = gindex << directory_shift;
712 int uoffset = uindex << directory_shift;
713 int moffset = mindex << directory_shift;
714 int ptoffset0 = (ptindex >> 1) << (leaf_shift + 1);
715 int ptoffset1 = ptoffset0 | (1 << (leaf_shift));
716
717 uint32_t leafentry_size = 1 << (leaf_shift + 3);
718
719 /* Starting address - Page Table Base */
720 uint64_t vaddr = env->CP0_PWBase;
721
722 uint64_t dir_entry;
723 uint64_t paddr;
724 int prot;
725 int m;
726
727 if (!(env->CP0_Config3 & (1 << CP0C3_PW))) {
728 /* walker is unimplemented */
729 return false;
730 }
731 if (!(env->CP0_PWCtl & (1 << CP0PC_PWEN))) {
732 /* walker is disabled */
733 return false;
734 }
735 if (!(gdw > 0 || udw > 0 || mdw > 0)) {
736 /* no structure to walk */
737 return false;
738 }
739 if ((directory_shift == -1) || (leaf_shift == -1)) {
740 return false;
741 }
742
743 /* Global Directory */
744 if (gdw > 0) {
745 vaddr |= goffset;
746 switch (walk_directory(env, &vaddr, pf_gdw, &huge_page, &hgpg_gdhit,
747 &pw_entrylo0, &pw_entrylo1))
748 {
749 case 0:
750 return false;
751 case 1:
752 goto refill;
753 case 2:
754 default:
755 break;
756 }
757 }
758
759 /* Upper directory */
760 if (udw > 0) {
761 vaddr |= uoffset;
762 switch (walk_directory(env, &vaddr, pf_udw, &huge_page, &hgpg_udhit,
763 &pw_entrylo0, &pw_entrylo1))
764 {
765 case 0:
766 return false;
767 case 1:
768 goto refill;
769 case 2:
770 default:
771 break;
772 }
773 }
774
775 /* Middle directory */
776 if (mdw > 0) {
777 vaddr |= moffset;
778 switch (walk_directory(env, &vaddr, pf_mdw, &huge_page, &hgpg_mdhit,
779 &pw_entrylo0, &pw_entrylo1))
780 {
781 case 0:
782 return false;
783 case 1:
784 goto refill;
785 case 2:
786 default:
787 break;
788 }
789 }
790
791 /* Leaf Level Page Table - First half of PTE pair */
792 vaddr |= ptoffset0;
793 if (get_physical_address(env, &paddr, &prot, vaddr, MMU_DATA_LOAD,
794 ACCESS_INT, cpu_mmu_index(env, false)) !=
795 TLBRET_MATCH) {
796 return false;
797 }
798 if (!get_pte(env, vaddr, leafentry_size, &dir_entry)) {
799 return false;
800 }
801 dir_entry = get_tlb_entry_layout(env, dir_entry, leafentry_size, pf_ptew);
802 pw_entrylo0 = dir_entry;
803
804 /* Leaf Level Page Table - Second half of PTE pair */
805 vaddr |= ptoffset1;
806 if (get_physical_address(env, &paddr, &prot, vaddr, MMU_DATA_LOAD,
807 ACCESS_INT, cpu_mmu_index(env, false)) !=
808 TLBRET_MATCH) {
809 return false;
810 }
811 if (!get_pte(env, vaddr, leafentry_size, &dir_entry)) {
812 return false;
813 }
814 dir_entry = get_tlb_entry_layout(env, dir_entry, leafentry_size, pf_ptew);
815 pw_entrylo1 = dir_entry;
816
817refill:
818
819 m = (1 << pf_ptw) - 1;
820
821 if (huge_page) {
822 switch (hgpg_bdhit << 3 | hgpg_gdhit << 2 | hgpg_udhit << 1 |
823 hgpg_mdhit)
824 {
825 case 4:
826 m = (1 << pf_gdw) - 1;
827 if (pf_gdw & 1) {
828 m >>= 1;
829 }
830 break;
831 case 2:
832 m = (1 << pf_udw) - 1;
833 if (pf_udw & 1) {
834 m >>= 1;
835 }
836 break;
837 case 1:
838 m = (1 << pf_mdw) - 1;
839 if (pf_mdw & 1) {
840 m >>= 1;
841 }
842 break;
843 }
844 }
845 pw_pagemask = m >> 12;
846 update_pagemask(env, pw_pagemask << 13, &pw_pagemask);
847 pw_entryhi = (address & ~0x1fff) | (env->CP0_EntryHi & 0xFF);
848 {
849 target_ulong tmp_entryhi = env->CP0_EntryHi;
850 int32_t tmp_pagemask = env->CP0_PageMask;
851 uint64_t tmp_entrylo0 = env->CP0_EntryLo0;
852 uint64_t tmp_entrylo1 = env->CP0_EntryLo1;
853
854 env->CP0_EntryHi = pw_entryhi;
855 env->CP0_PageMask = pw_pagemask;
856 env->CP0_EntryLo0 = pw_entrylo0;
857 env->CP0_EntryLo1 = pw_entrylo1;
858
859 /*
860 * The hardware page walker inserts a page into the TLB in a manner
861 * identical to a TLBWR instruction as executed by the software refill
862 * handler.
863 */
864 r4k_helper_tlbwr(env);
865
866 env->CP0_EntryHi = tmp_entryhi;
867 env->CP0_PageMask = tmp_pagemask;
868 env->CP0_EntryLo0 = tmp_entrylo0;
869 env->CP0_EntryLo1 = tmp_entrylo1;
870 }
871 return true;
872}
873#endif
874#endif
875
98670d47 876int mips_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size, int rw,
7510454e 877 int mmu_idx)
6af0bf9c 878{
7510454e
AF
879 MIPSCPU *cpu = MIPS_CPU(cs);
880 CPUMIPSState *env = &cpu->env;
932e71cd 881#if !defined(CONFIG_USER_ONLY)
a8170e5e 882 hwaddr physical;
6af0bf9c 883 int prot;
6af0bf9c 884 int access_type;
99e43d36 885#endif
6af0bf9c
FB
886 int ret = 0;
887
4ad40f36 888#if 0
7510454e 889 log_cpu_state(cs, 0);
4ad40f36 890#endif
339aaf5b
AP
891 qemu_log_mask(CPU_LOG_MMU,
892 "%s pc " TARGET_FMT_lx " ad %" VADDR_PRIx " rw %d mmu_idx %d\n",
97b348e7 893 __func__, env->active_tc.PC, address, rw, mmu_idx);
4ad40f36 894
6af0bf9c 895 /* data access */
99e43d36 896#if !defined(CONFIG_USER_ONLY)
074cfcb4 897 /* XXX: put correct access by using cpu_restore_state() correctly */
6af0bf9c 898 access_type = ACCESS_INT;
6af0bf9c 899 ret = get_physical_address(env, &physical, &prot,
9fbf4a58 900 address, rw, access_type, mmu_idx);
def74c0c
PMD
901 switch (ret) {
902 case TLBRET_MATCH:
903 qemu_log_mask(CPU_LOG_MMU,
904 "%s address=%" VADDR_PRIx " physical " TARGET_FMT_plx
905 " prot %d\n", __func__, address, physical, prot);
906 break;
907 default:
908 qemu_log_mask(CPU_LOG_MMU,
909 "%s address=%" VADDR_PRIx " ret %d\n", __func__, address,
910 ret);
911 break;
912 }
43057ab1 913 if (ret == TLBRET_MATCH) {
0c591eb0 914 tlb_set_page(cs, address & TARGET_PAGE_MASK,
99e43d36
AJ
915 physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
916 mmu_idx, TARGET_PAGE_SIZE);
917 ret = 0;
932e71cd
AJ
918 } else if (ret < 0)
919#endif
920 {
074cfcb4
YK
921#if !defined(CONFIG_USER_ONLY)
922#if !defined(TARGET_MIPS64)
923 if ((ret == TLBRET_NOMATCH) && (env->tlb->nb_tlb > 1)) {
924 /*
925 * Memory reads during hardware page table walking are performed
926 * as if they were kernel-mode load instructions.
927 */
928 int mode = (env->hflags & MIPS_HFLAG_KSU);
929 bool ret_walker;
930 env->hflags &= ~MIPS_HFLAG_KSU;
931 ret_walker = page_table_walk_refill(env, address, rw, mmu_idx);
932 env->hflags |= mode;
933 if (ret_walker) {
934 ret = get_physical_address(env, &physical, &prot,
935 address, rw, access_type, mmu_idx);
936 if (ret == TLBRET_MATCH) {
937 tlb_set_page(cs, address & TARGET_PAGE_MASK,
938 physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
939 mmu_idx, TARGET_PAGE_SIZE);
940 ret = 0;
941 return ret;
942 }
943 }
944 }
945#endif
946#endif
1147e189 947 raise_mmu_exception(env, address, rw, ret);
6af0bf9c
FB
948 ret = 1;
949 }
950
951 return ret;
952}
953
25b91e32 954#if !defined(CONFIG_USER_ONLY)
a8170e5e 955hwaddr cpu_mips_translate_address(CPUMIPSState *env, target_ulong address, int rw)
25b91e32 956{
a8170e5e 957 hwaddr physical;
25b91e32
AJ
958 int prot;
959 int access_type;
960 int ret = 0;
961
25b91e32
AJ
962 /* data access */
963 access_type = ACCESS_INT;
9fbf4a58
JH
964 ret = get_physical_address(env, &physical, &prot, address, rw, access_type,
965 cpu_mmu_index(env, false));
25b91e32
AJ
966 if (ret != TLBRET_MATCH) {
967 raise_mmu_exception(env, address, rw, ret);
c36bbb28
AJ
968 return -1LL;
969 } else {
970 return physical;
25b91e32 971 }
25b91e32 972}
25b91e32 973
9a5d878f
TS
974static const char * const excp_names[EXCP_LAST + 1] = {
975 [EXCP_RESET] = "reset",
976 [EXCP_SRESET] = "soft reset",
977 [EXCP_DSS] = "debug single step",
978 [EXCP_DINT] = "debug interrupt",
979 [EXCP_NMI] = "non-maskable interrupt",
980 [EXCP_MCHECK] = "machine check",
981 [EXCP_EXT_INTERRUPT] = "interrupt",
982 [EXCP_DFWATCH] = "deferred watchpoint",
983 [EXCP_DIB] = "debug instruction breakpoint",
984 [EXCP_IWATCH] = "instruction fetch watchpoint",
985 [EXCP_AdEL] = "address error load",
986 [EXCP_AdES] = "address error store",
987 [EXCP_TLBF] = "TLB refill",
988 [EXCP_IBE] = "instruction bus error",
989 [EXCP_DBp] = "debug breakpoint",
990 [EXCP_SYSCALL] = "syscall",
991 [EXCP_BREAK] = "break",
992 [EXCP_CpU] = "coprocessor unusable",
993 [EXCP_RI] = "reserved instruction",
994 [EXCP_OVERFLOW] = "arithmetic overflow",
995 [EXCP_TRAP] = "trap",
996 [EXCP_FPE] = "floating point",
997 [EXCP_DDBS] = "debug data break store",
998 [EXCP_DWATCH] = "data watchpoint",
999 [EXCP_LTLBL] = "TLB modify",
1000 [EXCP_TLBL] = "TLB load",
1001 [EXCP_TLBS] = "TLB store",
1002 [EXCP_DBE] = "data bus error",
1003 [EXCP_DDBL] = "debug data break load",
1004 [EXCP_THREAD] = "thread",
1005 [EXCP_MDMX] = "MDMX",
1006 [EXCP_C2E] = "precise coprocessor 2",
1007 [EXCP_CACHE] = "cache error",
92ceb440
LA
1008 [EXCP_TLBXI] = "TLB execute-inhibit",
1009 [EXCP_TLBRI] = "TLB read-inhibit",
b10ac204
YK
1010 [EXCP_MSADIS] = "MSA disabled",
1011 [EXCP_MSAFPE] = "MSA floating point",
14e51cc7 1012};
d4fa5354 1013#endif
14e51cc7 1014
1239b472 1015target_ulong exception_resume_pc (CPUMIPSState *env)
32188a03
NF
1016{
1017 target_ulong bad_pc;
1018 target_ulong isa_mode;
1019
1020 isa_mode = !!(env->hflags & MIPS_HFLAG_M16);
1021 bad_pc = env->active_tc.PC | isa_mode;
1022 if (env->hflags & MIPS_HFLAG_BMASK) {
1023 /* If the exception was raised from a delay slot, come back to
1024 the jump. */
1025 bad_pc -= (env->hflags & MIPS_HFLAG_B16 ? 2 : 4);
1026 }
1027
1028 return bad_pc;
1029}
bbfa8f72 1030
1239b472 1031#if !defined(CONFIG_USER_ONLY)
7db13fae 1032static void set_hflags_for_handler (CPUMIPSState *env)
bbfa8f72
NF
1033{
1034 /* Exception handlers are entered in 32-bit mode. */
1035 env->hflags &= ~(MIPS_HFLAG_M16);
1036 /* ...except that microMIPS lets you choose. */
1037 if (env->insn_flags & ASE_MICROMIPS) {
1038 env->hflags |= (!!(env->CP0_Config3
1039 & (1 << CP0C3_ISA_ON_EXC))
1040 << MIPS_HFLAG_M16_SHIFT);
1041 }
1042}
aea14095
LA
1043
1044static inline void set_badinstr_registers(CPUMIPSState *env)
1045{
7a5f784a
SM
1046 if (env->insn_flags & ISA_NANOMIPS32) {
1047 if (env->CP0_Config3 & (1 << CP0C3_BI)) {
1048 uint32_t instr = (cpu_lduw_code(env, env->active_tc.PC)) << 16;
1049 if ((instr & 0x10000000) == 0) {
1050 instr |= cpu_lduw_code(env, env->active_tc.PC + 2);
1051 }
1052 env->CP0_BadInstr = instr;
1053
1054 if ((instr & 0xFC000000) == 0x60000000) {
1055 instr = cpu_lduw_code(env, env->active_tc.PC + 4) << 16;
1056 env->CP0_BadInstrX = instr;
1057 }
1058 }
1059 return;
1060 }
1061
aea14095
LA
1062 if (env->hflags & MIPS_HFLAG_M16) {
1063 /* TODO: add BadInstr support for microMIPS */
1064 return;
1065 }
1066 if (env->CP0_Config3 & (1 << CP0C3_BI)) {
1067 env->CP0_BadInstr = cpu_ldl_code(env, env->active_tc.PC);
1068 }
1069 if ((env->CP0_Config3 & (1 << CP0C3_BP)) &&
1070 (env->hflags & MIPS_HFLAG_BMASK)) {
1071 env->CP0_BadInstrP = cpu_ldl_code(env, env->active_tc.PC - 4);
1072 }
1073}
32188a03
NF
1074#endif
1075
97a8ea5a 1076void mips_cpu_do_interrupt(CPUState *cs)
6af0bf9c 1077{
27103424 1078#if !defined(CONFIG_USER_ONLY)
97a8ea5a
AF
1079 MIPSCPU *cpu = MIPS_CPU(cs);
1080 CPUMIPSState *env = &cpu->env;
aea14095 1081 bool update_badinstr = 0;
932e71cd
AJ
1082 target_ulong offset;
1083 int cause = -1;
1084 const char *name;
100ce988 1085
c8557016
RH
1086 if (qemu_loglevel_mask(CPU_LOG_INT)
1087 && cs->exception_index != EXCP_EXT_INTERRUPT) {
27103424 1088 if (cs->exception_index < 0 || cs->exception_index > EXCP_LAST) {
932e71cd 1089 name = "unknown";
27103424
AF
1090 } else {
1091 name = excp_names[cs->exception_index];
1092 }
b67bfe8d 1093
c8557016
RH
1094 qemu_log("%s enter: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx
1095 " %s exception\n",
93fcfe39 1096 __func__, env->active_tc.PC, env->CP0_EPC, name);
932e71cd 1097 }
27103424
AF
1098 if (cs->exception_index == EXCP_EXT_INTERRUPT &&
1099 (env->hflags & MIPS_HFLAG_DM)) {
1100 cs->exception_index = EXCP_DINT;
1101 }
932e71cd 1102 offset = 0x180;
27103424 1103 switch (cs->exception_index) {
932e71cd
AJ
1104 case EXCP_DSS:
1105 env->CP0_Debug |= 1 << CP0DB_DSS;
1106 /* Debug single step cannot be raised inside a delay slot and
1107 resume will always occur on the next instruction
1108 (but we assume the pc has always been updated during
1109 code translation). */
32188a03 1110 env->CP0_DEPC = env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16);
932e71cd
AJ
1111 goto enter_debug_mode;
1112 case EXCP_DINT:
1113 env->CP0_Debug |= 1 << CP0DB_DINT;
1114 goto set_DEPC;
1115 case EXCP_DIB:
1116 env->CP0_Debug |= 1 << CP0DB_DIB;
1117 goto set_DEPC;
1118 case EXCP_DBp:
1119 env->CP0_Debug |= 1 << CP0DB_DBp;
c6c2c0fc
PD
1120 /* Setup DExcCode - SDBBP instruction */
1121 env->CP0_Debug = (env->CP0_Debug & ~(0x1fULL << CP0DB_DEC)) | 9 << CP0DB_DEC;
932e71cd
AJ
1122 goto set_DEPC;
1123 case EXCP_DDBS:
1124 env->CP0_Debug |= 1 << CP0DB_DDBS;
1125 goto set_DEPC;
1126 case EXCP_DDBL:
1127 env->CP0_Debug |= 1 << CP0DB_DDBL;
1128 set_DEPC:
32188a03
NF
1129 env->CP0_DEPC = exception_resume_pc(env);
1130 env->hflags &= ~MIPS_HFLAG_BMASK;
0eaef5aa 1131 enter_debug_mode:
d9224450
MR
1132 if (env->insn_flags & ISA_MIPS3) {
1133 env->hflags |= MIPS_HFLAG_64;
7871abb9
JH
1134 if (!(env->insn_flags & ISA_MIPS64R6) ||
1135 env->CP0_Status & (1 << CP0St_KX)) {
1136 env->hflags &= ~MIPS_HFLAG_AWRAP;
1137 }
d9224450
MR
1138 }
1139 env->hflags |= MIPS_HFLAG_DM | MIPS_HFLAG_CP0;
932e71cd
AJ
1140 env->hflags &= ~(MIPS_HFLAG_KSU);
1141 /* EJTAG probe trap enable is not implemented... */
1142 if (!(env->CP0_Status & (1 << CP0St_EXL)))
f45cb2f4 1143 env->CP0_Cause &= ~(1U << CP0Ca_BD);
89777fd1 1144 env->active_tc.PC = env->exception_base + 0x480;
bbfa8f72 1145 set_hflags_for_handler(env);
932e71cd
AJ
1146 break;
1147 case EXCP_RESET:
fca1be7c 1148 cpu_reset(CPU(cpu));
932e71cd
AJ
1149 break;
1150 case EXCP_SRESET:
1151 env->CP0_Status |= (1 << CP0St_SR);
9d989c73 1152 memset(env->CP0_WatchLo, 0, sizeof(env->CP0_WatchLo));
932e71cd
AJ
1153 goto set_error_EPC;
1154 case EXCP_NMI:
1155 env->CP0_Status |= (1 << CP0St_NMI);
0eaef5aa 1156 set_error_EPC:
32188a03
NF
1157 env->CP0_ErrorEPC = exception_resume_pc(env);
1158 env->hflags &= ~MIPS_HFLAG_BMASK;
932e71cd 1159 env->CP0_Status |= (1 << CP0St_ERL) | (1 << CP0St_BEV);
d9224450
MR
1160 if (env->insn_flags & ISA_MIPS3) {
1161 env->hflags |= MIPS_HFLAG_64;
7871abb9
JH
1162 if (!(env->insn_flags & ISA_MIPS64R6) ||
1163 env->CP0_Status & (1 << CP0St_KX)) {
1164 env->hflags &= ~MIPS_HFLAG_AWRAP;
1165 }
d9224450
MR
1166 }
1167 env->hflags |= MIPS_HFLAG_CP0;
932e71cd
AJ
1168 env->hflags &= ~(MIPS_HFLAG_KSU);
1169 if (!(env->CP0_Status & (1 << CP0St_EXL)))
f45cb2f4 1170 env->CP0_Cause &= ~(1U << CP0Ca_BD);
89777fd1 1171 env->active_tc.PC = env->exception_base;
bbfa8f72 1172 set_hflags_for_handler(env);
932e71cd
AJ
1173 break;
1174 case EXCP_EXT_INTERRUPT:
1175 cause = 0;
da52a4df
YK
1176 if (env->CP0_Cause & (1 << CP0Ca_IV)) {
1177 uint32_t spacing = (env->CP0_IntCtl >> CP0IntCtl_VS) & 0x1f;
1178
1179 if ((env->CP0_Status & (1 << CP0St_BEV)) || spacing == 0) {
1180 offset = 0x200;
1181 } else {
1182 uint32_t vector = 0;
1183 uint32_t pending = (env->CP0_Cause & CP0Ca_IP_mask) >> CP0Ca_IP;
1184
1185 if (env->CP0_Config3 & (1 << CP0C3_VEIC)) {
1186 /* For VEIC mode, the external interrupt controller feeds
1187 * the vector through the CP0Cause IP lines. */
1188 vector = pending;
1189 } else {
1190 /* Vectored Interrupts
1191 * Mask with Status.IM7-IM0 to get enabled interrupts. */
1192 pending &= (env->CP0_Status >> CP0St_IM) & 0xff;
1193 /* Find the highest-priority interrupt. */
1194 while (pending >>= 1) {
1195 vector++;
138afb02 1196 }
138afb02 1197 }
da52a4df 1198 offset = 0x200 + (vector * (spacing << 5));
138afb02 1199 }
138afb02 1200 }
932e71cd
AJ
1201 goto set_EPC;
1202 case EXCP_LTLBL:
1203 cause = 1;
aea14095 1204 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
932e71cd
AJ
1205 goto set_EPC;
1206 case EXCP_TLBL:
1207 cause = 2;
aea14095
LA
1208 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
1209 if ((env->error_code & EXCP_TLB_NOMATCH) &&
1210 !(env->CP0_Status & (1 << CP0St_EXL))) {
0eaef5aa 1211#if defined(TARGET_MIPS64)
932e71cd
AJ
1212 int R = env->CP0_BadVAddr >> 62;
1213 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
932e71cd 1214 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
0eaef5aa 1215
480e79ae
JH
1216 if ((R != 0 || UX) && (R != 3 || KX) &&
1217 (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F)))) {
932e71cd 1218 offset = 0x080;
480e79ae 1219 } else {
0eaef5aa 1220#endif
932e71cd 1221 offset = 0x000;
480e79ae
JH
1222#if defined(TARGET_MIPS64)
1223 }
1224#endif
932e71cd
AJ
1225 }
1226 goto set_EPC;
1227 case EXCP_TLBS:
1228 cause = 3;
aea14095
LA
1229 update_badinstr = 1;
1230 if ((env->error_code & EXCP_TLB_NOMATCH) &&
1231 !(env->CP0_Status & (1 << CP0St_EXL))) {
0eaef5aa 1232#if defined(TARGET_MIPS64)
932e71cd
AJ
1233 int R = env->CP0_BadVAddr >> 62;
1234 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
932e71cd 1235 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
0eaef5aa 1236
480e79ae
JH
1237 if ((R != 0 || UX) && (R != 3 || KX) &&
1238 (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F)))) {
932e71cd 1239 offset = 0x080;
480e79ae 1240 } else {
0eaef5aa 1241#endif
932e71cd 1242 offset = 0x000;
480e79ae
JH
1243#if defined(TARGET_MIPS64)
1244 }
1245#endif
932e71cd
AJ
1246 }
1247 goto set_EPC;
1248 case EXCP_AdEL:
1249 cause = 4;
aea14095 1250 update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL);
932e71cd
AJ
1251 goto set_EPC;
1252 case EXCP_AdES:
1253 cause = 5;
aea14095 1254 update_badinstr = 1;
932e71cd
AJ
1255 goto set_EPC;
1256 case EXCP_IBE:
1257 cause = 6;
1258 goto set_EPC;
1259 case EXCP_DBE:
1260 cause = 7;
1261 goto set_EPC;
1262 case EXCP_SYSCALL:
1263 cause = 8;
aea14095 1264 update_badinstr = 1;
932e71cd
AJ
1265 goto set_EPC;
1266 case EXCP_BREAK:
1267 cause = 9;
aea14095 1268 update_badinstr = 1;
932e71cd
AJ
1269 goto set_EPC;
1270 case EXCP_RI:
1271 cause = 10;
aea14095 1272 update_badinstr = 1;
932e71cd
AJ
1273 goto set_EPC;
1274 case EXCP_CpU:
1275 cause = 11;
aea14095 1276 update_badinstr = 1;
932e71cd
AJ
1277 env->CP0_Cause = (env->CP0_Cause & ~(0x3 << CP0Ca_CE)) |
1278 (env->error_code << CP0Ca_CE);
1279 goto set_EPC;
1280 case EXCP_OVERFLOW:
1281 cause = 12;
aea14095 1282 update_badinstr = 1;
932e71cd
AJ
1283 goto set_EPC;
1284 case EXCP_TRAP:
1285 cause = 13;
aea14095 1286 update_badinstr = 1;
932e71cd 1287 goto set_EPC;
b10ac204
YK
1288 case EXCP_MSAFPE:
1289 cause = 14;
1290 update_badinstr = 1;
1291 goto set_EPC;
932e71cd
AJ
1292 case EXCP_FPE:
1293 cause = 15;
aea14095 1294 update_badinstr = 1;
932e71cd
AJ
1295 goto set_EPC;
1296 case EXCP_C2E:
1297 cause = 18;
1298 goto set_EPC;
92ceb440
LA
1299 case EXCP_TLBRI:
1300 cause = 19;
aea14095 1301 update_badinstr = 1;
92ceb440
LA
1302 goto set_EPC;
1303 case EXCP_TLBXI:
1304 cause = 20;
1305 goto set_EPC;
b10ac204
YK
1306 case EXCP_MSADIS:
1307 cause = 21;
1308 update_badinstr = 1;
1309 goto set_EPC;
932e71cd
AJ
1310 case EXCP_MDMX:
1311 cause = 22;
1312 goto set_EPC;
1313 case EXCP_DWATCH:
1314 cause = 23;
67cc32eb 1315 /* XXX: TODO: manage deferred watch exceptions */
932e71cd
AJ
1316 goto set_EPC;
1317 case EXCP_MCHECK:
1318 cause = 24;
1319 goto set_EPC;
1320 case EXCP_THREAD:
1321 cause = 25;
1322 goto set_EPC;
853c3240
JL
1323 case EXCP_DSPDIS:
1324 cause = 26;
1325 goto set_EPC;
932e71cd
AJ
1326 case EXCP_CACHE:
1327 cause = 30;
74dbf824 1328 offset = 0x100;
0eaef5aa 1329 set_EPC:
932e71cd 1330 if (!(env->CP0_Status & (1 << CP0St_EXL))) {
32188a03 1331 env->CP0_EPC = exception_resume_pc(env);
aea14095
LA
1332 if (update_badinstr) {
1333 set_badinstr_registers(env);
1334 }
932e71cd 1335 if (env->hflags & MIPS_HFLAG_BMASK) {
f45cb2f4 1336 env->CP0_Cause |= (1U << CP0Ca_BD);
0eaef5aa 1337 } else {
f45cb2f4 1338 env->CP0_Cause &= ~(1U << CP0Ca_BD);
0eaef5aa 1339 }
932e71cd 1340 env->CP0_Status |= (1 << CP0St_EXL);
d9224450
MR
1341 if (env->insn_flags & ISA_MIPS3) {
1342 env->hflags |= MIPS_HFLAG_64;
7871abb9
JH
1343 if (!(env->insn_flags & ISA_MIPS64R6) ||
1344 env->CP0_Status & (1 << CP0St_KX)) {
1345 env->hflags &= ~MIPS_HFLAG_AWRAP;
1346 }
d9224450
MR
1347 }
1348 env->hflags |= MIPS_HFLAG_CP0;
932e71cd 1349 env->hflags &= ~(MIPS_HFLAG_KSU);
6af0bf9c 1350 }
932e71cd
AJ
1351 env->hflags &= ~MIPS_HFLAG_BMASK;
1352 if (env->CP0_Status & (1 << CP0St_BEV)) {
89777fd1 1353 env->active_tc.PC = env->exception_base + 0x200;
74dbf824
JH
1354 } else if (cause == 30 && !(env->CP0_Config3 & (1 << CP0C3_SC) &&
1355 env->CP0_Config5 & (1 << CP0C5_CV))) {
1356 /* Force KSeg1 for cache errors */
67433345 1357 env->active_tc.PC = KSEG1_BASE | (env->CP0_EBase & 0x1FFFF000);
932e71cd 1358 } else {
74dbf824 1359 env->active_tc.PC = env->CP0_EBase & ~0xfff;
6af0bf9c 1360 }
74dbf824 1361
932e71cd 1362 env->active_tc.PC += offset;
bbfa8f72 1363 set_hflags_for_handler(env);
932e71cd
AJ
1364 env->CP0_Cause = (env->CP0_Cause & ~(0x1f << CP0Ca_EC)) | (cause << CP0Ca_EC);
1365 break;
1366 default:
c8557016 1367 abort();
932e71cd 1368 }
c8557016
RH
1369 if (qemu_loglevel_mask(CPU_LOG_INT)
1370 && cs->exception_index != EXCP_EXT_INTERRUPT) {
93fcfe39 1371 qemu_log("%s: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " cause %d\n"
c8557016
RH
1372 " S %08x C %08x A " TARGET_FMT_lx " D " TARGET_FMT_lx "\n",
1373 __func__, env->active_tc.PC, env->CP0_EPC, cause,
1374 env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr,
1375 env->CP0_DEPC);
6af0bf9c 1376 }
932e71cd 1377#endif
27103424 1378 cs->exception_index = EXCP_NONE;
6af0bf9c 1379}
2ee4aed8 1380
fa4faba4
RH
1381bool mips_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
1382{
1383 if (interrupt_request & CPU_INTERRUPT_HARD) {
1384 MIPSCPU *cpu = MIPS_CPU(cs);
1385 CPUMIPSState *env = &cpu->env;
1386
71ca034a
LA
1387 if (cpu_mips_hw_interrupts_enabled(env) &&
1388 cpu_mips_hw_interrupts_pending(env)) {
fa4faba4
RH
1389 /* Raise it */
1390 cs->exception_index = EXCP_EXT_INTERRUPT;
1391 env->error_code = 0;
1392 mips_cpu_do_interrupt(cs);
1393 return true;
1394 }
1395 }
1396 return false;
1397}
1398
3c7b48b7 1399#if !defined(CONFIG_USER_ONLY)
7db13fae 1400void r4k_invalidate_tlb (CPUMIPSState *env, int idx, int use_extra)
2ee4aed8 1401{
31b030d4
AF
1402 MIPSCPU *cpu = mips_env_get_cpu(env);
1403 CPUState *cs;
c227f099 1404 r4k_tlb_t *tlb;
3b1c8be4
TS
1405 target_ulong addr;
1406 target_ulong end;
2d72e7b0 1407 uint16_t ASID = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask;
3b1c8be4 1408 target_ulong mask;
2ee4aed8 1409
ead9360e 1410 tlb = &env->tlb->mmu.r4k.tlb[idx];
f2e9ebef 1411 /* The qemu TLB is flushed when the ASID changes, so no need to
2ee4aed8
FB
1412 flush these entries again. */
1413 if (tlb->G == 0 && tlb->ASID != ASID) {
1414 return;
1415 }
1416
ead9360e 1417 if (use_extra && env->tlb->tlb_in_use < MIPS_TLB_MAX) {
2ee4aed8 1418 /* For tlbwr, we can shadow the discarded entry into
6958549d
AJ
1419 a new (fake) TLB entry, as long as the guest can not
1420 tell that it's there. */
ead9360e
TS
1421 env->tlb->mmu.r4k.tlb[env->tlb->tlb_in_use] = *tlb;
1422 env->tlb->tlb_in_use++;
2ee4aed8
FB
1423 return;
1424 }
1425
3b1c8be4 1426 /* 1k pages are not supported. */
f2e9ebef 1427 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
3b1c8be4 1428 if (tlb->V0) {
31b030d4 1429 cs = CPU(cpu);
f2e9ebef 1430 addr = tlb->VPN & ~mask;
d26bc211 1431#if defined(TARGET_MIPS64)
e034e2c3 1432 if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) {
100ce988
TS
1433 addr |= 0x3FFFFF0000000000ULL;
1434 }
1435#endif
3b1c8be4
TS
1436 end = addr | (mask >> 1);
1437 while (addr < end) {
31b030d4 1438 tlb_flush_page(cs, addr);
3b1c8be4
TS
1439 addr += TARGET_PAGE_SIZE;
1440 }
1441 }
1442 if (tlb->V1) {
31b030d4 1443 cs = CPU(cpu);
f2e9ebef 1444 addr = (tlb->VPN & ~mask) | ((mask >> 1) + 1);
d26bc211 1445#if defined(TARGET_MIPS64)
e034e2c3 1446 if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) {
100ce988
TS
1447 addr |= 0x3FFFFF0000000000ULL;
1448 }
1449#endif
3b1c8be4 1450 end = addr | mask;
53715e48 1451 while (addr - 1 < end) {
31b030d4 1452 tlb_flush_page(cs, addr);
3b1c8be4
TS
1453 addr += TARGET_PAGE_SIZE;
1454 }
1455 }
2ee4aed8 1456}
3c7b48b7 1457#endif
33c11879
PB
1458
1459void QEMU_NORETURN do_raise_exception_err(CPUMIPSState *env,
1460 uint32_t exception,
1461 int error_code,
1462 uintptr_t pc)
1463{
1464 CPUState *cs = CPU(mips_env_get_cpu(env));
1465
1466 if (exception < EXCP_SC) {
1467 qemu_log_mask(CPU_LOG_INT, "%s: %d %d\n",
1468 __func__, exception, error_code);
1469 }
1470 cs->exception_index = exception;
1471 env->error_code = error_code;
1472
1473 cpu_loop_exit_restore(cs, pc);
1474}