cpu-exec.c 34 KB

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