2
0

cpu_loop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #include "qemu/osdep.h"
  20. #include "qemu.h"
  21. #include "user-internals.h"
  22. #include "user/cpu_loop.h"
  23. #include "signal-common.h"
  24. #include "elf.h"
  25. #include "internal.h"
  26. #include "fpu_helper.h"
  27. # ifdef TARGET_ABI_MIPSO32
  28. # define MIPS_SYSCALL_NUMBER_UNUSED -1
  29. static const int8_t mips_syscall_args[] = {
  30. #include "syscall-args-o32.c.inc"
  31. };
  32. # endif /* O32 */
  33. /* Break codes */
  34. enum {
  35. BRK_OVERFLOW = 6,
  36. BRK_DIVZERO = 7
  37. };
  38. static void do_tr_or_bp(CPUMIPSState *env, unsigned int code, bool trap)
  39. {
  40. target_ulong pc = env->active_tc.PC;
  41. switch (code) {
  42. case BRK_OVERFLOW:
  43. force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTOVF, pc);
  44. break;
  45. case BRK_DIVZERO:
  46. force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTDIV, pc);
  47. break;
  48. default:
  49. if (trap) {
  50. force_sig(TARGET_SIGTRAP);
  51. } else {
  52. force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, pc);
  53. }
  54. break;
  55. }
  56. }
  57. void cpu_loop(CPUMIPSState *env)
  58. {
  59. CPUState *cs = env_cpu(env);
  60. int trapnr, si_code;
  61. unsigned int code;
  62. abi_long ret;
  63. # ifdef TARGET_ABI_MIPSO32
  64. unsigned int syscall_num;
  65. # endif
  66. for(;;) {
  67. cpu_exec_start(cs);
  68. trapnr = cpu_exec(cs);
  69. cpu_exec_end(cs);
  70. process_queued_cpu_work(cs);
  71. switch(trapnr) {
  72. case EXCP_SYSCALL:
  73. env->active_tc.PC += 4;
  74. # ifdef TARGET_ABI_MIPSO32
  75. syscall_num = env->active_tc.gpr[2] - 4000;
  76. if (syscall_num >= sizeof(mips_syscall_args)) {
  77. /* syscall_num is larger that any defined for MIPS O32 */
  78. ret = -TARGET_ENOSYS;
  79. } else if (mips_syscall_args[syscall_num] ==
  80. MIPS_SYSCALL_NUMBER_UNUSED) {
  81. /* syscall_num belongs to the range not defined for MIPS O32 */
  82. ret = -TARGET_ENOSYS;
  83. } else {
  84. /* syscall_num is valid */
  85. int nb_args;
  86. abi_ulong sp_reg;
  87. abi_ulong arg5 = 0, arg6 = 0, arg7 = 0, arg8 = 0;
  88. nb_args = mips_syscall_args[syscall_num];
  89. sp_reg = env->active_tc.gpr[29];
  90. switch (nb_args) {
  91. /* these arguments are taken from the stack */
  92. case 8:
  93. if ((ret = get_user_ual(arg8, sp_reg + 28)) != 0) {
  94. goto done_syscall;
  95. }
  96. /* fall through */
  97. case 7:
  98. if ((ret = get_user_ual(arg7, sp_reg + 24)) != 0) {
  99. goto done_syscall;
  100. }
  101. /* fall through */
  102. case 6:
  103. if ((ret = get_user_ual(arg6, sp_reg + 20)) != 0) {
  104. goto done_syscall;
  105. }
  106. /* fall through */
  107. case 5:
  108. if ((ret = get_user_ual(arg5, sp_reg + 16)) != 0) {
  109. goto done_syscall;
  110. }
  111. /* fall through */
  112. default:
  113. break;
  114. }
  115. ret = do_syscall(env, env->active_tc.gpr[2],
  116. env->active_tc.gpr[4],
  117. env->active_tc.gpr[5],
  118. env->active_tc.gpr[6],
  119. env->active_tc.gpr[7],
  120. arg5, arg6, arg7, arg8);
  121. }
  122. done_syscall:
  123. # else
  124. ret = do_syscall(env, env->active_tc.gpr[2],
  125. env->active_tc.gpr[4], env->active_tc.gpr[5],
  126. env->active_tc.gpr[6], env->active_tc.gpr[7],
  127. env->active_tc.gpr[8], env->active_tc.gpr[9],
  128. env->active_tc.gpr[10], env->active_tc.gpr[11]);
  129. # endif /* O32 */
  130. if (ret == -QEMU_ERESTARTSYS) {
  131. env->active_tc.PC -= 4;
  132. break;
  133. }
  134. if (ret == -QEMU_ESIGRETURN) {
  135. /* Returning from a successful sigreturn syscall.
  136. Avoid clobbering register state. */
  137. break;
  138. }
  139. if ((abi_ulong)ret >= (abi_ulong)-1133) {
  140. env->active_tc.gpr[7] = 1; /* error flag */
  141. ret = -ret;
  142. } else {
  143. env->active_tc.gpr[7] = 0; /* error flag */
  144. }
  145. env->active_tc.gpr[2] = ret;
  146. break;
  147. case EXCP_CpU:
  148. case EXCP_RI:
  149. case EXCP_DSPDIS:
  150. force_sig(TARGET_SIGILL);
  151. break;
  152. case EXCP_INTERRUPT:
  153. /* just indicate that signals should be handled asap */
  154. break;
  155. case EXCP_DEBUG:
  156. force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT,
  157. env->active_tc.PC);
  158. break;
  159. case EXCP_FPE:
  160. si_code = TARGET_FPE_FLTUNK;
  161. if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INVALID) {
  162. si_code = TARGET_FPE_FLTINV;
  163. } else if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_DIV0) {
  164. si_code = TARGET_FPE_FLTDIV;
  165. } else if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_OVERFLOW) {
  166. si_code = TARGET_FPE_FLTOVF;
  167. } else if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_UNDERFLOW) {
  168. si_code = TARGET_FPE_FLTUND;
  169. } else if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INEXACT) {
  170. si_code = TARGET_FPE_FLTRES;
  171. }
  172. force_sig_fault(TARGET_SIGFPE, si_code, env->active_tc.PC);
  173. break;
  174. case EXCP_OVERFLOW:
  175. force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTOVF, env->active_tc.PC);
  176. break;
  177. /* The code below was inspired by the MIPS Linux kernel trap
  178. * handling code in arch/mips/kernel/traps.c.
  179. */
  180. case EXCP_BREAK:
  181. /*
  182. * As described in the original Linux kernel code, the below
  183. * checks on 'code' are to work around an old assembly bug.
  184. */
  185. code = env->error_code;
  186. if (code >= (1 << 10)) {
  187. code >>= 10;
  188. }
  189. do_tr_or_bp(env, code, false);
  190. break;
  191. case EXCP_TRAP:
  192. do_tr_or_bp(env, env->error_code, true);
  193. break;
  194. case EXCP_ATOMIC:
  195. cpu_exec_step_atomic(cs);
  196. break;
  197. default:
  198. EXCP_DUMP(env, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr);
  199. abort();
  200. }
  201. process_pending_signals(env);
  202. }
  203. }
  204. void target_cpu_copy_regs(CPUArchState *env, target_pt_regs *regs)
  205. {
  206. CPUState *cpu = env_cpu(env);
  207. TaskState *ts = get_task_state(cpu);
  208. struct image_info *info = ts->info;
  209. int i;
  210. struct mode_req {
  211. bool single;
  212. bool soft;
  213. bool fr1;
  214. bool frdefault;
  215. bool fre;
  216. };
  217. static const struct mode_req fpu_reqs[] = {
  218. [MIPS_ABI_FP_ANY] = { true, true, true, true, true },
  219. [MIPS_ABI_FP_DOUBLE] = { false, false, false, true, true },
  220. [MIPS_ABI_FP_SINGLE] = { true, false, false, false, false },
  221. [MIPS_ABI_FP_SOFT] = { false, true, false, false, false },
  222. [MIPS_ABI_FP_OLD_64] = { false, false, false, false, false },
  223. [MIPS_ABI_FP_XX] = { false, false, true, true, true },
  224. [MIPS_ABI_FP_64] = { false, false, true, false, false },
  225. [MIPS_ABI_FP_64A] = { false, false, true, false, true }
  226. };
  227. /*
  228. * Mode requirements when .MIPS.abiflags is not present in the ELF.
  229. * Not present means that everything is acceptable except FR1.
  230. */
  231. static struct mode_req none_req = { true, true, false, true, true };
  232. struct mode_req prog_req;
  233. struct mode_req interp_req;
  234. for(i = 0; i < 32; i++) {
  235. env->active_tc.gpr[i] = regs->regs[i];
  236. }
  237. env->active_tc.PC = regs->cp0_epc & ~(target_ulong)1;
  238. if (regs->cp0_epc & 1) {
  239. env->hflags |= MIPS_HFLAG_M16;
  240. }
  241. #ifdef TARGET_ABI_MIPSO32
  242. # define MAX_FP_ABI MIPS_ABI_FP_64A
  243. #else
  244. # define MAX_FP_ABI MIPS_ABI_FP_SOFT
  245. #endif
  246. if ((info->fp_abi > MAX_FP_ABI && info->fp_abi != MIPS_ABI_FP_UNKNOWN)
  247. || (info->interp_fp_abi > MAX_FP_ABI &&
  248. info->interp_fp_abi != MIPS_ABI_FP_UNKNOWN)) {
  249. fprintf(stderr, "qemu: Unexpected FPU mode\n");
  250. exit(1);
  251. }
  252. prog_req = (info->fp_abi == MIPS_ABI_FP_UNKNOWN) ? none_req
  253. : fpu_reqs[info->fp_abi];
  254. interp_req = (info->interp_fp_abi == MIPS_ABI_FP_UNKNOWN) ? none_req
  255. : fpu_reqs[info->interp_fp_abi];
  256. prog_req.single &= interp_req.single;
  257. prog_req.soft &= interp_req.soft;
  258. prog_req.fr1 &= interp_req.fr1;
  259. prog_req.frdefault &= interp_req.frdefault;
  260. prog_req.fre &= interp_req.fre;
  261. bool cpu_has_mips_r2_r6 = env->insn_flags & ISA_MIPS_R2 ||
  262. env->insn_flags & ISA_MIPS_R6;
  263. if (prog_req.fre && !prog_req.frdefault && !prog_req.fr1) {
  264. env->CP0_Config5 |= (1 << CP0C5_FRE);
  265. if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) {
  266. env->hflags |= MIPS_HFLAG_FRE;
  267. }
  268. } else if ((prog_req.fr1 && prog_req.frdefault) ||
  269. (prog_req.single && !prog_req.frdefault)) {
  270. if ((env->active_fpu.fcr0 & (1 << FCR0_F64)
  271. && cpu_has_mips_r2_r6) || prog_req.fr1) {
  272. env->CP0_Status |= (1 << CP0St_FR);
  273. env->hflags |= MIPS_HFLAG_F64;
  274. }
  275. } else if (prog_req.fr1) {
  276. env->CP0_Status |= (1 << CP0St_FR);
  277. env->hflags |= MIPS_HFLAG_F64;
  278. } else if (!prog_req.fre && !prog_req.frdefault &&
  279. !prog_req.fr1 && !prog_req.single && !prog_req.soft) {
  280. fprintf(stderr, "qemu: Can't find a matching FPU mode\n");
  281. exit(1);
  282. }
  283. if (env->insn_flags & ISA_NANOMIPS32) {
  284. return;
  285. }
  286. if (((info->elf_flags & EF_MIPS_NAN2008) != 0) !=
  287. ((env->active_fpu.fcr31 & (1 << FCR31_NAN2008)) != 0)) {
  288. if ((env->active_fpu.fcr31_rw_bitmask &
  289. (1 << FCR31_NAN2008)) == 0) {
  290. fprintf(stderr, "ELF binary's NaN mode not supported by CPU\n");
  291. exit(1);
  292. }
  293. if ((info->elf_flags & EF_MIPS_NAN2008) != 0) {
  294. env->active_fpu.fcr31 |= (1 << FCR31_NAN2008);
  295. } else {
  296. env->active_fpu.fcr31 &= ~(1 << FCR31_NAN2008);
  297. }
  298. restore_snan_bit_mode(env);
  299. }
  300. }