Home | History | Annotate | Line # | Download | only in x86
autoconf.c revision 1.18.2.1
      1 /*	$NetBSD: autoconf.c,v 1.18.2.1 2018/10/13 17:16:12 martin Exp $	*/
      2 /*	NetBSD: autoconf.c,v 1.75 2003/12/30 12:33:22 pk Exp 	*/
      3 
      4 /*-
      5  * Copyright (c) 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * William Jolitz.
     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. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)autoconf.c	7.1 (Berkeley) 5/9/91
     36  */
     37 
     38 /*
     39  * Setup the system to run on the current machine.
     40  *
     41  * Configure() is called at boot time and initializes the vba
     42  * device tables and the memory controller monitoring.  Available
     43  * devices are determined (from possibilities mentioned in ioconf.c),
     44  * and the drivers are initialized.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.18.2.1 2018/10/13 17:16:12 martin Exp $");
     49 
     50 #include "opt_xen.h"
     51 #include "opt_compat_oldboot.h"
     52 #include "opt_multiprocessor.h"
     53 #include "opt_nfs_boot.h"
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/buf.h>
     58 #include <sys/disklabel.h>
     59 #include <sys/disk.h>
     60 #include <sys/conf.h>
     61 #ifdef COMPAT_OLDBOOT
     62 #include <sys/reboot.h>
     63 #endif
     64 #include <sys/device.h>
     65 #include <sys/vnode.h>
     66 #include <sys/fcntl.h>
     67 #include <sys/dkio.h>
     68 #include <sys/proc.h>
     69 #include <sys/kauth.h>
     70 
     71 #ifdef NFS_BOOT_BOOTSTATIC
     72 #include <net/if.h>
     73 #include <net/if_ether.h>
     74 #include <netinet/in.h>
     75 #include <nfs/rpcv2.h>
     76 #include <nfs/nfsproto.h>
     77 #include <nfs/nfs.h>
     78 #include <nfs/nfsmount.h>
     79 #include <nfs/nfsdiskless.h>
     80 #include <xen/if_xennetvar.h>
     81 #endif
     82 
     83 #include <machine/pte.h>
     84 #include <machine/cpu.h>
     85 #include <machine/gdt.h>
     86 #include <machine/pcb.h>
     87 #include <machine/bootinfo.h>
     88 
     89 static int is_valid_disk(device_t);
     90 
     91 struct disklist *x86_alldisks;
     92 int x86_ndisks;
     93 
     94 #include "bios32.h"
     95 #if NBIOS32 > 0
     96 #include <machine/bios32.h>
     97 /* XXX */
     98 extern void platform_init(void);
     99 #endif
    100 
    101 #include "opt_pcibios.h"
    102 #ifdef PCIBIOS
    103 #include <dev/pci/pcireg.h>
    104 #include <dev/pci/pcivar.h>
    105 #include <i386/pci/pcibios.h>
    106 #endif
    107 
    108 /*
    109  * Determine i/o configuration for a machine.
    110  */
    111 void
    112 cpu_configure(void)
    113 {
    114 	struct pcb *pcb;
    115 
    116 	startrtclock();
    117 
    118 #if defined(DOM0OPS)
    119 	if (xendomain_is_dom0()) {
    120 #if NBIOS32 > 0
    121 		bios32_init();
    122 		platform_init();
    123 		/* identify hypervisor type from SMBIOS */
    124 		identify_hypervisor();
    125 #endif /* NBIOS32 > 0 */
    126 	} else
    127 #endif /* DOM0OPS */
    128 		vm_guest = VM_GUEST_XEN;
    129 #ifdef PCIBIOS
    130 	pcibios_init();
    131 #endif
    132 
    133 	if (config_rootfound("mainbus", NULL) == NULL)
    134 		panic("configure: mainbus not configured");
    135 
    136 #ifdef INTRDEBUG
    137 	intr_printconfig();
    138 #endif
    139 
    140 	/* resync cr0 after FPU configuration */
    141 	pcb = lwp_getpcb(&lwp0);
    142 	pcb->pcb_cr0 = rcr0();
    143 #ifdef MULTIPROCESSOR
    144 	/* propagate this to the idle pcb's. */
    145 	cpu_init_idle_lwps();
    146 #endif
    147 
    148 	spl0();
    149 }
    150 
    151 void
    152 cpu_rootconf(void)
    153 {
    154 	cpu_bootconf();
    155 
    156 	printf("boot device: %s\n",
    157 	    booted_device ? device_xname(booted_device) :
    158 	    bootspec ? bootspec : "<unknown>");
    159 	rootconf();
    160 }
    161 
    162 
    163 /*
    164  * Attempt to find the device from which we were booted.
    165  */
    166 void
    167 cpu_bootconf(void)
    168 {
    169 	device_t dv;
    170 	deviter_t di;
    171 	union xen_cmdline_parseinfo xcp;
    172 	static char bootspecbuf[sizeof(xcp.xcp_bootdev)];
    173 
    174 	if (booted_device)
    175 		return;
    176 
    177 	xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
    178 
    179 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    180 	     dv != NULL;
    181 	     dv = deviter_next(&di)) {
    182 		bool is_ifnet, is_disk;
    183 		const char *devname;
    184 
    185 		is_ifnet = (device_class(dv) == DV_IFNET);
    186 		is_disk = is_valid_disk(dv);
    187 		devname = device_xname(dv);
    188 
    189 		if (!is_ifnet && !is_disk)
    190 			continue;
    191 
    192 		if (is_disk && xcp.xcp_bootdev[0] == 0) {
    193 			booted_device = dv;
    194 			break;
    195 		}
    196 
    197 		if (strncmp(xcp.xcp_bootdev, devname, strlen(devname)))
    198 			continue;
    199 
    200 		if (is_disk && strlen(xcp.xcp_bootdev) > strlen(devname)) {
    201 			booted_partition = toupper(
    202 				xcp.xcp_bootdev[strlen(devname)]) - 'A';
    203 		}
    204 
    205 		booted_device = dv;
    206 		break;
    207 	}
    208 	deviter_release(&di);
    209 
    210 	if (booted_device)
    211 		return;
    212 
    213 	/*
    214 	 * not a boot device name, pass through to MI code
    215 	 */
    216 	if (xcp.xcp_bootdev[0] != '\0') {
    217 		strlcpy(bootspecbuf, xcp.xcp_bootdev, sizeof(bootspecbuf));
    218 		bootspec = bootspecbuf;
    219 		return;
    220 	}
    221 }
    222 
    223 #include "pci.h"
    224 
    225 #include <dev/isa/isavar.h>
    226 #if NPCI > 0
    227 #include <dev/pci/pcivar.h>
    228 #endif
    229 
    230 
    231 #if defined(NFS_BOOT_BOOTSTATIC) && defined(DOM0OPS)
    232 static int
    233 dom0_bootstatic_callback(struct nfs_diskless *nd)
    234 {
    235 #if 0
    236 	struct ifnet *ifp = nd->nd_ifp;
    237 #endif
    238 	int flags = 0;
    239 	union xen_cmdline_parseinfo xcp;
    240 	struct sockaddr_in *sin;
    241 
    242 	memset(&xcp, 0, sizeof(xcp.xcp_netinfo));
    243 	xcp.xcp_netinfo.xi_ifno = 0; /* XXX first interface hardcoded */
    244 	xcp.xcp_netinfo.xi_root = nd->nd_root.ndm_host;
    245 	xen_parse_cmdline(XEN_PARSE_NETINFO, &xcp);
    246 
    247 	if (xcp.xcp_netinfo.xi_root[0] != '\0') {
    248 		flags |= NFS_BOOT_HAS_SERVER;
    249 		if (strchr(xcp.xcp_netinfo.xi_root, ':') != NULL)
    250 			flags |= NFS_BOOT_HAS_ROOTPATH;
    251 	}
    252 
    253 	nd->nd_myip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[0]);
    254 	nd->nd_gwip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[2]);
    255 	nd->nd_mask.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[3]);
    256 
    257 	sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
    258 	memset((void *)sin, 0, sizeof(*sin));
    259 	sin->sin_len = sizeof(*sin);
    260 	sin->sin_family = AF_INET;
    261 	sin->sin_addr.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[1]);
    262 
    263 	if (nd->nd_myip.s_addr)
    264 		flags |= NFS_BOOT_HAS_MYIP;
    265 	if (nd->nd_gwip.s_addr)
    266 		flags |= NFS_BOOT_HAS_GWIP;
    267 	if (nd->nd_mask.s_addr)
    268 		flags |= NFS_BOOT_HAS_MASK;
    269 	if (sin->sin_addr.s_addr)
    270 		flags |= NFS_BOOT_HAS_SERVADDR;
    271 
    272 	return flags;
    273 }
    274 #endif
    275 
    276 void
    277 device_register(device_t dev, void *aux)
    278 {
    279 	/*
    280 	 * Handle network interfaces here, the attachment information is
    281 	 * not available driver independently later.
    282 	 * For disks, there is nothing useful available at attach time.
    283 	 */
    284 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0 || defined(DOM0OPS)
    285 	if (device_class(dev) == DV_IFNET) {
    286 		union xen_cmdline_parseinfo xcp;
    287 
    288 #ifdef NFS_BOOT_BOOTSTATIC
    289 #ifdef DOM0OPS
    290 		if (xendomain_is_privileged()) {
    291 			nfs_bootstatic_callback = dom0_bootstatic_callback;
    292 		} else
    293 #endif
    294 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0
    295 		nfs_bootstatic_callback = xennet_bootstatic_callback;
    296 #endif
    297 #endif
    298 		xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
    299 		if (strncmp(xcp.xcp_bootdev, device_xname(dev),
    300 		    sizeof(xcp.xcp_bootdev)) == 0)
    301 		{
    302 			goto found;
    303 		}
    304 	}
    305 #endif
    306 	if (device_class(dev) == DV_IFNET) {
    307 		struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF);
    308 		if (bin == NULL)
    309 			return;
    310 
    311 		/*
    312 		 * We don't check the driver name against the device name
    313 		 * passed by the boot ROM. The ROM should stay usable
    314 		 * if the driver gets obsoleted.
    315 		 * The physical attachment information (checked below)
    316 		 * must be sufficient to identify the device.
    317 		 */
    318 
    319 		if (bin->bus == BI_BUS_ISA &&
    320 		    device_is_a(device_parent(dev), "isa")) {
    321 			struct isa_attach_args *iaa = aux;
    322 
    323 			/* compare IO base address */
    324 			/* XXXJRT what about multiple I/O addrs? */
    325 			if (iaa->ia_nio > 0 &&
    326 			    bin->addr.iobase == iaa->ia_io[0].ir_addr)
    327 				goto found;
    328 		}
    329 #if NPCI > 0
    330 		if (bin->bus == BI_BUS_PCI &&
    331 		    device_is_a(device_parent(dev), "pci")) {
    332 			struct pci_attach_args *paa = aux;
    333 			int b, d, f;
    334 
    335 			/*
    336 			 * Calculate BIOS representation of:
    337 			 *
    338 			 *	<bus,device,function>
    339 			 *
    340 			 * and compare.
    341 			 */
    342 			pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f);
    343 			if (bin->addr.tag == ((b << 8) | (d << 3) | f))
    344 				goto found;
    345 		}
    346 #endif
    347 	}
    348 	return;
    349 
    350 found:
    351 	if (booted_device) {
    352 		/* XXX should be a "panic()" */
    353 		printf("warning: double match for boot device (%s, %s)\n",
    354 		    device_xname(booted_device), device_xname(dev));
    355 		return;
    356 	}
    357 	booted_device = dev;
    358 }
    359 
    360 static int
    361 is_valid_disk(device_t dv)
    362 {
    363 
    364 	if (device_class(dv) != DV_DISK)
    365 		return (0);
    366 
    367 	return (device_is_a(dv, "dk") ||
    368 		device_is_a(dv, "sd") ||
    369 		device_is_a(dv, "wd") ||
    370 		device_is_a(dv, "ld") ||
    371 		device_is_a(dv, "ed") ||
    372 		device_is_a(dv, "xbd"));
    373 }
    374