]>
Commit | Line | Data |
---|---|---|
ad71ed68 BS |
1 | /* |
2 | * PowerPC exception emulation helpers for QEMU. | |
3 | * | |
4 | * Copyright (c) 2003-2007 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 | |
17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
0d75590d | 19 | #include "qemu/osdep.h" |
ad71ed68 | 20 | #include "cpu.h" |
2ef6175a | 21 | #include "exec/helper-proto.h" |
63c91552 | 22 | #include "exec/exec-all.h" |
f08b6170 | 23 | #include "exec/cpu_ldst.h" |
ad71ed68 BS |
24 | |
25 | #include "helper_regs.h" | |
26 | ||
27 | //#define DEBUG_OP | |
48880da6 | 28 | //#define DEBUG_SOFTWARE_TLB |
ad71ed68 BS |
29 | //#define DEBUG_EXCEPTIONS |
30 | ||
c79c73f6 BS |
31 | #ifdef DEBUG_EXCEPTIONS |
32 | # define LOG_EXCP(...) qemu_log(__VA_ARGS__) | |
33 | #else | |
34 | # define LOG_EXCP(...) do { } while (0) | |
35 | #endif | |
36 | ||
37 | /*****************************************************************************/ | |
38 | /* PowerPC Hypercall emulation */ | |
39 | ||
1b14670a | 40 | void (*cpu_ppc_hypercall)(PowerPCCPU *); |
c79c73f6 BS |
41 | |
42 | /*****************************************************************************/ | |
43 | /* Exception processing */ | |
44 | #if defined(CONFIG_USER_ONLY) | |
97a8ea5a | 45 | void ppc_cpu_do_interrupt(CPUState *cs) |
c79c73f6 | 46 | { |
97a8ea5a AF |
47 | PowerPCCPU *cpu = POWERPC_CPU(cs); |
48 | CPUPPCState *env = &cpu->env; | |
49 | ||
27103424 | 50 | cs->exception_index = POWERPC_EXCP_NONE; |
c79c73f6 BS |
51 | env->error_code = 0; |
52 | } | |
53 | ||
458dd766 | 54 | static void ppc_hw_interrupt(CPUPPCState *env) |
c79c73f6 | 55 | { |
27103424 AF |
56 | CPUState *cs = CPU(ppc_env_get_cpu(env)); |
57 | ||
58 | cs->exception_index = POWERPC_EXCP_NONE; | |
c79c73f6 BS |
59 | env->error_code = 0; |
60 | } | |
61 | #else /* defined(CONFIG_USER_ONLY) */ | |
62 | static inline void dump_syscall(CPUPPCState *env) | |
63 | { | |
64 | qemu_log_mask(CPU_LOG_INT, "syscall r0=%016" PRIx64 " r3=%016" PRIx64 | |
65 | " r4=%016" PRIx64 " r5=%016" PRIx64 " r6=%016" PRIx64 | |
66 | " nip=" TARGET_FMT_lx "\n", | |
67 | ppc_dump_gpr(env, 0), ppc_dump_gpr(env, 3), | |
68 | ppc_dump_gpr(env, 4), ppc_dump_gpr(env, 5), | |
69 | ppc_dump_gpr(env, 6), env->nip); | |
70 | } | |
71 | ||
72 | /* Note that this function should be greatly optimized | |
73 | * when called with a constant excp, from ppc_hw_interrupt | |
74 | */ | |
5c26a5b3 | 75 | static inline void powerpc_excp(PowerPCCPU *cpu, int excp_model, int excp) |
c79c73f6 | 76 | { |
27103424 | 77 | CPUState *cs = CPU(cpu); |
5c26a5b3 | 78 | CPUPPCState *env = &cpu->env; |
c79c73f6 | 79 | target_ulong msr, new_msr, vector; |
6d49d6d4 BH |
80 | int srr0, srr1, asrr0, asrr1, lev, ail; |
81 | bool lpes0; | |
c79c73f6 BS |
82 | |
83 | qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx | |
84 | " => %08x (%02x)\n", env->nip, excp, env->error_code); | |
85 | ||
86 | /* new srr1 value excluding must-be-zero bits */ | |
a1bb7384 SW |
87 | if (excp_model == POWERPC_EXCP_BOOKE) { |
88 | msr = env->msr; | |
89 | } else { | |
90 | msr = env->msr & ~0x783f0000ULL; | |
91 | } | |
c79c73f6 | 92 | |
6d49d6d4 BH |
93 | /* new interrupt handler msr preserves existing HV and ME unless |
94 | * explicitly overriden | |
95 | */ | |
96 | new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB); | |
c79c73f6 BS |
97 | |
98 | /* target registers */ | |
99 | srr0 = SPR_SRR0; | |
100 | srr1 = SPR_SRR1; | |
101 | asrr0 = -1; | |
102 | asrr1 = -1; | |
103 | ||
7778a575 BH |
104 | /* check for special resume at 0x100 from doze/nap/sleep/winkle on P7/P8 */ |
105 | if (env->in_pm_state) { | |
106 | env->in_pm_state = false; | |
107 | ||
108 | /* Pretend to be returning from doze always as we don't lose state */ | |
109 | msr |= (0x1ull << (63 - 47)); | |
110 | ||
111 | /* Non-machine check are routed to 0x100 with a wakeup cause | |
112 | * encoded in SRR1 | |
113 | */ | |
114 | if (excp != POWERPC_EXCP_MCHECK) { | |
115 | switch (excp) { | |
116 | case POWERPC_EXCP_RESET: | |
117 | msr |= 0x4ull << (63 - 45); | |
118 | break; | |
119 | case POWERPC_EXCP_EXTERNAL: | |
120 | msr |= 0x8ull << (63 - 45); | |
121 | break; | |
122 | case POWERPC_EXCP_DECR: | |
123 | msr |= 0x6ull << (63 - 45); | |
124 | break; | |
125 | case POWERPC_EXCP_SDOOR: | |
126 | msr |= 0x5ull << (63 - 45); | |
127 | break; | |
128 | case POWERPC_EXCP_SDOOR_HV: | |
129 | msr |= 0x3ull << (63 - 45); | |
130 | break; | |
131 | case POWERPC_EXCP_HV_MAINT: | |
132 | msr |= 0xaull << (63 - 45); | |
133 | break; | |
134 | default: | |
135 | cpu_abort(cs, "Unsupported exception %d in Power Save mode\n", | |
136 | excp); | |
137 | } | |
138 | excp = POWERPC_EXCP_RESET; | |
139 | } | |
140 | } | |
141 | ||
5c94b2a5 | 142 | /* Exception targetting modifiers |
6d49d6d4 BH |
143 | * |
144 | * LPES0 is supported on POWER7/8 | |
145 | * LPES1 is not supported (old iSeries mode) | |
146 | * | |
147 | * On anything else, we behave as if LPES0 is 1 | |
148 | * (externals don't alter MSR:HV) | |
5c94b2a5 CLG |
149 | * |
150 | * AIL is initialized here but can be cleared by | |
151 | * selected exceptions | |
152 | */ | |
153 | #if defined(TARGET_PPC64) | |
154 | if (excp_model == POWERPC_EXCP_POWER7 || | |
155 | excp_model == POWERPC_EXCP_POWER8) { | |
6d49d6d4 | 156 | lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); |
5c94b2a5 CLG |
157 | if (excp_model == POWERPC_EXCP_POWER8) { |
158 | ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT; | |
159 | } else { | |
160 | ail = 0; | |
161 | } | |
162 | } else | |
163 | #endif /* defined(TARGET_PPC64) */ | |
164 | { | |
6d49d6d4 | 165 | lpes0 = true; |
5c94b2a5 CLG |
166 | ail = 0; |
167 | } | |
168 | ||
9b2fadda BH |
169 | /* Hypervisor emulation assistance interrupt only exists on server |
170 | * arch 2.05 server or later. We also don't want to generate it if | |
171 | * we don't have HVB in msr_mask (PAPR mode). | |
172 | */ | |
173 | if (excp == POWERPC_EXCP_HV_EMU | |
174 | #if defined(TARGET_PPC64) | |
175 | && !((env->mmu_model & POWERPC_MMU_64) && (env->msr_mask & MSR_HVB)) | |
176 | #endif /* defined(TARGET_PPC64) */ | |
177 | ||
178 | ) { | |
179 | excp = POWERPC_EXCP_PROGRAM; | |
180 | } | |
181 | ||
c79c73f6 BS |
182 | switch (excp) { |
183 | case POWERPC_EXCP_NONE: | |
184 | /* Should never happen */ | |
185 | return; | |
186 | case POWERPC_EXCP_CRITICAL: /* Critical input */ | |
187 | switch (excp_model) { | |
188 | case POWERPC_EXCP_40x: | |
189 | srr0 = SPR_40x_SRR2; | |
190 | srr1 = SPR_40x_SRR3; | |
191 | break; | |
192 | case POWERPC_EXCP_BOOKE: | |
193 | srr0 = SPR_BOOKE_CSRR0; | |
194 | srr1 = SPR_BOOKE_CSRR1; | |
195 | break; | |
196 | case POWERPC_EXCP_G2: | |
197 | break; | |
198 | default: | |
199 | goto excp_invalid; | |
200 | } | |
bd6fefe7 | 201 | break; |
c79c73f6 BS |
202 | case POWERPC_EXCP_MCHECK: /* Machine check exception */ |
203 | if (msr_me == 0) { | |
204 | /* Machine check exception is not enabled. | |
205 | * Enter checkstop state. | |
206 | */ | |
013a2942 PB |
207 | fprintf(stderr, "Machine check while not allowed. " |
208 | "Entering checkstop state\n"); | |
209 | if (qemu_log_separate()) { | |
c79c73f6 BS |
210 | qemu_log("Machine check while not allowed. " |
211 | "Entering checkstop state\n"); | |
c79c73f6 | 212 | } |
259186a7 AF |
213 | cs->halted = 1; |
214 | cs->interrupt_request |= CPU_INTERRUPT_EXITTB; | |
c79c73f6 | 215 | } |
6d49d6d4 | 216 | new_msr |= (target_ulong)MSR_HVB; |
5c94b2a5 | 217 | ail = 0; |
c79c73f6 BS |
218 | |
219 | /* machine check exceptions don't have ME set */ | |
220 | new_msr &= ~((target_ulong)1 << MSR_ME); | |
221 | ||
222 | /* XXX: should also have something loaded in DAR / DSISR */ | |
223 | switch (excp_model) { | |
224 | case POWERPC_EXCP_40x: | |
225 | srr0 = SPR_40x_SRR2; | |
226 | srr1 = SPR_40x_SRR3; | |
227 | break; | |
228 | case POWERPC_EXCP_BOOKE: | |
a1bb7384 | 229 | /* FIXME: choose one or the other based on CPU type */ |
c79c73f6 BS |
230 | srr0 = SPR_BOOKE_MCSRR0; |
231 | srr1 = SPR_BOOKE_MCSRR1; | |
232 | asrr0 = SPR_BOOKE_CSRR0; | |
233 | asrr1 = SPR_BOOKE_CSRR1; | |
234 | break; | |
235 | default: | |
236 | break; | |
237 | } | |
bd6fefe7 | 238 | break; |
c79c73f6 BS |
239 | case POWERPC_EXCP_DSI: /* Data storage exception */ |
240 | LOG_EXCP("DSI exception: DSISR=" TARGET_FMT_lx" DAR=" TARGET_FMT_lx | |
241 | "\n", env->spr[SPR_DSISR], env->spr[SPR_DAR]); | |
bd6fefe7 | 242 | break; |
c79c73f6 BS |
243 | case POWERPC_EXCP_ISI: /* Instruction storage exception */ |
244 | LOG_EXCP("ISI exception: msr=" TARGET_FMT_lx ", nip=" TARGET_FMT_lx | |
245 | "\n", msr, env->nip); | |
c79c73f6 | 246 | msr |= env->error_code; |
bd6fefe7 | 247 | break; |
c79c73f6 | 248 | case POWERPC_EXCP_EXTERNAL: /* External input */ |
fdfba1a2 EI |
249 | cs = CPU(cpu); |
250 | ||
6d49d6d4 | 251 | if (!lpes0) { |
c79c73f6 | 252 | new_msr |= (target_ulong)MSR_HVB; |
6d49d6d4 BH |
253 | new_msr |= env->msr & ((target_ulong)1 << MSR_RI); |
254 | srr0 = SPR_HSRR0; | |
255 | srr1 = SPR_HSRR1; | |
c79c73f6 | 256 | } |
68c2dd70 AG |
257 | if (env->mpic_proxy) { |
258 | /* IACK the IRQ on delivery */ | |
fdfba1a2 | 259 | env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack); |
68c2dd70 | 260 | } |
bd6fefe7 | 261 | break; |
c79c73f6 | 262 | case POWERPC_EXCP_ALIGN: /* Alignment exception */ |
c79c73f6 | 263 | /* Get rS/rD and rA from faulting opcode */ |
3433b732 BH |
264 | /* Note: the opcode fields will not be set properly for a direct |
265 | * store load/store, but nobody cares as nobody actually uses | |
266 | * direct store segments. | |
267 | */ | |
268 | env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16; | |
bd6fefe7 | 269 | break; |
c79c73f6 BS |
270 | case POWERPC_EXCP_PROGRAM: /* Program exception */ |
271 | switch (env->error_code & ~0xF) { | |
272 | case POWERPC_EXCP_FP: | |
273 | if ((msr_fe0 == 0 && msr_fe1 == 0) || msr_fp == 0) { | |
274 | LOG_EXCP("Ignore floating point exception\n"); | |
27103424 | 275 | cs->exception_index = POWERPC_EXCP_NONE; |
c79c73f6 BS |
276 | env->error_code = 0; |
277 | return; | |
278 | } | |
1b7d17ca BH |
279 | |
280 | /* FP exceptions always have NIP pointing to the faulting | |
281 | * instruction, so always use store_next and claim we are | |
282 | * precise in the MSR. | |
283 | */ | |
c79c73f6 | 284 | msr |= 0x00100000; |
bd6fefe7 | 285 | break; |
c79c73f6 BS |
286 | case POWERPC_EXCP_INVAL: |
287 | LOG_EXCP("Invalid instruction at " TARGET_FMT_lx "\n", env->nip); | |
c79c73f6 BS |
288 | msr |= 0x00080000; |
289 | env->spr[SPR_BOOKE_ESR] = ESR_PIL; | |
290 | break; | |
291 | case POWERPC_EXCP_PRIV: | |
c79c73f6 BS |
292 | msr |= 0x00040000; |
293 | env->spr[SPR_BOOKE_ESR] = ESR_PPR; | |
294 | break; | |
295 | case POWERPC_EXCP_TRAP: | |
c79c73f6 BS |
296 | msr |= 0x00020000; |
297 | env->spr[SPR_BOOKE_ESR] = ESR_PTR; | |
298 | break; | |
299 | default: | |
300 | /* Should never occur */ | |
a47dddd7 | 301 | cpu_abort(cs, "Invalid program exception %d. Aborting\n", |
c79c73f6 BS |
302 | env->error_code); |
303 | break; | |
304 | } | |
bd6fefe7 | 305 | break; |
c79c73f6 BS |
306 | case POWERPC_EXCP_SYSCALL: /* System call exception */ |
307 | dump_syscall(env); | |
308 | lev = env->error_code; | |
6d49d6d4 | 309 | |
bd6fefe7 BH |
310 | /* We need to correct the NIP which in this case is supposed |
311 | * to point to the next instruction | |
312 | */ | |
313 | env->nip += 4; | |
314 | ||
6d49d6d4 | 315 | /* "PAPR mode" built-in hypercall emulation */ |
c79c73f6 | 316 | if ((lev == 1) && cpu_ppc_hypercall) { |
1b14670a | 317 | cpu_ppc_hypercall(cpu); |
c79c73f6 BS |
318 | return; |
319 | } | |
6d49d6d4 | 320 | if (lev == 1) { |
c79c73f6 BS |
321 | new_msr |= (target_ulong)MSR_HVB; |
322 | } | |
bd6fefe7 BH |
323 | break; |
324 | case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ | |
c79c73f6 | 325 | case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */ |
c79c73f6 | 326 | case POWERPC_EXCP_DECR: /* Decrementer exception */ |
bd6fefe7 | 327 | break; |
c79c73f6 BS |
328 | case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */ |
329 | /* FIT on 4xx */ | |
330 | LOG_EXCP("FIT exception\n"); | |
bd6fefe7 | 331 | break; |
c79c73f6 BS |
332 | case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */ |
333 | LOG_EXCP("WDT exception\n"); | |
334 | switch (excp_model) { | |
335 | case POWERPC_EXCP_BOOKE: | |
336 | srr0 = SPR_BOOKE_CSRR0; | |
337 | srr1 = SPR_BOOKE_CSRR1; | |
338 | break; | |
339 | default: | |
340 | break; | |
341 | } | |
bd6fefe7 | 342 | break; |
c79c73f6 | 343 | case POWERPC_EXCP_DTLB: /* Data TLB error */ |
c79c73f6 | 344 | case POWERPC_EXCP_ITLB: /* Instruction TLB error */ |
bd6fefe7 | 345 | break; |
c79c73f6 BS |
346 | case POWERPC_EXCP_DEBUG: /* Debug interrupt */ |
347 | switch (excp_model) { | |
348 | case POWERPC_EXCP_BOOKE: | |
a1bb7384 | 349 | /* FIXME: choose one or the other based on CPU type */ |
c79c73f6 BS |
350 | srr0 = SPR_BOOKE_DSRR0; |
351 | srr1 = SPR_BOOKE_DSRR1; | |
352 | asrr0 = SPR_BOOKE_CSRR0; | |
353 | asrr1 = SPR_BOOKE_CSRR1; | |
354 | break; | |
355 | default: | |
356 | break; | |
357 | } | |
358 | /* XXX: TODO */ | |
a47dddd7 | 359 | cpu_abort(cs, "Debug exception is not implemented yet !\n"); |
bd6fefe7 | 360 | break; |
c79c73f6 BS |
361 | case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavailable */ |
362 | env->spr[SPR_BOOKE_ESR] = ESR_SPV; | |
bd6fefe7 | 363 | break; |
c79c73f6 BS |
364 | case POWERPC_EXCP_EFPDI: /* Embedded floating-point data interrupt */ |
365 | /* XXX: TODO */ | |
a47dddd7 | 366 | cpu_abort(cs, "Embedded floating point data exception " |
c79c73f6 BS |
367 | "is not implemented yet !\n"); |
368 | env->spr[SPR_BOOKE_ESR] = ESR_SPV; | |
bd6fefe7 | 369 | break; |
c79c73f6 BS |
370 | case POWERPC_EXCP_EFPRI: /* Embedded floating-point round interrupt */ |
371 | /* XXX: TODO */ | |
a47dddd7 | 372 | cpu_abort(cs, "Embedded floating point round exception " |
c79c73f6 BS |
373 | "is not implemented yet !\n"); |
374 | env->spr[SPR_BOOKE_ESR] = ESR_SPV; | |
bd6fefe7 | 375 | break; |
c79c73f6 BS |
376 | case POWERPC_EXCP_EPERFM: /* Embedded performance monitor interrupt */ |
377 | /* XXX: TODO */ | |
a47dddd7 | 378 | cpu_abort(cs, |
c79c73f6 | 379 | "Performance counter exception is not implemented yet !\n"); |
bd6fefe7 | 380 | break; |
c79c73f6 | 381 | case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */ |
bd6fefe7 | 382 | break; |
c79c73f6 BS |
383 | case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */ |
384 | srr0 = SPR_BOOKE_CSRR0; | |
385 | srr1 = SPR_BOOKE_CSRR1; | |
bd6fefe7 | 386 | break; |
c79c73f6 BS |
387 | case POWERPC_EXCP_RESET: /* System reset exception */ |
388 | if (msr_pow) { | |
389 | /* indicate that we resumed from power save mode */ | |
390 | msr |= 0x10000; | |
391 | } else { | |
392 | new_msr &= ~((target_ulong)1 << MSR_ME); | |
393 | } | |
394 | ||
6d49d6d4 | 395 | new_msr |= (target_ulong)MSR_HVB; |
5c94b2a5 | 396 | ail = 0; |
bd6fefe7 | 397 | break; |
c79c73f6 | 398 | case POWERPC_EXCP_DSEG: /* Data segment exception */ |
c79c73f6 | 399 | case POWERPC_EXCP_ISEG: /* Instruction segment exception */ |
c79c73f6 | 400 | case POWERPC_EXCP_TRACE: /* Trace exception */ |
bd6fefe7 BH |
401 | break; |
402 | case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */ | |
c79c73f6 | 403 | case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */ |
c79c73f6 | 404 | case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */ |
c79c73f6 | 405 | case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */ |
c79c73f6 | 406 | case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment exception */ |
bd6fefe7 | 407 | case POWERPC_EXCP_HV_EMU: |
c79c73f6 BS |
408 | srr0 = SPR_HSRR0; |
409 | srr1 = SPR_HSRR1; | |
410 | new_msr |= (target_ulong)MSR_HVB; | |
411 | new_msr |= env->msr & ((target_ulong)1 << MSR_RI); | |
bd6fefe7 | 412 | break; |
c79c73f6 | 413 | case POWERPC_EXCP_VPU: /* Vector unavailable exception */ |
1f29871c | 414 | case POWERPC_EXCP_VSXU: /* VSX unavailable exception */ |
7019cb3d | 415 | case POWERPC_EXCP_FU: /* Facility unavailable exception */ |
bd6fefe7 | 416 | break; |
c79c73f6 BS |
417 | case POWERPC_EXCP_PIT: /* Programmable interval timer interrupt */ |
418 | LOG_EXCP("PIT exception\n"); | |
bd6fefe7 | 419 | break; |
c79c73f6 BS |
420 | case POWERPC_EXCP_IO: /* IO error exception */ |
421 | /* XXX: TODO */ | |
a47dddd7 | 422 | cpu_abort(cs, "601 IO error exception is not implemented yet !\n"); |
bd6fefe7 | 423 | break; |
c79c73f6 BS |
424 | case POWERPC_EXCP_RUNM: /* Run mode exception */ |
425 | /* XXX: TODO */ | |
a47dddd7 | 426 | cpu_abort(cs, "601 run mode exception is not implemented yet !\n"); |
bd6fefe7 | 427 | break; |
c79c73f6 BS |
428 | case POWERPC_EXCP_EMUL: /* Emulation trap exception */ |
429 | /* XXX: TODO */ | |
a47dddd7 | 430 | cpu_abort(cs, "602 emulation trap exception " |
c79c73f6 | 431 | "is not implemented yet !\n"); |
bd6fefe7 | 432 | break; |
c79c73f6 | 433 | case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */ |
c79c73f6 BS |
434 | switch (excp_model) { |
435 | case POWERPC_EXCP_602: | |
436 | case POWERPC_EXCP_603: | |
437 | case POWERPC_EXCP_603E: | |
438 | case POWERPC_EXCP_G2: | |
439 | goto tlb_miss_tgpr; | |
440 | case POWERPC_EXCP_7x5: | |
441 | goto tlb_miss; | |
442 | case POWERPC_EXCP_74xx: | |
443 | goto tlb_miss_74xx; | |
444 | default: | |
a47dddd7 | 445 | cpu_abort(cs, "Invalid instruction TLB miss exception\n"); |
c79c73f6 BS |
446 | break; |
447 | } | |
448 | break; | |
449 | case POWERPC_EXCP_DLTLB: /* Data load TLB miss */ | |
c79c73f6 BS |
450 | switch (excp_model) { |
451 | case POWERPC_EXCP_602: | |
452 | case POWERPC_EXCP_603: | |
453 | case POWERPC_EXCP_603E: | |
454 | case POWERPC_EXCP_G2: | |
455 | goto tlb_miss_tgpr; | |
456 | case POWERPC_EXCP_7x5: | |
457 | goto tlb_miss; | |
458 | case POWERPC_EXCP_74xx: | |
459 | goto tlb_miss_74xx; | |
460 | default: | |
a47dddd7 | 461 | cpu_abort(cs, "Invalid data load TLB miss exception\n"); |
c79c73f6 BS |
462 | break; |
463 | } | |
464 | break; | |
465 | case POWERPC_EXCP_DSTLB: /* Data store TLB miss */ | |
c79c73f6 BS |
466 | switch (excp_model) { |
467 | case POWERPC_EXCP_602: | |
468 | case POWERPC_EXCP_603: | |
469 | case POWERPC_EXCP_603E: | |
470 | case POWERPC_EXCP_G2: | |
471 | tlb_miss_tgpr: | |
472 | /* Swap temporary saved registers with GPRs */ | |
473 | if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) { | |
474 | new_msr |= (target_ulong)1 << MSR_TGPR; | |
475 | hreg_swap_gpr_tgpr(env); | |
476 | } | |
477 | goto tlb_miss; | |
478 | case POWERPC_EXCP_7x5: | |
479 | tlb_miss: | |
480 | #if defined(DEBUG_SOFTWARE_TLB) | |
481 | if (qemu_log_enabled()) { | |
482 | const char *es; | |
483 | target_ulong *miss, *cmp; | |
484 | int en; | |
485 | ||
486 | if (excp == POWERPC_EXCP_IFTLB) { | |
487 | es = "I"; | |
488 | en = 'I'; | |
489 | miss = &env->spr[SPR_IMISS]; | |
490 | cmp = &env->spr[SPR_ICMP]; | |
491 | } else { | |
492 | if (excp == POWERPC_EXCP_DLTLB) { | |
493 | es = "DL"; | |
494 | } else { | |
495 | es = "DS"; | |
496 | } | |
497 | en = 'D'; | |
498 | miss = &env->spr[SPR_DMISS]; | |
499 | cmp = &env->spr[SPR_DCMP]; | |
500 | } | |
501 | qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx " %cC " | |
502 | TARGET_FMT_lx " H1 " TARGET_FMT_lx " H2 " | |
503 | TARGET_FMT_lx " %08x\n", es, en, *miss, en, *cmp, | |
504 | env->spr[SPR_HASH1], env->spr[SPR_HASH2], | |
505 | env->error_code); | |
506 | } | |
507 | #endif | |
508 | msr |= env->crf[0] << 28; | |
509 | msr |= env->error_code; /* key, D/I, S/L bits */ | |
510 | /* Set way using a LRU mechanism */ | |
511 | msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17; | |
512 | break; | |
513 | case POWERPC_EXCP_74xx: | |
514 | tlb_miss_74xx: | |
515 | #if defined(DEBUG_SOFTWARE_TLB) | |
516 | if (qemu_log_enabled()) { | |
517 | const char *es; | |
518 | target_ulong *miss, *cmp; | |
519 | int en; | |
520 | ||
521 | if (excp == POWERPC_EXCP_IFTLB) { | |
522 | es = "I"; | |
523 | en = 'I'; | |
524 | miss = &env->spr[SPR_TLBMISS]; | |
525 | cmp = &env->spr[SPR_PTEHI]; | |
526 | } else { | |
527 | if (excp == POWERPC_EXCP_DLTLB) { | |
528 | es = "DL"; | |
529 | } else { | |
530 | es = "DS"; | |
531 | } | |
532 | en = 'D'; | |
533 | miss = &env->spr[SPR_TLBMISS]; | |
534 | cmp = &env->spr[SPR_PTEHI]; | |
535 | } | |
536 | qemu_log("74xx %sTLB miss: %cM " TARGET_FMT_lx " %cC " | |
537 | TARGET_FMT_lx " %08x\n", es, en, *miss, en, *cmp, | |
538 | env->error_code); | |
539 | } | |
540 | #endif | |
541 | msr |= env->error_code; /* key bit */ | |
542 | break; | |
543 | default: | |
a47dddd7 | 544 | cpu_abort(cs, "Invalid data store TLB miss exception\n"); |
c79c73f6 BS |
545 | break; |
546 | } | |
bd6fefe7 | 547 | break; |
c79c73f6 BS |
548 | case POWERPC_EXCP_FPA: /* Floating-point assist exception */ |
549 | /* XXX: TODO */ | |
a47dddd7 | 550 | cpu_abort(cs, "Floating point assist exception " |
c79c73f6 | 551 | "is not implemented yet !\n"); |
bd6fefe7 | 552 | break; |
c79c73f6 BS |
553 | case POWERPC_EXCP_DABR: /* Data address breakpoint */ |
554 | /* XXX: TODO */ | |
a47dddd7 | 555 | cpu_abort(cs, "DABR exception is not implemented yet !\n"); |
bd6fefe7 | 556 | break; |
c79c73f6 BS |
557 | case POWERPC_EXCP_IABR: /* Instruction address breakpoint */ |
558 | /* XXX: TODO */ | |
a47dddd7 | 559 | cpu_abort(cs, "IABR exception is not implemented yet !\n"); |
bd6fefe7 | 560 | break; |
c79c73f6 BS |
561 | case POWERPC_EXCP_SMI: /* System management interrupt */ |
562 | /* XXX: TODO */ | |
a47dddd7 | 563 | cpu_abort(cs, "SMI exception is not implemented yet !\n"); |
bd6fefe7 | 564 | break; |
c79c73f6 BS |
565 | case POWERPC_EXCP_THERM: /* Thermal interrupt */ |
566 | /* XXX: TODO */ | |
a47dddd7 | 567 | cpu_abort(cs, "Thermal management exception " |
c79c73f6 | 568 | "is not implemented yet !\n"); |
bd6fefe7 | 569 | break; |
c79c73f6 | 570 | case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */ |
c79c73f6 | 571 | /* XXX: TODO */ |
a47dddd7 | 572 | cpu_abort(cs, |
c79c73f6 | 573 | "Performance counter exception is not implemented yet !\n"); |
bd6fefe7 | 574 | break; |
c79c73f6 BS |
575 | case POWERPC_EXCP_VPUA: /* Vector assist exception */ |
576 | /* XXX: TODO */ | |
a47dddd7 | 577 | cpu_abort(cs, "VPU assist exception is not implemented yet !\n"); |
bd6fefe7 | 578 | break; |
c79c73f6 BS |
579 | case POWERPC_EXCP_SOFTP: /* Soft patch exception */ |
580 | /* XXX: TODO */ | |
a47dddd7 | 581 | cpu_abort(cs, |
c79c73f6 | 582 | "970 soft-patch exception is not implemented yet !\n"); |
bd6fefe7 | 583 | break; |
c79c73f6 BS |
584 | case POWERPC_EXCP_MAINT: /* Maintenance exception */ |
585 | /* XXX: TODO */ | |
a47dddd7 | 586 | cpu_abort(cs, |
c79c73f6 | 587 | "970 maintenance exception is not implemented yet !\n"); |
bd6fefe7 | 588 | break; |
c79c73f6 BS |
589 | case POWERPC_EXCP_MEXTBR: /* Maskable external breakpoint */ |
590 | /* XXX: TODO */ | |
a47dddd7 | 591 | cpu_abort(cs, "Maskable external exception " |
c79c73f6 | 592 | "is not implemented yet !\n"); |
bd6fefe7 | 593 | break; |
c79c73f6 BS |
594 | case POWERPC_EXCP_NMEXTBR: /* Non maskable external breakpoint */ |
595 | /* XXX: TODO */ | |
a47dddd7 | 596 | cpu_abort(cs, "Non maskable external exception " |
c79c73f6 | 597 | "is not implemented yet !\n"); |
bd6fefe7 | 598 | break; |
c79c73f6 BS |
599 | default: |
600 | excp_invalid: | |
a47dddd7 | 601 | cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); |
c79c73f6 | 602 | break; |
c79c73f6 | 603 | } |
bd6fefe7 BH |
604 | |
605 | /* Save PC */ | |
606 | env->spr[srr0] = env->nip; | |
607 | ||
c79c73f6 BS |
608 | /* Save MSR */ |
609 | env->spr[srr1] = msr; | |
6d49d6d4 BH |
610 | |
611 | /* Sanity check */ | |
612 | if (!(env->msr_mask & MSR_HVB) && (srr0 == SPR_HSRR0)) { | |
613 | cpu_abort(cs, "Trying to deliver HV exception %d with " | |
614 | "no HV support\n", excp); | |
615 | } | |
616 | ||
c79c73f6 BS |
617 | /* If any alternate SRR register are defined, duplicate saved values */ |
618 | if (asrr0 != -1) { | |
619 | env->spr[asrr0] = env->spr[srr0]; | |
620 | } | |
621 | if (asrr1 != -1) { | |
622 | env->spr[asrr1] = env->spr[srr1]; | |
623 | } | |
d5ac4f54 | 624 | |
6d49d6d4 BH |
625 | /* Sort out endianness of interrupt, this differs depending on the |
626 | * CPU, the HV mode, etc... | |
627 | */ | |
1e0c7e55 | 628 | #ifdef TARGET_PPC64 |
6d49d6d4 BH |
629 | if (excp_model == POWERPC_EXCP_POWER7) { |
630 | if (!(new_msr & MSR_HVB) && (env->spr[SPR_LPCR] & LPCR_ILE)) { | |
631 | new_msr |= (target_ulong)1 << MSR_LE; | |
632 | } | |
633 | } else if (excp_model == POWERPC_EXCP_POWER8) { | |
634 | if (new_msr & MSR_HVB) { | |
635 | if (env->spr[SPR_HID0] & HID0_HILE) { | |
636 | new_msr |= (target_ulong)1 << MSR_LE; | |
637 | } | |
638 | } else if (env->spr[SPR_LPCR] & LPCR_ILE) { | |
1e0c7e55 AB |
639 | new_msr |= (target_ulong)1 << MSR_LE; |
640 | } | |
641 | } else if (msr_ile) { | |
642 | new_msr |= (target_ulong)1 << MSR_LE; | |
643 | } | |
644 | #else | |
c79c73f6 BS |
645 | if (msr_ile) { |
646 | new_msr |= (target_ulong)1 << MSR_LE; | |
647 | } | |
1e0c7e55 | 648 | #endif |
c79c73f6 BS |
649 | |
650 | /* Jump to handler */ | |
651 | vector = env->excp_vectors[excp]; | |
652 | if (vector == (target_ulong)-1ULL) { | |
a47dddd7 | 653 | cpu_abort(cs, "Raised an exception without defined vector %d\n", |
c79c73f6 BS |
654 | excp); |
655 | } | |
656 | vector |= env->excp_prefix; | |
5c94b2a5 CLG |
657 | |
658 | /* AIL only works if there is no HV transition and we are running with | |
659 | * translations enabled | |
660 | */ | |
6d49d6d4 BH |
661 | if (!((msr >> MSR_IR) & 1) || !((msr >> MSR_DR) & 1) || |
662 | ((new_msr & MSR_HVB) && !(msr & MSR_HVB))) { | |
5c94b2a5 CLG |
663 | ail = 0; |
664 | } | |
665 | /* Handle AIL */ | |
666 | if (ail) { | |
667 | new_msr |= (1 << MSR_IR) | (1 << MSR_DR); | |
668 | switch(ail) { | |
669 | case AIL_0001_8000: | |
670 | vector |= 0x18000; | |
671 | break; | |
672 | case AIL_C000_0000_0000_4000: | |
673 | vector |= 0xc000000000004000ull; | |
674 | break; | |
675 | default: | |
676 | cpu_abort(cs, "Invalid AIL combination %d\n", ail); | |
677 | break; | |
678 | } | |
679 | } | |
680 | ||
c79c73f6 BS |
681 | #if defined(TARGET_PPC64) |
682 | if (excp_model == POWERPC_EXCP_BOOKE) { | |
e42a61f1 AG |
683 | if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) { |
684 | /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */ | |
c79c73f6 | 685 | new_msr |= (target_ulong)1 << MSR_CM; |
e42a61f1 AG |
686 | } else { |
687 | vector = (uint32_t)vector; | |
c79c73f6 BS |
688 | } |
689 | } else { | |
690 | if (!msr_isf && !(env->mmu_model & POWERPC_MMU_64)) { | |
691 | vector = (uint32_t)vector; | |
692 | } else { | |
693 | new_msr |= (target_ulong)1 << MSR_SF; | |
694 | } | |
695 | } | |
696 | #endif | |
1c953ba5 BH |
697 | /* We don't use hreg_store_msr here as already have treated |
698 | * any special case that could occur. Just store MSR and update hflags | |
699 | * | |
700 | * Note: We *MUST* not use hreg_store_msr() as-is anyway because it | |
701 | * will prevent setting of the HV bit which some exceptions might need | |
702 | * to do. | |
c79c73f6 BS |
703 | */ |
704 | env->msr = new_msr & env->msr_mask; | |
705 | hreg_compute_hflags(env); | |
706 | env->nip = vector; | |
707 | /* Reset exception state */ | |
27103424 | 708 | cs->exception_index = POWERPC_EXCP_NONE; |
c79c73f6 | 709 | env->error_code = 0; |
cd0c6f47 BH |
710 | |
711 | /* Any interrupt is context synchronizing, check if TCG TLB | |
712 | * needs a delayed flush on ppc64 | |
713 | */ | |
e3cffe6f | 714 | check_tlb_flush(env, false); |
c79c73f6 BS |
715 | } |
716 | ||
97a8ea5a | 717 | void ppc_cpu_do_interrupt(CPUState *cs) |
c79c73f6 | 718 | { |
97a8ea5a AF |
719 | PowerPCCPU *cpu = POWERPC_CPU(cs); |
720 | CPUPPCState *env = &cpu->env; | |
5c26a5b3 | 721 | |
27103424 | 722 | powerpc_excp(cpu, env->excp_model, cs->exception_index); |
c79c73f6 BS |
723 | } |
724 | ||
458dd766 | 725 | static void ppc_hw_interrupt(CPUPPCState *env) |
c79c73f6 | 726 | { |
5c26a5b3 | 727 | PowerPCCPU *cpu = ppc_env_get_cpu(env); |
c79c73f6 | 728 | #if 0 |
259186a7 AF |
729 | CPUState *cs = CPU(cpu); |
730 | ||
c79c73f6 | 731 | qemu_log_mask(CPU_LOG_INT, "%s: %p pending %08x req %08x me %d ee %d\n", |
259186a7 AF |
732 | __func__, env, env->pending_interrupts, |
733 | cs->interrupt_request, (int)msr_me, (int)msr_ee); | |
c79c73f6 BS |
734 | #endif |
735 | /* External reset */ | |
736 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) { | |
737 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET); | |
5c26a5b3 | 738 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_RESET); |
c79c73f6 BS |
739 | return; |
740 | } | |
741 | /* Machine check exception */ | |
742 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) { | |
743 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK); | |
5c26a5b3 | 744 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_MCHECK); |
c79c73f6 BS |
745 | return; |
746 | } | |
747 | #if 0 /* TODO */ | |
748 | /* External debug exception */ | |
749 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) { | |
750 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG); | |
5c26a5b3 | 751 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_DEBUG); |
c79c73f6 BS |
752 | return; |
753 | } | |
754 | #endif | |
4b236b62 BH |
755 | /* Hypervisor decrementer exception */ |
756 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) { | |
757 | /* LPCR will be clear when not supported so this will work */ | |
758 | bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); | |
759 | if ((msr_ee != 0 || msr_hv == 0) && hdice) { | |
760 | /* HDEC clears on delivery */ | |
761 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR); | |
5c26a5b3 | 762 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_HDECR); |
c79c73f6 BS |
763 | return; |
764 | } | |
765 | } | |
d1dbe37c BH |
766 | /* Extermal interrupt can ignore MSR:EE under some circumstances */ |
767 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) { | |
768 | bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); | |
769 | if (msr_ee != 0 || (env->has_hv_mode && msr_hv == 0 && !lpes0)) { | |
770 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_EXTERNAL); | |
771 | return; | |
772 | } | |
773 | } | |
c79c73f6 BS |
774 | if (msr_ce != 0) { |
775 | /* External critical interrupt */ | |
776 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) { | |
777 | /* Taking a critical external interrupt does not clear the external | |
778 | * critical interrupt status | |
779 | */ | |
780 | #if 0 | |
781 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CEXT); | |
782 | #endif | |
5c26a5b3 | 783 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_CRITICAL); |
c79c73f6 BS |
784 | return; |
785 | } | |
786 | } | |
787 | if (msr_ee != 0) { | |
788 | /* Watchdog timer on embedded PowerPC */ | |
789 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) { | |
790 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT); | |
5c26a5b3 | 791 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_WDT); |
c79c73f6 BS |
792 | return; |
793 | } | |
794 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) { | |
795 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL); | |
5c26a5b3 | 796 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_DOORCI); |
c79c73f6 BS |
797 | return; |
798 | } | |
799 | /* Fixed interval timer on embedded PowerPC */ | |
800 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) { | |
801 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT); | |
5c26a5b3 | 802 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_FIT); |
c79c73f6 BS |
803 | return; |
804 | } | |
805 | /* Programmable interval timer on embedded PowerPC */ | |
806 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) { | |
807 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT); | |
5c26a5b3 | 808 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_PIT); |
c79c73f6 BS |
809 | return; |
810 | } | |
811 | /* Decrementer exception */ | |
812 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) { | |
e81a982a AG |
813 | if (ppc_decr_clear_on_delivery(env)) { |
814 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR); | |
815 | } | |
5c26a5b3 | 816 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_DECR); |
c79c73f6 BS |
817 | return; |
818 | } | |
c79c73f6 BS |
819 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) { |
820 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL); | |
5c26a5b3 | 821 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_DOORI); |
c79c73f6 BS |
822 | return; |
823 | } | |
824 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) { | |
825 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM); | |
5c26a5b3 | 826 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_PERFM); |
c79c73f6 BS |
827 | return; |
828 | } | |
829 | /* Thermal interrupt */ | |
830 | if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) { | |
831 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM); | |
5c26a5b3 | 832 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_THERM); |
c79c73f6 BS |
833 | return; |
834 | } | |
835 | } | |
836 | } | |
34316482 AK |
837 | |
838 | void ppc_cpu_do_system_reset(CPUState *cs) | |
839 | { | |
840 | PowerPCCPU *cpu = POWERPC_CPU(cs); | |
841 | CPUPPCState *env = &cpu->env; | |
842 | ||
843 | powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_RESET); | |
844 | } | |
c79c73f6 BS |
845 | #endif /* !CONFIG_USER_ONLY */ |
846 | ||
458dd766 RH |
847 | bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request) |
848 | { | |
849 | PowerPCCPU *cpu = POWERPC_CPU(cs); | |
850 | CPUPPCState *env = &cpu->env; | |
851 | ||
852 | if (interrupt_request & CPU_INTERRUPT_HARD) { | |
853 | ppc_hw_interrupt(env); | |
854 | if (env->pending_interrupts == 0) { | |
855 | cs->interrupt_request &= ~CPU_INTERRUPT_HARD; | |
856 | } | |
857 | return true; | |
858 | } | |
859 | return false; | |
860 | } | |
861 | ||
c79c73f6 BS |
862 | #if defined(DEBUG_OP) |
863 | static void cpu_dump_rfi(target_ulong RA, target_ulong msr) | |
864 | { | |
865 | qemu_log("Return from exception at " TARGET_FMT_lx " with flags " | |
866 | TARGET_FMT_lx "\n", RA, msr); | |
867 | } | |
868 | #endif | |
869 | ||
ad71ed68 BS |
870 | /*****************************************************************************/ |
871 | /* Exceptions processing helpers */ | |
872 | ||
db789c6c BH |
873 | void raise_exception_err_ra(CPUPPCState *env, uint32_t exception, |
874 | uint32_t error_code, uintptr_t raddr) | |
ad71ed68 | 875 | { |
27103424 AF |
876 | CPUState *cs = CPU(ppc_env_get_cpu(env)); |
877 | ||
27103424 | 878 | cs->exception_index = exception; |
ad71ed68 | 879 | env->error_code = error_code; |
db789c6c BH |
880 | cpu_loop_exit_restore(cs, raddr); |
881 | } | |
882 | ||
883 | void raise_exception_err(CPUPPCState *env, uint32_t exception, | |
884 | uint32_t error_code) | |
885 | { | |
886 | raise_exception_err_ra(env, exception, error_code, 0); | |
887 | } | |
888 | ||
889 | void raise_exception(CPUPPCState *env, uint32_t exception) | |
890 | { | |
891 | raise_exception_err_ra(env, exception, 0, 0); | |
892 | } | |
893 | ||
894 | void raise_exception_ra(CPUPPCState *env, uint32_t exception, | |
895 | uintptr_t raddr) | |
896 | { | |
897 | raise_exception_err_ra(env, exception, 0, raddr); | |
898 | } | |
899 | ||
900 | void helper_raise_exception_err(CPUPPCState *env, uint32_t exception, | |
901 | uint32_t error_code) | |
902 | { | |
903 | raise_exception_err_ra(env, exception, error_code, 0); | |
ad71ed68 BS |
904 | } |
905 | ||
e5f17ac6 | 906 | void helper_raise_exception(CPUPPCState *env, uint32_t exception) |
ad71ed68 | 907 | { |
db789c6c | 908 | raise_exception_err_ra(env, exception, 0, 0); |
ad71ed68 BS |
909 | } |
910 | ||
911 | #if !defined(CONFIG_USER_ONLY) | |
e5f17ac6 | 912 | void helper_store_msr(CPUPPCState *env, target_ulong val) |
ad71ed68 | 913 | { |
db789c6c | 914 | uint32_t excp = hreg_store_msr(env, val, 0); |
259186a7 | 915 | |
db789c6c BH |
916 | if (excp != 0) { |
917 | CPUState *cs = CPU(ppc_env_get_cpu(env)); | |
259186a7 | 918 | cs->interrupt_request |= CPU_INTERRUPT_EXITTB; |
db789c6c | 919 | raise_exception(env, excp); |
ad71ed68 BS |
920 | } |
921 | } | |
922 | ||
7778a575 BH |
923 | #if defined(TARGET_PPC64) |
924 | void helper_pminsn(CPUPPCState *env, powerpc_pm_insn_t insn) | |
925 | { | |
926 | CPUState *cs; | |
927 | ||
928 | cs = CPU(ppc_env_get_cpu(env)); | |
929 | cs->halted = 1; | |
930 | env->in_pm_state = true; | |
931 | ||
4b236b62 BH |
932 | /* The architecture specifies that HDEC interrupts are |
933 | * discarded in PM states | |
934 | */ | |
935 | env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR); | |
936 | ||
7778a575 BH |
937 | /* Technically, nap doesn't set EE, but if we don't set it |
938 | * then ppc_hw_interrupt() won't deliver. We could add some | |
939 | * other tests there based on LPCR but it's simpler to just | |
940 | * whack EE in. It will be cleared by the 0x100 at wakeup | |
941 | * anyway. It will still be observable by the guest in SRR1 | |
942 | * but this doesn't seem to be a problem. | |
943 | */ | |
944 | env->msr |= (1ull << MSR_EE); | |
db789c6c | 945 | raise_exception(env, EXCP_HLT); |
7778a575 BH |
946 | } |
947 | #endif /* defined(TARGET_PPC64) */ | |
948 | ||
a2e71b28 | 949 | static inline void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr) |
ad71ed68 | 950 | { |
259186a7 AF |
951 | CPUState *cs = CPU(ppc_env_get_cpu(env)); |
952 | ||
a2e71b28 BH |
953 | /* MSR:POW cannot be set by any form of rfi */ |
954 | msr &= ~(1ULL << MSR_POW); | |
955 | ||
ad71ed68 | 956 | #if defined(TARGET_PPC64) |
a2e71b28 BH |
957 | /* Switching to 32-bit ? Crop the nip */ |
958 | if (!msr_is_64bit(env, msr)) { | |
ad71ed68 | 959 | nip = (uint32_t)nip; |
ad71ed68 BS |
960 | } |
961 | #else | |
962 | nip = (uint32_t)nip; | |
ad71ed68 BS |
963 | #endif |
964 | /* XXX: beware: this is false if VLE is supported */ | |
965 | env->nip = nip & ~((target_ulong)0x00000003); | |
966 | hreg_store_msr(env, msr, 1); | |
967 | #if defined(DEBUG_OP) | |
968 | cpu_dump_rfi(env->nip, env->msr); | |
969 | #endif | |
970 | /* No need to raise an exception here, | |
971 | * as rfi is always the last insn of a TB | |
972 | */ | |
259186a7 | 973 | cs->interrupt_request |= CPU_INTERRUPT_EXITTB; |
cd0c6f47 BH |
974 | |
975 | /* Context synchronizing: check if TCG TLB needs flush */ | |
e3cffe6f | 976 | check_tlb_flush(env, false); |
ad71ed68 BS |
977 | } |
978 | ||
e5f17ac6 | 979 | void helper_rfi(CPUPPCState *env) |
ad71ed68 | 980 | { |
a2e71b28 | 981 | do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful); |
ad71ed68 BS |
982 | } |
983 | ||
a2e71b28 | 984 | #define MSR_BOOK3S_MASK |
ad71ed68 | 985 | #if defined(TARGET_PPC64) |
e5f17ac6 | 986 | void helper_rfid(CPUPPCState *env) |
ad71ed68 | 987 | { |
a2e71b28 BH |
988 | /* The architeture defines a number of rules for which bits |
989 | * can change but in practice, we handle this in hreg_store_msr() | |
990 | * which will be called by do_rfi(), so there is no need to filter | |
991 | * here | |
992 | */ | |
993 | do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]); | |
ad71ed68 BS |
994 | } |
995 | ||
e5f17ac6 | 996 | void helper_hrfid(CPUPPCState *env) |
ad71ed68 | 997 | { |
a2e71b28 | 998 | do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]); |
ad71ed68 BS |
999 | } |
1000 | #endif | |
1001 | ||
1002 | /*****************************************************************************/ | |
1003 | /* Embedded PowerPC specific helpers */ | |
e5f17ac6 | 1004 | void helper_40x_rfci(CPUPPCState *env) |
ad71ed68 | 1005 | { |
a2e71b28 | 1006 | do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]); |
ad71ed68 BS |
1007 | } |
1008 | ||
e5f17ac6 | 1009 | void helper_rfci(CPUPPCState *env) |
ad71ed68 | 1010 | { |
a2e71b28 | 1011 | do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]); |
ad71ed68 BS |
1012 | } |
1013 | ||
e5f17ac6 | 1014 | void helper_rfdi(CPUPPCState *env) |
ad71ed68 | 1015 | { |
a1bb7384 | 1016 | /* FIXME: choose CSRR1 or DSRR1 based on cpu type */ |
a2e71b28 | 1017 | do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]); |
ad71ed68 BS |
1018 | } |
1019 | ||
e5f17ac6 | 1020 | void helper_rfmci(CPUPPCState *env) |
ad71ed68 | 1021 | { |
a1bb7384 | 1022 | /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */ |
a2e71b28 | 1023 | do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]); |
ad71ed68 BS |
1024 | } |
1025 | #endif | |
1026 | ||
e5f17ac6 BS |
1027 | void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2, |
1028 | uint32_t flags) | |
ad71ed68 BS |
1029 | { |
1030 | if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) || | |
1031 | ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) || | |
1032 | ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) || | |
1033 | ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) || | |
1034 | ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) { | |
72073dcc BH |
1035 | raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, |
1036 | POWERPC_EXCP_TRAP, GETPC()); | |
ad71ed68 BS |
1037 | } |
1038 | } | |
1039 | ||
1040 | #if defined(TARGET_PPC64) | |
e5f17ac6 BS |
1041 | void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2, |
1042 | uint32_t flags) | |
ad71ed68 BS |
1043 | { |
1044 | if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) || | |
1045 | ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) || | |
1046 | ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) || | |
1047 | ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) || | |
1048 | ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) { | |
72073dcc BH |
1049 | raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, |
1050 | POWERPC_EXCP_TRAP, GETPC()); | |
ad71ed68 BS |
1051 | } |
1052 | } | |
1053 | #endif | |
1054 | ||
1055 | #if !defined(CONFIG_USER_ONLY) | |
1056 | /*****************************************************************************/ | |
1057 | /* PowerPC 601 specific instructions (POWER bridge) */ | |
1058 | ||
e5f17ac6 | 1059 | void helper_rfsvc(CPUPPCState *env) |
ad71ed68 | 1060 | { |
a2e71b28 | 1061 | do_rfi(env, env->lr, env->ctr & 0x0000FFFF); |
ad71ed68 BS |
1062 | } |
1063 | ||
1064 | /* Embedded.Processor Control */ | |
1065 | static int dbell2irq(target_ulong rb) | |
1066 | { | |
1067 | int msg = rb & DBELL_TYPE_MASK; | |
1068 | int irq = -1; | |
1069 | ||
1070 | switch (msg) { | |
1071 | case DBELL_TYPE_DBELL: | |
1072 | irq = PPC_INTERRUPT_DOORBELL; | |
1073 | break; | |
1074 | case DBELL_TYPE_DBELL_CRIT: | |
1075 | irq = PPC_INTERRUPT_CDOORBELL; | |
1076 | break; | |
1077 | case DBELL_TYPE_G_DBELL: | |
1078 | case DBELL_TYPE_G_DBELL_CRIT: | |
1079 | case DBELL_TYPE_G_DBELL_MC: | |
1080 | /* XXX implement */ | |
1081 | default: | |
1082 | break; | |
1083 | } | |
1084 | ||
1085 | return irq; | |
1086 | } | |
1087 | ||
e5f17ac6 | 1088 | void helper_msgclr(CPUPPCState *env, target_ulong rb) |
ad71ed68 BS |
1089 | { |
1090 | int irq = dbell2irq(rb); | |
1091 | ||
1092 | if (irq < 0) { | |
1093 | return; | |
1094 | } | |
1095 | ||
1096 | env->pending_interrupts &= ~(1 << irq); | |
1097 | } | |
1098 | ||
1099 | void helper_msgsnd(target_ulong rb) | |
1100 | { | |
1101 | int irq = dbell2irq(rb); | |
1102 | int pir = rb & DBELL_PIRTAG_MASK; | |
182735ef | 1103 | CPUState *cs; |
ad71ed68 BS |
1104 | |
1105 | if (irq < 0) { | |
1106 | return; | |
1107 | } | |
1108 | ||
bdc44640 | 1109 | CPU_FOREACH(cs) { |
182735ef AF |
1110 | PowerPCCPU *cpu = POWERPC_CPU(cs); |
1111 | CPUPPCState *cenv = &cpu->env; | |
1112 | ||
ad71ed68 BS |
1113 | if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) { |
1114 | cenv->pending_interrupts |= 1 << irq; | |
182735ef | 1115 | cpu_interrupt(cs, CPU_INTERRUPT_HARD); |
ad71ed68 BS |
1116 | } |
1117 | } | |
1118 | } | |
1119 | #endif |