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