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