qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH v10 1/2] virtio-console: Add a virtio-serial bus, su


From: Amit Shah
Subject: [Qemu-devel] [PATCH v10 1/2] virtio-console: Add a virtio-serial bus, support for multiple ports
Date: Mon, 26 Oct 2009 17:30:36 +0530

This patch migrates virtio-console to the qdev infrastructure and
creates a new virtio-serial bus on which multiple ports are exposed as
devices. The bulk of the code now resides in a new file with
virtio-console.c being just a simple qdev device.

This interface enables spawning of multiple virtio consoles as well as generic
serial ports.

The older -virtconsole argument still works, but when using the new
functionality, it is recommended to use

    -device virtio-serial-pci -device virtconsole,...

The virtconsole device type accepts a chardev as an argument and a 'name'
argument to identify the corresponding consoles on the host as well as the
guest. The name, if given, is exposed via the 'name' sysfs attribute.

Care has been taken to ensure compatibility with kernels that do not
support multiple ports as well as accepting incoming migrations from older
qemu versions.

Signed-off-by: Amit Shah <address@hidden>
---
 Makefile.target        |    2 +-
 hw/pc.c                |    9 -
 hw/ppc440_bamboo.c     |    7 -
 hw/qdev.c              |    8 +-
 hw/virtio-console.c    |  180 +++++-------
 hw/virtio-console.h    |   19 --
 hw/virtio-pci.c        |    8 +-
 hw/virtio-serial-bus.c |  785 ++++++++++++++++++++++++++++++++++++++++++++++++
 hw/virtio-serial.h     |  227 ++++++++++++++
 hw/virtio.h            |    2 +-
 qemu-options.hx        |    6 +-
 sysemu.h               |    6 -
 vl.c                   |   65 +++--
 13 files changed, 1139 insertions(+), 185 deletions(-)
 delete mode 100644 hw/virtio-console.h
 create mode 100644 hw/virtio-serial-bus.c
 create mode 100644 hw/virtio-serial.h

diff --git a/Makefile.target b/Makefile.target
index 8d146c5..fd86eeb 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -157,7 +157,7 @@ ifdef CONFIG_SOFTMMU
 obj-y = vl.o monitor.o pci.o machine.o gdbstub.o
 # virtio has to be here due to weird dependency between PCI and virtio-net.
 # need to fix this properly
-obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-console.o 
virtio-pci.o
+obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-serial-bus.o 
virtio-console.o virtio-pci.o
 obj-$(CONFIG_KVM) += kvm.o kvm-all.o
 obj-$(CONFIG_ISA_MMIO) += isa_mmio.o
 LIBS+=-lz
diff --git a/hw/pc.c b/hw/pc.c
index 408d6d6..3dbe718 100644
--- a/hw/pc.c
+++ b/hw/pc.c
@@ -1319,15 +1319,6 @@ static void pc_init1(ram_addr_t ram_size,
             pci_create_simple(pci_bus, -1, "lsi53c895a");
         }
     }
-
-    /* Add virtio console devices */
-    if (pci_enabled) {
-        for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
-            if (virtcon_hds[i]) {
-                pci_create_simple(pci_bus, -1, "virtio-console-pci");
-            }
-        }
-    }
 }
 
 static void pc_init_pci(ram_addr_t ram_size,
diff --git a/hw/ppc440_bamboo.c b/hw/ppc440_bamboo.c
index a488240..1ab9872 100644
--- a/hw/ppc440_bamboo.c
+++ b/hw/ppc440_bamboo.c
@@ -108,13 +108,6 @@ static void bamboo_init(ram_addr_t ram_size,
     env = ppc440ep_init(&ram_size, &pcibus, pci_irq_nrs, 1, cpu_model);
 
     if (pcibus) {
-        /* Add virtio console devices */
-        for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
-            if (virtcon_hds[i]) {
-                pci_create_simple(pcibus, -1, "virtio-console-pci");
-            }
-        }
-
         /* Register network interfaces. */
         for (i = 0; i < nb_nics; i++) {
             /* There are no PCI NICs on the Bamboo board, but there are
diff --git a/hw/qdev.c b/hw/qdev.c
index 20f931c..374d2d0 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -321,13 +321,9 @@ void qdev_machine_creation_done(void)
 CharDriverState *qdev_init_chardev(DeviceState *dev)
 {
     static int next_serial;
-    static int next_virtconsole;
+
     /* FIXME: This is a nasty hack that needs to go away.  */
-    if (strncmp(dev->info->name, "virtio", 6) == 0) {
-        return virtcon_hds[next_virtconsole++];
-    } else {
-        return serial_hds[next_serial++];
-    }
+    return serial_hds[next_serial++];
 }
 
 BusState *qdev_get_parent_bus(DeviceState *dev)
diff --git a/hw/virtio-console.c b/hw/virtio-console.c
index 57f8f89..a14133b 100644
--- a/hw/virtio-console.c
+++ b/hw/virtio-console.c
@@ -1,143 +1,109 @@
 /*
- * Virtio Console Device
+ * Virtio Console and Generic Port Devices
  *
- * Copyright IBM, Corp. 2008
+ * Copyright Red Hat, Inc. 2009
  *
  * Authors:
- *  Christian Ehrhardt <address@hidden>
+ *  Amit Shah <address@hidden>
  *
  * This work is licensed under the terms of the GNU GPL, version 2.  See
  * the COPYING file in the top-level directory.
- *
  */
 
-#include "hw.h"
 #include "qemu-char.h"
-#include "virtio.h"
-#include "virtio-console.h"
-
+#include "virtio-serial.h"
 
-typedef struct VirtIOConsole
-{
-    VirtIODevice vdev;
-    VirtQueue *ivq, *ovq;
+typedef struct VirtConsole {
+    VirtIOSerialPort port;
     CharDriverState *chr;
-} VirtIOConsole;
+} VirtConsole;
 
-static VirtIOConsole *to_virtio_console(VirtIODevice *vdev)
-{
-    return (VirtIOConsole *)vdev;
-}
 
-static void virtio_console_handle_output(VirtIODevice *vdev, VirtQueue *vq)
+/* Callback function that's called when the guest sends us data */
+static size_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
 {
-    VirtIOConsole *s = to_virtio_console(vdev);
-    VirtQueueElement elem;
-
-    while (virtqueue_pop(vq, &elem)) {
-        ssize_t len = 0;
-        int d;
-
-        for (d = 0; d < elem.out_num; d++) {
-            len += qemu_chr_write(s->chr, (uint8_t *)elem.out_sg[d].iov_base,
-                                  elem.out_sg[d].iov_len);
-        }
-        virtqueue_push(vq, &elem, len);
-        virtio_notify(vdev, vq);
-    }
-}
+    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
 
-static void virtio_console_handle_input(VirtIODevice *vdev, VirtQueue *vq)
-{
+    return qemu_chr_write(vcon->chr, buf, len);
 }
 
-static uint32_t virtio_console_get_features(VirtIODevice *vdev)
-{
-    return 0;
-}
 
-static int vcon_can_read(void *opaque)
+/* Readiness of the guest to accept data on a port */
+static int chr_can_read(void *opaque)
 {
-    VirtIOConsole *s = (VirtIOConsole *) opaque;
-
-    if (!virtio_queue_ready(s->ivq) ||
-        !(s->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
-        virtio_queue_empty(s->ivq))
-        return 0;
-
-    /* current implementations have a page sized buffer.
-     * We fall back to a one byte per read if there is not enough room.
-     * It would be cool to have a function that returns the available byte
-     * instead of checking for a limit */
-    if (virtqueue_avail_bytes(s->ivq, TARGET_PAGE_SIZE, 0))
-        return TARGET_PAGE_SIZE;
-    if (virtqueue_avail_bytes(s->ivq, 1, 0))
-        return 1;
-    return 0;
-}
+    VirtConsole *vcon = opaque;
 
-static void vcon_read(void *opaque, const uint8_t *buf, int size)
-{
-    VirtIOConsole *s = (VirtIOConsole *) opaque;
-    VirtQueueElement elem;
-    int offset = 0;
-
-    /* The current kernel implementation has only one outstanding input
-     * buffer of PAGE_SIZE. Nevertheless, this function is prepared to
-     * handle multiple buffers with multiple sg element for input */
-    while (offset < size) {
-        int i = 0;
-        if (!virtqueue_pop(s->ivq, &elem))
-                break;
-        while (offset < size && i < elem.in_num) {
-            int len = MIN(elem.in_sg[i].iov_len, size - offset);
-            memcpy(elem.in_sg[i].iov_base, buf + offset, len);
-            offset += len;
-            i++;
-        }
-        virtqueue_push(s->ivq, &elem, size);
-    }
-    virtio_notify(&s->vdev, s->ivq);
+    return virtio_serial_guest_ready(&vcon->port);
 }
 
-static void vcon_event(void *opaque, int event)
+/* Send data from a char device over to the guest */
+static void chr_read(void *opaque, const uint8_t *buf, int size)
 {
-    /* we will ignore any event for the time being */
+    VirtConsole *vcon = opaque;
+
+    virtio_serial_write(&vcon->port, buf, size);
 }
 
-static void virtio_console_save(QEMUFile *f, void *opaque)
+static void chr_event(void *opaque, int event)
 {
-    VirtIOConsole *s = opaque;
+    VirtConsole *vcon = opaque;
 
-    virtio_save(&s->vdev, f);
+    switch (event) {
+    case CHR_EVENT_OPENED: {
+        virtio_serial_open(&vcon->port);
+        break;
+    }
+    case CHR_EVENT_CLOSED:
+        virtio_serial_close(&vcon->port);
+        break;
+    }
 }
 
-static int virtio_console_load(QEMUFile *f, void *opaque, int version_id)
+/* Virtio Console Ports */
+static int vcon_initfn(VirtIOSerialDevice *dev)
 {
-    VirtIOConsole *s = opaque;
-
-    if (version_id != 1)
-        return -EINVAL;
-
-    virtio_load(&s->vdev, f);
+    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
+    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
+
+    port->info = dev->info;
+
+    /*
+     * We're not interested in data the guest sends while nothing is
+     * connected on the host side. Just ignore it instead of saving it
+     * for later consumption
+     */
+    port->cache_buffers = 0;
+
+    /* Tell the guest we're a console so it attaches us to an hvc console */
+    port->is_console = true;
+
+    /*
+     * For console devices, a tty is spawned on /dev/hvc0 and our
+     * /dev/vconNN will never be opened. Set this here.
+     */
+    port->guest_connected = true;
+
+    if (vcon->chr) {
+        qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
+                              vcon);
+    }
     return 0;
 }
 
-VirtIODevice *virtio_console_init(DeviceState *dev)
+static VirtIOSerialPortInfo virtcon_info = {
+    .qdev.name     = "virtconsole",
+    .qdev.size     = sizeof(VirtConsole),
+    .init          = vcon_initfn,
+    .have_data     = flush_buf,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_CHR("chardev", VirtConsole, chr),
+        DEFINE_PROP_STRING("name", VirtConsole, port.name),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
+
+static void virtcon_register(void)
 {
-    VirtIOConsole *s;
-    s = (VirtIOConsole *)virtio_common_init("virtio-console",
-                                            VIRTIO_ID_CONSOLE,
-                                            0, sizeof(VirtIOConsole));
-    s->vdev.get_features = virtio_console_get_features;
-
-    s->ivq = virtio_add_queue(&s->vdev, 128, virtio_console_handle_input);
-    s->ovq = virtio_add_queue(&s->vdev, 128, virtio_console_handle_output);
-
-    s->chr = qdev_init_chardev(dev);
-    qemu_chr_add_handlers(s->chr, vcon_can_read, vcon_read, vcon_event, s);
-
-    register_savevm("virtio-console", -1, 1, virtio_console_save, 
virtio_console_load, s);
-
-    return &s->vdev;
+    virtio_serial_port_qdev_register(&virtcon_info);
 }
+device_init(virtcon_register)
diff --git a/hw/virtio-console.h b/hw/virtio-console.h
deleted file mode 100644
index 84d0717..0000000
--- a/hw/virtio-console.h
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Virtio Console Support
- *
- * Copyright IBM, Corp. 2008
- *
- * Authors:
- *  Christian Ehrhardt <address@hidden>
- *
- * This work is licensed under the terms of the GNU GPL, version 2.  See
- * the COPYING file in the top-level directory.
- *
- */
-#ifndef _QEMU_VIRTIO_CONSOLE_H
-#define _QEMU_VIRTIO_CONSOLE_H
-
-/* The ID for virtio console */
-#define VIRTIO_ID_CONSOLE 3
-
-#endif
diff --git a/hw/virtio-pci.c b/hw/virtio-pci.c
index e07a2a7..cee15b7 100644
--- a/hw/virtio-pci.c
+++ b/hw/virtio-pci.c
@@ -467,7 +467,7 @@ static int virtio_blk_exit_pci(PCIDevice *pci_dev)
     return virtio_exit_pci(pci_dev);
 }
 
-static int virtio_console_init_pci(PCIDevice *pci_dev)
+static int virtio_serial_init_pci(PCIDevice *pci_dev)
 {
     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
     VirtIODevice *vdev;
@@ -477,7 +477,7 @@ static int virtio_console_init_pci(PCIDevice *pci_dev)
         proxy->class_code != PCI_CLASS_OTHERS)          /* qemu-kvm  */
         proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
 
-    vdev = virtio_console_init(&pci_dev->qdev);
+    vdev = virtio_serial_init(&pci_dev->qdev);
     if (!vdev) {
         return -1;
     }
@@ -551,9 +551,9 @@ static PCIDeviceInfo virtio_info[] = {
         },
         .qdev.reset = virtio_pci_reset,
     },{
-        .qdev.name = "virtio-console-pci",
+        .qdev.name = "virtio-serial-pci",
         .qdev.size = sizeof(VirtIOPCIProxy),
-        .init      = virtio_console_init_pci,
+        .init      = virtio_serial_init_pci,
         .exit      = virtio_exit_pci,
         .qdev.props = (Property[]) {
             DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
diff --git a/hw/virtio-serial-bus.c b/hw/virtio-serial-bus.c
new file mode 100644
index 0000000..7f8281b
--- /dev/null
+++ b/hw/virtio-serial-bus.c
@@ -0,0 +1,785 @@
+/*
+ * A bus for connecting virtio serial and console ports
+ *
+ * Copyright (C) 2009 Red Hat, Inc.
+ *
+ * Author(s):
+ *  Amit Shah <address@hidden>
+ *
+ * Some earlier parts are:
+ *  Copyright IBM, Corp. 2008
+ * authored by
+ *  Christian Ehrhardt <address@hidden>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "monitor.h"
+#include "qemu-queue.h"
+#include "sysbus.h"
+#include "virtio.h"
+#include "virtio-serial.h"
+
+/* The virtio-serial bus on top of which the ports will ride as devices */
+struct VirtIOSerialBus {
+    BusState qbus;
+    VirtIOSerial *vser;
+};
+
+struct VirtIOSerial {
+    VirtIODevice vdev;
+    VirtQueue *ivq, *ovq;
+
+    VirtIOSerialBus *bus;
+
+    VirtIOSerialPort* ports[MAX_VIRTIO_SERIAL_PORTS];
+    struct virtio_console_config config;
+
+    uint32_t guest_features;
+};
+
+/* This struct holds individual buffers received for each port */
+typedef struct VirtIOSerialPortBuffer {
+    QTAILQ_ENTRY(VirtIOSerialPortBuffer) next;
+
+    uint8_t *buf;
+
+    size_t len; /* length of the buffer */
+
+    /*
+     * The size of one write request as issued by the guest. The
+     * buffer could be split in this list but using the size value in
+     * the first buffer for each write we can identify complete
+     * writes
+     */
+    size_t size;
+} VirtIOSerialPortBuffer;
+
+
+static VirtIOSerialPort *get_port_from_id(VirtIOSerial *vser, uint32_t id)
+{
+    if (id > vser->config.nr_active_ports) {
+        return NULL;
+    }
+    return vser->ports[id];
+}
+
+static uint32_t get_id_from_port(VirtIOSerialPort *port)
+{
+    return port->id;
+}
+
+static bool use_multiport(VirtIOSerial *vser)
+{
+    return vser->guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
+}
+
+static bool is_internal(uint32_t flags)
+{
+    return flags & VIRTIO_CONSOLE_ID_INTERNAL;
+}
+
+
+static size_t write_to_port(VirtIOSerialPort *port,
+                            const uint8_t *buf, size_t size, uint32_t flags)
+{
+    VirtQueue *vq = port->vser->ivq;
+    VirtQueueElement elem;
+    size_t offset = 0;
+    size_t len = 0;
+
+    if (!virtio_queue_ready(vq)) {
+        return 0;
+    }
+    if (!use_multiport(port->vser) && is_internal(flags)) {
+        return 0;
+    }
+    while (offset < size) {
+        struct virtio_console_header header;
+        int i, header_len;
+
+        header_len = use_multiport(port->vser) ? sizeof(header) : 0;
+
+        if (!virtqueue_pop(vq, &elem)) {
+            break;
+        }
+        if (elem.in_sg[0].iov_len < header_len) {
+            /* We can't even store our port number in this buffer. Bug? */
+            qemu_error("virtio-serial: size %zd less than expected\n",
+                    elem.in_sg[0].iov_len);
+            exit(1);
+        }
+        header.id = get_id_from_port(port);
+        header.flags = flags;
+        memcpy(elem.in_sg[0].iov_base, &header, header_len);
+
+        for (i = 0; offset < size && i < elem.in_num; i++) {
+            len = MIN(elem.in_sg[i].iov_len - header_len, size - offset);
+
+            memcpy(elem.in_sg[i].iov_base + header_len, buf + offset, len);
+            offset += len;
+            header_len = 0;
+        }
+        header_len = use_multiport(port->vser) ? sizeof(header) : 0;
+        virtqueue_push(vq, &elem, len + header_len);
+    }
+    virtio_notify(&port->vser->vdev, vq);
+    return offset;
+}
+
+static void send_control_event(VirtIOSerialPort *port,
+                               struct virtio_console_control *cpkt)
+{
+    write_to_port(port, (uint8_t *)cpkt, sizeof(*cpkt),
+                  VIRTIO_CONSOLE_ID_INTERNAL);
+}
+
+
+static bool has_complete_data(VirtIOSerialPort *port)
+{
+    VirtIOSerialPortBuffer *buf;
+    size_t len, size;
+
+    len = 0;
+    size = 0;
+    QTAILQ_FOREACH(buf, &port->unflushed_buffer_head, next) {
+        if (!buf->size && buf == QTAILQ_FIRST(&port->unflushed_buffer_head)) {
+            /* We have a buffer that's lost its way; just flush it */
+            return true;
+        }
+        if (size && buf->size) {
+            /* Start of the next write request */
+            return true;
+        }
+        if (buf->size) {
+            size = buf->size;
+        }
+        len += buf->len;
+        if (len == size) {
+            return true;
+        }
+    }
+    return false;
+}
+
+static void flush_queue(VirtIOSerialPort *port)
+{
+    VirtIOSerialPortBuffer *buf, *buf2;
+    uint8_t *outbuf;
+    size_t outlen, outsize;
+
+    /*
+     * If a device is interested in buffering packets till it's
+     * opened, cache the data the guest sends us till a connection is
+     * established.
+     */
+    if (!port->host_connected && port->cache_buffers) {
+        return;
+    }
+    while (!QTAILQ_EMPTY(&port->unflushed_buffer_head)) {
+        if (!has_complete_data(port)) {
+            break;
+        }
+        buf = QTAILQ_FIRST(&port->unflushed_buffer_head);
+        if (!buf->size) {
+            /*
+             * This is a buf that didn't get consumed as part of a
+             * previous data stream. Bad thing, shouldn't
+             * happen. But let's handle it nonetheless
+             */
+            pthread_mutex_lock(&port->buffer_list_lock);
+            QTAILQ_REMOVE(&port->unflushed_buffer_head, buf, next);
+            port->nr_bytes -= buf->len;
+            pthread_mutex_unlock(&port->buffer_list_lock);
+            if (port->host_connected) {
+                port->info->have_data(port, buf->buf, buf->len);
+            }
+            qemu_free(buf->buf);
+            qemu_free(buf);
+            continue;
+        }
+        outlen = 0;
+        outsize = buf->size;
+        outbuf = qemu_mallocz(outsize);
+        QTAILQ_FOREACH_SAFE(buf, &port->unflushed_buffer_head, next, buf2) {
+            memcpy(outbuf + outlen, buf->buf, buf->len);
+            outlen += buf->len;
+            pthread_mutex_lock(&port->buffer_list_lock);
+            QTAILQ_REMOVE(&port->unflushed_buffer_head, buf, next);
+            port->nr_bytes -= buf->len;
+            pthread_mutex_unlock(&port->buffer_list_lock);
+            qemu_free(buf->buf);
+            qemu_free(buf);
+
+            if (outlen == outsize) {
+                break;
+            }
+        }
+        if (port->host_connected) {
+            port->info->have_data(port, outbuf, outlen);
+        }
+        qemu_free(outbuf);
+    }
+    if (port->host_throttled && port->nr_bytes < port->byte_limit) {
+        struct virtio_console_control cpkt;
+
+        port->host_throttled = false;
+        cpkt.event = VIRTIO_CONSOLE_THROTTLE_PORT;
+        cpkt.value = false;
+        send_control_event(port, &cpkt);
+    }
+}
+
+/* Functions for use inside qemu to open and read from/write to ports */
+int virtio_serial_open(VirtIOSerialPort *port)
+{
+    struct virtio_console_control cpkt;
+
+    /* Don't allow opening an already-open port */
+    if (port->host_connected) {
+        return 0;
+    }
+    /* Send port open notification to the guest */
+    port->host_connected = true;
+    cpkt.event = VIRTIO_CONSOLE_PORT_OPEN;
+    cpkt.value = 1;
+    send_control_event(port, &cpkt);
+
+    /* Flush any buffers that were cached while the port was closed */
+    if (port->cache_buffers && port->info->have_data) {
+        flush_queue(port);
+    }
+    return 0;
+}
+
+int virtio_serial_close(VirtIOSerialPort *port)
+{
+    struct VirtIOSerialPortBuffer *buf, *buf2;
+    struct virtio_console_control cpkt;
+
+    port->host_connected = false;
+    cpkt.event = VIRTIO_CONSOLE_PORT_OPEN;
+    cpkt.value = 0;
+    send_control_event(port, &cpkt);
+
+    if (!port->cache_buffers) {
+        pthread_mutex_lock(&port->buffer_list_lock);
+        QTAILQ_FOREACH_SAFE(buf, &port->unflushed_buffer_head, next, buf2) {
+            QTAILQ_REMOVE(&port->unflushed_buffer_head, buf, next);
+            qemu_free(buf->buf);
+            qemu_free(buf);
+        }
+        pthread_mutex_unlock(&port->buffer_list_lock);
+    }
+    return 0;
+}
+
+/* Individual ports/apps call this function to write to the guest. */
+size_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf, size_t 
size)
+{
+    if (!port || !port->host_connected || !port->guest_connected) {
+        return 0;
+    }
+    return write_to_port(port, buf, size, false);
+}
+
+/*
+ * Readiness of the guest to accept data on a port.
+ * Returns max. data the guest can receive
+ */
+size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
+{
+    VirtQueue *vq = port->vser->ivq;
+    size_t size, header_len;
+
+    if (use_multiport(port->vser)) {
+        header_len = sizeof(struct virtio_console_header);
+    } else {
+        header_len = 0;
+    }
+    if (!virtio_queue_ready(vq) ||
+        !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
+        virtio_queue_empty(vq)) {
+        return 0;
+    }
+    if (use_multiport(port->vser) && !port->guest_connected) {
+        return 0;
+    }
+    size = TARGET_PAGE_SIZE;
+    if (virtqueue_avail_bytes(vq, size, 0)) {
+        return size - header_len;
+    }
+    size = header_len + 1;
+    if (virtqueue_avail_bytes(vq, size, 0)) {
+        return size - header_len;
+    }
+    return 0;
+}
+
+
+/* Guest wants to notify us of some event */
+static void handle_control_message(VirtIOSerialPort *port,
+                                   struct virtio_console_control *cpkt)
+{
+    uint8_t *buffer;
+    size_t buffer_len;
+
+    switch(cpkt->event) {
+    case VIRTIO_CONSOLE_PORT_OPEN:
+        port->guest_connected = cpkt->value;
+        if (cpkt->value && port->info->guest_open) {
+            /* Send the guest opened notification if an app is interested */
+            port->info->guest_open(port);
+        }
+        if (!cpkt->value && port->info->guest_close) {
+            /* Send the guest closed notification if an app is interested */
+            port->info->guest_close(port);
+        }
+        break;
+    case VIRTIO_CONSOLE_PORT_NAME:
+        if (port->name) {
+            buffer_len = sizeof(*cpkt) + strlen(port->name) + 1;
+            buffer = qemu_malloc(buffer_len);
+
+            memcpy(buffer, cpkt, sizeof(*cpkt));
+            memcpy(buffer + sizeof(*cpkt), port->name, strlen(port->name));
+            buffer[buffer_len - 1] = 0;
+
+            write_to_port(port, buffer, buffer_len, 
VIRTIO_CONSOLE_ID_INTERNAL);
+            qemu_free(buffer);
+        }
+        /*
+         * Now that we know the guest asked for the port name, we're
+         * sure the guest has initialised whatever state is necessary
+         * for this port. Now's a good time to let the guest know if
+         * this port is a console port so that the guest can hook it
+         * up to hvc.
+         */
+        if (port->is_console) {
+            cpkt->event = VIRTIO_CONSOLE_CONSOLE_PORT;
+            cpkt->value = true;
+            send_control_event(port, cpkt);
+        }
+        /*
+         * We also want to signal to the guest whether or not the port
+         * is set to caching the buffers when disconnected.
+         */
+        if (port->cache_buffers) {
+            cpkt->event = VIRTIO_CONSOLE_CACHE_BUFFERS;
+            cpkt->value = true;
+            send_control_event(port, cpkt);
+        }
+        /*
+         * If any limit for guest throttling has been specified, send
+         * it out to the guest. The guest accepts this limit only in MBs.
+         */
+        if (port->guest_byte_limit) {
+            cpkt->event = VIRTIO_CONSOLE_BUFFER_LIMIT;
+            cpkt->value = port->guest_byte_limit / (1024 * 1024);
+            send_control_event(port, cpkt);
+        }
+        /*
+         * When the guest has asked us for this information it means
+         * the guest is all setup and has its virtqueues
+         * initialised. If some app is interested in knowing about
+         * this event, let it know
+         */
+        if (port->info->guest_ready) {
+            port->info->guest_ready(port);
+        }
+        break;
+    case VIRTIO_CONSOLE_THROTTLE_PORT:
+        /*
+         * Just manipulating the guest_connected value is sufficient.
+         * We use this value only to check if guest is ready to accept
+         * any data we have to send
+         */
+        port->guest_connected = !cpkt->value;
+        break;
+    }
+}
+
+/*
+ * Guest wrote something to some port.
+ *
+ * Flush the data in the entire chunk that we received rather than
+ * splitting it into multiple buffers. VNC clients don't consume split
+ * buffers
+ */
+static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
+{
+    VirtIOSerial *vser;
+    VirtQueueElement elem;
+
+    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
+
+    while (virtqueue_pop(vq, &elem)) {
+        VirtIOSerialPort *port;
+        VirtIOSerialPortBuffer *buf;
+        struct virtio_console_header header;
+        int header_len;
+
+        if (use_multiport(vser)) {
+            header_len = sizeof(header);
+
+            memcpy(&header, elem.out_sg[0].iov_base, header_len);
+            port = get_port_from_id(vser, header.id);
+        } else {
+            header_len = 0;
+            port = get_port_from_id(vser, 0);
+        }
+        if (!port) {
+            goto next_buf;
+        }
+        /*
+         * A port may not have any handler registered for consuming the
+         * data that the guest sends or it may not have a chardev associated
+         * with it. Just ignore the data in that case
+         */
+        if (!is_internal(header.flags) && !port->info->have_data) {
+            goto next_buf;
+        }
+        /* The guest always sends only one sg */
+        buf = qemu_mallocz(sizeof(*buf));
+        buf->len = elem.out_sg[0].iov_len - header_len;
+        buf->buf = qemu_mallocz(buf->len);
+        memcpy(buf->buf, elem.out_sg[0].iov_base + header_len, buf->len);
+
+        if (use_multiport(vser) && is_internal(header.flags)) {
+            handle_control_message(port,
+                                   (struct virtio_console_control *)buf->buf);
+            qemu_free(buf->buf);
+            qemu_free(buf);
+            goto next_buf;
+        }
+        pthread_mutex_lock(&port->buffer_list_lock);
+        QTAILQ_INSERT_TAIL(&port->unflushed_buffer_head, buf, next);
+        port->nr_bytes += buf->len;
+        pthread_mutex_unlock(&port->buffer_list_lock);
+        if (use_multiport(vser)) {
+            /*
+             * Only the first buffer in a stream will have this
+             * set. This will help us identify the first buffer and
+             * the remaining buffers in the stream based on length
+             */
+            buf->size = header.size;
+        } else {
+            /* We always want to flush all the buffers in this case */
+            buf->size = buf->len;
+        }
+        if (!port->host_throttled && port->byte_limit &&
+           port->nr_bytes >= port->byte_limit) {
+            struct virtio_console_control cpkt;
+
+            port->host_throttled = true;
+            cpkt.event = VIRTIO_CONSOLE_THROTTLE_PORT;
+            cpkt.value = true;
+            send_control_event(port, &cpkt);
+        }
+        flush_queue(port);
+    next_buf:
+        virtqueue_push(vq, &elem, elem.out_sg[0].iov_len);
+    }
+    virtio_notify(vdev, vq);
+}
+
+static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
+{
+}
+
+static uint32_t get_features(VirtIODevice *vdev)
+{
+    return 1 << VIRTIO_CONSOLE_F_MULTIPORT;
+}
+
+static void set_features(VirtIODevice *vdev, uint32_t features)
+{
+    VirtIOSerial *vser;
+
+    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
+    vser->guest_features = features;
+}
+
+/* Guest requested config info */
+static void get_config(VirtIODevice *vdev, uint8_t *config_data)
+{
+    VirtIOSerial *vser;
+
+    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
+    memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
+}
+
+static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
+{
+    struct virtio_console_config config;
+
+    memcpy(&config, config_data, sizeof(config));
+}
+
+/*
+ * These booleans from the per-port struct are packed into a byte
+ * for optimising save/load
+ */
+#define GUEST_CONN    (1 << 0)
+#define HOST_THROTTLE (1 << 1)
+
+static void virtio_serial_save(QEMUFile *f, void *opaque)
+{
+    VirtIOSerial *s = opaque;
+    VirtIOSerialPort *port;
+    unsigned int i, nr_bufs;
+
+    /* The virtio device */
+    virtio_save(&s->vdev, f);
+    /* The config space */
+    qemu_put_be16s(f, &s->config.cols);
+    qemu_put_be16s(f, &s->config.rows);
+    qemu_put_be32s(f, &s->config.nr_active_ports);
+
+    /* Items in struct VirtIOSerial */
+    qemu_put_be32s(f, &s->guest_features);
+
+    /*
+     * Items in struct VirtIOSerialPort
+     */
+    for (i = 0; i < s->config.nr_active_ports; i++) {
+        uint8_t port_bools = 0;
+        VirtIOSerialPortBuffer *buf;
+
+        port = get_port_from_id(s, i);
+        if (!port) {
+            continue;
+        }
+        /*
+         * We put the port number because we may not have an active
+         * port at id 0 that's reserved for a console port.
+         */
+        qemu_put_be32s(f, &i);
+        qemu_put_be64s(f, &port->byte_limit);
+        qemu_put_be64s(f, &port->nr_bytes);
+        qemu_put_be64s(f, &port->guest_byte_limit);
+        if (port->guest_connected) {
+            port_bools |= GUEST_CONN;
+        }
+        if (port->host_throttled) {
+            port_bools |= HOST_THROTTLE;
+        }
+        qemu_put_byte(f, port_bools);
+
+        /* All the pending buffers from active ports */
+        nr_bufs = 0;
+        QTAILQ_FOREACH(buf, &port->unflushed_buffer_head, next) {
+            nr_bufs++;
+        }
+        qemu_put_be32s(f, &nr_bufs);
+        if (!nr_bufs) {
+            continue;
+        }
+        QTAILQ_FOREACH(buf, &port->unflushed_buffer_head, next) {
+            qemu_put_be64s(f, &buf->len);
+            qemu_put_be64s(f, &buf->size);
+            qemu_put_buffer(f, buf->buf, buf->len);
+        }
+    }
+}
+
+static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
+{
+    VirtIOSerial *s = opaque;
+    VirtIOSerialPort *port;
+    unsigned int i;
+
+    if (version_id > 2) {
+        return -EINVAL;
+    }
+    /* The virtio device */
+    virtio_load(&s->vdev, f);
+
+    if (version_id < 2) {
+        return 0;
+    }
+    /* The config space */
+    qemu_get_be16s(f, &s->config.cols);
+    qemu_get_be16s(f, &s->config.rows);
+    s->config.nr_active_ports = qemu_get_be32(f);
+
+    /* Items in struct VirtIOSerial */
+    qemu_get_be32s(f, &s->guest_features);
+
+    /*
+     * If no virtconsole port specified, the src won't send any
+     * port-specific information for port 0
+     */
+    if (get_port_from_id(s, 0)) {
+        i = 0;
+    } else {
+        i = 1;
+    }
+    /* Items in struct VirtIOSerialPort */
+    for (; i < s->config.nr_active_ports; i++) {
+        VirtIOSerialPortBuffer *buf;
+        uint32_t id;
+        unsigned int nr_bufs;
+        uint8_t port_bools;
+
+        id = qemu_get_be32(f);
+        port = get_port_from_id(s, id);
+
+        port->byte_limit = qemu_get_be64(f);
+        port->nr_bytes   = qemu_get_be64(f);
+        port->guest_byte_limit = qemu_get_be64(f);
+        port_bools = qemu_get_byte(f);
+        if (port_bools & GUEST_CONN) {
+            port->guest_connected = true;
+        }
+        if (port_bools & HOST_THROTTLE) {
+            port->host_throttled = true;
+        }
+        /* All the pending buffers from active ports */
+        qemu_get_be32s(f, &nr_bufs);
+        if (!nr_bufs) {
+            continue;
+        }
+        for (; nr_bufs; nr_bufs--) {
+            buf = qemu_malloc(sizeof(*buf));
+
+            qemu_get_be64s(f, &buf->len);
+            qemu_get_be64s(f, &buf->size);
+            buf->buf = qemu_malloc(buf->len);
+            qemu_get_buffer(f, buf->buf, buf->len);
+            QTAILQ_INSERT_TAIL(&port->unflushed_buffer_head, buf, next);
+        }
+        /*
+         * Applications might have internal state they track when they
+         * receive a guest_open() callback. Apps could also open a
+         * connection to this core in that case. So if the guest was
+         * open at the time of migration, send a guest_open callback
+         */
+        if (port->guest_connected && port->info->guest_open) {
+            port->info->guest_open(port);
+        }
+    }
+    return 0;
+}
+
+static void virtser_bus_print(Monitor *mon, DeviceState *qdev, int indent);
+
+static struct BusInfo virtser_bus_info = {
+    .name      = "virtio-serial-bus",
+    .size      = sizeof(VirtIOSerialBus),
+    .print_dev = virtser_bus_print,
+};
+
+static VirtIOSerialBus *virtser_bus_new(DeviceState *dev)
+{
+    VirtIOSerialBus *bus;
+
+    bus = FROM_QBUS(VirtIOSerialBus, qbus_create(&virtser_bus_info, dev, 
NULL));
+    bus->qbus.allow_hotplug = 1;
+
+    return bus;
+}
+
+static void virtser_bus_print(Monitor *mon, DeviceState *qdev, int indent)
+{
+    VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
+    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
+
+    monitor_printf(mon, "%*s id: %u, name: %s, is_console: %d, "
+                   "guest_connected: %d, host_connected: %d, "
+                   "host_throttled: %d, cache_buffers: %d\n",
+                   indent, "", port->id, port->name, port->is_console,
+                   port->guest_connected, port->host_connected,
+                   port->host_throttled, port->cache_buffers);
+    monitor_printf(mon, "%*s byte_limit: %zu, nr_bytes: %zu, "
+                   "guest_byte_limit: %zu\n",
+                   indent, "", port->byte_limit, port->nr_bytes,
+                   port->guest_byte_limit);
+}
+
+static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
+{
+    VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
+    VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, base);
+    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
+    VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
+    int ret;
+
+    port->vser = bus->vser;
+
+    if (port->vser->config.nr_active_ports == MAX_VIRTIO_SERIAL_PORTS) {
+        qemu_error("virtio-serial-bus: Maximum device limit reached\n");
+        return -1;
+    }
+    dev->info = info;
+
+    ret = info->init(dev);
+    if (ret) {
+        return ret;
+    }
+    QTAILQ_INIT(&port->unflushed_buffer_head);
+    pthread_mutex_init(&port->buffer_list_lock, NULL);
+
+    if (port->is_console && !port->vser->ports[0]) {
+        /*
+         * This is the first console port we're seeing so put it up at
+         * location 0. This is done for backward compatibility (old
+         * kernel, new qemu).
+         */
+        port->id = 0;
+    } else {
+        port->id = port->vser->config.nr_active_ports++;
+    }
+    port->vser->ports[port->id] = port;
+    /* Send an update to the guest about this new port added */
+    virtio_notify_config(&port->vser->vdev);
+
+    return ret;
+}
+
+void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info)
+{
+    info->qdev.init = virtser_port_qdev_init;
+    info->qdev.bus_info = &virtser_bus_info;
+    qdev_register(&info->qdev);
+}
+
+VirtIODevice *virtio_serial_init(DeviceState *dev)
+{
+    VirtIOSerial *s;
+
+    s = (VirtIOSerial *)virtio_common_init("virtio-serial",
+                                            VIRTIO_ID_CONSOLE,
+                                            sizeof(struct 
virtio_console_config),
+                                            sizeof(VirtIOSerial));
+
+    s->vdev.get_features = get_features;
+    s->vdev.set_features = set_features;
+    s->vdev.get_config = get_config;
+    s->vdev.set_config = set_config;
+
+    /* Add queue for host to guest transfers */
+    s->ivq = virtio_add_queue(&s->vdev, 128, handle_input);
+    /* Add queue for guest to host transfers */
+    s->ovq = virtio_add_queue(&s->vdev, 128, handle_output);
+
+    /*
+     * Register for the savevm section with the virtio-console name
+     * to preserve backward compat
+     */
+    register_savevm("virtio-console", -1, 2, virtio_serial_save,
+                    virtio_serial_load, s);
+
+    /* Spawn a new virtio-serial bus on which the ports will ride as devices */
+    s->bus = virtser_bus_new(dev);
+    s->bus->vser = s;
+
+    /*
+     * Reserve location 0 for a console port for backward compat
+     * (old kernel, new qemu)
+     */
+    s->config.nr_active_ports = 1;
+
+    return &s->vdev;
+}
diff --git a/hw/virtio-serial.h b/hw/virtio-serial.h
new file mode 100644
index 0000000..ea36630
--- /dev/null
+++ b/hw/virtio-serial.h
@@ -0,0 +1,227 @@
+/*
+ * Virtio Serial / Console Support
+ *
+ * Copyright IBM, Corp. 2008
+ * Copyright Red Hat, Inc. 2009
+ *
+ * Authors:
+ *  Christian Ehrhardt <address@hidden>
+ *  Amit Shah <address@hidden>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+#ifndef _QEMU_VIRTIO_SERIAL_H
+#define _QEMU_VIRTIO_SERIAL_H
+
+#include <pthread.h>
+#include <stdbool.h>
+#include "qdev.h"
+
+/* == Interface shared between the guest kernel and qemu == */
+
+/* The ID for virtio console / serial ports */
+#define VIRTIO_ID_CONSOLE 3
+
+/* Invalid port number */
+#define VIRTIO_CONSOLE_BAD_ID          (~(uint32_t)0)
+
+/* Features supported */
+#define VIRTIO_CONSOLE_F_MULTIPORT     1
+
+struct virtio_console_config
+{
+    /*
+     * These two fields are used by VIRTIO_CONSOLE_F_SIZE which
+     * isn't implemented here yet
+     */
+    uint16_t cols;
+    uint16_t rows;
+
+    uint32_t nr_active_ports;
+} __attribute__((packed));
+
+struct virtio_console_control
+{
+    uint16_t event;
+    uint16_t value;
+};
+
+struct virtio_console_header {
+    uint32_t id; /* Port id */
+    uint32_t flags; /* Some message between host and guest */
+    uint32_t size; /* Size that's sent with the first buffer of each stream */
+} __attribute__((packed));
+
+/* Messages between host and guest */
+#define VIRTIO_CONSOLE_ID_INTERNAL     (1 << 0)
+
+/* Some events for the internal messages (control packets) */
+#define VIRTIO_CONSOLE_PORT_OPEN       0
+#define VIRTIO_CONSOLE_PORT_NAME       1
+#define VIRTIO_CONSOLE_CONSOLE_PORT    2
+#define VIRTIO_CONSOLE_CACHE_BUFFERS   3
+#define VIRTIO_CONSOLE_THROTTLE_PORT   4
+#define VIRTIO_CONSOLE_BUFFER_LIMIT    5
+
+/* == In-qemu interface == */
+
+/* Max. virtio console ports per device */
+#define MAX_VIRTIO_SERIAL_PORTS                64
+
+typedef struct VirtIOSerial VirtIOSerial;
+typedef struct VirtIOSerialBus VirtIOSerialBus;
+typedef struct VirtIOSerialPort VirtIOSerialPort;
+typedef struct VirtIOSerialPortInfo VirtIOSerialPortInfo;
+
+typedef struct VirtIOSerialDevice {
+    DeviceState qdev;
+    VirtIOSerialPortInfo *info;
+} VirtIOSerialDevice;
+
+/*
+ * This is the state that's shared between all the ports.  Some of the
+ * state is configurable via command-line options. Some of it can be
+ * set by individual devices in their initfn routines. Some of the
+ * state is set by the generic qdev device init routine.
+ */
+struct VirtIOSerialPort {
+    DeviceState dev;
+    VirtIOSerialPortInfo *info;
+
+    /*
+     * This field gives us the virtio device as well as the qdev bus
+     * that we are associated with
+     */
+    VirtIOSerial *vser;
+
+    /*
+     * This name is sent to the guest and exported via sysfs.
+     * The guest could create symlinks based on this information.
+     * The name is in the reverse fqdn format, like org.qemu.console.0
+     */
+    char *name;
+
+    /*
+     * This list holds buffers pushed by the guest in case the guest
+     * sent incomplete messages or the host connection was down and
+     * the device requested to cache the data.
+     */
+    QTAILQ_HEAD(, VirtIOSerialPortBuffer) unflushed_buffer_head;
+
+    /*
+     * This lock protects the unflushed_buffer_head list
+     */
+    pthread_mutex_t buffer_list_lock;
+
+    /*
+     * This id helps identify ports between the guest and the host.
+     * The guest sends a "header" with this id with each data packet
+     * that it sends and the host can then find out which associated
+     * device to send out this data to
+     */
+    uint32_t id;
+
+    /*
+     * Each port can specify the limit on number of bytes that can be
+     * outstanding in the unread buffers. This is to prevent any OOM
+     * situtation if a rogue process on the guest keeps injecting
+     * data.
+     */
+    size_t byte_limit;
+
+    /*
+     * The number of bytes we have queued up in our unread queue
+     */
+    size_t nr_bytes;
+
+    /*
+     * Per-port limit to restrict the outstanding buffers for the guest
+     */
+    size_t guest_byte_limit;
+
+    /*
+     * This boolean, when set, means "queue data that gets sent to
+     * this port when the host is not connected". The queued data, if
+     * any, is then sent out to the port when the host connection is
+     * opened.
+     */
+    int cache_buffers;
+
+    /* Identify if this is a port that binds with hvc in the guest */
+    bool is_console;
+
+    /* Is the corresponding guest device open? */
+    bool guest_connected;
+    /* Is this device open for IO on the host? */
+    bool host_connected;
+    /* Have we sent a throttle message to the guest? */
+    bool host_throttled;
+};
+
+
+struct VirtIOSerialPortInfo {
+    DeviceInfo qdev;
+    /*
+     * The per-port (or per-app) init function that's called when a
+     * new device is found on the bus.
+     */
+    int (*init)(VirtIOSerialDevice *dev);
+
+    /* Callbacks for guest events */
+        /*
+        * Guest opened device. This could be invoked even when an
+        * application thinks the guest is open. This can happen if
+        * the host is migrated to another machine when the connection
+        * was open and is called from the destination machine if
+        * there's any app-specific initialisation to be done in such
+        * a case.
+        */
+    void (*guest_open)(VirtIOSerialPort *port);
+       /* Guest closed device */
+    void (*guest_close)(VirtIOSerialPort *port);
+
+    /* Guest is now ready to accept data (virtqueues set up) */
+    void (*guest_ready)(VirtIOSerialPort *port);
+    /*
+     * Guest wrote some data to the port. This data is handed over to
+     * the app via this callback. The data is not maintained anymore
+     * after the callback returns. This means the app has to ensure it
+     * read all the data and consumes it.
+     *
+     * If an app needs to have the data for a longer time, it's upto
+     * the app to cache it.
+     */
+    size_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf, size_t 
len);
+};
+
+/* Interface to the virtio-serial bus */
+
+/*
+ * Individual ports/apps should call this function to register the port
+ * with the virtio-serial bus
+ */
+void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info);
+/*
+ * Open a connection to the port
+ *   Returns 0 on success (always)
+ */
+int virtio_serial_open(VirtIOSerialPort *port);
+/*
+ * Close the connection to the port
+ *   Returns 0 on successful close, or, if buffer caching is disabled,
+ * -EAGAIN if there's some uncosued data for the app.
+ */
+int virtio_serial_close(VirtIOSerialPort *port);
+/*
+ * Send data to Guest
+ */
+size_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
+                          size_t size);
+/*
+ * Query whether a guest is ready to receive data.
+ */
+size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
+
+#endif
diff --git a/hw/virtio.h b/hw/virtio.h
index 0f9be7d..a9601a2 100644
--- a/hw/virtio.h
+++ b/hw/virtio.h
@@ -164,7 +164,7 @@ void virtio_bind_device(VirtIODevice *vdev, const 
VirtIOBindings *binding,
 /* Base devices.  */
 VirtIODevice *virtio_blk_init(DeviceState *dev, DriveInfo *dinfo);
 VirtIODevice *virtio_net_init(DeviceState *dev);
-VirtIODevice *virtio_console_init(DeviceState *dev);
+VirtIODevice *virtio_serial_init(DeviceState *dev);
 VirtIODevice *virtio_balloon_init(DeviceState *dev);
 
 #endif
diff --git a/qemu-options.hx b/qemu-options.hx
index 20aa242..ca4d353 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -1635,10 +1635,14 @@ ETEXI
 
 DEF("virtioconsole", HAS_ARG, QEMU_OPTION_virtiocon, \
     "-virtioconsole c\n" \
-    "                set virtio console\n")
+    "                define virtio console\n")
 STEXI
 @item -virtioconsole @var{c}
 Set virtio console.
+
+This option is maintained for backward compatibility.
+
+Please use @code{-device virtconsole} for the new way of invocation
 ETEXI
 
 DEF("show-cursor", 0, QEMU_OPTION_show_cursor, \
diff --git a/sysemu.h b/sysemu.h
index 763861d..9129728 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -226,12 +226,6 @@ extern CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 
 extern CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 
-/* virtio consoles */
-
-#define MAX_VIRTIO_CONSOLES 1
-
-extern CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
-
 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
 
 #ifdef HAS_AUDIO
diff --git a/vl.c b/vl.c
index eb2744e..c1e880e 100644
--- a/vl.c
+++ b/vl.c
@@ -180,6 +180,8 @@ int main(int argc, char **argv)
 /* Maximum number of monitor devices */
 #define MAX_MONITOR_DEVICES 10
 
+#define MAX_VIRTIO_CONSOLES 1
+
 static const char *data_dir;
 const char *bios_name = NULL;
 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
@@ -215,7 +217,6 @@ static int no_frame = 0;
 int no_quit = 0;
 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
-CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 #ifdef TARGET_I386
 int win2k_install_hack = 0;
 int rtc_td_hack = 0;
@@ -4752,6 +4753,7 @@ int main(int argc, char **argv, char **envp)
     const char *parallel_devices[MAX_PARALLEL_PORTS];
     int parallel_device_index;
     const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
+    CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
     int virtio_console_index;
     const char *loadvm = NULL;
     QEMUMachine *machine;
@@ -4828,8 +4830,10 @@ int main(int argc, char **argv, char **envp)
         parallel_devices[i] = NULL;
     parallel_device_index = 0;
 
-    for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
+    for (i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
         virtio_consoles[i] = NULL;
+        virtcon_hds[i] = NULL;
+    }
     virtio_console_index = 0;
 
     monitor_devices[0] = "vc:80Cx24C";
@@ -5799,20 +5803,6 @@ int main(int argc, char **argv, char **envp)
         }
     }
 
-    for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
-        const char *devname = virtio_consoles[i];
-        if (devname && strcmp(devname, "none")) {
-            char label[32];
-            snprintf(label, sizeof(label), "virtcon%d", i);
-            virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
-            if (!virtcon_hds[i]) {
-                fprintf(stderr, "qemu: could not open virtio console '%s': 
%s\n",
-                        devname, strerror(errno));
-                exit(1);
-            }
-        }
-    }
-
     module_call_init(MODULE_INIT_DEVICE);
 
     if (watchdog) {
@@ -5853,6 +5843,41 @@ int main(int argc, char **argv, char **envp)
     if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
         exit(1);
 
+    /*
+     * This code is left for the old -virtconsole cmd line arg support.
+     * For using the other serial ports, use -device virtconsole and
+     * -device virtserialport
+     *
+     * If / when -virtioconsole gets deprecated, this may be safely removed
+     */
+    for (i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
+        const char *devname = virtio_consoles[i];
+        QemuOpts *opts;
+
+        if (!devname) {
+            continue;
+        }
+        opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
+        qemu_opt_set(opts, "driver", "virtio-serial-pci");
+        qdev_device_add(opts);
+
+        qemu_opt_set(opts, "driver", "virtconsole");
+
+        if (strcmp(devname, "none")) {
+            char label[32];
+            snprintf(label, sizeof(label), "virtcon%d", i);
+            virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
+            if (!virtcon_hds[i]) {
+                fprintf(stderr, "qemu: could not open virtio console '%s': 
%s\n",
+                        devname, strerror(errno));
+                exit(1);
+            }
+            qemu_opt_set(opts, "chardev", label);
+        }
+        qdev_device_add(opts);
+    }
+
+
     if (!display_state)
         dumb_display_init();
     /* just use the first displaystate for the moment */
@@ -5941,14 +5966,6 @@ int main(int argc, char **argv, char **envp)
         }
     }
 
-    for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
-        const char *devname = virtio_consoles[i];
-        if (virtcon_hds[i] && devname) {
-            if (strstart(devname, "vc", 0))
-                qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
-        }
-    }
-
     if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
         fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
                 gdbstub_dev);
-- 
1.6.2.5





reply via email to

[Prev in Thread] Current Thread [Next in Thread]