qemu.h 13 KB

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