2
0

exec-all.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * internal execution defines for qemu
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _EXEC_ALL_H_
  20. #define _EXEC_ALL_H_
  21. #include "qemu-common.h"
  22. /* allow to see translation results - the slowdown should be negligible, so we leave it */
  23. #define DEBUG_DISAS
  24. /* Page tracking code uses ram addresses in system mode, and virtual
  25. addresses in userspace mode. Define tb_page_addr_t to be an appropriate
  26. type. */
  27. #if defined(CONFIG_USER_ONLY)
  28. typedef abi_ulong tb_page_addr_t;
  29. #else
  30. typedef ram_addr_t tb_page_addr_t;
  31. #endif
  32. /* is_jmp field values */
  33. #define DISAS_NEXT 0 /* next instruction can be analyzed */
  34. #define DISAS_JUMP 1 /* only pc was modified dynamically */
  35. #define DISAS_UPDATE 2 /* cpu state was modified dynamically */
  36. #define DISAS_TB_JUMP 3 /* only pc was modified statically */
  37. struct TranslationBlock;
  38. typedef struct TranslationBlock TranslationBlock;
  39. /* XXX: make safe guess about sizes */
  40. #define MAX_OP_PER_INSTR 208
  41. #if HOST_LONG_BITS == 32
  42. #define MAX_OPC_PARAM_PER_ARG 2
  43. #else
  44. #define MAX_OPC_PARAM_PER_ARG 1
  45. #endif
  46. #define MAX_OPC_PARAM_IARGS 4
  47. #define MAX_OPC_PARAM_OARGS 1
  48. #define MAX_OPC_PARAM_ARGS (MAX_OPC_PARAM_IARGS + MAX_OPC_PARAM_OARGS)
  49. /* A Call op needs up to 4 + 2N parameters on 32-bit archs,
  50. * and up to 4 + N parameters on 64-bit archs
  51. * (N = number of input arguments + output arguments). */
  52. #define MAX_OPC_PARAM (4 + (MAX_OPC_PARAM_PER_ARG * MAX_OPC_PARAM_ARGS))
  53. #define OPC_BUF_SIZE 640
  54. #define OPC_MAX_SIZE (OPC_BUF_SIZE - MAX_OP_PER_INSTR)
  55. /* Maximum size a TCG op can expand to. This is complicated because a
  56. single op may require several host instructions and register reloads.
  57. For now take a wild guess at 192 bytes, which should allow at least
  58. a couple of fixup instructions per argument. */
  59. #define TCG_MAX_OP_SIZE 192
  60. #define OPPARAM_BUF_SIZE (OPC_BUF_SIZE * MAX_OPC_PARAM)
  61. extern target_ulong gen_opc_pc[OPC_BUF_SIZE];
  62. extern uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
  63. extern uint16_t gen_opc_icount[OPC_BUF_SIZE];
  64. #include "qemu-log.h"
  65. void gen_intermediate_code(CPUArchState *env, struct TranslationBlock *tb);
  66. void gen_intermediate_code_pc(CPUArchState *env, struct TranslationBlock *tb);
  67. void restore_state_to_opc(CPUArchState *env, struct TranslationBlock *tb,
  68. int pc_pos);
  69. void cpu_gen_init(void);
  70. int cpu_gen_code(CPUArchState *env, struct TranslationBlock *tb,
  71. int *gen_code_size_ptr);
  72. int cpu_restore_state(struct TranslationBlock *tb,
  73. CPUArchState *env, uintptr_t searched_pc);
  74. void QEMU_NORETURN cpu_resume_from_signal(CPUArchState *env1, void *puc);
  75. void QEMU_NORETURN cpu_io_recompile(CPUArchState *env, uintptr_t retaddr);
  76. TranslationBlock *tb_gen_code(CPUArchState *env,
  77. target_ulong pc, target_ulong cs_base, int flags,
  78. int cflags);
  79. void cpu_exec_init(CPUArchState *env);
  80. void QEMU_NORETURN cpu_loop_exit(CPUArchState *env1);
  81. int page_unprotect(target_ulong address, uintptr_t pc, void *puc);
  82. void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
  83. int is_cpu_write_access);
  84. void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end,
  85. int is_cpu_write_access);
  86. #if !defined(CONFIG_USER_ONLY)
  87. /* cputlb.c */
  88. void tlb_flush_page(CPUArchState *env, target_ulong addr);
  89. void tlb_flush(CPUArchState *env, int flush_global);
  90. void tlb_set_page(CPUArchState *env, target_ulong vaddr,
  91. target_phys_addr_t paddr, int prot,
  92. int mmu_idx, target_ulong size);
  93. void tb_invalidate_phys_addr(target_phys_addr_t addr);
  94. #else
  95. static inline void tlb_flush_page(CPUArchState *env, target_ulong addr)
  96. {
  97. }
  98. static inline void tlb_flush(CPUArchState *env, int flush_global)
  99. {
  100. }
  101. #endif
  102. #define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */
  103. #define CODE_GEN_PHYS_HASH_BITS 15
  104. #define CODE_GEN_PHYS_HASH_SIZE (1 << CODE_GEN_PHYS_HASH_BITS)
  105. #define MIN_CODE_GEN_BUFFER_SIZE (1024 * 1024)
  106. /* estimated block size for TB allocation */
  107. /* XXX: use a per code average code fragment size and modulate it
  108. according to the host CPU */
  109. #if defined(CONFIG_SOFTMMU)
  110. #define CODE_GEN_AVG_BLOCK_SIZE 128
  111. #else
  112. #define CODE_GEN_AVG_BLOCK_SIZE 64
  113. #endif
  114. #if defined(_ARCH_PPC) || defined(__x86_64__) || defined(__arm__) || defined(__i386__)
  115. #define USE_DIRECT_JUMP
  116. #elif defined(CONFIG_TCG_INTERPRETER)
  117. #define USE_DIRECT_JUMP
  118. #endif
  119. struct TranslationBlock {
  120. target_ulong pc; /* simulated PC corresponding to this block (EIP + CS base) */
  121. target_ulong cs_base; /* CS base for this block */
  122. uint64_t flags; /* flags defining in which context the code was generated */
  123. uint16_t size; /* size of target code for this block (1 <=
  124. size <= TARGET_PAGE_SIZE) */
  125. uint16_t cflags; /* compile flags */
  126. #define CF_COUNT_MASK 0x7fff
  127. #define CF_LAST_IO 0x8000 /* Last insn may be an IO access. */
  128. uint8_t *tc_ptr; /* pointer to the translated code */
  129. /* next matching tb for physical address. */
  130. struct TranslationBlock *phys_hash_next;
  131. /* first and second physical page containing code. The lower bit
  132. of the pointer tells the index in page_next[] */
  133. struct TranslationBlock *page_next[2];
  134. tb_page_addr_t page_addr[2];
  135. /* the following data are used to directly call another TB from
  136. the code of this one. */
  137. uint16_t tb_next_offset[2]; /* offset of original jump target */
  138. #ifdef USE_DIRECT_JUMP
  139. uint16_t tb_jmp_offset[2]; /* offset of jump instruction */
  140. #else
  141. uintptr_t tb_next[2]; /* address of jump generated code */
  142. #endif
  143. /* list of TBs jumping to this one. This is a circular list using
  144. the two least significant bits of the pointers to tell what is
  145. the next pointer: 0 = jmp_next[0], 1 = jmp_next[1], 2 =
  146. jmp_first */
  147. struct TranslationBlock *jmp_next[2];
  148. struct TranslationBlock *jmp_first;
  149. uint32_t icount;
  150. };
  151. static inline unsigned int tb_jmp_cache_hash_page(target_ulong pc)
  152. {
  153. target_ulong tmp;
  154. tmp = pc ^ (pc >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS));
  155. return (tmp >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS)) & TB_JMP_PAGE_MASK;
  156. }
  157. static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc)
  158. {
  159. target_ulong tmp;
  160. tmp = pc ^ (pc >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS));
  161. return (((tmp >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS)) & TB_JMP_PAGE_MASK)
  162. | (tmp & TB_JMP_ADDR_MASK));
  163. }
  164. static inline unsigned int tb_phys_hash_func(tb_page_addr_t pc)
  165. {
  166. return (pc >> 2) & (CODE_GEN_PHYS_HASH_SIZE - 1);
  167. }
  168. void tb_free(TranslationBlock *tb);
  169. void tb_flush(CPUArchState *env);
  170. void tb_link_page(TranslationBlock *tb,
  171. tb_page_addr_t phys_pc, tb_page_addr_t phys_page2);
  172. void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr);
  173. extern TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
  174. #if defined(USE_DIRECT_JUMP)
  175. #if defined(CONFIG_TCG_INTERPRETER)
  176. static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
  177. {
  178. /* patch the branch destination */
  179. *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
  180. /* no need to flush icache explicitly */
  181. }
  182. #elif defined(_ARCH_PPC)
  183. void ppc_tb_set_jmp_target(unsigned long jmp_addr, unsigned long addr);
  184. #define tb_set_jmp_target1 ppc_tb_set_jmp_target
  185. #elif defined(__i386__) || defined(__x86_64__)
  186. static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
  187. {
  188. /* patch the branch destination */
  189. *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
  190. /* no need to flush icache explicitly */
  191. }
  192. #elif defined(__arm__)
  193. static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
  194. {
  195. #if !QEMU_GNUC_PREREQ(4, 1)
  196. register unsigned long _beg __asm ("a1");
  197. register unsigned long _end __asm ("a2");
  198. register unsigned long _flg __asm ("a3");
  199. #endif
  200. /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the flush */
  201. *(uint32_t *)jmp_addr =
  202. (*(uint32_t *)jmp_addr & ~0xffffff)
  203. | (((addr - (jmp_addr + 8)) >> 2) & 0xffffff);
  204. #if QEMU_GNUC_PREREQ(4, 1)
  205. __builtin___clear_cache((char *) jmp_addr, (char *) jmp_addr + 4);
  206. #else
  207. /* flush icache */
  208. _beg = jmp_addr;
  209. _end = jmp_addr + 4;
  210. _flg = 0;
  211. __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg));
  212. #endif
  213. }
  214. #else
  215. #error tb_set_jmp_target1 is missing
  216. #endif
  217. static inline void tb_set_jmp_target(TranslationBlock *tb,
  218. int n, uintptr_t addr)
  219. {
  220. uint16_t offset = tb->tb_jmp_offset[n];
  221. tb_set_jmp_target1((uintptr_t)(tb->tc_ptr + offset), addr);
  222. }
  223. #else
  224. /* set the jump target */
  225. static inline void tb_set_jmp_target(TranslationBlock *tb,
  226. int n, uintptr_t addr)
  227. {
  228. tb->tb_next[n] = addr;
  229. }
  230. #endif
  231. static inline void tb_add_jump(TranslationBlock *tb, int n,
  232. TranslationBlock *tb_next)
  233. {
  234. /* NOTE: this test is only needed for thread safety */
  235. if (!tb->jmp_next[n]) {
  236. /* patch the native jump address */
  237. tb_set_jmp_target(tb, n, (uintptr_t)tb_next->tc_ptr);
  238. /* add in TB jmp circular list */
  239. tb->jmp_next[n] = tb_next->jmp_first;
  240. tb_next->jmp_first = (TranslationBlock *)((uintptr_t)(tb) | (n));
  241. }
  242. }
  243. TranslationBlock *tb_find_pc(uintptr_t pc_ptr);
  244. #include "qemu-lock.h"
  245. extern spinlock_t tb_lock;
  246. extern int tb_invalidated_flag;
  247. /* The return address may point to the start of the next instruction.
  248. Subtracting one gets us the call instruction itself. */
  249. #if defined(CONFIG_TCG_INTERPRETER)
  250. /* Alpha and SH4 user mode emulations and Softmmu call GETPC().
  251. For all others, GETPC remains undefined (which makes TCI a little faster. */
  252. # if defined(CONFIG_SOFTMMU) || defined(TARGET_ALPHA) || defined(TARGET_SH4)
  253. extern uintptr_t tci_tb_ptr;
  254. # define GETPC() tci_tb_ptr
  255. # endif
  256. #elif defined(__s390__) && !defined(__s390x__)
  257. # define GETPC() \
  258. (((uintptr_t)__builtin_return_address(0) & 0x7fffffffUL) - 1)
  259. #elif defined(__arm__)
  260. /* Thumb return addresses have the low bit set, so we need to subtract two.
  261. This is still safe in ARM mode because instructions are 4 bytes. */
  262. # define GETPC() ((uintptr_t)__builtin_return_address(0) - 2)
  263. #else
  264. # define GETPC() ((uintptr_t)__builtin_return_address(0) - 1)
  265. #endif
  266. #if !defined(CONFIG_USER_ONLY)
  267. struct MemoryRegion *iotlb_to_region(target_phys_addr_t index);
  268. uint64_t io_mem_read(struct MemoryRegion *mr, target_phys_addr_t addr,
  269. unsigned size);
  270. void io_mem_write(struct MemoryRegion *mr, target_phys_addr_t addr,
  271. uint64_t value, unsigned size);
  272. void tlb_fill(CPUArchState *env1, target_ulong addr, int is_write, int mmu_idx,
  273. uintptr_t retaddr);
  274. #include "softmmu_defs.h"
  275. #define ACCESS_TYPE (NB_MMU_MODES + 1)
  276. #define MEMSUFFIX _code
  277. #ifndef CONFIG_TCG_PASS_AREG0
  278. #define env cpu_single_env
  279. #endif
  280. #define DATA_SIZE 1
  281. #include "softmmu_header.h"
  282. #define DATA_SIZE 2
  283. #include "softmmu_header.h"
  284. #define DATA_SIZE 4
  285. #include "softmmu_header.h"
  286. #define DATA_SIZE 8
  287. #include "softmmu_header.h"
  288. #undef ACCESS_TYPE
  289. #undef MEMSUFFIX
  290. #undef env
  291. #endif
  292. #if defined(CONFIG_USER_ONLY)
  293. static inline tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)
  294. {
  295. return addr;
  296. }
  297. #else
  298. /* cputlb.c */
  299. tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr);
  300. #endif
  301. typedef void (CPUDebugExcpHandler)(CPUArchState *env);
  302. CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler);
  303. /* vl.c */
  304. extern int singlestep;
  305. /* cpu-exec.c */
  306. extern volatile sig_atomic_t exit_request;
  307. /* Deterministic execution requires that IO only be performed on the last
  308. instruction of a TB so that interrupts take effect immediately. */
  309. static inline int can_do_io(CPUArchState *env)
  310. {
  311. if (!use_icount) {
  312. return 1;
  313. }
  314. /* If not executing code then assume we are ok. */
  315. if (!env->current_tb) {
  316. return 1;
  317. }
  318. return env->can_do_io != 0;
  319. }
  320. #endif