Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.105
      1 /*	$NetBSD: kern_subr.c,v 1.105 2003/09/14 11:12:14 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and by Luke Mewburn.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  * (c) UNIX System Laboratories, Inc.
     44  * All or some portions of this file are derived from material licensed
     45  * to the University of California by American Telephone and Telegraph
     46  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     47  * the permission of UNIX System Laboratories, Inc.
     48  *
     49  * Copyright (c) 1992, 1993
     50  *	The Regents of the University of California.  All rights reserved.
     51  *
     52  * This software was developed by the Computer Systems Engineering group
     53  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     54  * contributed to Berkeley.
     55  *
     56  * All advertising materials mentioning features or use of this software
     57  * must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Lawrence Berkeley Laboratory.
     60  *
     61  * Redistribution and use in source and binary forms, with or without
     62  * modification, are permitted provided that the following conditions
     63  * are met:
     64  * 1. Redistributions of source code must retain the above copyright
     65  *    notice, this list of conditions and the following disclaimer.
     66  * 2. Redistributions in binary form must reproduce the above copyright
     67  *    notice, this list of conditions and the following disclaimer in the
     68  *    documentation and/or other materials provided with the distribution.
     69  * 3. Neither the name of the University nor the names of its contributors
     70  *    may be used to endorse or promote products derived from this software
     71  *    without specific prior written permission.
     72  *
     73  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     74  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     75  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     76  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     77  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     78  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     79  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     80  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     81  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     82  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     83  * SUCH DAMAGE.
     84  *
     85  *	@(#)kern_subr.c	8.4 (Berkeley) 2/14/95
     86  */
     87 
     88 #include <sys/cdefs.h>
     89 __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.105 2003/09/14 11:12:14 yamt Exp $");
     90 
     91 #include "opt_ddb.h"
     92 #include "opt_md.h"
     93 #include "opt_syscall_debug.h"
     94 #include "opt_ktrace.h"
     95 #include "opt_systrace.h"
     96 
     97 #include <sys/param.h>
     98 #include <sys/systm.h>
     99 #include <sys/proc.h>
    100 #include <sys/malloc.h>
    101 #include <sys/mount.h>
    102 #include <sys/device.h>
    103 #include <sys/reboot.h>
    104 #include <sys/conf.h>
    105 #include <sys/disklabel.h>
    106 #include <sys/queue.h>
    107 #include <sys/systrace.h>
    108 #include <sys/ktrace.h>
    109 
    110 #include <uvm/uvm_extern.h>
    111 
    112 #include <dev/cons.h>
    113 
    114 #include <net/if.h>
    115 
    116 /* XXX these should eventually move to subr_autoconf.c */
    117 static struct device *finddevice __P((const char *));
    118 static struct device *getdisk __P((char *, int, int, dev_t *, int));
    119 static struct device *parsedisk __P((char *, int, int, dev_t *));
    120 
    121 /*
    122  * A generic linear hook.
    123  */
    124 struct hook_desc {
    125 	LIST_ENTRY(hook_desc) hk_list;
    126 	void	(*hk_fn) __P((void *));
    127 	void	*hk_arg;
    128 };
    129 typedef LIST_HEAD(, hook_desc) hook_list_t;
    130 
    131 static void *hook_establish __P((hook_list_t *, void (*)(void *), void *));
    132 static void hook_disestablish __P((hook_list_t *, void *));
    133 static void hook_destroy __P((hook_list_t *));
    134 static void hook_proc_run __P((hook_list_t *, struct proc *));
    135 
    136 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
    137 
    138 int
    139 uiomove(buf, n, uio)
    140 	void *buf;
    141 	size_t n;
    142 	struct uio *uio;
    143 {
    144 	struct iovec *iov;
    145 	u_int cnt;
    146 	int error = 0;
    147 	char *cp = buf;
    148 	struct proc *p = uio->uio_procp;
    149 
    150 #ifdef DIAGNOSTIC
    151 	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
    152 		panic("uiomove: mode");
    153 #endif
    154 	while (n > 0 && uio->uio_resid) {
    155 		iov = uio->uio_iov;
    156 		cnt = iov->iov_len;
    157 		if (cnt == 0) {
    158 			KASSERT(uio->uio_iovcnt > 0);
    159 			uio->uio_iov++;
    160 			uio->uio_iovcnt--;
    161 			continue;
    162 		}
    163 		if (cnt > n)
    164 			cnt = n;
    165 		switch (uio->uio_segflg) {
    166 
    167 		case UIO_USERSPACE:
    168 			if (curcpu()->ci_schedstate.spc_flags &
    169 			    SPCF_SHOULDYIELD)
    170 				preempt(1);
    171 			if (__predict_true(p == curproc)) {
    172 				if (uio->uio_rw == UIO_READ)
    173 					error = copyout(cp, iov->iov_base, cnt);
    174 				else
    175 					error = copyin(iov->iov_base, cp, cnt);
    176 			} else {
    177 				if (uio->uio_rw == UIO_READ)
    178 					error = copyout_proc(p, cp,
    179 					    iov->iov_base, cnt);
    180 				else
    181 					error = copyin_proc(p, iov->iov_base,
    182 					    cp, cnt);
    183 			}
    184 			if (error)
    185 				return (error);
    186 			break;
    187 
    188 		case UIO_SYSSPACE:
    189 			if (uio->uio_rw == UIO_READ)
    190 				error = kcopy(cp, iov->iov_base, cnt);
    191 			else
    192 				error = kcopy(iov->iov_base, cp, cnt);
    193 			if (error)
    194 				return (error);
    195 			break;
    196 		}
    197 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    198 		iov->iov_len -= cnt;
    199 		uio->uio_resid -= cnt;
    200 		uio->uio_offset += cnt;
    201 		cp += cnt;
    202 		KDASSERT(cnt <= n);
    203 		n -= cnt;
    204 	}
    205 	return (error);
    206 }
    207 
    208 /*
    209  * Give next character to user as result of read.
    210  */
    211 int
    212 ureadc(c, uio)
    213 	int c;
    214 	struct uio *uio;
    215 {
    216 	struct iovec *iov;
    217 
    218 	if (uio->uio_resid <= 0)
    219 		panic("ureadc: non-positive resid");
    220 again:
    221 	if (uio->uio_iovcnt <= 0)
    222 		panic("ureadc: non-positive iovcnt");
    223 	iov = uio->uio_iov;
    224 	if (iov->iov_len <= 0) {
    225 		uio->uio_iovcnt--;
    226 		uio->uio_iov++;
    227 		goto again;
    228 	}
    229 	switch (uio->uio_segflg) {
    230 
    231 	case UIO_USERSPACE:
    232 		if (subyte(iov->iov_base, c) < 0)
    233 			return (EFAULT);
    234 		break;
    235 
    236 	case UIO_SYSSPACE:
    237 		*(char *)iov->iov_base = c;
    238 		break;
    239 	}
    240 	iov->iov_base = (caddr_t)iov->iov_base + 1;
    241 	iov->iov_len--;
    242 	uio->uio_resid--;
    243 	uio->uio_offset++;
    244 	return (0);
    245 }
    246 
    247 /*
    248  * Like copyin(), but operates on an arbitrary process.
    249  */
    250 int
    251 copyin_proc(struct proc *p, const void *uaddr, void *kaddr, size_t len)
    252 {
    253 	struct iovec iov;
    254 	struct uio uio;
    255 	int error;
    256 
    257 	if (len == 0)
    258 		return (0);
    259 
    260 	iov.iov_base = kaddr;
    261 	iov.iov_len = len;
    262 	uio.uio_iov = &iov;
    263 	uio.uio_iovcnt = 1;
    264 	uio.uio_offset = (off_t)(intptr_t)uaddr;
    265 	uio.uio_resid = len;
    266 	uio.uio_segflg = UIO_SYSSPACE;
    267 	uio.uio_rw = UIO_READ;
    268 	uio.uio_procp = NULL;
    269 
    270 	/* XXXCDC: how should locking work here? */
    271 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
    272 		return (EFAULT);
    273 	p->p_vmspace->vm_refcnt++;	/* XXX */
    274 	error = uvm_io(&p->p_vmspace->vm_map, &uio);
    275 	uvmspace_free(p->p_vmspace);
    276 
    277 	return (error);
    278 }
    279 
    280 /*
    281  * Like copyout(), but operates on an arbitrary process.
    282  */
    283 int
    284 copyout_proc(struct proc *p, const void *kaddr, void *uaddr, size_t len)
    285 {
    286 	struct iovec iov;
    287 	struct uio uio;
    288 	int error;
    289 
    290 	if (len == 0)
    291 		return (0);
    292 
    293 	iov.iov_base = (void *) kaddr;	/* XXX cast away const */
    294 	iov.iov_len = len;
    295 	uio.uio_iov = &iov;
    296 	uio.uio_iovcnt = 1;
    297 	uio.uio_offset = (off_t)(intptr_t)uaddr;
    298 	uio.uio_resid = len;
    299 	uio.uio_segflg = UIO_SYSSPACE;
    300 	uio.uio_rw = UIO_WRITE;
    301 	uio.uio_procp = NULL;
    302 
    303 	/* XXXCDC: how should locking work here? */
    304 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
    305 		return (EFAULT);
    306 	p->p_vmspace->vm_refcnt++;	/* XXX */
    307 	error = uvm_io(&p->p_vmspace->vm_map, &uio);
    308 	uvmspace_free(p->p_vmspace);
    309 
    310 	return (error);
    311 }
    312 
    313 /*
    314  * General routine to allocate a hash table.
    315  * Allocate enough memory to hold at least `elements' list-head pointers.
    316  * Return a pointer to the allocated space and set *hashmask to a pattern
    317  * suitable for masking a value to use as an index into the returned array.
    318  */
    319 void *
    320 hashinit(elements, htype, mtype, mflags, hashmask)
    321 	u_int elements;
    322 	enum hashtype htype;
    323 	struct malloc_type *mtype;
    324 	int mflags;
    325 	u_long *hashmask;
    326 {
    327 	u_long hashsize, i;
    328 	LIST_HEAD(, generic) *hashtbl_list;
    329 	TAILQ_HEAD(, generic) *hashtbl_tailq;
    330 	size_t esize;
    331 	void *p;
    332 
    333 	if (elements == 0)
    334 		panic("hashinit: bad cnt");
    335 	for (hashsize = 1; hashsize < elements; hashsize <<= 1)
    336 		continue;
    337 
    338 	switch (htype) {
    339 	case HASH_LIST:
    340 		esize = sizeof(*hashtbl_list);
    341 		break;
    342 	case HASH_TAILQ:
    343 		esize = sizeof(*hashtbl_tailq);
    344 		break;
    345 #ifdef DIAGNOSTIC
    346 	default:
    347 		panic("hashinit: invalid table type");
    348 #endif
    349 	}
    350 
    351 	if ((p = malloc(hashsize * esize, mtype, mflags)) == NULL)
    352 		return (NULL);
    353 
    354 	switch (htype) {
    355 	case HASH_LIST:
    356 		hashtbl_list = p;
    357 		for (i = 0; i < hashsize; i++)
    358 			LIST_INIT(&hashtbl_list[i]);
    359 		break;
    360 	case HASH_TAILQ:
    361 		hashtbl_tailq = p;
    362 		for (i = 0; i < hashsize; i++)
    363 			TAILQ_INIT(&hashtbl_tailq[i]);
    364 		break;
    365 	}
    366 	*hashmask = hashsize - 1;
    367 	return (p);
    368 }
    369 
    370 /*
    371  * Free memory from hash table previosly allocated via hashinit().
    372  */
    373 void
    374 hashdone(hashtbl, mtype)
    375 	void *hashtbl;
    376 	struct malloc_type *mtype;
    377 {
    378 
    379 	free(hashtbl, mtype);
    380 }
    381 
    382 
    383 static void *
    384 hook_establish(list, fn, arg)
    385 	hook_list_t *list;
    386 	void (*fn) __P((void *));
    387 	void *arg;
    388 {
    389 	struct hook_desc *hd;
    390 
    391 	hd = malloc(sizeof(*hd), M_DEVBUF, M_NOWAIT);
    392 	if (hd == NULL)
    393 		return (NULL);
    394 
    395 	hd->hk_fn = fn;
    396 	hd->hk_arg = arg;
    397 	LIST_INSERT_HEAD(list, hd, hk_list);
    398 
    399 	return (hd);
    400 }
    401 
    402 static void
    403 hook_disestablish(list, vhook)
    404 	hook_list_t *list;
    405 	void *vhook;
    406 {
    407 #ifdef DIAGNOSTIC
    408 	struct hook_desc *hd;
    409 
    410 	LIST_FOREACH(hd, list, hk_list) {
    411                 if (hd == vhook)
    412 			break;
    413 	}
    414 
    415 	if (hd == NULL)
    416 		panic("hook_disestablish: hook %p not established", vhook);
    417 #endif
    418 	LIST_REMOVE((struct hook_desc *)vhook, hk_list);
    419 	free(vhook, M_DEVBUF);
    420 }
    421 
    422 static void
    423 hook_destroy(list)
    424 	hook_list_t *list;
    425 {
    426 	struct hook_desc *hd;
    427 
    428 	while ((hd = LIST_FIRST(list)) != NULL) {
    429 		LIST_REMOVE(hd, hk_list);
    430 		free(hd, M_DEVBUF);
    431 	}
    432 }
    433 
    434 static void
    435 hook_proc_run(list, p)
    436 	hook_list_t *list;
    437 	struct proc *p;
    438 {
    439 	struct hook_desc *hd;
    440 
    441 	for (hd = LIST_FIRST(list); hd != NULL; hd = LIST_NEXT(hd, hk_list)) {
    442 		((void (*) __P((struct proc *, void *)))*hd->hk_fn)(p,
    443 		    hd->hk_arg);
    444 	}
    445 }
    446 
    447 /*
    448  * "Shutdown hook" types, functions, and variables.
    449  *
    450  * Should be invoked immediately before the
    451  * system is halted or rebooted, i.e. after file systems unmounted,
    452  * after crash dump done, etc.
    453  *
    454  * Each shutdown hook is removed from the list before it's run, so that
    455  * it won't be run again.
    456  */
    457 
    458 hook_list_t shutdownhook_list;
    459 
    460 void *
    461 shutdownhook_establish(fn, arg)
    462 	void (*fn) __P((void *));
    463 	void *arg;
    464 {
    465 	return hook_establish(&shutdownhook_list, fn, arg);
    466 }
    467 
    468 void
    469 shutdownhook_disestablish(vhook)
    470 	void *vhook;
    471 {
    472 	hook_disestablish(&shutdownhook_list, vhook);
    473 }
    474 
    475 /*
    476  * Run shutdown hooks.  Should be invoked immediately before the
    477  * system is halted or rebooted, i.e. after file systems unmounted,
    478  * after crash dump done, etc.
    479  *
    480  * Each shutdown hook is removed from the list before it's run, so that
    481  * it won't be run again.
    482  */
    483 void
    484 doshutdownhooks()
    485 {
    486 	struct hook_desc *dp;
    487 
    488 	while ((dp = LIST_FIRST(&shutdownhook_list)) != NULL) {
    489 		LIST_REMOVE(dp, hk_list);
    490 		(*dp->hk_fn)(dp->hk_arg);
    491 #if 0
    492 		/*
    493 		 * Don't bother freeing the hook structure,, since we may
    494 		 * be rebooting because of a memory corruption problem,
    495 		 * and this might only make things worse.  It doesn't
    496 		 * matter, anyway, since the system is just about to
    497 		 * reboot.
    498 		 */
    499 		free(dp, M_DEVBUF);
    500 #endif
    501 	}
    502 }
    503 
    504 /*
    505  * "Mountroot hook" types, functions, and variables.
    506  */
    507 
    508 hook_list_t mountroothook_list;
    509 
    510 void *
    511 mountroothook_establish(fn, dev)
    512 	void (*fn) __P((struct device *));
    513 	struct device *dev;
    514 {
    515 	return hook_establish(&mountroothook_list, (void (*)__P((void *)))fn,
    516 	    dev);
    517 }
    518 
    519 void
    520 mountroothook_disestablish(vhook)
    521 	void *vhook;
    522 {
    523 	hook_disestablish(&mountroothook_list, vhook);
    524 }
    525 
    526 void
    527 mountroothook_destroy()
    528 {
    529 	hook_destroy(&mountroothook_list);
    530 }
    531 
    532 void
    533 domountroothook()
    534 {
    535 	struct hook_desc *hd;
    536 
    537 	LIST_FOREACH(hd, &mountroothook_list, hk_list) {
    538 		if (hd->hk_arg == (void *)root_device) {
    539 			(*hd->hk_fn)(hd->hk_arg);
    540 			return;
    541 		}
    542 	}
    543 }
    544 
    545 hook_list_t exechook_list;
    546 
    547 void *
    548 exechook_establish(fn, arg)
    549 	void (*fn) __P((struct proc *, void *));
    550 	void *arg;
    551 {
    552 	return hook_establish(&exechook_list, (void (*) __P((void *)))fn, arg);
    553 }
    554 
    555 void
    556 exechook_disestablish(vhook)
    557 	void *vhook;
    558 {
    559 	hook_disestablish(&exechook_list, vhook);
    560 }
    561 
    562 /*
    563  * Run exec hooks.
    564  */
    565 void
    566 doexechooks(p)
    567 	struct proc *p;
    568 {
    569 	hook_proc_run(&exechook_list, p);
    570 }
    571 
    572 hook_list_t exithook_list;
    573 
    574 void *
    575 exithook_establish(fn, arg)
    576 	void (*fn) __P((struct proc *, void *));
    577 	void *arg;
    578 {
    579 	return hook_establish(&exithook_list, (void (*) __P((void *)))fn, arg);
    580 }
    581 
    582 void
    583 exithook_disestablish(vhook)
    584 	void *vhook;
    585 {
    586 	hook_disestablish(&exithook_list, vhook);
    587 }
    588 
    589 /*
    590  * Run exit hooks.
    591  */
    592 void
    593 doexithooks(p)
    594 	struct proc *p;
    595 {
    596 	hook_proc_run(&exithook_list, p);
    597 }
    598 
    599 hook_list_t forkhook_list;
    600 
    601 void *
    602 forkhook_establish(fn)
    603 	void (*fn) __P((struct proc *, struct proc *));
    604 {
    605 	return hook_establish(&forkhook_list, (void (*) __P((void *)))fn, NULL);
    606 }
    607 
    608 void
    609 forkhook_disestablish(vhook)
    610 	void *vhook;
    611 {
    612 	hook_disestablish(&forkhook_list, vhook);
    613 }
    614 
    615 /*
    616  * Run fork hooks.
    617  */
    618 void
    619 doforkhooks(p2, p1)
    620 	struct proc *p2, *p1;
    621 {
    622 	struct hook_desc *hd;
    623 
    624 	LIST_FOREACH(hd, &forkhook_list, hk_list) {
    625 		((void (*) __P((struct proc *, struct proc *)))*hd->hk_fn)
    626 		    (p2, p1);
    627 	}
    628 }
    629 
    630 /*
    631  * "Power hook" types, functions, and variables.
    632  * The list of power hooks is kept ordered with the last registered hook
    633  * first.
    634  * When running the hooks on power down the hooks are called in reverse
    635  * registration order, when powering up in registration order.
    636  */
    637 struct powerhook_desc {
    638 	CIRCLEQ_ENTRY(powerhook_desc) sfd_list;
    639 	void	(*sfd_fn) __P((int, void *));
    640 	void	*sfd_arg;
    641 };
    642 
    643 CIRCLEQ_HEAD(, powerhook_desc) powerhook_list =
    644 	CIRCLEQ_HEAD_INITIALIZER(powerhook_list);
    645 
    646 void *
    647 powerhook_establish(fn, arg)
    648 	void (*fn) __P((int, void *));
    649 	void *arg;
    650 {
    651 	struct powerhook_desc *ndp;
    652 
    653 	ndp = (struct powerhook_desc *)
    654 	    malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
    655 	if (ndp == NULL)
    656 		return (NULL);
    657 
    658 	ndp->sfd_fn = fn;
    659 	ndp->sfd_arg = arg;
    660 	CIRCLEQ_INSERT_HEAD(&powerhook_list, ndp, sfd_list);
    661 
    662 	return (ndp);
    663 }
    664 
    665 void
    666 powerhook_disestablish(vhook)
    667 	void *vhook;
    668 {
    669 #ifdef DIAGNOSTIC
    670 	struct powerhook_desc *dp;
    671 
    672 	CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list)
    673                 if (dp == vhook)
    674 			goto found;
    675 	panic("powerhook_disestablish: hook %p not established", vhook);
    676  found:
    677 #endif
    678 
    679 	CIRCLEQ_REMOVE(&powerhook_list, (struct powerhook_desc *)vhook,
    680 	    sfd_list);
    681 	free(vhook, M_DEVBUF);
    682 }
    683 
    684 /*
    685  * Run power hooks.
    686  */
    687 void
    688 dopowerhooks(why)
    689 	int why;
    690 {
    691 	struct powerhook_desc *dp;
    692 
    693 	if (why == PWR_RESUME || why == PWR_SOFTRESUME) {
    694 		CIRCLEQ_FOREACH_REVERSE(dp, &powerhook_list, sfd_list) {
    695 			(*dp->sfd_fn)(why, dp->sfd_arg);
    696 		}
    697 	} else {
    698 		CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list) {
    699 			(*dp->sfd_fn)(why, dp->sfd_arg);
    700 		}
    701 	}
    702 }
    703 
    704 /*
    705  * Determine the root device and, if instructed to, the root file system.
    706  */
    707 
    708 #include "md.h"
    709 #if NMD == 0
    710 #undef MEMORY_DISK_HOOKS
    711 #endif
    712 
    713 #ifdef MEMORY_DISK_HOOKS
    714 static struct device fakemdrootdev[NMD];
    715 #endif
    716 
    717 #include "raid.h"
    718 #if NRAID == 1
    719 #define BOOT_FROM_RAID_HOOKS 1
    720 #endif
    721 
    722 #ifdef BOOT_FROM_RAID_HOOKS
    723 extern int numraid;
    724 extern struct device *raidrootdev;
    725 #endif
    726 
    727 void
    728 setroot(bootdv, bootpartition)
    729 	struct device *bootdv;
    730 	int bootpartition;
    731 {
    732 	struct device *dv;
    733 	int len;
    734 #ifdef MEMORY_DISK_HOOKS
    735 	int i;
    736 #endif
    737 	dev_t nrootdev;
    738 	dev_t ndumpdev = NODEV;
    739 	char buf[128];
    740 	const char *rootdevname;
    741 	const char *dumpdevname;
    742 	struct device *rootdv = NULL;		/* XXX gcc -Wuninitialized */
    743 	struct device *dumpdv = NULL;
    744 	struct ifnet *ifp;
    745 	const char *deffsname;
    746 	struct vfsops *vops;
    747 
    748 #ifdef MEMORY_DISK_HOOKS
    749 	for (i = 0; i < NMD; i++) {
    750 		fakemdrootdev[i].dv_class  = DV_DISK;
    751 		fakemdrootdev[i].dv_cfdata = NULL;
    752 		fakemdrootdev[i].dv_unit   = i;
    753 		fakemdrootdev[i].dv_parent = NULL;
    754 		sprintf(fakemdrootdev[i].dv_xname, "md%d", i);
    755 	}
    756 #endif /* MEMORY_DISK_HOOKS */
    757 
    758 #ifdef MEMORY_DISK_IS_ROOT
    759 	bootdv = &fakemdrootdev[0];
    760 	bootpartition = 0;
    761 #endif
    762 
    763 	/*
    764 	 * If NFS is specified as the file system, and we found
    765 	 * a DV_DISK boot device (or no boot device at all), then
    766 	 * find a reasonable network interface for "rootspec".
    767 	 */
    768 	vops = vfs_getopsbyname("nfs");
    769 	if (vops != NULL && vops->vfs_mountroot == mountroot &&
    770 	    rootspec == NULL &&
    771 	    (bootdv == NULL || bootdv->dv_class != DV_IFNET)) {
    772 		TAILQ_FOREACH(ifp, &ifnet, if_list) {
    773 			if ((ifp->if_flags &
    774 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    775 				break;
    776 		}
    777 		if (ifp == NULL) {
    778 			/*
    779 			 * Can't find a suitable interface; ask the
    780 			 * user.
    781 			 */
    782 			boothowto |= RB_ASKNAME;
    783 		} else {
    784 			/*
    785 			 * Have a suitable interface; behave as if
    786 			 * the user specified this interface.
    787 			 */
    788 			rootspec = (const char *)ifp->if_xname;
    789 		}
    790 	}
    791 
    792 	/*
    793 	 * If wildcarded root and we the boot device wasn't determined,
    794 	 * ask the user.
    795 	 */
    796 	if (rootspec == NULL && bootdv == NULL)
    797 		boothowto |= RB_ASKNAME;
    798 
    799  top:
    800 	if (boothowto & RB_ASKNAME) {
    801 		struct device *defdumpdv;
    802 
    803 		for (;;) {
    804 			printf("root device");
    805 			if (bootdv != NULL) {
    806 				printf(" (default %s", bootdv->dv_xname);
    807 				if (bootdv->dv_class == DV_DISK)
    808 					printf("%c", bootpartition + 'a');
    809 				printf(")");
    810 			}
    811 			printf(": ");
    812 			len = cngetsn(buf, sizeof(buf));
    813 			if (len == 0 && bootdv != NULL) {
    814 				strlcpy(buf, bootdv->dv_xname, sizeof(buf));
    815 				len = strlen(buf);
    816 			}
    817 			if (len > 0 && buf[len - 1] == '*') {
    818 				buf[--len] = '\0';
    819 				dv = getdisk(buf, len, 1, &nrootdev, 0);
    820 				if (dv != NULL) {
    821 					rootdv = dv;
    822 					break;
    823 				}
    824 			}
    825 			dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
    826 			if (dv != NULL) {
    827 				rootdv = dv;
    828 				break;
    829 			}
    830 		}
    831 
    832 		/*
    833 		 * Set up the default dump device.  If root is on
    834 		 * a network device, there is no default dump
    835 		 * device, since we don't support dumps to the
    836 		 * network.
    837 		 */
    838 		if (rootdv->dv_class == DV_IFNET)
    839 			defdumpdv = NULL;
    840 		else
    841 			defdumpdv = rootdv;
    842 
    843 		for (;;) {
    844 			printf("dump device");
    845 			if (defdumpdv != NULL) {
    846 				/*
    847 				 * Note, we know it's a disk if we get here.
    848 				 */
    849 				printf(" (default %sb)", defdumpdv->dv_xname);
    850 			}
    851 			printf(": ");
    852 			len = cngetsn(buf, sizeof(buf));
    853 			if (len == 0) {
    854 				if (defdumpdv != NULL) {
    855 					ndumpdev = MAKEDISKDEV(major(nrootdev),
    856 					    DISKUNIT(nrootdev), 1);
    857 				}
    858 				dumpdv = defdumpdv;
    859 				break;
    860 			}
    861 			if (len == 4 && strcmp(buf, "none") == 0) {
    862 				dumpdv = NULL;
    863 				break;
    864 			}
    865 			dv = getdisk(buf, len, 1, &ndumpdev, 1);
    866 			if (dv != NULL) {
    867 				dumpdv = dv;
    868 				break;
    869 			}
    870 		}
    871 
    872 		rootdev = nrootdev;
    873 		dumpdev = ndumpdev;
    874 
    875 		for (vops = LIST_FIRST(&vfs_list); vops != NULL;
    876 		     vops = LIST_NEXT(vops, vfs_list)) {
    877 			if (vops->vfs_mountroot != NULL &&
    878 			    vops->vfs_mountroot == mountroot)
    879 			break;
    880 		}
    881 
    882 		if (vops == NULL) {
    883 			mountroot = NULL;
    884 			deffsname = "generic";
    885 		} else
    886 			deffsname = vops->vfs_name;
    887 
    888 		for (;;) {
    889 			printf("file system (default %s): ", deffsname);
    890 			len = cngetsn(buf, sizeof(buf));
    891 			if (len == 0)
    892 				break;
    893 			if (len == 4 && strcmp(buf, "halt") == 0)
    894 				cpu_reboot(RB_HALT, NULL);
    895 			else if (len == 6 && strcmp(buf, "reboot") == 0)
    896 				cpu_reboot(0, NULL);
    897 #if defined(DDB)
    898 			else if (len == 3 && strcmp(buf, "ddb") == 0) {
    899 				console_debugger();
    900 			}
    901 #endif
    902 			else if (len == 7 && strcmp(buf, "generic") == 0) {
    903 				mountroot = NULL;
    904 				break;
    905 			}
    906 			vops = vfs_getopsbyname(buf);
    907 			if (vops == NULL || vops->vfs_mountroot == NULL) {
    908 				printf("use one of: generic");
    909 				for (vops = LIST_FIRST(&vfs_list);
    910 				     vops != NULL;
    911 				     vops = LIST_NEXT(vops, vfs_list)) {
    912 					if (vops->vfs_mountroot != NULL)
    913 						printf(" %s", vops->vfs_name);
    914 				}
    915 #if defined(DDB)
    916 				printf(" ddb");
    917 #endif
    918 				printf(" halt reboot\n");
    919 			} else {
    920 				mountroot = vops->vfs_mountroot;
    921 				break;
    922 			}
    923 		}
    924 
    925 	} else if (rootspec == NULL) {
    926 		int majdev;
    927 
    928 		/*
    929 		 * Wildcarded root; use the boot device.
    930 		 */
    931 		rootdv = bootdv;
    932 
    933 		majdev = devsw_name2blk(bootdv->dv_xname, NULL, 0);
    934 		if (majdev >= 0) {
    935 			/*
    936 			 * Root is on a disk.  `bootpartition' is root.
    937 			 */
    938 			rootdev = MAKEDISKDEV(majdev, bootdv->dv_unit,
    939 			    bootpartition);
    940 		}
    941 	} else {
    942 
    943 		/*
    944 		 * `root on <dev> ...'
    945 		 */
    946 
    947 		/*
    948 		 * If it's a network interface, we can bail out
    949 		 * early.
    950 		 */
    951 		dv = finddevice(rootspec);
    952 		if (dv != NULL && dv->dv_class == DV_IFNET) {
    953 			rootdv = dv;
    954 			goto haveroot;
    955 		}
    956 
    957 		rootdevname = devsw_blk2name(major(rootdev));
    958 		if (rootdevname == NULL) {
    959 			printf("unknown device major 0x%x\n", rootdev);
    960 			boothowto |= RB_ASKNAME;
    961 			goto top;
    962 		}
    963 		memset(buf, 0, sizeof(buf));
    964 		sprintf(buf, "%s%d", rootdevname, DISKUNIT(rootdev));
    965 
    966 		rootdv = finddevice(buf);
    967 		if (rootdv == NULL) {
    968 			printf("device %s (0x%x) not configured\n",
    969 			    buf, rootdev);
    970 			boothowto |= RB_ASKNAME;
    971 			goto top;
    972 		}
    973 	}
    974 
    975  haveroot:
    976 
    977 	root_device = rootdv;
    978 
    979 	switch (rootdv->dv_class) {
    980 	case DV_IFNET:
    981 		aprint_normal("root on %s", rootdv->dv_xname);
    982 		break;
    983 
    984 	case DV_DISK:
    985 		aprint_normal("root on %s%c", rootdv->dv_xname,
    986 		    DISKPART(rootdev) + 'a');
    987 		break;
    988 
    989 	default:
    990 		printf("can't determine root device\n");
    991 		boothowto |= RB_ASKNAME;
    992 		goto top;
    993 	}
    994 
    995 	/*
    996 	 * Now configure the dump device.
    997 	 *
    998 	 * If we haven't figured out the dump device, do so, with
    999 	 * the following rules:
   1000 	 *
   1001 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
   1002 	 *
   1003 	 *	(b) If dumpspec is set, try to use it.  If the device
   1004 	 *	    is not available, punt.
   1005 	 *
   1006 	 *	(c) If dumpspec is not set, the dump device is
   1007 	 *	    wildcarded or unspecified.  If the root device
   1008 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
   1009 	 *	    of the root device.
   1010 	 */
   1011 
   1012 	if (boothowto & RB_ASKNAME) {		/* (a) */
   1013 		if (dumpdv == NULL)
   1014 			goto nodumpdev;
   1015 	} else if (dumpspec != NULL) {		/* (b) */
   1016 		if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
   1017 			/*
   1018 			 * Operator doesn't want a dump device.
   1019 			 * Or looks like they tried to pick a network
   1020 			 * device.  Oops.
   1021 			 */
   1022 			goto nodumpdev;
   1023 		}
   1024 
   1025 		dumpdevname = devsw_blk2name(major(dumpdev));
   1026 		if (dumpdevname == NULL)
   1027 			goto nodumpdev;
   1028 		memset(buf, 0, sizeof(buf));
   1029 		sprintf(buf, "%s%d", dumpdevname, DISKUNIT(dumpdev));
   1030 
   1031 		dumpdv = finddevice(buf);
   1032 		if (dumpdv == NULL) {
   1033 			/*
   1034 			 * Device not configured.
   1035 			 */
   1036 			goto nodumpdev;
   1037 		}
   1038 	} else {				/* (c) */
   1039 		if (rootdv->dv_class == DV_IFNET)
   1040 			goto nodumpdev;
   1041 		else {
   1042 			dumpdv = rootdv;
   1043 			dumpdev = MAKEDISKDEV(major(rootdev),
   1044 			    dumpdv->dv_unit, 1);
   1045 		}
   1046 	}
   1047 
   1048 	aprint_normal(" dumps on %s%c\n", dumpdv->dv_xname,
   1049 	    DISKPART(dumpdev) + 'a');
   1050 	return;
   1051 
   1052  nodumpdev:
   1053 	dumpdev = NODEV;
   1054 	aprint_normal("\n");
   1055 }
   1056 
   1057 static struct device *
   1058 finddevice(name)
   1059 	const char *name;
   1060 {
   1061 	struct device *dv;
   1062 #ifdef BOOT_FROM_RAID_HOOKS
   1063 	int j;
   1064 
   1065 	for (j = 0; j < numraid; j++) {
   1066 		if (strcmp(name, raidrootdev[j].dv_xname) == 0) {
   1067 			dv = &raidrootdev[j];
   1068 			return (dv);
   1069 		}
   1070 	}
   1071 #endif
   1072 
   1073 	for (dv = TAILQ_FIRST(&alldevs); dv != NULL;
   1074 	    dv = TAILQ_NEXT(dv, dv_list))
   1075 		if (strcmp(dv->dv_xname, name) == 0)
   1076 			break;
   1077 	return (dv);
   1078 }
   1079 
   1080 static struct device *
   1081 getdisk(str, len, defpart, devp, isdump)
   1082 	char *str;
   1083 	int len, defpart;
   1084 	dev_t *devp;
   1085 	int isdump;
   1086 {
   1087 	struct device	*dv;
   1088 #ifdef MEMORY_DISK_HOOKS
   1089 	int		i;
   1090 #endif
   1091 #ifdef BOOT_FROM_RAID_HOOKS
   1092 	int 		j;
   1093 #endif
   1094 
   1095 	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
   1096 		printf("use one of:");
   1097 #ifdef MEMORY_DISK_HOOKS
   1098 		if (isdump == 0)
   1099 			for (i = 0; i < NMD; i++)
   1100 				printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
   1101 				    'a' + MAXPARTITIONS - 1);
   1102 #endif
   1103 #ifdef BOOT_FROM_RAID_HOOKS
   1104 		if (isdump == 0)
   1105 			for (j = 0; j < numraid; j++)
   1106 				printf(" %s[a-%c]", raidrootdev[j].dv_xname,
   1107 				    'a' + MAXPARTITIONS - 1);
   1108 #endif
   1109 		TAILQ_FOREACH(dv, &alldevs, dv_list) {
   1110 			if (dv->dv_class == DV_DISK)
   1111 				printf(" %s[a-%c]", dv->dv_xname,
   1112 				    'a' + MAXPARTITIONS - 1);
   1113 			if (isdump == 0 && dv->dv_class == DV_IFNET)
   1114 				printf(" %s", dv->dv_xname);
   1115 		}
   1116 		if (isdump)
   1117 			printf(" none");
   1118 #if defined(DDB)
   1119 		printf(" ddb");
   1120 #endif
   1121 		printf(" halt reboot\n");
   1122 	}
   1123 	return (dv);
   1124 }
   1125 
   1126 static struct device *
   1127 parsedisk(str, len, defpart, devp)
   1128 	char *str;
   1129 	int len, defpart;
   1130 	dev_t *devp;
   1131 {
   1132 	struct device *dv;
   1133 	char *cp, c;
   1134 	int majdev, part;
   1135 #ifdef MEMORY_DISK_HOOKS
   1136 	int i;
   1137 #endif
   1138 	if (len == 0)
   1139 		return (NULL);
   1140 
   1141 	if (len == 4 && strcmp(str, "halt") == 0)
   1142 		cpu_reboot(RB_HALT, NULL);
   1143 	else if (len == 6 && strcmp(str, "reboot") == 0)
   1144 		cpu_reboot(0, NULL);
   1145 #if defined(DDB)
   1146 	else if (len == 3 && strcmp(str, "ddb") == 0)
   1147 		console_debugger();
   1148 #endif
   1149 
   1150 	cp = str + len - 1;
   1151 	c = *cp;
   1152 	if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
   1153 		part = c - 'a';
   1154 		*cp = '\0';
   1155 	} else
   1156 		part = defpart;
   1157 
   1158 #ifdef MEMORY_DISK_HOOKS
   1159 	for (i = 0; i < NMD; i++)
   1160 		if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) {
   1161 			dv = &fakemdrootdev[i];
   1162 			goto gotdisk;
   1163 		}
   1164 #endif
   1165 
   1166 	dv = finddevice(str);
   1167 	if (dv != NULL) {
   1168 		if (dv->dv_class == DV_DISK) {
   1169 #ifdef MEMORY_DISK_HOOKS
   1170  gotdisk:
   1171 #endif
   1172 			majdev = devsw_name2blk(dv->dv_xname, NULL, 0);
   1173 			if (majdev < 0)
   1174 				panic("parsedisk");
   1175 			*devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
   1176 		}
   1177 
   1178 		if (dv->dv_class == DV_IFNET)
   1179 			*devp = NODEV;
   1180 	}
   1181 
   1182 	*cp = c;
   1183 	return (dv);
   1184 }
   1185 
   1186 /*
   1187  * snprintf() `bytes' into `buf', reformatting it so that the number,
   1188  * plus a possible `x' + suffix extension) fits into len bytes (including
   1189  * the terminating NUL).
   1190  * Returns the number of bytes stored in buf, or -1 if there was a problem.
   1191  * E.g, given a len of 9 and a suffix of `B':
   1192  *	bytes		result
   1193  *	-----		------
   1194  *	99999		`99999 B'
   1195  *	100000		`97 kB'
   1196  *	66715648	`65152 kB'
   1197  *	252215296	`240 MB'
   1198  */
   1199 int
   1200 humanize_number(buf, len, bytes, suffix, divisor)
   1201 	char		*buf;
   1202 	size_t		 len;
   1203 	u_int64_t	 bytes;
   1204 	const char	*suffix;
   1205 	int 		divisor;
   1206 {
   1207        	/* prefixes are: (none), kilo, Mega, Giga, Tera, Peta, Exa */
   1208 	const char *prefixes;
   1209 	int		r;
   1210 	u_int64_t	max;
   1211 	size_t		i, suffixlen;
   1212 
   1213 	if (buf == NULL || suffix == NULL)
   1214 		return (-1);
   1215 	if (len > 0)
   1216 		buf[0] = '\0';
   1217 	suffixlen = strlen(suffix);
   1218 	/* check if enough room for `x y' + suffix + `\0' */
   1219 	if (len < 4 + suffixlen)
   1220 		return (-1);
   1221 
   1222 	if (divisor == 1024) {
   1223 		/*
   1224 		 * binary multiplies
   1225 		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
   1226 		 */
   1227 		prefixes = " KMGTPE";
   1228 	} else
   1229 		prefixes = " kMGTPE"; /* SI for decimal multiplies */
   1230 
   1231 	max = 1;
   1232 	for (i = 0; i < len - suffixlen - 3; i++)
   1233 		max *= 10;
   1234 	for (i = 0; bytes >= max && prefixes[i + 1]; i++)
   1235 		bytes /= divisor;
   1236 
   1237 	r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
   1238 	    i == 0 ? "" : " ", prefixes[i], suffix);
   1239 
   1240 	return (r);
   1241 }
   1242 
   1243 int
   1244 format_bytes(buf, len, bytes)
   1245 	char		*buf;
   1246 	size_t		 len;
   1247 	u_int64_t	 bytes;
   1248 {
   1249 	int	rv;
   1250 	size_t	nlen;
   1251 
   1252 	rv = humanize_number(buf, len, bytes, "B", 1024);
   1253 	if (rv != -1) {
   1254 			/* nuke the trailing ` B' if it exists */
   1255 		nlen = strlen(buf) - 2;
   1256 		if (strcmp(&buf[nlen], " B") == 0)
   1257 			buf[nlen] = '\0';
   1258 	}
   1259 	return (rv);
   1260 }
   1261 
   1262 /*
   1263  * Start trace of particular system call. If process is being traced,
   1264  * this routine is called by MD syscall dispatch code just before
   1265  * a system call is actually executed.
   1266  * MD caller guarantees the passed 'code' is within the supported
   1267  * system call number range for emulation the process runs under.
   1268  */
   1269 int
   1270 trace_enter(struct lwp *l, register_t code,
   1271 	register_t realcode, const struct sysent *callp, void *args,
   1272 	register_t rval[])
   1273 {
   1274 #if defined(KTRACE) || defined(SYSTRACE)
   1275 	struct proc *p = l->l_proc;
   1276 #endif
   1277 
   1278 #ifdef SYSCALL_DEBUG
   1279 	scdebug_call(l, code, args);
   1280 #endif /* SYSCALL_DEBUG */
   1281 
   1282 #ifdef KTRACE
   1283 	if (KTRPOINT(p, KTR_SYSCALL))
   1284 		ktrsyscall(p, code, realcode, callp, args);
   1285 #endif /* KTRACE */
   1286 
   1287 #ifdef SYSTRACE
   1288 	if (ISSET(p->p_flag, P_SYSTRACE))
   1289 		return systrace_enter(p, code, args, rval);
   1290 #endif
   1291 	return 0;
   1292 }
   1293 
   1294 /*
   1295  * End trace of particular system call. If process is being traced,
   1296  * this routine is called by MD syscall dispatch code just after
   1297  * a system call finishes.
   1298  * MD caller guarantees the passed 'code' is within the supported
   1299  * system call number range for emulation the process runs under.
   1300  */
   1301 void
   1302 trace_exit(struct lwp *l, register_t code, void *args, register_t rval[],
   1303     int error)
   1304 {
   1305 #if defined(KTRACE) || defined(SYSTRACE)
   1306 	struct proc *p = l->l_proc;
   1307 #endif
   1308 
   1309 #ifdef SYSCALL_DEBUG
   1310 	scdebug_ret(l, code, error, rval);
   1311 #endif /* SYSCALL_DEBUG */
   1312 
   1313 #ifdef KTRACE
   1314 	if (KTRPOINT(p, KTR_SYSRET)) {
   1315 		KERNEL_PROC_LOCK(l);
   1316 		ktrsysret(p, code, error, rval);
   1317 		KERNEL_PROC_UNLOCK(l);
   1318 	}
   1319 #endif /* KTRACE */
   1320 
   1321 #ifdef SYSTRACE
   1322 	if (ISSET(p->p_flag, P_SYSTRACE))
   1323 		systrace_exit(p, code, args, rval, error);
   1324 #endif
   1325 }
   1326