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