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