xref: /qemu/bsd-user/qemu.h (revision 6538c682)
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 
41c2bdd9a1SWarner Losh /*
42c2bdd9a1SWarner Losh  * This struct is used to hold certain information about the image.  Basically,
43c2bdd9a1SWarner Losh  * it replicates in user space what would be certain task_struct fields in the
44c2bdd9a1SWarner Losh  * kernel
4584778508Sblueswir1  */
4684778508Sblueswir1 struct image_info {
470475f8faSWarner Losh     abi_ulong load_bias;
4884778508Sblueswir1     abi_ulong load_addr;
4984778508Sblueswir1     abi_ulong start_code;
5084778508Sblueswir1     abi_ulong end_code;
5184778508Sblueswir1     abi_ulong start_data;
5284778508Sblueswir1     abi_ulong end_data;
5384778508Sblueswir1     abi_ulong brk;
5484778508Sblueswir1     abi_ulong rss;
5584778508Sblueswir1     abi_ulong start_stack;
5684778508Sblueswir1     abi_ulong entry;
5784778508Sblueswir1     abi_ulong code_offset;
5884778508Sblueswir1     abi_ulong data_offset;
590475f8faSWarner Losh     abi_ulong arg_start;
600475f8faSWarner Losh     abi_ulong arg_end;
610475f8faSWarner Losh     uint32_t  elf_flags;
6284778508Sblueswir1 };
6384778508Sblueswir1 
6484778508Sblueswir1 struct emulated_sigtable {
6584778508Sblueswir1     int pending; /* true if signal is pending */
66b46d4ad7SWarner Losh     target_siginfo_t info;
6784778508Sblueswir1 };
6884778508Sblueswir1 
69c2bdd9a1SWarner Losh /*
70c2bdd9a1SWarner Losh  * NOTE: we force a big alignment so that the stack stored after is aligned too
71c2bdd9a1SWarner Losh  */
7284778508Sblueswir1 typedef struct TaskState {
73bd88c780SAlex Bennée     pid_t ts_tid;     /* tid (or pid) of this task */
74bd88c780SAlex Bennée 
7584778508Sblueswir1     struct TaskState *next;
76031fe7afSWarner Losh     struct bsd_binprm *bprm;
7784778508Sblueswir1     struct image_info *info;
7884778508Sblueswir1 
7938be620cSWarner Losh     struct emulated_sigtable sync_signal;
8038be620cSWarner Losh     /*
8138be620cSWarner Losh      * TODO: Since we block all signals while returning to the main CPU
8238be620cSWarner Losh      * loop, this needn't be an array
8338be620cSWarner Losh      */
8484778508Sblueswir1     struct emulated_sigtable sigtab[TARGET_NSIG];
8548047225SWarner Losh     /*
8648047225SWarner Losh      * Nonzero if process_pending_signals() needs to do something (either
8748047225SWarner Losh      * handle a pending signal or unblock signals).
8848047225SWarner Losh      * This flag is written from a signal handler so should be accessed via
8948047225SWarner Losh      * the qatomic_read() and qatomic_set() functions. (It is not accessed
9048047225SWarner Losh      * from multiple threads.)
9148047225SWarner Losh      */
9248047225SWarner Losh     int signal_pending;
936c6d4b56SWarner Losh     /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */
946c6d4b56SWarner Losh     bool in_sigsuspend;
95149076adSWarner Losh     /*
96149076adSWarner Losh      * This thread's signal mask, as requested by the guest program.
97149076adSWarner Losh      * The actual signal mask of this thread may differ:
98149076adSWarner Losh      *  + we don't let SIGSEGV and SIGBUS be blocked while running guest code
99149076adSWarner Losh      *  + sometimes we block all signals to avoid races
100149076adSWarner Losh      */
101149076adSWarner Losh     sigset_t signal_mask;
1026c6d4b56SWarner Losh     /*
1036c6d4b56SWarner Losh      * The signal mask imposed by a guest sigsuspend syscall, if we are
1046c6d4b56SWarner Losh      * currently in the middle of such a syscall
1056c6d4b56SWarner Losh      */
1066c6d4b56SWarner Losh     sigset_t sigsuspend_mask;
10784778508Sblueswir1 
10846f4f76dSWarner Losh     /* This thread's sigaltstack, if it has one */
10946f4f76dSWarner Losh     struct target_sigaltstack sigaltstack_used;
11084778508Sblueswir1 } __attribute__((aligned(16))) TaskState;
11184778508Sblueswir1 
112653ccec2SWarner Losh void stop_all_tasks(void);
11384778508Sblueswir1 extern const char *qemu_uname_release;
11484778508Sblueswir1 
11584778508Sblueswir1 /*
116e5e44263SWarner Losh  * TARGET_ARG_MAX defines the number of bytes allocated for arguments
117e5e44263SWarner Losh  * and envelope for the new program. 256k should suffice for a reasonable
118e5e44263SWarner Losh  * maxiumum env+arg in 32-bit environments, bump it up to 512k for !ILP32
119e5e44263SWarner Losh  * platforms.
12084778508Sblueswir1  */
121e5e44263SWarner Losh #if TARGET_ABI_BITS > 32
122e5e44263SWarner Losh #define TARGET_ARG_MAX (512 * KiB)
123e5e44263SWarner Losh #else
124e5e44263SWarner Losh #define TARGET_ARG_MAX (256 * KiB)
125e5e44263SWarner Losh #endif
126e5e44263SWarner Losh #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
12784778508Sblueswir1 
12884778508Sblueswir1 /*
12984778508Sblueswir1  * This structure is used to hold the arguments that are
13084778508Sblueswir1  * used when loading binaries.
13184778508Sblueswir1  */
132afcbcff8SWarner Losh struct bsd_binprm {
13384778508Sblueswir1         char buf[128];
13484778508Sblueswir1         void *page[MAX_ARG_PAGES];
13584778508Sblueswir1         abi_ulong p;
13698b34d35SWarner Losh         abi_ulong stringp;
13784778508Sblueswir1         int fd;
13884778508Sblueswir1         int e_uid, e_gid;
13984778508Sblueswir1         int argc, envc;
14084778508Sblueswir1         char **argv;
14184778508Sblueswir1         char **envp;
1421b50ff64SWarner Losh         char *filename;         /* (Given) Name of binary */
1431b50ff64SWarner Losh         char *fullpath;         /* Full path of binary */
1440475f8faSWarner Losh         int (*core_dump)(int, CPUArchState *);
14584778508Sblueswir1 };
14684778508Sblueswir1 
14784778508Sblueswir1 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
14884778508Sblueswir1 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
149ffa03665SWarner Losh                               abi_ulong stringp);
15084778508Sblueswir1 int loader_exec(const char *filename, char **argv, char **envp,
151d37853f9SWarner Losh                 struct target_pt_regs *regs, struct image_info *infop,
152d37853f9SWarner Losh                 struct bsd_binprm *bprm);
15384778508Sblueswir1 
154afcbcff8SWarner Losh int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
15584778508Sblueswir1                     struct image_info *info);
156afcbcff8SWarner Losh int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
15784778508Sblueswir1                     struct image_info *info);
1580475f8faSWarner Losh int is_target_elf_binary(int fd);
15984778508Sblueswir1 
16084778508Sblueswir1 abi_long memcpy_to_target(abi_ulong dest, const void *src,
16184778508Sblueswir1                           unsigned long len);
16284778508Sblueswir1 void target_set_brk(abi_ulong new_brk);
16384778508Sblueswir1 abi_long do_brk(abi_ulong new_brk);
16484778508Sblueswir1 void syscall_init(void);
16584778508Sblueswir1 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
16684778508Sblueswir1                             abi_long arg2, abi_long arg3, abi_long arg4,
16778cfb07fSJuergen Lock                             abi_long arg5, abi_long arg6, abi_long arg7,
16878cfb07fSJuergen Lock                             abi_long arg8);
16984778508Sblueswir1 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
17084778508Sblueswir1                            abi_long arg2, abi_long arg3, abi_long arg4,
17184778508Sblueswir1                            abi_long arg5, abi_long arg6);
17284778508Sblueswir1 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
17384778508Sblueswir1                             abi_long arg2, abi_long arg3, abi_long arg4,
17484778508Sblueswir1                             abi_long arg5, abi_long arg6);
1759edc6313SMarc-André Lureau void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
176d42df502SWarner Losh extern __thread CPUState *thread_cpu;
1779349b4f9SAndreas Färber void cpu_loop(CPUArchState *env);
17884778508Sblueswir1 char *target_strerror(int err);
17984778508Sblueswir1 int get_osversion(void);
18084778508Sblueswir1 void fork_start(void);
18184778508Sblueswir1 void fork_end(int child);
18284778508Sblueswir1 
1831de7afc9SPaolo Bonzini #include "qemu/log.h"
18484778508Sblueswir1 
18584778508Sblueswir1 /* strace.c */
18688dae46dSSean Bruno struct syscallname {
18788dae46dSSean Bruno     int nr;
18888dae46dSSean Bruno     const char *name;
18988dae46dSSean Bruno     const char *format;
19088dae46dSSean Bruno     void (*call)(const struct syscallname *,
19188dae46dSSean Bruno                  abi_long, abi_long, abi_long,
19288dae46dSSean Bruno                  abi_long, abi_long, abi_long);
19388dae46dSSean Bruno     void (*result)(const struct syscallname *, abi_long);
19488dae46dSSean Bruno };
19588dae46dSSean Bruno 
19684778508Sblueswir1 void
19784778508Sblueswir1 print_freebsd_syscall(int num,
19884778508Sblueswir1                       abi_long arg1, abi_long arg2, abi_long arg3,
19984778508Sblueswir1                       abi_long arg4, abi_long arg5, abi_long arg6);
20084778508Sblueswir1 void print_freebsd_syscall_ret(int num, abi_long ret);
20184778508Sblueswir1 void
20284778508Sblueswir1 print_netbsd_syscall(int num,
20384778508Sblueswir1                      abi_long arg1, abi_long arg2, abi_long arg3,
20484778508Sblueswir1                      abi_long arg4, abi_long arg5, abi_long arg6);
20584778508Sblueswir1 void print_netbsd_syscall_ret(int num, abi_long ret);
20684778508Sblueswir1 void
20784778508Sblueswir1 print_openbsd_syscall(int num,
20884778508Sblueswir1                       abi_long arg1, abi_long arg2, abi_long arg3,
20984778508Sblueswir1                       abi_long arg4, abi_long arg5, abi_long arg6);
21084778508Sblueswir1 void print_openbsd_syscall_ret(int num, abi_long ret);
211fd5bec9aSWarner Losh /**
212fd5bec9aSWarner Losh  * print_taken_signal:
213fd5bec9aSWarner Losh  * @target_signum: target signal being taken
214fd5bec9aSWarner Losh  * @tinfo: target_siginfo_t which will be passed to the guest for the signal
215fd5bec9aSWarner Losh  *
216fd5bec9aSWarner Losh  * Print strace output indicating that this signal is being taken by the guest,
217fd5bec9aSWarner Losh  * in a format similar to:
218fd5bec9aSWarner Losh  * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
219fd5bec9aSWarner Losh  */
220fd5bec9aSWarner Losh void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
22184778508Sblueswir1 extern int do_strace;
22284778508Sblueswir1 
22384778508Sblueswir1 /* mmap.c */
22484778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot);
22584778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
226be04f210SWarner Losh                      int flags, int fd, off_t offset);
22784778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len);
22884778508Sblueswir1 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
22984778508Sblueswir1                        abi_ulong new_size, unsigned long flags,
23084778508Sblueswir1                        abi_ulong new_addr);
23184778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags);
232be04f210SWarner Losh extern abi_ulong mmap_next_start;
233be04f210SWarner Losh abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
234e022d9caSEmanuele Giuseppe Esposito void TSA_NO_TSA mmap_fork_start(void);
235e022d9caSEmanuele Giuseppe Esposito void TSA_NO_TSA mmap_fork_end(int child);
23684778508Sblueswir1 
23728e738dcSBlue Swirl /* main.c */
23801a298a5SWarner Losh extern char qemu_proc_pathname[];
239312a0b1cSWarner Losh extern unsigned long target_maxtsiz;
240312a0b1cSWarner Losh extern unsigned long target_dfldsiz;
241312a0b1cSWarner Losh extern unsigned long target_maxdsiz;
242312a0b1cSWarner Losh extern unsigned long target_dflssiz;
243312a0b1cSWarner Losh extern unsigned long target_maxssiz;
244312a0b1cSWarner Losh extern unsigned long target_sgrowsiz;
24528e738dcSBlue Swirl 
246deeff83bSWarner Losh /* os-syscall.c */
247e5f674f0SWarner Losh abi_long get_errno(abi_long ret);
248e5f674f0SWarner Losh bool is_error(abi_long ret);
249deeff83bSWarner Losh int host_to_target_errno(int err);
250e5f674f0SWarner Losh 
251da07e694SWarner Losh /* os-sys.c */
2527adda6deSKyle Evans abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
2537adda6deSKyle Evans         abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
2546da777e2SKyle Evans abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
2556da777e2SKyle Evans         int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp,
2566da777e2SKyle Evans         abi_ulong newlen);
257da07e694SWarner Losh abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
258da07e694SWarner Losh 
25984778508Sblueswir1 /* user access */
26084778508Sblueswir1 
2611720751fSRichard Henderson #define VERIFY_READ  PAGE_READ
2621720751fSRichard Henderson #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
26384778508Sblueswir1 
2641720751fSRichard Henderson static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
26584778508Sblueswir1 {
266bef6f008SRichard Henderson     return page_check_range((target_ulong)addr, size, type);
26784778508Sblueswir1 }
26884778508Sblueswir1 
269c2bdd9a1SWarner Losh /*
270c2bdd9a1SWarner Losh  * NOTE __get_user and __put_user use host pointers and don't check access.
271c2bdd9a1SWarner Losh  *
272c2bdd9a1SWarner Losh  * These are usually used to access struct data members once the struct has been
273c2bdd9a1SWarner Losh  * locked - usually with lock_user_struct().
27484778508Sblueswir1  */
275*6538c682SWarner Losh #define __put_user_e(x, hptr, e)                                            \
276*6538c682SWarner Losh     do {                                                                    \
277*6538c682SWarner Losh         PRAGMA_DISABLE_PACKED_WARNING;                                      \
278*6538c682SWarner Losh         (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p,                 \
279*6538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p,            \
280*6538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p,            \
281*6538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort))))  \
282*6538c682SWarner Losh             ((hptr), (x)), (void)0);                                        \
283*6538c682SWarner Losh         PRAGMA_REENABLE_PACKED_WARNING;                                     \
284*6538c682SWarner Losh     } while (0)
28584778508Sblueswir1 
286*6538c682SWarner Losh #define __get_user_e(x, hptr, e)                                            \
287*6538c682SWarner Losh     do {                                                                    \
288*6538c682SWarner Losh         PRAGMA_DISABLE_PACKED_WARNING;                                      \
289*6538c682SWarner Losh         ((x) = (typeof(*hptr))(                                             \
290*6538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p,                 \
291*6538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p,           \
292*6538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p,            \
293*6538c682SWarner Losh         __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort))))  \
294*6538c682SWarner Losh             (hptr)), (void)0);                                              \
295*6538c682SWarner Losh         PRAGMA_REENABLE_PACKED_WARNING;                                     \
296*6538c682SWarner Losh     } while (0)
297*6538c682SWarner Losh 
298*6538c682SWarner Losh 
299*6538c682SWarner Losh #if TARGET_BIG_ENDIAN
300*6538c682SWarner Losh # define __put_user(x, hptr)  __put_user_e(x, hptr, be)
301*6538c682SWarner Losh # define __get_user(x, hptr)  __get_user_e(x, hptr, be)
302*6538c682SWarner Losh #else
303*6538c682SWarner Losh # define __put_user(x, hptr)  __put_user_e(x, hptr, le)
304*6538c682SWarner Losh # define __get_user(x, hptr)  __get_user_e(x, hptr, le)
305*6538c682SWarner Losh #endif
30684778508Sblueswir1 
307c2bdd9a1SWarner Losh /*
308c2bdd9a1SWarner Losh  * put_user()/get_user() take a guest address and check access
309c2bdd9a1SWarner Losh  *
310c2bdd9a1SWarner Losh  * These are usually used to access an atomic data type, such as an int, that
311c2bdd9a1SWarner Losh  * has been passed by address.  These internally perform locking and unlocking
312c2bdd9a1SWarner Losh  * on the data type.
31384778508Sblueswir1  */
31484778508Sblueswir1 #define put_user(x, gaddr, target_type)                                 \
31584778508Sblueswir1 ({                                                                      \
31684778508Sblueswir1     abi_ulong __gaddr = (gaddr);                                        \
31784778508Sblueswir1     target_type *__hptr;                                                \
318*6538c682SWarner Losh     abi_long __ret = 0;                                                 \
31933066934SWarner Losh     __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0);  \
32033066934SWarner Losh     if (__hptr) {                                                       \
321*6538c682SWarner Losh         __put_user((x), __hptr);                                        \
32284778508Sblueswir1         unlock_user(__hptr, __gaddr, sizeof(target_type));              \
32384778508Sblueswir1     } else                                                              \
32484778508Sblueswir1         __ret = -TARGET_EFAULT;                                         \
32584778508Sblueswir1     __ret;                                                              \
32684778508Sblueswir1 })
32784778508Sblueswir1 
32884778508Sblueswir1 #define get_user(x, gaddr, target_type)                                 \
32984778508Sblueswir1 ({                                                                      \
33084778508Sblueswir1     abi_ulong __gaddr = (gaddr);                                        \
33184778508Sblueswir1     target_type *__hptr;                                                \
332*6538c682SWarner Losh     abi_long __ret = 0;                                                 \
33333066934SWarner Losh     __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1);   \
33433066934SWarner Losh     if (__hptr) {                                                       \
335*6538c682SWarner Losh         __get_user((x), __hptr);                                        \
33684778508Sblueswir1         unlock_user(__hptr, __gaddr, 0);                                \
33784778508Sblueswir1     } else {                                                            \
33884778508Sblueswir1         (x) = 0;                                                        \
33984778508Sblueswir1         __ret = -TARGET_EFAULT;                                         \
34084778508Sblueswir1     }                                                                   \
34184778508Sblueswir1     __ret;                                                              \
34284778508Sblueswir1 })
34384778508Sblueswir1 
34484778508Sblueswir1 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
34584778508Sblueswir1 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
34684778508Sblueswir1 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
34784778508Sblueswir1 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
34884778508Sblueswir1 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
34984778508Sblueswir1 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
35084778508Sblueswir1 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
35184778508Sblueswir1 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
35284778508Sblueswir1 #define put_user_u8(x, gaddr)  put_user((x), (gaddr), uint8_t)
35384778508Sblueswir1 #define put_user_s8(x, gaddr)  put_user((x), (gaddr), int8_t)
35484778508Sblueswir1 
35584778508Sblueswir1 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
35684778508Sblueswir1 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
35784778508Sblueswir1 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
35884778508Sblueswir1 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
35984778508Sblueswir1 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
36084778508Sblueswir1 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
36184778508Sblueswir1 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
36284778508Sblueswir1 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
36384778508Sblueswir1 #define get_user_u8(x, gaddr)  get_user((x), (gaddr), uint8_t)
36484778508Sblueswir1 #define get_user_s8(x, gaddr)  get_user((x), (gaddr), int8_t)
36584778508Sblueswir1 
366c2bdd9a1SWarner Losh /*
367c2bdd9a1SWarner Losh  * copy_from_user() and copy_to_user() are usually used to copy data
36884778508Sblueswir1  * buffers between the target and host.  These internally perform
36984778508Sblueswir1  * locking/unlocking of the memory.
37084778508Sblueswir1  */
37184778508Sblueswir1 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
37284778508Sblueswir1 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
37384778508Sblueswir1 
374c2bdd9a1SWarner Losh /*
375c2bdd9a1SWarner Losh  * Functions for accessing guest memory.  The tget and tput functions
376c2bdd9a1SWarner Losh  * read/write single values, byteswapping as necessary.  The lock_user function
377c2bdd9a1SWarner Losh  * gets a pointer to a contiguous area of guest memory, but does not perform
378c2bdd9a1SWarner Losh  * any byteswapping.  lock_user may return either a pointer to the guest
379c2bdd9a1SWarner Losh  * memory, or a temporary buffer.
380c2bdd9a1SWarner Losh  */
38184778508Sblueswir1 
382c2bdd9a1SWarner Losh /*
383c2bdd9a1SWarner Losh  * Lock an area of guest memory into the host.  If copy is true then the
384c2bdd9a1SWarner Losh  * host area will have the same contents as the guest.
385c2bdd9a1SWarner Losh  */
386c2bdd9a1SWarner Losh static inline void *lock_user(int type, abi_ulong guest_addr, long len,
387c2bdd9a1SWarner Losh                               int copy)
38884778508Sblueswir1 {
389cb0ea019SWarner Losh     if (!access_ok(type, guest_addr, len)) {
39084778508Sblueswir1         return NULL;
391cb0ea019SWarner Losh     }
39284778508Sblueswir1 #ifdef DEBUG_REMAP
39384778508Sblueswir1     {
39484778508Sblueswir1         void *addr;
395fd9a3048SMd Haris Iqbal         addr = g_malloc(len);
396cb0ea019SWarner Losh         if (copy) {
3973e8f1628SRichard Henderson             memcpy(addr, g2h_untagged(guest_addr), len);
398cb0ea019SWarner Losh         } else {
39984778508Sblueswir1             memset(addr, 0, len);
400cb0ea019SWarner Losh         }
40184778508Sblueswir1         return addr;
40284778508Sblueswir1     }
40384778508Sblueswir1 #else
4043e8f1628SRichard Henderson     return g2h_untagged(guest_addr);
40584778508Sblueswir1 #endif
40684778508Sblueswir1 }
40784778508Sblueswir1 
408c2bdd9a1SWarner Losh /*
409c2bdd9a1SWarner Losh  * Unlock an area of guest memory.  The first LEN bytes must be flushed back to
410c2bdd9a1SWarner Losh  * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
411c2bdd9a1SWarner Losh  */
41284778508Sblueswir1 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
41384778508Sblueswir1                                long len)
41484778508Sblueswir1 {
41584778508Sblueswir1 
41684778508Sblueswir1 #ifdef DEBUG_REMAP
417cb0ea019SWarner Losh     if (!host_ptr) {
41884778508Sblueswir1         return;
419cb0ea019SWarner Losh     }
420cb0ea019SWarner Losh     if (host_ptr == g2h_untagged(guest_addr)) {
42184778508Sblueswir1         return;
422cb0ea019SWarner Losh     }
423cb0ea019SWarner Losh     if (len > 0) {
4243e8f1628SRichard Henderson         memcpy(g2h_untagged(guest_addr), host_ptr, len);
425cb0ea019SWarner Losh     }
426fd9a3048SMd Haris Iqbal     g_free(host_ptr);
42784778508Sblueswir1 #endif
42884778508Sblueswir1 }
42984778508Sblueswir1 
430c2bdd9a1SWarner Losh /*
431c2bdd9a1SWarner Losh  * Return the length of a string in target memory or -TARGET_EFAULT if access
432c2bdd9a1SWarner Losh  * error.
433c2bdd9a1SWarner Losh  */
43484778508Sblueswir1 abi_long target_strlen(abi_ulong gaddr);
43584778508Sblueswir1 
43684778508Sblueswir1 /* Like lock_user but for null terminated strings.  */
43784778508Sblueswir1 static inline void *lock_user_string(abi_ulong guest_addr)
43884778508Sblueswir1 {
43984778508Sblueswir1     abi_long len;
44084778508Sblueswir1     len = target_strlen(guest_addr);
441cb0ea019SWarner Losh     if (len < 0) {
44284778508Sblueswir1         return NULL;
443cb0ea019SWarner Losh     }
44484778508Sblueswir1     return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
44584778508Sblueswir1 }
44684778508Sblueswir1 
44741d1af4dSStefan Weil /* Helper macros for locking/unlocking a target struct.  */
44884778508Sblueswir1 #define lock_user_struct(type, host_ptr, guest_addr, copy)      \
44984778508Sblueswir1     (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
45084778508Sblueswir1 #define unlock_user_struct(host_ptr, guest_addr, copy)          \
45184778508Sblueswir1     unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
45284778508Sblueswir1 
4530ff05082SWarner Losh static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
4540ff05082SWarner Losh {
4550ff05082SWarner Losh #if TARGET_ABI_BITS == 32
456ee3eb3a7SMarc-André Lureau #if TARGET_BIG_ENDIAN
4570ff05082SWarner Losh     return ((uint64_t)word0 << 32) | word1;
4580ff05082SWarner Losh #else
4590ff05082SWarner Losh     return ((uint64_t)word1 << 32) | word0;
4600ff05082SWarner Losh #endif
4610ff05082SWarner Losh #else /* TARGET_ABI_BITS != 32 */
4620ff05082SWarner Losh     return word0;
4630ff05082SWarner Losh #endif /* TARGET_ABI_BITS != 32 */
4640ff05082SWarner Losh }
4650ff05082SWarner Losh 
46684778508Sblueswir1 #include <pthread.h>
46784778508Sblueswir1 
468aae57ac3SWarner Losh #include "user/safe-syscall.h"
469aae57ac3SWarner Losh 
47084778508Sblueswir1 #endif /* QEMU_H */
471