2
0

qemu.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #ifndef QEMU_H
  2. #define QEMU_H
  3. #include <signal.h>
  4. #include <string.h>
  5. #include "cpu.h"
  6. #undef DEBUG_REMAP
  7. #ifdef DEBUG_REMAP
  8. #include <stdlib.h>
  9. #endif /* DEBUG_REMAP */
  10. #include "exec/user/abitypes.h"
  11. enum BSDType {
  12. target_freebsd,
  13. target_netbsd,
  14. target_openbsd,
  15. };
  16. extern enum BSDType bsd_type;
  17. #include "syscall_defs.h"
  18. #include "syscall.h"
  19. #include "target_signal.h"
  20. #include "exec/gdbstub.h"
  21. #if defined(CONFIG_USE_NPTL)
  22. #define THREAD __thread
  23. #else
  24. #define THREAD
  25. #endif
  26. /* This struct is used to hold certain information about the image.
  27. * Basically, it replicates in user space what would be certain
  28. * task_struct fields in the kernel
  29. */
  30. struct image_info {
  31. abi_ulong load_addr;
  32. abi_ulong start_code;
  33. abi_ulong end_code;
  34. abi_ulong start_data;
  35. abi_ulong end_data;
  36. abi_ulong start_brk;
  37. abi_ulong brk;
  38. abi_ulong start_mmap;
  39. abi_ulong mmap;
  40. abi_ulong rss;
  41. abi_ulong start_stack;
  42. abi_ulong entry;
  43. abi_ulong code_offset;
  44. abi_ulong data_offset;
  45. int personality;
  46. };
  47. #define MAX_SIGQUEUE_SIZE 1024
  48. struct sigqueue {
  49. struct sigqueue *next;
  50. //target_siginfo_t info;
  51. };
  52. struct emulated_sigtable {
  53. int pending; /* true if signal is pending */
  54. struct sigqueue *first;
  55. struct sigqueue info; /* in order to always have memory for the
  56. first signal, we put it here */
  57. };
  58. /* NOTE: we force a big alignment so that the stack stored after is
  59. aligned too */
  60. typedef struct TaskState {
  61. struct TaskState *next;
  62. int used; /* non zero if used */
  63. struct image_info *info;
  64. struct emulated_sigtable sigtab[TARGET_NSIG];
  65. struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
  66. struct sigqueue *first_free; /* first free siginfo queue entry */
  67. int signal_pending; /* non zero if a signal may be pending */
  68. uint8_t stack[0];
  69. } __attribute__((aligned(16))) TaskState;
  70. void init_task_state(TaskState *ts);
  71. extern const char *qemu_uname_release;
  72. #if defined(CONFIG_USE_GUEST_BASE)
  73. extern unsigned long mmap_min_addr;
  74. #endif
  75. /* ??? See if we can avoid exposing so much of the loader internals. */
  76. /*
  77. * MAX_ARG_PAGES defines the number of pages allocated for arguments
  78. * and envelope for the new program. 32 should suffice, this gives
  79. * a maximum env+arg of 128kB w/4KB pages!
  80. */
  81. #define MAX_ARG_PAGES 32
  82. /*
  83. * This structure is used to hold the arguments that are
  84. * used when loading binaries.
  85. */
  86. struct linux_binprm {
  87. char buf[128];
  88. void *page[MAX_ARG_PAGES];
  89. abi_ulong p;
  90. int fd;
  91. int e_uid, e_gid;
  92. int argc, envc;
  93. char **argv;
  94. char **envp;
  95. char * filename; /* Name of binary */
  96. };
  97. void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
  98. abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
  99. abi_ulong stringp, int push_ptr);
  100. int loader_exec(const char * filename, char ** argv, char ** envp,
  101. struct target_pt_regs * regs, struct image_info *infop);
  102. int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
  103. struct image_info * info);
  104. int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
  105. struct image_info * info);
  106. abi_long memcpy_to_target(abi_ulong dest, const void *src,
  107. unsigned long len);
  108. void target_set_brk(abi_ulong new_brk);
  109. abi_long do_brk(abi_ulong new_brk);
  110. void syscall_init(void);
  111. abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
  112. abi_long arg2, abi_long arg3, abi_long arg4,
  113. abi_long arg5, abi_long arg6, abi_long arg7,
  114. abi_long arg8);
  115. abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
  116. abi_long arg2, abi_long arg3, abi_long arg4,
  117. abi_long arg5, abi_long arg6);
  118. abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
  119. abi_long arg2, abi_long arg3, abi_long arg4,
  120. abi_long arg5, abi_long arg6);
  121. void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
  122. extern THREAD CPUArchState *thread_env;
  123. void cpu_loop(CPUArchState *env);
  124. char *target_strerror(int err);
  125. int get_osversion(void);
  126. void fork_start(void);
  127. void fork_end(int child);
  128. #include "qemu/log.h"
  129. /* strace.c */
  130. void
  131. print_freebsd_syscall(int num,
  132. abi_long arg1, abi_long arg2, abi_long arg3,
  133. abi_long arg4, abi_long arg5, abi_long arg6);
  134. void print_freebsd_syscall_ret(int num, abi_long ret);
  135. void
  136. print_netbsd_syscall(int num,
  137. abi_long arg1, abi_long arg2, abi_long arg3,
  138. abi_long arg4, abi_long arg5, abi_long arg6);
  139. void print_netbsd_syscall_ret(int num, abi_long ret);
  140. void
  141. print_openbsd_syscall(int num,
  142. abi_long arg1, abi_long arg2, abi_long arg3,
  143. abi_long arg4, abi_long arg5, abi_long arg6);
  144. void print_openbsd_syscall_ret(int num, abi_long ret);
  145. extern int do_strace;
  146. /* signal.c */
  147. void process_pending_signals(CPUArchState *cpu_env);
  148. void signal_init(void);
  149. //int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info);
  150. //void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
  151. //void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
  152. long do_sigreturn(CPUArchState *env);
  153. long do_rt_sigreturn(CPUArchState *env);
  154. abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
  155. /* mmap.c */
  156. int target_mprotect(abi_ulong start, abi_ulong len, int prot);
  157. abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
  158. int flags, int fd, abi_ulong offset);
  159. int target_munmap(abi_ulong start, abi_ulong len);
  160. abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
  161. abi_ulong new_size, unsigned long flags,
  162. abi_ulong new_addr);
  163. int target_msync(abi_ulong start, abi_ulong len, int flags);
  164. extern unsigned long last_brk;
  165. void mmap_lock(void);
  166. void mmap_unlock(void);
  167. void cpu_list_lock(void);
  168. void cpu_list_unlock(void);
  169. #if defined(CONFIG_USE_NPTL)
  170. void mmap_fork_start(void);
  171. void mmap_fork_end(int child);
  172. #endif
  173. /* main.c */
  174. extern unsigned long x86_stack_size;
  175. /* user access */
  176. #define VERIFY_READ 0
  177. #define VERIFY_WRITE 1 /* implies read access */
  178. static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
  179. {
  180. return page_check_range((target_ulong)addr, size,
  181. (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0;
  182. }
  183. /* NOTE __get_user and __put_user use host pointers and don't check access. */
  184. /* These are usually used to access struct data members once the
  185. * struct has been locked - usually with lock_user_struct().
  186. */
  187. #define __put_user(x, hptr)\
  188. ({\
  189. int size = sizeof(*hptr);\
  190. switch(size) {\
  191. case 1:\
  192. *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
  193. break;\
  194. case 2:\
  195. *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
  196. break;\
  197. case 4:\
  198. *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
  199. break;\
  200. case 8:\
  201. *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
  202. break;\
  203. default:\
  204. abort();\
  205. }\
  206. 0;\
  207. })
  208. #define __get_user(x, hptr) \
  209. ({\
  210. int size = sizeof(*hptr);\
  211. switch(size) {\
  212. case 1:\
  213. x = (typeof(*hptr))*(uint8_t *)(hptr);\
  214. break;\
  215. case 2:\
  216. x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
  217. break;\
  218. case 4:\
  219. x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
  220. break;\
  221. case 8:\
  222. x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
  223. break;\
  224. default:\
  225. /* avoid warning */\
  226. x = 0;\
  227. abort();\
  228. }\
  229. 0;\
  230. })
  231. /* put_user()/get_user() take a guest address and check access */
  232. /* These are usually used to access an atomic data type, such as an int,
  233. * that has been passed by address. These internally perform locking
  234. * and unlocking on the data type.
  235. */
  236. #define put_user(x, gaddr, target_type) \
  237. ({ \
  238. abi_ulong __gaddr = (gaddr); \
  239. target_type *__hptr; \
  240. abi_long __ret; \
  241. if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
  242. __ret = __put_user((x), __hptr); \
  243. unlock_user(__hptr, __gaddr, sizeof(target_type)); \
  244. } else \
  245. __ret = -TARGET_EFAULT; \
  246. __ret; \
  247. })
  248. #define get_user(x, gaddr, target_type) \
  249. ({ \
  250. abi_ulong __gaddr = (gaddr); \
  251. target_type *__hptr; \
  252. abi_long __ret; \
  253. if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
  254. __ret = __get_user((x), __hptr); \
  255. unlock_user(__hptr, __gaddr, 0); \
  256. } else { \
  257. /* avoid warning */ \
  258. (x) = 0; \
  259. __ret = -TARGET_EFAULT; \
  260. } \
  261. __ret; \
  262. })
  263. #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
  264. #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
  265. #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
  266. #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
  267. #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
  268. #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
  269. #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
  270. #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
  271. #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
  272. #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
  273. #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
  274. #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
  275. #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
  276. #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
  277. #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
  278. #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
  279. #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
  280. #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
  281. #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
  282. #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
  283. /* copy_from_user() and copy_to_user() are usually used to copy data
  284. * buffers between the target and host. These internally perform
  285. * locking/unlocking of the memory.
  286. */
  287. abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
  288. abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
  289. /* Functions for accessing guest memory. The tget and tput functions
  290. read/write single values, byteswapping as necessary. The lock_user
  291. gets a pointer to a contiguous area of guest memory, but does not perform
  292. and byteswapping. lock_user may return either a pointer to the guest
  293. memory, or a temporary buffer. */
  294. /* Lock an area of guest memory into the host. If copy is true then the
  295. host area will have the same contents as the guest. */
  296. static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy)
  297. {
  298. if (!access_ok(type, guest_addr, len))
  299. return NULL;
  300. #ifdef DEBUG_REMAP
  301. {
  302. void *addr;
  303. addr = malloc(len);
  304. if (copy)
  305. memcpy(addr, g2h(guest_addr), len);
  306. else
  307. memset(addr, 0, len);
  308. return addr;
  309. }
  310. #else
  311. return g2h(guest_addr);
  312. #endif
  313. }
  314. /* Unlock an area of guest memory. The first LEN bytes must be
  315. flushed back to guest memory. host_ptr = NULL is explicitly
  316. allowed and does nothing. */
  317. static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
  318. long len)
  319. {
  320. #ifdef DEBUG_REMAP
  321. if (!host_ptr)
  322. return;
  323. if (host_ptr == g2h(guest_addr))
  324. return;
  325. if (len > 0)
  326. memcpy(g2h(guest_addr), host_ptr, len);
  327. free(host_ptr);
  328. #endif
  329. }
  330. /* Return the length of a string in target memory or -TARGET_EFAULT if
  331. access error. */
  332. abi_long target_strlen(abi_ulong gaddr);
  333. /* Like lock_user but for null terminated strings. */
  334. static inline void *lock_user_string(abi_ulong guest_addr)
  335. {
  336. abi_long len;
  337. len = target_strlen(guest_addr);
  338. if (len < 0)
  339. return NULL;
  340. return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
  341. }
  342. /* Helper macros for locking/ulocking a target struct. */
  343. #define lock_user_struct(type, host_ptr, guest_addr, copy) \
  344. (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
  345. #define unlock_user_struct(host_ptr, guest_addr, copy) \
  346. unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
  347. #if defined(CONFIG_USE_NPTL)
  348. #include <pthread.h>
  349. #endif
  350. #endif /* QEMU_H */