cpu-exec.c 28 KB

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