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 4580da1b00SWarner Losh /* I/O */ 4680da1b00SWarner Losh safe_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes); 4780da1b00SWarner Losh safe_syscall4(ssize_t, pread, int, fd, void *, buf, size_t, nbytes, off_t, 4880da1b00SWarner Losh offset); 4980da1b00SWarner Losh safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt); 5080da1b00SWarner Losh safe_syscall4(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt, 5180da1b00SWarner Losh off_t, offset); 5280da1b00SWarner Losh 53*770d8abaSWarner Losh safe_syscall3(ssize_t, write, int, fd, void *, buf, size_t, nbytes); 54*770d8abaSWarner Losh safe_syscall4(ssize_t, pwrite, int, fd, void *, buf, size_t, nbytes, off_t, 55*770d8abaSWarner Losh offset); 56*770d8abaSWarner Losh safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt); 57*770d8abaSWarner Losh safe_syscall4(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt, 58*770d8abaSWarner Losh off_t, offset); 59*770d8abaSWarner Losh 6066eed099SWarner Losh void target_set_brk(abi_ulong new_brk) 6166eed099SWarner Losh { 6266eed099SWarner Losh } 6366eed099SWarner Losh 64deeff83bSWarner Losh /* 65deeff83bSWarner Losh * errno conversion. 66deeff83bSWarner Losh */ 67deeff83bSWarner Losh abi_long get_errno(abi_long ret) 68deeff83bSWarner Losh { 69deeff83bSWarner Losh if (ret == -1) { 70deeff83bSWarner Losh return -host_to_target_errno(errno); 71deeff83bSWarner Losh } else { 72deeff83bSWarner Losh return ret; 73deeff83bSWarner Losh } 74deeff83bSWarner Losh } 75deeff83bSWarner Losh 76deeff83bSWarner Losh int host_to_target_errno(int err) 77deeff83bSWarner Losh { 78deeff83bSWarner Losh /* 79deeff83bSWarner Losh * All the BSDs have the property that the error numbers are uniform across 80deeff83bSWarner Losh * all architectures for a given BSD, though they may vary between different 81deeff83bSWarner Losh * BSDs. 82deeff83bSWarner Losh */ 83deeff83bSWarner Losh return err; 84deeff83bSWarner Losh } 85deeff83bSWarner Losh 8666eed099SWarner Losh bool is_error(abi_long ret) 8766eed099SWarner Losh { 8866eed099SWarner Losh return (abi_ulong)ret >= (abi_ulong)(-4096); 8966eed099SWarner Losh } 9066eed099SWarner Losh 9166eed099SWarner Losh /* 921ed771b2SWarner Losh * Unlocks a iovec. Unlike unlock_iovec, it assumes the tvec array itself is 931ed771b2SWarner Losh * already locked from target_addr. It will be unlocked as well as all the iovec 941ed771b2SWarner Losh * elements. 951ed771b2SWarner Losh */ 961ed771b2SWarner Losh static void helper_unlock_iovec(struct target_iovec *target_vec, 971ed771b2SWarner Losh abi_ulong target_addr, struct iovec *vec, 981ed771b2SWarner Losh int count, int copy) 991ed771b2SWarner Losh { 1001ed771b2SWarner Losh for (int i = 0; i < count; i++) { 1011ed771b2SWarner Losh abi_ulong base = tswapal(target_vec[i].iov_base); 1021ed771b2SWarner Losh 1031ed771b2SWarner Losh if (vec[i].iov_base) { 1041ed771b2SWarner Losh unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0); 1051ed771b2SWarner Losh } 1061ed771b2SWarner Losh } 1071ed771b2SWarner Losh unlock_user(target_vec, target_addr, 0); 1081ed771b2SWarner Losh } 1091ed771b2SWarner Losh 1101ed771b2SWarner Losh struct iovec *lock_iovec(int type, abi_ulong target_addr, 1111ed771b2SWarner Losh int count, int copy) 1121ed771b2SWarner Losh { 1131ed771b2SWarner Losh struct target_iovec *target_vec; 1141ed771b2SWarner Losh struct iovec *vec; 1151ed771b2SWarner Losh abi_ulong total_len, max_len; 1161ed771b2SWarner Losh int i; 1171ed771b2SWarner Losh int err = 0; 1181ed771b2SWarner Losh 1191ed771b2SWarner Losh if (count == 0) { 1201ed771b2SWarner Losh errno = 0; 1211ed771b2SWarner Losh return NULL; 1221ed771b2SWarner Losh } 1231ed771b2SWarner Losh if (count < 0 || count > IOV_MAX) { 1241ed771b2SWarner Losh errno = EINVAL; 1251ed771b2SWarner Losh return NULL; 1261ed771b2SWarner Losh } 1271ed771b2SWarner Losh 1281ed771b2SWarner Losh vec = g_try_new0(struct iovec, count); 1291ed771b2SWarner Losh if (vec == NULL) { 1301ed771b2SWarner Losh errno = ENOMEM; 1311ed771b2SWarner Losh return NULL; 1321ed771b2SWarner Losh } 1331ed771b2SWarner Losh 1341ed771b2SWarner Losh target_vec = lock_user(VERIFY_READ, target_addr, 1351ed771b2SWarner Losh count * sizeof(struct target_iovec), 1); 1361ed771b2SWarner Losh if (target_vec == NULL) { 1371ed771b2SWarner Losh err = EFAULT; 1381ed771b2SWarner Losh goto fail2; 1391ed771b2SWarner Losh } 1401ed771b2SWarner Losh 1411ed771b2SWarner Losh max_len = 0x7fffffff & MIN(TARGET_PAGE_MASK, PAGE_MASK); 1421ed771b2SWarner Losh total_len = 0; 1431ed771b2SWarner Losh 1441ed771b2SWarner Losh for (i = 0; i < count; i++) { 1451ed771b2SWarner Losh abi_ulong base = tswapal(target_vec[i].iov_base); 1461ed771b2SWarner Losh abi_long len = tswapal(target_vec[i].iov_len); 1471ed771b2SWarner Losh 1481ed771b2SWarner Losh if (len < 0) { 1491ed771b2SWarner Losh err = EINVAL; 1501ed771b2SWarner Losh goto fail; 1511ed771b2SWarner Losh } else if (len == 0) { 1521ed771b2SWarner Losh /* Zero length pointer is ignored. */ 1531ed771b2SWarner Losh vec[i].iov_base = 0; 1541ed771b2SWarner Losh } else { 1551ed771b2SWarner Losh vec[i].iov_base = lock_user(type, base, len, copy); 1561ed771b2SWarner Losh /* 1571ed771b2SWarner Losh * If the first buffer pointer is bad, this is a fault. But 1581ed771b2SWarner Losh * subsequent bad buffers will result in a partial write; this is 1591ed771b2SWarner Losh * realized by filling the vector with null pointers and zero 1601ed771b2SWarner Losh * lengths. 1611ed771b2SWarner Losh */ 1621ed771b2SWarner Losh if (!vec[i].iov_base) { 1631ed771b2SWarner Losh if (i == 0) { 1641ed771b2SWarner Losh err = EFAULT; 1651ed771b2SWarner Losh goto fail; 1661ed771b2SWarner Losh } else { 1671ed771b2SWarner Losh /* 1681ed771b2SWarner Losh * Fail all the subsequent addresses, they are already 1691ed771b2SWarner Losh * zero'd. 1701ed771b2SWarner Losh */ 1711ed771b2SWarner Losh goto out; 1721ed771b2SWarner Losh } 1731ed771b2SWarner Losh } 1741ed771b2SWarner Losh if (len > max_len - total_len) { 1751ed771b2SWarner Losh len = max_len - total_len; 1761ed771b2SWarner Losh } 1771ed771b2SWarner Losh } 1781ed771b2SWarner Losh vec[i].iov_len = len; 1791ed771b2SWarner Losh total_len += len; 1801ed771b2SWarner Losh } 1811ed771b2SWarner Losh out: 1821ed771b2SWarner Losh unlock_user(target_vec, target_addr, 0); 1831ed771b2SWarner Losh return vec; 1841ed771b2SWarner Losh 1851ed771b2SWarner Losh fail: 1861ed771b2SWarner Losh helper_unlock_iovec(target_vec, target_addr, vec, i, copy); 1871ed771b2SWarner Losh fail2: 1881ed771b2SWarner Losh g_free(vec); 1891ed771b2SWarner Losh errno = err; 1901ed771b2SWarner Losh return NULL; 1911ed771b2SWarner Losh } 1921ed771b2SWarner Losh 193883808d8SWarner Losh void unlock_iovec(struct iovec *vec, abi_ulong target_addr, 194883808d8SWarner Losh int count, int copy) 195883808d8SWarner Losh { 196883808d8SWarner Losh struct target_iovec *target_vec; 197883808d8SWarner Losh 198883808d8SWarner Losh target_vec = lock_user(VERIFY_READ, target_addr, 199883808d8SWarner Losh count * sizeof(struct target_iovec), 1); 200883808d8SWarner Losh if (target_vec) { 201883808d8SWarner Losh helper_unlock_iovec(target_vec, target_addr, vec, count, copy); 202883808d8SWarner Losh } 203883808d8SWarner Losh 204883808d8SWarner Losh g_free(vec); 205883808d8SWarner Losh } 206883808d8SWarner Losh 2071ed771b2SWarner Losh /* 208db697887SWarner Losh * All errnos that freebsd_syscall() returns must be -TARGET_<errcode>. 209db697887SWarner Losh */ 210db697887SWarner Losh static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, 211db697887SWarner Losh abi_long arg2, abi_long arg3, abi_long arg4, 212db697887SWarner Losh abi_long arg5, abi_long arg6, abi_long arg7, 213db697887SWarner Losh abi_long arg8) 214db697887SWarner Losh { 215db697887SWarner Losh abi_long ret; 216db697887SWarner Losh 217db697887SWarner Losh switch (num) { 21880da1b00SWarner Losh 21980da1b00SWarner Losh /* 22080da1b00SWarner Losh * File system calls. 22180da1b00SWarner Losh */ 22280da1b00SWarner Losh case TARGET_FREEBSD_NR_read: /* read(2) */ 22380da1b00SWarner Losh ret = do_bsd_read(arg1, arg2, arg3); 22480da1b00SWarner Losh break; 22580da1b00SWarner Losh 22680da1b00SWarner Losh case TARGET_FREEBSD_NR_pread: /* pread(2) */ 22780da1b00SWarner Losh ret = do_bsd_pread(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); 22880da1b00SWarner Losh break; 22980da1b00SWarner Losh 23080da1b00SWarner Losh case TARGET_FREEBSD_NR_readv: /* readv(2) */ 23180da1b00SWarner Losh ret = do_bsd_readv(arg1, arg2, arg3); 23280da1b00SWarner Losh break; 23380da1b00SWarner Losh 23480da1b00SWarner Losh case TARGET_FREEBSD_NR_preadv: /* preadv(2) */ 23580da1b00SWarner Losh ret = do_bsd_preadv(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); 236*770d8abaSWarner Losh 237*770d8abaSWarner Losh case TARGET_FREEBSD_NR_write: /* write(2) */ 238*770d8abaSWarner Losh ret = do_bsd_write(arg1, arg2, arg3); 239*770d8abaSWarner Losh break; 240*770d8abaSWarner Losh 241*770d8abaSWarner Losh case TARGET_FREEBSD_NR_pwrite: /* pwrite(2) */ 242*770d8abaSWarner Losh ret = do_bsd_pwrite(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); 243*770d8abaSWarner Losh break; 244*770d8abaSWarner Losh 245*770d8abaSWarner Losh case TARGET_FREEBSD_NR_writev: /* writev(2) */ 246*770d8abaSWarner Losh ret = do_bsd_writev(arg1, arg2, arg3); 247*770d8abaSWarner Losh break; 248*770d8abaSWarner Losh 249*770d8abaSWarner Losh case TARGET_FREEBSD_NR_pwritev: /* pwritev(2) */ 250*770d8abaSWarner Losh ret = do_bsd_pwritev(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); 25180da1b00SWarner Losh break; 25280da1b00SWarner Losh 253db697887SWarner Losh default: 254db697887SWarner Losh qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num); 255db697887SWarner Losh ret = -TARGET_ENOSYS; 256db697887SWarner Losh break; 257db697887SWarner Losh } 258db697887SWarner Losh 259db697887SWarner Losh return ret; 260db697887SWarner Losh } 261db697887SWarner Losh 262db697887SWarner Losh /* 263db697887SWarner Losh * do_freebsd_syscall() should always have a single exit point at the end so 264db697887SWarner Losh * that actions, such as logging of syscall results, can be performed. This 265db697887SWarner Losh * as a wrapper around freebsd_syscall() so that actually happens. Since 266db697887SWarner Losh * that is a singleton, modern compilers will inline it anyway... 26766eed099SWarner Losh */ 26866eed099SWarner Losh abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, 26966eed099SWarner Losh abi_long arg2, abi_long arg3, abi_long arg4, 27066eed099SWarner Losh abi_long arg5, abi_long arg6, abi_long arg7, 27166eed099SWarner Losh abi_long arg8) 27266eed099SWarner Losh { 273db697887SWarner Losh CPUState *cpu = env_cpu(cpu_env); 274db697887SWarner Losh int ret; 275db697887SWarner Losh 276db697887SWarner Losh trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); 277db697887SWarner Losh if (do_strace) { 278db697887SWarner Losh print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6); 279db697887SWarner Losh } 280db697887SWarner Losh 281db697887SWarner Losh ret = freebsd_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6, 282db697887SWarner Losh arg7, arg8); 283db697887SWarner Losh if (do_strace) { 284db697887SWarner Losh print_freebsd_syscall_ret(num, ret); 285db697887SWarner Losh } 286db697887SWarner Losh trace_guest_user_syscall_ret(cpu, num, ret); 287db697887SWarner Losh 288db697887SWarner Losh return ret; 28966eed099SWarner Losh } 29066eed099SWarner Losh 29166eed099SWarner Losh void syscall_init(void) 29266eed099SWarner Losh { 29366eed099SWarner Losh } 294