Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.110
      1 /*	$NetBSD: kern_subr.c,v 1.110 2004/04/21 18:40:38 itojun 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.110 2004/04/21 18:40:38 itojun 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(const char *);
    118 static struct device *getdisk(char *, int, int, dev_t *, int);
    119 static struct device *parsedisk(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)(void *);
    127 	void	*hk_arg;
    128 };
    129 typedef LIST_HEAD(, hook_desc) hook_list_t;
    130 
    131 static void *hook_establish(hook_list_t *, void (*)(void *), void *);
    132 static void hook_disestablish(hook_list_t *, void *);
    133 static void hook_destroy(hook_list_t *);
    134 static void hook_proc_run(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  * Wrapper for uiomove() that validates the arguments against a known-good
    210  * kernel buffer.  Currently, uiomove accepts a signed (n) argument, which
    211  * is almost definitely a bad thing, so we catch that here as well.  We
    212  * return a runtime failure, but it might be desirable to generate a runtime
    213  * assertion failure instead.
    214  */
    215 int
    216 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
    217 {
    218 	unsigned int offset, n;
    219 
    220 	if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
    221 	    (offset = uio->uio_offset) != uio->uio_offset)
    222 		return (EINVAL);
    223 	if (buflen <= 0 || offset >= buflen)
    224 		return (0);
    225 	if ((n = buflen - offset) > INT_MAX)
    226 		return (EINVAL);
    227 	return (uiomove((char *)buf + offset, n, uio));
    228 }
    229 
    230 /*
    231  * Give next character to user as result of read.
    232  */
    233 int
    234 ureadc(c, uio)
    235 	int c;
    236 	struct uio *uio;
    237 {
    238 	struct iovec *iov;
    239 
    240 	if (uio->uio_resid <= 0)
    241 		panic("ureadc: non-positive resid");
    242 again:
    243 	if (uio->uio_iovcnt <= 0)
    244 		panic("ureadc: non-positive iovcnt");
    245 	iov = uio->uio_iov;
    246 	if (iov->iov_len <= 0) {
    247 		uio->uio_iovcnt--;
    248 		uio->uio_iov++;
    249 		goto again;
    250 	}
    251 	switch (uio->uio_segflg) {
    252 
    253 	case UIO_USERSPACE:
    254 		if (subyte(iov->iov_base, c) < 0)
    255 			return (EFAULT);
    256 		break;
    257 
    258 	case UIO_SYSSPACE:
    259 		*(char *)iov->iov_base = c;
    260 		break;
    261 	}
    262 	iov->iov_base = (caddr_t)iov->iov_base + 1;
    263 	iov->iov_len--;
    264 	uio->uio_resid--;
    265 	uio->uio_offset++;
    266 	return (0);
    267 }
    268 
    269 /*
    270  * Like copyin(), but operates on an arbitrary process.
    271  */
    272 int
    273 copyin_proc(struct proc *p, const void *uaddr, void *kaddr, size_t len)
    274 {
    275 	struct iovec iov;
    276 	struct uio uio;
    277 	int error;
    278 
    279 	if (len == 0)
    280 		return (0);
    281 
    282 	iov.iov_base = kaddr;
    283 	iov.iov_len = len;
    284 	uio.uio_iov = &iov;
    285 	uio.uio_iovcnt = 1;
    286 	uio.uio_offset = (off_t)(intptr_t)uaddr;
    287 	uio.uio_resid = len;
    288 	uio.uio_segflg = UIO_SYSSPACE;
    289 	uio.uio_rw = UIO_READ;
    290 	uio.uio_procp = NULL;
    291 
    292 	/* XXXCDC: how should locking work here? */
    293 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
    294 		return (EFAULT);
    295 	p->p_vmspace->vm_refcnt++;	/* XXX */
    296 	error = uvm_io(&p->p_vmspace->vm_map, &uio);
    297 	uvmspace_free(p->p_vmspace);
    298 
    299 	return (error);
    300 }
    301 
    302 /*
    303  * Like copyout(), but operates on an arbitrary process.
    304  */
    305 int
    306 copyout_proc(struct proc *p, const void *kaddr, void *uaddr, size_t len)
    307 {
    308 	struct iovec iov;
    309 	struct uio uio;
    310 	int error;
    311 
    312 	if (len == 0)
    313 		return (0);
    314 
    315 	iov.iov_base = (void *) kaddr;	/* XXX cast away const */
    316 	iov.iov_len = len;
    317 	uio.uio_iov = &iov;
    318 	uio.uio_iovcnt = 1;
    319 	uio.uio_offset = (off_t)(intptr_t)uaddr;
    320 	uio.uio_resid = len;
    321 	uio.uio_segflg = UIO_SYSSPACE;
    322 	uio.uio_rw = UIO_WRITE;
    323 	uio.uio_procp = NULL;
    324 
    325 	/* XXXCDC: how should locking work here? */
    326 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
    327 		return (EFAULT);
    328 	p->p_vmspace->vm_refcnt++;	/* XXX */
    329 	error = uvm_io(&p->p_vmspace->vm_map, &uio);
    330 	uvmspace_free(p->p_vmspace);
    331 
    332 	return (error);
    333 }
    334 
    335 /*
    336  * General routine to allocate a hash table.
    337  * Allocate enough memory to hold at least `elements' list-head pointers.
    338  * Return a pointer to the allocated space and set *hashmask to a pattern
    339  * suitable for masking a value to use as an index into the returned array.
    340  */
    341 void *
    342 hashinit(elements, htype, mtype, mflags, hashmask)
    343 	u_int elements;
    344 	enum hashtype htype;
    345 	struct malloc_type *mtype;
    346 	int mflags;
    347 	u_long *hashmask;
    348 {
    349 	u_long hashsize, i;
    350 	LIST_HEAD(, generic) *hashtbl_list;
    351 	TAILQ_HEAD(, generic) *hashtbl_tailq;
    352 	size_t esize;
    353 	void *p;
    354 
    355 	if (elements == 0)
    356 		panic("hashinit: bad cnt");
    357 	for (hashsize = 1; hashsize < elements; hashsize <<= 1)
    358 		continue;
    359 
    360 	switch (htype) {
    361 	case HASH_LIST:
    362 		esize = sizeof(*hashtbl_list);
    363 		break;
    364 	case HASH_TAILQ:
    365 		esize = sizeof(*hashtbl_tailq);
    366 		break;
    367 	default:
    368 #ifdef DIAGNOSTIC
    369 		panic("hashinit: invalid table type");
    370 #else
    371 		return NULL;
    372 #endif
    373 	}
    374 
    375 	if ((p = malloc(hashsize * esize, mtype, mflags)) == NULL)
    376 		return (NULL);
    377 
    378 	switch (htype) {
    379 	case HASH_LIST:
    380 		hashtbl_list = p;
    381 		for (i = 0; i < hashsize; i++)
    382 			LIST_INIT(&hashtbl_list[i]);
    383 		break;
    384 	case HASH_TAILQ:
    385 		hashtbl_tailq = p;
    386 		for (i = 0; i < hashsize; i++)
    387 			TAILQ_INIT(&hashtbl_tailq[i]);
    388 		break;
    389 	}
    390 	*hashmask = hashsize - 1;
    391 	return (p);
    392 }
    393 
    394 /*
    395  * Free memory from hash table previosly allocated via hashinit().
    396  */
    397 void
    398 hashdone(hashtbl, mtype)
    399 	void *hashtbl;
    400 	struct malloc_type *mtype;
    401 {
    402 
    403 	free(hashtbl, mtype);
    404 }
    405 
    406 
    407 static void *
    408 hook_establish(list, fn, arg)
    409 	hook_list_t *list;
    410 	void (*fn)(void *);
    411 	void *arg;
    412 {
    413 	struct hook_desc *hd;
    414 
    415 	hd = malloc(sizeof(*hd), M_DEVBUF, M_NOWAIT);
    416 	if (hd == NULL)
    417 		return (NULL);
    418 
    419 	hd->hk_fn = fn;
    420 	hd->hk_arg = arg;
    421 	LIST_INSERT_HEAD(list, hd, hk_list);
    422 
    423 	return (hd);
    424 }
    425 
    426 static void
    427 hook_disestablish(list, vhook)
    428 	hook_list_t *list;
    429 	void *vhook;
    430 {
    431 #ifdef DIAGNOSTIC
    432 	struct hook_desc *hd;
    433 
    434 	LIST_FOREACH(hd, list, hk_list) {
    435                 if (hd == vhook)
    436 			break;
    437 	}
    438 
    439 	if (hd == NULL)
    440 		panic("hook_disestablish: hook %p not established", vhook);
    441 #endif
    442 	LIST_REMOVE((struct hook_desc *)vhook, hk_list);
    443 	free(vhook, M_DEVBUF);
    444 }
    445 
    446 static void
    447 hook_destroy(list)
    448 	hook_list_t *list;
    449 {
    450 	struct hook_desc *hd;
    451 
    452 	while ((hd = LIST_FIRST(list)) != NULL) {
    453 		LIST_REMOVE(hd, hk_list);
    454 		free(hd, M_DEVBUF);
    455 	}
    456 }
    457 
    458 static void
    459 hook_proc_run(list, p)
    460 	hook_list_t *list;
    461 	struct proc *p;
    462 {
    463 	struct hook_desc *hd;
    464 
    465 	for (hd = LIST_FIRST(list); hd != NULL; hd = LIST_NEXT(hd, hk_list)) {
    466 		((void (*)(struct proc *, void *))*hd->hk_fn)(p,
    467 		    hd->hk_arg);
    468 	}
    469 }
    470 
    471 /*
    472  * "Shutdown hook" types, functions, and variables.
    473  *
    474  * Should be invoked immediately before the
    475  * system is halted or rebooted, i.e. after file systems unmounted,
    476  * after crash dump done, etc.
    477  *
    478  * Each shutdown hook is removed from the list before it's run, so that
    479  * it won't be run again.
    480  */
    481 
    482 hook_list_t shutdownhook_list;
    483 
    484 void *
    485 shutdownhook_establish(fn, arg)
    486 	void (*fn)(void *);
    487 	void *arg;
    488 {
    489 	return hook_establish(&shutdownhook_list, fn, arg);
    490 }
    491 
    492 void
    493 shutdownhook_disestablish(vhook)
    494 	void *vhook;
    495 {
    496 	hook_disestablish(&shutdownhook_list, vhook);
    497 }
    498 
    499 /*
    500  * Run shutdown hooks.  Should be invoked immediately before the
    501  * system is halted or rebooted, i.e. after file systems unmounted,
    502  * after crash dump done, etc.
    503  *
    504  * Each shutdown hook is removed from the list before it's run, so that
    505  * it won't be run again.
    506  */
    507 void
    508 doshutdownhooks()
    509 {
    510 	struct hook_desc *dp;
    511 
    512 	while ((dp = LIST_FIRST(&shutdownhook_list)) != NULL) {
    513 		LIST_REMOVE(dp, hk_list);
    514 		(*dp->hk_fn)(dp->hk_arg);
    515 #if 0
    516 		/*
    517 		 * Don't bother freeing the hook structure,, since we may
    518 		 * be rebooting because of a memory corruption problem,
    519 		 * and this might only make things worse.  It doesn't
    520 		 * matter, anyway, since the system is just about to
    521 		 * reboot.
    522 		 */
    523 		free(dp, M_DEVBUF);
    524 #endif
    525 	}
    526 }
    527 
    528 /*
    529  * "Mountroot hook" types, functions, and variables.
    530  */
    531 
    532 hook_list_t mountroothook_list;
    533 
    534 void *
    535 mountroothook_establish(fn, dev)
    536 	void (*fn)(struct device *);
    537 	struct device *dev;
    538 {
    539 	return hook_establish(&mountroothook_list, (void (*)(void *))fn, dev);
    540 }
    541 
    542 void
    543 mountroothook_disestablish(vhook)
    544 	void *vhook;
    545 {
    546 	hook_disestablish(&mountroothook_list, vhook);
    547 }
    548 
    549 void
    550 mountroothook_destroy()
    551 {
    552 	hook_destroy(&mountroothook_list);
    553 }
    554 
    555 void
    556 domountroothook()
    557 {
    558 	struct hook_desc *hd;
    559 
    560 	LIST_FOREACH(hd, &mountroothook_list, hk_list) {
    561 		if (hd->hk_arg == (void *)root_device) {
    562 			(*hd->hk_fn)(hd->hk_arg);
    563 			return;
    564 		}
    565 	}
    566 }
    567 
    568 hook_list_t exechook_list;
    569 
    570 void *
    571 exechook_establish(fn, arg)
    572 	void (*fn)(struct proc *, void *);
    573 	void *arg;
    574 {
    575 	return hook_establish(&exechook_list, (void (*)(void *))fn, arg);
    576 }
    577 
    578 void
    579 exechook_disestablish(vhook)
    580 	void *vhook;
    581 {
    582 	hook_disestablish(&exechook_list, vhook);
    583 }
    584 
    585 /*
    586  * Run exec hooks.
    587  */
    588 void
    589 doexechooks(p)
    590 	struct proc *p;
    591 {
    592 	hook_proc_run(&exechook_list, p);
    593 }
    594 
    595 hook_list_t exithook_list;
    596 
    597 void *
    598 exithook_establish(fn, arg)
    599 	void (*fn)(struct proc *, void *);
    600 	void *arg;
    601 {
    602 	return hook_establish(&exithook_list, (void (*)(void *))fn, arg);
    603 }
    604 
    605 void
    606 exithook_disestablish(vhook)
    607 	void *vhook;
    608 {
    609 	hook_disestablish(&exithook_list, vhook);
    610 }
    611 
    612 /*
    613  * Run exit hooks.
    614  */
    615 void
    616 doexithooks(p)
    617 	struct proc *p;
    618 {
    619 	hook_proc_run(&exithook_list, p);
    620 }
    621 
    622 hook_list_t forkhook_list;
    623 
    624 void *
    625 forkhook_establish(fn)
    626 	void (*fn)(struct proc *, struct proc *);
    627 {
    628 	return hook_establish(&forkhook_list, (void (*)(void *))fn, NULL);
    629 }
    630 
    631 void
    632 forkhook_disestablish(vhook)
    633 	void *vhook;
    634 {
    635 	hook_disestablish(&forkhook_list, vhook);
    636 }
    637 
    638 /*
    639  * Run fork hooks.
    640  */
    641 void
    642 doforkhooks(p2, p1)
    643 	struct proc *p2, *p1;
    644 {
    645 	struct hook_desc *hd;
    646 
    647 	LIST_FOREACH(hd, &forkhook_list, hk_list) {
    648 		((void (*)(struct proc *, struct proc *))*hd->hk_fn)
    649 		    (p2, p1);
    650 	}
    651 }
    652 
    653 /*
    654  * "Power hook" types, functions, and variables.
    655  * The list of power hooks is kept ordered with the last registered hook
    656  * first.
    657  * When running the hooks on power down the hooks are called in reverse
    658  * registration order, when powering up in registration order.
    659  */
    660 struct powerhook_desc {
    661 	CIRCLEQ_ENTRY(powerhook_desc) sfd_list;
    662 	void	(*sfd_fn)(int, void *);
    663 	void	*sfd_arg;
    664 };
    665 
    666 CIRCLEQ_HEAD(, powerhook_desc) powerhook_list =
    667 	CIRCLEQ_HEAD_INITIALIZER(powerhook_list);
    668 
    669 void *
    670 powerhook_establish(fn, arg)
    671 	void (*fn)(int, void *);
    672 	void *arg;
    673 {
    674 	struct powerhook_desc *ndp;
    675 
    676 	ndp = (struct powerhook_desc *)
    677 	    malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
    678 	if (ndp == NULL)
    679 		return (NULL);
    680 
    681 	ndp->sfd_fn = fn;
    682 	ndp->sfd_arg = arg;
    683 	CIRCLEQ_INSERT_HEAD(&powerhook_list, ndp, sfd_list);
    684 
    685 	return (ndp);
    686 }
    687 
    688 void
    689 powerhook_disestablish(vhook)
    690 	void *vhook;
    691 {
    692 #ifdef DIAGNOSTIC
    693 	struct powerhook_desc *dp;
    694 
    695 	CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list)
    696                 if (dp == vhook)
    697 			goto found;
    698 	panic("powerhook_disestablish: hook %p not established", vhook);
    699  found:
    700 #endif
    701 
    702 	CIRCLEQ_REMOVE(&powerhook_list, (struct powerhook_desc *)vhook,
    703 	    sfd_list);
    704 	free(vhook, M_DEVBUF);
    705 }
    706 
    707 /*
    708  * Run power hooks.
    709  */
    710 void
    711 dopowerhooks(why)
    712 	int why;
    713 {
    714 	struct powerhook_desc *dp;
    715 
    716 	if (why == PWR_RESUME || why == PWR_SOFTRESUME) {
    717 		CIRCLEQ_FOREACH_REVERSE(dp, &powerhook_list, sfd_list) {
    718 			(*dp->sfd_fn)(why, dp->sfd_arg);
    719 		}
    720 	} else {
    721 		CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list) {
    722 			(*dp->sfd_fn)(why, dp->sfd_arg);
    723 		}
    724 	}
    725 }
    726 
    727 /*
    728  * Determine the root device and, if instructed to, the root file system.
    729  */
    730 
    731 #include "md.h"
    732 #if NMD == 0
    733 #undef MEMORY_DISK_HOOKS
    734 #endif
    735 
    736 #ifdef MEMORY_DISK_HOOKS
    737 static struct device fakemdrootdev[NMD];
    738 #endif
    739 
    740 #ifdef MEMORY_DISK_IS_ROOT
    741 #define BOOT_FROM_MEMORY_HOOKS 1
    742 #endif
    743 
    744 #include "raid.h"
    745 #if NRAID == 1
    746 #define BOOT_FROM_RAID_HOOKS 1
    747 #endif
    748 
    749 #ifdef BOOT_FROM_RAID_HOOKS
    750 extern int numraid;
    751 extern struct device *raidrootdev;
    752 #endif
    753 
    754 void
    755 setroot(bootdv, bootpartition)
    756 	struct device *bootdv;
    757 	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 || bootdv->dv_class != DV_IFNET)) {
    800 		TAILQ_FOREACH(ifp, &ifnet, if_list) {
    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 (bootdv->dv_class == DV_DISK)
    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 (rootdv->dv_class == DV_IFNET)
    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 			 */
    966 			rootdev = MAKEDISKDEV(majdev, bootdv->dv_unit,
    967 			    bootpartition);
    968 		}
    969 	} else {
    970 
    971 		/*
    972 		 * `root on <dev> ...'
    973 		 */
    974 
    975 		/*
    976 		 * If it's a network interface, we can bail out
    977 		 * early.
    978 		 */
    979 		dv = finddevice(rootspec);
    980 		if (dv != NULL && dv->dv_class == DV_IFNET) {
    981 			rootdv = dv;
    982 			goto haveroot;
    983 		}
    984 
    985 		rootdevname = devsw_blk2name(major(rootdev));
    986 		if (rootdevname == NULL) {
    987 			printf("unknown device major 0x%x\n", rootdev);
    988 			boothowto |= RB_ASKNAME;
    989 			goto top;
    990 		}
    991 		memset(buf, 0, sizeof(buf));
    992 		snprintf(buf, sizeof(buf), "%s%d", rootdevname,
    993 		    DISKUNIT(rootdev));
    994 
    995 		rootdv = finddevice(buf);
    996 		if (rootdv == NULL) {
    997 			printf("device %s (0x%x) not configured\n",
    998 			    buf, rootdev);
    999 			boothowto |= RB_ASKNAME;
   1000 			goto top;
   1001 		}
   1002 	}
   1003 
   1004  haveroot:
   1005 
   1006 	root_device = rootdv;
   1007 
   1008 	switch (rootdv->dv_class) {
   1009 	case DV_IFNET:
   1010 		aprint_normal("root on %s", rootdv->dv_xname);
   1011 		break;
   1012 
   1013 	case DV_DISK:
   1014 		aprint_normal("root on %s%c", rootdv->dv_xname,
   1015 		    DISKPART(rootdev) + 'a');
   1016 		break;
   1017 
   1018 	default:
   1019 		printf("can't determine root device\n");
   1020 		boothowto |= RB_ASKNAME;
   1021 		goto top;
   1022 	}
   1023 
   1024 	/*
   1025 	 * Now configure the dump device.
   1026 	 *
   1027 	 * If we haven't figured out the dump device, do so, with
   1028 	 * the following rules:
   1029 	 *
   1030 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
   1031 	 *
   1032 	 *	(b) If dumpspec is set, try to use it.  If the device
   1033 	 *	    is not available, punt.
   1034 	 *
   1035 	 *	(c) If dumpspec is not set, the dump device is
   1036 	 *	    wildcarded or unspecified.  If the root device
   1037 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
   1038 	 *	    of the root device.
   1039 	 */
   1040 
   1041 	if (boothowto & RB_ASKNAME) {		/* (a) */
   1042 		if (dumpdv == NULL)
   1043 			goto nodumpdev;
   1044 	} else if (dumpspec != NULL) {		/* (b) */
   1045 		if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
   1046 			/*
   1047 			 * Operator doesn't want a dump device.
   1048 			 * Or looks like they tried to pick a network
   1049 			 * device.  Oops.
   1050 			 */
   1051 			goto nodumpdev;
   1052 		}
   1053 
   1054 		dumpdevname = devsw_blk2name(major(dumpdev));
   1055 		if (dumpdevname == NULL)
   1056 			goto nodumpdev;
   1057 		memset(buf, 0, sizeof(buf));
   1058 		snprintf(buf, sizeof(buf), "%s%d", dumpdevname,
   1059 		    DISKUNIT(dumpdev));
   1060 
   1061 		dumpdv = finddevice(buf);
   1062 		if (dumpdv == NULL) {
   1063 			/*
   1064 			 * Device not configured.
   1065 			 */
   1066 			goto nodumpdev;
   1067 		}
   1068 	} else {				/* (c) */
   1069 		if (rootdv->dv_class == DV_IFNET)
   1070 			goto nodumpdev;
   1071 		else {
   1072 			dumpdv = rootdv;
   1073 			dumpdev = MAKEDISKDEV(major(rootdev),
   1074 			    dumpdv->dv_unit, 1);
   1075 		}
   1076 	}
   1077 
   1078 	aprint_normal(" dumps on %s%c\n", dumpdv->dv_xname,
   1079 	    DISKPART(dumpdev) + 'a');
   1080 	return;
   1081 
   1082  nodumpdev:
   1083 	dumpdev = NODEV;
   1084 	aprint_normal("\n");
   1085 }
   1086 
   1087 static struct device *
   1088 finddevice(name)
   1089 	const char *name;
   1090 {
   1091 	struct device *dv;
   1092 #if defined(BOOT_FROM_RAID_HOOKS) || defined(BOOT_FROM_MEMORY_HOOKS)
   1093 	int j;
   1094 #endif /* BOOT_FROM_RAID_HOOKS || BOOT_FROM_MEMORY_HOOKS */
   1095 
   1096 #ifdef BOOT_FROM_RAID_HOOKS
   1097 	for (j = 0; j < numraid; j++) {
   1098 		if (strcmp(name, raidrootdev[j].dv_xname) == 0) {
   1099 			dv = &raidrootdev[j];
   1100 			return (dv);
   1101 		}
   1102 	}
   1103 #endif /* BOOT_FROM_RAID_HOOKS */
   1104 
   1105 #ifdef BOOT_FROM_MEMORY_HOOKS
   1106 	for (j = 0; j < NMD; j++) {
   1107 		if (strcmp(name, fakemdrootdev[j].dv_xname) == 0) {
   1108 			dv = &fakemdrootdev[j];
   1109 			return (dv);
   1110 		}
   1111 	}
   1112 #endif /* BOOT_FROM_MEMORY_HOOKS */
   1113 
   1114 	for (dv = TAILQ_FIRST(&alldevs); dv != NULL;
   1115 	    dv = TAILQ_NEXT(dv, dv_list))
   1116 		if (strcmp(dv->dv_xname, name) == 0)
   1117 			break;
   1118 	return (dv);
   1119 }
   1120 
   1121 static struct device *
   1122 getdisk(str, len, defpart, devp, isdump)
   1123 	char *str;
   1124 	int len, defpart;
   1125 	dev_t *devp;
   1126 	int isdump;
   1127 {
   1128 	struct device	*dv;
   1129 #ifdef MEMORY_DISK_HOOKS
   1130 	int		i;
   1131 #endif
   1132 #ifdef BOOT_FROM_RAID_HOOKS
   1133 	int 		j;
   1134 #endif
   1135 
   1136 	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
   1137 		printf("use one of:");
   1138 #ifdef MEMORY_DISK_HOOKS
   1139 		if (isdump == 0)
   1140 			for (i = 0; i < NMD; i++)
   1141 				printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
   1142 				    'a' + MAXPARTITIONS - 1);
   1143 #endif
   1144 #ifdef BOOT_FROM_RAID_HOOKS
   1145 		if (isdump == 0)
   1146 			for (j = 0; j < numraid; j++)
   1147 				printf(" %s[a-%c]", raidrootdev[j].dv_xname,
   1148 				    'a' + MAXPARTITIONS - 1);
   1149 #endif
   1150 		TAILQ_FOREACH(dv, &alldevs, dv_list) {
   1151 			if (dv->dv_class == DV_DISK)
   1152 				printf(" %s[a-%c]", dv->dv_xname,
   1153 				    'a' + MAXPARTITIONS - 1);
   1154 			if (isdump == 0 && dv->dv_class == DV_IFNET)
   1155 				printf(" %s", dv->dv_xname);
   1156 		}
   1157 		if (isdump)
   1158 			printf(" none");
   1159 #if defined(DDB)
   1160 		printf(" ddb");
   1161 #endif
   1162 		printf(" halt reboot\n");
   1163 	}
   1164 	return (dv);
   1165 }
   1166 
   1167 static struct device *
   1168 parsedisk(str, len, defpart, devp)
   1169 	char *str;
   1170 	int len, defpart;
   1171 	dev_t *devp;
   1172 {
   1173 	struct device *dv;
   1174 	char *cp, c;
   1175 	int majdev, part;
   1176 #ifdef MEMORY_DISK_HOOKS
   1177 	int i;
   1178 #endif
   1179 	if (len == 0)
   1180 		return (NULL);
   1181 
   1182 	if (len == 4 && strcmp(str, "halt") == 0)
   1183 		cpu_reboot(RB_HALT, NULL);
   1184 	else if (len == 6 && strcmp(str, "reboot") == 0)
   1185 		cpu_reboot(0, NULL);
   1186 #if defined(DDB)
   1187 	else if (len == 3 && strcmp(str, "ddb") == 0)
   1188 		console_debugger();
   1189 #endif
   1190 
   1191 	cp = str + len - 1;
   1192 	c = *cp;
   1193 	if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
   1194 		part = c - 'a';
   1195 		*cp = '\0';
   1196 	} else
   1197 		part = defpart;
   1198 
   1199 #ifdef MEMORY_DISK_HOOKS
   1200 	for (i = 0; i < NMD; i++)
   1201 		if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) {
   1202 			dv = &fakemdrootdev[i];
   1203 			goto gotdisk;
   1204 		}
   1205 #endif
   1206 
   1207 	dv = finddevice(str);
   1208 	if (dv != NULL) {
   1209 		if (dv->dv_class == DV_DISK) {
   1210 #ifdef MEMORY_DISK_HOOKS
   1211  gotdisk:
   1212 #endif
   1213 			majdev = devsw_name2blk(dv->dv_xname, NULL, 0);
   1214 			if (majdev < 0)
   1215 				panic("parsedisk");
   1216 			*devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
   1217 		}
   1218 
   1219 		if (dv->dv_class == DV_IFNET)
   1220 			*devp = NODEV;
   1221 	}
   1222 
   1223 	*cp = c;
   1224 	return (dv);
   1225 }
   1226 
   1227 /*
   1228  * snprintf() `bytes' into `buf', reformatting it so that the number,
   1229  * plus a possible `x' + suffix extension) fits into len bytes (including
   1230  * the terminating NUL).
   1231  * Returns the number of bytes stored in buf, or -1 if there was a problem.
   1232  * E.g, given a len of 9 and a suffix of `B':
   1233  *	bytes		result
   1234  *	-----		------
   1235  *	99999		`99999 B'
   1236  *	100000		`97 kB'
   1237  *	66715648	`65152 kB'
   1238  *	252215296	`240 MB'
   1239  */
   1240 int
   1241 humanize_number(buf, len, bytes, suffix, divisor)
   1242 	char		*buf;
   1243 	size_t		 len;
   1244 	u_int64_t	 bytes;
   1245 	const char	*suffix;
   1246 	int 		divisor;
   1247 {
   1248        	/* prefixes are: (none), kilo, Mega, Giga, Tera, Peta, Exa */
   1249 	const char *prefixes;
   1250 	int		r;
   1251 	u_int64_t	max;
   1252 	size_t		i, suffixlen;
   1253 
   1254 	if (buf == NULL || suffix == NULL)
   1255 		return (-1);
   1256 	if (len > 0)
   1257 		buf[0] = '\0';
   1258 	suffixlen = strlen(suffix);
   1259 	/* check if enough room for `x y' + suffix + `\0' */
   1260 	if (len < 4 + suffixlen)
   1261 		return (-1);
   1262 
   1263 	if (divisor == 1024) {
   1264 		/*
   1265 		 * binary multiplies
   1266 		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
   1267 		 */
   1268 		prefixes = " KMGTPE";
   1269 	} else
   1270 		prefixes = " kMGTPE"; /* SI for decimal multiplies */
   1271 
   1272 	max = 1;
   1273 	for (i = 0; i < len - suffixlen - 3; i++)
   1274 		max *= 10;
   1275 	for (i = 0; bytes >= max && prefixes[i + 1]; i++)
   1276 		bytes /= divisor;
   1277 
   1278 	r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
   1279 	    i == 0 ? "" : " ", prefixes[i], suffix);
   1280 
   1281 	return (r);
   1282 }
   1283 
   1284 int
   1285 format_bytes(buf, len, bytes)
   1286 	char		*buf;
   1287 	size_t		 len;
   1288 	u_int64_t	 bytes;
   1289 {
   1290 	int	rv;
   1291 	size_t	nlen;
   1292 
   1293 	rv = humanize_number(buf, len, bytes, "B", 1024);
   1294 	if (rv != -1) {
   1295 			/* nuke the trailing ` B' if it exists */
   1296 		nlen = strlen(buf) - 2;
   1297 		if (strcmp(&buf[nlen], " B") == 0)
   1298 			buf[nlen] = '\0';
   1299 	}
   1300 	return (rv);
   1301 }
   1302 
   1303 /*
   1304  * Start trace of particular system call. If process is being traced,
   1305  * this routine is called by MD syscall dispatch code just before
   1306  * a system call is actually executed.
   1307  * MD caller guarantees the passed 'code' is within the supported
   1308  * system call number range for emulation the process runs under.
   1309  */
   1310 int
   1311 trace_enter(struct lwp *l, register_t code,
   1312 	register_t realcode, const struct sysent *callp, void *args)
   1313 {
   1314 #if defined(KTRACE) || defined(SYSTRACE)
   1315 	struct proc *p = l->l_proc;
   1316 #endif
   1317 
   1318 #ifdef SYSCALL_DEBUG
   1319 	scdebug_call(l, code, args);
   1320 #endif /* SYSCALL_DEBUG */
   1321 
   1322 #ifdef KTRACE
   1323 	if (KTRPOINT(p, KTR_SYSCALL))
   1324 		ktrsyscall(p, code, realcode, callp, args);
   1325 #endif /* KTRACE */
   1326 
   1327 #ifdef SYSTRACE
   1328 	if (ISSET(p->p_flag, P_SYSTRACE))
   1329 		return systrace_enter(p, code, args);
   1330 #endif
   1331 	return 0;
   1332 }
   1333 
   1334 /*
   1335  * End trace of particular system call. If process is being traced,
   1336  * this routine is called by MD syscall dispatch code just after
   1337  * a system call finishes.
   1338  * MD caller guarantees the passed 'code' is within the supported
   1339  * system call number range for emulation the process runs under.
   1340  */
   1341 void
   1342 trace_exit(struct lwp *l, register_t code, void *args, register_t rval[],
   1343     int error)
   1344 {
   1345 #if defined(KTRACE) || defined(SYSTRACE)
   1346 	struct proc *p = l->l_proc;
   1347 #endif
   1348 
   1349 #ifdef SYSCALL_DEBUG
   1350 	scdebug_ret(l, code, error, rval);
   1351 #endif /* SYSCALL_DEBUG */
   1352 
   1353 #ifdef KTRACE
   1354 	if (KTRPOINT(p, KTR_SYSRET)) {
   1355 		KERNEL_PROC_LOCK(l);
   1356 		ktrsysret(p, code, error, rval);
   1357 		KERNEL_PROC_UNLOCK(l);
   1358 	}
   1359 #endif /* KTRACE */
   1360 
   1361 #ifdef SYSTRACE
   1362 	if (ISSET(p->p_flag, P_SYSTRACE))
   1363 		systrace_exit(p, code, args, rval, error);
   1364 #endif
   1365 }
   1366