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
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/resourcevar.h>
49 #include <sys/vnode.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_map.h>
55 static void uio_yield __P((void));
61 register struct uio *uio;
63 register struct iovec *iov;
68 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
70 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_procp == curproc,
74 save = curproc->p_flag & P_DEADLKTREAT;
75 curproc->p_flag |= P_DEADLKTREAT;
78 while (n > 0 && uio->uio_resid) {
89 switch (uio->uio_segflg) {
93 if (ticks - switchticks >= hogticks)
95 if (uio->uio_rw == UIO_READ)
96 error = copyout(cp, iov->iov_base, cnt);
98 error = copyin(iov->iov_base, cp, cnt);
104 if (uio->uio_rw == UIO_READ)
105 bcopy((caddr_t)cp, iov->iov_base, cnt);
107 bcopy(iov->iov_base, (caddr_t)cp, cnt);
112 iov->iov_base += cnt;
114 uio->uio_resid -= cnt;
115 uio->uio_offset += cnt;
120 curproc->p_flag = (curproc->p_flag & ~P_DEADLKTREAT) | save;
125 uiomoveco(cp, n, uio, obj)
129 struct vm_object *obj;
135 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
136 ("uiomoveco: mode"));
137 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_procp == curproc,
140 while (n > 0 && uio->uio_resid) {
151 switch (uio->uio_segflg) {
155 if (ticks - switchticks >= hogticks)
157 if (uio->uio_rw == UIO_READ) {
158 #ifdef ENABLE_VFS_IOOPT
159 if (vfs_ioopt && ((cnt & PAGE_MASK) == 0) &&
160 ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
161 ((uio->uio_offset & PAGE_MASK) == 0) &&
162 ((((intptr_t) cp) & PAGE_MASK) == 0)) {
163 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
164 uio->uio_offset, cnt,
165 (vm_offset_t) iov->iov_base, NULL);
169 error = copyout(cp, iov->iov_base, cnt);
172 error = copyin(iov->iov_base, cp, cnt);
179 if (uio->uio_rw == UIO_READ)
180 bcopy((caddr_t)cp, iov->iov_base, cnt);
182 bcopy(iov->iov_base, (caddr_t)cp, cnt);
187 iov->iov_base += cnt;
189 uio->uio_resid -= cnt;
190 uio->uio_offset += cnt;
197 #ifdef ENABLE_VFS_IOOPT
200 uioread(n, uio, obj, nread)
203 struct vm_object *obj;
217 while (n > 0 && uio->uio_resid) {
228 if ((uio->uio_segflg == UIO_USERSPACE) &&
229 ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
230 ((uio->uio_offset & PAGE_MASK) == 0) ) {
237 if (ticks - switchticks >= hogticks)
239 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
240 uio->uio_offset, cnt,
241 (vm_offset_t) iov->iov_base, &npagesmoved);
243 if (npagesmoved == 0)
246 tcnt = npagesmoved * PAGE_SIZE;
252 iov->iov_base += cnt;
254 uio->uio_resid -= cnt;
255 uio->uio_offset += cnt;
268 * Give next character to user as result of read.
273 register struct uio *uio;
275 register struct iovec *iov;
278 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
281 if (iov->iov_len == 0) {
286 switch (uio->uio_segflg) {
289 if (subyte(iov->iov_base, c) < 0)
298 if (suibyte(iov->iov_base, c) < 0)
311 #ifdef vax /* unused except by ct.c, other oddities XXX */
313 * Get next character written in by user from uio.
319 register struct iovec *iov;
322 if (uio->uio_resid <= 0)
325 if (uio->uio_iovcnt <= 0)
328 if (iov->iov_len == 0) {
330 if (--uio->uio_iovcnt == 0)
334 switch (uio->uio_segflg) {
337 c = fubyte(iov->iov_base);
341 c = *(u_char *) iov->iov_base;
345 c = fuibyte(iov->iov_base);
359 * General routine to allocate a hash table.
362 hashinit(elements, type, hashmask)
364 struct malloc_type *type;
368 LIST_HEAD(generic, generic) *hashtbl;
372 panic("hashinit: bad elements");
373 for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
376 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
377 for (i = 0; i < hashsize; i++)
378 LIST_INIT(&hashtbl[i]);
379 *hashmask = hashsize - 1;
383 static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
384 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
385 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
386 #define NPRIMES (sizeof(primes) / sizeof(primes[0]))
389 * General routine to allocate a prime number sized hash table.
392 phashinit(elements, type, nentries)
394 struct malloc_type *type;
398 LIST_HEAD(generic, generic) *hashtbl;
402 panic("phashinit: bad elements");
403 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
407 hashsize = primes[i];
409 hashsize = primes[i - 1];
410 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
411 for (i = 0; i < hashsize; i++)
412 LIST_INIT(&hashtbl[i]);
413 *nentries = hashsize;
424 p->p_priority = p->p_usrpri;
427 p->p_stats->p_ru.ru_nivcsw++;