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