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 21 #include "qemu/osdep.h" 22 #include "cpu.h" 23 #include "qemu/units.h" 24 #include "exec/cpu_ldst.h" 25 #include "exec/exec-all.h" 26 27 #undef DEBUG_REMAP 28 29 #include "exec/user/abitypes.h" 30 31 extern char **environ; 32 33 enum BSDType { 34 target_freebsd, 35 target_netbsd, 36 target_openbsd, 37 }; 38 extern enum BSDType bsd_type; 39 40 #include "exec/user/thunk.h" 41 #include "target_arch.h" 42 #include "syscall_defs.h" 43 #include "target_syscall.h" 44 #include "target_os_vmparam.h" 45 #include "exec/gdbstub.h" 46 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 51 */ 52 struct image_info { 53 abi_ulong load_addr; 54 abi_ulong start_code; 55 abi_ulong end_code; 56 abi_ulong start_data; 57 abi_ulong end_data; 58 abi_ulong start_brk; 59 abi_ulong brk; 60 abi_ulong start_mmap; 61 abi_ulong mmap; 62 abi_ulong rss; 63 abi_ulong start_stack; 64 abi_ulong entry; 65 abi_ulong code_offset; 66 abi_ulong data_offset; 67 }; 68 69 #define MAX_SIGQUEUE_SIZE 1024 70 71 struct sigqueue { 72 struct sigqueue *next; 73 }; 74 75 struct emulated_sigtable { 76 int pending; /* true if signal is pending */ 77 struct sigqueue *first; 78 /* in order to always have memory for the first signal, we put it here */ 79 struct sigqueue info; 80 }; 81 82 /* 83 * NOTE: we force a big alignment so that the stack stored after is aligned too 84 */ 85 typedef struct TaskState { 86 pid_t ts_tid; /* tid (or pid) of this task */ 87 88 struct TaskState *next; 89 struct bsd_binprm *bprm; 90 int used; /* non zero if used */ 91 struct image_info *info; 92 93 struct emulated_sigtable sigtab[TARGET_NSIG]; 94 struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */ 95 struct sigqueue *first_free; /* first free siginfo queue entry */ 96 int signal_pending; /* non zero if a signal may be pending */ 97 98 uint8_t stack[]; 99 } __attribute__((aligned(16))) TaskState; 100 101 void init_task_state(TaskState *ts); 102 extern const char *qemu_uname_release; 103 extern unsigned long mmap_min_addr; 104 105 /* 106 * TARGET_ARG_MAX defines the number of bytes allocated for arguments 107 * and envelope for the new program. 256k should suffice for a reasonable 108 * maxiumum env+arg in 32-bit environments, bump it up to 512k for !ILP32 109 * platforms. 110 */ 111 #if TARGET_ABI_BITS > 32 112 #define TARGET_ARG_MAX (512 * KiB) 113 #else 114 #define TARGET_ARG_MAX (256 * KiB) 115 #endif 116 #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE) 117 118 /* 119 * This structure is used to hold the arguments that are 120 * used when loading binaries. 121 */ 122 struct bsd_binprm { 123 char buf[128]; 124 void *page[MAX_ARG_PAGES]; 125 abi_ulong p; 126 int fd; 127 int e_uid, e_gid; 128 int argc, envc; 129 char **argv; 130 char **envp; 131 char *filename; /* (Given) Name of binary */ 132 char *fullpath; /* Full path of binary */ 133 }; 134 135 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop); 136 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp, 137 abi_ulong stringp); 138 int loader_exec(const char *filename, char **argv, char **envp, 139 struct target_pt_regs *regs, struct image_info *infop, 140 struct bsd_binprm *bprm); 141 142 int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs, 143 struct image_info *info); 144 int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs, 145 struct image_info *info); 146 147 abi_long memcpy_to_target(abi_ulong dest, const void *src, 148 unsigned long len); 149 void target_set_brk(abi_ulong new_brk); 150 abi_long do_brk(abi_ulong new_brk); 151 void syscall_init(void); 152 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, 153 abi_long arg2, abi_long arg3, abi_long arg4, 154 abi_long arg5, abi_long arg6, abi_long arg7, 155 abi_long arg8); 156 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1, 157 abi_long arg2, abi_long arg3, abi_long arg4, 158 abi_long arg5, abi_long arg6); 159 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1, 160 abi_long arg2, abi_long arg3, abi_long arg4, 161 abi_long arg5, abi_long arg6); 162 void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2); 163 extern __thread CPUState *thread_cpu; 164 void cpu_loop(CPUArchState *env); 165 char *target_strerror(int err); 166 int get_osversion(void); 167 void fork_start(void); 168 void fork_end(int child); 169 170 #include "qemu/log.h" 171 172 /* strace.c */ 173 struct syscallname { 174 int nr; 175 const char *name; 176 const char *format; 177 void (*call)(const struct syscallname *, 178 abi_long, abi_long, abi_long, 179 abi_long, abi_long, abi_long); 180 void (*result)(const struct syscallname *, abi_long); 181 }; 182 183 void 184 print_freebsd_syscall(int num, 185 abi_long arg1, abi_long arg2, abi_long arg3, 186 abi_long arg4, abi_long arg5, abi_long arg6); 187 void print_freebsd_syscall_ret(int num, abi_long ret); 188 void 189 print_netbsd_syscall(int num, 190 abi_long arg1, abi_long arg2, abi_long arg3, 191 abi_long arg4, abi_long arg5, abi_long arg6); 192 void print_netbsd_syscall_ret(int num, abi_long ret); 193 void 194 print_openbsd_syscall(int num, 195 abi_long arg1, abi_long arg2, abi_long arg3, 196 abi_long arg4, abi_long arg5, abi_long arg6); 197 void print_openbsd_syscall_ret(int num, abi_long ret); 198 extern int do_strace; 199 200 /* signal.c */ 201 void process_pending_signals(CPUArchState *cpu_env); 202 void signal_init(void); 203 long do_sigreturn(CPUArchState *env); 204 long do_rt_sigreturn(CPUArchState *env); 205 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp); 206 207 /* mmap.c */ 208 int target_mprotect(abi_ulong start, abi_ulong len, int prot); 209 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, 210 int flags, int fd, abi_ulong offset); 211 int target_munmap(abi_ulong start, abi_ulong len); 212 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, 213 abi_ulong new_size, unsigned long flags, 214 abi_ulong new_addr); 215 int target_msync(abi_ulong start, abi_ulong len, int flags); 216 extern unsigned long last_brk; 217 void mmap_fork_start(void); 218 void mmap_fork_end(int child); 219 220 /* main.c */ 221 extern char qemu_proc_pathname[]; 222 extern unsigned long target_maxtsiz; 223 extern unsigned long target_dfldsiz; 224 extern unsigned long target_maxdsiz; 225 extern unsigned long target_dflssiz; 226 extern unsigned long target_maxssiz; 227 extern unsigned long target_sgrowsiz; 228 229 /* user access */ 230 231 #define VERIFY_READ PAGE_READ 232 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE) 233 234 static inline bool access_ok(int type, abi_ulong addr, abi_ulong size) 235 { 236 return page_check_range((target_ulong)addr, size, type) == 0; 237 } 238 239 /* 240 * NOTE __get_user and __put_user use host pointers and don't check access. 241 * 242 * These are usually used to access struct data members once the struct has been 243 * locked - usually with lock_user_struct(). 244 */ 245 #define __put_user(x, hptr)\ 246 ({\ 247 int size = sizeof(*hptr);\ 248 switch (size) {\ 249 case 1:\ 250 *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\ 251 break;\ 252 case 2:\ 253 *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\ 254 break;\ 255 case 4:\ 256 *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\ 257 break;\ 258 case 8:\ 259 *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\ 260 break;\ 261 default:\ 262 abort();\ 263 } \ 264 0;\ 265 }) 266 267 #define __get_user(x, hptr) \ 268 ({\ 269 int size = sizeof(*hptr);\ 270 switch (size) {\ 271 case 1:\ 272 x = (typeof(*hptr))*(uint8_t *)(hptr);\ 273 break;\ 274 case 2:\ 275 x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\ 276 break;\ 277 case 4:\ 278 x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\ 279 break;\ 280 case 8:\ 281 x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\ 282 break;\ 283 default:\ 284 x = 0;\ 285 abort();\ 286 } \ 287 0;\ 288 }) 289 290 /* 291 * put_user()/get_user() take a guest address and check access 292 * 293 * These are usually used to access an atomic data type, such as an int, that 294 * has been passed by address. These internally perform locking and unlocking 295 * on the data type. 296 */ 297 #define put_user(x, gaddr, target_type) \ 298 ({ \ 299 abi_ulong __gaddr = (gaddr); \ 300 target_type *__hptr; \ 301 abi_long __ret; \ 302 __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \ 303 if (__hptr) { \ 304 __ret = __put_user((x), __hptr); \ 305 unlock_user(__hptr, __gaddr, sizeof(target_type)); \ 306 } else \ 307 __ret = -TARGET_EFAULT; \ 308 __ret; \ 309 }) 310 311 #define get_user(x, gaddr, target_type) \ 312 ({ \ 313 abi_ulong __gaddr = (gaddr); \ 314 target_type *__hptr; \ 315 abi_long __ret; \ 316 __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \ 317 if (__hptr) { \ 318 __ret = __get_user((x), __hptr); \ 319 unlock_user(__hptr, __gaddr, 0); \ 320 } else { \ 321 (x) = 0; \ 322 __ret = -TARGET_EFAULT; \ 323 } \ 324 __ret; \ 325 }) 326 327 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong) 328 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long) 329 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t) 330 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t) 331 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t) 332 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t) 333 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t) 334 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t) 335 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t) 336 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t) 337 338 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong) 339 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long) 340 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t) 341 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t) 342 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t) 343 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t) 344 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t) 345 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t) 346 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t) 347 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t) 348 349 /* 350 * copy_from_user() and copy_to_user() are usually used to copy data 351 * buffers between the target and host. These internally perform 352 * locking/unlocking of the memory. 353 */ 354 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len); 355 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len); 356 357 /* 358 * Functions for accessing guest memory. The tget and tput functions 359 * read/write single values, byteswapping as necessary. The lock_user function 360 * gets a pointer to a contiguous area of guest memory, but does not perform 361 * any byteswapping. lock_user may return either a pointer to the guest 362 * memory, or a temporary buffer. 363 */ 364 365 /* 366 * Lock an area of guest memory into the host. If copy is true then the 367 * host area will have the same contents as the guest. 368 */ 369 static inline void *lock_user(int type, abi_ulong guest_addr, long len, 370 int copy) 371 { 372 if (!access_ok(type, guest_addr, len)) { 373 return NULL; 374 } 375 #ifdef DEBUG_REMAP 376 { 377 void *addr; 378 addr = g_malloc(len); 379 if (copy) { 380 memcpy(addr, g2h_untagged(guest_addr), len); 381 } else { 382 memset(addr, 0, len); 383 } 384 return addr; 385 } 386 #else 387 return g2h_untagged(guest_addr); 388 #endif 389 } 390 391 /* 392 * Unlock an area of guest memory. The first LEN bytes must be flushed back to 393 * guest memory. host_ptr = NULL is explicitly allowed and does nothing. 394 */ 395 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr, 396 long len) 397 { 398 399 #ifdef DEBUG_REMAP 400 if (!host_ptr) { 401 return; 402 } 403 if (host_ptr == g2h_untagged(guest_addr)) { 404 return; 405 } 406 if (len > 0) { 407 memcpy(g2h_untagged(guest_addr), host_ptr, len); 408 } 409 g_free(host_ptr); 410 #endif 411 } 412 413 /* 414 * Return the length of a string in target memory or -TARGET_EFAULT if access 415 * error. 416 */ 417 abi_long target_strlen(abi_ulong gaddr); 418 419 /* Like lock_user but for null terminated strings. */ 420 static inline void *lock_user_string(abi_ulong guest_addr) 421 { 422 abi_long len; 423 len = target_strlen(guest_addr); 424 if (len < 0) { 425 return NULL; 426 } 427 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1); 428 } 429 430 /* Helper macros for locking/unlocking a target struct. */ 431 #define lock_user_struct(type, host_ptr, guest_addr, copy) \ 432 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy)) 433 #define unlock_user_struct(host_ptr, guest_addr, copy) \ 434 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0) 435 436 #include <pthread.h> 437 438 #endif /* QEMU_H */ 439