qemu.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. #ifndef QEMU_H
  2. #define QEMU_H
  3. #include "hostdep.h"
  4. #include "cpu.h"
  5. #include "exec/exec-all.h"
  6. #include "exec/cpu_ldst.h"
  7. #undef DEBUG_REMAP
  8. #ifdef DEBUG_REMAP
  9. #endif /* DEBUG_REMAP */
  10. #include "exec/user/abitypes.h"
  11. #include "exec/user/thunk.h"
  12. #include "syscall_defs.h"
  13. #include "target_syscall.h"
  14. #include "exec/gdbstub.h"
  15. #include "qemu/queue.h"
  16. /* This is the size of the host kernel's sigset_t, needed where we make
  17. * direct system calls that take a sigset_t pointer and a size.
  18. */
  19. #define SIGSET_T_SIZE (_NSIG / 8)
  20. /* This struct is used to hold certain information about the image.
  21. * Basically, it replicates in user space what would be certain
  22. * task_struct fields in the kernel
  23. */
  24. struct image_info {
  25. abi_ulong load_bias;
  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 start_stack;
  35. abi_ulong stack_limit;
  36. abi_ulong entry;
  37. abi_ulong code_offset;
  38. abi_ulong data_offset;
  39. abi_ulong saved_auxv;
  40. abi_ulong auxv_len;
  41. abi_ulong arg_start;
  42. abi_ulong arg_end;
  43. abi_ulong arg_strings;
  44. abi_ulong env_strings;
  45. abi_ulong file_string;
  46. uint32_t elf_flags;
  47. int personality;
  48. abi_ulong alignment;
  49. /* The fields below are used in FDPIC mode. */
  50. abi_ulong loadmap_addr;
  51. uint16_t nsegs;
  52. void *loadsegs;
  53. abi_ulong pt_dynamic_addr;
  54. abi_ulong interpreter_loadmap_addr;
  55. abi_ulong interpreter_pt_dynamic_addr;
  56. struct image_info *other_info;
  57. #ifdef TARGET_MIPS
  58. int fp_abi;
  59. int interp_fp_abi;
  60. #endif
  61. };
  62. #ifdef TARGET_I386
  63. /* Information about the current linux thread */
  64. struct vm86_saved_state {
  65. uint32_t eax; /* return code */
  66. uint32_t ebx;
  67. uint32_t ecx;
  68. uint32_t edx;
  69. uint32_t esi;
  70. uint32_t edi;
  71. uint32_t ebp;
  72. uint32_t esp;
  73. uint32_t eflags;
  74. uint32_t eip;
  75. uint16_t cs, ss, ds, es, fs, gs;
  76. };
  77. #endif
  78. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  79. /* FPU emulator */
  80. #include "nwfpe/fpa11.h"
  81. #endif
  82. #define MAX_SIGQUEUE_SIZE 1024
  83. struct emulated_sigtable {
  84. int pending; /* true if signal is pending */
  85. target_siginfo_t info;
  86. };
  87. /* NOTE: we force a big alignment so that the stack stored after is
  88. aligned too */
  89. typedef struct TaskState {
  90. pid_t ts_tid; /* tid (or pid) of this task */
  91. #ifdef TARGET_ARM
  92. # ifdef TARGET_ABI32
  93. /* FPA state */
  94. FPA11 fpa;
  95. # endif
  96. int swi_errno;
  97. #endif
  98. #if defined(TARGET_I386) && !defined(TARGET_X86_64)
  99. abi_ulong target_v86;
  100. struct vm86_saved_state vm86_saved_regs;
  101. struct target_vm86plus_struct vm86plus;
  102. uint32_t v86flags;
  103. uint32_t v86mask;
  104. #endif
  105. abi_ulong child_tidptr;
  106. #ifdef TARGET_M68K
  107. int sim_syscalls;
  108. abi_ulong tp_value;
  109. #endif
  110. #if defined(TARGET_ARM) || defined(TARGET_M68K)
  111. /* Extra fields for semihosted binaries. */
  112. abi_ulong heap_base;
  113. abi_ulong heap_limit;
  114. #endif
  115. abi_ulong stack_base;
  116. int used; /* non zero if used */
  117. struct image_info *info;
  118. struct linux_binprm *bprm;
  119. struct emulated_sigtable sync_signal;
  120. struct emulated_sigtable sigtab[TARGET_NSIG];
  121. /* This thread's signal mask, as requested by the guest program.
  122. * The actual signal mask of this thread may differ:
  123. * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
  124. * + sometimes we block all signals to avoid races
  125. */
  126. sigset_t signal_mask;
  127. /* The signal mask imposed by a guest sigsuspend syscall, if we are
  128. * currently in the middle of such a syscall
  129. */
  130. sigset_t sigsuspend_mask;
  131. /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */
  132. int in_sigsuspend;
  133. /* Nonzero if process_pending_signals() needs to do something (either
  134. * handle a pending signal or unblock signals).
  135. * This flag is written from a signal handler so should be accessed via
  136. * the atomic_read() and atomic_set() functions. (It is not accessed
  137. * from multiple threads.)
  138. */
  139. int signal_pending;
  140. } __attribute__((aligned(16))) TaskState;
  141. extern char *exec_path;
  142. void init_task_state(TaskState *ts);
  143. void task_settid(TaskState *);
  144. void stop_all_tasks(void);
  145. extern const char *qemu_uname_release;
  146. extern unsigned long mmap_min_addr;
  147. /* ??? See if we can avoid exposing so much of the loader internals. */
  148. /* Read a good amount of data initially, to hopefully get all the
  149. program headers loaded. */
  150. #define BPRM_BUF_SIZE 1024
  151. /*
  152. * This structure is used to hold the arguments that are
  153. * used when loading binaries.
  154. */
  155. struct linux_binprm {
  156. char buf[BPRM_BUF_SIZE] __attribute__((aligned));
  157. abi_ulong p;
  158. int fd;
  159. int e_uid, e_gid;
  160. int argc, envc;
  161. char **argv;
  162. char **envp;
  163. char * filename; /* Name of binary */
  164. int (*core_dump)(int, const CPUArchState *); /* coredump routine */
  165. };
  166. void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
  167. abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
  168. abi_ulong stringp, int push_ptr);
  169. int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
  170. struct target_pt_regs * regs, struct image_info *infop,
  171. struct linux_binprm *);
  172. /* Returns true if the image uses the FDPIC ABI. If this is the case,
  173. * we have to provide some information (loadmap, pt_dynamic_info) such
  174. * that the program can be relocated adequately. This is also useful
  175. * when handling signals.
  176. */
  177. int info_is_fdpic(struct image_info *info);
  178. uint32_t get_elf_eflags(int fd);
  179. int load_elf_binary(struct linux_binprm *bprm, struct image_info *info);
  180. int load_flt_binary(struct linux_binprm *bprm, struct image_info *info);
  181. abi_long memcpy_to_target(abi_ulong dest, const void *src,
  182. unsigned long len);
  183. void target_set_brk(abi_ulong new_brk);
  184. abi_long do_brk(abi_ulong new_brk);
  185. void syscall_init(void);
  186. abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
  187. abi_long arg2, abi_long arg3, abi_long arg4,
  188. abi_long arg5, abi_long arg6, abi_long arg7,
  189. abi_long arg8);
  190. void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
  191. extern __thread CPUState *thread_cpu;
  192. void cpu_loop(CPUArchState *env);
  193. const char *target_strerror(int err);
  194. int get_osversion(void);
  195. void init_qemu_uname_release(void);
  196. void fork_start(void);
  197. void fork_end(int child);
  198. /* Creates the initial guest address space in the host memory space using
  199. * the given host start address hint and size. The guest_start parameter
  200. * specifies the start address of the guest space. guest_base will be the
  201. * difference between the host start address computed by this function and
  202. * guest_start. If fixed is specified, then the mapped address space must
  203. * start at host_start. The real start address of the mapped memory space is
  204. * returned or -1 if there was an error.
  205. */
  206. unsigned long init_guest_space(unsigned long host_start,
  207. unsigned long host_size,
  208. unsigned long guest_start,
  209. bool fixed);
  210. #include "qemu/log.h"
  211. /* safe_syscall.S */
  212. /**
  213. * safe_syscall:
  214. * @int number: number of system call to make
  215. * ...: arguments to the system call
  216. *
  217. * Call a system call if guest signal not pending.
  218. * This has the same API as the libc syscall() function, except that it
  219. * may return -1 with errno == TARGET_ERESTARTSYS if a signal was pending.
  220. *
  221. * Returns: the system call result, or -1 with an error code in errno
  222. * (Errnos are host errnos; we rely on TARGET_ERESTARTSYS not clashing
  223. * with any of the host errno values.)
  224. */
  225. /* A guide to using safe_syscall() to handle interactions between guest
  226. * syscalls and guest signals:
  227. *
  228. * Guest syscalls come in two flavours:
  229. *
  230. * (1) Non-interruptible syscalls
  231. *
  232. * These are guest syscalls that never get interrupted by signals and
  233. * so never return EINTR. They can be implemented straightforwardly in
  234. * QEMU: just make sure that if the implementation code has to make any
  235. * blocking calls that those calls are retried if they return EINTR.
  236. * It's also OK to implement these with safe_syscall, though it will be
  237. * a little less efficient if a signal is delivered at the 'wrong' moment.
  238. *
  239. * Some non-interruptible syscalls need to be handled using block_signals()
  240. * to block signals for the duration of the syscall. This mainly applies
  241. * to code which needs to modify the data structures used by the
  242. * host_signal_handler() function and the functions it calls, including
  243. * all syscalls which change the thread's signal mask.
  244. *
  245. * (2) Interruptible syscalls
  246. *
  247. * These are guest syscalls that can be interrupted by signals and
  248. * for which we need to either return EINTR or arrange for the guest
  249. * syscall to be restarted. This category includes both syscalls which
  250. * always restart (and in the kernel return -ERESTARTNOINTR), ones
  251. * which only restart if there is no handler (kernel returns -ERESTARTNOHAND
  252. * or -ERESTART_RESTARTBLOCK), and the most common kind which restart
  253. * if the handler was registered with SA_RESTART (kernel returns
  254. * -ERESTARTSYS). System calls which are only interruptible in some
  255. * situations (like 'open') also need to be handled this way.
  256. *
  257. * Here it is important that the host syscall is made
  258. * via this safe_syscall() function, and *not* via the host libc.
  259. * If the host libc is used then the implementation will appear to work
  260. * most of the time, but there will be a race condition where a
  261. * signal could arrive just before we make the host syscall inside libc,
  262. * and then then guest syscall will not correctly be interrupted.
  263. * Instead the implementation of the guest syscall can use the safe_syscall
  264. * function but otherwise just return the result or errno in the usual
  265. * way; the main loop code will take care of restarting the syscall
  266. * if appropriate.
  267. *
  268. * (If the implementation needs to make multiple host syscalls this is
  269. * OK; any which might really block must be via safe_syscall(); for those
  270. * which are only technically blocking (ie which we know in practice won't
  271. * stay in the host kernel indefinitely) it's OK to use libc if necessary.
  272. * You must be able to cope with backing out correctly if some safe_syscall
  273. * you make in the implementation returns either -TARGET_ERESTARTSYS or
  274. * EINTR though.)
  275. *
  276. * block_signals() cannot be used for interruptible syscalls.
  277. *
  278. *
  279. * How and why the safe_syscall implementation works:
  280. *
  281. * The basic setup is that we make the host syscall via a known
  282. * section of host native assembly. If a signal occurs, our signal
  283. * handler checks the interrupted host PC against the addresse of that
  284. * known section. If the PC is before or at the address of the syscall
  285. * instruction then we change the PC to point at a "return
  286. * -TARGET_ERESTARTSYS" code path instead, and then exit the signal handler
  287. * (causing the safe_syscall() call to immediately return that value).
  288. * Then in the main.c loop if we see this magic return value we adjust
  289. * the guest PC to wind it back to before the system call, and invoke
  290. * the guest signal handler as usual.
  291. *
  292. * This winding-back will happen in two cases:
  293. * (1) signal came in just before we took the host syscall (a race);
  294. * in this case we'll take the guest signal and have another go
  295. * at the syscall afterwards, and this is indistinguishable for the
  296. * guest from the timing having been different such that the guest
  297. * signal really did win the race
  298. * (2) signal came in while the host syscall was blocking, and the
  299. * host kernel decided the syscall should be restarted;
  300. * in this case we want to restart the guest syscall also, and so
  301. * rewinding is the right thing. (Note that "restart" semantics mean
  302. * "first call the signal handler, then reattempt the syscall".)
  303. * The other situation to consider is when a signal came in while the
  304. * host syscall was blocking, and the host kernel decided that the syscall
  305. * should not be restarted; in this case QEMU's host signal handler will
  306. * be invoked with the PC pointing just after the syscall instruction,
  307. * with registers indicating an EINTR return; the special code in the
  308. * handler will not kick in, and we will return EINTR to the guest as
  309. * we should.
  310. *
  311. * Notice that we can leave the host kernel to make the decision for
  312. * us about whether to do a restart of the syscall or not; we do not
  313. * need to check SA_RESTART flags in QEMU or distinguish the various
  314. * kinds of restartability.
  315. */
  316. #ifdef HAVE_SAFE_SYSCALL
  317. /* The core part of this function is implemented in assembly */
  318. extern long safe_syscall_base(int *pending, long number, ...);
  319. #define safe_syscall(...) \
  320. ({ \
  321. long ret_; \
  322. int *psp_ = &((TaskState *)thread_cpu->opaque)->signal_pending; \
  323. ret_ = safe_syscall_base(psp_, __VA_ARGS__); \
  324. if (is_error(ret_)) { \
  325. errno = -ret_; \
  326. ret_ = -1; \
  327. } \
  328. ret_; \
  329. })
  330. #else
  331. /* Fallback for architectures which don't yet provide a safe-syscall assembly
  332. * fragment; note that this is racy!
  333. * This should go away when all host architectures have been updated.
  334. */
  335. #define safe_syscall syscall
  336. #endif
  337. /* syscall.c */
  338. int host_to_target_waitstatus(int status);
  339. /* strace.c */
  340. void print_syscall(int num,
  341. abi_long arg1, abi_long arg2, abi_long arg3,
  342. abi_long arg4, abi_long arg5, abi_long arg6);
  343. void print_syscall_ret(int num, abi_long arg1);
  344. /**
  345. * print_taken_signal:
  346. * @target_signum: target signal being taken
  347. * @tinfo: target_siginfo_t which will be passed to the guest for the signal
  348. *
  349. * Print strace output indicating that this signal is being taken by the guest,
  350. * in a format similar to:
  351. * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
  352. */
  353. void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
  354. extern int do_strace;
  355. /* signal.c */
  356. void process_pending_signals(CPUArchState *cpu_env);
  357. void signal_init(void);
  358. int queue_signal(CPUArchState *env, int sig, int si_type,
  359. target_siginfo_t *info);
  360. void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
  361. void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
  362. int target_to_host_signal(int sig);
  363. int host_to_target_signal(int sig);
  364. long do_sigreturn(CPUArchState *env);
  365. long do_rt_sigreturn(CPUArchState *env);
  366. abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
  367. int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
  368. abi_long do_swapcontext(CPUArchState *env, abi_ulong uold_ctx,
  369. abi_ulong unew_ctx, abi_long ctx_size);
  370. /**
  371. * block_signals: block all signals while handling this guest syscall
  372. *
  373. * Block all signals, and arrange that the signal mask is returned to
  374. * its correct value for the guest before we resume execution of guest code.
  375. * If this function returns non-zero, then the caller should immediately
  376. * return -TARGET_ERESTARTSYS to the main loop, which will take the pending
  377. * signal and restart execution of the syscall.
  378. * If block_signals() returns zero, then the caller can continue with
  379. * emulation of the system call knowing that no signals can be taken
  380. * (and therefore that no race conditions will result).
  381. * This should only be called once, because if it is called a second time
  382. * it will always return non-zero. (Think of it like a mutex that can't
  383. * be recursively locked.)
  384. * Signals will be unblocked again by process_pending_signals().
  385. *
  386. * Return value: non-zero if there was a pending signal, zero if not.
  387. */
  388. int block_signals(void); /* Returns non zero if signal pending */
  389. #ifdef TARGET_I386
  390. /* vm86.c */
  391. void save_v86_state(CPUX86State *env);
  392. void handle_vm86_trap(CPUX86State *env, int trapno);
  393. void handle_vm86_fault(CPUX86State *env);
  394. int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
  395. #elif defined(TARGET_SPARC64)
  396. void sparc64_set_context(CPUSPARCState *env);
  397. void sparc64_get_context(CPUSPARCState *env);
  398. #endif
  399. /* mmap.c */
  400. int target_mprotect(abi_ulong start, abi_ulong len, int prot);
  401. abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
  402. int flags, int fd, abi_ulong offset);
  403. int target_munmap(abi_ulong start, abi_ulong len);
  404. abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
  405. abi_ulong new_size, unsigned long flags,
  406. abi_ulong new_addr);
  407. extern unsigned long last_brk;
  408. extern abi_ulong mmap_next_start;
  409. abi_ulong mmap_find_vma(abi_ulong, abi_ulong);
  410. void mmap_fork_start(void);
  411. void mmap_fork_end(int child);
  412. /* main.c */
  413. extern unsigned long guest_stack_size;
  414. /* user access */
  415. #define VERIFY_READ 0
  416. #define VERIFY_WRITE 1 /* implies read access */
  417. static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
  418. {
  419. return page_check_range((target_ulong)addr, size,
  420. (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0;
  421. }
  422. /* NOTE __get_user and __put_user use host pointers and don't check access.
  423. These are usually used to access struct data members once the struct has
  424. been locked - usually with lock_user_struct. */
  425. /*
  426. * Tricky points:
  427. * - Use __builtin_choose_expr to avoid type promotion from ?:,
  428. * - Invalid sizes result in a compile time error stemming from
  429. * the fact that abort has no parameters.
  430. * - It's easier to use the endian-specific unaligned load/store
  431. * functions than host-endian unaligned load/store plus tswapN.
  432. * - The pragmas are necessary only to silence a clang false-positive
  433. * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
  434. * - We have to disable -Wpragmas warnings to avoid a complaint about
  435. * an unknown warning type from older compilers that don't know about
  436. * -Waddress-of-packed-member.
  437. * - gcc has bugs in its _Pragma() support in some versions, eg
  438. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
  439. * include the warning-suppression pragmas for clang
  440. */
  441. #ifdef __clang__
  442. #define PRAGMA_DISABLE_PACKED_WARNING \
  443. _Pragma("GCC diagnostic push"); \
  444. _Pragma("GCC diagnostic ignored \"-Wpragmas\""); \
  445. _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
  446. #define PRAGMA_REENABLE_PACKED_WARNING \
  447. _Pragma("GCC diagnostic pop")
  448. #else
  449. #define PRAGMA_DISABLE_PACKED_WARNING
  450. #define PRAGMA_REENABLE_PACKED_WARNING
  451. #endif
  452. #define __put_user_e(x, hptr, e) \
  453. do { \
  454. PRAGMA_DISABLE_PACKED_WARNING; \
  455. (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
  456. __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
  457. __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
  458. __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
  459. ((hptr), (x)), (void)0); \
  460. PRAGMA_REENABLE_PACKED_WARNING; \
  461. } while (0)
  462. #define __get_user_e(x, hptr, e) \
  463. do { \
  464. PRAGMA_DISABLE_PACKED_WARNING; \
  465. ((x) = (typeof(*hptr))( \
  466. __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
  467. __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
  468. __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
  469. __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
  470. (hptr)), (void)0); \
  471. PRAGMA_REENABLE_PACKED_WARNING; \
  472. } while (0)
  473. #ifdef TARGET_WORDS_BIGENDIAN
  474. # define __put_user(x, hptr) __put_user_e(x, hptr, be)
  475. # define __get_user(x, hptr) __get_user_e(x, hptr, be)
  476. #else
  477. # define __put_user(x, hptr) __put_user_e(x, hptr, le)
  478. # define __get_user(x, hptr) __get_user_e(x, hptr, le)
  479. #endif
  480. /* put_user()/get_user() take a guest address and check access */
  481. /* These are usually used to access an atomic data type, such as an int,
  482. * that has been passed by address. These internally perform locking
  483. * and unlocking on the data type.
  484. */
  485. #define put_user(x, gaddr, target_type) \
  486. ({ \
  487. abi_ulong __gaddr = (gaddr); \
  488. target_type *__hptr; \
  489. abi_long __ret = 0; \
  490. if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
  491. __put_user((x), __hptr); \
  492. unlock_user(__hptr, __gaddr, sizeof(target_type)); \
  493. } else \
  494. __ret = -TARGET_EFAULT; \
  495. __ret; \
  496. })
  497. #define get_user(x, gaddr, target_type) \
  498. ({ \
  499. abi_ulong __gaddr = (gaddr); \
  500. target_type *__hptr; \
  501. abi_long __ret = 0; \
  502. if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
  503. __get_user((x), __hptr); \
  504. unlock_user(__hptr, __gaddr, 0); \
  505. } else { \
  506. /* avoid warning */ \
  507. (x) = 0; \
  508. __ret = -TARGET_EFAULT; \
  509. } \
  510. __ret; \
  511. })
  512. #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
  513. #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
  514. #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
  515. #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
  516. #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
  517. #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
  518. #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
  519. #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
  520. #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
  521. #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
  522. #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
  523. #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
  524. #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
  525. #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
  526. #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
  527. #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
  528. #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
  529. #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
  530. #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
  531. #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
  532. /* copy_from_user() and copy_to_user() are usually used to copy data
  533. * buffers between the target and host. These internally perform
  534. * locking/unlocking of the memory.
  535. */
  536. abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
  537. abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
  538. /* Functions for accessing guest memory. The tget and tput functions
  539. read/write single values, byteswapping as necessary. The lock_user function
  540. gets a pointer to a contiguous area of guest memory, but does not perform
  541. any byteswapping. lock_user may return either a pointer to the guest
  542. memory, or a temporary buffer. */
  543. /* Lock an area of guest memory into the host. If copy is true then the
  544. host area will have the same contents as the guest. */
  545. static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy)
  546. {
  547. if (!access_ok(type, guest_addr, len))
  548. return NULL;
  549. #ifdef DEBUG_REMAP
  550. {
  551. void *addr;
  552. addr = g_malloc(len);
  553. if (copy)
  554. memcpy(addr, g2h(guest_addr), len);
  555. else
  556. memset(addr, 0, len);
  557. return addr;
  558. }
  559. #else
  560. return g2h(guest_addr);
  561. #endif
  562. }
  563. /* Unlock an area of guest memory. The first LEN bytes must be
  564. flushed back to guest memory. host_ptr = NULL is explicitly
  565. allowed and does nothing. */
  566. static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
  567. long len)
  568. {
  569. #ifdef DEBUG_REMAP
  570. if (!host_ptr)
  571. return;
  572. if (host_ptr == g2h(guest_addr))
  573. return;
  574. if (len > 0)
  575. memcpy(g2h(guest_addr), host_ptr, len);
  576. g_free(host_ptr);
  577. #endif
  578. }
  579. /* Return the length of a string in target memory or -TARGET_EFAULT if
  580. access error. */
  581. abi_long target_strlen(abi_ulong gaddr);
  582. /* Like lock_user but for null terminated strings. */
  583. static inline void *lock_user_string(abi_ulong guest_addr)
  584. {
  585. abi_long len;
  586. len = target_strlen(guest_addr);
  587. if (len < 0)
  588. return NULL;
  589. return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
  590. }
  591. /* Helper macros for locking/unlocking a target struct. */
  592. #define lock_user_struct(type, host_ptr, guest_addr, copy) \
  593. (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
  594. #define unlock_user_struct(host_ptr, guest_addr, copy) \
  595. unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
  596. #include <pthread.h>
  597. static inline int is_error(abi_long ret)
  598. {
  599. return (abi_ulong)ret >= (abi_ulong)(-4096);
  600. }
  601. /**
  602. * preexit_cleanup: housekeeping before the guest exits
  603. *
  604. * env: the CPU state
  605. * code: the exit code
  606. */
  607. void preexit_cleanup(CPUArchState *env, int code);
  608. /* Include target-specific struct and function definitions;
  609. * they may need access to the target-independent structures
  610. * above, so include them last.
  611. */
  612. #include "target_cpu.h"
  613. #include "target_structs.h"
  614. #endif /* QEMU_H */