qemu.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * qemu bsd user mode definition
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef QEMU_H
  18. #define QEMU_H
  19. #include <sys/param.h>
  20. #include "qemu/int128.h"
  21. #include "cpu.h"
  22. #include "qemu/units.h"
  23. #include "exec/cpu_ldst.h"
  24. #include "exec/exec-all.h"
  25. #include "user/abitypes.h"
  26. #include "user/cpu_loop.h"
  27. #include "user/page-protection.h"
  28. extern char **environ;
  29. #include "user/thunk.h"
  30. #include "target_arch.h"
  31. #include "syscall_defs.h"
  32. #include "target_syscall.h"
  33. #include "target_os_vmparam.h"
  34. #include "target_os_signal.h"
  35. #include "target.h"
  36. #include "exec/gdbstub.h"
  37. #include "exec/page-protection.h"
  38. #include "qemu/clang-tsa.h"
  39. #include "accel/tcg/vcpu-state.h"
  40. #include "qemu-os.h"
  41. /*
  42. * TODO: Remove these and rely only on qemu_real_host_page_size().
  43. */
  44. extern uintptr_t qemu_host_page_size;
  45. extern intptr_t qemu_host_page_mask;
  46. #define HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_host_page_size)
  47. /*
  48. * This struct is used to hold certain information about the image. Basically,
  49. * it replicates in user space what would be certain task_struct fields in the
  50. * kernel
  51. */
  52. struct image_info {
  53. abi_ulong load_bias;
  54. abi_ulong load_addr;
  55. abi_ulong start_code;
  56. abi_ulong end_code;
  57. abi_ulong start_data;
  58. abi_ulong end_data;
  59. abi_ulong brk;
  60. abi_ulong rss;
  61. abi_ulong start_stack;
  62. abi_ulong entry;
  63. abi_ulong code_offset;
  64. abi_ulong data_offset;
  65. abi_ulong arg_start;
  66. abi_ulong arg_end;
  67. uint32_t elf_flags;
  68. };
  69. struct emulated_sigtable {
  70. int pending; /* true if signal is pending */
  71. target_siginfo_t info;
  72. };
  73. /*
  74. * NOTE: we force a big alignment so that the stack stored after is aligned too
  75. */
  76. struct TaskState {
  77. pid_t ts_tid; /* tid (or pid) of this task */
  78. struct TaskState *next;
  79. struct bsd_binprm *bprm;
  80. struct image_info *info;
  81. struct emulated_sigtable sync_signal;
  82. /*
  83. * TODO: Since we block all signals while returning to the main CPU
  84. * loop, this needn't be an array
  85. */
  86. struct emulated_sigtable sigtab[TARGET_NSIG];
  87. /*
  88. * Nonzero if process_pending_signals() needs to do something (either
  89. * handle a pending signal or unblock signals).
  90. * This flag is written from a signal handler so should be accessed via
  91. * the qatomic_read() and qatomic_set() functions. (It is not accessed
  92. * from multiple threads.)
  93. */
  94. int signal_pending;
  95. /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */
  96. bool in_sigsuspend;
  97. /*
  98. * This thread's signal mask, as requested by the guest program.
  99. * The actual signal mask of this thread may differ:
  100. * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
  101. * + sometimes we block all signals to avoid races
  102. */
  103. sigset_t signal_mask;
  104. /*
  105. * The signal mask imposed by a guest sigsuspend syscall, if we are
  106. * currently in the middle of such a syscall
  107. */
  108. sigset_t sigsuspend_mask;
  109. /* This thread's sigaltstack, if it has one */
  110. struct target_sigaltstack sigaltstack_used;
  111. } __attribute__((aligned(16)));
  112. void stop_all_tasks(void);
  113. extern const char *interp_prefix;
  114. extern const char *qemu_uname_release;
  115. /*
  116. * TARGET_ARG_MAX defines the number of bytes allocated for arguments
  117. * and envelope for the new program. 256k should suffice for a reasonable
  118. * maximum env+arg in 32-bit environments, bump it up to 512k for !ILP32
  119. * platforms.
  120. */
  121. #if TARGET_ABI_BITS > 32
  122. #define TARGET_ARG_MAX (512 * KiB)
  123. #else
  124. #define TARGET_ARG_MAX (256 * KiB)
  125. #endif
  126. #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
  127. /*
  128. * This structure is used to hold the arguments that are
  129. * used when loading binaries.
  130. */
  131. struct bsd_binprm {
  132. char buf[128];
  133. void *page[MAX_ARG_PAGES];
  134. abi_ulong p;
  135. abi_ulong stringp;
  136. int fd;
  137. int e_uid, e_gid;
  138. int argc, envc;
  139. char **argv;
  140. char **envp;
  141. char *filename; /* (Given) Name of binary */
  142. char *fullpath; /* Full path of binary */
  143. int (*core_dump)(int, CPUArchState *);
  144. };
  145. void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
  146. abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
  147. abi_ulong stringp);
  148. int loader_exec(const char *filename, char **argv, char **envp,
  149. struct target_pt_regs *regs, struct image_info *infop,
  150. struct bsd_binprm *bprm);
  151. int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
  152. struct image_info *info);
  153. int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
  154. struct image_info *info);
  155. int is_target_elf_binary(int fd);
  156. abi_long memcpy_to_target(abi_ulong dest, const void *src,
  157. unsigned long len);
  158. void target_set_brk(abi_ulong new_brk);
  159. abi_long do_brk(abi_ulong new_brk);
  160. void syscall_init(void);
  161. abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
  162. abi_long arg2, abi_long arg3, abi_long arg4,
  163. abi_long arg5, abi_long arg6, abi_long arg7,
  164. abi_long arg8);
  165. abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
  166. abi_long arg2, abi_long arg3, abi_long arg4,
  167. abi_long arg5, abi_long arg6);
  168. abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
  169. abi_long arg2, abi_long arg3, abi_long arg4,
  170. abi_long arg5, abi_long arg6);
  171. void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
  172. extern __thread CPUState *thread_cpu;
  173. char *target_strerror(int err);
  174. int get_osversion(void);
  175. void fork_start(void);
  176. void fork_end(pid_t pid);
  177. #include "qemu/log.h"
  178. /* strace.c */
  179. struct syscallname {
  180. int nr;
  181. const char *name;
  182. const char *format;
  183. void (*call)(const struct syscallname *,
  184. abi_long, abi_long, abi_long,
  185. abi_long, abi_long, abi_long);
  186. void (*result)(const struct syscallname *, abi_long);
  187. };
  188. void
  189. print_freebsd_syscall(int num,
  190. abi_long arg1, abi_long arg2, abi_long arg3,
  191. abi_long arg4, abi_long arg5, abi_long arg6);
  192. void print_freebsd_syscall_ret(int num, abi_long ret);
  193. void
  194. print_netbsd_syscall(int num,
  195. abi_long arg1, abi_long arg2, abi_long arg3,
  196. abi_long arg4, abi_long arg5, abi_long arg6);
  197. void print_netbsd_syscall_ret(int num, abi_long ret);
  198. void
  199. print_openbsd_syscall(int num,
  200. abi_long arg1, abi_long arg2, abi_long arg3,
  201. abi_long arg4, abi_long arg5, abi_long arg6);
  202. void print_openbsd_syscall_ret(int num, abi_long ret);
  203. /**
  204. * print_taken_signal:
  205. * @target_signum: target signal being taken
  206. * @tinfo: target_siginfo_t which will be passed to the guest for the signal
  207. *
  208. * Print strace output indicating that this signal is being taken by the guest,
  209. * in a format similar to:
  210. * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
  211. */
  212. void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
  213. extern int do_strace;
  214. /* mmap.c */
  215. int target_mprotect(abi_ulong start, abi_ulong len, int prot);
  216. abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
  217. int flags, int fd, off_t offset);
  218. int target_munmap(abi_ulong start, abi_ulong len);
  219. abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
  220. abi_ulong new_size, unsigned long flags,
  221. abi_ulong new_addr);
  222. int target_msync(abi_ulong start, abi_ulong len, int flags);
  223. extern abi_ulong mmap_next_start;
  224. abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
  225. void mmap_reserve(abi_ulong start, abi_ulong size);
  226. void TSA_NO_TSA mmap_fork_start(void);
  227. void TSA_NO_TSA mmap_fork_end(int child);
  228. /* main.c */
  229. extern char qemu_proc_pathname[];
  230. extern unsigned long target_maxtsiz;
  231. extern unsigned long target_dfldsiz;
  232. extern unsigned long target_maxdsiz;
  233. extern unsigned long target_dflssiz;
  234. extern unsigned long target_maxssiz;
  235. extern unsigned long target_sgrowsiz;
  236. /* os-syscall.c */
  237. abi_long get_errno(abi_long ret);
  238. bool is_error(abi_long ret);
  239. int host_to_target_errno(int err);
  240. /* os-proc.c */
  241. abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
  242. abi_ulong guest_envp, int do_fexec);
  243. abi_long do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2,
  244. abi_ulong arg3, abi_ulong arg4, abi_ulong arg5, abi_ulong arg6);
  245. /* os-sys.c */
  246. abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
  247. abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
  248. abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
  249. int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp,
  250. abi_ulong newlen);
  251. abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
  252. /* user access */
  253. #define VERIFY_READ PAGE_READ
  254. #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
  255. static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
  256. {
  257. return page_check_range((target_ulong)addr, size, type);
  258. }
  259. /*
  260. * NOTE __get_user and __put_user use host pointers and don't check access.
  261. *
  262. * These are usually used to access struct data members once the struct has been
  263. * locked - usually with lock_user_struct().
  264. */
  265. /*
  266. * Tricky points:
  267. * - Use __builtin_choose_expr to avoid type promotion from ?:,
  268. * - Invalid sizes result in a compile time error stemming from
  269. * the fact that abort has no parameters.
  270. * - It's easier to use the endian-specific unaligned load/store
  271. * functions than host-endian unaligned load/store plus tswapN.
  272. * - The pragmas are necessary only to silence a clang false-positive
  273. * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
  274. * - gcc has bugs in its _Pragma() support in some versions, eg
  275. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
  276. * include the warning-suppression pragmas for clang
  277. */
  278. #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
  279. #define PRAGMA_DISABLE_PACKED_WARNING \
  280. _Pragma("GCC diagnostic push"); \
  281. _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
  282. #define PRAGMA_REENABLE_PACKED_WARNING \
  283. _Pragma("GCC diagnostic pop")
  284. #else
  285. #define PRAGMA_DISABLE_PACKED_WARNING
  286. #define PRAGMA_REENABLE_PACKED_WARNING
  287. #endif
  288. #define __put_user_e(x, hptr, e) \
  289. do { \
  290. PRAGMA_DISABLE_PACKED_WARNING; \
  291. (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
  292. __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
  293. __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
  294. __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
  295. ((hptr), (x)), (void)0); \
  296. PRAGMA_REENABLE_PACKED_WARNING; \
  297. } while (0)
  298. #define __get_user_e(x, hptr, e) \
  299. do { \
  300. PRAGMA_DISABLE_PACKED_WARNING; \
  301. ((x) = (typeof(*hptr))( \
  302. __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
  303. __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
  304. __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
  305. __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
  306. (hptr)), (void)0); \
  307. PRAGMA_REENABLE_PACKED_WARNING; \
  308. } while (0)
  309. #if TARGET_BIG_ENDIAN
  310. # define __put_user(x, hptr) __put_user_e(x, hptr, be)
  311. # define __get_user(x, hptr) __get_user_e(x, hptr, be)
  312. #else
  313. # define __put_user(x, hptr) __put_user_e(x, hptr, le)
  314. # define __get_user(x, hptr) __get_user_e(x, hptr, le)
  315. #endif
  316. /*
  317. * put_user()/get_user() take a guest address and check access
  318. *
  319. * These are usually used to access an atomic data type, such as an int, that
  320. * has been passed by address. These internally perform locking and unlocking
  321. * on the data type.
  322. */
  323. #define put_user(x, gaddr, target_type) \
  324. ({ \
  325. abi_ulong __gaddr = (gaddr); \
  326. target_type *__hptr; \
  327. abi_long __ret = 0; \
  328. __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \
  329. if (__hptr) { \
  330. __put_user((x), __hptr); \
  331. unlock_user(__hptr, __gaddr, sizeof(target_type)); \
  332. } else \
  333. __ret = -TARGET_EFAULT; \
  334. __ret; \
  335. })
  336. #define get_user(x, gaddr, target_type) \
  337. ({ \
  338. abi_ulong __gaddr = (gaddr); \
  339. target_type *__hptr; \
  340. abi_long __ret = 0; \
  341. __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \
  342. if (__hptr) { \
  343. __get_user((x), __hptr); \
  344. unlock_user(__hptr, __gaddr, 0); \
  345. } else { \
  346. (x) = 0; \
  347. __ret = -TARGET_EFAULT; \
  348. } \
  349. __ret; \
  350. })
  351. #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
  352. #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
  353. #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
  354. #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
  355. #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
  356. #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
  357. #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
  358. #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
  359. #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
  360. #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
  361. #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
  362. #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
  363. #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
  364. #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
  365. #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
  366. #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
  367. #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
  368. #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
  369. #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
  370. #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
  371. /*
  372. * copy_from_user() and copy_to_user() are usually used to copy data
  373. * buffers between the target and host. These internally perform
  374. * locking/unlocking of the memory.
  375. */
  376. abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
  377. abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
  378. /*
  379. * Functions for accessing guest memory. The tget and tput functions
  380. * read/write single values, byteswapping as necessary. The lock_user function
  381. * gets a pointer to a contiguous area of guest memory, but does not perform
  382. * any byteswapping. lock_user may return either a pointer to the guest
  383. * memory, or a temporary buffer.
  384. */
  385. /*
  386. * Lock an area of guest memory into the host. If copy is true then the
  387. * host area will have the same contents as the guest.
  388. */
  389. static inline void *lock_user(int type, abi_ulong guest_addr, long len,
  390. int copy)
  391. {
  392. if (!access_ok(type, guest_addr, len)) {
  393. return NULL;
  394. }
  395. #ifdef CONFIG_DEBUG_REMAP
  396. {
  397. void *addr;
  398. addr = g_malloc(len);
  399. if (copy) {
  400. memcpy(addr, g2h_untagged(guest_addr), len);
  401. } else {
  402. memset(addr, 0, len);
  403. }
  404. return addr;
  405. }
  406. #else
  407. return g2h_untagged(guest_addr);
  408. #endif
  409. }
  410. /*
  411. * Unlock an area of guest memory. The first LEN bytes must be flushed back to
  412. * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
  413. */
  414. static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
  415. long len)
  416. {
  417. #ifdef CONFIG_DEBUG_REMAP
  418. if (!host_ptr) {
  419. return;
  420. }
  421. if (host_ptr == g2h_untagged(guest_addr)) {
  422. return;
  423. }
  424. if (len > 0) {
  425. memcpy(g2h_untagged(guest_addr), host_ptr, len);
  426. }
  427. g_free(host_ptr);
  428. #endif
  429. }
  430. /*
  431. * Return the length of a string in target memory or -TARGET_EFAULT if access
  432. * error.
  433. */
  434. abi_long target_strlen(abi_ulong gaddr);
  435. /* Like lock_user but for null terminated strings. */
  436. static inline void *lock_user_string(abi_ulong guest_addr)
  437. {
  438. abi_long len;
  439. len = target_strlen(guest_addr);
  440. if (len < 0) {
  441. return NULL;
  442. }
  443. return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
  444. }
  445. /* Helper macros for locking/unlocking a target struct. */
  446. #define lock_user_struct(type, host_ptr, guest_addr, copy) \
  447. (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
  448. #define unlock_user_struct(host_ptr, guest_addr, copy) \
  449. unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
  450. static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
  451. {
  452. #if TARGET_ABI_BITS == 32
  453. #if TARGET_BIG_ENDIAN
  454. return ((uint64_t)word0 << 32) | word1;
  455. #else
  456. return ((uint64_t)word1 << 32) | word0;
  457. #endif
  458. #else /* TARGET_ABI_BITS != 32 */
  459. return word0;
  460. #endif /* TARGET_ABI_BITS != 32 */
  461. }
  462. #include <pthread.h>
  463. #include "user/safe-syscall.h"
  464. #endif /* QEMU_H */