blob: 9d2fc7148ebfe3ddb46d20863b4008594dd7112a [file] [log] [blame]
Sean Bruno88dae462014-06-08 09:57:23 -07001/*
2 * qemu bsd user mode definition
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
blueswir184778502008-10-26 20:33:16 +000017#ifndef QEMU_H
18#define QEMU_H
19
blueswir184778502008-10-26 20:33:16 +000020#include "cpu.h"
Warner Loshe5e44262021-08-04 00:19:23 -060021#include "qemu/units.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010022#include "exec/cpu_ldst.h"
Warner Loshab77bd82021-08-03 23:27:25 -060023#include "exec/exec-all.h"
blueswir184778502008-10-26 20:33:16 +000024
Philippe Mathieu-Daudé22879b62024-03-22 11:10:04 +010025#include "user/abitypes.h"
blueswir184778502008-10-26 20:33:16 +000026
Warner Losh4b599842021-04-23 10:41:11 -060027extern char **environ;
28
Philippe Mathieu-Daudé4e111652024-03-22 11:08:12 +010029#include "user/thunk.h"
Warner Loshab77bd82021-08-03 23:27:25 -060030#include "target_arch.h"
blueswir184778502008-10-26 20:33:16 +000031#include "syscall_defs.h"
Lluís Vilanova0c6940d2016-02-01 19:38:47 +010032#include "target_syscall.h"
Warner Losh82792242021-08-04 00:39:39 -060033#include "target_os_vmparam.h"
Warner Losh790baac2021-08-05 13:41:52 -060034#include "target_os_signal.h"
Warner Losh647afdf2022-01-31 16:01:05 -070035#include "target.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010036#include "exec/gdbstub.h"
Philippe Mathieu-Daudé74781c02023-12-06 20:27:32 +010037#include "exec/page-protection.h"
Emanuele Giuseppe Espositoe022d9c2023-01-17 08:52:02 -050038#include "qemu/clang-tsa.h"
Philippe Mathieu-Daudé80196012024-04-25 11:11:49 +020039#include "accel/tcg/vcpu-state.h"
blueswir184778502008-10-26 20:33:16 +000040
Stacey Son9b4a9022023-08-13 10:41:25 +020041#include "qemu-os.h"
Warner Loshc2bdd9a2021-04-23 19:50:53 -060042/*
Richard Henderson8c450392024-01-02 12:58:02 +110043 * TODO: Remove these and rely only on qemu_real_host_page_size().
44 */
45extern uintptr_t qemu_host_page_size;
46extern intptr_t qemu_host_page_mask;
47#define HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_host_page_size)
48
49/*
Warner Loshc2bdd9a2021-04-23 19:50:53 -060050 * This struct is used to hold certain information about the image. Basically,
51 * it replicates in user space what would be certain task_struct fields in the
52 * kernel
blueswir184778502008-10-26 20:33:16 +000053 */
54struct image_info {
Warner Losh0475f8f2021-08-06 14:20:16 -060055 abi_ulong load_bias;
blueswir184778502008-10-26 20:33:16 +000056 abi_ulong load_addr;
57 abi_ulong start_code;
58 abi_ulong end_code;
59 abi_ulong start_data;
60 abi_ulong end_data;
blueswir184778502008-10-26 20:33:16 +000061 abi_ulong brk;
blueswir184778502008-10-26 20:33:16 +000062 abi_ulong rss;
63 abi_ulong start_stack;
64 abi_ulong entry;
65 abi_ulong code_offset;
66 abi_ulong data_offset;
Warner Losh0475f8f2021-08-06 14:20:16 -060067 abi_ulong arg_start;
68 abi_ulong arg_end;
69 uint32_t elf_flags;
blueswir184778502008-10-26 20:33:16 +000070};
71
blueswir184778502008-10-26 20:33:16 +000072struct emulated_sigtable {
73 int pending; /* true if signal is pending */
Warner Loshb46d4ad2022-01-16 16:14:18 -070074 target_siginfo_t info;
blueswir184778502008-10-26 20:33:16 +000075};
76
Warner Loshc2bdd9a2021-04-23 19:50:53 -060077/*
78 * NOTE: we force a big alignment so that the stack stored after is aligned too
79 */
Philippe Mathieu-Daudéef932e22024-01-10 12:41:55 +010080struct TaskState {
Alex Bennéebd88c782017-07-12 11:52:15 +010081 pid_t ts_tid; /* tid (or pid) of this task */
82
blueswir184778502008-10-26 20:33:16 +000083 struct TaskState *next;
Warner Losh031fe7a2021-08-03 19:05:40 -060084 struct bsd_binprm *bprm;
blueswir184778502008-10-26 20:33:16 +000085 struct image_info *info;
86
Warner Losh38be6202022-01-08 21:15:11 -070087 struct emulated_sigtable sync_signal;
88 /*
89 * TODO: Since we block all signals while returning to the main CPU
90 * loop, this needn't be an array
91 */
blueswir184778502008-10-26 20:33:16 +000092 struct emulated_sigtable sigtab[TARGET_NSIG];
Warner Losh48047222022-01-16 16:33:21 -070093 /*
94 * Nonzero if process_pending_signals() needs to do something (either
95 * handle a pending signal or unblock signals).
96 * This flag is written from a signal handler so should be accessed via
97 * the qatomic_read() and qatomic_set() functions. (It is not accessed
98 * from multiple threads.)
99 */
100 int signal_pending;
Warner Losh6c6d4b52022-01-08 21:46:07 -0700101 /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */
102 bool in_sigsuspend;
Warner Losh149076a2022-01-08 16:57:31 -0700103 /*
104 * This thread's signal mask, as requested by the guest program.
105 * The actual signal mask of this thread may differ:
106 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
107 * + sometimes we block all signals to avoid races
108 */
109 sigset_t signal_mask;
Warner Losh6c6d4b52022-01-08 21:46:07 -0700110 /*
111 * The signal mask imposed by a guest sigsuspend syscall, if we are
112 * currently in the middle of such a syscall
113 */
114 sigset_t sigsuspend_mask;
blueswir184778502008-10-26 20:33:16 +0000115
Warner Losh46f4f762022-01-08 21:40:28 -0700116 /* This thread's sigaltstack, if it has one */
117 struct target_sigaltstack sigaltstack_used;
Philippe Mathieu-Daudéef932e22024-01-10 12:41:55 +0100118} __attribute__((aligned(16)));
blueswir184778502008-10-26 20:33:16 +0000119
Warner Losh653ccec2021-09-19 01:11:43 -0600120void stop_all_tasks(void);
Stacey Son86327292023-09-25 21:24:17 +0300121extern const char *interp_prefix;
blueswir184778502008-10-26 20:33:16 +0000122extern const char *qemu_uname_release;
123
blueswir184778502008-10-26 20:33:16 +0000124/*
Warner Loshe5e44262021-08-04 00:19:23 -0600125 * TARGET_ARG_MAX defines the number of bytes allocated for arguments
126 * and envelope for the new program. 256k should suffice for a reasonable
Michael Tokarev944399f2023-07-14 14:06:43 +0300127 * maximum env+arg in 32-bit environments, bump it up to 512k for !ILP32
Warner Loshe5e44262021-08-04 00:19:23 -0600128 * platforms.
blueswir184778502008-10-26 20:33:16 +0000129 */
Warner Loshe5e44262021-08-04 00:19:23 -0600130#if TARGET_ABI_BITS > 32
131#define TARGET_ARG_MAX (512 * KiB)
132#else
133#define TARGET_ARG_MAX (256 * KiB)
134#endif
135#define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
blueswir184778502008-10-26 20:33:16 +0000136
137/*
138 * This structure is used to hold the arguments that are
139 * used when loading binaries.
140 */
Warner Loshafcbcff2021-04-29 10:04:28 -0600141struct bsd_binprm {
blueswir184778502008-10-26 20:33:16 +0000142 char buf[128];
143 void *page[MAX_ARG_PAGES];
144 abi_ulong p;
Warner Losh98b34d32021-08-04 17:13:24 -0600145 abi_ulong stringp;
blueswir184778502008-10-26 20:33:16 +0000146 int fd;
147 int e_uid, e_gid;
148 int argc, envc;
149 char **argv;
150 char **envp;
Warner Losh1b50ff62021-04-29 19:34:34 -0600151 char *filename; /* (Given) Name of binary */
152 char *fullpath; /* Full path of binary */
Warner Losh0475f8f2021-08-06 14:20:16 -0600153 int (*core_dump)(int, CPUArchState *);
blueswir184778502008-10-26 20:33:16 +0000154};
155
156void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
157abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
Warner Loshffa03662021-04-30 08:17:23 -0600158 abi_ulong stringp);
Warner Losh036a0132021-04-23 19:45:20 -0600159int loader_exec(const char *filename, char **argv, char **envp,
Warner Loshd37853f2021-04-29 18:45:13 -0600160 struct target_pt_regs *regs, struct image_info *infop,
161 struct bsd_binprm *bprm);
blueswir184778502008-10-26 20:33:16 +0000162
Warner Loshafcbcff2021-04-29 10:04:28 -0600163int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
Warner Losh036a0132021-04-23 19:45:20 -0600164 struct image_info *info);
Warner Loshafcbcff2021-04-29 10:04:28 -0600165int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
Warner Losh036a0132021-04-23 19:45:20 -0600166 struct image_info *info);
Warner Losh0475f8f2021-08-06 14:20:16 -0600167int is_target_elf_binary(int fd);
blueswir184778502008-10-26 20:33:16 +0000168
169abi_long memcpy_to_target(abi_ulong dest, const void *src,
170 unsigned long len);
171void target_set_brk(abi_ulong new_brk);
172abi_long do_brk(abi_ulong new_brk);
173void syscall_init(void);
174abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
175 abi_long arg2, abi_long arg3, abi_long arg4,
Juergen Lock78cfb072009-10-17 00:34:26 +0200176 abi_long arg5, abi_long arg6, abi_long arg7,
177 abi_long arg8);
blueswir184778502008-10-26 20:33:16 +0000178abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
179 abi_long arg2, abi_long arg3, abi_long arg4,
180 abi_long arg5, abi_long arg6);
181abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
182 abi_long arg2, abi_long arg3, abi_long arg4,
183 abi_long arg5, abi_long arg6);
Marc-André Lureau9edc6312022-02-20 20:39:25 +0400184void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
Warner Loshd42df502021-08-03 12:34:52 -0600185extern __thread CPUState *thread_cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100186void cpu_loop(CPUArchState *env);
blueswir184778502008-10-26 20:33:16 +0000187char *target_strerror(int err);
188int get_osversion(void);
189void fork_start(void);
Ilya Leoshkevich4edc98f2024-03-05 12:09:42 +0000190void fork_end(pid_t pid);
blueswir184778502008-10-26 20:33:16 +0000191
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100192#include "qemu/log.h"
blueswir184778502008-10-26 20:33:16 +0000193
194/* strace.c */
Sean Bruno88dae462014-06-08 09:57:23 -0700195struct syscallname {
196 int nr;
197 const char *name;
198 const char *format;
199 void (*call)(const struct syscallname *,
200 abi_long, abi_long, abi_long,
201 abi_long, abi_long, abi_long);
202 void (*result)(const struct syscallname *, abi_long);
203};
204
blueswir184778502008-10-26 20:33:16 +0000205void
206print_freebsd_syscall(int num,
207 abi_long arg1, abi_long arg2, abi_long arg3,
208 abi_long arg4, abi_long arg5, abi_long arg6);
209void print_freebsd_syscall_ret(int num, abi_long ret);
210void
211print_netbsd_syscall(int num,
212 abi_long arg1, abi_long arg2, abi_long arg3,
213 abi_long arg4, abi_long arg5, abi_long arg6);
214void print_netbsd_syscall_ret(int num, abi_long ret);
215void
216print_openbsd_syscall(int num,
217 abi_long arg1, abi_long arg2, abi_long arg3,
218 abi_long arg4, abi_long arg5, abi_long arg6);
219void print_openbsd_syscall_ret(int num, abi_long ret);
Warner Loshfd5bec92022-01-08 20:04:18 -0700220/**
221 * print_taken_signal:
222 * @target_signum: target signal being taken
223 * @tinfo: target_siginfo_t which will be passed to the guest for the signal
224 *
225 * Print strace output indicating that this signal is being taken by the guest,
226 * in a format similar to:
227 * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
228 */
229void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
blueswir184778502008-10-26 20:33:16 +0000230extern int do_strace;
231
blueswir184778502008-10-26 20:33:16 +0000232/* mmap.c */
233int target_mprotect(abi_ulong start, abi_ulong len, int prot);
234abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
Warner Loshbe04f212021-08-05 18:15:47 -0600235 int flags, int fd, off_t offset);
blueswir184778502008-10-26 20:33:16 +0000236int target_munmap(abi_ulong start, abi_ulong len);
237abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
238 abi_ulong new_size, unsigned long flags,
239 abi_ulong new_addr);
240int target_msync(abi_ulong start, abi_ulong len, int flags);
Warner Loshbe04f212021-08-05 18:15:47 -0600241extern abi_ulong mmap_next_start;
242abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
Stacey Son4e00b7d2023-09-25 21:27:08 +0300243void mmap_reserve(abi_ulong start, abi_ulong size);
Emanuele Giuseppe Espositoe022d9c2023-01-17 08:52:02 -0500244void TSA_NO_TSA mmap_fork_start(void);
245void TSA_NO_TSA mmap_fork_end(int child);
blueswir184778502008-10-26 20:33:16 +0000246
Blue Swirl28e738d2009-08-01 10:29:42 +0000247/* main.c */
Warner Losh01a298a2021-08-03 13:39:31 -0600248extern char qemu_proc_pathname[];
Warner Losh312a0b12021-08-06 18:48:37 -0600249extern unsigned long target_maxtsiz;
250extern unsigned long target_dfldsiz;
251extern unsigned long target_maxdsiz;
252extern unsigned long target_dflssiz;
253extern unsigned long target_maxssiz;
254extern unsigned long target_sgrowsiz;
Blue Swirl28e738d2009-08-01 10:29:42 +0000255
Warner Loshdeeff832022-01-31 13:19:44 -0700256/* os-syscall.c */
Warner Loshe5f674f2021-09-18 00:26:49 -0600257abi_long get_errno(abi_long ret);
258bool is_error(abi_long ret);
Warner Loshdeeff832022-01-31 13:19:44 -0700259int host_to_target_errno(int err);
Warner Loshe5f674f2021-09-18 00:26:49 -0600260
Stacey Soncc473902023-09-25 21:24:01 +0300261/* os-proc.c */
262abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
263 abi_ulong guest_envp, int do_fexec);
264abi_long do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2,
265 abi_ulong arg3, abi_ulong arg4, abi_ulong arg5, abi_ulong arg6);
266
Warner Loshda07e692021-09-20 13:41:38 -0600267/* os-sys.c */
Kyle Evans7adda6d2023-02-10 10:02:45 -0700268abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
269 abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
Kyle Evans6da777e2023-02-10 11:16:25 -0700270abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
271 int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp,
272 abi_ulong newlen);
Warner Loshda07e692021-09-20 13:41:38 -0600273abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
274
blueswir184778502008-10-26 20:33:16 +0000275/* user access */
276
Richard Henderson17207512021-02-12 10:48:39 -0800277#define VERIFY_READ PAGE_READ
278#define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
blueswir184778502008-10-26 20:33:16 +0000279
Richard Henderson17207512021-02-12 10:48:39 -0800280static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
blueswir184778502008-10-26 20:33:16 +0000281{
Richard Hendersonbef6f002023-07-07 21:40:52 +0100282 return page_check_range((target_ulong)addr, size, type);
blueswir184778502008-10-26 20:33:16 +0000283}
284
Warner Loshc2bdd9a2021-04-23 19:50:53 -0600285/*
286 * NOTE __get_user and __put_user use host pointers and don't check access.
287 *
288 * These are usually used to access struct data members once the struct has been
289 * locked - usually with lock_user_struct().
blueswir184778502008-10-26 20:33:16 +0000290 */
Warner Losh8a459622023-08-30 08:44:01 -0600291
292/*
293 * Tricky points:
294 * - Use __builtin_choose_expr to avoid type promotion from ?:,
295 * - Invalid sizes result in a compile time error stemming from
296 * the fact that abort has no parameters.
297 * - It's easier to use the endian-specific unaligned load/store
298 * functions than host-endian unaligned load/store plus tswapN.
299 * - The pragmas are necessary only to silence a clang false-positive
300 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
301 * - gcc has bugs in its _Pragma() support in some versions, eg
302 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
303 * include the warning-suppression pragmas for clang
304 */
305#if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
306#define PRAGMA_DISABLE_PACKED_WARNING \
307 _Pragma("GCC diagnostic push"); \
308 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
309
310#define PRAGMA_REENABLE_PACKED_WARNING \
311 _Pragma("GCC diagnostic pop")
312
313#else
314#define PRAGMA_DISABLE_PACKED_WARNING
315#define PRAGMA_REENABLE_PACKED_WARNING
316#endif
317
Warner Losh6538c682023-08-13 10:41:24 +0200318#define __put_user_e(x, hptr, e) \
319 do { \
320 PRAGMA_DISABLE_PACKED_WARNING; \
321 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
322 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
323 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
324 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
325 ((hptr), (x)), (void)0); \
326 PRAGMA_REENABLE_PACKED_WARNING; \
327 } while (0)
blueswir184778502008-10-26 20:33:16 +0000328
Warner Losh6538c682023-08-13 10:41:24 +0200329#define __get_user_e(x, hptr, e) \
330 do { \
331 PRAGMA_DISABLE_PACKED_WARNING; \
332 ((x) = (typeof(*hptr))( \
333 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
334 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
335 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
336 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
337 (hptr)), (void)0); \
338 PRAGMA_REENABLE_PACKED_WARNING; \
339 } while (0)
340
341
342#if TARGET_BIG_ENDIAN
343# define __put_user(x, hptr) __put_user_e(x, hptr, be)
344# define __get_user(x, hptr) __get_user_e(x, hptr, be)
345#else
346# define __put_user(x, hptr) __put_user_e(x, hptr, le)
347# define __get_user(x, hptr) __get_user_e(x, hptr, le)
348#endif
blueswir184778502008-10-26 20:33:16 +0000349
Warner Loshc2bdd9a2021-04-23 19:50:53 -0600350/*
351 * put_user()/get_user() take a guest address and check access
352 *
353 * These are usually used to access an atomic data type, such as an int, that
354 * has been passed by address. These internally perform locking and unlocking
355 * on the data type.
blueswir184778502008-10-26 20:33:16 +0000356 */
357#define put_user(x, gaddr, target_type) \
358({ \
359 abi_ulong __gaddr = (gaddr); \
360 target_type *__hptr; \
Warner Losh6538c682023-08-13 10:41:24 +0200361 abi_long __ret = 0; \
Warner Losh33066932021-04-23 19:53:36 -0600362 __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \
363 if (__hptr) { \
Warner Losh6538c682023-08-13 10:41:24 +0200364 __put_user((x), __hptr); \
blueswir184778502008-10-26 20:33:16 +0000365 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
366 } else \
367 __ret = -TARGET_EFAULT; \
368 __ret; \
369})
370
371#define get_user(x, gaddr, target_type) \
372({ \
373 abi_ulong __gaddr = (gaddr); \
374 target_type *__hptr; \
Warner Losh6538c682023-08-13 10:41:24 +0200375 abi_long __ret = 0; \
Warner Losh33066932021-04-23 19:53:36 -0600376 __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \
377 if (__hptr) { \
Warner Losh6538c682023-08-13 10:41:24 +0200378 __get_user((x), __hptr); \
blueswir184778502008-10-26 20:33:16 +0000379 unlock_user(__hptr, __gaddr, 0); \
380 } else { \
blueswir184778502008-10-26 20:33:16 +0000381 (x) = 0; \
382 __ret = -TARGET_EFAULT; \
383 } \
384 __ret; \
385})
386
387#define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
388#define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
389#define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
390#define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
391#define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
392#define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
393#define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
394#define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
395#define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
396#define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
397
398#define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
399#define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
400#define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
401#define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
402#define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
403#define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
404#define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
405#define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
406#define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
407#define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
408
Warner Loshc2bdd9a2021-04-23 19:50:53 -0600409/*
410 * copy_from_user() and copy_to_user() are usually used to copy data
blueswir184778502008-10-26 20:33:16 +0000411 * buffers between the target and host. These internally perform
412 * locking/unlocking of the memory.
413 */
414abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
415abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
416
Warner Loshc2bdd9a2021-04-23 19:50:53 -0600417/*
418 * Functions for accessing guest memory. The tget and tput functions
419 * read/write single values, byteswapping as necessary. The lock_user function
420 * gets a pointer to a contiguous area of guest memory, but does not perform
421 * any byteswapping. lock_user may return either a pointer to the guest
422 * memory, or a temporary buffer.
423 */
blueswir184778502008-10-26 20:33:16 +0000424
Warner Loshc2bdd9a2021-04-23 19:50:53 -0600425/*
426 * Lock an area of guest memory into the host. If copy is true then the
427 * host area will have the same contents as the guest.
428 */
429static inline void *lock_user(int type, abi_ulong guest_addr, long len,
430 int copy)
blueswir184778502008-10-26 20:33:16 +0000431{
Warner Loshcb0ea012021-04-23 19:55:03 -0600432 if (!access_ok(type, guest_addr, len)) {
blueswir184778502008-10-26 20:33:16 +0000433 return NULL;
Warner Loshcb0ea012021-04-23 19:55:03 -0600434 }
Ilya Leoshkevich1f2355f2024-03-12 01:23:30 +0100435#ifdef CONFIG_DEBUG_REMAP
blueswir184778502008-10-26 20:33:16 +0000436 {
437 void *addr;
Md Haris Iqbalfd9a3042016-04-05 18:39:03 +0530438 addr = g_malloc(len);
Warner Loshcb0ea012021-04-23 19:55:03 -0600439 if (copy) {
Richard Henderson3e8f1622021-02-12 10:48:43 -0800440 memcpy(addr, g2h_untagged(guest_addr), len);
Warner Loshcb0ea012021-04-23 19:55:03 -0600441 } else {
blueswir184778502008-10-26 20:33:16 +0000442 memset(addr, 0, len);
Warner Loshcb0ea012021-04-23 19:55:03 -0600443 }
blueswir184778502008-10-26 20:33:16 +0000444 return addr;
445 }
446#else
Richard Henderson3e8f1622021-02-12 10:48:43 -0800447 return g2h_untagged(guest_addr);
blueswir184778502008-10-26 20:33:16 +0000448#endif
449}
450
Warner Loshc2bdd9a2021-04-23 19:50:53 -0600451/*
452 * Unlock an area of guest memory. The first LEN bytes must be flushed back to
453 * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
454 */
blueswir184778502008-10-26 20:33:16 +0000455static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
456 long len)
457{
458
Ilya Leoshkevich1f2355f2024-03-12 01:23:30 +0100459#ifdef CONFIG_DEBUG_REMAP
Warner Loshcb0ea012021-04-23 19:55:03 -0600460 if (!host_ptr) {
blueswir184778502008-10-26 20:33:16 +0000461 return;
Warner Loshcb0ea012021-04-23 19:55:03 -0600462 }
463 if (host_ptr == g2h_untagged(guest_addr)) {
blueswir184778502008-10-26 20:33:16 +0000464 return;
Warner Loshcb0ea012021-04-23 19:55:03 -0600465 }
466 if (len > 0) {
Richard Henderson3e8f1622021-02-12 10:48:43 -0800467 memcpy(g2h_untagged(guest_addr), host_ptr, len);
Warner Loshcb0ea012021-04-23 19:55:03 -0600468 }
Md Haris Iqbalfd9a3042016-04-05 18:39:03 +0530469 g_free(host_ptr);
blueswir184778502008-10-26 20:33:16 +0000470#endif
471}
472
Warner Loshc2bdd9a2021-04-23 19:50:53 -0600473/*
474 * Return the length of a string in target memory or -TARGET_EFAULT if access
475 * error.
476 */
blueswir184778502008-10-26 20:33:16 +0000477abi_long target_strlen(abi_ulong gaddr);
478
479/* Like lock_user but for null terminated strings. */
480static inline void *lock_user_string(abi_ulong guest_addr)
481{
482 abi_long len;
483 len = target_strlen(guest_addr);
Warner Loshcb0ea012021-04-23 19:55:03 -0600484 if (len < 0) {
blueswir184778502008-10-26 20:33:16 +0000485 return NULL;
Warner Loshcb0ea012021-04-23 19:55:03 -0600486 }
blueswir184778502008-10-26 20:33:16 +0000487 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
488}
489
Stefan Weil41d1af42013-09-12 19:57:41 +0200490/* Helper macros for locking/unlocking a target struct. */
blueswir184778502008-10-26 20:33:16 +0000491#define lock_user_struct(type, host_ptr, guest_addr, copy) \
492 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
493#define unlock_user_struct(host_ptr, guest_addr, copy) \
494 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
495
Warner Losh0ff05082022-01-31 16:06:46 -0700496static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
497{
498#if TARGET_ABI_BITS == 32
Marc-André Lureauee3eb3a2022-03-23 19:57:18 +0400499#if TARGET_BIG_ENDIAN
Warner Losh0ff05082022-01-31 16:06:46 -0700500 return ((uint64_t)word0 << 32) | word1;
501#else
502 return ((uint64_t)word1 << 32) | word0;
503#endif
504#else /* TARGET_ABI_BITS != 32 */
505 return word0;
506#endif /* TARGET_ABI_BITS != 32 */
507}
508
blueswir184778502008-10-26 20:33:16 +0000509#include <pthread.h>
blueswir184778502008-10-26 20:33:16 +0000510
Warner Loshaae57ac2022-01-08 17:55:56 -0700511#include "user/safe-syscall.h"
512
blueswir184778502008-10-26 20:33:16 +0000513#endif /* QEMU_H */