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