qemu.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. #include "thunk.h"
  12. #include "syscall_defs.h"
  13. #include "syscall.h"
  14. #include "target_signal.h"
  15. #include "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. /* Return true if the proposed guest_base is suitable for the guest.
  186. * The guest code may leave a page mapped and populate it if the
  187. * address is suitable.
  188. */
  189. bool guest_validate_base(unsigned long guest_base);
  190. #include "qemu-log.h"
  191. /* strace.c */
  192. void print_syscall(int num,
  193. abi_long arg1, abi_long arg2, abi_long arg3,
  194. abi_long arg4, abi_long arg5, abi_long arg6);
  195. void print_syscall_ret(int num, abi_long arg1);
  196. extern int do_strace;
  197. /* signal.c */
  198. void process_pending_signals(CPUArchState *cpu_env);
  199. void signal_init(void);
  200. int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info);
  201. void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
  202. void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
  203. int target_to_host_signal(int sig);
  204. int host_to_target_signal(int sig);
  205. long do_sigreturn(CPUArchState *env);
  206. long do_rt_sigreturn(CPUArchState *env);
  207. abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
  208. #ifdef TARGET_I386
  209. /* vm86.c */
  210. void save_v86_state(CPUX86State *env);
  211. void handle_vm86_trap(CPUX86State *env, int trapno);
  212. void handle_vm86_fault(CPUX86State *env);
  213. int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
  214. #elif defined(TARGET_SPARC64)
  215. void sparc64_set_context(CPUSPARCState *env);
  216. void sparc64_get_context(CPUSPARCState *env);
  217. #endif
  218. /* mmap.c */
  219. int target_mprotect(abi_ulong start, abi_ulong len, int prot);
  220. abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
  221. int flags, int fd, abi_ulong offset);
  222. int target_munmap(abi_ulong start, abi_ulong len);
  223. abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
  224. abi_ulong new_size, unsigned long flags,
  225. abi_ulong new_addr);
  226. int target_msync(abi_ulong start, abi_ulong len, int flags);
  227. extern unsigned long last_brk;
  228. extern abi_ulong mmap_next_start;
  229. void mmap_lock(void);
  230. void mmap_unlock(void);
  231. abi_ulong mmap_find_vma(abi_ulong, abi_ulong);
  232. void cpu_list_lock(void);
  233. void cpu_list_unlock(void);
  234. #if defined(CONFIG_USE_NPTL)
  235. void mmap_fork_start(void);
  236. void mmap_fork_end(int child);
  237. #endif
  238. /* main.c */
  239. extern unsigned long guest_stack_size;
  240. /* user access */
  241. #define VERIFY_READ 0
  242. #define VERIFY_WRITE 1 /* implies read access */
  243. static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
  244. {
  245. return page_check_range((target_ulong)addr, size,
  246. (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0;
  247. }
  248. /* NOTE __get_user and __put_user use host pointers and don't check access. */
  249. /* These are usually used to access struct data members once the
  250. * struct has been locked - usually with lock_user_struct().
  251. */
  252. #define __put_user(x, hptr)\
  253. ({\
  254. switch(sizeof(*hptr)) {\
  255. case 1:\
  256. *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
  257. break;\
  258. case 2:\
  259. *(uint16_t *)(hptr) = tswap16((uint16_t)(typeof(*hptr))(x));\
  260. break;\
  261. case 4:\
  262. *(uint32_t *)(hptr) = tswap32((uint32_t)(typeof(*hptr))(x));\
  263. break;\
  264. case 8:\
  265. *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
  266. break;\
  267. default:\
  268. abort();\
  269. }\
  270. 0;\
  271. })
  272. #define __get_user(x, hptr) \
  273. ({\
  274. switch(sizeof(*hptr)) {\
  275. case 1:\
  276. x = (typeof(*hptr))*(uint8_t *)(hptr);\
  277. break;\
  278. case 2:\
  279. x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
  280. break;\
  281. case 4:\
  282. x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
  283. break;\
  284. case 8:\
  285. x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
  286. break;\
  287. default:\
  288. /* avoid warning */\
  289. x = 0;\
  290. abort();\
  291. }\
  292. 0;\
  293. })
  294. /* put_user()/get_user() take a guest address and check access */
  295. /* These are usually used to access an atomic data type, such as an int,
  296. * that has been passed by address. These internally perform locking
  297. * and unlocking on the data type.
  298. */
  299. #define put_user(x, gaddr, target_type) \
  300. ({ \
  301. abi_ulong __gaddr = (gaddr); \
  302. target_type *__hptr; \
  303. abi_long __ret; \
  304. if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
  305. __ret = __put_user((x), __hptr); \
  306. unlock_user(__hptr, __gaddr, sizeof(target_type)); \
  307. } else \
  308. __ret = -TARGET_EFAULT; \
  309. __ret; \
  310. })
  311. #define get_user(x, gaddr, target_type) \
  312. ({ \
  313. abi_ulong __gaddr = (gaddr); \
  314. target_type *__hptr; \
  315. abi_long __ret; \
  316. if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
  317. __ret = __get_user((x), __hptr); \
  318. unlock_user(__hptr, __gaddr, 0); \
  319. } else { \
  320. /* avoid warning */ \
  321. (x) = 0; \
  322. __ret = -TARGET_EFAULT; \
  323. } \
  324. __ret; \
  325. })
  326. #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
  327. #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
  328. #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
  329. #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
  330. #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
  331. #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
  332. #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
  333. #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
  334. #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
  335. #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
  336. #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
  337. #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
  338. #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
  339. #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
  340. #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
  341. #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
  342. #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
  343. #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
  344. #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
  345. #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
  346. /* copy_from_user() and copy_to_user() are usually used to copy data
  347. * buffers between the target and host. These internally perform
  348. * locking/unlocking of the memory.
  349. */
  350. abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
  351. abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
  352. /* Functions for accessing guest memory. The tget and tput functions
  353. read/write single values, byteswapping as necessary. The lock_user
  354. gets a pointer to a contiguous area of guest memory, but does not perform
  355. and byteswapping. lock_user may return either a pointer to the guest
  356. memory, or a temporary buffer. */
  357. /* Lock an area of guest memory into the host. If copy is true then the
  358. host area will have the same contents as the guest. */
  359. static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy)
  360. {
  361. if (!access_ok(type, guest_addr, len))
  362. return NULL;
  363. #ifdef DEBUG_REMAP
  364. {
  365. void *addr;
  366. addr = malloc(len);
  367. if (copy)
  368. memcpy(addr, g2h(guest_addr), len);
  369. else
  370. memset(addr, 0, len);
  371. return addr;
  372. }
  373. #else
  374. return g2h(guest_addr);
  375. #endif
  376. }
  377. /* Unlock an area of guest memory. The first LEN bytes must be
  378. flushed back to guest memory. host_ptr = NULL is explicitly
  379. allowed and does nothing. */
  380. static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
  381. long len)
  382. {
  383. #ifdef DEBUG_REMAP
  384. if (!host_ptr)
  385. return;
  386. if (host_ptr == g2h(guest_addr))
  387. return;
  388. if (len > 0)
  389. memcpy(g2h(guest_addr), host_ptr, len);
  390. free(host_ptr);
  391. #endif
  392. }
  393. /* Return the length of a string in target memory or -TARGET_EFAULT if
  394. access error. */
  395. abi_long target_strlen(abi_ulong gaddr);
  396. /* Like lock_user but for null terminated strings. */
  397. static inline void *lock_user_string(abi_ulong guest_addr)
  398. {
  399. abi_long len;
  400. len = target_strlen(guest_addr);
  401. if (len < 0)
  402. return NULL;
  403. return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
  404. }
  405. /* Helper macros for locking/ulocking a target struct. */
  406. #define lock_user_struct(type, host_ptr, guest_addr, copy) \
  407. (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
  408. #define unlock_user_struct(host_ptr, guest_addr, copy) \
  409. unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
  410. #if defined(CONFIG_USE_NPTL)
  411. #include <pthread.h>
  412. #endif
  413. #endif /* QEMU_H */