Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.212
      1  1.212  christos /*	$NetBSD: kern_subr.c,v 1.212 2011/09/19 23:54:29 christos Exp $	*/
      2   1.31   thorpej 
      3   1.31   thorpej /*-
      4  1.185        ad  * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
      5   1.31   thorpej  * All rights reserved.
      6   1.31   thorpej  *
      7   1.31   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.31   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.48     lukem  * NASA Ames Research Center, and by Luke Mewburn.
     10   1.31   thorpej  *
     11   1.31   thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.31   thorpej  * modification, are permitted provided that the following conditions
     13   1.31   thorpej  * are met:
     14   1.31   thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.31   thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.31   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.31   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.31   thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.31   thorpej  *
     20   1.31   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.31   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.31   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.31   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.31   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.31   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.31   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.31   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.31   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.31   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.31   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.31   thorpej  */
     32   1.12       cgd 
     33    1.9       cgd /*
     34   1.10       cgd  * Copyright (c) 1982, 1986, 1991, 1993
     35   1.10       cgd  *	The Regents of the University of California.  All rights reserved.
     36    1.9       cgd  * (c) UNIX System Laboratories, Inc.
     37    1.9       cgd  * All or some portions of this file are derived from material licensed
     38    1.9       cgd  * to the University of California by American Telephone and Telegraph
     39    1.9       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40    1.9       cgd  * the permission of UNIX System Laboratories, Inc.
     41    1.9       cgd  *
     42   1.18   thorpej  * Copyright (c) 1992, 1993
     43   1.18   thorpej  *	The Regents of the University of California.  All rights reserved.
     44   1.18   thorpej  *
     45   1.18   thorpej  * This software was developed by the Computer Systems Engineering group
     46   1.18   thorpej  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     47   1.18   thorpej  * contributed to Berkeley.
     48   1.18   thorpej  *
     49   1.18   thorpej  * All advertising materials mentioning features or use of this software
     50   1.18   thorpej  * must display the following acknowledgement:
     51   1.18   thorpej  *	This product includes software developed by the University of
     52   1.18   thorpej  *	California, Lawrence Berkeley Laboratory.
     53   1.18   thorpej  *
     54    1.9       cgd  * Redistribution and use in source and binary forms, with or without
     55    1.9       cgd  * modification, are permitted provided that the following conditions
     56    1.9       cgd  * are met:
     57    1.9       cgd  * 1. Redistributions of source code must retain the above copyright
     58    1.9       cgd  *    notice, this list of conditions and the following disclaimer.
     59    1.9       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     60    1.9       cgd  *    notice, this list of conditions and the following disclaimer in the
     61    1.9       cgd  *    documentation and/or other materials provided with the distribution.
     62  1.103       agc  * 3. Neither the name of the University nor the names of its contributors
     63    1.9       cgd  *    may be used to endorse or promote products derived from this software
     64    1.9       cgd  *    without specific prior written permission.
     65    1.9       cgd  *
     66    1.9       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     67    1.9       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     68    1.9       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     69    1.9       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     70    1.9       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     71    1.9       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     72    1.9       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     73    1.9       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     74    1.9       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     75    1.9       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     76    1.9       cgd  * SUCH DAMAGE.
     77    1.9       cgd  *
     78   1.38      fvdl  *	@(#)kern_subr.c	8.4 (Berkeley) 2/14/95
     79    1.9       cgd  */
     80   1.77     lukem 
     81   1.77     lukem #include <sys/cdefs.h>
     82  1.212  christos __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.212 2011/09/19 23:54:29 christos Exp $");
     83   1.34       mrg 
     84   1.78   thorpej #include "opt_ddb.h"
     85   1.43  jonathan #include "opt_md.h"
     86   1.81  christos #include "opt_syscall_debug.h"
     87   1.81  christos #include "opt_ktrace.h"
     88  1.142      matt #include "opt_ptrace.h"
     89  1.157      manu #include "opt_tftproot.h"
     90    1.9       cgd 
     91    1.9       cgd #include <sys/param.h>
     92    1.9       cgd #include <sys/systm.h>
     93    1.9       cgd #include <sys/proc.h>
     94   1.18   thorpej #include <sys/mount.h>
     95   1.18   thorpej #include <sys/device.h>
     96   1.18   thorpej #include <sys/reboot.h>
     97   1.18   thorpej #include <sys/conf.h>
     98  1.159    dyoung #include <sys/disk.h>
     99   1.18   thorpej #include <sys/disklabel.h>
    100   1.10       cgd #include <sys/queue.h>
    101   1.81  christos #include <sys/ktrace.h>
    102  1.128  christos #include <sys/ptrace.h>
    103  1.119   reinoud #include <sys/fcntl.h>
    104  1.160  christos #include <sys/kauth.h>
    105  1.208   hannken #include <sys/stat.h>
    106  1.160  christos #include <sys/vnode.h>
    107  1.194        ad #include <sys/module.h>
    108    1.9       cgd 
    109   1.18   thorpej #include <dev/cons.h>
    110   1.18   thorpej 
    111   1.18   thorpej #include <net/if.h>
    112   1.18   thorpej 
    113   1.18   thorpej /* XXX these should eventually move to subr_autoconf.c */
    114  1.200    dyoung static device_t finddevice(const char *);
    115  1.200    dyoung static device_t getdisk(char *, int, int, dev_t *, int);
    116  1.200    dyoung static device_t parsedisk(char *, int, int, dev_t *);
    117  1.159    dyoung static const char *getwedgename(const char *, int);
    118   1.18   thorpej 
    119  1.157      manu #ifdef TFTPROOT
    120  1.200    dyoung int tftproot_dhcpboot(device_t);
    121  1.157      manu #endif
    122  1.157      manu 
    123  1.175        ad dev_t	dumpcdev;	/* for savecore */
    124   1.18   thorpej 
    125  1.160  christos static int
    126  1.200    dyoung isswap(device_t dv)
    127  1.160  christos {
    128  1.160  christos 	struct dkwedge_info wi;
    129  1.160  christos 	struct vnode *vn;
    130  1.160  christos 	int error;
    131  1.160  christos 
    132  1.160  christos 	if (device_class(dv) != DV_DISK || !device_is_a(dv, "dk"))
    133  1.160  christos 		return 0;
    134  1.160  christos 
    135  1.160  christos 	if ((vn = opendisk(dv)) == NULL)
    136  1.160  christos 		return 0;
    137  1.160  christos 
    138  1.166     pooka 	error = VOP_IOCTL(vn, DIOCGWEDGEINFO, &wi, FREAD, NOCRED);
    139  1.166     pooka 	VOP_CLOSE(vn, FREAD, NOCRED);
    140  1.160  christos 	vput(vn);
    141  1.160  christos 	if (error) {
    142  1.160  christos #ifdef DEBUG_WEDGE
    143  1.184    cegger 		printf("%s: Get wedge info returned %d\n", device_xname(dv), error);
    144  1.160  christos #endif
    145  1.160  christos 		return 0;
    146  1.160  christos 	}
    147  1.160  christos 	return strcmp(wi.dkw_ptype, DKW_PTYPE_SWAP) == 0;
    148  1.160  christos }
    149  1.160  christos 
    150   1.18   thorpej /*
    151   1.18   thorpej  * Determine the root device and, if instructed to, the root file system.
    152   1.18   thorpej  */
    153   1.18   thorpej 
    154  1.108  christos #ifdef MEMORY_DISK_IS_ROOT
    155  1.187        ad int md_is_root = 1;
    156  1.187        ad #else
    157  1.187        ad int md_is_root = 0;
    158  1.108  christos #endif
    159  1.108  christos 
    160  1.113   thorpej /*
    161  1.113   thorpej  * The device and wedge that we booted from.  If booted_wedge is NULL,
    162  1.113   thorpej  * the we might consult booted_partition.
    163  1.113   thorpej  */
    164  1.200    dyoung device_t booted_device;
    165  1.200    dyoung device_t booted_wedge;
    166  1.113   thorpej int booted_partition;
    167  1.113   thorpej 
    168  1.113   thorpej /*
    169  1.113   thorpej  * Use partition letters if it's a disk class but not a wedge.
    170  1.113   thorpej  * XXX Check for wedge is kinda gross.
    171  1.113   thorpej  */
    172  1.113   thorpej #define	DEV_USES_PARTITIONS(dv)						\
    173  1.124   thorpej 	(device_class((dv)) == DV_DISK &&				\
    174  1.125   thorpej 	 !device_is_a((dv), "dk"))
    175  1.113   thorpej 
    176   1.18   thorpej void
    177  1.200    dyoung setroot(device_t bootdv, int bootpartition)
    178   1.18   thorpej {
    179  1.200    dyoung 	device_t dv;
    180  1.203    dyoung 	deviter_t di;
    181  1.159    dyoung 	int len, majdev;
    182   1.25       mrg 	dev_t nrootdev;
    183   1.26   thorpej 	dev_t ndumpdev = NODEV;
    184   1.18   thorpej 	char buf[128];
    185   1.18   thorpej 	const char *rootdevname;
    186   1.26   thorpej 	const char *dumpdevname;
    187  1.200    dyoung 	device_t rootdv = NULL;		/* XXX gcc -Wuninitialized */
    188  1.200    dyoung 	device_t dumpdv = NULL;
    189   1.18   thorpej 	struct ifnet *ifp;
    190   1.18   thorpej 	const char *deffsname;
    191   1.18   thorpej 	struct vfsops *vops;
    192   1.18   thorpej 
    193  1.157      manu #ifdef TFTPROOT
    194  1.157      manu 	if (tftproot_dhcpboot(bootdv) != 0)
    195  1.157      manu 		boothowto |= RB_ASKNAME;
    196  1.157      manu #endif
    197  1.157      manu 
    198  1.208   hannken 	/*
    199  1.208   hannken 	 * For root on md0 we have to force the attachment of md0.
    200  1.208   hannken 	 */
    201  1.187        ad 	if (md_is_root) {
    202  1.208   hannken 		int md_major;
    203  1.208   hannken 		dev_t md_dev;
    204  1.208   hannken 
    205  1.208   hannken 		bootdv = NULL;
    206  1.208   hannken 		md_major = devsw_name2blk("md", NULL, 0);
    207  1.208   hannken 		if (md_major >= 0) {
    208  1.208   hannken 			md_dev = MAKEDISKDEV(md_major, 0, RAW_PART);
    209  1.208   hannken 			if (bdev_open(md_dev, FREAD, S_IFBLK, curlwp) == 0)
    210  1.208   hannken 				bootdv = device_find_by_xname("md0");
    211  1.208   hannken 		}
    212  1.208   hannken 		if (bootdv == NULL)
    213  1.208   hannken 			panic("Cannot open \"md0\" (root)");
    214   1.28       leo 	}
    215   1.18   thorpej 
    216   1.18   thorpej 	/*
    217   1.18   thorpej 	 * If NFS is specified as the file system, and we found
    218   1.18   thorpej 	 * a DV_DISK boot device (or no boot device at all), then
    219   1.18   thorpej 	 * find a reasonable network interface for "rootspec".
    220   1.18   thorpej 	 */
    221  1.197  pgoyette 	vops = vfs_getopsbyname(MOUNT_NFS);
    222  1.197  pgoyette 	if (vops != NULL && strcmp(rootfstype, MOUNT_NFS) == 0 &&
    223   1.18   thorpej 	    rootspec == NULL &&
    224  1.124   thorpej 	    (bootdv == NULL || device_class(bootdv) != DV_IFNET)) {
    225  1.115      matt 		IFNET_FOREACH(ifp) {
    226   1.18   thorpej 			if ((ifp->if_flags &
    227   1.18   thorpej 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    228   1.18   thorpej 				break;
    229   1.83      matt 		}
    230   1.18   thorpej 		if (ifp == NULL) {
    231   1.18   thorpej 			/*
    232   1.18   thorpej 			 * Can't find a suitable interface; ask the
    233   1.18   thorpej 			 * user.
    234   1.18   thorpej 			 */
    235   1.18   thorpej 			boothowto |= RB_ASKNAME;
    236   1.18   thorpej 		} else {
    237   1.18   thorpej 			/*
    238   1.18   thorpej 			 * Have a suitable interface; behave as if
    239   1.18   thorpej 			 * the user specified this interface.
    240   1.18   thorpej 			 */
    241   1.18   thorpej 			rootspec = (const char *)ifp->if_xname;
    242   1.18   thorpej 		}
    243   1.18   thorpej 	}
    244  1.165        ad 	if (vops != NULL)
    245  1.165        ad 		vfs_delref(vops);
    246   1.24   thorpej 
    247   1.24   thorpej 	/*
    248   1.24   thorpej 	 * If wildcarded root and we the boot device wasn't determined,
    249   1.24   thorpej 	 * ask the user.
    250   1.24   thorpej 	 */
    251   1.24   thorpej 	if (rootspec == NULL && bootdv == NULL)
    252   1.24   thorpej 		boothowto |= RB_ASKNAME;
    253   1.18   thorpej 
    254   1.18   thorpej  top:
    255   1.18   thorpej 	if (boothowto & RB_ASKNAME) {
    256  1.200    dyoung 		device_t defdumpdv;
    257   1.26   thorpej 
    258   1.18   thorpej 		for (;;) {
    259   1.18   thorpej 			printf("root device");
    260   1.18   thorpej 			if (bootdv != NULL) {
    261  1.184    cegger 				printf(" (default %s", device_xname(bootdv));
    262  1.113   thorpej 				if (DEV_USES_PARTITIONS(bootdv))
    263   1.18   thorpej 					printf("%c", bootpartition + 'a');
    264   1.18   thorpej 				printf(")");
    265   1.18   thorpej 			}
    266   1.18   thorpej 			printf(": ");
    267   1.64    itojun 			len = cngetsn(buf, sizeof(buf));
    268   1.18   thorpej 			if (len == 0 && bootdv != NULL) {
    269  1.184    cegger 				strlcpy(buf, device_xname(bootdv), sizeof(buf));
    270   1.18   thorpej 				len = strlen(buf);
    271   1.18   thorpej 			}
    272   1.18   thorpej 			if (len > 0 && buf[len - 1] == '*') {
    273   1.18   thorpej 				buf[--len] = '\0';
    274   1.51   thorpej 				dv = getdisk(buf, len, 1, &nrootdev, 0);
    275   1.18   thorpej 				if (dv != NULL) {
    276   1.18   thorpej 					rootdv = dv;
    277   1.25       mrg 					break;
    278   1.18   thorpej 				}
    279   1.18   thorpej 			}
    280   1.51   thorpej 			dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
    281   1.18   thorpej 			if (dv != NULL) {
    282   1.18   thorpej 				rootdv = dv;
    283   1.18   thorpej 				break;
    284   1.18   thorpej 			}
    285   1.18   thorpej 		}
    286   1.18   thorpej 
    287   1.26   thorpej 		/*
    288   1.26   thorpej 		 * Set up the default dump device.  If root is on
    289   1.26   thorpej 		 * a network device, there is no default dump
    290   1.26   thorpej 		 * device, since we don't support dumps to the
    291   1.26   thorpej 		 * network.
    292   1.26   thorpej 		 */
    293  1.113   thorpej 		if (DEV_USES_PARTITIONS(rootdv) == 0)
    294   1.26   thorpej 			defdumpdv = NULL;
    295   1.26   thorpej 		else
    296   1.26   thorpej 			defdumpdv = rootdv;
    297   1.26   thorpej 
    298   1.26   thorpej 		for (;;) {
    299   1.26   thorpej 			printf("dump device");
    300   1.26   thorpej 			if (defdumpdv != NULL) {
    301   1.26   thorpej 				/*
    302   1.26   thorpej 				 * Note, we know it's a disk if we get here.
    303   1.26   thorpej 				 */
    304  1.184    cegger 				printf(" (default %sb)", device_xname(defdumpdv));
    305   1.26   thorpej 			}
    306   1.26   thorpej 			printf(": ");
    307   1.64    itojun 			len = cngetsn(buf, sizeof(buf));
    308   1.26   thorpej 			if (len == 0) {
    309   1.26   thorpej 				if (defdumpdv != NULL) {
    310   1.26   thorpej 					ndumpdev = MAKEDISKDEV(major(nrootdev),
    311   1.26   thorpej 					    DISKUNIT(nrootdev), 1);
    312   1.26   thorpej 				}
    313   1.54     enami 				dumpdv = defdumpdv;
    314   1.26   thorpej 				break;
    315   1.26   thorpej 			}
    316   1.26   thorpej 			if (len == 4 && strcmp(buf, "none") == 0) {
    317   1.55     enami 				dumpdv = NULL;
    318   1.54     enami 				break;
    319   1.26   thorpej 			}
    320   1.51   thorpej 			dv = getdisk(buf, len, 1, &ndumpdev, 1);
    321   1.55     enami 			if (dv != NULL) {
    322   1.26   thorpej 				dumpdv = dv;
    323   1.26   thorpej 				break;
    324   1.26   thorpej 			}
    325   1.26   thorpej 		}
    326   1.26   thorpej 
    327   1.18   thorpej 		rootdev = nrootdev;
    328   1.26   thorpej 		dumpdev = ndumpdev;
    329   1.18   thorpej 
    330   1.36   thorpej 		for (vops = LIST_FIRST(&vfs_list); vops != NULL;
    331   1.36   thorpej 		     vops = LIST_NEXT(vops, vfs_list)) {
    332   1.36   thorpej 			if (vops->vfs_mountroot != NULL &&
    333  1.197  pgoyette 			    strcmp(rootfstype, vops->vfs_name) == 0)
    334   1.38      fvdl 			break;
    335   1.18   thorpej 		}
    336   1.36   thorpej 
    337   1.36   thorpej 		if (vops == NULL) {
    338   1.18   thorpej 			deffsname = "generic";
    339   1.18   thorpej 		} else
    340   1.36   thorpej 			deffsname = vops->vfs_name;
    341   1.36   thorpej 
    342   1.18   thorpej 		for (;;) {
    343   1.18   thorpej 			printf("file system (default %s): ", deffsname);
    344   1.64    itojun 			len = cngetsn(buf, sizeof(buf));
    345  1.197  pgoyette 			if (len == 0) {
    346  1.197  pgoyette 				if (strcmp(deffsname, "generic") == 0)
    347  1.197  pgoyette 					rootfstype = ROOT_FSTYPE_ANY;
    348   1.18   thorpej 				break;
    349  1.197  pgoyette 			}
    350   1.18   thorpej 			if (len == 4 && strcmp(buf, "halt") == 0)
    351   1.23       gwr 				cpu_reboot(RB_HALT, NULL);
    352   1.76   thorpej 			else if (len == 6 && strcmp(buf, "reboot") == 0)
    353   1.76   thorpej 				cpu_reboot(0, NULL);
    354   1.78   thorpej #if defined(DDB)
    355   1.78   thorpej 			else if (len == 3 && strcmp(buf, "ddb") == 0) {
    356   1.78   thorpej 				console_debugger();
    357   1.78   thorpej 			}
    358   1.78   thorpej #endif
    359   1.18   thorpej 			else if (len == 7 && strcmp(buf, "generic") == 0) {
    360  1.197  pgoyette 				rootfstype = ROOT_FSTYPE_ANY;
    361   1.18   thorpej 				break;
    362   1.18   thorpej 			}
    363   1.18   thorpej 			vops = vfs_getopsbyname(buf);
    364   1.18   thorpej 			if (vops == NULL || vops->vfs_mountroot == NULL) {
    365   1.18   thorpej 				printf("use one of: generic");
    366   1.36   thorpej 				for (vops = LIST_FIRST(&vfs_list);
    367   1.36   thorpej 				     vops != NULL;
    368   1.36   thorpej 				     vops = LIST_NEXT(vops, vfs_list)) {
    369   1.36   thorpej 					if (vops->vfs_mountroot != NULL)
    370   1.36   thorpej 						printf(" %s", vops->vfs_name);
    371   1.36   thorpej 				}
    372  1.197  pgoyette 				if (vops != NULL)
    373  1.197  pgoyette 					vfs_delref(vops);
    374   1.78   thorpej #if defined(DDB)
    375   1.78   thorpej 				printf(" ddb");
    376   1.78   thorpej #endif
    377   1.76   thorpej 				printf(" halt reboot\n");
    378   1.18   thorpej 			} else {
    379  1.197  pgoyette 				/*
    380  1.197  pgoyette 				 * XXX If *vops gets freed between here and
    381  1.197  pgoyette 				 * the call to mountroot(), rootfstype will
    382  1.197  pgoyette 				 * point to something unexpected.  But in
    383  1.197  pgoyette 				 * this case the system will fail anyway.
    384  1.197  pgoyette 				 */
    385  1.197  pgoyette 				rootfstype = vops->vfs_name;
    386  1.165        ad 				vfs_delref(vops);
    387   1.18   thorpej 				break;
    388   1.18   thorpej 			}
    389   1.18   thorpej 		}
    390   1.18   thorpej 
    391   1.18   thorpej 	} else if (rootspec == NULL) {
    392   1.18   thorpej 		/*
    393   1.18   thorpej 		 * Wildcarded root; use the boot device.
    394   1.18   thorpej 		 */
    395   1.26   thorpej 		rootdv = bootdv;
    396   1.26   thorpej 
    397  1.188  christos 		if (bootdv)
    398  1.188  christos 			majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
    399  1.188  christos 		else
    400  1.188  christos 			majdev = -1;
    401   1.18   thorpej 		if (majdev >= 0) {
    402   1.18   thorpej 			/*
    403  1.113   thorpej 			 * Root is on a disk.  `bootpartition' is root,
    404  1.113   thorpej 			 * unless the device does not use partitions.
    405   1.18   thorpej 			 */
    406  1.113   thorpej 			if (DEV_USES_PARTITIONS(bootdv))
    407  1.135   thorpej 				rootdev = MAKEDISKDEV(majdev,
    408  1.135   thorpej 						      device_unit(bootdv),
    409  1.135   thorpej 						      bootpartition);
    410  1.113   thorpej 			else
    411  1.135   thorpej 				rootdev = makedev(majdev, device_unit(bootdv));
    412   1.18   thorpej 		}
    413   1.18   thorpej 	} else {
    414   1.18   thorpej 
    415   1.18   thorpej 		/*
    416   1.25       mrg 		 * `root on <dev> ...'
    417   1.18   thorpej 		 */
    418   1.18   thorpej 
    419   1.18   thorpej 		/*
    420   1.18   thorpej 		 * If it's a network interface, we can bail out
    421   1.18   thorpej 		 * early.
    422   1.18   thorpej 		 */
    423   1.56     enami 		dv = finddevice(rootspec);
    424  1.124   thorpej 		if (dv != NULL && device_class(dv) == DV_IFNET) {
    425   1.26   thorpej 			rootdv = dv;
    426   1.26   thorpej 			goto haveroot;
    427   1.18   thorpej 		}
    428   1.18   thorpej 
    429  1.159    dyoung 		if (rootdev == NODEV &&
    430  1.159    dyoung 		    device_class(dv) == DV_DISK && device_is_a(dv, "dk") &&
    431  1.184    cegger 		    (majdev = devsw_name2blk(device_xname(dv), NULL, 0)) >= 0)
    432  1.159    dyoung 			rootdev = makedev(majdev, device_unit(dv));
    433  1.159    dyoung 
    434   1.88   gehenna 		rootdevname = devsw_blk2name(major(rootdev));
    435   1.18   thorpej 		if (rootdevname == NULL) {
    436  1.198  christos 			printf("unknown device major 0x%llx\n",
    437  1.198  christos 			    (unsigned long long)rootdev);
    438   1.18   thorpej 			boothowto |= RB_ASKNAME;
    439   1.18   thorpej 			goto top;
    440   1.18   thorpej 		}
    441   1.41     perry 		memset(buf, 0, sizeof(buf));
    442  1.198  christos 		snprintf(buf, sizeof(buf), "%s%llu", rootdevname,
    443  1.198  christos 		    (unsigned long long)DISKUNIT(rootdev));
    444   1.18   thorpej 
    445   1.56     enami 		rootdv = finddevice(buf);
    446   1.26   thorpej 		if (rootdv == NULL) {
    447  1.198  christos 			printf("device %s (0x%llx) not configured\n",
    448  1.198  christos 			    buf, (unsigned long long)rootdev);
    449   1.18   thorpej 			boothowto |= RB_ASKNAME;
    450   1.18   thorpej 			goto top;
    451   1.18   thorpej 		}
    452   1.26   thorpej 	}
    453   1.18   thorpej 
    454   1.26   thorpej  haveroot:
    455   1.18   thorpej 
    456   1.18   thorpej 	root_device = rootdv;
    457   1.18   thorpej 
    458  1.124   thorpej 	switch (device_class(rootdv)) {
    459   1.18   thorpej 	case DV_IFNET:
    460  1.139  christos 	case DV_DISK:
    461  1.184    cegger 		aprint_normal("root on %s", device_xname(rootdv));
    462  1.140  christos 		if (DEV_USES_PARTITIONS(rootdv))
    463  1.198  christos 			aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
    464   1.18   thorpej 		break;
    465   1.18   thorpej 
    466   1.18   thorpej 	default:
    467   1.18   thorpej 		printf("can't determine root device\n");
    468   1.18   thorpej 		boothowto |= RB_ASKNAME;
    469   1.18   thorpej 		goto top;
    470   1.18   thorpej 	}
    471   1.26   thorpej 
    472   1.26   thorpej 	/*
    473   1.26   thorpej 	 * Now configure the dump device.
    474   1.55     enami 	 *
    475   1.26   thorpej 	 * If we haven't figured out the dump device, do so, with
    476   1.26   thorpej 	 * the following rules:
    477   1.26   thorpej 	 *
    478   1.26   thorpej 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
    479   1.26   thorpej 	 *
    480   1.26   thorpej 	 *	(b) If dumpspec is set, try to use it.  If the device
    481   1.26   thorpej 	 *	    is not available, punt.
    482   1.26   thorpej 	 *
    483   1.26   thorpej 	 *	(c) If dumpspec is not set, the dump device is
    484   1.26   thorpej 	 *	    wildcarded or unspecified.  If the root device
    485   1.26   thorpej 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
    486   1.26   thorpej 	 *	    of the root device.
    487   1.26   thorpej 	 */
    488   1.26   thorpej 
    489   1.55     enami 	if (boothowto & RB_ASKNAME) {		/* (a) */
    490   1.55     enami 		if (dumpdv == NULL)
    491   1.55     enami 			goto nodumpdev;
    492   1.55     enami 	} else if (dumpspec != NULL) {		/* (b) */
    493   1.55     enami 		if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
    494   1.26   thorpej 			/*
    495   1.55     enami 			 * Operator doesn't want a dump device.
    496   1.55     enami 			 * Or looks like they tried to pick a network
    497   1.26   thorpej 			 * device.  Oops.
    498   1.26   thorpej 			 */
    499   1.26   thorpej 			goto nodumpdev;
    500   1.26   thorpej 		}
    501   1.26   thorpej 
    502   1.88   gehenna 		dumpdevname = devsw_blk2name(major(dumpdev));
    503   1.26   thorpej 		if (dumpdevname == NULL)
    504   1.26   thorpej 			goto nodumpdev;
    505   1.41     perry 		memset(buf, 0, sizeof(buf));
    506  1.198  christos 		snprintf(buf, sizeof(buf), "%s%llu", dumpdevname,
    507  1.198  christos 		    (unsigned long long)DISKUNIT(dumpdev));
    508   1.26   thorpej 
    509   1.56     enami 		dumpdv = finddevice(buf);
    510   1.56     enami 		if (dumpdv == NULL) {
    511   1.26   thorpej 			/*
    512   1.26   thorpej 			 * Device not configured.
    513   1.26   thorpej 			 */
    514   1.26   thorpej 			goto nodumpdev;
    515   1.26   thorpej 		}
    516   1.55     enami 	} else {				/* (c) */
    517  1.160  christos 		if (DEV_USES_PARTITIONS(rootdv) == 0) {
    518  1.203    dyoung 			for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    519  1.203    dyoung 			     dv != NULL;
    520  1.203    dyoung 			     dv = deviter_next(&di))
    521  1.160  christos 				if (isswap(dv))
    522  1.160  christos 					break;
    523  1.203    dyoung 			deviter_release(&di);
    524  1.160  christos 			if (dv == NULL)
    525  1.160  christos 				goto nodumpdev;
    526  1.160  christos 
    527  1.184    cegger 			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
    528  1.160  christos 			if (majdev < 0)
    529  1.160  christos 				goto nodumpdev;
    530  1.160  christos 			dumpdv = dv;
    531  1.160  christos 			dumpdev = makedev(majdev, device_unit(dumpdv));
    532  1.160  christos 		} else {
    533   1.55     enami 			dumpdv = rootdv;
    534   1.55     enami 			dumpdev = MAKEDISKDEV(major(rootdev),
    535  1.135   thorpej 			    device_unit(dumpdv), 1);
    536   1.55     enami 		}
    537   1.26   thorpej 	}
    538   1.26   thorpej 
    539  1.175        ad 	dumpcdev = devsw_blk2chr(dumpdev);
    540  1.184    cegger 	aprint_normal(" dumps on %s", device_xname(dumpdv));
    541  1.140  christos 	if (DEV_USES_PARTITIONS(dumpdv))
    542  1.198  christos 		aprint_normal("%c", (int)DISKPART(dumpdev) + 'a');
    543  1.140  christos 	aprint_normal("\n");
    544   1.26   thorpej 	return;
    545   1.26   thorpej 
    546   1.26   thorpej  nodumpdev:
    547   1.26   thorpej 	dumpdev = NODEV;
    548  1.175        ad 	dumpcdev = NODEV;
    549  1.100   thorpej 	aprint_normal("\n");
    550   1.18   thorpej }
    551   1.18   thorpej 
    552  1.200    dyoung static device_t
    553  1.117   thorpej finddevice(const char *name)
    554   1.56     enami {
    555  1.159    dyoung 	const char *wname;
    556  1.108  christos 
    557  1.159    dyoung 	if ((wname = getwedgename(name, strlen(name))) != NULL)
    558  1.159    dyoung 		return dkwedge_find_by_wname(wname);
    559  1.159    dyoung 
    560  1.179     joerg 	return device_find_by_xname(name);
    561   1.56     enami }
    562   1.56     enami 
    563  1.200    dyoung static device_t
    564  1.117   thorpej getdisk(char *str, int len, int defpart, dev_t *devp, int isdump)
    565   1.18   thorpej {
    566  1.200    dyoung 	device_t dv;
    567  1.203    dyoung 	deviter_t di;
    568   1.18   thorpej 
    569   1.51   thorpej 	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
    570   1.18   thorpej 		printf("use one of:");
    571  1.203    dyoung 		for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
    572  1.203    dyoung 		     dv = deviter_next(&di)) {
    573  1.113   thorpej 			if (DEV_USES_PARTITIONS(dv))
    574  1.184    cegger 				printf(" %s[a-%c]", device_xname(dv),
    575   1.19       cgd 				    'a' + MAXPARTITIONS - 1);
    576  1.124   thorpej 			else if (device_class(dv) == DV_DISK)
    577  1.184    cegger 				printf(" %s", device_xname(dv));
    578  1.124   thorpej 			if (isdump == 0 && device_class(dv) == DV_IFNET)
    579  1.184    cegger 				printf(" %s", device_xname(dv));
    580   1.18   thorpej 		}
    581  1.203    dyoung 		deviter_release(&di);
    582  1.159    dyoung 		dkwedge_print_wnames();
    583   1.26   thorpej 		if (isdump)
    584   1.26   thorpej 			printf(" none");
    585   1.78   thorpej #if defined(DDB)
    586   1.78   thorpej 		printf(" ddb");
    587   1.78   thorpej #endif
    588   1.76   thorpej 		printf(" halt reboot\n");
    589   1.18   thorpej 	}
    590  1.159    dyoung 	return dv;
    591  1.159    dyoung }
    592  1.159    dyoung 
    593  1.159    dyoung static const char *
    594  1.159    dyoung getwedgename(const char *name, int namelen)
    595  1.159    dyoung {
    596  1.159    dyoung 	const char *wpfx = "wedge:";
    597  1.159    dyoung 	const int wpfxlen = strlen(wpfx);
    598  1.159    dyoung 
    599  1.159    dyoung 	if (namelen < wpfxlen || strncmp(name, wpfx, wpfxlen) != 0)
    600  1.159    dyoung 		return NULL;
    601  1.159    dyoung 
    602  1.159    dyoung 	return name + wpfxlen;
    603   1.18   thorpej }
    604   1.18   thorpej 
    605  1.200    dyoung static device_t
    606  1.117   thorpej parsedisk(char *str, int len, int defpart, dev_t *devp)
    607   1.18   thorpej {
    608  1.200    dyoung 	device_t dv;
    609  1.159    dyoung 	const char *wname;
    610   1.18   thorpej 	char *cp, c;
    611   1.29  drochner 	int majdev, part;
    612   1.18   thorpej 	if (len == 0)
    613   1.18   thorpej 		return (NULL);
    614   1.18   thorpej 
    615   1.18   thorpej 	if (len == 4 && strcmp(str, "halt") == 0)
    616   1.23       gwr 		cpu_reboot(RB_HALT, NULL);
    617   1.76   thorpej 	else if (len == 6 && strcmp(str, "reboot") == 0)
    618   1.76   thorpej 		cpu_reboot(0, NULL);
    619   1.78   thorpej #if defined(DDB)
    620   1.78   thorpej 	else if (len == 3 && strcmp(str, "ddb") == 0)
    621   1.78   thorpej 		console_debugger();
    622   1.78   thorpej #endif
    623   1.18   thorpej 
    624   1.18   thorpej 	cp = str + len - 1;
    625   1.18   thorpej 	c = *cp;
    626  1.159    dyoung 
    627  1.159    dyoung 	if ((wname = getwedgename(str, len)) != NULL) {
    628  1.159    dyoung 		if ((dv = dkwedge_find_by_wname(wname)) == NULL)
    629  1.159    dyoung 			return NULL;
    630  1.159    dyoung 		part = defpart;
    631  1.159    dyoung 		goto gotdisk;
    632  1.159    dyoung 	} else if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
    633   1.18   thorpej 		part = c - 'a';
    634   1.18   thorpej 		*cp = '\0';
    635   1.18   thorpej 	} else
    636   1.18   thorpej 		part = defpart;
    637   1.18   thorpej 
    638   1.56     enami 	dv = finddevice(str);
    639   1.56     enami 	if (dv != NULL) {
    640  1.124   thorpej 		if (device_class(dv) == DV_DISK) {
    641   1.18   thorpej  gotdisk:
    642  1.184    cegger 			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
    643   1.18   thorpej 			if (majdev < 0)
    644   1.18   thorpej 				panic("parsedisk");
    645  1.113   thorpej 			if (DEV_USES_PARTITIONS(dv))
    646  1.135   thorpej 				*devp = MAKEDISKDEV(majdev, device_unit(dv),
    647  1.135   thorpej 						    part);
    648  1.113   thorpej 			else
    649  1.135   thorpej 				*devp = makedev(majdev, device_unit(dv));
    650   1.18   thorpej 		}
    651   1.18   thorpej 
    652  1.124   thorpej 		if (device_class(dv) == DV_IFNET)
    653   1.18   thorpej 			*devp = NODEV;
    654   1.18   thorpej 	}
    655   1.18   thorpej 
    656   1.18   thorpej 	*cp = c;
    657   1.18   thorpej 	return (dv);
    658   1.48     lukem }
    659   1.48     lukem 
    660   1.48     lukem /*
    661  1.154   thorpej  * Return true if system call tracing is enabled for the specified process.
    662  1.130   thorpej  */
    663  1.153   thorpej bool
    664  1.130   thorpej trace_is_enabled(struct proc *p)
    665  1.130   thorpej {
    666  1.131   thorpej #ifdef SYSCALL_DEBUG
    667  1.154   thorpej 	return (true);
    668  1.131   thorpej #endif
    669  1.130   thorpej #ifdef KTRACE
    670  1.130   thorpej 	if (ISSET(p->p_traceflag, (KTRFAC_SYSCALL | KTRFAC_SYSRET)))
    671  1.154   thorpej 		return (true);
    672  1.130   thorpej #endif
    673  1.142      matt #ifdef PTRACE
    674  1.151        ad 	if (ISSET(p->p_slflag, PSL_SYSCALL))
    675  1.154   thorpej 		return (true);
    676  1.142      matt #endif
    677  1.130   thorpej 
    678  1.154   thorpej 	return (false);
    679  1.130   thorpej }
    680  1.130   thorpej 
    681  1.130   thorpej /*
    682   1.92  jdolecek  * Start trace of particular system call. If process is being traced,
    683   1.92  jdolecek  * this routine is called by MD syscall dispatch code just before
    684   1.92  jdolecek  * a system call is actually executed.
    685   1.92  jdolecek  */
    686   1.81  christos int
    687  1.177       dsl trace_enter(register_t code, const register_t *args, int narg)
    688   1.81  christos {
    689  1.210  jmcneill 	int error = 0;
    690  1.210  jmcneill 
    691   1.81  christos #ifdef SYSCALL_DEBUG
    692  1.171       dsl 	scdebug_call(code, args);
    693   1.81  christos #endif /* SYSCALL_DEBUG */
    694   1.81  christos 
    695  1.177       dsl 	ktrsyscall(code, args, narg);
    696   1.81  christos 
    697  1.142      matt #ifdef PTRACE
    698  1.172       dsl 	if ((curlwp->l_proc->p_slflag & (PSL_SYSCALL|PSL_TRACED)) ==
    699  1.210  jmcneill 	    (PSL_SYSCALL|PSL_TRACED)) {
    700  1.172       dsl 		process_stoptrace();
    701  1.210  jmcneill 		if (curlwp->l_proc->p_slflag & PSL_SYSCALLEMU) {
    702  1.210  jmcneill 			/* tracer will emulate syscall for us */
    703  1.210  jmcneill 			error = EJUSTRETURN;
    704  1.210  jmcneill 		}
    705  1.210  jmcneill 	}
    706  1.142      matt #endif
    707  1.210  jmcneill 	return error;
    708   1.81  christos }
    709   1.81  christos 
    710   1.92  jdolecek /*
    711   1.92  jdolecek  * End trace of particular system call. If process is being traced,
    712   1.92  jdolecek  * this routine is called by MD syscall dispatch code just after
    713   1.92  jdolecek  * a system call finishes.
    714   1.92  jdolecek  * MD caller guarantees the passed 'code' is within the supported
    715   1.92  jdolecek  * system call number range for emulation the process runs under.
    716   1.92  jdolecek  */
    717   1.81  christos void
    718  1.177       dsl trace_exit(register_t code, register_t rval[], int error)
    719   1.81  christos {
    720  1.212  christos #ifdef PTRACE
    721  1.211  jmcneill 	struct proc *p = curlwp->l_proc;
    722  1.212  christos #endif
    723  1.211  jmcneill 
    724   1.81  christos #ifdef SYSCALL_DEBUG
    725  1.171       dsl 	scdebug_ret(code, error, rval);
    726   1.81  christos #endif /* SYSCALL_DEBUG */
    727   1.81  christos 
    728  1.164        ad 	ktrsysret(code, error, rval);
    729  1.128  christos 
    730  1.142      matt #ifdef PTRACE
    731  1.211  jmcneill 	if ((p->p_slflag & (PSL_SYSCALL|PSL_TRACED|PSL_SYSCALLEMU)) ==
    732  1.151        ad 	    (PSL_SYSCALL|PSL_TRACED))
    733  1.172       dsl 		process_stoptrace();
    734  1.211  jmcneill 	CLR(p->p_slflag, PSL_SYSCALLEMU);
    735  1.142      matt #endif
    736   1.10       cgd }
    737