Initial import from FreeBSD RELENG_4:
[games.git] / sys / kern / kern_subr.c
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  *      @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
39  * $FreeBSD: src/sys/kern/kern_subr.c,v 1.31.2.2 2002/04/21 08:09:37 bde Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/malloc.h>
47 #include <sys/lock.h>
48 #include <sys/resourcevar.h>
49 #include <sys/vnode.h>
50
51 #include <vm/vm.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_map.h>
54
55 int
56 uiomove(cp, n, uio)
57         register caddr_t cp;
58         register int n;
59         register struct uio *uio;
60 {
61         register struct iovec *iov;
62         u_int cnt;
63         int error = 0;
64         int save = 0;
65
66         KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
67             ("uiomove: mode"));
68         KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_procp == curproc,
69             ("uiomove proc"));
70
71         if (curproc) {
72                 save = curproc->p_flag & P_DEADLKTREAT;
73                 curproc->p_flag |= P_DEADLKTREAT;
74         }
75
76         while (n > 0 && uio->uio_resid) {
77                 iov = uio->uio_iov;
78                 cnt = iov->iov_len;
79                 if (cnt == 0) {
80                         uio->uio_iov++;
81                         uio->uio_iovcnt--;
82                         continue;
83                 }
84                 if (cnt > n)
85                         cnt = n;
86
87                 switch (uio->uio_segflg) {
88
89                 case UIO_USERSPACE:
90                 case UIO_USERISPACE:
91                         if (ticks - switchticks >= hogticks)
92                                 uio_yield();
93                         if (uio->uio_rw == UIO_READ)
94                                 error = copyout(cp, iov->iov_base, cnt);
95                         else
96                                 error = copyin(iov->iov_base, cp, cnt);
97                         if (error)
98                                 break;
99                         break;
100
101                 case UIO_SYSSPACE:
102                         if (uio->uio_rw == UIO_READ)
103                                 bcopy((caddr_t)cp, iov->iov_base, cnt);
104                         else
105                                 bcopy(iov->iov_base, (caddr_t)cp, cnt);
106                         break;
107                 case UIO_NOCOPY:
108                         break;
109                 }
110                 iov->iov_base += cnt;
111                 iov->iov_len -= cnt;
112                 uio->uio_resid -= cnt;
113                 uio->uio_offset += cnt;
114                 cp += cnt;
115                 n -= cnt;
116         }
117         if (curproc)
118                 curproc->p_flag = (curproc->p_flag & ~P_DEADLKTREAT) | save;
119         return (error);
120 }
121
122 int
123 uiomoveco(cp, n, uio, obj)
124         caddr_t cp;
125         int n;
126         struct uio *uio;
127         struct vm_object *obj;
128 {
129         struct iovec *iov;
130         u_int cnt;
131         int error;
132
133         KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
134             ("uiomoveco: mode"));
135         KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_procp == curproc,
136             ("uiomoveco proc"));
137
138         while (n > 0 && uio->uio_resid) {
139                 iov = uio->uio_iov;
140                 cnt = iov->iov_len;
141                 if (cnt == 0) {
142                         uio->uio_iov++;
143                         uio->uio_iovcnt--;
144                         continue;
145                 }
146                 if (cnt > n)
147                         cnt = n;
148
149                 switch (uio->uio_segflg) {
150
151                 case UIO_USERSPACE:
152                 case UIO_USERISPACE:
153                         if (ticks - switchticks >= hogticks)
154                                 uio_yield();
155                         if (uio->uio_rw == UIO_READ) {
156 #ifdef ENABLE_VFS_IOOPT
157                                 if (vfs_ioopt && ((cnt & PAGE_MASK) == 0) &&
158                                         ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
159                                         ((uio->uio_offset & PAGE_MASK) == 0) &&
160                                         ((((intptr_t) cp) & PAGE_MASK) == 0)) {
161                                                 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
162                                                                 uio->uio_offset, cnt,
163                                                                 (vm_offset_t) iov->iov_base, NULL);
164                                 } else
165 #endif
166                                 {
167                                         error = copyout(cp, iov->iov_base, cnt);
168                                 }
169                         } else {
170                                 error = copyin(iov->iov_base, cp, cnt);
171                         }
172                         if (error)
173                                 return (error);
174                         break;
175
176                 case UIO_SYSSPACE:
177                         if (uio->uio_rw == UIO_READ)
178                                 bcopy((caddr_t)cp, iov->iov_base, cnt);
179                         else
180                                 bcopy(iov->iov_base, (caddr_t)cp, cnt);
181                         break;
182                 case UIO_NOCOPY:
183                         break;
184                 }
185                 iov->iov_base += cnt;
186                 iov->iov_len -= cnt;
187                 uio->uio_resid -= cnt;
188                 uio->uio_offset += cnt;
189                 cp += cnt;
190                 n -= cnt;
191         }
192         return (0);
193 }
194
195 #ifdef ENABLE_VFS_IOOPT
196
197 int
198 uioread(n, uio, obj, nread)
199         int n;
200         struct uio *uio;
201         struct vm_object *obj;
202         int *nread;
203 {
204         int npagesmoved;
205         struct iovec *iov;
206         u_int cnt, tcnt;
207         int error;
208
209         *nread = 0;
210         if (vfs_ioopt < 2)
211                 return 0;
212
213         error = 0;
214
215         while (n > 0 && uio->uio_resid) {
216                 iov = uio->uio_iov;
217                 cnt = iov->iov_len;
218                 if (cnt == 0) {
219                         uio->uio_iov++;
220                         uio->uio_iovcnt--;
221                         continue;
222                 }
223                 if (cnt > n)
224                         cnt = n;
225
226                 if ((uio->uio_segflg == UIO_USERSPACE) &&
227                         ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
228                                  ((uio->uio_offset & PAGE_MASK) == 0) ) {
229
230                         if (cnt < PAGE_SIZE)
231                                 break;
232
233                         cnt &= ~PAGE_MASK;
234
235                         if (ticks - switchticks >= hogticks)
236                                 uio_yield();
237                         error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
238                                                 uio->uio_offset, cnt,
239                                                 (vm_offset_t) iov->iov_base, &npagesmoved);
240
241                         if (npagesmoved == 0)
242                                 break;
243
244                         tcnt = npagesmoved * PAGE_SIZE;
245                         cnt = tcnt;
246
247                         if (error)
248                                 break;
249
250                         iov->iov_base += cnt;
251                         iov->iov_len -= cnt;
252                         uio->uio_resid -= cnt;
253                         uio->uio_offset += cnt;
254                         *nread += cnt;
255                         n -= cnt;
256                 } else {
257                         break;
258                 }
259         }
260         return error;
261 }
262
263 #endif
264
265 /*
266  * Give next character to user as result of read.
267  */
268 int
269 ureadc(c, uio)
270         register int c;
271         register struct uio *uio;
272 {
273         register struct iovec *iov;
274
275 again:
276         if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
277                 panic("ureadc");
278         iov = uio->uio_iov;
279         if (iov->iov_len == 0) {
280                 uio->uio_iovcnt--;
281                 uio->uio_iov++;
282                 goto again;
283         }
284         switch (uio->uio_segflg) {
285
286         case UIO_USERSPACE:
287                 if (subyte(iov->iov_base, c) < 0)
288                         return (EFAULT);
289                 break;
290
291         case UIO_SYSSPACE:
292                 *iov->iov_base = c;
293                 break;
294
295         case UIO_USERISPACE:
296                 if (suibyte(iov->iov_base, c) < 0)
297                         return (EFAULT);
298                 break;
299         case UIO_NOCOPY:
300                 break;
301         }
302         iov->iov_base++;
303         iov->iov_len--;
304         uio->uio_resid--;
305         uio->uio_offset++;
306         return (0);
307 }
308
309 #ifdef vax      /* unused except by ct.c, other oddities XXX */
310 /*
311  * Get next character written in by user from uio.
312  */
313 int
314 uwritec(uio)
315         struct uio *uio;
316 {
317         register struct iovec *iov;
318         register int c;
319
320         if (uio->uio_resid <= 0)
321                 return (-1);
322 again:
323         if (uio->uio_iovcnt <= 0)
324                 panic("uwritec");
325         iov = uio->uio_iov;
326         if (iov->iov_len == 0) {
327                 uio->uio_iov++;
328                 if (--uio->uio_iovcnt == 0)
329                         return (-1);
330                 goto again;
331         }
332         switch (uio->uio_segflg) {
333
334         case UIO_USERSPACE:
335                 c = fubyte(iov->iov_base);
336                 break;
337
338         case UIO_SYSSPACE:
339                 c = *(u_char *) iov->iov_base;
340                 break;
341
342         case UIO_USERISPACE:
343                 c = fuibyte(iov->iov_base);
344                 break;
345         }
346         if (c < 0)
347                 return (-1);
348         iov->iov_base++;
349         iov->iov_len--;
350         uio->uio_resid--;
351         uio->uio_offset++;
352         return (c);
353 }
354 #endif /* vax */
355
356 /*
357  * General routine to allocate a hash table.
358  */
359 void *
360 hashinit(elements, type, hashmask)
361         int elements;
362         struct malloc_type *type;
363         u_long *hashmask;
364 {
365         long hashsize;
366         LIST_HEAD(generic, generic) *hashtbl;
367         int i;
368
369         if (elements <= 0)
370                 panic("hashinit: bad elements");
371         for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
372                 continue;
373         hashsize >>= 1;
374         hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
375         for (i = 0; i < hashsize; i++)
376                 LIST_INIT(&hashtbl[i]);
377         *hashmask = hashsize - 1;
378         return (hashtbl);
379 }
380
381 static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
382                         2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
383                         7159, 7673, 8191, 12281, 16381, 24571, 32749 };
384 #define NPRIMES (sizeof(primes) / sizeof(primes[0]))
385
386 /*
387  * General routine to allocate a prime number sized hash table.
388  */
389 void *
390 phashinit(elements, type, nentries)
391         int elements;
392         struct malloc_type *type;
393         u_long *nentries;
394 {
395         long hashsize;
396         LIST_HEAD(generic, generic) *hashtbl;
397         int i;
398
399         if (elements <= 0)
400                 panic("phashinit: bad elements");
401         for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
402                 i++;
403                 if (i == NPRIMES)
404                         break;
405                 hashsize = primes[i];
406         }
407         hashsize = primes[i - 1];
408         hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
409         for (i = 0; i < hashsize; i++)
410                 LIST_INIT(&hashtbl[i]);
411         *nentries = hashsize;
412         return (hashtbl);
413 }
414
415 void
416 uio_yield()
417 {
418         struct proc *p;
419         int s;
420
421         p = curproc;
422         s = splhigh();
423         p->p_priority = p->p_usrpri;
424         setrunqueue(p);
425         p->p_stats->p_ru.ru_nivcsw++;
426         mi_switch();
427         splx(s);
428 }