]>
Commit | Line | Data |
---|---|---|
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 <[email protected]> | |
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" |
e2ae6159 | 18 | #include "qapi-event.h" |
98b19252 | 19 | |
be21c336 | 20 | #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport" |
0399a381 | 21 | #define VIRTIO_CONSOLE(obj) \ |
be21c336 | 22 | OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT) |
0399a381 | 23 | |
98b19252 | 24 | typedef struct VirtConsole { |
0399a381 AF |
25 | VirtIOSerialPort parent_obj; |
26 | ||
becdfa00 | 27 | CharBackend chr; |
c3d6b96e | 28 | guint watch; |
98b19252 AS |
29 | } VirtConsole; |
30 | ||
7df4d457 AS |
31 | /* |
32 | * Callback function that's called from chardevs when backend becomes | |
33 | * writable. | |
34 | */ | |
35 | static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond, | |
36 | void *opaque) | |
37 | { | |
38 | VirtConsole *vcon = opaque; | |
39 | ||
c3d6b96e | 40 | vcon->watch = 0; |
0399a381 | 41 | virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false); |
7df4d457 AS |
42 | return FALSE; |
43 | } | |
98b19252 AS |
44 | |
45 | /* Callback function that's called when the guest sends us data */ | |
f9fb0532 HG |
46 | static ssize_t flush_buf(VirtIOSerialPort *port, |
47 | const uint8_t *buf, ssize_t len) | |
98b19252 | 48 | { |
0399a381 | 49 | VirtConsole *vcon = VIRTIO_CONSOLE(port); |
d02e4fa4 | 50 | ssize_t ret; |
98b19252 | 51 | |
5345fdb4 | 52 | if (!qemu_chr_fe_get_driver(&vcon->chr)) { |
6640422c AS |
53 | /* If there's no backend, we can just say we consumed all data. */ |
54 | return len; | |
55 | } | |
56 | ||
5345fdb4 | 57 | ret = qemu_chr_fe_write(&vcon->chr, buf, len); |
d02e4fa4 | 58 | trace_virtio_console_flush_buf(port->id, len, ret); |
0219d732 | 59 | |
f9fb0532 | 60 | if (ret < len) { |
d6258c93 AS |
61 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
62 | ||
0219d732 AS |
63 | /* |
64 | * Ideally we'd get a better error code than just -1, but | |
65 | * that's what the chardev interface gives us right now. If | |
66 | * we had a finer-grained message, like -EPIPE, we could close | |
d6258c93 | 67 | * this connection. |
0219d732 | 68 | */ |
f9fb0532 HG |
69 | if (ret < 0) |
70 | ret = 0; | |
6ab3fc32 DB |
71 | |
72 | /* XXX we should be queuing data to send later for the | |
73 | * console devices too rather than silently dropping | |
74 | * console data on EAGAIN. The Linux virtio-console | |
75 | * hvc driver though does sends with spinlocks held, | |
76 | * so if we enable throttling that'll stall the entire | |
77 | * guest kernel, not merely the process writing to the | |
78 | * console. | |
79 | * | |
80 | * While we could queue data for later write without | |
81 | * enabling throttling, this would result in the guest | |
82 | * being able to trigger arbitrary memory usage in QEMU | |
83 | * buffering data for later writes. | |
84 | * | |
85 | * So fixing this problem likely requires fixing the | |
86 | * Linux virtio-console hvc driver to not hold spinlocks | |
87 | * while writing, and instead merely block the process | |
88 | * that's writing. QEMU would then need some way to detect | |
89 | * if the guest had the fixed driver too, before we can | |
90 | * use throttling on host side. | |
91 | */ | |
d6258c93 AS |
92 | if (!k->is_console) { |
93 | virtio_serial_throttle_port(port, true); | |
c3d6b96e | 94 | if (!vcon->watch) { |
5345fdb4 MAL |
95 | vcon->watch = qemu_chr_fe_add_watch(&vcon->chr, |
96 | G_IO_OUT|G_IO_HUP, | |
c3d6b96e HG |
97 | chr_write_unblocked, vcon); |
98 | } | |
d6258c93 | 99 | } |
0219d732 | 100 | } |
d02e4fa4 | 101 | return ret; |
98b19252 AS |
102 | } |
103 | ||
b2c1394a HG |
104 | /* Callback function that's called when the guest opens/closes the port */ |
105 | static void set_guest_connected(VirtIOSerialPort *port, int guest_connected) | |
0b6d2266 | 106 | { |
0399a381 | 107 | VirtConsole *vcon = VIRTIO_CONSOLE(port); |
e2ae6159 | 108 | DeviceState *dev = DEVICE(port); |
bce6261e | 109 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
0b6d2266 | 110 | |
fa394ed6 | 111 | if (!k->is_console) { |
5345fdb4 | 112 | qemu_chr_fe_set_open(&vcon->chr, guest_connected); |
e2ae6159 LE |
113 | } |
114 | ||
115 | if (dev->id) { | |
116 | qapi_event_send_vserport_change(dev->id, guest_connected, | |
117 | &error_abort); | |
6640422c | 118 | } |
0b6d2266 HG |
119 | } |
120 | ||
246ca55f MAL |
121 | static 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 */ |
129 | static 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 */ | |
137 | static 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 | ||
146 | static 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 | ||
2ef66625 | 166 | static void virtconsole_realize(DeviceState *dev, Error **errp) |
98b19252 | 167 | { |
2ef66625 AF |
168 | VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev); |
169 | VirtConsole *vcon = VIRTIO_CONSOLE(dev); | |
170 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev); | |
0ec7b3e7 | 171 | Chardev *chr = qemu_chr_fe_get_driver(&vcon->chr); |
a15bb0d6 | 172 | |
f82e35e3 | 173 | if (port->id == 0 && !k->is_console) { |
2ef66625 AF |
174 | error_setg(errp, "Port number 0 on virtio-serial devices reserved " |
175 | "for virtconsole devices for backward compatibility."); | |
176 | return; | |
7edfe652 MA |
177 | } |
178 | ||
5345fdb4 | 179 | if (chr) { |
bce6261e DB |
180 | /* |
181 | * For consoles we don't block guest data transfer just | |
182 | * because nothing is connected - we'll just let it go | |
183 | * whetherever the chardev wants - /dev/null probably. | |
184 | * | |
185 | * For serial ports we need 100% reliable data transfer | |
186 | * so we use the opened/closed signals from chardev to | |
187 | * trigger open/close of the device | |
188 | */ | |
189 | if (k->is_console) { | |
5345fdb4 | 190 | qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read, |
81517ba3 | 191 | NULL, NULL, vcon, NULL, true); |
bce6261e DB |
192 | virtio_serial_open(port); |
193 | } else { | |
5345fdb4 | 194 | qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read, |
81517ba3 | 195 | chr_event, NULL, vcon, NULL, false); |
bce6261e | 196 | } |
98b19252 | 197 | } |
cbe77b61 AS |
198 | } |
199 | ||
2ef66625 | 200 | static void virtconsole_unrealize(DeviceState *dev, Error **errp) |
c3d6b96e | 201 | { |
2ef66625 | 202 | VirtConsole *vcon = VIRTIO_CONSOLE(dev); |
c3d6b96e HG |
203 | |
204 | if (vcon->watch) { | |
205 | g_source_remove(vcon->watch); | |
206 | } | |
c3d6b96e HG |
207 | } |
208 | ||
f82e35e3 AL |
209 | static void virtconsole_class_init(ObjectClass *klass, void *data) |
210 | { | |
211 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); | |
212 | ||
213 | k->is_console = true; | |
f82e35e3 AL |
214 | } |
215 | ||
8c43a6f0 | 216 | static const TypeInfo virtconsole_info = { |
be21c336 AF |
217 | .name = "virtconsole", |
218 | .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT, | |
39bffca2 | 219 | .class_init = virtconsole_class_init, |
98b19252 AS |
220 | }; |
221 | ||
f82e35e3 AL |
222 | static Property virtserialport_properties[] = { |
223 | DEFINE_PROP_CHR("chardev", VirtConsole, chr), | |
224 | DEFINE_PROP_END_OF_LIST(), | |
225 | }; | |
226 | ||
227 | static void virtserialport_class_init(ObjectClass *klass, void *data) | |
228 | { | |
39bffca2 | 229 | DeviceClass *dc = DEVICE_CLASS(klass); |
f82e35e3 AL |
230 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); |
231 | ||
2ef66625 AF |
232 | k->realize = virtconsole_realize; |
233 | k->unrealize = virtconsole_unrealize; | |
f82e35e3 | 234 | k->have_data = flush_buf; |
b2c1394a | 235 | k->set_guest_connected = set_guest_connected; |
246ca55f | 236 | k->guest_writable = guest_writable; |
39bffca2 | 237 | dc->props = virtserialport_properties; |
f82e35e3 AL |
238 | } |
239 | ||
8c43a6f0 | 240 | static const TypeInfo virtserialport_info = { |
be21c336 | 241 | .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT, |
39bffca2 AL |
242 | .parent = TYPE_VIRTIO_SERIAL_PORT, |
243 | .instance_size = sizeof(VirtConsole), | |
244 | .class_init = virtserialport_class_init, | |
b60c470b AS |
245 | }; |
246 | ||
83f7d43a | 247 | static void virtconsole_register_types(void) |
b60c470b | 248 | { |
39bffca2 | 249 | type_register_static(&virtserialport_info); |
be21c336 | 250 | type_register_static(&virtconsole_info); |
b60c470b | 251 | } |
83f7d43a AF |
252 | |
253 | type_init(virtconsole_register_types) |