X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=hw%2Fvirtio-console.c;h=fe5e188bf47a896e21b1a35b59e4b68853c9d71b;hb=d6cca4b048c5b63547d5c09fb47a10f0057b88bf;hp=57f8f89afaf620edb0d0076a887b38d82b7eaeea;hpb=bf0cb498c581b6be7bf4c31f500487600abf2db5;p=qemu.git diff --git a/hw/virtio-console.c b/hw/virtio-console.c index 57f8f89af..fe5e188bf 100644 --- a/hw/virtio-console.c +++ b/hw/virtio-console.c @@ -1,143 +1,168 @@ /* - * Virtio Console Device + * Virtio Console and Generic Serial Port Devices * - * Copyright IBM, Corp. 2008 + * Copyright Red Hat, Inc. 2009, 2010 * * Authors: - * Christian Ehrhardt + * Amit Shah * * 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 "qemu-error.h" +#include "trace.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 ssize_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); + ssize_t ret; + + ret = qemu_chr_write(vcon->chr, buf, len); + trace_virtio_console_flush_buf(port->id, len, ret); + + if (ret < 0) { + /* + * Ideally we'd get a better error code than just -1, but + * that's what the chardev interface gives us right now. If + * we had a finer-grained message, like -EPIPE, we could close + * this connection. Absent such error messages, the most we + * can do is to return 0 here. + * + * This will prevent stray -1 values to go to + * virtio-serial-bus.c and cause abort()s in + * do_flush_queued_data(). + */ + ret = 0; } + return ret; } -static void virtio_console_handle_input(VirtIODevice *vdev, VirtQueue *vq) +/* Callback function that's called when the guest opens the port */ +static void guest_open(VirtIOSerialPort *port) { -} + VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); -static uint32_t virtio_console_get_features(VirtIODevice *vdev) -{ - return 0; + qemu_chr_guest_open(vcon->chr); } -static int vcon_can_read(void *opaque) +/* Callback function that's called when the guest closes the port */ +static void guest_close(VirtIOSerialPort *port) { - 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 = DO_UPCAST(VirtConsole, port, port); + + qemu_chr_guest_close(vcon->chr); } -static void vcon_read(void *opaque, const uint8_t *buf, int size) +/* Readiness of the guest to accept data on a port */ +static int chr_can_read(void *opaque) { - 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); + VirtConsole *vcon = opaque; + + 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; + + trace_virtio_console_chr_read(vcon->port.id, size); + 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; - - virtio_save(&s->vdev, f); + VirtConsole *vcon = opaque; + + trace_virtio_console_chr_event(vcon->port.id, event); + 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) +static int virtconsole_initfn(VirtIOSerialPort *port) { - VirtIOConsole *s = opaque; + VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); + VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, + vcon->port.dev.info); - if (version_id != 1) - return -EINVAL; + if (port->id == 0 && !info->is_console) { + error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility."); + return -1; + } + + if (vcon->chr) { + qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, + vcon); + info->have_data = flush_buf; + info->guest_open = guest_open; + info->guest_close = guest_close; + } - virtio_load(&s->vdev, f); return 0; } -VirtIODevice *virtio_console_init(DeviceState *dev) +static int virtconsole_exitfn(VirtIOSerialPort *port) { - 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); + VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); + + if (vcon->chr) { + /* + * Instead of closing the chardev, free it so it can be used + * for other purposes. + */ + qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL); + } - register_savevm("virtio-console", -1, 1, virtio_console_save, virtio_console_load, s); + return 0; +} - return &s->vdev; +static VirtIOSerialPortInfo virtconsole_info = { + .qdev.name = "virtconsole", + .qdev.size = sizeof(VirtConsole), + .is_console = true, + .init = virtconsole_initfn, + .exit = virtconsole_exitfn, + .qdev.props = (Property[]) { + DEFINE_PROP_CHR("chardev", VirtConsole, chr), + DEFINE_PROP_END_OF_LIST(), + }, +}; + +static void virtconsole_register(void) +{ + virtio_serial_port_qdev_register(&virtconsole_info); +} +device_init(virtconsole_register) + +static VirtIOSerialPortInfo virtserialport_info = { + .qdev.name = "virtserialport", + .qdev.size = sizeof(VirtConsole), + .init = virtconsole_initfn, + .exit = virtconsole_exitfn, + .qdev.props = (Property[]) { + DEFINE_PROP_CHR("chardev", VirtConsole, chr), + DEFINE_PROP_END_OF_LIST(), + }, +}; + +static void virtserialport_register(void) +{ + virtio_serial_port_qdev_register(&virtserialport_info); } +device_init(virtserialport_register)