2
0

cpu_loop.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #define SPARC64_STACK_BIAS 2047
  24. //#define DEBUG_WIN
  25. /* WARNING: dealing with register windows _is_ complicated. More info
  26. can be found at http://www.sics.se/~psm/sparcstack.html */
  27. static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
  28. {
  29. index = (index + cwp * 16) % (16 * env->nwindows);
  30. /* wrap handling : if cwp is on the last window, then we use the
  31. registers 'after' the end */
  32. if (index < 8 && env->cwp == env->nwindows - 1)
  33. index += 16 * env->nwindows;
  34. return index;
  35. }
  36. /* save the register window 'cwp1' */
  37. static inline void save_window_offset(CPUSPARCState *env, int cwp1)
  38. {
  39. unsigned int i;
  40. abi_ulong sp_ptr;
  41. sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
  42. #ifdef TARGET_SPARC64
  43. if (sp_ptr & 3)
  44. sp_ptr += SPARC64_STACK_BIAS;
  45. #endif
  46. #if defined(DEBUG_WIN)
  47. printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
  48. sp_ptr, cwp1);
  49. #endif
  50. for(i = 0; i < 16; i++) {
  51. /* FIXME - what to do if put_user() fails? */
  52. put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
  53. sp_ptr += sizeof(abi_ulong);
  54. }
  55. }
  56. static void save_window(CPUSPARCState *env)
  57. {
  58. #ifndef TARGET_SPARC64
  59. unsigned int new_wim;
  60. new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
  61. ((1LL << env->nwindows) - 1);
  62. save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
  63. env->wim = new_wim;
  64. #else
  65. /*
  66. * cansave is zero if the spill trap handler is triggered by `save` and
  67. * nonzero if triggered by a `flushw`
  68. */
  69. save_window_offset(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2));
  70. env->cansave++;
  71. env->canrestore--;
  72. #endif
  73. }
  74. static void restore_window(CPUSPARCState *env)
  75. {
  76. #ifndef TARGET_SPARC64
  77. unsigned int new_wim;
  78. #endif
  79. unsigned int i, cwp1;
  80. abi_ulong sp_ptr;
  81. #ifndef TARGET_SPARC64
  82. new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
  83. ((1LL << env->nwindows) - 1);
  84. #endif
  85. /* restore the invalid window */
  86. cwp1 = cpu_cwp_inc(env, env->cwp + 1);
  87. sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
  88. #ifdef TARGET_SPARC64
  89. if (sp_ptr & 3)
  90. sp_ptr += SPARC64_STACK_BIAS;
  91. #endif
  92. #if defined(DEBUG_WIN)
  93. printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
  94. sp_ptr, cwp1);
  95. #endif
  96. for(i = 0; i < 16; i++) {
  97. /* FIXME - what to do if get_user() fails? */
  98. get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
  99. sp_ptr += sizeof(abi_ulong);
  100. }
  101. #ifdef TARGET_SPARC64
  102. env->canrestore++;
  103. if (env->cleanwin < env->nwindows - 1)
  104. env->cleanwin++;
  105. env->cansave--;
  106. #else
  107. env->wim = new_wim;
  108. #endif
  109. }
  110. static void flush_windows(CPUSPARCState *env)
  111. {
  112. int offset, cwp1;
  113. offset = 1;
  114. for(;;) {
  115. /* if restore would invoke restore_window(), then we can stop */
  116. cwp1 = cpu_cwp_inc(env, env->cwp + offset);
  117. #ifndef TARGET_SPARC64
  118. if (env->wim & (1 << cwp1))
  119. break;
  120. #else
  121. if (env->canrestore == 0)
  122. break;
  123. env->cansave++;
  124. env->canrestore--;
  125. #endif
  126. save_window_offset(env, cwp1);
  127. offset++;
  128. }
  129. cwp1 = cpu_cwp_inc(env, env->cwp + 1);
  130. #ifndef TARGET_SPARC64
  131. /* set wim so that restore will reload the registers */
  132. env->wim = 1 << cwp1;
  133. #endif
  134. #if defined(DEBUG_WIN)
  135. printf("flush_windows: nb=%d\n", offset - 1);
  136. #endif
  137. }
  138. void cpu_loop (CPUSPARCState *env)
  139. {
  140. CPUState *cs = env_cpu(env);
  141. int trapnr;
  142. abi_long ret;
  143. target_siginfo_t info;
  144. while (1) {
  145. cpu_exec_start(cs);
  146. trapnr = cpu_exec(cs);
  147. cpu_exec_end(cs);
  148. process_queued_cpu_work(cs);
  149. /* Compute PSR before exposing state. */
  150. if (env->cc_op != CC_OP_FLAGS) {
  151. cpu_get_psr(env);
  152. }
  153. switch (trapnr) {
  154. #ifndef TARGET_SPARC64
  155. case 0x88:
  156. case 0x90:
  157. #else
  158. case 0x110:
  159. case 0x16d:
  160. #endif
  161. ret = do_syscall (env, env->gregs[1],
  162. env->regwptr[0], env->regwptr[1],
  163. env->regwptr[2], env->regwptr[3],
  164. env->regwptr[4], env->regwptr[5],
  165. 0, 0);
  166. if (ret == -TARGET_ERESTARTSYS || ret == -TARGET_QEMU_ESIGRETURN) {
  167. break;
  168. }
  169. if ((abi_ulong)ret >= (abi_ulong)(-515)) {
  170. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  171. env->xcc |= PSR_CARRY;
  172. #else
  173. env->psr |= PSR_CARRY;
  174. #endif
  175. ret = -ret;
  176. } else {
  177. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  178. env->xcc &= ~PSR_CARRY;
  179. #else
  180. env->psr &= ~PSR_CARRY;
  181. #endif
  182. }
  183. env->regwptr[0] = ret;
  184. /* next instruction */
  185. env->pc = env->npc;
  186. env->npc = env->npc + 4;
  187. break;
  188. case 0x83: /* flush windows */
  189. #ifdef TARGET_ABI32
  190. case 0x103:
  191. #endif
  192. flush_windows(env);
  193. /* next instruction */
  194. env->pc = env->npc;
  195. env->npc = env->npc + 4;
  196. break;
  197. #ifndef TARGET_SPARC64
  198. case TT_WIN_OVF: /* window overflow */
  199. save_window(env);
  200. break;
  201. case TT_WIN_UNF: /* window underflow */
  202. restore_window(env);
  203. break;
  204. case TT_TFAULT:
  205. case TT_DFAULT:
  206. {
  207. info.si_signo = TARGET_SIGSEGV;
  208. info.si_errno = 0;
  209. /* XXX: check env->error_code */
  210. info.si_code = TARGET_SEGV_MAPERR;
  211. info._sifields._sigfault._addr = env->mmuregs[4];
  212. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  213. }
  214. break;
  215. #else
  216. case TT_SPILL: /* window overflow */
  217. save_window(env);
  218. break;
  219. case TT_FILL: /* window underflow */
  220. restore_window(env);
  221. break;
  222. case TT_TFAULT:
  223. case TT_DFAULT:
  224. {
  225. info.si_signo = TARGET_SIGSEGV;
  226. info.si_errno = 0;
  227. /* XXX: check env->error_code */
  228. info.si_code = TARGET_SEGV_MAPERR;
  229. if (trapnr == TT_DFAULT)
  230. info._sifields._sigfault._addr = env->dmmu.mmuregs[4];
  231. else
  232. info._sifields._sigfault._addr = cpu_tsptr(env)->tpc;
  233. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  234. }
  235. break;
  236. #ifndef TARGET_ABI32
  237. case 0x16e:
  238. flush_windows(env);
  239. sparc64_get_context(env);
  240. break;
  241. case 0x16f:
  242. flush_windows(env);
  243. sparc64_set_context(env);
  244. break;
  245. #endif
  246. #endif
  247. case EXCP_INTERRUPT:
  248. /* just indicate that signals should be handled asap */
  249. break;
  250. case TT_ILL_INSN:
  251. {
  252. info.si_signo = TARGET_SIGILL;
  253. info.si_errno = 0;
  254. info.si_code = TARGET_ILL_ILLOPC;
  255. info._sifields._sigfault._addr = env->pc;
  256. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  257. }
  258. break;
  259. case EXCP_DEBUG:
  260. info.si_signo = TARGET_SIGTRAP;
  261. info.si_errno = 0;
  262. info.si_code = TARGET_TRAP_BRKPT;
  263. queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
  264. break;
  265. case EXCP_ATOMIC:
  266. cpu_exec_step_atomic(cs);
  267. break;
  268. default:
  269. fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr);
  270. cpu_dump_state(cs, stderr, 0);
  271. exit(EXIT_FAILURE);
  272. }
  273. process_pending_signals (env);
  274. }
  275. }
  276. void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
  277. {
  278. int i;
  279. env->pc = regs->pc;
  280. env->npc = regs->npc;
  281. env->y = regs->y;
  282. for(i = 0; i < 8; i++)
  283. env->gregs[i] = regs->u_regs[i];
  284. for(i = 0; i < 8; i++)
  285. env->regwptr[i] = regs->u_regs[i + 8];
  286. }