1 /* $NetBSD: kern_subr.c,v 1.187 2008/05/02 13:02:31 ad 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.187 2008/05/02 13:02:31 ad 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 727 #if NMD > 0 728 static struct device fakemdrootdev[NMD]; 729 extern struct cfdriver md_cd; 730 #endif 731 732 #ifdef MEMORY_DISK_IS_ROOT 733 int md_is_root = 1; 734 #else 735 int md_is_root = 0; 736 #endif 737 738 /* 739 * The device and wedge that we booted from. If booted_wedge is NULL, 740 * the we might consult booted_partition. 741 */ 742 struct device *booted_device; 743 struct device *booted_wedge; 744 int booted_partition; 745 746 /* 747 * Use partition letters if it's a disk class but not a wedge. 748 * XXX Check for wedge is kinda gross. 749 */ 750 #define DEV_USES_PARTITIONS(dv) \ 751 (device_class((dv)) == DV_DISK && \ 752 !device_is_a((dv), "dk")) 753 754 void 755 setroot(struct device *bootdv, int bootpartition) 756 { 757 struct device *dv; 758 int len, majdev; 759 dev_t nrootdev; 760 dev_t ndumpdev = NODEV; 761 char buf[128]; 762 const char *rootdevname; 763 const char *dumpdevname; 764 struct device *rootdv = NULL; /* XXX gcc -Wuninitialized */ 765 struct device *dumpdv = NULL; 766 struct ifnet *ifp; 767 const char *deffsname; 768 struct vfsops *vops; 769 770 #ifdef TFTPROOT 771 if (tftproot_dhcpboot(bootdv) != 0) 772 boothowto |= RB_ASKNAME; 773 #endif 774 775 #if NMD > 0 776 if (md_is_root) { 777 int i; 778 for (i = 0; i < NMD; i++) { 779 fakemdrootdev[i].dv_class = DV_DISK; 780 fakemdrootdev[i].dv_cfdata = NULL; 781 fakemdrootdev[i].dv_cfdriver = &md_cd; 782 fakemdrootdev[i].dv_unit = i; 783 fakemdrootdev[i].dv_parent = NULL; 784 snprintf(fakemdrootdev[i].dv_xname, 785 sizeof(fakemdrootdev[i].dv_xname), "md%d", i); 786 } 787 bootdv = &fakemdrootdev[0]; 788 bootpartition = 0; 789 } 790 #endif 791 792 /* 793 * If NFS is specified as the file system, and we found 794 * a DV_DISK boot device (or no boot device at all), then 795 * find a reasonable network interface for "rootspec". 796 */ 797 vops = vfs_getopsbyname("nfs"); 798 if (vops != NULL && vops->vfs_mountroot == mountroot && 799 rootspec == NULL && 800 (bootdv == NULL || device_class(bootdv) != DV_IFNET)) { 801 IFNET_FOREACH(ifp) { 802 if ((ifp->if_flags & 803 (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0) 804 break; 805 } 806 if (ifp == NULL) { 807 /* 808 * Can't find a suitable interface; ask the 809 * user. 810 */ 811 boothowto |= RB_ASKNAME; 812 } else { 813 /* 814 * Have a suitable interface; behave as if 815 * the user specified this interface. 816 */ 817 rootspec = (const char *)ifp->if_xname; 818 } 819 } 820 if (vops != NULL) 821 vfs_delref(vops); 822 823 /* 824 * If wildcarded root and we the boot device wasn't determined, 825 * ask the user. 826 */ 827 if (rootspec == NULL && bootdv == NULL) 828 boothowto |= RB_ASKNAME; 829 830 top: 831 if (boothowto & RB_ASKNAME) { 832 struct device *defdumpdv; 833 834 for (;;) { 835 printf("root device"); 836 if (bootdv != NULL) { 837 printf(" (default %s", device_xname(bootdv)); 838 if (DEV_USES_PARTITIONS(bootdv)) 839 printf("%c", bootpartition + 'a'); 840 printf(")"); 841 } 842 printf(": "); 843 len = cngetsn(buf, sizeof(buf)); 844 if (len == 0 && bootdv != NULL) { 845 strlcpy(buf, device_xname(bootdv), sizeof(buf)); 846 len = strlen(buf); 847 } 848 if (len > 0 && buf[len - 1] == '*') { 849 buf[--len] = '\0'; 850 dv = getdisk(buf, len, 1, &nrootdev, 0); 851 if (dv != NULL) { 852 rootdv = dv; 853 break; 854 } 855 } 856 dv = getdisk(buf, len, bootpartition, &nrootdev, 0); 857 if (dv != NULL) { 858 rootdv = dv; 859 break; 860 } 861 } 862 863 /* 864 * Set up the default dump device. If root is on 865 * a network device, there is no default dump 866 * device, since we don't support dumps to the 867 * network. 868 */ 869 if (DEV_USES_PARTITIONS(rootdv) == 0) 870 defdumpdv = NULL; 871 else 872 defdumpdv = rootdv; 873 874 for (;;) { 875 printf("dump device"); 876 if (defdumpdv != NULL) { 877 /* 878 * Note, we know it's a disk if we get here. 879 */ 880 printf(" (default %sb)", device_xname(defdumpdv)); 881 } 882 printf(": "); 883 len = cngetsn(buf, sizeof(buf)); 884 if (len == 0) { 885 if (defdumpdv != NULL) { 886 ndumpdev = MAKEDISKDEV(major(nrootdev), 887 DISKUNIT(nrootdev), 1); 888 } 889 dumpdv = defdumpdv; 890 break; 891 } 892 if (len == 4 && strcmp(buf, "none") == 0) { 893 dumpdv = NULL; 894 break; 895 } 896 dv = getdisk(buf, len, 1, &ndumpdev, 1); 897 if (dv != NULL) { 898 dumpdv = dv; 899 break; 900 } 901 } 902 903 rootdev = nrootdev; 904 dumpdev = ndumpdev; 905 906 for (vops = LIST_FIRST(&vfs_list); vops != NULL; 907 vops = LIST_NEXT(vops, vfs_list)) { 908 if (vops->vfs_mountroot != NULL && 909 vops->vfs_mountroot == mountroot) 910 break; 911 } 912 913 if (vops == NULL) { 914 mountroot = NULL; 915 deffsname = "generic"; 916 } else 917 deffsname = vops->vfs_name; 918 919 for (;;) { 920 printf("file system (default %s): ", deffsname); 921 len = cngetsn(buf, sizeof(buf)); 922 if (len == 0) 923 break; 924 if (len == 4 && strcmp(buf, "halt") == 0) 925 cpu_reboot(RB_HALT, NULL); 926 else if (len == 6 && strcmp(buf, "reboot") == 0) 927 cpu_reboot(0, NULL); 928 #if defined(DDB) 929 else if (len == 3 && strcmp(buf, "ddb") == 0) { 930 console_debugger(); 931 } 932 #endif 933 else if (len == 7 && strcmp(buf, "generic") == 0) { 934 mountroot = NULL; 935 break; 936 } 937 vops = vfs_getopsbyname(buf); 938 if (vops == NULL || vops->vfs_mountroot == NULL) { 939 printf("use one of: generic"); 940 for (vops = LIST_FIRST(&vfs_list); 941 vops != NULL; 942 vops = LIST_NEXT(vops, vfs_list)) { 943 if (vops->vfs_mountroot != NULL) 944 printf(" %s", vops->vfs_name); 945 } 946 #if defined(DDB) 947 printf(" ddb"); 948 #endif 949 printf(" halt reboot\n"); 950 } else { 951 mountroot = vops->vfs_mountroot; 952 vfs_delref(vops); 953 break; 954 } 955 } 956 957 } else if (rootspec == NULL) { 958 /* 959 * Wildcarded root; use the boot device. 960 */ 961 rootdv = bootdv; 962 963 majdev = devsw_name2blk(device_xname(bootdv), NULL, 0); 964 if (majdev >= 0) { 965 /* 966 * Root is on a disk. `bootpartition' is root, 967 * unless the device does not use partitions. 968 */ 969 if (DEV_USES_PARTITIONS(bootdv)) 970 rootdev = MAKEDISKDEV(majdev, 971 device_unit(bootdv), 972 bootpartition); 973 else 974 rootdev = makedev(majdev, device_unit(bootdv)); 975 } 976 } else { 977 978 /* 979 * `root on <dev> ...' 980 */ 981 982 /* 983 * If it's a network interface, we can bail out 984 * early. 985 */ 986 dv = finddevice(rootspec); 987 if (dv != NULL && device_class(dv) == DV_IFNET) { 988 rootdv = dv; 989 goto haveroot; 990 } 991 992 if (rootdev == NODEV && 993 device_class(dv) == DV_DISK && device_is_a(dv, "dk") && 994 (majdev = devsw_name2blk(device_xname(dv), NULL, 0)) >= 0) 995 rootdev = makedev(majdev, device_unit(dv)); 996 997 rootdevname = devsw_blk2name(major(rootdev)); 998 if (rootdevname == NULL) { 999 printf("unknown device major 0x%x\n", rootdev); 1000 boothowto |= RB_ASKNAME; 1001 goto top; 1002 } 1003 memset(buf, 0, sizeof(buf)); 1004 snprintf(buf, sizeof(buf), "%s%d", rootdevname, 1005 DISKUNIT(rootdev)); 1006 1007 rootdv = finddevice(buf); 1008 if (rootdv == NULL) { 1009 printf("device %s (0x%x) not configured\n", 1010 buf, rootdev); 1011 boothowto |= RB_ASKNAME; 1012 goto top; 1013 } 1014 } 1015 1016 haveroot: 1017 1018 root_device = rootdv; 1019 1020 switch (device_class(rootdv)) { 1021 case DV_IFNET: 1022 case DV_DISK: 1023 aprint_normal("root on %s", device_xname(rootdv)); 1024 if (DEV_USES_PARTITIONS(rootdv)) 1025 aprint_normal("%c", DISKPART(rootdev) + 'a'); 1026 break; 1027 1028 default: 1029 printf("can't determine root device\n"); 1030 boothowto |= RB_ASKNAME; 1031 goto top; 1032 } 1033 1034 /* 1035 * Now configure the dump device. 1036 * 1037 * If we haven't figured out the dump device, do so, with 1038 * the following rules: 1039 * 1040 * (a) We already know dumpdv in the RB_ASKNAME case. 1041 * 1042 * (b) If dumpspec is set, try to use it. If the device 1043 * is not available, punt. 1044 * 1045 * (c) If dumpspec is not set, the dump device is 1046 * wildcarded or unspecified. If the root device 1047 * is DV_IFNET, punt. Otherwise, use partition b 1048 * of the root device. 1049 */ 1050 1051 if (boothowto & RB_ASKNAME) { /* (a) */ 1052 if (dumpdv == NULL) 1053 goto nodumpdev; 1054 } else if (dumpspec != NULL) { /* (b) */ 1055 if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) { 1056 /* 1057 * Operator doesn't want a dump device. 1058 * Or looks like they tried to pick a network 1059 * device. Oops. 1060 */ 1061 goto nodumpdev; 1062 } 1063 1064 dumpdevname = devsw_blk2name(major(dumpdev)); 1065 if (dumpdevname == NULL) 1066 goto nodumpdev; 1067 memset(buf, 0, sizeof(buf)); 1068 snprintf(buf, sizeof(buf), "%s%d", dumpdevname, 1069 DISKUNIT(dumpdev)); 1070 1071 dumpdv = finddevice(buf); 1072 if (dumpdv == NULL) { 1073 /* 1074 * Device not configured. 1075 */ 1076 goto nodumpdev; 1077 } 1078 } else { /* (c) */ 1079 if (DEV_USES_PARTITIONS(rootdv) == 0) { 1080 for (dv = TAILQ_FIRST(&alldevs); dv != NULL; 1081 dv = TAILQ_NEXT(dv, dv_list)) 1082 if (isswap(dv)) 1083 break; 1084 if (dv == NULL) 1085 goto nodumpdev; 1086 1087 majdev = devsw_name2blk(device_xname(dv), NULL, 0); 1088 if (majdev < 0) 1089 goto nodumpdev; 1090 dumpdv = dv; 1091 dumpdev = makedev(majdev, device_unit(dumpdv)); 1092 } else { 1093 dumpdv = rootdv; 1094 dumpdev = MAKEDISKDEV(major(rootdev), 1095 device_unit(dumpdv), 1); 1096 } 1097 } 1098 1099 dumpcdev = devsw_blk2chr(dumpdev); 1100 aprint_normal(" dumps on %s", device_xname(dumpdv)); 1101 if (DEV_USES_PARTITIONS(dumpdv)) 1102 aprint_normal("%c", DISKPART(dumpdev) + 'a'); 1103 aprint_normal("\n"); 1104 return; 1105 1106 nodumpdev: 1107 dumpdev = NODEV; 1108 dumpcdev = NODEV; 1109 aprint_normal("\n"); 1110 } 1111 1112 static struct device * 1113 finddevice(const char *name) 1114 { 1115 const char *wname; 1116 1117 if ((wname = getwedgename(name, strlen(name))) != NULL) 1118 return dkwedge_find_by_wname(wname); 1119 1120 #if NMD > 0 1121 if (md_is_root) { 1122 int j; 1123 for (j = 0; j < NMD; j++) { 1124 if (strcmp(name, fakemdrootdev[j].dv_xname) == 0) 1125 return &fakemdrootdev[j]; 1126 } 1127 } 1128 #endif 1129 1130 return device_find_by_xname(name); 1131 } 1132 1133 static struct device * 1134 getdisk(char *str, int len, int defpart, dev_t *devp, int isdump) 1135 { 1136 struct device *dv; 1137 1138 if ((dv = parsedisk(str, len, defpart, devp)) == NULL) { 1139 printf("use one of:"); 1140 #if NMD > 0 1141 if (isdump == 0 && md_is_root) { 1142 int i; 1143 for (i = 0; i < NMD; i++) 1144 printf(" %s[a-%c]", fakemdrootdev[i].dv_xname, 1145 'a' + MAXPARTITIONS - 1); 1146 } 1147 #endif 1148 TAILQ_FOREACH(dv, &alldevs, dv_list) { 1149 if (DEV_USES_PARTITIONS(dv)) 1150 printf(" %s[a-%c]", device_xname(dv), 1151 'a' + MAXPARTITIONS - 1); 1152 else if (device_class(dv) == DV_DISK) 1153 printf(" %s", device_xname(dv)); 1154 if (isdump == 0 && device_class(dv) == DV_IFNET) 1155 printf(" %s", device_xname(dv)); 1156 } 1157 dkwedge_print_wnames(); 1158 if (isdump) 1159 printf(" none"); 1160 #if defined(DDB) 1161 printf(" ddb"); 1162 #endif 1163 printf(" halt reboot\n"); 1164 } 1165 return dv; 1166 } 1167 1168 static const char * 1169 getwedgename(const char *name, int namelen) 1170 { 1171 const char *wpfx = "wedge:"; 1172 const int wpfxlen = strlen(wpfx); 1173 1174 if (namelen < wpfxlen || strncmp(name, wpfx, wpfxlen) != 0) 1175 return NULL; 1176 1177 return name + wpfxlen; 1178 } 1179 1180 static struct device * 1181 parsedisk(char *str, int len, int defpart, dev_t *devp) 1182 { 1183 struct device *dv; 1184 const char *wname; 1185 char *cp, c; 1186 int majdev, part; 1187 if (len == 0) 1188 return (NULL); 1189 1190 if (len == 4 && strcmp(str, "halt") == 0) 1191 cpu_reboot(RB_HALT, NULL); 1192 else if (len == 6 && strcmp(str, "reboot") == 0) 1193 cpu_reboot(0, NULL); 1194 #if defined(DDB) 1195 else if (len == 3 && strcmp(str, "ddb") == 0) 1196 console_debugger(); 1197 #endif 1198 1199 cp = str + len - 1; 1200 c = *cp; 1201 1202 if ((wname = getwedgename(str, len)) != NULL) { 1203 if ((dv = dkwedge_find_by_wname(wname)) == NULL) 1204 return NULL; 1205 part = defpart; 1206 goto gotdisk; 1207 } else if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) { 1208 part = c - 'a'; 1209 *cp = '\0'; 1210 } else 1211 part = defpart; 1212 1213 #if NMD > 0 1214 if (md_is_root) { 1215 int i; 1216 for (i = 0; i < NMD; i++) { 1217 if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) { 1218 dv = &fakemdrootdev[i]; 1219 goto gotdisk; 1220 } 1221 } 1222 } 1223 #endif 1224 1225 dv = finddevice(str); 1226 if (dv != NULL) { 1227 if (device_class(dv) == DV_DISK) { 1228 gotdisk: 1229 majdev = devsw_name2blk(device_xname(dv), NULL, 0); 1230 if (majdev < 0) 1231 panic("parsedisk"); 1232 if (DEV_USES_PARTITIONS(dv)) 1233 *devp = MAKEDISKDEV(majdev, device_unit(dv), 1234 part); 1235 else 1236 *devp = makedev(majdev, device_unit(dv)); 1237 } 1238 1239 if (device_class(dv) == DV_IFNET) 1240 *devp = NODEV; 1241 } 1242 1243 *cp = c; 1244 return (dv); 1245 } 1246 1247 /* 1248 * snprintf() `bytes' into `buf', reformatting it so that the number, 1249 * plus a possible `x' + suffix extension) fits into len bytes (including 1250 * the terminating NUL). 1251 * Returns the number of bytes stored in buf, or -1 if there was a problem. 1252 * E.g, given a len of 9 and a suffix of `B': 1253 * bytes result 1254 * ----- ------ 1255 * 99999 `99999 B' 1256 * 100000 `97 kB' 1257 * 66715648 `65152 kB' 1258 * 252215296 `240 MB' 1259 */ 1260 int 1261 humanize_number(char *buf, size_t len, uint64_t bytes, const char *suffix, 1262 int divisor) 1263 { 1264 /* prefixes are: (none), kilo, Mega, Giga, Tera, Peta, Exa */ 1265 const char *prefixes; 1266 int r; 1267 uint64_t umax; 1268 size_t i, suffixlen; 1269 1270 if (buf == NULL || suffix == NULL) 1271 return (-1); 1272 if (len > 0) 1273 buf[0] = '\0'; 1274 suffixlen = strlen(suffix); 1275 /* check if enough room for `x y' + suffix + `\0' */ 1276 if (len < 4 + suffixlen) 1277 return (-1); 1278 1279 if (divisor == 1024) { 1280 /* 1281 * binary multiplies 1282 * XXX IEC 60027-2 recommends Ki, Mi, Gi... 1283 */ 1284 prefixes = " KMGTPE"; 1285 } else 1286 prefixes = " kMGTPE"; /* SI for decimal multiplies */ 1287 1288 umax = 1; 1289 for (i = 0; i < len - suffixlen - 3; i++) 1290 umax *= 10; 1291 for (i = 0; bytes >= umax && prefixes[i + 1]; i++) 1292 bytes /= divisor; 1293 1294 r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes, 1295 i == 0 ? "" : " ", prefixes[i], suffix); 1296 1297 return (r); 1298 } 1299 1300 int 1301 format_bytes(char *buf, size_t len, uint64_t bytes) 1302 { 1303 int rv; 1304 size_t nlen; 1305 1306 rv = humanize_number(buf, len, bytes, "B", 1024); 1307 if (rv != -1) { 1308 /* nuke the trailing ` B' if it exists */ 1309 nlen = strlen(buf) - 2; 1310 if (strcmp(&buf[nlen], " B") == 0) 1311 buf[nlen] = '\0'; 1312 } 1313 return (rv); 1314 } 1315 1316 /* 1317 * Return true if system call tracing is enabled for the specified process. 1318 */ 1319 bool 1320 trace_is_enabled(struct proc *p) 1321 { 1322 #ifdef SYSCALL_DEBUG 1323 return (true); 1324 #endif 1325 #ifdef KTRACE 1326 if (ISSET(p->p_traceflag, (KTRFAC_SYSCALL | KTRFAC_SYSRET))) 1327 return (true); 1328 #endif 1329 #ifdef PTRACE 1330 if (ISSET(p->p_slflag, PSL_SYSCALL)) 1331 return (true); 1332 #endif 1333 1334 return (false); 1335 } 1336 1337 /* 1338 * Start trace of particular system call. If process is being traced, 1339 * this routine is called by MD syscall dispatch code just before 1340 * a system call is actually executed. 1341 */ 1342 int 1343 trace_enter(register_t code, const register_t *args, int narg) 1344 { 1345 #ifdef SYSCALL_DEBUG 1346 scdebug_call(code, args); 1347 #endif /* SYSCALL_DEBUG */ 1348 1349 ktrsyscall(code, args, narg); 1350 1351 #ifdef PTRACE 1352 if ((curlwp->l_proc->p_slflag & (PSL_SYSCALL|PSL_TRACED)) == 1353 (PSL_SYSCALL|PSL_TRACED)) 1354 process_stoptrace(); 1355 #endif 1356 return 0; 1357 } 1358 1359 /* 1360 * End trace of particular system call. If process is being traced, 1361 * this routine is called by MD syscall dispatch code just after 1362 * a system call finishes. 1363 * MD caller guarantees the passed 'code' is within the supported 1364 * system call number range for emulation the process runs under. 1365 */ 1366 void 1367 trace_exit(register_t code, register_t rval[], int error) 1368 { 1369 #ifdef SYSCALL_DEBUG 1370 scdebug_ret(code, error, rval); 1371 #endif /* SYSCALL_DEBUG */ 1372 1373 ktrsysret(code, error, rval); 1374 1375 #ifdef PTRACE 1376 if ((curlwp->l_proc->p_slflag & (PSL_SYSCALL|PSL_TRACED)) == 1377 (PSL_SYSCALL|PSL_TRACED)) 1378 process_stoptrace(); 1379 #endif 1380 } 1381