exec-all.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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(CPUState *env, struct TranslationBlock *tb);
  66. void gen_intermediate_code_pc(CPUState *env, struct TranslationBlock *tb);
  67. void restore_state_to_opc(CPUState *env, struct TranslationBlock *tb,
  68. int pc_pos);
  69. void cpu_gen_init(void);
  70. int cpu_gen_code(CPUState *env, struct TranslationBlock *tb,
  71. int *gen_code_size_ptr);
  72. int cpu_restore_state(struct TranslationBlock *tb,
  73. CPUState *env, unsigned long searched_pc);
  74. void cpu_resume_from_signal(CPUState *env1, void *puc);
  75. void cpu_io_recompile(CPUState *env, void *retaddr);
  76. TranslationBlock *tb_gen_code(CPUState *env,
  77. target_ulong pc, target_ulong cs_base, int flags,
  78. int cflags);
  79. void cpu_exec_init(CPUState *env);
  80. void QEMU_NORETURN cpu_loop_exit(CPUState *env1);
  81. int page_unprotect(target_ulong address, unsigned long 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 tlb_flush_page(CPUState *env, target_ulong addr);
  85. void tlb_flush(CPUState *env, int flush_global);
  86. #if !defined(CONFIG_USER_ONLY)
  87. void tlb_set_page(CPUState *env, target_ulong vaddr,
  88. target_phys_addr_t paddr, int prot,
  89. int mmu_idx, target_ulong size);
  90. #endif
  91. #define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */
  92. #define CODE_GEN_PHYS_HASH_BITS 15
  93. #define CODE_GEN_PHYS_HASH_SIZE (1 << CODE_GEN_PHYS_HASH_BITS)
  94. #define MIN_CODE_GEN_BUFFER_SIZE (1024 * 1024)
  95. /* estimated block size for TB allocation */
  96. /* XXX: use a per code average code fragment size and modulate it
  97. according to the host CPU */
  98. #if defined(CONFIG_SOFTMMU)
  99. #define CODE_GEN_AVG_BLOCK_SIZE 128
  100. #else
  101. #define CODE_GEN_AVG_BLOCK_SIZE 64
  102. #endif
  103. #if defined(_ARCH_PPC) || defined(__x86_64__) || defined(__arm__) || defined(__i386__)
  104. #define USE_DIRECT_JUMP
  105. #elif defined(CONFIG_TCG_INTERPRETER)
  106. #define USE_DIRECT_JUMP
  107. #endif
  108. struct TranslationBlock {
  109. target_ulong pc; /* simulated PC corresponding to this block (EIP + CS base) */
  110. target_ulong cs_base; /* CS base for this block */
  111. uint64_t flags; /* flags defining in which context the code was generated */
  112. uint16_t size; /* size of target code for this block (1 <=
  113. size <= TARGET_PAGE_SIZE) */
  114. uint16_t cflags; /* compile flags */
  115. #define CF_COUNT_MASK 0x7fff
  116. #define CF_LAST_IO 0x8000 /* Last insn may be an IO access. */
  117. uint8_t *tc_ptr; /* pointer to the translated code */
  118. /* next matching tb for physical address. */
  119. struct TranslationBlock *phys_hash_next;
  120. /* first and second physical page containing code. The lower bit
  121. of the pointer tells the index in page_next[] */
  122. struct TranslationBlock *page_next[2];
  123. tb_page_addr_t page_addr[2];
  124. /* the following data are used to directly call another TB from
  125. the code of this one. */
  126. uint16_t tb_next_offset[2]; /* offset of original jump target */
  127. #ifdef USE_DIRECT_JUMP
  128. uint16_t tb_jmp_offset[2]; /* offset of jump instruction */
  129. #else
  130. unsigned long tb_next[2]; /* address of jump generated code */
  131. #endif
  132. /* list of TBs jumping to this one. This is a circular list using
  133. the two least significant bits of the pointers to tell what is
  134. the next pointer: 0 = jmp_next[0], 1 = jmp_next[1], 2 =
  135. jmp_first */
  136. struct TranslationBlock *jmp_next[2];
  137. struct TranslationBlock *jmp_first;
  138. uint32_t icount;
  139. };
  140. static inline unsigned int tb_jmp_cache_hash_page(target_ulong pc)
  141. {
  142. target_ulong tmp;
  143. tmp = pc ^ (pc >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS));
  144. return (tmp >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS)) & TB_JMP_PAGE_MASK;
  145. }
  146. static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc)
  147. {
  148. target_ulong tmp;
  149. tmp = pc ^ (pc >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS));
  150. return (((tmp >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS)) & TB_JMP_PAGE_MASK)
  151. | (tmp & TB_JMP_ADDR_MASK));
  152. }
  153. static inline unsigned int tb_phys_hash_func(tb_page_addr_t pc)
  154. {
  155. return (pc >> 2) & (CODE_GEN_PHYS_HASH_SIZE - 1);
  156. }
  157. void tb_free(TranslationBlock *tb);
  158. void tb_flush(CPUState *env);
  159. void tb_link_page(TranslationBlock *tb,
  160. tb_page_addr_t phys_pc, tb_page_addr_t phys_page2);
  161. void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr);
  162. extern TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
  163. #if defined(USE_DIRECT_JUMP)
  164. #if defined(CONFIG_TCG_INTERPRETER)
  165. static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
  166. {
  167. /* patch the branch destination */
  168. *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
  169. /* no need to flush icache explicitly */
  170. }
  171. #elif defined(_ARCH_PPC)
  172. void ppc_tb_set_jmp_target(unsigned long jmp_addr, unsigned long addr);
  173. #define tb_set_jmp_target1 ppc_tb_set_jmp_target
  174. #elif defined(__i386__) || defined(__x86_64__)
  175. static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr)
  176. {
  177. /* patch the branch destination */
  178. *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
  179. /* no need to flush icache explicitly */
  180. }
  181. #elif defined(__arm__)
  182. static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr)
  183. {
  184. #if !QEMU_GNUC_PREREQ(4, 1)
  185. register unsigned long _beg __asm ("a1");
  186. register unsigned long _end __asm ("a2");
  187. register unsigned long _flg __asm ("a3");
  188. #endif
  189. /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the flush */
  190. *(uint32_t *)jmp_addr =
  191. (*(uint32_t *)jmp_addr & ~0xffffff)
  192. | (((addr - (jmp_addr + 8)) >> 2) & 0xffffff);
  193. #if QEMU_GNUC_PREREQ(4, 1)
  194. __builtin___clear_cache((char *) jmp_addr, (char *) jmp_addr + 4);
  195. #else
  196. /* flush icache */
  197. _beg = jmp_addr;
  198. _end = jmp_addr + 4;
  199. _flg = 0;
  200. __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg));
  201. #endif
  202. }
  203. #else
  204. #error tb_set_jmp_target1 is missing
  205. #endif
  206. static inline void tb_set_jmp_target(TranslationBlock *tb,
  207. int n, unsigned long addr)
  208. {
  209. unsigned long offset;
  210. offset = tb->tb_jmp_offset[n];
  211. tb_set_jmp_target1((unsigned long)(tb->tc_ptr + offset), addr);
  212. }
  213. #else
  214. /* set the jump target */
  215. static inline void tb_set_jmp_target(TranslationBlock *tb,
  216. int n, unsigned long addr)
  217. {
  218. tb->tb_next[n] = addr;
  219. }
  220. #endif
  221. static inline void tb_add_jump(TranslationBlock *tb, int n,
  222. TranslationBlock *tb_next)
  223. {
  224. /* NOTE: this test is only needed for thread safety */
  225. if (!tb->jmp_next[n]) {
  226. /* patch the native jump address */
  227. tb_set_jmp_target(tb, n, (unsigned long)tb_next->tc_ptr);
  228. /* add in TB jmp circular list */
  229. tb->jmp_next[n] = tb_next->jmp_first;
  230. tb_next->jmp_first = (TranslationBlock *)((long)(tb) | (n));
  231. }
  232. }
  233. TranslationBlock *tb_find_pc(unsigned long pc_ptr);
  234. #include "qemu-lock.h"
  235. extern spinlock_t tb_lock;
  236. extern int tb_invalidated_flag;
  237. /* The return address may point to the start of the next instruction.
  238. Subtracting one gets us the call instruction itself. */
  239. #if defined(CONFIG_TCG_INTERPRETER)
  240. /* Alpha and SH4 user mode emulations and Softmmu call GETPC().
  241. For all others, GETPC remains undefined (which makes TCI a little faster. */
  242. # if defined(CONFIG_SOFTMMU) || defined(TARGET_ALPHA) || defined(TARGET_SH4)
  243. extern void *tci_tb_ptr;
  244. # define GETPC() tci_tb_ptr
  245. # endif
  246. #elif defined(__s390__) && !defined(__s390x__)
  247. # define GETPC() ((void*)(((unsigned long)__builtin_return_address(0) & 0x7fffffffUL) - 1))
  248. #elif defined(__arm__)
  249. /* Thumb return addresses have the low bit set, so we need to subtract two.
  250. This is still safe in ARM mode because instructions are 4 bytes. */
  251. # define GETPC() ((void *)((unsigned long)__builtin_return_address(0) - 2))
  252. #else
  253. # define GETPC() ((void *)((unsigned long)__builtin_return_address(0) - 1))
  254. #endif
  255. #if !defined(CONFIG_USER_ONLY)
  256. extern CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
  257. extern CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
  258. extern void *io_mem_opaque[IO_MEM_NB_ENTRIES];
  259. void tlb_fill(CPUState *env1, target_ulong addr, int is_write, int mmu_idx,
  260. void *retaddr);
  261. #include "softmmu_defs.h"
  262. #define ACCESS_TYPE (NB_MMU_MODES + 1)
  263. #define MEMSUFFIX _code
  264. #define env cpu_single_env
  265. #define DATA_SIZE 1
  266. #include "softmmu_header.h"
  267. #define DATA_SIZE 2
  268. #include "softmmu_header.h"
  269. #define DATA_SIZE 4
  270. #include "softmmu_header.h"
  271. #define DATA_SIZE 8
  272. #include "softmmu_header.h"
  273. #undef ACCESS_TYPE
  274. #undef MEMSUFFIX
  275. #undef env
  276. #endif
  277. #if defined(CONFIG_USER_ONLY)
  278. static inline tb_page_addr_t get_page_addr_code(CPUState *env1, target_ulong addr)
  279. {
  280. return addr;
  281. }
  282. #else
  283. /* NOTE: this function can trigger an exception */
  284. /* NOTE2: the returned address is not exactly the physical address: it
  285. is the offset relative to phys_ram_base */
  286. static inline tb_page_addr_t get_page_addr_code(CPUState *env1, target_ulong addr)
  287. {
  288. int mmu_idx, page_index, pd;
  289. void *p;
  290. page_index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  291. mmu_idx = cpu_mmu_index(env1);
  292. if (unlikely(env1->tlb_table[mmu_idx][page_index].addr_code !=
  293. (addr & TARGET_PAGE_MASK))) {
  294. ldub_code(addr);
  295. }
  296. pd = env1->tlb_table[mmu_idx][page_index].addr_code & ~TARGET_PAGE_MASK;
  297. if (pd > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
  298. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_SPARC)
  299. cpu_unassigned_access(env1, addr, 0, 1, 0, 4);
  300. #else
  301. cpu_abort(env1, "Trying to execute code outside RAM or ROM at 0x" TARGET_FMT_lx "\n", addr);
  302. #endif
  303. }
  304. p = (void *)((uintptr_t)addr + env1->tlb_table[mmu_idx][page_index].addend);
  305. return qemu_ram_addr_from_host_nofail(p);
  306. }
  307. #endif
  308. typedef void (CPUDebugExcpHandler)(CPUState *env);
  309. CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler);
  310. /* vl.c */
  311. extern int singlestep;
  312. /* cpu-exec.c */
  313. extern volatile sig_atomic_t exit_request;
  314. /* Deterministic execution requires that IO only be performed on the last
  315. instruction of a TB so that interrupts take effect immediately. */
  316. static inline int can_do_io(CPUState *env)
  317. {
  318. if (!use_icount) {
  319. return 1;
  320. }
  321. /* If not executing code then assume we are ok. */
  322. if (!env->current_tb) {
  323. return 1;
  324. }
  325. return env->can_do_io != 0;
  326. }
  327. #endif