kern_subr.c revision 1.82 1 /* $NetBSD: kern_subr.c,v 1.82 2002/07/20 03:58:24 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Luke Mewburn.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1991, 1993
42 * The Regents of the University of California. All rights reserved.
43 * (c) UNIX System Laboratories, Inc.
44 * All or some portions of this file are derived from material licensed
45 * to the University of California by American Telephone and Telegraph
46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 * the permission of UNIX System Laboratories, Inc.
48 *
49 * Copyright (c) 1992, 1993
50 * The Regents of the University of California. All rights reserved.
51 *
52 * This software was developed by the Computer Systems Engineering group
53 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
54 * contributed to Berkeley.
55 *
56 * All advertising materials mentioning features or use of this software
57 * must display the following acknowledgement:
58 * This product includes software developed by the University of
59 * California, Lawrence Berkeley Laboratory.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * 2. Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * 3. All advertising materials mentioning features or use of this software
70 * must display the following acknowledgement:
71 * This product includes software developed by the University of
72 * California, Berkeley and its contributors.
73 * 4. Neither the name of the University nor the names of its contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 *
89 * @(#)kern_subr.c 8.4 (Berkeley) 2/14/95
90 */
91
92 #include <sys/cdefs.h>
93 __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.82 2002/07/20 03:58:24 thorpej Exp $");
94
95 #include "opt_ddb.h"
96 #include "opt_md.h"
97 #include "opt_syscall_debug.h"
98 #include "opt_ktrace.h"
99 #include "opt_systrace.h"
100
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/proc.h>
104 #include <sys/malloc.h>
105 #include <sys/mount.h>
106 #include <sys/device.h>
107 #include <sys/reboot.h>
108 #include <sys/conf.h>
109 #include <sys/disklabel.h>
110 #include <sys/queue.h>
111 #include <sys/systrace.h>
112 #include <sys/ktrace.h>
113
114 #include <uvm/uvm_extern.h>
115
116 #include <dev/cons.h>
117
118 #include <net/if.h>
119
120 /* XXX these should eventually move to subr_autoconf.c */
121 static int findblkmajor __P((const char *));
122 const char *findblkname __P((int));
123 static struct device *finddevice __P((const char *));
124 static struct device *getdisk __P((char *, int, int, dev_t *, int));
125 static struct device *parsedisk __P((char *, int, int, dev_t *));
126
127 /*
128 * A generic linear hook.
129 */
130 struct hook_desc {
131 LIST_ENTRY(hook_desc) hk_list;
132 void (*hk_fn) __P((void *));
133 void *hk_arg;
134 };
135 typedef LIST_HEAD(, hook_desc) hook_list_t;
136
137 static void *hook_establish __P((hook_list_t *, void (*)(void *), void *));
138 static void hook_disestablish __P((hook_list_t *, void *));
139 static void hook_destroy __P((hook_list_t *));
140 static void hook_proc_run __P((hook_list_t *, struct proc *));
141
142 int
143 uiomove(buf, n, uio)
144 void *buf;
145 int n;
146 struct uio *uio;
147 {
148 struct iovec *iov;
149 u_int cnt;
150 int error = 0;
151 char *cp = buf;
152 struct proc *p = uio->uio_procp;
153
154 #ifdef DIAGNOSTIC
155 if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
156 panic("uiomove: mode");
157 #endif
158 while (n > 0 && uio->uio_resid) {
159 iov = uio->uio_iov;
160 cnt = iov->iov_len;
161 if (cnt == 0) {
162 uio->uio_iov++;
163 uio->uio_iovcnt--;
164 continue;
165 }
166 if (cnt > n)
167 cnt = n;
168 switch (uio->uio_segflg) {
169
170 case UIO_USERSPACE:
171 if (curproc->p_cpu->ci_schedstate.spc_flags &
172 SPCF_SHOULDYIELD)
173 preempt(NULL);
174 if (__predict_true(p == curproc)) {
175 if (uio->uio_rw == UIO_READ)
176 error = copyout(cp, iov->iov_base, cnt);
177 else
178 error = copyin(iov->iov_base, cp, cnt);
179 } else {
180 if (uio->uio_rw == UIO_READ)
181 error = copyout_proc(p, cp,
182 iov->iov_base, cnt);
183 else
184 error = copyin_proc(p, iov->iov_base,
185 cp, cnt);
186 }
187 if (error)
188 return (error);
189 break;
190
191 case UIO_SYSSPACE:
192 if (uio->uio_rw == UIO_READ)
193 error = kcopy(cp, iov->iov_base, cnt);
194 else
195 error = kcopy(iov->iov_base, cp, cnt);
196 if (error)
197 return (error);
198 break;
199 }
200 iov->iov_base = (caddr_t)iov->iov_base + cnt;
201 iov->iov_len -= cnt;
202 uio->uio_resid -= cnt;
203 uio->uio_offset += cnt;
204 cp += cnt;
205 n -= cnt;
206 }
207 return (error);
208 }
209
210 /*
211 * Give next character to user as result of read.
212 */
213 int
214 ureadc(c, uio)
215 int c;
216 struct uio *uio;
217 {
218 struct iovec *iov;
219
220 if (uio->uio_resid <= 0)
221 panic("ureadc: non-positive resid");
222 again:
223 if (uio->uio_iovcnt <= 0)
224 panic("ureadc: non-positive iovcnt");
225 iov = uio->uio_iov;
226 if (iov->iov_len <= 0) {
227 uio->uio_iovcnt--;
228 uio->uio_iov++;
229 goto again;
230 }
231 switch (uio->uio_segflg) {
232
233 case UIO_USERSPACE:
234 if (subyte(iov->iov_base, c) < 0)
235 return (EFAULT);
236 break;
237
238 case UIO_SYSSPACE:
239 *(char *)iov->iov_base = c;
240 break;
241 }
242 iov->iov_base = (caddr_t)iov->iov_base + 1;
243 iov->iov_len--;
244 uio->uio_resid--;
245 uio->uio_offset++;
246 return (0);
247 }
248
249 /*
250 * Like copyin(), but operates on an arbitrary process.
251 */
252 int
253 copyin_proc(struct proc *p, const void *uaddr, void *kaddr, size_t len)
254 {
255 struct iovec iov;
256 struct uio uio;
257 int error;
258
259 if (len == 0)
260 return (0);
261
262 iov.iov_base = kaddr;
263 iov.iov_len = len;
264 uio.uio_iov = &iov;
265 uio.uio_iovcnt = 1;
266 uio.uio_offset = (off_t)(intptr_t)uaddr;
267 uio.uio_resid = len;
268 uio.uio_segflg = UIO_SYSSPACE;
269 uio.uio_rw = UIO_READ;
270 uio.uio_procp = NULL;
271
272 /* XXXCDC: how should locking work here? */
273 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
274 return (EFAULT);
275 p->p_vmspace->vm_refcnt++; /* XXX */
276 error = uvm_io(&p->p_vmspace->vm_map, &uio);
277 uvmspace_free(p->p_vmspace);
278
279 return (error);
280 }
281
282 /*
283 * Like copyout(), but operates on an arbitrary process.
284 */
285 int
286 copyout_proc(struct proc *p, const void *kaddr, void *uaddr, size_t len)
287 {
288 struct iovec iov;
289 struct uio uio;
290 int error;
291
292 if (len == 0)
293 return (0);
294
295 iov.iov_base = (void *) kaddr; /* XXX cast away const */
296 iov.iov_len = len;
297 uio.uio_iov = &iov;
298 uio.uio_iovcnt = 1;
299 uio.uio_offset = (off_t)(intptr_t)uaddr;
300 uio.uio_resid = len;
301 uio.uio_segflg = UIO_SYSSPACE;
302 uio.uio_rw = UIO_WRITE;
303 uio.uio_procp = NULL;
304
305 /* XXXCDC: how should locking work here? */
306 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
307 return (EFAULT);
308 p->p_vmspace->vm_refcnt++; /* XXX */
309 error = uvm_io(&p->p_vmspace->vm_map, &uio);
310 uvmspace_free(p->p_vmspace);
311
312 return (error);
313 }
314
315 /*
316 * General routine to allocate a hash table.
317 * Allocate enough memory to hold at least `elements' list-head pointers.
318 * Return a pointer to the allocated space and set *hashmask to a pattern
319 * suitable for masking a value to use as an index into the returned array.
320 */
321 void *
322 hashinit(elements, htype, mtype, mflags, hashmask)
323 int elements;
324 enum hashtype htype;
325 int mtype, mflags;
326 u_long *hashmask;
327 {
328 long hashsize;
329 LIST_HEAD(, generic) *hashtbl_list;
330 TAILQ_HEAD(, generic) *hashtbl_tailq;
331 int i, esize;
332 void *p;
333
334 if (elements <= 0)
335 panic("hashinit: bad cnt");
336 for (hashsize = 1; hashsize < elements; hashsize <<= 1)
337 continue;
338
339 switch (htype) {
340 case HASH_LIST:
341 esize = sizeof(*hashtbl_list);
342 break;
343 case HASH_TAILQ:
344 esize = sizeof(*hashtbl_tailq);
345 break;
346 #ifdef DIAGNOSTIC
347 default:
348 panic("hashinit: invalid table type");
349 #endif
350 }
351
352 if ((p = malloc((u_long)hashsize * esize, mtype, mflags)) == NULL)
353 return (NULL);
354
355 switch (htype) {
356 case HASH_LIST:
357 hashtbl_list = p;
358 for (i = 0; i < hashsize; i++)
359 LIST_INIT(&hashtbl_list[i]);
360 break;
361 case HASH_TAILQ:
362 hashtbl_tailq = p;
363 for (i = 0; i < hashsize; i++)
364 TAILQ_INIT(&hashtbl_tailq[i]);
365 break;
366 }
367 *hashmask = hashsize - 1;
368 return (p);
369 }
370
371 /*
372 * Free memory from hash table previosly allocated via hashinit().
373 */
374 void
375 hashdone(hashtbl, mtype)
376 void *hashtbl;
377 int mtype;
378 {
379
380 free(hashtbl, mtype);
381 }
382
383
384 static void *
385 hook_establish(list, fn, arg)
386 hook_list_t *list;
387 void (*fn) __P((void *));
388 void *arg;
389 {
390 struct hook_desc *hd;
391
392 hd = malloc(sizeof(*hd), M_DEVBUF, M_NOWAIT);
393 if (hd == NULL)
394 return (NULL);
395
396 hd->hk_fn = fn;
397 hd->hk_arg = arg;
398 LIST_INSERT_HEAD(list, hd, hk_list);
399
400 return (hd);
401 }
402
403 static void
404 hook_disestablish(list, vhook)
405 hook_list_t *list;
406 void *vhook;
407 {
408 #ifdef DIAGNOSTIC
409 struct hook_desc *hd;
410
411 for (hd = list->lh_first; hd != NULL; hd = hd->hk_list.le_next)
412 if (hd == vhook)
413 break;
414 if (hd == NULL)
415 panic("hook_disestablish: hook not established");
416 #endif
417 LIST_REMOVE((struct hook_desc *)vhook, hk_list);
418 free(vhook, M_DEVBUF);
419 }
420
421 static void
422 hook_destroy(list)
423 hook_list_t *list;
424 {
425 struct hook_desc *hd;
426
427 while ((hd = list->lh_first) != NULL) {
428 LIST_REMOVE(hd, hk_list);
429 free(hd, M_DEVBUF);
430 }
431 }
432
433 static void
434 hook_proc_run(list, p)
435 hook_list_t *list;
436 struct proc *p;
437 {
438 struct hook_desc *hd;
439
440 for (hd = LIST_FIRST(list); hd != NULL; hd = LIST_NEXT(hd, hk_list)) {
441 ((void (*) __P((struct proc *, void *)))*hd->hk_fn)(p,
442 hd->hk_arg);
443 }
444 }
445
446 /*
447 * "Shutdown hook" types, functions, and variables.
448 *
449 * Should be invoked immediately before the
450 * system is halted or rebooted, i.e. after file systems unmounted,
451 * after crash dump done, etc.
452 *
453 * Each shutdown hook is removed from the list before it's run, so that
454 * it won't be run again.
455 */
456
457 hook_list_t shutdownhook_list;
458
459 void *
460 shutdownhook_establish(fn, arg)
461 void (*fn) __P((void *));
462 void *arg;
463 {
464 return hook_establish(&shutdownhook_list, fn, arg);
465 }
466
467 void
468 shutdownhook_disestablish(vhook)
469 void *vhook;
470 {
471 return hook_disestablish(&shutdownhook_list, vhook);
472 }
473
474 /*
475 * Run shutdown hooks. Should be invoked immediately before the
476 * system is halted or rebooted, i.e. after file systems unmounted,
477 * after crash dump done, etc.
478 *
479 * Each shutdown hook is removed from the list before it's run, so that
480 * it won't be run again.
481 */
482 void
483 doshutdownhooks()
484 {
485 struct hook_desc *dp;
486
487 while ((dp = shutdownhook_list.lh_first) != NULL) {
488 LIST_REMOVE(dp, hk_list);
489 (*dp->hk_fn)(dp->hk_arg);
490 #if 0
491 /*
492 * Don't bother freeing the hook structure,, since we may
493 * be rebooting because of a memory corruption problem,
494 * and this might only make things worse. It doesn't
495 * matter, anyway, since the system is just about to
496 * reboot.
497 */
498 free(dp, M_DEVBUF);
499 #endif
500 }
501 }
502
503 /*
504 * "Mountroot hook" types, functions, and variables.
505 */
506
507 hook_list_t mountroothook_list;
508
509 void *
510 mountroothook_establish(fn, dev)
511 void (*fn) __P((struct device *));
512 struct device *dev;
513 {
514 return hook_establish(&mountroothook_list, (void (*)__P((void *)))fn,
515 dev);
516 }
517
518 void
519 mountroothook_disestablish(vhook)
520 void *vhook;
521 {
522 return hook_disestablish(&mountroothook_list, vhook);
523 }
524
525 void
526 mountroothook_destroy()
527 {
528 hook_destroy(&mountroothook_list);
529 }
530
531 void
532 domountroothook()
533 {
534 struct hook_desc *hd;
535
536 for (hd = mountroothook_list.lh_first; hd != NULL;
537 hd = hd->hk_list.le_next) {
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 not established");
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 for (ifp = ifnet.tqh_first; ifp != NULL;
742 ifp = ifp->if_list.tqe_next)
743 if ((ifp->if_flags &
744 (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
745 break;
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 = findblkmajor(bootdv->dv_xname);
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 = findblkname(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 = findblkname(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 int
1026 findblkmajor(name)
1027 const char *name;
1028 {
1029 int i;
1030
1031 for (i = 0; dev_name2blk[i].d_name != NULL; i++)
1032 if (strncmp(name, dev_name2blk[i].d_name,
1033 strlen(dev_name2blk[i].d_name)) == 0)
1034 return (dev_name2blk[i].d_maj);
1035 return (-1);
1036 }
1037
1038 const char *
1039 findblkname(maj)
1040 int maj;
1041 {
1042 int i;
1043
1044 for (i = 0; dev_name2blk[i].d_name != NULL; i++)
1045 if (dev_name2blk[i].d_maj == maj)
1046 return (dev_name2blk[i].d_name);
1047 return (NULL);
1048 }
1049
1050 static struct device *
1051 finddevice(name)
1052 const char *name;
1053 {
1054 struct device *dv;
1055 #ifdef BOOT_FROM_RAID_HOOKS
1056 int j;
1057
1058 for (j = 0; j < numraid; j++) {
1059 if (strcmp(name, raidrootdev[j].dv_xname) == 0) {
1060 dv = &raidrootdev[j];
1061 return (dv);
1062 }
1063 }
1064 #endif
1065
1066 for (dv = TAILQ_FIRST(&alldevs); dv != NULL;
1067 dv = TAILQ_NEXT(dv, dv_list))
1068 if (strcmp(dv->dv_xname, name) == 0)
1069 break;
1070 return (dv);
1071 }
1072
1073 static struct device *
1074 getdisk(str, len, defpart, devp, isdump)
1075 char *str;
1076 int len, defpart;
1077 dev_t *devp;
1078 int isdump;
1079 {
1080 struct device *dv;
1081 #ifdef MEMORY_DISK_HOOKS
1082 int i;
1083 #endif
1084 #ifdef BOOT_FROM_RAID_HOOKS
1085 int j;
1086 #endif
1087
1088 if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
1089 printf("use one of:");
1090 #ifdef MEMORY_DISK_HOOKS
1091 if (isdump == 0)
1092 for (i = 0; i < NMD; i++)
1093 printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
1094 'a' + MAXPARTITIONS - 1);
1095 #endif
1096 #ifdef BOOT_FROM_RAID_HOOKS
1097 if (isdump == 0)
1098 for (j = 0; j < numraid; j++)
1099 printf(" %s[a-%c]", raidrootdev[j].dv_xname,
1100 'a' + MAXPARTITIONS - 1);
1101 #endif
1102 for (dv = alldevs.tqh_first; dv != NULL;
1103 dv = dv->dv_list.tqe_next) {
1104 if (dv->dv_class == DV_DISK)
1105 printf(" %s[a-%c]", dv->dv_xname,
1106 'a' + MAXPARTITIONS - 1);
1107 if (isdump == 0 && dv->dv_class == DV_IFNET)
1108 printf(" %s", dv->dv_xname);
1109 }
1110 if (isdump)
1111 printf(" none");
1112 #if defined(DDB)
1113 printf(" ddb");
1114 #endif
1115 printf(" halt reboot\n");
1116 }
1117 return (dv);
1118 }
1119
1120 static struct device *
1121 parsedisk(str, len, defpart, devp)
1122 char *str;
1123 int len, defpart;
1124 dev_t *devp;
1125 {
1126 struct device *dv;
1127 char *cp, c;
1128 int majdev, part;
1129 #ifdef MEMORY_DISK_HOOKS
1130 int i;
1131 #endif
1132 if (len == 0)
1133 return (NULL);
1134
1135 if (len == 4 && strcmp(str, "halt") == 0)
1136 cpu_reboot(RB_HALT, NULL);
1137 else if (len == 6 && strcmp(str, "reboot") == 0)
1138 cpu_reboot(0, NULL);
1139 #if defined(DDB)
1140 else if (len == 3 && strcmp(str, "ddb") == 0)
1141 console_debugger();
1142 #endif
1143
1144 cp = str + len - 1;
1145 c = *cp;
1146 if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
1147 part = c - 'a';
1148 *cp = '\0';
1149 } else
1150 part = defpart;
1151
1152 #ifdef MEMORY_DISK_HOOKS
1153 for (i = 0; i < NMD; i++)
1154 if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) {
1155 dv = &fakemdrootdev[i];
1156 goto gotdisk;
1157 }
1158 #endif
1159
1160 dv = finddevice(str);
1161 if (dv != NULL) {
1162 if (dv->dv_class == DV_DISK) {
1163 #ifdef MEMORY_DISK_HOOKS
1164 gotdisk:
1165 #endif
1166 majdev = findblkmajor(dv->dv_xname);
1167 if (majdev < 0)
1168 panic("parsedisk");
1169 *devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
1170 }
1171
1172 if (dv->dv_class == DV_IFNET)
1173 *devp = NODEV;
1174 }
1175
1176 *cp = c;
1177 return (dv);
1178 }
1179
1180 /*
1181 * snprintf() `bytes' into `buf', reformatting it so that the number,
1182 * plus a possible `x' + suffix extension) fits into len bytes (including
1183 * the terminating NUL).
1184 * Returns the number of bytes stored in buf, or -1 if there was a problem.
1185 * E.g, given a len of 9 and a suffix of `B':
1186 * bytes result
1187 * ----- ------
1188 * 99999 `99999 B'
1189 * 100000 `97 KB'
1190 * 66715648 `65152 KB'
1191 * 252215296 `240 MB'
1192 */
1193 int
1194 humanize_number(buf, len, bytes, suffix, divisor)
1195 char *buf;
1196 size_t len;
1197 u_int64_t bytes;
1198 const char *suffix;
1199 int divisor;
1200 {
1201 /* prefixes are: (none), Kilo, Mega, Giga, Tera, Peta, Exa */
1202 static const char prefixes[] = " KMGTPE";
1203
1204 int i, r;
1205 u_int64_t max;
1206 size_t suffixlen;
1207
1208 if (buf == NULL || suffix == NULL)
1209 return (-1);
1210 if (len > 0)
1211 buf[0] = '\0';
1212 suffixlen = strlen(suffix);
1213 /* check if enough room for `x y' + suffix + `\0' */
1214 if (len < 4 + suffixlen)
1215 return (-1);
1216
1217 max = 1;
1218 for (i = 0; i < len - suffixlen - 3; i++)
1219 max *= 10;
1220 for (i = 0; bytes >= max && i < sizeof(prefixes); i++)
1221 bytes /= divisor;
1222
1223 r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
1224 i == 0 ? "" : " ", prefixes[i], suffix);
1225
1226 return (r);
1227 }
1228
1229 int
1230 format_bytes(buf, len, bytes)
1231 char *buf;
1232 size_t len;
1233 u_int64_t bytes;
1234 {
1235 int rv;
1236 size_t nlen;
1237
1238 rv = humanize_number(buf, len, bytes, "B", 1024);
1239 if (rv != -1) {
1240 /* nuke the trailing ` B' if it exists */
1241 nlen = strlen(buf) - 2;
1242 if (strcmp(&buf[nlen], " B") == 0)
1243 buf[nlen] = '\0';
1244 }
1245 return (rv);
1246 }
1247
1248 int
1249 trace_enter(struct proc *p, register_t code, void *args, register_t rval[])
1250 {
1251 #ifdef SYSCALL_DEBUG
1252 scdebug_call(p, code, args);
1253 #endif /* SYSCALL_DEBUG */
1254
1255 #ifdef KTRACE
1256 if (KTRPOINT(p, KTR_SYSCALL))
1257 ktrsyscall(p, code, args);
1258 #endif /* KTRACE */
1259
1260 #ifdef SYSTRACE
1261 if (ISSET(p->p_flag, P_SYSTRACE))
1262 return systrace_enter(p, code, args, rval);
1263 #endif
1264 return 0;
1265 }
1266
1267 void
1268 trace_exit(struct proc *p, register_t code, void *args, register_t rval[],
1269 int error)
1270 {
1271 #ifdef SYSCALL_DEBUG
1272 scdebug_ret(p, code, error, rval);
1273 #endif /* SYSCALL_DEBUG */
1274
1275 #ifdef KTRACE
1276 if (KTRPOINT(p, KTR_SYSRET)) {
1277 KERNEL_PROC_LOCK(p);
1278 ktrsysret(p, code, error, rval[0]);
1279 KERNEL_PROC_UNLOCK(p);
1280 }
1281 #endif /* KTRACE */
1282
1283 #ifdef SYSTRACE
1284 if (ISSET(p->p_flag, P_SYSTRACE))
1285 systrace_exit(p, code, args, rval, error);
1286 #endif
1287 }
1288