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