xref: /openbmc/qemu/accel/tcg/translator.c (revision ad1a706f)
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 in the case of single-steping.  */
37     if (db->singlestep_enabled || singlestep) {
38         return false;
39     }
40 
41     /* Check for the dest on the same page as the start of the TB.  */
42     return ((db->pc_first ^ dest) & TARGET_PAGE_MASK) == 0;
43 }
44 
45 void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
46                      CPUState *cpu, TranslationBlock *tb, int max_insns)
47 {
48     int bp_insn = 0;
49     bool plugin_enabled;
50 
51     /* Initialize DisasContext */
52     db->tb = tb;
53     db->pc_first = tb->pc;
54     db->pc_next = db->pc_first;
55     db->is_jmp = DISAS_NEXT;
56     db->num_insns = 0;
57     db->max_insns = max_insns;
58     db->singlestep_enabled = cpu->singlestep_enabled;
59 
60     ops->init_disas_context(db, cpu);
61     tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
62 
63     /* Reset the temp count so that we can identify leaks */
64     tcg_clear_temp_count();
65 
66     /* Start translating.  */
67     gen_tb_start(db->tb);
68     ops->tb_start(db, cpu);
69     tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
70 
71     plugin_enabled = plugin_gen_tb_start(cpu, tb,
72                                          tb_cflags(db->tb) & CF_MEMI_ONLY);
73 
74     while (true) {
75         db->num_insns++;
76         ops->insn_start(db, cpu);
77         tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
78 
79         if (plugin_enabled) {
80             plugin_gen_insn_start(cpu, db);
81         }
82 
83         /* Pass breakpoint hits to target for further processing */
84         if (!db->singlestep_enabled
85             && unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
86             CPUBreakpoint *bp;
87             QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
88                 if (bp->pc == db->pc_next) {
89                     if (ops->breakpoint_check(db, cpu, bp)) {
90                         bp_insn = 1;
91                         break;
92                     }
93                 }
94             }
95             /* The breakpoint_check hook may use DISAS_TOO_MANY to indicate
96                that only one more instruction is to be executed.  Otherwise
97                it should use DISAS_NORETURN when generating an exception,
98                but may use a DISAS_TARGET_* value for Something Else.  */
99             if (db->is_jmp > DISAS_TOO_MANY) {
100                 break;
101             }
102         }
103 
104         /* Disassemble one instruction.  The translate_insn hook should
105            update db->pc_next and db->is_jmp to indicate what should be
106            done next -- either exiting this loop or locate the start of
107            the next instruction.  */
108         if (db->num_insns == db->max_insns
109             && (tb_cflags(db->tb) & CF_LAST_IO)) {
110             /* Accept I/O on the last instruction.  */
111             gen_io_start();
112             ops->translate_insn(db, cpu);
113         } else {
114             /* we should only see CF_MEMI_ONLY for io_recompile */
115             tcg_debug_assert(!(tb_cflags(db->tb) & CF_MEMI_ONLY));
116             ops->translate_insn(db, cpu);
117         }
118 
119         /* Stop translation if translate_insn so indicated.  */
120         if (db->is_jmp != DISAS_NEXT) {
121             break;
122         }
123 
124         /*
125          * We can't instrument after instructions that change control
126          * flow although this only really affects post-load operations.
127          */
128         if (plugin_enabled) {
129             plugin_gen_insn_end();
130         }
131 
132         /* Stop translation if the output buffer is full,
133            or we have executed all of the allowed instructions.  */
134         if (tcg_op_buf_full() || db->num_insns >= db->max_insns) {
135             db->is_jmp = DISAS_TOO_MANY;
136             break;
137         }
138     }
139 
140     /* Emit code to exit the TB, as indicated by db->is_jmp.  */
141     ops->tb_stop(db, cpu);
142     gen_tb_end(db->tb, db->num_insns - bp_insn);
143 
144     if (plugin_enabled) {
145         plugin_gen_tb_end(cpu);
146     }
147 
148     /* The disas_log hook may use these values rather than recompute.  */
149     tb->size = db->pc_next - db->pc_first;
150     tb->icount = db->num_insns;
151 
152 #ifdef DEBUG_DISAS
153     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
154         && qemu_log_in_addr_range(db->pc_first)) {
155         FILE *logfile = qemu_log_lock();
156         qemu_log("----------------\n");
157         ops->disas_log(db, cpu);
158         qemu_log("\n");
159         qemu_log_unlock(logfile);
160     }
161 #endif
162 }
163