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