188dae46dSSean Bruno /*
288dae46dSSean Bruno * qemu bsd user mode definition
388dae46dSSean Bruno *
488dae46dSSean Bruno * This program is free software; you can redistribute it and/or modify
588dae46dSSean Bruno * it under the terms of the GNU General Public License as published by
688dae46dSSean Bruno * the Free Software Foundation; either version 2 of the License, or
788dae46dSSean Bruno * (at your option) any later version.
888dae46dSSean Bruno *
988dae46dSSean Bruno * This program is distributed in the hope that it will be useful,
1088dae46dSSean Bruno * but WITHOUT ANY WARRANTY; without even the implied warranty of
1188dae46dSSean Bruno * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1288dae46dSSean Bruno * GNU General Public License for more details.
1388dae46dSSean Bruno *
1488dae46dSSean Bruno * You should have received a copy of the GNU General Public License
1588dae46dSSean Bruno * along with this program; if not, see <http://www.gnu.org/licenses/>.
1688dae46dSSean Bruno */
1784778508Sblueswir1 #ifndef QEMU_H
1884778508Sblueswir1 #define QEMU_H
1984778508Sblueswir1
2084778508Sblueswir1 #include "cpu.h"
21e5e44263SWarner Losh #include "qemu/units.h"
22f08b6170SPaolo Bonzini #include "exec/cpu_ldst.h"
23ab77bd84SWarner Losh #include "exec/exec-all.h"
2484778508Sblueswir1
2584778508Sblueswir1 #undef DEBUG_REMAP
2684778508Sblueswir1
27022c62cbSPaolo Bonzini #include "exec/user/abitypes.h"
2884778508Sblueswir1
294b599848SWarner Losh extern char **environ;
304b599848SWarner Losh
31ab77bd84SWarner Losh #include "exec/user/thunk.h"
32ab77bd84SWarner Losh #include "target_arch.h"
3384778508Sblueswir1 #include "syscall_defs.h"
340c6940d0SLluís Vilanova #include "target_syscall.h"
3582792244SWarner Losh #include "target_os_vmparam.h"
36790baaccSWarner Losh #include "target_os_signal.h"
37647afdf1SWarner Losh #include "target.h"
38022c62cbSPaolo Bonzini #include "exec/gdbstub.h"
39e022d9caSEmanuele Giuseppe Esposito #include "qemu/clang-tsa.h"
4084778508Sblueswir1
419b4a902dSStacey Son #include "qemu-os.h"
42c2bdd9a1SWarner Losh /*
43c2bdd9a1SWarner Losh * This struct is used to hold certain information about the image. Basically,
44c2bdd9a1SWarner Losh * it replicates in user space what would be certain task_struct fields in the
45c2bdd9a1SWarner Losh * kernel
4684778508Sblueswir1 */
4784778508Sblueswir1 struct image_info {
480475f8faSWarner Losh abi_ulong load_bias;
4984778508Sblueswir1 abi_ulong load_addr;
5084778508Sblueswir1 abi_ulong start_code;
5184778508Sblueswir1 abi_ulong end_code;
5284778508Sblueswir1 abi_ulong start_data;
5384778508Sblueswir1 abi_ulong end_data;
5484778508Sblueswir1 abi_ulong brk;
5584778508Sblueswir1 abi_ulong rss;
5684778508Sblueswir1 abi_ulong start_stack;
5784778508Sblueswir1 abi_ulong entry;
5884778508Sblueswir1 abi_ulong code_offset;
5984778508Sblueswir1 abi_ulong data_offset;
600475f8faSWarner Losh abi_ulong arg_start;
610475f8faSWarner Losh abi_ulong arg_end;
620475f8faSWarner Losh uint32_t elf_flags;
6384778508Sblueswir1 };
6484778508Sblueswir1
6584778508Sblueswir1 struct emulated_sigtable {
6684778508Sblueswir1 int pending; /* true if signal is pending */
67b46d4ad7SWarner Losh target_siginfo_t info;
6884778508Sblueswir1 };
6984778508Sblueswir1
70c2bdd9a1SWarner Losh /*
71c2bdd9a1SWarner Losh * NOTE: we force a big alignment so that the stack stored after is aligned too
72c2bdd9a1SWarner Losh */
7384778508Sblueswir1 typedef struct TaskState {
74bd88c780SAlex Bennée pid_t ts_tid; /* tid (or pid) of this task */
75bd88c780SAlex Bennée
7684778508Sblueswir1 struct TaskState *next;
77031fe7afSWarner Losh struct bsd_binprm *bprm;
7884778508Sblueswir1 struct image_info *info;
7984778508Sblueswir1
8038be620cSWarner Losh struct emulated_sigtable sync_signal;
8138be620cSWarner Losh /*
8238be620cSWarner Losh * TODO: Since we block all signals while returning to the main CPU
8338be620cSWarner Losh * loop, this needn't be an array
8438be620cSWarner Losh */
8584778508Sblueswir1 struct emulated_sigtable sigtab[TARGET_NSIG];
8648047225SWarner Losh /*
8748047225SWarner Losh * Nonzero if process_pending_signals() needs to do something (either
8848047225SWarner Losh * handle a pending signal or unblock signals).
8948047225SWarner Losh * This flag is written from a signal handler so should be accessed via
9048047225SWarner Losh * the qatomic_read() and qatomic_set() functions. (It is not accessed
9148047225SWarner Losh * from multiple threads.)
9248047225SWarner Losh */
9348047225SWarner Losh int signal_pending;
946c6d4b56SWarner Losh /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */
956c6d4b56SWarner Losh bool in_sigsuspend;
96149076adSWarner Losh /*
97149076adSWarner Losh * This thread's signal mask, as requested by the guest program.
98149076adSWarner Losh * The actual signal mask of this thread may differ:
99149076adSWarner Losh * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
100149076adSWarner Losh * + sometimes we block all signals to avoid races
101149076adSWarner Losh */
102149076adSWarner Losh sigset_t signal_mask;
1036c6d4b56SWarner Losh /*
1046c6d4b56SWarner Losh * The signal mask imposed by a guest sigsuspend syscall, if we are
1056c6d4b56SWarner Losh * currently in the middle of such a syscall
1066c6d4b56SWarner Losh */
1076c6d4b56SWarner Losh sigset_t sigsuspend_mask;
10884778508Sblueswir1
10946f4f76dSWarner Losh /* This thread's sigaltstack, if it has one */
11046f4f76dSWarner Losh struct target_sigaltstack sigaltstack_used;
11184778508Sblueswir1 } __attribute__((aligned(16))) TaskState;
11284778508Sblueswir1
113653ccec2SWarner Losh void stop_all_tasks(void);
11486327290SStacey Son extern const char *interp_prefix;
11584778508Sblueswir1 extern const char *qemu_uname_release;
11684778508Sblueswir1
11784778508Sblueswir1 /*
118e5e44263SWarner Losh * TARGET_ARG_MAX defines the number of bytes allocated for arguments
119e5e44263SWarner Losh * and envelope for the new program. 256k should suffice for a reasonable
120944399ffSMichael Tokarev * maximum env+arg in 32-bit environments, bump it up to 512k for !ILP32
121e5e44263SWarner Losh * platforms.
12284778508Sblueswir1 */
123e5e44263SWarner Losh #if TARGET_ABI_BITS > 32
124e5e44263SWarner Losh #define TARGET_ARG_MAX (512 * KiB)
125e5e44263SWarner Losh #else
126e5e44263SWarner Losh #define TARGET_ARG_MAX (256 * KiB)
127e5e44263SWarner Losh #endif
128e5e44263SWarner Losh #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
12984778508Sblueswir1
13084778508Sblueswir1 /*
13184778508Sblueswir1 * This structure is used to hold the arguments that are
13284778508Sblueswir1 * used when loading binaries.
13384778508Sblueswir1 */
134afcbcff8SWarner Losh struct bsd_binprm {
13584778508Sblueswir1 char buf[128];
13684778508Sblueswir1 void *page[MAX_ARG_PAGES];
13784778508Sblueswir1 abi_ulong p;
13898b34d35SWarner Losh abi_ulong stringp;
13984778508Sblueswir1 int fd;
14084778508Sblueswir1 int e_uid, e_gid;
14184778508Sblueswir1 int argc, envc;
14284778508Sblueswir1 char **argv;
14384778508Sblueswir1 char **envp;
1441b50ff64SWarner Losh char *filename; /* (Given) Name of binary */
1451b50ff64SWarner Losh char *fullpath; /* Full path of binary */
1460475f8faSWarner Losh int (*core_dump)(int, CPUArchState *);
14784778508Sblueswir1 };
14884778508Sblueswir1
14984778508Sblueswir1 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
15084778508Sblueswir1 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
151ffa03665SWarner Losh abi_ulong stringp);
15284778508Sblueswir1 int loader_exec(const char *filename, char **argv, char **envp,
153d37853f9SWarner Losh struct target_pt_regs *regs, struct image_info *infop,
154d37853f9SWarner Losh struct bsd_binprm *bprm);
15584778508Sblueswir1
156afcbcff8SWarner Losh int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
15784778508Sblueswir1 struct image_info *info);
158afcbcff8SWarner Losh int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
15984778508Sblueswir1 struct image_info *info);
1600475f8faSWarner Losh int is_target_elf_binary(int fd);
16184778508Sblueswir1
16284778508Sblueswir1 abi_long memcpy_to_target(abi_ulong dest, const void *src,
16384778508Sblueswir1 unsigned long len);
16484778508Sblueswir1 void target_set_brk(abi_ulong new_brk);
16584778508Sblueswir1 abi_long do_brk(abi_ulong new_brk);
16684778508Sblueswir1 void syscall_init(void);
16784778508Sblueswir1 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
16884778508Sblueswir1 abi_long arg2, abi_long arg3, abi_long arg4,
16978cfb07fSJuergen Lock abi_long arg5, abi_long arg6, abi_long arg7,
17078cfb07fSJuergen Lock abi_long arg8);
17184778508Sblueswir1 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
17284778508Sblueswir1 abi_long arg2, abi_long arg3, abi_long arg4,
17384778508Sblueswir1 abi_long arg5, abi_long arg6);
17484778508Sblueswir1 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
17584778508Sblueswir1 abi_long arg2, abi_long arg3, abi_long arg4,
17684778508Sblueswir1 abi_long arg5, abi_long arg6);
1779edc6313SMarc-André Lureau void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
178d42df502SWarner Losh extern __thread CPUState *thread_cpu;
1799349b4f9SAndreas Färber void cpu_loop(CPUArchState *env);
18084778508Sblueswir1 char *target_strerror(int err);
18184778508Sblueswir1 int get_osversion(void);
18284778508Sblueswir1 void fork_start(void);
18384778508Sblueswir1 void fork_end(int child);
18484778508Sblueswir1
1851de7afc9SPaolo Bonzini #include "qemu/log.h"
18684778508Sblueswir1
18784778508Sblueswir1 /* strace.c */
18888dae46dSSean Bruno struct syscallname {
18988dae46dSSean Bruno int nr;
19088dae46dSSean Bruno const char *name;
19188dae46dSSean Bruno const char *format;
19288dae46dSSean Bruno void (*call)(const struct syscallname *,
19388dae46dSSean Bruno abi_long, abi_long, abi_long,
19488dae46dSSean Bruno abi_long, abi_long, abi_long);
19588dae46dSSean Bruno void (*result)(const struct syscallname *, abi_long);
19688dae46dSSean Bruno };
19788dae46dSSean Bruno
19884778508Sblueswir1 void
19984778508Sblueswir1 print_freebsd_syscall(int num,
20084778508Sblueswir1 abi_long arg1, abi_long arg2, abi_long arg3,
20184778508Sblueswir1 abi_long arg4, abi_long arg5, abi_long arg6);
20284778508Sblueswir1 void print_freebsd_syscall_ret(int num, abi_long ret);
20384778508Sblueswir1 void
20484778508Sblueswir1 print_netbsd_syscall(int num,
20584778508Sblueswir1 abi_long arg1, abi_long arg2, abi_long arg3,
20684778508Sblueswir1 abi_long arg4, abi_long arg5, abi_long arg6);
20784778508Sblueswir1 void print_netbsd_syscall_ret(int num, abi_long ret);
20884778508Sblueswir1 void
20984778508Sblueswir1 print_openbsd_syscall(int num,
21084778508Sblueswir1 abi_long arg1, abi_long arg2, abi_long arg3,
21184778508Sblueswir1 abi_long arg4, abi_long arg5, abi_long arg6);
21284778508Sblueswir1 void print_openbsd_syscall_ret(int num, abi_long ret);
213fd5bec9aSWarner Losh /**
214fd5bec9aSWarner Losh * print_taken_signal:
215fd5bec9aSWarner Losh * @target_signum: target signal being taken
216fd5bec9aSWarner Losh * @tinfo: target_siginfo_t which will be passed to the guest for the signal
217fd5bec9aSWarner Losh *
218fd5bec9aSWarner Losh * Print strace output indicating that this signal is being taken by the guest,
219fd5bec9aSWarner Losh * in a format similar to:
220fd5bec9aSWarner Losh * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
221fd5bec9aSWarner Losh */
222fd5bec9aSWarner Losh void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
22384778508Sblueswir1 extern int do_strace;
22484778508Sblueswir1
22584778508Sblueswir1 /* mmap.c */
22684778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot);
22784778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
228be04f210SWarner Losh int flags, int fd, off_t offset);
22984778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len);
23084778508Sblueswir1 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
23184778508Sblueswir1 abi_ulong new_size, unsigned long flags,
23284778508Sblueswir1 abi_ulong new_addr);
23384778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags);
234be04f210SWarner Losh extern abi_ulong mmap_next_start;
235be04f210SWarner Losh abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
236*4e00b7d8SStacey Son void mmap_reserve(abi_ulong start, abi_ulong size);
237e022d9caSEmanuele Giuseppe Esposito void TSA_NO_TSA mmap_fork_start(void);
238e022d9caSEmanuele Giuseppe Esposito void TSA_NO_TSA mmap_fork_end(int child);
23984778508Sblueswir1
24028e738dcSBlue Swirl /* main.c */
24101a298a5SWarner Losh extern char qemu_proc_pathname[];
242312a0b1cSWarner Losh extern unsigned long target_maxtsiz;
243312a0b1cSWarner Losh extern unsigned long target_dfldsiz;
244312a0b1cSWarner Losh extern unsigned long target_maxdsiz;
245312a0b1cSWarner Losh extern unsigned long target_dflssiz;
246312a0b1cSWarner Losh extern unsigned long target_maxssiz;
247312a0b1cSWarner Losh extern unsigned long target_sgrowsiz;
24828e738dcSBlue Swirl
249deeff83bSWarner Losh /* os-syscall.c */
250e5f674f0SWarner Losh abi_long get_errno(abi_long ret);
251e5f674f0SWarner Losh bool is_error(abi_long ret);
252deeff83bSWarner Losh int host_to_target_errno(int err);
253e5f674f0SWarner Losh
254cc47390cSStacey Son /* os-proc.c */
255cc47390cSStacey Son abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
256cc47390cSStacey Son abi_ulong guest_envp, int do_fexec);
257cc47390cSStacey Son abi_long do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2,
258cc47390cSStacey Son abi_ulong arg3, abi_ulong arg4, abi_ulong arg5, abi_ulong arg6);
259cc47390cSStacey Son
260da07e694SWarner Losh /* os-sys.c */
2617adda6deSKyle Evans abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
2627adda6deSKyle Evans abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
2636da777e2SKyle Evans abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
2646da777e2SKyle Evans int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp,
2656da777e2SKyle Evans abi_ulong newlen);
266da07e694SWarner Losh abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
267da07e694SWarner Losh
26884778508Sblueswir1 /* user access */
26984778508Sblueswir1
2701720751fSRichard Henderson #define VERIFY_READ PAGE_READ
2711720751fSRichard Henderson #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
27284778508Sblueswir1
access_ok(int type,abi_ulong addr,abi_ulong size)2731720751fSRichard Henderson static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
27484778508Sblueswir1 {
275bef6f008SRichard Henderson return page_check_range((target_ulong)addr, size, type);
27684778508Sblueswir1 }
27784778508Sblueswir1
278c2bdd9a1SWarner Losh /*
279c2bdd9a1SWarner Losh * NOTE __get_user and __put_user use host pointers and don't check access.
280c2bdd9a1SWarner Losh *
281c2bdd9a1SWarner Losh * These are usually used to access struct data members once the struct has been
282c2bdd9a1SWarner Losh * locked - usually with lock_user_struct().
28384778508Sblueswir1 */
2848a45962bSWarner Losh
2858a45962bSWarner Losh /*
2868a45962bSWarner Losh * Tricky points:
2878a45962bSWarner Losh * - Use __builtin_choose_expr to avoid type promotion from ?:,
2888a45962bSWarner Losh * - Invalid sizes result in a compile time error stemming from
2898a45962bSWarner Losh * the fact that abort has no parameters.
2908a45962bSWarner Losh * - It's easier to use the endian-specific unaligned load/store
2918a45962bSWarner Losh * functions than host-endian unaligned load/store plus tswapN.
2928a45962bSWarner Losh * - The pragmas are necessary only to silence a clang false-positive
2938a45962bSWarner Losh * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
2948a45962bSWarner Losh * - gcc has bugs in its _Pragma() support in some versions, eg
2958a45962bSWarner Losh * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
2968a45962bSWarner Losh * include the warning-suppression pragmas for clang
2978a45962bSWarner Losh */
2988a45962bSWarner Losh #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
2998a45962bSWarner Losh #define PRAGMA_DISABLE_PACKED_WARNING \
3008a45962bSWarner Losh _Pragma("GCC diagnostic push"); \
3018a45962bSWarner Losh _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
3028a45962bSWarner Losh
3038a45962bSWarner Losh #define PRAGMA_REENABLE_PACKED_WARNING \
3048a45962bSWarner Losh _Pragma("GCC diagnostic pop")
3058a45962bSWarner Losh
3068a45962bSWarner Losh #else
3078a45962bSWarner Losh #define PRAGMA_DISABLE_PACKED_WARNING
3088a45962bSWarner Losh #define PRAGMA_REENABLE_PACKED_WARNING
3098a45962bSWarner Losh #endif
3108a45962bSWarner Losh
3116538c682SWarner Losh #define __put_user_e(x, hptr, e) \
3126538c682SWarner Losh do { \
3136538c682SWarner Losh PRAGMA_DISABLE_PACKED_WARNING; \
3146538c682SWarner Losh (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
3156538c682SWarner Losh __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
3166538c682SWarner Losh __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
3176538c682SWarner Losh __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
3186538c682SWarner Losh ((hptr), (x)), (void)0); \
3196538c682SWarner Losh PRAGMA_REENABLE_PACKED_WARNING; \
3206538c682SWarner Losh } while (0)
32184778508Sblueswir1
3226538c682SWarner Losh #define __get_user_e(x, hptr, e) \
3236538c682SWarner Losh do { \
3246538c682SWarner Losh PRAGMA_DISABLE_PACKED_WARNING; \
3256538c682SWarner Losh ((x) = (typeof(*hptr))( \
3266538c682SWarner Losh __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
3276538c682SWarner Losh __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
3286538c682SWarner Losh __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
3296538c682SWarner Losh __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
3306538c682SWarner Losh (hptr)), (void)0); \
3316538c682SWarner Losh PRAGMA_REENABLE_PACKED_WARNING; \
3326538c682SWarner Losh } while (0)
3336538c682SWarner Losh
3346538c682SWarner Losh
3356538c682SWarner Losh #if TARGET_BIG_ENDIAN
3366538c682SWarner Losh # define __put_user(x, hptr) __put_user_e(x, hptr, be)
3376538c682SWarner Losh # define __get_user(x, hptr) __get_user_e(x, hptr, be)
3386538c682SWarner Losh #else
3396538c682SWarner Losh # define __put_user(x, hptr) __put_user_e(x, hptr, le)
3406538c682SWarner Losh # define __get_user(x, hptr) __get_user_e(x, hptr, le)
3416538c682SWarner Losh #endif
34284778508Sblueswir1
343c2bdd9a1SWarner Losh /*
344c2bdd9a1SWarner Losh * put_user()/get_user() take a guest address and check access
345c2bdd9a1SWarner Losh *
346c2bdd9a1SWarner Losh * These are usually used to access an atomic data type, such as an int, that
347c2bdd9a1SWarner Losh * has been passed by address. These internally perform locking and unlocking
348c2bdd9a1SWarner Losh * on the data type.
34984778508Sblueswir1 */
35084778508Sblueswir1 #define put_user(x, gaddr, target_type) \
35184778508Sblueswir1 ({ \
35284778508Sblueswir1 abi_ulong __gaddr = (gaddr); \
35384778508Sblueswir1 target_type *__hptr; \
3546538c682SWarner Losh abi_long __ret = 0; \
35533066934SWarner Losh __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \
35633066934SWarner Losh if (__hptr) { \
3576538c682SWarner Losh __put_user((x), __hptr); \
35884778508Sblueswir1 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
35984778508Sblueswir1 } else \
36084778508Sblueswir1 __ret = -TARGET_EFAULT; \
36184778508Sblueswir1 __ret; \
36284778508Sblueswir1 })
36384778508Sblueswir1
36484778508Sblueswir1 #define get_user(x, gaddr, target_type) \
36584778508Sblueswir1 ({ \
36684778508Sblueswir1 abi_ulong __gaddr = (gaddr); \
36784778508Sblueswir1 target_type *__hptr; \
3686538c682SWarner Losh abi_long __ret = 0; \
36933066934SWarner Losh __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \
37033066934SWarner Losh if (__hptr) { \
3716538c682SWarner Losh __get_user((x), __hptr); \
37284778508Sblueswir1 unlock_user(__hptr, __gaddr, 0); \
37384778508Sblueswir1 } else { \
37484778508Sblueswir1 (x) = 0; \
37584778508Sblueswir1 __ret = -TARGET_EFAULT; \
37684778508Sblueswir1 } \
37784778508Sblueswir1 __ret; \
37884778508Sblueswir1 })
37984778508Sblueswir1
38084778508Sblueswir1 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
38184778508Sblueswir1 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
38284778508Sblueswir1 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
38384778508Sblueswir1 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
38484778508Sblueswir1 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
38584778508Sblueswir1 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
38684778508Sblueswir1 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
38784778508Sblueswir1 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
38884778508Sblueswir1 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
38984778508Sblueswir1 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
39084778508Sblueswir1
39184778508Sblueswir1 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
39284778508Sblueswir1 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
39384778508Sblueswir1 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
39484778508Sblueswir1 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
39584778508Sblueswir1 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
39684778508Sblueswir1 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
39784778508Sblueswir1 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
39884778508Sblueswir1 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
39984778508Sblueswir1 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
40084778508Sblueswir1 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
40184778508Sblueswir1
402c2bdd9a1SWarner Losh /*
403c2bdd9a1SWarner Losh * copy_from_user() and copy_to_user() are usually used to copy data
40484778508Sblueswir1 * buffers between the target and host. These internally perform
40584778508Sblueswir1 * locking/unlocking of the memory.
40684778508Sblueswir1 */
40784778508Sblueswir1 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
40884778508Sblueswir1 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
40984778508Sblueswir1
410c2bdd9a1SWarner Losh /*
411c2bdd9a1SWarner Losh * Functions for accessing guest memory. The tget and tput functions
412c2bdd9a1SWarner Losh * read/write single values, byteswapping as necessary. The lock_user function
413c2bdd9a1SWarner Losh * gets a pointer to a contiguous area of guest memory, but does not perform
414c2bdd9a1SWarner Losh * any byteswapping. lock_user may return either a pointer to the guest
415c2bdd9a1SWarner Losh * memory, or a temporary buffer.
416c2bdd9a1SWarner Losh */
41784778508Sblueswir1
418c2bdd9a1SWarner Losh /*
419c2bdd9a1SWarner Losh * Lock an area of guest memory into the host. If copy is true then the
420c2bdd9a1SWarner Losh * host area will have the same contents as the guest.
421c2bdd9a1SWarner Losh */
lock_user(int type,abi_ulong guest_addr,long len,int copy)422c2bdd9a1SWarner Losh static inline void *lock_user(int type, abi_ulong guest_addr, long len,
423c2bdd9a1SWarner Losh int copy)
42484778508Sblueswir1 {
425cb0ea019SWarner Losh if (!access_ok(type, guest_addr, len)) {
42684778508Sblueswir1 return NULL;
427cb0ea019SWarner Losh }
42884778508Sblueswir1 #ifdef DEBUG_REMAP
42984778508Sblueswir1 {
43084778508Sblueswir1 void *addr;
431fd9a3048SMd Haris Iqbal addr = g_malloc(len);
432cb0ea019SWarner Losh if (copy) {
4333e8f1628SRichard Henderson memcpy(addr, g2h_untagged(guest_addr), len);
434cb0ea019SWarner Losh } else {
43584778508Sblueswir1 memset(addr, 0, len);
436cb0ea019SWarner Losh }
43784778508Sblueswir1 return addr;
43884778508Sblueswir1 }
43984778508Sblueswir1 #else
4403e8f1628SRichard Henderson return g2h_untagged(guest_addr);
44184778508Sblueswir1 #endif
44284778508Sblueswir1 }
44384778508Sblueswir1
444c2bdd9a1SWarner Losh /*
445c2bdd9a1SWarner Losh * Unlock an area of guest memory. The first LEN bytes must be flushed back to
446c2bdd9a1SWarner Losh * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
447c2bdd9a1SWarner Losh */
unlock_user(void * host_ptr,abi_ulong guest_addr,long len)44884778508Sblueswir1 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
44984778508Sblueswir1 long len)
45084778508Sblueswir1 {
45184778508Sblueswir1
45284778508Sblueswir1 #ifdef DEBUG_REMAP
453cb0ea019SWarner Losh if (!host_ptr) {
45484778508Sblueswir1 return;
455cb0ea019SWarner Losh }
456cb0ea019SWarner Losh if (host_ptr == g2h_untagged(guest_addr)) {
45784778508Sblueswir1 return;
458cb0ea019SWarner Losh }
459cb0ea019SWarner Losh if (len > 0) {
4603e8f1628SRichard Henderson memcpy(g2h_untagged(guest_addr), host_ptr, len);
461cb0ea019SWarner Losh }
462fd9a3048SMd Haris Iqbal g_free(host_ptr);
46384778508Sblueswir1 #endif
46484778508Sblueswir1 }
46584778508Sblueswir1
466c2bdd9a1SWarner Losh /*
467c2bdd9a1SWarner Losh * Return the length of a string in target memory or -TARGET_EFAULT if access
468c2bdd9a1SWarner Losh * error.
469c2bdd9a1SWarner Losh */
47084778508Sblueswir1 abi_long target_strlen(abi_ulong gaddr);
47184778508Sblueswir1
47284778508Sblueswir1 /* Like lock_user but for null terminated strings. */
lock_user_string(abi_ulong guest_addr)47384778508Sblueswir1 static inline void *lock_user_string(abi_ulong guest_addr)
47484778508Sblueswir1 {
47584778508Sblueswir1 abi_long len;
47684778508Sblueswir1 len = target_strlen(guest_addr);
477cb0ea019SWarner Losh if (len < 0) {
47884778508Sblueswir1 return NULL;
479cb0ea019SWarner Losh }
48084778508Sblueswir1 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
48184778508Sblueswir1 }
48284778508Sblueswir1
48341d1af4dSStefan Weil /* Helper macros for locking/unlocking a target struct. */
48484778508Sblueswir1 #define lock_user_struct(type, host_ptr, guest_addr, copy) \
48584778508Sblueswir1 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
48684778508Sblueswir1 #define unlock_user_struct(host_ptr, guest_addr, copy) \
48784778508Sblueswir1 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
48884778508Sblueswir1
target_arg64(uint32_t word0,uint32_t word1)4890ff05082SWarner Losh static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
4900ff05082SWarner Losh {
4910ff05082SWarner Losh #if TARGET_ABI_BITS == 32
492ee3eb3a7SMarc-André Lureau #if TARGET_BIG_ENDIAN
4930ff05082SWarner Losh return ((uint64_t)word0 << 32) | word1;
4940ff05082SWarner Losh #else
4950ff05082SWarner Losh return ((uint64_t)word1 << 32) | word0;
4960ff05082SWarner Losh #endif
4970ff05082SWarner Losh #else /* TARGET_ABI_BITS != 32 */
4980ff05082SWarner Losh return word0;
4990ff05082SWarner Losh #endif /* TARGET_ABI_BITS != 32 */
5000ff05082SWarner Losh }
5010ff05082SWarner Losh
50284778508Sblueswir1 #include <pthread.h>
50384778508Sblueswir1
504aae57ac3SWarner Losh #include "user/safe-syscall.h"
505aae57ac3SWarner Losh
50684778508Sblueswir1 #endif /* QEMU_H */
507