4 * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
42 * Copyright (c) 1992, 1993
43 * The Regents of the University of California. All rights reserved.
45 * This software was developed by the Computer Systems Engineering group
46 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
47 * contributed to Berkeley.
49 * All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Lawrence Berkeley Laboratory.
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
57 * 1. Redistributions of source code must retain the above copyright
58 * notice, this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright
60 * notice, this list of conditions and the following disclaimer in the
61 * documentation and/or other materials provided with the distribution.
62 * 3. Neither the name of the University nor the names of its contributors
63 * may be used to endorse or promote products derived from this software
64 * without specific prior written permission.
66 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 * @(#)kern_subr.c 8.4 (Berkeley) 2/14/95
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD$");
84 #include <sys/param.h>
85 #include <sys/fcntl.h>
87 #include <sys/systm.h>
89 #include <uvm/uvm_extern.h>
92 uio_setup_sysspace(struct uio
*uio
)
95 uio
->uio_vmspace
= vmspace_kernel();
99 uiomove(void *buf
, size_t n
, struct uio
*uio
)
101 struct vmspace
*vm
= uio
->uio_vmspace
;
110 if (uio
->uio_rw
!= UIO_READ
&& uio
->uio_rw
!= UIO_WRITE
)
111 panic("uiomove: mode");
113 while (n
> 0 && uio
->uio_resid
) {
117 KASSERT(uio
->uio_iovcnt
> 0);
124 if (!VMSPACE_IS_KERNEL_P(vm
)) {
125 if (curcpu()->ci_schedstate
.spc_flags
&
130 if (uio
->uio_rw
== UIO_READ
) {
131 error
= copyout_vmspace(vm
, cp
, iov
->iov_base
,
134 error
= copyin_vmspace(vm
, iov
->iov_base
, cp
,
140 iov
->iov_base
= (char *)iov
->iov_base
+ cnt
;
142 uio
->uio_resid
-= cnt
;
143 uio
->uio_offset
+= cnt
;
153 * Wrapper for uiomove() that validates the arguments against a known-good
157 uiomove_frombuf(void *buf
, size_t buflen
, struct uio
*uio
)
161 if (uio
->uio_offset
< 0 || /* uio->uio_resid < 0 || */
162 (offset
= uio
->uio_offset
) != uio
->uio_offset
)
164 if (offset
>= buflen
)
166 return (uiomove((char *)buf
+ offset
, buflen
- offset
, uio
));
170 * Give next character to user as result of read.
173 ureadc(int c
, struct uio
*uio
)
177 if (uio
->uio_resid
<= 0)
178 panic("ureadc: non-positive resid");
180 if (uio
->uio_iovcnt
<= 0)
181 panic("ureadc: non-positive iovcnt");
183 if (iov
->iov_len
<= 0) {
188 if (!VMSPACE_IS_KERNEL_P(uio
->uio_vmspace
)) {
189 if (subyte(iov
->iov_base
, c
) < 0)
192 *(char *)iov
->iov_base
= c
;
194 iov
->iov_base
= (char *)iov
->iov_base
+ 1;
202 * Like copyin(), but operates on an arbitrary vmspace.
205 copyin_vmspace(struct vmspace
*vm
, const void *uaddr
, void *kaddr
, size_t len
)
214 if (VMSPACE_IS_KERNEL_P(vm
)) {
215 return kcopy(uaddr
, kaddr
, len
);
217 if (__predict_true(vm
== curproc
->p_vmspace
)) {
218 return copyin(uaddr
, kaddr
, len
);
221 iov
.iov_base
= kaddr
;
225 uio
.uio_offset
= (off_t
)(uintptr_t)uaddr
;
227 uio
.uio_rw
= UIO_READ
;
228 UIO_SETUP_SYSSPACE(&uio
);
229 error
= uvm_io(&vm
->vm_map
, &uio
);
235 * Like copyout(), but operates on an arbitrary vmspace.
238 copyout_vmspace(struct vmspace
*vm
, const void *kaddr
, void *uaddr
, size_t len
)
247 if (VMSPACE_IS_KERNEL_P(vm
)) {
248 return kcopy(kaddr
, uaddr
, len
);
250 if (__predict_true(vm
== curproc
->p_vmspace
)) {
251 return copyout(kaddr
, uaddr
, len
);
254 iov
.iov_base
= __UNCONST(kaddr
); /* XXXUNCONST cast away const */
258 uio
.uio_offset
= (off_t
)(uintptr_t)uaddr
;
260 uio
.uio_rw
= UIO_WRITE
;
261 UIO_SETUP_SYSSPACE(&uio
);
262 error
= uvm_io(&vm
->vm_map
, &uio
);
268 * Like copyin(), but operates on an arbitrary process.
271 copyin_proc(struct proc
*p
, const void *uaddr
, void *kaddr
, size_t len
)
276 error
= proc_vmspace_getref(p
, &vm
);
280 error
= copyin_vmspace(vm
, uaddr
, kaddr
, len
);
287 * Like copyout(), but operates on an arbitrary process.
290 copyout_proc(struct proc
*p
, const void *kaddr
, void *uaddr
, size_t len
)
295 error
= proc_vmspace_getref(p
, &vm
);
299 error
= copyout_vmspace(vm
, kaddr
, uaddr
, len
);
306 * Like copyin(), except it operates on kernel addresses when the FKIOCTL
307 * flag is passed in `ioctlflags' from the ioctl call.
310 ioctl_copyin(int ioctlflags
, const void *src
, void *dst
, size_t len
)
312 if (ioctlflags
& FKIOCTL
)
313 return kcopy(src
, dst
, len
);
314 return copyin(src
, dst
, len
);
318 * Like copyout(), except it operates on kernel addresses when the FKIOCTL
319 * flag is passed in `ioctlflags' from the ioctl call.
322 ioctl_copyout(int ioctlflags
, const void *src
, void *dst
, size_t len
)
324 if (ioctlflags
& FKIOCTL
)
325 return kcopy(src
, dst
, len
);
326 return copyout(src
, dst
, len
);