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