1 /* 2 * qemu user cpu loop 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program 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 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "qemu/osdep.h" 21 #include "qemu-common.h" 22 #include "qemu.h" 23 #include "qemu/timer.h" 24 #include "user-internals.h" 25 #include "cpu_loop-common.h" 26 #include "signal-common.h" 27 28 static inline uint64_t cpu_ppc_get_tb(CPUPPCState *env) 29 { 30 return cpu_get_host_ticks(); 31 } 32 33 uint64_t cpu_ppc_load_tbl(CPUPPCState *env) 34 { 35 return cpu_ppc_get_tb(env); 36 } 37 38 uint32_t cpu_ppc_load_tbu(CPUPPCState *env) 39 { 40 return cpu_ppc_get_tb(env) >> 32; 41 } 42 43 uint64_t cpu_ppc_load_atbl(CPUPPCState *env) 44 { 45 return cpu_ppc_get_tb(env); 46 } 47 48 uint32_t cpu_ppc_load_atbu(CPUPPCState *env) 49 { 50 return cpu_ppc_get_tb(env) >> 32; 51 } 52 53 uint64_t cpu_ppc_load_vtb(CPUPPCState *env) 54 { 55 return cpu_ppc_get_tb(env); 56 } 57 58 /* XXX: to be fixed */ 59 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp) 60 { 61 return -1; 62 } 63 64 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val) 65 { 66 return -1; 67 } 68 69 void cpu_loop(CPUPPCState *env) 70 { 71 CPUState *cs = env_cpu(env); 72 int trapnr, si_signo, si_code; 73 target_ulong ret; 74 75 for(;;) { 76 bool arch_interrupt; 77 78 cpu_exec_start(cs); 79 trapnr = cpu_exec(cs); 80 cpu_exec_end(cs); 81 process_queued_cpu_work(cs); 82 83 arch_interrupt = true; 84 switch (trapnr) { 85 case POWERPC_EXCP_NONE: 86 /* Just go on */ 87 break; 88 case POWERPC_EXCP_CRITICAL: /* Critical input */ 89 cpu_abort(cs, "Critical interrupt while in user mode. " 90 "Aborting\n"); 91 break; 92 case POWERPC_EXCP_MCHECK: /* Machine check exception */ 93 cpu_abort(cs, "Machine check exception while in user mode. " 94 "Aborting\n"); 95 break; 96 case POWERPC_EXCP_DSI: /* Data storage exception */ 97 case POWERPC_EXCP_ISI: /* Instruction storage exception */ 98 /* FIXME: handle maperr in ppc_cpu_record_sigsegv. */ 99 force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MAPERR, 100 env->spr[SPR_DAR]); 101 break; 102 case POWERPC_EXCP_EXTERNAL: /* External input */ 103 cpu_abort(cs, "External interrupt while in user mode. " 104 "Aborting\n"); 105 break; 106 case POWERPC_EXCP_PROGRAM: /* Program exception */ 107 case POWERPC_EXCP_HV_EMU: /* HV emulation */ 108 /* XXX: check this */ 109 switch (env->error_code & ~0xF) { 110 case POWERPC_EXCP_FP: 111 si_signo = TARGET_SIGFPE; 112 switch (env->error_code & 0xF) { 113 case POWERPC_EXCP_FP_OX: 114 si_code = TARGET_FPE_FLTOVF; 115 break; 116 case POWERPC_EXCP_FP_UX: 117 si_code = TARGET_FPE_FLTUND; 118 break; 119 case POWERPC_EXCP_FP_ZX: 120 case POWERPC_EXCP_FP_VXZDZ: 121 si_code = TARGET_FPE_FLTDIV; 122 break; 123 case POWERPC_EXCP_FP_XX: 124 si_code = TARGET_FPE_FLTRES; 125 break; 126 case POWERPC_EXCP_FP_VXSOFT: 127 si_code = TARGET_FPE_FLTINV; 128 break; 129 case POWERPC_EXCP_FP_VXSNAN: 130 case POWERPC_EXCP_FP_VXISI: 131 case POWERPC_EXCP_FP_VXIDI: 132 case POWERPC_EXCP_FP_VXIMZ: 133 case POWERPC_EXCP_FP_VXVC: 134 case POWERPC_EXCP_FP_VXSQRT: 135 case POWERPC_EXCP_FP_VXCVI: 136 si_code = TARGET_FPE_FLTSUB; 137 break; 138 default: 139 EXCP_DUMP(env, "Unknown floating point exception (%02x)\n", 140 env->error_code); 141 si_code = 0; 142 break; 143 } 144 break; 145 case POWERPC_EXCP_INVAL: 146 si_signo = TARGET_SIGILL; 147 switch (env->error_code & 0xF) { 148 case POWERPC_EXCP_INVAL_INVAL: 149 si_code = TARGET_ILL_ILLOPC; 150 break; 151 case POWERPC_EXCP_INVAL_LSWX: 152 si_code = TARGET_ILL_ILLOPN; 153 break; 154 case POWERPC_EXCP_INVAL_SPR: 155 si_code = TARGET_ILL_PRVREG; 156 break; 157 case POWERPC_EXCP_INVAL_FP: 158 si_code = TARGET_ILL_COPROC; 159 break; 160 default: 161 EXCP_DUMP(env, "Unknown invalid operation (%02x)\n", 162 env->error_code & 0xF); 163 si_code = TARGET_ILL_ILLADR; 164 break; 165 } 166 break; 167 case POWERPC_EXCP_PRIV: 168 si_signo = TARGET_SIGILL; 169 switch (env->error_code & 0xF) { 170 case POWERPC_EXCP_PRIV_OPC: 171 si_code = TARGET_ILL_PRVOPC; 172 break; 173 case POWERPC_EXCP_PRIV_REG: 174 si_code = TARGET_ILL_PRVREG; 175 break; 176 default: 177 EXCP_DUMP(env, "Unknown privilege violation (%02x)\n", 178 env->error_code & 0xF); 179 si_code = TARGET_ILL_PRVOPC; 180 break; 181 } 182 break; 183 case POWERPC_EXCP_TRAP: 184 cpu_abort(cs, "Tried to call a TRAP\n"); 185 break; 186 default: 187 /* Should not happen ! */ 188 cpu_abort(cs, "Unknown program exception (%02x)\n", 189 env->error_code); 190 break; 191 } 192 force_sig_fault(si_signo, si_code, env->nip); 193 break; 194 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 195 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */ 196 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavail. */ 197 case POWERPC_EXCP_VPU: /* Vector unavailable exception */ 198 force_sig_fault(TARGET_SIGILL, TARGET_ILL_COPROC, env->nip); 199 break; 200 case POWERPC_EXCP_SYSCALL: /* System call exception */ 201 case POWERPC_EXCP_SYSCALL_VECTORED: 202 cpu_abort(cs, "Syscall exception while in user mode. " 203 "Aborting\n"); 204 break; 205 case POWERPC_EXCP_DECR: /* Decrementer exception */ 206 cpu_abort(cs, "Decrementer interrupt while in user mode. " 207 "Aborting\n"); 208 break; 209 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */ 210 cpu_abort(cs, "Fix interval timer interrupt while in user mode. " 211 "Aborting\n"); 212 break; 213 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */ 214 cpu_abort(cs, "Watchdog timer interrupt while in user mode. " 215 "Aborting\n"); 216 break; 217 case POWERPC_EXCP_DTLB: /* Data TLB error */ 218 cpu_abort(cs, "Data TLB exception while in user mode. " 219 "Aborting\n"); 220 break; 221 case POWERPC_EXCP_ITLB: /* Instruction TLB error */ 222 cpu_abort(cs, "Instruction TLB exception while in user mode. " 223 "Aborting\n"); 224 break; 225 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data IRQ */ 226 cpu_abort(cs, "Embedded floating-point data IRQ not handled\n"); 227 break; 228 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round IRQ */ 229 cpu_abort(cs, "Embedded floating-point round IRQ not handled\n"); 230 break; 231 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor IRQ */ 232 cpu_abort(cs, "Performance monitor exception not handled\n"); 233 break; 234 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */ 235 cpu_abort(cs, "Doorbell interrupt while in user mode. " 236 "Aborting\n"); 237 break; 238 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */ 239 cpu_abort(cs, "Doorbell critical interrupt while in user mode. " 240 "Aborting\n"); 241 break; 242 case POWERPC_EXCP_RESET: /* System reset exception */ 243 cpu_abort(cs, "Reset interrupt while in user mode. " 244 "Aborting\n"); 245 break; 246 case POWERPC_EXCP_DSEG: /* Data segment exception */ 247 cpu_abort(cs, "Data segment exception while in user mode. " 248 "Aborting\n"); 249 break; 250 case POWERPC_EXCP_ISEG: /* Instruction segment exception */ 251 cpu_abort(cs, "Instruction segment exception " 252 "while in user mode. Aborting\n"); 253 break; 254 /* PowerPC 64 with hypervisor mode support */ 255 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */ 256 cpu_abort(cs, "Hypervisor decrementer interrupt " 257 "while in user mode. Aborting\n"); 258 break; 259 case POWERPC_EXCP_TRACE: /* Trace exception */ 260 /* Nothing to do: 261 * we use this exception to emulate step-by-step execution mode. 262 */ 263 break; 264 /* PowerPC 64 with hypervisor mode support */ 265 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */ 266 cpu_abort(cs, "Hypervisor data storage exception " 267 "while in user mode. Aborting\n"); 268 break; 269 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage excp */ 270 cpu_abort(cs, "Hypervisor instruction storage exception " 271 "while in user mode. Aborting\n"); 272 break; 273 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */ 274 cpu_abort(cs, "Hypervisor data segment exception " 275 "while in user mode. Aborting\n"); 276 break; 277 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment excp */ 278 cpu_abort(cs, "Hypervisor instruction segment exception " 279 "while in user mode. Aborting\n"); 280 break; 281 case POWERPC_EXCP_PIT: /* Programmable interval timer IRQ */ 282 cpu_abort(cs, "Programmable interval timer interrupt " 283 "while in user mode. Aborting\n"); 284 break; 285 case POWERPC_EXCP_EMUL: /* Emulation trap exception */ 286 cpu_abort(cs, "Emulation trap exception not handled\n"); 287 break; 288 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */ 289 cpu_abort(cs, "Instruction fetch TLB exception " 290 "while in user-mode. Aborting"); 291 break; 292 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */ 293 cpu_abort(cs, "Data load TLB exception while in user-mode. " 294 "Aborting"); 295 break; 296 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */ 297 cpu_abort(cs, "Data store TLB exception while in user-mode. " 298 "Aborting"); 299 break; 300 case POWERPC_EXCP_FPA: /* Floating-point assist exception */ 301 cpu_abort(cs, "Floating-point assist exception not handled\n"); 302 break; 303 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */ 304 cpu_abort(cs, "Instruction address breakpoint exception " 305 "not handled\n"); 306 break; 307 case POWERPC_EXCP_SMI: /* System management interrupt */ 308 cpu_abort(cs, "System management interrupt while in user mode. " 309 "Aborting\n"); 310 break; 311 case POWERPC_EXCP_THERM: /* Thermal interrupt */ 312 cpu_abort(cs, "Thermal interrupt interrupt while in user mode. " 313 "Aborting\n"); 314 break; 315 case POWERPC_EXCP_PERFM: /* Embedded performance monitor IRQ */ 316 cpu_abort(cs, "Performance monitor exception not handled\n"); 317 break; 318 case POWERPC_EXCP_VPUA: /* Vector assist exception */ 319 cpu_abort(cs, "Vector assist exception not handled\n"); 320 break; 321 case POWERPC_EXCP_SOFTP: /* Soft patch exception */ 322 cpu_abort(cs, "Soft patch exception not handled\n"); 323 break; 324 case POWERPC_EXCP_MAINT: /* Maintenance exception */ 325 cpu_abort(cs, "Maintenance exception while in user mode. " 326 "Aborting\n"); 327 break; 328 case POWERPC_EXCP_SYSCALL_USER: 329 /* system call in user-mode emulation */ 330 /* WARNING: 331 * PPC ABI uses overflow flag in cr0 to signal an error 332 * in syscalls. 333 */ 334 env->crf[0] &= ~0x1; 335 env->nip += 4; 336 ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4], 337 env->gpr[5], env->gpr[6], env->gpr[7], 338 env->gpr[8], 0, 0); 339 if (ret == -QEMU_ERESTARTSYS) { 340 env->nip -= 4; 341 break; 342 } 343 if (ret == (target_ulong)(-QEMU_ESIGRETURN)) { 344 /* Returning from a successful sigreturn syscall. 345 Avoid corrupting register state. */ 346 break; 347 } 348 if (ret > (target_ulong)(-515)) { 349 env->crf[0] |= 0x1; 350 ret = -ret; 351 } 352 env->gpr[3] = ret; 353 break; 354 case EXCP_DEBUG: 355 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->nip); 356 break; 357 case EXCP_INTERRUPT: 358 /* just indicate that signals should be handled asap */ 359 break; 360 case EXCP_ATOMIC: 361 cpu_exec_step_atomic(cs); 362 arch_interrupt = false; 363 break; 364 default: 365 cpu_abort(cs, "Unknown exception 0x%x. Aborting\n", trapnr); 366 break; 367 } 368 process_pending_signals(env); 369 370 /* Most of the traps imply a transition through kernel mode, 371 * which implies an REI instruction has been executed. Which 372 * means that RX and LOCK_ADDR should be cleared. But there 373 * are a few exceptions for traps internal to QEMU. 374 */ 375 if (arch_interrupt) { 376 env->reserve_addr = -1; 377 } 378 } 379 } 380 381 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs) 382 { 383 int i; 384 385 #if defined(TARGET_PPC64) 386 int flag = (env->insns_flags2 & PPC2_BOOKE206) ? MSR_CM : MSR_SF; 387 #if defined(TARGET_ABI32) 388 ppc_store_msr(env, env->msr & ~((target_ulong)1 << flag)); 389 #else 390 ppc_store_msr(env, env->msr | (target_ulong)1 << flag); 391 #endif 392 #endif 393 394 env->nip = regs->nip; 395 for(i = 0; i < 32; i++) { 396 env->gpr[i] = regs->gpr[i]; 397 } 398 } 399