166eed099SWarner Losh /* 266eed099SWarner Losh * BSD syscalls 366eed099SWarner Losh * 466eed099SWarner Losh * Copyright (c) 2003-2008 Fabrice Bellard 566eed099SWarner Losh * Copyright (c) 2013-2014 Stacey D. Son 666eed099SWarner Losh * 766eed099SWarner Losh * This program is free software; you can redistribute it and/or modify 866eed099SWarner Losh * it under the terms of the GNU General Public License as published by 966eed099SWarner Losh * the Free Software Foundation; either version 2 of the License, or 1066eed099SWarner Losh * (at your option) any later version. 1166eed099SWarner Losh * 1266eed099SWarner Losh * This program is distributed in the hope that it will be useful, 1366eed099SWarner Losh * but WITHOUT ANY WARRANTY; without even the implied warranty of 1466eed099SWarner Losh * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1566eed099SWarner Losh * GNU General Public License for more details. 1666eed099SWarner Losh * 1766eed099SWarner Losh * You should have received a copy of the GNU General Public License 1866eed099SWarner Losh * along with this program; if not, see <http://www.gnu.org/licenses/>. 1966eed099SWarner Losh */ 2066eed099SWarner Losh 2166eed099SWarner Losh /* 2266eed099SWarner Losh * We need the FreeBSD "legacy" definitions. Rust needs the FreeBSD 11 system 2366eed099SWarner Losh * calls since it doesn't use libc at all, so we have to emulate that despite 2466eed099SWarner Losh * FreeBSD 11 being EOL'd. 2566eed099SWarner Losh */ 2666eed099SWarner Losh #define _WANT_FREEBSD11_STAT 2766eed099SWarner Losh #define _WANT_FREEBSD11_STATFS 2866eed099SWarner Losh #define _WANT_FREEBSD11_DIRENT 2966eed099SWarner Losh #define _WANT_KERNEL_ERRNO 3066eed099SWarner Losh #define _WANT_SEMUN 3166eed099SWarner Losh #include "qemu/osdep.h" 3266eed099SWarner Losh #include "qemu/cutils.h" 3366eed099SWarner Losh #include "qemu/path.h" 3466eed099SWarner Losh #include <sys/syscall.h> 3566eed099SWarner Losh #include <sys/param.h> 3666eed099SWarner Losh #include <sys/sysctl.h> 3766eed099SWarner Losh #include <utime.h> 3866eed099SWarner Losh 3966eed099SWarner Losh #include "qemu.h" 4066eed099SWarner Losh #include "signal-common.h" 4166eed099SWarner Losh #include "user/syscall-trace.h" 4266eed099SWarner Losh 43c5c84d16SWarner Losh #include "bsd-file.h" 44c5c84d16SWarner Losh 4566eed099SWarner Losh void target_set_brk(abi_ulong new_brk) 4666eed099SWarner Losh { 4766eed099SWarner Losh } 4866eed099SWarner Losh 49deeff83bSWarner Losh /* 50deeff83bSWarner Losh * errno conversion. 51deeff83bSWarner Losh */ 52deeff83bSWarner Losh abi_long get_errno(abi_long ret) 53deeff83bSWarner Losh { 54deeff83bSWarner Losh if (ret == -1) { 55deeff83bSWarner Losh return -host_to_target_errno(errno); 56deeff83bSWarner Losh } else { 57deeff83bSWarner Losh return ret; 58deeff83bSWarner Losh } 59deeff83bSWarner Losh } 60deeff83bSWarner Losh 61deeff83bSWarner Losh int host_to_target_errno(int err) 62deeff83bSWarner Losh { 63deeff83bSWarner Losh /* 64deeff83bSWarner Losh * All the BSDs have the property that the error numbers are uniform across 65deeff83bSWarner Losh * all architectures for a given BSD, though they may vary between different 66deeff83bSWarner Losh * BSDs. 67deeff83bSWarner Losh */ 68deeff83bSWarner Losh return err; 69deeff83bSWarner Losh } 70deeff83bSWarner Losh 7166eed099SWarner Losh bool is_error(abi_long ret) 7266eed099SWarner Losh { 7366eed099SWarner Losh return (abi_ulong)ret >= (abi_ulong)(-4096); 7466eed099SWarner Losh } 7566eed099SWarner Losh 7666eed099SWarner Losh /* 771ed771b2SWarner Losh * Unlocks a iovec. Unlike unlock_iovec, it assumes the tvec array itself is 781ed771b2SWarner Losh * already locked from target_addr. It will be unlocked as well as all the iovec 791ed771b2SWarner Losh * elements. 801ed771b2SWarner Losh */ 811ed771b2SWarner Losh static void helper_unlock_iovec(struct target_iovec *target_vec, 821ed771b2SWarner Losh abi_ulong target_addr, struct iovec *vec, 831ed771b2SWarner Losh int count, int copy) 841ed771b2SWarner Losh { 851ed771b2SWarner Losh for (int i = 0; i < count; i++) { 861ed771b2SWarner Losh abi_ulong base = tswapal(target_vec[i].iov_base); 871ed771b2SWarner Losh 881ed771b2SWarner Losh if (vec[i].iov_base) { 891ed771b2SWarner Losh unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0); 901ed771b2SWarner Losh } 911ed771b2SWarner Losh } 921ed771b2SWarner Losh unlock_user(target_vec, target_addr, 0); 931ed771b2SWarner Losh } 941ed771b2SWarner Losh 951ed771b2SWarner Losh struct iovec *lock_iovec(int type, abi_ulong target_addr, 961ed771b2SWarner Losh int count, int copy) 971ed771b2SWarner Losh { 981ed771b2SWarner Losh struct target_iovec *target_vec; 991ed771b2SWarner Losh struct iovec *vec; 1001ed771b2SWarner Losh abi_ulong total_len, max_len; 1011ed771b2SWarner Losh int i; 1021ed771b2SWarner Losh int err = 0; 1031ed771b2SWarner Losh 1041ed771b2SWarner Losh if (count == 0) { 1051ed771b2SWarner Losh errno = 0; 1061ed771b2SWarner Losh return NULL; 1071ed771b2SWarner Losh } 1081ed771b2SWarner Losh if (count < 0 || count > IOV_MAX) { 1091ed771b2SWarner Losh errno = EINVAL; 1101ed771b2SWarner Losh return NULL; 1111ed771b2SWarner Losh } 1121ed771b2SWarner Losh 1131ed771b2SWarner Losh vec = g_try_new0(struct iovec, count); 1141ed771b2SWarner Losh if (vec == NULL) { 1151ed771b2SWarner Losh errno = ENOMEM; 1161ed771b2SWarner Losh return NULL; 1171ed771b2SWarner Losh } 1181ed771b2SWarner Losh 1191ed771b2SWarner Losh target_vec = lock_user(VERIFY_READ, target_addr, 1201ed771b2SWarner Losh count * sizeof(struct target_iovec), 1); 1211ed771b2SWarner Losh if (target_vec == NULL) { 1221ed771b2SWarner Losh err = EFAULT; 1231ed771b2SWarner Losh goto fail2; 1241ed771b2SWarner Losh } 1251ed771b2SWarner Losh 1261ed771b2SWarner Losh max_len = 0x7fffffff & MIN(TARGET_PAGE_MASK, PAGE_MASK); 1271ed771b2SWarner Losh total_len = 0; 1281ed771b2SWarner Losh 1291ed771b2SWarner Losh for (i = 0; i < count; i++) { 1301ed771b2SWarner Losh abi_ulong base = tswapal(target_vec[i].iov_base); 1311ed771b2SWarner Losh abi_long len = tswapal(target_vec[i].iov_len); 1321ed771b2SWarner Losh 1331ed771b2SWarner Losh if (len < 0) { 1341ed771b2SWarner Losh err = EINVAL; 1351ed771b2SWarner Losh goto fail; 1361ed771b2SWarner Losh } else if (len == 0) { 1371ed771b2SWarner Losh /* Zero length pointer is ignored. */ 1381ed771b2SWarner Losh vec[i].iov_base = 0; 1391ed771b2SWarner Losh } else { 1401ed771b2SWarner Losh vec[i].iov_base = lock_user(type, base, len, copy); 1411ed771b2SWarner Losh /* 1421ed771b2SWarner Losh * If the first buffer pointer is bad, this is a fault. But 1431ed771b2SWarner Losh * subsequent bad buffers will result in a partial write; this is 1441ed771b2SWarner Losh * realized by filling the vector with null pointers and zero 1451ed771b2SWarner Losh * lengths. 1461ed771b2SWarner Losh */ 1471ed771b2SWarner Losh if (!vec[i].iov_base) { 1481ed771b2SWarner Losh if (i == 0) { 1491ed771b2SWarner Losh err = EFAULT; 1501ed771b2SWarner Losh goto fail; 1511ed771b2SWarner Losh } else { 1521ed771b2SWarner Losh /* 1531ed771b2SWarner Losh * Fail all the subsequent addresses, they are already 1541ed771b2SWarner Losh * zero'd. 1551ed771b2SWarner Losh */ 1561ed771b2SWarner Losh goto out; 1571ed771b2SWarner Losh } 1581ed771b2SWarner Losh } 1591ed771b2SWarner Losh if (len > max_len - total_len) { 1601ed771b2SWarner Losh len = max_len - total_len; 1611ed771b2SWarner Losh } 1621ed771b2SWarner Losh } 1631ed771b2SWarner Losh vec[i].iov_len = len; 1641ed771b2SWarner Losh total_len += len; 1651ed771b2SWarner Losh } 1661ed771b2SWarner Losh out: 1671ed771b2SWarner Losh unlock_user(target_vec, target_addr, 0); 1681ed771b2SWarner Losh return vec; 1691ed771b2SWarner Losh 1701ed771b2SWarner Losh fail: 1711ed771b2SWarner Losh helper_unlock_iovec(target_vec, target_addr, vec, i, copy); 1721ed771b2SWarner Losh fail2: 1731ed771b2SWarner Losh g_free(vec); 1741ed771b2SWarner Losh errno = err; 1751ed771b2SWarner Losh return NULL; 1761ed771b2SWarner Losh } 1771ed771b2SWarner Losh 178883808d8SWarner Losh void unlock_iovec(struct iovec *vec, abi_ulong target_addr, 179883808d8SWarner Losh int count, int copy) 180883808d8SWarner Losh { 181883808d8SWarner Losh struct target_iovec *target_vec; 182883808d8SWarner Losh 183883808d8SWarner Losh target_vec = lock_user(VERIFY_READ, target_addr, 184883808d8SWarner Losh count * sizeof(struct target_iovec), 1); 185883808d8SWarner Losh if (target_vec) { 186883808d8SWarner Losh helper_unlock_iovec(target_vec, target_addr, vec, count, copy); 187883808d8SWarner Losh } 188883808d8SWarner Losh 189883808d8SWarner Losh g_free(vec); 190883808d8SWarner Losh } 191883808d8SWarner Losh 1921ed771b2SWarner Losh /* 193*db697887SWarner Losh * All errnos that freebsd_syscall() returns must be -TARGET_<errcode>. 194*db697887SWarner Losh */ 195*db697887SWarner Losh static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, 196*db697887SWarner Losh abi_long arg2, abi_long arg3, abi_long arg4, 197*db697887SWarner Losh abi_long arg5, abi_long arg6, abi_long arg7, 198*db697887SWarner Losh abi_long arg8) 199*db697887SWarner Losh { 200*db697887SWarner Losh abi_long ret; 201*db697887SWarner Losh 202*db697887SWarner Losh switch (num) { 203*db697887SWarner Losh default: 204*db697887SWarner Losh qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num); 205*db697887SWarner Losh ret = -TARGET_ENOSYS; 206*db697887SWarner Losh break; 207*db697887SWarner Losh } 208*db697887SWarner Losh 209*db697887SWarner Losh return ret; 210*db697887SWarner Losh } 211*db697887SWarner Losh 212*db697887SWarner Losh /* 213*db697887SWarner Losh * do_freebsd_syscall() should always have a single exit point at the end so 214*db697887SWarner Losh * that actions, such as logging of syscall results, can be performed. This 215*db697887SWarner Losh * as a wrapper around freebsd_syscall() so that actually happens. Since 216*db697887SWarner Losh * that is a singleton, modern compilers will inline it anyway... 21766eed099SWarner Losh */ 21866eed099SWarner Losh abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, 21966eed099SWarner Losh abi_long arg2, abi_long arg3, abi_long arg4, 22066eed099SWarner Losh abi_long arg5, abi_long arg6, abi_long arg7, 22166eed099SWarner Losh abi_long arg8) 22266eed099SWarner Losh { 223*db697887SWarner Losh CPUState *cpu = env_cpu(cpu_env); 224*db697887SWarner Losh int ret; 225*db697887SWarner Losh 226*db697887SWarner Losh trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); 227*db697887SWarner Losh if (do_strace) { 228*db697887SWarner Losh print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6); 229*db697887SWarner Losh } 230*db697887SWarner Losh 231*db697887SWarner Losh ret = freebsd_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6, 232*db697887SWarner Losh arg7, arg8); 233*db697887SWarner Losh if (do_strace) { 234*db697887SWarner Losh print_freebsd_syscall_ret(num, ret); 235*db697887SWarner Losh } 236*db697887SWarner Losh trace_guest_user_syscall_ret(cpu, num, ret); 237*db697887SWarner Losh 238*db697887SWarner Losh return ret; 23966eed099SWarner Losh } 24066eed099SWarner Losh 24166eed099SWarner Losh void syscall_init(void) 24266eed099SWarner Losh { 24366eed099SWarner Losh } 244