2
0

qemu.h 15 KB

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