.travis.yml: Shorten the runtime of the problematic jobs
[qemu/kevin.git] / bsd-user / qemu.h
blob1b0a591d2d2fcb91f3bf99dde7bf62157348d45d
1 /*
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/>.
17 #ifndef QEMU_H
18 #define QEMU_H
20 #include "cpu.h"
21 #include "qemu/units.h"
22 #include "exec/cpu_ldst.h"
23 #include "exec/exec-all.h"
25 #undef DEBUG_REMAP
27 #include "exec/user/abitypes.h"
29 extern char **environ;
31 #include "exec/user/thunk.h"
32 #include "target_arch.h"
33 #include "syscall_defs.h"
34 #include "target_syscall.h"
35 #include "target_os_vmparam.h"
36 #include "target_os_signal.h"
37 #include "target.h"
38 #include "exec/gdbstub.h"
39 #include "qemu/clang-tsa.h"
41 #include "qemu-os.h"
43 * TODO: Remove these and rely only on qemu_real_host_page_size().
45 extern uintptr_t qemu_host_page_size;
46 extern intptr_t qemu_host_page_mask;
47 #define HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_host_page_size)
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
54 struct image_info {
55 abi_ulong load_bias;
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;
61 abi_ulong brk;
62 abi_ulong rss;
63 abi_ulong start_stack;
64 abi_ulong entry;
65 abi_ulong code_offset;
66 abi_ulong data_offset;
67 abi_ulong arg_start;
68 abi_ulong arg_end;
69 uint32_t elf_flags;
72 struct emulated_sigtable {
73 int pending; /* true if signal is pending */
74 target_siginfo_t info;
78 * NOTE: we force a big alignment so that the stack stored after is aligned too
80 typedef struct TaskState {
81 pid_t ts_tid; /* tid (or pid) of this task */
83 struct TaskState *next;
84 struct bsd_binprm *bprm;
85 struct image_info *info;
87 struct emulated_sigtable sync_signal;
89 * TODO: Since we block all signals while returning to the main CPU
90 * loop, this needn't be an array
92 struct emulated_sigtable sigtab[TARGET_NSIG];
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.)
100 int signal_pending;
101 /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */
102 bool in_sigsuspend;
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
109 sigset_t signal_mask;
111 * The signal mask imposed by a guest sigsuspend syscall, if we are
112 * currently in the middle of such a syscall
114 sigset_t sigsuspend_mask;
116 /* This thread's sigaltstack, if it has one */
117 struct target_sigaltstack sigaltstack_used;
118 } __attribute__((aligned(16))) TaskState;
120 static inline TaskState *get_task_state(CPUState *cs)
122 return cs->opaque;
125 void stop_all_tasks(void);
126 extern const char *interp_prefix;
127 extern const char *qemu_uname_release;
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
132 * maximum env+arg in 32-bit environments, bump it up to 512k for !ILP32
133 * platforms.
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)
143 * This structure is used to hold the arguments that are
144 * used when loading binaries.
146 struct bsd_binprm {
147 char buf[128];
148 void *page[MAX_ARG_PAGES];
149 abi_ulong p;
150 abi_ulong stringp;
151 int fd;
152 int e_uid, e_gid;
153 int argc, envc;
154 char **argv;
155 char **envp;
156 char *filename; /* (Given) Name of binary */
157 char *fullpath; /* Full path of binary */
158 int (*core_dump)(int, CPUArchState *);
161 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
162 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
163 abi_ulong stringp);
164 int loader_exec(const char *filename, char **argv, char **envp,
165 struct target_pt_regs *regs, struct image_info *infop,
166 struct bsd_binprm *bprm);
168 int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
169 struct image_info *info);
170 int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
171 struct image_info *info);
172 int is_target_elf_binary(int fd);
174 abi_long memcpy_to_target(abi_ulong dest, const void *src,
175 unsigned long len);
176 void target_set_brk(abi_ulong new_brk);
177 abi_long do_brk(abi_ulong new_brk);
178 void syscall_init(void);
179 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
180 abi_long arg2, abi_long arg3, abi_long arg4,
181 abi_long arg5, abi_long arg6, abi_long arg7,
182 abi_long arg8);
183 abi_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);
186 abi_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);
189 void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
190 extern __thread CPUState *thread_cpu;
191 void cpu_loop(CPUArchState *env);
192 char *target_strerror(int err);
193 int get_osversion(void);
194 void fork_start(void);
195 void fork_end(pid_t pid);
197 #include "qemu/log.h"
199 /* strace.c */
200 struct 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);
210 void
211 print_freebsd_syscall(int num,
212 abi_long arg1, abi_long arg2, abi_long arg3,
213 abi_long arg4, abi_long arg5, abi_long arg6);
214 void print_freebsd_syscall_ret(int num, abi_long ret);
215 void
216 print_netbsd_syscall(int num,
217 abi_long arg1, abi_long arg2, abi_long arg3,
218 abi_long arg4, abi_long arg5, abi_long arg6);
219 void print_netbsd_syscall_ret(int num, abi_long ret);
220 void
221 print_openbsd_syscall(int num,
222 abi_long arg1, abi_long arg2, abi_long arg3,
223 abi_long arg4, abi_long arg5, abi_long arg6);
224 void print_openbsd_syscall_ret(int num, abi_long ret);
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
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} ---
234 void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
235 extern int do_strace;
237 /* mmap.c */
238 int target_mprotect(abi_ulong start, abi_ulong len, int prot);
239 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
240 int flags, int fd, off_t offset);
241 int target_munmap(abi_ulong start, abi_ulong len);
242 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
243 abi_ulong new_size, unsigned long flags,
244 abi_ulong new_addr);
245 int target_msync(abi_ulong start, abi_ulong len, int flags);
246 extern abi_ulong mmap_next_start;
247 abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
248 void mmap_reserve(abi_ulong start, abi_ulong size);
249 void TSA_NO_TSA mmap_fork_start(void);
250 void TSA_NO_TSA mmap_fork_end(int child);
252 /* main.c */
253 extern char qemu_proc_pathname[];
254 extern unsigned long target_maxtsiz;
255 extern unsigned long target_dfldsiz;
256 extern unsigned long target_maxdsiz;
257 extern unsigned long target_dflssiz;
258 extern unsigned long target_maxssiz;
259 extern unsigned long target_sgrowsiz;
261 /* os-syscall.c */
262 abi_long get_errno(abi_long ret);
263 bool is_error(abi_long ret);
264 int host_to_target_errno(int err);
266 /* os-proc.c */
267 abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
268 abi_ulong guest_envp, int do_fexec);
269 abi_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);
272 /* os-sys.c */
273 abi_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);
275 abi_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);
278 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
280 /* user access */
282 #define VERIFY_READ PAGE_READ
283 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
285 static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
287 return page_check_range((target_ulong)addr, size, type);
291 * NOTE __get_user and __put_user use host pointers and don't check access.
293 * These are usually used to access struct data members once the struct has been
294 * locked - usually with lock_user_struct().
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
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\"")
315 #define PRAGMA_REENABLE_PACKED_WARNING \
316 _Pragma("GCC diagnostic pop")
318 #else
319 #define PRAGMA_DISABLE_PACKED_WARNING
320 #define PRAGMA_REENABLE_PACKED_WARNING
321 #endif
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)
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)
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
356 * put_user()/get_user() take a guest address and check access
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.
362 #define put_user(x, gaddr, target_type) \
363 ({ \
364 abi_ulong __gaddr = (gaddr); \
365 target_type *__hptr; \
366 abi_long __ret = 0; \
367 __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \
368 if (__hptr) { \
369 __put_user((x), __hptr); \
370 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
371 } else \
372 __ret = -TARGET_EFAULT; \
373 __ret; \
376 #define get_user(x, gaddr, target_type) \
377 ({ \
378 abi_ulong __gaddr = (gaddr); \
379 target_type *__hptr; \
380 abi_long __ret = 0; \
381 __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \
382 if (__hptr) { \
383 __get_user((x), __hptr); \
384 unlock_user(__hptr, __gaddr, 0); \
385 } else { \
386 (x) = 0; \
387 __ret = -TARGET_EFAULT; \
389 __ret; \
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)
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)
415 * copy_from_user() and copy_to_user() are usually used to copy data
416 * buffers between the target and host. These internally perform
417 * locking/unlocking of the memory.
419 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
420 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
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.
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.
434 static inline void *lock_user(int type, abi_ulong guest_addr, long len,
435 int copy)
437 if (!access_ok(type, guest_addr, len)) {
438 return NULL;
440 #ifdef DEBUG_REMAP
442 void *addr;
443 addr = g_malloc(len);
444 if (copy) {
445 memcpy(addr, g2h_untagged(guest_addr), len);
446 } else {
447 memset(addr, 0, len);
449 return addr;
451 #else
452 return g2h_untagged(guest_addr);
453 #endif
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.
460 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
461 long len)
464 #ifdef DEBUG_REMAP
465 if (!host_ptr) {
466 return;
468 if (host_ptr == g2h_untagged(guest_addr)) {
469 return;
471 if (len > 0) {
472 memcpy(g2h_untagged(guest_addr), host_ptr, len);
474 g_free(host_ptr);
475 #endif
479 * Return the length of a string in target memory or -TARGET_EFAULT if access
480 * error.
482 abi_long target_strlen(abi_ulong gaddr);
484 /* Like lock_user but for null terminated strings. */
485 static inline void *lock_user_string(abi_ulong guest_addr)
487 abi_long len;
488 len = target_strlen(guest_addr);
489 if (len < 0) {
490 return NULL;
492 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
495 /* Helper macros for locking/unlocking a target struct. */
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)
501 static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
503 #if TARGET_ABI_BITS == 32
504 #if TARGET_BIG_ENDIAN
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 */
514 #include <pthread.h>
516 #include "user/safe-syscall.h"
518 #endif /* QEMU_H */