cpu-exec.c 21 KB

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