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>
47 #include <sys/mutex.h>
49 #include <sys/malloc.h>
50 #include <sys/resourcevar.h>
51 #include <sys/sysctl.h>
52 #include <sys/vnode.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_map.h>
58 SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
59 "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
65 register struct uio *uio;
67 struct thread *td = curthread;
68 register struct iovec *iov;
73 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
75 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
79 mtx_lock_spin(&sched_lock);
80 save = td->td_flags & TDF_DEADLKTREAT;
81 td->td_flags |= TDF_DEADLKTREAT;
82 mtx_unlock_spin(&sched_lock);
85 while (n > 0 && uio->uio_resid) {
96 switch (uio->uio_segflg) {
100 if (ticks - PCPU_GET(switchticks) >= hogticks)
102 if (uio->uio_rw == UIO_READ)
103 error = copyout(cp, iov->iov_base, cnt);
105 error = copyin(iov->iov_base, cp, cnt);
111 if (uio->uio_rw == UIO_READ)
112 bcopy((caddr_t)cp, iov->iov_base, cnt);
114 bcopy(iov->iov_base, (caddr_t)cp, cnt);
119 iov->iov_base += cnt;
121 uio->uio_resid -= cnt;
122 uio->uio_offset += cnt;
127 if (td != curthread) printf("uiomove: IT CHANGED!");
128 td = curthread; /* Might things have changed in copyin/copyout? */
130 mtx_lock_spin(&sched_lock);
131 td->td_flags = (td->td_flags & ~TDF_DEADLKTREAT) | save;
132 mtx_unlock_spin(&sched_lock);
138 uiomoveco(cp, n, uio, obj)
142 struct vm_object *obj;
148 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
149 ("uiomoveco: mode"));
150 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
153 while (n > 0 && uio->uio_resid) {
164 switch (uio->uio_segflg) {
168 if (ticks - PCPU_GET(switchticks) >= hogticks)
170 if (uio->uio_rw == UIO_READ) {
171 #ifdef ENABLE_VFS_IOOPT
172 if (vfs_ioopt && ((cnt & PAGE_MASK) == 0) &&
173 ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
174 ((uio->uio_offset & PAGE_MASK) == 0) &&
175 ((((intptr_t) cp) & PAGE_MASK) == 0)) {
176 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
177 uio->uio_offset, cnt,
178 (vm_offset_t) iov->iov_base, NULL);
182 error = copyout(cp, iov->iov_base, cnt);
185 error = copyin(iov->iov_base, cp, cnt);
192 if (uio->uio_rw == UIO_READ)
193 bcopy((caddr_t)cp, iov->iov_base, cnt);
195 bcopy(iov->iov_base, (caddr_t)cp, cnt);
200 iov->iov_base += cnt;
202 uio->uio_resid -= cnt;
203 uio->uio_offset += cnt;
210 #ifdef ENABLE_VFS_IOOPT
213 uioread(n, uio, obj, nread)
216 struct vm_object *obj;
230 while (n > 0 && uio->uio_resid) {
241 if ((uio->uio_segflg == UIO_USERSPACE) &&
242 ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
243 ((uio->uio_offset & PAGE_MASK) == 0) ) {
250 if (ticks - PCPU_GET(switchticks) >= hogticks)
252 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
253 uio->uio_offset, cnt,
254 (vm_offset_t) iov->iov_base, &npagesmoved);
256 if (npagesmoved == 0)
259 tcnt = npagesmoved * PAGE_SIZE;
265 iov->iov_base += cnt;
267 uio->uio_resid -= cnt;
268 uio->uio_offset += cnt;
281 * Give next character to user as result of read.
286 register struct uio *uio;
288 register struct iovec *iov;
291 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
294 if (iov->iov_len == 0) {
299 switch (uio->uio_segflg) {
302 if (subyte(iov->iov_base, c) < 0)
311 if (suibyte(iov->iov_base, c) < 0)
325 * General routine to allocate a hash table.
328 hashinit(elements, type, hashmask)
330 struct malloc_type *type;
334 LIST_HEAD(generic, generic) *hashtbl;
338 panic("hashinit: bad elements");
339 for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
342 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
343 for (i = 0; i < hashsize; i++)
344 LIST_INIT(&hashtbl[i]);
345 *hashmask = hashsize - 1;
349 static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
350 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
351 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
352 #define NPRIMES (sizeof(primes) / sizeof(primes[0]))
355 * General routine to allocate a prime number sized hash table.
358 phashinit(elements, type, nentries)
360 struct malloc_type *type;
364 LIST_HEAD(generic, generic) *hashtbl;
368 panic("phashinit: bad elements");
369 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
373 hashsize = primes[i];
375 hashsize = primes[i - 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 *nentries = hashsize;
389 mtx_lock_spin(&sched_lock);
391 td->td_priority = td->td_ksegrp->kg_user_pri; /* XXXKSE */
393 td->td_proc->p_stats->p_ru.ru_nivcsw++;
395 mtx_unlock_spin(&sched_lock);
400 copyinfrom(const void *src, void *dst, size_t len, int seg)
406 error = copyin(src, dst, len);
409 bcopy(src, dst, len);
412 panic("copyinfrom: bad seg %d\n", seg);
418 copyinstrfrom(const void *src, void *dst, size_t len, size_t *copied, int seg)
424 error = copyinstr(src, dst, len, copied);
427 error = copystr(src, dst, len, copied);
430 panic("copyinstrfrom: bad seg %d\n", seg);