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 "sysemu/char.h"
15 #include "qemu/error-report.h"
17 #include "hw/virtio/virtio-serial.h"
18 #include "qapi-event.h"
20 #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport"
21 #define VIRTIO_CONSOLE(obj) \
22 OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT)
24 typedef struct VirtConsole
{
25 VirtIOSerialPort parent_obj
;
32 * Callback function that's called from chardevs when backend becomes
35 static gboolean
chr_write_unblocked(GIOChannel
*chan
, GIOCondition cond
,
38 VirtConsole
*vcon
= opaque
;
41 virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon
), false);
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
)
49 VirtConsole
*vcon
= VIRTIO_CONSOLE(port
);
52 if (!qemu_chr_fe_get_driver(&vcon
->chr
)) {
53 /* If there's no backend, we can just say we consumed all data. */
57 ret
= qemu_chr_fe_write(&vcon
->chr
, buf
, len
);
58 trace_virtio_console_flush_buf(port
->id
, len
, ret
);
61 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
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
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
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.
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.
93 virtio_serial_throttle_port(port
, true);
95 vcon
->watch
= qemu_chr_fe_add_watch(&vcon
->chr
,
97 chr_write_unblocked
, vcon
);
104 /* Callback function that's called when the guest opens/closes the port */
105 static void set_guest_connected(VirtIOSerialPort
*port
, int guest_connected
)
107 VirtConsole
*vcon
= VIRTIO_CONSOLE(port
);
108 DeviceState
*dev
= DEVICE(port
);
109 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
111 if (!k
->is_console
) {
112 qemu_chr_fe_set_open(&vcon
->chr
, guest_connected
);
116 qapi_event_send_vserport_change(dev
->id
, guest_connected
,
121 static void guest_writable(VirtIOSerialPort
*port
)
123 VirtConsole
*vcon
= VIRTIO_CONSOLE(port
);
125 qemu_chr_fe_accept_input(&vcon
->chr
);
128 /* Readiness of the guest to accept data on a port */
129 static int chr_can_read(void *opaque
)
131 VirtConsole
*vcon
= opaque
;
133 return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon
));
136 /* Send data from a char device over to the guest */
137 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
139 VirtConsole
*vcon
= opaque
;
140 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(vcon
);
142 trace_virtio_console_chr_read(port
->id
, size
);
143 virtio_serial_write(port
, buf
, size
);
146 static void chr_event(void *opaque
, int event
)
148 VirtConsole
*vcon
= opaque
;
149 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(vcon
);
151 trace_virtio_console_chr_event(port
->id
, event
);
153 case CHR_EVENT_OPENED
:
154 virtio_serial_open(port
);
156 case CHR_EVENT_CLOSED
:
158 g_source_remove(vcon
->watch
);
161 virtio_serial_close(port
);
166 static void virtconsole_realize(DeviceState
*dev
, Error
**errp
)
168 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
169 VirtConsole
*vcon
= VIRTIO_CONSOLE(dev
);
170 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_GET_CLASS(dev
);
171 Chardev
*chr
= qemu_chr_fe_get_driver(&vcon
->chr
);
173 if (port
->id
== 0 && !k
->is_console
) {
174 error_setg(errp
, "Port number 0 on virtio-serial devices reserved "
175 "for virtconsole devices for backward compatibility.");
181 * For consoles we don't block guest data transfer just
182 * because nothing is connected - we'll just let it go
183 * whetherever the chardev wants - /dev/null probably.
185 * For serial ports we need 100% reliable data transfer
186 * so we use the opened/closed signals from chardev to
187 * trigger open/close of the device
190 qemu_chr_fe_set_handlers(&vcon
->chr
, chr_can_read
, chr_read
,
191 NULL
, vcon
, NULL
, true);
192 virtio_serial_open(port
);
194 qemu_chr_fe_set_handlers(&vcon
->chr
, chr_can_read
, chr_read
,
195 chr_event
, vcon
, NULL
, false);
200 static void virtconsole_unrealize(DeviceState
*dev
, Error
**errp
)
202 VirtConsole
*vcon
= VIRTIO_CONSOLE(dev
);
205 g_source_remove(vcon
->watch
);
209 static void virtconsole_class_init(ObjectClass
*klass
, void *data
)
211 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_CLASS(klass
);
213 k
->is_console
= true;
216 static const TypeInfo virtconsole_info
= {
217 .name
= "virtconsole",
218 .parent
= TYPE_VIRTIO_CONSOLE_SERIAL_PORT
,
219 .class_init
= virtconsole_class_init
,
222 static Property virtserialport_properties
[] = {
223 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
224 DEFINE_PROP_END_OF_LIST(),
227 static void virtserialport_class_init(ObjectClass
*klass
, void *data
)
229 DeviceClass
*dc
= DEVICE_CLASS(klass
);
230 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_CLASS(klass
);
232 k
->realize
= virtconsole_realize
;
233 k
->unrealize
= virtconsole_unrealize
;
234 k
->have_data
= flush_buf
;
235 k
->set_guest_connected
= set_guest_connected
;
236 k
->guest_writable
= guest_writable
;
237 dc
->props
= virtserialport_properties
;
240 static const TypeInfo virtserialport_info
= {
241 .name
= TYPE_VIRTIO_CONSOLE_SERIAL_PORT
,
242 .parent
= TYPE_VIRTIO_SERIAL_PORT
,
243 .instance_size
= sizeof(VirtConsole
),
244 .class_init
= virtserialport_class_init
,
247 static void virtconsole_register_types(void)
249 type_register_static(&virtserialport_info
);
250 type_register_static(&virtconsole_info
);
253 type_init(virtconsole_register_types
)