2
0

qemu.h 13 KB

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