1 /* 2 * Generic intermediate code generation. 3 * 4 * Copyright (C) 2016-2017 Lluís Vilanova <vilanova@ac.upc.edu> 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or later. 7 * See the COPYING file in the top-level directory. 8 */ 9 10 #include "qemu/osdep.h" 11 #include "qemu/error-report.h" 12 #include "tcg/tcg.h" 13 #include "tcg/tcg-op.h" 14 #include "exec/exec-all.h" 15 #include "exec/gen-icount.h" 16 #include "exec/log.h" 17 #include "exec/translator.h" 18 #include "exec/plugin-gen.h" 19 #include "sysemu/replay.h" 20 21 /* Pairs with tcg_clear_temp_count. 22 To be called by #TranslatorOps.{translate_insn,tb_stop} if 23 (1) the target is sufficiently clean to support reporting, 24 (2) as and when all temporaries are known to be consumed. 25 For most targets, (2) is at the end of translate_insn. */ 26 void translator_loop_temp_check(DisasContextBase *db) 27 { 28 if (tcg_check_temp_count()) { 29 qemu_log("warning: TCG temporary leaks before " 30 TARGET_FMT_lx "\n", db->pc_next); 31 } 32 } 33 34 bool translator_use_goto_tb(DisasContextBase *db, target_ulong dest) 35 { 36 /* Suppress goto_tb if requested. */ 37 if (tb_cflags(db->tb) & CF_NO_GOTO_TB) { 38 return false; 39 } 40 41 /* Suppress goto_tb in the case of single-steping. */ 42 if (db->singlestep_enabled) { 43 return false; 44 } 45 46 /* Check for the dest on the same page as the start of the TB. */ 47 return ((db->pc_first ^ dest) & TARGET_PAGE_MASK) == 0; 48 } 49 50 void translator_loop(const TranslatorOps *ops, DisasContextBase *db, 51 CPUState *cpu, TranslationBlock *tb, int max_insns) 52 { 53 int bp_insn = 0; 54 bool plugin_enabled; 55 56 /* Initialize DisasContext */ 57 db->tb = tb; 58 db->pc_first = tb->pc; 59 db->pc_next = db->pc_first; 60 db->is_jmp = DISAS_NEXT; 61 db->num_insns = 0; 62 db->max_insns = max_insns; 63 db->singlestep_enabled = cpu->singlestep_enabled; 64 65 ops->init_disas_context(db, cpu); 66 tcg_debug_assert(db->is_jmp == DISAS_NEXT); /* no early exit */ 67 68 /* Reset the temp count so that we can identify leaks */ 69 tcg_clear_temp_count(); 70 71 /* Start translating. */ 72 gen_tb_start(db->tb); 73 ops->tb_start(db, cpu); 74 tcg_debug_assert(db->is_jmp == DISAS_NEXT); /* no early exit */ 75 76 plugin_enabled = plugin_gen_tb_start(cpu, tb, 77 tb_cflags(db->tb) & CF_MEMI_ONLY); 78 79 while (true) { 80 db->num_insns++; 81 ops->insn_start(db, cpu); 82 tcg_debug_assert(db->is_jmp == DISAS_NEXT); /* no early exit */ 83 84 if (plugin_enabled) { 85 plugin_gen_insn_start(cpu, db); 86 } 87 88 /* Pass breakpoint hits to target for further processing */ 89 if (!db->singlestep_enabled 90 && unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) { 91 CPUBreakpoint *bp; 92 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) { 93 if (bp->pc == db->pc_next) { 94 if (ops->breakpoint_check(db, cpu, bp)) { 95 bp_insn = 1; 96 break; 97 } 98 } 99 } 100 /* The breakpoint_check hook may use DISAS_TOO_MANY to indicate 101 that only one more instruction is to be executed. Otherwise 102 it should use DISAS_NORETURN when generating an exception, 103 but may use a DISAS_TARGET_* value for Something Else. */ 104 if (db->is_jmp > DISAS_TOO_MANY) { 105 break; 106 } 107 } 108 109 /* Disassemble one instruction. The translate_insn hook should 110 update db->pc_next and db->is_jmp to indicate what should be 111 done next -- either exiting this loop or locate the start of 112 the next instruction. */ 113 if (db->num_insns == db->max_insns 114 && (tb_cflags(db->tb) & CF_LAST_IO)) { 115 /* Accept I/O on the last instruction. */ 116 gen_io_start(); 117 ops->translate_insn(db, cpu); 118 } else { 119 /* we should only see CF_MEMI_ONLY for io_recompile */ 120 tcg_debug_assert(!(tb_cflags(db->tb) & CF_MEMI_ONLY)); 121 ops->translate_insn(db, cpu); 122 } 123 124 /* Stop translation if translate_insn so indicated. */ 125 if (db->is_jmp != DISAS_NEXT) { 126 break; 127 } 128 129 /* 130 * We can't instrument after instructions that change control 131 * flow although this only really affects post-load operations. 132 */ 133 if (plugin_enabled) { 134 plugin_gen_insn_end(); 135 } 136 137 /* Stop translation if the output buffer is full, 138 or we have executed all of the allowed instructions. */ 139 if (tcg_op_buf_full() || db->num_insns >= db->max_insns) { 140 db->is_jmp = DISAS_TOO_MANY; 141 break; 142 } 143 } 144 145 /* Emit code to exit the TB, as indicated by db->is_jmp. */ 146 ops->tb_stop(db, cpu); 147 gen_tb_end(db->tb, db->num_insns - bp_insn); 148 149 if (plugin_enabled) { 150 plugin_gen_tb_end(cpu); 151 } 152 153 /* The disas_log hook may use these values rather than recompute. */ 154 tb->size = db->pc_next - db->pc_first; 155 tb->icount = db->num_insns; 156 157 #ifdef DEBUG_DISAS 158 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) 159 && qemu_log_in_addr_range(db->pc_first)) { 160 FILE *logfile = qemu_log_lock(); 161 qemu_log("----------------\n"); 162 ops->disas_log(db, cpu); 163 qemu_log("\n"); 164 qemu_log_unlock(logfile); 165 } 166 #endif 167 } 168