xref: /NextBSD/sys/compat/cloudabi64/cloudabi64_fd.c (revision cfe5aa9ba837b52a161c1e5a35d8b6d4d75e2cb2)
1 /*-
2  * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
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>
35 #include <sys/uio.h>
36 
37 #include <compat/cloudabi64/cloudabi64_syscalldefs.h>
38 #include <compat/cloudabi64/cloudabi64_proto.h>
39 
40 /* Copies in 64-bit iovec structures from userspace. */
41 static int
cloudabi64_copyinuio(const cloudabi64_iovec_t * iovp,size_t iovcnt,struct uio ** uiop)42 cloudabi64_copyinuio(const cloudabi64_iovec_t *iovp, size_t iovcnt,
43     struct uio **uiop)
44 {
45 	cloudabi64_iovec_t iovobj;
46 	struct uio *uio;
47 	struct iovec *iov;
48 	size_t i;
49 	int error;
50 
51 	/* Allocate uio and iovecs. */
52 	if (iovcnt > UIO_MAXIOV)
53 		return (EINVAL);
54 	uio = malloc(sizeof(struct uio) + iovcnt * sizeof(struct iovec),
55 	    M_IOV, M_WAITOK);
56 	iov = (struct iovec *)(uio + 1);
57 
58 	/* Initialize uio. */
59 	uio->uio_iov = iov;
60 	uio->uio_iovcnt = iovcnt;
61 	uio->uio_segflg = UIO_USERSPACE;
62 	uio->uio_offset = -1;
63 	uio->uio_resid = 0;
64 
65 	/* Copy in iovecs. */
66 	for (i = 0; i < iovcnt; i++) {
67 		error = copyin(&iovp[i], &iovobj, sizeof(iovobj));
68 		if (error != 0) {
69 			free(uio, M_IOV);
70 			return (error);
71 		}
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) {
75 			free(uio, M_IOV);
76 			return (EINVAL);
77 		}
78 		uio->uio_resid += iov[i].iov_len;
79 	}
80 
81 	*uiop = uio;
82 	return (0);
83 }
84 
85 int
cloudabi64_sys_fd_pread(struct thread * td,struct cloudabi64_sys_fd_pread_args * uap)86 cloudabi64_sys_fd_pread(struct thread *td,
87     struct cloudabi64_sys_fd_pread_args *uap)
88 {
89 	struct uio *uio;
90 	int error;
91 
92 	error = cloudabi64_copyinuio(uap->iov, uap->iovcnt, &uio);
93 	if (error != 0)
94 		return (error);
95 	error = kern_preadv(td, uap->fd, uio, uap->offset);
96 	free(uio, M_IOV);
97 	return (error);
98 }
99 
100 int
cloudabi64_sys_fd_pwrite(struct thread * td,struct cloudabi64_sys_fd_pwrite_args * uap)101 cloudabi64_sys_fd_pwrite(struct thread *td,
102     struct cloudabi64_sys_fd_pwrite_args *uap)
103 {
104 	struct uio *uio;
105 	int error;
106 
107 	error = cloudabi64_copyinuio((const cloudabi64_iovec_t *)uap->iov,
108 	    uap->iovcnt, &uio);
109 	if (error != 0)
110 		return (error);
111 	error = kern_pwritev(td, uap->fd, uio, uap->offset);
112 	free(uio, M_IOV);
113 	return (error);
114 }
115 
116 int
cloudabi64_sys_fd_read(struct thread * td,struct cloudabi64_sys_fd_read_args * uap)117 cloudabi64_sys_fd_read(struct thread *td,
118     struct cloudabi64_sys_fd_read_args *uap)
119 {
120 	struct uio *uio;
121 	int error;
122 
123 	error = cloudabi64_copyinuio(uap->iov, uap->iovcnt, &uio);
124 	if (error != 0)
125 		return (error);
126 	error = kern_readv(td, uap->fd, uio);
127 	free(uio, M_IOV);
128 	return (error);
129 }
130 
131 int
cloudabi64_sys_fd_write(struct thread * td,struct cloudabi64_sys_fd_write_args * uap)132 cloudabi64_sys_fd_write(struct thread *td,
133     struct cloudabi64_sys_fd_write_args *uap)
134 {
135 	struct uio *uio;
136 	int error;
137 
138 	error = cloudabi64_copyinuio((const cloudabi64_iovec_t *)uap->iov,
139 	    uap->iovcnt, &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