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 * $DragonFly: src/sys/kern/kern_subr.c,v 1.19 2004/10/25 19:14:32 dillon Exp $ 41 */ 42 43 #include "opt_ddb.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/proc.h> 49 #include <sys/malloc.h> 50 #include <sys/lock.h> 51 #include <sys/resourcevar.h> 52 #include <sys/vnode.h> 53 #include <machine/limits.h> 54 55 #include <vm/vm.h> 56 #include <vm/vm_page.h> 57 #include <vm/vm_map.h> 58 59 /* 60 * UIO_READ: copy the kernelspace cp to the user or kernelspace UIO 61 * UIO_WRITE: copy the user or kernelspace UIO to cp 62 * 63 * For userspace UIO's, uio_td must be the current thread. 64 */ 65 int 66 uiomove(caddr_t cp, int n, struct uio *uio) 67 { 68 struct iovec *iov; 69 u_int cnt; 70 int error = 0; 71 int save = 0; 72 int baseticks = ticks; 73 74 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 75 ("uiomove: mode")); 76 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 77 ("uiomove proc")); 78 79 if (curproc) { 80 save = curproc->p_flag & P_DEADLKTREAT; 81 curproc->p_flag |= P_DEADLKTREAT; 82 } 83 84 while (n > 0 && uio->uio_resid) { 85 iov = uio->uio_iov; 86 cnt = iov->iov_len; 87 if (cnt == 0) { 88 uio->uio_iov++; 89 uio->uio_iovcnt--; 90 continue; 91 } 92 if (cnt > n) 93 cnt = n; 94 95 switch (uio->uio_segflg) { 96 97 case UIO_USERSPACE: 98 if (ticks - baseticks >= hogticks) { 99 uio_yield(); 100 baseticks = ticks; 101 } 102 if (uio->uio_rw == UIO_READ) 103 error = copyout(cp, iov->iov_base, cnt); 104 else 105 error = copyin(iov->iov_base, cp, cnt); 106 if (error) 107 break; 108 break; 109 110 case UIO_SYSSPACE: 111 if (uio->uio_rw == UIO_READ) 112 bcopy((caddr_t)cp, iov->iov_base, cnt); 113 else 114 bcopy(iov->iov_base, (caddr_t)cp, cnt); 115 break; 116 case UIO_NOCOPY: 117 break; 118 } 119 iov->iov_base += cnt; 120 iov->iov_len -= cnt; 121 uio->uio_resid -= cnt; 122 uio->uio_offset += cnt; 123 cp += cnt; 124 n -= cnt; 125 } 126 if (curproc) 127 curproc->p_flag = (curproc->p_flag & ~P_DEADLKTREAT) | save; 128 return (error); 129 } 130 /* 131 * Wrapper for uiomove() that validates the arguments against a known-good 132 * kernel buffer. Currently, uiomove accepts a signed (n) argument, which 133 * is almost definitely a bad thing, so we catch that here as well. We 134 * return a runtime failure, but it might be desirable to generate a runtime 135 * assertion failure instead. 136 */ 137 int 138 uiomove_frombuf(void *buf, int buflen, struct uio *uio) 139 { 140 unsigned int offset, n; 141 142 if (uio->uio_offset < 0 || uio->uio_resid < 0 || 143 (offset = uio->uio_offset) != uio->uio_offset) 144 return (EINVAL); 145 if (buflen <= 0 || offset >= buflen) 146 return (0); 147 if ((n = buflen - offset) > INT_MAX) 148 return (EINVAL); 149 return (uiomove((char *)buf + offset, n, uio)); 150 } 151 152 153 int 154 uiomoveco(cp, n, uio, obj) 155 caddr_t cp; 156 int n; 157 struct uio *uio; 158 struct vm_object *obj; 159 { 160 struct iovec *iov; 161 u_int cnt; 162 int error; 163 int baseticks = ticks; 164 165 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 166 ("uiomoveco: mode")); 167 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 168 ("uiomoveco proc")); 169 170 while (n > 0 && uio->uio_resid) { 171 iov = uio->uio_iov; 172 cnt = iov->iov_len; 173 if (cnt == 0) { 174 uio->uio_iov++; 175 uio->uio_iovcnt--; 176 continue; 177 } 178 if (cnt > n) 179 cnt = n; 180 181 switch (uio->uio_segflg) { 182 183 case UIO_USERSPACE: 184 if (ticks - baseticks >= hogticks) { 185 uio_yield(); 186 baseticks = ticks; 187 } 188 if (uio->uio_rw == UIO_READ) { 189 error = copyout(cp, iov->iov_base, cnt); 190 } else { 191 error = copyin(iov->iov_base, cp, cnt); 192 } 193 if (error) 194 return (error); 195 break; 196 197 case UIO_SYSSPACE: 198 if (uio->uio_rw == UIO_READ) 199 bcopy((caddr_t)cp, iov->iov_base, cnt); 200 else 201 bcopy(iov->iov_base, (caddr_t)cp, cnt); 202 break; 203 case UIO_NOCOPY: 204 break; 205 } 206 iov->iov_base += cnt; 207 iov->iov_len -= cnt; 208 uio->uio_resid -= cnt; 209 uio->uio_offset += cnt; 210 cp += cnt; 211 n -= cnt; 212 } 213 return (0); 214 } 215 216 /* 217 * Give next character to user as result of read. 218 */ 219 int 220 ureadc(c, uio) 221 int c; 222 struct uio *uio; 223 { 224 struct iovec *iov; 225 226 again: 227 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0) 228 panic("ureadc"); 229 iov = uio->uio_iov; 230 if (iov->iov_len == 0) { 231 uio->uio_iovcnt--; 232 uio->uio_iov++; 233 goto again; 234 } 235 switch (uio->uio_segflg) { 236 237 case UIO_USERSPACE: 238 if (subyte(iov->iov_base, c) < 0) 239 return (EFAULT); 240 break; 241 242 case UIO_SYSSPACE: 243 *iov->iov_base = c; 244 break; 245 246 case UIO_NOCOPY: 247 break; 248 } 249 iov->iov_base++; 250 iov->iov_len--; 251 uio->uio_resid--; 252 uio->uio_offset++; 253 return (0); 254 } 255 256 /* 257 * General routine to allocate a hash table. Make the hash table size a 258 * power of 2 greater or equal to the number of elements requested, and 259 * store the masking value in *hashmask. 260 */ 261 void * 262 hashinit(elements, type, hashmask) 263 int elements; 264 struct malloc_type *type; 265 u_long *hashmask; 266 { 267 long hashsize; 268 LIST_HEAD(generic, generic) *hashtbl; 269 int i; 270 271 if (elements <= 0) 272 panic("hashinit: bad elements"); 273 for (hashsize = 2; hashsize < elements; hashsize <<= 1) 274 continue; 275 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); 276 for (i = 0; i < hashsize; i++) 277 LIST_INIT(&hashtbl[i]); 278 *hashmask = hashsize - 1; 279 return (hashtbl); 280 } 281 282 static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039, 283 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653, 284 7159, 7673, 8191, 12281, 16381, 24571, 32749 }; 285 #define NPRIMES (sizeof(primes) / sizeof(primes[0])) 286 287 /* 288 * General routine to allocate a prime number sized hash table. 289 */ 290 void * 291 phashinit(elements, type, nentries) 292 int elements; 293 struct malloc_type *type; 294 u_long *nentries; 295 { 296 long hashsize; 297 LIST_HEAD(generic, generic) *hashtbl; 298 int i; 299 300 if (elements <= 0) 301 panic("phashinit: bad elements"); 302 for (i = 1, hashsize = primes[1]; hashsize <= elements;) { 303 i++; 304 if (i == NPRIMES) 305 break; 306 hashsize = primes[i]; 307 } 308 hashsize = primes[i - 1]; 309 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); 310 for (i = 0; i < hashsize; i++) 311 LIST_INIT(&hashtbl[i]); 312 *nentries = hashsize; 313 return (hashtbl); 314 } 315 316 /* 317 * Copyin an iovec. If the iovec array fits, use the preallocated small 318 * iovec structure. If it is too big, dynamically allocate an iovec array 319 * of sufficient size. 320 */ 321 int 322 iovec_copyin(struct iovec *uiov, struct iovec **kiov, struct iovec *siov, 323 size_t iov_cnt, size_t *iov_len) 324 { 325 struct iovec *iovp; 326 int error, i; 327 328 if (iov_cnt >= UIO_MAXIOV) 329 return EMSGSIZE; 330 if (iov_cnt >= UIO_SMALLIOV) { 331 MALLOC(*kiov, struct iovec *, sizeof(struct iovec) * iov_cnt, 332 M_IOV, M_WAITOK); 333 } else { 334 *kiov = siov; 335 } 336 error = copyin(uiov, *kiov, iov_cnt * sizeof(struct iovec)); 337 if (error) 338 goto cleanup; 339 *iov_len = 0; 340 for (i = 0, iovp = *kiov; i < iov_cnt; i++, iovp++) 341 *iov_len += iovp->iov_len; 342 343 cleanup: 344 if (error) 345 iovec_free(kiov, siov); 346 return (error); 347 } 348