cpu-exec.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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 "qemu/osdep.h"
  20. #include "cpu.h"
  21. #include "trace-root.h"
  22. #include "disas/disas.h"
  23. #include "exec/exec-all.h"
  24. #include "tcg.h"
  25. #include "qemu/atomic.h"
  26. #include "sysemu/qtest.h"
  27. #include "qemu/timer.h"
  28. #include "exec/address-spaces.h"
  29. #include "qemu/rcu.h"
  30. #include "exec/tb-hash.h"
  31. #include "exec/log.h"
  32. #include "qemu/main-loop.h"
  33. #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
  34. #include "hw/i386/apic.h"
  35. #endif
  36. #include "sysemu/cpus.h"
  37. #include "sysemu/replay.h"
  38. /* -icount align implementation. */
  39. typedef struct SyncClocks {
  40. int64_t diff_clk;
  41. int64_t last_cpu_icount;
  42. int64_t realtime_clock;
  43. } SyncClocks;
  44. #if !defined(CONFIG_USER_ONLY)
  45. /* Allow the guest to have a max 3ms advance.
  46. * The difference between the 2 clocks could therefore
  47. * oscillate around 0.
  48. */
  49. #define VM_CLOCK_ADVANCE 3000000
  50. #define THRESHOLD_REDUCE 1.5
  51. #define MAX_DELAY_PRINT_RATE 2000000000LL
  52. #define MAX_NB_PRINTS 100
  53. static void align_clocks(SyncClocks *sc, const CPUState *cpu)
  54. {
  55. int64_t cpu_icount;
  56. if (!icount_align_option) {
  57. return;
  58. }
  59. cpu_icount = cpu->icount_extra + cpu->icount_decr.u16.low;
  60. sc->diff_clk += cpu_icount_to_ns(sc->last_cpu_icount - cpu_icount);
  61. sc->last_cpu_icount = cpu_icount;
  62. if (sc->diff_clk > VM_CLOCK_ADVANCE) {
  63. #ifndef _WIN32
  64. struct timespec sleep_delay, rem_delay;
  65. sleep_delay.tv_sec = sc->diff_clk / 1000000000LL;
  66. sleep_delay.tv_nsec = sc->diff_clk % 1000000000LL;
  67. if (nanosleep(&sleep_delay, &rem_delay) < 0) {
  68. sc->diff_clk = rem_delay.tv_sec * 1000000000LL + rem_delay.tv_nsec;
  69. } else {
  70. sc->diff_clk = 0;
  71. }
  72. #else
  73. Sleep(sc->diff_clk / SCALE_MS);
  74. sc->diff_clk = 0;
  75. #endif
  76. }
  77. }
  78. static void print_delay(const SyncClocks *sc)
  79. {
  80. static float threshold_delay;
  81. static int64_t last_realtime_clock;
  82. static int nb_prints;
  83. if (icount_align_option &&
  84. sc->realtime_clock - last_realtime_clock >= MAX_DELAY_PRINT_RATE &&
  85. nb_prints < MAX_NB_PRINTS) {
  86. if ((-sc->diff_clk / (float)1000000000LL > threshold_delay) ||
  87. (-sc->diff_clk / (float)1000000000LL <
  88. (threshold_delay - THRESHOLD_REDUCE))) {
  89. threshold_delay = (-sc->diff_clk / 1000000000LL) + 1;
  90. printf("Warning: The guest is now late by %.1f to %.1f seconds\n",
  91. threshold_delay - 1,
  92. threshold_delay);
  93. nb_prints++;
  94. last_realtime_clock = sc->realtime_clock;
  95. }
  96. }
  97. }
  98. static void init_delay_params(SyncClocks *sc,
  99. const CPUState *cpu)
  100. {
  101. if (!icount_align_option) {
  102. return;
  103. }
  104. sc->realtime_clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
  105. sc->diff_clk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - sc->realtime_clock;
  106. sc->last_cpu_icount = cpu->icount_extra + cpu->icount_decr.u16.low;
  107. if (sc->diff_clk < max_delay) {
  108. max_delay = sc->diff_clk;
  109. }
  110. if (sc->diff_clk > max_advance) {
  111. max_advance = sc->diff_clk;
  112. }
  113. /* Print every 2s max if the guest is late. We limit the number
  114. of printed messages to NB_PRINT_MAX(currently 100) */
  115. print_delay(sc);
  116. }
  117. #else
  118. static void align_clocks(SyncClocks *sc, const CPUState *cpu)
  119. {
  120. }
  121. static void init_delay_params(SyncClocks *sc, const CPUState *cpu)
  122. {
  123. }
  124. #endif /* CONFIG USER ONLY */
  125. /* Execute a TB, and fix up the CPU state afterwards if necessary */
  126. static inline tcg_target_ulong cpu_tb_exec(CPUState *cpu, TranslationBlock *itb)
  127. {
  128. CPUArchState *env = cpu->env_ptr;
  129. uintptr_t ret;
  130. TranslationBlock *last_tb;
  131. int tb_exit;
  132. uint8_t *tb_ptr = itb->tc_ptr;
  133. qemu_log_mask_and_addr(CPU_LOG_EXEC, itb->pc,
  134. "Trace %p [%d: " TARGET_FMT_lx "] %s\n",
  135. itb->tc_ptr, cpu->cpu_index, itb->pc,
  136. lookup_symbol(itb->pc));
  137. #if defined(DEBUG_DISAS)
  138. if (qemu_loglevel_mask(CPU_LOG_TB_CPU)
  139. && qemu_log_in_addr_range(itb->pc)) {
  140. qemu_log_lock();
  141. #if defined(TARGET_I386)
  142. log_cpu_state(cpu, CPU_DUMP_CCOP);
  143. #else
  144. log_cpu_state(cpu, 0);
  145. #endif
  146. qemu_log_unlock();
  147. }
  148. #endif /* DEBUG_DISAS */
  149. cpu->can_do_io = !use_icount;
  150. ret = tcg_qemu_tb_exec(env, tb_ptr);
  151. cpu->can_do_io = 1;
  152. last_tb = (TranslationBlock *)(ret & ~TB_EXIT_MASK);
  153. tb_exit = ret & TB_EXIT_MASK;
  154. trace_exec_tb_exit(last_tb, tb_exit);
  155. if (tb_exit > TB_EXIT_IDX1) {
  156. /* We didn't start executing this TB (eg because the instruction
  157. * counter hit zero); we must restore the guest PC to the address
  158. * of the start of the TB.
  159. */
  160. CPUClass *cc = CPU_GET_CLASS(cpu);
  161. qemu_log_mask_and_addr(CPU_LOG_EXEC, last_tb->pc,
  162. "Stopped execution of TB chain before %p ["
  163. TARGET_FMT_lx "] %s\n",
  164. last_tb->tc_ptr, last_tb->pc,
  165. lookup_symbol(last_tb->pc));
  166. if (cc->synchronize_from_tb) {
  167. cc->synchronize_from_tb(cpu, last_tb);
  168. } else {
  169. assert(cc->set_pc);
  170. cc->set_pc(cpu, last_tb->pc);
  171. }
  172. }
  173. return ret;
  174. }
  175. #ifndef CONFIG_USER_ONLY
  176. /* Execute the code without caching the generated code. An interpreter
  177. could be used if available. */
  178. static void cpu_exec_nocache(CPUState *cpu, int max_cycles,
  179. TranslationBlock *orig_tb, bool ignore_icount)
  180. {
  181. TranslationBlock *tb;
  182. /* Should never happen.
  183. We only end up here when an existing TB is too long. */
  184. if (max_cycles > CF_COUNT_MASK)
  185. max_cycles = CF_COUNT_MASK;
  186. tb_lock();
  187. tb = tb_gen_code(cpu, orig_tb->pc, orig_tb->cs_base, orig_tb->flags,
  188. max_cycles | CF_NOCACHE
  189. | (ignore_icount ? CF_IGNORE_ICOUNT : 0));
  190. tb->orig_tb = orig_tb;
  191. tb_unlock();
  192. /* execute the generated code */
  193. trace_exec_tb_nocache(tb, tb->pc);
  194. cpu_tb_exec(cpu, tb);
  195. tb_lock();
  196. tb_phys_invalidate(tb, -1);
  197. tb_free(tb);
  198. tb_unlock();
  199. }
  200. #endif
  201. static void cpu_exec_step(CPUState *cpu)
  202. {
  203. CPUClass *cc = CPU_GET_CLASS(cpu);
  204. CPUArchState *env = (CPUArchState *)cpu->env_ptr;
  205. TranslationBlock *tb;
  206. target_ulong cs_base, pc;
  207. uint32_t flags;
  208. cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
  209. if (sigsetjmp(cpu->jmp_env, 0) == 0) {
  210. mmap_lock();
  211. tb_lock();
  212. tb = tb_gen_code(cpu, pc, cs_base, flags,
  213. 1 | CF_NOCACHE | CF_IGNORE_ICOUNT);
  214. tb->orig_tb = NULL;
  215. tb_unlock();
  216. mmap_unlock();
  217. cc->cpu_exec_enter(cpu);
  218. /* execute the generated code */
  219. trace_exec_tb_nocache(tb, pc);
  220. cpu_tb_exec(cpu, tb);
  221. cc->cpu_exec_exit(cpu);
  222. tb_lock();
  223. tb_phys_invalidate(tb, -1);
  224. tb_free(tb);
  225. tb_unlock();
  226. } else {
  227. /* We may have exited due to another problem here, so we need
  228. * to reset any tb_locks we may have taken but didn't release.
  229. * The mmap_lock is dropped by tb_gen_code if it runs out of
  230. * memory.
  231. */
  232. #ifndef CONFIG_SOFTMMU
  233. tcg_debug_assert(!have_mmap_lock());
  234. #endif
  235. tb_lock_reset();
  236. }
  237. }
  238. void cpu_exec_step_atomic(CPUState *cpu)
  239. {
  240. start_exclusive();
  241. /* Since we got here, we know that parallel_cpus must be true. */
  242. parallel_cpus = false;
  243. cpu_exec_step(cpu);
  244. parallel_cpus = true;
  245. end_exclusive();
  246. }
  247. struct tb_desc {
  248. target_ulong pc;
  249. target_ulong cs_base;
  250. CPUArchState *env;
  251. tb_page_addr_t phys_page1;
  252. uint32_t flags;
  253. };
  254. static bool tb_cmp(const void *p, const void *d)
  255. {
  256. const TranslationBlock *tb = p;
  257. const struct tb_desc *desc = d;
  258. if (tb->pc == desc->pc &&
  259. tb->page_addr[0] == desc->phys_page1 &&
  260. tb->cs_base == desc->cs_base &&
  261. tb->flags == desc->flags &&
  262. !atomic_read(&tb->invalid)) {
  263. /* check next page if needed */
  264. if (tb->page_addr[1] == -1) {
  265. return true;
  266. } else {
  267. tb_page_addr_t phys_page2;
  268. target_ulong virt_page2;
  269. virt_page2 = (desc->pc & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
  270. phys_page2 = get_page_addr_code(desc->env, virt_page2);
  271. if (tb->page_addr[1] == phys_page2) {
  272. return true;
  273. }
  274. }
  275. }
  276. return false;
  277. }
  278. static TranslationBlock *tb_htable_lookup(CPUState *cpu,
  279. target_ulong pc,
  280. target_ulong cs_base,
  281. uint32_t flags)
  282. {
  283. tb_page_addr_t phys_pc;
  284. struct tb_desc desc;
  285. uint32_t h;
  286. desc.env = (CPUArchState *)cpu->env_ptr;
  287. desc.cs_base = cs_base;
  288. desc.flags = flags;
  289. desc.pc = pc;
  290. phys_pc = get_page_addr_code(desc.env, pc);
  291. desc.phys_page1 = phys_pc & TARGET_PAGE_MASK;
  292. h = tb_hash_func(phys_pc, pc, flags);
  293. return qht_lookup(&tcg_ctx.tb_ctx.htable, tb_cmp, &desc, h);
  294. }
  295. static inline TranslationBlock *tb_find(CPUState *cpu,
  296. TranslationBlock *last_tb,
  297. int tb_exit)
  298. {
  299. CPUArchState *env = (CPUArchState *)cpu->env_ptr;
  300. TranslationBlock *tb;
  301. target_ulong cs_base, pc;
  302. uint32_t flags;
  303. bool have_tb_lock = false;
  304. /* we record a subset of the CPU state. It will
  305. always be the same before a given translated block
  306. is executed. */
  307. cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
  308. tb = atomic_rcu_read(&cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)]);
  309. if (unlikely(!tb || tb->pc != pc || tb->cs_base != cs_base ||
  310. tb->flags != flags)) {
  311. tb = tb_htable_lookup(cpu, pc, cs_base, flags);
  312. if (!tb) {
  313. /* mmap_lock is needed by tb_gen_code, and mmap_lock must be
  314. * taken outside tb_lock. As system emulation is currently
  315. * single threaded the locks are NOPs.
  316. */
  317. mmap_lock();
  318. tb_lock();
  319. have_tb_lock = true;
  320. /* There's a chance that our desired tb has been translated while
  321. * taking the locks so we check again inside the lock.
  322. */
  323. tb = tb_htable_lookup(cpu, pc, cs_base, flags);
  324. if (!tb) {
  325. /* if no translated code available, then translate it now */
  326. tb = tb_gen_code(cpu, pc, cs_base, flags, 0);
  327. }
  328. mmap_unlock();
  329. }
  330. /* We add the TB in the virtual pc hash table for the fast lookup */
  331. atomic_set(&cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)], tb);
  332. }
  333. #ifndef CONFIG_USER_ONLY
  334. /* We don't take care of direct jumps when address mapping changes in
  335. * system emulation. So it's not safe to make a direct jump to a TB
  336. * spanning two pages because the mapping for the second page can change.
  337. */
  338. if (tb->page_addr[1] != -1) {
  339. last_tb = NULL;
  340. }
  341. #endif
  342. /* See if we can patch the calling TB. */
  343. if (last_tb && !qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN)) {
  344. if (!have_tb_lock) {
  345. tb_lock();
  346. have_tb_lock = true;
  347. }
  348. if (!tb->invalid) {
  349. tb_add_jump(last_tb, tb_exit, tb);
  350. }
  351. }
  352. if (have_tb_lock) {
  353. tb_unlock();
  354. }
  355. return tb;
  356. }
  357. static inline bool cpu_handle_halt(CPUState *cpu)
  358. {
  359. if (cpu->halted) {
  360. #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
  361. if ((cpu->interrupt_request & CPU_INTERRUPT_POLL)
  362. && replay_interrupt()) {
  363. X86CPU *x86_cpu = X86_CPU(cpu);
  364. qemu_mutex_lock_iothread();
  365. apic_poll_irq(x86_cpu->apic_state);
  366. cpu_reset_interrupt(cpu, CPU_INTERRUPT_POLL);
  367. qemu_mutex_unlock_iothread();
  368. }
  369. #endif
  370. if (!cpu_has_work(cpu)) {
  371. return true;
  372. }
  373. cpu->halted = 0;
  374. }
  375. return false;
  376. }
  377. static inline void cpu_handle_debug_exception(CPUState *cpu)
  378. {
  379. CPUClass *cc = CPU_GET_CLASS(cpu);
  380. CPUWatchpoint *wp;
  381. if (!cpu->watchpoint_hit) {
  382. QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
  383. wp->flags &= ~BP_WATCHPOINT_HIT;
  384. }
  385. }
  386. cc->debug_excp_handler(cpu);
  387. }
  388. static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
  389. {
  390. if (cpu->exception_index >= 0) {
  391. if (cpu->exception_index >= EXCP_INTERRUPT) {
  392. /* exit request from the cpu execution loop */
  393. *ret = cpu->exception_index;
  394. if (*ret == EXCP_DEBUG) {
  395. cpu_handle_debug_exception(cpu);
  396. }
  397. cpu->exception_index = -1;
  398. return true;
  399. } else {
  400. #if defined(CONFIG_USER_ONLY)
  401. /* if user mode only, we simulate a fake exception
  402. which will be handled outside the cpu execution
  403. loop */
  404. #if defined(TARGET_I386)
  405. CPUClass *cc = CPU_GET_CLASS(cpu);
  406. cc->do_interrupt(cpu);
  407. #endif
  408. *ret = cpu->exception_index;
  409. cpu->exception_index = -1;
  410. return true;
  411. #else
  412. if (replay_exception()) {
  413. CPUClass *cc = CPU_GET_CLASS(cpu);
  414. qemu_mutex_lock_iothread();
  415. cc->do_interrupt(cpu);
  416. qemu_mutex_unlock_iothread();
  417. cpu->exception_index = -1;
  418. } else if (!replay_has_interrupt()) {
  419. /* give a chance to iothread in replay mode */
  420. *ret = EXCP_INTERRUPT;
  421. return true;
  422. }
  423. #endif
  424. }
  425. #ifndef CONFIG_USER_ONLY
  426. } else if (replay_has_exception()
  427. && cpu->icount_decr.u16.low + cpu->icount_extra == 0) {
  428. /* try to cause an exception pending in the log */
  429. cpu_exec_nocache(cpu, 1, tb_find(cpu, NULL, 0), true);
  430. *ret = -1;
  431. return true;
  432. #endif
  433. }
  434. return false;
  435. }
  436. static inline bool cpu_handle_interrupt(CPUState *cpu,
  437. TranslationBlock **last_tb)
  438. {
  439. CPUClass *cc = CPU_GET_CLASS(cpu);
  440. if (unlikely(atomic_read(&cpu->interrupt_request))) {
  441. int interrupt_request;
  442. qemu_mutex_lock_iothread();
  443. interrupt_request = cpu->interrupt_request;
  444. if (unlikely(cpu->singlestep_enabled & SSTEP_NOIRQ)) {
  445. /* Mask out external interrupts for this step. */
  446. interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK;
  447. }
  448. if (interrupt_request & CPU_INTERRUPT_DEBUG) {
  449. cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
  450. cpu->exception_index = EXCP_DEBUG;
  451. qemu_mutex_unlock_iothread();
  452. return true;
  453. }
  454. if (replay_mode == REPLAY_MODE_PLAY && !replay_has_interrupt()) {
  455. /* Do nothing */
  456. } else if (interrupt_request & CPU_INTERRUPT_HALT) {
  457. replay_interrupt();
  458. cpu->interrupt_request &= ~CPU_INTERRUPT_HALT;
  459. cpu->halted = 1;
  460. cpu->exception_index = EXCP_HLT;
  461. qemu_mutex_unlock_iothread();
  462. return true;
  463. }
  464. #if defined(TARGET_I386)
  465. else if (interrupt_request & CPU_INTERRUPT_INIT) {
  466. X86CPU *x86_cpu = X86_CPU(cpu);
  467. CPUArchState *env = &x86_cpu->env;
  468. replay_interrupt();
  469. cpu_svm_check_intercept_param(env, SVM_EXIT_INIT, 0, 0);
  470. do_cpu_init(x86_cpu);
  471. cpu->exception_index = EXCP_HALTED;
  472. qemu_mutex_unlock_iothread();
  473. return true;
  474. }
  475. #else
  476. else if (interrupt_request & CPU_INTERRUPT_RESET) {
  477. replay_interrupt();
  478. cpu_reset(cpu);
  479. qemu_mutex_unlock_iothread();
  480. return true;
  481. }
  482. #endif
  483. /* The target hook has 3 exit conditions:
  484. False when the interrupt isn't processed,
  485. True when it is, and we should restart on a new TB,
  486. and via longjmp via cpu_loop_exit. */
  487. else {
  488. if (cc->cpu_exec_interrupt(cpu, interrupt_request)) {
  489. replay_interrupt();
  490. *last_tb = NULL;
  491. }
  492. /* The target hook may have updated the 'cpu->interrupt_request';
  493. * reload the 'interrupt_request' value */
  494. interrupt_request = cpu->interrupt_request;
  495. }
  496. if (interrupt_request & CPU_INTERRUPT_EXITTB) {
  497. cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
  498. /* ensure that no TB jump will be modified as
  499. the program flow was changed */
  500. *last_tb = NULL;
  501. }
  502. /* If we exit via cpu_loop_exit/longjmp it is reset in cpu_exec */
  503. qemu_mutex_unlock_iothread();
  504. }
  505. /* Finally, check if we need to exit to the main loop. */
  506. if (unlikely(atomic_read(&cpu->exit_request)
  507. || (use_icount && cpu->icount_decr.u16.low + cpu->icount_extra == 0))) {
  508. atomic_set(&cpu->exit_request, 0);
  509. cpu->exception_index = EXCP_INTERRUPT;
  510. return true;
  511. }
  512. return false;
  513. }
  514. static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,
  515. TranslationBlock **last_tb, int *tb_exit)
  516. {
  517. uintptr_t ret;
  518. int32_t insns_left;
  519. trace_exec_tb(tb, tb->pc);
  520. ret = cpu_tb_exec(cpu, tb);
  521. tb = (TranslationBlock *)(ret & ~TB_EXIT_MASK);
  522. *tb_exit = ret & TB_EXIT_MASK;
  523. if (*tb_exit != TB_EXIT_REQUESTED) {
  524. *last_tb = tb;
  525. return;
  526. }
  527. *last_tb = NULL;
  528. insns_left = atomic_read(&cpu->icount_decr.u32);
  529. atomic_set(&cpu->icount_decr.u16.high, 0);
  530. if (insns_left < 0) {
  531. /* Something asked us to stop executing chained TBs; just
  532. * continue round the main loop. Whatever requested the exit
  533. * will also have set something else (eg exit_request or
  534. * interrupt_request) which we will handle next time around
  535. * the loop. But we need to ensure the zeroing of icount_decr
  536. * comes before the next read of cpu->exit_request
  537. * or cpu->interrupt_request.
  538. */
  539. smp_mb();
  540. return;
  541. }
  542. /* Instruction counter expired. */
  543. assert(use_icount);
  544. #ifndef CONFIG_USER_ONLY
  545. /* Ensure global icount has gone forward */
  546. cpu_update_icount(cpu);
  547. /* Refill decrementer and continue execution. */
  548. insns_left = MIN(0xffff, cpu->icount_budget);
  549. cpu->icount_decr.u16.low = insns_left;
  550. cpu->icount_extra = cpu->icount_budget - insns_left;
  551. if (!cpu->icount_extra) {
  552. /* Execute any remaining instructions, then let the main loop
  553. * handle the next event.
  554. */
  555. if (insns_left > 0) {
  556. cpu_exec_nocache(cpu, insns_left, tb, false);
  557. }
  558. }
  559. #endif
  560. }
  561. /* main execution loop */
  562. int cpu_exec(CPUState *cpu)
  563. {
  564. CPUClass *cc = CPU_GET_CLASS(cpu);
  565. int ret;
  566. SyncClocks sc = { 0 };
  567. /* replay_interrupt may need current_cpu */
  568. current_cpu = cpu;
  569. if (cpu_handle_halt(cpu)) {
  570. return EXCP_HALTED;
  571. }
  572. rcu_read_lock();
  573. cc->cpu_exec_enter(cpu);
  574. /* Calculate difference between guest clock and host clock.
  575. * This delay includes the delay of the last cycle, so
  576. * what we have to do is sleep until it is 0. As for the
  577. * advance/delay we gain here, we try to fix it next time.
  578. */
  579. init_delay_params(&sc, cpu);
  580. /* prepare setjmp context for exception handling */
  581. if (sigsetjmp(cpu->jmp_env, 0) != 0) {
  582. #if defined(__clang__) || !QEMU_GNUC_PREREQ(4, 6)
  583. /* Some compilers wrongly smash all local variables after
  584. * siglongjmp. There were bug reports for gcc 4.5.0 and clang.
  585. * Reload essential local variables here for those compilers.
  586. * Newer versions of gcc would complain about this code (-Wclobbered). */
  587. cpu = current_cpu;
  588. cc = CPU_GET_CLASS(cpu);
  589. #else /* buggy compiler */
  590. /* Assert that the compiler does not smash local variables. */
  591. g_assert(cpu == current_cpu);
  592. g_assert(cc == CPU_GET_CLASS(cpu));
  593. #endif /* buggy compiler */
  594. cpu->can_do_io = 1;
  595. tb_lock_reset();
  596. if (qemu_mutex_iothread_locked()) {
  597. qemu_mutex_unlock_iothread();
  598. }
  599. }
  600. /* if an exception is pending, we execute it here */
  601. while (!cpu_handle_exception(cpu, &ret)) {
  602. TranslationBlock *last_tb = NULL;
  603. int tb_exit = 0;
  604. while (!cpu_handle_interrupt(cpu, &last_tb)) {
  605. TranslationBlock *tb = tb_find(cpu, last_tb, tb_exit);
  606. cpu_loop_exec_tb(cpu, tb, &last_tb, &tb_exit);
  607. /* Try to align the host and virtual clocks
  608. if the guest is in advance */
  609. align_clocks(&sc, cpu);
  610. }
  611. }
  612. cc->cpu_exec_exit(cpu);
  613. rcu_read_unlock();
  614. return ret;
  615. }