]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/qemu.h
Merge tag 'pull-maintainer-may24-160524-2' of https://gitlab.com/stsquad/qemu into...
[mirror_qemu.git] / bsd-user / qemu.h
CommitLineData
88dae46d
SB
1/*
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 */
84778508
BS
17#ifndef QEMU_H
18#define QEMU_H
19
84778508 20#include "cpu.h"
e5e44263 21#include "qemu/units.h"
f08b6170 22#include "exec/cpu_ldst.h"
ab77bd84 23#include "exec/exec-all.h"
84778508 24
22879b66 25#include "user/abitypes.h"
84778508 26
4b599848
WL
27extern char **environ;
28
4e111653 29#include "user/thunk.h"
ab77bd84 30#include "target_arch.h"
84778508 31#include "syscall_defs.h"
0c6940d0 32#include "target_syscall.h"
82792244 33#include "target_os_vmparam.h"
790baacc 34#include "target_os_signal.h"
647afdf1 35#include "target.h"
022c62cb 36#include "exec/gdbstub.h"
74781c08 37#include "exec/page-protection.h"
e022d9ca 38#include "qemu/clang-tsa.h"
80196013 39#include "accel/tcg/vcpu-state.h"
84778508 40
9b4a902d 41#include "qemu-os.h"
8c45039f
RH
42/*
43 * 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
c2bdd9a1
WL
49/*
50 * 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
84778508
BS
53 */
54struct image_info {
0475f8fa 55 abi_ulong load_bias;
84778508
BS
56 abi_ulong load_addr;
57 abi_ulong start_code;
58 abi_ulong end_code;
59 abi_ulong start_data;
60 abi_ulong end_data;
84778508 61 abi_ulong brk;
84778508
BS
62 abi_ulong rss;
63 abi_ulong start_stack;
64 abi_ulong entry;
65 abi_ulong code_offset;
66 abi_ulong data_offset;
0475f8fa
WL
67 abi_ulong arg_start;
68 abi_ulong arg_end;
69 uint32_t elf_flags;
84778508
BS
70};
71
84778508
BS
72struct emulated_sigtable {
73 int pending; /* true if signal is pending */
b46d4ad7 74 target_siginfo_t info;
84778508
BS
75};
76
c2bdd9a1
WL
77/*
78 * NOTE: we force a big alignment so that the stack stored after is aligned too
79 */
ef932e21 80struct TaskState {
bd88c780
AB
81 pid_t ts_tid; /* tid (or pid) of this task */
82
84778508 83 struct TaskState *next;
031fe7af 84 struct bsd_binprm *bprm;
84778508
BS
85 struct image_info *info;
86
38be620c
WL
87 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 */
84778508 92 struct emulated_sigtable sigtab[TARGET_NSIG];
48047225
WL
93 /*
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;
6c6d4b56
WL
101 /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */
102 bool in_sigsuspend;
149076ad
WL
103 /*
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;
6c6d4b56
WL
110 /*
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;
84778508 115
46f4f76d
WL
116 /* This thread's sigaltstack, if it has one */
117 struct target_sigaltstack sigaltstack_used;
ef932e21 118} __attribute__((aligned(16)));
84778508 119
653ccec2 120void stop_all_tasks(void);
86327290 121extern const char *interp_prefix;
84778508
BS
122extern const char *qemu_uname_release;
123
84778508 124/*
e5e44263
WL
125 * TARGET_ARG_MAX defines the number of bytes allocated for arguments
126 * and envelope for the new program. 256k should suffice for a reasonable
944399ff 127 * maximum env+arg in 32-bit environments, bump it up to 512k for !ILP32
e5e44263 128 * platforms.
84778508 129 */
e5e44263
WL
130#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)
84778508
BS
136
137/*
138 * This structure is used to hold the arguments that are
139 * used when loading binaries.
140 */
afcbcff8 141struct bsd_binprm {
84778508
BS
142 char buf[128];
143 void *page[MAX_ARG_PAGES];
144 abi_ulong p;
98b34d35 145 abi_ulong stringp;
84778508
BS
146 int fd;
147 int e_uid, e_gid;
148 int argc, envc;
149 char **argv;
150 char **envp;
1b50ff64
WL
151 char *filename; /* (Given) Name of binary */
152 char *fullpath; /* Full path of binary */
0475f8fa 153 int (*core_dump)(int, CPUArchState *);
84778508
BS
154};
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,
ffa03665 158 abi_ulong stringp);
036a013f 159int loader_exec(const char *filename, char **argv, char **envp,
d37853f9
WL
160 struct target_pt_regs *regs, struct image_info *infop,
161 struct bsd_binprm *bprm);
84778508 162
afcbcff8 163int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
036a013f 164 struct image_info *info);
afcbcff8 165int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
036a013f 166 struct image_info *info);
0475f8fa 167int is_target_elf_binary(int fd);
84778508
BS
168
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,
78cfb07f
JL
176 abi_long arg5, abi_long arg6, abi_long arg7,
177 abi_long arg8);
84778508
BS
178abi_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);
9edc6313 184void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
d42df502 185extern __thread CPUState *thread_cpu;
9349b4f9 186void cpu_loop(CPUArchState *env);
84778508
BS
187char *target_strerror(int err);
188int get_osversion(void);
189void fork_start(void);
4edc98fc 190void fork_end(pid_t pid);
84778508 191
1de7afc9 192#include "qemu/log.h"
84778508
BS
193
194/* strace.c */
88dae46d
SB
195struct 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
84778508
BS
205void
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);
fd5bec9a
WL
220/**
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);
84778508
BS
230extern int do_strace;
231
84778508
BS
232/* 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,
be04f210 235 int flags, int fd, off_t offset);
84778508
BS
236int 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);
be04f210
WL
241extern abi_ulong mmap_next_start;
242abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
4e00b7d8 243void mmap_reserve(abi_ulong start, abi_ulong size);
e022d9ca
EGE
244void TSA_NO_TSA mmap_fork_start(void);
245void TSA_NO_TSA mmap_fork_end(int child);
84778508 246
28e738dc 247/* main.c */
01a298a5 248extern char qemu_proc_pathname[];
312a0b1c
WL
249extern 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;
28e738dc 255
deeff83b 256/* os-syscall.c */
e5f674f0
WL
257abi_long get_errno(abi_long ret);
258bool is_error(abi_long ret);
deeff83b 259int host_to_target_errno(int err);
e5f674f0 260
cc47390c
SS
261/* 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
da07e694 267/* os-sys.c */
7adda6de
KE
268abi_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);
6da777e2
KE
270abi_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);
da07e694
WL
273abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
274
84778508
BS
275/* user access */
276
1720751f
RH
277#define VERIFY_READ PAGE_READ
278#define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
84778508 279
1720751f 280static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
84778508 281{
bef6f008 282 return page_check_range((target_ulong)addr, size, type);
84778508
BS
283}
284
c2bdd9a1
WL
285/*
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().
84778508 290 */
8a45962b
WL
291
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
6538c682
WL
318#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)
328
329#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
84778508 349
c2bdd9a1
WL
350/*
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.
84778508
BS
356 */
357#define put_user(x, gaddr, target_type) \
358({ \
359 abi_ulong __gaddr = (gaddr); \
360 target_type *__hptr; \
6538c682 361 abi_long __ret = 0; \
33066934
WL
362 __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \
363 if (__hptr) { \
6538c682 364 __put_user((x), __hptr); \
84778508
BS
365 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; \
6538c682 375 abi_long __ret = 0; \
33066934
WL
376 __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \
377 if (__hptr) { \
6538c682 378 __get_user((x), __hptr); \
84778508
BS
379 unlock_user(__hptr, __gaddr, 0); \
380 } else { \
84778508
BS
381 (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
c2bdd9a1
WL
409/*
410 * copy_from_user() and copy_to_user() are usually used to copy data
84778508
BS
411 * 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
c2bdd9a1
WL
417/*
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 */
84778508 424
c2bdd9a1
WL
425/*
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)
84778508 431{
cb0ea019 432 if (!access_ok(type, guest_addr, len)) {
84778508 433 return NULL;
cb0ea019 434 }
1f2355f5 435#ifdef CONFIG_DEBUG_REMAP
84778508
BS
436 {
437 void *addr;
fd9a3048 438 addr = g_malloc(len);
cb0ea019 439 if (copy) {
3e8f1628 440 memcpy(addr, g2h_untagged(guest_addr), len);
cb0ea019 441 } else {
84778508 442 memset(addr, 0, len);
cb0ea019 443 }
84778508
BS
444 return addr;
445 }
446#else
3e8f1628 447 return g2h_untagged(guest_addr);
84778508
BS
448#endif
449}
450
c2bdd9a1
WL
451/*
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 */
84778508
BS
455static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
456 long len)
457{
458
1f2355f5 459#ifdef CONFIG_DEBUG_REMAP
cb0ea019 460 if (!host_ptr) {
84778508 461 return;
cb0ea019
WL
462 }
463 if (host_ptr == g2h_untagged(guest_addr)) {
84778508 464 return;
cb0ea019
WL
465 }
466 if (len > 0) {
3e8f1628 467 memcpy(g2h_untagged(guest_addr), host_ptr, len);
cb0ea019 468 }
fd9a3048 469 g_free(host_ptr);
84778508
BS
470#endif
471}
472
c2bdd9a1
WL
473/*
474 * Return the length of a string in target memory or -TARGET_EFAULT if access
475 * error.
476 */
84778508
BS
477abi_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);
cb0ea019 484 if (len < 0) {
84778508 485 return NULL;
cb0ea019 486 }
84778508
BS
487 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
488}
489
41d1af4d 490/* Helper macros for locking/unlocking a target struct. */
84778508
BS
491#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
0ff05082
WL
496static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
497{
498#if TARGET_ABI_BITS == 32
ee3eb3a7 499#if TARGET_BIG_ENDIAN
0ff05082
WL
500 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
84778508 509#include <pthread.h>
84778508 510
aae57ac3
WL
511#include "user/safe-syscall.h"
512
84778508 513#endif /* QEMU_H */