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