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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_subr.c,v 1.31.2.2 2002/04/21 08:09:37 bde Exp $
40 * $DragonFly: src/sys/kern/kern_subr.c,v 1.27 2007/01/29 20:44:02 tgen Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/resourcevar.h>
52 #include <sys/sysctl.h>
54 #include <sys/vnode.h>
55 #include <sys/sfbuf.h>
56 #include <sys/thread2.h>
57 #include <machine/limits.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_map.h>
63 SYSCTL_INT(_kern
, KERN_IOV_MAX
, iov_max
, CTLFLAG_RD
, NULL
, UIO_MAXIOV
,
64 "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
67 * UIO_READ: copy the kernelspace cp to the user or kernelspace UIO
68 * UIO_WRITE: copy the user or kernelspace UIO to the kernelspace cp
70 * For userspace UIO's, uio_td must be the current thread.
73 uiomove(caddr_t cp
, int n
, struct uio
*uio
)
79 int baseticks
= ticks
;
81 KASSERT(uio
->uio_rw
== UIO_READ
|| uio
->uio_rw
== UIO_WRITE
,
83 KASSERT(uio
->uio_segflg
!= UIO_USERSPACE
|| uio
->uio_td
== curthread
,
87 save
= curproc
->p_flag
& P_DEADLKTREAT
;
88 curproc
->p_flag
|= P_DEADLKTREAT
;
91 while (n
> 0 && uio
->uio_resid
) {
102 switch (uio
->uio_segflg
) {
105 if (ticks
- baseticks
>= hogticks
) {
109 if (uio
->uio_rw
== UIO_READ
)
110 error
= copyout(cp
, iov
->iov_base
, cnt
);
112 error
= copyin(iov
->iov_base
, cp
, cnt
);
118 if (uio
->uio_rw
== UIO_READ
)
119 bcopy((caddr_t
)cp
, iov
->iov_base
, cnt
);
121 bcopy(iov
->iov_base
, (caddr_t
)cp
, cnt
);
126 iov
->iov_base
+= cnt
;
128 uio
->uio_resid
-= cnt
;
129 uio
->uio_offset
+= cnt
;
134 curproc
->p_flag
= (curproc
->p_flag
& ~P_DEADLKTREAT
) | save
;
138 * Wrapper for uiomove() that validates the arguments against a known-good
139 * kernel buffer. Currently, uiomove accepts a signed (n) argument, which
140 * is almost definitely a bad thing, so we catch that here as well. We
141 * return a runtime failure, but it might be desirable to generate a runtime
142 * assertion failure instead.
145 uiomove_frombuf(void *buf
, int buflen
, struct uio
*uio
)
147 unsigned int offset
, n
;
149 if (uio
->uio_offset
< 0 || uio
->uio_resid
< 0 ||
150 (offset
= uio
->uio_offset
) != uio
->uio_offset
)
152 if (buflen
<= 0 || offset
>= buflen
)
154 if ((n
= buflen
- offset
) > INT_MAX
)
156 return (uiomove((char *)buf
+ offset
, n
, uio
));
161 uiomoveco(caddr_t cp
, int n
, struct uio
*uio
, struct vm_object
*obj
)
166 int baseticks
= ticks
;
168 KASSERT(uio
->uio_rw
== UIO_READ
|| uio
->uio_rw
== UIO_WRITE
,
169 ("uiomoveco: mode"));
170 KASSERT(uio
->uio_segflg
!= UIO_USERSPACE
|| uio
->uio_td
== curthread
,
173 while (n
> 0 && uio
->uio_resid
) {
184 switch (uio
->uio_segflg
) {
187 if (ticks
- baseticks
>= hogticks
) {
191 if (uio
->uio_rw
== UIO_READ
) {
192 error
= copyout(cp
, iov
->iov_base
, cnt
);
194 error
= copyin(iov
->iov_base
, cp
, cnt
);
201 if (uio
->uio_rw
== UIO_READ
)
202 bcopy((caddr_t
)cp
, iov
->iov_base
, cnt
);
204 bcopy(iov
->iov_base
, (caddr_t
)cp
, cnt
);
209 iov
->iov_base
+= cnt
;
211 uio
->uio_resid
-= cnt
;
212 uio
->uio_offset
+= cnt
;
220 * Give next character to user as result of read.
223 ureadc(int c
, struct uio
*uio
)
228 if (uio
->uio_iovcnt
== 0 || uio
->uio_resid
== 0)
231 if (iov
->iov_len
== 0) {
236 switch (uio
->uio_segflg
) {
239 if (subyte(iov
->iov_base
, c
) < 0)
258 * General routine to allocate a hash table. Make the hash table size a
259 * power of 2 greater or equal to the number of elements requested, and
260 * store the masking value in *hashmask.
263 hashinit(int elements
, struct malloc_type
*type
, u_long
*hashmask
)
266 LIST_HEAD(generic
, generic
) *hashtbl
;
270 panic("hashinit: bad elements");
271 for (hashsize
= 2; hashsize
< elements
; hashsize
<<= 1)
273 hashtbl
= kmalloc((u_long
)hashsize
* sizeof(*hashtbl
), type
, M_WAITOK
);
274 for (i
= 0; i
< hashsize
; i
++)
275 LIST_INIT(&hashtbl
[i
]);
276 *hashmask
= hashsize
- 1;
280 static int primes
[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
281 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
282 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
283 #define NPRIMES (sizeof(primes) / sizeof(primes[0]))
286 * General routine to allocate a prime number sized hash table.
289 phashinit(int elements
, struct malloc_type
*type
, u_long
*nentries
)
292 LIST_HEAD(generic
, generic
) *hashtbl
;
296 panic("phashinit: bad elements");
297 for (i
= 1, hashsize
= primes
[1]; hashsize
<= elements
;) {
301 hashsize
= primes
[i
];
303 hashsize
= primes
[i
- 1];
304 hashtbl
= kmalloc((u_long
)hashsize
* sizeof(*hashtbl
), type
, M_WAITOK
);
305 for (i
= 0; i
< hashsize
; i
++)
306 LIST_INIT(&hashtbl
[i
]);
307 *nentries
= hashsize
;
312 * Copyin an iovec. If the iovec array fits, use the preallocated small
313 * iovec structure. If it is too big, dynamically allocate an iovec array
314 * of sufficient size.
319 iovec_copyin(struct iovec
*uiov
, struct iovec
**kiov
, struct iovec
*siov
,
320 size_t iov_cnt
, int *iov_len
)
325 if (iov_cnt
> UIO_MAXIOV
)
327 if (iov_cnt
> UIO_SMALLIOV
) {
328 MALLOC(*kiov
, struct iovec
*, sizeof(struct iovec
) * iov_cnt
,
333 error
= copyin(uiov
, *kiov
, iov_cnt
* sizeof(struct iovec
));
336 for (i
= 0, iovp
= *kiov
; i
< iov_cnt
; i
++, iovp
++) {
338 * Check for both *iov_len overflows and out of
339 * range iovp->iov_len's. We limit to the
340 * capabilities of signed integers.
342 if (*iov_len
+ (int)iovp
->iov_len
< *iov_len
)
344 *iov_len
+= (int)iovp
->iov_len
;
348 iovec_free(kiov
, siov
);
354 * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
355 * Copyright (c) 1982, 1986, 1991, 1993
356 * The Regents of the University of California. All rights reserved.
357 * (c) UNIX System Laboratories, Inc.
358 * All or some portions of this file are derived from material licensed
359 * to the University of California by American Telephone and Telegraph
360 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
361 * the permission of UNIX System Laboratories, Inc.
363 * Redistribution and use in source and binary forms, with or without
364 * modification, are permitted provided that the following conditions
366 * 1. Redistributions of source code must retain the above copyright
367 * notice, this list of conditions and the following disclaimer.
368 * 2. Redistributions in binary form must reproduce the above copyright
369 * notice, this list of conditions and the following disclaimer in the
370 * documentation and/or other materials provided with the distribution.
371 * 4. Neither the name of the University nor the names of its contributors
372 * may be used to endorse or promote products derived from this software
373 * without specific prior written permission.
375 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
376 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
377 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
378 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
379 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
380 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
381 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
382 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
383 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
384 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
387 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
388 * $FreeBSD: src/sys/i386/i386/uio_machdep.c,v 1.1 2004/03/21 20:28:36 alc Exp $
392 * Implement uiomove(9) from physical memory using sf_bufs to reduce
393 * the creation and destruction of ephemeral mappings.
396 uiomove_fromphys(vm_page_t
*ma
, vm_offset_t offset
, int n
, struct uio
*uio
)
399 struct thread
*td
= curthread
;
402 vm_offset_t page_offset
;
408 KASSERT(uio
->uio_rw
== UIO_READ
|| uio
->uio_rw
== UIO_WRITE
,
409 ("uiomove_fromphys: mode"));
410 KASSERT(uio
->uio_segflg
!= UIO_USERSPACE
|| uio
->uio_td
== curthread
,
411 ("uiomove_fromphys proc"));
414 save
= td
->td_flags
& TDF_DEADLKTREAT
;
415 td
->td_flags
|= TDF_DEADLKTREAT
;
418 while (n
> 0 && uio
->uio_resid
) {
428 page_offset
= offset
& PAGE_MASK
;
429 cnt
= min(cnt
, PAGE_SIZE
- page_offset
);
430 m
= ma
[offset
>> PAGE_SHIFT
];
431 sf
= sf_buf_alloc(m
, SFB_CPUPRIVATE
);
432 cp
= (char *)sf_buf_kva(sf
) + page_offset
;
433 switch (uio
->uio_segflg
) {
436 * note: removed uioyield (it was the wrong place to
439 if (uio
->uio_rw
== UIO_READ
)
440 error
= copyout(cp
, iov
->iov_base
, cnt
);
442 error
= copyin(iov
->iov_base
, cp
, cnt
);
449 if (uio
->uio_rw
== UIO_READ
)
450 bcopy(cp
, iov
->iov_base
, cnt
);
452 bcopy(iov
->iov_base
, cp
, cnt
);
458 iov
->iov_base
= (char *)iov
->iov_base
+ cnt
;
460 uio
->uio_resid
-= cnt
;
461 uio
->uio_offset
+= cnt
;
468 td
->td_flags
&= ~TDF_DEADLKTREAT
;