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