qemu.h 15 KB

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