kern_subr.c revision 1.188 1 /* $NetBSD: kern_subr.c,v 1.188 2008/05/24 16:49:30 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
41 *
42 * Copyright (c) 1992, 1993
43 * The Regents of the University of California. All rights reserved.
44 *
45 * This software was developed by the Computer Systems Engineering group
46 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
47 * contributed to Berkeley.
48 *
49 * All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Lawrence Berkeley Laboratory.
53 *
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
56 * are met:
57 * 1. Redistributions of source code must retain the above copyright
58 * notice, this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright
60 * notice, this list of conditions and the following disclaimer in the
61 * documentation and/or other materials provided with the distribution.
62 * 3. Neither the name of the University nor the names of its contributors
63 * may be used to endorse or promote products derived from this software
64 * without specific prior written permission.
65 *
66 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 * SUCH DAMAGE.
77 *
78 * @(#)kern_subr.c 8.4 (Berkeley) 2/14/95
79 */
80
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.188 2008/05/24 16:49:30 christos Exp $");
83
84 #include "opt_ddb.h"
85 #include "opt_md.h"
86 #include "opt_syscall_debug.h"
87 #include "opt_ktrace.h"
88 #include "opt_ptrace.h"
89 #include "opt_powerhook.h"
90 #include "opt_tftproot.h"
91
92 #include <sys/param.h>
93 #include <sys/systm.h>
94 #include <sys/proc.h>
95 #include <sys/malloc.h>
96 #include <sys/mount.h>
97 #include <sys/device.h>
98 #include <sys/reboot.h>
99 #include <sys/conf.h>
100 #include <sys/disk.h>
101 #include <sys/disklabel.h>
102 #include <sys/queue.h>
103 #include <sys/ktrace.h>
104 #include <sys/ptrace.h>
105 #include <sys/fcntl.h>
106 #include <sys/kauth.h>
107 #include <sys/vnode.h>
108 #include <sys/pmf.h>
109
110 #include <uvm/uvm_extern.h>
111
112 #include <dev/cons.h>
113
114 #include <net/if.h>
115
116 /* XXX these should eventually move to subr_autoconf.c */
117 static struct device *finddevice(const char *);
118 static struct device *getdisk(char *, int, int, dev_t *, int);
119 static struct device *parsedisk(char *, int, int, dev_t *);
120 static const char *getwedgename(const char *, int);
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 #ifdef TFTPROOT
135 int tftproot_dhcpboot(struct device *);
136 #endif
137
138 dev_t dumpcdev; /* for savecore */
139
140 void
141 uio_setup_sysspace(struct uio *uio)
142 {
143
144 uio->uio_vmspace = vmspace_kernel();
145 }
146
147 int
148 uiomove(void *buf, size_t n, struct uio *uio)
149 {
150 struct vmspace *vm = uio->uio_vmspace;
151 struct iovec *iov;
152 size_t cnt;
153 int error = 0;
154 char *cp = buf;
155
156 ASSERT_SLEEPABLE();
157
158 #ifdef DIAGNOSTIC
159 if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
160 panic("uiomove: mode");
161 #endif
162 while (n > 0 && uio->uio_resid) {
163 iov = uio->uio_iov;
164 cnt = iov->iov_len;
165 if (cnt == 0) {
166 KASSERT(uio->uio_iovcnt > 0);
167 uio->uio_iov++;
168 uio->uio_iovcnt--;
169 continue;
170 }
171 if (cnt > n)
172 cnt = n;
173 if (!VMSPACE_IS_KERNEL_P(vm)) {
174 if (curcpu()->ci_schedstate.spc_flags &
175 SPCF_SHOULDYIELD)
176 preempt();
177 }
178
179 if (uio->uio_rw == UIO_READ) {
180 error = copyout_vmspace(vm, cp, iov->iov_base,
181 cnt);
182 } else {
183 error = copyin_vmspace(vm, iov->iov_base, cp,
184 cnt);
185 }
186 if (error) {
187 break;
188 }
189 iov->iov_base = (char *)iov->iov_base + cnt;
190 iov->iov_len -= cnt;
191 uio->uio_resid -= cnt;
192 uio->uio_offset += cnt;
193 cp += cnt;
194 KDASSERT(cnt <= n);
195 n -= cnt;
196 }
197
198 return (error);
199 }
200
201 /*
202 * Wrapper for uiomove() that validates the arguments against a known-good
203 * kernel buffer.
204 */
205 int
206 uiomove_frombuf(void *buf, size_t buflen, struct uio *uio)
207 {
208 size_t offset;
209
210 if (uio->uio_offset < 0 || /* uio->uio_resid < 0 || */
211 (offset = uio->uio_offset) != uio->uio_offset)
212 return (EINVAL);
213 if (offset >= buflen)
214 return (0);
215 return (uiomove((char *)buf + offset, buflen - offset, uio));
216 }
217
218 /*
219 * Give next character to user as result of read.
220 */
221 int
222 ureadc(int c, struct uio *uio)
223 {
224 struct iovec *iov;
225
226 if (uio->uio_resid <= 0)
227 panic("ureadc: non-positive resid");
228 again:
229 if (uio->uio_iovcnt <= 0)
230 panic("ureadc: non-positive iovcnt");
231 iov = uio->uio_iov;
232 if (iov->iov_len <= 0) {
233 uio->uio_iovcnt--;
234 uio->uio_iov++;
235 goto again;
236 }
237 if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
238 if (subyte(iov->iov_base, c) < 0)
239 return (EFAULT);
240 } else {
241 *(char *)iov->iov_base = c;
242 }
243 iov->iov_base = (char *)iov->iov_base + 1;
244 iov->iov_len--;
245 uio->uio_resid--;
246 uio->uio_offset++;
247 return (0);
248 }
249
250 /*
251 * Like copyin(), but operates on an arbitrary vmspace.
252 */
253 int
254 copyin_vmspace(struct vmspace *vm, const void *uaddr, void *kaddr, size_t len)
255 {
256 struct iovec iov;
257 struct uio uio;
258 int error;
259
260 if (len == 0)
261 return (0);
262
263 if (VMSPACE_IS_KERNEL_P(vm)) {
264 return kcopy(uaddr, kaddr, len);
265 }
266 if (__predict_true(vm == curproc->p_vmspace)) {
267 return copyin(uaddr, kaddr, len);
268 }
269
270 iov.iov_base = kaddr;
271 iov.iov_len = len;
272 uio.uio_iov = &iov;
273 uio.uio_iovcnt = 1;
274 uio.uio_offset = (off_t)(intptr_t)uaddr;
275 uio.uio_resid = len;
276 uio.uio_rw = UIO_READ;
277 UIO_SETUP_SYSSPACE(&uio);
278 error = uvm_io(&vm->vm_map, &uio);
279
280 return (error);
281 }
282
283 /*
284 * Like copyout(), but operates on an arbitrary vmspace.
285 */
286 int
287 copyout_vmspace(struct vmspace *vm, const void *kaddr, void *uaddr, size_t len)
288 {
289 struct iovec iov;
290 struct uio uio;
291 int error;
292
293 if (len == 0)
294 return (0);
295
296 if (VMSPACE_IS_KERNEL_P(vm)) {
297 return kcopy(kaddr, uaddr, len);
298 }
299 if (__predict_true(vm == curproc->p_vmspace)) {
300 return copyout(kaddr, uaddr, len);
301 }
302
303 iov.iov_base = __UNCONST(kaddr); /* XXXUNCONST cast away const */
304 iov.iov_len = len;
305 uio.uio_iov = &iov;
306 uio.uio_iovcnt = 1;
307 uio.uio_offset = (off_t)(intptr_t)uaddr;
308 uio.uio_resid = len;
309 uio.uio_rw = UIO_WRITE;
310 UIO_SETUP_SYSSPACE(&uio);
311 error = uvm_io(&vm->vm_map, &uio);
312
313 return (error);
314 }
315
316 /*
317 * Like copyin(), but operates on an arbitrary process.
318 */
319 int
320 copyin_proc(struct proc *p, const void *uaddr, void *kaddr, size_t len)
321 {
322 struct vmspace *vm;
323 int error;
324
325 error = proc_vmspace_getref(p, &vm);
326 if (error) {
327 return error;
328 }
329 error = copyin_vmspace(vm, uaddr, kaddr, len);
330 uvmspace_free(vm);
331
332 return error;
333 }
334
335 /*
336 * Like copyout(), but operates on an arbitrary process.
337 */
338 int
339 copyout_proc(struct proc *p, const void *kaddr, void *uaddr, size_t len)
340 {
341 struct vmspace *vm;
342 int error;
343
344 error = proc_vmspace_getref(p, &vm);
345 if (error) {
346 return error;
347 }
348 error = copyout_vmspace(vm, kaddr, uaddr, len);
349 uvmspace_free(vm);
350
351 return error;
352 }
353
354 /*
355 * Like copyin(), except it operates on kernel addresses when the FKIOCTL
356 * flag is passed in `ioctlflags' from the ioctl call.
357 */
358 int
359 ioctl_copyin(int ioctlflags, const void *src, void *dst, size_t len)
360 {
361 if (ioctlflags & FKIOCTL)
362 return kcopy(src, dst, len);
363 return copyin(src, dst, len);
364 }
365
366 /*
367 * Like copyout(), except it operates on kernel addresses when the FKIOCTL
368 * flag is passed in `ioctlflags' from the ioctl call.
369 */
370 int
371 ioctl_copyout(int ioctlflags, const void *src, void *dst, size_t len)
372 {
373 if (ioctlflags & FKIOCTL)
374 return kcopy(src, dst, len);
375 return copyout(src, dst, len);
376 }
377
378 static void *
379 hook_establish(hook_list_t *list, void (*fn)(void *), void *arg)
380 {
381 struct hook_desc *hd;
382
383 hd = malloc(sizeof(*hd), M_DEVBUF, M_NOWAIT);
384 if (hd == NULL)
385 return (NULL);
386
387 hd->hk_fn = fn;
388 hd->hk_arg = arg;
389 LIST_INSERT_HEAD(list, hd, hk_list);
390
391 return (hd);
392 }
393
394 static void
395 hook_disestablish(hook_list_t *list, void *vhook)
396 {
397 #ifdef DIAGNOSTIC
398 struct hook_desc *hd;
399
400 LIST_FOREACH(hd, list, hk_list) {
401 if (hd == vhook)
402 break;
403 }
404
405 if (hd == NULL)
406 panic("hook_disestablish: hook %p not established", vhook);
407 #endif
408 LIST_REMOVE((struct hook_desc *)vhook, hk_list);
409 free(vhook, M_DEVBUF);
410 }
411
412 static void
413 hook_destroy(hook_list_t *list)
414 {
415 struct hook_desc *hd;
416
417 while ((hd = LIST_FIRST(list)) != NULL) {
418 LIST_REMOVE(hd, hk_list);
419 free(hd, M_DEVBUF);
420 }
421 }
422
423 static void
424 hook_proc_run(hook_list_t *list, struct proc *p)
425 {
426 struct hook_desc *hd;
427
428 LIST_FOREACH(hd, list, hk_list)
429 ((void (*)(struct proc *, void *))*hd->hk_fn)(p, hd->hk_arg);
430 }
431
432 /*
433 * "Shutdown hook" types, functions, and variables.
434 *
435 * Should be invoked immediately before the
436 * system is halted or rebooted, i.e. after file systems unmounted,
437 * after crash dump done, etc.
438 *
439 * Each shutdown hook is removed from the list before it's run, so that
440 * it won't be run again.
441 */
442
443 static hook_list_t shutdownhook_list;
444
445 void *
446 shutdownhook_establish(void (*fn)(void *), void *arg)
447 {
448 return hook_establish(&shutdownhook_list, fn, arg);
449 }
450
451 void
452 shutdownhook_disestablish(void *vhook)
453 {
454 hook_disestablish(&shutdownhook_list, vhook);
455 }
456
457 /*
458 * Run shutdown hooks. Should be invoked immediately before the
459 * system is halted or rebooted, i.e. after file systems unmounted,
460 * after crash dump done, etc.
461 *
462 * Each shutdown hook is removed from the list before it's run, so that
463 * it won't be run again.
464 */
465 void
466 doshutdownhooks(void)
467 {
468 struct hook_desc *dp;
469
470 if (panicstr != NULL) {
471 /*
472 * Do as few things as possible after a panic.
473 * We don't know the state the system is in.
474 */
475 return;
476 }
477
478 while ((dp = LIST_FIRST(&shutdownhook_list)) != NULL) {
479 LIST_REMOVE(dp, hk_list);
480 (*dp->hk_fn)(dp->hk_arg);
481 #if 0
482 /*
483 * Don't bother freeing the hook structure,, since we may
484 * be rebooting because of a memory corruption problem,
485 * and this might only make things worse. It doesn't
486 * matter, anyway, since the system is just about to
487 * reboot.
488 */
489 free(dp, M_DEVBUF);
490 #endif
491 }
492
493 pmf_system_shutdown(boothowto);
494 }
495
496 /*
497 * "Mountroot hook" types, functions, and variables.
498 */
499
500 static hook_list_t mountroothook_list;
501
502 void *
503 mountroothook_establish(void (*fn)(struct device *), struct device *dev)
504 {
505 return hook_establish(&mountroothook_list, (void (*)(void *))fn, dev);
506 }
507
508 void
509 mountroothook_disestablish(void *vhook)
510 {
511 hook_disestablish(&mountroothook_list, vhook);
512 }
513
514 void
515 mountroothook_destroy(void)
516 {
517 hook_destroy(&mountroothook_list);
518 }
519
520 void
521 domountroothook(void)
522 {
523 struct hook_desc *hd;
524
525 LIST_FOREACH(hd, &mountroothook_list, hk_list) {
526 if (hd->hk_arg == (void *)root_device) {
527 (*hd->hk_fn)(hd->hk_arg);
528 return;
529 }
530 }
531 }
532
533 static hook_list_t exechook_list;
534
535 void *
536 exechook_establish(void (*fn)(struct proc *, void *), void *arg)
537 {
538 return hook_establish(&exechook_list, (void (*)(void *))fn, arg);
539 }
540
541 void
542 exechook_disestablish(void *vhook)
543 {
544 hook_disestablish(&exechook_list, vhook);
545 }
546
547 /*
548 * Run exec hooks.
549 */
550 void
551 doexechooks(struct proc *p)
552 {
553 hook_proc_run(&exechook_list, p);
554 }
555
556 static hook_list_t exithook_list;
557
558 void *
559 exithook_establish(void (*fn)(struct proc *, void *), void *arg)
560 {
561 return hook_establish(&exithook_list, (void (*)(void *))fn, arg);
562 }
563
564 void
565 exithook_disestablish(void *vhook)
566 {
567 hook_disestablish(&exithook_list, vhook);
568 }
569
570 /*
571 * Run exit hooks.
572 */
573 void
574 doexithooks(struct proc *p)
575 {
576 hook_proc_run(&exithook_list, p);
577 }
578
579 static hook_list_t forkhook_list;
580
581 void *
582 forkhook_establish(void (*fn)(struct proc *, struct proc *))
583 {
584 return hook_establish(&forkhook_list, (void (*)(void *))fn, NULL);
585 }
586
587 void
588 forkhook_disestablish(void *vhook)
589 {
590 hook_disestablish(&forkhook_list, vhook);
591 }
592
593 /*
594 * Run fork hooks.
595 */
596 void
597 doforkhooks(struct proc *p2, struct proc *p1)
598 {
599 struct hook_desc *hd;
600
601 LIST_FOREACH(hd, &forkhook_list, hk_list) {
602 ((void (*)(struct proc *, struct proc *))*hd->hk_fn)
603 (p2, p1);
604 }
605 }
606
607 /*
608 * "Power hook" types, functions, and variables.
609 * The list of power hooks is kept ordered with the last registered hook
610 * first.
611 * When running the hooks on power down the hooks are called in reverse
612 * registration order, when powering up in registration order.
613 */
614 struct powerhook_desc {
615 CIRCLEQ_ENTRY(powerhook_desc) sfd_list;
616 void (*sfd_fn)(int, void *);
617 void *sfd_arg;
618 char sfd_name[16];
619 };
620
621 static CIRCLEQ_HEAD(, powerhook_desc) powerhook_list =
622 CIRCLEQ_HEAD_INITIALIZER(powerhook_list);
623
624 void *
625 powerhook_establish(const char *name, void (*fn)(int, void *), void *arg)
626 {
627 struct powerhook_desc *ndp;
628
629 ndp = (struct powerhook_desc *)
630 malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
631 if (ndp == NULL)
632 return (NULL);
633
634 ndp->sfd_fn = fn;
635 ndp->sfd_arg = arg;
636 strlcpy(ndp->sfd_name, name, sizeof(ndp->sfd_name));
637 CIRCLEQ_INSERT_HEAD(&powerhook_list, ndp, sfd_list);
638
639 aprint_error("%s: WARNING: powerhook_establish is deprecated\n", name);
640 return (ndp);
641 }
642
643 void
644 powerhook_disestablish(void *vhook)
645 {
646 #ifdef DIAGNOSTIC
647 struct powerhook_desc *dp;
648
649 CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list)
650 if (dp == vhook)
651 goto found;
652 panic("powerhook_disestablish: hook %p not established", vhook);
653 found:
654 #endif
655
656 CIRCLEQ_REMOVE(&powerhook_list, (struct powerhook_desc *)vhook,
657 sfd_list);
658 free(vhook, M_DEVBUF);
659 }
660
661 /*
662 * Run power hooks.
663 */
664 void
665 dopowerhooks(int why)
666 {
667 struct powerhook_desc *dp;
668
669 #ifdef POWERHOOK_DEBUG
670 const char *why_name;
671 static const char * pwr_names[] = {PWR_NAMES};
672 why_name = why < __arraycount(pwr_names) ? pwr_names[why] : "???";
673 #endif
674
675 if (why == PWR_RESUME || why == PWR_SOFTRESUME) {
676 CIRCLEQ_FOREACH_REVERSE(dp, &powerhook_list, sfd_list) {
677 #ifdef POWERHOOK_DEBUG
678 printf("dopowerhooks %s: %s (%p)\n", why_name, dp->sfd_name, dp);
679 #endif
680 (*dp->sfd_fn)(why, dp->sfd_arg);
681 }
682 } else {
683 CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list) {
684 #ifdef POWERHOOK_DEBUG
685 printf("dopowerhooks %s: %s (%p)\n", why_name, dp->sfd_name, dp);
686 #endif
687 (*dp->sfd_fn)(why, dp->sfd_arg);
688 }
689 }
690
691 #ifdef POWERHOOK_DEBUG
692 printf("dopowerhooks: %s done\n", why_name);
693 #endif
694 }
695
696 static int
697 isswap(struct device *dv)
698 {
699 struct dkwedge_info wi;
700 struct vnode *vn;
701 int error;
702
703 if (device_class(dv) != DV_DISK || !device_is_a(dv, "dk"))
704 return 0;
705
706 if ((vn = opendisk(dv)) == NULL)
707 return 0;
708
709 error = VOP_IOCTL(vn, DIOCGWEDGEINFO, &wi, FREAD, NOCRED);
710 VOP_CLOSE(vn, FREAD, NOCRED);
711 vput(vn);
712 if (error) {
713 #ifdef DEBUG_WEDGE
714 printf("%s: Get wedge info returned %d\n", device_xname(dv), error);
715 #endif
716 return 0;
717 }
718 return strcmp(wi.dkw_ptype, DKW_PTYPE_SWAP) == 0;
719 }
720
721 /*
722 * Determine the root device and, if instructed to, the root file system.
723 */
724
725 #include "md.h"
726
727 #if NMD > 0
728 static struct device fakemdrootdev[NMD];
729 extern struct cfdriver md_cd;
730 #endif
731
732 #ifdef MEMORY_DISK_IS_ROOT
733 int md_is_root = 1;
734 #else
735 int md_is_root = 0;
736 #endif
737
738 /*
739 * The device and wedge that we booted from. If booted_wedge is NULL,
740 * the we might consult booted_partition.
741 */
742 struct device *booted_device;
743 struct device *booted_wedge;
744 int booted_partition;
745
746 /*
747 * Use partition letters if it's a disk class but not a wedge.
748 * XXX Check for wedge is kinda gross.
749 */
750 #define DEV_USES_PARTITIONS(dv) \
751 (device_class((dv)) == DV_DISK && \
752 !device_is_a((dv), "dk"))
753
754 void
755 setroot(struct device *bootdv, int bootpartition)
756 {
757 struct device *dv;
758 int len, majdev;
759 dev_t nrootdev;
760 dev_t ndumpdev = NODEV;
761 char buf[128];
762 const char *rootdevname;
763 const char *dumpdevname;
764 struct device *rootdv = NULL; /* XXX gcc -Wuninitialized */
765 struct device *dumpdv = NULL;
766 struct ifnet *ifp;
767 const char *deffsname;
768 struct vfsops *vops;
769
770 #ifdef TFTPROOT
771 if (tftproot_dhcpboot(bootdv) != 0)
772 boothowto |= RB_ASKNAME;
773 #endif
774
775 #if NMD > 0
776 if (md_is_root) {
777 int i;
778 for (i = 0; i < NMD; i++) {
779 fakemdrootdev[i].dv_class = DV_DISK;
780 fakemdrootdev[i].dv_cfdata = NULL;
781 fakemdrootdev[i].dv_cfdriver = &md_cd;
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 bootdv = &fakemdrootdev[0];
788 bootpartition = 0;
789 }
790 #endif
791
792 /*
793 * If NFS is specified as the file system, and we found
794 * a DV_DISK boot device (or no boot device at all), then
795 * find a reasonable network interface for "rootspec".
796 */
797 vops = vfs_getopsbyname("nfs");
798 if (vops != NULL && vops->vfs_mountroot == mountroot &&
799 rootspec == NULL &&
800 (bootdv == NULL || device_class(bootdv) != DV_IFNET)) {
801 IFNET_FOREACH(ifp) {
802 if ((ifp->if_flags &
803 (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
804 break;
805 }
806 if (ifp == NULL) {
807 /*
808 * Can't find a suitable interface; ask the
809 * user.
810 */
811 boothowto |= RB_ASKNAME;
812 } else {
813 /*
814 * Have a suitable interface; behave as if
815 * the user specified this interface.
816 */
817 rootspec = (const char *)ifp->if_xname;
818 }
819 }
820 if (vops != NULL)
821 vfs_delref(vops);
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", device_xname(bootdv));
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, device_xname(bootdv), 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)", device_xname(defdumpdv));
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 vfs_delref(vops);
953 break;
954 }
955 }
956
957 } else if (rootspec == NULL) {
958 /*
959 * Wildcarded root; use the boot device.
960 */
961 rootdv = bootdv;
962
963 if (bootdv)
964 majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
965 else
966 majdev = -1;
967 if (majdev >= 0) {
968 /*
969 * Root is on a disk. `bootpartition' is root,
970 * unless the device does not use partitions.
971 */
972 if (DEV_USES_PARTITIONS(bootdv))
973 rootdev = MAKEDISKDEV(majdev,
974 device_unit(bootdv),
975 bootpartition);
976 else
977 rootdev = makedev(majdev, device_unit(bootdv));
978 }
979 } else {
980
981 /*
982 * `root on <dev> ...'
983 */
984
985 /*
986 * If it's a network interface, we can bail out
987 * early.
988 */
989 dv = finddevice(rootspec);
990 if (dv != NULL && device_class(dv) == DV_IFNET) {
991 rootdv = dv;
992 goto haveroot;
993 }
994
995 if (rootdev == NODEV &&
996 device_class(dv) == DV_DISK && device_is_a(dv, "dk") &&
997 (majdev = devsw_name2blk(device_xname(dv), NULL, 0)) >= 0)
998 rootdev = makedev(majdev, device_unit(dv));
999
1000 rootdevname = devsw_blk2name(major(rootdev));
1001 if (rootdevname == NULL) {
1002 printf("unknown device major 0x%x\n", rootdev);
1003 boothowto |= RB_ASKNAME;
1004 goto top;
1005 }
1006 memset(buf, 0, sizeof(buf));
1007 snprintf(buf, sizeof(buf), "%s%d", rootdevname,
1008 DISKUNIT(rootdev));
1009
1010 rootdv = finddevice(buf);
1011 if (rootdv == NULL) {
1012 printf("device %s (0x%x) not configured\n",
1013 buf, rootdev);
1014 boothowto |= RB_ASKNAME;
1015 goto top;
1016 }
1017 }
1018
1019 haveroot:
1020
1021 root_device = rootdv;
1022
1023 switch (device_class(rootdv)) {
1024 case DV_IFNET:
1025 case DV_DISK:
1026 aprint_normal("root on %s", device_xname(rootdv));
1027 if (DEV_USES_PARTITIONS(rootdv))
1028 aprint_normal("%c", DISKPART(rootdev) + 'a');
1029 break;
1030
1031 default:
1032 printf("can't determine root device\n");
1033 boothowto |= RB_ASKNAME;
1034 goto top;
1035 }
1036
1037 /*
1038 * Now configure the dump device.
1039 *
1040 * If we haven't figured out the dump device, do so, with
1041 * the following rules:
1042 *
1043 * (a) We already know dumpdv in the RB_ASKNAME case.
1044 *
1045 * (b) If dumpspec is set, try to use it. If the device
1046 * is not available, punt.
1047 *
1048 * (c) If dumpspec is not set, the dump device is
1049 * wildcarded or unspecified. If the root device
1050 * is DV_IFNET, punt. Otherwise, use partition b
1051 * of the root device.
1052 */
1053
1054 if (boothowto & RB_ASKNAME) { /* (a) */
1055 if (dumpdv == NULL)
1056 goto nodumpdev;
1057 } else if (dumpspec != NULL) { /* (b) */
1058 if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
1059 /*
1060 * Operator doesn't want a dump device.
1061 * Or looks like they tried to pick a network
1062 * device. Oops.
1063 */
1064 goto nodumpdev;
1065 }
1066
1067 dumpdevname = devsw_blk2name(major(dumpdev));
1068 if (dumpdevname == NULL)
1069 goto nodumpdev;
1070 memset(buf, 0, sizeof(buf));
1071 snprintf(buf, sizeof(buf), "%s%d", dumpdevname,
1072 DISKUNIT(dumpdev));
1073
1074 dumpdv = finddevice(buf);
1075 if (dumpdv == NULL) {
1076 /*
1077 * Device not configured.
1078 */
1079 goto nodumpdev;
1080 }
1081 } else { /* (c) */
1082 if (DEV_USES_PARTITIONS(rootdv) == 0) {
1083 for (dv = TAILQ_FIRST(&alldevs); dv != NULL;
1084 dv = TAILQ_NEXT(dv, dv_list))
1085 if (isswap(dv))
1086 break;
1087 if (dv == NULL)
1088 goto nodumpdev;
1089
1090 majdev = devsw_name2blk(device_xname(dv), NULL, 0);
1091 if (majdev < 0)
1092 goto nodumpdev;
1093 dumpdv = dv;
1094 dumpdev = makedev(majdev, device_unit(dumpdv));
1095 } else {
1096 dumpdv = rootdv;
1097 dumpdev = MAKEDISKDEV(major(rootdev),
1098 device_unit(dumpdv), 1);
1099 }
1100 }
1101
1102 dumpcdev = devsw_blk2chr(dumpdev);
1103 aprint_normal(" dumps on %s", device_xname(dumpdv));
1104 if (DEV_USES_PARTITIONS(dumpdv))
1105 aprint_normal("%c", DISKPART(dumpdev) + 'a');
1106 aprint_normal("\n");
1107 return;
1108
1109 nodumpdev:
1110 dumpdev = NODEV;
1111 dumpcdev = NODEV;
1112 aprint_normal("\n");
1113 }
1114
1115 static struct device *
1116 finddevice(const char *name)
1117 {
1118 const char *wname;
1119
1120 if ((wname = getwedgename(name, strlen(name))) != NULL)
1121 return dkwedge_find_by_wname(wname);
1122
1123 #if NMD > 0
1124 if (md_is_root) {
1125 int j;
1126 for (j = 0; j < NMD; j++) {
1127 if (strcmp(name, fakemdrootdev[j].dv_xname) == 0)
1128 return &fakemdrootdev[j];
1129 }
1130 }
1131 #endif
1132
1133 return device_find_by_xname(name);
1134 }
1135
1136 static struct device *
1137 getdisk(char *str, int len, int defpart, dev_t *devp, int isdump)
1138 {
1139 struct device *dv;
1140
1141 if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
1142 printf("use one of:");
1143 #if NMD > 0
1144 if (isdump == 0 && md_is_root) {
1145 int i;
1146 for (i = 0; i < NMD; i++)
1147 printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
1148 'a' + MAXPARTITIONS - 1);
1149 }
1150 #endif
1151 TAILQ_FOREACH(dv, &alldevs, dv_list) {
1152 if (DEV_USES_PARTITIONS(dv))
1153 printf(" %s[a-%c]", device_xname(dv),
1154 'a' + MAXPARTITIONS - 1);
1155 else if (device_class(dv) == DV_DISK)
1156 printf(" %s", device_xname(dv));
1157 if (isdump == 0 && device_class(dv) == DV_IFNET)
1158 printf(" %s", device_xname(dv));
1159 }
1160 dkwedge_print_wnames();
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 const char *
1172 getwedgename(const char *name, int namelen)
1173 {
1174 const char *wpfx = "wedge:";
1175 const int wpfxlen = strlen(wpfx);
1176
1177 if (namelen < wpfxlen || strncmp(name, wpfx, wpfxlen) != 0)
1178 return NULL;
1179
1180 return name + wpfxlen;
1181 }
1182
1183 static struct device *
1184 parsedisk(char *str, int len, int defpart, dev_t *devp)
1185 {
1186 struct device *dv;
1187 const char *wname;
1188 char *cp, c;
1189 int majdev, part;
1190 if (len == 0)
1191 return (NULL);
1192
1193 if (len == 4 && strcmp(str, "halt") == 0)
1194 cpu_reboot(RB_HALT, NULL);
1195 else if (len == 6 && strcmp(str, "reboot") == 0)
1196 cpu_reboot(0, NULL);
1197 #if defined(DDB)
1198 else if (len == 3 && strcmp(str, "ddb") == 0)
1199 console_debugger();
1200 #endif
1201
1202 cp = str + len - 1;
1203 c = *cp;
1204
1205 if ((wname = getwedgename(str, len)) != NULL) {
1206 if ((dv = dkwedge_find_by_wname(wname)) == NULL)
1207 return NULL;
1208 part = defpart;
1209 goto gotdisk;
1210 } else if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
1211 part = c - 'a';
1212 *cp = '\0';
1213 } else
1214 part = defpart;
1215
1216 #if NMD > 0
1217 if (md_is_root) {
1218 int i;
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 }
1225 }
1226 #endif
1227
1228 dv = finddevice(str);
1229 if (dv != NULL) {
1230 if (device_class(dv) == DV_DISK) {
1231 gotdisk:
1232 majdev = devsw_name2blk(device_xname(dv), 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 bool
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 PTRACE
1333 if (ISSET(p->p_slflag, PSL_SYSCALL))
1334 return (true);
1335 #endif
1336
1337 return (false);
1338 }
1339
1340 /*
1341 * Start trace of particular system call. If process is being traced,
1342 * this routine is called by MD syscall dispatch code just before
1343 * a system call is actually executed.
1344 */
1345 int
1346 trace_enter(register_t code, const register_t *args, int narg)
1347 {
1348 #ifdef SYSCALL_DEBUG
1349 scdebug_call(code, args);
1350 #endif /* SYSCALL_DEBUG */
1351
1352 ktrsyscall(code, args, narg);
1353
1354 #ifdef PTRACE
1355 if ((curlwp->l_proc->p_slflag & (PSL_SYSCALL|PSL_TRACED)) ==
1356 (PSL_SYSCALL|PSL_TRACED))
1357 process_stoptrace();
1358 #endif
1359 return 0;
1360 }
1361
1362 /*
1363 * End trace of particular system call. If process is being traced,
1364 * this routine is called by MD syscall dispatch code just after
1365 * a system call finishes.
1366 * MD caller guarantees the passed 'code' is within the supported
1367 * system call number range for emulation the process runs under.
1368 */
1369 void
1370 trace_exit(register_t code, register_t rval[], int error)
1371 {
1372 #ifdef SYSCALL_DEBUG
1373 scdebug_ret(code, error, rval);
1374 #endif /* SYSCALL_DEBUG */
1375
1376 ktrsysret(code, error, rval);
1377
1378 #ifdef PTRACE
1379 if ((curlwp->l_proc->p_slflag & (PSL_SYSCALL|PSL_TRACED)) ==
1380 (PSL_SYSCALL|PSL_TRACED))
1381 process_stoptrace();
1382 #endif
1383 }
1384