qemu.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "qemu/osdep.h"
  20. #include "cpu.h"
  21. #include "qemu/units.h"
  22. #include "exec/cpu_ldst.h"
  23. #include "exec/exec-all.h"
  24. #undef DEBUG_REMAP
  25. #include "exec/user/abitypes.h"
  26. extern char **environ;
  27. #include "exec/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. /*
  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 mmap_fork_start(void);
  218. void 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_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
  233. /* user access */
  234. #define VERIFY_READ PAGE_READ
  235. #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
  236. static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
  237. {
  238. return page_check_range((target_ulong)addr, size, type) == 0;
  239. }
  240. /*
  241. * NOTE __get_user and __put_user use host pointers and don't check access.
  242. *
  243. * These are usually used to access struct data members once the struct has been
  244. * locked - usually with lock_user_struct().
  245. */
  246. #define __put_user(x, hptr)\
  247. ({\
  248. int size = sizeof(*hptr);\
  249. switch (size) {\
  250. case 1:\
  251. *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
  252. break;\
  253. case 2:\
  254. *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
  255. break;\
  256. case 4:\
  257. *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
  258. break;\
  259. case 8:\
  260. *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
  261. break;\
  262. default:\
  263. abort();\
  264. } \
  265. 0;\
  266. })
  267. #define __get_user(x, hptr) \
  268. ({\
  269. int size = sizeof(*hptr);\
  270. switch (size) {\
  271. case 1:\
  272. x = (typeof(*hptr))*(uint8_t *)(hptr);\
  273. break;\
  274. case 2:\
  275. x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
  276. break;\
  277. case 4:\
  278. x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
  279. break;\
  280. case 8:\
  281. x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
  282. break;\
  283. default:\
  284. x = 0;\
  285. abort();\
  286. } \
  287. 0;\
  288. })
  289. /*
  290. * put_user()/get_user() take a guest address and check access
  291. *
  292. * These are usually used to access an atomic data type, such as an int, that
  293. * has been passed by address. These internally perform locking and unlocking
  294. * on the data type.
  295. */
  296. #define put_user(x, gaddr, target_type) \
  297. ({ \
  298. abi_ulong __gaddr = (gaddr); \
  299. target_type *__hptr; \
  300. abi_long __ret; \
  301. __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \
  302. if (__hptr) { \
  303. __ret = __put_user((x), __hptr); \
  304. unlock_user(__hptr, __gaddr, sizeof(target_type)); \
  305. } else \
  306. __ret = -TARGET_EFAULT; \
  307. __ret; \
  308. })
  309. #define get_user(x, gaddr, target_type) \
  310. ({ \
  311. abi_ulong __gaddr = (gaddr); \
  312. target_type *__hptr; \
  313. abi_long __ret; \
  314. __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \
  315. if (__hptr) { \
  316. __ret = __get_user((x), __hptr); \
  317. unlock_user(__hptr, __gaddr, 0); \
  318. } else { \
  319. (x) = 0; \
  320. __ret = -TARGET_EFAULT; \
  321. } \
  322. __ret; \
  323. })
  324. #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
  325. #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
  326. #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
  327. #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
  328. #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
  329. #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
  330. #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
  331. #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
  332. #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
  333. #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
  334. #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
  335. #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
  336. #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
  337. #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
  338. #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
  339. #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
  340. #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
  341. #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
  342. #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
  343. #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
  344. /*
  345. * copy_from_user() and copy_to_user() are usually used to copy data
  346. * buffers between the target and host. These internally perform
  347. * locking/unlocking of the memory.
  348. */
  349. abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
  350. abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
  351. /*
  352. * Functions for accessing guest memory. The tget and tput functions
  353. * read/write single values, byteswapping as necessary. The lock_user function
  354. * gets a pointer to a contiguous area of guest memory, but does not perform
  355. * any byteswapping. lock_user may return either a pointer to the guest
  356. * memory, or a temporary buffer.
  357. */
  358. /*
  359. * Lock an area of guest memory into the host. If copy is true then the
  360. * host area will have the same contents as the guest.
  361. */
  362. static inline void *lock_user(int type, abi_ulong guest_addr, long len,
  363. int copy)
  364. {
  365. if (!access_ok(type, guest_addr, len)) {
  366. return NULL;
  367. }
  368. #ifdef DEBUG_REMAP
  369. {
  370. void *addr;
  371. addr = g_malloc(len);
  372. if (copy) {
  373. memcpy(addr, g2h_untagged(guest_addr), len);
  374. } else {
  375. memset(addr, 0, len);
  376. }
  377. return addr;
  378. }
  379. #else
  380. return g2h_untagged(guest_addr);
  381. #endif
  382. }
  383. /*
  384. * Unlock an area of guest memory. The first LEN bytes must be flushed back to
  385. * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
  386. */
  387. static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
  388. long len)
  389. {
  390. #ifdef DEBUG_REMAP
  391. if (!host_ptr) {
  392. return;
  393. }
  394. if (host_ptr == g2h_untagged(guest_addr)) {
  395. return;
  396. }
  397. if (len > 0) {
  398. memcpy(g2h_untagged(guest_addr), host_ptr, len);
  399. }
  400. g_free(host_ptr);
  401. #endif
  402. }
  403. /*
  404. * Return the length of a string in target memory or -TARGET_EFAULT if access
  405. * error.
  406. */
  407. abi_long target_strlen(abi_ulong gaddr);
  408. /* Like lock_user but for null terminated strings. */
  409. static inline void *lock_user_string(abi_ulong guest_addr)
  410. {
  411. abi_long len;
  412. len = target_strlen(guest_addr);
  413. if (len < 0) {
  414. return NULL;
  415. }
  416. return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
  417. }
  418. /* Helper macros for locking/unlocking a target struct. */
  419. #define lock_user_struct(type, host_ptr, guest_addr, copy) \
  420. (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
  421. #define unlock_user_struct(host_ptr, guest_addr, copy) \
  422. unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
  423. static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
  424. {
  425. #if TARGET_ABI_BITS == 32
  426. #ifdef TARGET_WORDS_BIGENDIAN
  427. return ((uint64_t)word0 << 32) | word1;
  428. #else
  429. return ((uint64_t)word1 << 32) | word0;
  430. #endif
  431. #else /* TARGET_ABI_BITS != 32 */
  432. return word0;
  433. #endif /* TARGET_ABI_BITS != 32 */
  434. }
  435. #include <pthread.h>
  436. #include "user/safe-syscall.h"
  437. #endif /* QEMU_H */