]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_uio.c
MFC
[FreeBSD/FreeBSD.git] / sys / kern / subr_uio.c
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/mman.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/resourcevar.h>
51 #include <sys/sched.h>
52 #include <sys/sysctl.h>
53 #include <sys/vnode.h>
54
55 #include <vm/vm.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_map.h>
59 #ifdef ZERO_COPY_SOCKETS
60 #include <vm/vm_param.h>
61 #include <vm/vm_object.h>
62 #endif
63
64 SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
65         "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
66
67 static int uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault);
68
69 #ifdef ZERO_COPY_SOCKETS
70 /* Declared in uipc_socket.c */
71 extern int so_zero_copy_receive;
72
73 /*
74  * Identify the physical page mapped at the given kernel virtual
75  * address.  Insert this physical page into the given address space at
76  * the given virtual address, replacing the physical page, if any,
77  * that already exists there.
78  */
79 static int
80 vm_pgmoveco(vm_map_t mapa, vm_offset_t kaddr, vm_offset_t uaddr)
81 {
82         vm_map_t map = mapa;
83         vm_page_t kern_pg, user_pg;
84         vm_object_t uobject;
85         vm_map_entry_t entry;
86         vm_pindex_t upindex;
87         vm_prot_t prot;
88         vm_page_bits_t vbits;
89         boolean_t wired;
90
91         KASSERT((uaddr & PAGE_MASK) == 0,
92             ("vm_pgmoveco: uaddr is not page aligned"));
93
94         /*
95          * Herein the physical page is validated and dirtied.  It is
96          * unwired in sf_buf_mext().
97          */
98         kern_pg = PHYS_TO_VM_PAGE(vtophys(kaddr));
99         vbits = kern_pg->valid;
100         kern_pg->valid = VM_PAGE_BITS_ALL;
101         KASSERT(kern_pg->queue == PQ_NONE && kern_pg->wire_count == 1,
102             ("vm_pgmoveco: kern_pg is not correctly wired"));
103
104         if ((vm_map_lookup(&map, uaddr,
105                            VM_PROT_WRITE, &entry, &uobject,
106                            &upindex, &prot, &wired)) != KERN_SUCCESS) {
107                 return(EFAULT);
108         }
109         VM_OBJECT_LOCK(uobject);
110         if (vm_page_insert(kern_pg, uobject, upindex) != 0) {
111                 kern_pg->valid = vbits;
112                 VM_OBJECT_UNLOCK(uobject);
113                 vm_map_lookup_done(map, entry);
114                 return(ENOMEM);
115         }
116         vm_page_dirty(kern_pg);
117 retry:
118         if ((user_pg = vm_page_lookup(uobject, upindex)) != NULL) {
119                 if (vm_page_sleep_if_busy(user_pg, TRUE, "vm_pgmoveco"))
120                         goto retry;
121                 vm_page_lock(user_pg);
122                 pmap_remove_all(user_pg);
123                 vm_page_free(user_pg);
124                 vm_page_unlock(user_pg);
125         } else {
126                 /*
127                  * Even if a physical page does not exist in the
128                  * object chain's first object, a physical page from a
129                  * backing object may be mapped read only.
130                  */
131                 if (uobject->backing_object != NULL)
132                         pmap_remove(map->pmap, uaddr, uaddr + PAGE_SIZE);
133         }
134         VM_OBJECT_UNLOCK(uobject);
135         vm_map_lookup_done(map, entry);
136         return(KERN_SUCCESS);
137 }
138 #endif /* ZERO_COPY_SOCKETS */
139
140 int
141 copyin_nofault(const void *udaddr, void *kaddr, size_t len)
142 {
143         int error, save;
144
145         save = vm_fault_disable_pagefaults();
146         error = copyin(udaddr, kaddr, len);
147         vm_fault_enable_pagefaults(save);
148         return (error);
149 }
150
151 int
152 copyout_nofault(const void *kaddr, void *udaddr, size_t len)
153 {
154         int error, save;
155
156         save = vm_fault_disable_pagefaults();
157         error = copyout(kaddr, udaddr, len);
158         vm_fault_enable_pagefaults(save);
159         return (error);
160 }
161
162 int
163 uiomove(void *cp, int n, struct uio *uio)
164 {
165
166         return (uiomove_faultflag(cp, n, uio, 0));
167 }
168
169 int
170 uiomove_nofault(void *cp, int n, struct uio *uio)
171 {
172
173         return (uiomove_faultflag(cp, n, uio, 1));
174 }
175
176 static int
177 uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault)
178 {
179         struct thread *td;
180         struct iovec *iov;
181         size_t cnt;
182         int error, newflags, save;
183
184         td = curthread;
185         error = 0;
186
187         KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
188             ("uiomove: mode"));
189         KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == td,
190             ("uiomove proc"));
191         if (!nofault)
192                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
193                     "Calling uiomove()");
194
195         /* XXX does it make a sense to set TDP_DEADLKTREAT for UIO_SYSSPACE ? */
196         newflags = TDP_DEADLKTREAT;
197         if (uio->uio_segflg == UIO_USERSPACE && nofault)
198                 newflags |= TDP_NOFAULTING;
199         save = curthread_pflags_set(newflags);
200
201         while (n > 0 && uio->uio_resid) {
202                 iov = uio->uio_iov;
203                 cnt = iov->iov_len;
204                 if (cnt == 0) {
205                         uio->uio_iov++;
206                         uio->uio_iovcnt--;
207                         continue;
208                 }
209                 if (cnt > n)
210                         cnt = n;
211
212                 switch (uio->uio_segflg) {
213
214                 case UIO_USERSPACE:
215                         maybe_yield();
216                         if (uio->uio_rw == UIO_READ)
217                                 error = copyout(cp, iov->iov_base, cnt);
218                         else
219                                 error = copyin(iov->iov_base, cp, cnt);
220                         if (error)
221                                 goto out;
222                         break;
223
224                 case UIO_SYSSPACE:
225                         if (uio->uio_rw == UIO_READ)
226                                 bcopy(cp, iov->iov_base, cnt);
227                         else
228                                 bcopy(iov->iov_base, cp, cnt);
229                         break;
230                 case UIO_NOCOPY:
231                         break;
232                 }
233                 iov->iov_base = (char *)iov->iov_base + cnt;
234                 iov->iov_len -= cnt;
235                 uio->uio_resid -= cnt;
236                 uio->uio_offset += cnt;
237                 cp = (char *)cp + cnt;
238                 n -= cnt;
239         }
240 out:
241         curthread_pflags_restore(save);
242         return (error);
243 }
244
245 /*
246  * Wrapper for uiomove() that validates the arguments against a known-good
247  * kernel buffer.  Currently, uiomove accepts a signed (n) argument, which
248  * is almost definitely a bad thing, so we catch that here as well.  We
249  * return a runtime failure, but it might be desirable to generate a runtime
250  * assertion failure instead.
251  */
252 int
253 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
254 {
255         size_t offset, n;
256
257         if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
258             (offset = uio->uio_offset) != uio->uio_offset)
259                 return (EINVAL);
260         if (buflen <= 0 || offset >= buflen)
261                 return (0);
262         if ((n = buflen - offset) > IOSIZE_MAX)
263                 return (EINVAL);
264         return (uiomove((char *)buf + offset, n, uio));
265 }
266
267 #ifdef ZERO_COPY_SOCKETS
268 /*
269  * Experimental support for zero-copy I/O
270  */
271 static int
272 userspaceco(void *cp, u_int cnt, struct uio *uio, int disposable)
273 {
274         struct iovec *iov;
275         int error;
276
277         iov = uio->uio_iov;
278         if (uio->uio_rw == UIO_READ) {
279                 if ((so_zero_copy_receive != 0)
280                  && ((cnt & PAGE_MASK) == 0)
281                  && ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0)
282                  && ((uio->uio_offset & PAGE_MASK) == 0)
283                  && ((((intptr_t) cp) & PAGE_MASK) == 0)
284                  && (disposable != 0)) {
285                         /* SOCKET: use page-trading */
286                         /*
287                          * We only want to call vm_pgmoveco() on
288                          * disposeable pages, since it gives the
289                          * kernel page to the userland process.
290                          */
291                         error = vm_pgmoveco(&curproc->p_vmspace->vm_map,
292                             (vm_offset_t)cp, (vm_offset_t)iov->iov_base);
293
294                         /*
295                          * If we get an error back, attempt
296                          * to use copyout() instead.  The
297                          * disposable page should be freed
298                          * automatically if we weren't able to move
299                          * it into userland.
300                          */
301                         if (error != 0)
302                                 error = copyout(cp, iov->iov_base, cnt);
303                 } else {
304                         error = copyout(cp, iov->iov_base, cnt);
305                 }
306         } else {
307                 error = copyin(iov->iov_base, cp, cnt);
308         }
309         return (error);
310 }
311
312 int
313 uiomoveco(void *cp, int n, struct uio *uio, int disposable)
314 {
315         struct iovec *iov;
316         u_int cnt;
317         int error;
318
319         KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
320             ("uiomoveco: mode"));
321         KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
322             ("uiomoveco proc"));
323
324         while (n > 0 && uio->uio_resid) {
325                 iov = uio->uio_iov;
326                 cnt = iov->iov_len;
327                 if (cnt == 0) {
328                         uio->uio_iov++;
329                         uio->uio_iovcnt--;
330                         continue;
331                 }
332                 if (cnt > n)
333                         cnt = n;
334
335                 switch (uio->uio_segflg) {
336
337                 case UIO_USERSPACE:
338                         maybe_yield();
339                         error = userspaceco(cp, cnt, uio, disposable);
340                         if (error)
341                                 return (error);
342                         break;
343
344                 case UIO_SYSSPACE:
345                         if (uio->uio_rw == UIO_READ)
346                                 bcopy(cp, iov->iov_base, cnt);
347                         else
348                                 bcopy(iov->iov_base, cp, cnt);
349                         break;
350                 case UIO_NOCOPY:
351                         break;
352                 }
353                 iov->iov_base = (char *)iov->iov_base + cnt;
354                 iov->iov_len -= cnt;
355                 uio->uio_resid -= cnt;
356                 uio->uio_offset += cnt;
357                 cp = (char *)cp + cnt;
358                 n -= cnt;
359         }
360         return (0);
361 }
362 #endif /* ZERO_COPY_SOCKETS */
363
364 /*
365  * Give next character to user as result of read.
366  */
367 int
368 ureadc(int c, struct uio *uio)
369 {
370         struct iovec *iov;
371         char *iov_base;
372
373         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
374             "Calling ureadc()");
375
376 again:
377         if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
378                 panic("ureadc");
379         iov = uio->uio_iov;
380         if (iov->iov_len == 0) {
381                 uio->uio_iovcnt--;
382                 uio->uio_iov++;
383                 goto again;
384         }
385         switch (uio->uio_segflg) {
386
387         case UIO_USERSPACE:
388                 if (subyte(iov->iov_base, c) < 0)
389                         return (EFAULT);
390                 break;
391
392         case UIO_SYSSPACE:
393                 iov_base = iov->iov_base;
394                 *iov_base = c;
395                 iov->iov_base = iov_base;
396                 break;
397
398         case UIO_NOCOPY:
399                 break;
400         }
401         iov->iov_base = (char *)iov->iov_base + 1;
402         iov->iov_len--;
403         uio->uio_resid--;
404         uio->uio_offset++;
405         return (0);
406 }
407
408 int
409 copyinfrom(const void * __restrict src, void * __restrict dst, size_t len,
410     int seg)
411 {
412         int error = 0;
413
414         switch (seg) {
415         case UIO_USERSPACE:
416                 error = copyin(src, dst, len);
417                 break;
418         case UIO_SYSSPACE:
419                 bcopy(src, dst, len);
420                 break;
421         default:
422                 panic("copyinfrom: bad seg %d\n", seg);
423         }
424         return (error);
425 }
426
427 int
428 copyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len,
429     size_t * __restrict copied, int seg)
430 {
431         int error = 0;
432
433         switch (seg) {
434         case UIO_USERSPACE:
435                 error = copyinstr(src, dst, len, copied);
436                 break;
437         case UIO_SYSSPACE:
438                 error = copystr(src, dst, len, copied);
439                 break;
440         default:
441                 panic("copyinstrfrom: bad seg %d\n", seg);
442         }
443         return (error);
444 }
445
446 int
447 copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
448 {
449         u_int iovlen;
450
451         *iov = NULL;
452         if (iovcnt > UIO_MAXIOV)
453                 return (error);
454         iovlen = iovcnt * sizeof (struct iovec);
455         *iov = malloc(iovlen, M_IOV, M_WAITOK);
456         error = copyin(iovp, *iov, iovlen);
457         if (error) {
458                 free(*iov, M_IOV);
459                 *iov = NULL;
460         }
461         return (error);
462 }
463
464 int
465 copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
466 {
467         struct iovec *iov;
468         struct uio *uio;
469         u_int iovlen;
470         int error, i;
471
472         *uiop = NULL;
473         if (iovcnt > UIO_MAXIOV)
474                 return (EINVAL);
475         iovlen = iovcnt * sizeof (struct iovec);
476         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
477         iov = (struct iovec *)(uio + 1);
478         error = copyin(iovp, iov, iovlen);
479         if (error) {
480                 free(uio, M_IOV);
481                 return (error);
482         }
483         uio->uio_iov = iov;
484         uio->uio_iovcnt = iovcnt;
485         uio->uio_segflg = UIO_USERSPACE;
486         uio->uio_offset = -1;
487         uio->uio_resid = 0;
488         for (i = 0; i < iovcnt; i++) {
489                 if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) {
490                         free(uio, M_IOV);
491                         return (EINVAL);
492                 }
493                 uio->uio_resid += iov->iov_len;
494                 iov++;
495         }
496         *uiop = uio;
497         return (0);
498 }
499
500 struct uio *
501 cloneuio(struct uio *uiop)
502 {
503         struct uio *uio;
504         int iovlen;
505
506         iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
507         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
508         *uio = *uiop;
509         uio->uio_iov = (struct iovec *)(uio + 1);
510         bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
511         return (uio);
512 }
513
514 /*
515  * Map some anonymous memory in user space of size sz, rounded up to the page
516  * boundary.
517  */
518 int
519 copyout_map(struct thread *td, vm_offset_t *addr, size_t sz)
520 {
521         struct vmspace *vms;
522         int error;
523         vm_size_t size;
524
525         vms = td->td_proc->p_vmspace;
526
527         /*
528          * Map somewhere after heap in process memory.
529          */
530         PROC_LOCK(td->td_proc);
531         *addr = round_page((vm_offset_t)vms->vm_daddr +
532             lim_max(td->td_proc, RLIMIT_DATA));
533         PROC_UNLOCK(td->td_proc);
534
535         /* round size up to page boundry */
536         size = (vm_size_t)round_page(sz);
537
538         error = vm_mmap(&vms->vm_map, addr, size, PROT_READ | PROT_WRITE,
539             VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, OBJT_DEFAULT, NULL, 0);
540
541         return (error);
542 }
543
544 /*
545  * Unmap memory in user space.
546  */
547 int
548 copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz)
549 {
550         vm_map_t map;
551         vm_size_t size;
552
553         if (sz == 0)
554                 return (0);
555
556         map = &td->td_proc->p_vmspace->vm_map;
557         size = (vm_size_t)round_page(sz);
558
559         if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS)
560                 return (EINVAL);
561
562         return (0);
563 }