xref: /qemu/include/exec/translator.h (revision 4e116893c6079b51efdc9e226be3f1a530f47f5e)
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 
21bb2e0039SLluís Vilanova 
22409c1a0bSEmilio G. Cota #include "qemu/bswap.h"
23bb2e0039SLluís Vilanova #include "exec/exec-all.h"
24409c1a0bSEmilio G. Cota #include "exec/cpu_ldst.h"
25409c1a0bSEmilio G. Cota #include "exec/plugin-gen.h"
26bb2e0039SLluís Vilanova #include "tcg/tcg.h"
27bb2e0039SLluís Vilanova 
28bb2e0039SLluís Vilanova 
2977fc6f5eSLluís Vilanova /**
3077fc6f5eSLluís Vilanova  * DisasJumpType:
3177fc6f5eSLluís Vilanova  * @DISAS_NEXT: Next instruction in program order.
3277fc6f5eSLluís Vilanova  * @DISAS_TOO_MANY: Too many instructions translated.
3377fc6f5eSLluís Vilanova  * @DISAS_NORETURN: Following code is dead.
3477fc6f5eSLluís Vilanova  * @DISAS_TARGET_*: Start of target-specific conditions.
3577fc6f5eSLluís Vilanova  *
3677fc6f5eSLluís Vilanova  * What instruction to disassemble next.
3777fc6f5eSLluís Vilanova  */
3877fc6f5eSLluís Vilanova typedef enum DisasJumpType {
3977fc6f5eSLluís Vilanova     DISAS_NEXT,
4077fc6f5eSLluís Vilanova     DISAS_TOO_MANY,
4177fc6f5eSLluís Vilanova     DISAS_NORETURN,
4277fc6f5eSLluís Vilanova     DISAS_TARGET_0,
4377fc6f5eSLluís Vilanova     DISAS_TARGET_1,
4477fc6f5eSLluís Vilanova     DISAS_TARGET_2,
4577fc6f5eSLluís Vilanova     DISAS_TARGET_3,
4677fc6f5eSLluís Vilanova     DISAS_TARGET_4,
4777fc6f5eSLluís Vilanova     DISAS_TARGET_5,
4877fc6f5eSLluís Vilanova     DISAS_TARGET_6,
4977fc6f5eSLluís Vilanova     DISAS_TARGET_7,
5077fc6f5eSLluís Vilanova     DISAS_TARGET_8,
5177fc6f5eSLluís Vilanova     DISAS_TARGET_9,
5277fc6f5eSLluís Vilanova     DISAS_TARGET_10,
5377fc6f5eSLluís Vilanova     DISAS_TARGET_11,
5477fc6f5eSLluís Vilanova } DisasJumpType;
5577fc6f5eSLluís Vilanova 
56bb2e0039SLluís Vilanova /**
57bb2e0039SLluís Vilanova  * DisasContextBase:
58bb2e0039SLluís Vilanova  * @tb: Translation block for this disassembly.
59bb2e0039SLluís Vilanova  * @pc_first: Address of first guest instruction in this TB.
60bb2e0039SLluís Vilanova  * @pc_next: Address of next guest instruction in this TB (current during
61bb2e0039SLluís Vilanova  *           disassembly).
62bb2e0039SLluís Vilanova  * @is_jmp: What instruction to disassemble next.
63bb2e0039SLluís Vilanova  * @num_insns: Number of translated instructions (including current).
64b542683dSEmilio G. Cota  * @max_insns: Maximum number of instructions to be translated in this TB.
65bb2e0039SLluís Vilanova  * @singlestep_enabled: "Hardware" single stepping enabled.
66bb2e0039SLluís Vilanova  *
67bb2e0039SLluís Vilanova  * Architecture-agnostic disassembly context.
68bb2e0039SLluís Vilanova  */
69bb2e0039SLluís Vilanova typedef struct DisasContextBase {
70d9971435SRichard Henderson     const TranslationBlock *tb;
71bb2e0039SLluís Vilanova     target_ulong pc_first;
72bb2e0039SLluís Vilanova     target_ulong pc_next;
73bb2e0039SLluís Vilanova     DisasJumpType is_jmp;
74b542683dSEmilio G. Cota     int num_insns;
75b542683dSEmilio G. Cota     int max_insns;
76bb2e0039SLluís Vilanova     bool singlestep_enabled;
77bb2e0039SLluís Vilanova } DisasContextBase;
78bb2e0039SLluís Vilanova 
79bb2e0039SLluís Vilanova /**
80bb2e0039SLluís Vilanova  * TranslatorOps:
81bb2e0039SLluís Vilanova  * @init_disas_context:
82bb2e0039SLluís Vilanova  *      Initialize the target-specific portions of DisasContext struct.
83bb2e0039SLluís Vilanova  *      The generic DisasContextBase has already been initialized.
84bb2e0039SLluís Vilanova  *
85bb2e0039SLluís Vilanova  * @tb_start:
86bb2e0039SLluís Vilanova  *      Emit any code required before the start of the main loop,
87bb2e0039SLluís Vilanova  *      after the generic gen_tb_start().
88bb2e0039SLluís Vilanova  *
89bb2e0039SLluís Vilanova  * @insn_start:
90bb2e0039SLluís Vilanova  *      Emit the tcg_gen_insn_start opcode.
91bb2e0039SLluís Vilanova  *
92bb2e0039SLluís Vilanova  * @translate_insn:
93bb2e0039SLluís Vilanova  *      Disassemble one instruction and set db->pc_next for the start
94bb2e0039SLluís Vilanova  *      of the following instruction.  Set db->is_jmp as necessary to
95bb2e0039SLluís Vilanova  *      terminate the main loop.
96bb2e0039SLluís Vilanova  *
97bb2e0039SLluís Vilanova  * @tb_stop:
98bb2e0039SLluís Vilanova  *      Emit any opcodes required to exit the TB, based on db->is_jmp.
99bb2e0039SLluís Vilanova  *
100bb2e0039SLluís Vilanova  * @disas_log:
101bb2e0039SLluís Vilanova  *      Print instruction disassembly to log.
102bb2e0039SLluís Vilanova  */
103bb2e0039SLluís Vilanova typedef struct TranslatorOps {
104b542683dSEmilio G. Cota     void (*init_disas_context)(DisasContextBase *db, CPUState *cpu);
105bb2e0039SLluís Vilanova     void (*tb_start)(DisasContextBase *db, CPUState *cpu);
106bb2e0039SLluís Vilanova     void (*insn_start)(DisasContextBase *db, CPUState *cpu);
107bb2e0039SLluís Vilanova     void (*translate_insn)(DisasContextBase *db, CPUState *cpu);
108bb2e0039SLluís Vilanova     void (*tb_stop)(DisasContextBase *db, CPUState *cpu);
109bb2e0039SLluís Vilanova     void (*disas_log)(const DisasContextBase *db, CPUState *cpu);
110bb2e0039SLluís Vilanova } TranslatorOps;
111bb2e0039SLluís Vilanova 
112bb2e0039SLluís Vilanova /**
113bb2e0039SLluís Vilanova  * translator_loop:
114bb2e0039SLluís Vilanova  * @ops: Target-specific operations.
115bb2e0039SLluís Vilanova  * @db: Disassembly context.
116bb2e0039SLluís Vilanova  * @cpu: Target vCPU.
117bb2e0039SLluís Vilanova  * @tb: Translation block.
1188b86d6d2SRichard Henderson  * @max_insns: Maximum number of insns to translate.
119bb2e0039SLluís Vilanova  *
120bb2e0039SLluís Vilanova  * Generic translator loop.
121bb2e0039SLluís Vilanova  *
122bb2e0039SLluís Vilanova  * Translation will stop in the following cases (in order):
123bb2e0039SLluís Vilanova  * - When is_jmp set by #TranslatorOps::breakpoint_check.
124bb2e0039SLluís Vilanova  *   - set to DISAS_TOO_MANY exits after translating one more insn
125bb2e0039SLluís Vilanova  *   - set to any other value than DISAS_NEXT exits immediately.
126bb2e0039SLluís Vilanova  * - When is_jmp set by #TranslatorOps::translate_insn.
127bb2e0039SLluís Vilanova  *   - set to any value other than DISAS_NEXT exits immediately.
128bb2e0039SLluís Vilanova  * - When the TCG operation buffer is full.
129bb2e0039SLluís Vilanova  * - When single-stepping is enabled (system-wide or on the current vCPU).
130bb2e0039SLluís Vilanova  * - When too many instructions have been translated.
131bb2e0039SLluís Vilanova  */
132bb2e0039SLluís Vilanova void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
1338b86d6d2SRichard Henderson                      CPUState *cpu, TranslationBlock *tb, int max_insns);
134bb2e0039SLluís Vilanova 
135bb2e0039SLluís Vilanova void translator_loop_temp_check(DisasContextBase *db);
136bb2e0039SLluís Vilanova 
137d3a2a1d8SRichard Henderson /**
138d3a2a1d8SRichard Henderson  * translator_use_goto_tb
139d3a2a1d8SRichard Henderson  * @db: Disassembly context
140d3a2a1d8SRichard Henderson  * @dest: target pc of the goto
141d3a2a1d8SRichard Henderson  *
142d3a2a1d8SRichard Henderson  * Return true if goto_tb is allowed between the current TB
143d3a2a1d8SRichard Henderson  * and the destination PC.
144d3a2a1d8SRichard Henderson  */
145d3a2a1d8SRichard Henderson bool translator_use_goto_tb(DisasContextBase *db, target_ulong dest);
146d3a2a1d8SRichard Henderson 
147409c1a0bSEmilio G. Cota /*
148409c1a0bSEmilio G. Cota  * Translator Load Functions
149409c1a0bSEmilio G. Cota  *
150a6d456dfSRichard Henderson  * These are intended to replace the direct usage of the cpu_ld*_code
151a6d456dfSRichard Henderson  * functions and are mandatory for front-ends that have been migrated
152a6d456dfSRichard Henderson  * to the common translator_loop. These functions are only intended
153a6d456dfSRichard Henderson  * to be called from the translation stage and should not be called
154a6d456dfSRichard Henderson  * from helper functions. Those functions should be converted to encode
155a6d456dfSRichard Henderson  * the relevant information at translation time.
156409c1a0bSEmilio G. Cota  */
157409c1a0bSEmilio G. Cota 
158a6d456dfSRichard Henderson #define GEN_TRANSLATOR_LD(fullname, type, load_fn, swap_fn)             \
159409c1a0bSEmilio G. Cota     static inline type                                                  \
160*4e116893SIlya Leoshkevich     fullname ## _swap(CPUArchState *env, DisasContextBase *dcbase,      \
161*4e116893SIlya Leoshkevich                       abi_ptr pc, bool do_swap)                         \
162409c1a0bSEmilio G. Cota     {                                                                   \
163a6d456dfSRichard Henderson         type ret = load_fn(env, pc);                                    \
164409c1a0bSEmilio G. Cota         if (do_swap) {                                                  \
165409c1a0bSEmilio G. Cota             ret = swap_fn(ret);                                         \
166409c1a0bSEmilio G. Cota         }                                                               \
167409c1a0bSEmilio G. Cota         plugin_insn_append(&ret, sizeof(ret));                          \
168409c1a0bSEmilio G. Cota         return ret;                                                     \
169409c1a0bSEmilio G. Cota     }                                                                   \
170*4e116893SIlya Leoshkevich     static inline type fullname(CPUArchState *env,                      \
171*4e116893SIlya Leoshkevich                                 DisasContextBase *dcbase, abi_ptr pc)   \
172409c1a0bSEmilio G. Cota     {                                                                   \
173*4e116893SIlya Leoshkevich         return fullname ## _swap(env, dcbase, pc, false);               \
174409c1a0bSEmilio G. Cota     }
175409c1a0bSEmilio G. Cota 
176a6d456dfSRichard Henderson GEN_TRANSLATOR_LD(translator_ldub, uint8_t, cpu_ldub_code, /* no swap */)
177a6d456dfSRichard Henderson GEN_TRANSLATOR_LD(translator_ldsw, int16_t, cpu_ldsw_code, bswap16)
178a6d456dfSRichard Henderson GEN_TRANSLATOR_LD(translator_lduw, uint16_t, cpu_lduw_code, bswap16)
179a6d456dfSRichard Henderson GEN_TRANSLATOR_LD(translator_ldl, uint32_t, cpu_ldl_code, bswap32)
180a6d456dfSRichard Henderson GEN_TRANSLATOR_LD(translator_ldq, uint64_t, cpu_ldq_code, bswap64)
181409c1a0bSEmilio G. Cota #undef GEN_TRANSLATOR_LD
182409c1a0bSEmilio G. Cota 
18377fc6f5eSLluís Vilanova #endif  /* EXEC__TRANSLATOR_H */
184