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 <contrib/cloudabi/cloudabi32_types.h>
39 #include <compat/cloudabi32/cloudabi32_proto.h>
40 #include <compat/cloudabi32/cloudabi32_util.h>
42 /* Copies in 32-bit iovec structures from userspace. */
44 cloudabi32_copyinuio(const cloudabi32_iovec_t *iovp, size_t iovcnt,
47 cloudabi32_iovec_t iovobj;
53 /* Allocate uio and iovecs. */
54 if (iovcnt > UIO_MAXIOV)
56 uio = malloc(sizeof(struct uio) + iovcnt * sizeof(struct iovec),
58 iov = (struct iovec *)(uio + 1);
62 uio->uio_iovcnt = iovcnt;
63 uio->uio_segflg = UIO_USERSPACE;
68 for (i = 0; i < iovcnt; i++) {
69 error = copyin(&iovp[i], &iovobj, sizeof(iovobj));
74 iov[i].iov_base = TO_PTR(iovobj.buf);
75 iov[i].iov_len = iovobj.buf_len;
76 if (iov[i].iov_len > INT32_MAX - uio->uio_resid) {
80 uio->uio_resid += iov[i].iov_len;
88 cloudabi32_sys_fd_pread(struct thread *td,
89 struct cloudabi32_sys_fd_pread_args *uap)
94 error = cloudabi32_copyinuio(uap->iovs, uap->iovs_len, &uio);
97 error = kern_preadv(td, uap->fd, uio, uap->offset);
103 cloudabi32_sys_fd_pwrite(struct thread *td,
104 struct cloudabi32_sys_fd_pwrite_args *uap)
109 error = cloudabi32_copyinuio(TO_PTR(uap->iovs), uap->iovs_len, &uio);
112 error = kern_pwritev(td, uap->fd, uio, uap->offset);
118 cloudabi32_sys_fd_read(struct thread *td,
119 struct cloudabi32_sys_fd_read_args *uap)
124 error = cloudabi32_copyinuio(uap->iovs, uap->iovs_len, &uio);
127 error = kern_readv(td, uap->fd, uio);
133 cloudabi32_sys_fd_write(struct thread *td,
134 struct cloudabi32_sys_fd_write_args *uap)
139 error = cloudabi32_copyinuio(TO_PTR(uap->iovs), uap->iovs_len, &uio);
142 error = kern_writev(td, uap->fd, uio);