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 */ 17 #ifndef QEMU_H 18 #define QEMU_H 19 20 #include <sys/param.h> 21 22 #include "qemu/int128.h" 23 #include "cpu.h" 24 #include "qemu/units.h" 25 #include "exec/cpu_ldst.h" 26 #include "exec/exec-all.h" 27 28 #include "user/abitypes.h" 29 30 extern char **environ; 31 32 #include "user/thunk.h" 33 #include "target_arch.h" 34 #include "syscall_defs.h" 35 #include "target_syscall.h" 36 #include "target_os_vmparam.h" 37 #include "target_os_signal.h" 38 #include "target.h" 39 #include "exec/gdbstub.h" 40 #include "exec/page-protection.h" 41 #include "qemu/clang-tsa.h" 42 #include "accel/tcg/vcpu-state.h" 43 44 #include "qemu-os.h" 45 /* 46 * TODO: Remove these and rely only on qemu_real_host_page_size(). 47 */ 48 extern uintptr_t qemu_host_page_size; 49 extern intptr_t qemu_host_page_mask; 50 #define HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_host_page_size) 51 52 /* 53 * This struct is used to hold certain information about the image. Basically, 54 * it replicates in user space what would be certain task_struct fields in the 55 * kernel 56 */ 57 struct image_info { 58 abi_ulong load_bias; 59 abi_ulong load_addr; 60 abi_ulong start_code; 61 abi_ulong end_code; 62 abi_ulong start_data; 63 abi_ulong end_data; 64 abi_ulong brk; 65 abi_ulong rss; 66 abi_ulong start_stack; 67 abi_ulong entry; 68 abi_ulong code_offset; 69 abi_ulong data_offset; 70 abi_ulong arg_start; 71 abi_ulong arg_end; 72 uint32_t elf_flags; 73 }; 74 75 struct emulated_sigtable { 76 int pending; /* true if signal is pending */ 77 target_siginfo_t info; 78 }; 79 80 /* 81 * NOTE: we force a big alignment so that the stack stored after is aligned too 82 */ 83 struct TaskState { 84 pid_t ts_tid; /* tid (or pid) of this task */ 85 86 struct TaskState *next; 87 struct bsd_binprm *bprm; 88 struct image_info *info; 89 90 struct emulated_sigtable sync_signal; 91 /* 92 * TODO: Since we block all signals while returning to the main CPU 93 * loop, this needn't be an array 94 */ 95 struct emulated_sigtable sigtab[TARGET_NSIG]; 96 /* 97 * Nonzero if process_pending_signals() needs to do something (either 98 * handle a pending signal or unblock signals). 99 * This flag is written from a signal handler so should be accessed via 100 * the qatomic_read() and qatomic_set() functions. (It is not accessed 101 * from multiple threads.) 102 */ 103 int signal_pending; 104 /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */ 105 bool in_sigsuspend; 106 /* 107 * This thread's signal mask, as requested by the guest program. 108 * The actual signal mask of this thread may differ: 109 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code 110 * + sometimes we block all signals to avoid races 111 */ 112 sigset_t signal_mask; 113 /* 114 * The signal mask imposed by a guest sigsuspend syscall, if we are 115 * currently in the middle of such a syscall 116 */ 117 sigset_t sigsuspend_mask; 118 119 /* This thread's sigaltstack, if it has one */ 120 struct target_sigaltstack sigaltstack_used; 121 } __attribute__((aligned(16))); 122 123 void stop_all_tasks(void); 124 extern const char *interp_prefix; 125 extern const char *qemu_uname_release; 126 127 /* 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 131 * platforms. 132 */ 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) 139 140 /* 141 * This structure is used to hold the arguments that are 142 * used when loading binaries. 143 */ 144 struct bsd_binprm { 145 char buf[128]; 146 void *page[MAX_ARG_PAGES]; 147 abi_ulong p; 148 abi_ulong stringp; 149 int fd; 150 int e_uid, e_gid; 151 int argc, envc; 152 char **argv; 153 char **envp; 154 char *filename; /* (Given) Name of binary */ 155 char *fullpath; /* Full path of binary */ 156 int (*core_dump)(int, CPUArchState *); 157 }; 158 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, 161 abi_ulong stringp); 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); 165 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); 171 172 abi_long memcpy_to_target(abi_ulong dest, const void *src, 173 unsigned long len); 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, 180 abi_long arg8); 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); 194 195 #include "qemu/log.h" 196 197 /* strace.c */ 198 struct 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 208 void 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); 213 void 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); 218 void 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); 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 */ 232 void print_taken_signal(int target_signum, const target_siginfo_t *tinfo); 233 extern int do_strace; 234 235 /* mmap.c */ 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, 242 abi_ulong new_addr); 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); 249 250 /* main.c */ 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; 258 259 /* os-syscall.c */ 260 abi_long get_errno(abi_long ret); 261 bool is_error(abi_long ret); 262 int host_to_target_errno(int err); 263 264 /* os-proc.c */ 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); 269 270 /* os-sys.c */ 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, 275 abi_ulong newlen); 276 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2); 277 278 /* user access */ 279 280 #define VERIFY_READ PAGE_READ 281 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE) 282 283 static inline bool access_ok(int type, abi_ulong addr, abi_ulong size) 284 { 285 return page_check_range((target_ulong)addr, size, type); 286 } 287 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(). 293 */ 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 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 352 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. 359 */ 360 #define put_user(x, gaddr, target_type) \ 361 ({ \ 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); \ 366 if (__hptr) { \ 367 __put_user((x), __hptr); \ 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; \ 378 abi_long __ret = 0; \ 379 __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \ 380 if (__hptr) { \ 381 __get_user((x), __hptr); \ 382 unlock_user(__hptr, __gaddr, 0); \ 383 } else { \ 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 412 /* 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. 416 */ 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); 419 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 */ 427 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 */ 432 static inline void *lock_user(int type, abi_ulong guest_addr, long len, 433 int copy) 434 { 435 if (!access_ok(type, guest_addr, len)) { 436 return NULL; 437 } 438 #ifdef CONFIG_DEBUG_REMAP 439 { 440 void *addr; 441 addr = g_malloc(len); 442 if (copy) { 443 memcpy(addr, g2h_untagged(guest_addr), len); 444 } else { 445 memset(addr, 0, len); 446 } 447 return addr; 448 } 449 #else 450 return g2h_untagged(guest_addr); 451 #endif 452 } 453 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 */ 458 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr, 459 long len) 460 { 461 462 #ifdef CONFIG_DEBUG_REMAP 463 if (!host_ptr) { 464 return; 465 } 466 if (host_ptr == g2h_untagged(guest_addr)) { 467 return; 468 } 469 if (len > 0) { 470 memcpy(g2h_untagged(guest_addr), host_ptr, len); 471 } 472 g_free(host_ptr); 473 #endif 474 } 475 476 /* 477 * Return the length of a string in target memory or -TARGET_EFAULT if access 478 * error. 479 */ 480 abi_long target_strlen(abi_ulong gaddr); 481 482 /* Like lock_user but for null terminated strings. */ 483 static inline void *lock_user_string(abi_ulong guest_addr) 484 { 485 abi_long len; 486 len = target_strlen(guest_addr); 487 if (len < 0) { 488 return NULL; 489 } 490 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1); 491 } 492 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) 498 499 static inline uint64_t target_arg64(uint32_t word0, uint32_t word1) 500 { 501 #if TARGET_ABI_BITS == 32 502 #if TARGET_BIG_ENDIAN 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 512 #include <pthread.h> 513 514 #include "user/safe-syscall.h" 515 516 #endif /* QEMU_H */ 517