2
0

qemu.h 14 KB

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