Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.10
      1 /*
      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.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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.
     25  *
     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
     36  * SUCH DAMAGE.
     37  *
     38  *	from: @(#)kern_subr.c	8.3 (Berkeley) 1/21/94
     39  *	$Id: kern_subr.c,v 1.10 1994/05/18 10:21:18 cgd Exp $
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/proc.h>
     45 #include <sys/malloc.h>
     46 #include <sys/queue.h>
     47 
     48 uiomove(cp, n, uio)
     49 	register caddr_t cp;
     50 	register int n;
     51 	register struct uio *uio;
     52 {
     53 	register struct iovec *iov;
     54 	u_int cnt;
     55 	int error = 0;
     56 
     57 #ifdef DIAGNOSTIC
     58 	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
     59 		panic("uiomove: mode");
     60 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
     61 		panic("uiomove proc");
     62 #endif
     63 	while (n > 0 && uio->uio_resid) {
     64 		iov = uio->uio_iov;
     65 		cnt = iov->iov_len;
     66 		if (cnt == 0) {
     67 			uio->uio_iov++;
     68 			uio->uio_iovcnt--;
     69 			continue;
     70 		}
     71 		if (cnt > n)
     72 			cnt = n;
     73 		switch (uio->uio_segflg) {
     74 
     75 		case UIO_USERSPACE:
     76 		case UIO_USERISPACE:
     77 			if (uio->uio_rw == UIO_READ)
     78 				error = copyout(cp, iov->iov_base, cnt);
     79 			else
     80 				error = copyin(iov->iov_base, cp, cnt);
     81 			if (error)
     82 				return (error);
     83 			break;
     84 
     85 		case UIO_SYSSPACE:
     86 			if (uio->uio_rw == UIO_READ)
     87 				bcopy((caddr_t)cp, iov->iov_base, cnt);
     88 			else
     89 				bcopy(iov->iov_base, (caddr_t)cp, cnt);
     90 			break;
     91 		}
     92 		iov->iov_base += cnt;
     93 		iov->iov_len -= cnt;
     94 		uio->uio_resid -= cnt;
     95 		uio->uio_offset += cnt;
     96 		cp += cnt;
     97 		n -= cnt;
     98 	}
     99 	return (error);
    100 }
    101 
    102 /*
    103  * Give next character to user as result of read.
    104  */
    105 ureadc(c, uio)
    106 	register int c;
    107 	register struct uio *uio;
    108 {
    109 	register struct iovec *iov;
    110 
    111 again:
    112 	if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
    113 		panic("ureadc");
    114 	iov = uio->uio_iov;
    115 	if (iov->iov_len == 0) {
    116 		uio->uio_iovcnt--;
    117 		uio->uio_iov++;
    118 		goto again;
    119 	}
    120 	switch (uio->uio_segflg) {
    121 
    122 	case UIO_USERSPACE:
    123 		if (subyte(iov->iov_base, c) < 0)
    124 			return (EFAULT);
    125 		break;
    126 
    127 	case UIO_SYSSPACE:
    128 		*iov->iov_base = c;
    129 		break;
    130 
    131 	case UIO_USERISPACE:
    132 		if (suibyte(iov->iov_base, c) < 0)
    133 			return (EFAULT);
    134 		break;
    135 	}
    136 	iov->iov_base++;
    137 	iov->iov_len--;
    138 	uio->uio_resid--;
    139 	uio->uio_offset++;
    140 	return (0);
    141 }
    142 
    143 #ifdef vax	/* unused except by ct.c, other oddities XXX */
    144 /*
    145  * Get next character written in by user from uio.
    146  */
    147 uwritec(uio)
    148 	struct uio *uio;
    149 {
    150 	register struct iovec *iov;
    151 	register int c;
    152 
    153 	if (uio->uio_resid <= 0)
    154 		return (-1);
    155 again:
    156 	if (uio->uio_iovcnt <= 0)
    157 		panic("uwritec");
    158 	iov = uio->uio_iov;
    159 	if (iov->iov_len == 0) {
    160 		uio->uio_iov++;
    161 		if (--uio->uio_iovcnt == 0)
    162 			return (-1);
    163 		goto again;
    164 	}
    165 	switch (uio->uio_segflg) {
    166 
    167 	case UIO_USERSPACE:
    168 		c = fubyte(iov->iov_base);
    169 		break;
    170 
    171 	case UIO_SYSSPACE:
    172 		c = *(u_char *) iov->iov_base;
    173 		break;
    174 
    175 	case UIO_USERISPACE:
    176 		c = fuibyte(iov->iov_base);
    177 		break;
    178 	}
    179 	if (c < 0)
    180 		return (-1);
    181 	iov->iov_base++;
    182 	iov->iov_len--;
    183 	uio->uio_resid--;
    184 	uio->uio_offset++;
    185 	return (c);
    186 }
    187 #endif /* vax */
    188 
    189 /*
    190  * General routine to allocate a hash table.
    191  */
    192 void *
    193 hashinit(elements, type, hashmask)
    194 	int elements, type;
    195 	u_long *hashmask;
    196 {
    197 	long hashsize;
    198 	LIST_HEAD(generic, generic) *hashtbl;
    199 	int i;
    200 
    201 	if (elements <= 0)
    202 		panic("hashinit: bad cnt");
    203 	for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
    204 		continue;
    205 	hashsize >>= 1;
    206 	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
    207 	for (i = 0; i < hashsize; i++)
    208 		LIST_INIT(&hashtbl[i]);
    209 	*hashmask = hashsize - 1;
    210 	return (hashtbl);
    211 }
    212