cpu-common.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #ifndef CPU_COMMON_H
  2. #define CPU_COMMON_H
  3. /* CPU interfaces that are target independent. */
  4. #ifndef CONFIG_USER_ONLY
  5. #include "exec/hwaddr.h"
  6. #endif
  7. #define EXCP_INTERRUPT 0x10000 /* async interruption */
  8. #define EXCP_HLT 0x10001 /* hlt instruction reached */
  9. #define EXCP_DEBUG 0x10002 /* cpu stopped after a breakpoint or singlestep */
  10. #define EXCP_HALTED 0x10003 /* cpu is halted (waiting for external event) */
  11. #define EXCP_YIELD 0x10004 /* cpu wants to yield timeslice to another */
  12. #define EXCP_ATOMIC 0x10005 /* stop-the-world and emulate atomic */
  13. /**
  14. * vaddr:
  15. * Type wide enough to contain any #target_ulong virtual address.
  16. */
  17. typedef uint64_t vaddr;
  18. #define VADDR_PRId PRId64
  19. #define VADDR_PRIu PRIu64
  20. #define VADDR_PRIo PRIo64
  21. #define VADDR_PRIx PRIx64
  22. #define VADDR_PRIX PRIX64
  23. #define VADDR_MAX UINT64_MAX
  24. void cpu_exec_init_all(void);
  25. void cpu_exec_step_atomic(CPUState *cpu);
  26. /* Using intptr_t ensures that qemu_*_page_mask is sign-extended even
  27. * when intptr_t is 32-bit and we are aligning a long long.
  28. */
  29. extern uintptr_t qemu_host_page_size;
  30. extern intptr_t qemu_host_page_mask;
  31. #define HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_host_page_size)
  32. #define REAL_HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_real_host_page_size())
  33. /* The CPU list lock nests outside page_(un)lock or mmap_(un)lock */
  34. extern QemuMutex qemu_cpu_list_lock;
  35. void qemu_init_cpu_list(void);
  36. void cpu_list_lock(void);
  37. void cpu_list_unlock(void);
  38. unsigned int cpu_list_generation_id_get(void);
  39. void tcg_iommu_init_notifier_list(CPUState *cpu);
  40. void tcg_iommu_free_notifier_list(CPUState *cpu);
  41. #if !defined(CONFIG_USER_ONLY)
  42. enum device_endian {
  43. DEVICE_NATIVE_ENDIAN,
  44. DEVICE_BIG_ENDIAN,
  45. DEVICE_LITTLE_ENDIAN,
  46. };
  47. #if HOST_BIG_ENDIAN
  48. #define DEVICE_HOST_ENDIAN DEVICE_BIG_ENDIAN
  49. #else
  50. #define DEVICE_HOST_ENDIAN DEVICE_LITTLE_ENDIAN
  51. #endif
  52. /* address in the RAM (different from a physical address) */
  53. #if defined(CONFIG_XEN_BACKEND)
  54. typedef uint64_t ram_addr_t;
  55. # define RAM_ADDR_MAX UINT64_MAX
  56. # define RAM_ADDR_FMT "%" PRIx64
  57. #else
  58. typedef uintptr_t ram_addr_t;
  59. # define RAM_ADDR_MAX UINTPTR_MAX
  60. # define RAM_ADDR_FMT "%" PRIxPTR
  61. #endif
  62. /* memory API */
  63. void qemu_ram_remap(ram_addr_t addr, ram_addr_t length);
  64. /* This should not be used by devices. */
  65. ram_addr_t qemu_ram_addr_from_host(void *ptr);
  66. ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr);
  67. RAMBlock *qemu_ram_block_by_name(const char *name);
  68. /*
  69. * Translates a host ptr back to a RAMBlock and an offset in that RAMBlock.
  70. *
  71. * @ptr: The host pointer to translate.
  72. * @round_offset: Whether to round the result offset down to a target page
  73. * @offset: Will be set to the offset within the returned RAMBlock.
  74. *
  75. * Returns: RAMBlock (or NULL if not found)
  76. *
  77. * By the time this function returns, the returned pointer is not protected
  78. * by RCU anymore. If the caller is not within an RCU critical section and
  79. * does not hold the iothread lock, it must have other means of protecting the
  80. * pointer, such as a reference to the memory region that owns the RAMBlock.
  81. */
  82. RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset,
  83. ram_addr_t *offset);
  84. ram_addr_t qemu_ram_block_host_offset(RAMBlock *rb, void *host);
  85. void qemu_ram_set_idstr(RAMBlock *block, const char *name, DeviceState *dev);
  86. void qemu_ram_unset_idstr(RAMBlock *block);
  87. const char *qemu_ram_get_idstr(RAMBlock *rb);
  88. void *qemu_ram_get_host_addr(RAMBlock *rb);
  89. ram_addr_t qemu_ram_get_offset(RAMBlock *rb);
  90. ram_addr_t qemu_ram_get_used_length(RAMBlock *rb);
  91. ram_addr_t qemu_ram_get_max_length(RAMBlock *rb);
  92. bool qemu_ram_is_shared(RAMBlock *rb);
  93. bool qemu_ram_is_noreserve(RAMBlock *rb);
  94. bool qemu_ram_is_uf_zeroable(RAMBlock *rb);
  95. void qemu_ram_set_uf_zeroable(RAMBlock *rb);
  96. bool qemu_ram_is_migratable(RAMBlock *rb);
  97. void qemu_ram_set_migratable(RAMBlock *rb);
  98. void qemu_ram_unset_migratable(RAMBlock *rb);
  99. bool qemu_ram_is_named_file(RAMBlock *rb);
  100. int qemu_ram_get_fd(RAMBlock *rb);
  101. size_t qemu_ram_pagesize(RAMBlock *block);
  102. size_t qemu_ram_pagesize_largest(void);
  103. /**
  104. * cpu_address_space_init:
  105. * @cpu: CPU to add this address space to
  106. * @asidx: integer index of this address space
  107. * @prefix: prefix to be used as name of address space
  108. * @mr: the root memory region of address space
  109. *
  110. * Add the specified address space to the CPU's cpu_ases list.
  111. * The address space added with @asidx 0 is the one used for the
  112. * convenience pointer cpu->as.
  113. * The target-specific code which registers ASes is responsible
  114. * for defining what semantics address space 0, 1, 2, etc have.
  115. *
  116. * Before the first call to this function, the caller must set
  117. * cpu->num_ases to the total number of address spaces it needs
  118. * to support.
  119. *
  120. * Note that with KVM only one address space is supported.
  121. */
  122. void cpu_address_space_init(CPUState *cpu, int asidx,
  123. const char *prefix, MemoryRegion *mr);
  124. void cpu_physical_memory_rw(hwaddr addr, void *buf,
  125. hwaddr len, bool is_write);
  126. static inline void cpu_physical_memory_read(hwaddr addr,
  127. void *buf, hwaddr len)
  128. {
  129. cpu_physical_memory_rw(addr, buf, len, false);
  130. }
  131. static inline void cpu_physical_memory_write(hwaddr addr,
  132. const void *buf, hwaddr len)
  133. {
  134. cpu_physical_memory_rw(addr, (void *)buf, len, true);
  135. }
  136. void *cpu_physical_memory_map(hwaddr addr,
  137. hwaddr *plen,
  138. bool is_write);
  139. void cpu_physical_memory_unmap(void *buffer, hwaddr len,
  140. bool is_write, hwaddr access_len);
  141. void cpu_register_map_client(QEMUBH *bh);
  142. void cpu_unregister_map_client(QEMUBH *bh);
  143. bool cpu_physical_memory_is_io(hwaddr phys_addr);
  144. /* Coalesced MMIO regions are areas where write operations can be reordered.
  145. * This usually implies that write operations are side-effect free. This allows
  146. * batching which can make a major impact on performance when using
  147. * virtualization.
  148. */
  149. void qemu_flush_coalesced_mmio_buffer(void);
  150. void cpu_flush_icache_range(hwaddr start, hwaddr len);
  151. typedef int (RAMBlockIterFunc)(RAMBlock *rb, void *opaque);
  152. int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque);
  153. int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length);
  154. #endif
  155. /* Returns: 0 on success, -1 on error */
  156. int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
  157. void *ptr, size_t len, bool is_write);
  158. /* vl.c */
  159. void list_cpus(void);
  160. #ifdef CONFIG_TCG
  161. /**
  162. * cpu_unwind_state_data:
  163. * @cpu: the cpu context
  164. * @host_pc: the host pc within the translation
  165. * @data: output data
  166. *
  167. * Attempt to load the the unwind state for a host pc occurring in
  168. * translated code. If @host_pc is not in translated code, the
  169. * function returns false; otherwise @data is loaded.
  170. * This is the same unwind info as given to restore_state_to_opc.
  171. */
  172. bool cpu_unwind_state_data(CPUState *cpu, uintptr_t host_pc, uint64_t *data);
  173. /**
  174. * cpu_restore_state:
  175. * @cpu: the cpu context
  176. * @host_pc: the host pc within the translation
  177. * @return: true if state was restored, false otherwise
  178. *
  179. * Attempt to restore the state for a fault occurring in translated
  180. * code. If @host_pc is not in translated code no state is
  181. * restored and the function returns false.
  182. */
  183. bool cpu_restore_state(CPUState *cpu, uintptr_t host_pc);
  184. G_NORETURN void cpu_loop_exit_noexc(CPUState *cpu);
  185. G_NORETURN void cpu_loop_exit_atomic(CPUState *cpu, uintptr_t pc);
  186. #endif /* CONFIG_TCG */
  187. G_NORETURN void cpu_loop_exit(CPUState *cpu);
  188. G_NORETURN void cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc);
  189. #endif /* CPU_COMMON_H */