cpu-exec.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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.1 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 "qemu/qemu-print.h"
  21. #include "qapi/error.h"
  22. #include "qapi/type-helpers.h"
  23. #include "hw/core/cpu.h"
  24. #include "accel/tcg/cpu-ops.h"
  25. #include "trace.h"
  26. #include "disas/disas.h"
  27. #include "exec/cpu-common.h"
  28. #include "exec/page-protection.h"
  29. #include "exec/translation-block.h"
  30. #include "tcg/tcg.h"
  31. #include "tcg/tcg-apple-jit.h"
  32. #include "qemu/atomic.h"
  33. #include "qemu/rcu.h"
  34. #include "exec/log.h"
  35. #include "qemu/main-loop.h"
  36. #include "exec/cpu-all.h"
  37. #include "system/cpu-timers.h"
  38. #include "exec/replay-core.h"
  39. #include "system/tcg.h"
  40. #include "exec/helper-proto-common.h"
  41. #include "tb-jmp-cache.h"
  42. #include "tb-hash.h"
  43. #include "tb-context.h"
  44. #include "tb-internal.h"
  45. #include "internal-common.h"
  46. #include "internal-target.h"
  47. /* -icount align implementation. */
  48. typedef struct SyncClocks {
  49. int64_t diff_clk;
  50. int64_t last_cpu_icount;
  51. int64_t realtime_clock;
  52. } SyncClocks;
  53. #if !defined(CONFIG_USER_ONLY)
  54. /* Allow the guest to have a max 3ms advance.
  55. * The difference between the 2 clocks could therefore
  56. * oscillate around 0.
  57. */
  58. #define VM_CLOCK_ADVANCE 3000000
  59. #define THRESHOLD_REDUCE 1.5
  60. #define MAX_DELAY_PRINT_RATE 2000000000LL
  61. #define MAX_NB_PRINTS 100
  62. int64_t max_delay;
  63. int64_t max_advance;
  64. static void align_clocks(SyncClocks *sc, CPUState *cpu)
  65. {
  66. int64_t cpu_icount;
  67. if (!icount_align_option) {
  68. return;
  69. }
  70. cpu_icount = cpu->icount_extra + cpu->neg.icount_decr.u16.low;
  71. sc->diff_clk += icount_to_ns(sc->last_cpu_icount - cpu_icount);
  72. sc->last_cpu_icount = cpu_icount;
  73. if (sc->diff_clk > VM_CLOCK_ADVANCE) {
  74. #ifndef _WIN32
  75. struct timespec sleep_delay, rem_delay;
  76. sleep_delay.tv_sec = sc->diff_clk / 1000000000LL;
  77. sleep_delay.tv_nsec = sc->diff_clk % 1000000000LL;
  78. if (nanosleep(&sleep_delay, &rem_delay) < 0) {
  79. sc->diff_clk = rem_delay.tv_sec * 1000000000LL + rem_delay.tv_nsec;
  80. } else {
  81. sc->diff_clk = 0;
  82. }
  83. #else
  84. Sleep(sc->diff_clk / SCALE_MS);
  85. sc->diff_clk = 0;
  86. #endif
  87. }
  88. }
  89. static void print_delay(const SyncClocks *sc)
  90. {
  91. static float threshold_delay;
  92. static int64_t last_realtime_clock;
  93. static int nb_prints;
  94. if (icount_align_option &&
  95. sc->realtime_clock - last_realtime_clock >= MAX_DELAY_PRINT_RATE &&
  96. nb_prints < MAX_NB_PRINTS) {
  97. if ((-sc->diff_clk / (float)1000000000LL > threshold_delay) ||
  98. (-sc->diff_clk / (float)1000000000LL <
  99. (threshold_delay - THRESHOLD_REDUCE))) {
  100. threshold_delay = (-sc->diff_clk / 1000000000LL) + 1;
  101. qemu_printf("Warning: The guest is now late by %.1f to %.1f seconds\n",
  102. threshold_delay - 1,
  103. threshold_delay);
  104. nb_prints++;
  105. last_realtime_clock = sc->realtime_clock;
  106. }
  107. }
  108. }
  109. static void init_delay_params(SyncClocks *sc, CPUState *cpu)
  110. {
  111. if (!icount_align_option) {
  112. return;
  113. }
  114. sc->realtime_clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
  115. sc->diff_clk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - sc->realtime_clock;
  116. sc->last_cpu_icount
  117. = cpu->icount_extra + cpu->neg.icount_decr.u16.low;
  118. if (sc->diff_clk < max_delay) {
  119. max_delay = sc->diff_clk;
  120. }
  121. if (sc->diff_clk > max_advance) {
  122. max_advance = sc->diff_clk;
  123. }
  124. /* Print every 2s max if the guest is late. We limit the number
  125. of printed messages to NB_PRINT_MAX(currently 100) */
  126. print_delay(sc);
  127. }
  128. #else
  129. static void align_clocks(SyncClocks *sc, const CPUState *cpu)
  130. {
  131. }
  132. static void init_delay_params(SyncClocks *sc, const CPUState *cpu)
  133. {
  134. }
  135. #endif /* CONFIG USER ONLY */
  136. struct tb_desc {
  137. vaddr pc;
  138. uint64_t cs_base;
  139. CPUArchState *env;
  140. tb_page_addr_t page_addr0;
  141. uint32_t flags;
  142. uint32_t cflags;
  143. };
  144. static bool tb_lookup_cmp(const void *p, const void *d)
  145. {
  146. const TranslationBlock *tb = p;
  147. const struct tb_desc *desc = d;
  148. if ((tb_cflags(tb) & CF_PCREL || tb->pc == desc->pc) &&
  149. tb_page_addr0(tb) == desc->page_addr0 &&
  150. tb->cs_base == desc->cs_base &&
  151. tb->flags == desc->flags &&
  152. tb_cflags(tb) == desc->cflags) {
  153. /* check next page if needed */
  154. tb_page_addr_t tb_phys_page1 = tb_page_addr1(tb);
  155. if (tb_phys_page1 == -1) {
  156. return true;
  157. } else {
  158. tb_page_addr_t phys_page1;
  159. vaddr virt_page1;
  160. /*
  161. * We know that the first page matched, and an otherwise valid TB
  162. * encountered an incomplete instruction at the end of that page,
  163. * therefore we know that generating a new TB from the current PC
  164. * must also require reading from the next page -- even if the
  165. * second pages do not match, and therefore the resulting insn
  166. * is different for the new TB. Therefore any exception raised
  167. * here by the faulting lookup is not premature.
  168. */
  169. virt_page1 = TARGET_PAGE_ALIGN(desc->pc);
  170. phys_page1 = get_page_addr_code(desc->env, virt_page1);
  171. if (tb_phys_page1 == phys_page1) {
  172. return true;
  173. }
  174. }
  175. }
  176. return false;
  177. }
  178. static TranslationBlock *tb_htable_lookup(CPUState *cpu, vaddr pc,
  179. uint64_t cs_base, uint32_t flags,
  180. uint32_t cflags)
  181. {
  182. tb_page_addr_t phys_pc;
  183. struct tb_desc desc;
  184. uint32_t h;
  185. desc.env = cpu_env(cpu);
  186. desc.cs_base = cs_base;
  187. desc.flags = flags;
  188. desc.cflags = cflags;
  189. desc.pc = pc;
  190. phys_pc = get_page_addr_code(desc.env, pc);
  191. if (phys_pc == -1) {
  192. return NULL;
  193. }
  194. desc.page_addr0 = phys_pc;
  195. h = tb_hash_func(phys_pc, (cflags & CF_PCREL ? 0 : pc),
  196. flags, cs_base, cflags);
  197. return qht_lookup_custom(&tb_ctx.htable, &desc, h, tb_lookup_cmp);
  198. }
  199. /**
  200. * tb_lookup:
  201. * @cpu: CPU that will execute the returned translation block
  202. * @pc: guest PC
  203. * @cs_base: arch-specific value associated with translation block
  204. * @flags: arch-specific translation block flags
  205. * @cflags: CF_* flags
  206. *
  207. * Look up a translation block inside the QHT using @pc, @cs_base, @flags and
  208. * @cflags. Uses @cpu's tb_jmp_cache. Might cause an exception, so have a
  209. * longjmp destination ready.
  210. *
  211. * Returns: an existing translation block or NULL.
  212. */
  213. static inline TranslationBlock *tb_lookup(CPUState *cpu, vaddr pc,
  214. uint64_t cs_base, uint32_t flags,
  215. uint32_t cflags)
  216. {
  217. TranslationBlock *tb;
  218. CPUJumpCache *jc;
  219. uint32_t hash;
  220. /* we should never be trying to look up an INVALID tb */
  221. tcg_debug_assert(!(cflags & CF_INVALID));
  222. hash = tb_jmp_cache_hash_func(pc);
  223. jc = cpu->tb_jmp_cache;
  224. tb = qatomic_read(&jc->array[hash].tb);
  225. if (likely(tb &&
  226. jc->array[hash].pc == pc &&
  227. tb->cs_base == cs_base &&
  228. tb->flags == flags &&
  229. tb_cflags(tb) == cflags)) {
  230. goto hit;
  231. }
  232. tb = tb_htable_lookup(cpu, pc, cs_base, flags, cflags);
  233. if (tb == NULL) {
  234. return NULL;
  235. }
  236. jc->array[hash].pc = pc;
  237. qatomic_set(&jc->array[hash].tb, tb);
  238. hit:
  239. /*
  240. * As long as tb is not NULL, the contents are consistent. Therefore,
  241. * the virtual PC has to match for non-CF_PCREL translations.
  242. */
  243. assert((tb_cflags(tb) & CF_PCREL) || tb->pc == pc);
  244. return tb;
  245. }
  246. static void log_cpu_exec(vaddr pc, CPUState *cpu,
  247. const TranslationBlock *tb)
  248. {
  249. if (qemu_log_in_addr_range(pc)) {
  250. qemu_log_mask(CPU_LOG_EXEC,
  251. "Trace %d: %p [%08" PRIx64
  252. "/%016" VADDR_PRIx "/%08x/%08x] %s\n",
  253. cpu->cpu_index, tb->tc.ptr, tb->cs_base, pc,
  254. tb->flags, tb->cflags, lookup_symbol(pc));
  255. if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
  256. FILE *logfile = qemu_log_trylock();
  257. if (logfile) {
  258. int flags = 0;
  259. if (qemu_loglevel_mask(CPU_LOG_TB_FPU)) {
  260. flags |= CPU_DUMP_FPU;
  261. }
  262. #if defined(TARGET_I386)
  263. flags |= CPU_DUMP_CCOP;
  264. #endif
  265. if (qemu_loglevel_mask(CPU_LOG_TB_VPU)) {
  266. flags |= CPU_DUMP_VPU;
  267. }
  268. cpu_dump_state(cpu, logfile, flags);
  269. qemu_log_unlock(logfile);
  270. }
  271. }
  272. }
  273. }
  274. static bool check_for_breakpoints_slow(CPUState *cpu, vaddr pc,
  275. uint32_t *cflags)
  276. {
  277. CPUBreakpoint *bp;
  278. bool match_page = false;
  279. /*
  280. * Singlestep overrides breakpoints.
  281. * This requirement is visible in the record-replay tests, where
  282. * we would fail to make forward progress in reverse-continue.
  283. *
  284. * TODO: gdb singlestep should only override gdb breakpoints,
  285. * so that one could (gdb) singlestep into the guest kernel's
  286. * architectural breakpoint handler.
  287. */
  288. if (cpu->singlestep_enabled) {
  289. return false;
  290. }
  291. QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
  292. /*
  293. * If we have an exact pc match, trigger the breakpoint.
  294. * Otherwise, note matches within the page.
  295. */
  296. if (pc == bp->pc) {
  297. bool match_bp = false;
  298. if (bp->flags & BP_GDB) {
  299. match_bp = true;
  300. } else if (bp->flags & BP_CPU) {
  301. #ifdef CONFIG_USER_ONLY
  302. g_assert_not_reached();
  303. #else
  304. const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
  305. assert(tcg_ops->debug_check_breakpoint);
  306. match_bp = tcg_ops->debug_check_breakpoint(cpu);
  307. #endif
  308. }
  309. if (match_bp) {
  310. cpu->exception_index = EXCP_DEBUG;
  311. return true;
  312. }
  313. } else if (((pc ^ bp->pc) & TARGET_PAGE_MASK) == 0) {
  314. match_page = true;
  315. }
  316. }
  317. /*
  318. * Within the same page as a breakpoint, single-step,
  319. * returning to helper_lookup_tb_ptr after each insn looking
  320. * for the actual breakpoint.
  321. *
  322. * TODO: Perhaps better to record all of the TBs associated
  323. * with a given virtual page that contains a breakpoint, and
  324. * then invalidate them when a new overlapping breakpoint is
  325. * set on the page. Non-overlapping TBs would not be
  326. * invalidated, nor would any TB need to be invalidated as
  327. * breakpoints are removed.
  328. */
  329. if (match_page) {
  330. *cflags = (*cflags & ~CF_COUNT_MASK) | CF_NO_GOTO_TB | CF_BP_PAGE | 1;
  331. }
  332. return false;
  333. }
  334. static inline bool check_for_breakpoints(CPUState *cpu, vaddr pc,
  335. uint32_t *cflags)
  336. {
  337. return unlikely(!QTAILQ_EMPTY(&cpu->breakpoints)) &&
  338. check_for_breakpoints_slow(cpu, pc, cflags);
  339. }
  340. /**
  341. * helper_lookup_tb_ptr: quick check for next tb
  342. * @env: current cpu state
  343. *
  344. * Look for an existing TB matching the current cpu state.
  345. * If found, return the code pointer. If not found, return
  346. * the tcg epilogue so that we return into cpu_tb_exec.
  347. */
  348. const void *HELPER(lookup_tb_ptr)(CPUArchState *env)
  349. {
  350. CPUState *cpu = env_cpu(env);
  351. TranslationBlock *tb;
  352. vaddr pc;
  353. uint64_t cs_base;
  354. uint32_t flags, cflags;
  355. /*
  356. * By definition we've just finished a TB, so I/O is OK.
  357. * Avoid the possibility of calling cpu_io_recompile() if
  358. * a page table walk triggered by tb_lookup() calling
  359. * probe_access_internal() happens to touch an MMIO device.
  360. * The next TB, if we chain to it, will clear the flag again.
  361. */
  362. cpu->neg.can_do_io = true;
  363. cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
  364. cflags = curr_cflags(cpu);
  365. if (check_for_breakpoints(cpu, pc, &cflags)) {
  366. cpu_loop_exit(cpu);
  367. }
  368. tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
  369. if (tb == NULL) {
  370. return tcg_code_gen_epilogue;
  371. }
  372. if (qemu_loglevel_mask(CPU_LOG_TB_CPU | CPU_LOG_EXEC)) {
  373. log_cpu_exec(pc, cpu, tb);
  374. }
  375. return tb->tc.ptr;
  376. }
  377. /* Return the current PC from CPU, which may be cached in TB. */
  378. static vaddr log_pc(CPUState *cpu, const TranslationBlock *tb)
  379. {
  380. if (tb_cflags(tb) & CF_PCREL) {
  381. return cpu->cc->get_pc(cpu);
  382. } else {
  383. return tb->pc;
  384. }
  385. }
  386. /* Execute a TB, and fix up the CPU state afterwards if necessary */
  387. /*
  388. * Disable CFI checks.
  389. * TCG creates binary blobs at runtime, with the transformed code.
  390. * A TB is a blob of binary code, created at runtime and called with an
  391. * indirect function call. Since such function did not exist at compile time,
  392. * the CFI runtime has no way to verify its signature and would fail.
  393. * TCG is not considered a security-sensitive part of QEMU so this does not
  394. * affect the impact of CFI in environment with high security requirements
  395. */
  396. static inline TranslationBlock * QEMU_DISABLE_CFI
  397. cpu_tb_exec(CPUState *cpu, TranslationBlock *itb, int *tb_exit)
  398. {
  399. uintptr_t ret;
  400. TranslationBlock *last_tb;
  401. const void *tb_ptr = itb->tc.ptr;
  402. if (qemu_loglevel_mask(CPU_LOG_TB_CPU | CPU_LOG_EXEC)) {
  403. log_cpu_exec(log_pc(cpu, itb), cpu, itb);
  404. }
  405. qemu_thread_jit_execute();
  406. ret = tcg_qemu_tb_exec(cpu_env(cpu), tb_ptr);
  407. cpu->neg.can_do_io = true;
  408. qemu_plugin_disable_mem_helpers(cpu);
  409. /*
  410. * TODO: Delay swapping back to the read-write region of the TB
  411. * until we actually need to modify the TB. The read-only copy,
  412. * coming from the rx region, shares the same host TLB entry as
  413. * the code that executed the exit_tb opcode that arrived here.
  414. * If we insist on touching both the RX and the RW pages, we
  415. * double the host TLB pressure.
  416. */
  417. last_tb = tcg_splitwx_to_rw((void *)(ret & ~TB_EXIT_MASK));
  418. *tb_exit = ret & TB_EXIT_MASK;
  419. trace_exec_tb_exit(last_tb, *tb_exit);
  420. if (*tb_exit > TB_EXIT_IDX1) {
  421. /* We didn't start executing this TB (eg because the instruction
  422. * counter hit zero); we must restore the guest PC to the address
  423. * of the start of the TB.
  424. */
  425. CPUClass *cc = cpu->cc;
  426. const TCGCPUOps *tcg_ops = cc->tcg_ops;
  427. if (tcg_ops->synchronize_from_tb) {
  428. tcg_ops->synchronize_from_tb(cpu, last_tb);
  429. } else {
  430. tcg_debug_assert(!(tb_cflags(last_tb) & CF_PCREL));
  431. assert(cc->set_pc);
  432. cc->set_pc(cpu, last_tb->pc);
  433. }
  434. if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
  435. vaddr pc = log_pc(cpu, last_tb);
  436. if (qemu_log_in_addr_range(pc)) {
  437. qemu_log("Stopped execution of TB chain before %p [%016"
  438. VADDR_PRIx "] %s\n",
  439. last_tb->tc.ptr, pc, lookup_symbol(pc));
  440. }
  441. }
  442. }
  443. /*
  444. * If gdb single-step, and we haven't raised another exception,
  445. * raise a debug exception. Single-step with another exception
  446. * is handled in cpu_handle_exception.
  447. */
  448. if (unlikely(cpu->singlestep_enabled) && cpu->exception_index == -1) {
  449. cpu->exception_index = EXCP_DEBUG;
  450. cpu_loop_exit(cpu);
  451. }
  452. return last_tb;
  453. }
  454. static void cpu_exec_enter(CPUState *cpu)
  455. {
  456. const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
  457. if (tcg_ops->cpu_exec_enter) {
  458. tcg_ops->cpu_exec_enter(cpu);
  459. }
  460. }
  461. static void cpu_exec_exit(CPUState *cpu)
  462. {
  463. const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
  464. if (tcg_ops->cpu_exec_exit) {
  465. tcg_ops->cpu_exec_exit(cpu);
  466. }
  467. }
  468. static void cpu_exec_longjmp_cleanup(CPUState *cpu)
  469. {
  470. /* Non-buggy compilers preserve this; assert the correct value. */
  471. g_assert(cpu == current_cpu);
  472. #ifdef CONFIG_USER_ONLY
  473. clear_helper_retaddr();
  474. if (have_mmap_lock()) {
  475. mmap_unlock();
  476. }
  477. #else
  478. /*
  479. * For softmmu, a tlb_fill fault during translation will land here,
  480. * and we need to release any page locks held. In system mode we
  481. * have one tcg_ctx per thread, so we know it was this cpu doing
  482. * the translation.
  483. *
  484. * Alternative 1: Install a cleanup to be called via an exception
  485. * handling safe longjmp. It seems plausible that all our hosts
  486. * support such a thing. We'd have to properly register unwind info
  487. * for the JIT for EH, rather that just for GDB.
  488. *
  489. * Alternative 2: Set and restore cpu->jmp_env in tb_gen_code to
  490. * capture the cpu_loop_exit longjmp, perform the cleanup, and
  491. * jump again to arrive here.
  492. */
  493. if (tcg_ctx->gen_tb) {
  494. tb_unlock_pages(tcg_ctx->gen_tb);
  495. tcg_ctx->gen_tb = NULL;
  496. }
  497. #endif
  498. if (bql_locked()) {
  499. bql_unlock();
  500. }
  501. assert_no_pages_locked();
  502. }
  503. void cpu_exec_step_atomic(CPUState *cpu)
  504. {
  505. CPUArchState *env = cpu_env(cpu);
  506. TranslationBlock *tb;
  507. vaddr pc;
  508. uint64_t cs_base;
  509. uint32_t flags, cflags;
  510. int tb_exit;
  511. if (sigsetjmp(cpu->jmp_env, 0) == 0) {
  512. start_exclusive();
  513. g_assert(cpu == current_cpu);
  514. g_assert(!cpu->running);
  515. cpu->running = true;
  516. cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
  517. cflags = curr_cflags(cpu);
  518. /* Execute in a serial context. */
  519. cflags &= ~CF_PARALLEL;
  520. /* After 1 insn, return and release the exclusive lock. */
  521. cflags |= CF_NO_GOTO_TB | CF_NO_GOTO_PTR | 1;
  522. /*
  523. * No need to check_for_breakpoints here.
  524. * We only arrive in cpu_exec_step_atomic after beginning execution
  525. * of an insn that includes an atomic operation we can't handle.
  526. * Any breakpoint for this insn will have been recognized earlier.
  527. */
  528. tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
  529. if (tb == NULL) {
  530. mmap_lock();
  531. tb = tb_gen_code(cpu, pc, cs_base, flags, cflags);
  532. mmap_unlock();
  533. }
  534. cpu_exec_enter(cpu);
  535. /* execute the generated code */
  536. trace_exec_tb(tb, pc);
  537. cpu_tb_exec(cpu, tb, &tb_exit);
  538. cpu_exec_exit(cpu);
  539. } else {
  540. cpu_exec_longjmp_cleanup(cpu);
  541. }
  542. /*
  543. * As we start the exclusive region before codegen we must still
  544. * be in the region if we longjump out of either the codegen or
  545. * the execution.
  546. */
  547. g_assert(cpu_in_exclusive_context(cpu));
  548. cpu->running = false;
  549. end_exclusive();
  550. }
  551. void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr)
  552. {
  553. /*
  554. * Get the rx view of the structure, from which we find the
  555. * executable code address, and tb_target_set_jmp_target can
  556. * produce a pc-relative displacement to jmp_target_addr[n].
  557. */
  558. const TranslationBlock *c_tb = tcg_splitwx_to_rx(tb);
  559. uintptr_t offset = tb->jmp_insn_offset[n];
  560. uintptr_t jmp_rx = (uintptr_t)tb->tc.ptr + offset;
  561. uintptr_t jmp_rw = jmp_rx - tcg_splitwx_diff;
  562. tb->jmp_target_addr[n] = addr;
  563. tb_target_set_jmp_target(c_tb, n, jmp_rx, jmp_rw);
  564. }
  565. static inline void tb_add_jump(TranslationBlock *tb, int n,
  566. TranslationBlock *tb_next)
  567. {
  568. uintptr_t old;
  569. qemu_thread_jit_write();
  570. assert(n < ARRAY_SIZE(tb->jmp_list_next));
  571. qemu_spin_lock(&tb_next->jmp_lock);
  572. /* make sure the destination TB is valid */
  573. if (tb_next->cflags & CF_INVALID) {
  574. goto out_unlock_next;
  575. }
  576. /* Atomically claim the jump destination slot only if it was NULL */
  577. old = qatomic_cmpxchg(&tb->jmp_dest[n], (uintptr_t)NULL,
  578. (uintptr_t)tb_next);
  579. if (old) {
  580. goto out_unlock_next;
  581. }
  582. /* patch the native jump address */
  583. tb_set_jmp_target(tb, n, (uintptr_t)tb_next->tc.ptr);
  584. /* add in TB jmp list */
  585. tb->jmp_list_next[n] = tb_next->jmp_list_head;
  586. tb_next->jmp_list_head = (uintptr_t)tb | n;
  587. qemu_spin_unlock(&tb_next->jmp_lock);
  588. qemu_log_mask(CPU_LOG_EXEC, "Linking TBs %p index %d -> %p\n",
  589. tb->tc.ptr, n, tb_next->tc.ptr);
  590. return;
  591. out_unlock_next:
  592. qemu_spin_unlock(&tb_next->jmp_lock);
  593. return;
  594. }
  595. static inline bool cpu_handle_halt(CPUState *cpu)
  596. {
  597. #ifndef CONFIG_USER_ONLY
  598. if (cpu->halted) {
  599. const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
  600. bool leave_halt = tcg_ops->cpu_exec_halt(cpu);
  601. if (!leave_halt) {
  602. return true;
  603. }
  604. cpu->halted = 0;
  605. }
  606. #endif /* !CONFIG_USER_ONLY */
  607. return false;
  608. }
  609. static inline void cpu_handle_debug_exception(CPUState *cpu)
  610. {
  611. const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
  612. CPUWatchpoint *wp;
  613. if (!cpu->watchpoint_hit) {
  614. QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
  615. wp->flags &= ~BP_WATCHPOINT_HIT;
  616. }
  617. }
  618. if (tcg_ops->debug_excp_handler) {
  619. tcg_ops->debug_excp_handler(cpu);
  620. }
  621. }
  622. static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
  623. {
  624. if (cpu->exception_index < 0) {
  625. #ifndef CONFIG_USER_ONLY
  626. if (replay_has_exception()
  627. && cpu->neg.icount_decr.u16.low + cpu->icount_extra == 0) {
  628. /* Execute just one insn to trigger exception pending in the log */
  629. cpu->cflags_next_tb = (curr_cflags(cpu) & ~CF_USE_ICOUNT)
  630. | CF_NOIRQ | 1;
  631. }
  632. #endif
  633. return false;
  634. }
  635. if (cpu->exception_index >= EXCP_INTERRUPT) {
  636. /* exit request from the cpu execution loop */
  637. *ret = cpu->exception_index;
  638. if (*ret == EXCP_DEBUG) {
  639. cpu_handle_debug_exception(cpu);
  640. }
  641. cpu->exception_index = -1;
  642. return true;
  643. }
  644. #if defined(CONFIG_USER_ONLY)
  645. /*
  646. * If user mode only, we simulate a fake exception which will be
  647. * handled outside the cpu execution loop.
  648. */
  649. #if defined(TARGET_I386)
  650. const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
  651. tcg_ops->fake_user_interrupt(cpu);
  652. #endif /* TARGET_I386 */
  653. *ret = cpu->exception_index;
  654. cpu->exception_index = -1;
  655. return true;
  656. #else
  657. if (replay_exception()) {
  658. const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
  659. bql_lock();
  660. tcg_ops->do_interrupt(cpu);
  661. bql_unlock();
  662. cpu->exception_index = -1;
  663. if (unlikely(cpu->singlestep_enabled)) {
  664. /*
  665. * After processing the exception, ensure an EXCP_DEBUG is
  666. * raised when single-stepping so that GDB doesn't miss the
  667. * next instruction.
  668. */
  669. *ret = EXCP_DEBUG;
  670. cpu_handle_debug_exception(cpu);
  671. return true;
  672. }
  673. } else if (!replay_has_interrupt()) {
  674. /* give a chance to iothread in replay mode */
  675. *ret = EXCP_INTERRUPT;
  676. return true;
  677. }
  678. #endif
  679. return false;
  680. }
  681. static inline bool icount_exit_request(CPUState *cpu)
  682. {
  683. if (!icount_enabled()) {
  684. return false;
  685. }
  686. if (cpu->cflags_next_tb != -1 && !(cpu->cflags_next_tb & CF_USE_ICOUNT)) {
  687. return false;
  688. }
  689. return cpu->neg.icount_decr.u16.low + cpu->icount_extra == 0;
  690. }
  691. static inline bool cpu_handle_interrupt(CPUState *cpu,
  692. TranslationBlock **last_tb)
  693. {
  694. /*
  695. * If we have requested custom cflags with CF_NOIRQ we should
  696. * skip checking here. Any pending interrupts will get picked up
  697. * by the next TB we execute under normal cflags.
  698. */
  699. if (cpu->cflags_next_tb != -1 && cpu->cflags_next_tb & CF_NOIRQ) {
  700. return false;
  701. }
  702. /* Clear the interrupt flag now since we're processing
  703. * cpu->interrupt_request and cpu->exit_request.
  704. * Ensure zeroing happens before reading cpu->exit_request or
  705. * cpu->interrupt_request (see also smp_wmb in cpu_exit())
  706. */
  707. qatomic_set_mb(&cpu->neg.icount_decr.u16.high, 0);
  708. if (unlikely(qatomic_read(&cpu->interrupt_request))) {
  709. int interrupt_request;
  710. bql_lock();
  711. interrupt_request = cpu->interrupt_request;
  712. if (unlikely(cpu->singlestep_enabled & SSTEP_NOIRQ)) {
  713. /* Mask out external interrupts for this step. */
  714. interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK;
  715. }
  716. if (interrupt_request & CPU_INTERRUPT_DEBUG) {
  717. cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
  718. cpu->exception_index = EXCP_DEBUG;
  719. bql_unlock();
  720. return true;
  721. }
  722. #if !defined(CONFIG_USER_ONLY)
  723. if (replay_mode == REPLAY_MODE_PLAY && !replay_has_interrupt()) {
  724. /* Do nothing */
  725. } else if (interrupt_request & CPU_INTERRUPT_HALT) {
  726. replay_interrupt();
  727. cpu->interrupt_request &= ~CPU_INTERRUPT_HALT;
  728. cpu->halted = 1;
  729. cpu->exception_index = EXCP_HLT;
  730. bql_unlock();
  731. return true;
  732. }
  733. #if defined(TARGET_I386)
  734. else if (interrupt_request & CPU_INTERRUPT_INIT) {
  735. X86CPU *x86_cpu = X86_CPU(cpu);
  736. CPUArchState *env = &x86_cpu->env;
  737. replay_interrupt();
  738. cpu_svm_check_intercept_param(env, SVM_EXIT_INIT, 0, 0);
  739. do_cpu_init(x86_cpu);
  740. cpu->exception_index = EXCP_HALTED;
  741. bql_unlock();
  742. return true;
  743. }
  744. #else
  745. else if (interrupt_request & CPU_INTERRUPT_RESET) {
  746. replay_interrupt();
  747. cpu_reset(cpu);
  748. bql_unlock();
  749. return true;
  750. }
  751. #endif /* !TARGET_I386 */
  752. /* The target hook has 3 exit conditions:
  753. False when the interrupt isn't processed,
  754. True when it is, and we should restart on a new TB,
  755. and via longjmp via cpu_loop_exit. */
  756. else {
  757. const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
  758. if (tcg_ops->cpu_exec_interrupt(cpu, interrupt_request)) {
  759. if (!tcg_ops->need_replay_interrupt ||
  760. tcg_ops->need_replay_interrupt(interrupt_request)) {
  761. replay_interrupt();
  762. }
  763. /*
  764. * After processing the interrupt, ensure an EXCP_DEBUG is
  765. * raised when single-stepping so that GDB doesn't miss the
  766. * next instruction.
  767. */
  768. if (unlikely(cpu->singlestep_enabled)) {
  769. cpu->exception_index = EXCP_DEBUG;
  770. bql_unlock();
  771. return true;
  772. }
  773. cpu->exception_index = -1;
  774. *last_tb = NULL;
  775. }
  776. /* The target hook may have updated the 'cpu->interrupt_request';
  777. * reload the 'interrupt_request' value */
  778. interrupt_request = cpu->interrupt_request;
  779. }
  780. #endif /* !CONFIG_USER_ONLY */
  781. if (interrupt_request & CPU_INTERRUPT_EXITTB) {
  782. cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
  783. /* ensure that no TB jump will be modified as
  784. the program flow was changed */
  785. *last_tb = NULL;
  786. }
  787. /* If we exit via cpu_loop_exit/longjmp it is reset in cpu_exec */
  788. bql_unlock();
  789. }
  790. /* Finally, check if we need to exit to the main loop. */
  791. if (unlikely(qatomic_read(&cpu->exit_request)) || icount_exit_request(cpu)) {
  792. qatomic_set(&cpu->exit_request, 0);
  793. if (cpu->exception_index == -1) {
  794. cpu->exception_index = EXCP_INTERRUPT;
  795. }
  796. return true;
  797. }
  798. return false;
  799. }
  800. static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,
  801. vaddr pc, TranslationBlock **last_tb,
  802. int *tb_exit)
  803. {
  804. trace_exec_tb(tb, pc);
  805. tb = cpu_tb_exec(cpu, tb, tb_exit);
  806. if (*tb_exit != TB_EXIT_REQUESTED) {
  807. *last_tb = tb;
  808. return;
  809. }
  810. *last_tb = NULL;
  811. if (cpu_loop_exit_requested(cpu)) {
  812. /* Something asked us to stop executing chained TBs; just
  813. * continue round the main loop. Whatever requested the exit
  814. * will also have set something else (eg exit_request or
  815. * interrupt_request) which will be handled by
  816. * cpu_handle_interrupt. cpu_handle_interrupt will also
  817. * clear cpu->icount_decr.u16.high.
  818. */
  819. return;
  820. }
  821. /* Instruction counter expired. */
  822. assert(icount_enabled());
  823. #ifndef CONFIG_USER_ONLY
  824. /* Ensure global icount has gone forward */
  825. icount_update(cpu);
  826. /* Refill decrementer and continue execution. */
  827. int32_t insns_left = MIN(0xffff, cpu->icount_budget);
  828. cpu->neg.icount_decr.u16.low = insns_left;
  829. cpu->icount_extra = cpu->icount_budget - insns_left;
  830. /*
  831. * If the next tb has more instructions than we have left to
  832. * execute we need to ensure we find/generate a TB with exactly
  833. * insns_left instructions in it.
  834. */
  835. if (insns_left > 0 && insns_left < tb->icount) {
  836. assert(insns_left <= CF_COUNT_MASK);
  837. assert(cpu->icount_extra == 0);
  838. cpu->cflags_next_tb = (tb->cflags & ~CF_COUNT_MASK) | insns_left;
  839. }
  840. #endif
  841. }
  842. /* main execution loop */
  843. static int __attribute__((noinline))
  844. cpu_exec_loop(CPUState *cpu, SyncClocks *sc)
  845. {
  846. int ret;
  847. /* if an exception is pending, we execute it here */
  848. while (!cpu_handle_exception(cpu, &ret)) {
  849. TranslationBlock *last_tb = NULL;
  850. int tb_exit = 0;
  851. while (!cpu_handle_interrupt(cpu, &last_tb)) {
  852. TranslationBlock *tb;
  853. vaddr pc;
  854. uint64_t cs_base;
  855. uint32_t flags, cflags;
  856. cpu_get_tb_cpu_state(cpu_env(cpu), &pc, &cs_base, &flags);
  857. /*
  858. * When requested, use an exact setting for cflags for the next
  859. * execution. This is used for icount, precise smc, and stop-
  860. * after-access watchpoints. Since this request should never
  861. * have CF_INVALID set, -1 is a convenient invalid value that
  862. * does not require tcg headers for cpu_common_reset.
  863. */
  864. cflags = cpu->cflags_next_tb;
  865. if (cflags == -1) {
  866. cflags = curr_cflags(cpu);
  867. } else {
  868. cpu->cflags_next_tb = -1;
  869. }
  870. if (check_for_breakpoints(cpu, pc, &cflags)) {
  871. break;
  872. }
  873. tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
  874. if (tb == NULL) {
  875. CPUJumpCache *jc;
  876. uint32_t h;
  877. mmap_lock();
  878. tb = tb_gen_code(cpu, pc, cs_base, flags, cflags);
  879. mmap_unlock();
  880. /*
  881. * We add the TB in the virtual pc hash table
  882. * for the fast lookup
  883. */
  884. h = tb_jmp_cache_hash_func(pc);
  885. jc = cpu->tb_jmp_cache;
  886. jc->array[h].pc = pc;
  887. qatomic_set(&jc->array[h].tb, tb);
  888. }
  889. #ifndef CONFIG_USER_ONLY
  890. /*
  891. * We don't take care of direct jumps when address mapping
  892. * changes in system emulation. So it's not safe to make a
  893. * direct jump to a TB spanning two pages because the mapping
  894. * for the second page can change.
  895. */
  896. if (tb_page_addr1(tb) != -1) {
  897. last_tb = NULL;
  898. }
  899. #endif
  900. /* See if we can patch the calling TB. */
  901. if (last_tb) {
  902. tb_add_jump(last_tb, tb_exit, tb);
  903. }
  904. cpu_loop_exec_tb(cpu, tb, pc, &last_tb, &tb_exit);
  905. /* Try to align the host and virtual clocks
  906. if the guest is in advance */
  907. align_clocks(sc, cpu);
  908. }
  909. }
  910. return ret;
  911. }
  912. static int cpu_exec_setjmp(CPUState *cpu, SyncClocks *sc)
  913. {
  914. /* Prepare setjmp context for exception handling. */
  915. if (unlikely(sigsetjmp(cpu->jmp_env, 0) != 0)) {
  916. cpu_exec_longjmp_cleanup(cpu);
  917. }
  918. return cpu_exec_loop(cpu, sc);
  919. }
  920. int cpu_exec(CPUState *cpu)
  921. {
  922. int ret;
  923. SyncClocks sc = { 0 };
  924. /* replay_interrupt may need current_cpu */
  925. current_cpu = cpu;
  926. if (cpu_handle_halt(cpu)) {
  927. return EXCP_HALTED;
  928. }
  929. RCU_READ_LOCK_GUARD();
  930. cpu_exec_enter(cpu);
  931. /*
  932. * Calculate difference between guest clock and host clock.
  933. * This delay includes the delay of the last cycle, so
  934. * what we have to do is sleep until it is 0. As for the
  935. * advance/delay we gain here, we try to fix it next time.
  936. */
  937. init_delay_params(&sc, cpu);
  938. ret = cpu_exec_setjmp(cpu, &sc);
  939. cpu_exec_exit(cpu);
  940. return ret;
  941. }
  942. bool tcg_exec_realizefn(CPUState *cpu, Error **errp)
  943. {
  944. static bool tcg_target_initialized;
  945. if (!tcg_target_initialized) {
  946. /* Check mandatory TCGCPUOps handlers */
  947. const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
  948. #ifndef CONFIG_USER_ONLY
  949. assert(tcg_ops->cpu_exec_halt);
  950. assert(tcg_ops->cpu_exec_interrupt);
  951. #endif /* !CONFIG_USER_ONLY */
  952. assert(tcg_ops->translate_code);
  953. tcg_ops->initialize();
  954. tcg_target_initialized = true;
  955. }
  956. cpu->tb_jmp_cache = g_new0(CPUJumpCache, 1);
  957. tlb_init(cpu);
  958. #ifndef CONFIG_USER_ONLY
  959. tcg_iommu_init_notifier_list(cpu);
  960. #endif /* !CONFIG_USER_ONLY */
  961. /* qemu_plugin_vcpu_init_hook delayed until cpu_index assigned. */
  962. return true;
  963. }
  964. /* undo the initializations in reverse order */
  965. void tcg_exec_unrealizefn(CPUState *cpu)
  966. {
  967. #ifndef CONFIG_USER_ONLY
  968. tcg_iommu_free_notifier_list(cpu);
  969. #endif /* !CONFIG_USER_ONLY */
  970. tlb_destroy(cpu);
  971. g_free_rcu(cpu->tb_jmp_cache, rcu);
  972. }