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