qemu.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #ifndef QEMU_H
  2. #define QEMU_H
  3. #include <signal.h>
  4. #include <string.h>
  5. #include "cpu.h"
  6. #include "exec/cpu_ldst.h"
  7. #undef DEBUG_REMAP
  8. #ifdef DEBUG_REMAP
  9. #include <stdlib.h>
  10. #endif /* DEBUG_REMAP */
  11. #include "exec/user/abitypes.h"
  12. #include "exec/user/thunk.h"
  13. #include "syscall_defs.h"
  14. #include "syscall.h"
  15. #include "exec/gdbstub.h"
  16. #include "qemu/queue.h"
  17. #define THREAD __thread
  18. /* This struct is used to hold certain information about the image.
  19. * Basically, it replicates in user space what would be certain
  20. * task_struct fields in the kernel
  21. */
  22. struct image_info {
  23. abi_ulong load_bias;
  24. abi_ulong load_addr;
  25. abi_ulong start_code;
  26. abi_ulong end_code;
  27. abi_ulong start_data;
  28. abi_ulong end_data;
  29. abi_ulong start_brk;
  30. abi_ulong brk;
  31. abi_ulong start_mmap;
  32. abi_ulong mmap;
  33. abi_ulong rss;
  34. abi_ulong start_stack;
  35. abi_ulong stack_limit;
  36. abi_ulong entry;
  37. abi_ulong code_offset;
  38. abi_ulong data_offset;
  39. abi_ulong saved_auxv;
  40. abi_ulong auxv_len;
  41. abi_ulong arg_start;
  42. abi_ulong arg_end;
  43. uint32_t elf_flags;
  44. int personality;
  45. #ifdef CONFIG_USE_FDPIC
  46. abi_ulong loadmap_addr;
  47. uint16_t nsegs;
  48. void *loadsegs;
  49. abi_ulong pt_dynamic_addr;
  50. struct image_info *other_info;
  51. #endif
  52. };
  53. #ifdef TARGET_I386
  54. /* Information about the current linux thread */
  55. struct vm86_saved_state {
  56. uint32_t eax; /* return code */
  57. uint32_t ebx;
  58. uint32_t ecx;
  59. uint32_t edx;
  60. uint32_t esi;
  61. uint32_t edi;
  62. uint32_t ebp;
  63. uint32_t esp;
  64. uint32_t eflags;
  65. uint32_t eip;
  66. uint16_t cs, ss, ds, es, fs, gs;
  67. };
  68. #endif
  69. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  70. /* FPU emulator */
  71. #include "nwfpe/fpa11.h"
  72. #endif
  73. #define MAX_SIGQUEUE_SIZE 1024
  74. struct sigqueue {
  75. struct sigqueue *next;
  76. target_siginfo_t info;
  77. };
  78. struct emulated_sigtable {
  79. int pending; /* true if signal is pending */
  80. struct sigqueue *first;
  81. struct sigqueue info; /* in order to always have memory for the
  82. first signal, we put it here */
  83. };
  84. /* NOTE: we force a big alignment so that the stack stored after is
  85. aligned too */
  86. typedef struct TaskState {
  87. pid_t ts_tid; /* tid (or pid) of this task */
  88. #ifdef TARGET_ARM
  89. # ifdef TARGET_ABI32
  90. /* FPA state */
  91. FPA11 fpa;
  92. # endif
  93. int swi_errno;
  94. #endif
  95. #ifdef TARGET_UNICORE32
  96. int swi_errno;
  97. #endif
  98. #if defined(TARGET_I386) && !defined(TARGET_X86_64)
  99. abi_ulong target_v86;
  100. struct vm86_saved_state vm86_saved_regs;
  101. struct target_vm86plus_struct vm86plus;
  102. uint32_t v86flags;
  103. uint32_t v86mask;
  104. #endif
  105. abi_ulong child_tidptr;
  106. #ifdef TARGET_M68K
  107. int sim_syscalls;
  108. abi_ulong tp_value;
  109. #endif
  110. #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
  111. /* Extra fields for semihosted binaries. */
  112. uint32_t heap_base;
  113. uint32_t heap_limit;
  114. #endif
  115. uint32_t stack_base;
  116. int used; /* non zero if used */
  117. bool sigsegv_blocked; /* SIGSEGV blocked by guest */
  118. struct image_info *info;
  119. struct linux_binprm *bprm;
  120. struct emulated_sigtable sigtab[TARGET_NSIG];
  121. struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
  122. struct sigqueue *first_free; /* first free siginfo queue entry */
  123. int signal_pending; /* non zero if a signal may be pending */
  124. } __attribute__((aligned(16))) TaskState;
  125. extern char *exec_path;
  126. void init_task_state(TaskState *ts);
  127. void task_settid(TaskState *);
  128. void stop_all_tasks(void);
  129. extern const char *qemu_uname_release;
  130. extern unsigned long mmap_min_addr;
  131. /* ??? See if we can avoid exposing so much of the loader internals. */
  132. /*
  133. * MAX_ARG_PAGES defines the number of pages allocated for arguments
  134. * and envelope for the new program. 32 should suffice, this gives
  135. * a maximum env+arg of 128kB w/4KB pages!
  136. */
  137. #define MAX_ARG_PAGES 33
  138. /* Read a good amount of data initially, to hopefully get all the
  139. program headers loaded. */
  140. #define BPRM_BUF_SIZE 1024
  141. /*
  142. * This structure is used to hold the arguments that are
  143. * used when loading binaries.
  144. */
  145. struct linux_binprm {
  146. char buf[BPRM_BUF_SIZE] __attribute__((aligned));
  147. void *page[MAX_ARG_PAGES];
  148. abi_ulong p;
  149. int fd;
  150. int e_uid, e_gid;
  151. int argc, envc;
  152. char **argv;
  153. char **envp;
  154. char * filename; /* Name of binary */
  155. int (*core_dump)(int, const CPUArchState *); /* coredump routine */
  156. };
  157. void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
  158. abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
  159. abi_ulong stringp, int push_ptr);
  160. int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
  161. struct target_pt_regs * regs, struct image_info *infop,
  162. struct linux_binprm *);
  163. int load_elf_binary(struct linux_binprm *bprm, struct image_info *info);
  164. int load_flt_binary(struct linux_binprm *bprm, struct image_info *info);
  165. abi_long memcpy_to_target(abi_ulong dest, const void *src,
  166. unsigned long len);
  167. void target_set_brk(abi_ulong new_brk);
  168. abi_long do_brk(abi_ulong new_brk);
  169. void syscall_init(void);
  170. abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
  171. abi_long arg2, abi_long arg3, abi_long arg4,
  172. abi_long arg5, abi_long arg6, abi_long arg7,
  173. abi_long arg8);
  174. void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
  175. extern THREAD CPUState *thread_cpu;
  176. void cpu_loop(CPUArchState *env);
  177. char *target_strerror(int err);
  178. int get_osversion(void);
  179. void init_qemu_uname_release(void);
  180. void fork_start(void);
  181. void fork_end(int child);
  182. /* Creates the initial guest address space in the host memory space using
  183. * the given host start address hint and size. The guest_start parameter
  184. * specifies the start address of the guest space. guest_base will be the
  185. * difference between the host start address computed by this function and
  186. * guest_start. If fixed is specified, then the mapped address space must
  187. * start at host_start. The real start address of the mapped memory space is
  188. * returned or -1 if there was an error.
  189. */
  190. unsigned long init_guest_space(unsigned long host_start,
  191. unsigned long host_size,
  192. unsigned long guest_start,
  193. bool fixed);
  194. #include "qemu/log.h"
  195. /* syscall.c */
  196. int host_to_target_waitstatus(int status);
  197. /* strace.c */
  198. void print_syscall(int num,
  199. abi_long arg1, abi_long arg2, abi_long arg3,
  200. abi_long arg4, abi_long arg5, abi_long arg6);
  201. void print_syscall_ret(int num, abi_long arg1);
  202. extern int do_strace;
  203. /* signal.c */
  204. void process_pending_signals(CPUArchState *cpu_env);
  205. void signal_init(void);
  206. int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info);
  207. void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
  208. void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
  209. int target_to_host_signal(int sig);
  210. int host_to_target_signal(int sig);
  211. long do_sigreturn(CPUArchState *env);
  212. long do_rt_sigreturn(CPUArchState *env);
  213. abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
  214. int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
  215. #ifdef TARGET_I386
  216. /* vm86.c */
  217. void save_v86_state(CPUX86State *env);
  218. void handle_vm86_trap(CPUX86State *env, int trapno);
  219. void handle_vm86_fault(CPUX86State *env);
  220. int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
  221. #elif defined(TARGET_SPARC64)
  222. void sparc64_set_context(CPUSPARCState *env);
  223. void sparc64_get_context(CPUSPARCState *env);
  224. #endif
  225. /* mmap.c */
  226. int target_mprotect(abi_ulong start, abi_ulong len, int prot);
  227. abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
  228. int flags, int fd, abi_ulong offset);
  229. int target_munmap(abi_ulong start, abi_ulong len);
  230. abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
  231. abi_ulong new_size, unsigned long flags,
  232. abi_ulong new_addr);
  233. int target_msync(abi_ulong start, abi_ulong len, int flags);
  234. extern unsigned long last_brk;
  235. extern abi_ulong mmap_next_start;
  236. void mmap_lock(void);
  237. void mmap_unlock(void);
  238. abi_ulong mmap_find_vma(abi_ulong, abi_ulong);
  239. void cpu_list_lock(void);
  240. void cpu_list_unlock(void);
  241. void mmap_fork_start(void);
  242. void mmap_fork_end(int child);
  243. /* main.c */
  244. extern unsigned long guest_stack_size;
  245. /* user access */
  246. #define VERIFY_READ 0
  247. #define VERIFY_WRITE 1 /* implies read access */
  248. static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
  249. {
  250. return page_check_range((target_ulong)addr, size,
  251. (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0;
  252. }
  253. /* NOTE __get_user and __put_user use host pointers and don't check access.
  254. These are usually used to access struct data members once the struct has
  255. been locked - usually with lock_user_struct. */
  256. /* Tricky points:
  257. - Use __builtin_choose_expr to avoid type promotion from ?:,
  258. - Invalid sizes result in a compile time error stemming from
  259. the fact that abort has no parameters.
  260. - It's easier to use the endian-specific unaligned load/store
  261. functions than host-endian unaligned load/store plus tswapN. */
  262. #define __put_user_e(x, hptr, e) \
  263. (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
  264. __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
  265. __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
  266. __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
  267. ((hptr), (x)), (void)0)
  268. #define __get_user_e(x, hptr, e) \
  269. ((x) = (typeof(*hptr))( \
  270. __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
  271. __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
  272. __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
  273. __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
  274. (hptr)), (void)0)
  275. #ifdef TARGET_WORDS_BIGENDIAN
  276. # define __put_user(x, hptr) __put_user_e(x, hptr, be)
  277. # define __get_user(x, hptr) __get_user_e(x, hptr, be)
  278. #else
  279. # define __put_user(x, hptr) __put_user_e(x, hptr, le)
  280. # define __get_user(x, hptr) __get_user_e(x, hptr, le)
  281. #endif
  282. /* put_user()/get_user() take a guest address and check access */
  283. /* These are usually used to access an atomic data type, such as an int,
  284. * that has been passed by address. These internally perform locking
  285. * and unlocking on the data type.
  286. */
  287. #define put_user(x, gaddr, target_type) \
  288. ({ \
  289. abi_ulong __gaddr = (gaddr); \
  290. target_type *__hptr; \
  291. abi_long __ret = 0; \
  292. if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
  293. __put_user((x), __hptr); \
  294. unlock_user(__hptr, __gaddr, sizeof(target_type)); \
  295. } else \
  296. __ret = -TARGET_EFAULT; \
  297. __ret; \
  298. })
  299. #define get_user(x, gaddr, target_type) \
  300. ({ \
  301. abi_ulong __gaddr = (gaddr); \
  302. target_type *__hptr; \
  303. abi_long __ret = 0; \
  304. if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
  305. __get_user((x), __hptr); \
  306. unlock_user(__hptr, __gaddr, 0); \
  307. } else { \
  308. /* avoid warning */ \
  309. (x) = 0; \
  310. __ret = -TARGET_EFAULT; \
  311. } \
  312. __ret; \
  313. })
  314. #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
  315. #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
  316. #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
  317. #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
  318. #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
  319. #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
  320. #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
  321. #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
  322. #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
  323. #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
  324. #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
  325. #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
  326. #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
  327. #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
  328. #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
  329. #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
  330. #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
  331. #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
  332. #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
  333. #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
  334. /* copy_from_user() and copy_to_user() are usually used to copy data
  335. * buffers between the target and host. These internally perform
  336. * locking/unlocking of the memory.
  337. */
  338. abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
  339. abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
  340. /* Functions for accessing guest memory. The tget and tput functions
  341. read/write single values, byteswapping as necessary. The lock_user function
  342. gets a pointer to a contiguous area of guest memory, but does not perform
  343. any byteswapping. lock_user may return either a pointer to the guest
  344. memory, or a temporary buffer. */
  345. /* Lock an area of guest memory into the host. If copy is true then the
  346. host area will have the same contents as the guest. */
  347. static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy)
  348. {
  349. if (!access_ok(type, guest_addr, len))
  350. return NULL;
  351. #ifdef DEBUG_REMAP
  352. {
  353. void *addr;
  354. addr = malloc(len);
  355. if (copy)
  356. memcpy(addr, g2h(guest_addr), len);
  357. else
  358. memset(addr, 0, len);
  359. return addr;
  360. }
  361. #else
  362. return g2h(guest_addr);
  363. #endif
  364. }
  365. /* Unlock an area of guest memory. The first LEN bytes must be
  366. flushed back to guest memory. host_ptr = NULL is explicitly
  367. allowed and does nothing. */
  368. static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
  369. long len)
  370. {
  371. #ifdef DEBUG_REMAP
  372. if (!host_ptr)
  373. return;
  374. if (host_ptr == g2h(guest_addr))
  375. return;
  376. if (len > 0)
  377. memcpy(g2h(guest_addr), host_ptr, len);
  378. free(host_ptr);
  379. #endif
  380. }
  381. /* Return the length of a string in target memory or -TARGET_EFAULT if
  382. access error. */
  383. abi_long target_strlen(abi_ulong gaddr);
  384. /* Like lock_user but for null terminated strings. */
  385. static inline void *lock_user_string(abi_ulong guest_addr)
  386. {
  387. abi_long len;
  388. len = target_strlen(guest_addr);
  389. if (len < 0)
  390. return NULL;
  391. return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
  392. }
  393. /* Helper macros for locking/unlocking a target struct. */
  394. #define lock_user_struct(type, host_ptr, guest_addr, copy) \
  395. (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
  396. #define unlock_user_struct(host_ptr, guest_addr, copy) \
  397. unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
  398. #include <pthread.h>
  399. /* Include target-specific struct and function definitions;
  400. * they may need access to the target-independent structures
  401. * above, so include them last.
  402. */
  403. #include "target_cpu.h"
  404. #include "target_signal.h"
  405. #include "target_structs.h"
  406. #endif /* QEMU_H */