subr_copy.c revision 1.8 1 1.8 chs /* $NetBSD: subr_copy.c,v 1.8 2018/05/28 21:04:41 chs Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
5 1.1 pooka * All rights reserved.
6 1.1 pooka *
7 1.1 pooka * This code is derived from software contributed to The NetBSD Foundation
8 1.1 pooka * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 pooka * NASA Ames Research Center.
10 1.1 pooka *
11 1.1 pooka * Redistribution and use in source and binary forms, with or without
12 1.1 pooka * modification, are permitted provided that the following conditions
13 1.1 pooka * are met:
14 1.1 pooka * 1. Redistributions of source code must retain the above copyright
15 1.1 pooka * notice, this list of conditions and the following disclaimer.
16 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 pooka * notice, this list of conditions and the following disclaimer in the
18 1.1 pooka * documentation and/or other materials provided with the distribution.
19 1.1 pooka *
20 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 pooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 pooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 pooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 pooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 pooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 pooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 pooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 pooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 pooka * POSSIBILITY OF SUCH DAMAGE.
31 1.1 pooka */
32 1.1 pooka
33 1.1 pooka /*
34 1.1 pooka * Copyright (c) 1982, 1986, 1991, 1993
35 1.1 pooka * The Regents of the University of California. All rights reserved.
36 1.1 pooka * (c) UNIX System Laboratories, Inc.
37 1.1 pooka * All or some portions of this file are derived from material licensed
38 1.1 pooka * to the University of California by American Telephone and Telegraph
39 1.1 pooka * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 1.1 pooka * the permission of UNIX System Laboratories, Inc.
41 1.1 pooka *
42 1.1 pooka * Copyright (c) 1992, 1993
43 1.1 pooka * The Regents of the University of California. All rights reserved.
44 1.1 pooka *
45 1.1 pooka * This software was developed by the Computer Systems Engineering group
46 1.1 pooka * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
47 1.1 pooka * contributed to Berkeley.
48 1.1 pooka *
49 1.1 pooka * All advertising materials mentioning features or use of this software
50 1.1 pooka * must display the following acknowledgement:
51 1.1 pooka * This product includes software developed by the University of
52 1.1 pooka * California, Lawrence Berkeley Laboratory.
53 1.1 pooka *
54 1.1 pooka * Redistribution and use in source and binary forms, with or without
55 1.1 pooka * modification, are permitted provided that the following conditions
56 1.1 pooka * are met:
57 1.1 pooka * 1. Redistributions of source code must retain the above copyright
58 1.1 pooka * notice, this list of conditions and the following disclaimer.
59 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
60 1.1 pooka * notice, this list of conditions and the following disclaimer in the
61 1.1 pooka * documentation and/or other materials provided with the distribution.
62 1.1 pooka * 3. Neither the name of the University nor the names of its contributors
63 1.1 pooka * may be used to endorse or promote products derived from this software
64 1.1 pooka * without specific prior written permission.
65 1.1 pooka *
66 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 1.1 pooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 1.1 pooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 1.1 pooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 1.1 pooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 1.1 pooka * SUCH DAMAGE.
77 1.1 pooka *
78 1.1 pooka * @(#)kern_subr.c 8.4 (Berkeley) 2/14/95
79 1.1 pooka */
80 1.1 pooka
81 1.1 pooka #include <sys/cdefs.h>
82 1.8 chs __KERNEL_RCSID(0, "$NetBSD: subr_copy.c,v 1.8 2018/05/28 21:04:41 chs Exp $");
83 1.1 pooka
84 1.1 pooka #include <sys/param.h>
85 1.1 pooka #include <sys/fcntl.h>
86 1.1 pooka #include <sys/proc.h>
87 1.1 pooka #include <sys/systm.h>
88 1.1 pooka
89 1.1 pooka #include <uvm/uvm_extern.h>
90 1.1 pooka
91 1.1 pooka void
92 1.1 pooka uio_setup_sysspace(struct uio *uio)
93 1.1 pooka {
94 1.1 pooka
95 1.1 pooka uio->uio_vmspace = vmspace_kernel();
96 1.1 pooka }
97 1.1 pooka
98 1.1 pooka int
99 1.1 pooka uiomove(void *buf, size_t n, struct uio *uio)
100 1.1 pooka {
101 1.1 pooka struct vmspace *vm = uio->uio_vmspace;
102 1.1 pooka struct iovec *iov;
103 1.1 pooka size_t cnt;
104 1.1 pooka int error = 0;
105 1.1 pooka char *cp = buf;
106 1.1 pooka
107 1.1 pooka ASSERT_SLEEPABLE();
108 1.1 pooka
109 1.6 riastrad KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE);
110 1.1 pooka while (n > 0 && uio->uio_resid) {
111 1.1 pooka iov = uio->uio_iov;
112 1.1 pooka cnt = iov->iov_len;
113 1.1 pooka if (cnt == 0) {
114 1.1 pooka KASSERT(uio->uio_iovcnt > 0);
115 1.1 pooka uio->uio_iov++;
116 1.1 pooka uio->uio_iovcnt--;
117 1.1 pooka continue;
118 1.1 pooka }
119 1.1 pooka if (cnt > n)
120 1.1 pooka cnt = n;
121 1.1 pooka if (!VMSPACE_IS_KERNEL_P(vm)) {
122 1.1 pooka if (curcpu()->ci_schedstate.spc_flags &
123 1.1 pooka SPCF_SHOULDYIELD)
124 1.1 pooka preempt();
125 1.1 pooka }
126 1.1 pooka
127 1.1 pooka if (uio->uio_rw == UIO_READ) {
128 1.1 pooka error = copyout_vmspace(vm, cp, iov->iov_base,
129 1.1 pooka cnt);
130 1.1 pooka } else {
131 1.1 pooka error = copyin_vmspace(vm, iov->iov_base, cp,
132 1.1 pooka cnt);
133 1.1 pooka }
134 1.1 pooka if (error) {
135 1.1 pooka break;
136 1.1 pooka }
137 1.1 pooka iov->iov_base = (char *)iov->iov_base + cnt;
138 1.1 pooka iov->iov_len -= cnt;
139 1.1 pooka uio->uio_resid -= cnt;
140 1.1 pooka uio->uio_offset += cnt;
141 1.1 pooka cp += cnt;
142 1.1 pooka KDASSERT(cnt <= n);
143 1.1 pooka n -= cnt;
144 1.1 pooka }
145 1.1 pooka
146 1.1 pooka return (error);
147 1.1 pooka }
148 1.1 pooka
149 1.1 pooka /*
150 1.1 pooka * Wrapper for uiomove() that validates the arguments against a known-good
151 1.1 pooka * kernel buffer.
152 1.1 pooka */
153 1.1 pooka int
154 1.1 pooka uiomove_frombuf(void *buf, size_t buflen, struct uio *uio)
155 1.1 pooka {
156 1.1 pooka size_t offset;
157 1.1 pooka
158 1.1 pooka if (uio->uio_offset < 0 || /* uio->uio_resid < 0 || */
159 1.1 pooka (offset = uio->uio_offset) != uio->uio_offset)
160 1.1 pooka return (EINVAL);
161 1.1 pooka if (offset >= buflen)
162 1.1 pooka return (0);
163 1.1 pooka return (uiomove((char *)buf + offset, buflen - offset, uio));
164 1.1 pooka }
165 1.1 pooka
166 1.1 pooka /*
167 1.1 pooka * Give next character to user as result of read.
168 1.1 pooka */
169 1.1 pooka int
170 1.1 pooka ureadc(int c, struct uio *uio)
171 1.1 pooka {
172 1.1 pooka struct iovec *iov;
173 1.1 pooka
174 1.1 pooka if (uio->uio_resid <= 0)
175 1.1 pooka panic("ureadc: non-positive resid");
176 1.1 pooka again:
177 1.1 pooka if (uio->uio_iovcnt <= 0)
178 1.1 pooka panic("ureadc: non-positive iovcnt");
179 1.1 pooka iov = uio->uio_iov;
180 1.1 pooka if (iov->iov_len <= 0) {
181 1.1 pooka uio->uio_iovcnt--;
182 1.1 pooka uio->uio_iov++;
183 1.1 pooka goto again;
184 1.1 pooka }
185 1.1 pooka if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
186 1.1 pooka if (subyte(iov->iov_base, c) < 0)
187 1.1 pooka return (EFAULT);
188 1.1 pooka } else {
189 1.1 pooka *(char *)iov->iov_base = c;
190 1.1 pooka }
191 1.1 pooka iov->iov_base = (char *)iov->iov_base + 1;
192 1.1 pooka iov->iov_len--;
193 1.1 pooka uio->uio_resid--;
194 1.1 pooka uio->uio_offset++;
195 1.1 pooka return (0);
196 1.1 pooka }
197 1.1 pooka
198 1.1 pooka /*
199 1.1 pooka * Like copyin(), but operates on an arbitrary vmspace.
200 1.1 pooka */
201 1.1 pooka int
202 1.1 pooka copyin_vmspace(struct vmspace *vm, const void *uaddr, void *kaddr, size_t len)
203 1.1 pooka {
204 1.1 pooka struct iovec iov;
205 1.1 pooka struct uio uio;
206 1.1 pooka int error;
207 1.1 pooka
208 1.1 pooka if (len == 0)
209 1.1 pooka return (0);
210 1.1 pooka
211 1.3 riastrad if (VMSPACE_IS_KERNEL_P(vm)) {
212 1.3 riastrad return kcopy(uaddr, kaddr, len);
213 1.3 riastrad }
214 1.2 riastrad if (__predict_true(vm == curproc->p_vmspace)) {
215 1.2 riastrad return copyin(uaddr, kaddr, len);
216 1.2 riastrad }
217 1.1 pooka
218 1.1 pooka iov.iov_base = kaddr;
219 1.1 pooka iov.iov_len = len;
220 1.1 pooka uio.uio_iov = &iov;
221 1.1 pooka uio.uio_iovcnt = 1;
222 1.1 pooka uio.uio_offset = (off_t)(uintptr_t)uaddr;
223 1.1 pooka uio.uio_resid = len;
224 1.1 pooka uio.uio_rw = UIO_READ;
225 1.1 pooka UIO_SETUP_SYSSPACE(&uio);
226 1.7 christos error = uvm_io(&vm->vm_map, &uio, 0);
227 1.1 pooka
228 1.1 pooka return (error);
229 1.1 pooka }
230 1.1 pooka
231 1.1 pooka /*
232 1.1 pooka * Like copyout(), but operates on an arbitrary vmspace.
233 1.1 pooka */
234 1.1 pooka int
235 1.1 pooka copyout_vmspace(struct vmspace *vm, const void *kaddr, void *uaddr, size_t len)
236 1.1 pooka {
237 1.1 pooka struct iovec iov;
238 1.1 pooka struct uio uio;
239 1.1 pooka int error;
240 1.1 pooka
241 1.1 pooka if (len == 0)
242 1.1 pooka return (0);
243 1.1 pooka
244 1.3 riastrad if (VMSPACE_IS_KERNEL_P(vm)) {
245 1.3 riastrad return kcopy(kaddr, uaddr, len);
246 1.3 riastrad }
247 1.2 riastrad if (__predict_true(vm == curproc->p_vmspace)) {
248 1.2 riastrad return copyout(kaddr, uaddr, len);
249 1.2 riastrad }
250 1.1 pooka
251 1.1 pooka iov.iov_base = __UNCONST(kaddr); /* XXXUNCONST cast away const */
252 1.1 pooka iov.iov_len = len;
253 1.1 pooka uio.uio_iov = &iov;
254 1.1 pooka uio.uio_iovcnt = 1;
255 1.1 pooka uio.uio_offset = (off_t)(uintptr_t)uaddr;
256 1.1 pooka uio.uio_resid = len;
257 1.1 pooka uio.uio_rw = UIO_WRITE;
258 1.1 pooka UIO_SETUP_SYSSPACE(&uio);
259 1.7 christos error = uvm_io(&vm->vm_map, &uio, 0);
260 1.1 pooka
261 1.1 pooka return (error);
262 1.1 pooka }
263 1.1 pooka
264 1.1 pooka /*
265 1.1 pooka * Like copyin(), but operates on an arbitrary process.
266 1.1 pooka */
267 1.1 pooka int
268 1.1 pooka copyin_proc(struct proc *p, const void *uaddr, void *kaddr, size_t len)
269 1.1 pooka {
270 1.1 pooka struct vmspace *vm;
271 1.1 pooka int error;
272 1.1 pooka
273 1.1 pooka error = proc_vmspace_getref(p, &vm);
274 1.1 pooka if (error) {
275 1.1 pooka return error;
276 1.1 pooka }
277 1.1 pooka error = copyin_vmspace(vm, uaddr, kaddr, len);
278 1.1 pooka uvmspace_free(vm);
279 1.1 pooka
280 1.1 pooka return error;
281 1.1 pooka }
282 1.1 pooka
283 1.1 pooka /*
284 1.1 pooka * Like copyout(), but operates on an arbitrary process.
285 1.1 pooka */
286 1.1 pooka int
287 1.1 pooka copyout_proc(struct proc *p, const void *kaddr, void *uaddr, size_t len)
288 1.1 pooka {
289 1.1 pooka struct vmspace *vm;
290 1.1 pooka int error;
291 1.1 pooka
292 1.1 pooka error = proc_vmspace_getref(p, &vm);
293 1.1 pooka if (error) {
294 1.1 pooka return error;
295 1.1 pooka }
296 1.1 pooka error = copyout_vmspace(vm, kaddr, uaddr, len);
297 1.1 pooka uvmspace_free(vm);
298 1.1 pooka
299 1.1 pooka return error;
300 1.1 pooka }
301 1.1 pooka
302 1.1 pooka /*
303 1.8 chs * Like copyin(), but operates on an arbitrary pid.
304 1.8 chs */
305 1.8 chs int
306 1.8 chs copyin_pid(pid_t pid, const void *uaddr, void *kaddr, size_t len)
307 1.8 chs {
308 1.8 chs struct proc *p;
309 1.8 chs struct vmspace *vm;
310 1.8 chs int error;
311 1.8 chs
312 1.8 chs mutex_enter(proc_lock);
313 1.8 chs p = proc_find(pid);
314 1.8 chs if (p == NULL) {
315 1.8 chs mutex_exit(proc_lock);
316 1.8 chs return ESRCH;
317 1.8 chs }
318 1.8 chs mutex_enter(p->p_lock);
319 1.8 chs proc_vmspace_getref(p, &vm);
320 1.8 chs mutex_exit(p->p_lock);
321 1.8 chs mutex_exit(proc_lock);
322 1.8 chs
323 1.8 chs error = copyin_vmspace(vm, uaddr, kaddr, len);
324 1.8 chs
325 1.8 chs uvmspace_free(vm);
326 1.8 chs return error;
327 1.8 chs }
328 1.8 chs
329 1.8 chs /*
330 1.1 pooka * Like copyin(), except it operates on kernel addresses when the FKIOCTL
331 1.1 pooka * flag is passed in `ioctlflags' from the ioctl call.
332 1.1 pooka */
333 1.1 pooka int
334 1.1 pooka ioctl_copyin(int ioctlflags, const void *src, void *dst, size_t len)
335 1.1 pooka {
336 1.1 pooka if (ioctlflags & FKIOCTL)
337 1.1 pooka return kcopy(src, dst, len);
338 1.1 pooka return copyin(src, dst, len);
339 1.1 pooka }
340 1.1 pooka
341 1.1 pooka /*
342 1.1 pooka * Like copyout(), except it operates on kernel addresses when the FKIOCTL
343 1.1 pooka * flag is passed in `ioctlflags' from the ioctl call.
344 1.1 pooka */
345 1.1 pooka int
346 1.1 pooka ioctl_copyout(int ioctlflags, const void *src, void *dst, size_t len)
347 1.1 pooka {
348 1.1 pooka if (ioctlflags & FKIOCTL)
349 1.1 pooka return kcopy(src, dst, len);
350 1.1 pooka return copyout(src, dst, len);
351 1.1 pooka }
352