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