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