tests/vm: update NetBSD to 9.1
[qemu/ar7.git] / hw / char / virtio-console.c
blobbc752cf90f36fb4a1196c28c5e316c11ff7cdda8
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 "qemu/module.h"
17 #include "trace.h"
18 #include "hw/qdev-properties.h"
19 #include "hw/virtio/virtio-serial.h"
20 #include "qapi/error.h"
21 #include "qapi/qapi-events-char.h"
22 #include "qom/object.h"
24 #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport"
25 typedef struct VirtConsole VirtConsole;
26 DECLARE_INSTANCE_CHECKER(VirtConsole, VIRTIO_CONSOLE,
27 TYPE_VIRTIO_CONSOLE_SERIAL_PORT)
29 struct VirtConsole {
30 VirtIOSerialPort parent_obj;
32 CharBackend chr;
33 guint watch;
37 * Callback function that's called from chardevs when backend becomes
38 * writable.
40 static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
41 void *opaque)
43 VirtConsole *vcon = opaque;
45 vcon->watch = 0;
46 virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false);
47 return FALSE;
50 /* Callback function that's called when the guest sends us data */
51 static ssize_t flush_buf(VirtIOSerialPort *port,
52 const uint8_t *buf, ssize_t len)
54 VirtConsole *vcon = VIRTIO_CONSOLE(port);
55 ssize_t ret;
57 if (!qemu_chr_fe_backend_connected(&vcon->chr)) {
58 /* If there's no backend, we can just say we consumed all data. */
59 return len;
62 ret = qemu_chr_fe_write(&vcon->chr, buf, len);
63 trace_virtio_console_flush_buf(port->id, len, ret);
65 if (ret < len) {
66 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
69 * Ideally we'd get a better error code than just -1, but
70 * that's what the chardev interface gives us right now. If
71 * we had a finer-grained message, like -EPIPE, we could close
72 * this connection.
74 if (ret < 0)
75 ret = 0;
77 /* XXX we should be queuing data to send later for the
78 * console devices too rather than silently dropping
79 * console data on EAGAIN. The Linux virtio-console
80 * hvc driver though does sends with spinlocks held,
81 * so if we enable throttling that'll stall the entire
82 * guest kernel, not merely the process writing to the
83 * console.
85 * While we could queue data for later write without
86 * enabling throttling, this would result in the guest
87 * being able to trigger arbitrary memory usage in QEMU
88 * buffering data for later writes.
90 * So fixing this problem likely requires fixing the
91 * Linux virtio-console hvc driver to not hold spinlocks
92 * while writing, and instead merely block the process
93 * that's writing. QEMU would then need some way to detect
94 * if the guest had the fixed driver too, before we can
95 * use throttling on host side.
97 if (!k->is_console) {
98 virtio_serial_throttle_port(port, true);
99 if (!vcon->watch) {
100 vcon->watch = qemu_chr_fe_add_watch(&vcon->chr,
101 G_IO_OUT|G_IO_HUP,
102 chr_write_unblocked, vcon);
106 return ret;
109 /* Callback function that's called when the guest opens/closes the port */
110 static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
112 VirtConsole *vcon = VIRTIO_CONSOLE(port);
113 DeviceState *dev = DEVICE(port);
114 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
116 if (!k->is_console) {
117 qemu_chr_fe_set_open(&vcon->chr, guest_connected);
120 if (dev->id) {
121 qapi_event_send_vserport_change(dev->id, guest_connected);
125 static void guest_writable(VirtIOSerialPort *port)
127 VirtConsole *vcon = VIRTIO_CONSOLE(port);
129 qemu_chr_fe_accept_input(&vcon->chr);
132 /* Readiness of the guest to accept data on a port */
133 static int chr_can_read(void *opaque)
135 VirtConsole *vcon = opaque;
137 return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon));
140 /* Send data from a char device over to the guest */
141 static void chr_read(void *opaque, const uint8_t *buf, int size)
143 VirtConsole *vcon = opaque;
144 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
146 trace_virtio_console_chr_read(port->id, size);
147 virtio_serial_write(port, buf, size);
150 static void chr_event(void *opaque, QEMUChrEvent event)
152 VirtConsole *vcon = opaque;
153 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
155 trace_virtio_console_chr_event(port->id, event);
156 switch (event) {
157 case CHR_EVENT_OPENED:
158 virtio_serial_open(port);
159 break;
160 case CHR_EVENT_CLOSED:
161 if (vcon->watch) {
162 g_source_remove(vcon->watch);
163 vcon->watch = 0;
165 virtio_serial_close(port);
166 break;
167 case CHR_EVENT_BREAK:
168 case CHR_EVENT_MUX_IN:
169 case CHR_EVENT_MUX_OUT:
170 /* Ignore */
171 break;
175 static int chr_be_change(void *opaque)
177 VirtConsole *vcon = opaque;
178 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
179 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
181 if (k->is_console) {
182 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
183 NULL, chr_be_change, vcon, NULL, true);
184 } else {
185 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
186 chr_event, chr_be_change, vcon, NULL, false);
189 if (vcon->watch) {
190 g_source_remove(vcon->watch);
191 vcon->watch = qemu_chr_fe_add_watch(&vcon->chr,
192 G_IO_OUT | G_IO_HUP,
193 chr_write_unblocked, vcon);
196 return 0;
199 static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
201 VirtConsole *vcon = VIRTIO_CONSOLE(port);
203 if (!qemu_chr_fe_backend_connected(&vcon->chr)) {
204 return;
207 if (enable) {
208 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
210 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
211 k->is_console ? NULL : chr_event,
212 chr_be_change, vcon, NULL, false);
213 } else {
214 qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL, NULL,
215 NULL, NULL, NULL, false);
219 static void virtconsole_realize(DeviceState *dev, Error **errp)
221 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
222 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
223 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
225 if (port->id == 0 && !k->is_console) {
226 error_setg(errp, "Port number 0 on virtio-serial devices reserved "
227 "for virtconsole devices for backward compatibility.");
228 return;
231 if (qemu_chr_fe_backend_connected(&vcon->chr)) {
233 * For consoles we don't block guest data transfer just
234 * because nothing is connected - we'll just let it go
235 * whetherever the chardev wants - /dev/null probably.
237 * For serial ports we need 100% reliable data transfer
238 * so we use the opened/closed signals from chardev to
239 * trigger open/close of the device
241 if (k->is_console) {
242 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
243 NULL, chr_be_change,
244 vcon, NULL, true);
245 virtio_serial_open(port);
246 } else {
247 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
248 chr_event, chr_be_change,
249 vcon, NULL, false);
254 static void virtconsole_unrealize(DeviceState *dev)
256 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
258 if (vcon->watch) {
259 g_source_remove(vcon->watch);
263 static void virtconsole_class_init(ObjectClass *klass, void *data)
265 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
267 k->is_console = true;
270 static const TypeInfo virtconsole_info = {
271 .name = "virtconsole",
272 .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
273 .class_init = virtconsole_class_init,
276 static Property virtserialport_properties[] = {
277 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
278 DEFINE_PROP_END_OF_LIST(),
281 static void virtserialport_class_init(ObjectClass *klass, void *data)
283 DeviceClass *dc = DEVICE_CLASS(klass);
284 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
286 k->realize = virtconsole_realize;
287 k->unrealize = virtconsole_unrealize;
288 k->have_data = flush_buf;
289 k->set_guest_connected = set_guest_connected;
290 k->enable_backend = virtconsole_enable_backend;
291 k->guest_writable = guest_writable;
292 device_class_set_props(dc, virtserialport_properties);
295 static const TypeInfo virtserialport_info = {
296 .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
297 .parent = TYPE_VIRTIO_SERIAL_PORT,
298 .instance_size = sizeof(VirtConsole),
299 .class_init = virtserialport_class_init,
302 static void virtconsole_register_types(void)
304 type_register_static(&virtserialport_info);
305 type_register_static(&virtconsole_info);
308 type_init(virtconsole_register_types)