]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/qemu.h
libvhost-user: No need to check for NULL when unmapping
[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
BS
24
25#undef DEBUG_REMAP
84778508 26
022c62cb 27#include "exec/user/abitypes.h"
84778508 28
4b599848
WL
29extern char **environ;
30
ab77bd84
WL
31#include "exec/user/thunk.h"
32#include "target_arch.h"
84778508 33#include "syscall_defs.h"
0c6940d0 34#include "target_syscall.h"
82792244 35#include "target_os_vmparam.h"
790baacc 36#include "target_os_signal.h"
647afdf1 37#include "target.h"
022c62cb 38#include "exec/gdbstub.h"
e022d9ca 39#include "qemu/clang-tsa.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 */
84778508 80typedef struct 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;
84778508
BS
118} __attribute__((aligned(16))) TaskState;
119
e4e5cb4a
IL
120static inline TaskState *get_task_state(CPUState *cs)
121{
122 return cs->opaque;
123}
124
653ccec2 125void stop_all_tasks(void);
86327290 126extern const char *interp_prefix;
84778508
BS
127extern const char *qemu_uname_release;
128
84778508 129/*
e5e44263
WL
130 * TARGET_ARG_MAX defines the number of bytes allocated for arguments
131 * and envelope for the new program. 256k should suffice for a reasonable
944399ff 132 * maximum env+arg in 32-bit environments, bump it up to 512k for !ILP32
e5e44263 133 * platforms.
84778508 134 */
e5e44263
WL
135#if TARGET_ABI_BITS > 32
136#define TARGET_ARG_MAX (512 * KiB)
137#else
138#define TARGET_ARG_MAX (256 * KiB)
139#endif
140#define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
84778508
BS
141
142/*
143 * This structure is used to hold the arguments that are
144 * used when loading binaries.
145 */
afcbcff8 146struct bsd_binprm {
84778508
BS
147 char buf[128];
148 void *page[MAX_ARG_PAGES];
149 abi_ulong p;
98b34d35 150 abi_ulong stringp;
84778508
BS
151 int fd;
152 int e_uid, e_gid;
153 int argc, envc;
154 char **argv;
155 char **envp;
1b50ff64
WL
156 char *filename; /* (Given) Name of binary */
157 char *fullpath; /* Full path of binary */
0475f8fa 158 int (*core_dump)(int, CPUArchState *);
84778508
BS
159};
160
161void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
162abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
ffa03665 163 abi_ulong stringp);
036a013f 164int loader_exec(const char *filename, char **argv, char **envp,
d37853f9
WL
165 struct target_pt_regs *regs, struct image_info *infop,
166 struct bsd_binprm *bprm);
84778508 167
afcbcff8 168int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
036a013f 169 struct image_info *info);
afcbcff8 170int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
036a013f 171 struct image_info *info);
0475f8fa 172int is_target_elf_binary(int fd);
84778508
BS
173
174abi_long memcpy_to_target(abi_ulong dest, const void *src,
175 unsigned long len);
176void target_set_brk(abi_ulong new_brk);
177abi_long do_brk(abi_ulong new_brk);
178void syscall_init(void);
179abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
180 abi_long arg2, abi_long arg3, abi_long arg4,
78cfb07f
JL
181 abi_long arg5, abi_long arg6, abi_long arg7,
182 abi_long arg8);
84778508
BS
183abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
184 abi_long arg2, abi_long arg3, abi_long arg4,
185 abi_long arg5, abi_long arg6);
186abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
187 abi_long arg2, abi_long arg3, abi_long arg4,
188 abi_long arg5, abi_long arg6);
9edc6313 189void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
d42df502 190extern __thread CPUState *thread_cpu;
9349b4f9 191void cpu_loop(CPUArchState *env);
84778508
BS
192char *target_strerror(int err);
193int get_osversion(void);
194void fork_start(void);
4edc98fc 195void fork_end(pid_t pid);
84778508 196
1de7afc9 197#include "qemu/log.h"
84778508
BS
198
199/* strace.c */
88dae46d
SB
200struct syscallname {
201 int nr;
202 const char *name;
203 const char *format;
204 void (*call)(const struct syscallname *,
205 abi_long, abi_long, abi_long,
206 abi_long, abi_long, abi_long);
207 void (*result)(const struct syscallname *, abi_long);
208};
209
84778508
BS
210void
211print_freebsd_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_freebsd_syscall_ret(int num, abi_long ret);
215void
216print_netbsd_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_netbsd_syscall_ret(int num, abi_long ret);
220void
221print_openbsd_syscall(int num,
222 abi_long arg1, abi_long arg2, abi_long arg3,
223 abi_long arg4, abi_long arg5, abi_long arg6);
224void print_openbsd_syscall_ret(int num, abi_long ret);
fd5bec9a
WL
225/**
226 * print_taken_signal:
227 * @target_signum: target signal being taken
228 * @tinfo: target_siginfo_t which will be passed to the guest for the signal
229 *
230 * Print strace output indicating that this signal is being taken by the guest,
231 * in a format similar to:
232 * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
233 */
234void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
84778508
BS
235extern int do_strace;
236
84778508
BS
237/* mmap.c */
238int target_mprotect(abi_ulong start, abi_ulong len, int prot);
239abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
be04f210 240 int flags, int fd, off_t offset);
84778508
BS
241int target_munmap(abi_ulong start, abi_ulong len);
242abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
243 abi_ulong new_size, unsigned long flags,
244 abi_ulong new_addr);
245int target_msync(abi_ulong start, abi_ulong len, int flags);
be04f210
WL
246extern abi_ulong mmap_next_start;
247abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
4e00b7d8 248void mmap_reserve(abi_ulong start, abi_ulong size);
e022d9ca
EGE
249void TSA_NO_TSA mmap_fork_start(void);
250void TSA_NO_TSA mmap_fork_end(int child);
84778508 251
28e738dc 252/* main.c */
01a298a5 253extern char qemu_proc_pathname[];
312a0b1c
WL
254extern unsigned long target_maxtsiz;
255extern unsigned long target_dfldsiz;
256extern unsigned long target_maxdsiz;
257extern unsigned long target_dflssiz;
258extern unsigned long target_maxssiz;
259extern unsigned long target_sgrowsiz;
28e738dc 260
deeff83b 261/* os-syscall.c */
e5f674f0
WL
262abi_long get_errno(abi_long ret);
263bool is_error(abi_long ret);
deeff83b 264int host_to_target_errno(int err);
e5f674f0 265
cc47390c
SS
266/* os-proc.c */
267abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
268 abi_ulong guest_envp, int do_fexec);
269abi_long do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2,
270 abi_ulong arg3, abi_ulong arg4, abi_ulong arg5, abi_ulong arg6);
271
da07e694 272/* os-sys.c */
7adda6de
KE
273abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
274 abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
6da777e2
KE
275abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
276 int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp,
277 abi_ulong newlen);
da07e694
WL
278abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
279
84778508
BS
280/* user access */
281
1720751f
RH
282#define VERIFY_READ PAGE_READ
283#define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
84778508 284
1720751f 285static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
84778508 286{
bef6f008 287 return page_check_range((target_ulong)addr, size, type);
84778508
BS
288}
289
c2bdd9a1
WL
290/*
291 * NOTE __get_user and __put_user use host pointers and don't check access.
292 *
293 * These are usually used to access struct data members once the struct has been
294 * locked - usually with lock_user_struct().
84778508 295 */
8a45962b
WL
296
297/*
298 * Tricky points:
299 * - Use __builtin_choose_expr to avoid type promotion from ?:,
300 * - Invalid sizes result in a compile time error stemming from
301 * the fact that abort has no parameters.
302 * - It's easier to use the endian-specific unaligned load/store
303 * functions than host-endian unaligned load/store plus tswapN.
304 * - The pragmas are necessary only to silence a clang false-positive
305 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
306 * - gcc has bugs in its _Pragma() support in some versions, eg
307 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
308 * include the warning-suppression pragmas for clang
309 */
310#if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
311#define PRAGMA_DISABLE_PACKED_WARNING \
312 _Pragma("GCC diagnostic push"); \
313 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
314
315#define PRAGMA_REENABLE_PACKED_WARNING \
316 _Pragma("GCC diagnostic pop")
317
318#else
319#define PRAGMA_DISABLE_PACKED_WARNING
320#define PRAGMA_REENABLE_PACKED_WARNING
321#endif
322
6538c682
WL
323#define __put_user_e(x, hptr, e) \
324 do { \
325 PRAGMA_DISABLE_PACKED_WARNING; \
326 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
327 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
328 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
329 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
330 ((hptr), (x)), (void)0); \
331 PRAGMA_REENABLE_PACKED_WARNING; \
332 } while (0)
333
334#define __get_user_e(x, hptr, e) \
335 do { \
336 PRAGMA_DISABLE_PACKED_WARNING; \
337 ((x) = (typeof(*hptr))( \
338 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
339 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
340 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
341 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
342 (hptr)), (void)0); \
343 PRAGMA_REENABLE_PACKED_WARNING; \
344 } while (0)
345
346
347#if TARGET_BIG_ENDIAN
348# define __put_user(x, hptr) __put_user_e(x, hptr, be)
349# define __get_user(x, hptr) __get_user_e(x, hptr, be)
350#else
351# define __put_user(x, hptr) __put_user_e(x, hptr, le)
352# define __get_user(x, hptr) __get_user_e(x, hptr, le)
353#endif
84778508 354
c2bdd9a1
WL
355/*
356 * put_user()/get_user() take a guest address and check access
357 *
358 * These are usually used to access an atomic data type, such as an int, that
359 * has been passed by address. These internally perform locking and unlocking
360 * on the data type.
84778508
BS
361 */
362#define put_user(x, gaddr, target_type) \
363({ \
364 abi_ulong __gaddr = (gaddr); \
365 target_type *__hptr; \
6538c682 366 abi_long __ret = 0; \
33066934
WL
367 __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \
368 if (__hptr) { \
6538c682 369 __put_user((x), __hptr); \
84778508
BS
370 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
371 } else \
372 __ret = -TARGET_EFAULT; \
373 __ret; \
374})
375
376#define get_user(x, gaddr, target_type) \
377({ \
378 abi_ulong __gaddr = (gaddr); \
379 target_type *__hptr; \
6538c682 380 abi_long __ret = 0; \
33066934
WL
381 __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \
382 if (__hptr) { \
6538c682 383 __get_user((x), __hptr); \
84778508
BS
384 unlock_user(__hptr, __gaddr, 0); \
385 } else { \
84778508
BS
386 (x) = 0; \
387 __ret = -TARGET_EFAULT; \
388 } \
389 __ret; \
390})
391
392#define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
393#define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
394#define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
395#define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
396#define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
397#define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
398#define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
399#define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
400#define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
401#define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
402
403#define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
404#define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
405#define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
406#define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
407#define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
408#define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
409#define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
410#define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
411#define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
412#define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
413
c2bdd9a1
WL
414/*
415 * copy_from_user() and copy_to_user() are usually used to copy data
84778508
BS
416 * buffers between the target and host. These internally perform
417 * locking/unlocking of the memory.
418 */
419abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
420abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
421
c2bdd9a1
WL
422/*
423 * Functions for accessing guest memory. The tget and tput functions
424 * read/write single values, byteswapping as necessary. The lock_user function
425 * gets a pointer to a contiguous area of guest memory, but does not perform
426 * any byteswapping. lock_user may return either a pointer to the guest
427 * memory, or a temporary buffer.
428 */
84778508 429
c2bdd9a1
WL
430/*
431 * Lock an area of guest memory into the host. If copy is true then the
432 * host area will have the same contents as the guest.
433 */
434static inline void *lock_user(int type, abi_ulong guest_addr, long len,
435 int copy)
84778508 436{
cb0ea019 437 if (!access_ok(type, guest_addr, len)) {
84778508 438 return NULL;
cb0ea019 439 }
84778508
BS
440#ifdef DEBUG_REMAP
441 {
442 void *addr;
fd9a3048 443 addr = g_malloc(len);
cb0ea019 444 if (copy) {
3e8f1628 445 memcpy(addr, g2h_untagged(guest_addr), len);
cb0ea019 446 } else {
84778508 447 memset(addr, 0, len);
cb0ea019 448 }
84778508
BS
449 return addr;
450 }
451#else
3e8f1628 452 return g2h_untagged(guest_addr);
84778508
BS
453#endif
454}
455
c2bdd9a1
WL
456/*
457 * Unlock an area of guest memory. The first LEN bytes must be flushed back to
458 * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
459 */
84778508
BS
460static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
461 long len)
462{
463
464#ifdef DEBUG_REMAP
cb0ea019 465 if (!host_ptr) {
84778508 466 return;
cb0ea019
WL
467 }
468 if (host_ptr == g2h_untagged(guest_addr)) {
84778508 469 return;
cb0ea019
WL
470 }
471 if (len > 0) {
3e8f1628 472 memcpy(g2h_untagged(guest_addr), host_ptr, len);
cb0ea019 473 }
fd9a3048 474 g_free(host_ptr);
84778508
BS
475#endif
476}
477
c2bdd9a1
WL
478/*
479 * Return the length of a string in target memory or -TARGET_EFAULT if access
480 * error.
481 */
84778508
BS
482abi_long target_strlen(abi_ulong gaddr);
483
484/* Like lock_user but for null terminated strings. */
485static inline void *lock_user_string(abi_ulong guest_addr)
486{
487 abi_long len;
488 len = target_strlen(guest_addr);
cb0ea019 489 if (len < 0) {
84778508 490 return NULL;
cb0ea019 491 }
84778508
BS
492 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
493}
494
41d1af4d 495/* Helper macros for locking/unlocking a target struct. */
84778508
BS
496#define lock_user_struct(type, host_ptr, guest_addr, copy) \
497 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
498#define unlock_user_struct(host_ptr, guest_addr, copy) \
499 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
500
0ff05082
WL
501static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
502{
503#if TARGET_ABI_BITS == 32
ee3eb3a7 504#if TARGET_BIG_ENDIAN
0ff05082
WL
505 return ((uint64_t)word0 << 32) | word1;
506#else
507 return ((uint64_t)word1 << 32) | word0;
508#endif
509#else /* TARGET_ABI_BITS != 32 */
510 return word0;
511#endif /* TARGET_ABI_BITS != 32 */
512}
513
84778508 514#include <pthread.h>
84778508 515
aae57ac3
WL
516#include "user/safe-syscall.h"
517
84778508 518#endif /* QEMU_H */