]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/char/virtio-console.c
qapi: Drop qapi_event_send_FOO()'s Error ** argument
[thirdparty/qemu.git] / hw / char / virtio-console.c
CommitLineData
98b19252
AS
1/*
2 * Virtio Console and Generic Serial Port Devices
3 *
71c092e9 4 * Copyright Red Hat, Inc. 2009, 2010
98b19252
AS
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
9b8bfe21 13#include "qemu/osdep.h"
4d43a603 14#include "chardev/char-fe.h"
1de7afc9 15#include "qemu/error-report.h"
d02e4fa4 16#include "trace.h"
0d09e41a 17#include "hw/virtio/virtio-serial.h"
e688df6b 18#include "qapi/error.h"
9af23989 19#include "qapi/qapi-events-char.h"
98b19252 20
be21c336 21#define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport"
0399a381 22#define VIRTIO_CONSOLE(obj) \
be21c336 23 OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT)
0399a381 24
98b19252 25typedef struct VirtConsole {
0399a381
AF
26 VirtIOSerialPort parent_obj;
27
becdfa00 28 CharBackend chr;
c3d6b96e 29 guint watch;
98b19252
AS
30} VirtConsole;
31
7df4d457
AS
32/*
33 * Callback function that's called from chardevs when backend becomes
34 * writable.
35 */
36static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
37 void *opaque)
38{
39 VirtConsole *vcon = opaque;
40
c3d6b96e 41 vcon->watch = 0;
0399a381 42 virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false);
7df4d457
AS
43 return FALSE;
44}
98b19252
AS
45
46/* Callback function that's called when the guest sends us data */
f9fb0532
HG
47static ssize_t flush_buf(VirtIOSerialPort *port,
48 const uint8_t *buf, ssize_t len)
98b19252 49{
0399a381 50 VirtConsole *vcon = VIRTIO_CONSOLE(port);
d02e4fa4 51 ssize_t ret;
98b19252 52
30650701 53 if (!qemu_chr_fe_backend_connected(&vcon->chr)) {
6640422c
AS
54 /* If there's no backend, we can just say we consumed all data. */
55 return len;
56 }
57
5345fdb4 58 ret = qemu_chr_fe_write(&vcon->chr, buf, len);
d02e4fa4 59 trace_virtio_console_flush_buf(port->id, len, ret);
0219d732 60
f9fb0532 61 if (ret < len) {
d6258c93
AS
62 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
63
0219d732
AS
64 /*
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
d6258c93 68 * this connection.
0219d732 69 */
f9fb0532
HG
70 if (ret < 0)
71 ret = 0;
6ab3fc32
DB
72
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.
80 *
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.
85 *
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.
92 */
d6258c93
AS
93 if (!k->is_console) {
94 virtio_serial_throttle_port(port, true);
c3d6b96e 95 if (!vcon->watch) {
5345fdb4
MAL
96 vcon->watch = qemu_chr_fe_add_watch(&vcon->chr,
97 G_IO_OUT|G_IO_HUP,
c3d6b96e
HG
98 chr_write_unblocked, vcon);
99 }
d6258c93 100 }
0219d732 101 }
d02e4fa4 102 return ret;
98b19252
AS
103}
104
b2c1394a
HG
105/* Callback function that's called when the guest opens/closes the port */
106static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
0b6d2266 107{
0399a381 108 VirtConsole *vcon = VIRTIO_CONSOLE(port);
e2ae6159 109 DeviceState *dev = DEVICE(port);
bce6261e 110 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
0b6d2266 111
fa394ed6 112 if (!k->is_console) {
5345fdb4 113 qemu_chr_fe_set_open(&vcon->chr, guest_connected);
e2ae6159
LE
114 }
115
116 if (dev->id) {
3ab72385 117 qapi_event_send_vserport_change(dev->id, guest_connected);
6640422c 118 }
0b6d2266
HG
119}
120
246ca55f
MAL
121static void guest_writable(VirtIOSerialPort *port)
122{
123 VirtConsole *vcon = VIRTIO_CONSOLE(port);
124
fa394ed6 125 qemu_chr_fe_accept_input(&vcon->chr);
246ca55f
MAL
126}
127
98b19252
AS
128/* Readiness of the guest to accept data on a port */
129static int chr_can_read(void *opaque)
130{
131 VirtConsole *vcon = opaque;
132
0399a381 133 return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon));
98b19252
AS
134}
135
136/* Send data from a char device over to the guest */
137static void chr_read(void *opaque, const uint8_t *buf, int size)
138{
139 VirtConsole *vcon = opaque;
0399a381 140 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
98b19252 141
0399a381
AF
142 trace_virtio_console_chr_read(port->id, size);
143 virtio_serial_write(port, buf, size);
98b19252
AS
144}
145
146static void chr_event(void *opaque, int event)
147{
148 VirtConsole *vcon = opaque;
0399a381 149 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
98b19252 150
0399a381 151 trace_virtio_console_chr_event(port->id, event);
98b19252 152 switch (event) {
28eaf465 153 case CHR_EVENT_OPENED:
0399a381 154 virtio_serial_open(port);
98b19252 155 break;
98b19252 156 case CHR_EVENT_CLOSED:
c3d6b96e
HG
157 if (vcon->watch) {
158 g_source_remove(vcon->watch);
159 vcon->watch = 0;
160 }
0399a381 161 virtio_serial_close(port);
98b19252
AS
162 break;
163 }
164}
165
af50855c
AN
166static int chr_be_change(void *opaque)
167{
168 VirtConsole *vcon = opaque;
169 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
170 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
171
172 if (k->is_console) {
173 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
174 NULL, chr_be_change, vcon, NULL, true);
175 } else {
176 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
177 chr_event, chr_be_change, vcon, NULL, false);
178 }
179
180 if (vcon->watch) {
181 g_source_remove(vcon->watch);
182 vcon->watch = qemu_chr_fe_add_watch(&vcon->chr,
183 G_IO_OUT | G_IO_HUP,
184 chr_write_unblocked, vcon);
185 }
186
187 return 0;
188}
189
55289fb0
PB
190static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
191{
192 VirtConsole *vcon = VIRTIO_CONSOLE(port);
193
194 if (!qemu_chr_fe_backend_connected(&vcon->chr)) {
195 return;
196 }
197
198 if (enable) {
199 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
200
201 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
202 k->is_console ? NULL : chr_event,
203 chr_be_change, vcon, NULL, false);
204 } else {
205 qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL, NULL,
206 NULL, NULL, NULL, false);
207 }
208}
209
2ef66625 210static void virtconsole_realize(DeviceState *dev, Error **errp)
98b19252 211{
2ef66625
AF
212 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
213 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
214 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
a15bb0d6 215
f82e35e3 216 if (port->id == 0 && !k->is_console) {
2ef66625
AF
217 error_setg(errp, "Port number 0 on virtio-serial devices reserved "
218 "for virtconsole devices for backward compatibility.");
219 return;
7edfe652
MA
220 }
221
30650701 222 if (qemu_chr_fe_backend_connected(&vcon->chr)) {
bce6261e
DB
223 /*
224 * For consoles we don't block guest data transfer just
225 * because nothing is connected - we'll just let it go
226 * whetherever the chardev wants - /dev/null probably.
227 *
228 * For serial ports we need 100% reliable data transfer
229 * so we use the opened/closed signals from chardev to
230 * trigger open/close of the device
231 */
232 if (k->is_console) {
5345fdb4 233 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
af50855c
AN
234 NULL, chr_be_change,
235 vcon, NULL, true);
bce6261e
DB
236 virtio_serial_open(port);
237 } else {
5345fdb4 238 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
af50855c
AN
239 chr_event, chr_be_change,
240 vcon, NULL, false);
bce6261e 241 }
98b19252 242 }
cbe77b61
AS
243}
244
2ef66625 245static void virtconsole_unrealize(DeviceState *dev, Error **errp)
c3d6b96e 246{
2ef66625 247 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
c3d6b96e
HG
248
249 if (vcon->watch) {
250 g_source_remove(vcon->watch);
251 }
c3d6b96e
HG
252}
253
f82e35e3
AL
254static void virtconsole_class_init(ObjectClass *klass, void *data)
255{
256 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
257
258 k->is_console = true;
f82e35e3
AL
259}
260
8c43a6f0 261static const TypeInfo virtconsole_info = {
be21c336
AF
262 .name = "virtconsole",
263 .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
39bffca2 264 .class_init = virtconsole_class_init,
98b19252
AS
265};
266
f82e35e3
AL
267static Property virtserialport_properties[] = {
268 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
269 DEFINE_PROP_END_OF_LIST(),
270};
271
272static void virtserialport_class_init(ObjectClass *klass, void *data)
273{
39bffca2 274 DeviceClass *dc = DEVICE_CLASS(klass);
f82e35e3
AL
275 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
276
2ef66625
AF
277 k->realize = virtconsole_realize;
278 k->unrealize = virtconsole_unrealize;
f82e35e3 279 k->have_data = flush_buf;
b2c1394a 280 k->set_guest_connected = set_guest_connected;
55289fb0 281 k->enable_backend = virtconsole_enable_backend;
246ca55f 282 k->guest_writable = guest_writable;
39bffca2 283 dc->props = virtserialport_properties;
f82e35e3
AL
284}
285
8c43a6f0 286static const TypeInfo virtserialport_info = {
be21c336 287 .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
39bffca2
AL
288 .parent = TYPE_VIRTIO_SERIAL_PORT,
289 .instance_size = sizeof(VirtConsole),
290 .class_init = virtserialport_class_init,
b60c470b
AS
291};
292
83f7d43a 293static void virtconsole_register_types(void)
b60c470b 294{
39bffca2 295 type_register_static(&virtserialport_info);
be21c336 296 type_register_static(&virtconsole_info);
b60c470b 297}
83f7d43a
AF
298
299type_init(virtconsole_register_types)