xref: /freebsd/sys/kern/subr_uio.c (revision d0b2dbfa)
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 
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 		case UIO_USERSPACE:
249 			maybe_yield();
250 			if (uio->uio_rw == UIO_READ)
251 				error = copyout(cp, iov->iov_base, cnt);
252 			else
253 				error = copyin(iov->iov_base, cp, cnt);
254 			if (error)
255 				goto out;
256 			break;
257 
258 		case UIO_SYSSPACE:
259 			if (uio->uio_rw == UIO_READ)
260 				bcopy(cp, iov->iov_base, cnt);
261 			else
262 				bcopy(iov->iov_base, cp, cnt);
263 			break;
264 		case UIO_NOCOPY:
265 			break;
266 		}
267 		iov->iov_base = (char *)iov->iov_base + cnt;
268 		iov->iov_len -= cnt;
269 		uio->uio_resid -= cnt;
270 		uio->uio_offset += cnt;
271 		cp = (char *)cp + cnt;
272 		n -= cnt;
273 	}
274 out:
275 	if (save)
276 		curthread_pflags_restore(save);
277 	return (error);
278 }
279 
280 /*
281  * Wrapper for uiomove() that validates the arguments against a known-good
282  * kernel buffer.  Currently, uiomove accepts a signed (n) argument, which
283  * is almost definitely a bad thing, so we catch that here as well.  We
284  * return a runtime failure, but it might be desirable to generate a runtime
285  * assertion failure instead.
286  */
287 int
288 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
289 {
290 	size_t offset, n;
291 
292 	if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
293 	    (offset = uio->uio_offset) != uio->uio_offset)
294 		return (EINVAL);
295 	if (buflen <= 0 || offset >= buflen)
296 		return (0);
297 	if ((n = buflen - offset) > IOSIZE_MAX)
298 		return (EINVAL);
299 	return (uiomove((char *)buf + offset, n, uio));
300 }
301 
302 /*
303  * Give next character to user as result of read.
304  */
305 int
306 ureadc(int c, struct uio *uio)
307 {
308 	struct iovec *iov;
309 	char *iov_base;
310 
311 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
312 	    "Calling ureadc()");
313 
314 again:
315 	if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
316 		panic("ureadc");
317 	iov = uio->uio_iov;
318 	if (iov->iov_len == 0) {
319 		uio->uio_iovcnt--;
320 		uio->uio_iov++;
321 		goto again;
322 	}
323 	switch (uio->uio_segflg) {
324 	case UIO_USERSPACE:
325 		if (subyte(iov->iov_base, c) < 0)
326 			return (EFAULT);
327 		break;
328 
329 	case UIO_SYSSPACE:
330 		iov_base = iov->iov_base;
331 		*iov_base = c;
332 		break;
333 
334 	case UIO_NOCOPY:
335 		break;
336 	}
337 	iov->iov_base = (char *)iov->iov_base + 1;
338 	iov->iov_len--;
339 	uio->uio_resid--;
340 	uio->uio_offset++;
341 	return (0);
342 }
343 
344 int
345 copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
346 {
347 	u_int iovlen;
348 
349 	*iov = NULL;
350 	if (iovcnt > UIO_MAXIOV)
351 		return (error);
352 	iovlen = iovcnt * sizeof (struct iovec);
353 	*iov = malloc(iovlen, M_IOV, M_WAITOK);
354 	error = copyin(iovp, *iov, iovlen);
355 	if (error) {
356 		free(*iov, M_IOV);
357 		*iov = NULL;
358 	}
359 	return (error);
360 }
361 
362 int
363 copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
364 {
365 	struct iovec *iov;
366 	struct uio *uio;
367 	u_int iovlen;
368 	int error, i;
369 
370 	*uiop = NULL;
371 	if (iovcnt > UIO_MAXIOV)
372 		return (EINVAL);
373 	iovlen = iovcnt * sizeof (struct iovec);
374 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
375 	iov = (struct iovec *)(uio + 1);
376 	error = copyin(iovp, iov, iovlen);
377 	if (error) {
378 		free(uio, M_IOV);
379 		return (error);
380 	}
381 	uio->uio_iov = iov;
382 	uio->uio_iovcnt = iovcnt;
383 	uio->uio_segflg = UIO_USERSPACE;
384 	uio->uio_offset = -1;
385 	uio->uio_resid = 0;
386 	for (i = 0; i < iovcnt; i++) {
387 		if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) {
388 			free(uio, M_IOV);
389 			return (EINVAL);
390 		}
391 		uio->uio_resid += iov->iov_len;
392 		iov++;
393 	}
394 	*uiop = uio;
395 	return (0);
396 }
397 
398 struct uio *
399 cloneuio(struct uio *uiop)
400 {
401 	struct uio *uio;
402 	int iovlen;
403 
404 	iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
405 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
406 	*uio = *uiop;
407 	uio->uio_iov = (struct iovec *)(uio + 1);
408 	bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
409 	return (uio);
410 }
411 
412 /*
413  * Map some anonymous memory in user space of size sz, rounded up to the page
414  * boundary.
415  */
416 int
417 copyout_map(struct thread *td, vm_offset_t *addr, size_t sz)
418 {
419 	struct vmspace *vms;
420 	int error;
421 	vm_size_t size;
422 
423 	vms = td->td_proc->p_vmspace;
424 
425 	/*
426 	 * Map somewhere after heap in process memory.
427 	 */
428 	*addr = round_page((vm_offset_t)vms->vm_daddr +
429 	    lim_max(td, RLIMIT_DATA));
430 
431 	/* round size up to page boundary */
432 	size = (vm_size_t)round_page(sz);
433 	if (size == 0)
434 		return (EINVAL);
435 	error = vm_mmap_object(&vms->vm_map, addr, size, VM_PROT_READ |
436 	    VM_PROT_WRITE, VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, NULL, 0,
437 	    FALSE, td);
438 	return (error);
439 }
440 
441 /*
442  * Unmap memory in user space.
443  */
444 int
445 copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz)
446 {
447 	vm_map_t map;
448 	vm_size_t size;
449 
450 	if (sz == 0)
451 		return (0);
452 
453 	map = &td->td_proc->p_vmspace->vm_map;
454 	size = (vm_size_t)round_page(sz);
455 
456 	if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS)
457 		return (EINVAL);
458 
459 	return (0);
460 }
461 
462 int32_t
463 fuword32(volatile const void *addr)
464 {
465 	int rv;
466 	int32_t val;
467 
468 	rv = fueword32(addr, &val);
469 	return (rv == -1 ? -1 : val);
470 }
471 
472 #ifdef _LP64
473 int64_t
474 fuword64(volatile const void *addr)
475 {
476 	int rv;
477 	int64_t val;
478 
479 	rv = fueword64(addr, &val);
480 	return (rv == -1 ? -1 : val);
481 }
482 #endif /* _LP64 */
483 
484 long
485 fuword(volatile const void *addr)
486 {
487 	long val;
488 	int rv;
489 
490 	rv = fueword(addr, &val);
491 	return (rv == -1 ? -1 : val);
492 }
493 
494 uint32_t
495 casuword32(volatile uint32_t *addr, uint32_t old, uint32_t new)
496 {
497 	int rv;
498 	uint32_t val;
499 
500 	rv = casueword32(addr, old, &val, new);
501 	return (rv == -1 ? -1 : val);
502 }
503 
504 u_long
505 casuword(volatile u_long *addr, u_long old, u_long new)
506 {
507 	int rv;
508 	u_long val;
509 
510 	rv = casueword(addr, old, &val, new);
511 	return (rv == -1 ? -1 : val);
512 }
513