xref: /openbmc/qemu/include/exec/translator.h (revision c1774bdb)
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 #ifndef EXEC__TRANSLATOR_H
11 #define EXEC__TRANSLATOR_H
12 
13 /*
14  * Include this header from a target-specific file, and add a
15  *
16  *     DisasContextBase base;
17  *
18  * member in your target-specific DisasContext.
19  */
20 
21 #include "qemu/bswap.h"
22 #include "exec/cpu_ldst.h"	/* for abi_ptr */
23 
24 /**
25  * gen_intermediate_code
26  * @cpu: cpu context
27  * @tb: translation block
28  * @max_insns: max number of instructions to translate
29  * @pc: guest virtual program counter address
30  * @host_pc: host physical program counter address
31  *
32  * This function must be provided by the target, which should create
33  * the target-specific DisasContext, and then invoke translator_loop.
34  */
35 void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int *max_insns,
36                            target_ulong pc, void *host_pc);
37 
38 /**
39  * DisasJumpType:
40  * @DISAS_NEXT: Next instruction in program order.
41  * @DISAS_TOO_MANY: Too many instructions translated.
42  * @DISAS_NORETURN: Following code is dead.
43  * @DISAS_TARGET_*: Start of target-specific conditions.
44  *
45  * What instruction to disassemble next.
46  */
47 typedef enum DisasJumpType {
48     DISAS_NEXT,
49     DISAS_TOO_MANY,
50     DISAS_NORETURN,
51     DISAS_TARGET_0,
52     DISAS_TARGET_1,
53     DISAS_TARGET_2,
54     DISAS_TARGET_3,
55     DISAS_TARGET_4,
56     DISAS_TARGET_5,
57     DISAS_TARGET_6,
58     DISAS_TARGET_7,
59     DISAS_TARGET_8,
60     DISAS_TARGET_9,
61     DISAS_TARGET_10,
62     DISAS_TARGET_11,
63 } DisasJumpType;
64 
65 /**
66  * DisasContextBase:
67  * @tb: Translation block for this disassembly.
68  * @pc_first: Address of first guest instruction in this TB.
69  * @pc_next: Address of next guest instruction in this TB (current during
70  *           disassembly).
71  * @is_jmp: What instruction to disassemble next.
72  * @num_insns: Number of translated instructions (including current).
73  * @max_insns: Maximum number of instructions to be translated in this TB.
74  * @singlestep_enabled: "Hardware" single stepping enabled.
75  * @saved_can_do_io: Known value of cpu->neg.can_do_io, or -1 for unknown.
76  *
77  * Architecture-agnostic disassembly context.
78  */
79 typedef struct DisasContextBase {
80     TranslationBlock *tb;
81     target_ulong pc_first;
82     target_ulong pc_next;
83     DisasJumpType is_jmp;
84     int num_insns;
85     int max_insns;
86     bool singlestep_enabled;
87     int8_t saved_can_do_io;
88     void *host_addr[2];
89 } DisasContextBase;
90 
91 /**
92  * TranslatorOps:
93  * @init_disas_context:
94  *      Initialize the target-specific portions of DisasContext struct.
95  *      The generic DisasContextBase has already been initialized.
96  *
97  * @tb_start:
98  *      Emit any code required before the start of the main loop,
99  *      after the generic gen_tb_start().
100  *
101  * @insn_start:
102  *      Emit the tcg_gen_insn_start opcode.
103  *
104  * @translate_insn:
105  *      Disassemble one instruction and set db->pc_next for the start
106  *      of the following instruction.  Set db->is_jmp as necessary to
107  *      terminate the main loop.
108  *
109  * @tb_stop:
110  *      Emit any opcodes required to exit the TB, based on db->is_jmp.
111  *
112  * @disas_log:
113  *      Print instruction disassembly to log.
114  */
115 typedef struct TranslatorOps {
116     void (*init_disas_context)(DisasContextBase *db, CPUState *cpu);
117     void (*tb_start)(DisasContextBase *db, CPUState *cpu);
118     void (*insn_start)(DisasContextBase *db, CPUState *cpu);
119     void (*translate_insn)(DisasContextBase *db, CPUState *cpu);
120     void (*tb_stop)(DisasContextBase *db, CPUState *cpu);
121     void (*disas_log)(const DisasContextBase *db, CPUState *cpu, FILE *f);
122 } TranslatorOps;
123 
124 /**
125  * translator_loop:
126  * @cpu: Target vCPU.
127  * @tb: Translation block.
128  * @max_insns: Maximum number of insns to translate.
129  * @pc: guest virtual program counter address
130  * @host_pc: host physical program counter address
131  * @ops: Target-specific operations.
132  * @db: Disassembly context.
133  *
134  * Generic translator loop.
135  *
136  * Translation will stop in the following cases (in order):
137  * - When is_jmp set by #TranslatorOps::breakpoint_check.
138  *   - set to DISAS_TOO_MANY exits after translating one more insn
139  *   - set to any other value than DISAS_NEXT exits immediately.
140  * - When is_jmp set by #TranslatorOps::translate_insn.
141  *   - set to any value other than DISAS_NEXT exits immediately.
142  * - When the TCG operation buffer is full.
143  * - When single-stepping is enabled (system-wide or on the current vCPU).
144  * - When too many instructions have been translated.
145  */
146 void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
147                      vaddr pc, void *host_pc, const TranslatorOps *ops,
148                      DisasContextBase *db);
149 
150 /**
151  * translator_use_goto_tb
152  * @db: Disassembly context
153  * @dest: target pc of the goto
154  *
155  * Return true if goto_tb is allowed between the current TB
156  * and the destination PC.
157  */
158 bool translator_use_goto_tb(DisasContextBase *db, vaddr dest);
159 
160 /**
161  * translator_io_start
162  * @db: Disassembly context
163  *
164  * If icount is enabled, set cpu->can_do_io, adjust db->is_jmp to
165  * DISAS_TOO_MANY if it is still DISAS_NEXT, and return true.
166  * Otherwise return false.
167  */
168 bool translator_io_start(DisasContextBase *db);
169 
170 /*
171  * Translator Load Functions
172  *
173  * These are intended to replace the direct usage of the cpu_ld*_code
174  * functions and are mandatory for front-ends that have been migrated
175  * to the common translator_loop. These functions are only intended
176  * to be called from the translation stage and should not be called
177  * from helper functions. Those functions should be converted to encode
178  * the relevant information at translation time.
179  */
180 
181 uint8_t translator_ldub(CPUArchState *env, DisasContextBase *db, abi_ptr pc);
182 uint16_t translator_lduw(CPUArchState *env, DisasContextBase *db, abi_ptr pc);
183 uint32_t translator_ldl(CPUArchState *env, DisasContextBase *db, abi_ptr pc);
184 uint64_t translator_ldq(CPUArchState *env, DisasContextBase *db, abi_ptr pc);
185 
186 static inline uint16_t
187 translator_lduw_swap(CPUArchState *env, DisasContextBase *db,
188                      abi_ptr pc, bool do_swap)
189 {
190     uint16_t ret = translator_lduw(env, db, pc);
191     if (do_swap) {
192         ret = bswap16(ret);
193     }
194     return ret;
195 }
196 
197 static inline uint32_t
198 translator_ldl_swap(CPUArchState *env, DisasContextBase *db,
199                     abi_ptr pc, bool do_swap)
200 {
201     uint32_t ret = translator_ldl(env, db, pc);
202     if (do_swap) {
203         ret = bswap32(ret);
204     }
205     return ret;
206 }
207 
208 static inline uint64_t
209 translator_ldq_swap(CPUArchState *env, DisasContextBase *db,
210                     abi_ptr pc, bool do_swap)
211 {
212     uint64_t ret = translator_ldq(env, db, pc);
213     if (do_swap) {
214         ret = bswap64(ret);
215     }
216     return ret;
217 }
218 
219 /**
220  * translator_fake_ldb - fake instruction load
221  * @insn8: byte of instruction
222  * @pc: program counter of instruction
223  *
224  * This is a special case helper used where the instruction we are
225  * about to translate comes from somewhere else (e.g. being
226  * re-synthesised for s390x "ex"). It ensures we update other areas of
227  * the translator with details of the executed instruction.
228  */
229 void translator_fake_ldb(uint8_t insn8, abi_ptr pc);
230 
231 /*
232  * Return whether addr is on the same page as where disassembly started.
233  * Translators can use this to enforce the rule that only single-insn
234  * translation blocks are allowed to cross page boundaries.
235  */
236 static inline bool is_same_page(const DisasContextBase *db, target_ulong addr)
237 {
238     return ((addr ^ db->pc_first) & TARGET_PAGE_MASK) == 0;
239 }
240 
241 #endif /* EXEC__TRANSLATOR_H */
242