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