exec-all.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #endif
  106. struct TranslationBlock {
  107. target_ulong pc; /* simulated PC corresponding to this block (EIP + CS base) */
  108. target_ulong cs_base; /* CS base for this block */
  109. uint64_t flags; /* flags defining in which context the code was generated */
  110. uint16_t size; /* size of target code for this block (1 <=
  111. size <= TARGET_PAGE_SIZE) */
  112. uint16_t cflags; /* compile flags */
  113. #define CF_COUNT_MASK 0x7fff
  114. #define CF_LAST_IO 0x8000 /* Last insn may be an IO access. */
  115. uint8_t *tc_ptr; /* pointer to the translated code */
  116. /* next matching tb for physical address. */
  117. struct TranslationBlock *phys_hash_next;
  118. /* first and second physical page containing code. The lower bit
  119. of the pointer tells the index in page_next[] */
  120. struct TranslationBlock *page_next[2];
  121. tb_page_addr_t page_addr[2];
  122. /* the following data are used to directly call another TB from
  123. the code of this one. */
  124. uint16_t tb_next_offset[2]; /* offset of original jump target */
  125. #ifdef USE_DIRECT_JUMP
  126. uint16_t tb_jmp_offset[2]; /* offset of jump instruction */
  127. #else
  128. unsigned long tb_next[2]; /* address of jump generated code */
  129. #endif
  130. /* list of TBs jumping to this one. This is a circular list using
  131. the two least significant bits of the pointers to tell what is
  132. the next pointer: 0 = jmp_next[0], 1 = jmp_next[1], 2 =
  133. jmp_first */
  134. struct TranslationBlock *jmp_next[2];
  135. struct TranslationBlock *jmp_first;
  136. uint32_t icount;
  137. };
  138. static inline unsigned int tb_jmp_cache_hash_page(target_ulong pc)
  139. {
  140. target_ulong tmp;
  141. tmp = pc ^ (pc >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS));
  142. return (tmp >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS)) & TB_JMP_PAGE_MASK;
  143. }
  144. static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc)
  145. {
  146. target_ulong tmp;
  147. tmp = pc ^ (pc >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS));
  148. return (((tmp >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS)) & TB_JMP_PAGE_MASK)
  149. | (tmp & TB_JMP_ADDR_MASK));
  150. }
  151. static inline unsigned int tb_phys_hash_func(tb_page_addr_t pc)
  152. {
  153. return (pc >> 2) & (CODE_GEN_PHYS_HASH_SIZE - 1);
  154. }
  155. void tb_free(TranslationBlock *tb);
  156. void tb_flush(CPUState *env);
  157. void tb_link_page(TranslationBlock *tb,
  158. tb_page_addr_t phys_pc, tb_page_addr_t phys_page2);
  159. void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr);
  160. extern TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
  161. #if defined(USE_DIRECT_JUMP)
  162. #if defined(_ARCH_PPC)
  163. void ppc_tb_set_jmp_target(unsigned long jmp_addr, unsigned long addr);
  164. #define tb_set_jmp_target1 ppc_tb_set_jmp_target
  165. #elif defined(__i386__) || defined(__x86_64__)
  166. static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr)
  167. {
  168. /* patch the branch destination */
  169. *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
  170. /* no need to flush icache explicitly */
  171. }
  172. #elif defined(__arm__)
  173. static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr)
  174. {
  175. #if !QEMU_GNUC_PREREQ(4, 1)
  176. register unsigned long _beg __asm ("a1");
  177. register unsigned long _end __asm ("a2");
  178. register unsigned long _flg __asm ("a3");
  179. #endif
  180. /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the flush */
  181. *(uint32_t *)jmp_addr =
  182. (*(uint32_t *)jmp_addr & ~0xffffff)
  183. | (((addr - (jmp_addr + 8)) >> 2) & 0xffffff);
  184. #if QEMU_GNUC_PREREQ(4, 1)
  185. __builtin___clear_cache((char *) jmp_addr, (char *) jmp_addr + 4);
  186. #else
  187. /* flush icache */
  188. _beg = jmp_addr;
  189. _end = jmp_addr + 4;
  190. _flg = 0;
  191. __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg));
  192. #endif
  193. }
  194. #endif
  195. static inline void tb_set_jmp_target(TranslationBlock *tb,
  196. int n, unsigned long addr)
  197. {
  198. unsigned long offset;
  199. offset = tb->tb_jmp_offset[n];
  200. tb_set_jmp_target1((unsigned long)(tb->tc_ptr + offset), addr);
  201. }
  202. #else
  203. /* set the jump target */
  204. static inline void tb_set_jmp_target(TranslationBlock *tb,
  205. int n, unsigned long addr)
  206. {
  207. tb->tb_next[n] = addr;
  208. }
  209. #endif
  210. static inline void tb_add_jump(TranslationBlock *tb, int n,
  211. TranslationBlock *tb_next)
  212. {
  213. /* NOTE: this test is only needed for thread safety */
  214. if (!tb->jmp_next[n]) {
  215. /* patch the native jump address */
  216. tb_set_jmp_target(tb, n, (unsigned long)tb_next->tc_ptr);
  217. /* add in TB jmp circular list */
  218. tb->jmp_next[n] = tb_next->jmp_first;
  219. tb_next->jmp_first = (TranslationBlock *)((long)(tb) | (n));
  220. }
  221. }
  222. TranslationBlock *tb_find_pc(unsigned long pc_ptr);
  223. #include "qemu-lock.h"
  224. extern spinlock_t tb_lock;
  225. extern int tb_invalidated_flag;
  226. #if !defined(CONFIG_USER_ONLY)
  227. extern CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
  228. extern CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
  229. extern void *io_mem_opaque[IO_MEM_NB_ENTRIES];
  230. void tlb_fill(target_ulong addr, int is_write, int mmu_idx,
  231. void *retaddr);
  232. #include "softmmu_defs.h"
  233. #define ACCESS_TYPE (NB_MMU_MODES + 1)
  234. #define MEMSUFFIX _code
  235. #define env cpu_single_env
  236. #define DATA_SIZE 1
  237. #include "softmmu_header.h"
  238. #define DATA_SIZE 2
  239. #include "softmmu_header.h"
  240. #define DATA_SIZE 4
  241. #include "softmmu_header.h"
  242. #define DATA_SIZE 8
  243. #include "softmmu_header.h"
  244. #undef ACCESS_TYPE
  245. #undef MEMSUFFIX
  246. #undef env
  247. #endif
  248. #if defined(CONFIG_USER_ONLY)
  249. static inline tb_page_addr_t get_page_addr_code(CPUState *env1, target_ulong addr)
  250. {
  251. return addr;
  252. }
  253. #else
  254. /* NOTE: this function can trigger an exception */
  255. /* NOTE2: the returned address is not exactly the physical address: it
  256. is the offset relative to phys_ram_base */
  257. static inline tb_page_addr_t get_page_addr_code(CPUState *env1, target_ulong addr)
  258. {
  259. int mmu_idx, page_index, pd;
  260. void *p;
  261. page_index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  262. mmu_idx = cpu_mmu_index(env1);
  263. if (unlikely(env1->tlb_table[mmu_idx][page_index].addr_code !=
  264. (addr & TARGET_PAGE_MASK))) {
  265. ldub_code(addr);
  266. }
  267. pd = env1->tlb_table[mmu_idx][page_index].addr_code & ~TARGET_PAGE_MASK;
  268. if (pd > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
  269. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_SPARC)
  270. cpu_unassigned_access(env1, addr, 0, 1, 0, 4);
  271. #else
  272. cpu_abort(env1, "Trying to execute code outside RAM or ROM at 0x" TARGET_FMT_lx "\n", addr);
  273. #endif
  274. }
  275. p = (void *)(unsigned long)addr
  276. + env1->tlb_table[mmu_idx][page_index].addend;
  277. return qemu_ram_addr_from_host_nofail(p);
  278. }
  279. #endif
  280. typedef void (CPUDebugExcpHandler)(CPUState *env);
  281. CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler);
  282. /* vl.c */
  283. extern int singlestep;
  284. /* cpu-exec.c */
  285. extern volatile sig_atomic_t exit_request;
  286. #endif