qemu.h 19 KB

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