2 * Virtio Console and Generic Serial Port Devices
4 * Copyright Red Hat, Inc. 2009, 2010
7 * Amit Shah <amit.shah@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "chardev/char-fe.h"
15 #include "qemu/error-report.h"
16 #include "qemu/module.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"
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
)
30 VirtIOSerialPort parent_obj
;
37 * Callback function that's called from chardevs when backend becomes
40 static gboolean
chr_write_unblocked(GIOChannel
*chan
, GIOCondition cond
,
43 VirtConsole
*vcon
= opaque
;
46 virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon
), false);
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
)
54 VirtConsole
*vcon
= VIRTIO_CONSOLE(port
);
57 if (!qemu_chr_fe_backend_connected(&vcon
->chr
)) {
58 /* If there's no backend, we can just say we consumed all data. */
62 ret
= qemu_chr_fe_write(&vcon
->chr
, buf
, len
);
63 trace_virtio_console_flush_buf(port
->id
, len
, ret
);
66 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
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
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
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.
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.
98 virtio_serial_throttle_port(port
, true);
100 vcon
->watch
= qemu_chr_fe_add_watch(&vcon
->chr
,
102 chr_write_unblocked
, vcon
);
109 /* Callback function that's called when the guest opens/closes the port */
110 static void set_guest_connected(VirtIOSerialPort
*port
, int guest_connected
)
112 VirtConsole
*vcon
= VIRTIO_CONSOLE(port
);
113 DeviceState
*dev
= DEVICE(port
);
114 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
116 if (!k
->is_console
) {
117 qemu_chr_fe_set_open(&vcon
->chr
, guest_connected
);
121 qapi_event_send_vserport_change(dev
->id
, guest_connected
);
125 static void guest_writable(VirtIOSerialPort
*port
)
127 VirtConsole
*vcon
= VIRTIO_CONSOLE(port
);
129 qemu_chr_fe_accept_input(&vcon
->chr
);
132 /* Readiness of the guest to accept data on a port */
133 static int chr_can_read(void *opaque
)
135 VirtConsole
*vcon
= opaque
;
137 return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon
));
140 /* Send data from a char device over to the guest */
141 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
143 VirtConsole
*vcon
= opaque
;
144 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(vcon
);
146 trace_virtio_console_chr_read(port
->id
, size
);
147 virtio_serial_write(port
, buf
, size
);
150 static void chr_event(void *opaque
, QEMUChrEvent event
)
152 VirtConsole
*vcon
= opaque
;
153 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(vcon
);
155 trace_virtio_console_chr_event(port
->id
, event
);
157 case CHR_EVENT_OPENED
:
158 virtio_serial_open(port
);
160 case CHR_EVENT_CLOSED
:
162 g_source_remove(vcon
->watch
);
165 virtio_serial_close(port
);
167 case CHR_EVENT_BREAK
:
168 case CHR_EVENT_MUX_IN
:
169 case CHR_EVENT_MUX_OUT
:
175 static int chr_be_change(void *opaque
)
177 VirtConsole
*vcon
= opaque
;
178 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(vcon
);
179 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
182 qemu_chr_fe_set_handlers(&vcon
->chr
, chr_can_read
, chr_read
,
183 NULL
, chr_be_change
, vcon
, NULL
, true);
185 qemu_chr_fe_set_handlers(&vcon
->chr
, chr_can_read
, chr_read
,
186 chr_event
, chr_be_change
, vcon
, NULL
, false);
190 g_source_remove(vcon
->watch
);
191 vcon
->watch
= qemu_chr_fe_add_watch(&vcon
->chr
,
193 chr_write_unblocked
, vcon
);
199 static void virtconsole_enable_backend(VirtIOSerialPort
*port
, bool enable
)
201 VirtConsole
*vcon
= VIRTIO_CONSOLE(port
);
203 if (!qemu_chr_fe_backend_connected(&vcon
->chr
)) {
208 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
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);
214 qemu_chr_fe_set_handlers(&vcon
->chr
, NULL
, NULL
, NULL
,
215 NULL
, NULL
, NULL
, false);
219 static void virtconsole_realize(DeviceState
*dev
, Error
**errp
)
221 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
222 VirtConsole
*vcon
= VIRTIO_CONSOLE(dev
);
223 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_GET_CLASS(dev
);
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.");
231 if (qemu_chr_fe_backend_connected(&vcon
->chr
)) {
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.
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
242 qemu_chr_fe_set_handlers(&vcon
->chr
, chr_can_read
, chr_read
,
245 virtio_serial_open(port
);
247 qemu_chr_fe_set_handlers(&vcon
->chr
, chr_can_read
, chr_read
,
248 chr_event
, chr_be_change
,
254 static void virtconsole_unrealize(DeviceState
*dev
)
256 VirtConsole
*vcon
= VIRTIO_CONSOLE(dev
);
259 g_source_remove(vcon
->watch
);
263 static void virtconsole_class_init(ObjectClass
*klass
, void *data
)
265 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_CLASS(klass
);
267 k
->is_console
= true;
270 static const TypeInfo virtconsole_info
= {
271 .name
= "virtconsole",
272 .parent
= TYPE_VIRTIO_CONSOLE_SERIAL_PORT
,
273 .class_init
= virtconsole_class_init
,
276 static Property virtserialport_properties
[] = {
277 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
278 DEFINE_PROP_END_OF_LIST(),
281 static void virtserialport_class_init(ObjectClass
*klass
, void *data
)
283 DeviceClass
*dc
= DEVICE_CLASS(klass
);
284 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_CLASS(klass
);
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
);
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
,
302 static void virtconsole_register_types(void)
304 type_register_static(&virtserialport_info
);
305 type_register_static(&virtconsole_info
);
308 type_init(virtconsole_register_types
)