xref: /openbmc/qemu/accel/tcg/translator.c (revision 6f03770d)
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 "cpu.h"
13 #include "tcg/tcg.h"
14 #include "tcg/tcg-op.h"
15 #include "exec/exec-all.h"
16 #include "exec/gen-icount.h"
17 #include "exec/log.h"
18 #include "exec/translator.h"
19 #include "exec/plugin-gen.h"
20 #include "sysemu/replay.h"
21 
22 /* Pairs with tcg_clear_temp_count.
23    To be called by #TranslatorOps.{translate_insn,tb_stop} if
24    (1) the target is sufficiently clean to support reporting,
25    (2) as and when all temporaries are known to be consumed.
26    For most targets, (2) is at the end of translate_insn.  */
27 void translator_loop_temp_check(DisasContextBase *db)
28 {
29     if (tcg_check_temp_count()) {
30         qemu_log("warning: TCG temporary leaks before "
31                  TARGET_FMT_lx "\n", db->pc_next);
32     }
33 }
34 
35 void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
36                      CPUState *cpu, TranslationBlock *tb, int max_insns)
37 {
38     int bp_insn = 0;
39     bool plugin_enabled;
40 
41     /* Initialize DisasContext */
42     db->tb = tb;
43     db->pc_first = tb->pc;
44     db->pc_next = db->pc_first;
45     db->is_jmp = DISAS_NEXT;
46     db->num_insns = 0;
47     db->max_insns = max_insns;
48     db->singlestep_enabled = cpu->singlestep_enabled;
49 
50     ops->init_disas_context(db, cpu);
51     tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
52 
53     /* Reset the temp count so that we can identify leaks */
54     tcg_clear_temp_count();
55 
56     /* Start translating.  */
57     gen_tb_start(db->tb);
58     ops->tb_start(db, cpu);
59     tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
60 
61     plugin_enabled = plugin_gen_tb_start(cpu, tb,
62                                          tb_cflags(db->tb) & CF_MEMI_ONLY);
63 
64     while (true) {
65         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         /* Pass breakpoint hits to target for further processing */
74         if (!db->singlestep_enabled
75             && unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
76             CPUBreakpoint *bp;
77             QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
78                 if (bp->pc == db->pc_next) {
79                     if (ops->breakpoint_check(db, cpu, bp)) {
80                         bp_insn = 1;
81                         break;
82                     }
83                 }
84             }
85             /* The breakpoint_check hook may use DISAS_TOO_MANY to indicate
86                that only one more instruction is to be executed.  Otherwise
87                it should use DISAS_NORETURN when generating an exception,
88                but may use a DISAS_TARGET_* value for Something Else.  */
89             if (db->is_jmp > DISAS_TOO_MANY) {
90                 break;
91             }
92         }
93 
94         /* Disassemble one instruction.  The translate_insn hook should
95            update db->pc_next and db->is_jmp to indicate what should be
96            done next -- either exiting this loop or locate the start of
97            the next instruction.  */
98         if (db->num_insns == db->max_insns
99             && (tb_cflags(db->tb) & CF_LAST_IO)) {
100             /* Accept I/O on the last instruction.  */
101             gen_io_start();
102             ops->translate_insn(db, cpu);
103         } else {
104             /* we should only see CF_MEMI_ONLY for io_recompile */
105             tcg_debug_assert(!(tb_cflags(db->tb) & CF_MEMI_ONLY));
106             ops->translate_insn(db, cpu);
107         }
108 
109         /* Stop translation if translate_insn so indicated.  */
110         if (db->is_jmp != DISAS_NEXT) {
111             break;
112         }
113 
114         /*
115          * We can't instrument after instructions that change control
116          * flow although this only really affects post-load operations.
117          */
118         if (plugin_enabled) {
119             plugin_gen_insn_end();
120         }
121 
122         /* Stop translation if the output buffer is full,
123            or we have executed all of the allowed instructions.  */
124         if (tcg_op_buf_full() || db->num_insns >= db->max_insns) {
125             db->is_jmp = DISAS_TOO_MANY;
126             break;
127         }
128     }
129 
130     /* Emit code to exit the TB, as indicated by db->is_jmp.  */
131     ops->tb_stop(db, cpu);
132     gen_tb_end(db->tb, db->num_insns - bp_insn);
133 
134     if (plugin_enabled) {
135         plugin_gen_tb_end(cpu);
136     }
137 
138     /* The disas_log hook may use these values rather than recompute.  */
139     tb->size = db->pc_next - db->pc_first;
140     tb->icount = db->num_insns;
141 
142 #ifdef DEBUG_DISAS
143     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
144         && qemu_log_in_addr_range(db->pc_first)) {
145         FILE *logfile = qemu_log_lock();
146         qemu_log("----------------\n");
147         ops->disas_log(db, cpu);
148         qemu_log("\n");
149         qemu_log_unlock(logfile);
150     }
151 #endif
152 }
153