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 "exec/replay-core.h" 20 21 bool translator_use_goto_tb(DisasContextBase *db, target_ulong dest) 22 { 23 /* Suppress goto_tb if requested. */ 24 if (tb_cflags(db->tb) & CF_NO_GOTO_TB) { 25 return false; 26 } 27 28 /* Check for the dest on the same page as the start of the TB. */ 29 return ((db->pc_first ^ dest) & TARGET_PAGE_MASK) == 0; 30 } 31 32 void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns, 33 target_ulong pc, void *host_pc, 34 const TranslatorOps *ops, DisasContextBase *db) 35 { 36 uint32_t cflags = tb_cflags(tb); 37 bool plugin_enabled; 38 39 /* Initialize DisasContext */ 40 db->tb = tb; 41 db->pc_first = pc; 42 db->pc_next = pc; 43 db->is_jmp = DISAS_NEXT; 44 db->num_insns = 0; 45 db->max_insns = *max_insns; 46 db->singlestep_enabled = cflags & CF_SINGLE_STEP; 47 db->host_addr[0] = host_pc; 48 db->host_addr[1] = NULL; 49 50 #ifdef CONFIG_USER_ONLY 51 page_protect(pc); 52 #endif 53 54 ops->init_disas_context(db, cpu); 55 tcg_debug_assert(db->is_jmp == DISAS_NEXT); /* no early exit */ 56 57 /* Start translating. */ 58 gen_tb_start(db->tb); 59 ops->tb_start(db, cpu); 60 tcg_debug_assert(db->is_jmp == DISAS_NEXT); /* no early exit */ 61 62 plugin_enabled = plugin_gen_tb_start(cpu, db, cflags & CF_MEMI_ONLY); 63 64 while (true) { 65 *max_insns = ++db->num_insns; 66 ops->insn_start(db, cpu); 67 tcg_debug_assert(db->is_jmp == DISAS_NEXT); /* no early exit */ 68 69 if (plugin_enabled) { 70 plugin_gen_insn_start(cpu, db); 71 } 72 73 /* Disassemble one instruction. The translate_insn hook should 74 update db->pc_next and db->is_jmp to indicate what should be 75 done next -- either exiting this loop or locate the start of 76 the next instruction. */ 77 if (db->num_insns == db->max_insns && (cflags & CF_LAST_IO)) { 78 /* Accept I/O on the last instruction. */ 79 gen_io_start(); 80 ops->translate_insn(db, cpu); 81 } else { 82 /* we should only see CF_MEMI_ONLY for io_recompile */ 83 tcg_debug_assert(!(cflags & CF_MEMI_ONLY)); 84 ops->translate_insn(db, cpu); 85 } 86 87 /* 88 * We can't instrument after instructions that change control 89 * flow although this only really affects post-load operations. 90 * 91 * Calling plugin_gen_insn_end() before we possibly stop translation 92 * is important. Even if this ends up as dead code, plugin generation 93 * needs to see a matching plugin_gen_insn_{start,end}() pair in order 94 * to accurately track instrumented helpers that might access memory. 95 */ 96 if (plugin_enabled) { 97 plugin_gen_insn_end(); 98 } 99 100 /* Stop translation if translate_insn so indicated. */ 101 if (db->is_jmp != DISAS_NEXT) { 102 break; 103 } 104 105 /* Stop translation if the output buffer is full, 106 or we have executed all of the allowed instructions. */ 107 if (tcg_op_buf_full() || db->num_insns >= db->max_insns) { 108 db->is_jmp = DISAS_TOO_MANY; 109 break; 110 } 111 } 112 113 /* Emit code to exit the TB, as indicated by db->is_jmp. */ 114 ops->tb_stop(db, cpu); 115 gen_tb_end(db->tb, db->num_insns); 116 117 if (plugin_enabled) { 118 plugin_gen_tb_end(cpu); 119 } 120 121 /* The disas_log hook may use these values rather than recompute. */ 122 tb->size = db->pc_next - db->pc_first; 123 tb->icount = db->num_insns; 124 125 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) 126 && qemu_log_in_addr_range(db->pc_first)) { 127 FILE *logfile = qemu_log_trylock(); 128 if (logfile) { 129 fprintf(logfile, "----------------\n"); 130 ops->disas_log(db, cpu, logfile); 131 fprintf(logfile, "\n"); 132 qemu_log_unlock(logfile); 133 } 134 } 135 } 136 137 static void *translator_access(CPUArchState *env, DisasContextBase *db, 138 target_ulong pc, size_t len) 139 { 140 void *host; 141 target_ulong base, end; 142 TranslationBlock *tb; 143 144 tb = db->tb; 145 146 /* Use slow path if first page is MMIO. */ 147 if (unlikely(tb_page_addr0(tb) == -1)) { 148 return NULL; 149 } 150 151 end = pc + len - 1; 152 if (likely(is_same_page(db, end))) { 153 host = db->host_addr[0]; 154 base = db->pc_first; 155 } else { 156 host = db->host_addr[1]; 157 base = TARGET_PAGE_ALIGN(db->pc_first); 158 if (host == NULL) { 159 tb_page_addr_t phys_page = 160 get_page_addr_code_hostp(env, base, &db->host_addr[1]); 161 162 /* 163 * If the second page is MMIO, treat as if the first page 164 * was MMIO as well, so that we do not cache the TB. 165 */ 166 if (unlikely(phys_page == -1)) { 167 tb_set_page_addr0(tb, -1); 168 return NULL; 169 } 170 171 tb_set_page_addr1(tb, phys_page); 172 #ifdef CONFIG_USER_ONLY 173 page_protect(end); 174 #endif 175 host = db->host_addr[1]; 176 } 177 178 /* Use slow path when crossing pages. */ 179 if (is_same_page(db, pc)) { 180 return NULL; 181 } 182 } 183 184 tcg_debug_assert(pc >= base); 185 return host + (pc - base); 186 } 187 188 uint8_t translator_ldub(CPUArchState *env, DisasContextBase *db, abi_ptr pc) 189 { 190 uint8_t ret; 191 void *p = translator_access(env, db, pc, sizeof(ret)); 192 193 if (p) { 194 plugin_insn_append(pc, p, sizeof(ret)); 195 return ldub_p(p); 196 } 197 ret = cpu_ldub_code(env, pc); 198 plugin_insn_append(pc, &ret, sizeof(ret)); 199 return ret; 200 } 201 202 uint16_t translator_lduw(CPUArchState *env, DisasContextBase *db, abi_ptr pc) 203 { 204 uint16_t ret, plug; 205 void *p = translator_access(env, db, pc, sizeof(ret)); 206 207 if (p) { 208 plugin_insn_append(pc, p, sizeof(ret)); 209 return lduw_p(p); 210 } 211 ret = cpu_lduw_code(env, pc); 212 plug = tswap16(ret); 213 plugin_insn_append(pc, &plug, sizeof(ret)); 214 return ret; 215 } 216 217 uint32_t translator_ldl(CPUArchState *env, DisasContextBase *db, abi_ptr pc) 218 { 219 uint32_t ret, plug; 220 void *p = translator_access(env, db, pc, sizeof(ret)); 221 222 if (p) { 223 plugin_insn_append(pc, p, sizeof(ret)); 224 return ldl_p(p); 225 } 226 ret = cpu_ldl_code(env, pc); 227 plug = tswap32(ret); 228 plugin_insn_append(pc, &plug, sizeof(ret)); 229 return ret; 230 } 231 232 uint64_t translator_ldq(CPUArchState *env, DisasContextBase *db, abi_ptr pc) 233 { 234 uint64_t ret, plug; 235 void *p = translator_access(env, db, pc, sizeof(ret)); 236 237 if (p) { 238 plugin_insn_append(pc, p, sizeof(ret)); 239 return ldq_p(p); 240 } 241 ret = cpu_ldq_code(env, pc); 242 plug = tswap64(ret); 243 plugin_insn_append(pc, &plug, sizeof(ret)); 244 return ret; 245 } 246