2
0

cpu-all.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. #define cpu_ldub_code(env1, p) ldub_raw(p)
  229. #define cpu_ldsb_code(env1, p) ldsb_raw(p)
  230. #define cpu_lduw_code(env1, p) lduw_raw(p)
  231. #define cpu_ldsw_code(env1, p) ldsw_raw(p)
  232. #define cpu_ldl_code(env1, p) ldl_raw(p)
  233. #define cpu_ldq_code(env1, p) ldq_raw(p)
  234. #define cpu_ldub_data(env, addr) ldub_raw(addr)
  235. #define cpu_lduw_data(env, addr) lduw_raw(addr)
  236. #define cpu_ldsw_data(env, addr) ldsw_raw(addr)
  237. #define cpu_ldl_data(env, addr) ldl_raw(addr)
  238. #define cpu_ldq_data(env, addr) ldq_raw(addr)
  239. #define cpu_stb_data(env, addr, data) stb_raw(addr, data)
  240. #define cpu_stw_data(env, addr, data) stw_raw(addr, data)
  241. #define cpu_stl_data(env, addr, data) stl_raw(addr, data)
  242. #define cpu_stq_data(env, addr, data) stq_raw(addr, data)
  243. #define cpu_ldub_kernel(env, addr) ldub_raw(addr)
  244. #define cpu_lduw_kernel(env, addr) lduw_raw(addr)
  245. #define cpu_ldsw_kernel(env, addr) ldsw_raw(addr)
  246. #define cpu_ldl_kernel(env, addr) ldl_raw(addr)
  247. #define cpu_ldq_kernel(env, addr) ldq_raw(addr)
  248. #define cpu_stb_kernel(env, addr, data) stb_raw(addr, data)
  249. #define cpu_stw_kernel(env, addr, data) stw_raw(addr, data)
  250. #define cpu_stl_kernel(env, addr, data) stl_raw(addr, data)
  251. #define cpu_stq_kernel(env, addr, data) stq_raw(addr, data)
  252. #define ldub_kernel(p) ldub_raw(p)
  253. #define ldsb_kernel(p) ldsb_raw(p)
  254. #define lduw_kernel(p) lduw_raw(p)
  255. #define ldsw_kernel(p) ldsw_raw(p)
  256. #define ldl_kernel(p) ldl_raw(p)
  257. #define ldq_kernel(p) ldq_raw(p)
  258. #define ldfl_kernel(p) ldfl_raw(p)
  259. #define ldfq_kernel(p) ldfq_raw(p)
  260. #define stb_kernel(p, v) stb_raw(p, v)
  261. #define stw_kernel(p, v) stw_raw(p, v)
  262. #define stl_kernel(p, v) stl_raw(p, v)
  263. #define stq_kernel(p, v) stq_raw(p, v)
  264. #define stfl_kernel(p, v) stfl_raw(p, v)
  265. #define stfq_kernel(p, vt) stfq_raw(p, v)
  266. #define cpu_ldub_data(env, addr) ldub_raw(addr)
  267. #define cpu_lduw_data(env, addr) lduw_raw(addr)
  268. #define cpu_ldl_data(env, addr) ldl_raw(addr)
  269. #define cpu_stb_data(env, addr, data) stb_raw(addr, data)
  270. #define cpu_stw_data(env, addr, data) stw_raw(addr, data)
  271. #define cpu_stl_data(env, addr, data) stl_raw(addr, data)
  272. #endif /* defined(CONFIG_USER_ONLY) */
  273. /* page related stuff */
  274. #define TARGET_PAGE_SIZE (1 << TARGET_PAGE_BITS)
  275. #define TARGET_PAGE_MASK ~(TARGET_PAGE_SIZE - 1)
  276. #define TARGET_PAGE_ALIGN(addr) (((addr) + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK)
  277. /* ??? These should be the larger of uintptr_t and target_ulong. */
  278. extern uintptr_t qemu_real_host_page_size;
  279. extern uintptr_t qemu_host_page_size;
  280. extern uintptr_t qemu_host_page_mask;
  281. #define HOST_PAGE_ALIGN(addr) (((addr) + qemu_host_page_size - 1) & qemu_host_page_mask)
  282. /* same as PROT_xxx */
  283. #define PAGE_READ 0x0001
  284. #define PAGE_WRITE 0x0002
  285. #define PAGE_EXEC 0x0004
  286. #define PAGE_BITS (PAGE_READ | PAGE_WRITE | PAGE_EXEC)
  287. #define PAGE_VALID 0x0008
  288. /* original state of the write flag (used when tracking self-modifying
  289. code */
  290. #define PAGE_WRITE_ORG 0x0010
  291. #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
  292. /* FIXME: Code that sets/uses this is broken and needs to go away. */
  293. #define PAGE_RESERVED 0x0020
  294. #endif
  295. #if defined(CONFIG_USER_ONLY)
  296. void page_dump(FILE *f);
  297. typedef int (*walk_memory_regions_fn)(void *, abi_ulong,
  298. abi_ulong, unsigned long);
  299. int walk_memory_regions(void *, walk_memory_regions_fn);
  300. int page_get_flags(target_ulong address);
  301. void page_set_flags(target_ulong start, target_ulong end, int flags);
  302. int page_check_range(target_ulong start, target_ulong len, int flags);
  303. #endif
  304. CPUArchState *cpu_copy(CPUArchState *env);
  305. CPUArchState *qemu_get_cpu(int cpu);
  306. #define CPU_DUMP_CODE 0x00010000
  307. void cpu_dump_state(CPUArchState *env, FILE *f, fprintf_function cpu_fprintf,
  308. int flags);
  309. void cpu_dump_statistics(CPUArchState *env, FILE *f, fprintf_function cpu_fprintf,
  310. int flags);
  311. void QEMU_NORETURN cpu_abort(CPUArchState *env, const char *fmt, ...)
  312. GCC_FMT_ATTR(2, 3);
  313. extern CPUArchState *first_cpu;
  314. DECLARE_TLS(CPUArchState *,cpu_single_env);
  315. #define cpu_single_env tls_var(cpu_single_env)
  316. /* Flags for use in ENV->INTERRUPT_PENDING.
  317. The numbers assigned here are non-sequential in order to preserve
  318. binary compatibility with the vmstate dump. Bit 0 (0x0001) was
  319. previously used for CPU_INTERRUPT_EXIT, and is cleared when loading
  320. the vmstate dump. */
  321. /* External hardware interrupt pending. This is typically used for
  322. interrupts from devices. */
  323. #define CPU_INTERRUPT_HARD 0x0002
  324. /* Exit the current TB. This is typically used when some system-level device
  325. makes some change to the memory mapping. E.g. the a20 line change. */
  326. #define CPU_INTERRUPT_EXITTB 0x0004
  327. /* Halt the CPU. */
  328. #define CPU_INTERRUPT_HALT 0x0020
  329. /* Debug event pending. */
  330. #define CPU_INTERRUPT_DEBUG 0x0080
  331. /* Several target-specific external hardware interrupts. Each target/cpu.h
  332. should define proper names based on these defines. */
  333. #define CPU_INTERRUPT_TGT_EXT_0 0x0008
  334. #define CPU_INTERRUPT_TGT_EXT_1 0x0010
  335. #define CPU_INTERRUPT_TGT_EXT_2 0x0040
  336. #define CPU_INTERRUPT_TGT_EXT_3 0x0200
  337. #define CPU_INTERRUPT_TGT_EXT_4 0x1000
  338. /* Several target-specific internal interrupts. These differ from the
  339. preceding target-specific interrupts in that they are intended to
  340. originate from within the cpu itself, typically in response to some
  341. instruction being executed. These, therefore, are not masked while
  342. single-stepping within the debugger. */
  343. #define CPU_INTERRUPT_TGT_INT_0 0x0100
  344. #define CPU_INTERRUPT_TGT_INT_1 0x0400
  345. #define CPU_INTERRUPT_TGT_INT_2 0x0800
  346. #define CPU_INTERRUPT_TGT_INT_3 0x2000
  347. /* First unused bit: 0x4000. */
  348. /* The set of all bits that should be masked when single-stepping. */
  349. #define CPU_INTERRUPT_SSTEP_MASK \
  350. (CPU_INTERRUPT_HARD \
  351. | CPU_INTERRUPT_TGT_EXT_0 \
  352. | CPU_INTERRUPT_TGT_EXT_1 \
  353. | CPU_INTERRUPT_TGT_EXT_2 \
  354. | CPU_INTERRUPT_TGT_EXT_3 \
  355. | CPU_INTERRUPT_TGT_EXT_4)
  356. #ifndef CONFIG_USER_ONLY
  357. typedef void (*CPUInterruptHandler)(CPUArchState *, int);
  358. extern CPUInterruptHandler cpu_interrupt_handler;
  359. static inline void cpu_interrupt(CPUArchState *s, int mask)
  360. {
  361. cpu_interrupt_handler(s, mask);
  362. }
  363. #else /* USER_ONLY */
  364. void cpu_interrupt(CPUArchState *env, int mask);
  365. #endif /* USER_ONLY */
  366. void cpu_reset_interrupt(CPUArchState *env, int mask);
  367. void cpu_exit(CPUArchState *s);
  368. bool qemu_cpu_has_work(CPUArchState *env);
  369. /* Breakpoint/watchpoint flags */
  370. #define BP_MEM_READ 0x01
  371. #define BP_MEM_WRITE 0x02
  372. #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
  373. #define BP_STOP_BEFORE_ACCESS 0x04
  374. #define BP_WATCHPOINT_HIT 0x08
  375. #define BP_GDB 0x10
  376. #define BP_CPU 0x20
  377. int cpu_breakpoint_insert(CPUArchState *env, target_ulong pc, int flags,
  378. CPUBreakpoint **breakpoint);
  379. int cpu_breakpoint_remove(CPUArchState *env, target_ulong pc, int flags);
  380. void cpu_breakpoint_remove_by_ref(CPUArchState *env, CPUBreakpoint *breakpoint);
  381. void cpu_breakpoint_remove_all(CPUArchState *env, int mask);
  382. int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
  383. int flags, CPUWatchpoint **watchpoint);
  384. int cpu_watchpoint_remove(CPUArchState *env, target_ulong addr,
  385. target_ulong len, int flags);
  386. void cpu_watchpoint_remove_by_ref(CPUArchState *env, CPUWatchpoint *watchpoint);
  387. void cpu_watchpoint_remove_all(CPUArchState *env, int mask);
  388. #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
  389. #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
  390. #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
  391. void cpu_single_step(CPUArchState *env, int enabled);
  392. int cpu_is_stopped(CPUArchState *env);
  393. void run_on_cpu(CPUArchState *env, void (*func)(void *data), void *data);
  394. #if !defined(CONFIG_USER_ONLY)
  395. /* Return the physical page corresponding to a virtual one. Use it
  396. only for debugging because no protection checks are done. Return -1
  397. if no page found. */
  398. target_phys_addr_t cpu_get_phys_page_debug(CPUArchState *env, target_ulong addr);
  399. /* memory API */
  400. extern int phys_ram_fd;
  401. extern ram_addr_t ram_size;
  402. /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
  403. #define RAM_PREALLOC_MASK (1 << 0)
  404. typedef struct RAMBlock {
  405. struct MemoryRegion *mr;
  406. uint8_t *host;
  407. ram_addr_t offset;
  408. ram_addr_t length;
  409. uint32_t flags;
  410. char idstr[256];
  411. QLIST_ENTRY(RAMBlock) next;
  412. #if defined(__linux__) && !defined(TARGET_S390X)
  413. int fd;
  414. #endif
  415. } RAMBlock;
  416. typedef struct RAMList {
  417. uint8_t *phys_dirty;
  418. QLIST_HEAD(, RAMBlock) blocks;
  419. uint64_t dirty_pages;
  420. } RAMList;
  421. extern RAMList ram_list;
  422. extern const char *mem_path;
  423. extern int mem_prealloc;
  424. /* Flags stored in the low bits of the TLB virtual address. These are
  425. defined so that fast path ram access is all zeros. */
  426. /* Zero if TLB entry is valid. */
  427. #define TLB_INVALID_MASK (1 << 3)
  428. /* Set if TLB entry references a clean RAM page. The iotlb entry will
  429. contain the page physical address. */
  430. #define TLB_NOTDIRTY (1 << 4)
  431. /* Set if TLB entry is an IO callback. */
  432. #define TLB_MMIO (1 << 5)
  433. void dump_exec_info(FILE *f, fprintf_function cpu_fprintf);
  434. #endif /* !CONFIG_USER_ONLY */
  435. int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
  436. uint8_t *buf, int len, int is_write);
  437. #endif /* CPU_ALL_H */