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 "cpu.h" 21 #include "qemu/units.h" 22 #include "exec/cpu_ldst.h" 23 #include "exec/exec-all.h" 24 25 #include "user/abitypes.h" 26 27 extern char **environ; 28 29 #include "user/thunk.h" 30 #include "target_arch.h" 31 #include "syscall_defs.h" 32 #include "target_syscall.h" 33 #include "target_os_vmparam.h" 34 #include "target_os_signal.h" 35 #include "target.h" 36 #include "exec/gdbstub.h" 37 #include "exec/page-protection.h" 38 #include "qemu/clang-tsa.h" 39 40 #include "qemu-os.h" 41 /* 42 * TODO: Remove these and rely only on qemu_real_host_page_size(). 43 */ 44 extern uintptr_t qemu_host_page_size; 45 extern intptr_t qemu_host_page_mask; 46 #define HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_host_page_size) 47 48 /* 49 * This struct is used to hold certain information about the image. Basically, 50 * it replicates in user space what would be certain task_struct fields in the 51 * kernel 52 */ 53 struct image_info { 54 abi_ulong load_bias; 55 abi_ulong load_addr; 56 abi_ulong start_code; 57 abi_ulong end_code; 58 abi_ulong start_data; 59 abi_ulong end_data; 60 abi_ulong brk; 61 abi_ulong rss; 62 abi_ulong start_stack; 63 abi_ulong entry; 64 abi_ulong code_offset; 65 abi_ulong data_offset; 66 abi_ulong arg_start; 67 abi_ulong arg_end; 68 uint32_t elf_flags; 69 }; 70 71 struct emulated_sigtable { 72 int pending; /* true if signal is pending */ 73 target_siginfo_t info; 74 }; 75 76 /* 77 * NOTE: we force a big alignment so that the stack stored after is aligned too 78 */ 79 struct TaskState { 80 pid_t ts_tid; /* tid (or pid) of this task */ 81 82 struct TaskState *next; 83 struct bsd_binprm *bprm; 84 struct image_info *info; 85 86 struct emulated_sigtable sync_signal; 87 /* 88 * TODO: Since we block all signals while returning to the main CPU 89 * loop, this needn't be an array 90 */ 91 struct emulated_sigtable sigtab[TARGET_NSIG]; 92 /* 93 * Nonzero if process_pending_signals() needs to do something (either 94 * handle a pending signal or unblock signals). 95 * This flag is written from a signal handler so should be accessed via 96 * the qatomic_read() and qatomic_set() functions. (It is not accessed 97 * from multiple threads.) 98 */ 99 int signal_pending; 100 /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */ 101 bool in_sigsuspend; 102 /* 103 * This thread's signal mask, as requested by the guest program. 104 * The actual signal mask of this thread may differ: 105 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code 106 * + sometimes we block all signals to avoid races 107 */ 108 sigset_t signal_mask; 109 /* 110 * The signal mask imposed by a guest sigsuspend syscall, if we are 111 * currently in the middle of such a syscall 112 */ 113 sigset_t sigsuspend_mask; 114 115 /* This thread's sigaltstack, if it has one */ 116 struct target_sigaltstack sigaltstack_used; 117 } __attribute__((aligned(16))); 118 119 static inline TaskState *get_task_state(CPUState *cs) 120 { 121 return cs->opaque; 122 } 123 124 void stop_all_tasks(void); 125 extern const char *interp_prefix; 126 extern const char *qemu_uname_release; 127 128 /* 129 * TARGET_ARG_MAX defines the number of bytes allocated for arguments 130 * and envelope for the new program. 256k should suffice for a reasonable 131 * maximum env+arg in 32-bit environments, bump it up to 512k for !ILP32 132 * platforms. 133 */ 134 #if TARGET_ABI_BITS > 32 135 #define TARGET_ARG_MAX (512 * KiB) 136 #else 137 #define TARGET_ARG_MAX (256 * KiB) 138 #endif 139 #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE) 140 141 /* 142 * This structure is used to hold the arguments that are 143 * used when loading binaries. 144 */ 145 struct bsd_binprm { 146 char buf[128]; 147 void *page[MAX_ARG_PAGES]; 148 abi_ulong p; 149 abi_ulong stringp; 150 int fd; 151 int e_uid, e_gid; 152 int argc, envc; 153 char **argv; 154 char **envp; 155 char *filename; /* (Given) Name of binary */ 156 char *fullpath; /* Full path of binary */ 157 int (*core_dump)(int, CPUArchState *); 158 }; 159 160 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop); 161 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp, 162 abi_ulong stringp); 163 int loader_exec(const char *filename, char **argv, char **envp, 164 struct target_pt_regs *regs, struct image_info *infop, 165 struct bsd_binprm *bprm); 166 167 int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs, 168 struct image_info *info); 169 int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs, 170 struct image_info *info); 171 int is_target_elf_binary(int fd); 172 173 abi_long memcpy_to_target(abi_ulong dest, const void *src, 174 unsigned long len); 175 void target_set_brk(abi_ulong new_brk); 176 abi_long do_brk(abi_ulong new_brk); 177 void syscall_init(void); 178 abi_long do_freebsd_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, abi_long arg7, 181 abi_long arg8); 182 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1, 183 abi_long arg2, abi_long arg3, abi_long arg4, 184 abi_long arg5, abi_long arg6); 185 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1, 186 abi_long arg2, abi_long arg3, abi_long arg4, 187 abi_long arg5, abi_long arg6); 188 void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2); 189 extern __thread CPUState *thread_cpu; 190 void cpu_loop(CPUArchState *env); 191 char *target_strerror(int err); 192 int get_osversion(void); 193 void fork_start(void); 194 void fork_end(pid_t pid); 195 196 #include "qemu/log.h" 197 198 /* strace.c */ 199 struct syscallname { 200 int nr; 201 const char *name; 202 const char *format; 203 void (*call)(const struct syscallname *, 204 abi_long, abi_long, abi_long, 205 abi_long, abi_long, abi_long); 206 void (*result)(const struct syscallname *, abi_long); 207 }; 208 209 void 210 print_freebsd_syscall(int num, 211 abi_long arg1, abi_long arg2, abi_long arg3, 212 abi_long arg4, abi_long arg5, abi_long arg6); 213 void print_freebsd_syscall_ret(int num, abi_long ret); 214 void 215 print_netbsd_syscall(int num, 216 abi_long arg1, abi_long arg2, abi_long arg3, 217 abi_long arg4, abi_long arg5, abi_long arg6); 218 void print_netbsd_syscall_ret(int num, abi_long ret); 219 void 220 print_openbsd_syscall(int num, 221 abi_long arg1, abi_long arg2, abi_long arg3, 222 abi_long arg4, abi_long arg5, abi_long arg6); 223 void print_openbsd_syscall_ret(int num, abi_long ret); 224 /** 225 * print_taken_signal: 226 * @target_signum: target signal being taken 227 * @tinfo: target_siginfo_t which will be passed to the guest for the signal 228 * 229 * Print strace output indicating that this signal is being taken by the guest, 230 * in a format similar to: 231 * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} --- 232 */ 233 void print_taken_signal(int target_signum, const target_siginfo_t *tinfo); 234 extern int do_strace; 235 236 /* mmap.c */ 237 int target_mprotect(abi_ulong start, abi_ulong len, int prot); 238 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, 239 int flags, int fd, off_t offset); 240 int target_munmap(abi_ulong start, abi_ulong len); 241 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, 242 abi_ulong new_size, unsigned long flags, 243 abi_ulong new_addr); 244 int target_msync(abi_ulong start, abi_ulong len, int flags); 245 extern abi_ulong mmap_next_start; 246 abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size); 247 void mmap_reserve(abi_ulong start, abi_ulong size); 248 void TSA_NO_TSA mmap_fork_start(void); 249 void TSA_NO_TSA mmap_fork_end(int child); 250 251 /* main.c */ 252 extern char qemu_proc_pathname[]; 253 extern unsigned long target_maxtsiz; 254 extern unsigned long target_dfldsiz; 255 extern unsigned long target_maxdsiz; 256 extern unsigned long target_dflssiz; 257 extern unsigned long target_maxssiz; 258 extern unsigned long target_sgrowsiz; 259 260 /* os-syscall.c */ 261 abi_long get_errno(abi_long ret); 262 bool is_error(abi_long ret); 263 int host_to_target_errno(int err); 264 265 /* os-proc.c */ 266 abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp, 267 abi_ulong guest_envp, int do_fexec); 268 abi_long do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2, 269 abi_ulong arg3, abi_ulong arg4, abi_ulong arg5, abi_ulong arg6); 270 271 /* os-sys.c */ 272 abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen, 273 abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen); 274 abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep, 275 int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, 276 abi_ulong newlen); 277 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2); 278 279 /* user access */ 280 281 #define VERIFY_READ PAGE_READ 282 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE) 283 284 static inline bool access_ok(int type, abi_ulong addr, abi_ulong size) 285 { 286 return page_check_range((target_ulong)addr, size, type); 287 } 288 289 /* 290 * NOTE __get_user and __put_user use host pointers and don't check access. 291 * 292 * These are usually used to access struct data members once the struct has been 293 * locked - usually with lock_user_struct(). 294 */ 295 296 /* 297 * Tricky points: 298 * - Use __builtin_choose_expr to avoid type promotion from ?:, 299 * - Invalid sizes result in a compile time error stemming from 300 * the fact that abort has no parameters. 301 * - It's easier to use the endian-specific unaligned load/store 302 * functions than host-endian unaligned load/store plus tswapN. 303 * - The pragmas are necessary only to silence a clang false-positive 304 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 . 305 * - gcc has bugs in its _Pragma() support in some versions, eg 306 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only 307 * include the warning-suppression pragmas for clang 308 */ 309 #if defined(__clang__) && __has_warning("-Waddress-of-packed-member") 310 #define PRAGMA_DISABLE_PACKED_WARNING \ 311 _Pragma("GCC diagnostic push"); \ 312 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"") 313 314 #define PRAGMA_REENABLE_PACKED_WARNING \ 315 _Pragma("GCC diagnostic pop") 316 317 #else 318 #define PRAGMA_DISABLE_PACKED_WARNING 319 #define PRAGMA_REENABLE_PACKED_WARNING 320 #endif 321 322 #define __put_user_e(x, hptr, e) \ 323 do { \ 324 PRAGMA_DISABLE_PACKED_WARNING; \ 325 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \ 326 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \ 327 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \ 328 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \ 329 ((hptr), (x)), (void)0); \ 330 PRAGMA_REENABLE_PACKED_WARNING; \ 331 } while (0) 332 333 #define __get_user_e(x, hptr, e) \ 334 do { \ 335 PRAGMA_DISABLE_PACKED_WARNING; \ 336 ((x) = (typeof(*hptr))( \ 337 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \ 338 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \ 339 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \ 340 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \ 341 (hptr)), (void)0); \ 342 PRAGMA_REENABLE_PACKED_WARNING; \ 343 } while (0) 344 345 346 #if TARGET_BIG_ENDIAN 347 # define __put_user(x, hptr) __put_user_e(x, hptr, be) 348 # define __get_user(x, hptr) __get_user_e(x, hptr, be) 349 #else 350 # define __put_user(x, hptr) __put_user_e(x, hptr, le) 351 # define __get_user(x, hptr) __get_user_e(x, hptr, le) 352 #endif 353 354 /* 355 * put_user()/get_user() take a guest address and check access 356 * 357 * These are usually used to access an atomic data type, such as an int, that 358 * has been passed by address. These internally perform locking and unlocking 359 * on the data type. 360 */ 361 #define put_user(x, gaddr, target_type) \ 362 ({ \ 363 abi_ulong __gaddr = (gaddr); \ 364 target_type *__hptr; \ 365 abi_long __ret = 0; \ 366 __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \ 367 if (__hptr) { \ 368 __put_user((x), __hptr); \ 369 unlock_user(__hptr, __gaddr, sizeof(target_type)); \ 370 } else \ 371 __ret = -TARGET_EFAULT; \ 372 __ret; \ 373 }) 374 375 #define get_user(x, gaddr, target_type) \ 376 ({ \ 377 abi_ulong __gaddr = (gaddr); \ 378 target_type *__hptr; \ 379 abi_long __ret = 0; \ 380 __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \ 381 if (__hptr) { \ 382 __get_user((x), __hptr); \ 383 unlock_user(__hptr, __gaddr, 0); \ 384 } else { \ 385 (x) = 0; \ 386 __ret = -TARGET_EFAULT; \ 387 } \ 388 __ret; \ 389 }) 390 391 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong) 392 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long) 393 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t) 394 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t) 395 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t) 396 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t) 397 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t) 398 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t) 399 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t) 400 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t) 401 402 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong) 403 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long) 404 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t) 405 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t) 406 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t) 407 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t) 408 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t) 409 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t) 410 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t) 411 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t) 412 413 /* 414 * copy_from_user() and copy_to_user() are usually used to copy data 415 * buffers between the target and host. These internally perform 416 * locking/unlocking of the memory. 417 */ 418 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len); 419 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len); 420 421 /* 422 * Functions for accessing guest memory. The tget and tput functions 423 * read/write single values, byteswapping as necessary. The lock_user function 424 * gets a pointer to a contiguous area of guest memory, but does not perform 425 * any byteswapping. lock_user may return either a pointer to the guest 426 * memory, or a temporary buffer. 427 */ 428 429 /* 430 * Lock an area of guest memory into the host. If copy is true then the 431 * host area will have the same contents as the guest. 432 */ 433 static inline void *lock_user(int type, abi_ulong guest_addr, long len, 434 int copy) 435 { 436 if (!access_ok(type, guest_addr, len)) { 437 return NULL; 438 } 439 #ifdef CONFIG_DEBUG_REMAP 440 { 441 void *addr; 442 addr = g_malloc(len); 443 if (copy) { 444 memcpy(addr, g2h_untagged(guest_addr), len); 445 } else { 446 memset(addr, 0, len); 447 } 448 return addr; 449 } 450 #else 451 return g2h_untagged(guest_addr); 452 #endif 453 } 454 455 /* 456 * Unlock an area of guest memory. The first LEN bytes must be flushed back to 457 * guest memory. host_ptr = NULL is explicitly allowed and does nothing. 458 */ 459 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr, 460 long len) 461 { 462 463 #ifdef CONFIG_DEBUG_REMAP 464 if (!host_ptr) { 465 return; 466 } 467 if (host_ptr == g2h_untagged(guest_addr)) { 468 return; 469 } 470 if (len > 0) { 471 memcpy(g2h_untagged(guest_addr), host_ptr, len); 472 } 473 g_free(host_ptr); 474 #endif 475 } 476 477 /* 478 * Return the length of a string in target memory or -TARGET_EFAULT if access 479 * error. 480 */ 481 abi_long target_strlen(abi_ulong gaddr); 482 483 /* Like lock_user but for null terminated strings. */ 484 static inline void *lock_user_string(abi_ulong guest_addr) 485 { 486 abi_long len; 487 len = target_strlen(guest_addr); 488 if (len < 0) { 489 return NULL; 490 } 491 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1); 492 } 493 494 /* Helper macros for locking/unlocking a target struct. */ 495 #define lock_user_struct(type, host_ptr, guest_addr, copy) \ 496 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy)) 497 #define unlock_user_struct(host_ptr, guest_addr, copy) \ 498 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0) 499 500 static inline uint64_t target_arg64(uint32_t word0, uint32_t word1) 501 { 502 #if TARGET_ABI_BITS == 32 503 #if TARGET_BIG_ENDIAN 504 return ((uint64_t)word0 << 32) | word1; 505 #else 506 return ((uint64_t)word1 << 32) | word0; 507 #endif 508 #else /* TARGET_ABI_BITS != 32 */ 509 return word0; 510 #endif /* TARGET_ABI_BITS != 32 */ 511 } 512 513 #include <pthread.h> 514 515 #include "user/safe-syscall.h" 516 517 #endif /* QEMU_H */ 518