1 /* $OpenBSD: kern_subr.c,v 1.14 2000/09/07 18:39:13 art Exp $ */ 2 /* $NetBSD: kern_subr.c,v 1.15 1996/04/09 17:21:56 ragge Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/proc.h> 47 #include <sys/malloc.h> 48 #include <sys/queue.h> 49 #include <sys/kernel.h> 50 #include <sys/resourcevar.h> 51 52 int 53 uiomove(cp, n, uio) 54 register caddr_t cp; 55 register int n; 56 register struct uio *uio; 57 { 58 register struct iovec *iov; 59 u_int cnt; 60 int error = 0; 61 struct proc *p; 62 63 p = uio->uio_procp; 64 65 #ifdef DIAGNOSTIC 66 if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE) 67 panic("uiomove: mode"); 68 if (uio->uio_segflg == UIO_USERSPACE && p != curproc) 69 panic("uiomove: proc"); 70 #endif 71 while (n > 0 && uio->uio_resid) { 72 iov = uio->uio_iov; 73 cnt = iov->iov_len; 74 if (cnt == 0) { 75 uio->uio_iov++; 76 uio->uio_iovcnt--; 77 continue; 78 } 79 if (cnt > n) 80 cnt = n; 81 switch (uio->uio_segflg) { 82 83 case UIO_USERSPACE: 84 if (p->p_schedflags & PSCHED_SHOULDYIELD) 85 preempt(NULL); 86 if (uio->uio_rw == UIO_READ) 87 error = copyout(cp, iov->iov_base, cnt); 88 else 89 error = copyin(iov->iov_base, cp, cnt); 90 if (error) 91 return (error); 92 break; 93 94 case UIO_SYSSPACE: 95 #if defined(UVM) 96 if (uio->uio_rw == UIO_READ) 97 error = kcopy(cp, iov->iov_base, cnt); 98 else 99 error = kcopy(iov->iov_base, cp, cnt); 100 if (error) 101 return(error); 102 #else 103 if (uio->uio_rw == UIO_READ) 104 bcopy((caddr_t)cp, iov->iov_base, cnt); 105 else 106 bcopy(iov->iov_base, (caddr_t)cp, cnt); 107 break; 108 #endif 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 return (error); 118 } 119 120 /* 121 * Give next character to user as result of read. 122 */ 123 int 124 ureadc(c, uio) 125 register int c; 126 register struct uio *uio; 127 { 128 register struct iovec *iov; 129 130 if (uio->uio_resid == 0) 131 #ifdef DIAGNOSTIC 132 panic("ureadc: zero resid"); 133 #else 134 return (EINVAL); 135 #endif 136 again: 137 if (uio->uio_iovcnt <= 0) 138 #ifdef DIAGNOSTIC 139 panic("ureadc: non-positive iovcnt"); 140 #else 141 return (EINVAL); 142 #endif 143 iov = uio->uio_iov; 144 if (iov->iov_len <= 0) { 145 uio->uio_iovcnt--; 146 uio->uio_iov++; 147 goto again; 148 } 149 switch (uio->uio_segflg) { 150 151 case UIO_USERSPACE: 152 if (subyte(iov->iov_base, c) < 0) 153 return (EFAULT); 154 break; 155 156 case UIO_SYSSPACE: 157 *(char *)iov->iov_base = c; 158 break; 159 } 160 iov->iov_base++; 161 iov->iov_len--; 162 uio->uio_resid--; 163 uio->uio_offset++; 164 return (0); 165 } 166 167 /* 168 * General routine to allocate a hash table. 169 */ 170 void * 171 hashinit(elements, type, flags, hashmask) 172 int elements, type, flags; 173 u_long *hashmask; 174 { 175 long hashsize; 176 LIST_HEAD(generic, generic) *hashtbl; 177 int i; 178 179 if (elements <= 0) 180 panic("hashinit: bad cnt"); 181 for (hashsize = 1; hashsize <= elements; hashsize <<= 1) 182 continue; 183 hashsize >>= 1; 184 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, flags); 185 for (i = 0; i < hashsize; i++) 186 LIST_INIT(&hashtbl[i]); 187 *hashmask = hashsize - 1; 188 return (hashtbl); 189 } 190 191 /* 192 * "Shutdown hook" types, functions, and variables. 193 */ 194 195 struct shutdownhook_desc { 196 LIST_ENTRY(shutdownhook_desc) sfd_list; 197 void (*sfd_fn) __P((void *)); 198 void *sfd_arg; 199 }; 200 201 LIST_HEAD(, shutdownhook_desc) shutdownhook_list; 202 203 int shutdownhooks_done; 204 205 void * 206 shutdownhook_establish(fn, arg) 207 void (*fn) __P((void *)); 208 void *arg; 209 { 210 struct shutdownhook_desc *ndp; 211 212 ndp = (struct shutdownhook_desc *) 213 malloc(sizeof (*ndp), M_DEVBUF, M_NOWAIT); 214 if (ndp == NULL) 215 return NULL; 216 217 ndp->sfd_fn = fn; 218 ndp->sfd_arg = arg; 219 LIST_INSERT_HEAD(&shutdownhook_list, ndp, sfd_list); 220 221 return (ndp); 222 } 223 224 void 225 shutdownhook_disestablish(vhook) 226 void *vhook; 227 { 228 #ifdef DIAGNOSTIC 229 struct shutdownhook_desc *dp; 230 231 for (dp = shutdownhook_list.lh_first; dp != NULL; 232 dp = dp->sfd_list.le_next) 233 if (dp == vhook) 234 break; 235 if (dp == NULL) 236 panic("shutdownhook_disestablish: hook not established"); 237 #endif 238 239 LIST_REMOVE((struct shutdownhook_desc *)vhook, sfd_list); 240 } 241 242 /* 243 * Run shutdown hooks. Should be invoked immediately before the 244 * system is halted or rebooted, i.e. after file systems unmounted, 245 * after crash dump done, etc. 246 */ 247 void 248 doshutdownhooks() 249 { 250 struct shutdownhook_desc *dp; 251 252 if (shutdownhooks_done) 253 return; 254 255 for (dp = shutdownhook_list.lh_first; dp != NULL; dp = 256 dp->sfd_list.le_next) 257 (*dp->sfd_fn)(dp->sfd_arg); 258 } 259 260 /* 261 * "Power hook" types, functions, and variables. 262 */ 263 264 struct powerhook_desc { 265 LIST_ENTRY(powerhook_desc) sfd_list; 266 void (*sfd_fn) __P((int, void *)); 267 void *sfd_arg; 268 }; 269 270 LIST_HEAD(, powerhook_desc) powerhook_list; 271 272 void * 273 powerhook_establish(fn, arg) 274 void (*fn) __P((int, void *)); 275 void *arg; 276 { 277 struct powerhook_desc *ndp; 278 279 ndp = (struct powerhook_desc *) 280 malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT); 281 if (ndp == NULL) 282 return NULL; 283 284 ndp->sfd_fn = fn; 285 ndp->sfd_arg = arg; 286 LIST_INSERT_HEAD(&powerhook_list, ndp, sfd_list); 287 288 return (ndp); 289 } 290 291 void 292 powerhook_disestablish(vhook) 293 void *vhook; 294 { 295 #ifdef DIAGNOSTIC 296 struct powerhook_desc *dp; 297 298 for (dp = powerhook_list.lh_first; dp != NULL; 299 dp = dp->sfd_list.le_next) 300 if (dp == vhook) 301 break; 302 if (dp == NULL) 303 panic("powerhook_disestablish: hook not established"); 304 #endif 305 306 LIST_REMOVE((struct powerhook_desc *)vhook, sfd_list); 307 free(vhook, M_DEVBUF); 308 } 309 310 /* 311 * Run power hooks. 312 */ 313 void 314 dopowerhooks(why) 315 int why; 316 { 317 struct powerhook_desc *dp; 318 int s; 319 320 s = splhigh(); 321 for (dp = LIST_FIRST(&powerhook_list); 322 dp != NULL; 323 dp = LIST_NEXT(dp, sfd_list)) { 324 (*dp->sfd_fn)(why, dp->sfd_arg); 325 } 326 splx(s); 327 } 328