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