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