Diff for /qemu/hw/virtio-console.c between versions 1.1.1.1 and 1.1.1.8

version 1.1.1.1, 2018/04/24 16:53:44 version 1.1.1.8, 2018/04/24 19:49:49
Line 1 Line 1
 /*  /*
  * Virtio Console Device   * Virtio Console and Generic Serial Port Devices
  *   *
  * Copyright IBM, Corp. 2008   * Copyright Red Hat, Inc. 2009, 2010
  *   *
  * Authors:   * Authors:
  *  Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>   *  Amit Shah <amit.shah@redhat.com>
  *   *
  * This work is licensed under the terms of the GNU GPL, version 2.  See   * This work is licensed under the terms of the GNU GPL, version 2.  See
  * the COPYING file in the top-level directory.   * the COPYING file in the top-level directory.
  *  
  */   */
   
 #include "hw.h"  
 #include "qemu-char.h"  #include "qemu-char.h"
 #include "virtio.h"  #include "qemu-error.h"
 #include "virtio-console.h"  #include "trace.h"
   #include "virtio-serial.h"
   
 typedef struct VirtIOConsole  typedef struct VirtConsole {
 {      VirtIOSerialPort port;
     VirtIODevice vdev;  
     VirtQueue *ivq, *dvq;  
     CharDriverState *chr;      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);      VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
     VirtQueueElement elem;      ssize_t ret;
   
       if (!vcon->chr) {
           /* If there's no backend, we can just say we consumed all data. */
           return len;
       }
   
     while (virtqueue_pop(vq, &elem)) {      ret = qemu_chr_fe_write(vcon->chr, buf, len);
         ssize_t len = 0;      trace_virtio_console_flush_buf(port->id, len, ret);
         int d;  
   
         for (d=0; d < elem.out_num; d++)      if (ret < 0) {
             len += qemu_chr_write(s->chr, elem.out_sg[d].iov_base,elem.out_sg[d].iov_len);          /*
         virtqueue_push(vq, &elem, len);           * Ideally we'd get a better error code than just -1, but
         virtio_notify(vdev, vq);           * 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)      if (!vcon->chr) {
 {          return;
     return 0;      }
       qemu_chr_fe_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;      VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
   
     if (!virtio_queue_ready(s->ivq) ||      if (!vcon->chr) {
         !(s->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||          return;
         virtio_queue_empty(s->ivq))      }
         return 0;      qemu_chr_fe_close(vcon->chr);
   
     /* 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;  
 }  }
   
 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;      VirtConsole *vcon = opaque;
     VirtQueueElement elem;  
     int offset = 0;  
   
     /* The current kernel implementation has only one outstanding input      return virtio_serial_guest_ready(&vcon->port);
      * 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);  
 }  }
   
 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;      VirtConsole *vcon = opaque;
   
     virtio_save(&s->vdev, f);      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);
       VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
   
     if (version_id != 1)      if (port->id == 0 && !k->is_console) {
         return -EINVAL;          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);
       }
   
     virtio_load(&s->vdev, f);  
     return 0;      return 0;
 }  }
   
 void *virtio_console_init(PCIBus *bus, CharDriverState *chr)  static Property virtconsole_properties[] = {
       DEFINE_PROP_CHR("chardev", VirtConsole, chr),
       DEFINE_PROP_END_OF_LIST(),
   };
   
   static void virtconsole_class_init(ObjectClass *klass, void *data)
 {  {
     VirtIOConsole *s;      DeviceClass *dc = DEVICE_CLASS(klass);
       VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
   
     s = (VirtIOConsole *)virtio_init_pci(bus, "virtio-console",      k->is_console = true;
                                          PCI_VENDOR_ID_REDHAT_QUMRANET,      k->init = virtconsole_initfn;
                                          PCI_DEVICE_ID_VIRTIO_CONSOLE,      k->have_data = flush_buf;
                                          PCI_VENDOR_ID_REDHAT_QUMRANET,      k->guest_open = guest_open;
                                          VIRTIO_ID_CONSOLE,      k->guest_close = guest_close;
                                          PCI_CLASS_DISPLAY_OTHER, 0x00,      dc->props = virtconsole_properties;
                                          0, sizeof(VirtIOConsole));  }
     if (s == NULL)  
         return NULL;  static TypeInfo virtconsole_info = {
       .name          = "virtconsole",
       .parent        = TYPE_VIRTIO_SERIAL_PORT,
       .instance_size = sizeof(VirtConsole),
       .class_init    = virtconsole_class_init,
   };
   
     s->vdev.get_features = virtio_console_get_features;  static Property virtserialport_properties[] = {
       DEFINE_PROP_CHR("chardev", VirtConsole, chr),
       DEFINE_PROP_END_OF_LIST(),
   };
   
     s->ivq = virtio_add_queue(&s->vdev, 128, virtio_console_handle_input);  static void virtserialport_class_init(ObjectClass *klass, void *data)
     s->dvq = virtio_add_queue(&s->vdev, 128, virtio_console_handle_output);  {
       DeviceClass *dc = DEVICE_CLASS(klass);
       VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
   
     s->chr = chr;      k->init = virtconsole_initfn;
     qemu_chr_add_handlers(chr, vcon_can_read, vcon_read, vcon_event, s);      k->have_data = flush_buf;
       k->guest_open = guest_open;
       k->guest_close = guest_close;
       dc->props = virtserialport_properties;
   }
   
     register_savevm("virtio-console", -1, 1, virtio_console_save, virtio_console_load, s);  static TypeInfo virtserialport_info = {
       .name          = "virtserialport",
       .parent        = TYPE_VIRTIO_SERIAL_PORT,
       .instance_size = sizeof(VirtConsole),
       .class_init    = virtserialport_class_init,
   };
   
     return &s->vdev;  static void virtconsole_register_types(void)
   {
       type_register_static(&virtconsole_info);
       type_register_static(&virtserialport_info);
 }  }
   
   type_init(virtconsole_register_types)

Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.8


unix.superglobalmegacorp.com