1bb2e0039SLluís Vilanova /* 2bb2e0039SLluís Vilanova * Generic intermediate code generation. 3bb2e0039SLluís Vilanova * 4bb2e0039SLluís Vilanova * Copyright (C) 2016-2017 Lluís Vilanova <vilanova@ac.upc.edu> 5bb2e0039SLluís Vilanova * 6bb2e0039SLluís Vilanova * This work is licensed under the terms of the GNU GPL, version 2 or later. 7bb2e0039SLluís Vilanova * See the COPYING file in the top-level directory. 8bb2e0039SLluís Vilanova */ 9bb2e0039SLluís Vilanova 10bb2e0039SLluís Vilanova #include "qemu/osdep.h" 11653c46daSRichard Henderson #include "qemu/log.h" 12bb2e0039SLluís Vilanova #include "qemu/error-report.h" 13bb2e0039SLluís Vilanova #include "exec/exec-all.h" 14bb2e0039SLluís Vilanova #include "exec/translator.h" 156ba6f818SEmilio G. Cota #include "exec/plugin-gen.h" 16653c46daSRichard Henderson #include "tcg/tcg-op-common.h" 174c268d6dSPhilippe Mathieu-Daudé #include "internal-target.h" 1856234233SRichard Henderson 190ca41ccfSRichard Henderson static void set_can_do_io(DisasContextBase *db, bool val) 2056234233SRichard Henderson { 210ca41ccfSRichard Henderson if (db->saved_can_do_io != val) { 220ca41ccfSRichard Henderson db->saved_can_do_io = val; 23464dacf6SRichard Henderson 24464dacf6SRichard Henderson QEMU_BUILD_BUG_ON(sizeof_field(CPUState, neg.can_do_io) != 1); 25ad75a51eSRichard Henderson tcg_gen_st8_i32(tcg_constant_i32(val), tcg_env, 26464dacf6SRichard Henderson offsetof(ArchCPU, parent_obj.neg.can_do_io) - 2756234233SRichard Henderson offsetof(ArchCPU, env)); 2856234233SRichard Henderson } 290ca41ccfSRichard Henderson } 3056234233SRichard Henderson 31dfd1b812SRichard Henderson bool translator_io_start(DisasContextBase *db) 32dfd1b812SRichard Henderson { 330ca41ccfSRichard Henderson set_can_do_io(db, true); 34dfd1b812SRichard Henderson 35dfd1b812SRichard Henderson /* 36dfd1b812SRichard Henderson * Ensure that this instruction will be the last in the TB. 37dfd1b812SRichard Henderson * The target may override this to something more forceful. 38dfd1b812SRichard Henderson */ 39dfd1b812SRichard Henderson if (db->is_jmp == DISAS_NEXT) { 40dfd1b812SRichard Henderson db->is_jmp = DISAS_TOO_MANY; 41dfd1b812SRichard Henderson } 42dfd1b812SRichard Henderson return true; 43dfd1b812SRichard Henderson } 44dfd1b812SRichard Henderson 450ca41ccfSRichard Henderson static TCGOp *gen_tb_start(DisasContextBase *db, uint32_t cflags) 4656234233SRichard Henderson { 47f47a90daSRichard Henderson TCGv_i32 count = NULL; 4856234233SRichard Henderson TCGOp *icount_start_insn = NULL; 4956234233SRichard Henderson 50f47a90daSRichard Henderson if ((cflags & CF_USE_ICOUNT) || !(cflags & CF_NOIRQ)) { 51f47a90daSRichard Henderson count = tcg_temp_new_i32(); 52ad75a51eSRichard Henderson tcg_gen_ld_i32(count, tcg_env, 533b3d7df5SRichard Henderson offsetof(ArchCPU, parent_obj.neg.icount_decr.u32) 543b3d7df5SRichard Henderson - offsetof(ArchCPU, env)); 55f47a90daSRichard Henderson } 5656234233SRichard Henderson 5756234233SRichard Henderson if (cflags & CF_USE_ICOUNT) { 5856234233SRichard Henderson /* 5956234233SRichard Henderson * We emit a sub with a dummy immediate argument. Keep the insn index 6056234233SRichard Henderson * of the sub so that we later (when we know the actual insn count) 6156234233SRichard Henderson * can update the argument with the actual insn count. 6256234233SRichard Henderson */ 6356234233SRichard Henderson tcg_gen_sub_i32(count, count, tcg_constant_i32(0)); 6456234233SRichard Henderson icount_start_insn = tcg_last_op(); 6556234233SRichard Henderson } 6656234233SRichard Henderson 6756234233SRichard Henderson /* 6856234233SRichard Henderson * Emit the check against icount_decr.u32 to see if we should exit 6956234233SRichard Henderson * unless we suppress the check with CF_NOIRQ. If we are using 7056234233SRichard Henderson * icount and have suppressed interruption the higher level code 7156234233SRichard Henderson * should have ensured we don't run more instructions than the 7256234233SRichard Henderson * budget. 7356234233SRichard Henderson */ 7456234233SRichard Henderson if (cflags & CF_NOIRQ) { 7556234233SRichard Henderson tcg_ctx->exitreq_label = NULL; 7656234233SRichard Henderson } else { 7756234233SRichard Henderson tcg_ctx->exitreq_label = gen_new_label(); 7856234233SRichard Henderson tcg_gen_brcondi_i32(TCG_COND_LT, count, 0, tcg_ctx->exitreq_label); 7956234233SRichard Henderson } 8056234233SRichard Henderson 8156234233SRichard Henderson if (cflags & CF_USE_ICOUNT) { 82ad75a51eSRichard Henderson tcg_gen_st16_i32(count, tcg_env, 833b3d7df5SRichard Henderson offsetof(ArchCPU, parent_obj.neg.icount_decr.u16.low) 843b3d7df5SRichard Henderson - offsetof(ArchCPU, env)); 8518a536f1SRichard Henderson } 8618a536f1SRichard Henderson 8756234233SRichard Henderson /* 88464dacf6SRichard Henderson * cpu->neg.can_do_io is set automatically here at the beginning of 8918a536f1SRichard Henderson * each translation block. The cost is minimal, plus it would be 9018a536f1SRichard Henderson * very easy to forget doing it in the translator. 9156234233SRichard Henderson */ 92*cf9b5790SRichard Henderson set_can_do_io(db, db->max_insns == 1); 9356234233SRichard Henderson 9456234233SRichard Henderson return icount_start_insn; 9556234233SRichard Henderson } 9656234233SRichard Henderson 9756234233SRichard Henderson static void gen_tb_end(const TranslationBlock *tb, uint32_t cflags, 9856234233SRichard Henderson TCGOp *icount_start_insn, int num_insns) 9956234233SRichard Henderson { 10056234233SRichard Henderson if (cflags & CF_USE_ICOUNT) { 10156234233SRichard Henderson /* 10256234233SRichard Henderson * Update the num_insn immediate parameter now that we know 10356234233SRichard Henderson * the actual insn count. 10456234233SRichard Henderson */ 10556234233SRichard Henderson tcg_set_insn_param(icount_start_insn, 2, 10656234233SRichard Henderson tcgv_i32_arg(tcg_constant_i32(num_insns))); 10756234233SRichard Henderson } 10856234233SRichard Henderson 10956234233SRichard Henderson if (tcg_ctx->exitreq_label) { 11056234233SRichard Henderson gen_set_label(tcg_ctx->exitreq_label); 11156234233SRichard Henderson tcg_gen_exit_tb(tb, TB_EXIT_REQUESTED); 11256234233SRichard Henderson } 11356234233SRichard Henderson } 11456234233SRichard Henderson 115b1c09220SAnton Johansson bool translator_use_goto_tb(DisasContextBase *db, vaddr dest) 116d3a2a1d8SRichard Henderson { 11784f15616SRichard Henderson /* Suppress goto_tb if requested. */ 11884f15616SRichard Henderson if (tb_cflags(db->tb) & CF_NO_GOTO_TB) { 11984f15616SRichard Henderson return false; 12084f15616SRichard Henderson } 12184f15616SRichard Henderson 122d3a2a1d8SRichard Henderson /* Check for the dest on the same page as the start of the TB. */ 123d3a2a1d8SRichard Henderson return ((db->pc_first ^ dest) & TARGET_PAGE_MASK) == 0; 124d3a2a1d8SRichard Henderson } 125d3a2a1d8SRichard Henderson 126597f9b2dSRichard Henderson void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns, 127b1c09220SAnton Johansson vaddr pc, void *host_pc, const TranslatorOps *ops, 128b1c09220SAnton Johansson DisasContextBase *db) 129bb2e0039SLluís Vilanova { 130d40c5c79SRichard Henderson uint32_t cflags = tb_cflags(tb); 13156234233SRichard Henderson TCGOp *icount_start_insn; 1326ba6f818SEmilio G. Cota bool plugin_enabled; 133f9f1f56eSPavel Dovgalyuk 134bb2e0039SLluís Vilanova /* Initialize DisasContext */ 135bb2e0039SLluís Vilanova db->tb = tb; 136306c8721SRichard Henderson db->pc_first = pc; 137306c8721SRichard Henderson db->pc_next = pc; 138bb2e0039SLluís Vilanova db->is_jmp = DISAS_NEXT; 139bb2e0039SLluís Vilanova db->num_insns = 0; 140597f9b2dSRichard Henderson db->max_insns = *max_insns; 141c2ffd754SRichard Henderson db->singlestep_enabled = cflags & CF_SINGLE_STEP; 1420ca41ccfSRichard Henderson db->saved_can_do_io = -1; 14350627f1bSRichard Henderson db->host_addr[0] = host_pc; 14450627f1bSRichard Henderson db->host_addr[1] = NULL; 14550627f1bSRichard Henderson 146b542683dSEmilio G. Cota ops->init_disas_context(db, cpu); 147bb2e0039SLluís Vilanova tcg_debug_assert(db->is_jmp == DISAS_NEXT); /* no early exit */ 148bb2e0039SLluís Vilanova 149bb2e0039SLluís Vilanova /* Start translating. */ 1500ca41ccfSRichard Henderson icount_start_insn = gen_tb_start(db, cflags); 151bb2e0039SLluís Vilanova ops->tb_start(db, cpu); 152bb2e0039SLluís Vilanova tcg_debug_assert(db->is_jmp == DISAS_NEXT); /* no early exit */ 153bb2e0039SLluís Vilanova 154*cf9b5790SRichard Henderson plugin_enabled = plugin_gen_tb_start(cpu, db, cflags & CF_MEMI_ONLY); 15528a4f0baSRichard Henderson db->plugin_enabled = plugin_enabled; 1566ba6f818SEmilio G. Cota 157bb2e0039SLluís Vilanova while (true) { 1589b1890adSRichard Henderson *max_insns = ++db->num_insns; 159bb2e0039SLluís Vilanova ops->insn_start(db, cpu); 160bb2e0039SLluís Vilanova tcg_debug_assert(db->is_jmp == DISAS_NEXT); /* no early exit */ 161bb2e0039SLluís Vilanova 1626ba6f818SEmilio G. Cota if (plugin_enabled) { 1636ba6f818SEmilio G. Cota plugin_gen_insn_start(cpu, db); 1646ba6f818SEmilio G. Cota } 1656ba6f818SEmilio G. Cota 166*cf9b5790SRichard Henderson /* 167*cf9b5790SRichard Henderson * Disassemble one instruction. The translate_insn hook should 168*cf9b5790SRichard Henderson * update db->pc_next and db->is_jmp to indicate what should be 169*cf9b5790SRichard Henderson * done next -- either exiting this loop or locate the start of 170*cf9b5790SRichard Henderson * the next instruction. 171*cf9b5790SRichard Henderson */ 172*cf9b5790SRichard Henderson if (db->num_insns == db->max_insns) { 173bb2e0039SLluís Vilanova /* Accept I/O on the last instruction. */ 1740ca41ccfSRichard Henderson set_can_do_io(db, true); 175bb2e0039SLluís Vilanova } 1765d97e946SRichard Henderson ops->translate_insn(db, cpu); 177bb2e0039SLluís Vilanova 1786ba6f818SEmilio G. Cota /* 1796ba6f818SEmilio G. Cota * We can't instrument after instructions that change control 1806ba6f818SEmilio G. Cota * flow although this only really affects post-load operations. 1810f92d94aSEmilio Cota * 1820f92d94aSEmilio Cota * Calling plugin_gen_insn_end() before we possibly stop translation 1830f92d94aSEmilio Cota * is important. Even if this ends up as dead code, plugin generation 1840f92d94aSEmilio Cota * needs to see a matching plugin_gen_insn_{start,end}() pair in order 1850f92d94aSEmilio Cota * to accurately track instrumented helpers that might access memory. 1866ba6f818SEmilio G. Cota */ 1876ba6f818SEmilio G. Cota if (plugin_enabled) { 1886ba6f818SEmilio G. Cota plugin_gen_insn_end(); 1896ba6f818SEmilio G. Cota } 1906ba6f818SEmilio G. Cota 1910f92d94aSEmilio Cota /* Stop translation if translate_insn so indicated. */ 1920f92d94aSEmilio Cota if (db->is_jmp != DISAS_NEXT) { 1930f92d94aSEmilio Cota break; 1940f92d94aSEmilio Cota } 1950f92d94aSEmilio Cota 196bb2e0039SLluís Vilanova /* Stop translation if the output buffer is full, 197bb2e0039SLluís Vilanova or we have executed all of the allowed instructions. */ 198b542683dSEmilio G. Cota if (tcg_op_buf_full() || db->num_insns >= db->max_insns) { 199bb2e0039SLluís Vilanova db->is_jmp = DISAS_TOO_MANY; 200bb2e0039SLluís Vilanova break; 201bb2e0039SLluís Vilanova } 202bb2e0039SLluís Vilanova } 203bb2e0039SLluís Vilanova 204bb2e0039SLluís Vilanova /* Emit code to exit the TB, as indicated by db->is_jmp. */ 205bb2e0039SLluís Vilanova ops->tb_stop(db, cpu); 20656234233SRichard Henderson gen_tb_end(tb, cflags, icount_start_insn, db->num_insns); 207bb2e0039SLluís Vilanova 2086ba6f818SEmilio G. Cota if (plugin_enabled) { 209a392277dSMatt Borgerson plugin_gen_tb_end(cpu, db->num_insns); 2106ba6f818SEmilio G. Cota } 2116ba6f818SEmilio G. Cota 212bb2e0039SLluís Vilanova /* The disas_log hook may use these values rather than recompute. */ 213d9971435SRichard Henderson tb->size = db->pc_next - db->pc_first; 214d9971435SRichard Henderson tb->icount = db->num_insns; 215bb2e0039SLluís Vilanova 216bb2e0039SLluís Vilanova if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) 217bb2e0039SLluís Vilanova && qemu_log_in_addr_range(db->pc_first)) { 218c60f599bSRichard Henderson FILE *logfile = qemu_log_trylock(); 21978b54858SRichard Henderson if (logfile) { 22078b54858SRichard Henderson fprintf(logfile, "----------------\n"); 2218eb806a7SRichard Henderson ops->disas_log(db, cpu, logfile); 22278b54858SRichard Henderson fprintf(logfile, "\n"); 223fc59d2d8SRobert Foley qemu_log_unlock(logfile); 224bb2e0039SLluís Vilanova } 22578b54858SRichard Henderson } 226bb2e0039SLluís Vilanova } 227f025692cSIlya Leoshkevich 22850627f1bSRichard Henderson static void *translator_access(CPUArchState *env, DisasContextBase *db, 229b1c09220SAnton Johansson vaddr pc, size_t len) 230f025692cSIlya Leoshkevich { 23150627f1bSRichard Henderson void *host; 232b1c09220SAnton Johansson vaddr base, end; 23350627f1bSRichard Henderson TranslationBlock *tb; 23450627f1bSRichard Henderson 23550627f1bSRichard Henderson tb = db->tb; 23650627f1bSRichard Henderson 23750627f1bSRichard Henderson /* Use slow path if first page is MMIO. */ 23828905cfbSRichard Henderson if (unlikely(tb_page_addr0(tb) == -1)) { 23950627f1bSRichard Henderson return NULL; 24050627f1bSRichard Henderson } 24150627f1bSRichard Henderson 24250627f1bSRichard Henderson end = pc + len - 1; 24350627f1bSRichard Henderson if (likely(is_same_page(db, end))) { 24450627f1bSRichard Henderson host = db->host_addr[0]; 24550627f1bSRichard Henderson base = db->pc_first; 24650627f1bSRichard Henderson } else { 24750627f1bSRichard Henderson host = db->host_addr[1]; 24850627f1bSRichard Henderson base = TARGET_PAGE_ALIGN(db->pc_first); 24950627f1bSRichard Henderson if (host == NULL) { 250deba7870SRichard Henderson tb_page_addr_t page0, old_page1, new_page1; 251deba7870SRichard Henderson 252deba7870SRichard Henderson new_page1 = get_page_addr_code_hostp(env, base, &db->host_addr[1]); 2532627e452SRichard Henderson 2542627e452SRichard Henderson /* 2552627e452SRichard Henderson * If the second page is MMIO, treat as if the first page 2562627e452SRichard Henderson * was MMIO as well, so that we do not cache the TB. 2572627e452SRichard Henderson */ 258deba7870SRichard Henderson if (unlikely(new_page1 == -1)) { 259deba7870SRichard Henderson tb_unlock_pages(tb); 2602627e452SRichard Henderson tb_set_page_addr0(tb, -1); 2612627e452SRichard Henderson return NULL; 2622627e452SRichard Henderson } 2632627e452SRichard Henderson 264deba7870SRichard Henderson /* 265deba7870SRichard Henderson * If this is not the first time around, and page1 matches, 266deba7870SRichard Henderson * then we already have the page locked. Alternately, we're 267deba7870SRichard Henderson * not doing anything to prevent the PTE from changing, so 268deba7870SRichard Henderson * we might wind up with a different page, requiring us to 269deba7870SRichard Henderson * re-do the locking. 270deba7870SRichard Henderson */ 271deba7870SRichard Henderson old_page1 = tb_page_addr1(tb); 272deba7870SRichard Henderson if (likely(new_page1 != old_page1)) { 273deba7870SRichard Henderson page0 = tb_page_addr0(tb); 274deba7870SRichard Henderson if (unlikely(old_page1 != -1)) { 275deba7870SRichard Henderson tb_unlock_page1(page0, old_page1); 276deba7870SRichard Henderson } 277deba7870SRichard Henderson tb_set_page_addr1(tb, new_page1); 278deba7870SRichard Henderson tb_lock_page1(page0, new_page1); 279deba7870SRichard Henderson } 28050627f1bSRichard Henderson host = db->host_addr[1]; 281f025692cSIlya Leoshkevich } 282f025692cSIlya Leoshkevich 28350627f1bSRichard Henderson /* Use slow path when crossing pages. */ 28450627f1bSRichard Henderson if (is_same_page(db, pc)) { 28550627f1bSRichard Henderson return NULL; 28650627f1bSRichard Henderson } 287f025692cSIlya Leoshkevich } 288f025692cSIlya Leoshkevich 28950627f1bSRichard Henderson tcg_debug_assert(pc >= base); 29050627f1bSRichard Henderson return host + (pc - base); 29150627f1bSRichard Henderson } 292f025692cSIlya Leoshkevich 293bc54ef8cSRichard Henderson static void plugin_insn_append(abi_ptr pc, const void *from, size_t size) 294bc54ef8cSRichard Henderson { 295bc54ef8cSRichard Henderson #ifdef CONFIG_PLUGIN 296bc54ef8cSRichard Henderson struct qemu_plugin_insn *insn = tcg_ctx->plugin_insn; 297bc54ef8cSRichard Henderson abi_ptr off; 298bc54ef8cSRichard Henderson 299bc54ef8cSRichard Henderson if (insn == NULL) { 300bc54ef8cSRichard Henderson return; 301bc54ef8cSRichard Henderson } 302bc54ef8cSRichard Henderson off = pc - insn->vaddr; 303bc54ef8cSRichard Henderson if (off < insn->data->len) { 304bc54ef8cSRichard Henderson g_byte_array_set_size(insn->data, off); 305bc54ef8cSRichard Henderson } else if (off > insn->data->len) { 306bc54ef8cSRichard Henderson /* we have an unexpected gap */ 307bc54ef8cSRichard Henderson g_assert_not_reached(); 308bc54ef8cSRichard Henderson } 309bc54ef8cSRichard Henderson 310bc54ef8cSRichard Henderson insn->data = g_byte_array_append(insn->data, from, size); 311bc54ef8cSRichard Henderson #endif 312bc54ef8cSRichard Henderson } 313bc54ef8cSRichard Henderson 31450627f1bSRichard Henderson uint8_t translator_ldub(CPUArchState *env, DisasContextBase *db, abi_ptr pc) 31550627f1bSRichard Henderson { 31650627f1bSRichard Henderson uint8_t ret; 31750627f1bSRichard Henderson void *p = translator_access(env, db, pc, sizeof(ret)); 31850627f1bSRichard Henderson 31950627f1bSRichard Henderson if (p) { 32050627f1bSRichard Henderson plugin_insn_append(pc, p, sizeof(ret)); 32150627f1bSRichard Henderson return ldub_p(p); 32250627f1bSRichard Henderson } 32350627f1bSRichard Henderson ret = cpu_ldub_code(env, pc); 32450627f1bSRichard Henderson plugin_insn_append(pc, &ret, sizeof(ret)); 32550627f1bSRichard Henderson return ret; 32650627f1bSRichard Henderson } 32750627f1bSRichard Henderson 32850627f1bSRichard Henderson uint16_t translator_lduw(CPUArchState *env, DisasContextBase *db, abi_ptr pc) 32950627f1bSRichard Henderson { 33050627f1bSRichard Henderson uint16_t ret, plug; 33150627f1bSRichard Henderson void *p = translator_access(env, db, pc, sizeof(ret)); 33250627f1bSRichard Henderson 33350627f1bSRichard Henderson if (p) { 33450627f1bSRichard Henderson plugin_insn_append(pc, p, sizeof(ret)); 33550627f1bSRichard Henderson return lduw_p(p); 33650627f1bSRichard Henderson } 33750627f1bSRichard Henderson ret = cpu_lduw_code(env, pc); 33850627f1bSRichard Henderson plug = tswap16(ret); 33950627f1bSRichard Henderson plugin_insn_append(pc, &plug, sizeof(ret)); 34050627f1bSRichard Henderson return ret; 34150627f1bSRichard Henderson } 34250627f1bSRichard Henderson 34350627f1bSRichard Henderson uint32_t translator_ldl(CPUArchState *env, DisasContextBase *db, abi_ptr pc) 34450627f1bSRichard Henderson { 34550627f1bSRichard Henderson uint32_t ret, plug; 34650627f1bSRichard Henderson void *p = translator_access(env, db, pc, sizeof(ret)); 34750627f1bSRichard Henderson 34850627f1bSRichard Henderson if (p) { 34950627f1bSRichard Henderson plugin_insn_append(pc, p, sizeof(ret)); 35050627f1bSRichard Henderson return ldl_p(p); 35150627f1bSRichard Henderson } 35250627f1bSRichard Henderson ret = cpu_ldl_code(env, pc); 35350627f1bSRichard Henderson plug = tswap32(ret); 35450627f1bSRichard Henderson plugin_insn_append(pc, &plug, sizeof(ret)); 35550627f1bSRichard Henderson return ret; 35650627f1bSRichard Henderson } 35750627f1bSRichard Henderson 35850627f1bSRichard Henderson uint64_t translator_ldq(CPUArchState *env, DisasContextBase *db, abi_ptr pc) 35950627f1bSRichard Henderson { 36050627f1bSRichard Henderson uint64_t ret, plug; 36150627f1bSRichard Henderson void *p = translator_access(env, db, pc, sizeof(ret)); 36250627f1bSRichard Henderson 36350627f1bSRichard Henderson if (p) { 36450627f1bSRichard Henderson plugin_insn_append(pc, p, sizeof(ret)); 36550627f1bSRichard Henderson return ldq_p(p); 36650627f1bSRichard Henderson } 36750627f1bSRichard Henderson ret = cpu_ldq_code(env, pc); 36850627f1bSRichard Henderson plug = tswap64(ret); 36950627f1bSRichard Henderson plugin_insn_append(pc, &plug, sizeof(ret)); 37050627f1bSRichard Henderson return ret; 37150627f1bSRichard Henderson } 372309e014dSRichard Henderson 373309e014dSRichard Henderson void translator_fake_ldb(uint8_t insn8, abi_ptr pc) 374309e014dSRichard Henderson { 375309e014dSRichard Henderson plugin_insn_append(pc, &insn8, sizeof(insn8)); 376309e014dSRichard Henderson } 377