translate-all.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * Host code generation
  3. *
  4. * Copyright (c) 2003 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 "trace.h"
  21. #include "disas/disas.h"
  22. #include "exec/exec-all.h"
  23. #include "tcg/tcg.h"
  24. #if defined(CONFIG_USER_ONLY)
  25. #include "qemu.h"
  26. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  27. #include <sys/param.h>
  28. #if __FreeBSD_version >= 700104
  29. #define HAVE_KINFO_GETVMMAP
  30. #define sigqueue sigqueue_freebsd /* avoid redefinition */
  31. #include <sys/proc.h>
  32. #include <machine/profile.h>
  33. #define _KERNEL
  34. #include <sys/user.h>
  35. #undef _KERNEL
  36. #undef sigqueue
  37. #include <libutil.h>
  38. #endif
  39. #endif
  40. #else
  41. #include "exec/ram_addr.h"
  42. #endif
  43. #include "exec/cputlb.h"
  44. #include "exec/translate-all.h"
  45. #include "exec/translator.h"
  46. #include "exec/tb-flush.h"
  47. #include "qemu/bitmap.h"
  48. #include "qemu/qemu-print.h"
  49. #include "qemu/main-loop.h"
  50. #include "qemu/cacheinfo.h"
  51. #include "qemu/timer.h"
  52. #include "exec/log.h"
  53. #include "system/cpus.h"
  54. #include "system/cpu-timers.h"
  55. #include "system/tcg.h"
  56. #include "qapi/error.h"
  57. #include "hw/core/tcg-cpu-ops.h"
  58. #include "tb-jmp-cache.h"
  59. #include "tb-hash.h"
  60. #include "tb-context.h"
  61. #include "internal-common.h"
  62. #include "internal-target.h"
  63. #include "tcg/perf.h"
  64. #include "tcg/insn-start-words.h"
  65. TBContext tb_ctx;
  66. /*
  67. * Encode VAL as a signed leb128 sequence at P.
  68. * Return P incremented past the encoded value.
  69. */
  70. static uint8_t *encode_sleb128(uint8_t *p, int64_t val)
  71. {
  72. int more, byte;
  73. do {
  74. byte = val & 0x7f;
  75. val >>= 7;
  76. more = !((val == 0 && (byte & 0x40) == 0)
  77. || (val == -1 && (byte & 0x40) != 0));
  78. if (more) {
  79. byte |= 0x80;
  80. }
  81. *p++ = byte;
  82. } while (more);
  83. return p;
  84. }
  85. /*
  86. * Decode a signed leb128 sequence at *PP; increment *PP past the
  87. * decoded value. Return the decoded value.
  88. */
  89. static int64_t decode_sleb128(const uint8_t **pp)
  90. {
  91. const uint8_t *p = *pp;
  92. int64_t val = 0;
  93. int byte, shift = 0;
  94. do {
  95. byte = *p++;
  96. val |= (int64_t)(byte & 0x7f) << shift;
  97. shift += 7;
  98. } while (byte & 0x80);
  99. if (shift < TARGET_LONG_BITS && (byte & 0x40)) {
  100. val |= -(int64_t)1 << shift;
  101. }
  102. *pp = p;
  103. return val;
  104. }
  105. /* Encode the data collected about the instructions while compiling TB.
  106. Place the data at BLOCK, and return the number of bytes consumed.
  107. The logical table consists of TARGET_INSN_START_WORDS target_ulong's,
  108. which come from the target's insn_start data, followed by a uintptr_t
  109. which comes from the host pc of the end of the code implementing the insn.
  110. Each line of the table is encoded as sleb128 deltas from the previous
  111. line. The seed for the first line is { tb->pc, 0..., tb->tc.ptr }.
  112. That is, the first column is seeded with the guest pc, the last column
  113. with the host pc, and the middle columns with zeros. */
  114. static int encode_search(TranslationBlock *tb, uint8_t *block)
  115. {
  116. uint8_t *highwater = tcg_ctx->code_gen_highwater;
  117. uint64_t *insn_data = tcg_ctx->gen_insn_data;
  118. uint16_t *insn_end_off = tcg_ctx->gen_insn_end_off;
  119. uint8_t *p = block;
  120. int i, j, n;
  121. for (i = 0, n = tb->icount; i < n; ++i) {
  122. uint64_t prev, curr;
  123. for (j = 0; j < TARGET_INSN_START_WORDS; ++j) {
  124. if (i == 0) {
  125. prev = (!(tb_cflags(tb) & CF_PCREL) && j == 0 ? tb->pc : 0);
  126. } else {
  127. prev = insn_data[(i - 1) * TARGET_INSN_START_WORDS + j];
  128. }
  129. curr = insn_data[i * TARGET_INSN_START_WORDS + j];
  130. p = encode_sleb128(p, curr - prev);
  131. }
  132. prev = (i == 0 ? 0 : insn_end_off[i - 1]);
  133. curr = insn_end_off[i];
  134. p = encode_sleb128(p, curr - prev);
  135. /* Test for (pending) buffer overflow. The assumption is that any
  136. one row beginning below the high water mark cannot overrun
  137. the buffer completely. Thus we can test for overflow after
  138. encoding a row without having to check during encoding. */
  139. if (unlikely(p > highwater)) {
  140. return -1;
  141. }
  142. }
  143. return p - block;
  144. }
  145. static int cpu_unwind_data_from_tb(TranslationBlock *tb, uintptr_t host_pc,
  146. uint64_t *data)
  147. {
  148. uintptr_t iter_pc = (uintptr_t)tb->tc.ptr;
  149. const uint8_t *p = tb->tc.ptr + tb->tc.size;
  150. int i, j, num_insns = tb->icount;
  151. host_pc -= GETPC_ADJ;
  152. if (host_pc < iter_pc) {
  153. return -1;
  154. }
  155. memset(data, 0, sizeof(uint64_t) * TARGET_INSN_START_WORDS);
  156. if (!(tb_cflags(tb) & CF_PCREL)) {
  157. data[0] = tb->pc;
  158. }
  159. /*
  160. * Reconstruct the stored insn data while looking for the point
  161. * at which the end of the insn exceeds host_pc.
  162. */
  163. for (i = 0; i < num_insns; ++i) {
  164. for (j = 0; j < TARGET_INSN_START_WORDS; ++j) {
  165. data[j] += decode_sleb128(&p);
  166. }
  167. iter_pc += decode_sleb128(&p);
  168. if (iter_pc > host_pc) {
  169. return num_insns - i;
  170. }
  171. }
  172. return -1;
  173. }
  174. /*
  175. * The cpu state corresponding to 'host_pc' is restored in
  176. * preparation for exiting the TB.
  177. */
  178. void cpu_restore_state_from_tb(CPUState *cpu, TranslationBlock *tb,
  179. uintptr_t host_pc)
  180. {
  181. uint64_t data[TARGET_INSN_START_WORDS];
  182. int insns_left = cpu_unwind_data_from_tb(tb, host_pc, data);
  183. if (insns_left < 0) {
  184. return;
  185. }
  186. if (tb_cflags(tb) & CF_USE_ICOUNT) {
  187. assert(icount_enabled());
  188. /*
  189. * Reset the cycle counter to the start of the block and
  190. * shift if to the number of actually executed instructions.
  191. */
  192. cpu->neg.icount_decr.u16.low += insns_left;
  193. }
  194. cpu->cc->tcg_ops->restore_state_to_opc(cpu, tb, data);
  195. }
  196. bool cpu_restore_state(CPUState *cpu, uintptr_t host_pc)
  197. {
  198. /*
  199. * The host_pc has to be in the rx region of the code buffer.
  200. * If it is not we will not be able to resolve it here.
  201. * The two cases where host_pc will not be correct are:
  202. *
  203. * - fault during translation (instruction fetch)
  204. * - fault from helper (not using GETPC() macro)
  205. *
  206. * Either way we need return early as we can't resolve it here.
  207. */
  208. if (in_code_gen_buffer((const void *)(host_pc - tcg_splitwx_diff))) {
  209. TranslationBlock *tb = tcg_tb_lookup(host_pc);
  210. if (tb) {
  211. cpu_restore_state_from_tb(cpu, tb, host_pc);
  212. return true;
  213. }
  214. }
  215. return false;
  216. }
  217. bool cpu_unwind_state_data(CPUState *cpu, uintptr_t host_pc, uint64_t *data)
  218. {
  219. if (in_code_gen_buffer((const void *)(host_pc - tcg_splitwx_diff))) {
  220. TranslationBlock *tb = tcg_tb_lookup(host_pc);
  221. if (tb) {
  222. return cpu_unwind_data_from_tb(tb, host_pc, data) >= 0;
  223. }
  224. }
  225. return false;
  226. }
  227. void page_init(void)
  228. {
  229. page_table_config_init();
  230. }
  231. /*
  232. * Isolate the portion of code gen which can setjmp/longjmp.
  233. * Return the size of the generated code, or negative on error.
  234. */
  235. static int setjmp_gen_code(CPUArchState *env, TranslationBlock *tb,
  236. vaddr pc, void *host_pc,
  237. int *max_insns, int64_t *ti)
  238. {
  239. int ret = sigsetjmp(tcg_ctx->jmp_trans, 0);
  240. if (unlikely(ret != 0)) {
  241. return ret;
  242. }
  243. tcg_func_start(tcg_ctx);
  244. tcg_ctx->cpu = env_cpu(env);
  245. gen_intermediate_code(env_cpu(env), tb, max_insns, pc, host_pc);
  246. assert(tb->size != 0);
  247. tcg_ctx->cpu = NULL;
  248. *max_insns = tb->icount;
  249. return tcg_gen_code(tcg_ctx, tb, pc);
  250. }
  251. /* Called with mmap_lock held for user mode emulation. */
  252. TranslationBlock *tb_gen_code(CPUState *cpu,
  253. vaddr pc, uint64_t cs_base,
  254. uint32_t flags, int cflags)
  255. {
  256. CPUArchState *env = cpu_env(cpu);
  257. TranslationBlock *tb, *existing_tb;
  258. tb_page_addr_t phys_pc, phys_p2;
  259. tcg_insn_unit *gen_code_buf;
  260. int gen_code_size, search_size, max_insns;
  261. int64_t ti;
  262. void *host_pc;
  263. assert_memory_lock();
  264. qemu_thread_jit_write();
  265. phys_pc = get_page_addr_code_hostp(env, pc, &host_pc);
  266. if (phys_pc == -1) {
  267. /* Generate a one-shot TB with 1 insn in it */
  268. cflags = (cflags & ~CF_COUNT_MASK) | 1;
  269. }
  270. max_insns = cflags & CF_COUNT_MASK;
  271. if (max_insns == 0) {
  272. max_insns = TCG_MAX_INSNS;
  273. }
  274. QEMU_BUILD_BUG_ON(CF_COUNT_MASK + 1 != TCG_MAX_INSNS);
  275. buffer_overflow:
  276. assert_no_pages_locked();
  277. tb = tcg_tb_alloc(tcg_ctx);
  278. if (unlikely(!tb)) {
  279. /* flush must be done */
  280. tb_flush(cpu);
  281. mmap_unlock();
  282. /* Make the execution loop process the flush as soon as possible. */
  283. cpu->exception_index = EXCP_INTERRUPT;
  284. cpu_loop_exit(cpu);
  285. }
  286. gen_code_buf = tcg_ctx->code_gen_ptr;
  287. tb->tc.ptr = tcg_splitwx_to_rx(gen_code_buf);
  288. if (!(cflags & CF_PCREL)) {
  289. tb->pc = pc;
  290. }
  291. tb->cs_base = cs_base;
  292. tb->flags = flags;
  293. tb->cflags = cflags;
  294. tb_set_page_addr0(tb, phys_pc);
  295. tb_set_page_addr1(tb, -1);
  296. if (phys_pc != -1) {
  297. tb_lock_page0(phys_pc);
  298. }
  299. tcg_ctx->gen_tb = tb;
  300. tcg_ctx->addr_type = TARGET_LONG_BITS == 32 ? TCG_TYPE_I32 : TCG_TYPE_I64;
  301. #ifdef CONFIG_SOFTMMU
  302. tcg_ctx->page_bits = TARGET_PAGE_BITS;
  303. tcg_ctx->page_mask = TARGET_PAGE_MASK;
  304. tcg_ctx->tlb_dyn_max_bits = CPU_TLB_DYN_MAX_BITS;
  305. #endif
  306. tcg_ctx->insn_start_words = TARGET_INSN_START_WORDS;
  307. #ifdef TCG_GUEST_DEFAULT_MO
  308. tcg_ctx->guest_mo = TCG_GUEST_DEFAULT_MO;
  309. #else
  310. tcg_ctx->guest_mo = TCG_MO_ALL;
  311. #endif
  312. restart_translate:
  313. trace_translate_block(tb, pc, tb->tc.ptr);
  314. gen_code_size = setjmp_gen_code(env, tb, pc, host_pc, &max_insns, &ti);
  315. if (unlikely(gen_code_size < 0)) {
  316. switch (gen_code_size) {
  317. case -1:
  318. /*
  319. * Overflow of code_gen_buffer, or the current slice of it.
  320. *
  321. * TODO: We don't need to re-do gen_intermediate_code, nor
  322. * should we re-do the tcg optimization currently hidden
  323. * inside tcg_gen_code. All that should be required is to
  324. * flush the TBs, allocate a new TB, re-initialize it per
  325. * above, and re-do the actual code generation.
  326. */
  327. qemu_log_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT,
  328. "Restarting code generation for "
  329. "code_gen_buffer overflow\n");
  330. tb_unlock_pages(tb);
  331. tcg_ctx->gen_tb = NULL;
  332. goto buffer_overflow;
  333. case -2:
  334. /*
  335. * The code generated for the TranslationBlock is too large.
  336. * The maximum size allowed by the unwind info is 64k.
  337. * There may be stricter constraints from relocations
  338. * in the tcg backend.
  339. *
  340. * Try again with half as many insns as we attempted this time.
  341. * If a single insn overflows, there's a bug somewhere...
  342. */
  343. assert(max_insns > 1);
  344. max_insns /= 2;
  345. qemu_log_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT,
  346. "Restarting code generation with "
  347. "smaller translation block (max %d insns)\n",
  348. max_insns);
  349. /*
  350. * The half-sized TB may not cross pages.
  351. * TODO: Fix all targets that cross pages except with
  352. * the first insn, at which point this can't be reached.
  353. */
  354. phys_p2 = tb_page_addr1(tb);
  355. if (unlikely(phys_p2 != -1)) {
  356. tb_unlock_page1(phys_pc, phys_p2);
  357. tb_set_page_addr1(tb, -1);
  358. }
  359. goto restart_translate;
  360. case -3:
  361. /*
  362. * We had a page lock ordering problem. In order to avoid
  363. * deadlock we had to drop the lock on page0, which means
  364. * that everything we translated so far is compromised.
  365. * Restart with locks held on both pages.
  366. */
  367. qemu_log_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT,
  368. "Restarting code generation with re-locked pages");
  369. goto restart_translate;
  370. default:
  371. g_assert_not_reached();
  372. }
  373. }
  374. tcg_ctx->gen_tb = NULL;
  375. search_size = encode_search(tb, (void *)gen_code_buf + gen_code_size);
  376. if (unlikely(search_size < 0)) {
  377. tb_unlock_pages(tb);
  378. goto buffer_overflow;
  379. }
  380. tb->tc.size = gen_code_size;
  381. /*
  382. * For CF_PCREL, attribute all executions of the generated code
  383. * to its first mapping.
  384. */
  385. perf_report_code(pc, tb, tcg_splitwx_to_rx(gen_code_buf));
  386. if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM) &&
  387. qemu_log_in_addr_range(pc)) {
  388. FILE *logfile = qemu_log_trylock();
  389. if (logfile) {
  390. int code_size, data_size;
  391. const tcg_target_ulong *rx_data_gen_ptr;
  392. size_t chunk_start;
  393. int insn = 0;
  394. if (tcg_ctx->data_gen_ptr) {
  395. rx_data_gen_ptr = tcg_splitwx_to_rx(tcg_ctx->data_gen_ptr);
  396. code_size = (const void *)rx_data_gen_ptr - tb->tc.ptr;
  397. data_size = gen_code_size - code_size;
  398. } else {
  399. rx_data_gen_ptr = 0;
  400. code_size = gen_code_size;
  401. data_size = 0;
  402. }
  403. /* Dump header and the first instruction */
  404. fprintf(logfile, "OUT: [size=%d]\n", gen_code_size);
  405. fprintf(logfile,
  406. " -- guest addr 0x%016" PRIx64 " + tb prologue\n",
  407. tcg_ctx->gen_insn_data[insn * TARGET_INSN_START_WORDS]);
  408. chunk_start = tcg_ctx->gen_insn_end_off[insn];
  409. disas(logfile, tb->tc.ptr, chunk_start);
  410. /*
  411. * Dump each instruction chunk, wrapping up empty chunks into
  412. * the next instruction. The whole array is offset so the
  413. * first entry is the beginning of the 2nd instruction.
  414. */
  415. while (insn < tb->icount) {
  416. size_t chunk_end = tcg_ctx->gen_insn_end_off[insn];
  417. if (chunk_end > chunk_start) {
  418. fprintf(logfile, " -- guest addr 0x%016" PRIx64 "\n",
  419. tcg_ctx->gen_insn_data[insn * TARGET_INSN_START_WORDS]);
  420. disas(logfile, tb->tc.ptr + chunk_start,
  421. chunk_end - chunk_start);
  422. chunk_start = chunk_end;
  423. }
  424. insn++;
  425. }
  426. if (chunk_start < code_size) {
  427. fprintf(logfile, " -- tb slow paths + alignment\n");
  428. disas(logfile, tb->tc.ptr + chunk_start,
  429. code_size - chunk_start);
  430. }
  431. /* Finally dump any data we may have after the block */
  432. if (data_size) {
  433. int i;
  434. fprintf(logfile, " data: [size=%d]\n", data_size);
  435. for (i = 0; i < data_size / sizeof(tcg_target_ulong); i++) {
  436. if (sizeof(tcg_target_ulong) == 8) {
  437. fprintf(logfile,
  438. "0x%08" PRIxPTR ": .quad 0x%016" TCG_PRIlx "\n",
  439. (uintptr_t)&rx_data_gen_ptr[i], rx_data_gen_ptr[i]);
  440. } else if (sizeof(tcg_target_ulong) == 4) {
  441. fprintf(logfile,
  442. "0x%08" PRIxPTR ": .long 0x%08" TCG_PRIlx "\n",
  443. (uintptr_t)&rx_data_gen_ptr[i], rx_data_gen_ptr[i]);
  444. } else {
  445. qemu_build_not_reached();
  446. }
  447. }
  448. }
  449. fprintf(logfile, "\n");
  450. qemu_log_unlock(logfile);
  451. }
  452. }
  453. qatomic_set(&tcg_ctx->code_gen_ptr, (void *)
  454. ROUND_UP((uintptr_t)gen_code_buf + gen_code_size + search_size,
  455. CODE_GEN_ALIGN));
  456. /* init jump list */
  457. qemu_spin_init(&tb->jmp_lock);
  458. tb->jmp_list_head = (uintptr_t)NULL;
  459. tb->jmp_list_next[0] = (uintptr_t)NULL;
  460. tb->jmp_list_next[1] = (uintptr_t)NULL;
  461. tb->jmp_dest[0] = (uintptr_t)NULL;
  462. tb->jmp_dest[1] = (uintptr_t)NULL;
  463. /* init original jump addresses which have been set during tcg_gen_code() */
  464. if (tb->jmp_reset_offset[0] != TB_JMP_OFFSET_INVALID) {
  465. tb_reset_jump(tb, 0);
  466. }
  467. if (tb->jmp_reset_offset[1] != TB_JMP_OFFSET_INVALID) {
  468. tb_reset_jump(tb, 1);
  469. }
  470. /*
  471. * If the TB is not associated with a physical RAM page then it must be
  472. * a temporary one-insn TB, and we have nothing left to do. Return early
  473. * before attempting to link to other TBs or add to the lookup table.
  474. */
  475. if (tb_page_addr0(tb) == -1) {
  476. assert_no_pages_locked();
  477. return tb;
  478. }
  479. /*
  480. * Insert TB into the corresponding region tree before publishing it
  481. * through QHT. Otherwise rewinding happened in the TB might fail to
  482. * lookup itself using host PC.
  483. */
  484. tcg_tb_insert(tb);
  485. /*
  486. * No explicit memory barrier is required -- tb_link_page() makes the
  487. * TB visible in a consistent state.
  488. */
  489. existing_tb = tb_link_page(tb);
  490. assert_no_pages_locked();
  491. /* if the TB already exists, discard what we just translated */
  492. if (unlikely(existing_tb != tb)) {
  493. uintptr_t orig_aligned = (uintptr_t)gen_code_buf;
  494. orig_aligned -= ROUND_UP(sizeof(*tb), qemu_icache_linesize);
  495. qatomic_set(&tcg_ctx->code_gen_ptr, (void *)orig_aligned);
  496. tcg_tb_remove(tb);
  497. return existing_tb;
  498. }
  499. return tb;
  500. }
  501. /* user-mode: call with mmap_lock held */
  502. void tb_check_watchpoint(CPUState *cpu, uintptr_t retaddr)
  503. {
  504. TranslationBlock *tb;
  505. assert_memory_lock();
  506. tb = tcg_tb_lookup(retaddr);
  507. if (tb) {
  508. /* We can use retranslation to find the PC. */
  509. cpu_restore_state_from_tb(cpu, tb, retaddr);
  510. tb_phys_invalidate(tb, -1);
  511. } else {
  512. /* The exception probably happened in a helper. The CPU state should
  513. have been saved before calling it. Fetch the PC from there. */
  514. CPUArchState *env = cpu_env(cpu);
  515. vaddr pc;
  516. uint64_t cs_base;
  517. tb_page_addr_t addr;
  518. uint32_t flags;
  519. cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
  520. addr = get_page_addr_code(env, pc);
  521. if (addr != -1) {
  522. tb_invalidate_phys_range(addr, addr);
  523. }
  524. }
  525. }
  526. #ifndef CONFIG_USER_ONLY
  527. /*
  528. * In deterministic execution mode, instructions doing device I/Os
  529. * must be at the end of the TB.
  530. *
  531. * Called by softmmu_template.h, with iothread mutex not held.
  532. */
  533. void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr)
  534. {
  535. TranslationBlock *tb;
  536. CPUClass *cc;
  537. uint32_t n;
  538. tb = tcg_tb_lookup(retaddr);
  539. if (!tb) {
  540. cpu_abort(cpu, "cpu_io_recompile: could not find TB for pc=%p",
  541. (void *)retaddr);
  542. }
  543. cpu_restore_state_from_tb(cpu, tb, retaddr);
  544. /*
  545. * Some guests must re-execute the branch when re-executing a delay
  546. * slot instruction. When this is the case, adjust icount and N
  547. * to account for the re-execution of the branch.
  548. */
  549. n = 1;
  550. cc = CPU_GET_CLASS(cpu);
  551. if (cc->tcg_ops->io_recompile_replay_branch &&
  552. cc->tcg_ops->io_recompile_replay_branch(cpu, tb)) {
  553. cpu->neg.icount_decr.u16.low++;
  554. n = 2;
  555. }
  556. /*
  557. * Exit the loop and potentially generate a new TB executing the
  558. * just the I/O insns. We also limit instrumentation to memory
  559. * operations only (which execute after completion) so we don't
  560. * double instrument the instruction.
  561. */
  562. cpu->cflags_next_tb = curr_cflags(cpu) | CF_MEMI_ONLY | n;
  563. if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
  564. vaddr pc = cpu->cc->get_pc(cpu);
  565. if (qemu_log_in_addr_range(pc)) {
  566. qemu_log("cpu_io_recompile: rewound execution of TB to %016"
  567. VADDR_PRIx "\n", pc);
  568. }
  569. }
  570. cpu_loop_exit_noexc(cpu);
  571. }
  572. #endif /* CONFIG_USER_ONLY */
  573. /*
  574. * Called by generic code at e.g. cpu reset after cpu creation,
  575. * therefore we must be prepared to allocate the jump cache.
  576. */
  577. void tcg_flush_jmp_cache(CPUState *cpu)
  578. {
  579. CPUJumpCache *jc = cpu->tb_jmp_cache;
  580. /* During early initialization, the cache may not yet be allocated. */
  581. if (unlikely(jc == NULL)) {
  582. return;
  583. }
  584. for (int i = 0; i < TB_JMP_CACHE_SIZE; i++) {
  585. qatomic_set(&jc->array[i].tb, NULL);
  586. }
  587. }