xref: /freebsd/sys/kern/subr_uio.c (revision 5a0bba9007c527b18db7f9b64f06b486cda4fe9d)
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  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_zero.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/proc.h>
49 #include <sys/sched.h>
50 #include <sys/sysctl.h>
51 #include <sys/vnode.h>
52 
53 #include <vm/vm.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_map.h>
56 #ifdef ZERO_COPY_SOCKETS
57 #include <vm/vm_param.h>
58 #include <vm/vm_object.h>
59 #endif
60 
61 SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
62 	"Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
63 
64 #ifdef ZERO_COPY_SOCKETS
65 /* Declared in uipc_socket.c */
66 extern int so_zero_copy_receive;
67 
68 /*
69  * Identify the physical page mapped at the given kernel virtual
70  * address.  Insert this physical page into the given address space at
71  * the given virtual address, replacing the physical page, if any,
72  * that already exists there.
73  */
74 static int
75 vm_pgmoveco(vm_map_t mapa, vm_offset_t kaddr, vm_offset_t uaddr)
76 {
77 	vm_map_t map = mapa;
78 	vm_page_t kern_pg, user_pg;
79 	vm_object_t uobject;
80 	vm_map_entry_t entry;
81 	vm_pindex_t upindex;
82 	vm_prot_t prot;
83 	boolean_t wired;
84 
85 	KASSERT((uaddr & PAGE_MASK) == 0,
86 	    ("vm_pgmoveco: uaddr is not page aligned"));
87 
88 	/*
89 	 * Herein the physical page is validated and dirtied.  It is
90 	 * unwired in sf_buf_mext().
91 	 */
92 	kern_pg = PHYS_TO_VM_PAGE(vtophys(kaddr));
93 	kern_pg->valid = VM_PAGE_BITS_ALL;
94 	KASSERT(kern_pg->queue == PQ_NONE && kern_pg->wire_count == 1,
95 	    ("vm_pgmoveco: kern_pg is not correctly wired"));
96 
97 	if ((vm_map_lookup(&map, uaddr,
98 			   VM_PROT_WRITE, &entry, &uobject,
99 			   &upindex, &prot, &wired)) != KERN_SUCCESS) {
100 		return(EFAULT);
101 	}
102 	VM_OBJECT_LOCK(uobject);
103 retry:
104 	if ((user_pg = vm_page_lookup(uobject, upindex)) != NULL) {
105 		if (vm_page_sleep_if_busy(user_pg, TRUE, "vm_pgmoveco"))
106 			goto retry;
107 		vm_page_lock(user_pg);
108 		pmap_remove_all(user_pg);
109 		vm_page_free(user_pg);
110 		vm_page_unlock(user_pg);
111 	} else {
112 		/*
113 		 * Even if a physical page does not exist in the
114 		 * object chain's first object, a physical page from a
115 		 * backing object may be mapped read only.
116 		 */
117 		if (uobject->backing_object != NULL)
118 			pmap_remove(map->pmap, uaddr, uaddr + PAGE_SIZE);
119 	}
120 	vm_page_insert(kern_pg, uobject, upindex);
121 	vm_page_dirty(kern_pg);
122 	VM_OBJECT_UNLOCK(uobject);
123 	vm_map_lookup_done(map, entry);
124 	return(KERN_SUCCESS);
125 }
126 #endif /* ZERO_COPY_SOCKETS */
127 
128 int
129 uiomove(void *cp, int n, struct uio *uio)
130 {
131 	struct thread *td = curthread;
132 	struct iovec *iov;
133 	u_int cnt;
134 	int error = 0;
135 	int save = 0;
136 
137 	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
138 	    ("uiomove: mode"));
139 	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
140 	    ("uiomove proc"));
141 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
142 	    "Calling uiomove()");
143 
144 	save = td->td_pflags & TDP_DEADLKTREAT;
145 	td->td_pflags |= TDP_DEADLKTREAT;
146 
147 	while (n > 0 && uio->uio_resid) {
148 		iov = uio->uio_iov;
149 		cnt = iov->iov_len;
150 		if (cnt == 0) {
151 			uio->uio_iov++;
152 			uio->uio_iovcnt--;
153 			continue;
154 		}
155 		if (cnt > n)
156 			cnt = n;
157 
158 		switch (uio->uio_segflg) {
159 
160 		case UIO_USERSPACE:
161 			maybe_yield();
162 			if (uio->uio_rw == UIO_READ)
163 				error = copyout(cp, iov->iov_base, cnt);
164 			else
165 				error = copyin(iov->iov_base, cp, cnt);
166 			if (error)
167 				goto out;
168 			break;
169 
170 		case UIO_SYSSPACE:
171 			if (uio->uio_rw == UIO_READ)
172 				bcopy(cp, iov->iov_base, cnt);
173 			else
174 				bcopy(iov->iov_base, cp, cnt);
175 			break;
176 		case UIO_NOCOPY:
177 			break;
178 		}
179 		iov->iov_base = (char *)iov->iov_base + cnt;
180 		iov->iov_len -= cnt;
181 		uio->uio_resid -= cnt;
182 		uio->uio_offset += cnt;
183 		cp = (char *)cp + cnt;
184 		n -= cnt;
185 	}
186 out:
187 	if (save == 0)
188 		td->td_pflags &= ~TDP_DEADLKTREAT;
189 	return (error);
190 }
191 
192 /*
193  * Wrapper for uiomove() that validates the arguments against a known-good
194  * kernel buffer.  Currently, uiomove accepts a signed (n) argument, which
195  * is almost definitely a bad thing, so we catch that here as well.  We
196  * return a runtime failure, but it might be desirable to generate a runtime
197  * assertion failure instead.
198  */
199 int
200 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
201 {
202 	unsigned int offset, n;
203 
204 	if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
205 	    (offset = uio->uio_offset) != uio->uio_offset)
206 		return (EINVAL);
207 	if (buflen <= 0 || offset >= buflen)
208 		return (0);
209 	if ((n = buflen - offset) > INT_MAX)
210 		return (EINVAL);
211 	return (uiomove((char *)buf + offset, n, uio));
212 }
213 
214 #ifdef ZERO_COPY_SOCKETS
215 /*
216  * Experimental support for zero-copy I/O
217  */
218 static int
219 userspaceco(void *cp, u_int cnt, struct uio *uio, int disposable)
220 {
221 	struct iovec *iov;
222 	int error;
223 
224 	iov = uio->uio_iov;
225 	if (uio->uio_rw == UIO_READ) {
226 		if ((so_zero_copy_receive != 0)
227 		 && ((cnt & PAGE_MASK) == 0)
228 		 && ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0)
229 		 && ((uio->uio_offset & PAGE_MASK) == 0)
230 		 && ((((intptr_t) cp) & PAGE_MASK) == 0)
231 		 && (disposable != 0)) {
232 			/* SOCKET: use page-trading */
233 			/*
234 			 * We only want to call vm_pgmoveco() on
235 			 * disposeable pages, since it gives the
236 			 * kernel page to the userland process.
237 			 */
238 			error =	vm_pgmoveco(&curproc->p_vmspace->vm_map,
239 			    (vm_offset_t)cp, (vm_offset_t)iov->iov_base);
240 
241 			/*
242 			 * If we get an error back, attempt
243 			 * to use copyout() instead.  The
244 			 * disposable page should be freed
245 			 * automatically if we weren't able to move
246 			 * it into userland.
247 			 */
248 			if (error != 0)
249 				error = copyout(cp, iov->iov_base, cnt);
250 		} else {
251 			error = copyout(cp, iov->iov_base, cnt);
252 		}
253 	} else {
254 		error = copyin(iov->iov_base, cp, cnt);
255 	}
256 	return (error);
257 }
258 
259 int
260 uiomoveco(void *cp, int n, struct uio *uio, int disposable)
261 {
262 	struct iovec *iov;
263 	u_int cnt;
264 	int error;
265 
266 	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
267 	    ("uiomoveco: mode"));
268 	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
269 	    ("uiomoveco proc"));
270 
271 	while (n > 0 && uio->uio_resid) {
272 		iov = uio->uio_iov;
273 		cnt = iov->iov_len;
274 		if (cnt == 0) {
275 			uio->uio_iov++;
276 			uio->uio_iovcnt--;
277 			continue;
278 		}
279 		if (cnt > n)
280 			cnt = n;
281 
282 		switch (uio->uio_segflg) {
283 
284 		case UIO_USERSPACE:
285 			maybe_yield();
286 			error = userspaceco(cp, cnt, uio, disposable);
287 			if (error)
288 				return (error);
289 			break;
290 
291 		case UIO_SYSSPACE:
292 			if (uio->uio_rw == UIO_READ)
293 				bcopy(cp, iov->iov_base, cnt);
294 			else
295 				bcopy(iov->iov_base, cp, cnt);
296 			break;
297 		case UIO_NOCOPY:
298 			break;
299 		}
300 		iov->iov_base = (char *)iov->iov_base + cnt;
301 		iov->iov_len -= cnt;
302 		uio->uio_resid -= cnt;
303 		uio->uio_offset += cnt;
304 		cp = (char *)cp + cnt;
305 		n -= cnt;
306 	}
307 	return (0);
308 }
309 #endif /* ZERO_COPY_SOCKETS */
310 
311 /*
312  * Give next character to user as result of read.
313  */
314 int
315 ureadc(int c, struct uio *uio)
316 {
317 	struct iovec *iov;
318 	char *iov_base;
319 
320 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
321 	    "Calling ureadc()");
322 
323 again:
324 	if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
325 		panic("ureadc");
326 	iov = uio->uio_iov;
327 	if (iov->iov_len == 0) {
328 		uio->uio_iovcnt--;
329 		uio->uio_iov++;
330 		goto again;
331 	}
332 	switch (uio->uio_segflg) {
333 
334 	case UIO_USERSPACE:
335 		if (subyte(iov->iov_base, c) < 0)
336 			return (EFAULT);
337 		break;
338 
339 	case UIO_SYSSPACE:
340 		iov_base = iov->iov_base;
341 		*iov_base = c;
342 		iov->iov_base = iov_base;
343 		break;
344 
345 	case UIO_NOCOPY:
346 		break;
347 	}
348 	iov->iov_base = (char *)iov->iov_base + 1;
349 	iov->iov_len--;
350 	uio->uio_resid--;
351 	uio->uio_offset++;
352 	return (0);
353 }
354 
355 int
356 should_yield(void)
357 {
358 
359 	return (ticks - PCPU_GET(switchticks) >= hogticks);
360 }
361 
362 void
363 maybe_yield(void)
364 {
365 
366 	if (should_yield())
367 		uio_yield();
368 }
369 
370 void
371 uio_yield(void)
372 {
373 	struct thread *td;
374 
375 	td = curthread;
376 	DROP_GIANT();
377 	thread_lock(td);
378 	sched_prio(td, td->td_user_pri);
379 	mi_switch(SW_INVOL | SWT_RELINQUISH, NULL);
380 	thread_unlock(td);
381 	PICKUP_GIANT();
382 }
383 
384 int
385 copyinfrom(const void * __restrict src, void * __restrict dst, size_t len,
386     int seg)
387 {
388 	int error = 0;
389 
390 	switch (seg) {
391 	case UIO_USERSPACE:
392 		error = copyin(src, dst, len);
393 		break;
394 	case UIO_SYSSPACE:
395 		bcopy(src, dst, len);
396 		break;
397 	default:
398 		panic("copyinfrom: bad seg %d\n", seg);
399 	}
400 	return (error);
401 }
402 
403 int
404 copyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len,
405     size_t * __restrict copied, int seg)
406 {
407 	int error = 0;
408 
409 	switch (seg) {
410 	case UIO_USERSPACE:
411 		error = copyinstr(src, dst, len, copied);
412 		break;
413 	case UIO_SYSSPACE:
414 		error = copystr(src, dst, len, copied);
415 		break;
416 	default:
417 		panic("copyinstrfrom: bad seg %d\n", seg);
418 	}
419 	return (error);
420 }
421 
422 int
423 copyiniov(struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
424 {
425 	u_int iovlen;
426 
427 	*iov = NULL;
428 	if (iovcnt > UIO_MAXIOV)
429 		return (error);
430 	iovlen = iovcnt * sizeof (struct iovec);
431 	*iov = malloc(iovlen, M_IOV, M_WAITOK);
432 	error = copyin(iovp, *iov, iovlen);
433 	if (error) {
434 		free(*iov, M_IOV);
435 		*iov = NULL;
436 	}
437 	return (error);
438 }
439 
440 int
441 copyinuio(struct iovec *iovp, u_int iovcnt, struct uio **uiop)
442 {
443 	struct iovec *iov;
444 	struct uio *uio;
445 	u_int iovlen;
446 	int error, i;
447 
448 	*uiop = NULL;
449 	if (iovcnt > UIO_MAXIOV)
450 		return (EINVAL);
451 	iovlen = iovcnt * sizeof (struct iovec);
452 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
453 	iov = (struct iovec *)(uio + 1);
454 	error = copyin(iovp, iov, iovlen);
455 	if (error) {
456 		free(uio, M_IOV);
457 		return (error);
458 	}
459 	uio->uio_iov = iov;
460 	uio->uio_iovcnt = iovcnt;
461 	uio->uio_segflg = UIO_USERSPACE;
462 	uio->uio_offset = -1;
463 	uio->uio_resid = 0;
464 	for (i = 0; i < iovcnt; i++) {
465 		if (iov->iov_len > INT_MAX - uio->uio_resid) {
466 			free(uio, M_IOV);
467 			return (EINVAL);
468 		}
469 		uio->uio_resid += iov->iov_len;
470 		iov++;
471 	}
472 	*uiop = uio;
473 	return (0);
474 }
475 
476 struct uio *
477 cloneuio(struct uio *uiop)
478 {
479 	struct uio *uio;
480 	int iovlen;
481 
482 	iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
483 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
484 	*uio = *uiop;
485 	uio->uio_iov = (struct iovec *)(uio + 1);
486 	bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
487 	return (uio);
488 }
489