qemu.h 16 KB

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