diff options
author | Konstantin Belousov <kib@FreeBSD.org> | 2021-09-12 22:50:39 +0000 |
---|---|---|
committer | Konstantin Belousov <kib@FreeBSD.org> | 2021-09-21 21:18:44 +0000 |
commit | cf0ee8738e31aa9e6fbf4dca4dac56d89226a71a (patch) | |
tree | 80dd53449d2afcd809cac8a4518480da87a468b5 /sys/compat/cloudabi | |
parent | c2ee4dfd04970f1597eea58bb30eb93e1ed5a491 (diff) |
Drop cloudabi
According to https://github.com/NuxiNL/cloudlibc:
CloudABI is no longer being maintained. It was an awesome experiment,
but it never got enough traction to be sustainable.
There is no reason to keep it in FreeBSD.
Approved by: ed (private mail)
Reviewed by: emaste
Sponsored by: The FreeBSD Foundation
Differential revision: https://reviews.freebsd.org/D31923
Diffstat (limited to 'sys/compat/cloudabi')
-rw-r--r-- | sys/compat/cloudabi/cloudabi_clock.c | 135 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_errno.c | 123 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_fd.c | 475 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_file.c | 763 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_futex.c | 1165 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_mem.c | 167 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_proc.c | 148 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_proto.h | 36 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_random.c | 53 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_sock.c | 186 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_thread.c | 66 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_util.h | 87 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_vdso.c | 90 | ||||
-rw-r--r-- | sys/compat/cloudabi/cloudabi_vdso.lds | 51 |
14 files changed, 0 insertions, 3545 deletions
diff --git a/sys/compat/cloudabi/cloudabi_clock.c b/sys/compat/cloudabi/cloudabi_clock.c deleted file mode 100644 index 508640f950e5..000000000000 --- a/sys/compat/cloudabi/cloudabi_clock.c +++ /dev/null @@ -1,135 +0,0 @@ -/*- - * Copyright (c) 2015 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/param.h> -#include <sys/proc.h> -#include <sys/stdint.h> -#include <sys/systm.h> -#include <sys/syscallsubr.h> - -#include <contrib/cloudabi/cloudabi_types_common.h> - -#include <compat/cloudabi/cloudabi_proto.h> -#include <compat/cloudabi/cloudabi_util.h> - -/* Converts a CloudABI clock ID to a FreeBSD clock ID. */ -static int -cloudabi_convert_clockid(cloudabi_clockid_t in, clockid_t *out) -{ - switch (in) { - case CLOUDABI_CLOCK_MONOTONIC: - *out = CLOCK_MONOTONIC; - return (0); - case CLOUDABI_CLOCK_PROCESS_CPUTIME_ID: - *out = CLOCK_PROCESS_CPUTIME_ID; - return (0); - case CLOUDABI_CLOCK_REALTIME: - *out = CLOCK_REALTIME; - return (0); - case CLOUDABI_CLOCK_THREAD_CPUTIME_ID: - *out = CLOCK_THREAD_CPUTIME_ID; - return (0); - default: - return (EINVAL); - } -} - -/* Converts a struct timespec to a CloudABI timestamp. */ -int -cloudabi_convert_timespec(const struct timespec *in, cloudabi_timestamp_t *out) -{ - cloudabi_timestamp_t s, ns; - - if (in->tv_sec < 0) { - /* Timestamps from before the Epoch cannot be expressed. */ - *out = 0; - return (EOVERFLOW); - } - s = in->tv_sec; - ns = in->tv_nsec; - if (s > UINT64_MAX / 1000000000 || - (s == UINT64_MAX / 1000000000 && ns > UINT64_MAX % 1000000000)) { - /* Addition of seconds and nanoseconds would overflow. */ - *out = UINT64_MAX; - return (EOVERFLOW); - } - *out = s * 1000000000 + ns; - return (0); -} - -/* Fetches the time value of a clock. */ -int -cloudabi_clock_time_get(struct thread *td, cloudabi_clockid_t clock_id, - cloudabi_timestamp_t *ret) -{ - struct timespec ts; - int error; - clockid_t clockid; - - error = cloudabi_convert_clockid(clock_id, &clockid); - if (error != 0) - return (error); - error = kern_clock_gettime(td, clockid, &ts); - if (error != 0) - return (error); - return (cloudabi_convert_timespec(&ts, ret)); -} - -int -cloudabi_sys_clock_res_get(struct thread *td, - struct cloudabi_sys_clock_res_get_args *uap) -{ - struct timespec ts; - cloudabi_timestamp_t cts; - int error; - clockid_t clockid; - - error = cloudabi_convert_clockid(uap->clock_id, &clockid); - if (error != 0) - return (error); - error = kern_clock_getres(td, clockid, &ts); - if (error != 0) - return (error); - error = cloudabi_convert_timespec(&ts, &cts); - if (error != 0) - return (error); - memcpy(td->td_retval, &cts, sizeof(cts)); - return (0); -} - -int -cloudabi_sys_clock_time_get(struct thread *td, - struct cloudabi_sys_clock_time_get_args *uap) -{ - cloudabi_timestamp_t ts; - int error; - - error = cloudabi_clock_time_get(td, uap->clock_id, &ts); - memcpy(td->td_retval, &ts, sizeof(ts)); - return (error); -} diff --git a/sys/compat/cloudabi/cloudabi_errno.c b/sys/compat/cloudabi/cloudabi_errno.c deleted file mode 100644 index 10304bad4af5..000000000000 --- a/sys/compat/cloudabi/cloudabi_errno.c +++ /dev/null @@ -1,123 +0,0 @@ -/*- - * Copyright (c) 2015 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/param.h> - -#include <contrib/cloudabi/cloudabi_types_common.h> - -#include <compat/cloudabi/cloudabi_util.h> - -/* Converts a FreeBSD errno to a CloudABI errno. */ -cloudabi_errno_t -cloudabi_convert_errno(int error) -{ - static const int table[] = { - [E2BIG] = CLOUDABI_E2BIG, - [EACCES] = CLOUDABI_EACCES, - [EADDRINUSE] = CLOUDABI_EADDRINUSE, - [EADDRNOTAVAIL] = CLOUDABI_EADDRNOTAVAIL, - [EAFNOSUPPORT] = CLOUDABI_EAFNOSUPPORT, - [EAGAIN] = CLOUDABI_EAGAIN, - [EALREADY] = CLOUDABI_EALREADY, - [EBADF] = CLOUDABI_EBADF, - [EBADMSG] = CLOUDABI_EBADMSG, - [EBUSY] = CLOUDABI_EBUSY, - [ECANCELED] = CLOUDABI_ECANCELED, - [ECHILD] = CLOUDABI_ECHILD, - [ECONNABORTED] = CLOUDABI_ECONNABORTED, - [ECONNREFUSED] = CLOUDABI_ECONNREFUSED, - [ECONNRESET] = CLOUDABI_ECONNRESET, - [EDEADLK] = CLOUDABI_EDEADLK, - [EDESTADDRREQ] = CLOUDABI_EDESTADDRREQ, - [EDOM] = CLOUDABI_EDOM, - [EDQUOT] = CLOUDABI_EDQUOT, - [EEXIST] = CLOUDABI_EEXIST, - [EFAULT] = CLOUDABI_EFAULT, - [EFBIG] = CLOUDABI_EFBIG, - [EHOSTUNREACH] = CLOUDABI_EHOSTUNREACH, - [EIDRM] = CLOUDABI_EIDRM, - [EILSEQ] = CLOUDABI_EILSEQ, - [EINPROGRESS] = CLOUDABI_EINPROGRESS, - [EINTEGRITY] = CLOUDABI_EINVAL, - [EINTR] = CLOUDABI_EINTR, - [EINVAL] = CLOUDABI_EINVAL, - [EIO] = CLOUDABI_EIO, - [EISCONN] = CLOUDABI_EISCONN, - [EISDIR] = CLOUDABI_EISDIR, - [ELOOP] = CLOUDABI_ELOOP, - [EMFILE] = CLOUDABI_EMFILE, - [EMLINK] = CLOUDABI_EMLINK, - [EMSGSIZE] = CLOUDABI_EMSGSIZE, - [EMULTIHOP] = CLOUDABI_EMULTIHOP, - [ENAMETOOLONG] = CLOUDABI_ENAMETOOLONG, - [ENETDOWN] = CLOUDABI_ENETDOWN, - [ENETRESET] = CLOUDABI_ENETRESET, - [ENETUNREACH] = CLOUDABI_ENETUNREACH, - [ENFILE] = CLOUDABI_ENFILE, - [ENOBUFS] = CLOUDABI_ENOBUFS, - [ENODEV] = CLOUDABI_ENODEV, - [ENOENT] = CLOUDABI_ENOENT, - [ENOEXEC] = CLOUDABI_ENOEXEC, - [ENOLCK] = CLOUDABI_ENOLCK, - [ENOLINK] = CLOUDABI_ENOLINK, - [ENOMEM] = CLOUDABI_ENOMEM, - [ENOMSG] = CLOUDABI_ENOMSG, - [ENOPROTOOPT] = CLOUDABI_ENOPROTOOPT, - [ENOSPC] = CLOUDABI_ENOSPC, - [ENOSYS] = CLOUDABI_ENOSYS, - [ENOTCONN] = CLOUDABI_ENOTCONN, - [ENOTDIR] = CLOUDABI_ENOTDIR, - [ENOTEMPTY] = CLOUDABI_ENOTEMPTY, - [ENOTRECOVERABLE] = CLOUDABI_ENOTRECOVERABLE, - [ENOTSOCK] = CLOUDABI_ENOTSOCK, - [ENOTSUP] = CLOUDABI_ENOTSUP, - [ENOTTY] = CLOUDABI_ENOTTY, - [ENXIO] = CLOUDABI_ENXIO, - [EOVERFLOW] = CLOUDABI_EOVERFLOW, - [EOWNERDEAD] = CLOUDABI_EOWNERDEAD, - [EPERM] = CLOUDABI_EPERM, - [EPIPE] = CLOUDABI_EPIPE, - [EPROTO] = CLOUDABI_EPROTO, - [EPROTONOSUPPORT] = CLOUDABI_EPROTONOSUPPORT, - [EPROTOTYPE] = CLOUDABI_EPROTOTYPE, - [ERANGE] = CLOUDABI_ERANGE, - [EROFS] = CLOUDABI_EROFS, - [ESPIPE] = CLOUDABI_ESPIPE, - [ESRCH] = CLOUDABI_ESRCH, - [ESTALE] = CLOUDABI_ESTALE, - [ETIMEDOUT] = CLOUDABI_ETIMEDOUT, - [ETXTBSY] = CLOUDABI_ETXTBSY, - [EXDEV] = CLOUDABI_EXDEV, - [ENOTCAPABLE] = CLOUDABI_ENOTCAPABLE, - }; - - /* Unknown error: fall back to returning ENOSYS. */ - if (error < 0 || error >= nitems(table) || table[error] == 0) - return (error == 0 ? 0 : CLOUDABI_ENOSYS); - return (table[error]); -} diff --git a/sys/compat/cloudabi/cloudabi_fd.c b/sys/compat/cloudabi/cloudabi_fd.c deleted file mode 100644 index ba93db3c0bfd..000000000000 --- a/sys/compat/cloudabi/cloudabi_fd.c +++ /dev/null @@ -1,475 +0,0 @@ -/*- - * Copyright (c) 2015 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/param.h> -#include <sys/capsicum.h> -#include <sys/fcntl.h> -#include <sys/filedesc.h> -#include <sys/proc.h> -#include <sys/mman.h> -#include <sys/socketvar.h> -#include <sys/syscallsubr.h> -#include <sys/sysproto.h> -#include <sys/systm.h> -#include <sys/unistd.h> -#include <sys/vnode.h> - -#include <contrib/cloudabi/cloudabi_types_common.h> - -#include <compat/cloudabi/cloudabi_proto.h> -#include <compat/cloudabi/cloudabi_util.h> - -/* Translation between CloudABI and Capsicum rights. */ -#define RIGHTS_MAPPINGS \ - MAPPING(CLOUDABI_RIGHT_FD_DATASYNC, CAP_FSYNC) \ - MAPPING(CLOUDABI_RIGHT_FD_READ, CAP_READ) \ - MAPPING(CLOUDABI_RIGHT_FD_SEEK, CAP_SEEK) \ - MAPPING(CLOUDABI_RIGHT_FD_STAT_PUT_FLAGS, CAP_FCNTL) \ - MAPPING(CLOUDABI_RIGHT_FD_SYNC, CAP_FSYNC) \ - MAPPING(CLOUDABI_RIGHT_FD_TELL, CAP_SEEK_TELL) \ - MAPPING(CLOUDABI_RIGHT_FD_WRITE, CAP_WRITE) \ - MAPPING(CLOUDABI_RIGHT_FILE_ADVISE) \ - MAPPING(CLOUDABI_RIGHT_FILE_ALLOCATE, CAP_WRITE) \ - MAPPING(CLOUDABI_RIGHT_FILE_CREATE_DIRECTORY, CAP_MKDIRAT) \ - MAPPING(CLOUDABI_RIGHT_FILE_CREATE_FILE, CAP_CREATE) \ - MAPPING(CLOUDABI_RIGHT_FILE_LINK_SOURCE, CAP_LINKAT_SOURCE) \ - MAPPING(CLOUDABI_RIGHT_FILE_LINK_TARGET, CAP_LINKAT_TARGET) \ - MAPPING(CLOUDABI_RIGHT_FILE_OPEN, CAP_LOOKUP) \ - MAPPING(CLOUDABI_RIGHT_FILE_READDIR, CAP_READ) \ - MAPPING(CLOUDABI_RIGHT_FILE_READLINK, CAP_LOOKUP) \ - MAPPING(CLOUDABI_RIGHT_FILE_RENAME_SOURCE, CAP_RENAMEAT_SOURCE) \ - MAPPING(CLOUDABI_RIGHT_FILE_RENAME_TARGET, CAP_RENAMEAT_TARGET) \ - MAPPING(CLOUDABI_RIGHT_FILE_STAT_FGET, CAP_FSTAT) \ - MAPPING(CLOUDABI_RIGHT_FILE_STAT_FPUT_SIZE, CAP_FTRUNCATE) \ - MAPPING(CLOUDABI_RIGHT_FILE_STAT_FPUT_TIMES, CAP_FUTIMES) \ - MAPPING(CLOUDABI_RIGHT_FILE_STAT_GET, CAP_FSTATAT) \ - MAPPING(CLOUDABI_RIGHT_FILE_STAT_PUT_TIMES, CAP_FUTIMESAT) \ - MAPPING(CLOUDABI_RIGHT_FILE_SYMLINK, CAP_SYMLINKAT) \ - MAPPING(CLOUDABI_RIGHT_FILE_UNLINK, CAP_UNLINKAT) \ - MAPPING(CLOUDABI_RIGHT_MEM_MAP, CAP_MMAP) \ - MAPPING(CLOUDABI_RIGHT_MEM_MAP_EXEC, CAP_MMAP_X) \ - MAPPING(CLOUDABI_RIGHT_POLL_FD_READWRITE, CAP_EVENT) \ - MAPPING(CLOUDABI_RIGHT_POLL_PROC_TERMINATE, CAP_EVENT) \ - MAPPING(CLOUDABI_RIGHT_PROC_EXEC, CAP_FEXECVE) \ - MAPPING(CLOUDABI_RIGHT_SOCK_SHUTDOWN, CAP_SHUTDOWN) \ - -int -cloudabi_sys_fd_close(struct thread *td, struct cloudabi_sys_fd_close_args *uap) -{ - - return (kern_close(td, uap->fd)); -} - -int -cloudabi_sys_fd_create1(struct thread *td, - struct cloudabi_sys_fd_create1_args *uap) -{ - struct filecaps fcaps = {}; - - switch (uap->type) { - case CLOUDABI_FILETYPE_SHARED_MEMORY: - cap_rights_init(&fcaps.fc_rights, CAP_FSTAT, CAP_FTRUNCATE, - CAP_MMAP_RWX); - return (kern_shm_open(td, SHM_ANON, O_RDWR | O_CLOEXEC, 0, - &fcaps)); - default: - return (EINVAL); - } -} - -int -cloudabi_sys_fd_create2(struct thread *td, - struct cloudabi_sys_fd_create2_args *uap) -{ - int fds[2]; - int error, type; - - switch (uap->type) { - case CLOUDABI_FILETYPE_SOCKET_DGRAM: - type = SOCK_DGRAM; - break; - case CLOUDABI_FILETYPE_SOCKET_STREAM: - type = SOCK_STREAM; - break; - default: - return (EINVAL); - } - - error = kern_socketpair(td, AF_UNIX, type, 0, fds); - if (error == 0) { - td->td_retval[0] = fds[0]; - td->td_retval[1] = fds[1]; - } - return (0); -} - -int -cloudabi_sys_fd_datasync(struct thread *td, - struct cloudabi_sys_fd_datasync_args *uap) -{ - - return (kern_fsync(td, uap->fd, false)); -} - -int -cloudabi_sys_fd_dup(struct thread *td, struct cloudabi_sys_fd_dup_args *uap) -{ - - return (kern_dup(td, FDDUP_NORMAL, 0, uap->from, 0)); -} - -int -cloudabi_sys_fd_replace(struct thread *td, - struct cloudabi_sys_fd_replace_args *uap) -{ - int error; - - /* - * CloudABI's equivalent to dup2(). CloudABI processes should - * not depend on hardcoded file descriptor layouts, but simply - * use the file descriptor numbers that are allocated by the - * kernel. Duplicating file descriptors to arbitrary numbers - * should not be done. - * - * Invoke kern_dup() with FDDUP_MUSTREPLACE, so that we return - * EBADF when duplicating to a nonexistent file descriptor. Also - * clear the return value, as this system call yields no return - * value. - */ - error = kern_dup(td, FDDUP_MUSTREPLACE, 0, uap->from, uap->to); - td->td_retval[0] = 0; - return (error); -} - -int -cloudabi_sys_fd_seek(struct thread *td, struct cloudabi_sys_fd_seek_args *uap) -{ - int whence; - - switch (uap->whence) { - case CLOUDABI_WHENCE_CUR: - whence = SEEK_CUR; - break; - case CLOUDABI_WHENCE_END: - whence = SEEK_END; - break; - case CLOUDABI_WHENCE_SET: - whence = SEEK_SET; - break; - default: - return (EINVAL); - } - - return (kern_lseek(td, uap->fd, uap->offset, whence)); -} - -/* Converts a file descriptor to a CloudABI file descriptor type. */ -cloudabi_filetype_t -cloudabi_convert_filetype(const struct file *fp) -{ - struct socket *so; - struct vnode *vp; - - switch (fp->f_type) { - case DTYPE_FIFO: - return (CLOUDABI_FILETYPE_SOCKET_STREAM); - case DTYPE_PIPE: - return (CLOUDABI_FILETYPE_SOCKET_STREAM); - case DTYPE_PROCDESC: - return (CLOUDABI_FILETYPE_PROCESS); - case DTYPE_SHM: - return (CLOUDABI_FILETYPE_SHARED_MEMORY); - case DTYPE_SOCKET: - so = fp->f_data; - switch (so->so_type) { - case SOCK_DGRAM: - return (CLOUDABI_FILETYPE_SOCKET_DGRAM); - case SOCK_STREAM: - return (CLOUDABI_FILETYPE_SOCKET_STREAM); - default: - return (CLOUDABI_FILETYPE_UNKNOWN); - } - case DTYPE_VNODE: - vp = fp->f_vnode; - switch (vp->v_type) { - case VBLK: - return (CLOUDABI_FILETYPE_BLOCK_DEVICE); - case VCHR: - return (CLOUDABI_FILETYPE_CHARACTER_DEVICE); - case VDIR: - return (CLOUDABI_FILETYPE_DIRECTORY); - case VFIFO: - return (CLOUDABI_FILETYPE_SOCKET_STREAM); - case VLNK: - return (CLOUDABI_FILETYPE_SYMBOLIC_LINK); - case VREG: - return (CLOUDABI_FILETYPE_REGULAR_FILE); - case VSOCK: - return (CLOUDABI_FILETYPE_SOCKET_STREAM); - default: - return (CLOUDABI_FILETYPE_UNKNOWN); - } - default: - return (CLOUDABI_FILETYPE_UNKNOWN); - } -} - -/* Removes rights that conflict with the file descriptor type. */ -void -cloudabi_remove_conflicting_rights(cloudabi_filetype_t filetype, - cloudabi_rights_t *base, cloudabi_rights_t *inheriting) -{ - - /* - * CloudABI has a small number of additional rights bits to - * disambiguate between multiple purposes. Remove the bits that - * don't apply to the type of the file descriptor. - * - * As file descriptor access modes (O_ACCMODE) has been fully - * replaced by rights bits, CloudABI distinguishes between - * rights that apply to the file descriptor itself (base) versus - * rights of new file descriptors derived from them - * (inheriting). The code below approximates the pair by - * decomposing depending on the file descriptor type. - * - * We need to be somewhat accurate about which actions can - * actually be performed on the file descriptor, as functions - * like fcntl(fd, F_GETFL) are emulated on top of this. - */ - switch (filetype) { - case CLOUDABI_FILETYPE_DIRECTORY: - *base &= CLOUDABI_RIGHT_FD_STAT_PUT_FLAGS | - CLOUDABI_RIGHT_FD_SYNC | CLOUDABI_RIGHT_FILE_ADVISE | - CLOUDABI_RIGHT_FILE_CREATE_DIRECTORY | - CLOUDABI_RIGHT_FILE_CREATE_FILE | - CLOUDABI_RIGHT_FILE_LINK_SOURCE | - CLOUDABI_RIGHT_FILE_LINK_TARGET | - CLOUDABI_RIGHT_FILE_OPEN | - CLOUDABI_RIGHT_FILE_READDIR | - CLOUDABI_RIGHT_FILE_READLINK | - CLOUDABI_RIGHT_FILE_RENAME_SOURCE | - CLOUDABI_RIGHT_FILE_RENAME_TARGET | - CLOUDABI_RIGHT_FILE_STAT_FGET | - CLOUDABI_RIGHT_FILE_STAT_FPUT_TIMES | - CLOUDABI_RIGHT_FILE_STAT_GET | - CLOUDABI_RIGHT_FILE_STAT_PUT_TIMES | - CLOUDABI_RIGHT_FILE_SYMLINK | - CLOUDABI_RIGHT_FILE_UNLINK | - CLOUDABI_RIGHT_POLL_FD_READWRITE; - *inheriting &= CLOUDABI_RIGHT_FD_DATASYNC | - CLOUDABI_RIGHT_FD_READ | - CLOUDABI_RIGHT_FD_SEEK | - CLOUDABI_RIGHT_FD_STAT_PUT_FLAGS | - CLOUDABI_RIGHT_FD_SYNC | - CLOUDABI_RIGHT_FD_TELL | - CLOUDABI_RIGHT_FD_WRITE | - CLOUDABI_RIGHT_FILE_ADVISE | - CLOUDABI_RIGHT_FILE_ALLOCATE | - CLOUDABI_RIGHT_FILE_CREATE_DIRECTORY | - CLOUDABI_RIGHT_FILE_CREATE_FILE | - CLOUDABI_RIGHT_FILE_LINK_SOURCE | - CLOUDABI_RIGHT_FILE_LINK_TARGET | - CLOUDABI_RIGHT_FILE_OPEN | - CLOUDABI_RIGHT_FILE_READDIR | - CLOUDABI_RIGHT_FILE_READLINK | - CLOUDABI_RIGHT_FILE_RENAME_SOURCE | - CLOUDABI_RIGHT_FILE_RENAME_TARGET | - CLOUDABI_RIGHT_FILE_STAT_FGET | - CLOUDABI_RIGHT_FILE_STAT_FPUT_SIZE | - CLOUDABI_RIGHT_FILE_STAT_FPUT_TIMES | - CLOUDABI_RIGHT_FILE_STAT_GET | - CLOUDABI_RIGHT_FILE_STAT_PUT_TIMES | - CLOUDABI_RIGHT_FILE_SYMLINK | - CLOUDABI_RIGHT_FILE_UNLINK | - CLOUDABI_RIGHT_MEM_MAP | - CLOUDABI_RIGHT_MEM_MAP_EXEC | - CLOUDABI_RIGHT_POLL_FD_READWRITE | - CLOUDABI_RIGHT_PROC_EXEC; - break; - case CLOUDABI_FILETYPE_PROCESS: - *base &= ~(CLOUDABI_RIGHT_FILE_ADVISE | - CLOUDABI_RIGHT_POLL_FD_READWRITE); - *inheriting = 0; - break; - case CLOUDABI_FILETYPE_REGULAR_FILE: - *base &= CLOUDABI_RIGHT_FD_DATASYNC | - CLOUDABI_RIGHT_FD_READ | - CLOUDABI_RIGHT_FD_SEEK | - CLOUDABI_RIGHT_FD_STAT_PUT_FLAGS | - CLOUDABI_RIGHT_FD_SYNC | - CLOUDABI_RIGHT_FD_TELL | - CLOUDABI_RIGHT_FD_WRITE | - CLOUDABI_RIGHT_FILE_ADVISE | - CLOUDABI_RIGHT_FILE_ALLOCATE | - CLOUDABI_RIGHT_FILE_STAT_FGET | - CLOUDABI_RIGHT_FILE_STAT_FPUT_SIZE | - CLOUDABI_RIGHT_FILE_STAT_FPUT_TIMES | - CLOUDABI_RIGHT_MEM_MAP | - CLOUDABI_RIGHT_MEM_MAP_EXEC | - CLOUDABI_RIGHT_POLL_FD_READWRITE | - CLOUDABI_RIGHT_PROC_EXEC; - *inheriting = 0; - break; - case CLOUDABI_FILETYPE_SHARED_MEMORY: - *base &= ~(CLOUDABI_RIGHT_FD_SEEK | - CLOUDABI_RIGHT_FD_TELL | - CLOUDABI_RIGHT_FILE_ADVISE | - CLOUDABI_RIGHT_FILE_ALLOCATE | - CLOUDABI_RIGHT_FILE_READDIR); - *inheriting = 0; - break; - case CLOUDABI_FILETYPE_SOCKET_DGRAM: - case CLOUDABI_FILETYPE_SOCKET_STREAM: - *base &= CLOUDABI_RIGHT_FD_READ | - CLOUDABI_RIGHT_FD_STAT_PUT_FLAGS | - CLOUDABI_RIGHT_FD_WRITE | - CLOUDABI_RIGHT_FILE_STAT_FGET | - CLOUDABI_RIGHT_POLL_FD_READWRITE | - CLOUDABI_RIGHT_SOCK_SHUTDOWN; - break; - default: - *inheriting = 0; - break; - } -} - -/* Converts FreeBSD's Capsicum rights to CloudABI's set of rights. */ -static void -convert_capabilities(const cap_rights_t *capabilities, - cloudabi_filetype_t filetype, cloudabi_rights_t *base, - cloudabi_rights_t *inheriting) -{ - cloudabi_rights_t rights; - - /* Convert FreeBSD bits to CloudABI bits. */ - rights = 0; -#define MAPPING(cloudabi, ...) do { \ - if (cap_rights_is_set(capabilities, ##__VA_ARGS__)) \ - rights |= (cloudabi); \ -} while (0); - RIGHTS_MAPPINGS -#undef MAPPING - - *base = rights; - *inheriting = rights; - cloudabi_remove_conflicting_rights(filetype, base, inheriting); -} - -int -cloudabi_sys_fd_stat_get(struct thread *td, - struct cloudabi_sys_fd_stat_get_args *uap) -{ - cloudabi_fdstat_t fsb = {0}; - struct file *fp; - cap_rights_t rights; - struct filecaps fcaps; - int error, oflags; - - /* Obtain file descriptor properties. */ - error = fget_cap(td, uap->fd, cap_rights_init(&rights), &fp, - &fcaps); - if (error != 0) - return (error); - oflags = OFLAGS(fp->f_flag); - fsb.fs_filetype = cloudabi_convert_filetype(fp); - fdrop(fp, td); - - /* Convert file descriptor flags. */ - if (oflags & O_APPEND) - fsb.fs_flags |= CLOUDABI_FDFLAG_APPEND; - if (oflags & O_NONBLOCK) - fsb.fs_flags |= CLOUDABI_FDFLAG_NONBLOCK; - if (oflags & O_SYNC) - fsb.fs_flags |= CLOUDABI_FDFLAG_SYNC; - - /* Convert capabilities to CloudABI rights. */ - convert_capabilities(&fcaps.fc_rights, fsb.fs_filetype, - &fsb.fs_rights_base, &fsb.fs_rights_inheriting); - filecaps_free(&fcaps); - return (copyout(&fsb, (void *)uap->buf, sizeof(fsb))); -} - -/* Converts CloudABI rights to a set of Capsicum capabilities. */ -int -cloudabi_convert_rights(cloudabi_rights_t in, cap_rights_t *out) -{ - - cap_rights_init(out); -#define MAPPING(cloudabi, ...) do { \ - if (in & (cloudabi)) { \ - cap_rights_set(out, ##__VA_ARGS__); \ - in &= ~(cloudabi); \ - } \ -} while (0); - RIGHTS_MAPPINGS -#undef MAPPING - if (in != 0) - return (ENOTCAPABLE); - return (0); -} - -int -cloudabi_sys_fd_stat_put(struct thread *td, - struct cloudabi_sys_fd_stat_put_args *uap) -{ - cloudabi_fdstat_t fsb; - cap_rights_t rights; - int error, oflags; - - error = copyin(uap->buf, &fsb, sizeof(fsb)); - if (error != 0) - return (error); - - if (uap->flags == CLOUDABI_FDSTAT_FLAGS) { - /* Convert flags. */ - oflags = 0; - if (fsb.fs_flags & CLOUDABI_FDFLAG_APPEND) - oflags |= O_APPEND; - if (fsb.fs_flags & CLOUDABI_FDFLAG_NONBLOCK) - oflags |= O_NONBLOCK; - if (fsb.fs_flags & (CLOUDABI_FDFLAG_SYNC | - CLOUDABI_FDFLAG_DSYNC | CLOUDABI_FDFLAG_RSYNC)) - oflags |= O_SYNC; - return (kern_fcntl(td, uap->fd, F_SETFL, oflags)); - } else if (uap->flags == CLOUDABI_FDSTAT_RIGHTS) { - /* Convert rights. */ - error = cloudabi_convert_rights( - fsb.fs_rights_base | fsb.fs_rights_inheriting, &rights); - if (error != 0) - return (error); - return (kern_cap_rights_limit(td, uap->fd, &rights)); - } - return (EINVAL); -} - -int -cloudabi_sys_fd_sync(struct thread *td, struct cloudabi_sys_fd_sync_args *uap) -{ - - return (kern_fsync(td, uap->fd, true)); -} diff --git a/sys/compat/cloudabi/cloudabi_file.c b/sys/compat/cloudabi/cloudabi_file.c deleted file mode 100644 index 83987b79aa0c..000000000000 --- a/sys/compat/cloudabi/cloudabi_file.c +++ /dev/null @@ -1,763 +0,0 @@ -/*- - * Copyright (c) 2015 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/param.h> -#include <sys/capsicum.h> -#include <sys/dirent.h> -#include <sys/fcntl.h> -#include <sys/kernel.h> -#include <sys/malloc.h> -#include <sys/namei.h> -#include <sys/proc.h> -#include <sys/stat.h> -#include <sys/syscallsubr.h> -#include <sys/uio.h> -#include <sys/vnode.h> - -#include <contrib/cloudabi/cloudabi_types_common.h> - -#include <compat/cloudabi/cloudabi_proto.h> -#include <compat/cloudabi/cloudabi_util.h> - -#include <security/mac/mac_framework.h> - -static MALLOC_DEFINE(M_CLOUDABI_PATH, "cloudabipath", "CloudABI pathnames"); - -/* - * Copying pathnames from userspace to kernelspace. - * - * Unlike most operating systems, CloudABI doesn't use null-terminated - * pathname strings. Processes always pass pathnames to the kernel by - * providing a base pointer and a length. This has a couple of reasons: - * - * - It makes it easier to use CloudABI in combination with programming - * languages other than C, that may use non-null terminated strings. - * - It allows for calling system calls on individual components of the - * pathname without modifying the input string. - * - * The function below copies in pathname strings and null-terminates it. - * It also ensure that the string itself does not contain any null - * bytes. - * - * TODO(ed): Add an abstraction to vfs_lookup.c that allows us to pass - * in unterminated pathname strings, so we can do away with - * the copying. - */ - -static int -copyin_path(const char *uaddr, size_t len, char **result) -{ - char *buf; - int error; - - if (len >= PATH_MAX) - return (ENAMETOOLONG); - buf = malloc(len + 1, M_CLOUDABI_PATH, M_WAITOK); - error = copyin(uaddr, buf, len); - if (error != 0) { - free(buf, M_CLOUDABI_PATH); - return (error); - } - if (memchr(buf, '\0', len) != NULL) { - free(buf, M_CLOUDABI_PATH); - return (EINVAL); - } - buf[len] = '\0'; - *result = buf; - return (0); -} - -static void -cloudabi_freestr(char *buf) -{ - - free(buf, M_CLOUDABI_PATH); -} - -int -cloudabi_sys_file_advise(struct thread *td, - struct cloudabi_sys_file_advise_args *uap) -{ - int advice; - - switch (uap->advice) { - case CLOUDABI_ADVICE_DONTNEED: - advice = POSIX_FADV_DONTNEED; - break; - case CLOUDABI_ADVICE_NOREUSE: - advice = POSIX_FADV_NOREUSE; - break; - case CLOUDABI_ADVICE_NORMAL: - advice = POSIX_FADV_NORMAL; - break; - case CLOUDABI_ADVICE_RANDOM: - advice = POSIX_FADV_RANDOM; - break; - case CLOUDABI_ADVICE_SEQUENTIAL: - advice = POSIX_FADV_SEQUENTIAL; - break; - case CLOUDABI_ADVICE_WILLNEED: - advice = POSIX_FADV_WILLNEED; - break; - default: - return (EINVAL); - } - - return (kern_posix_fadvise(td, uap->fd, uap->offset, uap->len, advice)); -} - -int -cloudabi_sys_file_allocate(struct thread *td, - struct cloudabi_sys_file_allocate_args *uap) -{ - - return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len)); -} - -int -cloudabi_sys_file_create(struct thread *td, - struct cloudabi_sys_file_create_args *uap) -{ - char *path; - int error; - - error = copyin_path(uap->path, uap->path_len, &path); - if (error != 0) - return (error); - - /* - * CloudABI processes cannot interact with UNIX credentials and - * permissions. Depend on the umask that is set prior to - * execution to restrict the file permissions. - */ - switch (uap->type) { - case CLOUDABI_FILETYPE_DIRECTORY: - error = kern_mkdirat(td, uap->fd, path, UIO_SYSSPACE, 0777); - break; - default: - error = EINVAL; - break; - } - cloudabi_freestr(path); - return (error); -} - -int -cloudabi_sys_file_link(struct thread *td, - struct cloudabi_sys_file_link_args *uap) -{ - char *path1, *path2; - int error; - - error = copyin_path(uap->path1, uap->path1_len, &path1); - if (error != 0) - return (error); - error = copyin_path(uap->path2, uap->path2_len, &path2); - if (error != 0) { - cloudabi_freestr(path1); - return (error); - } - - error = kern_linkat(td, uap->fd1.fd, uap->fd2, path1, path2, - UIO_SYSSPACE, (uap->fd1.flags & CLOUDABI_LOOKUP_SYMLINK_FOLLOW) ? - AT_SYMLINK_FOLLOW : 0); - cloudabi_freestr(path1); - cloudabi_freestr(path2); - return (error); -} - -int -cloudabi_sys_file_open(struct thread *td, - struct cloudabi_sys_file_open_args *uap) -{ - cloudabi_fdstat_t fds; - cap_rights_t rights; - struct filecaps fcaps = {}; - struct nameidata nd; - struct file *fp; - struct vnode *vp; - char *path; - int error, fd, fflags; - bool read, write; - - error = copyin(uap->fds, &fds, sizeof(fds)); - if (error != 0) - return (error); - - /* All the requested rights should be set on the descriptor. */ - error = cloudabi_convert_rights( - fds.fs_rights_base | fds.fs_rights_inheriting, &rights); - if (error != 0) - return (error); - cap_rights_set_one(&rights, CAP_LOOKUP); - - /* Convert rights to corresponding access mode. */ - read = (fds.fs_rights_base & (CLOUDABI_RIGHT_FD_READ | - CLOUDABI_RIGHT_FILE_READDIR | CLOUDABI_RIGHT_MEM_MAP_EXEC)) != 0; - write = (fds.fs_rights_base & (CLOUDABI_RIGHT_FD_DATASYNC | - CLOUDABI_RIGHT_FD_WRITE | CLOUDABI_RIGHT_FILE_ALLOCATE | - CLOUDABI_RIGHT_FILE_STAT_FPUT_SIZE)) != 0; - fflags = write ? read ? FREAD | FWRITE : FWRITE : FREAD; - - /* Convert open flags. */ - if ((uap->oflags & CLOUDABI_O_CREAT) != 0) { - fflags |= O_CREAT; - cap_rights_set_one(&rights, CAP_CREATE); - } - if ((uap->oflags & CLOUDABI_O_DIRECTORY) != 0) - fflags |= O_DIRECTORY; - if ((uap->oflags & CLOUDABI_O_EXCL) != 0) - fflags |= O_EXCL; - if ((uap->oflags & CLOUDABI_O_TRUNC) != 0) { - fflags |= O_TRUNC; - cap_rights_set_one(&rights, CAP_FTRUNCATE); - } - if ((fds.fs_flags & CLOUDABI_FDFLAG_APPEND) != 0) - fflags |= O_APPEND; - if ((fds.fs_flags & CLOUDABI_FDFLAG_NONBLOCK) != 0) - fflags |= O_NONBLOCK; - if ((fds.fs_flags & (CLOUDABI_FDFLAG_SYNC | CLOUDABI_FDFLAG_DSYNC | - CLOUDABI_FDFLAG_RSYNC)) != 0) { - fflags |= O_SYNC; - cap_rights_set_one(&rights, CAP_FSYNC); - } - if ((uap->dirfd.flags & CLOUDABI_LOOKUP_SYMLINK_FOLLOW) == 0) - fflags |= O_NOFOLLOW; - if (write && (fflags & (O_APPEND | O_TRUNC)) == 0) - cap_rights_set_one(&rights, CAP_SEEK); - - /* Allocate new file descriptor. */ - error = falloc_noinstall(td, &fp); - if (error != 0) - return (error); - fp->f_flag = fflags & FMASK; - - /* Open path. */ - error = copyin_path(uap->path, uap->path_len, &path); - if (error != 0) { - fdrop(fp, td); - return (error); - } - NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, uap->dirfd.fd, - &rights, td); - error = vn_open(&nd, &fflags, 0777 & ~td->td_proc->p_pd->pd_cmask, fp); - cloudabi_freestr(path); - if (error != 0) { - /* Custom operations provided. */ - if (error == ENXIO && fp->f_ops != &badfileops) - goto success; - - /* - * POSIX compliance: return ELOOP in case openat() is - * called on a symbolic link and O_NOFOLLOW is set. - */ - if (error == EMLINK) - error = ELOOP; - fdrop(fp, td); - return (error); - } - NDFREE(&nd, NDF_ONLY_PNBUF); - filecaps_free(&nd.ni_filecaps); - fp->f_vnode = vp = nd.ni_vp; - - /* Install vnode operations if no custom operations are provided. */ - if (fp->f_ops == &badfileops) { - fp->f_seqcount[UIO_READ] = 1; - fp->f_seqcount[UIO_WRITE] = 1; - finit(fp, (fflags & FMASK) | (fp->f_flag & FHASLOCK), - DTYPE_VNODE, vp, &vnops); - } - VOP_UNLOCK(vp); - - /* Truncate file. */ - if (fflags & O_TRUNC) { - error = fo_truncate(fp, 0, td->td_ucred, td); - if (error != 0) { - fdrop(fp, td); - return (error); - } - } - -success: - /* Determine which Capsicum rights to set on the file descriptor. */ - cloudabi_remove_conflicting_rights(cloudabi_convert_filetype(fp), - &fds.fs_rights_base, &fds.fs_rights_inheriting); - cloudabi_convert_rights(fds.fs_rights_base | fds.fs_rights_inheriting, - &fcaps.fc_rights); - if (cap_rights_is_set(&fcaps.fc_rights)) - fcaps.fc_fcntls = CAP_FCNTL_SETFL; - - error = finstall(td, fp, &fd, fflags, &fcaps); - fdrop(fp, td); - if (error != 0) - return (error); - td->td_retval[0] = fd; - return (0); -} - -/* Converts a FreeBSD directory entry structure and writes it to userspace. */ -static int -write_dirent(struct dirent *bde, cloudabi_dircookie_t cookie, struct uio *uio) -{ - cloudabi_dirent_t cde = { - .d_next = cookie, - .d_ino = bde->d_fileno, - .d_namlen = bde->d_namlen, - }; - size_t len; - int error; - - /* Convert file type. */ - switch (bde->d_type) { - case DT_BLK: - cde.d_type = CLOUDABI_FILETYPE_BLOCK_DEVICE; - break; - case DT_CHR: - cde.d_type = CLOUDABI_FILETYPE_CHARACTER_DEVICE; - break; - case DT_DIR: - cde.d_type = CLOUDABI_FILETYPE_DIRECTORY; - break; - case DT_FIFO: - cde.d_type = CLOUDABI_FILETYPE_SOCKET_STREAM; - break; - case DT_LNK: - cde.d_type = CLOUDABI_FILETYPE_SYMBOLIC_LINK; - break; - case DT_REG: - cde.d_type = CLOUDABI_FILETYPE_REGULAR_FILE; - break; - case DT_SOCK: - /* The exact socket type cannot be derived. */ - cde.d_type = CLOUDABI_FILETYPE_SOCKET_STREAM; - break; - default: - cde.d_type = CLOUDABI_FILETYPE_UNKNOWN; - break; - } - - /* Write directory entry structure. */ - len = sizeof(cde) < uio->uio_resid ? sizeof(cde) : uio->uio_resid; - error = uiomove(&cde, len, uio); - if (error != 0) - return (error); - - /* Write filename. */ - len = bde->d_namlen < uio->uio_resid ? bde->d_namlen : uio->uio_resid; - return (uiomove(bde->d_name, len, uio)); -} - -int -cloudabi_sys_file_readdir(struct thread *td, - struct cloudabi_sys_file_readdir_args *uap) -{ - struct iovec iov = { - .iov_base = uap->buf, - .iov_len = uap->buf_len - }; - struct uio uio = { - .uio_iov = &iov, - .uio_iovcnt = 1, - .uio_resid = iov.iov_len, - .uio_segflg = UIO_USERSPACE, - .uio_rw = UIO_READ, - .uio_td = td - }; - struct file *fp; - struct vnode *vp; - void *readbuf; - cloudabi_dircookie_t offset; - int error; - - /* Obtain directory vnode. */ - error = getvnode(td, uap->fd, &cap_read_rights, &fp); - if (error != 0) { - if (error == EINVAL) - return (ENOTDIR); - return (error); - } - if ((fp->f_flag & FREAD) == 0) { - fdrop(fp, td); - return (EBADF); - } - - /* - * Call VOP_READDIR() and convert resulting data until the user - * provided buffer is filled. - */ - readbuf = malloc(MAXBSIZE, M_TEMP, M_WAITOK); - offset = uap->cookie; - vp = fp->f_vnode; - while (uio.uio_resid > 0) { - struct iovec readiov = { - .iov_base = readbuf, - .iov_len = MAXBSIZE - }; - struct uio readuio = { - .uio_iov = &readiov, - .uio_iovcnt = 1, - .uio_rw = UIO_READ, - .uio_segflg = UIO_SYSSPACE, - .uio_td = td, - .uio_resid = MAXBSIZE, - .uio_offset = offset - }; - struct dirent *bde; - unsigned long *cookies, *cookie; - size_t readbuflen; - int eof, ncookies; - - /* Validate file type. */ - vn_lock(vp, LK_SHARED | LK_RETRY); - if (vp->v_type != VDIR) { - VOP_UNLOCK(vp); - error = ENOTDIR; - goto done; - } -#ifdef MAC - error = mac_vnode_check_readdir(td->td_ucred, vp); - if (error != 0) { - VOP_UNLOCK(vp); - goto done; - } -#endif /* MAC */ - - /* Read new directory entries. */ - cookies = NULL; - ncookies = 0; - error = VOP_READDIR(vp, &readuio, fp->f_cred, &eof, - &ncookies, &cookies); - VOP_UNLOCK(vp); - if (error != 0) - goto done; - - /* Convert entries to CloudABI's format. */ - readbuflen = MAXBSIZE - readuio.uio_resid; - bde = readbuf; - cookie = cookies; - while (readbuflen >= offsetof(struct dirent, d_name) && - uio.uio_resid > 0 && ncookies > 0) { - /* Ensure that the returned offset always increases. */ - if (readbuflen >= bde->d_reclen && bde->d_fileno != 0 && - *cookie > offset) { - error = write_dirent(bde, *cookie, &uio); - if (error != 0) { - free(cookies, M_TEMP); - goto done; - } - } - - if (offset < *cookie) - offset = *cookie; - ++cookie; - --ncookies; - readbuflen -= bde->d_reclen; - bde = (struct dirent *)((char *)bde + bde->d_reclen); - } - free(cookies, M_TEMP); - if (eof) - break; - } - -done: - fdrop(fp, td); - free(readbuf, M_TEMP); - if (error != 0) - return (error); - - /* Return number of bytes copied to userspace. */ - td->td_retval[0] = uap->buf_len - uio.uio_resid; - return (0); -} - -int -cloudabi_sys_file_readlink(struct thread *td, - struct cloudabi_sys_file_readlink_args *uap) -{ - char *path; - int error; - - error = copyin_path(uap->path, uap->path_len, &path); - if (error != 0) - return (error); - - error = kern_readlinkat(td, uap->fd, path, UIO_SYSSPACE, - uap->buf, UIO_USERSPACE, uap->buf_len); - cloudabi_freestr(path); - return (error); -} - -int -cloudabi_sys_file_rename(struct thread *td, - struct cloudabi_sys_file_rename_args *uap) -{ - char *old, *new; - int error; - - error = copyin_path(uap->path1, uap->path1_len, &old); - if (error != 0) - return (error); - error = copyin_path(uap->path2, uap->path2_len, &new); - if (error != 0) { - cloudabi_freestr(old); - return (error); - } - - error = kern_renameat(td, uap->fd1, old, uap->fd2, new, - UIO_SYSSPACE); - cloudabi_freestr(old); - cloudabi_freestr(new); - return (error); -} - -/* Converts a FreeBSD stat structure to a CloudABI stat structure. */ -static void -convert_stat(const struct stat *sb, cloudabi_filestat_t *csb) -{ - cloudabi_filestat_t res = { - .st_dev = sb->st_dev, - .st_ino = sb->st_ino, - .st_nlink = sb->st_nlink, - .st_size = sb->st_size, - }; - - cloudabi_convert_timespec(&sb->st_atim, &res.st_atim); - cloudabi_convert_timespec(&sb->st_mtim, &res.st_mtim); - cloudabi_convert_timespec(&sb->st_ctim, &res.st_ctim); - *csb = res; -} - -int -cloudabi_sys_file_stat_fget(struct thread *td, - struct cloudabi_sys_file_stat_fget_args *uap) -{ - struct stat sb; - cloudabi_filestat_t csb; - struct file *fp; - cloudabi_filetype_t filetype; - int error; - - memset(&csb, 0, sizeof(csb)); - - /* Fetch file descriptor attributes. */ - error = fget(td, uap->fd, &cap_fstat_rights, &fp); - if (error != 0) - return (error); - error = fo_stat(fp, &sb, td->td_ucred, td); - if (error != 0) { - fdrop(fp, td); - return (error); - } - filetype = cloudabi_convert_filetype(fp); - fdrop(fp, td); - - /* Convert attributes to CloudABI's format. */ - convert_stat(&sb, &csb); - csb.st_filetype = filetype; - return (copyout(&csb, uap->buf, sizeof(csb))); -} - -/* Converts timestamps to arguments to futimens() and utimensat(). */ -static void -convert_utimens_arguments(const cloudabi_filestat_t *fs, - cloudabi_fsflags_t flags, struct timespec *ts) -{ - - if ((flags & CLOUDABI_FILESTAT_ATIM_NOW) != 0) { - ts[0].tv_nsec = UTIME_NOW; - } else if ((flags & CLOUDABI_FILESTAT_ATIM) != 0) { - ts[0].tv_sec = fs->st_atim / 1000000000; - ts[0].tv_nsec = fs->st_atim % 1000000000; - } else { - ts[0].tv_nsec = UTIME_OMIT; - } - - if ((flags & CLOUDABI_FILESTAT_MTIM_NOW) != 0) { - ts[1].tv_nsec = UTIME_NOW; - } else if ((flags & CLOUDABI_FILESTAT_MTIM) != 0) { - ts[1].tv_sec = fs->st_mtim / 1000000000; - ts[1].tv_nsec = fs->st_mtim % 1000000000; - } else { - ts[1].tv_nsec = UTIME_OMIT; - } -} - -int -cloudabi_sys_file_stat_fput(struct thread *td, - struct cloudabi_sys_file_stat_fput_args *uap) -{ - cloudabi_filestat_t fs; - struct timespec ts[2]; - int error; - - error = copyin(uap->buf, &fs, sizeof(fs)); - if (error != 0) - return (error); - - /* - * Only support truncation and timestamp modification separately - * for now, to prevent unnecessary code duplication. - */ - if ((uap->flags & CLOUDABI_FILESTAT_SIZE) != 0) { - /* Call into kern_ftruncate() for file truncation. */ - if ((uap->flags & ~CLOUDABI_FILESTAT_SIZE) != 0) - return (EINVAL); - return (kern_ftruncate(td, uap->fd, fs.st_size)); - } else if ((uap->flags & (CLOUDABI_FILESTAT_ATIM | - CLOUDABI_FILESTAT_ATIM_NOW | CLOUDABI_FILESTAT_MTIM | - CLOUDABI_FILESTAT_MTIM_NOW)) != 0) { - /* Call into kern_futimens() for timestamp modification. */ - if ((uap->flags & ~(CLOUDABI_FILESTAT_ATIM | - CLOUDABI_FILESTAT_ATIM_NOW | CLOUDABI_FILESTAT_MTIM | - CLOUDABI_FILESTAT_MTIM_NOW)) != 0) - return (EINVAL); - convert_utimens_arguments(&fs, uap->flags, ts); - return (kern_futimens(td, uap->fd, ts, UIO_SYSSPACE)); - } - return (EINVAL); -} - -int -cloudabi_sys_file_stat_get(struct thread *td, - struct cloudabi_sys_file_stat_get_args *uap) -{ - struct stat sb; - cloudabi_filestat_t csb; - char *path; - int error; - - memset(&csb, 0, sizeof(csb)); - - error = copyin_path(uap->path, uap->path_len, &path); - if (error != 0) - return (error); - - error = kern_statat(td, - (uap->fd.flags & CLOUDABI_LOOKUP_SYMLINK_FOLLOW) != 0 ? 0 : - AT_SYMLINK_NOFOLLOW, uap->fd.fd, path, UIO_SYSSPACE, &sb, NULL); - cloudabi_freestr(path); - if (error != 0) - return (error); - - /* Convert results and return them. */ - convert_stat(&sb, &csb); - if (S_ISBLK(sb.st_mode)) - csb.st_filetype = CLOUDABI_FILETYPE_BLOCK_DEVICE; - else if (S_ISCHR(sb.st_mode)) - csb.st_filetype = CLOUDABI_FILETYPE_CHARACTER_DEVICE; - else if (S_ISDIR(sb.st_mode)) - csb.st_filetype = CLOUDABI_FILETYPE_DIRECTORY; - else if (S_ISFIFO(sb.st_mode)) - csb.st_filetype = CLOUDABI_FILETYPE_SOCKET_STREAM; - else if (S_ISREG(sb.st_mode)) - csb.st_filetype = CLOUDABI_FILETYPE_REGULAR_FILE; - else if (S_ISSOCK(sb.st_mode)) { - /* Inaccurate, but the best that we can do. */ - csb.st_filetype = CLOUDABI_FILETYPE_SOCKET_STREAM; - } else if (S_ISLNK(sb.st_mode)) - csb.st_filetype = CLOUDABI_FILETYPE_SYMBOLIC_LINK; - else - csb.st_filetype = CLOUDABI_FILETYPE_UNKNOWN; - return (copyout(&csb, uap->buf, sizeof(csb))); -} - -int -cloudabi_sys_file_stat_put(struct thread *td, - struct cloudabi_sys_file_stat_put_args *uap) -{ - cloudabi_filestat_t fs; - struct timespec ts[2]; - char *path; - int error; - - /* - * Only support timestamp modification for now, as there is no - * truncateat(). - */ - if ((uap->flags & ~(CLOUDABI_FILESTAT_ATIM | - CLOUDABI_FILESTAT_ATIM_NOW | CLOUDABI_FILESTAT_MTIM | - CLOUDABI_FILESTAT_MTIM_NOW)) != 0) - return (EINVAL); - - error = copyin(uap->buf, &fs, sizeof(fs)); - if (error != 0) - return (error); - error = copyin_path(uap->path, uap->path_len, &path); - if (error != 0) - return (error); - - convert_utimens_arguments(&fs, uap->flags, ts); - error = kern_utimensat(td, uap->fd.fd, path, UIO_SYSSPACE, ts, - UIO_SYSSPACE, (uap->fd.flags & CLOUDABI_LOOKUP_SYMLINK_FOLLOW) ? - 0 : AT_SYMLINK_NOFOLLOW); - cloudabi_freestr(path); - return (error); -} - -int -cloudabi_sys_file_symlink(struct thread *td, - struct cloudabi_sys_file_symlink_args *uap) -{ - char *path1, *path2; - int error; - - error = copyin_path(uap->path1, uap->path1_len, &path1); - if (error != 0) - return (error); - error = copyin_path(uap->path2, uap->path2_len, &path2); - if (error != 0) { - cloudabi_freestr(path1); - return (error); - } - - error = kern_symlinkat(td, path1, uap->fd, path2, UIO_SYSSPACE); - cloudabi_freestr(path1); - cloudabi_freestr(path2); - return (error); -} - -int -cloudabi_sys_file_unlink(struct thread *td, - struct cloudabi_sys_file_unlink_args *uap) -{ - char *path; - int error; - - error = copyin_path(uap->path, uap->path_len, &path); - if (error != 0) - return (error); - - if (uap->flags & CLOUDABI_UNLINK_REMOVEDIR) - error = kern_frmdirat(td, uap->fd, path, FD_NONE, - UIO_SYSSPACE, 0); - else - error = kern_funlinkat(td, uap->fd, path, FD_NONE, - UIO_SYSSPACE, 0, 0); - cloudabi_freestr(path); - return (error); -} diff --git a/sys/compat/cloudabi/cloudabi_futex.c b/sys/compat/cloudabi/cloudabi_futex.c deleted file mode 100644 index fd2a1d515483..000000000000 --- a/sys/compat/cloudabi/cloudabi_futex.c +++ /dev/null @@ -1,1165 +0,0 @@ -/*- - * Copyright (c) 2015 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/param.h> -#include <sys/kernel.h> -#include <sys/limits.h> -#include <sys/lock.h> -#include <sys/malloc.h> -#include <sys/mutex.h> -#include <sys/proc.h> -#include <sys/sx.h> -#include <sys/systm.h> -#include <sys/umtxvar.h> - -#include <contrib/cloudabi/cloudabi_types_common.h> - -#include <compat/cloudabi/cloudabi_proto.h> -#include <compat/cloudabi/cloudabi_util.h> - -/* - * Futexes for CloudABI. - * - * On most systems, futexes are implemented as objects of a single type - * on which a set of operations can be performed. CloudABI makes a clear - * distinction between locks and condition variables. A lock may have - * zero or more associated condition variables. A condition variable is - * always associated with exactly one lock. There is a strict topology. - * This approach has two advantages: - * - * - This topology is guaranteed to be acyclic. Requeueing of threads - * only happens in one direction (from condition variables to locks). - * This eases locking. - * - It means that a futex object for a lock exists when it is unlocked, - * but has threads waiting on associated condition variables. Threads - * can be requeued to a lock even if the thread performing the wakeup - * does not have the lock mapped in its address space. - * - * This futex implementation only implements a single lock type, namely - * a read-write lock. A regular mutex type would not be necessary, as - * the read-write lock is as efficient as a mutex if used as such. - * Userspace futex locks are 32 bits in size: - * - * - 1 bit: has threads waiting in kernel-space. - * - 1 bit: is write-locked. - * - 30 bits: - * - if write-locked: thread ID of owner. - * - if not write-locked: number of read locks held. - * - * Condition variables are also 32 bits in size. Its value is modified - * by kernel-space exclusively. Zero indicates that it has no waiting - * threads. Non-zero indicates the opposite. - * - * This implementation is optimal, in the sense that it only wakes up - * threads if they can actually continue execution. It does not suffer - * from the thundering herd problem. If multiple threads waiting on a - * condition variable need to be woken up, only a single thread is - * scheduled. All other threads are 'donated' to this thread. After the - * thread manages to reacquire the lock, it requeues its donated threads - * to the lock. - * - * TODO(ed): Integrate this functionality into kern_umtx.c instead. - * TODO(ed): Store futex objects in a hash table. - * TODO(ed): Add actual priority inheritance. - * TODO(ed): Let futex_queue also take priorities into account. - * TODO(ed): Make locking fine-grained. - * TODO(ed): Perform sleeps until an actual absolute point in time, - * instead of converting the timestamp to a relative value. - */ - -struct futex_address; -struct futex_condvar; -struct futex_lock; -struct futex_queue; -struct futex_waiter; - -/* Identifier of a location in memory. */ -struct futex_address { - struct umtx_key fa_key; -}; - -/* A set of waiting threads. */ -struct futex_queue { - STAILQ_HEAD(, futex_waiter) fq_list; - unsigned int fq_count; -}; - -/* Condition variables. */ -struct futex_condvar { - /* Address of the condition variable. */ - struct futex_address fc_address; - - /* The lock the waiters should be moved to when signalled. */ - struct futex_lock * fc_lock; - - /* Threads waiting on the condition variable. */ - struct futex_queue fc_waiters; - /* - * Number of threads blocked on this condition variable, or - * being blocked on the lock after being requeued. - */ - unsigned int fc_waitcount; - - /* Global list pointers. */ - LIST_ENTRY(futex_condvar) fc_next; -}; - -/* Read-write locks. */ -struct futex_lock { - /* Address of the lock. */ - struct futex_address fl_address; - - /* - * Current owner of the lock. LOCK_UNMANAGED if the lock is - * currently not owned by the kernel. LOCK_OWNER_UNKNOWN in case - * the owner is not known (e.g., when the lock is read-locked). - */ - cloudabi_tid_t fl_owner; -#define LOCK_UNMANAGED 0x0 -#define LOCK_OWNER_UNKNOWN 0x1 - - /* Writers blocked on the lock. */ - struct futex_queue fl_writers; - /* Readers blocked on the lock. */ - struct futex_queue fl_readers; - /* Number of threads blocked on this lock + condition variables. */ - unsigned int fl_waitcount; - - /* Global list pointers. */ - LIST_ENTRY(futex_lock) fl_next; -}; - -/* Information associated with a thread blocked on an object. */ -struct futex_waiter { - /* Thread ID. */ - cloudabi_tid_t fw_tid; - /* Condition variable used for waiting. */ - struct cv fw_wait; - - /* Queue this waiter is currently placed in. */ - struct futex_queue * fw_queue; - /* List pointers of fw_queue. */ - STAILQ_ENTRY(futex_waiter) fw_next; - - /* Lock has been acquired. */ - bool fw_locked; - /* If not locked, threads that should block after acquiring. */ - struct futex_queue fw_donated; -}; - -/* Global data structures. */ -static MALLOC_DEFINE(M_FUTEX, "futex", "CloudABI futex"); - -static struct sx futex_global_lock; -SX_SYSINIT(futex_global_lock, &futex_global_lock, "CloudABI futex global lock"); - -static LIST_HEAD(, futex_lock) futex_lock_list = - LIST_HEAD_INITIALIZER(&futex_lock_list); -static LIST_HEAD(, futex_condvar) futex_condvar_list = - LIST_HEAD_INITIALIZER(&futex_condvar_list); - -/* Utility functions. */ -static void futex_lock_assert(const struct futex_lock *); -static struct futex_lock *futex_lock_lookup_locked(struct futex_address *); -static void futex_lock_release(struct futex_lock *); -static int futex_lock_tryrdlock(struct futex_lock *, cloudabi_lock_t *); -static int futex_lock_unmanage(struct futex_lock *, cloudabi_lock_t *); -static int futex_lock_update_owner(struct futex_lock *, cloudabi_lock_t *); -static int futex_lock_wake_up_next(struct futex_lock *, cloudabi_lock_t *); -static unsigned int futex_queue_count(const struct futex_queue *); -static void futex_queue_init(struct futex_queue *); -static void futex_queue_requeue(struct futex_queue *, struct futex_queue *, - unsigned int); -static int futex_queue_sleep(struct futex_queue *, struct futex_lock *, - struct futex_waiter *, struct thread *, cloudabi_clockid_t, - cloudabi_timestamp_t, cloudabi_timestamp_t, bool); -static cloudabi_tid_t futex_queue_tid_best(const struct futex_queue *); -static void futex_queue_wake_up_all(struct futex_queue *); -static void futex_queue_wake_up_best(struct futex_queue *); -static void futex_queue_wake_up_donate(struct futex_queue *, unsigned int); -static int futex_user_load(uint32_t *, uint32_t *); -static int futex_user_store(uint32_t *, uint32_t); -static int futex_user_cmpxchg(uint32_t *, uint32_t, uint32_t *, uint32_t); - -/* - * futex_address operations. - */ - -static int -futex_address_create(struct futex_address *fa, struct thread *td, - const void *object, cloudabi_scope_t scope) -{ - - KASSERT(td == curthread, - ("Can only create umtx keys for the current thread")); - switch (scope) { - case CLOUDABI_SCOPE_PRIVATE: - return (umtx_key_get(object, TYPE_FUTEX, THREAD_SHARE, - &fa->fa_key)); - case CLOUDABI_SCOPE_SHARED: - return (umtx_key_get(object, TYPE_FUTEX, AUTO_SHARE, - &fa->fa_key)); - default: - return (EINVAL); - } -} - -static void -futex_address_free(struct futex_address *fa) -{ - - umtx_key_release(&fa->fa_key); -} - -static bool -futex_address_match(const struct futex_address *fa1, - const struct futex_address *fa2) -{ - - return (umtx_key_match(&fa1->fa_key, &fa2->fa_key)); -} - -/* - * futex_condvar operations. - */ - -static void -futex_condvar_assert(const struct futex_condvar *fc) -{ - - KASSERT(fc->fc_waitcount >= futex_queue_count(&fc->fc_waiters), - ("Total number of waiters cannot be smaller than the wait queue")); - futex_lock_assert(fc->fc_lock); -} - -static int -futex_condvar_lookup(struct thread *td, const cloudabi_condvar_t *address, - cloudabi_scope_t scope, struct futex_condvar **fcret) -{ - struct futex_address fa_condvar; - struct futex_condvar *fc; - int error; - - error = futex_address_create(&fa_condvar, td, address, scope); - if (error != 0) - return (error); - - sx_xlock(&futex_global_lock); - LIST_FOREACH(fc, &futex_condvar_list, fc_next) { - if (futex_address_match(&fc->fc_address, &fa_condvar)) { - /* Found matching lock object. */ - futex_address_free(&fa_condvar); - futex_condvar_assert(fc); - *fcret = fc; - return (0); - } - } - sx_xunlock(&futex_global_lock); - futex_address_free(&fa_condvar); - return (ENOENT); -} - -static int -futex_condvar_lookup_or_create(struct thread *td, - const cloudabi_condvar_t *condvar, cloudabi_scope_t condvar_scope, - const cloudabi_lock_t *lock, cloudabi_scope_t lock_scope, - struct futex_condvar **fcret) -{ - struct futex_address fa_condvar, fa_lock; - struct futex_condvar *fc; - struct futex_lock *fl; - int error; - - error = futex_address_create(&fa_condvar, td, condvar, condvar_scope); - if (error != 0) - return (error); - error = futex_address_create(&fa_lock, td, lock, lock_scope); - if (error != 0) { - futex_address_free(&fa_condvar); - return (error); - } - - sx_xlock(&futex_global_lock); - LIST_FOREACH(fc, &futex_condvar_list, fc_next) { - if (!futex_address_match(&fc->fc_address, &fa_condvar)) - continue; - fl = fc->fc_lock; - if (!futex_address_match(&fl->fl_address, &fa_lock)) { - /* Condition variable is owned by a different lock. */ - futex_address_free(&fa_condvar); - futex_address_free(&fa_lock); - sx_xunlock(&futex_global_lock); - return (EINVAL); - } - - /* Found fully matching condition variable. */ - futex_address_free(&fa_condvar); - futex_address_free(&fa_lock); - futex_condvar_assert(fc); - *fcret = fc; - return (0); - } - - /* None found. Create new condition variable object. */ - fc = malloc(sizeof(*fc), M_FUTEX, M_WAITOK); - fc->fc_address = fa_condvar; - fc->fc_lock = futex_lock_lookup_locked(&fa_lock); - futex_queue_init(&fc->fc_waiters); - fc->fc_waitcount = 0; - LIST_INSERT_HEAD(&futex_condvar_list, fc, fc_next); - *fcret = fc; - return (0); -} - -static void -futex_condvar_release(struct futex_condvar *fc) -{ - struct futex_lock *fl; - - futex_condvar_assert(fc); - fl = fc->fc_lock; - if (fc->fc_waitcount == 0) { - /* Condition variable has no waiters. Deallocate it. */ - futex_address_free(&fc->fc_address); - LIST_REMOVE(fc, fc_next); - free(fc, M_FUTEX); - } - futex_lock_release(fl); -} - -static int -futex_condvar_unmanage(struct futex_condvar *fc, - cloudabi_condvar_t *condvar) -{ - - if (futex_queue_count(&fc->fc_waiters) != 0) - return (0); - return (futex_user_store(condvar, CLOUDABI_CONDVAR_HAS_NO_WAITERS)); -} - -/* - * futex_lock operations. - */ - -static void -futex_lock_assert(const struct futex_lock *fl) -{ - - /* - * A futex lock can only be kernel-managed if it has waiters. - * Vice versa: if a futex lock has waiters, it must be - * kernel-managed. - */ - KASSERT((fl->fl_owner == LOCK_UNMANAGED) == - (futex_queue_count(&fl->fl_readers) == 0 && - futex_queue_count(&fl->fl_writers) == 0), - ("Managed locks must have waiting threads")); - KASSERT(fl->fl_waitcount != 0 || fl->fl_owner == LOCK_UNMANAGED, - ("Lock with no waiters must be unmanaged")); -} - -static int -futex_lock_lookup(struct thread *td, const cloudabi_lock_t *address, - cloudabi_scope_t scope, struct futex_lock **flret) -{ - struct futex_address fa; - int error; - - error = futex_address_create(&fa, td, address, scope); - if (error != 0) - return (error); - - sx_xlock(&futex_global_lock); - *flret = futex_lock_lookup_locked(&fa); - return (0); -} - -static struct futex_lock * -futex_lock_lookup_locked(struct futex_address *fa) -{ - struct futex_lock *fl; - - LIST_FOREACH(fl, &futex_lock_list, fl_next) { - if (futex_address_match(&fl->fl_address, fa)) { - /* Found matching lock object. */ - futex_address_free(fa); - futex_lock_assert(fl); - return (fl); - } - } - - /* None found. Create new lock object. */ - fl = malloc(sizeof(*fl), M_FUTEX, M_WAITOK); - fl->fl_address = *fa; - fl->fl_owner = LOCK_UNMANAGED; - futex_queue_init(&fl->fl_readers); - futex_queue_init(&fl->fl_writers); - fl->fl_waitcount = 0; - LIST_INSERT_HEAD(&futex_lock_list, fl, fl_next); - return (fl); -} - -static int -futex_lock_rdlock(struct futex_lock *fl, struct thread *td, - cloudabi_lock_t *lock, cloudabi_clockid_t clock_id, - cloudabi_timestamp_t timeout, cloudabi_timestamp_t precision, bool abstime) -{ - struct futex_waiter fw; - int error; - - error = futex_lock_tryrdlock(fl, lock); - if (error == EBUSY) { - /* Suspend execution. */ - KASSERT(fl->fl_owner != LOCK_UNMANAGED, - ("Attempted to sleep on an unmanaged lock")); - error = futex_queue_sleep(&fl->fl_readers, fl, &fw, td, - clock_id, timeout, precision, abstime); - KASSERT((error == 0) == fw.fw_locked, - ("Should have locked write lock on success")); - KASSERT(futex_queue_count(&fw.fw_donated) == 0, - ("Lock functions cannot receive threads")); - } - if (error != 0) - futex_lock_unmanage(fl, lock); - return (error); -} - -static void -futex_lock_release(struct futex_lock *fl) -{ - - futex_lock_assert(fl); - if (fl->fl_waitcount == 0) { - /* Lock object is unreferenced. Deallocate it. */ - KASSERT(fl->fl_owner == LOCK_UNMANAGED, - ("Attempted to free a managed lock")); - futex_address_free(&fl->fl_address); - LIST_REMOVE(fl, fl_next); - free(fl, M_FUTEX); - } - sx_xunlock(&futex_global_lock); -} - -static int -futex_lock_unmanage(struct futex_lock *fl, cloudabi_lock_t *lock) -{ - cloudabi_lock_t cmp, old; - int error; - - if (futex_queue_count(&fl->fl_readers) == 0 && - futex_queue_count(&fl->fl_writers) == 0) { - /* Lock should be unmanaged. */ - fl->fl_owner = LOCK_UNMANAGED; - - /* Clear kernel-managed bit. */ - error = futex_user_load(lock, &old); - if (error != 0) - return (error); - for (;;) { - cmp = old; - error = futex_user_cmpxchg(lock, cmp, &old, - cmp & ~CLOUDABI_LOCK_KERNEL_MANAGED); - if (error != 0) - return (error); - if (old == cmp) - break; - } - } - return (0); -} - -/* Sets an owner of a lock, based on a userspace lock value. */ -static void -futex_lock_set_owner(struct futex_lock *fl, cloudabi_lock_t lock) -{ - - /* Lock has no explicit owner. */ - if ((lock & ~CLOUDABI_LOCK_WRLOCKED) == 0) { - fl->fl_owner = LOCK_OWNER_UNKNOWN; - return; - } - lock &= ~(CLOUDABI_LOCK_WRLOCKED | CLOUDABI_LOCK_KERNEL_MANAGED); - - /* Don't allow userspace to silently unlock. */ - if (lock == LOCK_UNMANAGED) { - fl->fl_owner = LOCK_OWNER_UNKNOWN; - return; - } - fl->fl_owner = lock; -} - -static int -futex_lock_unlock(struct futex_lock *fl, struct thread *td, - cloudabi_lock_t *lock) -{ - int error; - - /* Validate that this thread is allowed to unlock. */ - error = futex_lock_update_owner(fl, lock); - if (error != 0) - return (error); - if (fl->fl_owner != LOCK_UNMANAGED && fl->fl_owner != td->td_tid) - return (EPERM); - return (futex_lock_wake_up_next(fl, lock)); -} - -/* Syncs in the owner of the lock from userspace if needed. */ -static int -futex_lock_update_owner(struct futex_lock *fl, cloudabi_lock_t *address) -{ - cloudabi_lock_t lock; - int error; - - if (fl->fl_owner == LOCK_OWNER_UNKNOWN) { - error = futex_user_load(address, &lock); - if (error != 0) - return (error); - futex_lock_set_owner(fl, lock); - } - return (0); -} - -static int -futex_lock_tryrdlock(struct futex_lock *fl, cloudabi_lock_t *address) -{ - cloudabi_lock_t old, cmp; - int error; - - if (fl->fl_owner != LOCK_UNMANAGED) { - /* Lock is already acquired. */ - return (EBUSY); - } - - old = CLOUDABI_LOCK_UNLOCKED; - for (;;) { - if ((old & CLOUDABI_LOCK_KERNEL_MANAGED) != 0) { - /* - * Userspace lock is kernel-managed, even though - * the kernel disagrees. - */ - return (EINVAL); - } - - if ((old & CLOUDABI_LOCK_WRLOCKED) == 0) { - /* - * Lock is not write-locked. Attempt to acquire - * it by increasing the read count. - */ - cmp = old; - error = futex_user_cmpxchg(address, cmp, &old, cmp + 1); - if (error != 0) - return (error); - if (old == cmp) { - /* Success. */ - return (0); - } - } else { - /* Lock is write-locked. Make it kernel-managed. */ - cmp = old; - error = futex_user_cmpxchg(address, cmp, &old, - cmp | CLOUDABI_LOCK_KERNEL_MANAGED); - if (error != 0) - return (error); - if (old == cmp) { - /* Success. */ - futex_lock_set_owner(fl, cmp); - return (EBUSY); - } - } - } -} - -static int -futex_lock_trywrlock(struct futex_lock *fl, cloudabi_lock_t *address, - cloudabi_tid_t tid, bool force_kernel_managed) -{ - cloudabi_lock_t old, new, cmp; - int error; - - if (fl->fl_owner == tid) { - /* Attempted to acquire lock recursively. */ - return (EDEADLK); - } - if (fl->fl_owner != LOCK_UNMANAGED) { - /* Lock is already acquired. */ - return (EBUSY); - } - - old = CLOUDABI_LOCK_UNLOCKED; - for (;;) { - if ((old & CLOUDABI_LOCK_KERNEL_MANAGED) != 0) { - /* - * Userspace lock is kernel-managed, even though - * the kernel disagrees. - */ - return (EINVAL); - } - if (old == (tid | CLOUDABI_LOCK_WRLOCKED)) { - /* Attempted to acquire lock recursively. */ - return (EDEADLK); - } - - if (old == CLOUDABI_LOCK_UNLOCKED) { - /* Lock is unlocked. Attempt to acquire it. */ - new = tid | CLOUDABI_LOCK_WRLOCKED; - if (force_kernel_managed) - new |= CLOUDABI_LOCK_KERNEL_MANAGED; - error = futex_user_cmpxchg(address, - CLOUDABI_LOCK_UNLOCKED, &old, new); - if (error != 0) - return (error); - if (old == CLOUDABI_LOCK_UNLOCKED) { - /* Success. */ - if (force_kernel_managed) - fl->fl_owner = tid; - return (0); - } - } else { - /* Lock is still locked. Make it kernel-managed. */ - cmp = old; - error = futex_user_cmpxchg(address, cmp, &old, - cmp | CLOUDABI_LOCK_KERNEL_MANAGED); - if (error != 0) - return (error); - if (old == cmp) { - /* Success. */ - futex_lock_set_owner(fl, cmp); - return (EBUSY); - } - } - } -} - -static int -futex_lock_wake_up_next(struct futex_lock *fl, cloudabi_lock_t *lock) -{ - cloudabi_tid_t tid; - int error; - - /* - * Determine which thread(s) to wake up. Prefer waking up - * writers over readers to prevent write starvation. - */ - if (futex_queue_count(&fl->fl_writers) > 0) { - /* Transfer ownership to a single write-locker. */ - if (futex_queue_count(&fl->fl_writers) > 1 || - futex_queue_count(&fl->fl_readers) > 0) { - /* Lock should remain managed afterwards. */ - tid = futex_queue_tid_best(&fl->fl_writers); - error = futex_user_store(lock, - tid | CLOUDABI_LOCK_WRLOCKED | - CLOUDABI_LOCK_KERNEL_MANAGED); - if (error != 0) - return (error); - - futex_queue_wake_up_best(&fl->fl_writers); - fl->fl_owner = tid; - } else { - /* Lock can become unmanaged afterwards. */ - error = futex_user_store(lock, - futex_queue_tid_best(&fl->fl_writers) | - CLOUDABI_LOCK_WRLOCKED); - if (error != 0) - return (error); - - futex_queue_wake_up_best(&fl->fl_writers); - fl->fl_owner = LOCK_UNMANAGED; - } - } else { - /* Transfer ownership to all read-lockers (if any). */ - error = futex_user_store(lock, - futex_queue_count(&fl->fl_readers)); - if (error != 0) - return (error); - - /* Wake up all threads. */ - futex_queue_wake_up_all(&fl->fl_readers); - fl->fl_owner = LOCK_UNMANAGED; - } - return (0); -} - -static int -futex_lock_wrlock(struct futex_lock *fl, struct thread *td, - cloudabi_lock_t *lock, cloudabi_clockid_t clock_id, - cloudabi_timestamp_t timeout, cloudabi_timestamp_t precision, bool abstime, - struct futex_queue *donated) -{ - struct futex_waiter fw; - int error; - - error = futex_lock_trywrlock(fl, lock, td->td_tid, - futex_queue_count(donated) > 0); - - if (error == 0 || error == EBUSY) { - /* Put donated threads in queue before suspending. */ - KASSERT(futex_queue_count(donated) == 0 || - fl->fl_owner != LOCK_UNMANAGED, - ("Lock should be managed if we are going to donate")); - futex_queue_requeue(donated, &fl->fl_writers, UINT_MAX); - } else { - /* - * This thread cannot deal with the donated threads. - * Wake up the next thread and let it try it by itself. - */ - futex_queue_wake_up_donate(donated, UINT_MAX); - } - - if (error == EBUSY) { - /* Suspend execution if the lock was busy. */ - KASSERT(fl->fl_owner != LOCK_UNMANAGED, - ("Attempted to sleep on an unmanaged lock")); - error = futex_queue_sleep(&fl->fl_writers, fl, &fw, td, - clock_id, timeout, precision, abstime); - KASSERT((error == 0) == fw.fw_locked, - ("Should have locked write lock on success")); - KASSERT(futex_queue_count(&fw.fw_donated) == 0, - ("Lock functions cannot receive threads")); - } - if (error != 0) - futex_lock_unmanage(fl, lock); - return (error); -} - -/* - * futex_queue operations. - */ - -static cloudabi_tid_t -futex_queue_tid_best(const struct futex_queue *fq) -{ - - return (STAILQ_FIRST(&fq->fq_list)->fw_tid); -} - -static unsigned int -futex_queue_count(const struct futex_queue *fq) -{ - - return (fq->fq_count); -} - -static void -futex_queue_init(struct futex_queue *fq) -{ - - STAILQ_INIT(&fq->fq_list); - fq->fq_count = 0; -} - -/* Converts a relative timestamp to an sbintime. */ -static sbintime_t -futex_queue_convert_timestamp_relative(cloudabi_timestamp_t ts) -{ - cloudabi_timestamp_t s, ns; - - s = ts / 1000000000; - ns = ts % 1000000000; - if (s > INT32_MAX) - return (INT64_MAX); - return ((s << 32) + (ns << 32) / 1000000000); -} - -/* Converts an absolute timestamp and precision to a pair of sbintime values. */ -static int -futex_queue_convert_timestamp(struct thread *td, cloudabi_clockid_t clock_id, - cloudabi_timestamp_t timeout, cloudabi_timestamp_t precision, - sbintime_t *sbttimeout, sbintime_t *sbtprecision, bool abstime) -{ - cloudabi_timestamp_t now; - int error; - - if (abstime) { - /* Make the time relative. */ - error = cloudabi_clock_time_get(td, clock_id, &now); - if (error != 0) - return (error); - timeout = timeout < now ? 0 : timeout - now; - } - - *sbttimeout = futex_queue_convert_timestamp_relative(timeout); - *sbtprecision = futex_queue_convert_timestamp_relative(precision); - return (0); -} - -static int -futex_queue_sleep(struct futex_queue *fq, struct futex_lock *fl, - struct futex_waiter *fw, struct thread *td, cloudabi_clockid_t clock_id, - cloudabi_timestamp_t timeout, cloudabi_timestamp_t precision, bool abstime) -{ - sbintime_t sbttimeout, sbtprecision; - int error; - - /* Initialize futex_waiter object. */ - fw->fw_tid = td->td_tid; - fw->fw_locked = false; - futex_queue_init(&fw->fw_donated); - - if (timeout != UINT64_MAX) { - /* Convert timeout duration. */ - error = futex_queue_convert_timestamp(td, clock_id, timeout, - precision, &sbttimeout, &sbtprecision, abstime); - if (error != 0) - return (error); - } - - /* Place object in the queue. */ - fw->fw_queue = fq; - STAILQ_INSERT_TAIL(&fq->fq_list, fw, fw_next); - ++fq->fq_count; - - cv_init(&fw->fw_wait, "futex"); - ++fl->fl_waitcount; - - futex_lock_assert(fl); - if (timeout == UINT64_MAX) { - /* Wait without a timeout. */ - error = cv_wait_sig(&fw->fw_wait, &futex_global_lock); - } else { - /* Wait respecting the timeout. */ - error = cv_timedwait_sig_sbt(&fw->fw_wait, &futex_global_lock, - sbttimeout, sbtprecision, 0); - futex_lock_assert(fl); - if (error == EWOULDBLOCK && - fw->fw_queue != NULL && fw->fw_queue != fq) { - /* - * We got signalled on a condition variable, but - * observed a timeout while waiting to reacquire - * the lock. In other words, we didn't actually - * time out. Go back to sleep and wait for the - * lock to be reacquired. - */ - error = cv_wait_sig(&fw->fw_wait, &futex_global_lock); - } - } - futex_lock_assert(fl); - - --fl->fl_waitcount; - cv_destroy(&fw->fw_wait); - - fq = fw->fw_queue; - if (fq == NULL) { - /* Thread got dequeued, so we've slept successfully. */ - return (0); - } - - /* Thread is still enqueued. Remove it. */ - KASSERT(error != 0, ("Woken up thread is still enqueued")); - STAILQ_REMOVE(&fq->fq_list, fw, futex_waiter, fw_next); - --fq->fq_count; - return (error == EWOULDBLOCK ? ETIMEDOUT : error); -} - -/* Moves up to nwaiters waiters from one queue to another. */ -static void -futex_queue_requeue(struct futex_queue *fqfrom, struct futex_queue *fqto, - unsigned int nwaiters) -{ - struct futex_waiter *fw; - - /* Move waiters to the target queue. */ - while (nwaiters-- > 0 && !STAILQ_EMPTY(&fqfrom->fq_list)) { - fw = STAILQ_FIRST(&fqfrom->fq_list); - STAILQ_REMOVE_HEAD(&fqfrom->fq_list, fw_next); - --fqfrom->fq_count; - - fw->fw_queue = fqto; - STAILQ_INSERT_TAIL(&fqto->fq_list, fw, fw_next); - ++fqto->fq_count; - } -} - -/* Wakes up all waiters in a queue. */ -static void -futex_queue_wake_up_all(struct futex_queue *fq) -{ - struct futex_waiter *fw; - - STAILQ_FOREACH(fw, &fq->fq_list, fw_next) { - fw->fw_locked = true; - fw->fw_queue = NULL; - cv_signal(&fw->fw_wait); - } - - STAILQ_INIT(&fq->fq_list); - fq->fq_count = 0; -} - -/* - * Wakes up the best waiter (i.e., the waiter having the highest - * priority) in a queue. - */ -static void -futex_queue_wake_up_best(struct futex_queue *fq) -{ - struct futex_waiter *fw; - - fw = STAILQ_FIRST(&fq->fq_list); - fw->fw_locked = true; - fw->fw_queue = NULL; - cv_signal(&fw->fw_wait); - - STAILQ_REMOVE_HEAD(&fq->fq_list, fw_next); - --fq->fq_count; -} - -static void -futex_queue_wake_up_donate(struct futex_queue *fq, unsigned int nwaiters) -{ - struct futex_waiter *fw; - - fw = STAILQ_FIRST(&fq->fq_list); - if (fw == NULL) - return; - fw->fw_locked = false; - fw->fw_queue = NULL; - cv_signal(&fw->fw_wait); - - STAILQ_REMOVE_HEAD(&fq->fq_list, fw_next); - --fq->fq_count; - futex_queue_requeue(fq, &fw->fw_donated, nwaiters); -} - -/* - * futex_user operations. Used to adjust values in userspace. - */ - -static int -futex_user_load(uint32_t *obj, uint32_t *val) -{ - - return (fueword32(obj, val) != 0 ? EFAULT : 0); -} - -static int -futex_user_store(uint32_t *obj, uint32_t val) -{ - - return (suword32(obj, val) != 0 ? EFAULT : 0); -} - -static int -futex_user_cmpxchg(uint32_t *obj, uint32_t cmp, uint32_t *old, uint32_t new) -{ - - return (casueword32(obj, cmp, old, new) != 0 ? EFAULT : 0); -} - -/* - * Blocking calls: acquiring locks, waiting on condition variables. - */ - -int -cloudabi_futex_condvar_wait(struct thread *td, cloudabi_condvar_t *condvar, - cloudabi_scope_t condvar_scope, cloudabi_lock_t *lock, - cloudabi_scope_t lock_scope, cloudabi_clockid_t clock_id, - cloudabi_timestamp_t timeout, cloudabi_timestamp_t precision, bool abstime) -{ - struct futex_condvar *fc; - struct futex_lock *fl; - struct futex_waiter fw; - int error, error2; - - /* Lookup condition variable object. */ - error = futex_condvar_lookup_or_create(td, condvar, condvar_scope, lock, - lock_scope, &fc); - if (error != 0) - return (error); - fl = fc->fc_lock; - - /* - * Set the condition variable to something other than - * CLOUDABI_CONDVAR_HAS_NO_WAITERS to make userspace threads - * call into the kernel to perform wakeups. - */ - error = futex_user_store(condvar, ~CLOUDABI_CONDVAR_HAS_NO_WAITERS); - if (error != 0) { - futex_condvar_release(fc); - return (error); - } - - /* Drop the lock. */ - error = futex_lock_unlock(fl, td, lock); - if (error != 0) { - futex_condvar_unmanage(fc, condvar); - futex_condvar_release(fc); - return (error); - } - - /* Go to sleep. */ - ++fc->fc_waitcount; - error = futex_queue_sleep(&fc->fc_waiters, fc->fc_lock, &fw, td, - clock_id, timeout, precision, abstime); - if (fw.fw_locked) { - /* Waited and got the lock assigned to us. */ - KASSERT(futex_queue_count(&fw.fw_donated) == 0, - ("Received threads while being locked")); - } else if (error == 0 || error == ETIMEDOUT) { - if (error != 0) - futex_condvar_unmanage(fc, condvar); - /* - * Got woken up without having the lock assigned to us. - * This can happen in two cases: - * - * 1. We observed a timeout on a condition variable. - * 2. We got signalled on a condition variable while the - * associated lock is unlocked. We are the first - * thread that gets woken up. This thread is - * responsible for reacquiring the userspace lock. - */ - error2 = futex_lock_wrlock(fl, td, lock, - CLOUDABI_CLOCK_MONOTONIC, UINT64_MAX, 0, abstime, - &fw.fw_donated); - if (error2 != 0) - error = error2; - } else { - KASSERT(futex_queue_count(&fw.fw_donated) == 0, - ("Received threads on error")); - futex_condvar_unmanage(fc, condvar); - futex_lock_unmanage(fl, lock); - } - --fc->fc_waitcount; - futex_condvar_release(fc); - return (error); -} - -int -cloudabi_futex_lock_rdlock(struct thread *td, cloudabi_lock_t *lock, - cloudabi_scope_t scope, cloudabi_clockid_t clock_id, - cloudabi_timestamp_t timeout, cloudabi_timestamp_t precision, bool abstime) -{ - struct futex_lock *fl; - int error; - - /* Look up lock object. */ - error = futex_lock_lookup(td, lock, scope, &fl); - if (error != 0) - return (error); - - error = futex_lock_rdlock(fl, td, lock, clock_id, timeout, - precision, abstime); - futex_lock_release(fl); - return (error); -} - -int -cloudabi_futex_lock_wrlock(struct thread *td, cloudabi_lock_t *lock, - cloudabi_scope_t scope, cloudabi_clockid_t clock_id, - cloudabi_timestamp_t timeout, cloudabi_timestamp_t precision, bool abstime) -{ - struct futex_lock *fl; - struct futex_queue fq; - int error; - - /* Look up lock object. */ - error = futex_lock_lookup(td, lock, scope, &fl); - if (error != 0) - return (error); - - futex_queue_init(&fq); - error = futex_lock_wrlock(fl, td, lock, clock_id, timeout, - precision, abstime, &fq); - futex_lock_release(fl); - return (error); -} - -/* - * Non-blocking calls: releasing locks, signalling condition variables. - */ - -int -cloudabi_sys_condvar_signal(struct thread *td, - struct cloudabi_sys_condvar_signal_args *uap) -{ - struct futex_condvar *fc; - struct futex_lock *fl; - cloudabi_nthreads_t nwaiters; - int error; - - nwaiters = uap->nwaiters; - if (nwaiters == 0) { - /* No threads to wake up. */ - return (0); - } - - /* Look up futex object. */ - error = futex_condvar_lookup(td, uap->condvar, uap->scope, &fc); - if (error != 0) { - /* Race condition: condition variable with no waiters. */ - return (error == ENOENT ? 0 : error); - } - fl = fc->fc_lock; - - if (fl->fl_owner == LOCK_UNMANAGED) { - /* - * The lock is currently not managed by the kernel, - * meaning we must attempt to acquire the userspace lock - * first. We cannot requeue threads to an unmanaged lock, - * as these threads will then never be scheduled. - * - * Unfortunately, the memory address of the lock is - * unknown from this context, meaning that we cannot - * acquire the lock on behalf of the first thread to be - * scheduled. The lock may even not be mapped within the - * address space of the current thread. - * - * To solve this, wake up a single waiter that will - * attempt to acquire the lock. Donate all of the other - * waiters that need to be woken up to this waiter, so - * it can requeue them after acquiring the lock. - */ - futex_queue_wake_up_donate(&fc->fc_waiters, nwaiters - 1); - } else { - /* - * Lock is already managed by the kernel. This makes it - * easy, as we can requeue the threads from the - * condition variable directly to the associated lock. - */ - futex_queue_requeue(&fc->fc_waiters, &fl->fl_writers, nwaiters); - } - - /* Clear userspace condition variable if all waiters are gone. */ - error = futex_condvar_unmanage(fc, uap->condvar); - futex_condvar_release(fc); - return (error); -} - -int -cloudabi_sys_lock_unlock(struct thread *td, - struct cloudabi_sys_lock_unlock_args *uap) -{ - struct futex_lock *fl; - int error; - - error = futex_lock_lookup(td, uap->lock, uap->scope, &fl); - if (error != 0) - return (error); - error = futex_lock_unlock(fl, td, uap->lock); - futex_lock_release(fl); - return (error); -} diff --git a/sys/compat/cloudabi/cloudabi_mem.c b/sys/compat/cloudabi/cloudabi_mem.c deleted file mode 100644 index 426adb6ff43f..000000000000 --- a/sys/compat/cloudabi/cloudabi_mem.c +++ /dev/null @@ -1,167 +0,0 @@ -/*- - * Copyright (c) 2015 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/param.h> -#include <sys/mman.h> -#include <sys/proc.h> -#include <sys/syscallsubr.h> - -#include <contrib/cloudabi/cloudabi_types_common.h> - -#include <compat/cloudabi/cloudabi_proto.h> - -/* Converts CloudABI's memory protection flags to FreeBSD's. */ -static int -convert_mprot(cloudabi_mprot_t in, int *out) -{ - - /* Unknown protection flags. */ - if ((in & ~(CLOUDABI_PROT_EXEC | CLOUDABI_PROT_WRITE | - CLOUDABI_PROT_READ)) != 0) - return (ENOTSUP); - /* W^X: Write and exec cannot be enabled at the same time. */ - if ((in & (CLOUDABI_PROT_EXEC | CLOUDABI_PROT_WRITE)) == - (CLOUDABI_PROT_EXEC | CLOUDABI_PROT_WRITE)) - return (ENOTSUP); - - *out = 0; - if (in & CLOUDABI_PROT_EXEC) - *out |= PROT_EXEC; - if (in & CLOUDABI_PROT_WRITE) - *out |= PROT_WRITE; - if (in & CLOUDABI_PROT_READ) - *out |= PROT_READ; - return (0); -} - -int -cloudabi_sys_mem_advise(struct thread *td, - struct cloudabi_sys_mem_advise_args *uap) -{ - int behav; - - switch (uap->advice) { - case CLOUDABI_ADVICE_DONTNEED: - behav = MADV_DONTNEED; - break; - case CLOUDABI_ADVICE_NORMAL: - behav = MADV_NORMAL; - break; - case CLOUDABI_ADVICE_RANDOM: - behav = MADV_RANDOM; - break; - case CLOUDABI_ADVICE_SEQUENTIAL: - behav = MADV_SEQUENTIAL; - break; - case CLOUDABI_ADVICE_WILLNEED: - behav = MADV_WILLNEED; - break; - default: - return (EINVAL); - } - - return (kern_madvise(td, (uintptr_t)uap->mapping, uap->mapping_len, - behav)); -} - -int -cloudabi_sys_mem_map(struct thread *td, struct cloudabi_sys_mem_map_args *uap) -{ - int error, flags, prot; - - /* Translate flags. */ - flags = 0; - if (uap->flags & CLOUDABI_MAP_ANON) - flags |= MAP_ANON; - if (uap->flags & CLOUDABI_MAP_FIXED) - flags |= MAP_FIXED; - if (uap->flags & CLOUDABI_MAP_PRIVATE) - flags |= MAP_PRIVATE; - if (uap->flags & CLOUDABI_MAP_SHARED) - flags |= MAP_SHARED; - - /* Translate protection. */ - error = convert_mprot(uap->prot, &prot); - if (error != 0) - return (error); - - return (kern_mmap(td, &(struct mmap_req){ - .mr_hint = (uintptr_t)uap->addr, - .mr_len = uap->len, - .mr_prot = prot, - .mr_flags = flags, - .mr_fd = uap->fd, - .mr_pos = uap->off, - })); -} - -int -cloudabi_sys_mem_protect(struct thread *td, - struct cloudabi_sys_mem_protect_args *uap) -{ - int error, prot; - - /* Translate protection. */ - error = convert_mprot(uap->prot, &prot); - if (error != 0) - return (error); - - return (kern_mprotect(td, (uintptr_t)uap->mapping, uap->mapping_len, - prot)); -} - -int -cloudabi_sys_mem_sync(struct thread *td, struct cloudabi_sys_mem_sync_args *uap) -{ - int flags; - - /* Convert flags. */ - switch (uap->flags & (CLOUDABI_MS_ASYNC | CLOUDABI_MS_SYNC)) { - case CLOUDABI_MS_ASYNC: - flags = MS_ASYNC; - break; - case CLOUDABI_MS_SYNC: - flags = MS_SYNC; - break; - default: - return (EINVAL); - } - if ((uap->flags & CLOUDABI_MS_INVALIDATE) != 0) - flags |= MS_INVALIDATE; - - return (kern_msync(td, (uintptr_t)uap->mapping, uap->mapping_len, - flags)); -} - -int -cloudabi_sys_mem_unmap(struct thread *td, - struct cloudabi_sys_mem_unmap_args *uap) -{ - - return (kern_munmap(td, (uintptr_t)uap->mapping, uap->mapping_len)); -} diff --git a/sys/compat/cloudabi/cloudabi_proc.c b/sys/compat/cloudabi/cloudabi_proc.c deleted file mode 100644 index 378409edaeff..000000000000 --- a/sys/compat/cloudabi/cloudabi_proc.c +++ /dev/null @@ -1,148 +0,0 @@ -/*- - * Copyright (c) 2015 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/param.h> -#include <sys/capsicum.h> -#include <sys/filedesc.h> -#include <sys/imgact.h> -#include <sys/lock.h> -#include <sys/module.h> -#include <sys/mutex.h> -#include <sys/proc.h> -#include <sys/signalvar.h> -#include <sys/syscallsubr.h> -#include <sys/unistd.h> - -#include <contrib/cloudabi/cloudabi_types_common.h> - -#include <compat/cloudabi/cloudabi_proto.h> - -int -cloudabi_sys_proc_exec(struct thread *td, - struct cloudabi_sys_proc_exec_args *uap) -{ - struct image_args args; - struct vmspace *oldvmspace; - int error; - - error = pre_execve(td, &oldvmspace); - if (error != 0) - return (error); - error = exec_copyin_data_fds(td, &args, uap->data, uap->data_len, - uap->fds, uap->fds_len); - if (error == 0) { - args.fd = uap->fd; - error = kern_execve(td, &args, NULL, oldvmspace); - } - post_execve(td, error, oldvmspace); - return (error); -} - -int -cloudabi_sys_proc_exit(struct thread *td, - struct cloudabi_sys_proc_exit_args *uap) -{ - - exit1(td, uap->rval, 0); - /* NOTREACHED */ -} - -int -cloudabi_sys_proc_fork(struct thread *td, - struct cloudabi_sys_proc_fork_args *uap) -{ - struct fork_req fr; - struct filecaps fcaps = {}; - int error, fd; - - cap_rights_init(&fcaps.fc_rights, CAP_FSTAT, CAP_EVENT); - bzero(&fr, sizeof(fr)); - fr.fr_flags = RFFDG | RFPROC | RFPROCDESC; - fr.fr_pd_fd = &fd; - fr.fr_pd_fcaps = &fcaps; - error = fork1(td, &fr); - if (error != 0) - return (error); - /* Return the file descriptor to the parent process. */ - td->td_retval[0] = fd; - return (0); -} - -int -cloudabi_sys_proc_raise(struct thread *td, - struct cloudabi_sys_proc_raise_args *uap) -{ - static const int signals[] = { - [CLOUDABI_SIGABRT] = SIGABRT, - [CLOUDABI_SIGALRM] = SIGALRM, - [CLOUDABI_SIGBUS] = SIGBUS, - [CLOUDABI_SIGCHLD] = SIGCHLD, - [CLOUDABI_SIGCONT] = SIGCONT, - [CLOUDABI_SIGFPE] = SIGFPE, - [CLOUDABI_SIGHUP] = SIGHUP, - [CLOUDABI_SIGILL] = SIGILL, - [CLOUDABI_SIGINT] = SIGINT, - [CLOUDABI_SIGKILL] = SIGKILL, - [CLOUDABI_SIGPIPE] = SIGPIPE, - [CLOUDABI_SIGQUIT] = SIGQUIT, - [CLOUDABI_SIGSEGV] = SIGSEGV, - [CLOUDABI_SIGSTOP] = SIGSTOP, - [CLOUDABI_SIGSYS] = SIGSYS, - [CLOUDABI_SIGTERM] = SIGTERM, - [CLOUDABI_SIGTRAP] = SIGTRAP, - [CLOUDABI_SIGTSTP] = SIGTSTP, - [CLOUDABI_SIGTTIN] = SIGTTIN, - [CLOUDABI_SIGTTOU] = SIGTTOU, - [CLOUDABI_SIGURG] = SIGURG, - [CLOUDABI_SIGUSR1] = SIGUSR1, - [CLOUDABI_SIGUSR2] = SIGUSR2, - [CLOUDABI_SIGVTALRM] = SIGVTALRM, - [CLOUDABI_SIGXCPU] = SIGXCPU, - [CLOUDABI_SIGXFSZ] = SIGXFSZ, - }; - ksiginfo_t ksi; - struct proc *p; - - if (uap->sig >= nitems(signals) || signals[uap->sig] == 0) { - /* Invalid signal, or the null signal. */ - return (uap->sig == 0 ? 0 : EINVAL); - } - - p = td->td_proc; - ksiginfo_init(&ksi); - ksi.ksi_signo = signals[uap->sig]; - ksi.ksi_code = SI_USER; - ksi.ksi_pid = p->p_pid; - ksi.ksi_uid = td->td_ucred->cr_ruid; - PROC_LOCK(p); - pksignal(p, ksi.ksi_signo, &ksi); - PROC_UNLOCK(p); - return (0); -} - -MODULE_VERSION(cloudabi, 1); diff --git a/sys/compat/cloudabi/cloudabi_proto.h b/sys/compat/cloudabi/cloudabi_proto.h deleted file mode 100644 index 95271fd3a575..000000000000 --- a/sys/compat/cloudabi/cloudabi_proto.h +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * Copyright (c) 2015 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -/* - * This should provide all prototypes for the machine-independent system - * calls. Unfortunately, we don't have a separate system call table for - * those, so rely on the system call table from COMPAT_CLOUDABI64. - */ - -#include <contrib/cloudabi/cloudabi64_types.h> - -#include <compat/cloudabi64/cloudabi64_proto.h> diff --git a/sys/compat/cloudabi/cloudabi_random.c b/sys/compat/cloudabi/cloudabi_random.c deleted file mode 100644 index 02a3252a1b54..000000000000 --- a/sys/compat/cloudabi/cloudabi_random.c +++ /dev/null @@ -1,53 +0,0 @@ -/*- - * Copyright (c) 2015 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/types.h> -#include <sys/random.h> -#include <sys/uio.h> - -#include <compat/cloudabi/cloudabi_proto.h> - -int -cloudabi_sys_random_get(struct thread *td, - struct cloudabi_sys_random_get_args *uap) -{ - struct iovec iov = { - .iov_base = uap->buf, - .iov_len = uap->buf_len - }; - struct uio uio = { - .uio_iov = &iov, - .uio_iovcnt = 1, - .uio_resid = iov.iov_len, - .uio_segflg = UIO_USERSPACE, - .uio_rw = UIO_READ, - .uio_td = td - }; - - return (read_random_uio(&uio, false)); -} diff --git a/sys/compat/cloudabi/cloudabi_sock.c b/sys/compat/cloudabi/cloudabi_sock.c deleted file mode 100644 index f9f9f0510f2a..000000000000 --- a/sys/compat/cloudabi/cloudabi_sock.c +++ /dev/null @@ -1,186 +0,0 @@ -/*- - * Copyright (c) 2015-2017 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/param.h> -#include <sys/capsicum.h> -#include <sys/lock.h> -#include <sys/malloc.h> -#include <sys/mbuf.h> -#include <sys/mutex.h> -#include <sys/proc.h> -#include <sys/protosw.h> -#include <sys/socket.h> -#include <sys/socketvar.h> -#include <sys/syscallsubr.h> -#include <sys/systm.h> - -#include <net/vnet.h> - -#include <netinet/in.h> - -#include <contrib/cloudabi/cloudabi_types_common.h> - -#include <compat/cloudabi/cloudabi_proto.h> -#include <compat/cloudabi/cloudabi_util.h> - -int -cloudabi_sys_sock_shutdown(struct thread *td, - struct cloudabi_sys_sock_shutdown_args *uap) -{ - int how; - - switch (uap->how) { - case CLOUDABI_SHUT_RD: - how = SHUT_RD; - break; - case CLOUDABI_SHUT_WR: - how = SHUT_WR; - break; - case CLOUDABI_SHUT_RD | CLOUDABI_SHUT_WR: - how = SHUT_RDWR; - break; - default: - return (EINVAL); - } - - return (kern_shutdown(td, uap->sock, how)); -} - -int -cloudabi_sock_recv(struct thread *td, cloudabi_fd_t fd, struct iovec *data, - size_t datalen, cloudabi_fd_t *fds, size_t fdslen, - cloudabi_riflags_t flags, size_t *rdatalen, size_t *rfdslen, - cloudabi_roflags_t *rflags) -{ - struct msghdr hdr = { - .msg_iov = data, - .msg_iovlen = datalen, - }; - struct mbuf *control; - int error; - - /* Convert flags. */ - if (flags & CLOUDABI_SOCK_RECV_PEEK) - hdr.msg_flags |= MSG_PEEK; - if (flags & CLOUDABI_SOCK_RECV_WAITALL) - hdr.msg_flags |= MSG_WAITALL; - - control = NULL; - error = kern_recvit(td, fd, &hdr, UIO_SYSSPACE, - fdslen > 0 ? &control : NULL); - if (error != 0) - return (error); - - /* Convert return values. */ - *rdatalen = td->td_retval[0]; - td->td_retval[0] = 0; - *rfdslen = 0; - *rflags = 0; - if (hdr.msg_flags & MSG_TRUNC) - *rflags |= CLOUDABI_SOCK_RECV_DATA_TRUNCATED; - - /* Extract file descriptors from SCM_RIGHTS messages. */ - if (control != NULL) { - struct cmsghdr *chdr; - - hdr.msg_control = mtod(control, void *); - hdr.msg_controllen = control->m_len; - for (chdr = CMSG_FIRSTHDR(&hdr); chdr != NULL; - chdr = CMSG_NXTHDR(&hdr, chdr)) { - if (chdr->cmsg_level == SOL_SOCKET && - chdr->cmsg_type == SCM_RIGHTS) { - size_t nfds; - - nfds = (chdr->cmsg_len - CMSG_LEN(0)) / - sizeof(int); - if (nfds > fdslen) { - /* Unable to store file descriptors. */ - *rflags |= - CLOUDABI_SOCK_RECV_FDS_TRUNCATED; - m_dispose_extcontrolm(control); - break; - } - error = copyout(CMSG_DATA(chdr), fds, - nfds * sizeof(int)); - if (error != 0) - break; - fds += nfds; - fdslen -= nfds; - *rfdslen += nfds; - } - } - if (control != NULL) { - if (error != 0) - m_dispose_extcontrolm(control); - m_free(control); - } - } - return (error); -} - -int -cloudabi_sock_send(struct thread *td, cloudabi_fd_t fd, struct iovec *data, - size_t datalen, const cloudabi_fd_t *fds, size_t fdslen, size_t *rdatalen) -{ - struct msghdr hdr = { - .msg_iov = data, - .msg_iovlen = datalen, - }; - struct mbuf *control; - int error; - - /* Convert file descriptor array to an SCM_RIGHTS message. */ - if (fdslen > MCLBYTES || CMSG_SPACE(fdslen * sizeof(int)) > MCLBYTES) { - return (EINVAL); - } else if (fdslen > 0) { - struct cmsghdr *chdr; - - control = m_get2(CMSG_SPACE(fdslen * sizeof(int)), - M_WAITOK, MT_CONTROL, 0); - control->m_len = CMSG_SPACE(fdslen * sizeof(int)); - - chdr = mtod(control, struct cmsghdr *); - chdr->cmsg_len = CMSG_LEN(fdslen * sizeof(int)); - chdr->cmsg_level = SOL_SOCKET; - chdr->cmsg_type = SCM_RIGHTS; - error = copyin(fds, CMSG_DATA(chdr), fdslen * sizeof(int)); - if (error != 0) { - m_free(control); - return (error); - } - } else { - control = NULL; - } - - error = kern_sendit(td, fd, &hdr, MSG_NOSIGNAL, control, UIO_USERSPACE); - if (error != 0) - return (error); - *rdatalen = td->td_retval[0]; - td->td_retval[0] = 0; - return (0); -} diff --git a/sys/compat/cloudabi/cloudabi_thread.c b/sys/compat/cloudabi/cloudabi_thread.c deleted file mode 100644 index f920b8b41ee8..000000000000 --- a/sys/compat/cloudabi/cloudabi_thread.c +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * Copyright (c) 2015 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/param.h> -#include <sys/proc.h> -#include <sys/sched.h> -#include <sys/syscallsubr.h> - -#include <contrib/cloudabi/cloudabi_types_common.h> - -#include <compat/cloudabi/cloudabi_proto.h> - -int -cloudabi_sys_thread_exit(struct thread *td, - struct cloudabi_sys_thread_exit_args *uap) -{ - struct cloudabi_sys_lock_unlock_args cloudabi_sys_lock_unlock_args = { - .lock = uap->lock, - .scope = uap->scope, - }; - - /* Wake up joining thread. */ - cloudabi_sys_lock_unlock(td, &cloudabi_sys_lock_unlock_args); - - /* - * Attempt to terminate the thread. Terminate the process if - * it's the last thread. - */ - kern_thr_exit(td); - exit1(td, 0, 0); - /* NOTREACHED */ -} - -int -cloudabi_sys_thread_yield(struct thread *td, - struct cloudabi_sys_thread_yield_args *uap) -{ - - sched_relinquish(td); - return (0); -} diff --git a/sys/compat/cloudabi/cloudabi_util.h b/sys/compat/cloudabi/cloudabi_util.h deleted file mode 100644 index 0f07dc53fa29..000000000000 --- a/sys/compat/cloudabi/cloudabi_util.h +++ /dev/null @@ -1,87 +0,0 @@ -/*- - * Copyright (c) 2015-2017 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _CLOUDABI_UTIL_H_ -#define _CLOUDABI_UTIL_H_ - -#include <sys/socket.h> - -#include <contrib/cloudabi/cloudabi_types_common.h> - -struct file; -struct sysentvec; -struct thread; -struct timespec; - -/* Fetches the time value of a clock. */ -int cloudabi_clock_time_get(struct thread *, cloudabi_clockid_t, - cloudabi_timestamp_t *); - -/* Converts a FreeBSD errno to a CloudABI errno. */ -cloudabi_errno_t cloudabi_convert_errno(int); - -/* Converts a file descriptor to a CloudABI file descriptor type. */ -cloudabi_filetype_t cloudabi_convert_filetype(const struct file *); - -/* Converts CloudABI rights to a set of Capsicum capabilities. */ -int cloudabi_convert_rights(cloudabi_rights_t, cap_rights_t *); - -/* Removes rights that conflict with the file descriptor type. */ -void cloudabi_remove_conflicting_rights(cloudabi_filetype_t, - cloudabi_rights_t *, cloudabi_rights_t *); - -/* Converts a struct timespec to a CloudABI timestamp. */ -int cloudabi_convert_timespec(const struct timespec *, cloudabi_timestamp_t *); - -/* - * Blocking futex functions. - * - * These functions are called by CloudABI's polling system calls to - * sleep on a lock or condition variable. - */ -int cloudabi_futex_condvar_wait(struct thread *, cloudabi_condvar_t *, - cloudabi_scope_t, cloudabi_lock_t *, cloudabi_scope_t, cloudabi_clockid_t, - cloudabi_timestamp_t, cloudabi_timestamp_t, bool); -int cloudabi_futex_lock_rdlock(struct thread *, cloudabi_lock_t *, - cloudabi_scope_t, cloudabi_clockid_t, cloudabi_timestamp_t, - cloudabi_timestamp_t, bool); -int cloudabi_futex_lock_wrlock(struct thread *, cloudabi_lock_t *, - cloudabi_scope_t, cloudabi_clockid_t, cloudabi_timestamp_t, - cloudabi_timestamp_t, bool); - -/* Socket operations. */ -int cloudabi_sock_recv(struct thread *, cloudabi_fd_t, struct iovec *, size_t, - cloudabi_fd_t *, size_t, cloudabi_riflags_t, size_t *, size_t *, - cloudabi_roflags_t *); -int cloudabi_sock_send(struct thread *, cloudabi_fd_t, struct iovec *, size_t, - const cloudabi_fd_t *, size_t, size_t *); - -/* vDSO setup and teardown. */ -void cloudabi_vdso_init(struct sysentvec *, char *, char *); -void cloudabi_vdso_destroy(struct sysentvec *); - -#endif diff --git a/sys/compat/cloudabi/cloudabi_vdso.c b/sys/compat/cloudabi/cloudabi_vdso.c deleted file mode 100644 index ab621b3c18b0..000000000000 --- a/sys/compat/cloudabi/cloudabi_vdso.c +++ /dev/null @@ -1,90 +0,0 @@ -/*- - * Copyright (c) 2016 Nuxi, https://nuxi.nl/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/param.h> -#include <sys/systm.h> -#include <sys/lock.h> -#include <sys/sysent.h> -#include <sys/rwlock.h> - -#include <vm/vm.h> -#include <vm/pmap.h> -#include <vm/vm_extern.h> -#include <vm/vm_object.h> -#include <vm/vm_page.h> -#include <vm/vm_pager.h> - -#include <compat/cloudabi/cloudabi_util.h> - -void -cloudabi_vdso_init(struct sysentvec *sv, char *begin, char *end) -{ - vm_page_t m; - vm_object_t obj; - vm_offset_t addr; - size_t i, pages, pages_length, vdso_length; - - /* Determine the number of pages needed to store the vDSO. */ - vdso_length = end - begin; - pages = howmany(vdso_length, PAGE_SIZE); - pages_length = pages * PAGE_SIZE; - - /* Allocate a VM object and fill it with the vDSO. */ - obj = vm_pager_allocate(OBJT_PHYS, 0, pages_length, - VM_PROT_DEFAULT, 0, NULL); - addr = kva_alloc(PAGE_SIZE); - for (i = 0; i < pages; ++i) { - VM_OBJECT_WLOCK(obj); - m = vm_page_grab(obj, i, VM_ALLOC_ZERO); - VM_OBJECT_WUNLOCK(obj); - vm_page_valid(m); - vm_page_xunbusy(m); - - pmap_qenter(addr, &m, 1); - memcpy((void *)addr, begin + i * PAGE_SIZE, - MIN(vdso_length - i * PAGE_SIZE, PAGE_SIZE)); - pmap_qremove(addr, 1); - } - kva_free(addr, PAGE_SIZE); - - /* - * Place the vDSO at the top of the address space. The user - * stack can start right below it. - */ - sv->sv_shared_page_base = sv->sv_maxuser - pages_length; - sv->sv_shared_page_len = pages_length; - sv->sv_shared_page_obj = obj; - sv->sv_usrstack = sv->sv_shared_page_base; -} - -void -cloudabi_vdso_destroy(struct sysentvec *sv) -{ - - vm_object_deallocate(sv->sv_shared_page_obj); -} diff --git a/sys/compat/cloudabi/cloudabi_vdso.lds b/sys/compat/cloudabi/cloudabi_vdso.lds deleted file mode 100644 index 807c48859bf2..000000000000 --- a/sys/compat/cloudabi/cloudabi_vdso.lds +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Linker script for the vDSO for CloudABI. - * Based on sys/amd64/linux/linux_vdso.lds.s - * - * $FreeBSD$ - */ - -SECTIONS -{ - . = . + SIZEOF_HEADERS; - - .hash : { *(.hash) } :text - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - - .note : { *(.note.*) } :text :note - - .eh_frame_hdr : { *(.eh_frame_hdr) } :text :eh_frame_hdr - .eh_frame : { KEEP (*(.eh_frame)) } :text - - .dynamic : { *(.dynamic) } :text :dynamic - - .rodata : { *(.rodata*) } :text - .data : { - *(.data*) - *(.sdata*) - *(.got.plt) *(.got) - *(.gnu.linkonce.d.*) - *(.bss*) - *(.dynbss*) - *(.gnu.linkonce.b.*) - } - - .altinstructions : { *(.altinstructions) } - .altinstr_replacement : { *(.altinstr_replacement) } - - . = ALIGN(0x100); - .text : { *(.test .text*) } :text =0x90909090 -} - -PHDRS -{ - text PT_LOAD FLAGS(5) FILEHDR PHDRS; /* PF_R|PF_X */ - dynamic PT_DYNAMIC FLAGS(4); /* PF_R */ - note PT_NOTE FLAGS(4); /* PF_R */ - eh_frame_hdr PT_GNU_EH_FRAME; -} |