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