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