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" 449554d330SWarner Losh #include "bsd-proc.h" 45c5c84d16SWarner Losh 4680da1b00SWarner Losh /* I/O */ 4777d3522bSWarner Losh safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode); 4877d3522bSWarner Losh safe_syscall4(int, openat, int, fd, const char *, path, int, flags, mode_t, 4977d3522bSWarner Losh mode); 5077d3522bSWarner Losh 5180da1b00SWarner Losh safe_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes); 5280da1b00SWarner Losh safe_syscall4(ssize_t, pread, int, fd, void *, buf, size_t, nbytes, off_t, 5380da1b00SWarner Losh offset); 5480da1b00SWarner Losh safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt); 5580da1b00SWarner Losh safe_syscall4(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt, 5680da1b00SWarner Losh off_t, offset); 5780da1b00SWarner Losh 58770d8abaSWarner Losh safe_syscall3(ssize_t, write, int, fd, void *, buf, size_t, nbytes); 59770d8abaSWarner Losh safe_syscall4(ssize_t, pwrite, int, fd, void *, buf, size_t, nbytes, off_t, 60770d8abaSWarner Losh offset); 61770d8abaSWarner Losh safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt); 62770d8abaSWarner Losh safe_syscall4(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt, 63770d8abaSWarner Losh off_t, offset); 64770d8abaSWarner Losh 6566eed099SWarner Losh void target_set_brk(abi_ulong new_brk) 6666eed099SWarner Losh { 6766eed099SWarner Losh } 6866eed099SWarner Losh 69deeff83bSWarner Losh /* 70deeff83bSWarner Losh * errno conversion. 71deeff83bSWarner Losh */ 72deeff83bSWarner Losh abi_long get_errno(abi_long ret) 73deeff83bSWarner Losh { 74deeff83bSWarner Losh if (ret == -1) { 75deeff83bSWarner Losh return -host_to_target_errno(errno); 76deeff83bSWarner Losh } else { 77deeff83bSWarner Losh return ret; 78deeff83bSWarner Losh } 79deeff83bSWarner Losh } 80deeff83bSWarner Losh 81deeff83bSWarner Losh int host_to_target_errno(int err) 82deeff83bSWarner Losh { 83deeff83bSWarner Losh /* 84deeff83bSWarner Losh * All the BSDs have the property that the error numbers are uniform across 85deeff83bSWarner Losh * all architectures for a given BSD, though they may vary between different 86deeff83bSWarner Losh * BSDs. 87deeff83bSWarner Losh */ 88deeff83bSWarner Losh return err; 89deeff83bSWarner Losh } 90deeff83bSWarner Losh 9166eed099SWarner Losh bool is_error(abi_long ret) 9266eed099SWarner Losh { 9366eed099SWarner Losh return (abi_ulong)ret >= (abi_ulong)(-4096); 9466eed099SWarner Losh } 9566eed099SWarner Losh 9666eed099SWarner Losh /* 971ed771b2SWarner Losh * Unlocks a iovec. Unlike unlock_iovec, it assumes the tvec array itself is 981ed771b2SWarner Losh * already locked from target_addr. It will be unlocked as well as all the iovec 991ed771b2SWarner Losh * elements. 1001ed771b2SWarner Losh */ 1011ed771b2SWarner Losh static void helper_unlock_iovec(struct target_iovec *target_vec, 1021ed771b2SWarner Losh abi_ulong target_addr, struct iovec *vec, 1031ed771b2SWarner Losh int count, int copy) 1041ed771b2SWarner Losh { 1051ed771b2SWarner Losh for (int i = 0; i < count; i++) { 1061ed771b2SWarner Losh abi_ulong base = tswapal(target_vec[i].iov_base); 1071ed771b2SWarner Losh 1081ed771b2SWarner Losh if (vec[i].iov_base) { 1091ed771b2SWarner Losh unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0); 1101ed771b2SWarner Losh } 1111ed771b2SWarner Losh } 1121ed771b2SWarner Losh unlock_user(target_vec, target_addr, 0); 1131ed771b2SWarner Losh } 1141ed771b2SWarner Losh 1151ed771b2SWarner Losh struct iovec *lock_iovec(int type, abi_ulong target_addr, 1161ed771b2SWarner Losh int count, int copy) 1171ed771b2SWarner Losh { 1181ed771b2SWarner Losh struct target_iovec *target_vec; 1191ed771b2SWarner Losh struct iovec *vec; 1201ed771b2SWarner Losh abi_ulong total_len, max_len; 1211ed771b2SWarner Losh int i; 1221ed771b2SWarner Losh int err = 0; 1231ed771b2SWarner Losh 1241ed771b2SWarner Losh if (count == 0) { 1251ed771b2SWarner Losh errno = 0; 1261ed771b2SWarner Losh return NULL; 1271ed771b2SWarner Losh } 1281ed771b2SWarner Losh if (count < 0 || count > IOV_MAX) { 1291ed771b2SWarner Losh errno = EINVAL; 1301ed771b2SWarner Losh return NULL; 1311ed771b2SWarner Losh } 1321ed771b2SWarner Losh 1331ed771b2SWarner Losh vec = g_try_new0(struct iovec, count); 1341ed771b2SWarner Losh if (vec == NULL) { 1351ed771b2SWarner Losh errno = ENOMEM; 1361ed771b2SWarner Losh return NULL; 1371ed771b2SWarner Losh } 1381ed771b2SWarner Losh 1391ed771b2SWarner Losh target_vec = lock_user(VERIFY_READ, target_addr, 1401ed771b2SWarner Losh count * sizeof(struct target_iovec), 1); 1411ed771b2SWarner Losh if (target_vec == NULL) { 1421ed771b2SWarner Losh err = EFAULT; 1431ed771b2SWarner Losh goto fail2; 1441ed771b2SWarner Losh } 1451ed771b2SWarner Losh 1461ed771b2SWarner Losh max_len = 0x7fffffff & MIN(TARGET_PAGE_MASK, PAGE_MASK); 1471ed771b2SWarner Losh total_len = 0; 1481ed771b2SWarner Losh 1491ed771b2SWarner Losh for (i = 0; i < count; i++) { 1501ed771b2SWarner Losh abi_ulong base = tswapal(target_vec[i].iov_base); 1511ed771b2SWarner Losh abi_long len = tswapal(target_vec[i].iov_len); 1521ed771b2SWarner Losh 1531ed771b2SWarner Losh if (len < 0) { 1541ed771b2SWarner Losh err = EINVAL; 1551ed771b2SWarner Losh goto fail; 1561ed771b2SWarner Losh } else if (len == 0) { 1571ed771b2SWarner Losh /* Zero length pointer is ignored. */ 1581ed771b2SWarner Losh vec[i].iov_base = 0; 1591ed771b2SWarner Losh } else { 1601ed771b2SWarner Losh vec[i].iov_base = lock_user(type, base, len, copy); 1611ed771b2SWarner Losh /* 1621ed771b2SWarner Losh * If the first buffer pointer is bad, this is a fault. But 1631ed771b2SWarner Losh * subsequent bad buffers will result in a partial write; this is 1641ed771b2SWarner Losh * realized by filling the vector with null pointers and zero 1651ed771b2SWarner Losh * lengths. 1661ed771b2SWarner Losh */ 1671ed771b2SWarner Losh if (!vec[i].iov_base) { 1681ed771b2SWarner Losh if (i == 0) { 1691ed771b2SWarner Losh err = EFAULT; 1701ed771b2SWarner Losh goto fail; 1711ed771b2SWarner Losh } else { 1721ed771b2SWarner Losh /* 1731ed771b2SWarner Losh * Fail all the subsequent addresses, they are already 1741ed771b2SWarner Losh * zero'd. 1751ed771b2SWarner Losh */ 1761ed771b2SWarner Losh goto out; 1771ed771b2SWarner Losh } 1781ed771b2SWarner Losh } 1791ed771b2SWarner Losh if (len > max_len - total_len) { 1801ed771b2SWarner Losh len = max_len - total_len; 1811ed771b2SWarner Losh } 1821ed771b2SWarner Losh } 1831ed771b2SWarner Losh vec[i].iov_len = len; 1841ed771b2SWarner Losh total_len += len; 1851ed771b2SWarner Losh } 1861ed771b2SWarner Losh out: 1871ed771b2SWarner Losh unlock_user(target_vec, target_addr, 0); 1881ed771b2SWarner Losh return vec; 1891ed771b2SWarner Losh 1901ed771b2SWarner Losh fail: 1911ed771b2SWarner Losh helper_unlock_iovec(target_vec, target_addr, vec, i, copy); 1921ed771b2SWarner Losh fail2: 1931ed771b2SWarner Losh g_free(vec); 1941ed771b2SWarner Losh errno = err; 1951ed771b2SWarner Losh return NULL; 1961ed771b2SWarner Losh } 1971ed771b2SWarner Losh 198883808d8SWarner Losh void unlock_iovec(struct iovec *vec, abi_ulong target_addr, 199883808d8SWarner Losh int count, int copy) 200883808d8SWarner Losh { 201883808d8SWarner Losh struct target_iovec *target_vec; 202883808d8SWarner Losh 203883808d8SWarner Losh target_vec = lock_user(VERIFY_READ, target_addr, 204883808d8SWarner Losh count * sizeof(struct target_iovec), 1); 205883808d8SWarner Losh if (target_vec) { 206883808d8SWarner Losh helper_unlock_iovec(target_vec, target_addr, vec, count, copy); 207883808d8SWarner Losh } 208883808d8SWarner Losh 209883808d8SWarner Losh g_free(vec); 210883808d8SWarner Losh } 211883808d8SWarner Losh 2121ed771b2SWarner Losh /* 213db697887SWarner Losh * All errnos that freebsd_syscall() returns must be -TARGET_<errcode>. 214db697887SWarner Losh */ 215db697887SWarner Losh static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, 216db697887SWarner Losh abi_long arg2, abi_long arg3, abi_long arg4, 217db697887SWarner Losh abi_long arg5, abi_long arg6, abi_long arg7, 218db697887SWarner Losh abi_long arg8) 219db697887SWarner Losh { 220db697887SWarner Losh abi_long ret; 221db697887SWarner Losh 222db697887SWarner Losh switch (num) { 2239554d330SWarner Losh /* 2249554d330SWarner Losh * process system calls 2259554d330SWarner Losh */ 2269554d330SWarner Losh case TARGET_FREEBSD_NR_exit: /* exit(2) */ 2279554d330SWarner Losh ret = do_bsd_exit(cpu_env, arg1); 2289554d330SWarner Losh break; 22980da1b00SWarner Losh 23080da1b00SWarner Losh /* 23180da1b00SWarner Losh * File system calls. 23280da1b00SWarner Losh */ 23380da1b00SWarner Losh case TARGET_FREEBSD_NR_read: /* read(2) */ 23480da1b00SWarner Losh ret = do_bsd_read(arg1, arg2, arg3); 23580da1b00SWarner Losh break; 23680da1b00SWarner Losh 23780da1b00SWarner Losh case TARGET_FREEBSD_NR_pread: /* pread(2) */ 23880da1b00SWarner Losh ret = do_bsd_pread(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); 23980da1b00SWarner Losh break; 24080da1b00SWarner Losh 24180da1b00SWarner Losh case TARGET_FREEBSD_NR_readv: /* readv(2) */ 24280da1b00SWarner Losh ret = do_bsd_readv(arg1, arg2, arg3); 24380da1b00SWarner Losh break; 24480da1b00SWarner Losh 24580da1b00SWarner Losh case TARGET_FREEBSD_NR_preadv: /* preadv(2) */ 24680da1b00SWarner Losh ret = do_bsd_preadv(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); 247770d8abaSWarner Losh 248770d8abaSWarner Losh case TARGET_FREEBSD_NR_write: /* write(2) */ 249770d8abaSWarner Losh ret = do_bsd_write(arg1, arg2, arg3); 250770d8abaSWarner Losh break; 251770d8abaSWarner Losh 252770d8abaSWarner Losh case TARGET_FREEBSD_NR_pwrite: /* pwrite(2) */ 253770d8abaSWarner Losh ret = do_bsd_pwrite(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); 254770d8abaSWarner Losh break; 255770d8abaSWarner Losh 256770d8abaSWarner Losh case TARGET_FREEBSD_NR_writev: /* writev(2) */ 257770d8abaSWarner Losh ret = do_bsd_writev(arg1, arg2, arg3); 258770d8abaSWarner Losh break; 259770d8abaSWarner Losh 260770d8abaSWarner Losh case TARGET_FREEBSD_NR_pwritev: /* pwritev(2) */ 261770d8abaSWarner Losh ret = do_bsd_pwritev(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); 26280da1b00SWarner Losh break; 26380da1b00SWarner Losh 26477d3522bSWarner Losh case TARGET_FREEBSD_NR_open: /* open(2) */ 26577d3522bSWarner Losh ret = do_bsd_open(arg1, arg2, arg3); 26677d3522bSWarner Losh break; 26777d3522bSWarner Losh 26877d3522bSWarner Losh case TARGET_FREEBSD_NR_openat: /* openat(2) */ 26977d3522bSWarner Losh ret = do_bsd_openat(arg1, arg2, arg3, arg4); 27077d3522bSWarner Losh break; 27177d3522bSWarner Losh 27277d3522bSWarner Losh case TARGET_FREEBSD_NR_close: /* close(2) */ 27377d3522bSWarner Losh ret = do_bsd_close(arg1); 27477d3522bSWarner Losh break; 27577d3522bSWarner Losh 276a2ba6c7bSWarner Losh case TARGET_FREEBSD_NR_fdatasync: /* fdatasync(2) */ 277a2ba6c7bSWarner Losh ret = do_bsd_fdatasync(arg1); 278a2ba6c7bSWarner Losh break; 279a2ba6c7bSWarner Losh 280a2ba6c7bSWarner Losh case TARGET_FREEBSD_NR_fsync: /* fsync(2) */ 281a2ba6c7bSWarner Losh ret = do_bsd_fsync(arg1); 282a2ba6c7bSWarner Losh break; 283a2ba6c7bSWarner Losh 284a2ba6c7bSWarner Losh case TARGET_FREEBSD_NR_freebsd12_closefrom: /* closefrom(2) */ 285a2ba6c7bSWarner Losh ret = do_bsd_closefrom(arg1); 286a2ba6c7bSWarner Losh break; 287a2ba6c7bSWarner Losh 28865c6c4c8SWarner Losh case TARGET_FREEBSD_NR_revoke: /* revoke(2) */ 28965c6c4c8SWarner Losh ret = do_bsd_revoke(arg1); 29065c6c4c8SWarner Losh break; 29165c6c4c8SWarner Losh 29265c6c4c8SWarner Losh case TARGET_FREEBSD_NR_access: /* access(2) */ 29365c6c4c8SWarner Losh ret = do_bsd_access(arg1, arg2); 29465c6c4c8SWarner Losh break; 29565c6c4c8SWarner Losh 29665c6c4c8SWarner Losh case TARGET_FREEBSD_NR_eaccess: /* eaccess(2) */ 29765c6c4c8SWarner Losh ret = do_bsd_eaccess(arg1, arg2); 29865c6c4c8SWarner Losh break; 29965c6c4c8SWarner Losh 30065c6c4c8SWarner Losh case TARGET_FREEBSD_NR_faccessat: /* faccessat(2) */ 30165c6c4c8SWarner Losh ret = do_bsd_faccessat(arg1, arg2, arg3, arg4); 30265c6c4c8SWarner Losh break; 30365c6c4c8SWarner Losh 304*390f547eSWarner Losh case TARGET_FREEBSD_NR_chdir: /* chdir(2) */ 305*390f547eSWarner Losh ret = do_bsd_chdir(arg1); 306*390f547eSWarner Losh break; 307*390f547eSWarner Losh 308*390f547eSWarner Losh case TARGET_FREEBSD_NR_fchdir: /* fchdir(2) */ 309*390f547eSWarner Losh ret = do_bsd_fchdir(arg1); 310*390f547eSWarner Losh break; 311*390f547eSWarner Losh 312db697887SWarner Losh default: 313db697887SWarner Losh qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num); 314db697887SWarner Losh ret = -TARGET_ENOSYS; 315db697887SWarner Losh break; 316db697887SWarner Losh } 317db697887SWarner Losh 318db697887SWarner Losh return ret; 319db697887SWarner Losh } 320db697887SWarner Losh 321db697887SWarner Losh /* 322db697887SWarner Losh * do_freebsd_syscall() should always have a single exit point at the end so 323db697887SWarner Losh * that actions, such as logging of syscall results, can be performed. This 324db697887SWarner Losh * as a wrapper around freebsd_syscall() so that actually happens. Since 325db697887SWarner Losh * that is a singleton, modern compilers will inline it anyway... 32666eed099SWarner Losh */ 32766eed099SWarner Losh abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, 32866eed099SWarner Losh abi_long arg2, abi_long arg3, abi_long arg4, 32966eed099SWarner Losh abi_long arg5, abi_long arg6, abi_long arg7, 33066eed099SWarner Losh abi_long arg8) 33166eed099SWarner Losh { 332db697887SWarner Losh CPUState *cpu = env_cpu(cpu_env); 333db697887SWarner Losh int ret; 334db697887SWarner Losh 335db697887SWarner Losh trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); 336db697887SWarner Losh if (do_strace) { 337db697887SWarner Losh print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6); 338db697887SWarner Losh } 339db697887SWarner Losh 340db697887SWarner Losh ret = freebsd_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6, 341db697887SWarner Losh arg7, arg8); 342db697887SWarner Losh if (do_strace) { 343db697887SWarner Losh print_freebsd_syscall_ret(num, ret); 344db697887SWarner Losh } 345db697887SWarner Losh trace_guest_user_syscall_ret(cpu, num, ret); 346db697887SWarner Losh 347db697887SWarner Losh return ret; 34866eed099SWarner Losh } 34966eed099SWarner Losh 35066eed099SWarner Losh void syscall_init(void) 35166eed099SWarner Losh { 35266eed099SWarner Losh } 353