cloudabi32_fd.c revision 316574
1178825Sdfr/*-
2233294Sstas * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3233294Sstas *
4233294Sstas * Redistribution and use in source and binary forms, with or without
5178825Sdfr * modification, are permitted provided that the following conditions
6233294Sstas * are met:
7233294Sstas * 1. Redistributions of source code must retain the above copyright
8233294Sstas *    notice, this list of conditions and the following disclaimer.
9178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
10233294Sstas *    notice, this list of conditions and the following disclaimer in the
11233294Sstas *    documentation and/or other materials provided with the distribution.
12178825Sdfr *
13233294Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16178825Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20178825Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23233294Sstas * SUCH DAMAGE.
24233294Sstas */
25233294Sstas
26233294Sstas#include <sys/cdefs.h>
27233294Sstas__FBSDID("$FreeBSD: stable/11/sys/compat/cloudabi32/cloudabi32_fd.c 316574 2017-04-06 15:10:36Z ed $");
28233294Sstas
29233294Sstas#include <sys/param.h>
30233294Sstas#include <sys/kernel.h>
31233294Sstas#include <sys/limits.h>
32178825Sdfr#include <sys/malloc.h>
33178825Sdfr#include <sys/syscallsubr.h>
34233294Sstas#include <sys/systm.h>
35178825Sdfr#include <sys/uio.h>
36233294Sstas
37178825Sdfr#include <contrib/cloudabi/cloudabi32_types.h>
38178825Sdfr
39178825Sdfr#include <compat/cloudabi32/cloudabi32_proto.h>
40178825Sdfr#include <compat/cloudabi32/cloudabi32_util.h>
41178825Sdfr
42178825Sdfr/* Copies in 32-bit iovec structures from userspace. */
43178825Sdfrstatic int
44178825Sdfrcloudabi32_copyinuio(const cloudabi32_iovec_t *iovp, size_t iovcnt,
45178825Sdfr    struct uio **uiop)
46178825Sdfr{
47178825Sdfr	cloudabi32_iovec_t iovobj;
48178825Sdfr	struct uio *uio;
49178825Sdfr	struct iovec *iov;
50178825Sdfr	size_t i;
51233294Sstas	int error;
52178825Sdfr
53178825Sdfr	/* Allocate uio and iovecs. */
54178825Sdfr	if (iovcnt > UIO_MAXIOV)
55233294Sstas		return (EINVAL);
56233294Sstas	uio = malloc(sizeof(struct uio) + iovcnt * sizeof(struct iovec),
57178825Sdfr	    M_IOV, M_WAITOK);
58178825Sdfr	iov = (struct iovec *)(uio + 1);
59178825Sdfr
60178825Sdfr	/* Initialize uio. */
61178825Sdfr	uio->uio_iov = iov;
62178825Sdfr	uio->uio_iovcnt = iovcnt;
63178825Sdfr	uio->uio_segflg = UIO_USERSPACE;
64178825Sdfr	uio->uio_offset = -1;
65178825Sdfr	uio->uio_resid = 0;
66178825Sdfr
67	/* Copy in iovecs. */
68	for (i = 0; i < iovcnt; i++) {
69		error = copyin(&iovp[i], &iovobj, sizeof(iovobj));
70		if (error != 0) {
71			free(uio, M_IOV);
72			return (error);
73		}
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) {
77			free(uio, M_IOV);
78			return (EINVAL);
79		}
80		uio->uio_resid += iov[i].iov_len;
81	}
82
83	*uiop = uio;
84	return (0);
85}
86
87int
88cloudabi32_sys_fd_pread(struct thread *td,
89    struct cloudabi32_sys_fd_pread_args *uap)
90{
91	struct uio *uio;
92	int error;
93
94	error = cloudabi32_copyinuio(uap->iovs, uap->iovs_len, &uio);
95	if (error != 0)
96		return (error);
97	error = kern_preadv(td, uap->fd, uio, uap->offset);
98	free(uio, M_IOV);
99	return (error);
100}
101
102int
103cloudabi32_sys_fd_pwrite(struct thread *td,
104    struct cloudabi32_sys_fd_pwrite_args *uap)
105{
106	struct uio *uio;
107	int error;
108
109	error = cloudabi32_copyinuio(TO_PTR(uap->iovs), uap->iovs_len, &uio);
110	if (error != 0)
111		return (error);
112	error = kern_pwritev(td, uap->fd, uio, uap->offset);
113	free(uio, M_IOV);
114	return (error);
115}
116
117int
118cloudabi32_sys_fd_read(struct thread *td,
119    struct cloudabi32_sys_fd_read_args *uap)
120{
121	struct uio *uio;
122	int error;
123
124	error = cloudabi32_copyinuio(uap->iovs, uap->iovs_len, &uio);
125	if (error != 0)
126		return (error);
127	error = kern_readv(td, uap->fd, uio);
128	free(uio, M_IOV);
129	return (error);
130}
131
132int
133cloudabi32_sys_fd_write(struct thread *td,
134    struct cloudabi32_sys_fd_write_args *uap)
135{
136	struct uio *uio;
137	int error;
138
139	error = cloudabi32_copyinuio(TO_PTR(uap->iovs), uap->iovs_len, &uio);
140	if (error != 0)
141		return (error);
142	error = kern_writev(td, uap->fd, uio);
143	free(uio, M_IOV);
144	return (error);
145}
146