]> 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_param.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_map.h>
60 #ifdef SOCKET_SEND_COW
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 SOCKET_SEND_COW
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 /* SOCKET_SEND_COW */
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 #define PHYS_PAGE_COUNT(len)    (howmany(len, PAGE_SIZE) + 1)
163
164 int
165 physcopyin(void *src, vm_paddr_t dst, size_t len)
166 {
167         vm_page_t m[PHYS_PAGE_COUNT(len)];
168         struct iovec iov[1];
169         struct uio uio;
170         int i;
171
172         iov[0].iov_base = src;
173         iov[0].iov_len = len;
174         uio.uio_iov = iov;
175         uio.uio_iovcnt = 1;
176         uio.uio_offset = 0;
177         uio.uio_resid = len;
178         uio.uio_segflg = UIO_SYSSPACE;
179         uio.uio_rw = UIO_WRITE;
180         for (i = 0; i < PHYS_PAGE_COUNT(len); i++, dst += PAGE_SIZE)
181                 m[i] = PHYS_TO_VM_PAGE(dst);
182         return (uiomove_fromphys(m, dst & PAGE_MASK, len, &uio));
183 }
184
185 int
186 physcopyout(vm_paddr_t src, void *dst, size_t len)
187 {
188         vm_page_t m[PHYS_PAGE_COUNT(len)];
189         struct iovec iov[1];
190         struct uio uio;
191         int i;
192
193         iov[0].iov_base = dst;
194         iov[0].iov_len = len;
195         uio.uio_iov = iov;
196         uio.uio_iovcnt = 1;
197         uio.uio_offset = 0;
198         uio.uio_resid = len;
199         uio.uio_segflg = UIO_SYSSPACE;
200         uio.uio_rw = UIO_READ;
201         for (i = 0; i < PHYS_PAGE_COUNT(len); i++, src += PAGE_SIZE)
202                 m[i] = PHYS_TO_VM_PAGE(src);
203         return (uiomove_fromphys(m, src & PAGE_MASK, len, &uio));
204 }
205
206 #undef PHYS_PAGE_COUNT
207
208 int
209 uiomove(void *cp, int n, struct uio *uio)
210 {
211
212         return (uiomove_faultflag(cp, n, uio, 0));
213 }
214
215 int
216 uiomove_nofault(void *cp, int n, struct uio *uio)
217 {
218
219         return (uiomove_faultflag(cp, n, uio, 1));
220 }
221
222 static int
223 uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault)
224 {
225         struct thread *td;
226         struct iovec *iov;
227         size_t cnt;
228         int error, newflags, save;
229
230         td = curthread;
231         error = 0;
232
233         KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
234             ("uiomove: mode"));
235         KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == td,
236             ("uiomove proc"));
237         if (!nofault)
238                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
239                     "Calling uiomove()");
240
241         /* XXX does it make a sense to set TDP_DEADLKTREAT for UIO_SYSSPACE ? */
242         newflags = TDP_DEADLKTREAT;
243         if (uio->uio_segflg == UIO_USERSPACE && nofault) {
244                 /*
245                  * Fail if a non-spurious page fault occurs.
246                  */
247                 newflags |= TDP_NOFAULTING | TDP_RESETSPUR;
248         }
249         save = curthread_pflags_set(newflags);
250
251         while (n > 0 && uio->uio_resid) {
252                 iov = uio->uio_iov;
253                 cnt = iov->iov_len;
254                 if (cnt == 0) {
255                         uio->uio_iov++;
256                         uio->uio_iovcnt--;
257                         continue;
258                 }
259                 if (cnt > n)
260                         cnt = n;
261
262                 switch (uio->uio_segflg) {
263
264                 case UIO_USERSPACE:
265                         maybe_yield();
266                         if (uio->uio_rw == UIO_READ)
267                                 error = copyout(cp, iov->iov_base, cnt);
268                         else
269                                 error = copyin(iov->iov_base, cp, cnt);
270                         if (error)
271                                 goto out;
272                         break;
273
274                 case UIO_SYSSPACE:
275                         if (uio->uio_rw == UIO_READ)
276                                 bcopy(cp, iov->iov_base, cnt);
277                         else
278                                 bcopy(iov->iov_base, cp, cnt);
279                         break;
280                 case UIO_NOCOPY:
281                         break;
282                 }
283                 iov->iov_base = (char *)iov->iov_base + cnt;
284                 iov->iov_len -= cnt;
285                 uio->uio_resid -= cnt;
286                 uio->uio_offset += cnt;
287                 cp = (char *)cp + cnt;
288                 n -= cnt;
289         }
290 out:
291         curthread_pflags_restore(save);
292         return (error);
293 }
294
295 /*
296  * Wrapper for uiomove() that validates the arguments against a known-good
297  * kernel buffer.  Currently, uiomove accepts a signed (n) argument, which
298  * is almost definitely a bad thing, so we catch that here as well.  We
299  * return a runtime failure, but it might be desirable to generate a runtime
300  * assertion failure instead.
301  */
302 int
303 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
304 {
305         size_t offset, n;
306
307         if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
308             (offset = uio->uio_offset) != uio->uio_offset)
309                 return (EINVAL);
310         if (buflen <= 0 || offset >= buflen)
311                 return (0);
312         if ((n = buflen - offset) > IOSIZE_MAX)
313                 return (EINVAL);
314         return (uiomove((char *)buf + offset, n, uio));
315 }
316
317 #ifdef SOCKET_RECV_PFLIP
318 /*
319  * Experimental support for zero-copy I/O
320  */
321 static int
322 userspaceco(void *cp, u_int cnt, struct uio *uio, int disposable)
323 {
324         struct iovec *iov;
325         int error;
326
327         iov = uio->uio_iov;
328         if (uio->uio_rw == UIO_READ) {
329                 if ((so_zero_copy_receive != 0)
330                  && ((cnt & PAGE_MASK) == 0)
331                  && ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0)
332                  && ((uio->uio_offset & PAGE_MASK) == 0)
333                  && ((((intptr_t) cp) & PAGE_MASK) == 0)
334                  && (disposable != 0)) {
335                         /* SOCKET: use page-trading */
336                         /*
337                          * We only want to call vm_pgmoveco() on
338                          * disposeable pages, since it gives the
339                          * kernel page to the userland process.
340                          */
341                         error = vm_pgmoveco(&curproc->p_vmspace->vm_map,
342                             (vm_offset_t)cp, (vm_offset_t)iov->iov_base);
343
344                         /*
345                          * If we get an error back, attempt
346                          * to use copyout() instead.  The
347                          * disposable page should be freed
348                          * automatically if we weren't able to move
349                          * it into userland.
350                          */
351                         if (error != 0)
352                                 error = copyout(cp, iov->iov_base, cnt);
353                 } else {
354                         error = copyout(cp, iov->iov_base, cnt);
355                 }
356         } else {
357                 error = copyin(iov->iov_base, cp, cnt);
358         }
359         return (error);
360 }
361
362 int
363 uiomoveco(void *cp, int n, struct uio *uio, int disposable)
364 {
365         struct iovec *iov;
366         u_int cnt;
367         int error;
368
369         KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
370             ("uiomoveco: mode"));
371         KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
372             ("uiomoveco proc"));
373
374         while (n > 0 && uio->uio_resid) {
375                 iov = uio->uio_iov;
376                 cnt = iov->iov_len;
377                 if (cnt == 0) {
378                         uio->uio_iov++;
379                         uio->uio_iovcnt--;
380                         continue;
381                 }
382                 if (cnt > n)
383                         cnt = n;
384
385                 switch (uio->uio_segflg) {
386
387                 case UIO_USERSPACE:
388                         maybe_yield();
389                         error = userspaceco(cp, cnt, uio, disposable);
390                         if (error)
391                                 return (error);
392                         break;
393
394                 case UIO_SYSSPACE:
395                         if (uio->uio_rw == UIO_READ)
396                                 bcopy(cp, iov->iov_base, cnt);
397                         else
398                                 bcopy(iov->iov_base, cp, cnt);
399                         break;
400                 case UIO_NOCOPY:
401                         break;
402                 }
403                 iov->iov_base = (char *)iov->iov_base + cnt;
404                 iov->iov_len -= cnt;
405                 uio->uio_resid -= cnt;
406                 uio->uio_offset += cnt;
407                 cp = (char *)cp + cnt;
408                 n -= cnt;
409         }
410         return (0);
411 }
412 #endif /* SOCKET_RECV_PFLIP */
413
414 /*
415  * Give next character to user as result of read.
416  */
417 int
418 ureadc(int c, struct uio *uio)
419 {
420         struct iovec *iov;
421         char *iov_base;
422
423         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
424             "Calling ureadc()");
425
426 again:
427         if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
428                 panic("ureadc");
429         iov = uio->uio_iov;
430         if (iov->iov_len == 0) {
431                 uio->uio_iovcnt--;
432                 uio->uio_iov++;
433                 goto again;
434         }
435         switch (uio->uio_segflg) {
436
437         case UIO_USERSPACE:
438                 if (subyte(iov->iov_base, c) < 0)
439                         return (EFAULT);
440                 break;
441
442         case UIO_SYSSPACE:
443                 iov_base = iov->iov_base;
444                 *iov_base = c;
445                 break;
446
447         case UIO_NOCOPY:
448                 break;
449         }
450         iov->iov_base = (char *)iov->iov_base + 1;
451         iov->iov_len--;
452         uio->uio_resid--;
453         uio->uio_offset++;
454         return (0);
455 }
456
457 int
458 copyinfrom(const void * __restrict src, void * __restrict dst, size_t len,
459     int seg)
460 {
461         int error = 0;
462
463         switch (seg) {
464         case UIO_USERSPACE:
465                 error = copyin(src, dst, len);
466                 break;
467         case UIO_SYSSPACE:
468                 bcopy(src, dst, len);
469                 break;
470         default:
471                 panic("copyinfrom: bad seg %d\n", seg);
472         }
473         return (error);
474 }
475
476 int
477 copyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len,
478     size_t * __restrict copied, int seg)
479 {
480         int error = 0;
481
482         switch (seg) {
483         case UIO_USERSPACE:
484                 error = copyinstr(src, dst, len, copied);
485                 break;
486         case UIO_SYSSPACE:
487                 error = copystr(src, dst, len, copied);
488                 break;
489         default:
490                 panic("copyinstrfrom: bad seg %d\n", seg);
491         }
492         return (error);
493 }
494
495 int
496 copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
497 {
498         u_int iovlen;
499
500         *iov = NULL;
501         if (iovcnt > UIO_MAXIOV)
502                 return (error);
503         iovlen = iovcnt * sizeof (struct iovec);
504         *iov = malloc(iovlen, M_IOV, M_WAITOK);
505         error = copyin(iovp, *iov, iovlen);
506         if (error) {
507                 free(*iov, M_IOV);
508                 *iov = NULL;
509         }
510         return (error);
511 }
512
513 int
514 copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
515 {
516         struct iovec *iov;
517         struct uio *uio;
518         u_int iovlen;
519         int error, i;
520
521         *uiop = NULL;
522         if (iovcnt > UIO_MAXIOV)
523                 return (EINVAL);
524         iovlen = iovcnt * sizeof (struct iovec);
525         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
526         iov = (struct iovec *)(uio + 1);
527         error = copyin(iovp, iov, iovlen);
528         if (error) {
529                 free(uio, M_IOV);
530                 return (error);
531         }
532         uio->uio_iov = iov;
533         uio->uio_iovcnt = iovcnt;
534         uio->uio_segflg = UIO_USERSPACE;
535         uio->uio_offset = -1;
536         uio->uio_resid = 0;
537         for (i = 0; i < iovcnt; i++) {
538                 if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) {
539                         free(uio, M_IOV);
540                         return (EINVAL);
541                 }
542                 uio->uio_resid += iov->iov_len;
543                 iov++;
544         }
545         *uiop = uio;
546         return (0);
547 }
548
549 struct uio *
550 cloneuio(struct uio *uiop)
551 {
552         struct uio *uio;
553         int iovlen;
554
555         iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
556         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
557         *uio = *uiop;
558         uio->uio_iov = (struct iovec *)(uio + 1);
559         bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
560         return (uio);
561 }
562
563 /*
564  * Map some anonymous memory in user space of size sz, rounded up to the page
565  * boundary.
566  */
567 int
568 copyout_map(struct thread *td, vm_offset_t *addr, size_t sz)
569 {
570         struct vmspace *vms;
571         int error;
572         vm_size_t size;
573
574         vms = td->td_proc->p_vmspace;
575
576         /*
577          * Map somewhere after heap in process memory.
578          */
579         PROC_LOCK(td->td_proc);
580         *addr = round_page((vm_offset_t)vms->vm_daddr +
581             lim_max(td->td_proc, RLIMIT_DATA));
582         PROC_UNLOCK(td->td_proc);
583
584         /* round size up to page boundry */
585         size = (vm_size_t)round_page(sz);
586
587         error = vm_mmap(&vms->vm_map, addr, size, PROT_READ | PROT_WRITE,
588             VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, OBJT_DEFAULT, NULL, 0);
589
590         return (error);
591 }
592
593 /*
594  * Unmap memory in user space.
595  */
596 int
597 copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz)
598 {
599         vm_map_t map;
600         vm_size_t size;
601
602         if (sz == 0)
603                 return (0);
604
605         map = &td->td_proc->p_vmspace->vm_map;
606         size = (vm_size_t)round_page(sz);
607
608         if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS)
609                 return (EINVAL);
610
611         return (0);
612 }