2
0

qemu.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #ifndef QEMU_H
  2. #define QEMU_H
  3. #include "cpu.h"
  4. #include "exec/cpu_ldst.h"
  5. #undef DEBUG_REMAP
  6. #include "exec/user/abitypes.h"
  7. #include "syscall_defs.h"
  8. #include "target_syscall.h"
  9. /*
  10. * This is the size of the host kernel's sigset_t, needed where we make
  11. * direct system calls that take a sigset_t pointer and a size.
  12. */
  13. #define SIGSET_T_SIZE (_NSIG / 8)
  14. /*
  15. * This struct is used to hold certain information about the image.
  16. * Basically, it replicates in user space what would be certain
  17. * task_struct fields in the kernel
  18. */
  19. struct image_info {
  20. abi_ulong load_bias;
  21. abi_ulong load_addr;
  22. abi_ulong start_code;
  23. abi_ulong end_code;
  24. abi_ulong start_data;
  25. abi_ulong end_data;
  26. abi_ulong start_brk;
  27. abi_ulong brk;
  28. abi_ulong reserve_brk;
  29. abi_ulong start_mmap;
  30. abi_ulong start_stack;
  31. abi_ulong stack_limit;
  32. abi_ulong entry;
  33. abi_ulong code_offset;
  34. abi_ulong data_offset;
  35. abi_ulong saved_auxv;
  36. abi_ulong auxv_len;
  37. abi_ulong argc;
  38. abi_ulong argv;
  39. abi_ulong envc;
  40. abi_ulong envp;
  41. abi_ulong file_string;
  42. uint32_t elf_flags;
  43. int personality;
  44. abi_ulong alignment;
  45. bool exec_stack;
  46. /* Generic semihosting knows about these pointers. */
  47. abi_ulong arg_strings; /* strings for argv */
  48. abi_ulong env_strings; /* strings for envp; ends arg_strings */
  49. /* The fields below are used in FDPIC mode. */
  50. abi_ulong loadmap_addr;
  51. uint16_t nsegs;
  52. void *loadsegs;
  53. abi_ulong pt_dynamic_addr;
  54. abi_ulong interpreter_loadmap_addr;
  55. abi_ulong interpreter_pt_dynamic_addr;
  56. struct image_info *other_info;
  57. /* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */
  58. uint32_t note_flags;
  59. #ifdef TARGET_MIPS
  60. int fp_abi;
  61. int interp_fp_abi;
  62. #endif
  63. };
  64. #ifdef TARGET_I386
  65. /* Information about the current linux thread */
  66. struct vm86_saved_state {
  67. uint32_t eax; /* return code */
  68. uint32_t ebx;
  69. uint32_t ecx;
  70. uint32_t edx;
  71. uint32_t esi;
  72. uint32_t edi;
  73. uint32_t ebp;
  74. uint32_t esp;
  75. uint32_t eflags;
  76. uint32_t eip;
  77. uint16_t cs, ss, ds, es, fs, gs;
  78. };
  79. #endif
  80. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  81. /* FPU emulator */
  82. #include "nwfpe/fpa11.h"
  83. #endif
  84. struct emulated_sigtable {
  85. int pending; /* true if signal is pending */
  86. target_siginfo_t info;
  87. };
  88. typedef struct TaskState {
  89. pid_t ts_tid; /* tid (or pid) of this task */
  90. #ifdef TARGET_ARM
  91. # ifdef TARGET_ABI32
  92. /* FPA state */
  93. FPA11 fpa;
  94. # endif
  95. #endif
  96. #if defined(TARGET_ARM) || defined(TARGET_RISCV)
  97. int swi_errno;
  98. #endif
  99. #if defined(TARGET_I386) && !defined(TARGET_X86_64)
  100. abi_ulong target_v86;
  101. struct vm86_saved_state vm86_saved_regs;
  102. struct target_vm86plus_struct vm86plus;
  103. uint32_t v86flags;
  104. uint32_t v86mask;
  105. #endif
  106. abi_ulong child_tidptr;
  107. #ifdef TARGET_M68K
  108. abi_ulong tp_value;
  109. #endif
  110. #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV)
  111. /* Extra fields for semihosted binaries. */
  112. abi_ulong heap_base;
  113. abi_ulong heap_limit;
  114. #endif
  115. abi_ulong stack_base;
  116. int used; /* non zero if used */
  117. struct image_info *info;
  118. struct linux_binprm *bprm;
  119. struct emulated_sigtable sync_signal;
  120. struct emulated_sigtable sigtab[TARGET_NSIG];
  121. /*
  122. * This thread's signal mask, as requested by the guest program.
  123. * The actual signal mask of this thread may differ:
  124. * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
  125. * + sometimes we block all signals to avoid races
  126. */
  127. sigset_t signal_mask;
  128. /*
  129. * The signal mask imposed by a guest sigsuspend syscall, if we are
  130. * currently in the middle of such a syscall
  131. */
  132. sigset_t sigsuspend_mask;
  133. /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */
  134. int in_sigsuspend;
  135. /*
  136. * Nonzero if process_pending_signals() needs to do something (either
  137. * handle a pending signal or unblock signals).
  138. * This flag is written from a signal handler so should be accessed via
  139. * the qatomic_read() and qatomic_set() functions. (It is not accessed
  140. * from multiple threads.)
  141. */
  142. int signal_pending;
  143. /* This thread's sigaltstack, if it has one */
  144. struct target_sigaltstack sigaltstack_used;
  145. /* Start time of task after system boot in clock ticks */
  146. uint64_t start_boottime;
  147. } TaskState;
  148. abi_long do_brk(abi_ulong new_brk);
  149. /* user access */
  150. #define VERIFY_READ PAGE_READ
  151. #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
  152. static inline bool access_ok_untagged(int type, abi_ulong addr, abi_ulong size)
  153. {
  154. if (size == 0
  155. ? !guest_addr_valid_untagged(addr)
  156. : !guest_range_valid_untagged(addr, size)) {
  157. return false;
  158. }
  159. return page_check_range((target_ulong)addr, size, type) == 0;
  160. }
  161. static inline bool access_ok(CPUState *cpu, int type,
  162. abi_ulong addr, abi_ulong size)
  163. {
  164. return access_ok_untagged(type, cpu_untagged_addr(cpu, addr), size);
  165. }
  166. /* NOTE __get_user and __put_user use host pointers and don't check access.
  167. These are usually used to access struct data members once the struct has
  168. been locked - usually with lock_user_struct. */
  169. /*
  170. * Tricky points:
  171. * - Use __builtin_choose_expr to avoid type promotion from ?:,
  172. * - Invalid sizes result in a compile time error stemming from
  173. * the fact that abort has no parameters.
  174. * - It's easier to use the endian-specific unaligned load/store
  175. * functions than host-endian unaligned load/store plus tswapN.
  176. * - The pragmas are necessary only to silence a clang false-positive
  177. * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
  178. * - gcc has bugs in its _Pragma() support in some versions, eg
  179. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
  180. * include the warning-suppression pragmas for clang
  181. */
  182. #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
  183. #define PRAGMA_DISABLE_PACKED_WARNING \
  184. _Pragma("GCC diagnostic push"); \
  185. _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
  186. #define PRAGMA_REENABLE_PACKED_WARNING \
  187. _Pragma("GCC diagnostic pop")
  188. #else
  189. #define PRAGMA_DISABLE_PACKED_WARNING
  190. #define PRAGMA_REENABLE_PACKED_WARNING
  191. #endif
  192. #define __put_user_e(x, hptr, e) \
  193. do { \
  194. PRAGMA_DISABLE_PACKED_WARNING; \
  195. (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
  196. __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
  197. __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
  198. __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
  199. ((hptr), (x)), (void)0); \
  200. PRAGMA_REENABLE_PACKED_WARNING; \
  201. } while (0)
  202. #define __get_user_e(x, hptr, e) \
  203. do { \
  204. PRAGMA_DISABLE_PACKED_WARNING; \
  205. ((x) = (typeof(*hptr))( \
  206. __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
  207. __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
  208. __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
  209. __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
  210. (hptr)), (void)0); \
  211. PRAGMA_REENABLE_PACKED_WARNING; \
  212. } while (0)
  213. #if TARGET_BIG_ENDIAN
  214. # define __put_user(x, hptr) __put_user_e(x, hptr, be)
  215. # define __get_user(x, hptr) __get_user_e(x, hptr, be)
  216. #else
  217. # define __put_user(x, hptr) __put_user_e(x, hptr, le)
  218. # define __get_user(x, hptr) __get_user_e(x, hptr, le)
  219. #endif
  220. /* put_user()/get_user() take a guest address and check access */
  221. /* These are usually used to access an atomic data type, such as an int,
  222. * that has been passed by address. These internally perform locking
  223. * and unlocking on the data type.
  224. */
  225. #define put_user(x, gaddr, target_type) \
  226. ({ \
  227. abi_ulong __gaddr = (gaddr); \
  228. target_type *__hptr; \
  229. abi_long __ret = 0; \
  230. if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
  231. __put_user((x), __hptr); \
  232. unlock_user(__hptr, __gaddr, sizeof(target_type)); \
  233. } else \
  234. __ret = -TARGET_EFAULT; \
  235. __ret; \
  236. })
  237. #define get_user(x, gaddr, target_type) \
  238. ({ \
  239. abi_ulong __gaddr = (gaddr); \
  240. target_type *__hptr; \
  241. abi_long __ret = 0; \
  242. if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
  243. __get_user((x), __hptr); \
  244. unlock_user(__hptr, __gaddr, 0); \
  245. } else { \
  246. /* avoid warning */ \
  247. (x) = 0; \
  248. __ret = -TARGET_EFAULT; \
  249. } \
  250. __ret; \
  251. })
  252. #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
  253. #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
  254. #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
  255. #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
  256. #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
  257. #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
  258. #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
  259. #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
  260. #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
  261. #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
  262. #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
  263. #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
  264. #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
  265. #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
  266. #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
  267. #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
  268. #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
  269. #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
  270. #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
  271. #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
  272. /* copy_from_user() and copy_to_user() are usually used to copy data
  273. * buffers between the target and host. These internally perform
  274. * locking/unlocking of the memory.
  275. */
  276. int copy_from_user(void *hptr, abi_ulong gaddr, ssize_t len);
  277. int copy_to_user(abi_ulong gaddr, void *hptr, ssize_t len);
  278. /* Functions for accessing guest memory. The tget and tput functions
  279. read/write single values, byteswapping as necessary. The lock_user function
  280. gets a pointer to a contiguous area of guest memory, but does not perform
  281. any byteswapping. lock_user may return either a pointer to the guest
  282. memory, or a temporary buffer. */
  283. /* Lock an area of guest memory into the host. If copy is true then the
  284. host area will have the same contents as the guest. */
  285. void *lock_user(int type, abi_ulong guest_addr, ssize_t len, bool copy);
  286. /* Unlock an area of guest memory. The first LEN bytes must be
  287. flushed back to guest memory. host_ptr = NULL is explicitly
  288. allowed and does nothing. */
  289. #ifndef DEBUG_REMAP
  290. static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
  291. ssize_t len)
  292. {
  293. /* no-op */
  294. }
  295. #else
  296. void unlock_user(void *host_ptr, abi_ulong guest_addr, ssize_t len);
  297. #endif
  298. /* Return the length of a string in target memory or -TARGET_EFAULT if
  299. access error. */
  300. ssize_t target_strlen(abi_ulong gaddr);
  301. /* Like lock_user but for null terminated strings. */
  302. void *lock_user_string(abi_ulong guest_addr);
  303. /* Helper macros for locking/unlocking a target struct. */
  304. #define lock_user_struct(type, host_ptr, guest_addr, copy) \
  305. (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
  306. #define unlock_user_struct(host_ptr, guest_addr, copy) \
  307. unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
  308. #endif /* QEMU_H */