include/standard-headers: add pvrdma related headers
[qemu.git] / hw / char / virtio-console.c
blob4be5d4ee52481b68984ed45d54cbf8806f0e9086
1 /*
2 * Virtio Console and Generic Serial Port Devices
4 * Copyright Red Hat, Inc. 2009, 2010
6 * Authors:
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 "trace.h"
17 #include "hw/virtio/virtio-serial.h"
18 #include "qapi-event.h"
19 #include "qapi/error.h"
21 #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport"
22 #define VIRTIO_CONSOLE(obj) \
23 OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT)
25 typedef struct VirtConsole {
26 VirtIOSerialPort parent_obj;
28 CharBackend chr;
29 guint watch;
30 } VirtConsole;
33 * Callback function that's called from chardevs when backend becomes
34 * writable.
36 static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
37 void *opaque)
39 VirtConsole *vcon = opaque;
41 vcon->watch = 0;
42 virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false);
43 return FALSE;
46 /* Callback function that's called when the guest sends us data */
47 static ssize_t flush_buf(VirtIOSerialPort *port,
48 const uint8_t *buf, ssize_t len)
50 VirtConsole *vcon = VIRTIO_CONSOLE(port);
51 ssize_t ret;
53 if (!qemu_chr_fe_backend_connected(&vcon->chr)) {
54 /* If there's no backend, we can just say we consumed all data. */
55 return len;
58 ret = qemu_chr_fe_write(&vcon->chr, buf, len);
59 trace_virtio_console_flush_buf(port->id, len, ret);
61 if (ret < len) {
62 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
65 * Ideally we'd get a better error code than just -1, but
66 * that's what the chardev interface gives us right now. If
67 * we had a finer-grained message, like -EPIPE, we could close
68 * this connection.
70 if (ret < 0)
71 ret = 0;
73 /* XXX we should be queuing data to send later for the
74 * console devices too rather than silently dropping
75 * console data on EAGAIN. The Linux virtio-console
76 * hvc driver though does sends with spinlocks held,
77 * so if we enable throttling that'll stall the entire
78 * guest kernel, not merely the process writing to the
79 * console.
81 * While we could queue data for later write without
82 * enabling throttling, this would result in the guest
83 * being able to trigger arbitrary memory usage in QEMU
84 * buffering data for later writes.
86 * So fixing this problem likely requires fixing the
87 * Linux virtio-console hvc driver to not hold spinlocks
88 * while writing, and instead merely block the process
89 * that's writing. QEMU would then need some way to detect
90 * if the guest had the fixed driver too, before we can
91 * use throttling on host side.
93 if (!k->is_console) {
94 virtio_serial_throttle_port(port, true);
95 if (!vcon->watch) {
96 vcon->watch = qemu_chr_fe_add_watch(&vcon->chr,
97 G_IO_OUT|G_IO_HUP,
98 chr_write_unblocked, vcon);
102 return ret;
105 /* Callback function that's called when the guest opens/closes the port */
106 static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
108 VirtConsole *vcon = VIRTIO_CONSOLE(port);
109 DeviceState *dev = DEVICE(port);
110 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
112 if (!k->is_console) {
113 qemu_chr_fe_set_open(&vcon->chr, guest_connected);
116 if (dev->id) {
117 qapi_event_send_vserport_change(dev->id, guest_connected,
118 &error_abort);
122 static void guest_writable(VirtIOSerialPort *port)
124 VirtConsole *vcon = VIRTIO_CONSOLE(port);
126 qemu_chr_fe_accept_input(&vcon->chr);
129 /* Readiness of the guest to accept data on a port */
130 static int chr_can_read(void *opaque)
132 VirtConsole *vcon = opaque;
134 return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon));
137 /* Send data from a char device over to the guest */
138 static void chr_read(void *opaque, const uint8_t *buf, int size)
140 VirtConsole *vcon = opaque;
141 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
143 trace_virtio_console_chr_read(port->id, size);
144 virtio_serial_write(port, buf, size);
147 static void chr_event(void *opaque, int event)
149 VirtConsole *vcon = opaque;
150 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
152 trace_virtio_console_chr_event(port->id, event);
153 switch (event) {
154 case CHR_EVENT_OPENED:
155 virtio_serial_open(port);
156 break;
157 case CHR_EVENT_CLOSED:
158 if (vcon->watch) {
159 g_source_remove(vcon->watch);
160 vcon->watch = 0;
162 virtio_serial_close(port);
163 break;
167 static int chr_be_change(void *opaque)
169 VirtConsole *vcon = opaque;
170 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
171 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
173 if (k->is_console) {
174 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
175 NULL, chr_be_change, vcon, NULL, true);
176 } else {
177 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
178 chr_event, chr_be_change, vcon, NULL, false);
181 if (vcon->watch) {
182 g_source_remove(vcon->watch);
183 vcon->watch = qemu_chr_fe_add_watch(&vcon->chr,
184 G_IO_OUT | G_IO_HUP,
185 chr_write_unblocked, vcon);
188 return 0;
191 static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
193 VirtConsole *vcon = VIRTIO_CONSOLE(port);
195 if (!qemu_chr_fe_backend_connected(&vcon->chr)) {
196 return;
199 if (enable) {
200 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
202 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
203 k->is_console ? NULL : chr_event,
204 chr_be_change, vcon, NULL, false);
205 } else {
206 qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL, NULL,
207 NULL, NULL, NULL, false);
211 static void virtconsole_realize(DeviceState *dev, Error **errp)
213 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
214 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
215 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
217 if (port->id == 0 && !k->is_console) {
218 error_setg(errp, "Port number 0 on virtio-serial devices reserved "
219 "for virtconsole devices for backward compatibility.");
220 return;
223 if (qemu_chr_fe_backend_connected(&vcon->chr)) {
225 * For consoles we don't block guest data transfer just
226 * because nothing is connected - we'll just let it go
227 * whetherever the chardev wants - /dev/null probably.
229 * For serial ports we need 100% reliable data transfer
230 * so we use the opened/closed signals from chardev to
231 * trigger open/close of the device
233 if (k->is_console) {
234 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
235 NULL, chr_be_change,
236 vcon, NULL, true);
237 virtio_serial_open(port);
238 } else {
239 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
240 chr_event, chr_be_change,
241 vcon, NULL, false);
246 static void virtconsole_unrealize(DeviceState *dev, Error **errp)
248 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
250 if (vcon->watch) {
251 g_source_remove(vcon->watch);
255 static void virtconsole_class_init(ObjectClass *klass, void *data)
257 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
259 k->is_console = true;
262 static const TypeInfo virtconsole_info = {
263 .name = "virtconsole",
264 .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
265 .class_init = virtconsole_class_init,
268 static Property virtserialport_properties[] = {
269 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
270 DEFINE_PROP_END_OF_LIST(),
273 static void virtserialport_class_init(ObjectClass *klass, void *data)
275 DeviceClass *dc = DEVICE_CLASS(klass);
276 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
278 k->realize = virtconsole_realize;
279 k->unrealize = virtconsole_unrealize;
280 k->have_data = flush_buf;
281 k->set_guest_connected = set_guest_connected;
282 k->enable_backend = virtconsole_enable_backend;
283 k->guest_writable = guest_writable;
284 dc->props = virtserialport_properties;
287 static const TypeInfo virtserialport_info = {
288 .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
289 .parent = TYPE_VIRTIO_SERIAL_PORT,
290 .instance_size = sizeof(VirtConsole),
291 .class_init = virtserialport_class_init,
294 static void virtconsole_register_types(void)
296 type_register_static(&virtserialport_info);
297 type_register_static(&virtconsole_info);
300 type_init(virtconsole_register_types)