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