xref: /qemu/include/exec/translator.h (revision 99977aefd07d85da791e0d851ba2d10d9d5c3094)
177fc6f5eSLluís Vilanova /*
277fc6f5eSLluís Vilanova  * Generic intermediate code generation.
377fc6f5eSLluís Vilanova  *
477fc6f5eSLluís Vilanova  * Copyright (C) 2016-2017 Lluís Vilanova <vilanova@ac.upc.edu>
577fc6f5eSLluís Vilanova  *
677fc6f5eSLluís Vilanova  * This work is licensed under the terms of the GNU GPL, version 2 or later.
777fc6f5eSLluís Vilanova  * See the COPYING file in the top-level directory.
877fc6f5eSLluís Vilanova  */
977fc6f5eSLluís Vilanova 
1077fc6f5eSLluís Vilanova #ifndef EXEC__TRANSLATOR_H
1177fc6f5eSLluís Vilanova #define EXEC__TRANSLATOR_H
1277fc6f5eSLluís Vilanova 
13bb2e0039SLluís Vilanova /*
14bb2e0039SLluís Vilanova  * Include this header from a target-specific file, and add a
15bb2e0039SLluís Vilanova  *
16bb2e0039SLluís Vilanova  *     DisasContextBase base;
17bb2e0039SLluís Vilanova  *
18bb2e0039SLluís Vilanova  * member in your target-specific DisasContext.
19bb2e0039SLluís Vilanova  */
20bb2e0039SLluís Vilanova 
21409c1a0bSEmilio G. Cota #include "qemu/bswap.h"
2280189472SRichard Henderson #include "exec/vaddr.h"
23bb2e0039SLluís Vilanova 
24306c8721SRichard Henderson /**
25306c8721SRichard Henderson  * gen_intermediate_code
26306c8721SRichard Henderson  * @cpu: cpu context
27306c8721SRichard Henderson  * @tb: translation block
28306c8721SRichard Henderson  * @max_insns: max number of instructions to translate
29306c8721SRichard Henderson  * @pc: guest virtual program counter address
30306c8721SRichard Henderson  * @host_pc: host physical program counter address
31306c8721SRichard Henderson  *
32306c8721SRichard Henderson  * This function must be provided by the target, which should create
33306c8721SRichard Henderson  * the target-specific DisasContext, and then invoke translator_loop.
34306c8721SRichard Henderson  */
35597f9b2dSRichard Henderson void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int *max_insns,
3632f0c394SAnton Johansson                            vaddr pc, void *host_pc);
37bb2e0039SLluís Vilanova 
3877fc6f5eSLluís Vilanova /**
3977fc6f5eSLluís Vilanova  * DisasJumpType:
4077fc6f5eSLluís Vilanova  * @DISAS_NEXT: Next instruction in program order.
4177fc6f5eSLluís Vilanova  * @DISAS_TOO_MANY: Too many instructions translated.
4277fc6f5eSLluís Vilanova  * @DISAS_NORETURN: Following code is dead.
4377fc6f5eSLluís Vilanova  * @DISAS_TARGET_*: Start of target-specific conditions.
4477fc6f5eSLluís Vilanova  *
4577fc6f5eSLluís Vilanova  * What instruction to disassemble next.
4677fc6f5eSLluís Vilanova  */
4777fc6f5eSLluís Vilanova typedef enum DisasJumpType {
4877fc6f5eSLluís Vilanova     DISAS_NEXT,
4977fc6f5eSLluís Vilanova     DISAS_TOO_MANY,
5077fc6f5eSLluís Vilanova     DISAS_NORETURN,
5177fc6f5eSLluís Vilanova     DISAS_TARGET_0,
5277fc6f5eSLluís Vilanova     DISAS_TARGET_1,
5377fc6f5eSLluís Vilanova     DISAS_TARGET_2,
5477fc6f5eSLluís Vilanova     DISAS_TARGET_3,
5577fc6f5eSLluís Vilanova     DISAS_TARGET_4,
5677fc6f5eSLluís Vilanova     DISAS_TARGET_5,
5777fc6f5eSLluís Vilanova     DISAS_TARGET_6,
5877fc6f5eSLluís Vilanova     DISAS_TARGET_7,
5977fc6f5eSLluís Vilanova     DISAS_TARGET_8,
6077fc6f5eSLluís Vilanova     DISAS_TARGET_9,
6177fc6f5eSLluís Vilanova     DISAS_TARGET_10,
6277fc6f5eSLluís Vilanova     DISAS_TARGET_11,
6377fc6f5eSLluís Vilanova } DisasJumpType;
6477fc6f5eSLluís Vilanova 
65bb2e0039SLluís Vilanova /**
66bb2e0039SLluís Vilanova  * DisasContextBase:
67bb2e0039SLluís Vilanova  * @tb: Translation block for this disassembly.
68bb2e0039SLluís Vilanova  * @pc_first: Address of first guest instruction in this TB.
69bb2e0039SLluís Vilanova  * @pc_next: Address of next guest instruction in this TB (current during
70bb2e0039SLluís Vilanova  *           disassembly).
71bb2e0039SLluís Vilanova  * @is_jmp: What instruction to disassemble next.
72bb2e0039SLluís Vilanova  * @num_insns: Number of translated instructions (including current).
73b542683dSEmilio G. Cota  * @max_insns: Maximum number of instructions to be translated in this TB.
74bb2e0039SLluís Vilanova  * @singlestep_enabled: "Hardware" single stepping enabled.
750ca41ccfSRichard Henderson  * @saved_can_do_io: Known value of cpu->neg.can_do_io, or -1 for unknown.
7628a4f0baSRichard Henderson  * @plugin_enabled: TCG plugin enabled in this TB.
77e7face70SRichard Henderson  * @insn_start: The last op emitted by the insn_start hook,
78e7face70SRichard Henderson  *              which is expected to be INDEX_op_insn_start.
79bb2e0039SLluís Vilanova  *
80bb2e0039SLluís Vilanova  * Architecture-agnostic disassembly context.
81bb2e0039SLluís Vilanova  */
82bb2e0039SLluís Vilanova typedef struct DisasContextBase {
8350627f1bSRichard Henderson     TranslationBlock *tb;
8485c19af6SAnton Johansson     vaddr pc_first;
8585c19af6SAnton Johansson     vaddr pc_next;
86bb2e0039SLluís Vilanova     DisasJumpType is_jmp;
87b542683dSEmilio G. Cota     int num_insns;
88b542683dSEmilio G. Cota     int max_insns;
89bb2e0039SLluís Vilanova     bool singlestep_enabled;
9028a4f0baSRichard Henderson     bool plugin_enabled;
91e7face70SRichard Henderson     struct TCGOp *insn_start;
9250627f1bSRichard Henderson     void *host_addr[2];
93bb2e0039SLluís Vilanova } DisasContextBase;
94bb2e0039SLluís Vilanova 
95bb2e0039SLluís Vilanova /**
96bb2e0039SLluís Vilanova  * TranslatorOps:
97bb2e0039SLluís Vilanova  * @init_disas_context:
98bb2e0039SLluís Vilanova  *      Initialize the target-specific portions of DisasContext struct.
99bb2e0039SLluís Vilanova  *      The generic DisasContextBase has already been initialized.
100bb2e0039SLluís Vilanova  *
101bb2e0039SLluís Vilanova  * @tb_start:
102bb2e0039SLluís Vilanova  *      Emit any code required before the start of the main loop,
103bb2e0039SLluís Vilanova  *      after the generic gen_tb_start().
104bb2e0039SLluís Vilanova  *
105bb2e0039SLluís Vilanova  * @insn_start:
106bb2e0039SLluís Vilanova  *      Emit the tcg_gen_insn_start opcode.
107bb2e0039SLluís Vilanova  *
108bb2e0039SLluís Vilanova  * @translate_insn:
109bb2e0039SLluís Vilanova  *      Disassemble one instruction and set db->pc_next for the start
110bb2e0039SLluís Vilanova  *      of the following instruction.  Set db->is_jmp as necessary to
111bb2e0039SLluís Vilanova  *      terminate the main loop.
112bb2e0039SLluís Vilanova  *
113bb2e0039SLluís Vilanova  * @tb_stop:
114bb2e0039SLluís Vilanova  *      Emit any opcodes required to exit the TB, based on db->is_jmp.
115bb2e0039SLluís Vilanova  *
116bb2e0039SLluís Vilanova  * @disas_log:
117bb2e0039SLluís Vilanova  *      Print instruction disassembly to log.
118bb2e0039SLluís Vilanova  */
119bb2e0039SLluís Vilanova typedef struct TranslatorOps {
120b542683dSEmilio G. Cota     void (*init_disas_context)(DisasContextBase *db, CPUState *cpu);
121bb2e0039SLluís Vilanova     void (*tb_start)(DisasContextBase *db, CPUState *cpu);
122bb2e0039SLluís Vilanova     void (*insn_start)(DisasContextBase *db, CPUState *cpu);
123bb2e0039SLluís Vilanova     void (*translate_insn)(DisasContextBase *db, CPUState *cpu);
124bb2e0039SLluís Vilanova     void (*tb_stop)(DisasContextBase *db, CPUState *cpu);
1258eb806a7SRichard Henderson     void (*disas_log)(const DisasContextBase *db, CPUState *cpu, FILE *f);
126bb2e0039SLluís Vilanova } TranslatorOps;
127bb2e0039SLluís Vilanova 
128bb2e0039SLluís Vilanova /**
129bb2e0039SLluís Vilanova  * translator_loop:
130bb2e0039SLluís Vilanova  * @cpu: Target vCPU.
131bb2e0039SLluís Vilanova  * @tb: Translation block.
1328b86d6d2SRichard Henderson  * @max_insns: Maximum number of insns to translate.
133306c8721SRichard Henderson  * @pc: guest virtual program counter address
134306c8721SRichard Henderson  * @host_pc: host physical program counter address
135306c8721SRichard Henderson  * @ops: Target-specific operations.
136306c8721SRichard Henderson  * @db: Disassembly context.
137bb2e0039SLluís Vilanova  *
138bb2e0039SLluís Vilanova  * Generic translator loop.
139bb2e0039SLluís Vilanova  *
140bb2e0039SLluís Vilanova  * Translation will stop in the following cases (in order):
141bb2e0039SLluís Vilanova  * - When is_jmp set by #TranslatorOps::breakpoint_check.
142bb2e0039SLluís Vilanova  *   - set to DISAS_TOO_MANY exits after translating one more insn
143bb2e0039SLluís Vilanova  *   - set to any other value than DISAS_NEXT exits immediately.
144bb2e0039SLluís Vilanova  * - When is_jmp set by #TranslatorOps::translate_insn.
145bb2e0039SLluís Vilanova  *   - set to any value other than DISAS_NEXT exits immediately.
146bb2e0039SLluís Vilanova  * - When the TCG operation buffer is full.
147bb2e0039SLluís Vilanova  * - When single-stepping is enabled (system-wide or on the current vCPU).
148bb2e0039SLluís Vilanova  * - When too many instructions have been translated.
149bb2e0039SLluís Vilanova  */
150597f9b2dSRichard Henderson void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
151b1c09220SAnton Johansson                      vaddr pc, void *host_pc, const TranslatorOps *ops,
152b1c09220SAnton Johansson                      DisasContextBase *db);
153bb2e0039SLluís Vilanova 
154d3a2a1d8SRichard Henderson /**
155d3a2a1d8SRichard Henderson  * translator_use_goto_tb
156d3a2a1d8SRichard Henderson  * @db: Disassembly context
157d3a2a1d8SRichard Henderson  * @dest: target pc of the goto
158d3a2a1d8SRichard Henderson  *
159d3a2a1d8SRichard Henderson  * Return true if goto_tb is allowed between the current TB
160d3a2a1d8SRichard Henderson  * and the destination PC.
161d3a2a1d8SRichard Henderson  */
162b1c09220SAnton Johansson bool translator_use_goto_tb(DisasContextBase *db, vaddr dest);
163d3a2a1d8SRichard Henderson 
164dfd1b812SRichard Henderson /**
165dfd1b812SRichard Henderson  * translator_io_start
166dfd1b812SRichard Henderson  * @db: Disassembly context
167dfd1b812SRichard Henderson  *
168d417e221SPhilippe Mathieu-Daudé  * If icount is enabled, set cpu->can_do_io, adjust db->is_jmp to
169dfd1b812SRichard Henderson  * DISAS_TOO_MANY if it is still DISAS_NEXT, and return true.
170dfd1b812SRichard Henderson  * Otherwise return false.
171dfd1b812SRichard Henderson  */
172dfd1b812SRichard Henderson bool translator_io_start(DisasContextBase *db);
173dfd1b812SRichard Henderson 
174409c1a0bSEmilio G. Cota /*
175409c1a0bSEmilio G. Cota  * Translator Load Functions
176409c1a0bSEmilio G. Cota  *
177a6d456dfSRichard Henderson  * These are intended to replace the direct usage of the cpu_ld*_code
178a6d456dfSRichard Henderson  * functions and are mandatory for front-ends that have been migrated
179a6d456dfSRichard Henderson  * to the common translator_loop. These functions are only intended
180a6d456dfSRichard Henderson  * to be called from the translation stage and should not be called
181a6d456dfSRichard Henderson  * from helper functions. Those functions should be converted to encode
182a6d456dfSRichard Henderson  * the relevant information at translation time.
183409c1a0bSEmilio G. Cota  */
184409c1a0bSEmilio G. Cota 
18580189472SRichard Henderson uint8_t translator_ldub(CPUArchState *env, DisasContextBase *db, vaddr pc);
18680189472SRichard Henderson uint16_t translator_lduw(CPUArchState *env, DisasContextBase *db, vaddr pc);
18780189472SRichard Henderson uint32_t translator_ldl(CPUArchState *env, DisasContextBase *db, vaddr pc);
18880189472SRichard Henderson uint64_t translator_ldq(CPUArchState *env, DisasContextBase *db, vaddr pc);
18950627f1bSRichard Henderson 
19050627f1bSRichard Henderson static inline uint16_t
19150627f1bSRichard Henderson translator_lduw_swap(CPUArchState *env, DisasContextBase *db,
19280189472SRichard Henderson                      vaddr pc, bool do_swap)
19350627f1bSRichard Henderson {
19450627f1bSRichard Henderson     uint16_t ret = translator_lduw(env, db, pc);
19550627f1bSRichard Henderson     if (do_swap) {
19650627f1bSRichard Henderson         ret = bswap16(ret);
19750627f1bSRichard Henderson     }
19850627f1bSRichard Henderson     return ret;
199409c1a0bSEmilio G. Cota }
200409c1a0bSEmilio G. Cota 
20150627f1bSRichard Henderson static inline uint32_t
20250627f1bSRichard Henderson translator_ldl_swap(CPUArchState *env, DisasContextBase *db,
20380189472SRichard Henderson                     vaddr pc, bool do_swap)
20450627f1bSRichard Henderson {
20550627f1bSRichard Henderson     uint32_t ret = translator_ldl(env, db, pc);
20650627f1bSRichard Henderson     if (do_swap) {
20750627f1bSRichard Henderson         ret = bswap32(ret);
20850627f1bSRichard Henderson     }
20950627f1bSRichard Henderson     return ret;
21050627f1bSRichard Henderson }
211f025692cSIlya Leoshkevich 
21250627f1bSRichard Henderson static inline uint64_t
21350627f1bSRichard Henderson translator_ldq_swap(CPUArchState *env, DisasContextBase *db,
21480189472SRichard Henderson                     vaddr pc, bool do_swap)
21550627f1bSRichard Henderson {
21650627f1bSRichard Henderson     uint64_t ret = translator_ldq(env, db, pc);
21750627f1bSRichard Henderson     if (do_swap) {
21850627f1bSRichard Henderson         ret = bswap64(ret);
21950627f1bSRichard Henderson     }
22050627f1bSRichard Henderson     return ret;
22150627f1bSRichard Henderson }
222409c1a0bSEmilio G. Cota 
2239fa97e04SAlex Bennée /**
2249fa97e04SAlex Bennée  * translator_fake_ldb - fake instruction load
225*99977aefSRichard Henderson  * @db: Disassembly context
2269fa97e04SAlex Bennée  * @pc: program counter of instruction
227*99977aefSRichard Henderson  * @insn8: byte of instruction
2289fa97e04SAlex Bennée  *
2299fa97e04SAlex Bennée  * This is a special case helper used where the instruction we are
2309fa97e04SAlex Bennée  * about to translate comes from somewhere else (e.g. being
2319fa97e04SAlex Bennée  * re-synthesised for s390x "ex"). It ensures we update other areas of
2329fa97e04SAlex Bennée  * the translator with details of the executed instruction.
2339fa97e04SAlex Bennée  */
234*99977aefSRichard Henderson void translator_fake_ldb(DisasContextBase *db, vaddr pc, uint8_t insn8);
2359fa97e04SAlex Bennée 
23666f3b79eSRichard Henderson #ifdef COMPILING_PER_TARGET
237f3b2b81bSIlya Leoshkevich /*
238f3b2b81bSIlya Leoshkevich  * Return whether addr is on the same page as where disassembly started.
239f3b2b81bSIlya Leoshkevich  * Translators can use this to enforce the rule that only single-insn
240f3b2b81bSIlya Leoshkevich  * translation blocks are allowed to cross page boundaries.
241f3b2b81bSIlya Leoshkevich  */
24285c19af6SAnton Johansson static inline bool is_same_page(const DisasContextBase *db, vaddr addr)
243f3b2b81bSIlya Leoshkevich {
244f3b2b81bSIlya Leoshkevich     return ((addr ^ db->pc_first) & TARGET_PAGE_MASK) == 0;
245f3b2b81bSIlya Leoshkevich }
24666f3b79eSRichard Henderson #endif
247f3b2b81bSIlya Leoshkevich 
24877fc6f5eSLluís Vilanova #endif /* EXEC__TRANSLATOR_H */
249