| 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.h" |
| 22 | #include "user-internals.h" |
| 23 | #include "user/cpu_loop.h" |
| 24 | #include "signal-common.h" |
| 25 | |
| 26 | void cpu_loop(CPUMBState *env) |
| 27 | { |
| 28 | int trapnr, ret, si_code, sig; |
| 29 | CPUState *cs = env_cpu(env); |
| 30 | |
| 31 | while (1) { |
| 32 | cpu_exec_start(cs); |
| 33 | trapnr = cpu_exec(cs); |
| 34 | cpu_exec_end(cs); |
| 35 | qemu_process_cpu_events(cs); |
| 36 | |
| 37 | switch (trapnr) { |
| 38 | case EXCP_INTERRUPT: |
| 39 | /* just indicate that signals should be handled asap */ |
| 40 | break; |
| 41 | case EXCP_SYSCALL: |
| 42 | /* Return address is 4 bytes after the call. */ |
| 43 | env->regs[14] += 4; |
| 44 | env->pc = env->regs[14]; |
| 45 | ret = do_syscall(env, |
| 46 | env->regs[12], |
| 47 | env->regs[5], |
| 48 | env->regs[6], |
| 49 | env->regs[7], |
| 50 | env->regs[8], |
| 51 | env->regs[9], |
| 52 | env->regs[10], |
| 53 | 0, 0); |
| 54 | if (ret == -QEMU_ERESTARTSYS) { |
| 55 | /* Wind back to before the syscall. */ |
| 56 | env->pc -= 4; |
| 57 | } else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC) { |
| 58 | env->regs[3] = ret; |
| 59 | } |
| 60 | /* All syscall exits result in guest r14 being equal to the |
| 61 | * PC we return to, because the kernel syscall exit "rtbd" does |
| 62 | * this. (This is true even for sigreturn(); note that r14 is |
| 63 | * not a userspace-usable register, as the kernel may clobber it |
| 64 | * at any point.) |
| 65 | */ |
| 66 | env->regs[14] = env->pc; |
| 67 | break; |
| 68 | |
| 69 | case EXCP_HW_EXCP: |
| 70 | env->regs[17] = env->pc + 4; |
| 71 | if (env->iflags & D_FLAG) { |
| 72 | env->esr |= 1 << 12; |
| 73 | env->pc -= 4; |
| 74 | /* FIXME: if branch was immed, replay the imm as well. */ |
| 75 | } |
| 76 | env->iflags &= ~(IMM_FLAG | D_FLAG); |
| 77 | switch (env->esr & 31) { |
| 78 | case ESR_EC_DIVZERO: |
| 79 | sig = TARGET_SIGFPE; |
| 80 | si_code = TARGET_FPE_INTDIV; |
| 81 | break; |
| 82 | case ESR_EC_FPU: |
| 83 | /* |
| 84 | * Note that the kernel passes along fsr as si_code |
| 85 | * if there's no recognized bit set. Possibly this |
| 86 | * implies that si_code is 0, but follow the structure. |
| 87 | */ |
| 88 | sig = TARGET_SIGFPE; |
| 89 | si_code = env->fsr; |
| 90 | if (si_code & FSR_IO) { |
| 91 | si_code = TARGET_FPE_FLTINV; |
| 92 | } else if (si_code & FSR_OF) { |
| 93 | si_code = TARGET_FPE_FLTOVF; |
| 94 | } else if (si_code & FSR_UF) { |
| 95 | si_code = TARGET_FPE_FLTUND; |
| 96 | } else if (si_code & FSR_DZ) { |
| 97 | si_code = TARGET_FPE_FLTDIV; |
| 98 | } else if (si_code & FSR_DO) { |
| 99 | si_code = TARGET_FPE_FLTRES; |
| 100 | } |
| 101 | break; |
| 102 | case ESR_EC_PRIVINSN: |
| 103 | sig = SIGILL; |
| 104 | si_code = ILL_PRVOPC; |
| 105 | break; |
| 106 | default: |
| 107 | fprintf(stderr, "Unhandled hw-exception: 0x%x\n", |
| 108 | env->esr & ESR_EC_MASK); |
| 109 | cpu_dump_state(cs, stderr, 0); |
| 110 | exit(EXIT_FAILURE); |
| 111 | } |
| 112 | force_sig_fault(sig, si_code, env->pc); |
| 113 | break; |
| 114 | |
| 115 | case EXCP_DEBUG: |
| 116 | force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc); |
| 117 | break; |
| 118 | case EXCP_ATOMIC: |
| 119 | cpu_exec_step_atomic(cs); |
| 120 | break; |
| 121 | default: |
| 122 | fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr); |
| 123 | cpu_dump_state(cs, stderr, 0); |
| 124 | exit(EXIT_FAILURE); |
| 125 | } |
| 126 | process_pending_signals (env); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void init_main_thread(CPUState *cs, struct image_info *info) |
| 131 | { |
| 132 | CPUArchState *env = cpu_env(cs); |
| 133 | |
| 134 | env->pc = info->entry; |
| 135 | env->regs[1] = info->start_stack; |
| 136 | } |