Home | History | Annotate | Line # | Download | only in x86
autoconf.c revision 1.22
      1 /*	$NetBSD: autoconf.c,v 1.22 2019/01/28 21:19:09 bad 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.22 2019/01/28 21:19:09 bad Exp $");
     49 
     50 #include "opt_xen.h"
     51 #include "opt_multiprocessor.h"
     52 #include "opt_nfs_boot.h"
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/buf.h>
     57 #include <sys/disklabel.h>
     58 #include <sys/disk.h>
     59 #include <sys/conf.h>
     60 #include <sys/device.h>
     61 #include <sys/vnode.h>
     62 #include <sys/fcntl.h>
     63 #include <sys/dkio.h>
     64 #include <sys/proc.h>
     65 #include <sys/kauth.h>
     66 
     67 #ifdef NFS_BOOT_BOOTSTATIC
     68 #include <net/if.h>
     69 #include <net/if_ether.h>
     70 #include <netinet/in.h>
     71 #include <nfs/rpcv2.h>
     72 #include <nfs/nfsproto.h>
     73 #include <nfs/nfs.h>
     74 #include <nfs/nfsmount.h>
     75 #include <nfs/nfsdiskless.h>
     76 #include <xen/if_xennetvar.h>
     77 #endif
     78 
     79 #include <machine/pte.h>
     80 #include <machine/cpu.h>
     81 #include <machine/gdt.h>
     82 #include <machine/pcb.h>
     83 #include <machine/bootinfo.h>
     84 
     85 static int is_valid_disk(device_t);
     86 
     87 struct disklist *x86_alldisks;
     88 int x86_ndisks;
     89 
     90 #include "bios32.h"
     91 #if NBIOS32 > 0
     92 #include <machine/bios32.h>
     93 /* XXX */
     94 extern void platform_init(void);
     95 #endif
     96 
     97 #include "opt_pcibios.h"
     98 #ifdef PCIBIOS
     99 #include <dev/pci/pcireg.h>
    100 #include <dev/pci/pcivar.h>
    101 #include <i386/pci/pcibios.h>
    102 #endif
    103 
    104 #ifdef DEBUG_GEOM
    105 #define DPRINTF(a) printf a
    106 #else
    107 #define DPRINTF(a)
    108 #endif
    109 
    110 /*
    111  * Determine i/o configuration for a machine.
    112  */
    113 void
    114 cpu_configure(void)
    115 {
    116 	struct pcb *pcb;
    117 
    118 	startrtclock();
    119 
    120 #if defined(DOM0OPS)
    121 	if (xendomain_is_dom0()) {
    122 #if NBIOS32 > 0
    123 		bios32_init();
    124 		platform_init();
    125 		/* identify hypervisor type from SMBIOS */
    126 		identify_hypervisor();
    127 #endif /* NBIOS32 > 0 */
    128 	} else
    129 #endif /* DOM0OPS */
    130 		vm_guest = VM_GUEST_XEN;
    131 #ifdef PCIBIOS
    132 	pcibios_init();
    133 #endif
    134 
    135 	if (config_rootfound("mainbus", NULL) == NULL)
    136 		panic("configure: mainbus not configured");
    137 
    138 #ifdef INTRDEBUG
    139 	intr_printconfig();
    140 #endif
    141 
    142 #if NIOAPIC > 0
    143 	ioapic_enable();
    144 #endif
    145 	/* resync cr0 after FPU configuration */
    146 	pcb = lwp_getpcb(&lwp0);
    147 	pcb->pcb_cr0 = rcr0();
    148 #ifdef MULTIPROCESSOR
    149 	/* propagate this to the idle pcb's. */
    150 	cpu_init_idle_lwps();
    151 #endif
    152 
    153 	spl0();
    154 }
    155 
    156 void
    157 cpu_rootconf(void)
    158 {
    159 	cpu_bootconf();
    160 
    161 	printf("boot device: %s\n",
    162 	    booted_device ? device_xname(booted_device) :
    163 	    bootspec ? bootspec : "<unknown>");
    164 	rootconf();
    165 }
    166 
    167 
    168 /*
    169  * Attempt to find the device from which we were booted.
    170  */
    171 void
    172 cpu_bootconf(void)
    173 {
    174 	device_t dv;
    175 	deviter_t di;
    176 	union xen_cmdline_parseinfo xcp;
    177 	static char bootspecbuf[sizeof(xcp.xcp_bootdev)];
    178 
    179 	if (booted_device) {
    180 		DPRINTF(("%s: preset booted_device: %s\n", __func__, device_xname(booted_device)));
    181 		return;
    182 	}
    183 
    184 	xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
    185 
    186 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    187 	     dv != NULL;
    188 	     dv = deviter_next(&di)) {
    189 		bool is_ifnet, is_disk;
    190 		const char *devname;
    191 
    192 		is_ifnet = (device_class(dv) == DV_IFNET);
    193 		is_disk = is_valid_disk(dv);
    194 		devname = device_xname(dv);
    195 
    196 		if (!is_ifnet && !is_disk)
    197 			continue;
    198 
    199 		if (is_disk && xcp.xcp_bootdev[0] == 0) {
    200 			booted_device = dv;
    201 			break;
    202 		}
    203 
    204 		if (strncmp(xcp.xcp_bootdev, devname, strlen(devname)))
    205 			continue;
    206 
    207 		if (is_disk && strlen(xcp.xcp_bootdev) > strlen(devname)) {
    208 			/* XXX check device_cfdata as in x86_autoconf.c? */
    209 			booted_partition = toupper(
    210 				xcp.xcp_bootdev[strlen(devname)]) - 'A';
    211 			DPRINTF(("%s: booted_partition: %d\n", __func__, booted_partition));
    212 		}
    213 
    214 		booted_device = dv;
    215 		booted_method = "bootinfo/bootdev";
    216 		break;
    217 	}
    218 	deviter_release(&di);
    219 
    220 	if (booted_device) {
    221 		DPRINTF(("%s: booted_device: %s\n", __func__, device_xname(booted_device)));
    222 		return;
    223 	}
    224 
    225 	/*
    226 	 * not a boot device name, pass through to MI code
    227 	 */
    228 	if (xcp.xcp_bootdev[0] != '\0') {
    229 		strlcpy(bootspecbuf, xcp.xcp_bootdev, sizeof(bootspecbuf));
    230 		bootspec = bootspecbuf;
    231 		booted_method = "bootinfo/bootspec";
    232 		DPRINTF(("%s: bootspec: %s\n", __func__, bootspec));
    233 		return;
    234 	}
    235 }
    236 
    237 #include "pci.h"
    238 
    239 #include <dev/isa/isavar.h>
    240 #if NPCI > 0
    241 #include <dev/pci/pcivar.h>
    242 #endif
    243 
    244 
    245 #if defined(NFS_BOOT_BOOTSTATIC) && defined(DOM0OPS)
    246 static int
    247 dom0_bootstatic_callback(struct nfs_diskless *nd)
    248 {
    249 #if 0
    250 	struct ifnet *ifp = nd->nd_ifp;
    251 #endif
    252 	int flags = 0;
    253 	union xen_cmdline_parseinfo xcp;
    254 	struct sockaddr_in *sin;
    255 
    256 	memset(&xcp, 0, sizeof(xcp.xcp_netinfo));
    257 	xcp.xcp_netinfo.xi_ifno = 0; /* XXX first interface hardcoded */
    258 	xcp.xcp_netinfo.xi_root = nd->nd_root.ndm_host;
    259 	xen_parse_cmdline(XEN_PARSE_NETINFO, &xcp);
    260 
    261 	if (xcp.xcp_netinfo.xi_root[0] != '\0') {
    262 		flags |= NFS_BOOT_HAS_SERVER;
    263 		if (strchr(xcp.xcp_netinfo.xi_root, ':') != NULL)
    264 			flags |= NFS_BOOT_HAS_ROOTPATH;
    265 	}
    266 
    267 	nd->nd_myip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[0]);
    268 	nd->nd_gwip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[2]);
    269 	nd->nd_mask.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[3]);
    270 
    271 	sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
    272 	memset((void *)sin, 0, sizeof(*sin));
    273 	sin->sin_len = sizeof(*sin);
    274 	sin->sin_family = AF_INET;
    275 	sin->sin_addr.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[1]);
    276 
    277 	if (nd->nd_myip.s_addr)
    278 		flags |= NFS_BOOT_HAS_MYIP;
    279 	if (nd->nd_gwip.s_addr)
    280 		flags |= NFS_BOOT_HAS_GWIP;
    281 	if (nd->nd_mask.s_addr)
    282 		flags |= NFS_BOOT_HAS_MASK;
    283 	if (sin->sin_addr.s_addr)
    284 		flags |= NFS_BOOT_HAS_SERVADDR;
    285 
    286 	return flags;
    287 }
    288 #endif
    289 
    290 void
    291 device_register(device_t dev, void *aux)
    292 {
    293 	/*
    294 	 * Handle network interfaces here, the attachment information is
    295 	 * not available driver independently later.
    296 	 * For disks, there is nothing useful available at attach time.
    297 	 */
    298 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0 || defined(DOM0OPS)
    299 	if (device_class(dev) == DV_IFNET) {
    300 		union xen_cmdline_parseinfo xcp;
    301 
    302 #ifdef NFS_BOOT_BOOTSTATIC
    303 #ifdef DOM0OPS
    304 		if (xendomain_is_privileged()) {
    305 			nfs_bootstatic_callback = dom0_bootstatic_callback;
    306 		} else
    307 #endif
    308 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0
    309 		nfs_bootstatic_callback = xennet_bootstatic_callback;
    310 #endif
    311 #endif
    312 		xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
    313 		if (strncmp(xcp.xcp_bootdev, device_xname(dev),
    314 		    sizeof(xcp.xcp_bootdev)) == 0)
    315 		{
    316 			goto found;
    317 		}
    318 	}
    319 #endif
    320 	if (device_class(dev) == DV_IFNET) {
    321 		struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF);
    322 		if (bin == NULL)
    323 			return;
    324 
    325 		/*
    326 		 * We don't check the driver name against the device name
    327 		 * passed by the boot ROM. The ROM should stay usable
    328 		 * if the driver gets obsoleted.
    329 		 * The physical attachment information (checked below)
    330 		 * must be sufficient to identify the device.
    331 		 */
    332 
    333 		if (bin->bus == BI_BUS_ISA &&
    334 		    device_is_a(device_parent(dev), "isa")) {
    335 			struct isa_attach_args *iaa = aux;
    336 
    337 			/* compare IO base address */
    338 			/* XXXJRT what about multiple I/O addrs? */
    339 			if (iaa->ia_nio > 0 &&
    340 			    bin->addr.iobase == iaa->ia_io[0].ir_addr)
    341 				goto found;
    342 		}
    343 #if NPCI > 0
    344 		if (bin->bus == BI_BUS_PCI &&
    345 		    device_is_a(device_parent(dev), "pci")) {
    346 			struct pci_attach_args *paa = aux;
    347 			int b, d, f;
    348 
    349 			/*
    350 			 * Calculate BIOS representation of:
    351 			 *
    352 			 *	<bus,device,function>
    353 			 *
    354 			 * and compare.
    355 			 */
    356 			pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f);
    357 			if (bin->addr.tag == ((b << 8) | (d << 3) | f))
    358 				goto found;
    359 		}
    360 #endif
    361 	}
    362 	return;
    363 
    364 found:
    365 	if (booted_device) {
    366 		/* XXX should be a "panic()" */
    367 		printf("warning: double match for boot device (%s, %s)\n",
    368 		    device_xname(booted_device), device_xname(dev));
    369 		return;
    370 	}
    371 	booted_device = dev;
    372 }
    373 
    374 static int
    375 is_valid_disk(device_t dv)
    376 {
    377 	if (device_class(dv) != DV_DISK)
    378 		return (0);
    379 
    380 	return (device_is_a(dv, "dk") ||
    381 		device_is_a(dv, "sd") ||
    382 		device_is_a(dv, "wd") ||
    383 		device_is_a(dv, "ld") ||
    384 		device_is_a(dv, "ed") ||
    385 		device_is_a(dv, "xbd"));
    386 }
    387