qemu.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. /* This is the size of the host kernel's sigset_t, needed where we make
  16. * direct system calls that take a sigset_t pointer and a size.
  17. */
  18. #define SIGSET_T_SIZE (_NSIG / 8)
  19. /* This struct is used to hold certain information about the image.
  20. * Basically, it replicates in user space what would be certain
  21. * task_struct fields in the kernel
  22. */
  23. struct image_info {
  24. abi_ulong load_bias;
  25. abi_ulong load_addr;
  26. abi_ulong start_code;
  27. abi_ulong end_code;
  28. abi_ulong start_data;
  29. abi_ulong end_data;
  30. abi_ulong start_brk;
  31. abi_ulong brk;
  32. abi_ulong reserve_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. abi_ulong tp_value;
  108. #endif
  109. #if defined(TARGET_ARM) || defined(TARGET_M68K)
  110. /* Extra fields for semihosted binaries. */
  111. abi_ulong heap_base;
  112. abi_ulong heap_limit;
  113. #endif
  114. abi_ulong stack_base;
  115. int used; /* non zero if used */
  116. struct image_info *info;
  117. struct linux_binprm *bprm;
  118. struct emulated_sigtable sync_signal;
  119. struct emulated_sigtable sigtab[TARGET_NSIG];
  120. /* This thread's signal mask, as requested by the guest program.
  121. * The actual signal mask of this thread may differ:
  122. * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
  123. * + sometimes we block all signals to avoid races
  124. */
  125. sigset_t signal_mask;
  126. /* The signal mask imposed by a guest sigsuspend syscall, if we are
  127. * currently in the middle of such a syscall
  128. */
  129. sigset_t sigsuspend_mask;
  130. /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */
  131. int in_sigsuspend;
  132. /* Nonzero if process_pending_signals() needs to do something (either
  133. * handle a pending signal or unblock signals).
  134. * This flag is written from a signal handler so should be accessed via
  135. * the qatomic_read() and qatomic_set() functions. (It is not accessed
  136. * from multiple threads.)
  137. */
  138. int signal_pending;
  139. /* This thread's sigaltstack, if it has one */
  140. struct target_sigaltstack sigaltstack_used;
  141. } __attribute__((aligned(16))) TaskState;
  142. extern char *exec_path;
  143. void init_task_state(TaskState *ts);
  144. void task_settid(TaskState *);
  145. void stop_all_tasks(void);
  146. extern const char *qemu_uname_release;
  147. extern unsigned long mmap_min_addr;
  148. /* ??? See if we can avoid exposing so much of the loader internals. */
  149. /* Read a good amount of data initially, to hopefully get all the
  150. program headers loaded. */
  151. #define BPRM_BUF_SIZE 1024
  152. /*
  153. * This structure is used to hold the arguments that are
  154. * used when loading binaries.
  155. */
  156. struct linux_binprm {
  157. char buf[BPRM_BUF_SIZE] __attribute__((aligned));
  158. abi_ulong p;
  159. int fd;
  160. int e_uid, e_gid;
  161. int argc, envc;
  162. char **argv;
  163. char **envp;
  164. char * filename; /* Name of binary */
  165. int (*core_dump)(int, const CPUArchState *); /* coredump routine */
  166. };
  167. typedef struct IOCTLEntry IOCTLEntry;
  168. typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
  169. int fd, int cmd, abi_long arg);
  170. struct IOCTLEntry {
  171. int target_cmd;
  172. unsigned int host_cmd;
  173. const char *name;
  174. int access;
  175. do_ioctl_fn *do_ioctl;
  176. const argtype arg_type[5];
  177. };
  178. extern IOCTLEntry ioctl_entries[];
  179. #define IOC_R 0x0001
  180. #define IOC_W 0x0002
  181. #define IOC_RW (IOC_R | IOC_W)
  182. void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
  183. abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
  184. abi_ulong stringp, int push_ptr);
  185. int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
  186. struct target_pt_regs * regs, struct image_info *infop,
  187. struct linux_binprm *);
  188. /* Returns true if the image uses the FDPIC ABI. If this is the case,
  189. * we have to provide some information (loadmap, pt_dynamic_info) such
  190. * that the program can be relocated adequately. This is also useful
  191. * when handling signals.
  192. */
  193. int info_is_fdpic(struct image_info *info);
  194. uint32_t get_elf_eflags(int fd);
  195. int load_elf_binary(struct linux_binprm *bprm, struct image_info *info);
  196. int load_flt_binary(struct linux_binprm *bprm, struct image_info *info);
  197. abi_long memcpy_to_target(abi_ulong dest, const void *src,
  198. unsigned long len);
  199. void target_set_brk(abi_ulong new_brk);
  200. abi_long do_brk(abi_ulong new_brk);
  201. void syscall_init(void);
  202. abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
  203. abi_long arg2, abi_long arg3, abi_long arg4,
  204. abi_long arg5, abi_long arg6, abi_long arg7,
  205. abi_long arg8);
  206. extern __thread CPUState *thread_cpu;
  207. void cpu_loop(CPUArchState *env);
  208. const char *target_strerror(int err);
  209. int get_osversion(void);
  210. void init_qemu_uname_release(void);
  211. void fork_start(void);
  212. void fork_end(int child);
  213. /**
  214. * probe_guest_base:
  215. * @image_name: the executable being loaded
  216. * @loaddr: the lowest fixed address in the executable
  217. * @hiaddr: the highest fixed address in the executable
  218. *
  219. * Creates the initial guest address space in the host memory space.
  220. *
  221. * If @loaddr == 0, then no address in the executable is fixed,
  222. * i.e. it is fully relocatable. In that case @hiaddr is the size
  223. * of the executable.
  224. *
  225. * This function will not return if a valid value for guest_base
  226. * cannot be chosen. On return, the executable loader can expect
  227. *
  228. * target_mmap(loaddr, hiaddr - loaddr, ...)
  229. *
  230. * to succeed.
  231. */
  232. void probe_guest_base(const char *image_name,
  233. abi_ulong loaddr, abi_ulong hiaddr);
  234. #include "qemu/log.h"
  235. /* safe_syscall.S */
  236. /**
  237. * safe_syscall:
  238. * @int number: number of system call to make
  239. * ...: arguments to the system call
  240. *
  241. * Call a system call if guest signal not pending.
  242. * This has the same API as the libc syscall() function, except that it
  243. * may return -1 with errno == TARGET_ERESTARTSYS if a signal was pending.
  244. *
  245. * Returns: the system call result, or -1 with an error code in errno
  246. * (Errnos are host errnos; we rely on TARGET_ERESTARTSYS not clashing
  247. * with any of the host errno values.)
  248. */
  249. /* A guide to using safe_syscall() to handle interactions between guest
  250. * syscalls and guest signals:
  251. *
  252. * Guest syscalls come in two flavours:
  253. *
  254. * (1) Non-interruptible syscalls
  255. *
  256. * These are guest syscalls that never get interrupted by signals and
  257. * so never return EINTR. They can be implemented straightforwardly in
  258. * QEMU: just make sure that if the implementation code has to make any
  259. * blocking calls that those calls are retried if they return EINTR.
  260. * It's also OK to implement these with safe_syscall, though it will be
  261. * a little less efficient if a signal is delivered at the 'wrong' moment.
  262. *
  263. * Some non-interruptible syscalls need to be handled using block_signals()
  264. * to block signals for the duration of the syscall. This mainly applies
  265. * to code which needs to modify the data structures used by the
  266. * host_signal_handler() function and the functions it calls, including
  267. * all syscalls which change the thread's signal mask.
  268. *
  269. * (2) Interruptible syscalls
  270. *
  271. * These are guest syscalls that can be interrupted by signals and
  272. * for which we need to either return EINTR or arrange for the guest
  273. * syscall to be restarted. This category includes both syscalls which
  274. * always restart (and in the kernel return -ERESTARTNOINTR), ones
  275. * which only restart if there is no handler (kernel returns -ERESTARTNOHAND
  276. * or -ERESTART_RESTARTBLOCK), and the most common kind which restart
  277. * if the handler was registered with SA_RESTART (kernel returns
  278. * -ERESTARTSYS). System calls which are only interruptible in some
  279. * situations (like 'open') also need to be handled this way.
  280. *
  281. * Here it is important that the host syscall is made
  282. * via this safe_syscall() function, and *not* via the host libc.
  283. * If the host libc is used then the implementation will appear to work
  284. * most of the time, but there will be a race condition where a
  285. * signal could arrive just before we make the host syscall inside libc,
  286. * and then then guest syscall will not correctly be interrupted.
  287. * Instead the implementation of the guest syscall can use the safe_syscall
  288. * function but otherwise just return the result or errno in the usual
  289. * way; the main loop code will take care of restarting the syscall
  290. * if appropriate.
  291. *
  292. * (If the implementation needs to make multiple host syscalls this is
  293. * OK; any which might really block must be via safe_syscall(); for those
  294. * which are only technically blocking (ie which we know in practice won't
  295. * stay in the host kernel indefinitely) it's OK to use libc if necessary.
  296. * You must be able to cope with backing out correctly if some safe_syscall
  297. * you make in the implementation returns either -TARGET_ERESTARTSYS or
  298. * EINTR though.)
  299. *
  300. * block_signals() cannot be used for interruptible syscalls.
  301. *
  302. *
  303. * How and why the safe_syscall implementation works:
  304. *
  305. * The basic setup is that we make the host syscall via a known
  306. * section of host native assembly. If a signal occurs, our signal
  307. * handler checks the interrupted host PC against the addresse of that
  308. * known section. If the PC is before or at the address of the syscall
  309. * instruction then we change the PC to point at a "return
  310. * -TARGET_ERESTARTSYS" code path instead, and then exit the signal handler
  311. * (causing the safe_syscall() call to immediately return that value).
  312. * Then in the main.c loop if we see this magic return value we adjust
  313. * the guest PC to wind it back to before the system call, and invoke
  314. * the guest signal handler as usual.
  315. *
  316. * This winding-back will happen in two cases:
  317. * (1) signal came in just before we took the host syscall (a race);
  318. * in this case we'll take the guest signal and have another go
  319. * at the syscall afterwards, and this is indistinguishable for the
  320. * guest from the timing having been different such that the guest
  321. * signal really did win the race
  322. * (2) signal came in while the host syscall was blocking, and the
  323. * host kernel decided the syscall should be restarted;
  324. * in this case we want to restart the guest syscall also, and so
  325. * rewinding is the right thing. (Note that "restart" semantics mean
  326. * "first call the signal handler, then reattempt the syscall".)
  327. * The other situation to consider is when a signal came in while the
  328. * host syscall was blocking, and the host kernel decided that the syscall
  329. * should not be restarted; in this case QEMU's host signal handler will
  330. * be invoked with the PC pointing just after the syscall instruction,
  331. * with registers indicating an EINTR return; the special code in the
  332. * handler will not kick in, and we will return EINTR to the guest as
  333. * we should.
  334. *
  335. * Notice that we can leave the host kernel to make the decision for
  336. * us about whether to do a restart of the syscall or not; we do not
  337. * need to check SA_RESTART flags in QEMU or distinguish the various
  338. * kinds of restartability.
  339. */
  340. #ifdef HAVE_SAFE_SYSCALL
  341. /* The core part of this function is implemented in assembly */
  342. extern long safe_syscall_base(int *pending, long number, ...);
  343. #define safe_syscall(...) \
  344. ({ \
  345. long ret_; \
  346. int *psp_ = &((TaskState *)thread_cpu->opaque)->signal_pending; \
  347. ret_ = safe_syscall_base(psp_, __VA_ARGS__); \
  348. if (is_error(ret_)) { \
  349. errno = -ret_; \
  350. ret_ = -1; \
  351. } \
  352. ret_; \
  353. })
  354. #else
  355. /* Fallback for architectures which don't yet provide a safe-syscall assembly
  356. * fragment; note that this is racy!
  357. * This should go away when all host architectures have been updated.
  358. */
  359. #define safe_syscall syscall
  360. #endif
  361. /* syscall.c */
  362. int host_to_target_waitstatus(int status);
  363. /* strace.c */
  364. void print_syscall(void *cpu_env, int num,
  365. abi_long arg1, abi_long arg2, abi_long arg3,
  366. abi_long arg4, abi_long arg5, abi_long arg6);
  367. void print_syscall_ret(void *cpu_env, int num, abi_long ret,
  368. abi_long arg1, abi_long arg2, abi_long arg3,
  369. abi_long arg4, abi_long arg5, abi_long arg6);
  370. /**
  371. * print_taken_signal:
  372. * @target_signum: target signal being taken
  373. * @tinfo: target_siginfo_t which will be passed to the guest for the signal
  374. *
  375. * Print strace output indicating that this signal is being taken by the guest,
  376. * in a format similar to:
  377. * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
  378. */
  379. void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
  380. /* signal.c */
  381. void process_pending_signals(CPUArchState *cpu_env);
  382. void signal_init(void);
  383. int queue_signal(CPUArchState *env, int sig, int si_type,
  384. target_siginfo_t *info);
  385. void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
  386. void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
  387. int target_to_host_signal(int sig);
  388. int host_to_target_signal(int sig);
  389. long do_sigreturn(CPUArchState *env);
  390. long do_rt_sigreturn(CPUArchState *env);
  391. abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
  392. int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
  393. abi_long do_swapcontext(CPUArchState *env, abi_ulong uold_ctx,
  394. abi_ulong unew_ctx, abi_long ctx_size);
  395. /**
  396. * block_signals: block all signals while handling this guest syscall
  397. *
  398. * Block all signals, and arrange that the signal mask is returned to
  399. * its correct value for the guest before we resume execution of guest code.
  400. * If this function returns non-zero, then the caller should immediately
  401. * return -TARGET_ERESTARTSYS to the main loop, which will take the pending
  402. * signal and restart execution of the syscall.
  403. * If block_signals() returns zero, then the caller can continue with
  404. * emulation of the system call knowing that no signals can be taken
  405. * (and therefore that no race conditions will result).
  406. * This should only be called once, because if it is called a second time
  407. * it will always return non-zero. (Think of it like a mutex that can't
  408. * be recursively locked.)
  409. * Signals will be unblocked again by process_pending_signals().
  410. *
  411. * Return value: non-zero if there was a pending signal, zero if not.
  412. */
  413. int block_signals(void); /* Returns non zero if signal pending */
  414. #ifdef TARGET_I386
  415. /* vm86.c */
  416. void save_v86_state(CPUX86State *env);
  417. void handle_vm86_trap(CPUX86State *env, int trapno);
  418. void handle_vm86_fault(CPUX86State *env);
  419. int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
  420. #elif defined(TARGET_SPARC64)
  421. void sparc64_set_context(CPUSPARCState *env);
  422. void sparc64_get_context(CPUSPARCState *env);
  423. #endif
  424. /* mmap.c */
  425. int target_mprotect(abi_ulong start, abi_ulong len, int prot);
  426. abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
  427. int flags, int fd, abi_ulong offset);
  428. int target_munmap(abi_ulong start, abi_ulong len);
  429. abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
  430. abi_ulong new_size, unsigned long flags,
  431. abi_ulong new_addr);
  432. extern unsigned long last_brk;
  433. extern abi_ulong mmap_next_start;
  434. abi_ulong mmap_find_vma(abi_ulong, abi_ulong, abi_ulong);
  435. void mmap_fork_start(void);
  436. void mmap_fork_end(int child);
  437. /* main.c */
  438. extern unsigned long guest_stack_size;
  439. /* user access */
  440. #define VERIFY_READ 0
  441. #define VERIFY_WRITE 1 /* implies read access */
  442. static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
  443. {
  444. return guest_addr_valid(addr) &&
  445. (size == 0 || guest_addr_valid(addr + size - 1)) &&
  446. page_check_range((target_ulong)addr, size,
  447. (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0;
  448. }
  449. /* NOTE __get_user and __put_user use host pointers and don't check access.
  450. These are usually used to access struct data members once the struct has
  451. been locked - usually with lock_user_struct. */
  452. /*
  453. * Tricky points:
  454. * - Use __builtin_choose_expr to avoid type promotion from ?:,
  455. * - Invalid sizes result in a compile time error stemming from
  456. * the fact that abort has no parameters.
  457. * - It's easier to use the endian-specific unaligned load/store
  458. * functions than host-endian unaligned load/store plus tswapN.
  459. * - The pragmas are necessary only to silence a clang false-positive
  460. * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
  461. * - gcc has bugs in its _Pragma() support in some versions, eg
  462. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
  463. * include the warning-suppression pragmas for clang
  464. */
  465. #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
  466. #define PRAGMA_DISABLE_PACKED_WARNING \
  467. _Pragma("GCC diagnostic push"); \
  468. _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
  469. #define PRAGMA_REENABLE_PACKED_WARNING \
  470. _Pragma("GCC diagnostic pop")
  471. #else
  472. #define PRAGMA_DISABLE_PACKED_WARNING
  473. #define PRAGMA_REENABLE_PACKED_WARNING
  474. #endif
  475. #define __put_user_e(x, hptr, e) \
  476. do { \
  477. PRAGMA_DISABLE_PACKED_WARNING; \
  478. (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
  479. __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
  480. __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
  481. __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
  482. ((hptr), (x)), (void)0); \
  483. PRAGMA_REENABLE_PACKED_WARNING; \
  484. } while (0)
  485. #define __get_user_e(x, hptr, e) \
  486. do { \
  487. PRAGMA_DISABLE_PACKED_WARNING; \
  488. ((x) = (typeof(*hptr))( \
  489. __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
  490. __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
  491. __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
  492. __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
  493. (hptr)), (void)0); \
  494. PRAGMA_REENABLE_PACKED_WARNING; \
  495. } while (0)
  496. #ifdef TARGET_WORDS_BIGENDIAN
  497. # define __put_user(x, hptr) __put_user_e(x, hptr, be)
  498. # define __get_user(x, hptr) __get_user_e(x, hptr, be)
  499. #else
  500. # define __put_user(x, hptr) __put_user_e(x, hptr, le)
  501. # define __get_user(x, hptr) __get_user_e(x, hptr, le)
  502. #endif
  503. /* put_user()/get_user() take a guest address and check access */
  504. /* These are usually used to access an atomic data type, such as an int,
  505. * that has been passed by address. These internally perform locking
  506. * and unlocking on the data type.
  507. */
  508. #define put_user(x, gaddr, target_type) \
  509. ({ \
  510. abi_ulong __gaddr = (gaddr); \
  511. target_type *__hptr; \
  512. abi_long __ret = 0; \
  513. if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
  514. __put_user((x), __hptr); \
  515. unlock_user(__hptr, __gaddr, sizeof(target_type)); \
  516. } else \
  517. __ret = -TARGET_EFAULT; \
  518. __ret; \
  519. })
  520. #define get_user(x, gaddr, target_type) \
  521. ({ \
  522. abi_ulong __gaddr = (gaddr); \
  523. target_type *__hptr; \
  524. abi_long __ret = 0; \
  525. if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
  526. __get_user((x), __hptr); \
  527. unlock_user(__hptr, __gaddr, 0); \
  528. } else { \
  529. /* avoid warning */ \
  530. (x) = 0; \
  531. __ret = -TARGET_EFAULT; \
  532. } \
  533. __ret; \
  534. })
  535. #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
  536. #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
  537. #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
  538. #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
  539. #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
  540. #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
  541. #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
  542. #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
  543. #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
  544. #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
  545. #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
  546. #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
  547. #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
  548. #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
  549. #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
  550. #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
  551. #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
  552. #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
  553. #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
  554. #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
  555. /* copy_from_user() and copy_to_user() are usually used to copy data
  556. * buffers between the target and host. These internally perform
  557. * locking/unlocking of the memory.
  558. */
  559. abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
  560. abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
  561. /* Functions for accessing guest memory. The tget and tput functions
  562. read/write single values, byteswapping as necessary. The lock_user function
  563. gets a pointer to a contiguous area of guest memory, but does not perform
  564. any byteswapping. lock_user may return either a pointer to the guest
  565. memory, or a temporary buffer. */
  566. /* Lock an area of guest memory into the host. If copy is true then the
  567. host area will have the same contents as the guest. */
  568. static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy)
  569. {
  570. if (!access_ok(type, guest_addr, len))
  571. return NULL;
  572. #ifdef DEBUG_REMAP
  573. {
  574. void *addr;
  575. addr = g_malloc(len);
  576. if (copy)
  577. memcpy(addr, g2h(guest_addr), len);
  578. else
  579. memset(addr, 0, len);
  580. return addr;
  581. }
  582. #else
  583. return g2h(guest_addr);
  584. #endif
  585. }
  586. /* Unlock an area of guest memory. The first LEN bytes must be
  587. flushed back to guest memory. host_ptr = NULL is explicitly
  588. allowed and does nothing. */
  589. static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
  590. long len)
  591. {
  592. #ifdef DEBUG_REMAP
  593. if (!host_ptr)
  594. return;
  595. if (host_ptr == g2h(guest_addr))
  596. return;
  597. if (len > 0)
  598. memcpy(g2h(guest_addr), host_ptr, len);
  599. g_free(host_ptr);
  600. #endif
  601. }
  602. /* Return the length of a string in target memory or -TARGET_EFAULT if
  603. access error. */
  604. abi_long target_strlen(abi_ulong gaddr);
  605. /* Like lock_user but for null terminated strings. */
  606. static inline void *lock_user_string(abi_ulong guest_addr)
  607. {
  608. abi_long len;
  609. len = target_strlen(guest_addr);
  610. if (len < 0)
  611. return NULL;
  612. return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
  613. }
  614. /* Helper macros for locking/unlocking a target struct. */
  615. #define lock_user_struct(type, host_ptr, guest_addr, copy) \
  616. (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
  617. #define unlock_user_struct(host_ptr, guest_addr, copy) \
  618. unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
  619. #include <pthread.h>
  620. static inline int is_error(abi_long ret)
  621. {
  622. return (abi_ulong)ret >= (abi_ulong)(-4096);
  623. }
  624. #if TARGET_ABI_BITS == 32
  625. static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
  626. {
  627. #ifdef TARGET_WORDS_BIGENDIAN
  628. return ((uint64_t)word0 << 32) | word1;
  629. #else
  630. return ((uint64_t)word1 << 32) | word0;
  631. #endif
  632. }
  633. #else /* TARGET_ABI_BITS == 32 */
  634. static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
  635. {
  636. return word0;
  637. }
  638. #endif /* TARGET_ABI_BITS != 32 */
  639. void print_termios(void *arg);
  640. /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
  641. #ifdef TARGET_ARM
  642. static inline int regpairs_aligned(void *cpu_env, int num)
  643. {
  644. return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
  645. }
  646. #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
  647. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  648. #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
  649. /*
  650. * SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
  651. * of registers which translates to the same as ARM/MIPS, because we start with
  652. * r3 as arg1
  653. */
  654. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  655. #elif defined(TARGET_SH4)
  656. /* SH4 doesn't align register pairs, except for p{read,write}64 */
  657. static inline int regpairs_aligned(void *cpu_env, int num)
  658. {
  659. switch (num) {
  660. case TARGET_NR_pread64:
  661. case TARGET_NR_pwrite64:
  662. return 1;
  663. default:
  664. return 0;
  665. }
  666. }
  667. #elif defined(TARGET_XTENSA)
  668. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  669. #else
  670. static inline int regpairs_aligned(void *cpu_env, int num) { return 0; }
  671. #endif
  672. /**
  673. * preexit_cleanup: housekeeping before the guest exits
  674. *
  675. * env: the CPU state
  676. * code: the exit code
  677. */
  678. void preexit_cleanup(CPUArchState *env, int code);
  679. /* Include target-specific struct and function definitions;
  680. * they may need access to the target-independent structures
  681. * above, so include them last.
  682. */
  683. #include "target_cpu.h"
  684. #include "target_structs.h"
  685. #endif /* QEMU_H */