2
0

qemu.h 13 KB

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