2 * SPDX-License-Identifier: BSD-3-Clause
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.
12 * Copyright (c) 2014 The FreeBSD Foundation
14 * Portions of this software were developed by Konstantin Belousov
15 * under sponsorship from the FreeBSD Foundation.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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
41 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/limits.h>
54 #include <sys/resourcevar.h>
55 #include <sys/rwlock.h>
56 #include <sys/sched.h>
57 #include <sys/sysctl.h>
58 #include <sys/vnode.h>
61 #include <vm/vm_param.h>
62 #include <vm/vm_extern.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_pageout.h>
65 #include <vm/vm_map.h>
67 #include <machine/bus.h>
69 SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, UIO_MAXIOV,
70 "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
72 static int uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault);
75 copyin_nofault(const void *udaddr, void *kaddr, size_t len)
79 save = vm_fault_disable_pagefaults();
80 error = copyin(udaddr, kaddr, len);
81 vm_fault_enable_pagefaults(save);
86 copyout_nofault(const void *kaddr, void *udaddr, size_t len)
90 save = vm_fault_disable_pagefaults();
91 error = copyout(kaddr, udaddr, len);
92 vm_fault_enable_pagefaults(save);
96 #define PHYS_PAGE_COUNT(len) (howmany(len, PAGE_SIZE) + 1)
99 physcopyin(void *src, vm_paddr_t dst, size_t len)
101 vm_page_t m[PHYS_PAGE_COUNT(len)];
106 iov[0].iov_base = src;
107 iov[0].iov_len = len;
112 uio.uio_segflg = UIO_SYSSPACE;
113 uio.uio_rw = UIO_WRITE;
114 for (i = 0; i < PHYS_PAGE_COUNT(len); i++, dst += PAGE_SIZE)
115 m[i] = PHYS_TO_VM_PAGE(dst);
116 return (uiomove_fromphys(m, dst & PAGE_MASK, len, &uio));
120 physcopyout(vm_paddr_t src, void *dst, size_t len)
122 vm_page_t m[PHYS_PAGE_COUNT(len)];
127 iov[0].iov_base = dst;
128 iov[0].iov_len = len;
133 uio.uio_segflg = UIO_SYSSPACE;
134 uio.uio_rw = UIO_READ;
135 for (i = 0; i < PHYS_PAGE_COUNT(len); i++, src += PAGE_SIZE)
136 m[i] = PHYS_TO_VM_PAGE(src);
137 return (uiomove_fromphys(m, src & PAGE_MASK, len, &uio));
140 #undef PHYS_PAGE_COUNT
143 physcopyin_vlist(bus_dma_segment_t *src, off_t offset, vm_paddr_t dst,
150 while (offset >= src->ds_len) {
151 offset -= src->ds_len;
155 while (len > 0 && error == 0) {
156 seg_len = MIN(src->ds_len - offset, len);
157 error = physcopyin((void *)(uintptr_t)(src->ds_addr + offset),
169 physcopyout_vlist(vm_paddr_t src, bus_dma_segment_t *dst, off_t offset,
176 while (offset >= dst->ds_len) {
177 offset -= dst->ds_len;
181 while (len > 0 && error == 0) {
182 seg_len = MIN(dst->ds_len - offset, len);
183 error = physcopyout(src, (void *)(uintptr_t)(dst->ds_addr +
195 uiomove(void *cp, int n, struct uio *uio)
198 return (uiomove_faultflag(cp, n, uio, 0));
202 uiomove_nofault(void *cp, int n, struct uio *uio)
205 return (uiomove_faultflag(cp, n, uio, 1));
209 uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault)
213 int error, newflags, save;
217 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
219 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
222 if (uio->uio_segflg == UIO_USERSPACE) {
223 newflags = TDP_DEADLKTREAT;
226 * Fail if a non-spurious page fault occurs.
228 newflags |= TDP_NOFAULTING | TDP_RESETSPUR;
230 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
231 "Calling uiomove()");
233 save = curthread_pflags_set(newflags);
235 KASSERT(nofault == 0, ("uiomove: nofault"));
238 while (n > 0 && uio->uio_resid) {
249 switch (uio->uio_segflg) {
252 if (uio->uio_rw == UIO_READ)
253 error = copyout(cp, iov->iov_base, cnt);
255 error = copyin(iov->iov_base, cp, cnt);
261 if (uio->uio_rw == UIO_READ)
262 bcopy(cp, iov->iov_base, cnt);
264 bcopy(iov->iov_base, cp, cnt);
269 iov->iov_base = (char *)iov->iov_base + cnt;
271 uio->uio_resid -= cnt;
272 uio->uio_offset += cnt;
273 cp = (char *)cp + cnt;
278 curthread_pflags_restore(save);
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.
290 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
294 if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
295 (offset = uio->uio_offset) != uio->uio_offset)
297 if (buflen <= 0 || offset >= buflen)
299 if ((n = buflen - offset) > IOSIZE_MAX)
301 return (uiomove((char *)buf + offset, n, uio));
305 * Give next character to user as result of read.
308 ureadc(int c, struct uio *uio)
313 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
317 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
320 if (iov->iov_len == 0) {
325 switch (uio->uio_segflg) {
327 if (subyte(iov->iov_base, c) < 0)
332 iov_base = iov->iov_base;
339 iov->iov_base = (char *)iov->iov_base + 1;
347 copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
352 if (iovcnt > UIO_MAXIOV)
354 iovlen = iovcnt * sizeof (struct iovec);
355 *iov = malloc(iovlen, M_IOV, M_WAITOK);
356 error = copyin(iovp, *iov, iovlen);
365 copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
373 if (iovcnt > UIO_MAXIOV)
375 iovlen = iovcnt * sizeof (struct iovec);
376 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
377 iov = (struct iovec *)(uio + 1);
378 error = copyin(iovp, iov, iovlen);
384 uio->uio_iovcnt = iovcnt;
385 uio->uio_segflg = UIO_USERSPACE;
386 uio->uio_offset = -1;
388 for (i = 0; i < iovcnt; i++) {
389 if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) {
393 uio->uio_resid += iov->iov_len;
401 cloneuio(struct uio *uiop)
406 iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
407 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
409 uio->uio_iov = (struct iovec *)(uio + 1);
410 bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
415 * Map some anonymous memory in user space of size sz, rounded up to the page
419 copyout_map(struct thread *td, vm_offset_t *addr, size_t sz)
425 vms = td->td_proc->p_vmspace;
428 * Map somewhere after heap in process memory.
430 *addr = round_page((vm_offset_t)vms->vm_daddr +
431 lim_max(td, RLIMIT_DATA));
433 /* round size up to page boundary */
434 size = (vm_size_t)round_page(sz);
437 error = vm_mmap_object(&vms->vm_map, addr, size, VM_PROT_READ |
438 VM_PROT_WRITE, VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, NULL, 0,
444 * Unmap memory in user space.
447 copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz)
455 map = &td->td_proc->p_vmspace->vm_map;
456 size = (vm_size_t)round_page(sz);
458 if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS)
465 fuword32(volatile const void *addr)
470 rv = fueword32(addr, &val);
471 return (rv == -1 ? -1 : val);
476 fuword64(volatile const void *addr)
481 rv = fueword64(addr, &val);
482 return (rv == -1 ? -1 : val);
487 fuword(volatile const void *addr)
492 rv = fueword(addr, &val);
493 return (rv == -1 ? -1 : val);
497 casuword32(volatile uint32_t *addr, uint32_t old, uint32_t new)
502 rv = casueword32(addr, old, &val, new);
503 return (rv == -1 ? -1 : val);
507 casuword(volatile u_long *addr, u_long old, u_long new)
512 rv = casueword(addr, old, &val, new);
513 return (rv == -1 ? -1 : val);