2
0

cpu-exec.c 27 KB

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