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