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