]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_uio.c
sys: Remove ancient SCCS tags.
[FreeBSD/FreeBSD.git] / sys / kern / subr_uio.c
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
42 #include <sys/cdefs.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/limits.h>
47 #include <sys/lock.h>
48 #include <sys/mman.h>
49 #include <sys/proc.h>
50 #include <sys/resourcevar.h>
51 #include <sys/rwlock.h>
52 #include <sys/sched.h>
53 #include <sys/sysctl.h>
54 #include <sys/vnode.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_param.h>
58 #include <vm/vm_extern.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_pageout.h>
61 #include <vm/vm_map.h>
62
63 #include <machine/bus.h>
64
65 SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, UIO_MAXIOV,
66         "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
67
68 static int uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault);
69
70 int
71 copyin_nofault(const void *udaddr, void *kaddr, size_t len)
72 {
73         int error, save;
74
75         save = vm_fault_disable_pagefaults();
76         error = copyin(udaddr, kaddr, len);
77         vm_fault_enable_pagefaults(save);
78         return (error);
79 }
80
81 int
82 copyout_nofault(const void *kaddr, void *udaddr, size_t len)
83 {
84         int error, save;
85
86         save = vm_fault_disable_pagefaults();
87         error = copyout(kaddr, udaddr, len);
88         vm_fault_enable_pagefaults(save);
89         return (error);
90 }
91
92 #define PHYS_PAGE_COUNT(len)    (howmany(len, PAGE_SIZE) + 1)
93
94 int
95 physcopyin(void *src, vm_paddr_t dst, size_t len)
96 {
97         vm_page_t m[PHYS_PAGE_COUNT(len)];
98         struct iovec iov[1];
99         struct uio uio;
100         int i;
101
102         iov[0].iov_base = src;
103         iov[0].iov_len = len;
104         uio.uio_iov = iov;
105         uio.uio_iovcnt = 1;
106         uio.uio_offset = 0;
107         uio.uio_resid = len;
108         uio.uio_segflg = UIO_SYSSPACE;
109         uio.uio_rw = UIO_WRITE;
110         for (i = 0; i < PHYS_PAGE_COUNT(len); i++, dst += PAGE_SIZE)
111                 m[i] = PHYS_TO_VM_PAGE(dst);
112         return (uiomove_fromphys(m, dst & PAGE_MASK, len, &uio));
113 }
114
115 int
116 physcopyout(vm_paddr_t src, void *dst, size_t len)
117 {
118         vm_page_t m[PHYS_PAGE_COUNT(len)];
119         struct iovec iov[1];
120         struct uio uio;
121         int i;
122
123         iov[0].iov_base = dst;
124         iov[0].iov_len = len;
125         uio.uio_iov = iov;
126         uio.uio_iovcnt = 1;
127         uio.uio_offset = 0;
128         uio.uio_resid = len;
129         uio.uio_segflg = UIO_SYSSPACE;
130         uio.uio_rw = UIO_READ;
131         for (i = 0; i < PHYS_PAGE_COUNT(len); i++, src += PAGE_SIZE)
132                 m[i] = PHYS_TO_VM_PAGE(src);
133         return (uiomove_fromphys(m, src & PAGE_MASK, len, &uio));
134 }
135
136 #undef PHYS_PAGE_COUNT
137
138 int
139 physcopyin_vlist(bus_dma_segment_t *src, off_t offset, vm_paddr_t dst,
140     size_t len)
141 {
142         size_t seg_len;
143         int error;
144
145         error = 0;
146         while (offset >= src->ds_len) {
147                 offset -= src->ds_len;
148                 src++;
149         }
150
151         while (len > 0 && error == 0) {
152                 seg_len = MIN(src->ds_len - offset, len);
153                 error = physcopyin((void *)(uintptr_t)(src->ds_addr + offset),
154                     dst, seg_len);
155                 offset = 0;
156                 src++;
157                 len -= seg_len;
158                 dst += seg_len;
159         }
160
161         return (error);
162 }
163
164 int
165 physcopyout_vlist(vm_paddr_t src, bus_dma_segment_t *dst, off_t offset,
166     size_t len)
167 {
168         size_t seg_len;
169         int error;
170
171         error = 0;
172         while (offset >= dst->ds_len) {
173                 offset -= dst->ds_len;
174                 dst++;
175         }
176
177         while (len > 0 && error == 0) {
178                 seg_len = MIN(dst->ds_len - offset, len);
179                 error = physcopyout(src, (void *)(uintptr_t)(dst->ds_addr +
180                     offset), seg_len);
181                 offset = 0;
182                 dst++;
183                 len -= seg_len;
184                 src += seg_len;
185         }
186
187         return (error);
188 }
189
190 int
191 uiomove(void *cp, int n, struct uio *uio)
192 {
193
194         return (uiomove_faultflag(cp, n, uio, 0));
195 }
196
197 int
198 uiomove_nofault(void *cp, int n, struct uio *uio)
199 {
200
201         return (uiomove_faultflag(cp, n, uio, 1));
202 }
203
204 static int
205 uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault)
206 {
207         struct iovec *iov;
208         size_t cnt;
209         int error, newflags, save;
210
211         save = error = 0;
212
213         KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
214             ("uiomove: mode"));
215         KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
216             ("uiomove proc"));
217         KASSERT(uio->uio_resid >= 0,
218             ("%s: uio %p resid underflow", __func__, uio));
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                 KASSERT(uio->uio_iovcnt > 0,
238                     ("%s: uio %p iovcnt underflow", __func__, uio));
239
240                 iov = uio->uio_iov;
241                 cnt = iov->iov_len;
242                 if (cnt == 0) {
243                         uio->uio_iov++;
244                         uio->uio_iovcnt--;
245                         continue;
246                 }
247                 if (cnt > n)
248                         cnt = n;
249
250                 switch (uio->uio_segflg) {
251                 case UIO_USERSPACE:
252                         maybe_yield();
253                         if (uio->uio_rw == UIO_READ)
254                                 error = copyout(cp, iov->iov_base, cnt);
255                         else
256                                 error = copyin(iov->iov_base, cp, cnt);
257                         if (error)
258                                 goto out;
259                         break;
260
261                 case UIO_SYSSPACE:
262                         if (uio->uio_rw == UIO_READ)
263                                 bcopy(cp, iov->iov_base, cnt);
264                         else
265                                 bcopy(iov->iov_base, cp, cnt);
266                         break;
267                 case UIO_NOCOPY:
268                         break;
269                 }
270                 iov->iov_base = (char *)iov->iov_base + cnt;
271                 iov->iov_len -= cnt;
272                 uio->uio_resid -= cnt;
273                 uio->uio_offset += cnt;
274                 cp = (char *)cp + cnt;
275                 n -= cnt;
276         }
277 out:
278         if (save)
279                 curthread_pflags_restore(save);
280         return (error);
281 }
282
283 /*
284  * Wrapper for uiomove() that validates the arguments against a known-good
285  * kernel buffer.  Currently, uiomove accepts a signed (n) argument, which
286  * is almost definitely a bad thing, so we catch that here as well.  We
287  * return a runtime failure, but it might be desirable to generate a runtime
288  * assertion failure instead.
289  */
290 int
291 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
292 {
293         size_t offset, n;
294
295         if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
296             (offset = uio->uio_offset) != uio->uio_offset)
297                 return (EINVAL);
298         if (buflen <= 0 || offset >= buflen)
299                 return (0);
300         if ((n = buflen - offset) > IOSIZE_MAX)
301                 return (EINVAL);
302         return (uiomove((char *)buf + offset, n, uio));
303 }
304
305 /*
306  * Give next character to user as result of read.
307  */
308 int
309 ureadc(int c, struct uio *uio)
310 {
311         struct iovec *iov;
312         char *iov_base;
313
314         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
315             "Calling ureadc()");
316
317 again:
318         if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
319                 panic("ureadc");
320         iov = uio->uio_iov;
321         if (iov->iov_len == 0) {
322                 uio->uio_iovcnt--;
323                 uio->uio_iov++;
324                 goto again;
325         }
326         switch (uio->uio_segflg) {
327         case UIO_USERSPACE:
328                 if (subyte(iov->iov_base, c) < 0)
329                         return (EFAULT);
330                 break;
331
332         case UIO_SYSSPACE:
333                 iov_base = iov->iov_base;
334                 *iov_base = c;
335                 break;
336
337         case UIO_NOCOPY:
338                 break;
339         }
340         iov->iov_base = (char *)iov->iov_base + 1;
341         iov->iov_len--;
342         uio->uio_resid--;
343         uio->uio_offset++;
344         return (0);
345 }
346
347 int
348 copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
349 {
350         u_int iovlen;
351
352         *iov = NULL;
353         if (iovcnt > UIO_MAXIOV)
354                 return (error);
355         iovlen = iovcnt * sizeof (struct iovec);
356         *iov = malloc(iovlen, M_IOV, M_WAITOK);
357         error = copyin(iovp, *iov, iovlen);
358         if (error) {
359                 free(*iov, M_IOV);
360                 *iov = NULL;
361         }
362         return (error);
363 }
364
365 int
366 copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
367 {
368         struct iovec *iov;
369         struct uio *uio;
370         u_int iovlen;
371         int error, i;
372
373         *uiop = NULL;
374         if (iovcnt > UIO_MAXIOV)
375                 return (EINVAL);
376         iovlen = iovcnt * sizeof (struct iovec);
377         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
378         iov = (struct iovec *)(uio + 1);
379         error = copyin(iovp, iov, iovlen);
380         if (error) {
381                 free(uio, M_IOV);
382                 return (error);
383         }
384         uio->uio_iov = iov;
385         uio->uio_iovcnt = iovcnt;
386         uio->uio_segflg = UIO_USERSPACE;
387         uio->uio_offset = -1;
388         uio->uio_resid = 0;
389         for (i = 0; i < iovcnt; i++) {
390                 if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) {
391                         free(uio, M_IOV);
392                         return (EINVAL);
393                 }
394                 uio->uio_resid += iov->iov_len;
395                 iov++;
396         }
397         *uiop = uio;
398         return (0);
399 }
400
401 struct uio *
402 cloneuio(struct uio *uiop)
403 {
404         struct uio *uio;
405         int iovlen;
406
407         iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
408         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
409         *uio = *uiop;
410         uio->uio_iov = (struct iovec *)(uio + 1);
411         bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
412         return (uio);
413 }
414
415 /*
416  * Map some anonymous memory in user space of size sz, rounded up to the page
417  * boundary.
418  */
419 int
420 copyout_map(struct thread *td, vm_offset_t *addr, size_t sz)
421 {
422         struct vmspace *vms;
423         int error;
424         vm_size_t size;
425
426         vms = td->td_proc->p_vmspace;
427
428         /*
429          * Map somewhere after heap in process memory.
430          */
431         *addr = round_page((vm_offset_t)vms->vm_daddr +
432             lim_max(td, RLIMIT_DATA));
433
434         /* round size up to page boundary */
435         size = (vm_size_t)round_page(sz);
436         if (size == 0)
437                 return (EINVAL);
438         error = vm_mmap_object(&vms->vm_map, addr, size, VM_PROT_READ |
439             VM_PROT_WRITE, VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, NULL, 0,
440             FALSE, td);
441         return (error);
442 }
443
444 /*
445  * Unmap memory in user space.
446  */
447 int
448 copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz)
449 {
450         vm_map_t map;
451         vm_size_t size;
452
453         if (sz == 0)
454                 return (0);
455
456         map = &td->td_proc->p_vmspace->vm_map;
457         size = (vm_size_t)round_page(sz);
458
459         if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS)
460                 return (EINVAL);
461
462         return (0);
463 }
464
465 int32_t
466 fuword32(volatile const void *addr)
467 {
468         int rv;
469         int32_t val;
470
471         rv = fueword32(addr, &val);
472         return (rv == -1 ? -1 : val);
473 }
474
475 #ifdef _LP64
476 int64_t
477 fuword64(volatile const void *addr)
478 {
479         int rv;
480         int64_t val;
481
482         rv = fueword64(addr, &val);
483         return (rv == -1 ? -1 : val);
484 }
485 #endif /* _LP64 */
486
487 long
488 fuword(volatile const void *addr)
489 {
490         long val;
491         int rv;
492
493         rv = fueword(addr, &val);
494         return (rv == -1 ? -1 : val);
495 }
496
497 uint32_t
498 casuword32(volatile uint32_t *addr, uint32_t old, uint32_t new)
499 {
500         int rv;
501         uint32_t val;
502
503         rv = casueword32(addr, old, &val, new);
504         return (rv == -1 ? -1 : val);
505 }
506
507 u_long
508 casuword(volatile u_long *addr, u_long old, u_long new)
509 {
510         int rv;
511         u_long val;
512
513         rv = casueword(addr, old, &val, new);
514         return (rv == -1 ? -1 : val);
515 }