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