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