2
0

cpu-exec.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * i386 emulator main execution loop
  3. *
  4. * Copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include "cpu.h"
  21. #include "disas.h"
  22. #include "tcg.h"
  23. #include "qemu-barrier.h"
  24. int tb_invalidated_flag;
  25. //#define CONFIG_DEBUG_EXEC
  26. bool qemu_cpu_has_work(CPUState *env)
  27. {
  28. return cpu_has_work(env);
  29. }
  30. void cpu_loop_exit(CPUState *env)
  31. {
  32. env->current_tb = NULL;
  33. longjmp(env->jmp_env, 1);
  34. }
  35. /* exit the current TB from a signal handler. The host registers are
  36. restored in a state compatible with the CPU emulator
  37. */
  38. #if defined(CONFIG_SOFTMMU)
  39. void cpu_resume_from_signal(CPUState *env, void *puc)
  40. {
  41. /* XXX: restore cpu registers saved in host registers */
  42. env->exception_index = -1;
  43. longjmp(env->jmp_env, 1);
  44. }
  45. #endif
  46. /* Execute the code without caching the generated code. An interpreter
  47. could be used if available. */
  48. static void cpu_exec_nocache(CPUState *env, int max_cycles,
  49. TranslationBlock *orig_tb)
  50. {
  51. unsigned long next_tb;
  52. TranslationBlock *tb;
  53. /* Should never happen.
  54. We only end up here when an existing TB is too long. */
  55. if (max_cycles > CF_COUNT_MASK)
  56. max_cycles = CF_COUNT_MASK;
  57. tb = tb_gen_code(env, orig_tb->pc, orig_tb->cs_base, orig_tb->flags,
  58. max_cycles);
  59. env->current_tb = tb;
  60. /* execute the generated code */
  61. next_tb = tcg_qemu_tb_exec(env, tb->tc_ptr);
  62. env->current_tb = NULL;
  63. if ((next_tb & 3) == 2) {
  64. /* Restore PC. This may happen if async event occurs before
  65. the TB starts executing. */
  66. cpu_pc_from_tb(env, tb);
  67. }
  68. tb_phys_invalidate(tb, -1);
  69. tb_free(tb);
  70. }
  71. static TranslationBlock *tb_find_slow(CPUState *env,
  72. target_ulong pc,
  73. target_ulong cs_base,
  74. uint64_t flags)
  75. {
  76. TranslationBlock *tb, **ptb1;
  77. unsigned int h;
  78. tb_page_addr_t phys_pc, phys_page1;
  79. target_ulong virt_page2;
  80. tb_invalidated_flag = 0;
  81. /* find translated block using physical mappings */
  82. phys_pc = get_page_addr_code(env, pc);
  83. phys_page1 = phys_pc & TARGET_PAGE_MASK;
  84. h = tb_phys_hash_func(phys_pc);
  85. ptb1 = &tb_phys_hash[h];
  86. for(;;) {
  87. tb = *ptb1;
  88. if (!tb)
  89. goto not_found;
  90. if (tb->pc == pc &&
  91. tb->page_addr[0] == phys_page1 &&
  92. tb->cs_base == cs_base &&
  93. tb->flags == flags) {
  94. /* check next page if needed */
  95. if (tb->page_addr[1] != -1) {
  96. tb_page_addr_t phys_page2;
  97. virt_page2 = (pc & TARGET_PAGE_MASK) +
  98. TARGET_PAGE_SIZE;
  99. phys_page2 = get_page_addr_code(env, virt_page2);
  100. if (tb->page_addr[1] == phys_page2)
  101. goto found;
  102. } else {
  103. goto found;
  104. }
  105. }
  106. ptb1 = &tb->phys_hash_next;
  107. }
  108. not_found:
  109. /* if no translated code available, then translate it now */
  110. tb = tb_gen_code(env, pc, cs_base, flags, 0);
  111. found:
  112. /* Move the last found TB to the head of the list */
  113. if (likely(*ptb1)) {
  114. *ptb1 = tb->phys_hash_next;
  115. tb->phys_hash_next = tb_phys_hash[h];
  116. tb_phys_hash[h] = tb;
  117. }
  118. /* we add the TB in the virtual pc hash table */
  119. env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)] = tb;
  120. return tb;
  121. }
  122. static inline TranslationBlock *tb_find_fast(CPUState *env)
  123. {
  124. TranslationBlock *tb;
  125. target_ulong cs_base, pc;
  126. int flags;
  127. /* we record a subset of the CPU state. It will
  128. always be the same before a given translated block
  129. is executed. */
  130. cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
  131. tb = env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)];
  132. if (unlikely(!tb || tb->pc != pc || tb->cs_base != cs_base ||
  133. tb->flags != flags)) {
  134. tb = tb_find_slow(env, pc, cs_base, flags);
  135. }
  136. return tb;
  137. }
  138. static CPUDebugExcpHandler *debug_excp_handler;
  139. CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler)
  140. {
  141. CPUDebugExcpHandler *old_handler = debug_excp_handler;
  142. debug_excp_handler = handler;
  143. return old_handler;
  144. }
  145. static void cpu_handle_debug_exception(CPUState *env)
  146. {
  147. CPUWatchpoint *wp;
  148. if (!env->watchpoint_hit) {
  149. QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
  150. wp->flags &= ~BP_WATCHPOINT_HIT;
  151. }
  152. }
  153. if (debug_excp_handler) {
  154. debug_excp_handler(env);
  155. }
  156. }
  157. /* main execution loop */
  158. volatile sig_atomic_t exit_request;
  159. int cpu_exec(CPUState *env)
  160. {
  161. int ret, interrupt_request;
  162. TranslationBlock *tb;
  163. uint8_t *tc_ptr;
  164. unsigned long next_tb;
  165. if (env->halted) {
  166. if (!cpu_has_work(env)) {
  167. return EXCP_HALTED;
  168. }
  169. env->halted = 0;
  170. }
  171. cpu_single_env = env;
  172. if (unlikely(exit_request)) {
  173. env->exit_request = 1;
  174. }
  175. #if defined(TARGET_I386)
  176. /* put eflags in CPU temporary format */
  177. CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
  178. DF = 1 - (2 * ((env->eflags >> 10) & 1));
  179. CC_OP = CC_OP_EFLAGS;
  180. env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
  181. #elif defined(TARGET_SPARC)
  182. #elif defined(TARGET_M68K)
  183. env->cc_op = CC_OP_FLAGS;
  184. env->cc_dest = env->sr & 0xf;
  185. env->cc_x = (env->sr >> 4) & 1;
  186. #elif defined(TARGET_ALPHA)
  187. #elif defined(TARGET_ARM)
  188. #elif defined(TARGET_UNICORE32)
  189. #elif defined(TARGET_PPC)
  190. env->reserve_addr = -1;
  191. #elif defined(TARGET_LM32)
  192. #elif defined(TARGET_MICROBLAZE)
  193. #elif defined(TARGET_MIPS)
  194. #elif defined(TARGET_SH4)
  195. #elif defined(TARGET_CRIS)
  196. #elif defined(TARGET_S390X)
  197. #elif defined(TARGET_XTENSA)
  198. /* XXXXX */
  199. #else
  200. #error unsupported target CPU
  201. #endif
  202. env->exception_index = -1;
  203. /* prepare setjmp context for exception handling */
  204. for(;;) {
  205. if (setjmp(env->jmp_env) == 0) {
  206. /* if an exception is pending, we execute it here */
  207. if (env->exception_index >= 0) {
  208. if (env->exception_index >= EXCP_INTERRUPT) {
  209. /* exit request from the cpu execution loop */
  210. ret = env->exception_index;
  211. if (ret == EXCP_DEBUG) {
  212. cpu_handle_debug_exception(env);
  213. }
  214. break;
  215. } else {
  216. #if defined(CONFIG_USER_ONLY)
  217. /* if user mode only, we simulate a fake exception
  218. which will be handled outside the cpu execution
  219. loop */
  220. #if defined(TARGET_I386)
  221. do_interrupt(env);
  222. #endif
  223. ret = env->exception_index;
  224. break;
  225. #else
  226. do_interrupt(env);
  227. env->exception_index = -1;
  228. #endif
  229. }
  230. }
  231. next_tb = 0; /* force lookup of first TB */
  232. for(;;) {
  233. interrupt_request = env->interrupt_request;
  234. if (unlikely(interrupt_request)) {
  235. if (unlikely(env->singlestep_enabled & SSTEP_NOIRQ)) {
  236. /* Mask out external interrupts for this step. */
  237. interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK;
  238. }
  239. if (interrupt_request & CPU_INTERRUPT_DEBUG) {
  240. env->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
  241. env->exception_index = EXCP_DEBUG;
  242. cpu_loop_exit(env);
  243. }
  244. #if defined(TARGET_ARM) || defined(TARGET_SPARC) || defined(TARGET_MIPS) || \
  245. defined(TARGET_PPC) || defined(TARGET_ALPHA) || defined(TARGET_CRIS) || \
  246. defined(TARGET_MICROBLAZE) || defined(TARGET_LM32) || defined(TARGET_UNICORE32)
  247. if (interrupt_request & CPU_INTERRUPT_HALT) {
  248. env->interrupt_request &= ~CPU_INTERRUPT_HALT;
  249. env->halted = 1;
  250. env->exception_index = EXCP_HLT;
  251. cpu_loop_exit(env);
  252. }
  253. #endif
  254. #if defined(TARGET_I386)
  255. if (interrupt_request & CPU_INTERRUPT_INIT) {
  256. svm_check_intercept(env, SVM_EXIT_INIT);
  257. do_cpu_init(env);
  258. env->exception_index = EXCP_HALTED;
  259. cpu_loop_exit(env);
  260. } else if (interrupt_request & CPU_INTERRUPT_SIPI) {
  261. do_cpu_sipi(env);
  262. } else if (env->hflags2 & HF2_GIF_MASK) {
  263. if ((interrupt_request & CPU_INTERRUPT_SMI) &&
  264. !(env->hflags & HF_SMM_MASK)) {
  265. svm_check_intercept(env, SVM_EXIT_SMI);
  266. env->interrupt_request &= ~CPU_INTERRUPT_SMI;
  267. do_smm_enter(env);
  268. next_tb = 0;
  269. } else if ((interrupt_request & CPU_INTERRUPT_NMI) &&
  270. !(env->hflags2 & HF2_NMI_MASK)) {
  271. env->interrupt_request &= ~CPU_INTERRUPT_NMI;
  272. env->hflags2 |= HF2_NMI_MASK;
  273. do_interrupt_x86_hardirq(env, EXCP02_NMI, 1);
  274. next_tb = 0;
  275. } else if (interrupt_request & CPU_INTERRUPT_MCE) {
  276. env->interrupt_request &= ~CPU_INTERRUPT_MCE;
  277. do_interrupt_x86_hardirq(env, EXCP12_MCHK, 0);
  278. next_tb = 0;
  279. } else if ((interrupt_request & CPU_INTERRUPT_HARD) &&
  280. (((env->hflags2 & HF2_VINTR_MASK) &&
  281. (env->hflags2 & HF2_HIF_MASK)) ||
  282. (!(env->hflags2 & HF2_VINTR_MASK) &&
  283. (env->eflags & IF_MASK &&
  284. !(env->hflags & HF_INHIBIT_IRQ_MASK))))) {
  285. int intno;
  286. svm_check_intercept(env, SVM_EXIT_INTR);
  287. env->interrupt_request &= ~(CPU_INTERRUPT_HARD | CPU_INTERRUPT_VIRQ);
  288. intno = cpu_get_pic_interrupt(env);
  289. qemu_log_mask(CPU_LOG_TB_IN_ASM, "Servicing hardware INT=0x%02x\n", intno);
  290. do_interrupt_x86_hardirq(env, intno, 1);
  291. /* ensure that no TB jump will be modified as
  292. the program flow was changed */
  293. next_tb = 0;
  294. #if !defined(CONFIG_USER_ONLY)
  295. } else if ((interrupt_request & CPU_INTERRUPT_VIRQ) &&
  296. (env->eflags & IF_MASK) &&
  297. !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
  298. int intno;
  299. /* FIXME: this should respect TPR */
  300. svm_check_intercept(env, SVM_EXIT_VINTR);
  301. intno = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_vector));
  302. qemu_log_mask(CPU_LOG_TB_IN_ASM, "Servicing virtual hardware INT=0x%02x\n", intno);
  303. do_interrupt_x86_hardirq(env, intno, 1);
  304. env->interrupt_request &= ~CPU_INTERRUPT_VIRQ;
  305. next_tb = 0;
  306. #endif
  307. }
  308. }
  309. #elif defined(TARGET_PPC)
  310. #if 0
  311. if ((interrupt_request & CPU_INTERRUPT_RESET)) {
  312. cpu_reset(env);
  313. }
  314. #endif
  315. if (interrupt_request & CPU_INTERRUPT_HARD) {
  316. ppc_hw_interrupt(env);
  317. if (env->pending_interrupts == 0)
  318. env->interrupt_request &= ~CPU_INTERRUPT_HARD;
  319. next_tb = 0;
  320. }
  321. #elif defined(TARGET_LM32)
  322. if ((interrupt_request & CPU_INTERRUPT_HARD)
  323. && (env->ie & IE_IE)) {
  324. env->exception_index = EXCP_IRQ;
  325. do_interrupt(env);
  326. next_tb = 0;
  327. }
  328. #elif defined(TARGET_MICROBLAZE)
  329. if ((interrupt_request & CPU_INTERRUPT_HARD)
  330. && (env->sregs[SR_MSR] & MSR_IE)
  331. && !(env->sregs[SR_MSR] & (MSR_EIP | MSR_BIP))
  332. && !(env->iflags & (D_FLAG | IMM_FLAG))) {
  333. env->exception_index = EXCP_IRQ;
  334. do_interrupt(env);
  335. next_tb = 0;
  336. }
  337. #elif defined(TARGET_MIPS)
  338. if ((interrupt_request & CPU_INTERRUPT_HARD) &&
  339. cpu_mips_hw_interrupts_pending(env)) {
  340. /* Raise it */
  341. env->exception_index = EXCP_EXT_INTERRUPT;
  342. env->error_code = 0;
  343. do_interrupt(env);
  344. next_tb = 0;
  345. }
  346. #elif defined(TARGET_SPARC)
  347. if (interrupt_request & CPU_INTERRUPT_HARD) {
  348. if (cpu_interrupts_enabled(env) &&
  349. env->interrupt_index > 0) {
  350. int pil = env->interrupt_index & 0xf;
  351. int type = env->interrupt_index & 0xf0;
  352. if (((type == TT_EXTINT) &&
  353. cpu_pil_allowed(env, pil)) ||
  354. type != TT_EXTINT) {
  355. env->exception_index = env->interrupt_index;
  356. do_interrupt(env);
  357. next_tb = 0;
  358. }
  359. }
  360. }
  361. #elif defined(TARGET_ARM)
  362. if (interrupt_request & CPU_INTERRUPT_FIQ
  363. && !(env->uncached_cpsr & CPSR_F)) {
  364. env->exception_index = EXCP_FIQ;
  365. do_interrupt(env);
  366. next_tb = 0;
  367. }
  368. /* ARMv7-M interrupt return works by loading a magic value
  369. into the PC. On real hardware the load causes the
  370. return to occur. The qemu implementation performs the
  371. jump normally, then does the exception return when the
  372. CPU tries to execute code at the magic address.
  373. This will cause the magic PC value to be pushed to
  374. the stack if an interrupt occurred at the wrong time.
  375. We avoid this by disabling interrupts when
  376. pc contains a magic address. */
  377. if (interrupt_request & CPU_INTERRUPT_HARD
  378. && ((IS_M(env) && env->regs[15] < 0xfffffff0)
  379. || !(env->uncached_cpsr & CPSR_I))) {
  380. env->exception_index = EXCP_IRQ;
  381. do_interrupt(env);
  382. next_tb = 0;
  383. }
  384. #elif defined(TARGET_UNICORE32)
  385. if (interrupt_request & CPU_INTERRUPT_HARD
  386. && !(env->uncached_asr & ASR_I)) {
  387. do_interrupt(env);
  388. next_tb = 0;
  389. }
  390. #elif defined(TARGET_SH4)
  391. if (interrupt_request & CPU_INTERRUPT_HARD) {
  392. do_interrupt(env);
  393. next_tb = 0;
  394. }
  395. #elif defined(TARGET_ALPHA)
  396. {
  397. int idx = -1;
  398. /* ??? This hard-codes the OSF/1 interrupt levels. */
  399. switch (env->pal_mode ? 7 : env->ps & PS_INT_MASK) {
  400. case 0 ... 3:
  401. if (interrupt_request & CPU_INTERRUPT_HARD) {
  402. idx = EXCP_DEV_INTERRUPT;
  403. }
  404. /* FALLTHRU */
  405. case 4:
  406. if (interrupt_request & CPU_INTERRUPT_TIMER) {
  407. idx = EXCP_CLK_INTERRUPT;
  408. }
  409. /* FALLTHRU */
  410. case 5:
  411. if (interrupt_request & CPU_INTERRUPT_SMP) {
  412. idx = EXCP_SMP_INTERRUPT;
  413. }
  414. /* FALLTHRU */
  415. case 6:
  416. if (interrupt_request & CPU_INTERRUPT_MCHK) {
  417. idx = EXCP_MCHK;
  418. }
  419. }
  420. if (idx >= 0) {
  421. env->exception_index = idx;
  422. env->error_code = 0;
  423. do_interrupt(env);
  424. next_tb = 0;
  425. }
  426. }
  427. #elif defined(TARGET_CRIS)
  428. if (interrupt_request & CPU_INTERRUPT_HARD
  429. && (env->pregs[PR_CCS] & I_FLAG)
  430. && !env->locked_irq) {
  431. env->exception_index = EXCP_IRQ;
  432. do_interrupt(env);
  433. next_tb = 0;
  434. }
  435. if (interrupt_request & CPU_INTERRUPT_NMI
  436. && (env->pregs[PR_CCS] & M_FLAG)) {
  437. env->exception_index = EXCP_NMI;
  438. do_interrupt(env);
  439. next_tb = 0;
  440. }
  441. #elif defined(TARGET_M68K)
  442. if (interrupt_request & CPU_INTERRUPT_HARD
  443. && ((env->sr & SR_I) >> SR_I_SHIFT)
  444. < env->pending_level) {
  445. /* Real hardware gets the interrupt vector via an
  446. IACK cycle at this point. Current emulated
  447. hardware doesn't rely on this, so we
  448. provide/save the vector when the interrupt is
  449. first signalled. */
  450. env->exception_index = env->pending_vector;
  451. do_interrupt_m68k_hardirq(env);
  452. next_tb = 0;
  453. }
  454. #elif defined(TARGET_S390X) && !defined(CONFIG_USER_ONLY)
  455. if ((interrupt_request & CPU_INTERRUPT_HARD) &&
  456. (env->psw.mask & PSW_MASK_EXT)) {
  457. do_interrupt(env);
  458. next_tb = 0;
  459. }
  460. #elif defined(TARGET_XTENSA)
  461. if (interrupt_request & CPU_INTERRUPT_HARD) {
  462. env->exception_index = EXC_IRQ;
  463. do_interrupt(env);
  464. next_tb = 0;
  465. }
  466. #endif
  467. /* Don't use the cached interrupt_request value,
  468. do_interrupt may have updated the EXITTB flag. */
  469. if (env->interrupt_request & CPU_INTERRUPT_EXITTB) {
  470. env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
  471. /* ensure that no TB jump will be modified as
  472. the program flow was changed */
  473. next_tb = 0;
  474. }
  475. }
  476. if (unlikely(env->exit_request)) {
  477. env->exit_request = 0;
  478. env->exception_index = EXCP_INTERRUPT;
  479. cpu_loop_exit(env);
  480. }
  481. #if defined(DEBUG_DISAS) || defined(CONFIG_DEBUG_EXEC)
  482. if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
  483. /* restore flags in standard format */
  484. #if defined(TARGET_I386)
  485. env->eflags = env->eflags | cpu_cc_compute_all(env, CC_OP)
  486. | (DF & DF_MASK);
  487. log_cpu_state(env, X86_DUMP_CCOP);
  488. env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
  489. #elif defined(TARGET_M68K)
  490. cpu_m68k_flush_flags(env, env->cc_op);
  491. env->cc_op = CC_OP_FLAGS;
  492. env->sr = (env->sr & 0xffe0)
  493. | env->cc_dest | (env->cc_x << 4);
  494. log_cpu_state(env, 0);
  495. #else
  496. log_cpu_state(env, 0);
  497. #endif
  498. }
  499. #endif /* DEBUG_DISAS || CONFIG_DEBUG_EXEC */
  500. spin_lock(&tb_lock);
  501. tb = tb_find_fast(env);
  502. /* Note: we do it here to avoid a gcc bug on Mac OS X when
  503. doing it in tb_find_slow */
  504. if (tb_invalidated_flag) {
  505. /* as some TB could have been invalidated because
  506. of memory exceptions while generating the code, we
  507. must recompute the hash index here */
  508. next_tb = 0;
  509. tb_invalidated_flag = 0;
  510. }
  511. #ifdef CONFIG_DEBUG_EXEC
  512. qemu_log_mask(CPU_LOG_EXEC, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
  513. (long)tb->tc_ptr, tb->pc,
  514. lookup_symbol(tb->pc));
  515. #endif
  516. /* see if we can patch the calling TB. When the TB
  517. spans two pages, we cannot safely do a direct
  518. jump. */
  519. if (next_tb != 0 && tb->page_addr[1] == -1) {
  520. tb_add_jump((TranslationBlock *)(next_tb & ~3), next_tb & 3, tb);
  521. }
  522. spin_unlock(&tb_lock);
  523. /* cpu_interrupt might be called while translating the
  524. TB, but before it is linked into a potentially
  525. infinite loop and becomes env->current_tb. Avoid
  526. starting execution if there is a pending interrupt. */
  527. env->current_tb = tb;
  528. barrier();
  529. if (likely(!env->exit_request)) {
  530. tc_ptr = tb->tc_ptr;
  531. /* execute the generated code */
  532. next_tb = tcg_qemu_tb_exec(env, tc_ptr);
  533. if ((next_tb & 3) == 2) {
  534. /* Instruction counter expired. */
  535. int insns_left;
  536. tb = (TranslationBlock *)(long)(next_tb & ~3);
  537. /* Restore PC. */
  538. cpu_pc_from_tb(env, tb);
  539. insns_left = env->icount_decr.u32;
  540. if (env->icount_extra && insns_left >= 0) {
  541. /* Refill decrementer and continue execution. */
  542. env->icount_extra += insns_left;
  543. if (env->icount_extra > 0xffff) {
  544. insns_left = 0xffff;
  545. } else {
  546. insns_left = env->icount_extra;
  547. }
  548. env->icount_extra -= insns_left;
  549. env->icount_decr.u16.low = insns_left;
  550. } else {
  551. if (insns_left > 0) {
  552. /* Execute remaining instructions. */
  553. cpu_exec_nocache(env, insns_left, tb);
  554. }
  555. env->exception_index = EXCP_INTERRUPT;
  556. next_tb = 0;
  557. cpu_loop_exit(env);
  558. }
  559. }
  560. }
  561. env->current_tb = NULL;
  562. /* reset soft MMU for next block (it can currently
  563. only be set by a memory fault) */
  564. } /* for(;;) */
  565. } else {
  566. /* Reload env after longjmp - the compiler may have smashed all
  567. * local variables as longjmp is marked 'noreturn'. */
  568. env = cpu_single_env;
  569. }
  570. } /* for(;;) */
  571. #if defined(TARGET_I386)
  572. /* restore flags in standard format */
  573. env->eflags = env->eflags | cpu_cc_compute_all(env, CC_OP)
  574. | (DF & DF_MASK);
  575. #elif defined(TARGET_ARM)
  576. /* XXX: Save/restore host fpu exception state?. */
  577. #elif defined(TARGET_UNICORE32)
  578. #elif defined(TARGET_SPARC)
  579. #elif defined(TARGET_PPC)
  580. #elif defined(TARGET_LM32)
  581. #elif defined(TARGET_M68K)
  582. cpu_m68k_flush_flags(env, env->cc_op);
  583. env->cc_op = CC_OP_FLAGS;
  584. env->sr = (env->sr & 0xffe0)
  585. | env->cc_dest | (env->cc_x << 4);
  586. #elif defined(TARGET_MICROBLAZE)
  587. #elif defined(TARGET_MIPS)
  588. #elif defined(TARGET_SH4)
  589. #elif defined(TARGET_ALPHA)
  590. #elif defined(TARGET_CRIS)
  591. #elif defined(TARGET_S390X)
  592. #elif defined(TARGET_XTENSA)
  593. /* XXXXX */
  594. #else
  595. #error unsupported target CPU
  596. #endif
  597. /* fail safe : never use cpu_single_env outside cpu_exec() */
  598. cpu_single_env = NULL;
  599. return ret;
  600. }