cpu-all.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * defines common to all virtual CPUs
  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 CPU_ALL_H
  20. #define CPU_ALL_H
  21. #include "qemu-common.h"
  22. #include "qemu-tls.h"
  23. #include "cpu-common.h"
  24. /* some important defines:
  25. *
  26. * WORDS_ALIGNED : if defined, the host cpu can only make word aligned
  27. * memory accesses.
  28. *
  29. * HOST_WORDS_BIGENDIAN : if defined, the host cpu is big endian and
  30. * otherwise little endian.
  31. *
  32. * (TARGET_WORDS_ALIGNED : same for target cpu (not supported yet))
  33. *
  34. * TARGET_WORDS_BIGENDIAN : same for target cpu
  35. */
  36. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  37. #define BSWAP_NEEDED
  38. #endif
  39. #ifdef BSWAP_NEEDED
  40. static inline uint16_t tswap16(uint16_t s)
  41. {
  42. return bswap16(s);
  43. }
  44. static inline uint32_t tswap32(uint32_t s)
  45. {
  46. return bswap32(s);
  47. }
  48. static inline uint64_t tswap64(uint64_t s)
  49. {
  50. return bswap64(s);
  51. }
  52. static inline void tswap16s(uint16_t *s)
  53. {
  54. *s = bswap16(*s);
  55. }
  56. static inline void tswap32s(uint32_t *s)
  57. {
  58. *s = bswap32(*s);
  59. }
  60. static inline void tswap64s(uint64_t *s)
  61. {
  62. *s = bswap64(*s);
  63. }
  64. #else
  65. static inline uint16_t tswap16(uint16_t s)
  66. {
  67. return s;
  68. }
  69. static inline uint32_t tswap32(uint32_t s)
  70. {
  71. return s;
  72. }
  73. static inline uint64_t tswap64(uint64_t s)
  74. {
  75. return s;
  76. }
  77. static inline void tswap16s(uint16_t *s)
  78. {
  79. }
  80. static inline void tswap32s(uint32_t *s)
  81. {
  82. }
  83. static inline void tswap64s(uint64_t *s)
  84. {
  85. }
  86. #endif
  87. #if TARGET_LONG_SIZE == 4
  88. #define tswapl(s) tswap32(s)
  89. #define tswapls(s) tswap32s((uint32_t *)(s))
  90. #define bswaptls(s) bswap32s(s)
  91. #else
  92. #define tswapl(s) tswap64(s)
  93. #define tswapls(s) tswap64s((uint64_t *)(s))
  94. #define bswaptls(s) bswap64s(s)
  95. #endif
  96. /* CPU memory access without any memory or io remapping */
  97. /*
  98. * the generic syntax for the memory accesses is:
  99. *
  100. * load: ld{type}{sign}{size}{endian}_{access_type}(ptr)
  101. *
  102. * store: st{type}{size}{endian}_{access_type}(ptr, val)
  103. *
  104. * type is:
  105. * (empty): integer access
  106. * f : float access
  107. *
  108. * sign is:
  109. * (empty): for floats or 32 bit size
  110. * u : unsigned
  111. * s : signed
  112. *
  113. * size is:
  114. * b: 8 bits
  115. * w: 16 bits
  116. * l: 32 bits
  117. * q: 64 bits
  118. *
  119. * endian is:
  120. * (empty): target cpu endianness or 8 bit access
  121. * r : reversed target cpu endianness (not implemented yet)
  122. * be : big endian (not implemented yet)
  123. * le : little endian (not implemented yet)
  124. *
  125. * access_type is:
  126. * raw : host memory access
  127. * user : user mode access using soft MMU
  128. * kernel : kernel mode access using soft MMU
  129. */
  130. /* target-endianness CPU memory access functions */
  131. #if defined(TARGET_WORDS_BIGENDIAN)
  132. #define lduw_p(p) lduw_be_p(p)
  133. #define ldsw_p(p) ldsw_be_p(p)
  134. #define ldl_p(p) ldl_be_p(p)
  135. #define ldq_p(p) ldq_be_p(p)
  136. #define ldfl_p(p) ldfl_be_p(p)
  137. #define ldfq_p(p) ldfq_be_p(p)
  138. #define stw_p(p, v) stw_be_p(p, v)
  139. #define stl_p(p, v) stl_be_p(p, v)
  140. #define stq_p(p, v) stq_be_p(p, v)
  141. #define stfl_p(p, v) stfl_be_p(p, v)
  142. #define stfq_p(p, v) stfq_be_p(p, v)
  143. #else
  144. #define lduw_p(p) lduw_le_p(p)
  145. #define ldsw_p(p) ldsw_le_p(p)
  146. #define ldl_p(p) ldl_le_p(p)
  147. #define ldq_p(p) ldq_le_p(p)
  148. #define ldfl_p(p) ldfl_le_p(p)
  149. #define ldfq_p(p) ldfq_le_p(p)
  150. #define stw_p(p, v) stw_le_p(p, v)
  151. #define stl_p(p, v) stl_le_p(p, v)
  152. #define stq_p(p, v) stq_le_p(p, v)
  153. #define stfl_p(p, v) stfl_le_p(p, v)
  154. #define stfq_p(p, v) stfq_le_p(p, v)
  155. #endif
  156. /* MMU memory access macros */
  157. #if defined(CONFIG_USER_ONLY)
  158. #include <assert.h>
  159. #include "qemu-types.h"
  160. /* On some host systems the guest address space is reserved on the host.
  161. * This allows the guest address space to be offset to a convenient location.
  162. */
  163. #if defined(CONFIG_USE_GUEST_BASE)
  164. extern unsigned long guest_base;
  165. extern int have_guest_base;
  166. extern unsigned long reserved_va;
  167. #define GUEST_BASE guest_base
  168. #define RESERVED_VA reserved_va
  169. #else
  170. #define GUEST_BASE 0ul
  171. #define RESERVED_VA 0ul
  172. #endif
  173. /* All direct uses of g2h and h2g need to go away for usermode softmmu. */
  174. #define g2h(x) ((void *)((unsigned long)(target_ulong)(x) + GUEST_BASE))
  175. #if HOST_LONG_BITS <= TARGET_VIRT_ADDR_SPACE_BITS
  176. #define h2g_valid(x) 1
  177. #else
  178. #define h2g_valid(x) ({ \
  179. unsigned long __guest = (unsigned long)(x) - GUEST_BASE; \
  180. (__guest < (1ul << TARGET_VIRT_ADDR_SPACE_BITS)) && \
  181. (!RESERVED_VA || (__guest < RESERVED_VA)); \
  182. })
  183. #endif
  184. #define h2g(x) ({ \
  185. unsigned long __ret = (unsigned long)(x) - GUEST_BASE; \
  186. /* Check if given address fits target address space */ \
  187. assert(h2g_valid(x)); \
  188. (abi_ulong)__ret; \
  189. })
  190. #define saddr(x) g2h(x)
  191. #define laddr(x) g2h(x)
  192. #else /* !CONFIG_USER_ONLY */
  193. /* NOTE: we use double casts if pointers and target_ulong have
  194. different sizes */
  195. #define saddr(x) (uint8_t *)(intptr_t)(x)
  196. #define laddr(x) (uint8_t *)(intptr_t)(x)
  197. #endif
  198. #define ldub_raw(p) ldub_p(laddr((p)))
  199. #define ldsb_raw(p) ldsb_p(laddr((p)))
  200. #define lduw_raw(p) lduw_p(laddr((p)))
  201. #define ldsw_raw(p) ldsw_p(laddr((p)))
  202. #define ldl_raw(p) ldl_p(laddr((p)))
  203. #define ldq_raw(p) ldq_p(laddr((p)))
  204. #define ldfl_raw(p) ldfl_p(laddr((p)))
  205. #define ldfq_raw(p) ldfq_p(laddr((p)))
  206. #define stb_raw(p, v) stb_p(saddr((p)), v)
  207. #define stw_raw(p, v) stw_p(saddr((p)), v)
  208. #define stl_raw(p, v) stl_p(saddr((p)), v)
  209. #define stq_raw(p, v) stq_p(saddr((p)), v)
  210. #define stfl_raw(p, v) stfl_p(saddr((p)), v)
  211. #define stfq_raw(p, v) stfq_p(saddr((p)), v)
  212. #if defined(CONFIG_USER_ONLY)
  213. /* if user mode, no other memory access functions */
  214. #define ldub(p) ldub_raw(p)
  215. #define ldsb(p) ldsb_raw(p)
  216. #define lduw(p) lduw_raw(p)
  217. #define ldsw(p) ldsw_raw(p)
  218. #define ldl(p) ldl_raw(p)
  219. #define ldq(p) ldq_raw(p)
  220. #define ldfl(p) ldfl_raw(p)
  221. #define ldfq(p) ldfq_raw(p)
  222. #define stb(p, v) stb_raw(p, v)
  223. #define stw(p, v) stw_raw(p, v)
  224. #define stl(p, v) stl_raw(p, v)
  225. #define stq(p, v) stq_raw(p, v)
  226. #define stfl(p, v) stfl_raw(p, v)
  227. #define stfq(p, v) stfq_raw(p, v)
  228. #ifndef CONFIG_TCG_PASS_AREG0
  229. #define ldub_code(p) ldub_raw(p)
  230. #define ldsb_code(p) ldsb_raw(p)
  231. #define lduw_code(p) lduw_raw(p)
  232. #define ldsw_code(p) ldsw_raw(p)
  233. #define ldl_code(p) ldl_raw(p)
  234. #define ldq_code(p) ldq_raw(p)
  235. #else
  236. #define cpu_ldub_code(env1, p) ldub_raw(p)
  237. #define cpu_ldsb_code(env1, p) ldsb_raw(p)
  238. #define cpu_lduw_code(env1, p) lduw_raw(p)
  239. #define cpu_ldsw_code(env1, p) ldsw_raw(p)
  240. #define cpu_ldl_code(env1, p) ldl_raw(p)
  241. #define cpu_ldq_code(env1, p) ldq_raw(p)
  242. #endif
  243. #define ldub_kernel(p) ldub_raw(p)
  244. #define ldsb_kernel(p) ldsb_raw(p)
  245. #define lduw_kernel(p) lduw_raw(p)
  246. #define ldsw_kernel(p) ldsw_raw(p)
  247. #define ldl_kernel(p) ldl_raw(p)
  248. #define ldq_kernel(p) ldq_raw(p)
  249. #define ldfl_kernel(p) ldfl_raw(p)
  250. #define ldfq_kernel(p) ldfq_raw(p)
  251. #define stb_kernel(p, v) stb_raw(p, v)
  252. #define stw_kernel(p, v) stw_raw(p, v)
  253. #define stl_kernel(p, v) stl_raw(p, v)
  254. #define stq_kernel(p, v) stq_raw(p, v)
  255. #define stfl_kernel(p, v) stfl_raw(p, v)
  256. #define stfq_kernel(p, vt) stfq_raw(p, v)
  257. #endif /* defined(CONFIG_USER_ONLY) */
  258. /* page related stuff */
  259. #define TARGET_PAGE_SIZE (1 << TARGET_PAGE_BITS)
  260. #define TARGET_PAGE_MASK ~(TARGET_PAGE_SIZE - 1)
  261. #define TARGET_PAGE_ALIGN(addr) (((addr) + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK)
  262. /* ??? These should be the larger of uintptr_t and target_ulong. */
  263. extern uintptr_t qemu_real_host_page_size;
  264. extern uintptr_t qemu_host_page_size;
  265. extern uintptr_t qemu_host_page_mask;
  266. #define HOST_PAGE_ALIGN(addr) (((addr) + qemu_host_page_size - 1) & qemu_host_page_mask)
  267. /* same as PROT_xxx */
  268. #define PAGE_READ 0x0001
  269. #define PAGE_WRITE 0x0002
  270. #define PAGE_EXEC 0x0004
  271. #define PAGE_BITS (PAGE_READ | PAGE_WRITE | PAGE_EXEC)
  272. #define PAGE_VALID 0x0008
  273. /* original state of the write flag (used when tracking self-modifying
  274. code */
  275. #define PAGE_WRITE_ORG 0x0010
  276. #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
  277. /* FIXME: Code that sets/uses this is broken and needs to go away. */
  278. #define PAGE_RESERVED 0x0020
  279. #endif
  280. #if defined(CONFIG_USER_ONLY)
  281. void page_dump(FILE *f);
  282. typedef int (*walk_memory_regions_fn)(void *, abi_ulong,
  283. abi_ulong, unsigned long);
  284. int walk_memory_regions(void *, walk_memory_regions_fn);
  285. int page_get_flags(target_ulong address);
  286. void page_set_flags(target_ulong start, target_ulong end, int flags);
  287. int page_check_range(target_ulong start, target_ulong len, int flags);
  288. #endif
  289. CPUArchState *cpu_copy(CPUArchState *env);
  290. CPUArchState *qemu_get_cpu(int cpu);
  291. #define CPU_DUMP_CODE 0x00010000
  292. void cpu_dump_state(CPUArchState *env, FILE *f, fprintf_function cpu_fprintf,
  293. int flags);
  294. void cpu_dump_statistics(CPUArchState *env, FILE *f, fprintf_function cpu_fprintf,
  295. int flags);
  296. void QEMU_NORETURN cpu_abort(CPUArchState *env, const char *fmt, ...)
  297. GCC_FMT_ATTR(2, 3);
  298. extern CPUArchState *first_cpu;
  299. DECLARE_TLS(CPUArchState *,cpu_single_env);
  300. #define cpu_single_env tls_var(cpu_single_env)
  301. /* Flags for use in ENV->INTERRUPT_PENDING.
  302. The numbers assigned here are non-sequential in order to preserve
  303. binary compatibility with the vmstate dump. Bit 0 (0x0001) was
  304. previously used for CPU_INTERRUPT_EXIT, and is cleared when loading
  305. the vmstate dump. */
  306. /* External hardware interrupt pending. This is typically used for
  307. interrupts from devices. */
  308. #define CPU_INTERRUPT_HARD 0x0002
  309. /* Exit the current TB. This is typically used when some system-level device
  310. makes some change to the memory mapping. E.g. the a20 line change. */
  311. #define CPU_INTERRUPT_EXITTB 0x0004
  312. /* Halt the CPU. */
  313. #define CPU_INTERRUPT_HALT 0x0020
  314. /* Debug event pending. */
  315. #define CPU_INTERRUPT_DEBUG 0x0080
  316. /* Several target-specific external hardware interrupts. Each target/cpu.h
  317. should define proper names based on these defines. */
  318. #define CPU_INTERRUPT_TGT_EXT_0 0x0008
  319. #define CPU_INTERRUPT_TGT_EXT_1 0x0010
  320. #define CPU_INTERRUPT_TGT_EXT_2 0x0040
  321. #define CPU_INTERRUPT_TGT_EXT_3 0x0200
  322. #define CPU_INTERRUPT_TGT_EXT_4 0x1000
  323. /* Several target-specific internal interrupts. These differ from the
  324. preceding target-specific interrupts in that they are intended to
  325. originate from within the cpu itself, typically in response to some
  326. instruction being executed. These, therefore, are not masked while
  327. single-stepping within the debugger. */
  328. #define CPU_INTERRUPT_TGT_INT_0 0x0100
  329. #define CPU_INTERRUPT_TGT_INT_1 0x0400
  330. #define CPU_INTERRUPT_TGT_INT_2 0x0800
  331. #define CPU_INTERRUPT_TGT_INT_3 0x2000
  332. /* First unused bit: 0x4000. */
  333. /* The set of all bits that should be masked when single-stepping. */
  334. #define CPU_INTERRUPT_SSTEP_MASK \
  335. (CPU_INTERRUPT_HARD \
  336. | CPU_INTERRUPT_TGT_EXT_0 \
  337. | CPU_INTERRUPT_TGT_EXT_1 \
  338. | CPU_INTERRUPT_TGT_EXT_2 \
  339. | CPU_INTERRUPT_TGT_EXT_3 \
  340. | CPU_INTERRUPT_TGT_EXT_4)
  341. #ifndef CONFIG_USER_ONLY
  342. typedef void (*CPUInterruptHandler)(CPUArchState *, int);
  343. extern CPUInterruptHandler cpu_interrupt_handler;
  344. static inline void cpu_interrupt(CPUArchState *s, int mask)
  345. {
  346. cpu_interrupt_handler(s, mask);
  347. }
  348. #else /* USER_ONLY */
  349. void cpu_interrupt(CPUArchState *env, int mask);
  350. #endif /* USER_ONLY */
  351. void cpu_reset_interrupt(CPUArchState *env, int mask);
  352. void cpu_exit(CPUArchState *s);
  353. bool qemu_cpu_has_work(CPUArchState *env);
  354. /* Breakpoint/watchpoint flags */
  355. #define BP_MEM_READ 0x01
  356. #define BP_MEM_WRITE 0x02
  357. #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
  358. #define BP_STOP_BEFORE_ACCESS 0x04
  359. #define BP_WATCHPOINT_HIT 0x08
  360. #define BP_GDB 0x10
  361. #define BP_CPU 0x20
  362. int cpu_breakpoint_insert(CPUArchState *env, target_ulong pc, int flags,
  363. CPUBreakpoint **breakpoint);
  364. int cpu_breakpoint_remove(CPUArchState *env, target_ulong pc, int flags);
  365. void cpu_breakpoint_remove_by_ref(CPUArchState *env, CPUBreakpoint *breakpoint);
  366. void cpu_breakpoint_remove_all(CPUArchState *env, int mask);
  367. int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
  368. int flags, CPUWatchpoint **watchpoint);
  369. int cpu_watchpoint_remove(CPUArchState *env, target_ulong addr,
  370. target_ulong len, int flags);
  371. void cpu_watchpoint_remove_by_ref(CPUArchState *env, CPUWatchpoint *watchpoint);
  372. void cpu_watchpoint_remove_all(CPUArchState *env, int mask);
  373. #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
  374. #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
  375. #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
  376. void cpu_single_step(CPUArchState *env, int enabled);
  377. void cpu_state_reset(CPUArchState *s);
  378. int cpu_is_stopped(CPUArchState *env);
  379. void run_on_cpu(CPUArchState *env, void (*func)(void *data), void *data);
  380. #define CPU_LOG_TB_OUT_ASM (1 << 0)
  381. #define CPU_LOG_TB_IN_ASM (1 << 1)
  382. #define CPU_LOG_TB_OP (1 << 2)
  383. #define CPU_LOG_TB_OP_OPT (1 << 3)
  384. #define CPU_LOG_INT (1 << 4)
  385. #define CPU_LOG_EXEC (1 << 5)
  386. #define CPU_LOG_PCALL (1 << 6)
  387. #define CPU_LOG_IOPORT (1 << 7)
  388. #define CPU_LOG_TB_CPU (1 << 8)
  389. #define CPU_LOG_RESET (1 << 9)
  390. /* define log items */
  391. typedef struct CPULogItem {
  392. int mask;
  393. const char *name;
  394. const char *help;
  395. } CPULogItem;
  396. extern const CPULogItem cpu_log_items[];
  397. void cpu_set_log(int log_flags);
  398. void cpu_set_log_filename(const char *filename);
  399. int cpu_str_to_log_mask(const char *str);
  400. #if !defined(CONFIG_USER_ONLY)
  401. /* Return the physical page corresponding to a virtual one. Use it
  402. only for debugging because no protection checks are done. Return -1
  403. if no page found. */
  404. target_phys_addr_t cpu_get_phys_page_debug(CPUArchState *env, target_ulong addr);
  405. /* memory API */
  406. extern int phys_ram_fd;
  407. extern ram_addr_t ram_size;
  408. /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
  409. #define RAM_PREALLOC_MASK (1 << 0)
  410. typedef struct RAMBlock {
  411. struct MemoryRegion *mr;
  412. uint8_t *host;
  413. ram_addr_t offset;
  414. ram_addr_t length;
  415. uint32_t flags;
  416. char idstr[256];
  417. QLIST_ENTRY(RAMBlock) next;
  418. #if defined(__linux__) && !defined(TARGET_S390X)
  419. int fd;
  420. #endif
  421. } RAMBlock;
  422. typedef struct RAMList {
  423. uint8_t *phys_dirty;
  424. QLIST_HEAD(, RAMBlock) blocks;
  425. } RAMList;
  426. extern RAMList ram_list;
  427. extern const char *mem_path;
  428. extern int mem_prealloc;
  429. /* Flags stored in the low bits of the TLB virtual address. These are
  430. defined so that fast path ram access is all zeros. */
  431. /* Zero if TLB entry is valid. */
  432. #define TLB_INVALID_MASK (1 << 3)
  433. /* Set if TLB entry references a clean RAM page. The iotlb entry will
  434. contain the page physical address. */
  435. #define TLB_NOTDIRTY (1 << 4)
  436. /* Set if TLB entry is an IO callback. */
  437. #define TLB_MMIO (1 << 5)
  438. void dump_exec_info(FILE *f, fprintf_function cpu_fprintf);
  439. #endif /* !CONFIG_USER_ONLY */
  440. int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
  441. uint8_t *buf, int len, int is_write);
  442. #endif /* CPU_ALL_H */