1 /* 2 * Virtio Console and Generic Serial Port Devices 3 * 4 * Copyright Red Hat, Inc. 2009, 2010 5 * 6 * Authors: 7 * Amit Shah <amit.shah@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "sysemu/char.h" 15 #include "qemu/error-report.h" 16 #include "trace.h" 17 #include "hw/virtio/virtio-serial.h" 18 #include "qapi-event.h" 19 20 #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport" 21 #define VIRTIO_CONSOLE(obj) \ 22 OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT) 23 24 typedef struct VirtConsole { 25 VirtIOSerialPort parent_obj; 26 27 CharDriverState *chr; 28 guint watch; 29 } VirtConsole; 30 31 /* 32 * Callback function that's called from chardevs when backend becomes 33 * writable. 34 */ 35 static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond, 36 void *opaque) 37 { 38 VirtConsole *vcon = opaque; 39 40 vcon->watch = 0; 41 virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false); 42 return FALSE; 43 } 44 45 /* Callback function that's called when the guest sends us data */ 46 static ssize_t flush_buf(VirtIOSerialPort *port, 47 const uint8_t *buf, ssize_t len) 48 { 49 VirtConsole *vcon = VIRTIO_CONSOLE(port); 50 ssize_t ret; 51 52 if (!vcon->chr) { 53 /* If there's no backend, we can just say we consumed all data. */ 54 return len; 55 } 56 57 ret = qemu_chr_fe_write(vcon->chr, buf, len); 58 trace_virtio_console_flush_buf(port->id, len, ret); 59 60 if (ret < len) { 61 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); 62 63 /* 64 * Ideally we'd get a better error code than just -1, but 65 * that's what the chardev interface gives us right now. If 66 * we had a finer-grained message, like -EPIPE, we could close 67 * this connection. 68 */ 69 if (ret < 0) 70 ret = 0; 71 72 /* XXX we should be queuing data to send later for the 73 * console devices too rather than silently dropping 74 * console data on EAGAIN. The Linux virtio-console 75 * hvc driver though does sends with spinlocks held, 76 * so if we enable throttling that'll stall the entire 77 * guest kernel, not merely the process writing to the 78 * console. 79 * 80 * While we could queue data for later write without 81 * enabling throttling, this would result in the guest 82 * being able to trigger arbitrary memory usage in QEMU 83 * buffering data for later writes. 84 * 85 * So fixing this problem likely requires fixing the 86 * Linux virtio-console hvc driver to not hold spinlocks 87 * while writing, and instead merely block the process 88 * that's writing. QEMU would then need some way to detect 89 * if the guest had the fixed driver too, before we can 90 * use throttling on host side. 91 */ 92 if (!k->is_console) { 93 virtio_serial_throttle_port(port, true); 94 if (!vcon->watch) { 95 vcon->watch = qemu_chr_fe_add_watch(vcon->chr, 96 G_IO_OUT|G_IO_HUP, 97 chr_write_unblocked, vcon); 98 } 99 } 100 } 101 return ret; 102 } 103 104 /* Callback function that's called when the guest opens/closes the port */ 105 static void set_guest_connected(VirtIOSerialPort *port, int guest_connected) 106 { 107 VirtConsole *vcon = VIRTIO_CONSOLE(port); 108 DeviceState *dev = DEVICE(port); 109 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); 110 111 if (vcon->chr && !k->is_console) { 112 qemu_chr_fe_set_open(vcon->chr, guest_connected); 113 } 114 115 if (dev->id) { 116 qapi_event_send_vserport_change(dev->id, guest_connected, 117 &error_abort); 118 } 119 } 120 121 static void guest_writable(VirtIOSerialPort *port) 122 { 123 VirtConsole *vcon = VIRTIO_CONSOLE(port); 124 125 if (vcon->chr) { 126 qemu_chr_accept_input(vcon->chr); 127 } 128 } 129 130 /* Readiness of the guest to accept data on a port */ 131 static int chr_can_read(void *opaque) 132 { 133 VirtConsole *vcon = opaque; 134 135 return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon)); 136 } 137 138 /* Send data from a char device over to the guest */ 139 static void chr_read(void *opaque, const uint8_t *buf, int size) 140 { 141 VirtConsole *vcon = opaque; 142 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon); 143 144 trace_virtio_console_chr_read(port->id, size); 145 virtio_serial_write(port, buf, size); 146 } 147 148 static void chr_event(void *opaque, int event) 149 { 150 VirtConsole *vcon = opaque; 151 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon); 152 153 trace_virtio_console_chr_event(port->id, event); 154 switch (event) { 155 case CHR_EVENT_OPENED: 156 virtio_serial_open(port); 157 break; 158 case CHR_EVENT_CLOSED: 159 if (vcon->watch) { 160 g_source_remove(vcon->watch); 161 vcon->watch = 0; 162 } 163 virtio_serial_close(port); 164 break; 165 } 166 } 167 168 static void virtconsole_realize(DeviceState *dev, Error **errp) 169 { 170 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev); 171 VirtConsole *vcon = VIRTIO_CONSOLE(dev); 172 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev); 173 174 if (port->id == 0 && !k->is_console) { 175 error_setg(errp, "Port number 0 on virtio-serial devices reserved " 176 "for virtconsole devices for backward compatibility."); 177 return; 178 } 179 180 if (vcon->chr) { 181 /* 182 * For consoles we don't block guest data transfer just 183 * because nothing is connected - we'll just let it go 184 * whetherever the chardev wants - /dev/null probably. 185 * 186 * For serial ports we need 100% reliable data transfer 187 * so we use the opened/closed signals from chardev to 188 * trigger open/close of the device 189 */ 190 if (k->is_console) { 191 vcon->chr->explicit_fe_open = 0; 192 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, 193 NULL, vcon); 194 virtio_serial_open(port); 195 } else { 196 vcon->chr->explicit_fe_open = 1; 197 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, 198 chr_event, vcon); 199 } 200 } 201 } 202 203 static void virtconsole_unrealize(DeviceState *dev, Error **errp) 204 { 205 VirtConsole *vcon = VIRTIO_CONSOLE(dev); 206 207 if (vcon->watch) { 208 g_source_remove(vcon->watch); 209 } 210 } 211 212 static void virtconsole_class_init(ObjectClass *klass, void *data) 213 { 214 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); 215 216 k->is_console = true; 217 } 218 219 static const TypeInfo virtconsole_info = { 220 .name = "virtconsole", 221 .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT, 222 .class_init = virtconsole_class_init, 223 }; 224 225 static Property virtserialport_properties[] = { 226 DEFINE_PROP_CHR("chardev", VirtConsole, chr), 227 DEFINE_PROP_END_OF_LIST(), 228 }; 229 230 static void virtserialport_class_init(ObjectClass *klass, void *data) 231 { 232 DeviceClass *dc = DEVICE_CLASS(klass); 233 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); 234 235 k->realize = virtconsole_realize; 236 k->unrealize = virtconsole_unrealize; 237 k->have_data = flush_buf; 238 k->set_guest_connected = set_guest_connected; 239 k->guest_writable = guest_writable; 240 dc->props = virtserialport_properties; 241 } 242 243 static const TypeInfo virtserialport_info = { 244 .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT, 245 .parent = TYPE_VIRTIO_SERIAL_PORT, 246 .instance_size = sizeof(VirtConsole), 247 .class_init = virtserialport_class_init, 248 }; 249 250 static void virtconsole_register_types(void) 251 { 252 type_register_static(&virtserialport_info); 253 type_register_static(&virtconsole_info); 254 } 255 256 type_init(virtconsole_register_types) 257