translator.h 8.1 KB

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