2
0

cpu_loop.c 8.6 KB

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