kern_subr.c revision 1.190 1 /* $NetBSD: kern_subr.c,v 1.190 2008/06/11 10:40:21 drochner 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.190 2008/06/11 10:40:21 drochner 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 while ((dp = LIST_FIRST(&shutdownhook_list)) != NULL) {
471 LIST_REMOVE(dp, hk_list);
472 (*dp->hk_fn)(dp->hk_arg);
473 #if 0
474 /*
475 * Don't bother freeing the hook structure,, since we may
476 * be rebooting because of a memory corruption problem,
477 * and this might only make things worse. It doesn't
478 * matter, anyway, since the system is just about to
479 * reboot.
480 */
481 free(dp, M_DEVBUF);
482 #endif
483 }
484
485 pmf_system_shutdown(boothowto);
486 }
487
488 /*
489 * "Mountroot hook" types, functions, and variables.
490 */
491
492 static hook_list_t mountroothook_list;
493
494 void *
495 mountroothook_establish(void (*fn)(struct device *), struct device *dev)
496 {
497 return hook_establish(&mountroothook_list, (void (*)(void *))fn, dev);
498 }
499
500 void
501 mountroothook_disestablish(void *vhook)
502 {
503 hook_disestablish(&mountroothook_list, vhook);
504 }
505
506 void
507 mountroothook_destroy(void)
508 {
509 hook_destroy(&mountroothook_list);
510 }
511
512 void
513 domountroothook(void)
514 {
515 struct hook_desc *hd;
516
517 LIST_FOREACH(hd, &mountroothook_list, hk_list) {
518 if (hd->hk_arg == (void *)root_device) {
519 (*hd->hk_fn)(hd->hk_arg);
520 return;
521 }
522 }
523 }
524
525 static hook_list_t exechook_list;
526
527 void *
528 exechook_establish(void (*fn)(struct proc *, void *), void *arg)
529 {
530 return hook_establish(&exechook_list, (void (*)(void *))fn, arg);
531 }
532
533 void
534 exechook_disestablish(void *vhook)
535 {
536 hook_disestablish(&exechook_list, vhook);
537 }
538
539 /*
540 * Run exec hooks.
541 */
542 void
543 doexechooks(struct proc *p)
544 {
545 hook_proc_run(&exechook_list, p);
546 }
547
548 static hook_list_t exithook_list;
549
550 void *
551 exithook_establish(void (*fn)(struct proc *, void *), void *arg)
552 {
553 return hook_establish(&exithook_list, (void (*)(void *))fn, arg);
554 }
555
556 void
557 exithook_disestablish(void *vhook)
558 {
559 hook_disestablish(&exithook_list, vhook);
560 }
561
562 /*
563 * Run exit hooks.
564 */
565 void
566 doexithooks(struct proc *p)
567 {
568 hook_proc_run(&exithook_list, p);
569 }
570
571 static hook_list_t forkhook_list;
572
573 void *
574 forkhook_establish(void (*fn)(struct proc *, struct proc *))
575 {
576 return hook_establish(&forkhook_list, (void (*)(void *))fn, NULL);
577 }
578
579 void
580 forkhook_disestablish(void *vhook)
581 {
582 hook_disestablish(&forkhook_list, vhook);
583 }
584
585 /*
586 * Run fork hooks.
587 */
588 void
589 doforkhooks(struct proc *p2, struct proc *p1)
590 {
591 struct hook_desc *hd;
592
593 LIST_FOREACH(hd, &forkhook_list, hk_list) {
594 ((void (*)(struct proc *, struct proc *))*hd->hk_fn)
595 (p2, p1);
596 }
597 }
598
599 /*
600 * "Power hook" types, functions, and variables.
601 * The list of power hooks is kept ordered with the last registered hook
602 * first.
603 * When running the hooks on power down the hooks are called in reverse
604 * registration order, when powering up in registration order.
605 */
606 struct powerhook_desc {
607 CIRCLEQ_ENTRY(powerhook_desc) sfd_list;
608 void (*sfd_fn)(int, void *);
609 void *sfd_arg;
610 char sfd_name[16];
611 };
612
613 static CIRCLEQ_HEAD(, powerhook_desc) powerhook_list =
614 CIRCLEQ_HEAD_INITIALIZER(powerhook_list);
615
616 void *
617 powerhook_establish(const char *name, void (*fn)(int, void *), void *arg)
618 {
619 struct powerhook_desc *ndp;
620
621 ndp = (struct powerhook_desc *)
622 malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
623 if (ndp == NULL)
624 return (NULL);
625
626 ndp->sfd_fn = fn;
627 ndp->sfd_arg = arg;
628 strlcpy(ndp->sfd_name, name, sizeof(ndp->sfd_name));
629 CIRCLEQ_INSERT_HEAD(&powerhook_list, ndp, sfd_list);
630
631 aprint_error("%s: WARNING: powerhook_establish is deprecated\n", name);
632 return (ndp);
633 }
634
635 void
636 powerhook_disestablish(void *vhook)
637 {
638 #ifdef DIAGNOSTIC
639 struct powerhook_desc *dp;
640
641 CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list)
642 if (dp == vhook)
643 goto found;
644 panic("powerhook_disestablish: hook %p not established", vhook);
645 found:
646 #endif
647
648 CIRCLEQ_REMOVE(&powerhook_list, (struct powerhook_desc *)vhook,
649 sfd_list);
650 free(vhook, M_DEVBUF);
651 }
652
653 /*
654 * Run power hooks.
655 */
656 void
657 dopowerhooks(int why)
658 {
659 struct powerhook_desc *dp;
660
661 #ifdef POWERHOOK_DEBUG
662 const char *why_name;
663 static const char * pwr_names[] = {PWR_NAMES};
664 why_name = why < __arraycount(pwr_names) ? pwr_names[why] : "???";
665 #endif
666
667 if (why == PWR_RESUME || why == PWR_SOFTRESUME) {
668 CIRCLEQ_FOREACH_REVERSE(dp, &powerhook_list, sfd_list) {
669 #ifdef POWERHOOK_DEBUG
670 printf("dopowerhooks %s: %s (%p)\n", why_name, dp->sfd_name, dp);
671 #endif
672 (*dp->sfd_fn)(why, dp->sfd_arg);
673 }
674 } else {
675 CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list) {
676 #ifdef POWERHOOK_DEBUG
677 printf("dopowerhooks %s: %s (%p)\n", why_name, dp->sfd_name, dp);
678 #endif
679 (*dp->sfd_fn)(why, dp->sfd_arg);
680 }
681 }
682
683 #ifdef POWERHOOK_DEBUG
684 printf("dopowerhooks: %s done\n", why_name);
685 #endif
686 }
687
688 static int
689 isswap(struct device *dv)
690 {
691 struct dkwedge_info wi;
692 struct vnode *vn;
693 int error;
694
695 if (device_class(dv) != DV_DISK || !device_is_a(dv, "dk"))
696 return 0;
697
698 if ((vn = opendisk(dv)) == NULL)
699 return 0;
700
701 error = VOP_IOCTL(vn, DIOCGWEDGEINFO, &wi, FREAD, NOCRED);
702 VOP_CLOSE(vn, FREAD, NOCRED);
703 vput(vn);
704 if (error) {
705 #ifdef DEBUG_WEDGE
706 printf("%s: Get wedge info returned %d\n", device_xname(dv), error);
707 #endif
708 return 0;
709 }
710 return strcmp(wi.dkw_ptype, DKW_PTYPE_SWAP) == 0;
711 }
712
713 /*
714 * Determine the root device and, if instructed to, the root file system.
715 */
716
717 #include "md.h"
718
719 #if NMD > 0
720 extern struct cfdriver md_cd;
721 #ifdef MEMORY_DISK_IS_ROOT
722 int md_is_root = 1;
723 #else
724 int md_is_root = 0;
725 #endif
726 #endif
727
728 /*
729 * The device and wedge that we booted from. If booted_wedge is NULL,
730 * the we might consult booted_partition.
731 */
732 struct device *booted_device;
733 struct device *booted_wedge;
734 int booted_partition;
735
736 /*
737 * Use partition letters if it's a disk class but not a wedge.
738 * XXX Check for wedge is kinda gross.
739 */
740 #define DEV_USES_PARTITIONS(dv) \
741 (device_class((dv)) == DV_DISK && \
742 !device_is_a((dv), "dk"))
743
744 void
745 setroot(struct device *bootdv, int bootpartition)
746 {
747 struct device *dv;
748 int len, majdev;
749 dev_t nrootdev;
750 dev_t ndumpdev = NODEV;
751 char buf[128];
752 const char *rootdevname;
753 const char *dumpdevname;
754 struct device *rootdv = NULL; /* XXX gcc -Wuninitialized */
755 struct device *dumpdv = NULL;
756 struct ifnet *ifp;
757 const char *deffsname;
758 struct vfsops *vops;
759
760 #ifdef TFTPROOT
761 if (tftproot_dhcpboot(bootdv) != 0)
762 boothowto |= RB_ASKNAME;
763 #endif
764
765 #if NMD > 0
766 if (md_is_root) {
767 /*
768 * XXX there should be "root on md0" in the config file,
769 * but it isn't always
770 */
771 bootdv = md_cd.cd_devs[0];
772 bootpartition = 0;
773 }
774 #endif
775
776 /*
777 * If NFS is specified as the file system, and we found
778 * a DV_DISK boot device (or no boot device at all), then
779 * find a reasonable network interface for "rootspec".
780 */
781 vops = vfs_getopsbyname("nfs");
782 if (vops != NULL && vops->vfs_mountroot == mountroot &&
783 rootspec == NULL &&
784 (bootdv == NULL || device_class(bootdv) != DV_IFNET)) {
785 IFNET_FOREACH(ifp) {
786 if ((ifp->if_flags &
787 (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
788 break;
789 }
790 if (ifp == NULL) {
791 /*
792 * Can't find a suitable interface; ask the
793 * user.
794 */
795 boothowto |= RB_ASKNAME;
796 } else {
797 /*
798 * Have a suitable interface; behave as if
799 * the user specified this interface.
800 */
801 rootspec = (const char *)ifp->if_xname;
802 }
803 }
804 if (vops != NULL)
805 vfs_delref(vops);
806
807 /*
808 * If wildcarded root and we the boot device wasn't determined,
809 * ask the user.
810 */
811 if (rootspec == NULL && bootdv == NULL)
812 boothowto |= RB_ASKNAME;
813
814 top:
815 if (boothowto & RB_ASKNAME) {
816 struct device *defdumpdv;
817
818 for (;;) {
819 printf("root device");
820 if (bootdv != NULL) {
821 printf(" (default %s", device_xname(bootdv));
822 if (DEV_USES_PARTITIONS(bootdv))
823 printf("%c", bootpartition + 'a');
824 printf(")");
825 }
826 printf(": ");
827 len = cngetsn(buf, sizeof(buf));
828 if (len == 0 && bootdv != NULL) {
829 strlcpy(buf, device_xname(bootdv), sizeof(buf));
830 len = strlen(buf);
831 }
832 if (len > 0 && buf[len - 1] == '*') {
833 buf[--len] = '\0';
834 dv = getdisk(buf, len, 1, &nrootdev, 0);
835 if (dv != NULL) {
836 rootdv = dv;
837 break;
838 }
839 }
840 dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
841 if (dv != NULL) {
842 rootdv = dv;
843 break;
844 }
845 }
846
847 /*
848 * Set up the default dump device. If root is on
849 * a network device, there is no default dump
850 * device, since we don't support dumps to the
851 * network.
852 */
853 if (DEV_USES_PARTITIONS(rootdv) == 0)
854 defdumpdv = NULL;
855 else
856 defdumpdv = rootdv;
857
858 for (;;) {
859 printf("dump device");
860 if (defdumpdv != NULL) {
861 /*
862 * Note, we know it's a disk if we get here.
863 */
864 printf(" (default %sb)", device_xname(defdumpdv));
865 }
866 printf(": ");
867 len = cngetsn(buf, sizeof(buf));
868 if (len == 0) {
869 if (defdumpdv != NULL) {
870 ndumpdev = MAKEDISKDEV(major(nrootdev),
871 DISKUNIT(nrootdev), 1);
872 }
873 dumpdv = defdumpdv;
874 break;
875 }
876 if (len == 4 && strcmp(buf, "none") == 0) {
877 dumpdv = NULL;
878 break;
879 }
880 dv = getdisk(buf, len, 1, &ndumpdev, 1);
881 if (dv != NULL) {
882 dumpdv = dv;
883 break;
884 }
885 }
886
887 rootdev = nrootdev;
888 dumpdev = ndumpdev;
889
890 for (vops = LIST_FIRST(&vfs_list); vops != NULL;
891 vops = LIST_NEXT(vops, vfs_list)) {
892 if (vops->vfs_mountroot != NULL &&
893 vops->vfs_mountroot == mountroot)
894 break;
895 }
896
897 if (vops == NULL) {
898 mountroot = NULL;
899 deffsname = "generic";
900 } else
901 deffsname = vops->vfs_name;
902
903 for (;;) {
904 printf("file system (default %s): ", deffsname);
905 len = cngetsn(buf, sizeof(buf));
906 if (len == 0)
907 break;
908 if (len == 4 && strcmp(buf, "halt") == 0)
909 cpu_reboot(RB_HALT, NULL);
910 else if (len == 6 && strcmp(buf, "reboot") == 0)
911 cpu_reboot(0, NULL);
912 #if defined(DDB)
913 else if (len == 3 && strcmp(buf, "ddb") == 0) {
914 console_debugger();
915 }
916 #endif
917 else if (len == 7 && strcmp(buf, "generic") == 0) {
918 mountroot = NULL;
919 break;
920 }
921 vops = vfs_getopsbyname(buf);
922 if (vops == NULL || vops->vfs_mountroot == NULL) {
923 printf("use one of: generic");
924 for (vops = LIST_FIRST(&vfs_list);
925 vops != NULL;
926 vops = LIST_NEXT(vops, vfs_list)) {
927 if (vops->vfs_mountroot != NULL)
928 printf(" %s", vops->vfs_name);
929 }
930 #if defined(DDB)
931 printf(" ddb");
932 #endif
933 printf(" halt reboot\n");
934 } else {
935 mountroot = vops->vfs_mountroot;
936 vfs_delref(vops);
937 break;
938 }
939 }
940
941 } else if (rootspec == NULL) {
942 /*
943 * Wildcarded root; use the boot device.
944 */
945 rootdv = bootdv;
946
947 if (bootdv)
948 majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
949 else
950 majdev = -1;
951 if (majdev >= 0) {
952 /*
953 * Root is on a disk. `bootpartition' is root,
954 * unless the device does not use partitions.
955 */
956 if (DEV_USES_PARTITIONS(bootdv))
957 rootdev = MAKEDISKDEV(majdev,
958 device_unit(bootdv),
959 bootpartition);
960 else
961 rootdev = makedev(majdev, device_unit(bootdv));
962 }
963 } else {
964
965 /*
966 * `root on <dev> ...'
967 */
968
969 /*
970 * If it's a network interface, we can bail out
971 * early.
972 */
973 dv = finddevice(rootspec);
974 if (dv != NULL && device_class(dv) == DV_IFNET) {
975 rootdv = dv;
976 goto haveroot;
977 }
978
979 if (rootdev == NODEV &&
980 device_class(dv) == DV_DISK && device_is_a(dv, "dk") &&
981 (majdev = devsw_name2blk(device_xname(dv), NULL, 0)) >= 0)
982 rootdev = makedev(majdev, device_unit(dv));
983
984 rootdevname = devsw_blk2name(major(rootdev));
985 if (rootdevname == NULL) {
986 printf("unknown device major 0x%x\n", rootdev);
987 boothowto |= RB_ASKNAME;
988 goto top;
989 }
990 memset(buf, 0, sizeof(buf));
991 snprintf(buf, sizeof(buf), "%s%d", rootdevname,
992 DISKUNIT(rootdev));
993
994 rootdv = finddevice(buf);
995 if (rootdv == NULL) {
996 printf("device %s (0x%x) not configured\n",
997 buf, rootdev);
998 boothowto |= RB_ASKNAME;
999 goto top;
1000 }
1001 }
1002
1003 haveroot:
1004
1005 root_device = rootdv;
1006
1007 switch (device_class(rootdv)) {
1008 case DV_IFNET:
1009 case DV_DISK:
1010 aprint_normal("root on %s", device_xname(rootdv));
1011 if (DEV_USES_PARTITIONS(rootdv))
1012 aprint_normal("%c", DISKPART(rootdev) + 'a');
1013 break;
1014
1015 default:
1016 printf("can't determine root device\n");
1017 boothowto |= RB_ASKNAME;
1018 goto top;
1019 }
1020
1021 /*
1022 * Now configure the dump device.
1023 *
1024 * If we haven't figured out the dump device, do so, with
1025 * the following rules:
1026 *
1027 * (a) We already know dumpdv in the RB_ASKNAME case.
1028 *
1029 * (b) If dumpspec is set, try to use it. If the device
1030 * is not available, punt.
1031 *
1032 * (c) If dumpspec is not set, the dump device is
1033 * wildcarded or unspecified. If the root device
1034 * is DV_IFNET, punt. Otherwise, use partition b
1035 * of the root device.
1036 */
1037
1038 if (boothowto & RB_ASKNAME) { /* (a) */
1039 if (dumpdv == NULL)
1040 goto nodumpdev;
1041 } else if (dumpspec != NULL) { /* (b) */
1042 if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
1043 /*
1044 * Operator doesn't want a dump device.
1045 * Or looks like they tried to pick a network
1046 * device. Oops.
1047 */
1048 goto nodumpdev;
1049 }
1050
1051 dumpdevname = devsw_blk2name(major(dumpdev));
1052 if (dumpdevname == NULL)
1053 goto nodumpdev;
1054 memset(buf, 0, sizeof(buf));
1055 snprintf(buf, sizeof(buf), "%s%d", dumpdevname,
1056 DISKUNIT(dumpdev));
1057
1058 dumpdv = finddevice(buf);
1059 if (dumpdv == NULL) {
1060 /*
1061 * Device not configured.
1062 */
1063 goto nodumpdev;
1064 }
1065 } else { /* (c) */
1066 if (DEV_USES_PARTITIONS(rootdv) == 0) {
1067 for (dv = TAILQ_FIRST(&alldevs); dv != NULL;
1068 dv = TAILQ_NEXT(dv, dv_list))
1069 if (isswap(dv))
1070 break;
1071 if (dv == NULL)
1072 goto nodumpdev;
1073
1074 majdev = devsw_name2blk(device_xname(dv), NULL, 0);
1075 if (majdev < 0)
1076 goto nodumpdev;
1077 dumpdv = dv;
1078 dumpdev = makedev(majdev, device_unit(dumpdv));
1079 } else {
1080 dumpdv = rootdv;
1081 dumpdev = MAKEDISKDEV(major(rootdev),
1082 device_unit(dumpdv), 1);
1083 }
1084 }
1085
1086 dumpcdev = devsw_blk2chr(dumpdev);
1087 aprint_normal(" dumps on %s", device_xname(dumpdv));
1088 if (DEV_USES_PARTITIONS(dumpdv))
1089 aprint_normal("%c", DISKPART(dumpdev) + 'a');
1090 aprint_normal("\n");
1091 return;
1092
1093 nodumpdev:
1094 dumpdev = NODEV;
1095 dumpcdev = NODEV;
1096 aprint_normal("\n");
1097 }
1098
1099 static struct device *
1100 finddevice(const char *name)
1101 {
1102 const char *wname;
1103
1104 if ((wname = getwedgename(name, strlen(name))) != NULL)
1105 return dkwedge_find_by_wname(wname);
1106
1107 return device_find_by_xname(name);
1108 }
1109
1110 static struct device *
1111 getdisk(char *str, int len, int defpart, dev_t *devp, int isdump)
1112 {
1113 struct device *dv;
1114
1115 if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
1116 printf("use one of:");
1117 TAILQ_FOREACH(dv, &alldevs, dv_list) {
1118 if (DEV_USES_PARTITIONS(dv))
1119 printf(" %s[a-%c]", device_xname(dv),
1120 'a' + MAXPARTITIONS - 1);
1121 else if (device_class(dv) == DV_DISK)
1122 printf(" %s", device_xname(dv));
1123 if (isdump == 0 && device_class(dv) == DV_IFNET)
1124 printf(" %s", device_xname(dv));
1125 }
1126 dkwedge_print_wnames();
1127 if (isdump)
1128 printf(" none");
1129 #if defined(DDB)
1130 printf(" ddb");
1131 #endif
1132 printf(" halt reboot\n");
1133 }
1134 return dv;
1135 }
1136
1137 static const char *
1138 getwedgename(const char *name, int namelen)
1139 {
1140 const char *wpfx = "wedge:";
1141 const int wpfxlen = strlen(wpfx);
1142
1143 if (namelen < wpfxlen || strncmp(name, wpfx, wpfxlen) != 0)
1144 return NULL;
1145
1146 return name + wpfxlen;
1147 }
1148
1149 static struct device *
1150 parsedisk(char *str, int len, int defpart, dev_t *devp)
1151 {
1152 struct device *dv;
1153 const char *wname;
1154 char *cp, c;
1155 int majdev, part;
1156 if (len == 0)
1157 return (NULL);
1158
1159 if (len == 4 && strcmp(str, "halt") == 0)
1160 cpu_reboot(RB_HALT, NULL);
1161 else if (len == 6 && strcmp(str, "reboot") == 0)
1162 cpu_reboot(0, NULL);
1163 #if defined(DDB)
1164 else if (len == 3 && strcmp(str, "ddb") == 0)
1165 console_debugger();
1166 #endif
1167
1168 cp = str + len - 1;
1169 c = *cp;
1170
1171 if ((wname = getwedgename(str, len)) != NULL) {
1172 if ((dv = dkwedge_find_by_wname(wname)) == NULL)
1173 return NULL;
1174 part = defpart;
1175 goto gotdisk;
1176 } else if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
1177 part = c - 'a';
1178 *cp = '\0';
1179 } else
1180 part = defpart;
1181
1182 dv = finddevice(str);
1183 if (dv != NULL) {
1184 if (device_class(dv) == DV_DISK) {
1185 gotdisk:
1186 majdev = devsw_name2blk(device_xname(dv), NULL, 0);
1187 if (majdev < 0)
1188 panic("parsedisk");
1189 if (DEV_USES_PARTITIONS(dv))
1190 *devp = MAKEDISKDEV(majdev, device_unit(dv),
1191 part);
1192 else
1193 *devp = makedev(majdev, device_unit(dv));
1194 }
1195
1196 if (device_class(dv) == DV_IFNET)
1197 *devp = NODEV;
1198 }
1199
1200 *cp = c;
1201 return (dv);
1202 }
1203
1204 /*
1205 * snprintf() `bytes' into `buf', reformatting it so that the number,
1206 * plus a possible `x' + suffix extension) fits into len bytes (including
1207 * the terminating NUL).
1208 * Returns the number of bytes stored in buf, or -1 if there was a problem.
1209 * E.g, given a len of 9 and a suffix of `B':
1210 * bytes result
1211 * ----- ------
1212 * 99999 `99999 B'
1213 * 100000 `97 kB'
1214 * 66715648 `65152 kB'
1215 * 252215296 `240 MB'
1216 */
1217 int
1218 humanize_number(char *buf, size_t len, uint64_t bytes, const char *suffix,
1219 int divisor)
1220 {
1221 /* prefixes are: (none), kilo, Mega, Giga, Tera, Peta, Exa */
1222 const char *prefixes;
1223 int r;
1224 uint64_t umax;
1225 size_t i, suffixlen;
1226
1227 if (buf == NULL || suffix == NULL)
1228 return (-1);
1229 if (len > 0)
1230 buf[0] = '\0';
1231 suffixlen = strlen(suffix);
1232 /* check if enough room for `x y' + suffix + `\0' */
1233 if (len < 4 + suffixlen)
1234 return (-1);
1235
1236 if (divisor == 1024) {
1237 /*
1238 * binary multiplies
1239 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
1240 */
1241 prefixes = " KMGTPE";
1242 } else
1243 prefixes = " kMGTPE"; /* SI for decimal multiplies */
1244
1245 umax = 1;
1246 for (i = 0; i < len - suffixlen - 3; i++)
1247 umax *= 10;
1248 for (i = 0; bytes >= umax && prefixes[i + 1]; i++)
1249 bytes /= divisor;
1250
1251 r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
1252 i == 0 ? "" : " ", prefixes[i], suffix);
1253
1254 return (r);
1255 }
1256
1257 int
1258 format_bytes(char *buf, size_t len, uint64_t bytes)
1259 {
1260 int rv;
1261 size_t nlen;
1262
1263 rv = humanize_number(buf, len, bytes, "B", 1024);
1264 if (rv != -1) {
1265 /* nuke the trailing ` B' if it exists */
1266 nlen = strlen(buf) - 2;
1267 if (strcmp(&buf[nlen], " B") == 0)
1268 buf[nlen] = '\0';
1269 }
1270 return (rv);
1271 }
1272
1273 /*
1274 * Return true if system call tracing is enabled for the specified process.
1275 */
1276 bool
1277 trace_is_enabled(struct proc *p)
1278 {
1279 #ifdef SYSCALL_DEBUG
1280 return (true);
1281 #endif
1282 #ifdef KTRACE
1283 if (ISSET(p->p_traceflag, (KTRFAC_SYSCALL | KTRFAC_SYSRET)))
1284 return (true);
1285 #endif
1286 #ifdef PTRACE
1287 if (ISSET(p->p_slflag, PSL_SYSCALL))
1288 return (true);
1289 #endif
1290
1291 return (false);
1292 }
1293
1294 /*
1295 * Start trace of particular system call. If process is being traced,
1296 * this routine is called by MD syscall dispatch code just before
1297 * a system call is actually executed.
1298 */
1299 int
1300 trace_enter(register_t code, const register_t *args, int narg)
1301 {
1302 #ifdef SYSCALL_DEBUG
1303 scdebug_call(code, args);
1304 #endif /* SYSCALL_DEBUG */
1305
1306 ktrsyscall(code, args, narg);
1307
1308 #ifdef PTRACE
1309 if ((curlwp->l_proc->p_slflag & (PSL_SYSCALL|PSL_TRACED)) ==
1310 (PSL_SYSCALL|PSL_TRACED))
1311 process_stoptrace();
1312 #endif
1313 return 0;
1314 }
1315
1316 /*
1317 * End trace of particular system call. If process is being traced,
1318 * this routine is called by MD syscall dispatch code just after
1319 * a system call finishes.
1320 * MD caller guarantees the passed 'code' is within the supported
1321 * system call number range for emulation the process runs under.
1322 */
1323 void
1324 trace_exit(register_t code, register_t rval[], int error)
1325 {
1326 #ifdef SYSCALL_DEBUG
1327 scdebug_ret(code, error, rval);
1328 #endif /* SYSCALL_DEBUG */
1329
1330 ktrsysret(code, error, rval);
1331
1332 #ifdef PTRACE
1333 if ((curlwp->l_proc->p_slflag & (PSL_SYSCALL|PSL_TRACED)) ==
1334 (PSL_SYSCALL|PSL_TRACED))
1335 process_stoptrace();
1336 #endif
1337 }
1338