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