2
0

qemu.h 19 KB

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