cpu_loop.c (5329B)
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 "cpu_loop-common.h" 24 #include "signal-common.h" 25 26 void cpu_loop(CPUNios2State *env) 27 { 28 CPUState *cs = env_cpu(env); 29 int trapnr, ret; 30 31 for (;;) { 32 cpu_exec_start(cs); 33 trapnr = cpu_exec(cs); 34 cpu_exec_end(cs); 35 36 switch (trapnr) { 37 case EXCP_INTERRUPT: 38 /* just indicate that signals should be handled asap */ 39 break; 40 41 case EXCP_DIV: 42 /* Match kernel's handle_diverror_c(). */ 43 env->pc -= 4; 44 force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTDIV, env->pc); 45 break; 46 47 case EXCP_UNALIGN: 48 case EXCP_UNALIGND: 49 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, 50 env->ctrl[CR_BADADDR]); 51 break; 52 53 case EXCP_ILLEGAL: 54 case EXCP_UNIMPL: 55 /* Match kernel's handle_illegal_c(). */ 56 env->pc -= 4; 57 force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPC, env->pc); 58 break; 59 case EXCP_SUPERI: 60 /* Match kernel's handle_supervisor_instr(). */ 61 env->pc -= 4; 62 force_sig_fault(TARGET_SIGILL, TARGET_ILL_PRVOPC, env->pc); 63 break; 64 65 case EXCP_TRAP: 66 switch (env->error_code) { 67 case 0: 68 qemu_log_mask(CPU_LOG_INT, "\nSyscall\n"); 69 70 ret = do_syscall(env, env->regs[2], 71 env->regs[4], env->regs[5], env->regs[6], 72 env->regs[7], env->regs[8], env->regs[9], 73 0, 0); 74 75 if (ret == -QEMU_ESIGRETURN) { 76 /* rt_sigreturn has set all state. */ 77 break; 78 } 79 if (ret == -QEMU_ERESTARTSYS) { 80 env->pc -= 4; 81 break; 82 } 83 /* 84 * See the code after translate_rc_and_ret: all negative 85 * values are errors (aided by userspace restricted to 2G), 86 * errno is returned positive in r2, and error indication 87 * is a boolean in r7. 88 */ 89 env->regs[2] = abs(ret); 90 env->regs[7] = ret < 0; 91 break; 92 93 case 1: 94 qemu_log_mask(CPU_LOG_INT, "\nTrap 1\n"); 95 force_sig_fault(TARGET_SIGUSR1, 0, env->pc); 96 break; 97 case 2: 98 qemu_log_mask(CPU_LOG_INT, "\nTrap 2\n"); 99 force_sig_fault(TARGET_SIGUSR2, 0, env->pc); 100 break; 101 case 31: 102 qemu_log_mask(CPU_LOG_INT, "\nTrap 31\n"); 103 /* Match kernel's breakpoint_c(). */ 104 env->pc -= 4; 105 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc); 106 break; 107 default: 108 qemu_log_mask(CPU_LOG_INT, "\nTrap %d\n", env->error_code); 109 force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLTRP, env->pc); 110 break; 111 112 case 16: /* QEMU specific, for __kuser_cmpxchg */ 113 { 114 abi_ptr g = env->regs[4]; 115 uint32_t *h, n, o; 116 117 if (g & 0x3) { 118 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, g); 119 break; 120 } 121 ret = page_get_flags(g); 122 if (!(ret & PAGE_VALID)) { 123 force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MAPERR, g); 124 break; 125 } 126 if (!(ret & PAGE_READ) || !(ret & PAGE_WRITE)) { 127 force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_ACCERR, g); 128 break; 129 } 130 h = g2h(cs, g); 131 o = env->regs[5]; 132 n = env->regs[6]; 133 env->regs[2] = qatomic_cmpxchg(h, o, n) - o; 134 } 135 break; 136 } 137 break; 138 139 case EXCP_DEBUG: 140 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc); 141 break; 142 default: 143 EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n", 144 trapnr); 145 abort(); 146 } 147 148 process_pending_signals(env); 149 } 150 } 151 152 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs) 153 { 154 env->regs[R_SP] = regs->sp; 155 env->pc = regs->ea; 156 }