xref: /csrg-svn/sys/kern/subr_xxx.c (revision 7722)
1 /*	subr_xxx.c	4.15	82/08/13	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/conf.h"
6 #include "../h/inode.h"
7 #include "../h/dir.h"
8 #include "../h/user.h"
9 #include "../h/buf.h"
10 #include "../h/proc.h"
11 #include "../h/fs.h"
12 #include "../h/vm.h"
13 #include "../h/pte.h"
14 #include "../h/cmap.h"
15 #include "../h/uio.h"
16 
17 /*
18  * Pass back  c  to the user at his location u_base;
19  * update u_base, u_count, and u_offset.  Return -1
20  * on the last character of the user's read.
21  * u_base is in the user address space unless u_segflg is set.
22  */
23 passc(c)
24 register c;
25 {
26 	register id;
27 
28 	if ((id = u.u_segflg) == 1)
29 		*u.u_base = c;
30 	else
31 		if (id?suibyte(u.u_base, c):subyte(u.u_base, c) < 0) {
32 			u.u_error = EFAULT;
33 			return (-1);
34 		}
35 	u.u_count--;
36 	u.u_offset++;
37 	u.u_base++;
38 	return (u.u_count == 0? -1: 0);
39 }
40 
41 #include "ct.h"
42 #if NCT > 0
43 /*
44  * Pick up and return the next character from the user's
45  * write call at location u_base;
46  * update u_base, u_count, and u_offset.  Return -1
47  * when u_count is exhausted.  u_base is in the user's
48  * address space unless u_segflg is set.
49  */
50 cpass()
51 {
52 	register c, id;
53 
54 	if (u.u_count == 0)
55 		return (-1);
56 	if ((id = u.u_segflg) == 1)
57 		c = *u.u_base;
58 	else
59 		if ((c = id==0?fubyte(u.u_base):fuibyte(u.u_base)) < 0) {
60 			u.u_error = EFAULT;
61 			return (-1);
62 		}
63 	u.u_count--;
64 	u.u_offset++;
65 	u.u_base++;
66 	return (c&0377);
67 }
68 #endif
69 
70 /*
71  * Routine which sets a user error; placed in
72  * illegal entries in the bdevsw and cdevsw tables.
73  */
74 nodev()
75 {
76 
77 	u.u_error = ENODEV;
78 }
79 
80 /*
81  * Null routine; placed in insignificant entries
82  * in the bdevsw and cdevsw tables.
83  */
84 nulldev()
85 {
86 
87 }
88 
89 imin(a, b)
90 {
91 
92 	return (a < b ? a : b);
93 }
94 
95 imax(a, b)
96 {
97 
98 	return (a > b ? a : b);
99 }
100 
101 unsigned
102 min(a, b)
103 	unsigned int a, b;
104 {
105 
106 	return (a < b ? a : b);
107 }
108 
109 unsigned
110 max(a, b)
111 	unsigned int a, b;
112 {
113 
114 	return (a > b ? a : b);
115 }
116 
117 struct proc *
118 pfind(pid)
119 	int pid;
120 {
121 	register struct proc *p;
122 
123 	for (p = &proc[pidhash[PIDHASH(pid)]]; p != &proc[0]; p = &proc[p->p_idhash])
124 		if (p->p_pid == pid)
125 			return (p);
126 	return ((struct proc *)0);
127 }
128 extern	cabase, calimit;
129 extern	struct pte camap[];
130 
131 caddr_t	cacur = (caddr_t)&cabase;
132 caddr_t	camax = (caddr_t)&cabase;
133 int	cax = 0;
134 /*
135  * This is a kernel-mode storage allocator.
136  * It is very primitive, currently, in that
137  * there is no way to give space back.
138  * It serves, for the time being, the needs of
139  * auto-configuration code and the like which
140  * need to allocate some stuff at boot time.
141  */
142 caddr_t
143 calloc(size)
144 	int size;
145 {
146 	register caddr_t res;
147 	register int i;
148 
149 	if (cacur+size >= (caddr_t)&calimit)
150 		panic("calloc");
151 	while (cacur+size > camax) {
152 		(void) vmemall(&camap[cax], CLSIZE, &proc[0], CSYS);
153 		vmaccess(&camap[cax], camax, CLSIZE);
154 		for (i = 0; i < CLSIZE; i++)
155 			clearseg(camap[cax++].pg_pfnum);
156 		camax += NBPG * CLSIZE;
157 	}
158 	res = cacur;
159 	cacur += size;
160 	return (res);
161 }
162 
163 #ifndef vax
164 ffs(mask)
165 	register long mask;
166 {
167 	register int i;
168 
169 	for(i=1; i<NSIG; i++) {
170 		if (mask & 1)
171 			return (i);
172 		mask >>= 1;
173 	}
174 	return (0);
175 }
176 
177 ffs(mask)
178 	register long mask;
179 {
180 	register int i;
181 
182 	for(i=1; i<NSIG; i++) {
183 		if (mask & 1)
184 			return (i);
185 		mask >>= 1;
186 	}
187 	return (0);
188 }
189 
190 bcmp(s1, s2, len)
191 	register char *s1, *s2;
192 	register int len;
193 {
194 
195 	while (--len)
196 		if (*s1++ != *s2++)
197 			return (1);
198 	return (0);
199 }
200 
201 strlen(s1)
202 	register char *s1;
203 {
204 	register int len;
205 
206 	for (len = 0; *s1++ != '\0'; len++)
207 		/* void */;
208 	return (len);
209 }
210 #endif
211 
212 copyuout(uio, to, len)
213 	register struct uio *uio;
214 	caddr_t to;
215 	int len;
216 {
217 	register struct iovec *iov = uio->uio_iov;
218 	int error = 0;
219 	int count;
220 
221 	while (uio->uio_iovcnt > 0) {
222 		count = iov->iov_len;
223 		if (count > len)
224 			count = len;
225 		if (copyout(iov->iov_base, to, count)) {
226 			error = EFAULT;
227 			break;
228 		}
229 		iov->iov_base += len;
230 		to += count;
231 		uio->uio_resid -= count;
232 		iov->iov_len -= len;
233 		iov++;
234 		uio->uio_iovcnt--;
235 		if (iov->iov_len)
236 			break;
237 	}
238 	return (error);
239 }
240 
241 /*
242  * Pass back c to the user.
243  */
244 passuc(c, uio)
245 	register c;
246 	struct uio *uio;
247 {
248 	register struct iovec *iov = uio->uio_iov;
249 	register id;
250 
251 	switch (uio->uio_segflg) {
252 
253 	case 0:
254 		if (subyte(iov->iov_base, c) < 0)
255 			goto fault;
256 		break;
257 
258 	case 1:
259 		*iov->iov_base = c;
260 		break;
261 
262 	case 2:
263 		if (suibyte(iov->iov_base, c) < 0)
264 			goto fault;
265 		break;
266 	}
267 	iov->iov_base++;
268 	iov->iov_len--;
269 	uio->uio_resid--;
270 	uio->uio_offset++;
271 	if (iov->iov_len <= 0) {
272 		uio->uio_iov++;
273 		uio->uio_iovcnt--;
274 	}
275 	return (0);
276 fault:
277 	return (EFAULT);
278 }
279