]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/qemu.h
user: Move 'abitypes.h' from 'exec/user' to 'user'
[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
ab77bd84
WL
29#include "exec/user/thunk.h"
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"
e022d9ca 37#include "qemu/clang-tsa.h"
84778508 38
9b4a902d 39#include "qemu-os.h"
8c45039f
RH
40/*
41 * TODO: Remove these and rely only on qemu_real_host_page_size().
42 */
43extern uintptr_t qemu_host_page_size;
44extern intptr_t qemu_host_page_mask;
45#define HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_host_page_size)
46
c2bdd9a1
WL
47/*
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
50 * kernel
84778508
BS
51 */
52struct image_info {
0475f8fa 53 abi_ulong load_bias;
84778508
BS
54 abi_ulong load_addr;
55 abi_ulong start_code;
56 abi_ulong end_code;
57 abi_ulong start_data;
58 abi_ulong end_data;
84778508 59 abi_ulong brk;
84778508
BS
60 abi_ulong rss;
61 abi_ulong start_stack;
62 abi_ulong entry;
63 abi_ulong code_offset;
64 abi_ulong data_offset;
0475f8fa
WL
65 abi_ulong arg_start;
66 abi_ulong arg_end;
67 uint32_t elf_flags;
84778508
BS
68};
69
84778508
BS
70struct emulated_sigtable {
71 int pending; /* true if signal is pending */
b46d4ad7 72 target_siginfo_t info;
84778508
BS
73};
74
c2bdd9a1
WL
75/*
76 * NOTE: we force a big alignment so that the stack stored after is aligned too
77 */
84778508 78typedef struct TaskState {
bd88c780
AB
79 pid_t ts_tid; /* tid (or pid) of this task */
80
84778508 81 struct TaskState *next;
031fe7af 82 struct bsd_binprm *bprm;
84778508
BS
83 struct image_info *info;
84
38be620c
WL
85 struct emulated_sigtable sync_signal;
86 /*
87 * TODO: Since we block all signals while returning to the main CPU
88 * loop, this needn't be an array
89 */
84778508 90 struct emulated_sigtable sigtab[TARGET_NSIG];
48047225
WL
91 /*
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.)
97 */
98 int signal_pending;
6c6d4b56
WL
99 /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */
100 bool in_sigsuspend;
149076ad
WL
101 /*
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
106 */
107 sigset_t signal_mask;
6c6d4b56
WL
108 /*
109 * The signal mask imposed by a guest sigsuspend syscall, if we are
110 * currently in the middle of such a syscall
111 */
112 sigset_t sigsuspend_mask;
84778508 113
46f4f76d
WL
114 /* This thread's sigaltstack, if it has one */
115 struct target_sigaltstack sigaltstack_used;
84778508
BS
116} __attribute__((aligned(16))) TaskState;
117
e4e5cb4a
IL
118static inline TaskState *get_task_state(CPUState *cs)
119{
120 return cs->opaque;
121}
122
653ccec2 123void stop_all_tasks(void);
86327290 124extern const char *interp_prefix;
84778508
BS
125extern const char *qemu_uname_release;
126
84778508 127/*
e5e44263
WL
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
944399ff 130 * maximum env+arg in 32-bit environments, bump it up to 512k for !ILP32
e5e44263 131 * platforms.
84778508 132 */
e5e44263
WL
133#if TARGET_ABI_BITS > 32
134#define TARGET_ARG_MAX (512 * KiB)
135#else
136#define TARGET_ARG_MAX (256 * KiB)
137#endif
138#define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
84778508
BS
139
140/*
141 * This structure is used to hold the arguments that are
142 * used when loading binaries.
143 */
afcbcff8 144struct bsd_binprm {
84778508
BS
145 char buf[128];
146 void *page[MAX_ARG_PAGES];
147 abi_ulong p;
98b34d35 148 abi_ulong stringp;
84778508
BS
149 int fd;
150 int e_uid, e_gid;
151 int argc, envc;
152 char **argv;
153 char **envp;
1b50ff64
WL
154 char *filename; /* (Given) Name of binary */
155 char *fullpath; /* Full path of binary */
0475f8fa 156 int (*core_dump)(int, CPUArchState *);
84778508
BS
157};
158
159void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
160abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
ffa03665 161 abi_ulong stringp);
036a013f 162int loader_exec(const char *filename, char **argv, char **envp,
d37853f9
WL
163 struct target_pt_regs *regs, struct image_info *infop,
164 struct bsd_binprm *bprm);
84778508 165
afcbcff8 166int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
036a013f 167 struct image_info *info);
afcbcff8 168int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
036a013f 169 struct image_info *info);
0475f8fa 170int is_target_elf_binary(int fd);
84778508
BS
171
172abi_long memcpy_to_target(abi_ulong dest, const void *src,
173 unsigned long len);
174void target_set_brk(abi_ulong new_brk);
175abi_long do_brk(abi_ulong new_brk);
176void syscall_init(void);
177abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
178 abi_long arg2, abi_long arg3, abi_long arg4,
78cfb07f
JL
179 abi_long arg5, abi_long arg6, abi_long arg7,
180 abi_long arg8);
84778508
BS
181abi_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);
184abi_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);
9edc6313 187void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
d42df502 188extern __thread CPUState *thread_cpu;
9349b4f9 189void cpu_loop(CPUArchState *env);
84778508
BS
190char *target_strerror(int err);
191int get_osversion(void);
192void fork_start(void);
4edc98fc 193void fork_end(pid_t pid);
84778508 194
1de7afc9 195#include "qemu/log.h"
84778508
BS
196
197/* strace.c */
88dae46d
SB
198struct syscallname {
199 int nr;
200 const char *name;
201 const char *format;
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);
206};
207
84778508
BS
208void
209print_freebsd_syscall(int num,
210 abi_long arg1, abi_long arg2, abi_long arg3,
211 abi_long arg4, abi_long arg5, abi_long arg6);
212void print_freebsd_syscall_ret(int num, abi_long ret);
213void
214print_netbsd_syscall(int num,
215 abi_long arg1, abi_long arg2, abi_long arg3,
216 abi_long arg4, abi_long arg5, abi_long arg6);
217void print_netbsd_syscall_ret(int num, abi_long ret);
218void
219print_openbsd_syscall(int num,
220 abi_long arg1, abi_long arg2, abi_long arg3,
221 abi_long arg4, abi_long arg5, abi_long arg6);
222void print_openbsd_syscall_ret(int num, abi_long ret);
fd5bec9a
WL
223/**
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
227 *
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} ---
231 */
232void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
84778508
BS
233extern int do_strace;
234
84778508
BS
235/* mmap.c */
236int target_mprotect(abi_ulong start, abi_ulong len, int prot);
237abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
be04f210 238 int flags, int fd, off_t offset);
84778508
BS
239int target_munmap(abi_ulong start, abi_ulong len);
240abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
241 abi_ulong new_size, unsigned long flags,
242 abi_ulong new_addr);
243int target_msync(abi_ulong start, abi_ulong len, int flags);
be04f210
WL
244extern abi_ulong mmap_next_start;
245abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
4e00b7d8 246void mmap_reserve(abi_ulong start, abi_ulong size);
e022d9ca
EGE
247void TSA_NO_TSA mmap_fork_start(void);
248void TSA_NO_TSA mmap_fork_end(int child);
84778508 249
28e738dc 250/* main.c */
01a298a5 251extern char qemu_proc_pathname[];
312a0b1c
WL
252extern unsigned long target_maxtsiz;
253extern unsigned long target_dfldsiz;
254extern unsigned long target_maxdsiz;
255extern unsigned long target_dflssiz;
256extern unsigned long target_maxssiz;
257extern unsigned long target_sgrowsiz;
28e738dc 258
deeff83b 259/* os-syscall.c */
e5f674f0
WL
260abi_long get_errno(abi_long ret);
261bool is_error(abi_long ret);
deeff83b 262int host_to_target_errno(int err);
e5f674f0 263
cc47390c
SS
264/* os-proc.c */
265abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
266 abi_ulong guest_envp, int do_fexec);
267abi_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);
269
da07e694 270/* os-sys.c */
7adda6de
KE
271abi_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);
6da777e2
KE
273abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
274 int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp,
275 abi_ulong newlen);
da07e694
WL
276abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
277
84778508
BS
278/* user access */
279
1720751f
RH
280#define VERIFY_READ PAGE_READ
281#define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
84778508 282
1720751f 283static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
84778508 284{
bef6f008 285 return page_check_range((target_ulong)addr, size, type);
84778508
BS
286}
287
c2bdd9a1
WL
288/*
289 * NOTE __get_user and __put_user use host pointers and don't check access.
290 *
291 * These are usually used to access struct data members once the struct has been
292 * locked - usually with lock_user_struct().
84778508 293 */
8a45962b
WL
294
295/*
296 * Tricky points:
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
307 */
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\"")
312
313#define PRAGMA_REENABLE_PACKED_WARNING \
314 _Pragma("GCC diagnostic pop")
315
316#else
317#define PRAGMA_DISABLE_PACKED_WARNING
318#define PRAGMA_REENABLE_PACKED_WARNING
319#endif
320
6538c682
WL
321#define __put_user_e(x, hptr, e) \
322 do { \
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; \
330 } while (0)
331
332#define __get_user_e(x, hptr, e) \
333 do { \
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)))) \
340 (hptr)), (void)0); \
341 PRAGMA_REENABLE_PACKED_WARNING; \
342 } while (0)
343
344
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)
348#else
349# define __put_user(x, hptr) __put_user_e(x, hptr, le)
350# define __get_user(x, hptr) __get_user_e(x, hptr, le)
351#endif
84778508 352
c2bdd9a1
WL
353/*
354 * put_user()/get_user() take a guest address and check access
355 *
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
358 * on the data type.
84778508
BS
359 */
360#define put_user(x, gaddr, target_type) \
361({ \
362 abi_ulong __gaddr = (gaddr); \
363 target_type *__hptr; \
6538c682 364 abi_long __ret = 0; \
33066934
WL
365 __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \
366 if (__hptr) { \
6538c682 367 __put_user((x), __hptr); \
84778508
BS
368 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
369 } else \
370 __ret = -TARGET_EFAULT; \
371 __ret; \
372})
373
374#define get_user(x, gaddr, target_type) \
375({ \
376 abi_ulong __gaddr = (gaddr); \
377 target_type *__hptr; \
6538c682 378 abi_long __ret = 0; \
33066934
WL
379 __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \
380 if (__hptr) { \
6538c682 381 __get_user((x), __hptr); \
84778508
BS
382 unlock_user(__hptr, __gaddr, 0); \
383 } else { \
84778508
BS
384 (x) = 0; \
385 __ret = -TARGET_EFAULT; \
386 } \
387 __ret; \
388})
389
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)
400
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)
411
c2bdd9a1
WL
412/*
413 * copy_from_user() and copy_to_user() are usually used to copy data
84778508
BS
414 * buffers between the target and host. These internally perform
415 * locking/unlocking of the memory.
416 */
417abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
418abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
419
c2bdd9a1
WL
420/*
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.
426 */
84778508 427
c2bdd9a1
WL
428/*
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.
431 */
432static inline void *lock_user(int type, abi_ulong guest_addr, long len,
433 int copy)
84778508 434{
cb0ea019 435 if (!access_ok(type, guest_addr, len)) {
84778508 436 return NULL;
cb0ea019 437 }
1f2355f5 438#ifdef CONFIG_DEBUG_REMAP
84778508
BS
439 {
440 void *addr;
fd9a3048 441 addr = g_malloc(len);
cb0ea019 442 if (copy) {
3e8f1628 443 memcpy(addr, g2h_untagged(guest_addr), len);
cb0ea019 444 } else {
84778508 445 memset(addr, 0, len);
cb0ea019 446 }
84778508
BS
447 return addr;
448 }
449#else
3e8f1628 450 return g2h_untagged(guest_addr);
84778508
BS
451#endif
452}
453
c2bdd9a1
WL
454/*
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.
457 */
84778508
BS
458static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
459 long len)
460{
461
1f2355f5 462#ifdef CONFIG_DEBUG_REMAP
cb0ea019 463 if (!host_ptr) {
84778508 464 return;
cb0ea019
WL
465 }
466 if (host_ptr == g2h_untagged(guest_addr)) {
84778508 467 return;
cb0ea019
WL
468 }
469 if (len > 0) {
3e8f1628 470 memcpy(g2h_untagged(guest_addr), host_ptr, len);
cb0ea019 471 }
fd9a3048 472 g_free(host_ptr);
84778508
BS
473#endif
474}
475
c2bdd9a1
WL
476/*
477 * Return the length of a string in target memory or -TARGET_EFAULT if access
478 * error.
479 */
84778508
BS
480abi_long target_strlen(abi_ulong gaddr);
481
482/* Like lock_user but for null terminated strings. */
483static inline void *lock_user_string(abi_ulong guest_addr)
484{
485 abi_long len;
486 len = target_strlen(guest_addr);
cb0ea019 487 if (len < 0) {
84778508 488 return NULL;
cb0ea019 489 }
84778508
BS
490 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
491}
492
41d1af4d 493/* Helper macros for locking/unlocking a target struct. */
84778508
BS
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)
498
0ff05082
WL
499static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
500{
501#if TARGET_ABI_BITS == 32
ee3eb3a7 502#if TARGET_BIG_ENDIAN
0ff05082
WL
503 return ((uint64_t)word0 << 32) | word1;
504#else
505 return ((uint64_t)word1 << 32) | word0;
506#endif
507#else /* TARGET_ABI_BITS != 32 */
508 return word0;
509#endif /* TARGET_ABI_BITS != 32 */
510}
511
84778508 512#include <pthread.h>
84778508 513
aae57ac3
WL
514#include "user/safe-syscall.h"
515
84778508 516#endif /* QEMU_H */