xref: /openbmc/qemu/bsd-user/qemu.h (revision 9b4a902d)
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 
41*9b4a902dSStacey 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);
11484778508Sblueswir1 extern const char *qemu_uname_release;
11584778508Sblueswir1 
11684778508Sblueswir1 /*
117e5e44263SWarner Losh  * TARGET_ARG_MAX defines the number of bytes allocated for arguments
118e5e44263SWarner Losh  * and envelope for the new program. 256k should suffice for a reasonable
119e5e44263SWarner Losh  * maxiumum env+arg in 32-bit environments, bump it up to 512k for !ILP32
120e5e44263SWarner Losh  * platforms.
12184778508Sblueswir1  */
122e5e44263SWarner Losh #if TARGET_ABI_BITS > 32
123e5e44263SWarner Losh #define TARGET_ARG_MAX (512 * KiB)
124e5e44263SWarner Losh #else
125e5e44263SWarner Losh #define TARGET_ARG_MAX (256 * KiB)
126e5e44263SWarner Losh #endif
127e5e44263SWarner Losh #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
12884778508Sblueswir1 
12984778508Sblueswir1 /*
13084778508Sblueswir1  * This structure is used to hold the arguments that are
13184778508Sblueswir1  * used when loading binaries.
13284778508Sblueswir1  */
133afcbcff8SWarner Losh struct bsd_binprm {
13484778508Sblueswir1         char buf[128];
13584778508Sblueswir1         void *page[MAX_ARG_PAGES];
13684778508Sblueswir1         abi_ulong p;
13798b34d35SWarner Losh         abi_ulong stringp;
13884778508Sblueswir1         int fd;
13984778508Sblueswir1         int e_uid, e_gid;
14084778508Sblueswir1         int argc, envc;
14184778508Sblueswir1         char **argv;
14284778508Sblueswir1         char **envp;
1431b50ff64SWarner Losh         char *filename;         /* (Given) Name of binary */
1441b50ff64SWarner Losh         char *fullpath;         /* Full path of binary */
1450475f8faSWarner Losh         int (*core_dump)(int, CPUArchState *);
14684778508Sblueswir1 };
14784778508Sblueswir1 
14884778508Sblueswir1 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
14984778508Sblueswir1 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
150ffa03665SWarner Losh                               abi_ulong stringp);
15184778508Sblueswir1 int loader_exec(const char *filename, char **argv, char **envp,
152d37853f9SWarner Losh                 struct target_pt_regs *regs, struct image_info *infop,
153d37853f9SWarner Losh                 struct bsd_binprm *bprm);
15484778508Sblueswir1 
155afcbcff8SWarner Losh int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
15684778508Sblueswir1                     struct image_info *info);
157afcbcff8SWarner Losh int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
15884778508Sblueswir1                     struct image_info *info);
1590475f8faSWarner Losh int is_target_elf_binary(int fd);
16084778508Sblueswir1 
16184778508Sblueswir1 abi_long memcpy_to_target(abi_ulong dest, const void *src,
16284778508Sblueswir1                           unsigned long len);
16384778508Sblueswir1 void target_set_brk(abi_ulong new_brk);
16484778508Sblueswir1 abi_long do_brk(abi_ulong new_brk);
16584778508Sblueswir1 void syscall_init(void);
16684778508Sblueswir1 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
16784778508Sblueswir1                             abi_long arg2, abi_long arg3, abi_long arg4,
16878cfb07fSJuergen Lock                             abi_long arg5, abi_long arg6, abi_long arg7,
16978cfb07fSJuergen Lock                             abi_long arg8);
17084778508Sblueswir1 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
17184778508Sblueswir1                            abi_long arg2, abi_long arg3, abi_long arg4,
17284778508Sblueswir1                            abi_long arg5, abi_long arg6);
17384778508Sblueswir1 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
17484778508Sblueswir1                             abi_long arg2, abi_long arg3, abi_long arg4,
17584778508Sblueswir1                             abi_long arg5, abi_long arg6);
1769edc6313SMarc-André Lureau void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
177d42df502SWarner Losh extern __thread CPUState *thread_cpu;
1789349b4f9SAndreas Färber void cpu_loop(CPUArchState *env);
17984778508Sblueswir1 char *target_strerror(int err);
18084778508Sblueswir1 int get_osversion(void);
18184778508Sblueswir1 void fork_start(void);
18284778508Sblueswir1 void fork_end(int child);
18384778508Sblueswir1 
1841de7afc9SPaolo Bonzini #include "qemu/log.h"
18584778508Sblueswir1 
18684778508Sblueswir1 /* strace.c */
18788dae46dSSean Bruno struct syscallname {
18888dae46dSSean Bruno     int nr;
18988dae46dSSean Bruno     const char *name;
19088dae46dSSean Bruno     const char *format;
19188dae46dSSean Bruno     void (*call)(const struct syscallname *,
19288dae46dSSean Bruno                  abi_long, abi_long, abi_long,
19388dae46dSSean Bruno                  abi_long, abi_long, abi_long);
19488dae46dSSean Bruno     void (*result)(const struct syscallname *, abi_long);
19588dae46dSSean Bruno };
19688dae46dSSean Bruno 
19784778508Sblueswir1 void
19884778508Sblueswir1 print_freebsd_syscall(int num,
19984778508Sblueswir1                       abi_long arg1, abi_long arg2, abi_long arg3,
20084778508Sblueswir1                       abi_long arg4, abi_long arg5, abi_long arg6);
20184778508Sblueswir1 void print_freebsd_syscall_ret(int num, abi_long ret);
20284778508Sblueswir1 void
20384778508Sblueswir1 print_netbsd_syscall(int num,
20484778508Sblueswir1                      abi_long arg1, abi_long arg2, abi_long arg3,
20584778508Sblueswir1                      abi_long arg4, abi_long arg5, abi_long arg6);
20684778508Sblueswir1 void print_netbsd_syscall_ret(int num, abi_long ret);
20784778508Sblueswir1 void
20884778508Sblueswir1 print_openbsd_syscall(int num,
20984778508Sblueswir1                       abi_long arg1, abi_long arg2, abi_long arg3,
21084778508Sblueswir1                       abi_long arg4, abi_long arg5, abi_long arg6);
21184778508Sblueswir1 void print_openbsd_syscall_ret(int num, abi_long ret);
212fd5bec9aSWarner Losh /**
213fd5bec9aSWarner Losh  * print_taken_signal:
214fd5bec9aSWarner Losh  * @target_signum: target signal being taken
215fd5bec9aSWarner Losh  * @tinfo: target_siginfo_t which will be passed to the guest for the signal
216fd5bec9aSWarner Losh  *
217fd5bec9aSWarner Losh  * Print strace output indicating that this signal is being taken by the guest,
218fd5bec9aSWarner Losh  * in a format similar to:
219fd5bec9aSWarner Losh  * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
220fd5bec9aSWarner Losh  */
221fd5bec9aSWarner Losh void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
22284778508Sblueswir1 extern int do_strace;
22384778508Sblueswir1 
22484778508Sblueswir1 /* mmap.c */
22584778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot);
22684778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
227be04f210SWarner Losh                      int flags, int fd, off_t offset);
22884778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len);
22984778508Sblueswir1 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
23084778508Sblueswir1                        abi_ulong new_size, unsigned long flags,
23184778508Sblueswir1                        abi_ulong new_addr);
23284778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags);
233be04f210SWarner Losh extern abi_ulong mmap_next_start;
234be04f210SWarner Losh abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
235e022d9caSEmanuele Giuseppe Esposito void TSA_NO_TSA mmap_fork_start(void);
236e022d9caSEmanuele Giuseppe Esposito void TSA_NO_TSA mmap_fork_end(int child);
23784778508Sblueswir1 
23828e738dcSBlue Swirl /* main.c */
23901a298a5SWarner Losh extern char qemu_proc_pathname[];
240312a0b1cSWarner Losh extern unsigned long target_maxtsiz;
241312a0b1cSWarner Losh extern unsigned long target_dfldsiz;
242312a0b1cSWarner Losh extern unsigned long target_maxdsiz;
243312a0b1cSWarner Losh extern unsigned long target_dflssiz;
244312a0b1cSWarner Losh extern unsigned long target_maxssiz;
245312a0b1cSWarner Losh extern unsigned long target_sgrowsiz;
24628e738dcSBlue Swirl 
247deeff83bSWarner Losh /* os-syscall.c */
248e5f674f0SWarner Losh abi_long get_errno(abi_long ret);
249e5f674f0SWarner Losh bool is_error(abi_long ret);
250deeff83bSWarner Losh int host_to_target_errno(int err);
251e5f674f0SWarner Losh 
252da07e694SWarner Losh /* os-sys.c */
2537adda6deSKyle Evans abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
2547adda6deSKyle Evans         abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
2556da777e2SKyle Evans abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
2566da777e2SKyle Evans         int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp,
2576da777e2SKyle Evans         abi_ulong newlen);
258da07e694SWarner Losh abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
259da07e694SWarner Losh 
26084778508Sblueswir1 /* user access */
26184778508Sblueswir1 
2621720751fSRichard Henderson #define VERIFY_READ  PAGE_READ
2631720751fSRichard Henderson #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
26484778508Sblueswir1 
2651720751fSRichard Henderson static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
26684778508Sblueswir1 {
267bef6f008SRichard Henderson     return page_check_range((target_ulong)addr, size, type);
26884778508Sblueswir1 }
26984778508Sblueswir1 
270c2bdd9a1SWarner Losh /*
271c2bdd9a1SWarner Losh  * NOTE __get_user and __put_user use host pointers and don't check access.
272c2bdd9a1SWarner Losh  *
273c2bdd9a1SWarner Losh  * These are usually used to access struct data members once the struct has been
274c2bdd9a1SWarner Losh  * locked - usually with lock_user_struct().
27584778508Sblueswir1  */
2766538c682SWarner Losh #define __put_user_e(x, hptr, e)                                            \
2776538c682SWarner Losh     do {                                                                    \
2786538c682SWarner Losh         PRAGMA_DISABLE_PACKED_WARNING;                                      \
2796538c682SWarner Losh         (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p,                 \
2806538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p,            \
2816538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p,            \
2826538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort))))  \
2836538c682SWarner Losh             ((hptr), (x)), (void)0);                                        \
2846538c682SWarner Losh         PRAGMA_REENABLE_PACKED_WARNING;                                     \
2856538c682SWarner Losh     } while (0)
28684778508Sblueswir1 
2876538c682SWarner Losh #define __get_user_e(x, hptr, e)                                            \
2886538c682SWarner Losh     do {                                                                    \
2896538c682SWarner Losh         PRAGMA_DISABLE_PACKED_WARNING;                                      \
2906538c682SWarner Losh         ((x) = (typeof(*hptr))(                                             \
2916538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p,                 \
2926538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p,           \
2936538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p,            \
2946538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort))))  \
2956538c682SWarner Losh             (hptr)), (void)0);                                              \
2966538c682SWarner Losh         PRAGMA_REENABLE_PACKED_WARNING;                                     \
2976538c682SWarner Losh     } while (0)
2986538c682SWarner Losh 
2996538c682SWarner Losh 
3006538c682SWarner Losh #if TARGET_BIG_ENDIAN
3016538c682SWarner Losh # define __put_user(x, hptr)  __put_user_e(x, hptr, be)
3026538c682SWarner Losh # define __get_user(x, hptr)  __get_user_e(x, hptr, be)
3036538c682SWarner Losh #else
3046538c682SWarner Losh # define __put_user(x, hptr)  __put_user_e(x, hptr, le)
3056538c682SWarner Losh # define __get_user(x, hptr)  __get_user_e(x, hptr, le)
3066538c682SWarner Losh #endif
30784778508Sblueswir1 
308c2bdd9a1SWarner Losh /*
309c2bdd9a1SWarner Losh  * put_user()/get_user() take a guest address and check access
310c2bdd9a1SWarner Losh  *
311c2bdd9a1SWarner Losh  * These are usually used to access an atomic data type, such as an int, that
312c2bdd9a1SWarner Losh  * has been passed by address.  These internally perform locking and unlocking
313c2bdd9a1SWarner Losh  * on the data type.
31484778508Sblueswir1  */
31584778508Sblueswir1 #define put_user(x, gaddr, target_type)                                 \
31684778508Sblueswir1 ({                                                                      \
31784778508Sblueswir1     abi_ulong __gaddr = (gaddr);                                        \
31884778508Sblueswir1     target_type *__hptr;                                                \
3196538c682SWarner Losh     abi_long __ret = 0;                                                 \
32033066934SWarner Losh     __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0);  \
32133066934SWarner Losh     if (__hptr) {                                                       \
3226538c682SWarner Losh         __put_user((x), __hptr);                                        \
32384778508Sblueswir1         unlock_user(__hptr, __gaddr, sizeof(target_type));              \
32484778508Sblueswir1     } else                                                              \
32584778508Sblueswir1         __ret = -TARGET_EFAULT;                                         \
32684778508Sblueswir1     __ret;                                                              \
32784778508Sblueswir1 })
32884778508Sblueswir1 
32984778508Sblueswir1 #define get_user(x, gaddr, target_type)                                 \
33084778508Sblueswir1 ({                                                                      \
33184778508Sblueswir1     abi_ulong __gaddr = (gaddr);                                        \
33284778508Sblueswir1     target_type *__hptr;                                                \
3336538c682SWarner Losh     abi_long __ret = 0;                                                 \
33433066934SWarner Losh     __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1);   \
33533066934SWarner Losh     if (__hptr) {                                                       \
3366538c682SWarner Losh         __get_user((x), __hptr);                                        \
33784778508Sblueswir1         unlock_user(__hptr, __gaddr, 0);                                \
33884778508Sblueswir1     } else {                                                            \
33984778508Sblueswir1         (x) = 0;                                                        \
34084778508Sblueswir1         __ret = -TARGET_EFAULT;                                         \
34184778508Sblueswir1     }                                                                   \
34284778508Sblueswir1     __ret;                                                              \
34384778508Sblueswir1 })
34484778508Sblueswir1 
34584778508Sblueswir1 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
34684778508Sblueswir1 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
34784778508Sblueswir1 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
34884778508Sblueswir1 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
34984778508Sblueswir1 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
35084778508Sblueswir1 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
35184778508Sblueswir1 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
35284778508Sblueswir1 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
35384778508Sblueswir1 #define put_user_u8(x, gaddr)  put_user((x), (gaddr), uint8_t)
35484778508Sblueswir1 #define put_user_s8(x, gaddr)  put_user((x), (gaddr), int8_t)
35584778508Sblueswir1 
35684778508Sblueswir1 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
35784778508Sblueswir1 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
35884778508Sblueswir1 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
35984778508Sblueswir1 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
36084778508Sblueswir1 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
36184778508Sblueswir1 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
36284778508Sblueswir1 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
36384778508Sblueswir1 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
36484778508Sblueswir1 #define get_user_u8(x, gaddr)  get_user((x), (gaddr), uint8_t)
36584778508Sblueswir1 #define get_user_s8(x, gaddr)  get_user((x), (gaddr), int8_t)
36684778508Sblueswir1 
367c2bdd9a1SWarner Losh /*
368c2bdd9a1SWarner Losh  * copy_from_user() and copy_to_user() are usually used to copy data
36984778508Sblueswir1  * buffers between the target and host.  These internally perform
37084778508Sblueswir1  * locking/unlocking of the memory.
37184778508Sblueswir1  */
37284778508Sblueswir1 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
37384778508Sblueswir1 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
37484778508Sblueswir1 
375c2bdd9a1SWarner Losh /*
376c2bdd9a1SWarner Losh  * Functions for accessing guest memory.  The tget and tput functions
377c2bdd9a1SWarner Losh  * read/write single values, byteswapping as necessary.  The lock_user function
378c2bdd9a1SWarner Losh  * gets a pointer to a contiguous area of guest memory, but does not perform
379c2bdd9a1SWarner Losh  * any byteswapping.  lock_user may return either a pointer to the guest
380c2bdd9a1SWarner Losh  * memory, or a temporary buffer.
381c2bdd9a1SWarner Losh  */
38284778508Sblueswir1 
383c2bdd9a1SWarner Losh /*
384c2bdd9a1SWarner Losh  * Lock an area of guest memory into the host.  If copy is true then the
385c2bdd9a1SWarner Losh  * host area will have the same contents as the guest.
386c2bdd9a1SWarner Losh  */
387c2bdd9a1SWarner Losh static inline void *lock_user(int type, abi_ulong guest_addr, long len,
388c2bdd9a1SWarner Losh                               int copy)
38984778508Sblueswir1 {
390cb0ea019SWarner Losh     if (!access_ok(type, guest_addr, len)) {
39184778508Sblueswir1         return NULL;
392cb0ea019SWarner Losh     }
39384778508Sblueswir1 #ifdef DEBUG_REMAP
39484778508Sblueswir1     {
39584778508Sblueswir1         void *addr;
396fd9a3048SMd Haris Iqbal         addr = g_malloc(len);
397cb0ea019SWarner Losh         if (copy) {
3983e8f1628SRichard Henderson             memcpy(addr, g2h_untagged(guest_addr), len);
399cb0ea019SWarner Losh         } else {
40084778508Sblueswir1             memset(addr, 0, len);
401cb0ea019SWarner Losh         }
40284778508Sblueswir1         return addr;
40384778508Sblueswir1     }
40484778508Sblueswir1 #else
4053e8f1628SRichard Henderson     return g2h_untagged(guest_addr);
40684778508Sblueswir1 #endif
40784778508Sblueswir1 }
40884778508Sblueswir1 
409c2bdd9a1SWarner Losh /*
410c2bdd9a1SWarner Losh  * Unlock an area of guest memory.  The first LEN bytes must be flushed back to
411c2bdd9a1SWarner Losh  * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
412c2bdd9a1SWarner Losh  */
41384778508Sblueswir1 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
41484778508Sblueswir1                                long len)
41584778508Sblueswir1 {
41684778508Sblueswir1 
41784778508Sblueswir1 #ifdef DEBUG_REMAP
418cb0ea019SWarner Losh     if (!host_ptr) {
41984778508Sblueswir1         return;
420cb0ea019SWarner Losh     }
421cb0ea019SWarner Losh     if (host_ptr == g2h_untagged(guest_addr)) {
42284778508Sblueswir1         return;
423cb0ea019SWarner Losh     }
424cb0ea019SWarner Losh     if (len > 0) {
4253e8f1628SRichard Henderson         memcpy(g2h_untagged(guest_addr), host_ptr, len);
426cb0ea019SWarner Losh     }
427fd9a3048SMd Haris Iqbal     g_free(host_ptr);
42884778508Sblueswir1 #endif
42984778508Sblueswir1 }
43084778508Sblueswir1 
431c2bdd9a1SWarner Losh /*
432c2bdd9a1SWarner Losh  * Return the length of a string in target memory or -TARGET_EFAULT if access
433c2bdd9a1SWarner Losh  * error.
434c2bdd9a1SWarner Losh  */
43584778508Sblueswir1 abi_long target_strlen(abi_ulong gaddr);
43684778508Sblueswir1 
43784778508Sblueswir1 /* Like lock_user but for null terminated strings.  */
43884778508Sblueswir1 static inline void *lock_user_string(abi_ulong guest_addr)
43984778508Sblueswir1 {
44084778508Sblueswir1     abi_long len;
44184778508Sblueswir1     len = target_strlen(guest_addr);
442cb0ea019SWarner Losh     if (len < 0) {
44384778508Sblueswir1         return NULL;
444cb0ea019SWarner Losh     }
44584778508Sblueswir1     return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
44684778508Sblueswir1 }
44784778508Sblueswir1 
44841d1af4dSStefan Weil /* Helper macros for locking/unlocking a target struct.  */
44984778508Sblueswir1 #define lock_user_struct(type, host_ptr, guest_addr, copy)      \
45084778508Sblueswir1     (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
45184778508Sblueswir1 #define unlock_user_struct(host_ptr, guest_addr, copy)          \
45284778508Sblueswir1     unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
45384778508Sblueswir1 
4540ff05082SWarner Losh static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
4550ff05082SWarner Losh {
4560ff05082SWarner Losh #if TARGET_ABI_BITS == 32
457ee3eb3a7SMarc-André Lureau #if TARGET_BIG_ENDIAN
4580ff05082SWarner Losh     return ((uint64_t)word0 << 32) | word1;
4590ff05082SWarner Losh #else
4600ff05082SWarner Losh     return ((uint64_t)word1 << 32) | word0;
4610ff05082SWarner Losh #endif
4620ff05082SWarner Losh #else /* TARGET_ABI_BITS != 32 */
4630ff05082SWarner Losh     return word0;
4640ff05082SWarner Losh #endif /* TARGET_ABI_BITS != 32 */
4650ff05082SWarner Losh }
4660ff05082SWarner Losh 
46784778508Sblueswir1 #include <pthread.h>
46884778508Sblueswir1 
469aae57ac3SWarner Losh #include "user/safe-syscall.h"
470aae57ac3SWarner Losh 
47184778508Sblueswir1 #endif /* QEMU_H */
472