]>
Commit | Line | Data |
---|---|---|
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 | 28 | enum { |
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 | 41 | int 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 | 50 | int 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 | 68 | int 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 |
111 | static 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 | ||
165 | static 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 | ||
189 | static 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 | 203 | static 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 | 336 | void 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. */ | |
346 | void 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 | ||
380 | void 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 | ||
413 | void 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 | 445 | static 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 | 525 | hwaddr 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 | |
98670d47 | 540 | int mips_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size, int rw, |
7510454e | 541 | int mmu_idx) |
6af0bf9c | 542 | { |
7510454e AF |
543 | MIPSCPU *cpu = MIPS_CPU(cs); |
544 | CPUMIPSState *env = &cpu->env; | |
932e71cd | 545 | #if !defined(CONFIG_USER_ONLY) |
a8170e5e | 546 | hwaddr physical; |
6af0bf9c | 547 | int prot; |
6af0bf9c | 548 | int access_type; |
99e43d36 | 549 | #endif |
6af0bf9c FB |
550 | int ret = 0; |
551 | ||
4ad40f36 | 552 | #if 0 |
7510454e | 553 | log_cpu_state(cs, 0); |
4ad40f36 | 554 | #endif |
339aaf5b AP |
555 | qemu_log_mask(CPU_LOG_MMU, |
556 | "%s pc " TARGET_FMT_lx " ad %" VADDR_PRIx " rw %d mmu_idx %d\n", | |
97b348e7 | 557 | __func__, env->active_tc.PC, address, rw, mmu_idx); |
4ad40f36 | 558 | |
6af0bf9c | 559 | /* data access */ |
99e43d36 | 560 | #if !defined(CONFIG_USER_ONLY) |
6af0bf9c FB |
561 | /* XXX: put correct access by using cpu_restore_state() |
562 | correctly */ | |
563 | access_type = ACCESS_INT; | |
6af0bf9c | 564 | ret = get_physical_address(env, &physical, &prot, |
9fbf4a58 | 565 | address, rw, access_type, mmu_idx); |
def74c0c PMD |
566 | switch (ret) { |
567 | case TLBRET_MATCH: | |
568 | qemu_log_mask(CPU_LOG_MMU, | |
569 | "%s address=%" VADDR_PRIx " physical " TARGET_FMT_plx | |
570 | " prot %d\n", __func__, address, physical, prot); | |
571 | break; | |
572 | default: | |
573 | qemu_log_mask(CPU_LOG_MMU, | |
574 | "%s address=%" VADDR_PRIx " ret %d\n", __func__, address, | |
575 | ret); | |
576 | break; | |
577 | } | |
43057ab1 | 578 | if (ret == TLBRET_MATCH) { |
0c591eb0 | 579 | tlb_set_page(cs, address & TARGET_PAGE_MASK, |
99e43d36 AJ |
580 | physical & TARGET_PAGE_MASK, prot | PAGE_EXEC, |
581 | mmu_idx, TARGET_PAGE_SIZE); | |
582 | ret = 0; | |
932e71cd AJ |
583 | } else if (ret < 0) |
584 | #endif | |
585 | { | |
1147e189 | 586 | raise_mmu_exception(env, address, rw, ret); |
6af0bf9c FB |
587 | ret = 1; |
588 | } | |
589 | ||
590 | return ret; | |
591 | } | |
592 | ||
25b91e32 | 593 | #if !defined(CONFIG_USER_ONLY) |
a8170e5e | 594 | hwaddr cpu_mips_translate_address(CPUMIPSState *env, target_ulong address, int rw) |
25b91e32 | 595 | { |
a8170e5e | 596 | hwaddr physical; |
25b91e32 AJ |
597 | int prot; |
598 | int access_type; | |
599 | int ret = 0; | |
600 | ||
25b91e32 AJ |
601 | /* data access */ |
602 | access_type = ACCESS_INT; | |
9fbf4a58 JH |
603 | ret = get_physical_address(env, &physical, &prot, address, rw, access_type, |
604 | cpu_mmu_index(env, false)); | |
25b91e32 AJ |
605 | if (ret != TLBRET_MATCH) { |
606 | raise_mmu_exception(env, address, rw, ret); | |
c36bbb28 AJ |
607 | return -1LL; |
608 | } else { | |
609 | return physical; | |
25b91e32 | 610 | } |
25b91e32 | 611 | } |
25b91e32 | 612 | |
9a5d878f TS |
613 | static const char * const excp_names[EXCP_LAST + 1] = { |
614 | [EXCP_RESET] = "reset", | |
615 | [EXCP_SRESET] = "soft reset", | |
616 | [EXCP_DSS] = "debug single step", | |
617 | [EXCP_DINT] = "debug interrupt", | |
618 | [EXCP_NMI] = "non-maskable interrupt", | |
619 | [EXCP_MCHECK] = "machine check", | |
620 | [EXCP_EXT_INTERRUPT] = "interrupt", | |
621 | [EXCP_DFWATCH] = "deferred watchpoint", | |
622 | [EXCP_DIB] = "debug instruction breakpoint", | |
623 | [EXCP_IWATCH] = "instruction fetch watchpoint", | |
624 | [EXCP_AdEL] = "address error load", | |
625 | [EXCP_AdES] = "address error store", | |
626 | [EXCP_TLBF] = "TLB refill", | |
627 | [EXCP_IBE] = "instruction bus error", | |
628 | [EXCP_DBp] = "debug breakpoint", | |
629 | [EXCP_SYSCALL] = "syscall", | |
630 | [EXCP_BREAK] = "break", | |
631 | [EXCP_CpU] = "coprocessor unusable", | |
632 | [EXCP_RI] = "reserved instruction", | |
633 | [EXCP_OVERFLOW] = "arithmetic overflow", | |
634 | [EXCP_TRAP] = "trap", | |
635 | [EXCP_FPE] = "floating point", | |
636 | [EXCP_DDBS] = "debug data break store", | |
637 | [EXCP_DWATCH] = "data watchpoint", | |
638 | [EXCP_LTLBL] = "TLB modify", | |
639 | [EXCP_TLBL] = "TLB load", | |
640 | [EXCP_TLBS] = "TLB store", | |
641 | [EXCP_DBE] = "data bus error", | |
642 | [EXCP_DDBL] = "debug data break load", | |
643 | [EXCP_THREAD] = "thread", | |
644 | [EXCP_MDMX] = "MDMX", | |
645 | [EXCP_C2E] = "precise coprocessor 2", | |
646 | [EXCP_CACHE] = "cache error", | |
92ceb440 LA |
647 | [EXCP_TLBXI] = "TLB execute-inhibit", |
648 | [EXCP_TLBRI] = "TLB read-inhibit", | |
b10ac204 YK |
649 | [EXCP_MSADIS] = "MSA disabled", |
650 | [EXCP_MSAFPE] = "MSA floating point", | |
14e51cc7 | 651 | }; |
d4fa5354 | 652 | #endif |
14e51cc7 | 653 | |
1239b472 | 654 | target_ulong exception_resume_pc (CPUMIPSState *env) |
32188a03 NF |
655 | { |
656 | target_ulong bad_pc; | |
657 | target_ulong isa_mode; | |
658 | ||
659 | isa_mode = !!(env->hflags & MIPS_HFLAG_M16); | |
660 | bad_pc = env->active_tc.PC | isa_mode; | |
661 | if (env->hflags & MIPS_HFLAG_BMASK) { | |
662 | /* If the exception was raised from a delay slot, come back to | |
663 | the jump. */ | |
664 | bad_pc -= (env->hflags & MIPS_HFLAG_B16 ? 2 : 4); | |
665 | } | |
666 | ||
667 | return bad_pc; | |
668 | } | |
bbfa8f72 | 669 | |
1239b472 | 670 | #if !defined(CONFIG_USER_ONLY) |
7db13fae | 671 | static void set_hflags_for_handler (CPUMIPSState *env) |
bbfa8f72 NF |
672 | { |
673 | /* Exception handlers are entered in 32-bit mode. */ | |
674 | env->hflags &= ~(MIPS_HFLAG_M16); | |
675 | /* ...except that microMIPS lets you choose. */ | |
676 | if (env->insn_flags & ASE_MICROMIPS) { | |
677 | env->hflags |= (!!(env->CP0_Config3 | |
678 | & (1 << CP0C3_ISA_ON_EXC)) | |
679 | << MIPS_HFLAG_M16_SHIFT); | |
680 | } | |
681 | } | |
aea14095 LA |
682 | |
683 | static inline void set_badinstr_registers(CPUMIPSState *env) | |
684 | { | |
7a5f784a SM |
685 | if (env->insn_flags & ISA_NANOMIPS32) { |
686 | if (env->CP0_Config3 & (1 << CP0C3_BI)) { | |
687 | uint32_t instr = (cpu_lduw_code(env, env->active_tc.PC)) << 16; | |
688 | if ((instr & 0x10000000) == 0) { | |
689 | instr |= cpu_lduw_code(env, env->active_tc.PC + 2); | |
690 | } | |
691 | env->CP0_BadInstr = instr; | |
692 | ||
693 | if ((instr & 0xFC000000) == 0x60000000) { | |
694 | instr = cpu_lduw_code(env, env->active_tc.PC + 4) << 16; | |
695 | env->CP0_BadInstrX = instr; | |
696 | } | |
697 | } | |
698 | return; | |
699 | } | |
700 | ||
aea14095 LA |
701 | if (env->hflags & MIPS_HFLAG_M16) { |
702 | /* TODO: add BadInstr support for microMIPS */ | |
703 | return; | |
704 | } | |
705 | if (env->CP0_Config3 & (1 << CP0C3_BI)) { | |
706 | env->CP0_BadInstr = cpu_ldl_code(env, env->active_tc.PC); | |
707 | } | |
708 | if ((env->CP0_Config3 & (1 << CP0C3_BP)) && | |
709 | (env->hflags & MIPS_HFLAG_BMASK)) { | |
710 | env->CP0_BadInstrP = cpu_ldl_code(env, env->active_tc.PC - 4); | |
711 | } | |
712 | } | |
32188a03 NF |
713 | #endif |
714 | ||
97a8ea5a | 715 | void mips_cpu_do_interrupt(CPUState *cs) |
6af0bf9c | 716 | { |
27103424 | 717 | #if !defined(CONFIG_USER_ONLY) |
97a8ea5a AF |
718 | MIPSCPU *cpu = MIPS_CPU(cs); |
719 | CPUMIPSState *env = &cpu->env; | |
aea14095 | 720 | bool update_badinstr = 0; |
932e71cd AJ |
721 | target_ulong offset; |
722 | int cause = -1; | |
723 | const char *name; | |
100ce988 | 724 | |
c8557016 RH |
725 | if (qemu_loglevel_mask(CPU_LOG_INT) |
726 | && cs->exception_index != EXCP_EXT_INTERRUPT) { | |
27103424 | 727 | if (cs->exception_index < 0 || cs->exception_index > EXCP_LAST) { |
932e71cd | 728 | name = "unknown"; |
27103424 AF |
729 | } else { |
730 | name = excp_names[cs->exception_index]; | |
731 | } | |
b67bfe8d | 732 | |
c8557016 RH |
733 | qemu_log("%s enter: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx |
734 | " %s exception\n", | |
93fcfe39 | 735 | __func__, env->active_tc.PC, env->CP0_EPC, name); |
932e71cd | 736 | } |
27103424 AF |
737 | if (cs->exception_index == EXCP_EXT_INTERRUPT && |
738 | (env->hflags & MIPS_HFLAG_DM)) { | |
739 | cs->exception_index = EXCP_DINT; | |
740 | } | |
932e71cd | 741 | offset = 0x180; |
27103424 | 742 | switch (cs->exception_index) { |
932e71cd AJ |
743 | case EXCP_DSS: |
744 | env->CP0_Debug |= 1 << CP0DB_DSS; | |
745 | /* Debug single step cannot be raised inside a delay slot and | |
746 | resume will always occur on the next instruction | |
747 | (but we assume the pc has always been updated during | |
748 | code translation). */ | |
32188a03 | 749 | env->CP0_DEPC = env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16); |
932e71cd AJ |
750 | goto enter_debug_mode; |
751 | case EXCP_DINT: | |
752 | env->CP0_Debug |= 1 << CP0DB_DINT; | |
753 | goto set_DEPC; | |
754 | case EXCP_DIB: | |
755 | env->CP0_Debug |= 1 << CP0DB_DIB; | |
756 | goto set_DEPC; | |
757 | case EXCP_DBp: | |
758 | env->CP0_Debug |= 1 << CP0DB_DBp; | |
c6c2c0fc PD |
759 | /* Setup DExcCode - SDBBP instruction */ |
760 | env->CP0_Debug = (env->CP0_Debug & ~(0x1fULL << CP0DB_DEC)) | 9 << CP0DB_DEC; | |
932e71cd AJ |
761 | goto set_DEPC; |
762 | case EXCP_DDBS: | |
763 | env->CP0_Debug |= 1 << CP0DB_DDBS; | |
764 | goto set_DEPC; | |
765 | case EXCP_DDBL: | |
766 | env->CP0_Debug |= 1 << CP0DB_DDBL; | |
767 | set_DEPC: | |
32188a03 NF |
768 | env->CP0_DEPC = exception_resume_pc(env); |
769 | env->hflags &= ~MIPS_HFLAG_BMASK; | |
0eaef5aa | 770 | enter_debug_mode: |
d9224450 MR |
771 | if (env->insn_flags & ISA_MIPS3) { |
772 | env->hflags |= MIPS_HFLAG_64; | |
7871abb9 JH |
773 | if (!(env->insn_flags & ISA_MIPS64R6) || |
774 | env->CP0_Status & (1 << CP0St_KX)) { | |
775 | env->hflags &= ~MIPS_HFLAG_AWRAP; | |
776 | } | |
d9224450 MR |
777 | } |
778 | env->hflags |= MIPS_HFLAG_DM | MIPS_HFLAG_CP0; | |
932e71cd AJ |
779 | env->hflags &= ~(MIPS_HFLAG_KSU); |
780 | /* EJTAG probe trap enable is not implemented... */ | |
781 | if (!(env->CP0_Status & (1 << CP0St_EXL))) | |
f45cb2f4 | 782 | env->CP0_Cause &= ~(1U << CP0Ca_BD); |
89777fd1 | 783 | env->active_tc.PC = env->exception_base + 0x480; |
bbfa8f72 | 784 | set_hflags_for_handler(env); |
932e71cd AJ |
785 | break; |
786 | case EXCP_RESET: | |
fca1be7c | 787 | cpu_reset(CPU(cpu)); |
932e71cd AJ |
788 | break; |
789 | case EXCP_SRESET: | |
790 | env->CP0_Status |= (1 << CP0St_SR); | |
9d989c73 | 791 | memset(env->CP0_WatchLo, 0, sizeof(env->CP0_WatchLo)); |
932e71cd AJ |
792 | goto set_error_EPC; |
793 | case EXCP_NMI: | |
794 | env->CP0_Status |= (1 << CP0St_NMI); | |
0eaef5aa | 795 | set_error_EPC: |
32188a03 NF |
796 | env->CP0_ErrorEPC = exception_resume_pc(env); |
797 | env->hflags &= ~MIPS_HFLAG_BMASK; | |
932e71cd | 798 | env->CP0_Status |= (1 << CP0St_ERL) | (1 << CP0St_BEV); |
d9224450 MR |
799 | if (env->insn_flags & ISA_MIPS3) { |
800 | env->hflags |= MIPS_HFLAG_64; | |
7871abb9 JH |
801 | if (!(env->insn_flags & ISA_MIPS64R6) || |
802 | env->CP0_Status & (1 << CP0St_KX)) { | |
803 | env->hflags &= ~MIPS_HFLAG_AWRAP; | |
804 | } | |
d9224450 MR |
805 | } |
806 | env->hflags |= MIPS_HFLAG_CP0; | |
932e71cd AJ |
807 | env->hflags &= ~(MIPS_HFLAG_KSU); |
808 | if (!(env->CP0_Status & (1 << CP0St_EXL))) | |
f45cb2f4 | 809 | env->CP0_Cause &= ~(1U << CP0Ca_BD); |
89777fd1 | 810 | env->active_tc.PC = env->exception_base; |
bbfa8f72 | 811 | set_hflags_for_handler(env); |
932e71cd AJ |
812 | break; |
813 | case EXCP_EXT_INTERRUPT: | |
814 | cause = 0; | |
da52a4df YK |
815 | if (env->CP0_Cause & (1 << CP0Ca_IV)) { |
816 | uint32_t spacing = (env->CP0_IntCtl >> CP0IntCtl_VS) & 0x1f; | |
817 | ||
818 | if ((env->CP0_Status & (1 << CP0St_BEV)) || spacing == 0) { | |
819 | offset = 0x200; | |
820 | } else { | |
821 | uint32_t vector = 0; | |
822 | uint32_t pending = (env->CP0_Cause & CP0Ca_IP_mask) >> CP0Ca_IP; | |
823 | ||
824 | if (env->CP0_Config3 & (1 << CP0C3_VEIC)) { | |
825 | /* For VEIC mode, the external interrupt controller feeds | |
826 | * the vector through the CP0Cause IP lines. */ | |
827 | vector = pending; | |
828 | } else { | |
829 | /* Vectored Interrupts | |
830 | * Mask with Status.IM7-IM0 to get enabled interrupts. */ | |
831 | pending &= (env->CP0_Status >> CP0St_IM) & 0xff; | |
832 | /* Find the highest-priority interrupt. */ | |
833 | while (pending >>= 1) { | |
834 | vector++; | |
138afb02 | 835 | } |
138afb02 | 836 | } |
da52a4df | 837 | offset = 0x200 + (vector * (spacing << 5)); |
138afb02 | 838 | } |
138afb02 | 839 | } |
932e71cd AJ |
840 | goto set_EPC; |
841 | case EXCP_LTLBL: | |
842 | cause = 1; | |
aea14095 | 843 | update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL); |
932e71cd AJ |
844 | goto set_EPC; |
845 | case EXCP_TLBL: | |
846 | cause = 2; | |
aea14095 LA |
847 | update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL); |
848 | if ((env->error_code & EXCP_TLB_NOMATCH) && | |
849 | !(env->CP0_Status & (1 << CP0St_EXL))) { | |
0eaef5aa | 850 | #if defined(TARGET_MIPS64) |
932e71cd AJ |
851 | int R = env->CP0_BadVAddr >> 62; |
852 | int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0; | |
932e71cd | 853 | int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0; |
0eaef5aa | 854 | |
480e79ae JH |
855 | if ((R != 0 || UX) && (R != 3 || KX) && |
856 | (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F)))) { | |
932e71cd | 857 | offset = 0x080; |
480e79ae | 858 | } else { |
0eaef5aa | 859 | #endif |
932e71cd | 860 | offset = 0x000; |
480e79ae JH |
861 | #if defined(TARGET_MIPS64) |
862 | } | |
863 | #endif | |
932e71cd AJ |
864 | } |
865 | goto set_EPC; | |
866 | case EXCP_TLBS: | |
867 | cause = 3; | |
aea14095 LA |
868 | update_badinstr = 1; |
869 | if ((env->error_code & EXCP_TLB_NOMATCH) && | |
870 | !(env->CP0_Status & (1 << CP0St_EXL))) { | |
0eaef5aa | 871 | #if defined(TARGET_MIPS64) |
932e71cd AJ |
872 | int R = env->CP0_BadVAddr >> 62; |
873 | int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0; | |
932e71cd | 874 | int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0; |
0eaef5aa | 875 | |
480e79ae JH |
876 | if ((R != 0 || UX) && (R != 3 || KX) && |
877 | (!(env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F)))) { | |
932e71cd | 878 | offset = 0x080; |
480e79ae | 879 | } else { |
0eaef5aa | 880 | #endif |
932e71cd | 881 | offset = 0x000; |
480e79ae JH |
882 | #if defined(TARGET_MIPS64) |
883 | } | |
884 | #endif | |
932e71cd AJ |
885 | } |
886 | goto set_EPC; | |
887 | case EXCP_AdEL: | |
888 | cause = 4; | |
aea14095 | 889 | update_badinstr = !(env->error_code & EXCP_INST_NOTAVAIL); |
932e71cd AJ |
890 | goto set_EPC; |
891 | case EXCP_AdES: | |
892 | cause = 5; | |
aea14095 | 893 | update_badinstr = 1; |
932e71cd AJ |
894 | goto set_EPC; |
895 | case EXCP_IBE: | |
896 | cause = 6; | |
897 | goto set_EPC; | |
898 | case EXCP_DBE: | |
899 | cause = 7; | |
900 | goto set_EPC; | |
901 | case EXCP_SYSCALL: | |
902 | cause = 8; | |
aea14095 | 903 | update_badinstr = 1; |
932e71cd AJ |
904 | goto set_EPC; |
905 | case EXCP_BREAK: | |
906 | cause = 9; | |
aea14095 | 907 | update_badinstr = 1; |
932e71cd AJ |
908 | goto set_EPC; |
909 | case EXCP_RI: | |
910 | cause = 10; | |
aea14095 | 911 | update_badinstr = 1; |
932e71cd AJ |
912 | goto set_EPC; |
913 | case EXCP_CpU: | |
914 | cause = 11; | |
aea14095 | 915 | update_badinstr = 1; |
932e71cd AJ |
916 | env->CP0_Cause = (env->CP0_Cause & ~(0x3 << CP0Ca_CE)) | |
917 | (env->error_code << CP0Ca_CE); | |
918 | goto set_EPC; | |
919 | case EXCP_OVERFLOW: | |
920 | cause = 12; | |
aea14095 | 921 | update_badinstr = 1; |
932e71cd AJ |
922 | goto set_EPC; |
923 | case EXCP_TRAP: | |
924 | cause = 13; | |
aea14095 | 925 | update_badinstr = 1; |
932e71cd | 926 | goto set_EPC; |
b10ac204 YK |
927 | case EXCP_MSAFPE: |
928 | cause = 14; | |
929 | update_badinstr = 1; | |
930 | goto set_EPC; | |
932e71cd AJ |
931 | case EXCP_FPE: |
932 | cause = 15; | |
aea14095 | 933 | update_badinstr = 1; |
932e71cd AJ |
934 | goto set_EPC; |
935 | case EXCP_C2E: | |
936 | cause = 18; | |
937 | goto set_EPC; | |
92ceb440 LA |
938 | case EXCP_TLBRI: |
939 | cause = 19; | |
aea14095 | 940 | update_badinstr = 1; |
92ceb440 LA |
941 | goto set_EPC; |
942 | case EXCP_TLBXI: | |
943 | cause = 20; | |
944 | goto set_EPC; | |
b10ac204 YK |
945 | case EXCP_MSADIS: |
946 | cause = 21; | |
947 | update_badinstr = 1; | |
948 | goto set_EPC; | |
932e71cd AJ |
949 | case EXCP_MDMX: |
950 | cause = 22; | |
951 | goto set_EPC; | |
952 | case EXCP_DWATCH: | |
953 | cause = 23; | |
67cc32eb | 954 | /* XXX: TODO: manage deferred watch exceptions */ |
932e71cd AJ |
955 | goto set_EPC; |
956 | case EXCP_MCHECK: | |
957 | cause = 24; | |
958 | goto set_EPC; | |
959 | case EXCP_THREAD: | |
960 | cause = 25; | |
961 | goto set_EPC; | |
853c3240 JL |
962 | case EXCP_DSPDIS: |
963 | cause = 26; | |
964 | goto set_EPC; | |
932e71cd AJ |
965 | case EXCP_CACHE: |
966 | cause = 30; | |
74dbf824 | 967 | offset = 0x100; |
0eaef5aa | 968 | set_EPC: |
932e71cd | 969 | if (!(env->CP0_Status & (1 << CP0St_EXL))) { |
32188a03 | 970 | env->CP0_EPC = exception_resume_pc(env); |
aea14095 LA |
971 | if (update_badinstr) { |
972 | set_badinstr_registers(env); | |
973 | } | |
932e71cd | 974 | if (env->hflags & MIPS_HFLAG_BMASK) { |
f45cb2f4 | 975 | env->CP0_Cause |= (1U << CP0Ca_BD); |
0eaef5aa | 976 | } else { |
f45cb2f4 | 977 | env->CP0_Cause &= ~(1U << CP0Ca_BD); |
0eaef5aa | 978 | } |
932e71cd | 979 | env->CP0_Status |= (1 << CP0St_EXL); |
d9224450 MR |
980 | if (env->insn_flags & ISA_MIPS3) { |
981 | env->hflags |= MIPS_HFLAG_64; | |
7871abb9 JH |
982 | if (!(env->insn_flags & ISA_MIPS64R6) || |
983 | env->CP0_Status & (1 << CP0St_KX)) { | |
984 | env->hflags &= ~MIPS_HFLAG_AWRAP; | |
985 | } | |
d9224450 MR |
986 | } |
987 | env->hflags |= MIPS_HFLAG_CP0; | |
932e71cd | 988 | env->hflags &= ~(MIPS_HFLAG_KSU); |
6af0bf9c | 989 | } |
932e71cd AJ |
990 | env->hflags &= ~MIPS_HFLAG_BMASK; |
991 | if (env->CP0_Status & (1 << CP0St_BEV)) { | |
89777fd1 | 992 | env->active_tc.PC = env->exception_base + 0x200; |
74dbf824 JH |
993 | } else if (cause == 30 && !(env->CP0_Config3 & (1 << CP0C3_SC) && |
994 | env->CP0_Config5 & (1 << CP0C5_CV))) { | |
995 | /* Force KSeg1 for cache errors */ | |
67433345 | 996 | env->active_tc.PC = KSEG1_BASE | (env->CP0_EBase & 0x1FFFF000); |
932e71cd | 997 | } else { |
74dbf824 | 998 | env->active_tc.PC = env->CP0_EBase & ~0xfff; |
6af0bf9c | 999 | } |
74dbf824 | 1000 | |
932e71cd | 1001 | env->active_tc.PC += offset; |
bbfa8f72 | 1002 | set_hflags_for_handler(env); |
932e71cd AJ |
1003 | env->CP0_Cause = (env->CP0_Cause & ~(0x1f << CP0Ca_EC)) | (cause << CP0Ca_EC); |
1004 | break; | |
1005 | default: | |
c8557016 | 1006 | abort(); |
932e71cd | 1007 | } |
c8557016 RH |
1008 | if (qemu_loglevel_mask(CPU_LOG_INT) |
1009 | && cs->exception_index != EXCP_EXT_INTERRUPT) { | |
93fcfe39 | 1010 | qemu_log("%s: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " cause %d\n" |
c8557016 RH |
1011 | " S %08x C %08x A " TARGET_FMT_lx " D " TARGET_FMT_lx "\n", |
1012 | __func__, env->active_tc.PC, env->CP0_EPC, cause, | |
1013 | env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr, | |
1014 | env->CP0_DEPC); | |
6af0bf9c | 1015 | } |
932e71cd | 1016 | #endif |
27103424 | 1017 | cs->exception_index = EXCP_NONE; |
6af0bf9c | 1018 | } |
2ee4aed8 | 1019 | |
fa4faba4 RH |
1020 | bool mips_cpu_exec_interrupt(CPUState *cs, int interrupt_request) |
1021 | { | |
1022 | if (interrupt_request & CPU_INTERRUPT_HARD) { | |
1023 | MIPSCPU *cpu = MIPS_CPU(cs); | |
1024 | CPUMIPSState *env = &cpu->env; | |
1025 | ||
71ca034a LA |
1026 | if (cpu_mips_hw_interrupts_enabled(env) && |
1027 | cpu_mips_hw_interrupts_pending(env)) { | |
fa4faba4 RH |
1028 | /* Raise it */ |
1029 | cs->exception_index = EXCP_EXT_INTERRUPT; | |
1030 | env->error_code = 0; | |
1031 | mips_cpu_do_interrupt(cs); | |
1032 | return true; | |
1033 | } | |
1034 | } | |
1035 | return false; | |
1036 | } | |
1037 | ||
3c7b48b7 | 1038 | #if !defined(CONFIG_USER_ONLY) |
7db13fae | 1039 | void r4k_invalidate_tlb (CPUMIPSState *env, int idx, int use_extra) |
2ee4aed8 | 1040 | { |
31b030d4 AF |
1041 | MIPSCPU *cpu = mips_env_get_cpu(env); |
1042 | CPUState *cs; | |
c227f099 | 1043 | r4k_tlb_t *tlb; |
3b1c8be4 TS |
1044 | target_ulong addr; |
1045 | target_ulong end; | |
2d72e7b0 | 1046 | uint16_t ASID = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask; |
3b1c8be4 | 1047 | target_ulong mask; |
2ee4aed8 | 1048 | |
ead9360e | 1049 | tlb = &env->tlb->mmu.r4k.tlb[idx]; |
f2e9ebef | 1050 | /* The qemu TLB is flushed when the ASID changes, so no need to |
2ee4aed8 FB |
1051 | flush these entries again. */ |
1052 | if (tlb->G == 0 && tlb->ASID != ASID) { | |
1053 | return; | |
1054 | } | |
1055 | ||
ead9360e | 1056 | if (use_extra && env->tlb->tlb_in_use < MIPS_TLB_MAX) { |
2ee4aed8 | 1057 | /* For tlbwr, we can shadow the discarded entry into |
6958549d AJ |
1058 | a new (fake) TLB entry, as long as the guest can not |
1059 | tell that it's there. */ | |
ead9360e TS |
1060 | env->tlb->mmu.r4k.tlb[env->tlb->tlb_in_use] = *tlb; |
1061 | env->tlb->tlb_in_use++; | |
2ee4aed8 FB |
1062 | return; |
1063 | } | |
1064 | ||
3b1c8be4 | 1065 | /* 1k pages are not supported. */ |
f2e9ebef | 1066 | mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1); |
3b1c8be4 | 1067 | if (tlb->V0) { |
31b030d4 | 1068 | cs = CPU(cpu); |
f2e9ebef | 1069 | addr = tlb->VPN & ~mask; |
d26bc211 | 1070 | #if defined(TARGET_MIPS64) |
e034e2c3 | 1071 | if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) { |
100ce988 TS |
1072 | addr |= 0x3FFFFF0000000000ULL; |
1073 | } | |
1074 | #endif | |
3b1c8be4 TS |
1075 | end = addr | (mask >> 1); |
1076 | while (addr < end) { | |
31b030d4 | 1077 | tlb_flush_page(cs, addr); |
3b1c8be4 TS |
1078 | addr += TARGET_PAGE_SIZE; |
1079 | } | |
1080 | } | |
1081 | if (tlb->V1) { | |
31b030d4 | 1082 | cs = CPU(cpu); |
f2e9ebef | 1083 | addr = (tlb->VPN & ~mask) | ((mask >> 1) + 1); |
d26bc211 | 1084 | #if defined(TARGET_MIPS64) |
e034e2c3 | 1085 | if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) { |
100ce988 TS |
1086 | addr |= 0x3FFFFF0000000000ULL; |
1087 | } | |
1088 | #endif | |
3b1c8be4 | 1089 | end = addr | mask; |
53715e48 | 1090 | while (addr - 1 < end) { |
31b030d4 | 1091 | tlb_flush_page(cs, addr); |
3b1c8be4 TS |
1092 | addr += TARGET_PAGE_SIZE; |
1093 | } | |
1094 | } | |
2ee4aed8 | 1095 | } |
3c7b48b7 | 1096 | #endif |
33c11879 PB |
1097 | |
1098 | void QEMU_NORETURN do_raise_exception_err(CPUMIPSState *env, | |
1099 | uint32_t exception, | |
1100 | int error_code, | |
1101 | uintptr_t pc) | |
1102 | { | |
1103 | CPUState *cs = CPU(mips_env_get_cpu(env)); | |
1104 | ||
1105 | if (exception < EXCP_SC) { | |
1106 | qemu_log_mask(CPU_LOG_INT, "%s: %d %d\n", | |
1107 | __func__, exception, error_code); | |
1108 | } | |
1109 | cs->exception_index = exception; | |
1110 | env->error_code = error_code; | |
1111 | ||
1112 | cpu_loop_exit_restore(cs, pc); | |
1113 | } |