2
0

cpu-exec.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 "trace.h"
  22. #include "disas/disas.h"
  23. #include "tcg.h"
  24. #include "qemu/atomic.h"
  25. #include "sysemu/qtest.h"
  26. #include "qemu/timer.h"
  27. /* -icount align implementation. */
  28. typedef struct SyncClocks {
  29. int64_t diff_clk;
  30. int64_t last_cpu_icount;
  31. int64_t realtime_clock;
  32. } SyncClocks;
  33. #if !defined(CONFIG_USER_ONLY)
  34. /* Allow the guest to have a max 3ms advance.
  35. * The difference between the 2 clocks could therefore
  36. * oscillate around 0.
  37. */
  38. #define VM_CLOCK_ADVANCE 3000000
  39. #define THRESHOLD_REDUCE 1.5
  40. #define MAX_DELAY_PRINT_RATE 2000000000LL
  41. #define MAX_NB_PRINTS 100
  42. static void align_clocks(SyncClocks *sc, const CPUState *cpu)
  43. {
  44. int64_t cpu_icount;
  45. if (!icount_align_option) {
  46. return;
  47. }
  48. cpu_icount = cpu->icount_extra + cpu->icount_decr.u16.low;
  49. sc->diff_clk += cpu_icount_to_ns(sc->last_cpu_icount - cpu_icount);
  50. sc->last_cpu_icount = cpu_icount;
  51. if (sc->diff_clk > VM_CLOCK_ADVANCE) {
  52. #ifndef _WIN32
  53. struct timespec sleep_delay, rem_delay;
  54. sleep_delay.tv_sec = sc->diff_clk / 1000000000LL;
  55. sleep_delay.tv_nsec = sc->diff_clk % 1000000000LL;
  56. if (nanosleep(&sleep_delay, &rem_delay) < 0) {
  57. sc->diff_clk -= (sleep_delay.tv_sec - rem_delay.tv_sec) * 1000000000LL;
  58. sc->diff_clk -= sleep_delay.tv_nsec - rem_delay.tv_nsec;
  59. } else {
  60. sc->diff_clk = 0;
  61. }
  62. #else
  63. Sleep(sc->diff_clk / SCALE_MS);
  64. sc->diff_clk = 0;
  65. #endif
  66. }
  67. }
  68. static void print_delay(const SyncClocks *sc)
  69. {
  70. static float threshold_delay;
  71. static int64_t last_realtime_clock;
  72. static int nb_prints;
  73. if (icount_align_option &&
  74. sc->realtime_clock - last_realtime_clock >= MAX_DELAY_PRINT_RATE &&
  75. nb_prints < MAX_NB_PRINTS) {
  76. if ((-sc->diff_clk / (float)1000000000LL > threshold_delay) ||
  77. (-sc->diff_clk / (float)1000000000LL <
  78. (threshold_delay - THRESHOLD_REDUCE))) {
  79. threshold_delay = (-sc->diff_clk / 1000000000LL) + 1;
  80. printf("Warning: The guest is now late by %.1f to %.1f seconds\n",
  81. threshold_delay - 1,
  82. threshold_delay);
  83. nb_prints++;
  84. last_realtime_clock = sc->realtime_clock;
  85. }
  86. }
  87. }
  88. static void init_delay_params(SyncClocks *sc,
  89. const CPUState *cpu)
  90. {
  91. if (!icount_align_option) {
  92. return;
  93. }
  94. sc->realtime_clock = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
  95. sc->diff_clk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) -
  96. sc->realtime_clock +
  97. cpu_get_clock_offset();
  98. sc->last_cpu_icount = cpu->icount_extra + cpu->icount_decr.u16.low;
  99. if (sc->diff_clk < max_delay) {
  100. max_delay = sc->diff_clk;
  101. }
  102. if (sc->diff_clk > max_advance) {
  103. max_advance = sc->diff_clk;
  104. }
  105. /* Print every 2s max if the guest is late. We limit the number
  106. of printed messages to NB_PRINT_MAX(currently 100) */
  107. print_delay(sc);
  108. }
  109. #else
  110. static void align_clocks(SyncClocks *sc, const CPUState *cpu)
  111. {
  112. }
  113. static void init_delay_params(SyncClocks *sc, const CPUState *cpu)
  114. {
  115. }
  116. #endif /* CONFIG USER ONLY */
  117. void cpu_loop_exit(CPUState *cpu)
  118. {
  119. cpu->current_tb = NULL;
  120. siglongjmp(cpu->jmp_env, 1);
  121. }
  122. /* exit the current TB from a signal handler. The host registers are
  123. restored in a state compatible with the CPU emulator
  124. */
  125. #if defined(CONFIG_SOFTMMU)
  126. void cpu_resume_from_signal(CPUState *cpu, void *puc)
  127. {
  128. /* XXX: restore cpu registers saved in host registers */
  129. cpu->exception_index = -1;
  130. siglongjmp(cpu->jmp_env, 1);
  131. }
  132. #endif
  133. /* Execute a TB, and fix up the CPU state afterwards if necessary */
  134. static inline tcg_target_ulong cpu_tb_exec(CPUState *cpu, uint8_t *tb_ptr)
  135. {
  136. CPUArchState *env = cpu->env_ptr;
  137. uintptr_t next_tb;
  138. #if defined(DEBUG_DISAS)
  139. if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
  140. #if defined(TARGET_I386)
  141. log_cpu_state(cpu, CPU_DUMP_CCOP);
  142. #elif defined(TARGET_M68K)
  143. /* ??? Should not modify env state for dumping. */
  144. cpu_m68k_flush_flags(env, env->cc_op);
  145. env->cc_op = CC_OP_FLAGS;
  146. env->sr = (env->sr & 0xffe0) | env->cc_dest | (env->cc_x << 4);
  147. log_cpu_state(cpu, 0);
  148. #else
  149. log_cpu_state(cpu, 0);
  150. #endif
  151. }
  152. #endif /* DEBUG_DISAS */
  153. next_tb = tcg_qemu_tb_exec(env, tb_ptr);
  154. trace_exec_tb_exit((void *) (next_tb & ~TB_EXIT_MASK),
  155. next_tb & TB_EXIT_MASK);
  156. if ((next_tb & TB_EXIT_MASK) > TB_EXIT_IDX1) {
  157. /* We didn't start executing this TB (eg because the instruction
  158. * counter hit zero); we must restore the guest PC to the address
  159. * of the start of the TB.
  160. */
  161. CPUClass *cc = CPU_GET_CLASS(cpu);
  162. TranslationBlock *tb = (TranslationBlock *)(next_tb & ~TB_EXIT_MASK);
  163. if (cc->synchronize_from_tb) {
  164. cc->synchronize_from_tb(cpu, tb);
  165. } else {
  166. assert(cc->set_pc);
  167. cc->set_pc(cpu, tb->pc);
  168. }
  169. }
  170. if ((next_tb & TB_EXIT_MASK) == TB_EXIT_REQUESTED) {
  171. /* We were asked to stop executing TBs (probably a pending
  172. * interrupt. We've now stopped, so clear the flag.
  173. */
  174. cpu->tcg_exit_req = 0;
  175. }
  176. return next_tb;
  177. }
  178. /* Execute the code without caching the generated code. An interpreter
  179. could be used if available. */
  180. static void cpu_exec_nocache(CPUArchState *env, int max_cycles,
  181. TranslationBlock *orig_tb)
  182. {
  183. CPUState *cpu = ENV_GET_CPU(env);
  184. TranslationBlock *tb;
  185. /* Should never happen.
  186. We only end up here when an existing TB is too long. */
  187. if (max_cycles > CF_COUNT_MASK)
  188. max_cycles = CF_COUNT_MASK;
  189. tb = tb_gen_code(cpu, orig_tb->pc, orig_tb->cs_base, orig_tb->flags,
  190. max_cycles);
  191. cpu->current_tb = tb;
  192. /* execute the generated code */
  193. trace_exec_tb_nocache(tb, tb->pc);
  194. cpu_tb_exec(cpu, tb->tc_ptr);
  195. cpu->current_tb = NULL;
  196. tb_phys_invalidate(tb, -1);
  197. tb_free(tb);
  198. }
  199. static TranslationBlock *tb_find_slow(CPUArchState *env,
  200. target_ulong pc,
  201. target_ulong cs_base,
  202. uint64_t flags)
  203. {
  204. CPUState *cpu = ENV_GET_CPU(env);
  205. TranslationBlock *tb, **ptb1;
  206. unsigned int h;
  207. tb_page_addr_t phys_pc, phys_page1;
  208. target_ulong virt_page2;
  209. tcg_ctx.tb_ctx.tb_invalidated_flag = 0;
  210. /* find translated block using physical mappings */
  211. phys_pc = get_page_addr_code(env, pc);
  212. phys_page1 = phys_pc & TARGET_PAGE_MASK;
  213. h = tb_phys_hash_func(phys_pc);
  214. ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
  215. for(;;) {
  216. tb = *ptb1;
  217. if (!tb)
  218. goto not_found;
  219. if (tb->pc == pc &&
  220. tb->page_addr[0] == phys_page1 &&
  221. tb->cs_base == cs_base &&
  222. tb->flags == flags) {
  223. /* check next page if needed */
  224. if (tb->page_addr[1] != -1) {
  225. tb_page_addr_t phys_page2;
  226. virt_page2 = (pc & TARGET_PAGE_MASK) +
  227. TARGET_PAGE_SIZE;
  228. phys_page2 = get_page_addr_code(env, virt_page2);
  229. if (tb->page_addr[1] == phys_page2)
  230. goto found;
  231. } else {
  232. goto found;
  233. }
  234. }
  235. ptb1 = &tb->phys_hash_next;
  236. }
  237. not_found:
  238. /* if no translated code available, then translate it now */
  239. tb = tb_gen_code(cpu, pc, cs_base, flags, 0);
  240. found:
  241. /* Move the last found TB to the head of the list */
  242. if (likely(*ptb1)) {
  243. *ptb1 = tb->phys_hash_next;
  244. tb->phys_hash_next = tcg_ctx.tb_ctx.tb_phys_hash[h];
  245. tcg_ctx.tb_ctx.tb_phys_hash[h] = tb;
  246. }
  247. /* we add the TB in the virtual pc hash table */
  248. cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)] = tb;
  249. return tb;
  250. }
  251. static inline TranslationBlock *tb_find_fast(CPUArchState *env)
  252. {
  253. CPUState *cpu = ENV_GET_CPU(env);
  254. TranslationBlock *tb;
  255. target_ulong cs_base, pc;
  256. int flags;
  257. /* we record a subset of the CPU state. It will
  258. always be the same before a given translated block
  259. is executed. */
  260. cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
  261. tb = cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)];
  262. if (unlikely(!tb || tb->pc != pc || tb->cs_base != cs_base ||
  263. tb->flags != flags)) {
  264. tb = tb_find_slow(env, pc, cs_base, flags);
  265. }
  266. return tb;
  267. }
  268. static void cpu_handle_debug_exception(CPUArchState *env)
  269. {
  270. CPUState *cpu = ENV_GET_CPU(env);
  271. CPUClass *cc = CPU_GET_CLASS(cpu);
  272. CPUWatchpoint *wp;
  273. if (!cpu->watchpoint_hit) {
  274. QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
  275. wp->flags &= ~BP_WATCHPOINT_HIT;
  276. }
  277. }
  278. cc->debug_excp_handler(cpu);
  279. }
  280. /* main execution loop */
  281. volatile sig_atomic_t exit_request;
  282. int cpu_exec(CPUArchState *env)
  283. {
  284. CPUState *cpu = ENV_GET_CPU(env);
  285. CPUClass *cc = CPU_GET_CLASS(cpu);
  286. #ifdef TARGET_I386
  287. X86CPU *x86_cpu = X86_CPU(cpu);
  288. #endif
  289. int ret, interrupt_request;
  290. TranslationBlock *tb;
  291. uint8_t *tc_ptr;
  292. uintptr_t next_tb;
  293. SyncClocks sc;
  294. /* This must be volatile so it is not trashed by longjmp() */
  295. volatile bool have_tb_lock = false;
  296. if (cpu->halted) {
  297. if (!cpu_has_work(cpu)) {
  298. return EXCP_HALTED;
  299. }
  300. cpu->halted = 0;
  301. }
  302. current_cpu = cpu;
  303. /* As long as current_cpu is null, up to the assignment just above,
  304. * requests by other threads to exit the execution loop are expected to
  305. * be issued using the exit_request global. We must make sure that our
  306. * evaluation of the global value is performed past the current_cpu
  307. * value transition point, which requires a memory barrier as well as
  308. * an instruction scheduling constraint on modern architectures. */
  309. smp_mb();
  310. if (unlikely(exit_request)) {
  311. cpu->exit_request = 1;
  312. }
  313. cc->cpu_exec_enter(cpu);
  314. cpu->exception_index = -1;
  315. /* Calculate difference between guest clock and host clock.
  316. * This delay includes the delay of the last cycle, so
  317. * what we have to do is sleep until it is 0. As for the
  318. * advance/delay we gain here, we try to fix it next time.
  319. */
  320. init_delay_params(&sc, cpu);
  321. /* prepare setjmp context for exception handling */
  322. for(;;) {
  323. if (sigsetjmp(cpu->jmp_env, 0) == 0) {
  324. /* if an exception is pending, we execute it here */
  325. if (cpu->exception_index >= 0) {
  326. if (cpu->exception_index >= EXCP_INTERRUPT) {
  327. /* exit request from the cpu execution loop */
  328. ret = cpu->exception_index;
  329. if (ret == EXCP_DEBUG) {
  330. cpu_handle_debug_exception(env);
  331. }
  332. break;
  333. } else {
  334. #if defined(CONFIG_USER_ONLY)
  335. /* if user mode only, we simulate a fake exception
  336. which will be handled outside the cpu execution
  337. loop */
  338. #if defined(TARGET_I386)
  339. cc->do_interrupt(cpu);
  340. #endif
  341. ret = cpu->exception_index;
  342. break;
  343. #else
  344. cc->do_interrupt(cpu);
  345. cpu->exception_index = -1;
  346. #endif
  347. }
  348. }
  349. next_tb = 0; /* force lookup of first TB */
  350. for(;;) {
  351. interrupt_request = cpu->interrupt_request;
  352. if (unlikely(interrupt_request)) {
  353. if (unlikely(cpu->singlestep_enabled & SSTEP_NOIRQ)) {
  354. /* Mask out external interrupts for this step. */
  355. interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK;
  356. }
  357. if (interrupt_request & CPU_INTERRUPT_DEBUG) {
  358. cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
  359. cpu->exception_index = EXCP_DEBUG;
  360. cpu_loop_exit(cpu);
  361. }
  362. if (interrupt_request & CPU_INTERRUPT_HALT) {
  363. cpu->interrupt_request &= ~CPU_INTERRUPT_HALT;
  364. cpu->halted = 1;
  365. cpu->exception_index = EXCP_HLT;
  366. cpu_loop_exit(cpu);
  367. }
  368. #if defined(TARGET_I386)
  369. if (interrupt_request & CPU_INTERRUPT_INIT) {
  370. cpu_svm_check_intercept_param(env, SVM_EXIT_INIT, 0);
  371. do_cpu_init(x86_cpu);
  372. cpu->exception_index = EXCP_HALTED;
  373. cpu_loop_exit(cpu);
  374. }
  375. #else
  376. if (interrupt_request & CPU_INTERRUPT_RESET) {
  377. cpu_reset(cpu);
  378. }
  379. #endif
  380. /* The target hook has 3 exit conditions:
  381. False when the interrupt isn't processed,
  382. True when it is, and we should restart on a new TB,
  383. and via longjmp via cpu_loop_exit. */
  384. if (cc->cpu_exec_interrupt(cpu, interrupt_request)) {
  385. next_tb = 0;
  386. }
  387. /* Don't use the cached interrupt_request value,
  388. do_interrupt may have updated the EXITTB flag. */
  389. if (cpu->interrupt_request & CPU_INTERRUPT_EXITTB) {
  390. cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
  391. /* ensure that no TB jump will be modified as
  392. the program flow was changed */
  393. next_tb = 0;
  394. }
  395. }
  396. if (unlikely(cpu->exit_request)) {
  397. cpu->exit_request = 0;
  398. cpu->exception_index = EXCP_INTERRUPT;
  399. cpu_loop_exit(cpu);
  400. }
  401. spin_lock(&tcg_ctx.tb_ctx.tb_lock);
  402. have_tb_lock = true;
  403. tb = tb_find_fast(env);
  404. /* Note: we do it here to avoid a gcc bug on Mac OS X when
  405. doing it in tb_find_slow */
  406. if (tcg_ctx.tb_ctx.tb_invalidated_flag) {
  407. /* as some TB could have been invalidated because
  408. of memory exceptions while generating the code, we
  409. must recompute the hash index here */
  410. next_tb = 0;
  411. tcg_ctx.tb_ctx.tb_invalidated_flag = 0;
  412. }
  413. if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
  414. qemu_log("Trace %p [" TARGET_FMT_lx "] %s\n",
  415. tb->tc_ptr, tb->pc, lookup_symbol(tb->pc));
  416. }
  417. /* see if we can patch the calling TB. When the TB
  418. spans two pages, we cannot safely do a direct
  419. jump. */
  420. if (next_tb != 0 && tb->page_addr[1] == -1) {
  421. tb_add_jump((TranslationBlock *)(next_tb & ~TB_EXIT_MASK),
  422. next_tb & TB_EXIT_MASK, tb);
  423. }
  424. have_tb_lock = false;
  425. spin_unlock(&tcg_ctx.tb_ctx.tb_lock);
  426. /* cpu_interrupt might be called while translating the
  427. TB, but before it is linked into a potentially
  428. infinite loop and becomes env->current_tb. Avoid
  429. starting execution if there is a pending interrupt. */
  430. cpu->current_tb = tb;
  431. barrier();
  432. if (likely(!cpu->exit_request)) {
  433. trace_exec_tb(tb, tb->pc);
  434. tc_ptr = tb->tc_ptr;
  435. /* execute the generated code */
  436. next_tb = cpu_tb_exec(cpu, tc_ptr);
  437. switch (next_tb & TB_EXIT_MASK) {
  438. case TB_EXIT_REQUESTED:
  439. /* Something asked us to stop executing
  440. * chained TBs; just continue round the main
  441. * loop. Whatever requested the exit will also
  442. * have set something else (eg exit_request or
  443. * interrupt_request) which we will handle
  444. * next time around the loop.
  445. */
  446. tb = (TranslationBlock *)(next_tb & ~TB_EXIT_MASK);
  447. next_tb = 0;
  448. break;
  449. case TB_EXIT_ICOUNT_EXPIRED:
  450. {
  451. /* Instruction counter expired. */
  452. int insns_left;
  453. tb = (TranslationBlock *)(next_tb & ~TB_EXIT_MASK);
  454. insns_left = cpu->icount_decr.u32;
  455. if (cpu->icount_extra && insns_left >= 0) {
  456. /* Refill decrementer and continue execution. */
  457. cpu->icount_extra += insns_left;
  458. if (cpu->icount_extra > 0xffff) {
  459. insns_left = 0xffff;
  460. } else {
  461. insns_left = cpu->icount_extra;
  462. }
  463. cpu->icount_extra -= insns_left;
  464. cpu->icount_decr.u16.low = insns_left;
  465. } else {
  466. if (insns_left > 0) {
  467. /* Execute remaining instructions. */
  468. cpu_exec_nocache(env, insns_left, tb);
  469. align_clocks(&sc, cpu);
  470. }
  471. cpu->exception_index = EXCP_INTERRUPT;
  472. next_tb = 0;
  473. cpu_loop_exit(cpu);
  474. }
  475. break;
  476. }
  477. default:
  478. break;
  479. }
  480. }
  481. cpu->current_tb = NULL;
  482. /* Try to align the host and virtual clocks
  483. if the guest is in advance */
  484. align_clocks(&sc, cpu);
  485. /* reset soft MMU for next block (it can currently
  486. only be set by a memory fault) */
  487. } /* for(;;) */
  488. } else {
  489. /* Reload env after longjmp - the compiler may have smashed all
  490. * local variables as longjmp is marked 'noreturn'. */
  491. cpu = current_cpu;
  492. env = cpu->env_ptr;
  493. cc = CPU_GET_CLASS(cpu);
  494. #ifdef TARGET_I386
  495. x86_cpu = X86_CPU(cpu);
  496. #endif
  497. if (have_tb_lock) {
  498. spin_unlock(&tcg_ctx.tb_ctx.tb_lock);
  499. have_tb_lock = false;
  500. }
  501. }
  502. } /* for(;;) */
  503. cc->cpu_exec_exit(cpu);
  504. /* fail safe : never use current_cpu outside cpu_exec() */
  505. current_cpu = NULL;
  506. return ret;
  507. }