2
0

qemu.h 15 KB

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