Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.84
      1 /*	$NetBSD: kern_subr.c,v 1.84 2002/08/25 20:01:13 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and by Luke Mewburn.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  * (c) UNIX System Laboratories, Inc.
     44  * All or some portions of this file are derived from material licensed
     45  * to the University of California by American Telephone and Telegraph
     46  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     47  * the permission of UNIX System Laboratories, Inc.
     48  *
     49  * Copyright (c) 1992, 1993
     50  *	The Regents of the University of California.  All rights reserved.
     51  *
     52  * This software was developed by the Computer Systems Engineering group
     53  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     54  * contributed to Berkeley.
     55  *
     56  * All advertising materials mentioning features or use of this software
     57  * must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Lawrence Berkeley Laboratory.
     60  *
     61  * Redistribution and use in source and binary forms, with or without
     62  * modification, are permitted provided that the following conditions
     63  * are met:
     64  * 1. Redistributions of source code must retain the above copyright
     65  *    notice, this list of conditions and the following disclaimer.
     66  * 2. Redistributions in binary form must reproduce the above copyright
     67  *    notice, this list of conditions and the following disclaimer in the
     68  *    documentation and/or other materials provided with the distribution.
     69  * 3. All advertising materials mentioning features or use of this software
     70  *    must display the following acknowledgement:
     71  *	This product includes software developed by the University of
     72  *	California, Berkeley and its contributors.
     73  * 4. Neither the name of the University nor the names of its contributors
     74  *    may be used to endorse or promote products derived from this software
     75  *    without specific prior written permission.
     76  *
     77  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     78  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     79  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     80  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     81  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     82  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     83  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     84  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     85  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     86  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     87  * SUCH DAMAGE.
     88  *
     89  *	@(#)kern_subr.c	8.4 (Berkeley) 2/14/95
     90  */
     91 
     92 #include <sys/cdefs.h>
     93 __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.84 2002/08/25 20:01:13 thorpej Exp $");
     94 
     95 #include "opt_ddb.h"
     96 #include "opt_md.h"
     97 #include "opt_syscall_debug.h"
     98 #include "opt_ktrace.h"
     99 #include "opt_systrace.h"
    100 
    101 #include <sys/param.h>
    102 #include <sys/systm.h>
    103 #include <sys/proc.h>
    104 #include <sys/malloc.h>
    105 #include <sys/mount.h>
    106 #include <sys/device.h>
    107 #include <sys/reboot.h>
    108 #include <sys/conf.h>
    109 #include <sys/disklabel.h>
    110 #include <sys/queue.h>
    111 #include <sys/systrace.h>
    112 #include <sys/ktrace.h>
    113 
    114 #include <uvm/uvm_extern.h>
    115 
    116 #include <dev/cons.h>
    117 
    118 #include <net/if.h>
    119 
    120 /* XXX these should eventually move to subr_autoconf.c */
    121 static int findblkmajor __P((const char *));
    122 const char *findblkname __P((int));
    123 static struct device *finddevice __P((const char *));
    124 static struct device *getdisk __P((char *, int, int, dev_t *, int));
    125 static struct device *parsedisk __P((char *, int, int, dev_t *));
    126 
    127 /*
    128  * A generic linear hook.
    129  */
    130 struct hook_desc {
    131 	LIST_ENTRY(hook_desc) hk_list;
    132 	void	(*hk_fn) __P((void *));
    133 	void	*hk_arg;
    134 };
    135 typedef LIST_HEAD(, hook_desc) hook_list_t;
    136 
    137 static void *hook_establish __P((hook_list_t *, void (*)(void *), void *));
    138 static void hook_disestablish __P((hook_list_t *, void *));
    139 static void hook_destroy __P((hook_list_t *));
    140 static void hook_proc_run __P((hook_list_t *, struct proc *));
    141 
    142 int
    143 uiomove(buf, n, uio)
    144 	void *buf;
    145 	int n;
    146 	struct uio *uio;
    147 {
    148 	struct iovec *iov;
    149 	u_int cnt;
    150 	int error = 0;
    151 	char *cp = buf;
    152 	struct proc *p = uio->uio_procp;
    153 
    154 #ifdef DIAGNOSTIC
    155 	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
    156 		panic("uiomove: mode");
    157 #endif
    158 	while (n > 0 && uio->uio_resid) {
    159 		iov = uio->uio_iov;
    160 		cnt = iov->iov_len;
    161 		if (cnt == 0) {
    162 			uio->uio_iov++;
    163 			uio->uio_iovcnt--;
    164 			continue;
    165 		}
    166 		if (cnt > n)
    167 			cnt = n;
    168 		switch (uio->uio_segflg) {
    169 
    170 		case UIO_USERSPACE:
    171 			if (curproc->p_cpu->ci_schedstate.spc_flags &
    172 			    SPCF_SHOULDYIELD)
    173 				preempt(NULL);
    174 			if (__predict_true(p == curproc)) {
    175 				if (uio->uio_rw == UIO_READ)
    176 					error = copyout(cp, iov->iov_base, cnt);
    177 				else
    178 					error = copyin(iov->iov_base, cp, cnt);
    179 			} else {
    180 				if (uio->uio_rw == UIO_READ)
    181 					error = copyout_proc(p, cp,
    182 					    iov->iov_base, cnt);
    183 				else
    184 					error = copyin_proc(p, iov->iov_base,
    185 					    cp, cnt);
    186 			}
    187 			if (error)
    188 				return (error);
    189 			break;
    190 
    191 		case UIO_SYSSPACE:
    192 			if (uio->uio_rw == UIO_READ)
    193 				error = kcopy(cp, iov->iov_base, cnt);
    194 			else
    195 				error = kcopy(iov->iov_base, cp, cnt);
    196 			if (error)
    197 				return (error);
    198 			break;
    199 		}
    200 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    201 		iov->iov_len -= cnt;
    202 		uio->uio_resid -= cnt;
    203 		uio->uio_offset += cnt;
    204 		cp += cnt;
    205 		n -= cnt;
    206 	}
    207 	return (error);
    208 }
    209 
    210 /*
    211  * Give next character to user as result of read.
    212  */
    213 int
    214 ureadc(c, uio)
    215 	int c;
    216 	struct uio *uio;
    217 {
    218 	struct iovec *iov;
    219 
    220 	if (uio->uio_resid <= 0)
    221 		panic("ureadc: non-positive resid");
    222 again:
    223 	if (uio->uio_iovcnt <= 0)
    224 		panic("ureadc: non-positive iovcnt");
    225 	iov = uio->uio_iov;
    226 	if (iov->iov_len <= 0) {
    227 		uio->uio_iovcnt--;
    228 		uio->uio_iov++;
    229 		goto again;
    230 	}
    231 	switch (uio->uio_segflg) {
    232 
    233 	case UIO_USERSPACE:
    234 		if (subyte(iov->iov_base, c) < 0)
    235 			return (EFAULT);
    236 		break;
    237 
    238 	case UIO_SYSSPACE:
    239 		*(char *)iov->iov_base = c;
    240 		break;
    241 	}
    242 	iov->iov_base = (caddr_t)iov->iov_base + 1;
    243 	iov->iov_len--;
    244 	uio->uio_resid--;
    245 	uio->uio_offset++;
    246 	return (0);
    247 }
    248 
    249 /*
    250  * Like copyin(), but operates on an arbitrary process.
    251  */
    252 int
    253 copyin_proc(struct proc *p, const void *uaddr, void *kaddr, size_t len)
    254 {
    255 	struct iovec iov;
    256 	struct uio uio;
    257 	int error;
    258 
    259 	if (len == 0)
    260 		return (0);
    261 
    262 	iov.iov_base = kaddr;
    263 	iov.iov_len = len;
    264 	uio.uio_iov = &iov;
    265 	uio.uio_iovcnt = 1;
    266 	uio.uio_offset = (off_t)(intptr_t)uaddr;
    267 	uio.uio_resid = len;
    268 	uio.uio_segflg = UIO_SYSSPACE;
    269 	uio.uio_rw = UIO_READ;
    270 	uio.uio_procp = NULL;
    271 
    272 	/* XXXCDC: how should locking work here? */
    273 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
    274 		return (EFAULT);
    275 	p->p_vmspace->vm_refcnt++;	/* XXX */
    276 	error = uvm_io(&p->p_vmspace->vm_map, &uio);
    277 	uvmspace_free(p->p_vmspace);
    278 
    279 	return (error);
    280 }
    281 
    282 /*
    283  * Like copyout(), but operates on an arbitrary process.
    284  */
    285 int
    286 copyout_proc(struct proc *p, const void *kaddr, void *uaddr, size_t len)
    287 {
    288 	struct iovec iov;
    289 	struct uio uio;
    290 	int error;
    291 
    292 	if (len == 0)
    293 		return (0);
    294 
    295 	iov.iov_base = (void *) kaddr;	/* XXX cast away const */
    296 	iov.iov_len = len;
    297 	uio.uio_iov = &iov;
    298 	uio.uio_iovcnt = 1;
    299 	uio.uio_offset = (off_t)(intptr_t)uaddr;
    300 	uio.uio_resid = len;
    301 	uio.uio_segflg = UIO_SYSSPACE;
    302 	uio.uio_rw = UIO_WRITE;
    303 	uio.uio_procp = NULL;
    304 
    305 	/* XXXCDC: how should locking work here? */
    306 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
    307 		return (EFAULT);
    308 	p->p_vmspace->vm_refcnt++;	/* XXX */
    309 	error = uvm_io(&p->p_vmspace->vm_map, &uio);
    310 	uvmspace_free(p->p_vmspace);
    311 
    312 	return (error);
    313 }
    314 
    315 /*
    316  * General routine to allocate a hash table.
    317  * Allocate enough memory to hold at least `elements' list-head pointers.
    318  * Return a pointer to the allocated space and set *hashmask to a pattern
    319  * suitable for masking a value to use as an index into the returned array.
    320  */
    321 void *
    322 hashinit(elements, htype, mtype, mflags, hashmask)
    323 	u_int elements;
    324 	enum hashtype htype;
    325 	int mtype, mflags;
    326 	u_long *hashmask;
    327 {
    328 	u_long hashsize, i;
    329 	LIST_HEAD(, generic) *hashtbl_list;
    330 	TAILQ_HEAD(, generic) *hashtbl_tailq;
    331 	size_t esize;
    332 	void *p;
    333 
    334 	if (elements == 0)
    335 		panic("hashinit: bad cnt");
    336 	for (hashsize = 1; hashsize < elements; hashsize <<= 1)
    337 		continue;
    338 
    339 	switch (htype) {
    340 	case HASH_LIST:
    341 		esize = sizeof(*hashtbl_list);
    342 		break;
    343 	case HASH_TAILQ:
    344 		esize = sizeof(*hashtbl_tailq);
    345 		break;
    346 #ifdef DIAGNOSTIC
    347 	default:
    348 		panic("hashinit: invalid table type");
    349 #endif
    350 	}
    351 
    352 	if ((p = malloc(hashsize * esize, mtype, mflags)) == NULL)
    353 		return (NULL);
    354 
    355 	switch (htype) {
    356 	case HASH_LIST:
    357 		hashtbl_list = p;
    358 		for (i = 0; i < hashsize; i++)
    359 			LIST_INIT(&hashtbl_list[i]);
    360 		break;
    361 	case HASH_TAILQ:
    362 		hashtbl_tailq = p;
    363 		for (i = 0; i < hashsize; i++)
    364 			TAILQ_INIT(&hashtbl_tailq[i]);
    365 		break;
    366 	}
    367 	*hashmask = hashsize - 1;
    368 	return (p);
    369 }
    370 
    371 /*
    372  * Free memory from hash table previosly allocated via hashinit().
    373  */
    374 void
    375 hashdone(hashtbl, mtype)
    376 	void *hashtbl;
    377 	int mtype;
    378 {
    379 
    380 	free(hashtbl, mtype);
    381 }
    382 
    383 
    384 static void *
    385 hook_establish(list, fn, arg)
    386 	hook_list_t *list;
    387 	void (*fn) __P((void *));
    388 	void *arg;
    389 {
    390 	struct hook_desc *hd;
    391 
    392 	hd = malloc(sizeof(*hd), M_DEVBUF, M_NOWAIT);
    393 	if (hd == NULL)
    394 		return (NULL);
    395 
    396 	hd->hk_fn = fn;
    397 	hd->hk_arg = arg;
    398 	LIST_INSERT_HEAD(list, hd, hk_list);
    399 
    400 	return (hd);
    401 }
    402 
    403 static void
    404 hook_disestablish(list, vhook)
    405 	hook_list_t *list;
    406 	void *vhook;
    407 {
    408 #ifdef DIAGNOSTIC
    409 	struct hook_desc *hd;
    410 
    411 	LIST_FOREACH(hd, list, hk_list) {
    412                 if (hd == vhook)
    413 			break;
    414 	}
    415 
    416 	if (hd == NULL)
    417 		panic("hook_disestablish: hook %p not established", vhook);
    418 #endif
    419 	LIST_REMOVE((struct hook_desc *)vhook, hk_list);
    420 	free(vhook, M_DEVBUF);
    421 }
    422 
    423 static void
    424 hook_destroy(list)
    425 	hook_list_t *list;
    426 {
    427 	struct hook_desc *hd;
    428 
    429 	while ((hd = list->lh_first) != NULL) {
    430 		LIST_REMOVE(hd, hk_list);
    431 		free(hd, M_DEVBUF);
    432 	}
    433 }
    434 
    435 static void
    436 hook_proc_run(list, p)
    437 	hook_list_t *list;
    438 	struct proc *p;
    439 {
    440 	struct hook_desc *hd;
    441 
    442 	for (hd = LIST_FIRST(list); hd != NULL; hd = LIST_NEXT(hd, hk_list)) {
    443 		((void (*) __P((struct proc *, void *)))*hd->hk_fn)(p,
    444 		    hd->hk_arg);
    445 	}
    446 }
    447 
    448 /*
    449  * "Shutdown hook" types, functions, and variables.
    450  *
    451  * Should be invoked immediately before the
    452  * system is halted or rebooted, i.e. after file systems unmounted,
    453  * after crash dump done, etc.
    454  *
    455  * Each shutdown hook is removed from the list before it's run, so that
    456  * it won't be run again.
    457  */
    458 
    459 hook_list_t shutdownhook_list;
    460 
    461 void *
    462 shutdownhook_establish(fn, arg)
    463 	void (*fn) __P((void *));
    464 	void *arg;
    465 {
    466 	return hook_establish(&shutdownhook_list, fn, arg);
    467 }
    468 
    469 void
    470 shutdownhook_disestablish(vhook)
    471 	void *vhook;
    472 {
    473 	return hook_disestablish(&shutdownhook_list, vhook);
    474 }
    475 
    476 /*
    477  * Run shutdown hooks.  Should be invoked immediately before the
    478  * system is halted or rebooted, i.e. after file systems unmounted,
    479  * after crash dump done, etc.
    480  *
    481  * Each shutdown hook is removed from the list before it's run, so that
    482  * it won't be run again.
    483  */
    484 void
    485 doshutdownhooks()
    486 {
    487 	struct hook_desc *dp;
    488 
    489 	while ((dp = shutdownhook_list.lh_first) != NULL) {
    490 		LIST_REMOVE(dp, hk_list);
    491 		(*dp->hk_fn)(dp->hk_arg);
    492 #if 0
    493 		/*
    494 		 * Don't bother freeing the hook structure,, since we may
    495 		 * be rebooting because of a memory corruption problem,
    496 		 * and this might only make things worse.  It doesn't
    497 		 * matter, anyway, since the system is just about to
    498 		 * reboot.
    499 		 */
    500 		free(dp, M_DEVBUF);
    501 #endif
    502 	}
    503 }
    504 
    505 /*
    506  * "Mountroot hook" types, functions, and variables.
    507  */
    508 
    509 hook_list_t mountroothook_list;
    510 
    511 void *
    512 mountroothook_establish(fn, dev)
    513 	void (*fn) __P((struct device *));
    514 	struct device *dev;
    515 {
    516 	return hook_establish(&mountroothook_list, (void (*)__P((void *)))fn,
    517 	    dev);
    518 }
    519 
    520 void
    521 mountroothook_disestablish(vhook)
    522 	void *vhook;
    523 {
    524 	return hook_disestablish(&mountroothook_list, vhook);
    525 }
    526 
    527 void
    528 mountroothook_destroy()
    529 {
    530 	hook_destroy(&mountroothook_list);
    531 }
    532 
    533 void
    534 domountroothook()
    535 {
    536 	struct hook_desc *hd;
    537 
    538 	LIST_FOREACH(hd, &mountroothook_list, hk_list) {
    539 		if (hd->hk_arg == (void *)root_device) {
    540 			(*hd->hk_fn)(hd->hk_arg);
    541 			return;
    542 		}
    543 	}
    544 }
    545 
    546 hook_list_t exechook_list;
    547 
    548 void *
    549 exechook_establish(fn, arg)
    550 	void (*fn) __P((struct proc *, void *));
    551 	void *arg;
    552 {
    553 	return hook_establish(&exechook_list, (void (*) __P((void *)))fn, arg);
    554 }
    555 
    556 void
    557 exechook_disestablish(vhook)
    558 	void *vhook;
    559 {
    560 	hook_disestablish(&exechook_list, vhook);
    561 }
    562 
    563 /*
    564  * Run exec hooks.
    565  */
    566 void
    567 doexechooks(p)
    568 	struct proc *p;
    569 {
    570 	hook_proc_run(&exechook_list, p);
    571 }
    572 
    573 hook_list_t exithook_list;
    574 
    575 void *
    576 exithook_establish(fn, arg)
    577 	void (*fn) __P((struct proc *, void *));
    578 	void *arg;
    579 {
    580 	return hook_establish(&exithook_list, (void (*) __P((void *)))fn, arg);
    581 }
    582 
    583 void
    584 exithook_disestablish(vhook)
    585 	void *vhook;
    586 {
    587 	hook_disestablish(&exithook_list, vhook);
    588 }
    589 
    590 /*
    591  * Run exit hooks.
    592  */
    593 void
    594 doexithooks(p)
    595 	struct proc *p;
    596 {
    597 	hook_proc_run(&exithook_list, p);
    598 }
    599 
    600 /*
    601  * "Power hook" types, functions, and variables.
    602  * The list of power hooks is kept ordered with the last registered hook
    603  * first.
    604  * When running the hooks on power down the hooks are called in reverse
    605  * registration order, when powering up in registration order.
    606  */
    607 struct powerhook_desc {
    608 	CIRCLEQ_ENTRY(powerhook_desc) sfd_list;
    609 	void	(*sfd_fn) __P((int, void *));
    610 	void	*sfd_arg;
    611 };
    612 
    613 CIRCLEQ_HEAD(, powerhook_desc) powerhook_list =
    614 	CIRCLEQ_HEAD_INITIALIZER(powerhook_list);
    615 
    616 void *
    617 powerhook_establish(fn, arg)
    618 	void (*fn) __P((int, void *));
    619 	void *arg;
    620 {
    621 	struct powerhook_desc *ndp;
    622 
    623 	ndp = (struct powerhook_desc *)
    624 	    malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
    625 	if (ndp == NULL)
    626 		return (NULL);
    627 
    628 	ndp->sfd_fn = fn;
    629 	ndp->sfd_arg = arg;
    630 	CIRCLEQ_INSERT_HEAD(&powerhook_list, ndp, sfd_list);
    631 
    632 	return (ndp);
    633 }
    634 
    635 void
    636 powerhook_disestablish(vhook)
    637 	void *vhook;
    638 {
    639 #ifdef DIAGNOSTIC
    640 	struct powerhook_desc *dp;
    641 
    642 	CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list)
    643                 if (dp == vhook)
    644 			goto found;
    645 	panic("powerhook_disestablish: hook %p not established", vhook);
    646  found:
    647 #endif
    648 
    649 	CIRCLEQ_REMOVE(&powerhook_list, (struct powerhook_desc *)vhook,
    650 	    sfd_list);
    651 	free(vhook, M_DEVBUF);
    652 }
    653 
    654 /*
    655  * Run power hooks.
    656  */
    657 void
    658 dopowerhooks(why)
    659 	int why;
    660 {
    661 	struct powerhook_desc *dp;
    662 
    663 	if (why == PWR_RESUME || why == PWR_SOFTRESUME) {
    664 		CIRCLEQ_FOREACH_REVERSE(dp, &powerhook_list, sfd_list) {
    665 			(*dp->sfd_fn)(why, dp->sfd_arg);
    666 		}
    667 	} else {
    668 		CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list) {
    669 			(*dp->sfd_fn)(why, dp->sfd_arg);
    670 		}
    671 	}
    672 }
    673 
    674 /*
    675  * Determine the root device and, if instructed to, the root file system.
    676  */
    677 
    678 #include "md.h"
    679 #if NMD == 0
    680 #undef MEMORY_DISK_HOOKS
    681 #endif
    682 
    683 #ifdef MEMORY_DISK_HOOKS
    684 static struct device fakemdrootdev[NMD];
    685 #endif
    686 
    687 #include "raid.h"
    688 #if NRAID == 1
    689 #define BOOT_FROM_RAID_HOOKS 1
    690 #endif
    691 
    692 #ifdef BOOT_FROM_RAID_HOOKS
    693 extern int numraid;
    694 extern struct device *raidrootdev;
    695 #endif
    696 
    697 void
    698 setroot(bootdv, bootpartition)
    699 	struct device *bootdv;
    700 	int bootpartition;
    701 {
    702 	struct device *dv;
    703 	int len;
    704 #ifdef MEMORY_DISK_HOOKS
    705 	int i;
    706 #endif
    707 	dev_t nrootdev;
    708 	dev_t ndumpdev = NODEV;
    709 	char buf[128];
    710 	const char *rootdevname;
    711 	const char *dumpdevname;
    712 	struct device *rootdv = NULL;		/* XXX gcc -Wuninitialized */
    713 	struct device *dumpdv = NULL;
    714 	struct ifnet *ifp;
    715 	const char *deffsname;
    716 	struct vfsops *vops;
    717 
    718 #ifdef MEMORY_DISK_HOOKS
    719 	for (i = 0; i < NMD; i++) {
    720 		fakemdrootdev[i].dv_class  = DV_DISK;
    721 		fakemdrootdev[i].dv_cfdata = NULL;
    722 		fakemdrootdev[i].dv_unit   = i;
    723 		fakemdrootdev[i].dv_parent = NULL;
    724 		sprintf(fakemdrootdev[i].dv_xname, "md%d", i);
    725 	}
    726 #endif /* MEMORY_DISK_HOOKS */
    727 
    728 #ifdef MEMORY_DISK_IS_ROOT
    729 	bootdv = &fakemdrootdev[0];
    730 	bootpartition = 0;
    731 #endif
    732 
    733 	/*
    734 	 * If NFS is specified as the file system, and we found
    735 	 * a DV_DISK boot device (or no boot device at all), then
    736 	 * find a reasonable network interface for "rootspec".
    737 	 */
    738 	vops = vfs_getopsbyname("nfs");
    739 	if (vops != NULL && vops->vfs_mountroot == mountroot &&
    740 	    rootspec == NULL &&
    741 	    (bootdv == NULL || bootdv->dv_class != DV_IFNET)) {
    742 		TAILQ_FOREACH(ifp, &ifnet, if_list) {
    743 			if ((ifp->if_flags &
    744 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    745 				break;
    746 		}
    747 		if (ifp == NULL) {
    748 			/*
    749 			 * Can't find a suitable interface; ask the
    750 			 * user.
    751 			 */
    752 			boothowto |= RB_ASKNAME;
    753 		} else {
    754 			/*
    755 			 * Have a suitable interface; behave as if
    756 			 * the user specified this interface.
    757 			 */
    758 			rootspec = (const char *)ifp->if_xname;
    759 		}
    760 	}
    761 
    762 	/*
    763 	 * If wildcarded root and we the boot device wasn't determined,
    764 	 * ask the user.
    765 	 */
    766 	if (rootspec == NULL && bootdv == NULL)
    767 		boothowto |= RB_ASKNAME;
    768 
    769  top:
    770 	if (boothowto & RB_ASKNAME) {
    771 		struct device *defdumpdv;
    772 
    773 		for (;;) {
    774 			printf("root device");
    775 			if (bootdv != NULL) {
    776 				printf(" (default %s", bootdv->dv_xname);
    777 				if (bootdv->dv_class == DV_DISK)
    778 					printf("%c", bootpartition + 'a');
    779 				printf(")");
    780 			}
    781 			printf(": ");
    782 			len = cngetsn(buf, sizeof(buf));
    783 			if (len == 0 && bootdv != NULL) {
    784 				strcpy(buf, bootdv->dv_xname);
    785 				len = strlen(buf);
    786 			}
    787 			if (len > 0 && buf[len - 1] == '*') {
    788 				buf[--len] = '\0';
    789 				dv = getdisk(buf, len, 1, &nrootdev, 0);
    790 				if (dv != NULL) {
    791 					rootdv = dv;
    792 					break;
    793 				}
    794 			}
    795 			dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
    796 			if (dv != NULL) {
    797 				rootdv = dv;
    798 				break;
    799 			}
    800 		}
    801 
    802 		/*
    803 		 * Set up the default dump device.  If root is on
    804 		 * a network device, there is no default dump
    805 		 * device, since we don't support dumps to the
    806 		 * network.
    807 		 */
    808 		if (rootdv->dv_class == DV_IFNET)
    809 			defdumpdv = NULL;
    810 		else
    811 			defdumpdv = rootdv;
    812 
    813 		for (;;) {
    814 			printf("dump device");
    815 			if (defdumpdv != NULL) {
    816 				/*
    817 				 * Note, we know it's a disk if we get here.
    818 				 */
    819 				printf(" (default %sb)", defdumpdv->dv_xname);
    820 			}
    821 			printf(": ");
    822 			len = cngetsn(buf, sizeof(buf));
    823 			if (len == 0) {
    824 				if (defdumpdv != NULL) {
    825 					ndumpdev = MAKEDISKDEV(major(nrootdev),
    826 					    DISKUNIT(nrootdev), 1);
    827 				}
    828 				dumpdv = defdumpdv;
    829 				break;
    830 			}
    831 			if (len == 4 && strcmp(buf, "none") == 0) {
    832 				dumpdv = NULL;
    833 				break;
    834 			}
    835 			dv = getdisk(buf, len, 1, &ndumpdev, 1);
    836 			if (dv != NULL) {
    837 				dumpdv = dv;
    838 				break;
    839 			}
    840 		}
    841 
    842 		rootdev = nrootdev;
    843 		dumpdev = ndumpdev;
    844 
    845 		for (vops = LIST_FIRST(&vfs_list); vops != NULL;
    846 		     vops = LIST_NEXT(vops, vfs_list)) {
    847 			if (vops->vfs_mountroot != NULL &&
    848 			    vops->vfs_mountroot == mountroot)
    849 			break;
    850 		}
    851 
    852 		if (vops == NULL) {
    853 			mountroot = NULL;
    854 			deffsname = "generic";
    855 		} else
    856 			deffsname = vops->vfs_name;
    857 
    858 		for (;;) {
    859 			printf("file system (default %s): ", deffsname);
    860 			len = cngetsn(buf, sizeof(buf));
    861 			if (len == 0)
    862 				break;
    863 			if (len == 4 && strcmp(buf, "halt") == 0)
    864 				cpu_reboot(RB_HALT, NULL);
    865 			else if (len == 6 && strcmp(buf, "reboot") == 0)
    866 				cpu_reboot(0, NULL);
    867 #if defined(DDB)
    868 			else if (len == 3 && strcmp(buf, "ddb") == 0) {
    869 				console_debugger();
    870 			}
    871 #endif
    872 			else if (len == 7 && strcmp(buf, "generic") == 0) {
    873 				mountroot = NULL;
    874 				break;
    875 			}
    876 			vops = vfs_getopsbyname(buf);
    877 			if (vops == NULL || vops->vfs_mountroot == NULL) {
    878 				printf("use one of: generic");
    879 				for (vops = LIST_FIRST(&vfs_list);
    880 				     vops != NULL;
    881 				     vops = LIST_NEXT(vops, vfs_list)) {
    882 					if (vops->vfs_mountroot != NULL)
    883 						printf(" %s", vops->vfs_name);
    884 				}
    885 #if defined(DDB)
    886 				printf(" ddb");
    887 #endif
    888 				printf(" halt reboot\n");
    889 			} else {
    890 				mountroot = vops->vfs_mountroot;
    891 				break;
    892 			}
    893 		}
    894 
    895 	} else if (rootspec == NULL) {
    896 		int majdev;
    897 
    898 		/*
    899 		 * Wildcarded root; use the boot device.
    900 		 */
    901 		rootdv = bootdv;
    902 
    903 		majdev = findblkmajor(bootdv->dv_xname);
    904 		if (majdev >= 0) {
    905 			/*
    906 			 * Root is on a disk.  `bootpartition' is root.
    907 			 */
    908 			rootdev = MAKEDISKDEV(majdev, bootdv->dv_unit,
    909 			    bootpartition);
    910 		}
    911 	} else {
    912 
    913 		/*
    914 		 * `root on <dev> ...'
    915 		 */
    916 
    917 		/*
    918 		 * If it's a network interface, we can bail out
    919 		 * early.
    920 		 */
    921 		dv = finddevice(rootspec);
    922 		if (dv != NULL && dv->dv_class == DV_IFNET) {
    923 			rootdv = dv;
    924 			goto haveroot;
    925 		}
    926 
    927 		rootdevname = findblkname(major(rootdev));
    928 		if (rootdevname == NULL) {
    929 			printf("unknown device major 0x%x\n", rootdev);
    930 			boothowto |= RB_ASKNAME;
    931 			goto top;
    932 		}
    933 		memset(buf, 0, sizeof(buf));
    934 		sprintf(buf, "%s%d", rootdevname, DISKUNIT(rootdev));
    935 
    936 		rootdv = finddevice(buf);
    937 		if (rootdv == NULL) {
    938 			printf("device %s (0x%x) not configured\n",
    939 			    buf, rootdev);
    940 			boothowto |= RB_ASKNAME;
    941 			goto top;
    942 		}
    943 	}
    944 
    945  haveroot:
    946 
    947 	root_device = rootdv;
    948 
    949 	switch (rootdv->dv_class) {
    950 	case DV_IFNET:
    951 		printf("root on %s", rootdv->dv_xname);
    952 		break;
    953 
    954 	case DV_DISK:
    955 		printf("root on %s%c", rootdv->dv_xname,
    956 		    DISKPART(rootdev) + 'a');
    957 		break;
    958 
    959 	default:
    960 		printf("can't determine root device\n");
    961 		boothowto |= RB_ASKNAME;
    962 		goto top;
    963 	}
    964 
    965 	/*
    966 	 * Now configure the dump device.
    967 	 *
    968 	 * If we haven't figured out the dump device, do so, with
    969 	 * the following rules:
    970 	 *
    971 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
    972 	 *
    973 	 *	(b) If dumpspec is set, try to use it.  If the device
    974 	 *	    is not available, punt.
    975 	 *
    976 	 *	(c) If dumpspec is not set, the dump device is
    977 	 *	    wildcarded or unspecified.  If the root device
    978 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
    979 	 *	    of the root device.
    980 	 */
    981 
    982 	if (boothowto & RB_ASKNAME) {		/* (a) */
    983 		if (dumpdv == NULL)
    984 			goto nodumpdev;
    985 	} else if (dumpspec != NULL) {		/* (b) */
    986 		if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
    987 			/*
    988 			 * Operator doesn't want a dump device.
    989 			 * Or looks like they tried to pick a network
    990 			 * device.  Oops.
    991 			 */
    992 			goto nodumpdev;
    993 		}
    994 
    995 		dumpdevname = findblkname(major(dumpdev));
    996 		if (dumpdevname == NULL)
    997 			goto nodumpdev;
    998 		memset(buf, 0, sizeof(buf));
    999 		sprintf(buf, "%s%d", dumpdevname, DISKUNIT(dumpdev));
   1000 
   1001 		dumpdv = finddevice(buf);
   1002 		if (dumpdv == NULL) {
   1003 			/*
   1004 			 * Device not configured.
   1005 			 */
   1006 			goto nodumpdev;
   1007 		}
   1008 	} else {				/* (c) */
   1009 		if (rootdv->dv_class == DV_IFNET)
   1010 			goto nodumpdev;
   1011 		else {
   1012 			dumpdv = rootdv;
   1013 			dumpdev = MAKEDISKDEV(major(rootdev),
   1014 			    dumpdv->dv_unit, 1);
   1015 		}
   1016 	}
   1017 
   1018 	printf(" dumps on %s%c\n", dumpdv->dv_xname, DISKPART(dumpdev) + 'a');
   1019 	return;
   1020 
   1021  nodumpdev:
   1022 	dumpdev = NODEV;
   1023 	printf("\n");
   1024 }
   1025 
   1026 static int
   1027 findblkmajor(name)
   1028 	const char *name;
   1029 {
   1030 	int i;
   1031 
   1032 	for (i = 0; dev_name2blk[i].d_name != NULL; i++)
   1033 		if (strncmp(name, dev_name2blk[i].d_name,
   1034 		    strlen(dev_name2blk[i].d_name)) == 0)
   1035 			return (dev_name2blk[i].d_maj);
   1036 	return (-1);
   1037 }
   1038 
   1039 const char *
   1040 findblkname(maj)
   1041 	int maj;
   1042 {
   1043 	int i;
   1044 
   1045 	for (i = 0; dev_name2blk[i].d_name != NULL; i++)
   1046 		if (dev_name2blk[i].d_maj == maj)
   1047 			return (dev_name2blk[i].d_name);
   1048 	return (NULL);
   1049 }
   1050 
   1051 static struct device *
   1052 finddevice(name)
   1053 	const char *name;
   1054 {
   1055 	struct device *dv;
   1056 #ifdef BOOT_FROM_RAID_HOOKS
   1057 	int j;
   1058 
   1059 	for (j = 0; j < numraid; j++) {
   1060 		if (strcmp(name, raidrootdev[j].dv_xname) == 0) {
   1061 			dv = &raidrootdev[j];
   1062 			return (dv);
   1063 		}
   1064 	}
   1065 #endif
   1066 
   1067 	for (dv = TAILQ_FIRST(&alldevs); dv != NULL;
   1068 	    dv = TAILQ_NEXT(dv, dv_list))
   1069 		if (strcmp(dv->dv_xname, name) == 0)
   1070 			break;
   1071 	return (dv);
   1072 }
   1073 
   1074 static struct device *
   1075 getdisk(str, len, defpart, devp, isdump)
   1076 	char *str;
   1077 	int len, defpart;
   1078 	dev_t *devp;
   1079 	int isdump;
   1080 {
   1081 	struct device	*dv;
   1082 #ifdef MEMORY_DISK_HOOKS
   1083 	int		i;
   1084 #endif
   1085 #ifdef BOOT_FROM_RAID_HOOKS
   1086 	int 		j;
   1087 #endif
   1088 
   1089 	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
   1090 		printf("use one of:");
   1091 #ifdef MEMORY_DISK_HOOKS
   1092 		if (isdump == 0)
   1093 			for (i = 0; i < NMD; i++)
   1094 				printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
   1095 				    'a' + MAXPARTITIONS - 1);
   1096 #endif
   1097 #ifdef BOOT_FROM_RAID_HOOKS
   1098 		if (isdump == 0)
   1099 			for (j = 0; j < numraid; j++)
   1100 				printf(" %s[a-%c]", raidrootdev[j].dv_xname,
   1101 				    'a' + MAXPARTITIONS - 1);
   1102 #endif
   1103 		TAILQ_FOREACH(dv, &alldevs, dv_list) {
   1104 			if (dv->dv_class == DV_DISK)
   1105 				printf(" %s[a-%c]", dv->dv_xname,
   1106 				    'a' + MAXPARTITIONS - 1);
   1107 			if (isdump == 0 && dv->dv_class == DV_IFNET)
   1108 				printf(" %s", dv->dv_xname);
   1109 		}
   1110 		if (isdump)
   1111 			printf(" none");
   1112 #if defined(DDB)
   1113 		printf(" ddb");
   1114 #endif
   1115 		printf(" halt reboot\n");
   1116 	}
   1117 	return (dv);
   1118 }
   1119 
   1120 static struct device *
   1121 parsedisk(str, len, defpart, devp)
   1122 	char *str;
   1123 	int len, defpart;
   1124 	dev_t *devp;
   1125 {
   1126 	struct device *dv;
   1127 	char *cp, c;
   1128 	int majdev, part;
   1129 #ifdef MEMORY_DISK_HOOKS
   1130 	int i;
   1131 #endif
   1132 	if (len == 0)
   1133 		return (NULL);
   1134 
   1135 	if (len == 4 && strcmp(str, "halt") == 0)
   1136 		cpu_reboot(RB_HALT, NULL);
   1137 	else if (len == 6 && strcmp(str, "reboot") == 0)
   1138 		cpu_reboot(0, NULL);
   1139 #if defined(DDB)
   1140 	else if (len == 3 && strcmp(str, "ddb") == 0)
   1141 		console_debugger();
   1142 #endif
   1143 
   1144 	cp = str + len - 1;
   1145 	c = *cp;
   1146 	if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
   1147 		part = c - 'a';
   1148 		*cp = '\0';
   1149 	} else
   1150 		part = defpart;
   1151 
   1152 #ifdef MEMORY_DISK_HOOKS
   1153 	for (i = 0; i < NMD; i++)
   1154 		if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) {
   1155 			dv = &fakemdrootdev[i];
   1156 			goto gotdisk;
   1157 		}
   1158 #endif
   1159 
   1160 	dv = finddevice(str);
   1161 	if (dv != NULL) {
   1162 		if (dv->dv_class == DV_DISK) {
   1163 #ifdef MEMORY_DISK_HOOKS
   1164  gotdisk:
   1165 #endif
   1166 			majdev = findblkmajor(dv->dv_xname);
   1167 			if (majdev < 0)
   1168 				panic("parsedisk");
   1169 			*devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
   1170 		}
   1171 
   1172 		if (dv->dv_class == DV_IFNET)
   1173 			*devp = NODEV;
   1174 	}
   1175 
   1176 	*cp = c;
   1177 	return (dv);
   1178 }
   1179 
   1180 /*
   1181  * snprintf() `bytes' into `buf', reformatting it so that the number,
   1182  * plus a possible `x' + suffix extension) fits into len bytes (including
   1183  * the terminating NUL).
   1184  * Returns the number of bytes stored in buf, or -1 if there was a problem.
   1185  * E.g, given a len of 9 and a suffix of `B':
   1186  *	bytes		result
   1187  *	-----		------
   1188  *	99999		`99999 B'
   1189  *	100000		`97 KB'
   1190  *	66715648	`65152 KB'
   1191  *	252215296	`240 MB'
   1192  */
   1193 int
   1194 humanize_number(buf, len, bytes, suffix, divisor)
   1195 	char		*buf;
   1196 	size_t		 len;
   1197 	u_int64_t	 bytes;
   1198 	const char	*suffix;
   1199 	int 		divisor;
   1200 {
   1201 		/* prefixes are: (none), Kilo, Mega, Giga, Tera, Peta, Exa */
   1202 	static const char prefixes[] = " KMGTPE";
   1203 
   1204 	int		i, r;
   1205 	u_int64_t	max;
   1206 	size_t		suffixlen;
   1207 
   1208 	if (buf == NULL || suffix == NULL)
   1209 		return (-1);
   1210 	if (len > 0)
   1211 		buf[0] = '\0';
   1212 	suffixlen = strlen(suffix);
   1213 			/* check if enough room for `x y' + suffix + `\0' */
   1214 	if (len < 4 + suffixlen)
   1215 		return (-1);
   1216 
   1217 	max = 1;
   1218 	for (i = 0; i < len - suffixlen - 3; i++)
   1219 		max *= 10;
   1220 	for (i = 0; bytes >= max && i < sizeof(prefixes); i++)
   1221 		bytes /= divisor;
   1222 
   1223 	r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
   1224 	    i == 0 ? "" : " ", prefixes[i], suffix);
   1225 
   1226 	return (r);
   1227 }
   1228 
   1229 int
   1230 format_bytes(buf, len, bytes)
   1231 	char		*buf;
   1232 	size_t		 len;
   1233 	u_int64_t	 bytes;
   1234 {
   1235 	int	rv;
   1236 	size_t	nlen;
   1237 
   1238 	rv = humanize_number(buf, len, bytes, "B", 1024);
   1239 	if (rv != -1) {
   1240 			/* nuke the trailing ` B' if it exists */
   1241 		nlen = strlen(buf) - 2;
   1242 		if (strcmp(&buf[nlen], " B") == 0)
   1243 			buf[nlen] = '\0';
   1244 	}
   1245 	return (rv);
   1246 }
   1247 
   1248 int
   1249 trace_enter(struct proc *p, register_t code, void *args, register_t rval[])
   1250 {
   1251 #ifdef SYSCALL_DEBUG
   1252 	scdebug_call(p, code, args);
   1253 #endif /* SYSCALL_DEBUG */
   1254 
   1255 #ifdef KTRACE
   1256 	if (KTRPOINT(p, KTR_SYSCALL))
   1257 		ktrsyscall(p, code, args);
   1258 #endif /* KTRACE */
   1259 
   1260 #ifdef SYSTRACE
   1261 	if (ISSET(p->p_flag, P_SYSTRACE))
   1262 		return systrace_enter(p, code, args, rval);
   1263 #endif
   1264 	return 0;
   1265 }
   1266 
   1267 void
   1268 trace_exit(struct proc *p, register_t code, void *args, register_t rval[],
   1269     int error)
   1270 {
   1271 #ifdef SYSCALL_DEBUG
   1272 	scdebug_ret(p, code, error, rval);
   1273 #endif /* SYSCALL_DEBUG */
   1274 
   1275 #ifdef KTRACE
   1276 	if (KTRPOINT(p, KTR_SYSRET)) {
   1277 		KERNEL_PROC_LOCK(p);
   1278 		ktrsysret(p, code, error, rval[0]);
   1279 		KERNEL_PROC_UNLOCK(p);
   1280 	}
   1281 #endif /* KTRACE */
   1282 
   1283 #ifdef SYSTRACE
   1284 	if (ISSET(p->p_flag, P_SYSTRACE))
   1285 		systrace_exit(p, code, args, rval, error);
   1286 #endif
   1287 }
   1288