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