Permalink
Browse files

MFC r257293.

Add support for PCI-to-ISA LPC bridge emulation. If the LPC bus is attached
to a virtual machine then we implicitly create COM1 and COM2 ISA devices.

Prior to this change the only way of attaching a COM port to the virtual
machine was by presenting it as a PCI device that is mapped at the legacy
I/O address 0x3F8 or 0x2F8.

There were some issues with the original approach:
- It did not work at all with UEFI because UEFI will reprogram the PCI device
  BARs and remap the COM1/COM2 ports at non-legacy addresses.
- OpenBSD GENERIC kernel does not create a /dev/console because it expects
  the uart device at the legacy 0x3F8/0x2F8 address to be an ISA device.
- It was functional with a FreeBSD guest but caused the console to appear
  on /dev/ttyu2 which was not intuitive.

The uart emulation is now independent of the bus on which it resides. Thus it
is possible to have uart devices on the PCI bus in addition to the legacy
COM1/COM2 devices behind the LPC bus.

The command line option to attach ISA COM1/COM2 ports to a virtual machine is
"-s <bus>,lpc -l com1,stdio".

The command line option to create a PCI-attached uart device is:
"-s <bus>,uart[,stdio]"

The command line option to create PCI-attached COM1/COM2 device is:
"-S <bus>,uart[,stdio]". This style of creating COM ports is deprecated.

Approved by:	re (glebius)
  • Loading branch information...
1 parent 9a9c7f1 commit 249db5aac359e977e2e92a1e27132c9d3037f3dd @neelnatu neelnatu committed Oct 30, 2013
@@ -157,7 +157,7 @@ while [ 1 ]; do
exit 1
fi
BOOTDISK=${isofile}
- installer_opt="-s 3:0,virtio-blk,${BOOTDISK}"
+ installer_opt="-s 31:0,virtio-blk,${BOOTDISK}"
else
BOOTDISK=${virtio_diskdev}
installer_opt=""
@@ -171,10 +171,11 @@ while [ 1 ]; do
${FBSDRUN} -c ${cpus} -m ${memsize} ${apic_opt} -AI -H -P \
-g ${gdbport} \
-s 0:0,hostbridge \
- -s 1:0,virtio-net,${tapdev} \
- -s 2:0,virtio-blk,${virtio_diskdev} \
+ -s 1:0,lpc \
+ -s 2:0,virtio-net,${tapdev} \
+ -s 3:0,virtio-blk,${virtio_diskdev} \
+ -l com1,stdio \
${installer_opt} \
- -S 31,uart,stdio \
${vmname}
if [ $? -ne 0 ]; then
break
View
@@ -7,10 +7,10 @@ PROG= bhyve
DEBUG_FLAGS= -g -O0
SRCS= acpi.c atpic.c bhyverun.c block_if.c consport.c dbgport.c elcr.c
-SRCS+= inout.c ioapic.c mem.c mevent.c mptbl.c pci_ahci.c
-SRCS+= pci_emul.c pci_hostbridge.c pci_passthru.c pci_virtio_block.c
+SRCS+= inout.c ioapic.c legacy_irq.c mem.c mevent.c mptbl.c pci_ahci.c
+SRCS+= pci_emul.c pci_hostbridge.c pci_lpc.c pci_passthru.c pci_virtio_block.c
SRCS+= pci_virtio_net.c pci_uart.c pit_8254.c pmtmr.c post.c rtc.c
-SRCS+= virtio.c xmsr.c spinup_ap.c
+SRCS+= uart_emul.c virtio.c xmsr.c spinup_ap.c
.PATH: ${.CURDIR}/../../sys/amd64/vmm
SRCS+= vmm_instruction_emul.c
View
@@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$");
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <err.h>
#include <libgen.h>
#include <unistd.h>
@@ -53,10 +54,12 @@ __FBSDID("$FreeBSD$");
#include "acpi.h"
#include "inout.h"
#include "dbgport.h"
+#include "legacy_irq.h"
#include "mem.h"
#include "mevent.h"
#include "mptbl.h"
#include "pci_emul.h"
+#include "pci_lpc.h"
#include "xmsr.h"
#include "ioapic.h"
#include "spinup_ap.h"
@@ -121,9 +124,8 @@ usage(int code)
{
fprintf(stderr,
- "Usage: %s [-aehAHIPW][-g <gdb port>][-s <pci>][-S <pci>]"
- "[-c vcpus][-p pincpu][-m mem]"
- " <vmname>\n"
+ "Usage: %s [-aehAHIPW] [-g <gdb port>] [-s <pci>] [-S <pci>]\n"
+ " %*s [-c vcpus] [-p pincpu] [-m mem] [-l <lpc>] <vm>\n"
" -a: local apic is in XAPIC mode (default is X2APIC)\n"
" -A: create an ACPI table\n"
" -g: gdb port\n"
@@ -132,13 +134,14 @@ usage(int code)
" -H: vmexit from the guest on hlt\n"
" -I: present an ioapic to the guest\n"
" -P: vmexit from the guest on pause\n"
- " -W: force virtio to use single-vector MSI\n"
- " -e: exit on unhandled i/o access\n"
+ " -W: force virtio to use single-vector MSI\n"
+ " -e: exit on unhandled I/O access\n"
" -h: help\n"
" -s: <slot,driver,configinfo> PCI slot config\n"
" -S: <slot,driver,configinfo> legacy PCI slot config\n"
+ " -l: LPC device configuration\n"
" -m: memory size in MB\n",
- progname);
+ progname, (int)strlen(progname), "");
exit(code);
}
@@ -553,7 +556,7 @@ main(int argc, char *argv[])
ioapic = 0;
memsize = 256 * MB;
- while ((c = getopt(argc, argv, "abehAHIPWp:g:c:s:S:m:")) != -1) {
+ while ((c = getopt(argc, argv, "abehAHIPWp:g:c:s:S:m:l:")) != -1) {
switch (c) {
case 'a':
disable_x2apic = 1;
@@ -573,6 +576,12 @@ main(int argc, char *argv[])
case 'g':
gdb_port = atoi(optarg);
break;
+ case 'l':
+ if (lpc_device_parse(optarg) != 0) {
+ errx(EX_USAGE, "invalid lpc device "
+ "configuration '%s'", optarg);
+ }
+ break;
case 's':
if (pci_parse_slot(optarg, 0) != 0)
exit(1);
@@ -640,6 +649,7 @@ main(int argc, char *argv[])
init_mem();
init_inout();
+ legacy_irq_init();
rtc_init(ctx);
View
@@ -84,7 +84,7 @@ register_default_iohandler(int start, int size)
iop.name = "default";
iop.port = start;
iop.size = size;
- iop.flags = IOPORT_F_INOUT;
+ iop.flags = IOPORT_F_INOUT | IOPORT_F_DEFAULT;
iop.handler = default_inout;
register_inout(&iop);
@@ -159,7 +159,18 @@ register_inout(struct inout_port *iop)
int i;
VERIFY_IOPORT(iop->port, iop->size);
-
+
+ /*
+ * Verify that the new registration is not overwriting an already
+ * allocated i/o range.
+ */
+ if ((iop->flags & IOPORT_F_DEFAULT) == 0) {
+ for (i = iop->port; i < iop->port + iop->size; i++) {
+ if ((inout_handlers[i].flags & IOPORT_F_DEFAULT) == 0)
+ return (-1);
+ }
+ }
+
for (i = iop->port; i < iop->port + iop->size; i++) {
inout_handlers[i].name = iop->name;
inout_handlers[i].flags = iop->flags;
View
@@ -46,7 +46,13 @@ struct inout_port {
};
#define IOPORT_F_IN 0x1
#define IOPORT_F_OUT 0x2
-#define IOPORT_F_INOUT 0x3
+#define IOPORT_F_INOUT (IOPORT_F_IN | IOPORT_F_OUT)
+
+/*
+ * The following flags are used internally and must not be used by
+ * device models.
+ */
+#define IOPORT_F_DEFAULT 0x80000000 /* claimed by default handler */
#define INOUT_PORT(name, port, flags, handler) \
static struct inout_port __CONCAT(__inout_port, __LINE__) = { \
@@ -0,0 +1,80 @@
+/*-
+ * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <stdbool.h>
+#include <assert.h>
+
+/*
+ * Used to keep track of legacy interrupt owners/requestors
+ */
+#define NLIRQ 16
+
+static struct lirqinfo {
+ bool li_generic;
+ bool li_allocated;
+} lirq[NLIRQ];
+
+void
+legacy_irq_init(void)
+{
+
+ /*
+ * Allow ISA IRQs 5,10,11,12, and 15 to be available for generic use.
+ */
+ lirq[5].li_generic = true;
+ lirq[10].li_generic = true;
+ lirq[11].li_generic = true;
+ lirq[12].li_generic = true;
+ lirq[15].li_generic = true;
+}
+
+int
+legacy_irq_alloc(int irq)
+{
+ int i;
+
+ assert(irq < NLIRQ);
+
+ if (irq < 0) {
+ for (i = 0; i < NLIRQ; i++) {
+ if (lirq[i].li_generic && !lirq[i].li_allocated) {
+ irq = i;
+ break;
+ }
+ }
+ } else {
+ if (lirq[irq].li_allocated)
+ irq = -1;
+ }
+
+ if (irq >= 0) {
+ lirq[irq].li_allocated = true;
+ return (irq);
+ } else
+ return (-1);
+}
@@ -0,0 +1,41 @@
+/*-
+ * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _LEGACY_IRQ_H_
+#define _LEGACY_IRQ_H_
+
+/*
+ * Allocate a legacy irq. The argument 'irq' can be set to -1 to allocate any
+ * available irq.
+ *
+ * Returns -1 on failure or the allocated irq number on success.
+ */
+int legacy_irq_alloc(int irq);
+void legacy_irq_init(void);
+
+#endif
Oops, something went wrong.

0 comments on commit 249db5a

Please sign in to comment.