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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
47 #include <sys/resourcevar.h>
48 #include <sys/rwlock.h>
49 #include <sys/sched.h>
50 #include <sys/sysctl.h>
51 #include <sys/vnode.h>
54 #include <vm/vm_param.h>
55 #include <vm/vm_extern.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_pageout.h>
58 #include <vm/vm_map.h>
60 SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
61 "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
63 static int uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault);
66 copyin_nofault(const void *udaddr, void *kaddr, size_t len)
70 save = vm_fault_disable_pagefaults();
71 error = copyin(udaddr, kaddr, len);
72 vm_fault_enable_pagefaults(save);
77 copyout_nofault(const void *kaddr, void *udaddr, size_t len)
81 save = vm_fault_disable_pagefaults();
82 error = copyout(kaddr, udaddr, len);
83 vm_fault_enable_pagefaults(save);
87 #define PHYS_PAGE_COUNT(len) (howmany(len, PAGE_SIZE) + 1)
90 physcopyin(void *src, vm_paddr_t dst, size_t len)
92 vm_page_t m[PHYS_PAGE_COUNT(len)];
97 iov[0].iov_base = src;
103 uio.uio_segflg = UIO_SYSSPACE;
104 uio.uio_rw = UIO_WRITE;
105 for (i = 0; i < PHYS_PAGE_COUNT(len); i++, dst += PAGE_SIZE)
106 m[i] = PHYS_TO_VM_PAGE(dst);
107 return (uiomove_fromphys(m, dst & PAGE_MASK, len, &uio));
111 physcopyout(vm_paddr_t src, void *dst, size_t len)
113 vm_page_t m[PHYS_PAGE_COUNT(len)];
118 iov[0].iov_base = dst;
119 iov[0].iov_len = len;
124 uio.uio_segflg = UIO_SYSSPACE;
125 uio.uio_rw = UIO_READ;
126 for (i = 0; i < PHYS_PAGE_COUNT(len); i++, src += PAGE_SIZE)
127 m[i] = PHYS_TO_VM_PAGE(src);
128 return (uiomove_fromphys(m, src & PAGE_MASK, len, &uio));
131 #undef PHYS_PAGE_COUNT
134 uiomove(void *cp, int n, struct uio *uio)
137 return (uiomove_faultflag(cp, n, uio, 0));
141 uiomove_nofault(void *cp, int n, struct uio *uio)
144 return (uiomove_faultflag(cp, n, uio, 1));
148 uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault)
153 int error, newflags, save;
158 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
160 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == td,
163 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
164 "Calling uiomove()");
166 /* XXX does it make a sense to set TDP_DEADLKTREAT for UIO_SYSSPACE ? */
167 newflags = TDP_DEADLKTREAT;
168 if (uio->uio_segflg == UIO_USERSPACE && nofault) {
170 * Fail if a non-spurious page fault occurs.
172 newflags |= TDP_NOFAULTING | TDP_RESETSPUR;
174 save = curthread_pflags_set(newflags);
176 while (n > 0 && uio->uio_resid) {
187 switch (uio->uio_segflg) {
191 if (uio->uio_rw == UIO_READ)
192 error = copyout(cp, iov->iov_base, cnt);
194 error = copyin(iov->iov_base, cp, cnt);
200 if (uio->uio_rw == UIO_READ)
201 bcopy(cp, iov->iov_base, cnt);
203 bcopy(iov->iov_base, cp, cnt);
208 iov->iov_base = (char *)iov->iov_base + cnt;
210 uio->uio_resid -= cnt;
211 uio->uio_offset += cnt;
212 cp = (char *)cp + cnt;
216 curthread_pflags_restore(save);
221 * Wrapper for uiomove() that validates the arguments against a known-good
222 * kernel buffer. Currently, uiomove accepts a signed (n) argument, which
223 * is almost definitely a bad thing, so we catch that here as well. We
224 * return a runtime failure, but it might be desirable to generate a runtime
225 * assertion failure instead.
228 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
232 if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
233 (offset = uio->uio_offset) != uio->uio_offset)
235 if (buflen <= 0 || offset >= buflen)
237 if ((n = buflen - offset) > IOSIZE_MAX)
239 return (uiomove((char *)buf + offset, n, uio));
243 * Give next character to user as result of read.
246 ureadc(int c, struct uio *uio)
251 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
255 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
258 if (iov->iov_len == 0) {
263 switch (uio->uio_segflg) {
266 if (subyte(iov->iov_base, c) < 0)
271 iov_base = iov->iov_base;
278 iov->iov_base = (char *)iov->iov_base + 1;
286 copyinfrom(const void * __restrict src, void * __restrict dst, size_t len,
293 error = copyin(src, dst, len);
296 bcopy(src, dst, len);
299 panic("copyinfrom: bad seg %d\n", seg);
305 copyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len,
306 size_t * __restrict copied, int seg)
312 error = copyinstr(src, dst, len, copied);
315 error = copystr(src, dst, len, copied);
318 panic("copyinstrfrom: bad seg %d\n", seg);
324 copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
329 if (iovcnt > UIO_MAXIOV)
331 iovlen = iovcnt * sizeof (struct iovec);
332 *iov = malloc(iovlen, M_IOV, M_WAITOK);
333 error = copyin(iovp, *iov, iovlen);
342 copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
350 if (iovcnt > UIO_MAXIOV)
352 iovlen = iovcnt * sizeof (struct iovec);
353 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
354 iov = (struct iovec *)(uio + 1);
355 error = copyin(iovp, iov, iovlen);
361 uio->uio_iovcnt = iovcnt;
362 uio->uio_segflg = UIO_USERSPACE;
363 uio->uio_offset = -1;
365 for (i = 0; i < iovcnt; i++) {
366 if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) {
370 uio->uio_resid += iov->iov_len;
378 cloneuio(struct uio *uiop)
383 iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
384 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
386 uio->uio_iov = (struct iovec *)(uio + 1);
387 bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
392 * Map some anonymous memory in user space of size sz, rounded up to the page
396 copyout_map(struct thread *td, vm_offset_t *addr, size_t sz)
402 vms = td->td_proc->p_vmspace;
405 * Map somewhere after heap in process memory.
407 PROC_LOCK(td->td_proc);
408 *addr = round_page((vm_offset_t)vms->vm_daddr +
409 lim_max(td->td_proc, RLIMIT_DATA));
410 PROC_UNLOCK(td->td_proc);
412 /* round size up to page boundry */
413 size = (vm_size_t)round_page(sz);
415 error = vm_mmap(&vms->vm_map, addr, size, PROT_READ | PROT_WRITE,
416 VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, OBJT_DEFAULT, NULL, 0);
422 * Unmap memory in user space.
425 copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz)
433 map = &td->td_proc->p_vmspace->vm_map;
434 size = (vm_size_t)round_page(sz);
436 if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS)