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