2 * qemu bsd user mode definition
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.
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.
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/>.
21 #include "qemu/units.h"
22 #include "exec/cpu_ldst.h"
23 #include "exec/exec-all.h"
25 #include "user/abitypes.h"
27 extern char **environ
;
29 #include "user/thunk.h"
30 #include "target_arch.h"
31 #include "syscall_defs.h"
32 #include "target_syscall.h"
33 #include "target_os_vmparam.h"
34 #include "target_os_signal.h"
36 #include "exec/gdbstub.h"
37 #include "qemu/clang-tsa.h"
41 * TODO: Remove these and rely only on qemu_real_host_page_size().
43 extern uintptr_t qemu_host_page_size
;
44 extern intptr_t qemu_host_page_mask
;
45 #define HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_host_page_size)
48 * This struct is used to hold certain information about the image. Basically,
49 * it replicates in user space what would be certain task_struct fields in the
61 abi_ulong start_stack
;
63 abi_ulong code_offset
;
64 abi_ulong data_offset
;
70 struct emulated_sigtable
{
71 int pending
; /* true if signal is pending */
72 target_siginfo_t info
;
76 * NOTE: we force a big alignment so that the stack stored after is aligned too
78 typedef struct TaskState
{
79 pid_t ts_tid
; /* tid (or pid) of this task */
81 struct TaskState
*next
;
82 struct bsd_binprm
*bprm
;
83 struct image_info
*info
;
85 struct emulated_sigtable sync_signal
;
87 * TODO: Since we block all signals while returning to the main CPU
88 * loop, this needn't be an array
90 struct emulated_sigtable sigtab
[TARGET_NSIG
];
92 * Nonzero if process_pending_signals() needs to do something (either
93 * handle a pending signal or unblock signals).
94 * This flag is written from a signal handler so should be accessed via
95 * the qatomic_read() and qatomic_set() functions. (It is not accessed
96 * from multiple threads.)
99 /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */
102 * This thread's signal mask, as requested by the guest program.
103 * The actual signal mask of this thread may differ:
104 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
105 * + sometimes we block all signals to avoid races
107 sigset_t signal_mask
;
109 * The signal mask imposed by a guest sigsuspend syscall, if we are
110 * currently in the middle of such a syscall
112 sigset_t sigsuspend_mask
;
114 /* This thread's sigaltstack, if it has one */
115 struct target_sigaltstack sigaltstack_used
;
116 } __attribute__((aligned(16))) TaskState
;
118 static inline TaskState
*get_task_state(CPUState
*cs
)
123 void stop_all_tasks(void);
124 extern const char *interp_prefix
;
125 extern const char *qemu_uname_release
;
128 * TARGET_ARG_MAX defines the number of bytes allocated for arguments
129 * and envelope for the new program. 256k should suffice for a reasonable
130 * maximum env+arg in 32-bit environments, bump it up to 512k for !ILP32
133 #if TARGET_ABI_BITS > 32
134 #define TARGET_ARG_MAX (512 * KiB)
136 #define TARGET_ARG_MAX (256 * KiB)
138 #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
141 * This structure is used to hold the arguments that are
142 * used when loading binaries.
146 void *page
[MAX_ARG_PAGES
];
154 char *filename
; /* (Given) Name of binary */
155 char *fullpath
; /* Full path of binary */
156 int (*core_dump
)(int, CPUArchState
*);
159 void do_init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
);
160 abi_ulong
loader_build_argptr(int envc
, int argc
, abi_ulong sp
,
162 int loader_exec(const char *filename
, char **argv
, char **envp
,
163 struct target_pt_regs
*regs
, struct image_info
*infop
,
164 struct bsd_binprm
*bprm
);
166 int load_elf_binary(struct bsd_binprm
*bprm
, struct target_pt_regs
*regs
,
167 struct image_info
*info
);
168 int load_flt_binary(struct bsd_binprm
*bprm
, struct target_pt_regs
*regs
,
169 struct image_info
*info
);
170 int is_target_elf_binary(int fd
);
172 abi_long
memcpy_to_target(abi_ulong dest
, const void *src
,
174 void target_set_brk(abi_ulong new_brk
);
175 abi_long
do_brk(abi_ulong new_brk
);
176 void syscall_init(void);
177 abi_long
do_freebsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
178 abi_long arg2
, abi_long arg3
, abi_long arg4
,
179 abi_long arg5
, abi_long arg6
, abi_long arg7
,
181 abi_long
do_netbsd_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
);
184 abi_long
do_openbsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
185 abi_long arg2
, abi_long arg3
, abi_long arg4
,
186 abi_long arg5
, abi_long arg6
);
187 void gemu_log(const char *fmt
, ...) G_GNUC_PRINTF(1, 2);
188 extern __thread CPUState
*thread_cpu
;
189 void cpu_loop(CPUArchState
*env
);
190 char *target_strerror(int err
);
191 int get_osversion(void);
192 void fork_start(void);
193 void fork_end(pid_t pid
);
195 #include "qemu/log.h"
202 void (*call
)(const struct syscallname
*,
203 abi_long
, abi_long
, abi_long
,
204 abi_long
, abi_long
, abi_long
);
205 void (*result
)(const struct syscallname
*, abi_long
);
209 print_freebsd_syscall(int num
,
210 abi_long arg1
, abi_long arg2
, abi_long arg3
,
211 abi_long arg4
, abi_long arg5
, abi_long arg6
);
212 void print_freebsd_syscall_ret(int num
, abi_long ret
);
214 print_netbsd_syscall(int num
,
215 abi_long arg1
, abi_long arg2
, abi_long arg3
,
216 abi_long arg4
, abi_long arg5
, abi_long arg6
);
217 void print_netbsd_syscall_ret(int num
, abi_long ret
);
219 print_openbsd_syscall(int num
,
220 abi_long arg1
, abi_long arg2
, abi_long arg3
,
221 abi_long arg4
, abi_long arg5
, abi_long arg6
);
222 void print_openbsd_syscall_ret(int num
, abi_long ret
);
224 * print_taken_signal:
225 * @target_signum: target signal being taken
226 * @tinfo: target_siginfo_t which will be passed to the guest for the signal
228 * Print strace output indicating that this signal is being taken by the guest,
229 * in a format similar to:
230 * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
232 void print_taken_signal(int target_signum
, const target_siginfo_t
*tinfo
);
233 extern int do_strace
;
236 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
);
237 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
238 int flags
, int fd
, off_t offset
);
239 int target_munmap(abi_ulong start
, abi_ulong len
);
240 abi_long
target_mremap(abi_ulong old_addr
, abi_ulong old_size
,
241 abi_ulong new_size
, unsigned long flags
,
243 int target_msync(abi_ulong start
, abi_ulong len
, int flags
);
244 extern abi_ulong mmap_next_start
;
245 abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
);
246 void mmap_reserve(abi_ulong start
, abi_ulong size
);
247 void TSA_NO_TSA
mmap_fork_start(void);
248 void TSA_NO_TSA
mmap_fork_end(int child
);
251 extern char qemu_proc_pathname
[];
252 extern unsigned long target_maxtsiz
;
253 extern unsigned long target_dfldsiz
;
254 extern unsigned long target_maxdsiz
;
255 extern unsigned long target_dflssiz
;
256 extern unsigned long target_maxssiz
;
257 extern unsigned long target_sgrowsiz
;
260 abi_long
get_errno(abi_long ret
);
261 bool is_error(abi_long ret
);
262 int host_to_target_errno(int err
);
265 abi_long
freebsd_exec_common(abi_ulong path_or_fd
, abi_ulong guest_argp
,
266 abi_ulong guest_envp
, int do_fexec
);
267 abi_long
do_freebsd_procctl(void *cpu_env
, int idtype
, abi_ulong arg2
,
268 abi_ulong arg3
, abi_ulong arg4
, abi_ulong arg5
, abi_ulong arg6
);
271 abi_long
do_freebsd_sysctl(CPUArchState
*env
, abi_ulong namep
, int32_t namelen
,
272 abi_ulong oldp
, abi_ulong oldlenp
, abi_ulong newp
, abi_ulong newlen
);
273 abi_long
do_freebsd_sysctlbyname(CPUArchState
*env
, abi_ulong namep
,
274 int32_t namelen
, abi_ulong oldp
, abi_ulong oldlenp
, abi_ulong newp
,
276 abi_long
do_freebsd_sysarch(void *cpu_env
, abi_long arg1
, abi_long arg2
);
280 #define VERIFY_READ PAGE_READ
281 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
283 static inline bool access_ok(int type
, abi_ulong addr
, abi_ulong size
)
285 return page_check_range((target_ulong
)addr
, size
, type
);
289 * NOTE __get_user and __put_user use host pointers and don't check access.
291 * These are usually used to access struct data members once the struct has been
292 * locked - usually with lock_user_struct().
297 * - Use __builtin_choose_expr to avoid type promotion from ?:,
298 * - Invalid sizes result in a compile time error stemming from
299 * the fact that abort has no parameters.
300 * - It's easier to use the endian-specific unaligned load/store
301 * functions than host-endian unaligned load/store plus tswapN.
302 * - The pragmas are necessary only to silence a clang false-positive
303 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
304 * - gcc has bugs in its _Pragma() support in some versions, eg
305 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
306 * include the warning-suppression pragmas for clang
308 #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
309 #define PRAGMA_DISABLE_PACKED_WARNING \
310 _Pragma("GCC diagnostic push"); \
311 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
313 #define PRAGMA_REENABLE_PACKED_WARNING \
314 _Pragma("GCC diagnostic pop")
317 #define PRAGMA_DISABLE_PACKED_WARNING
318 #define PRAGMA_REENABLE_PACKED_WARNING
321 #define __put_user_e(x, hptr, e) \
323 PRAGMA_DISABLE_PACKED_WARNING; \
324 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
325 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
326 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
327 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
328 ((hptr), (x)), (void)0); \
329 PRAGMA_REENABLE_PACKED_WARNING; \
332 #define __get_user_e(x, hptr, e) \
334 PRAGMA_DISABLE_PACKED_WARNING; \
335 ((x) = (typeof(*hptr))( \
336 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
337 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
338 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
339 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
341 PRAGMA_REENABLE_PACKED_WARNING; \
345 #if TARGET_BIG_ENDIAN
346 # define __put_user(x, hptr) __put_user_e(x, hptr, be)
347 # define __get_user(x, hptr) __get_user_e(x, hptr, be)
349 # define __put_user(x, hptr) __put_user_e(x, hptr, le)
350 # define __get_user(x, hptr) __get_user_e(x, hptr, le)
354 * put_user()/get_user() take a guest address and check access
356 * These are usually used to access an atomic data type, such as an int, that
357 * has been passed by address. These internally perform locking and unlocking
360 #define put_user(x, gaddr, target_type) \
362 abi_ulong __gaddr = (gaddr); \
363 target_type *__hptr; \
364 abi_long __ret = 0; \
365 __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \
367 __put_user((x), __hptr); \
368 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
370 __ret = -TARGET_EFAULT; \
374 #define get_user(x, gaddr, target_type) \
376 abi_ulong __gaddr = (gaddr); \
377 target_type *__hptr; \
378 abi_long __ret = 0; \
379 __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \
381 __get_user((x), __hptr); \
382 unlock_user(__hptr, __gaddr, 0); \
385 __ret = -TARGET_EFAULT; \
390 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
391 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
392 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
393 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
394 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
395 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
396 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
397 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
398 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
399 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
401 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
402 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
403 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
404 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
405 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
406 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
407 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
408 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
409 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
410 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
413 * copy_from_user() and copy_to_user() are usually used to copy data
414 * buffers between the target and host. These internally perform
415 * locking/unlocking of the memory.
417 abi_long
copy_from_user(void *hptr
, abi_ulong gaddr
, size_t len
);
418 abi_long
copy_to_user(abi_ulong gaddr
, void *hptr
, size_t len
);
421 * Functions for accessing guest memory. The tget and tput functions
422 * read/write single values, byteswapping as necessary. The lock_user function
423 * gets a pointer to a contiguous area of guest memory, but does not perform
424 * any byteswapping. lock_user may return either a pointer to the guest
425 * memory, or a temporary buffer.
429 * Lock an area of guest memory into the host. If copy is true then the
430 * host area will have the same contents as the guest.
432 static inline void *lock_user(int type
, abi_ulong guest_addr
, long len
,
435 if (!access_ok(type
, guest_addr
, len
)) {
438 #ifdef CONFIG_DEBUG_REMAP
441 addr
= g_malloc(len
);
443 memcpy(addr
, g2h_untagged(guest_addr
), len
);
445 memset(addr
, 0, len
);
450 return g2h_untagged(guest_addr
);
455 * Unlock an area of guest memory. The first LEN bytes must be flushed back to
456 * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
458 static inline void unlock_user(void *host_ptr
, abi_ulong guest_addr
,
462 #ifdef CONFIG_DEBUG_REMAP
466 if (host_ptr
== g2h_untagged(guest_addr
)) {
470 memcpy(g2h_untagged(guest_addr
), host_ptr
, len
);
477 * Return the length of a string in target memory or -TARGET_EFAULT if access
480 abi_long
target_strlen(abi_ulong gaddr
);
482 /* Like lock_user but for null terminated strings. */
483 static inline void *lock_user_string(abi_ulong guest_addr
)
486 len
= target_strlen(guest_addr
);
490 return lock_user(VERIFY_READ
, guest_addr
, (long)(len
+ 1), 1);
493 /* Helper macros for locking/unlocking a target struct. */
494 #define lock_user_struct(type, host_ptr, guest_addr, copy) \
495 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
496 #define unlock_user_struct(host_ptr, guest_addr, copy) \
497 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
499 static inline uint64_t target_arg64(uint32_t word0
, uint32_t word1
)
501 #if TARGET_ABI_BITS == 32
502 #if TARGET_BIG_ENDIAN
503 return ((uint64_t)word0
<< 32) | word1
;
505 return ((uint64_t)word1
<< 32) | word0
;
507 #else /* TARGET_ABI_BITS != 32 */
509 #endif /* TARGET_ABI_BITS != 32 */
514 #include "user/safe-syscall.h"