| 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 "chardev/char-fe.h" |
| 15 | #include "qemu/error-report.h" |
| 16 | #include "qemu/module.h" |
| 17 | #include "trace.h" |
| 18 | #include "hw/core/qdev-properties.h" |
| 19 | #include "hw/core/qdev-properties-system.h" |
| 20 | #include "hw/virtio/virtio-serial.h" |
| 21 | #include "qapi/error.h" |
| 22 | #include "qapi/qapi-events-char.h" |
| 23 | #include "qom/object.h" |
| 24 | |
| 25 | #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport" |
| 26 | typedef struct VirtConsole VirtConsole; |
| 27 | DECLARE_INSTANCE_CHECKER(VirtConsole, VIRTIO_CONSOLE, |
| 28 | TYPE_VIRTIO_CONSOLE_SERIAL_PORT) |
| 29 | |
| 30 | struct VirtConsole { |
| 31 | VirtIOSerialPort parent_obj; |
| 32 | |
| 33 | CharFrontend chr; |
| 34 | guint watch; |
| 35 | }; |
| 36 | |
| 37 | /* |
| 38 | * Callback function that's called from chardevs when backend becomes |
| 39 | * writable. |
| 40 | */ |
| 41 | static gboolean chr_write_unblocked(void *do_not_use, GIOCondition cond, |
| 42 | void *opaque) |
| 43 | { |
| 44 | VirtConsole *vcon = opaque; |
| 45 | |
| 46 | vcon->watch = 0; |
| 47 | virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false); |
| 48 | return G_SOURCE_REMOVE; |
| 49 | } |
| 50 | |
| 51 | /* Callback function that's called when the guest sends us data */ |
| 52 | static ssize_t flush_buf(VirtIOSerialPort *port, |
| 53 | const uint8_t *buf, ssize_t len) |
| 54 | { |
| 55 | VirtConsole *vcon = VIRTIO_CONSOLE(port); |
| 56 | ssize_t ret; |
| 57 | |
| 58 | if (!qemu_chr_fe_backend_connected(&vcon->chr)) { |
| 59 | /* If there's no backend, we can just say we consumed all data. */ |
| 60 | return len; |
| 61 | } |
| 62 | |
| 63 | ret = qemu_chr_fe_write(&vcon->chr, buf, len); |
| 64 | trace_virtio_console_flush_buf(port->id, len, ret); |
| 65 | |
| 66 | if (ret < len) { |
| 67 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
| 68 | |
| 69 | /* |
| 70 | * Ideally we'd get a better error code than just -1, but |
| 71 | * that's what the chardev interface gives us right now. If |
| 72 | * we had a finer-grained message, like -EPIPE, we could close |
| 73 | * this connection. |
| 74 | */ |
| 75 | if (ret < 0) |
| 76 | ret = 0; |
| 77 | |
| 78 | /* XXX we should be queuing data to send later for the |
| 79 | * console devices too rather than silently dropping |
| 80 | * console data on EAGAIN. The Linux virtio-console |
| 81 | * hvc driver though does sends with spinlocks held, |
| 82 | * so if we enable throttling that'll stall the entire |
| 83 | * guest kernel, not merely the process writing to the |
| 84 | * console. |
| 85 | * |
| 86 | * While we could queue data for later write without |
| 87 | * enabling throttling, this would result in the guest |
| 88 | * being able to trigger arbitrary memory usage in QEMU |
| 89 | * buffering data for later writes. |
| 90 | * |
| 91 | * So fixing this problem likely requires fixing the |
| 92 | * Linux virtio-console hvc driver to not hold spinlocks |
| 93 | * while writing, and instead merely block the process |
| 94 | * that's writing. QEMU would then need some way to detect |
| 95 | * if the guest had the fixed driver too, before we can |
| 96 | * use throttling on host side. |
| 97 | */ |
| 98 | if (!k->is_console) { |
| 99 | virtio_serial_throttle_port(port, true); |
| 100 | if (!vcon->watch) { |
| 101 | vcon->watch = qemu_chr_fe_add_watch(&vcon->chr, |
| 102 | G_IO_OUT|G_IO_HUP, |
| 103 | chr_write_unblocked, vcon); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | /* Callback function that's called when the guest opens/closes the port */ |
| 111 | static void set_guest_connected(VirtIOSerialPort *port, int guest_connected) |
| 112 | { |
| 113 | VirtConsole *vcon = VIRTIO_CONSOLE(port); |
| 114 | DeviceState *dev = DEVICE(port); |
| 115 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
| 116 | |
| 117 | if (!k->is_console) { |
| 118 | qemu_chr_fe_set_open(&vcon->chr, guest_connected); |
| 119 | } |
| 120 | |
| 121 | if (dev->id) { |
| 122 | qapi_event_send_vserport_change(dev->id, guest_connected); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static void guest_writable(VirtIOSerialPort *port) |
| 127 | { |
| 128 | VirtConsole *vcon = VIRTIO_CONSOLE(port); |
| 129 | |
| 130 | qemu_chr_fe_accept_input(&vcon->chr); |
| 131 | } |
| 132 | |
| 133 | /* Readiness of the guest to accept data on a port */ |
| 134 | static int chr_can_read(void *opaque) |
| 135 | { |
| 136 | VirtConsole *vcon = opaque; |
| 137 | |
| 138 | return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon)); |
| 139 | } |
| 140 | |
| 141 | /* Send data from a char device over to the guest */ |
| 142 | static void chr_read(void *opaque, const uint8_t *buf, int size) |
| 143 | { |
| 144 | VirtConsole *vcon = opaque; |
| 145 | VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon); |
| 146 | |
| 147 | trace_virtio_console_chr_read(port->id, size); |
| 148 | virtio_serial_write(port, buf, size); |
| 149 | } |
| 150 | |
| 151 | static void chr_event(void *opaque, QEMUChrEvent event) |
| 152 | { |
| 153 | VirtConsole *vcon = opaque; |
| 154 | VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon); |
| 155 | |
| 156 | trace_virtio_console_chr_event(port->id, event); |
| 157 | switch (event) { |
| 158 | case CHR_EVENT_OPENED: |
| 159 | virtio_serial_open(port); |
| 160 | break; |
| 161 | case CHR_EVENT_CLOSED: |
| 162 | g_clear_handle_id(&vcon->watch, g_source_remove); |
| 163 | virtio_serial_close(port); |
| 164 | break; |
| 165 | case CHR_EVENT_BREAK: |
| 166 | case CHR_EVENT_MUX_IN: |
| 167 | case CHR_EVENT_MUX_OUT: |
| 168 | /* Ignore */ |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static int chr_be_change(void *opaque) |
| 174 | { |
| 175 | VirtConsole *vcon = opaque; |
| 176 | VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon); |
| 177 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
| 178 | |
| 179 | if (k->is_console) { |
| 180 | qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read, |
| 181 | NULL, chr_be_change, vcon, NULL, true); |
| 182 | } else { |
| 183 | qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read, |
| 184 | chr_event, chr_be_change, vcon, NULL, false); |
| 185 | } |
| 186 | |
| 187 | if (vcon->watch) { |
| 188 | g_source_remove(vcon->watch); |
| 189 | vcon->watch = qemu_chr_fe_add_watch(&vcon->chr, |
| 190 | G_IO_OUT | G_IO_HUP, |
| 191 | chr_write_unblocked, vcon); |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable) |
| 198 | { |
| 199 | VirtConsole *vcon = VIRTIO_CONSOLE(port); |
| 200 | |
| 201 | if (!qemu_chr_fe_backend_connected(&vcon->chr)) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (enable) { |
| 206 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
| 207 | |
| 208 | qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read, |
| 209 | k->is_console ? NULL : chr_event, |
| 210 | chr_be_change, vcon, NULL, false); |
| 211 | } else { |
| 212 | qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL, NULL, |
| 213 | NULL, NULL, NULL, false); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static void virtconsole_realize(DeviceState *dev, Error **errp) |
| 218 | { |
| 219 | VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev); |
| 220 | VirtConsole *vcon = VIRTIO_CONSOLE(dev); |
| 221 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev); |
| 222 | |
| 223 | if (port->id == 0 && !k->is_console) { |
| 224 | error_setg(errp, "Port number 0 on virtio-serial devices reserved " |
| 225 | "for virtconsole devices for backward compatibility."); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | if (qemu_chr_fe_backend_connected(&vcon->chr)) { |
| 230 | /* |
| 231 | * For consoles we don't block guest data transfer just |
| 232 | * because nothing is connected - we'll just let it go |
| 233 | * whetherever the chardev wants - /dev/null probably. |
| 234 | * |
| 235 | * For serial ports we need 100% reliable data transfer |
| 236 | * so we use the opened/closed signals from chardev to |
| 237 | * trigger open/close of the device |
| 238 | */ |
| 239 | if (k->is_console) { |
| 240 | qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read, |
| 241 | NULL, chr_be_change, |
| 242 | vcon, NULL, true); |
| 243 | virtio_serial_open(port); |
| 244 | } else { |
| 245 | qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read, |
| 246 | chr_event, chr_be_change, |
| 247 | vcon, NULL, false); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static void virtconsole_unrealize(DeviceState *dev) |
| 253 | { |
| 254 | VirtConsole *vcon = VIRTIO_CONSOLE(dev); |
| 255 | g_clear_handle_id(&vcon->watch, g_source_remove); |
| 256 | } |
| 257 | |
| 258 | static void virtconsole_class_init(ObjectClass *klass, const void *data) |
| 259 | { |
| 260 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); |
| 261 | |
| 262 | k->is_console = true; |
| 263 | } |
| 264 | |
| 265 | static const TypeInfo virtconsole_info = { |
| 266 | .name = "virtconsole", |
| 267 | .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT, |
| 268 | .class_init = virtconsole_class_init, |
| 269 | }; |
| 270 | |
| 271 | static const Property virtserialport_properties[] = { |
| 272 | DEFINE_PROP_CHR("chardev", VirtConsole, chr), |
| 273 | }; |
| 274 | |
| 275 | static void virtserialport_class_init(ObjectClass *klass, const void *data) |
| 276 | { |
| 277 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 278 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); |
| 279 | |
| 280 | k->realize = virtconsole_realize; |
| 281 | k->unrealize = virtconsole_unrealize; |
| 282 | k->have_data = flush_buf; |
| 283 | k->set_guest_connected = set_guest_connected; |
| 284 | k->enable_backend = virtconsole_enable_backend; |
| 285 | k->guest_writable = guest_writable; |
| 286 | device_class_set_props(dc, virtserialport_properties); |
| 287 | } |
| 288 | |
| 289 | static const TypeInfo virtserialport_info = { |
| 290 | .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT, |
| 291 | .parent = TYPE_VIRTIO_SERIAL_PORT, |
| 292 | .instance_size = sizeof(VirtConsole), |
| 293 | .class_init = virtserialport_class_init, |
| 294 | }; |
| 295 | |
| 296 | static void virtconsole_register_types(void) |
| 297 | { |
| 298 | type_register_static(&virtserialport_info); |
| 299 | type_register_static(&virtconsole_info); |
| 300 | } |
| 301 | |
| 302 | type_init(virtconsole_register_types) |