cpu_loop.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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-common.h"
  21. #include "qemu.h"
  22. #include "cpu_loop-common.h"
  23. void cpu_loop(CPUAlphaState *env)
  24. {
  25. CPUState *cs = env_cpu(env);
  26. int trapnr;
  27. target_siginfo_t info;
  28. abi_long sysret;
  29. while (1) {
  30. bool arch_interrupt = true;
  31. cpu_exec_start(cs);
  32. trapnr = cpu_exec(cs);
  33. cpu_exec_end(cs);
  34. process_queued_cpu_work(cs);
  35. switch (trapnr) {
  36. case EXCP_RESET:
  37. fprintf(stderr, "Reset requested. Exit\n");
  38. exit(EXIT_FAILURE);
  39. break;
  40. case EXCP_MCHK:
  41. fprintf(stderr, "Machine check exception. Exit\n");
  42. exit(EXIT_FAILURE);
  43. break;
  44. case EXCP_SMP_INTERRUPT:
  45. case EXCP_CLK_INTERRUPT:
  46. case EXCP_DEV_INTERRUPT:
  47. fprintf(stderr, "External interrupt. Exit\n");
  48. exit(EXIT_FAILURE);
  49. break;
  50. case EXCP_MMFAULT:
  51. info.si_signo = TARGET_SIGSEGV;
  52. info.si_errno = 0;
  53. info.si_code = (page_get_flags(env->trap_arg0) & PAGE_VALID
  54. ? TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR);
  55. info._sifields._sigfault._addr = env->trap_arg0;
  56. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  57. break;
  58. case EXCP_UNALIGN:
  59. info.si_signo = TARGET_SIGBUS;
  60. info.si_errno = 0;
  61. info.si_code = TARGET_BUS_ADRALN;
  62. info._sifields._sigfault._addr = env->trap_arg0;
  63. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  64. break;
  65. case EXCP_OPCDEC:
  66. do_sigill:
  67. info.si_signo = TARGET_SIGILL;
  68. info.si_errno = 0;
  69. info.si_code = TARGET_ILL_ILLOPC;
  70. info._sifields._sigfault._addr = env->pc;
  71. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  72. break;
  73. case EXCP_ARITH:
  74. info.si_signo = TARGET_SIGFPE;
  75. info.si_errno = 0;
  76. info.si_code = TARGET_FPE_FLTINV;
  77. info._sifields._sigfault._addr = env->pc;
  78. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  79. break;
  80. case EXCP_FEN:
  81. /* No-op. Linux simply re-enables the FPU. */
  82. break;
  83. case EXCP_CALL_PAL:
  84. switch (env->error_code) {
  85. case 0x80:
  86. /* BPT */
  87. info.si_signo = TARGET_SIGTRAP;
  88. info.si_errno = 0;
  89. info.si_code = TARGET_TRAP_BRKPT;
  90. info._sifields._sigfault._addr = env->pc;
  91. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  92. break;
  93. case 0x81:
  94. /* BUGCHK */
  95. info.si_signo = TARGET_SIGTRAP;
  96. info.si_errno = 0;
  97. info.si_code = 0;
  98. info._sifields._sigfault._addr = env->pc;
  99. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  100. break;
  101. case 0x83:
  102. /* CALLSYS */
  103. trapnr = env->ir[IR_V0];
  104. sysret = do_syscall(env, trapnr,
  105. env->ir[IR_A0], env->ir[IR_A1],
  106. env->ir[IR_A2], env->ir[IR_A3],
  107. env->ir[IR_A4], env->ir[IR_A5],
  108. 0, 0);
  109. if (sysret == -TARGET_ERESTARTSYS) {
  110. env->pc -= 4;
  111. break;
  112. }
  113. if (sysret == -TARGET_QEMU_ESIGRETURN) {
  114. break;
  115. }
  116. /* Syscall writes 0 to V0 to bypass error check, similar
  117. to how this is handled internal to Linux kernel.
  118. (Ab)use trapnr temporarily as boolean indicating error. */
  119. trapnr = (env->ir[IR_V0] != 0 && sysret < 0);
  120. env->ir[IR_V0] = (trapnr ? -sysret : sysret);
  121. env->ir[IR_A3] = trapnr;
  122. break;
  123. case 0x86:
  124. /* IMB */
  125. /* ??? We can probably elide the code using page_unprotect
  126. that is checking for self-modifying code. Instead we
  127. could simply call tb_flush here. Until we work out the
  128. changes required to turn off the extra write protection,
  129. this can be a no-op. */
  130. break;
  131. case 0x9E:
  132. /* RDUNIQUE */
  133. /* Handled in the translator for usermode. */
  134. abort();
  135. case 0x9F:
  136. /* WRUNIQUE */
  137. /* Handled in the translator for usermode. */
  138. abort();
  139. case 0xAA:
  140. /* GENTRAP */
  141. info.si_signo = TARGET_SIGFPE;
  142. switch (env->ir[IR_A0]) {
  143. case TARGET_GEN_INTOVF:
  144. info.si_code = TARGET_FPE_INTOVF;
  145. break;
  146. case TARGET_GEN_INTDIV:
  147. info.si_code = TARGET_FPE_INTDIV;
  148. break;
  149. case TARGET_GEN_FLTOVF:
  150. info.si_code = TARGET_FPE_FLTOVF;
  151. break;
  152. case TARGET_GEN_FLTUND:
  153. info.si_code = TARGET_FPE_FLTUND;
  154. break;
  155. case TARGET_GEN_FLTINV:
  156. info.si_code = TARGET_FPE_FLTINV;
  157. break;
  158. case TARGET_GEN_FLTINE:
  159. info.si_code = TARGET_FPE_FLTRES;
  160. break;
  161. case TARGET_GEN_ROPRAND:
  162. info.si_code = 0;
  163. break;
  164. default:
  165. info.si_signo = TARGET_SIGTRAP;
  166. info.si_code = 0;
  167. break;
  168. }
  169. info.si_errno = 0;
  170. info._sifields._sigfault._addr = env->pc;
  171. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  172. break;
  173. default:
  174. goto do_sigill;
  175. }
  176. break;
  177. case EXCP_DEBUG:
  178. info.si_signo = TARGET_SIGTRAP;
  179. info.si_errno = 0;
  180. info.si_code = TARGET_TRAP_BRKPT;
  181. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  182. break;
  183. case EXCP_INTERRUPT:
  184. /* Just indicate that signals should be handled asap. */
  185. break;
  186. case EXCP_ATOMIC:
  187. cpu_exec_step_atomic(cs);
  188. arch_interrupt = false;
  189. break;
  190. default:
  191. fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr);
  192. cpu_dump_state(cs, stderr, 0);
  193. exit(EXIT_FAILURE);
  194. }
  195. process_pending_signals (env);
  196. /* Most of the traps imply a transition through PALcode, which
  197. implies an REI instruction has been executed. Which means
  198. that RX and LOCK_ADDR should be cleared. But there are a
  199. few exceptions for traps internal to QEMU. */
  200. if (arch_interrupt) {
  201. env->flags &= ~ENV_FLAG_RX_FLAG;
  202. env->lock_addr = -1;
  203. }
  204. }
  205. }
  206. void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
  207. {
  208. int i;
  209. for(i = 0; i < 28; i++) {
  210. env->ir[i] = ((abi_ulong *)regs)[i];
  211. }
  212. env->ir[IR_SP] = regs->usp;
  213. env->pc = regs->pc;
  214. }