qemu.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * qemu bsd user mode definition
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef QEMU_H
  18. #define QEMU_H
  19. #include "cpu.h"
  20. #include "exec/cpu_ldst.h"
  21. #undef DEBUG_REMAP
  22. #ifdef DEBUG_REMAP
  23. #endif /* DEBUG_REMAP */
  24. #include "exec/user/abitypes.h"
  25. enum BSDType {
  26. target_freebsd,
  27. target_netbsd,
  28. target_openbsd,
  29. };
  30. extern enum BSDType bsd_type;
  31. #include "syscall_defs.h"
  32. #include "target_syscall.h"
  33. #include "target_signal.h"
  34. #include "exec/gdbstub.h"
  35. #if defined(CONFIG_USE_NPTL)
  36. #define THREAD __thread
  37. #else
  38. #define THREAD
  39. #endif
  40. /* This struct is used to hold certain information about the image.
  41. * Basically, it replicates in user space what would be certain
  42. * task_struct fields in the kernel
  43. */
  44. struct image_info {
  45. abi_ulong load_addr;
  46. abi_ulong start_code;
  47. abi_ulong end_code;
  48. abi_ulong start_data;
  49. abi_ulong end_data;
  50. abi_ulong start_brk;
  51. abi_ulong brk;
  52. abi_ulong start_mmap;
  53. abi_ulong mmap;
  54. abi_ulong rss;
  55. abi_ulong start_stack;
  56. abi_ulong entry;
  57. abi_ulong code_offset;
  58. abi_ulong data_offset;
  59. int personality;
  60. };
  61. #define MAX_SIGQUEUE_SIZE 1024
  62. struct sigqueue {
  63. struct sigqueue *next;
  64. //target_siginfo_t info;
  65. };
  66. struct emulated_sigtable {
  67. int pending; /* true if signal is pending */
  68. struct sigqueue *first;
  69. struct sigqueue info; /* in order to always have memory for the
  70. first signal, we put it here */
  71. };
  72. /* NOTE: we force a big alignment so that the stack stored after is
  73. aligned too */
  74. typedef struct TaskState {
  75. pid_t ts_tid; /* tid (or pid) of this task */
  76. struct TaskState *next;
  77. int used; /* non zero if used */
  78. struct image_info *info;
  79. struct emulated_sigtable sigtab[TARGET_NSIG];
  80. struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
  81. struct sigqueue *first_free; /* first free siginfo queue entry */
  82. int signal_pending; /* non zero if a signal may be pending */
  83. uint8_t stack[0];
  84. } __attribute__((aligned(16))) TaskState;
  85. void init_task_state(TaskState *ts);
  86. extern const char *qemu_uname_release;
  87. extern unsigned long mmap_min_addr;
  88. /* ??? See if we can avoid exposing so much of the loader internals. */
  89. /*
  90. * MAX_ARG_PAGES defines the number of pages allocated for arguments
  91. * and envelope for the new program. 32 should suffice, this gives
  92. * a maximum env+arg of 128kB w/4KB pages!
  93. */
  94. #define MAX_ARG_PAGES 32
  95. /*
  96. * This structure is used to hold the arguments that are
  97. * used when loading binaries.
  98. */
  99. struct linux_binprm {
  100. char buf[128];
  101. void *page[MAX_ARG_PAGES];
  102. abi_ulong p;
  103. int fd;
  104. int e_uid, e_gid;
  105. int argc, envc;
  106. char **argv;
  107. char **envp;
  108. char * filename; /* Name of binary */
  109. };
  110. void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
  111. abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
  112. abi_ulong stringp, int push_ptr);
  113. int loader_exec(const char * filename, char ** argv, char ** envp,
  114. struct target_pt_regs * regs, struct image_info *infop);
  115. int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
  116. struct image_info * info);
  117. int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
  118. struct image_info * info);
  119. abi_long memcpy_to_target(abi_ulong dest, const void *src,
  120. unsigned long len);
  121. void target_set_brk(abi_ulong new_brk);
  122. abi_long do_brk(abi_ulong new_brk);
  123. void syscall_init(void);
  124. abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
  125. abi_long arg2, abi_long arg3, abi_long arg4,
  126. abi_long arg5, abi_long arg6, abi_long arg7,
  127. abi_long arg8);
  128. abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
  129. abi_long arg2, abi_long arg3, abi_long arg4,
  130. abi_long arg5, abi_long arg6);
  131. abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
  132. abi_long arg2, abi_long arg3, abi_long arg4,
  133. abi_long arg5, abi_long arg6);
  134. void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
  135. extern THREAD CPUState *thread_cpu;
  136. void cpu_loop(CPUArchState *env);
  137. char *target_strerror(int err);
  138. int get_osversion(void);
  139. void fork_start(void);
  140. void fork_end(int child);
  141. #include "qemu/log.h"
  142. /* strace.c */
  143. struct syscallname {
  144. int nr;
  145. const char *name;
  146. const char *format;
  147. void (*call)(const struct syscallname *,
  148. abi_long, abi_long, abi_long,
  149. abi_long, abi_long, abi_long);
  150. void (*result)(const struct syscallname *, abi_long);
  151. };
  152. void
  153. print_freebsd_syscall(int num,
  154. abi_long arg1, abi_long arg2, abi_long arg3,
  155. abi_long arg4, abi_long arg5, abi_long arg6);
  156. void print_freebsd_syscall_ret(int num, abi_long ret);
  157. void
  158. print_netbsd_syscall(int num,
  159. abi_long arg1, abi_long arg2, abi_long arg3,
  160. abi_long arg4, abi_long arg5, abi_long arg6);
  161. void print_netbsd_syscall_ret(int num, abi_long ret);
  162. void
  163. print_openbsd_syscall(int num,
  164. abi_long arg1, abi_long arg2, abi_long arg3,
  165. abi_long arg4, abi_long arg5, abi_long arg6);
  166. void print_openbsd_syscall_ret(int num, abi_long ret);
  167. extern int do_strace;
  168. /* signal.c */
  169. void process_pending_signals(CPUArchState *cpu_env);
  170. void signal_init(void);
  171. //int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info);
  172. //void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
  173. //void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
  174. long do_sigreturn(CPUArchState *env);
  175. long do_rt_sigreturn(CPUArchState *env);
  176. abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
  177. /* mmap.c */
  178. int target_mprotect(abi_ulong start, abi_ulong len, int prot);
  179. abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
  180. int flags, int fd, abi_ulong offset);
  181. int target_munmap(abi_ulong start, abi_ulong len);
  182. abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
  183. abi_ulong new_size, unsigned long flags,
  184. abi_ulong new_addr);
  185. int target_msync(abi_ulong start, abi_ulong len, int flags);
  186. extern unsigned long last_brk;
  187. void mmap_fork_start(void);
  188. void mmap_fork_end(int child);
  189. /* main.c */
  190. extern unsigned long x86_stack_size;
  191. /* user access */
  192. #define VERIFY_READ 0
  193. #define VERIFY_WRITE 1 /* implies read access */
  194. static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
  195. {
  196. return page_check_range((target_ulong)addr, size,
  197. (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0;
  198. }
  199. /* NOTE __get_user and __put_user use host pointers and don't check access. */
  200. /* These are usually used to access struct data members once the
  201. * struct has been locked - usually with lock_user_struct().
  202. */
  203. #define __put_user(x, hptr)\
  204. ({\
  205. int size = sizeof(*hptr);\
  206. switch(size) {\
  207. case 1:\
  208. *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
  209. break;\
  210. case 2:\
  211. *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
  212. break;\
  213. case 4:\
  214. *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
  215. break;\
  216. case 8:\
  217. *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
  218. break;\
  219. default:\
  220. abort();\
  221. }\
  222. 0;\
  223. })
  224. #define __get_user(x, hptr) \
  225. ({\
  226. int size = sizeof(*hptr);\
  227. switch(size) {\
  228. case 1:\
  229. x = (typeof(*hptr))*(uint8_t *)(hptr);\
  230. break;\
  231. case 2:\
  232. x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
  233. break;\
  234. case 4:\
  235. x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
  236. break;\
  237. case 8:\
  238. x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
  239. break;\
  240. default:\
  241. /* avoid warning */\
  242. x = 0;\
  243. abort();\
  244. }\
  245. 0;\
  246. })
  247. /* put_user()/get_user() take a guest address and check access */
  248. /* These are usually used to access an atomic data type, such as an int,
  249. * that has been passed by address. These internally perform locking
  250. * and unlocking on the data type.
  251. */
  252. #define put_user(x, gaddr, target_type) \
  253. ({ \
  254. abi_ulong __gaddr = (gaddr); \
  255. target_type *__hptr; \
  256. abi_long __ret; \
  257. if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
  258. __ret = __put_user((x), __hptr); \
  259. unlock_user(__hptr, __gaddr, sizeof(target_type)); \
  260. } else \
  261. __ret = -TARGET_EFAULT; \
  262. __ret; \
  263. })
  264. #define get_user(x, gaddr, target_type) \
  265. ({ \
  266. abi_ulong __gaddr = (gaddr); \
  267. target_type *__hptr; \
  268. abi_long __ret; \
  269. if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
  270. __ret = __get_user((x), __hptr); \
  271. unlock_user(__hptr, __gaddr, 0); \
  272. } else { \
  273. /* avoid warning */ \
  274. (x) = 0; \
  275. __ret = -TARGET_EFAULT; \
  276. } \
  277. __ret; \
  278. })
  279. #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
  280. #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
  281. #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
  282. #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
  283. #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
  284. #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
  285. #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
  286. #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
  287. #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
  288. #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
  289. #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
  290. #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
  291. #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
  292. #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
  293. #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
  294. #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
  295. #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
  296. #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
  297. #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
  298. #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
  299. /* copy_from_user() and copy_to_user() are usually used to copy data
  300. * buffers between the target and host. These internally perform
  301. * locking/unlocking of the memory.
  302. */
  303. abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
  304. abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
  305. /* Functions for accessing guest memory. The tget and tput functions
  306. read/write single values, byteswapping as necessary. The lock_user function
  307. gets a pointer to a contiguous area of guest memory, but does not perform
  308. any byteswapping. lock_user may return either a pointer to the guest
  309. memory, or a temporary buffer. */
  310. /* Lock an area of guest memory into the host. If copy is true then the
  311. host area will have the same contents as the guest. */
  312. static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy)
  313. {
  314. if (!access_ok(type, guest_addr, len))
  315. return NULL;
  316. #ifdef DEBUG_REMAP
  317. {
  318. void *addr;
  319. addr = g_malloc(len);
  320. if (copy)
  321. memcpy(addr, g2h(guest_addr), len);
  322. else
  323. memset(addr, 0, len);
  324. return addr;
  325. }
  326. #else
  327. return g2h(guest_addr);
  328. #endif
  329. }
  330. /* Unlock an area of guest memory. The first LEN bytes must be
  331. flushed back to guest memory. host_ptr = NULL is explicitly
  332. allowed and does nothing. */
  333. static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
  334. long len)
  335. {
  336. #ifdef DEBUG_REMAP
  337. if (!host_ptr)
  338. return;
  339. if (host_ptr == g2h(guest_addr))
  340. return;
  341. if (len > 0)
  342. memcpy(g2h(guest_addr), host_ptr, len);
  343. g_free(host_ptr);
  344. #endif
  345. }
  346. /* Return the length of a string in target memory or -TARGET_EFAULT if
  347. access error. */
  348. abi_long target_strlen(abi_ulong gaddr);
  349. /* Like lock_user but for null terminated strings. */
  350. static inline void *lock_user_string(abi_ulong guest_addr)
  351. {
  352. abi_long len;
  353. len = target_strlen(guest_addr);
  354. if (len < 0)
  355. return NULL;
  356. return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
  357. }
  358. /* Helper macros for locking/unlocking a target struct. */
  359. #define lock_user_struct(type, host_ptr, guest_addr, copy) \
  360. (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
  361. #define unlock_user_struct(host_ptr, guest_addr, copy) \
  362. unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
  363. #if defined(CONFIG_USE_NPTL)
  364. #include <pthread.h>
  365. #endif
  366. #endif /* QEMU_H */