xref: /freebsd/sys/kern/subr_uio.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Copyright (c) 2014 The FreeBSD Foundation
13  *
14  * Portions of this software were developed by Konstantin Belousov
15  * under sponsorship from the FreeBSD Foundation.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
42  */
43 
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
49 #include <sys/lock.h>
50 #include <sys/mman.h>
51 #include <sys/proc.h>
52 #include <sys/resourcevar.h>
53 #include <sys/rwlock.h>
54 #include <sys/sched.h>
55 #include <sys/sysctl.h>
56 #include <sys/vnode.h>
57 
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60 #include <vm/vm_extern.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_pageout.h>
63 #include <vm/vm_map.h>
64 
65 #include <machine/bus.h>
66 
67 SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, UIO_MAXIOV,
68 	"Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
69 
70 static int uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault);
71 
72 int
73 copyin_nofault(const void *udaddr, void *kaddr, size_t len)
74 {
75 	int error, save;
76 
77 	save = vm_fault_disable_pagefaults();
78 	error = copyin(udaddr, kaddr, len);
79 	vm_fault_enable_pagefaults(save);
80 	return (error);
81 }
82 
83 int
84 copyout_nofault(const void *kaddr, void *udaddr, size_t len)
85 {
86 	int error, save;
87 
88 	save = vm_fault_disable_pagefaults();
89 	error = copyout(kaddr, udaddr, len);
90 	vm_fault_enable_pagefaults(save);
91 	return (error);
92 }
93 
94 #define	PHYS_PAGE_COUNT(len)	(howmany(len, PAGE_SIZE) + 1)
95 
96 int
97 physcopyin(void *src, vm_paddr_t dst, size_t len)
98 {
99 	vm_page_t m[PHYS_PAGE_COUNT(len)];
100 	struct iovec iov[1];
101 	struct uio uio;
102 	int i;
103 
104 	iov[0].iov_base = src;
105 	iov[0].iov_len = len;
106 	uio.uio_iov = iov;
107 	uio.uio_iovcnt = 1;
108 	uio.uio_offset = 0;
109 	uio.uio_resid = len;
110 	uio.uio_segflg = UIO_SYSSPACE;
111 	uio.uio_rw = UIO_WRITE;
112 	for (i = 0; i < PHYS_PAGE_COUNT(len); i++, dst += PAGE_SIZE)
113 		m[i] = PHYS_TO_VM_PAGE(dst);
114 	return (uiomove_fromphys(m, dst & PAGE_MASK, len, &uio));
115 }
116 
117 int
118 physcopyout(vm_paddr_t src, void *dst, size_t len)
119 {
120 	vm_page_t m[PHYS_PAGE_COUNT(len)];
121 	struct iovec iov[1];
122 	struct uio uio;
123 	int i;
124 
125 	iov[0].iov_base = dst;
126 	iov[0].iov_len = len;
127 	uio.uio_iov = iov;
128 	uio.uio_iovcnt = 1;
129 	uio.uio_offset = 0;
130 	uio.uio_resid = len;
131 	uio.uio_segflg = UIO_SYSSPACE;
132 	uio.uio_rw = UIO_READ;
133 	for (i = 0; i < PHYS_PAGE_COUNT(len); i++, src += PAGE_SIZE)
134 		m[i] = PHYS_TO_VM_PAGE(src);
135 	return (uiomove_fromphys(m, src & PAGE_MASK, len, &uio));
136 }
137 
138 #undef PHYS_PAGE_COUNT
139 
140 int
141 physcopyin_vlist(bus_dma_segment_t *src, off_t offset, vm_paddr_t dst,
142     size_t len)
143 {
144 	size_t seg_len;
145 	int error;
146 
147 	error = 0;
148 	while (offset >= src->ds_len) {
149 		offset -= src->ds_len;
150 		src++;
151 	}
152 
153 	while (len > 0 && error == 0) {
154 		seg_len = MIN(src->ds_len - offset, len);
155 		error = physcopyin((void *)(uintptr_t)(src->ds_addr + offset),
156 		    dst, seg_len);
157 		offset = 0;
158 		src++;
159 		len -= seg_len;
160 		dst += seg_len;
161 	}
162 
163 	return (error);
164 }
165 
166 int
167 physcopyout_vlist(vm_paddr_t src, bus_dma_segment_t *dst, off_t offset,
168     size_t len)
169 {
170 	size_t seg_len;
171 	int error;
172 
173 	error = 0;
174 	while (offset >= dst->ds_len) {
175 		offset -= dst->ds_len;
176 		dst++;
177 	}
178 
179 	while (len > 0 && error == 0) {
180 		seg_len = MIN(dst->ds_len - offset, len);
181 		error = physcopyout(src, (void *)(uintptr_t)(dst->ds_addr +
182 		    offset), seg_len);
183 		offset = 0;
184 		dst++;
185 		len -= seg_len;
186 		src += seg_len;
187 	}
188 
189 	return (error);
190 }
191 
192 int
193 uiomove(void *cp, int n, struct uio *uio)
194 {
195 
196 	return (uiomove_faultflag(cp, n, uio, 0));
197 }
198 
199 int
200 uiomove_nofault(void *cp, int n, struct uio *uio)
201 {
202 
203 	return (uiomove_faultflag(cp, n, uio, 1));
204 }
205 
206 static int
207 uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault)
208 {
209 	struct iovec *iov;
210 	size_t cnt;
211 	int error, newflags, save;
212 
213 	save = error = 0;
214 
215 	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
216 	    ("uiomove: mode"));
217 	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
218 	    ("uiomove proc"));
219 	KASSERT(uio->uio_resid >= 0,
220 	    ("%s: uio %p resid underflow", __func__, uio));
221 
222 	if (uio->uio_segflg == UIO_USERSPACE) {
223 		newflags = TDP_DEADLKTREAT;
224 		if (nofault) {
225 			/*
226 			 * Fail if a non-spurious page fault occurs.
227 			 */
228 			newflags |= TDP_NOFAULTING | TDP_RESETSPUR;
229 		} else {
230 			WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
231 			    "Calling uiomove()");
232 		}
233 		save = curthread_pflags_set(newflags);
234 	} else {
235 		KASSERT(nofault == 0, ("uiomove: nofault"));
236 	}
237 
238 	while (n > 0 && uio->uio_resid) {
239 		KASSERT(uio->uio_iovcnt > 0,
240 		    ("%s: uio %p iovcnt underflow", __func__, uio));
241 
242 		iov = uio->uio_iov;
243 		cnt = iov->iov_len;
244 		if (cnt == 0) {
245 			uio->uio_iov++;
246 			uio->uio_iovcnt--;
247 			continue;
248 		}
249 		if (cnt > n)
250 			cnt = n;
251 
252 		switch (uio->uio_segflg) {
253 		case UIO_USERSPACE:
254 			maybe_yield();
255 			if (uio->uio_rw == UIO_READ)
256 				error = copyout(cp, iov->iov_base, cnt);
257 			else
258 				error = copyin(iov->iov_base, cp, cnt);
259 			if (error)
260 				goto out;
261 			break;
262 
263 		case UIO_SYSSPACE:
264 			if (uio->uio_rw == UIO_READ)
265 				bcopy(cp, iov->iov_base, cnt);
266 			else
267 				bcopy(iov->iov_base, cp, cnt);
268 			break;
269 		case UIO_NOCOPY:
270 			break;
271 		}
272 		iov->iov_base = (char *)iov->iov_base + cnt;
273 		iov->iov_len -= cnt;
274 		uio->uio_resid -= cnt;
275 		uio->uio_offset += cnt;
276 		cp = (char *)cp + cnt;
277 		n -= cnt;
278 	}
279 out:
280 	if (save)
281 		curthread_pflags_restore(save);
282 	return (error);
283 }
284 
285 /*
286  * Wrapper for uiomove() that validates the arguments against a known-good
287  * kernel buffer.  Currently, uiomove accepts a signed (n) argument, which
288  * is almost definitely a bad thing, so we catch that here as well.  We
289  * return a runtime failure, but it might be desirable to generate a runtime
290  * assertion failure instead.
291  */
292 int
293 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
294 {
295 	size_t offset, n;
296 
297 	if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
298 	    (offset = uio->uio_offset) != uio->uio_offset)
299 		return (EINVAL);
300 	if (buflen <= 0 || offset >= buflen)
301 		return (0);
302 	if ((n = buflen - offset) > IOSIZE_MAX)
303 		return (EINVAL);
304 	return (uiomove((char *)buf + offset, n, uio));
305 }
306 
307 /*
308  * Give next character to user as result of read.
309  */
310 int
311 ureadc(int c, struct uio *uio)
312 {
313 	struct iovec *iov;
314 	char *iov_base;
315 
316 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
317 	    "Calling ureadc()");
318 
319 again:
320 	if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
321 		panic("ureadc");
322 	iov = uio->uio_iov;
323 	if (iov->iov_len == 0) {
324 		uio->uio_iovcnt--;
325 		uio->uio_iov++;
326 		goto again;
327 	}
328 	switch (uio->uio_segflg) {
329 	case UIO_USERSPACE:
330 		if (subyte(iov->iov_base, c) < 0)
331 			return (EFAULT);
332 		break;
333 
334 	case UIO_SYSSPACE:
335 		iov_base = iov->iov_base;
336 		*iov_base = c;
337 		break;
338 
339 	case UIO_NOCOPY:
340 		break;
341 	}
342 	iov->iov_base = (char *)iov->iov_base + 1;
343 	iov->iov_len--;
344 	uio->uio_resid--;
345 	uio->uio_offset++;
346 	return (0);
347 }
348 
349 int
350 copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
351 {
352 	u_int iovlen;
353 
354 	*iov = NULL;
355 	if (iovcnt > UIO_MAXIOV)
356 		return (error);
357 	iovlen = iovcnt * sizeof (struct iovec);
358 	*iov = malloc(iovlen, M_IOV, M_WAITOK);
359 	error = copyin(iovp, *iov, iovlen);
360 	if (error) {
361 		free(*iov, M_IOV);
362 		*iov = NULL;
363 	}
364 	return (error);
365 }
366 
367 int
368 copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
369 {
370 	struct iovec *iov;
371 	struct uio *uio;
372 	u_int iovlen;
373 	int error, i;
374 
375 	*uiop = NULL;
376 	if (iovcnt > UIO_MAXIOV)
377 		return (EINVAL);
378 	iovlen = iovcnt * sizeof (struct iovec);
379 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
380 	iov = (struct iovec *)(uio + 1);
381 	error = copyin(iovp, iov, iovlen);
382 	if (error) {
383 		free(uio, M_IOV);
384 		return (error);
385 	}
386 	uio->uio_iov = iov;
387 	uio->uio_iovcnt = iovcnt;
388 	uio->uio_segflg = UIO_USERSPACE;
389 	uio->uio_offset = -1;
390 	uio->uio_resid = 0;
391 	for (i = 0; i < iovcnt; i++) {
392 		if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) {
393 			free(uio, M_IOV);
394 			return (EINVAL);
395 		}
396 		uio->uio_resid += iov->iov_len;
397 		iov++;
398 	}
399 	*uiop = uio;
400 	return (0);
401 }
402 
403 struct uio *
404 cloneuio(struct uio *uiop)
405 {
406 	struct uio *uio;
407 	int iovlen;
408 
409 	iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
410 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
411 	*uio = *uiop;
412 	uio->uio_iov = (struct iovec *)(uio + 1);
413 	bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
414 	return (uio);
415 }
416 
417 /*
418  * Map some anonymous memory in user space of size sz, rounded up to the page
419  * boundary.
420  */
421 int
422 copyout_map(struct thread *td, vm_offset_t *addr, size_t sz)
423 {
424 	struct vmspace *vms;
425 	int error;
426 	vm_size_t size;
427 
428 	vms = td->td_proc->p_vmspace;
429 
430 	/*
431 	 * Map somewhere after heap in process memory.
432 	 */
433 	*addr = round_page((vm_offset_t)vms->vm_daddr +
434 	    lim_max(td, RLIMIT_DATA));
435 
436 	/* round size up to page boundary */
437 	size = (vm_size_t)round_page(sz);
438 	if (size == 0)
439 		return (EINVAL);
440 	error = vm_mmap_object(&vms->vm_map, addr, size, VM_PROT_READ |
441 	    VM_PROT_WRITE, VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, NULL, 0,
442 	    FALSE, td);
443 	return (error);
444 }
445 
446 /*
447  * Unmap memory in user space.
448  */
449 int
450 copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz)
451 {
452 	vm_map_t map;
453 	vm_size_t size;
454 
455 	if (sz == 0)
456 		return (0);
457 
458 	map = &td->td_proc->p_vmspace->vm_map;
459 	size = (vm_size_t)round_page(sz);
460 
461 	if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS)
462 		return (EINVAL);
463 
464 	return (0);
465 }
466 
467 int32_t
468 fuword32(volatile const void *addr)
469 {
470 	int rv;
471 	int32_t val;
472 
473 	rv = fueword32(addr, &val);
474 	return (rv == -1 ? -1 : val);
475 }
476 
477 #ifdef _LP64
478 int64_t
479 fuword64(volatile const void *addr)
480 {
481 	int rv;
482 	int64_t val;
483 
484 	rv = fueword64(addr, &val);
485 	return (rv == -1 ? -1 : val);
486 }
487 #endif /* _LP64 */
488 
489 long
490 fuword(volatile const void *addr)
491 {
492 	long val;
493 	int rv;
494 
495 	rv = fueword(addr, &val);
496 	return (rv == -1 ? -1 : val);
497 }
498 
499 uint32_t
500 casuword32(volatile uint32_t *addr, uint32_t old, uint32_t new)
501 {
502 	int rv;
503 	uint32_t val;
504 
505 	rv = casueword32(addr, old, &val, new);
506 	return (rv == -1 ? -1 : val);
507 }
508 
509 u_long
510 casuword(volatile u_long *addr, u_long old, u_long new)
511 {
512 	int rv;
513 	u_long val;
514 
515 	rv = casueword(addr, old, &val, new);
516 	return (rv == -1 ? -1 : val);
517 }
518