2 * Copyright (c) 2015 Nuxi, https://nuxi.nl/
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/limits.h>
32 #include <sys/malloc.h>
33 #include <sys/syscallsubr.h>
34 #include <sys/systm.h>
37 #include <compat/cloudabi64/cloudabi64_syscalldefs.h>
38 #include <compat/cloudabi64/cloudabi64_proto.h>
40 /* Copies in 64-bit iovec structures from userspace. */
42 cloudabi64_copyinuio(const cloudabi64_iovec_t *iovp, size_t iovcnt,
45 cloudabi64_iovec_t iovobj;
51 /* Allocate uio and iovecs. */
52 if (iovcnt > UIO_MAXIOV)
54 uio = malloc(sizeof(struct uio) + iovcnt * sizeof(struct iovec),
56 iov = (struct iovec *)(uio + 1);
60 uio->uio_iovcnt = iovcnt;
61 uio->uio_segflg = UIO_USERSPACE;
66 for (i = 0; i < iovcnt; i++) {
67 error = copyin(&iovp[i], &iovobj, sizeof(iovobj));
72 iov[i].iov_base = (void *)iovobj.iov_base;
73 iov[i].iov_len = iovobj.iov_len;
74 if (iov[i].iov_len > SSIZE_MAX - uio->uio_resid) {
78 uio->uio_resid += iov[i].iov_len;
86 cloudabi64_sys_fd_pread(struct thread *td,
87 struct cloudabi64_sys_fd_pread_args *uap)
92 error = cloudabi64_copyinuio(uap->iov, uap->iovcnt, &uio);
95 error = kern_preadv(td, uap->fd, uio, uap->offset);
101 cloudabi64_sys_fd_pwrite(struct thread *td,
102 struct cloudabi64_sys_fd_pwrite_args *uap)
107 error = cloudabi64_copyinuio((const cloudabi64_iovec_t *)uap->iov,
111 error = kern_pwritev(td, uap->fd, uio, uap->offset);
117 cloudabi64_sys_fd_read(struct thread *td,
118 struct cloudabi64_sys_fd_read_args *uap)
123 error = cloudabi64_copyinuio(uap->iov, uap->iovcnt, &uio);
126 error = kern_readv(td, uap->fd, uio);
132 cloudabi64_sys_fd_write(struct thread *td,
133 struct cloudabi64_sys_fd_write_args *uap)
138 error = cloudabi64_copyinuio((const cloudabi64_iovec_t *)uap->iov,
142 error = kern_writev(td, uap->fd, uio);