xref: /qemu/include/exec/translator.h (revision 306c872103b4d0986c9f671eb7538b0b70bf69b5)
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"
26f025692cSIlya Leoshkevich #include "exec/translate-all.h"
27bb2e0039SLluís Vilanova #include "tcg/tcg.h"
28bb2e0039SLluís Vilanova 
29*306c8721SRichard Henderson /**
30*306c8721SRichard Henderson  * gen_intermediate_code
31*306c8721SRichard Henderson  * @cpu: cpu context
32*306c8721SRichard Henderson  * @tb: translation block
33*306c8721SRichard Henderson  * @max_insns: max number of instructions to translate
34*306c8721SRichard Henderson  * @pc: guest virtual program counter address
35*306c8721SRichard Henderson  * @host_pc: host physical program counter address
36*306c8721SRichard Henderson  *
37*306c8721SRichard Henderson  * This function must be provided by the target, which should create
38*306c8721SRichard Henderson  * the target-specific DisasContext, and then invoke translator_loop.
39*306c8721SRichard Henderson  */
40*306c8721SRichard Henderson void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns,
41*306c8721SRichard Henderson                            target_ulong pc, void *host_pc);
42bb2e0039SLluís Vilanova 
4377fc6f5eSLluís Vilanova /**
4477fc6f5eSLluís Vilanova  * DisasJumpType:
4577fc6f5eSLluís Vilanova  * @DISAS_NEXT: Next instruction in program order.
4677fc6f5eSLluís Vilanova  * @DISAS_TOO_MANY: Too many instructions translated.
4777fc6f5eSLluís Vilanova  * @DISAS_NORETURN: Following code is dead.
4877fc6f5eSLluís Vilanova  * @DISAS_TARGET_*: Start of target-specific conditions.
4977fc6f5eSLluís Vilanova  *
5077fc6f5eSLluís Vilanova  * What instruction to disassemble next.
5177fc6f5eSLluís Vilanova  */
5277fc6f5eSLluís Vilanova typedef enum DisasJumpType {
5377fc6f5eSLluís Vilanova     DISAS_NEXT,
5477fc6f5eSLluís Vilanova     DISAS_TOO_MANY,
5577fc6f5eSLluís Vilanova     DISAS_NORETURN,
5677fc6f5eSLluís Vilanova     DISAS_TARGET_0,
5777fc6f5eSLluís Vilanova     DISAS_TARGET_1,
5877fc6f5eSLluís Vilanova     DISAS_TARGET_2,
5977fc6f5eSLluís Vilanova     DISAS_TARGET_3,
6077fc6f5eSLluís Vilanova     DISAS_TARGET_4,
6177fc6f5eSLluís Vilanova     DISAS_TARGET_5,
6277fc6f5eSLluís Vilanova     DISAS_TARGET_6,
6377fc6f5eSLluís Vilanova     DISAS_TARGET_7,
6477fc6f5eSLluís Vilanova     DISAS_TARGET_8,
6577fc6f5eSLluís Vilanova     DISAS_TARGET_9,
6677fc6f5eSLluís Vilanova     DISAS_TARGET_10,
6777fc6f5eSLluís Vilanova     DISAS_TARGET_11,
6877fc6f5eSLluís Vilanova } DisasJumpType;
6977fc6f5eSLluís Vilanova 
70bb2e0039SLluís Vilanova /**
71bb2e0039SLluís Vilanova  * DisasContextBase:
72bb2e0039SLluís Vilanova  * @tb: Translation block for this disassembly.
73bb2e0039SLluís Vilanova  * @pc_first: Address of first guest instruction in this TB.
74bb2e0039SLluís Vilanova  * @pc_next: Address of next guest instruction in this TB (current during
75bb2e0039SLluís Vilanova  *           disassembly).
76bb2e0039SLluís Vilanova  * @is_jmp: What instruction to disassemble next.
77bb2e0039SLluís Vilanova  * @num_insns: Number of translated instructions (including current).
78b542683dSEmilio G. Cota  * @max_insns: Maximum number of instructions to be translated in this TB.
79bb2e0039SLluís Vilanova  * @singlestep_enabled: "Hardware" single stepping enabled.
80bb2e0039SLluís Vilanova  *
81bb2e0039SLluís Vilanova  * Architecture-agnostic disassembly context.
82bb2e0039SLluís Vilanova  */
83bb2e0039SLluís Vilanova typedef struct DisasContextBase {
84d9971435SRichard Henderson     const TranslationBlock *tb;
85bb2e0039SLluís Vilanova     target_ulong pc_first;
86bb2e0039SLluís Vilanova     target_ulong pc_next;
87bb2e0039SLluís Vilanova     DisasJumpType is_jmp;
88b542683dSEmilio G. Cota     int num_insns;
89b542683dSEmilio G. Cota     int max_insns;
90bb2e0039SLluís Vilanova     bool singlestep_enabled;
91f025692cSIlya Leoshkevich #ifdef CONFIG_USER_ONLY
92f025692cSIlya Leoshkevich     /*
93f025692cSIlya Leoshkevich      * Guest address of the last byte of the last protected page.
94f025692cSIlya Leoshkevich      *
95f025692cSIlya Leoshkevich      * Pages containing the translated instructions are made non-writable in
96f025692cSIlya Leoshkevich      * order to achieve consistency in case another thread is modifying the
97f025692cSIlya Leoshkevich      * code while translate_insn() fetches the instruction bytes piecemeal.
98f025692cSIlya Leoshkevich      * Such writer threads are blocked on mmap_lock() in page_unprotect().
99f025692cSIlya Leoshkevich      */
100f025692cSIlya Leoshkevich     target_ulong page_protect_end;
101f025692cSIlya Leoshkevich #endif
102bb2e0039SLluís Vilanova } DisasContextBase;
103bb2e0039SLluís Vilanova 
104bb2e0039SLluís Vilanova /**
105bb2e0039SLluís Vilanova  * TranslatorOps:
106bb2e0039SLluís Vilanova  * @init_disas_context:
107bb2e0039SLluís Vilanova  *      Initialize the target-specific portions of DisasContext struct.
108bb2e0039SLluís Vilanova  *      The generic DisasContextBase has already been initialized.
109bb2e0039SLluís Vilanova  *
110bb2e0039SLluís Vilanova  * @tb_start:
111bb2e0039SLluís Vilanova  *      Emit any code required before the start of the main loop,
112bb2e0039SLluís Vilanova  *      after the generic gen_tb_start().
113bb2e0039SLluís Vilanova  *
114bb2e0039SLluís Vilanova  * @insn_start:
115bb2e0039SLluís Vilanova  *      Emit the tcg_gen_insn_start opcode.
116bb2e0039SLluís Vilanova  *
117bb2e0039SLluís Vilanova  * @translate_insn:
118bb2e0039SLluís Vilanova  *      Disassemble one instruction and set db->pc_next for the start
119bb2e0039SLluís Vilanova  *      of the following instruction.  Set db->is_jmp as necessary to
120bb2e0039SLluís Vilanova  *      terminate the main loop.
121bb2e0039SLluís Vilanova  *
122bb2e0039SLluís Vilanova  * @tb_stop:
123bb2e0039SLluís Vilanova  *      Emit any opcodes required to exit the TB, based on db->is_jmp.
124bb2e0039SLluís Vilanova  *
125bb2e0039SLluís Vilanova  * @disas_log:
126bb2e0039SLluís Vilanova  *      Print instruction disassembly to log.
127bb2e0039SLluís Vilanova  */
128bb2e0039SLluís Vilanova typedef struct TranslatorOps {
129b542683dSEmilio G. Cota     void (*init_disas_context)(DisasContextBase *db, CPUState *cpu);
130bb2e0039SLluís Vilanova     void (*tb_start)(DisasContextBase *db, CPUState *cpu);
131bb2e0039SLluís Vilanova     void (*insn_start)(DisasContextBase *db, CPUState *cpu);
132bb2e0039SLluís Vilanova     void (*translate_insn)(DisasContextBase *db, CPUState *cpu);
133bb2e0039SLluís Vilanova     void (*tb_stop)(DisasContextBase *db, CPUState *cpu);
1348eb806a7SRichard Henderson     void (*disas_log)(const DisasContextBase *db, CPUState *cpu, FILE *f);
135bb2e0039SLluís Vilanova } TranslatorOps;
136bb2e0039SLluís Vilanova 
137bb2e0039SLluís Vilanova /**
138bb2e0039SLluís Vilanova  * translator_loop:
139bb2e0039SLluís Vilanova  * @cpu: Target vCPU.
140bb2e0039SLluís Vilanova  * @tb: Translation block.
1418b86d6d2SRichard Henderson  * @max_insns: Maximum number of insns to translate.
142*306c8721SRichard Henderson  * @pc: guest virtual program counter address
143*306c8721SRichard Henderson  * @host_pc: host physical program counter address
144*306c8721SRichard Henderson  * @ops: Target-specific operations.
145*306c8721SRichard Henderson  * @db: Disassembly context.
146bb2e0039SLluís Vilanova  *
147bb2e0039SLluís Vilanova  * Generic translator loop.
148bb2e0039SLluís Vilanova  *
149bb2e0039SLluís Vilanova  * Translation will stop in the following cases (in order):
150bb2e0039SLluís Vilanova  * - When is_jmp set by #TranslatorOps::breakpoint_check.
151bb2e0039SLluís Vilanova  *   - set to DISAS_TOO_MANY exits after translating one more insn
152bb2e0039SLluís Vilanova  *   - set to any other value than DISAS_NEXT exits immediately.
153bb2e0039SLluís Vilanova  * - When is_jmp set by #TranslatorOps::translate_insn.
154bb2e0039SLluís Vilanova  *   - set to any value other than DISAS_NEXT exits immediately.
155bb2e0039SLluís Vilanova  * - When the TCG operation buffer is full.
156bb2e0039SLluís Vilanova  * - When single-stepping is enabled (system-wide or on the current vCPU).
157bb2e0039SLluís Vilanova  * - When too many instructions have been translated.
158bb2e0039SLluís Vilanova  */
159*306c8721SRichard Henderson void translator_loop(CPUState *cpu, TranslationBlock *tb, int max_insns,
160*306c8721SRichard Henderson                      target_ulong pc, void *host_pc,
161*306c8721SRichard Henderson                      const TranslatorOps *ops, DisasContextBase *db);
162bb2e0039SLluís Vilanova 
163bb2e0039SLluís Vilanova void translator_loop_temp_check(DisasContextBase *db);
164bb2e0039SLluís Vilanova 
165d3a2a1d8SRichard Henderson /**
166d3a2a1d8SRichard Henderson  * translator_use_goto_tb
167d3a2a1d8SRichard Henderson  * @db: Disassembly context
168d3a2a1d8SRichard Henderson  * @dest: target pc of the goto
169d3a2a1d8SRichard Henderson  *
170d3a2a1d8SRichard Henderson  * Return true if goto_tb is allowed between the current TB
171d3a2a1d8SRichard Henderson  * and the destination PC.
172d3a2a1d8SRichard Henderson  */
173d3a2a1d8SRichard Henderson bool translator_use_goto_tb(DisasContextBase *db, target_ulong dest);
174d3a2a1d8SRichard Henderson 
175409c1a0bSEmilio G. Cota /*
176409c1a0bSEmilio G. Cota  * Translator Load Functions
177409c1a0bSEmilio G. Cota  *
178a6d456dfSRichard Henderson  * These are intended to replace the direct usage of the cpu_ld*_code
179a6d456dfSRichard Henderson  * functions and are mandatory for front-ends that have been migrated
180a6d456dfSRichard Henderson  * to the common translator_loop. These functions are only intended
181a6d456dfSRichard Henderson  * to be called from the translation stage and should not be called
182a6d456dfSRichard Henderson  * from helper functions. Those functions should be converted to encode
183a6d456dfSRichard Henderson  * the relevant information at translation time.
184409c1a0bSEmilio G. Cota  */
185409c1a0bSEmilio G. Cota 
186a6d456dfSRichard Henderson #define GEN_TRANSLATOR_LD(fullname, type, load_fn, swap_fn)             \
187f025692cSIlya Leoshkevich     type fullname ## _swap(CPUArchState *env, DisasContextBase *dcbase, \
188f025692cSIlya Leoshkevich                            abi_ptr pc, bool do_swap);                   \
1894e116893SIlya Leoshkevich     static inline type fullname(CPUArchState *env,                      \
1904e116893SIlya Leoshkevich                                 DisasContextBase *dcbase, abi_ptr pc)   \
191409c1a0bSEmilio G. Cota     {                                                                   \
1924e116893SIlya Leoshkevich         return fullname ## _swap(env, dcbase, pc, false);               \
193409c1a0bSEmilio G. Cota     }
194409c1a0bSEmilio G. Cota 
195f025692cSIlya Leoshkevich #define FOR_EACH_TRANSLATOR_LD(F)                                       \
196f025692cSIlya Leoshkevich     F(translator_ldub, uint8_t, cpu_ldub_code, /* no swap */)           \
197f025692cSIlya Leoshkevich     F(translator_lduw, uint16_t, cpu_lduw_code, bswap16)                \
198f025692cSIlya Leoshkevich     F(translator_ldl, uint32_t, cpu_ldl_code, bswap32)                  \
199f025692cSIlya Leoshkevich     F(translator_ldq, uint64_t, cpu_ldq_code, bswap64)
200f025692cSIlya Leoshkevich 
201f025692cSIlya Leoshkevich FOR_EACH_TRANSLATOR_LD(GEN_TRANSLATOR_LD)
202f025692cSIlya Leoshkevich 
203409c1a0bSEmilio G. Cota #undef GEN_TRANSLATOR_LD
204409c1a0bSEmilio G. Cota 
205f3b2b81bSIlya Leoshkevich /*
206f3b2b81bSIlya Leoshkevich  * Return whether addr is on the same page as where disassembly started.
207f3b2b81bSIlya Leoshkevich  * Translators can use this to enforce the rule that only single-insn
208f3b2b81bSIlya Leoshkevich  * translation blocks are allowed to cross page boundaries.
209f3b2b81bSIlya Leoshkevich  */
210f3b2b81bSIlya Leoshkevich static inline bool is_same_page(const DisasContextBase *db, target_ulong addr)
211f3b2b81bSIlya Leoshkevich {
212f3b2b81bSIlya Leoshkevich     return ((addr ^ db->pc_first) & TARGET_PAGE_MASK) == 0;
213f3b2b81bSIlya Leoshkevich }
214f3b2b81bSIlya Leoshkevich 
21577fc6f5eSLluís Vilanova #endif /* EXEC__TRANSLATOR_H */
216