]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_subr.c
7e94acb9c9aa474a6b7f08854207997f50862e5f
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
83 register struct uio
*uio
;
85 return uiomove64((addr64_t
)((unsigned int)cp
), n
, uio
);
89 uiomove64(addr64_t cp
, int n
, struct uio
*uio
)
91 register struct iovec
*iov
;
96 if (uio
->uio_rw
!= UIO_READ
&& uio
->uio_rw
!= UIO_WRITE
)
97 panic("uiomove: mode");
98 if (uio
->uio_segflg
== UIO_USERSPACE
&& uio
->uio_procp
!= current_proc())
99 panic("uiomove proc");
102 while (n
> 0 && uio
->uio_resid
) {
112 switch (uio
->uio_segflg
) {
116 if (uio
->uio_rw
== UIO_READ
)
118 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYOUT
)) | DBG_FUNC_START
,
119 (caddr_t
)cp
, iov
->iov_base
, cnt
, 0,0);
121 error
= copyout((caddr_t
)cp
, iov
->iov_base
, cnt
);
123 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYOUT
)) | DBG_FUNC_END
,
124 (caddr_t
)cp
, iov
->iov_base
, cnt
, 0,0);
128 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYIN
)) | DBG_FUNC_START
,
129 iov
->iov_base
, (caddr_t
)cp
, cnt
, 0,0);
131 error
= copyin(iov
->iov_base
, (caddr_t
)cp
, cnt
);
133 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYIN
)) | DBG_FUNC_END
,
134 iov
->iov_base
, (caddr_t
)cp
, cnt
, 0,0);
141 if (uio
->uio_rw
== UIO_READ
)
142 error
= copywithin((caddr_t
)cp
, iov
->iov_base
,
145 error
= copywithin(iov
->iov_base
, (caddr_t
)cp
,
149 case UIO_PHYS_USERSPACE
:
150 if (uio
->uio_rw
== UIO_READ
)
152 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYOUT
)) | DBG_FUNC_START
,
153 cp
, iov
->iov_base
, cnt
, 1,0);
155 if (error
= copypv((addr64_t
)cp
, (addr64_t
)((unsigned int)iov
->iov_base
), cnt
, cppvPsrc
| cppvNoRefSrc
)) /* Copy physical to virtual */
158 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYOUT
)) | DBG_FUNC_END
,
159 cp
, iov
->iov_base
, cnt
, 1,0);
163 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYIN
)) | DBG_FUNC_START
,
164 iov
->iov_base
, cp
, cnt
, 1,0);
166 if (error
= copypv((addr64_t
)((unsigned int)iov
->iov_base
), (addr64_t
)cp
, cnt
, cppvPsnk
| cppvNoRefSrc
| cppvNoModSnk
)) /* Copy virtual to physical */
169 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, DBG_UIO_COPYIN
)) | DBG_FUNC_END
,
170 iov
->iov_base
, cp
, cnt
, 1,0);
176 iov
->iov_base
+= cnt
;
178 uio
->uio_resid
-= cnt
;
179 uio
->uio_offset
+= cnt
;
187 * Give next character to user as result of read.
192 register struct uio
*uio
;
194 register struct iovec
*iov
;
196 if (uio
->uio_resid
<= 0)
197 panic("ureadc: non-positive resid");
199 if (uio
->uio_iovcnt
== 0)
200 panic("ureadc: non-positive iovcnt");
202 if (iov
->iov_len
<= 0) {
207 switch (uio
->uio_segflg
) {
210 if (subyte(iov
->iov_base
, c
) < 0)
219 if (suibyte(iov
->iov_base
, c
) < 0)
230 #if defined(vax) || defined(ppc)
231 /* unused except by ct.c, other oddities XXX */
233 * Get next character written in by user from uio.
238 register struct iovec
*iov
;
241 if (uio
->uio_resid
<= 0)
244 if (uio
->uio_iovcnt
<= 0)
245 panic("uwritec: non-positive iovcnt");
247 if (iov
->iov_len
== 0) {
249 if (--uio
->uio_iovcnt
== 0)
253 switch (uio
->uio_segflg
) {
256 c
= fubyte(iov
->iov_base
);
260 c
= *iov
->iov_base
& 0377;
264 c
= fuibyte(iov
->iov_base
);
268 c
= 0; /* avoid uninitialized variable warning */
269 panic("uwritec: bogus uio_segflg");
280 #endif /* vax || ppc */
283 * General routine to allocate a hash table.
286 hashinit(elements
, type
, hashmask
)
291 LIST_HEAD(generic
, generic
) *hashtbl
;
295 panic("hashinit: bad cnt");
296 for (hashsize
= 1; hashsize
<= elements
; hashsize
<<= 1)
299 MALLOC(hashtbl
, struct generic
*,
300 (u_long
)hashsize
* sizeof(*hashtbl
), type
, M_WAITOK
);
301 bzero(hashtbl
, (u_long
)hashsize
* sizeof(*hashtbl
));
302 for (i
= 0; i
< hashsize
; i
++)
303 LIST_INIT(&hashtbl
[i
]);
304 *hashmask
= hashsize
- 1;