2
0

cpu-exec.c 30 KB

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