]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_subr.c
c0bba3e8e9b6e00a4119fa7dea4f84f754dfd2a9
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
29 * (c) UNIX System Laboratories, Inc.
30 * All or some portions of this file are derived from material licensed
31 * to the University of California by American Telephone and Telegraph
32 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
33 * the permission of UNIX System Laboratories, Inc.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/malloc.h>
70 #include <sys/queue.h>
75 #include <sys/kdebug.h>
76 #define DBG_UIO_COPYOUT 16
77 #define DBG_UIO_COPYIN 17
84 register struct uio
*uio
;
86 return uiomove64((addr64_t
)((unsigned int)cp
), n
, uio
);
90 uiomove64(addr64_t cp
, int n
, struct uio
*uio
)
92 register struct iovec
*iov
;
97 if (uio
->uio_rw
!= UIO_READ
&& uio
->uio_rw
!= UIO_WRITE
)
98 panic("uiomove: mode");
99 if (uio
->uio_segflg
== UIO_USERSPACE
&& uio
->uio_procp
!= current_proc())
100 panic("uiomove proc");
103 while (n
> 0 && uio
->uio_resid
) {
113 switch (uio
->uio_segflg
) {
117 if (uio
->uio_rw
== UIO_READ
)
119 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYOUT
)) | DBG_FUNC_START
,
120 (int)cp
, (int)iov
->iov_base
, cnt
, 0,0);
122 error
= copyout( CAST_DOWN(caddr_t
, cp
), iov
->iov_base
, cnt
);
124 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYOUT
)) | DBG_FUNC_END
,
125 (int)cp
, (int)iov
->iov_base
, cnt
, 0,0);
129 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYIN
)) | DBG_FUNC_START
,
130 (int)iov
->iov_base
, (int)cp
, cnt
, 0,0);
132 error
= copyin(iov
->iov_base
, CAST_DOWN(caddr_t
, cp
), cnt
);
134 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYIN
)) | DBG_FUNC_END
,
135 (int)iov
->iov_base
, (int)cp
, cnt
, 0,0);
142 if (uio
->uio_rw
== UIO_READ
)
143 error
= copywithin(CAST_DOWN(caddr_t
, cp
), iov
->iov_base
,
146 error
= copywithin(iov
->iov_base
, CAST_DOWN(caddr_t
, cp
),
150 case UIO_PHYS_USERSPACE
:
151 if (uio
->uio_rw
== UIO_READ
)
153 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYOUT
)) | DBG_FUNC_START
,
154 (int)cp
, (int)iov
->iov_base
, cnt
, 1,0);
156 if (error
= copypv((addr64_t
)cp
, (addr64_t
)((unsigned int)iov
->iov_base
), cnt
, cppvPsrc
| cppvNoRefSrc
)) /* Copy physical to virtual */
159 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYOUT
)) | DBG_FUNC_END
,
160 (int)cp
, (int)iov
->iov_base
, cnt
, 1,0);
164 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYIN
)) | DBG_FUNC_START
,
165 (int)iov
->iov_base
, (int)cp
, cnt
, 1,0);
167 if (error
= copypv((addr64_t
)((unsigned int)iov
->iov_base
), (addr64_t
)cp
, cnt
, cppvPsnk
| cppvNoRefSrc
| cppvNoModSnk
)) /* Copy virtual to physical */
170 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYIN
)) | DBG_FUNC_END
,
171 (int)iov
->iov_base
, (int)cp
, cnt
, 1,0);
177 case UIO_PHYS_SYSSPACE
:
178 if (uio
->uio_rw
== UIO_READ
)
180 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYOUT
)) | DBG_FUNC_START
,
181 (int)cp
, (int)iov
->iov_base
, cnt
, 2,0);
183 if (error
= copypv((addr64_t
)cp
, (addr64_t
)((unsigned int)iov
->iov_base
), cnt
, cppvKmap
| cppvPsrc
| cppvNoRefSrc
)) /* Copy physical to virtual */
186 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYOUT
)) | DBG_FUNC_END
,
187 (int)cp
, (int)iov
->iov_base
, cnt
, 2,0);
191 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYIN
)) | DBG_FUNC_START
,
192 (int)iov
->iov_base
, (int)cp
, cnt
, 2,0);
194 if (error
= copypv((addr64_t
)((unsigned int)iov
->iov_base
), (addr64_t
)cp
, cnt
, cppvKmap
| cppvPsnk
| cppvNoRefSrc
| cppvNoModSnk
)) /* Copy virtual to physical */
197 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYIN
)) | DBG_FUNC_END
,
198 (int)iov
->iov_base
, (int)cp
, cnt
, 2,0);
204 iov
->iov_base
+= cnt
;
206 uio
->uio_resid
-= cnt
;
207 uio
->uio_offset
+= cnt
;
215 * Give next character to user as result of read.
220 register struct uio
*uio
;
222 register struct iovec
*iov
;
224 if (uio
->uio_resid
<= 0)
225 panic("ureadc: non-positive resid");
227 if (uio
->uio_iovcnt
== 0)
228 panic("ureadc: non-positive iovcnt");
230 if (iov
->iov_len
<= 0) {
235 switch (uio
->uio_segflg
) {
238 if (subyte(iov
->iov_base
, c
) < 0)
247 if (suibyte(iov
->iov_base
, c
) < 0)
258 #if defined(vax) || defined(ppc)
259 /* unused except by ct.c, other oddities XXX */
261 * Get next character written in by user from uio.
266 register struct iovec
*iov
;
269 if (uio
->uio_resid
<= 0)
272 if (uio
->uio_iovcnt
<= 0)
273 panic("uwritec: non-positive iovcnt");
275 if (iov
->iov_len
== 0) {
277 if (--uio
->uio_iovcnt
== 0)
281 switch (uio
->uio_segflg
) {
284 c
= fubyte(iov
->iov_base
);
288 c
= *iov
->iov_base
& 0377;
292 c
= fuibyte(iov
->iov_base
);
296 c
= 0; /* avoid uninitialized variable warning */
297 panic("uwritec: bogus uio_segflg");
308 #endif /* vax || ppc */
311 * General routine to allocate a hash table.
314 hashinit(elements
, type
, hashmask
)
319 LIST_HEAD(generic
, generic
) *hashtbl
;
323 panic("hashinit: bad cnt");
324 for (hashsize
= 1; hashsize
<= elements
; hashsize
<<= 1)
327 MALLOC(hashtbl
, struct generic
*,
328 (u_long
)hashsize
* sizeof(*hashtbl
), type
, M_WAITOK
);
329 bzero(hashtbl
, (u_long
)hashsize
* sizeof(*hashtbl
));
330 for (i
= 0; i
< hashsize
; i
++)
331 LIST_INIT(&hashtbl
[i
]);
332 *hashmask
= hashsize
- 1;