xref: /freebsd/sys/kern/subr_uio.c (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Copyright (c) 2014 The FreeBSD Foundation
11  *
12  * Portions of this software were developed by Konstantin Belousov
13  * under sponsorship from the FreeBSD Foundation.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
40  */
41 
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44 
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 	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 
220 	if (uio->uio_segflg == UIO_USERSPACE) {
221 		newflags = TDP_DEADLKTREAT;
222 		if (nofault) {
223 			/*
224 			 * Fail if a non-spurious page fault occurs.
225 			 */
226 			newflags |= TDP_NOFAULTING | TDP_RESETSPUR;
227 		} else {
228 			WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
229 			    "Calling uiomove()");
230 		}
231 		save = curthread_pflags_set(newflags);
232 	} else {
233 		KASSERT(nofault == 0, ("uiomove: nofault"));
234 	}
235 
236 	while (n > 0 && uio->uio_resid) {
237 		iov = uio->uio_iov;
238 		cnt = iov->iov_len;
239 		if (cnt == 0) {
240 			uio->uio_iov++;
241 			uio->uio_iovcnt--;
242 			continue;
243 		}
244 		if (cnt > n)
245 			cnt = n;
246 
247 		switch (uio->uio_segflg) {
248 
249 		case UIO_USERSPACE:
250 			maybe_yield();
251 			if (uio->uio_rw == UIO_READ)
252 				error = copyout(cp, iov->iov_base, cnt);
253 			else
254 				error = copyin(iov->iov_base, cp, cnt);
255 			if (error)
256 				goto out;
257 			break;
258 
259 		case UIO_SYSSPACE:
260 			if (uio->uio_rw == UIO_READ)
261 				bcopy(cp, iov->iov_base, cnt);
262 			else
263 				bcopy(iov->iov_base, cp, cnt);
264 			break;
265 		case UIO_NOCOPY:
266 			break;
267 		}
268 		iov->iov_base = (char *)iov->iov_base + cnt;
269 		iov->iov_len -= cnt;
270 		uio->uio_resid -= cnt;
271 		uio->uio_offset += cnt;
272 		cp = (char *)cp + cnt;
273 		n -= cnt;
274 	}
275 out:
276 	if (uio->uio_segflg == UIO_USERSPACE)
277 		curthread_pflags_restore(save);
278 	return (error);
279 }
280 
281 /*
282  * Wrapper for uiomove() that validates the arguments against a known-good
283  * kernel buffer.  Currently, uiomove accepts a signed (n) argument, which
284  * is almost definitely a bad thing, so we catch that here as well.  We
285  * return a runtime failure, but it might be desirable to generate a runtime
286  * assertion failure instead.
287  */
288 int
289 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
290 {
291 	size_t offset, n;
292 
293 	if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
294 	    (offset = uio->uio_offset) != uio->uio_offset)
295 		return (EINVAL);
296 	if (buflen <= 0 || offset >= buflen)
297 		return (0);
298 	if ((n = buflen - offset) > IOSIZE_MAX)
299 		return (EINVAL);
300 	return (uiomove((char *)buf + offset, n, uio));
301 }
302 
303 /*
304  * Give next character to user as result of read.
305  */
306 int
307 ureadc(int c, struct uio *uio)
308 {
309 	struct iovec *iov;
310 	char *iov_base;
311 
312 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
313 	    "Calling ureadc()");
314 
315 again:
316 	if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
317 		panic("ureadc");
318 	iov = uio->uio_iov;
319 	if (iov->iov_len == 0) {
320 		uio->uio_iovcnt--;
321 		uio->uio_iov++;
322 		goto again;
323 	}
324 	switch (uio->uio_segflg) {
325 
326 	case UIO_USERSPACE:
327 		if (subyte(iov->iov_base, c) < 0)
328 			return (EFAULT);
329 		break;
330 
331 	case UIO_SYSSPACE:
332 		iov_base = iov->iov_base;
333 		*iov_base = c;
334 		break;
335 
336 	case UIO_NOCOPY:
337 		break;
338 	}
339 	iov->iov_base = (char *)iov->iov_base + 1;
340 	iov->iov_len--;
341 	uio->uio_resid--;
342 	uio->uio_offset++;
343 	return (0);
344 }
345 
346 int
347 copyinfrom(const void * __restrict src, void * __restrict dst, size_t len,
348     int seg)
349 {
350 	int error = 0;
351 
352 	switch (seg) {
353 	case UIO_USERSPACE:
354 		error = copyin(src, dst, len);
355 		break;
356 	case UIO_SYSSPACE:
357 		bcopy(src, dst, len);
358 		break;
359 	default:
360 		panic("copyinfrom: bad seg %d\n", seg);
361 	}
362 	return (error);
363 }
364 
365 int
366 copyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len,
367     size_t * __restrict copied, int seg)
368 {
369 	int error = 0;
370 
371 	switch (seg) {
372 	case UIO_USERSPACE:
373 		error = copyinstr(src, dst, len, copied);
374 		break;
375 	case UIO_SYSSPACE:
376 		error = copystr(src, dst, len, copied);
377 		break;
378 	default:
379 		panic("copyinstrfrom: bad seg %d\n", seg);
380 	}
381 	return (error);
382 }
383 
384 int
385 copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
386 {
387 	u_int iovlen;
388 
389 	*iov = NULL;
390 	if (iovcnt > UIO_MAXIOV)
391 		return (error);
392 	iovlen = iovcnt * sizeof (struct iovec);
393 	*iov = malloc(iovlen, M_IOV, M_WAITOK);
394 	error = copyin(iovp, *iov, iovlen);
395 	if (error) {
396 		free(*iov, M_IOV);
397 		*iov = NULL;
398 	}
399 	return (error);
400 }
401 
402 int
403 copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
404 {
405 	struct iovec *iov;
406 	struct uio *uio;
407 	u_int iovlen;
408 	int error, i;
409 
410 	*uiop = NULL;
411 	if (iovcnt > UIO_MAXIOV)
412 		return (EINVAL);
413 	iovlen = iovcnt * sizeof (struct iovec);
414 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
415 	iov = (struct iovec *)(uio + 1);
416 	error = copyin(iovp, iov, iovlen);
417 	if (error) {
418 		free(uio, M_IOV);
419 		return (error);
420 	}
421 	uio->uio_iov = iov;
422 	uio->uio_iovcnt = iovcnt;
423 	uio->uio_segflg = UIO_USERSPACE;
424 	uio->uio_offset = -1;
425 	uio->uio_resid = 0;
426 	for (i = 0; i < iovcnt; i++) {
427 		if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) {
428 			free(uio, M_IOV);
429 			return (EINVAL);
430 		}
431 		uio->uio_resid += iov->iov_len;
432 		iov++;
433 	}
434 	*uiop = uio;
435 	return (0);
436 }
437 
438 struct uio *
439 cloneuio(struct uio *uiop)
440 {
441 	struct uio *uio;
442 	int iovlen;
443 
444 	iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
445 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
446 	*uio = *uiop;
447 	uio->uio_iov = (struct iovec *)(uio + 1);
448 	bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
449 	return (uio);
450 }
451 
452 /*
453  * Map some anonymous memory in user space of size sz, rounded up to the page
454  * boundary.
455  */
456 int
457 copyout_map(struct thread *td, vm_offset_t *addr, size_t sz)
458 {
459 	struct vmspace *vms;
460 	int error;
461 	vm_size_t size;
462 
463 	vms = td->td_proc->p_vmspace;
464 
465 	/*
466 	 * Map somewhere after heap in process memory.
467 	 */
468 	*addr = round_page((vm_offset_t)vms->vm_daddr +
469 	    lim_max(td, RLIMIT_DATA));
470 
471 	/* round size up to page boundary */
472 	size = (vm_size_t)round_page(sz);
473 	if (size == 0)
474 		return (EINVAL);
475 	error = vm_mmap_object(&vms->vm_map, addr, size, VM_PROT_READ |
476 	    VM_PROT_WRITE, VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, NULL, 0,
477 	    FALSE, td);
478 	return (error);
479 }
480 
481 /*
482  * Unmap memory in user space.
483  */
484 int
485 copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz)
486 {
487 	vm_map_t map;
488 	vm_size_t size;
489 
490 	if (sz == 0)
491 		return (0);
492 
493 	map = &td->td_proc->p_vmspace->vm_map;
494 	size = (vm_size_t)round_page(sz);
495 
496 	if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS)
497 		return (EINVAL);
498 
499 	return (0);
500 }
501 
502 #ifdef NO_FUEWORD
503 /*
504  * XXXKIB The temporal implementation of fue*() functions which do not
505  * handle usermode -1 properly, mixing it with the fault code.  Keep
506  * this until MD code is written.  Currently sparc64 and mips do not
507  * have proper implementation.
508  */
509 
510 int
511 fueword(volatile const void *base, long *val)
512 {
513 	long res;
514 
515 	res = fuword(base);
516 	if (res == -1)
517 		return (-1);
518 	*val = res;
519 	return (0);
520 }
521 
522 int
523 fueword32(volatile const void *base, int32_t *val)
524 {
525 	int32_t res;
526 
527 	res = fuword32(base);
528 	if (res == -1)
529 		return (-1);
530 	*val = res;
531 	return (0);
532 }
533 
534 #ifdef _LP64
535 int
536 fueword64(volatile const void *base, int64_t *val)
537 {
538 	int64_t res;
539 
540 	res = fuword64(base);
541 	if (res == -1)
542 		return (-1);
543 	*val = res;
544 	return (0);
545 }
546 #endif
547 
548 int
549 casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp,
550     uint32_t newval)
551 {
552 	int32_t ov;
553 
554 	ov = casuword32(base, oldval, newval);
555 	if (ov == -1)
556 		return (-1);
557 	*oldvalp = ov;
558 	return (0);
559 }
560 
561 int
562 casueword(volatile u_long *p, u_long oldval, u_long *oldvalp, u_long newval)
563 {
564 	u_long ov;
565 
566 	ov = casuword(p, oldval, newval);
567 	if (ov == -1)
568 		return (-1);
569 	*oldvalp = ov;
570 	return (0);
571 }
572 #else /* NO_FUEWORD */
573 int32_t
574 fuword32(volatile const void *addr)
575 {
576 	int rv;
577 	int32_t val;
578 
579 	rv = fueword32(addr, &val);
580 	return (rv == -1 ? -1 : val);
581 }
582 
583 #ifdef _LP64
584 int64_t
585 fuword64(volatile const void *addr)
586 {
587 	int rv;
588 	int64_t val;
589 
590 	rv = fueword64(addr, &val);
591 	return (rv == -1 ? -1 : val);
592 }
593 #endif /* _LP64 */
594 
595 long
596 fuword(volatile const void *addr)
597 {
598 	long val;
599 	int rv;
600 
601 	rv = fueword(addr, &val);
602 	return (rv == -1 ? -1 : val);
603 }
604 
605 uint32_t
606 casuword32(volatile uint32_t *addr, uint32_t old, uint32_t new)
607 {
608 	int rv;
609 	uint32_t val;
610 
611 	rv = casueword32(addr, old, &val, new);
612 	return (rv == -1 ? -1 : val);
613 }
614 
615 u_long
616 casuword(volatile u_long *addr, u_long old, u_long new)
617 {
618 	int rv;
619 	u_long val;
620 
621 	rv = casueword(addr, old, &val, new);
622 	return (rv == -1 ? -1 : val);
623 }
624 
625 #endif /* NO_FUEWORD */
626