xref: /openbmc/qemu/accel/tcg/translator.c (revision b5cf7428)
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     bool plugin_enabled;
54 
55     /* Initialize DisasContext */
56     db->tb = tb;
57     db->pc_first = tb->pc;
58     db->pc_next = db->pc_first;
59     db->is_jmp = DISAS_NEXT;
60     db->num_insns = 0;
61     db->max_insns = max_insns;
62     db->singlestep_enabled = cpu->singlestep_enabled;
63 
64     ops->init_disas_context(db, cpu);
65     tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
66 
67     /* Reset the temp count so that we can identify leaks */
68     tcg_clear_temp_count();
69 
70     /* Start translating.  */
71     gen_tb_start(db->tb);
72     ops->tb_start(db, cpu);
73     tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
74 
75     plugin_enabled = plugin_gen_tb_start(cpu, tb,
76                                          tb_cflags(db->tb) & CF_MEMI_ONLY);
77 
78     while (true) {
79         db->num_insns++;
80         ops->insn_start(db, cpu);
81         tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
82 
83         if (plugin_enabled) {
84             plugin_gen_insn_start(cpu, db);
85         }
86 
87         /* Disassemble one instruction.  The translate_insn hook should
88            update db->pc_next and db->is_jmp to indicate what should be
89            done next -- either exiting this loop or locate the start of
90            the next instruction.  */
91         if (db->num_insns == db->max_insns
92             && (tb_cflags(db->tb) & CF_LAST_IO)) {
93             /* Accept I/O on the last instruction.  */
94             gen_io_start();
95             ops->translate_insn(db, cpu);
96         } else {
97             /* we should only see CF_MEMI_ONLY for io_recompile */
98             tcg_debug_assert(!(tb_cflags(db->tb) & CF_MEMI_ONLY));
99             ops->translate_insn(db, cpu);
100         }
101 
102         /* Stop translation if translate_insn so indicated.  */
103         if (db->is_jmp != DISAS_NEXT) {
104             break;
105         }
106 
107         /*
108          * We can't instrument after instructions that change control
109          * flow although this only really affects post-load operations.
110          */
111         if (plugin_enabled) {
112             plugin_gen_insn_end();
113         }
114 
115         /* Stop translation if the output buffer is full,
116            or we have executed all of the allowed instructions.  */
117         if (tcg_op_buf_full() || db->num_insns >= db->max_insns) {
118             db->is_jmp = DISAS_TOO_MANY;
119             break;
120         }
121     }
122 
123     /* Emit code to exit the TB, as indicated by db->is_jmp.  */
124     ops->tb_stop(db, cpu);
125     gen_tb_end(db->tb, db->num_insns);
126 
127     if (plugin_enabled) {
128         plugin_gen_tb_end(cpu);
129     }
130 
131     /* The disas_log hook may use these values rather than recompute.  */
132     tb->size = db->pc_next - db->pc_first;
133     tb->icount = db->num_insns;
134 
135 #ifdef DEBUG_DISAS
136     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
137         && qemu_log_in_addr_range(db->pc_first)) {
138         FILE *logfile = qemu_log_lock();
139         qemu_log("----------------\n");
140         ops->disas_log(db, cpu);
141         qemu_log("\n");
142         qemu_log_unlock(logfile);
143     }
144 #endif
145 }
146