xref: /openbmc/qemu/accel/tcg/translator.c (revision 47451466)
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-common.h"
12 #include "qemu/error-report.h"
13 #include "cpu.h"
14 #include "tcg/tcg.h"
15 #include "tcg/tcg-op.h"
16 #include "exec/exec-all.h"
17 #include "exec/gen-icount.h"
18 #include "exec/log.h"
19 #include "exec/translator.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 void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
35                      CPUState *cpu, TranslationBlock *tb)
36 {
37     /* Initialize DisasContext */
38     db->tb = tb;
39     db->pc_first = tb->pc;
40     db->pc_next = db->pc_first;
41     db->is_jmp = DISAS_NEXT;
42     db->num_insns = 0;
43     db->singlestep_enabled = cpu->singlestep_enabled;
44 
45     /* Instruction counting */
46     db->max_insns = tb_cflags(db->tb) & CF_COUNT_MASK;
47     if (db->max_insns == 0) {
48         db->max_insns = CF_COUNT_MASK;
49     }
50     if (db->max_insns > TCG_MAX_INSNS) {
51         db->max_insns = TCG_MAX_INSNS;
52     }
53     if (db->singlestep_enabled || singlestep) {
54         db->max_insns = 1;
55     }
56 
57     ops->init_disas_context(db, cpu);
58     tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
59 
60     /* Reset the temp count so that we can identify leaks */
61     tcg_clear_temp_count();
62 
63     /* Start translating.  */
64     gen_tb_start(db->tb);
65     ops->tb_start(db, cpu);
66     tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
67 
68     while (true) {
69         db->num_insns++;
70         ops->insn_start(db, cpu);
71         tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
72 
73         /* Pass breakpoint hits to target for further processing */
74         if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
75             CPUBreakpoint *bp;
76             QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
77                 if (bp->pc == db->pc_next) {
78                     if (ops->breakpoint_check(db, cpu, bp)) {
79                         break;
80                     }
81                 }
82             }
83             /* The breakpoint_check hook may use DISAS_TOO_MANY to indicate
84                that only one more instruction is to be executed.  Otherwise
85                it should use DISAS_NORETURN when generating an exception,
86                but may use a DISAS_TARGET_* value for Something Else.  */
87             if (db->is_jmp > DISAS_TOO_MANY) {
88                 break;
89             }
90         }
91 
92         /* Disassemble one instruction.  The translate_insn hook should
93            update db->pc_next and db->is_jmp to indicate what should be
94            done next -- either exiting this loop or locate the start of
95            the next instruction.  */
96         if (db->num_insns == db->max_insns
97             && (tb_cflags(db->tb) & CF_LAST_IO)) {
98             /* Accept I/O on the last instruction.  */
99             gen_io_start();
100             ops->translate_insn(db, cpu);
101             gen_io_end();
102         } else {
103             ops->translate_insn(db, cpu);
104         }
105 
106         /* Stop translation if translate_insn so indicated.  */
107         if (db->is_jmp != DISAS_NEXT) {
108             break;
109         }
110 
111         /* Stop translation if the output buffer is full,
112            or we have executed all of the allowed instructions.  */
113         if (tcg_op_buf_full() || db->num_insns >= db->max_insns) {
114             db->is_jmp = DISAS_TOO_MANY;
115             break;
116         }
117     }
118 
119     /* Emit code to exit the TB, as indicated by db->is_jmp.  */
120     ops->tb_stop(db, cpu);
121     gen_tb_end(db->tb, db->num_insns);
122 
123     /* The disas_log hook may use these values rather than recompute.  */
124     db->tb->size = db->pc_next - db->pc_first;
125     db->tb->icount = db->num_insns;
126 
127 #ifdef DEBUG_DISAS
128     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
129         && qemu_log_in_addr_range(db->pc_first)) {
130         qemu_log_lock();
131         qemu_log("----------------\n");
132         ops->disas_log(db, cpu);
133         qemu_log("\n");
134         qemu_log_unlock();
135     }
136 #endif
137 }
138