188dae46dSSean Bruno /* 288dae46dSSean Bruno * qemu bsd user mode definition 388dae46dSSean Bruno * 488dae46dSSean Bruno * This program is free software; you can redistribute it and/or modify 588dae46dSSean Bruno * it under the terms of the GNU General Public License as published by 688dae46dSSean Bruno * the Free Software Foundation; either version 2 of the License, or 788dae46dSSean Bruno * (at your option) any later version. 888dae46dSSean Bruno * 988dae46dSSean Bruno * This program is distributed in the hope that it will be useful, 1088dae46dSSean Bruno * but WITHOUT ANY WARRANTY; without even the implied warranty of 1188dae46dSSean Bruno * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1288dae46dSSean Bruno * GNU General Public License for more details. 1388dae46dSSean Bruno * 1488dae46dSSean Bruno * You should have received a copy of the GNU General Public License 1588dae46dSSean Bruno * along with this program; if not, see <http://www.gnu.org/licenses/>. 1688dae46dSSean Bruno */ 1784778508Sblueswir1 #ifndef QEMU_H 1884778508Sblueswir1 #define QEMU_H 1984778508Sblueswir1 20ab77bd84SWarner Losh #include "qemu/osdep.h" 2184778508Sblueswir1 #include "cpu.h" 22e5e44263SWarner Losh #include "qemu/units.h" 23f08b6170SPaolo Bonzini #include "exec/cpu_ldst.h" 24ab77bd84SWarner Losh #include "exec/exec-all.h" 2584778508Sblueswir1 2684778508Sblueswir1 #undef DEBUG_REMAP 2784778508Sblueswir1 28022c62cbSPaolo Bonzini #include "exec/user/abitypes.h" 2984778508Sblueswir1 304b599848SWarner Losh extern char **environ; 314b599848SWarner Losh 3284778508Sblueswir1 enum BSDType { 3384778508Sblueswir1 target_freebsd, 3484778508Sblueswir1 target_netbsd, 3584778508Sblueswir1 target_openbsd, 3684778508Sblueswir1 }; 3778cfb07fSJuergen Lock extern enum BSDType bsd_type; 3884778508Sblueswir1 39ab77bd84SWarner Losh #include "exec/user/thunk.h" 40ab77bd84SWarner Losh #include "target_arch.h" 4184778508Sblueswir1 #include "syscall_defs.h" 420c6940d0SLluís Vilanova #include "target_syscall.h" 4382792244SWarner Losh #include "target_os_vmparam.h" 44790baaccSWarner Losh #include "target_os_signal.h" 45022c62cbSPaolo Bonzini #include "exec/gdbstub.h" 4684778508Sblueswir1 47c2bdd9a1SWarner Losh /* 48c2bdd9a1SWarner Losh * This struct is used to hold certain information about the image. Basically, 49c2bdd9a1SWarner Losh * it replicates in user space what would be certain task_struct fields in the 50c2bdd9a1SWarner Losh * kernel 5184778508Sblueswir1 */ 5284778508Sblueswir1 struct image_info { 530475f8faSWarner Losh abi_ulong load_bias; 5484778508Sblueswir1 abi_ulong load_addr; 5584778508Sblueswir1 abi_ulong start_code; 5684778508Sblueswir1 abi_ulong end_code; 5784778508Sblueswir1 abi_ulong start_data; 5884778508Sblueswir1 abi_ulong end_data; 5984778508Sblueswir1 abi_ulong start_brk; 6084778508Sblueswir1 abi_ulong brk; 6184778508Sblueswir1 abi_ulong start_mmap; 6284778508Sblueswir1 abi_ulong mmap; 6384778508Sblueswir1 abi_ulong rss; 6484778508Sblueswir1 abi_ulong start_stack; 6584778508Sblueswir1 abi_ulong entry; 6684778508Sblueswir1 abi_ulong code_offset; 6784778508Sblueswir1 abi_ulong data_offset; 680475f8faSWarner Losh abi_ulong arg_start; 690475f8faSWarner Losh abi_ulong arg_end; 700475f8faSWarner Losh uint32_t elf_flags; 7184778508Sblueswir1 }; 7284778508Sblueswir1 7384778508Sblueswir1 struct emulated_sigtable { 7484778508Sblueswir1 int pending; /* true if signal is pending */ 75*b46d4ad7SWarner Losh target_siginfo_t info; 7684778508Sblueswir1 }; 7784778508Sblueswir1 78c2bdd9a1SWarner Losh /* 79c2bdd9a1SWarner Losh * NOTE: we force a big alignment so that the stack stored after is aligned too 80c2bdd9a1SWarner Losh */ 8184778508Sblueswir1 typedef struct TaskState { 82bd88c780SAlex Bennée pid_t ts_tid; /* tid (or pid) of this task */ 83bd88c780SAlex Bennée 8484778508Sblueswir1 struct TaskState *next; 85031fe7afSWarner Losh struct bsd_binprm *bprm; 8684778508Sblueswir1 struct image_info *info; 8784778508Sblueswir1 8884778508Sblueswir1 struct emulated_sigtable sigtab[TARGET_NSIG]; 8984778508Sblueswir1 int signal_pending; /* non zero if a signal may be pending */ 9084778508Sblueswir1 91f7795e40SPhilippe Mathieu-Daudé uint8_t stack[]; 9284778508Sblueswir1 } __attribute__((aligned(16))) TaskState; 9384778508Sblueswir1 94653ccec2SWarner Losh void stop_all_tasks(void); 9584778508Sblueswir1 extern const char *qemu_uname_release; 9684778508Sblueswir1 9784778508Sblueswir1 /* 98e5e44263SWarner Losh * TARGET_ARG_MAX defines the number of bytes allocated for arguments 99e5e44263SWarner Losh * and envelope for the new program. 256k should suffice for a reasonable 100e5e44263SWarner Losh * maxiumum env+arg in 32-bit environments, bump it up to 512k for !ILP32 101e5e44263SWarner Losh * platforms. 10284778508Sblueswir1 */ 103e5e44263SWarner Losh #if TARGET_ABI_BITS > 32 104e5e44263SWarner Losh #define TARGET_ARG_MAX (512 * KiB) 105e5e44263SWarner Losh #else 106e5e44263SWarner Losh #define TARGET_ARG_MAX (256 * KiB) 107e5e44263SWarner Losh #endif 108e5e44263SWarner Losh #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE) 10984778508Sblueswir1 11084778508Sblueswir1 /* 11184778508Sblueswir1 * This structure is used to hold the arguments that are 11284778508Sblueswir1 * used when loading binaries. 11384778508Sblueswir1 */ 114afcbcff8SWarner Losh struct bsd_binprm { 11584778508Sblueswir1 char buf[128]; 11684778508Sblueswir1 void *page[MAX_ARG_PAGES]; 11784778508Sblueswir1 abi_ulong p; 11898b34d35SWarner Losh abi_ulong stringp; 11984778508Sblueswir1 int fd; 12084778508Sblueswir1 int e_uid, e_gid; 12184778508Sblueswir1 int argc, envc; 12284778508Sblueswir1 char **argv; 12384778508Sblueswir1 char **envp; 1241b50ff64SWarner Losh char *filename; /* (Given) Name of binary */ 1251b50ff64SWarner Losh char *fullpath; /* Full path of binary */ 1260475f8faSWarner Losh int (*core_dump)(int, CPUArchState *); 12784778508Sblueswir1 }; 12884778508Sblueswir1 12984778508Sblueswir1 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop); 13084778508Sblueswir1 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp, 131ffa03665SWarner Losh abi_ulong stringp); 13284778508Sblueswir1 int loader_exec(const char *filename, char **argv, char **envp, 133d37853f9SWarner Losh struct target_pt_regs *regs, struct image_info *infop, 134d37853f9SWarner Losh struct bsd_binprm *bprm); 13584778508Sblueswir1 136afcbcff8SWarner Losh int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs, 13784778508Sblueswir1 struct image_info *info); 138afcbcff8SWarner Losh int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs, 13984778508Sblueswir1 struct image_info *info); 1400475f8faSWarner Losh int is_target_elf_binary(int fd); 14184778508Sblueswir1 14284778508Sblueswir1 abi_long memcpy_to_target(abi_ulong dest, const void *src, 14384778508Sblueswir1 unsigned long len); 14484778508Sblueswir1 void target_set_brk(abi_ulong new_brk); 14584778508Sblueswir1 abi_long do_brk(abi_ulong new_brk); 14684778508Sblueswir1 void syscall_init(void); 14784778508Sblueswir1 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, 14884778508Sblueswir1 abi_long arg2, abi_long arg3, abi_long arg4, 14978cfb07fSJuergen Lock abi_long arg5, abi_long arg6, abi_long arg7, 15078cfb07fSJuergen Lock abi_long arg8); 15184778508Sblueswir1 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1, 15284778508Sblueswir1 abi_long arg2, abi_long arg3, abi_long arg4, 15384778508Sblueswir1 abi_long arg5, abi_long arg6); 15484778508Sblueswir1 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1, 15584778508Sblueswir1 abi_long arg2, abi_long arg3, abi_long arg4, 15684778508Sblueswir1 abi_long arg5, abi_long arg6); 157e5924d89SStefan Weil void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2); 158d42df502SWarner Losh extern __thread CPUState *thread_cpu; 1599349b4f9SAndreas Färber void cpu_loop(CPUArchState *env); 16084778508Sblueswir1 char *target_strerror(int err); 16184778508Sblueswir1 int get_osversion(void); 16284778508Sblueswir1 void fork_start(void); 16384778508Sblueswir1 void fork_end(int child); 16484778508Sblueswir1 1651de7afc9SPaolo Bonzini #include "qemu/log.h" 16684778508Sblueswir1 16784778508Sblueswir1 /* strace.c */ 16888dae46dSSean Bruno struct syscallname { 16988dae46dSSean Bruno int nr; 17088dae46dSSean Bruno const char *name; 17188dae46dSSean Bruno const char *format; 17288dae46dSSean Bruno void (*call)(const struct syscallname *, 17388dae46dSSean Bruno abi_long, abi_long, abi_long, 17488dae46dSSean Bruno abi_long, abi_long, abi_long); 17588dae46dSSean Bruno void (*result)(const struct syscallname *, abi_long); 17688dae46dSSean Bruno }; 17788dae46dSSean Bruno 17884778508Sblueswir1 void 17984778508Sblueswir1 print_freebsd_syscall(int num, 18084778508Sblueswir1 abi_long arg1, abi_long arg2, abi_long arg3, 18184778508Sblueswir1 abi_long arg4, abi_long arg5, abi_long arg6); 18284778508Sblueswir1 void print_freebsd_syscall_ret(int num, abi_long ret); 18384778508Sblueswir1 void 18484778508Sblueswir1 print_netbsd_syscall(int num, 18584778508Sblueswir1 abi_long arg1, abi_long arg2, abi_long arg3, 18684778508Sblueswir1 abi_long arg4, abi_long arg5, abi_long arg6); 18784778508Sblueswir1 void print_netbsd_syscall_ret(int num, abi_long ret); 18884778508Sblueswir1 void 18984778508Sblueswir1 print_openbsd_syscall(int num, 19084778508Sblueswir1 abi_long arg1, abi_long arg2, abi_long arg3, 19184778508Sblueswir1 abi_long arg4, abi_long arg5, abi_long arg6); 19284778508Sblueswir1 void print_openbsd_syscall_ret(int num, abi_long ret); 19384778508Sblueswir1 extern int do_strace; 19484778508Sblueswir1 19584778508Sblueswir1 /* signal.c */ 1969349b4f9SAndreas Färber void process_pending_signals(CPUArchState *cpu_env); 19784778508Sblueswir1 void signal_init(void); 1989349b4f9SAndreas Färber long do_sigreturn(CPUArchState *env); 1999349b4f9SAndreas Färber long do_rt_sigreturn(CPUArchState *env); 2005abfac27SWarner Losh void queue_signal(CPUArchState *env, int sig, target_siginfo_t *info); 20184778508Sblueswir1 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp); 20284778508Sblueswir1 20384778508Sblueswir1 /* mmap.c */ 20484778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot); 20584778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, 206be04f210SWarner Losh int flags, int fd, off_t offset); 20784778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len); 20884778508Sblueswir1 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, 20984778508Sblueswir1 abi_ulong new_size, unsigned long flags, 21084778508Sblueswir1 abi_ulong new_addr); 21184778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags); 21284778508Sblueswir1 extern unsigned long last_brk; 213be04f210SWarner Losh extern abi_ulong mmap_next_start; 214be04f210SWarner Losh abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size); 21584778508Sblueswir1 void mmap_fork_start(void); 21684778508Sblueswir1 void mmap_fork_end(int child); 21784778508Sblueswir1 21828e738dcSBlue Swirl /* main.c */ 21901a298a5SWarner Losh extern char qemu_proc_pathname[]; 220312a0b1cSWarner Losh extern unsigned long target_maxtsiz; 221312a0b1cSWarner Losh extern unsigned long target_dfldsiz; 222312a0b1cSWarner Losh extern unsigned long target_maxdsiz; 223312a0b1cSWarner Losh extern unsigned long target_dflssiz; 224312a0b1cSWarner Losh extern unsigned long target_maxssiz; 225312a0b1cSWarner Losh extern unsigned long target_sgrowsiz; 22628e738dcSBlue Swirl 227e5f674f0SWarner Losh /* syscall.c */ 228e5f674f0SWarner Losh abi_long get_errno(abi_long ret); 229e5f674f0SWarner Losh bool is_error(abi_long ret); 230e5f674f0SWarner Losh 231da07e694SWarner Losh /* os-sys.c */ 232da07e694SWarner Losh abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2); 233da07e694SWarner Losh 23484778508Sblueswir1 /* user access */ 23584778508Sblueswir1 2361720751fSRichard Henderson #define VERIFY_READ PAGE_READ 2371720751fSRichard Henderson #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE) 23884778508Sblueswir1 2391720751fSRichard Henderson static inline bool access_ok(int type, abi_ulong addr, abi_ulong size) 24084778508Sblueswir1 { 2411720751fSRichard Henderson return page_check_range((target_ulong)addr, size, type) == 0; 24284778508Sblueswir1 } 24384778508Sblueswir1 244c2bdd9a1SWarner Losh /* 245c2bdd9a1SWarner Losh * NOTE __get_user and __put_user use host pointers and don't check access. 246c2bdd9a1SWarner Losh * 247c2bdd9a1SWarner Losh * These are usually used to access struct data members once the struct has been 248c2bdd9a1SWarner Losh * locked - usually with lock_user_struct(). 24984778508Sblueswir1 */ 25084778508Sblueswir1 #define __put_user(x, hptr)\ 25184778508Sblueswir1 ({\ 25284778508Sblueswir1 int size = sizeof(*hptr);\ 25384778508Sblueswir1 switch (size) {\ 25484778508Sblueswir1 case 1:\ 25584778508Sblueswir1 *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\ 25684778508Sblueswir1 break;\ 25784778508Sblueswir1 case 2:\ 25884778508Sblueswir1 *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\ 25984778508Sblueswir1 break;\ 26084778508Sblueswir1 case 4:\ 26184778508Sblueswir1 *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\ 26284778508Sblueswir1 break;\ 26384778508Sblueswir1 case 8:\ 26484778508Sblueswir1 *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\ 26584778508Sblueswir1 break;\ 26684778508Sblueswir1 default:\ 26784778508Sblueswir1 abort();\ 26884778508Sblueswir1 } \ 26984778508Sblueswir1 0;\ 27084778508Sblueswir1 }) 27184778508Sblueswir1 27284778508Sblueswir1 #define __get_user(x, hptr) \ 27384778508Sblueswir1 ({\ 27484778508Sblueswir1 int size = sizeof(*hptr);\ 27584778508Sblueswir1 switch (size) {\ 27684778508Sblueswir1 case 1:\ 27784778508Sblueswir1 x = (typeof(*hptr))*(uint8_t *)(hptr);\ 27884778508Sblueswir1 break;\ 27984778508Sblueswir1 case 2:\ 28084778508Sblueswir1 x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\ 28184778508Sblueswir1 break;\ 28284778508Sblueswir1 case 4:\ 28384778508Sblueswir1 x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\ 28484778508Sblueswir1 break;\ 28584778508Sblueswir1 case 8:\ 28684778508Sblueswir1 x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\ 28784778508Sblueswir1 break;\ 28884778508Sblueswir1 default:\ 28984778508Sblueswir1 x = 0;\ 29084778508Sblueswir1 abort();\ 29184778508Sblueswir1 } \ 29284778508Sblueswir1 0;\ 29384778508Sblueswir1 }) 29484778508Sblueswir1 295c2bdd9a1SWarner Losh /* 296c2bdd9a1SWarner Losh * put_user()/get_user() take a guest address and check access 297c2bdd9a1SWarner Losh * 298c2bdd9a1SWarner Losh * These are usually used to access an atomic data type, such as an int, that 299c2bdd9a1SWarner Losh * has been passed by address. These internally perform locking and unlocking 300c2bdd9a1SWarner Losh * on the data type. 30184778508Sblueswir1 */ 30284778508Sblueswir1 #define put_user(x, gaddr, target_type) \ 30384778508Sblueswir1 ({ \ 30484778508Sblueswir1 abi_ulong __gaddr = (gaddr); \ 30584778508Sblueswir1 target_type *__hptr; \ 30684778508Sblueswir1 abi_long __ret; \ 30733066934SWarner Losh __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \ 30833066934SWarner Losh if (__hptr) { \ 30984778508Sblueswir1 __ret = __put_user((x), __hptr); \ 31084778508Sblueswir1 unlock_user(__hptr, __gaddr, sizeof(target_type)); \ 31184778508Sblueswir1 } else \ 31284778508Sblueswir1 __ret = -TARGET_EFAULT; \ 31384778508Sblueswir1 __ret; \ 31484778508Sblueswir1 }) 31584778508Sblueswir1 31684778508Sblueswir1 #define get_user(x, gaddr, target_type) \ 31784778508Sblueswir1 ({ \ 31884778508Sblueswir1 abi_ulong __gaddr = (gaddr); \ 31984778508Sblueswir1 target_type *__hptr; \ 32084778508Sblueswir1 abi_long __ret; \ 32133066934SWarner Losh __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \ 32233066934SWarner Losh if (__hptr) { \ 32384778508Sblueswir1 __ret = __get_user((x), __hptr); \ 32484778508Sblueswir1 unlock_user(__hptr, __gaddr, 0); \ 32584778508Sblueswir1 } else { \ 32684778508Sblueswir1 (x) = 0; \ 32784778508Sblueswir1 __ret = -TARGET_EFAULT; \ 32884778508Sblueswir1 } \ 32984778508Sblueswir1 __ret; \ 33084778508Sblueswir1 }) 33184778508Sblueswir1 33284778508Sblueswir1 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong) 33384778508Sblueswir1 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long) 33484778508Sblueswir1 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t) 33584778508Sblueswir1 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t) 33684778508Sblueswir1 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t) 33784778508Sblueswir1 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t) 33884778508Sblueswir1 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t) 33984778508Sblueswir1 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t) 34084778508Sblueswir1 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t) 34184778508Sblueswir1 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t) 34284778508Sblueswir1 34384778508Sblueswir1 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong) 34484778508Sblueswir1 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long) 34584778508Sblueswir1 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t) 34684778508Sblueswir1 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t) 34784778508Sblueswir1 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t) 34884778508Sblueswir1 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t) 34984778508Sblueswir1 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t) 35084778508Sblueswir1 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t) 35184778508Sblueswir1 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t) 35284778508Sblueswir1 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t) 35384778508Sblueswir1 354c2bdd9a1SWarner Losh /* 355c2bdd9a1SWarner Losh * copy_from_user() and copy_to_user() are usually used to copy data 35684778508Sblueswir1 * buffers between the target and host. These internally perform 35784778508Sblueswir1 * locking/unlocking of the memory. 35884778508Sblueswir1 */ 35984778508Sblueswir1 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len); 36084778508Sblueswir1 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len); 36184778508Sblueswir1 362c2bdd9a1SWarner Losh /* 363c2bdd9a1SWarner Losh * Functions for accessing guest memory. The tget and tput functions 364c2bdd9a1SWarner Losh * read/write single values, byteswapping as necessary. The lock_user function 365c2bdd9a1SWarner Losh * gets a pointer to a contiguous area of guest memory, but does not perform 366c2bdd9a1SWarner Losh * any byteswapping. lock_user may return either a pointer to the guest 367c2bdd9a1SWarner Losh * memory, or a temporary buffer. 368c2bdd9a1SWarner Losh */ 36984778508Sblueswir1 370c2bdd9a1SWarner Losh /* 371c2bdd9a1SWarner Losh * Lock an area of guest memory into the host. If copy is true then the 372c2bdd9a1SWarner Losh * host area will have the same contents as the guest. 373c2bdd9a1SWarner Losh */ 374c2bdd9a1SWarner Losh static inline void *lock_user(int type, abi_ulong guest_addr, long len, 375c2bdd9a1SWarner Losh int copy) 37684778508Sblueswir1 { 377cb0ea019SWarner Losh if (!access_ok(type, guest_addr, len)) { 37884778508Sblueswir1 return NULL; 379cb0ea019SWarner Losh } 38084778508Sblueswir1 #ifdef DEBUG_REMAP 38184778508Sblueswir1 { 38284778508Sblueswir1 void *addr; 383fd9a3048SMd Haris Iqbal addr = g_malloc(len); 384cb0ea019SWarner Losh if (copy) { 3853e8f1628SRichard Henderson memcpy(addr, g2h_untagged(guest_addr), len); 386cb0ea019SWarner Losh } else { 38784778508Sblueswir1 memset(addr, 0, len); 388cb0ea019SWarner Losh } 38984778508Sblueswir1 return addr; 39084778508Sblueswir1 } 39184778508Sblueswir1 #else 3923e8f1628SRichard Henderson return g2h_untagged(guest_addr); 39384778508Sblueswir1 #endif 39484778508Sblueswir1 } 39584778508Sblueswir1 396c2bdd9a1SWarner Losh /* 397c2bdd9a1SWarner Losh * Unlock an area of guest memory. The first LEN bytes must be flushed back to 398c2bdd9a1SWarner Losh * guest memory. host_ptr = NULL is explicitly allowed and does nothing. 399c2bdd9a1SWarner Losh */ 40084778508Sblueswir1 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr, 40184778508Sblueswir1 long len) 40284778508Sblueswir1 { 40384778508Sblueswir1 40484778508Sblueswir1 #ifdef DEBUG_REMAP 405cb0ea019SWarner Losh if (!host_ptr) { 40684778508Sblueswir1 return; 407cb0ea019SWarner Losh } 408cb0ea019SWarner Losh if (host_ptr == g2h_untagged(guest_addr)) { 40984778508Sblueswir1 return; 410cb0ea019SWarner Losh } 411cb0ea019SWarner Losh if (len > 0) { 4123e8f1628SRichard Henderson memcpy(g2h_untagged(guest_addr), host_ptr, len); 413cb0ea019SWarner Losh } 414fd9a3048SMd Haris Iqbal g_free(host_ptr); 41584778508Sblueswir1 #endif 41684778508Sblueswir1 } 41784778508Sblueswir1 418c2bdd9a1SWarner Losh /* 419c2bdd9a1SWarner Losh * Return the length of a string in target memory or -TARGET_EFAULT if access 420c2bdd9a1SWarner Losh * error. 421c2bdd9a1SWarner Losh */ 42284778508Sblueswir1 abi_long target_strlen(abi_ulong gaddr); 42384778508Sblueswir1 42484778508Sblueswir1 /* Like lock_user but for null terminated strings. */ 42584778508Sblueswir1 static inline void *lock_user_string(abi_ulong guest_addr) 42684778508Sblueswir1 { 42784778508Sblueswir1 abi_long len; 42884778508Sblueswir1 len = target_strlen(guest_addr); 429cb0ea019SWarner Losh if (len < 0) { 43084778508Sblueswir1 return NULL; 431cb0ea019SWarner Losh } 43284778508Sblueswir1 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1); 43384778508Sblueswir1 } 43484778508Sblueswir1 43541d1af4dSStefan Weil /* Helper macros for locking/unlocking a target struct. */ 43684778508Sblueswir1 #define lock_user_struct(type, host_ptr, guest_addr, copy) \ 43784778508Sblueswir1 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy)) 43884778508Sblueswir1 #define unlock_user_struct(host_ptr, guest_addr, copy) \ 43984778508Sblueswir1 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0) 44084778508Sblueswir1 44184778508Sblueswir1 #include <pthread.h> 44284778508Sblueswir1 44384778508Sblueswir1 #endif /* QEMU_H */ 444