exec-all.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
  19. */
  20. #ifndef _EXEC_ALL_H_
  21. #define _EXEC_ALL_H_
  22. #include "qemu-common.h"
  23. /* allow to see translation results - the slowdown should be negligible, so we leave it */
  24. #define DEBUG_DISAS
  25. /* is_jmp field values */
  26. #define DISAS_NEXT 0 /* next instruction can be analyzed */
  27. #define DISAS_JUMP 1 /* only pc was modified dynamically */
  28. #define DISAS_UPDATE 2 /* cpu state was modified dynamically */
  29. #define DISAS_TB_JUMP 3 /* only pc was modified statically */
  30. typedef struct TranslationBlock TranslationBlock;
  31. /* XXX: make safe guess about sizes */
  32. #define MAX_OP_PER_INSTR 96
  33. /* A Call op needs up to 6 + 2N parameters (N = number of arguments). */
  34. #define MAX_OPC_PARAM 10
  35. #define OPC_BUF_SIZE 512
  36. #define OPC_MAX_SIZE (OPC_BUF_SIZE - MAX_OP_PER_INSTR)
  37. /* Maximum size a TCG op can expand to. This is complicated because a
  38. single op may require several host instructions and regirster reloads.
  39. For now take a wild guess at 128 bytes, which should allow at least
  40. a couple of fixup instructions per argument. */
  41. #define TCG_MAX_OP_SIZE 128
  42. #define OPPARAM_BUF_SIZE (OPC_BUF_SIZE * MAX_OPC_PARAM)
  43. extern target_ulong gen_opc_pc[OPC_BUF_SIZE];
  44. extern target_ulong gen_opc_npc[OPC_BUF_SIZE];
  45. extern uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
  46. extern uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
  47. extern uint16_t gen_opc_icount[OPC_BUF_SIZE];
  48. extern target_ulong gen_opc_jump_pc[2];
  49. extern uint32_t gen_opc_hflags[OPC_BUF_SIZE];
  50. #include "qemu-log.h"
  51. void gen_intermediate_code(CPUState *env, struct TranslationBlock *tb);
  52. void gen_intermediate_code_pc(CPUState *env, struct TranslationBlock *tb);
  53. void gen_pc_load(CPUState *env, struct TranslationBlock *tb,
  54. unsigned long searched_pc, int pc_pos, void *puc);
  55. unsigned long code_gen_max_block_size(void);
  56. void cpu_gen_init(void);
  57. int cpu_gen_code(CPUState *env, struct TranslationBlock *tb,
  58. int *gen_code_size_ptr);
  59. int cpu_restore_state(struct TranslationBlock *tb,
  60. CPUState *env, unsigned long searched_pc,
  61. void *puc);
  62. int cpu_restore_state_copy(struct TranslationBlock *tb,
  63. CPUState *env, unsigned long searched_pc,
  64. void *puc);
  65. void cpu_resume_from_signal(CPUState *env1, void *puc);
  66. void cpu_io_recompile(CPUState *env, void *retaddr);
  67. TranslationBlock *tb_gen_code(CPUState *env,
  68. target_ulong pc, target_ulong cs_base, int flags,
  69. int cflags);
  70. void cpu_exec_init(CPUState *env);
  71. void QEMU_NORETURN cpu_loop_exit(void);
  72. int page_unprotect(target_ulong address, unsigned long pc, void *puc);
  73. void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
  74. int is_cpu_write_access);
  75. void tb_invalidate_page_range(target_ulong start, target_ulong end);
  76. void tlb_flush_page(CPUState *env, target_ulong addr);
  77. void tlb_flush(CPUState *env, int flush_global);
  78. int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
  79. target_phys_addr_t paddr, int prot,
  80. int mmu_idx, int is_softmmu);
  81. static inline int tlb_set_page(CPUState *env1, target_ulong vaddr,
  82. target_phys_addr_t paddr, int prot,
  83. int mmu_idx, int is_softmmu)
  84. {
  85. if (prot & PAGE_READ)
  86. prot |= PAGE_EXEC;
  87. return tlb_set_page_exec(env1, vaddr, paddr, prot, mmu_idx, is_softmmu);
  88. }
  89. #define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */
  90. #define CODE_GEN_PHYS_HASH_BITS 15
  91. #define CODE_GEN_PHYS_HASH_SIZE (1 << CODE_GEN_PHYS_HASH_BITS)
  92. #define MIN_CODE_GEN_BUFFER_SIZE (1024 * 1024)
  93. /* estimated block size for TB allocation */
  94. /* XXX: use a per code average code fragment size and modulate it
  95. according to the host CPU */
  96. #if defined(CONFIG_SOFTMMU)
  97. #define CODE_GEN_AVG_BLOCK_SIZE 128
  98. #else
  99. #define CODE_GEN_AVG_BLOCK_SIZE 64
  100. #endif
  101. #if defined(_ARCH_PPC) || defined(__x86_64__) || defined(__arm__)
  102. #define USE_DIRECT_JUMP
  103. #endif
  104. #if defined(__i386__) && !defined(_WIN32)
  105. #define USE_DIRECT_JUMP
  106. #endif
  107. struct TranslationBlock {
  108. target_ulong pc; /* simulated PC corresponding to this block (EIP + CS base) */
  109. target_ulong cs_base; /* CS base for this block */
  110. uint64_t flags; /* flags defining in which context the code was generated */
  111. uint16_t size; /* size of target code for this block (1 <=
  112. size <= TARGET_PAGE_SIZE) */
  113. uint16_t cflags; /* compile flags */
  114. #define CF_COUNT_MASK 0x7fff
  115. #define CF_LAST_IO 0x8000 /* Last insn may be an IO access. */
  116. uint8_t *tc_ptr; /* pointer to the translated code */
  117. /* next matching tb for physical address. */
  118. struct TranslationBlock *phys_hash_next;
  119. /* first and second physical page containing code. The lower bit
  120. of the pointer tells the index in page_next[] */
  121. struct TranslationBlock *page_next[2];
  122. target_ulong page_addr[2];
  123. /* the following data are used to directly call another TB from
  124. the code of this one. */
  125. uint16_t tb_next_offset[2]; /* offset of original jump target */
  126. #ifdef USE_DIRECT_JUMP
  127. uint16_t tb_jmp_offset[4]; /* offset of jump instruction */
  128. #else
  129. unsigned long tb_next[2]; /* address of jump generated code */
  130. #endif
  131. /* list of TBs jumping to this one. This is a circular list using
  132. the two least significant bits of the pointers to tell what is
  133. the next pointer: 0 = jmp_next[0], 1 = jmp_next[1], 2 =
  134. jmp_first */
  135. struct TranslationBlock *jmp_next[2];
  136. struct TranslationBlock *jmp_first;
  137. uint32_t icount;
  138. };
  139. static inline unsigned int tb_jmp_cache_hash_page(target_ulong pc)
  140. {
  141. target_ulong tmp;
  142. tmp = pc ^ (pc >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS));
  143. return (tmp >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS)) & TB_JMP_PAGE_MASK;
  144. }
  145. static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc)
  146. {
  147. target_ulong tmp;
  148. tmp = pc ^ (pc >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS));
  149. return (((tmp >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS)) & TB_JMP_PAGE_MASK)
  150. | (tmp & TB_JMP_ADDR_MASK));
  151. }
  152. static inline unsigned int tb_phys_hash_func(unsigned long pc)
  153. {
  154. return pc & (CODE_GEN_PHYS_HASH_SIZE - 1);
  155. }
  156. TranslationBlock *tb_alloc(target_ulong pc);
  157. void tb_free(TranslationBlock *tb);
  158. void tb_flush(CPUState *env);
  159. void tb_link_phys(TranslationBlock *tb,
  160. target_ulong phys_pc, target_ulong phys_page2);
  161. void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr);
  162. extern TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
  163. extern uint8_t *code_gen_ptr;
  164. extern int code_gen_max_blocks;
  165. #if defined(USE_DIRECT_JUMP)
  166. #if defined(_ARCH_PPC)
  167. extern void ppc_tb_set_jmp_target(unsigned long jmp_addr, unsigned long addr);
  168. #define tb_set_jmp_target1 ppc_tb_set_jmp_target
  169. #elif defined(__i386__) || defined(__x86_64__)
  170. static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr)
  171. {
  172. /* patch the branch destination */
  173. *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
  174. /* no need to flush icache explicitly */
  175. }
  176. #elif defined(__arm__)
  177. static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr)
  178. {
  179. #if QEMU_GNUC_PREREQ(4, 1)
  180. void __clear_cache(char *beg, char *end);
  181. #else
  182. register unsigned long _beg __asm ("a1");
  183. register unsigned long _end __asm ("a2");
  184. register unsigned long _flg __asm ("a3");
  185. #endif
  186. /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the flush */
  187. *(uint32_t *)jmp_addr |= ((addr - (jmp_addr + 8)) >> 2) & 0xffffff;
  188. #if QEMU_GNUC_PREREQ(4, 1)
  189. __clear_cache((char *) jmp_addr, (char *) jmp_addr + 4);
  190. #else
  191. /* flush icache */
  192. _beg = jmp_addr;
  193. _end = jmp_addr + 4;
  194. _flg = 0;
  195. __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg));
  196. #endif
  197. }
  198. #endif
  199. static inline void tb_set_jmp_target(TranslationBlock *tb,
  200. int n, unsigned long addr)
  201. {
  202. unsigned long offset;
  203. offset = tb->tb_jmp_offset[n];
  204. tb_set_jmp_target1((unsigned long)(tb->tc_ptr + offset), addr);
  205. offset = tb->tb_jmp_offset[n + 2];
  206. if (offset != 0xffff)
  207. tb_set_jmp_target1((unsigned long)(tb->tc_ptr + offset), addr);
  208. }
  209. #else
  210. /* set the jump target */
  211. static inline void tb_set_jmp_target(TranslationBlock *tb,
  212. int n, unsigned long addr)
  213. {
  214. tb->tb_next[n] = addr;
  215. }
  216. #endif
  217. static inline void tb_add_jump(TranslationBlock *tb, int n,
  218. TranslationBlock *tb_next)
  219. {
  220. /* NOTE: this test is only needed for thread safety */
  221. if (!tb->jmp_next[n]) {
  222. /* patch the native jump address */
  223. tb_set_jmp_target(tb, n, (unsigned long)tb_next->tc_ptr);
  224. /* add in TB jmp circular list */
  225. tb->jmp_next[n] = tb_next->jmp_first;
  226. tb_next->jmp_first = (TranslationBlock *)((long)(tb) | (n));
  227. }
  228. }
  229. TranslationBlock *tb_find_pc(unsigned long pc_ptr);
  230. extern CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
  231. extern CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
  232. extern void *io_mem_opaque[IO_MEM_NB_ENTRIES];
  233. #include "qemu-lock.h"
  234. extern spinlock_t tb_lock;
  235. extern int tb_invalidated_flag;
  236. #if !defined(CONFIG_USER_ONLY)
  237. void tlb_fill(target_ulong addr, int is_write, int mmu_idx,
  238. void *retaddr);
  239. #include "softmmu_defs.h"
  240. #define ACCESS_TYPE (NB_MMU_MODES + 1)
  241. #define MEMSUFFIX _code
  242. #define env cpu_single_env
  243. #define DATA_SIZE 1
  244. #include "softmmu_header.h"
  245. #define DATA_SIZE 2
  246. #include "softmmu_header.h"
  247. #define DATA_SIZE 4
  248. #include "softmmu_header.h"
  249. #define DATA_SIZE 8
  250. #include "softmmu_header.h"
  251. #undef ACCESS_TYPE
  252. #undef MEMSUFFIX
  253. #undef env
  254. #endif
  255. #if defined(CONFIG_USER_ONLY)
  256. static inline target_ulong get_phys_addr_code(CPUState *env1, target_ulong addr)
  257. {
  258. return addr;
  259. }
  260. #else
  261. /* NOTE: this function can trigger an exception */
  262. /* NOTE2: the returned address is not exactly the physical address: it
  263. is the offset relative to phys_ram_base */
  264. static inline target_ulong get_phys_addr_code(CPUState *env1, target_ulong addr)
  265. {
  266. int mmu_idx, page_index, pd;
  267. page_index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  268. mmu_idx = cpu_mmu_index(env1);
  269. if (unlikely(env1->tlb_table[mmu_idx][page_index].addr_code !=
  270. (addr & TARGET_PAGE_MASK))) {
  271. ldub_code(addr);
  272. }
  273. pd = env1->tlb_table[mmu_idx][page_index].addr_code & ~TARGET_PAGE_MASK;
  274. if (pd > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
  275. #if defined(TARGET_SPARC) || defined(TARGET_MIPS)
  276. do_unassigned_access(addr, 0, 1, 0, 4);
  277. #else
  278. cpu_abort(env1, "Trying to execute code outside RAM or ROM at 0x" TARGET_FMT_lx "\n", addr);
  279. #endif
  280. }
  281. return addr + env1->tlb_table[mmu_idx][page_index].addend - (unsigned long)phys_ram_base;
  282. }
  283. /* Deterministic execution requires that IO only be performed on the last
  284. instruction of a TB so that interrupts take effect immediately. */
  285. static inline int can_do_io(CPUState *env)
  286. {
  287. if (!use_icount)
  288. return 1;
  289. /* If not executing code then assume we are ok. */
  290. if (!env->current_tb)
  291. return 1;
  292. return env->can_do_io != 0;
  293. }
  294. #endif
  295. #ifdef USE_KQEMU
  296. #define KQEMU_MODIFY_PAGE_MASK (0xff & ~(VGA_DIRTY_FLAG | CODE_DIRTY_FLAG))
  297. #define MSR_QPI_COMMBASE 0xfabe0010
  298. int kqemu_init(CPUState *env);
  299. int kqemu_cpu_exec(CPUState *env);
  300. void kqemu_flush_page(CPUState *env, target_ulong addr);
  301. void kqemu_flush(CPUState *env, int global);
  302. void kqemu_set_notdirty(CPUState *env, ram_addr_t ram_addr);
  303. void kqemu_modify_page(CPUState *env, ram_addr_t ram_addr);
  304. void kqemu_set_phys_mem(uint64_t start_addr, ram_addr_t size,
  305. ram_addr_t phys_offset);
  306. void kqemu_cpu_interrupt(CPUState *env);
  307. void kqemu_record_dump(void);
  308. extern uint32_t kqemu_comm_base;
  309. static inline int kqemu_is_ok(CPUState *env)
  310. {
  311. return(env->kqemu_enabled &&
  312. (env->cr[0] & CR0_PE_MASK) &&
  313. !(env->hflags & HF_INHIBIT_IRQ_MASK) &&
  314. (env->eflags & IF_MASK) &&
  315. !(env->eflags & VM_MASK) &&
  316. (env->kqemu_enabled == 2 ||
  317. ((env->hflags & HF_CPL_MASK) == 3 &&
  318. (env->eflags & IOPL_MASK) != IOPL_MASK)));
  319. }
  320. #endif
  321. typedef void (CPUDebugExcpHandler)(CPUState *env);
  322. CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler);
  323. #endif