master
c 654 lines 19.3 KB
Raw
1 /*
2 * Copyright (C) International Business Machines Corp., 2005
3 * Author(s): Anthony Liguori <aliguori@us.ibm.com>
4 *
5 * Copyright (C) Red Hat 2007
6 *
7 * Xen Console
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; under version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "qemu/osdep.h"
23 #include "qemu/cutils.h"
24 #include <sys/select.h>
25 #include <termios.h>
26
27 #include "qapi/error.h"
28 #include "system/system.h"
29 #include "chardev/char-fe.h"
30 #include "hw/xen/xen-backend.h"
31 #include "hw/xen/xen-bus-helper.h"
32 #include "hw/core/qdev-properties.h"
33 #include "hw/core/qdev-properties-system.h"
34 #include "hw/xen/interface/io/console.h"
35 #include "hw/xen/interface/io/xs_wire.h"
36 #include "hw/xen/interface/grant_table.h"
37 #include "hw/i386/kvm/xen_primary_console.h"
38 #include "trace.h"
39
40 struct buffer {
41 uint8_t *data;
42 size_t consumed;
43 size_t size;
44 size_t capacity;
45 size_t max_capacity;
46 };
47
48 struct XenConsole {
49 struct XenDevice xendev; /* must be first */
50 XenEventChannel *event_channel;
51 int dev;
52 struct buffer buffer;
53 char *fe_path;
54 unsigned int ring_ref;
55 void *sring;
56 CharFrontend chr;
57 int backlog;
58 };
59
60 #define TYPE_XEN_CONSOLE_DEVICE "xen-console"
61 OBJECT_DECLARE_SIMPLE_TYPE(XenConsole, XEN_CONSOLE_DEVICE)
62
63 static bool buffer_append(XenConsole *con)
64 {
65 struct buffer *buffer = &con->buffer;
66 XENCONS_RING_IDX cons, prod, size;
67 struct xencons_interface *intf = con->sring;
68
69 cons = intf->out_cons;
70 prod = intf->out_prod;
71 xen_mb();
72
73 size = prod - cons;
74 if ((size == 0) || (size > sizeof(intf->out)))
75 return false;
76
77 if ((buffer->capacity - buffer->size) < size) {
78 buffer->capacity += (size + 1024);
79 buffer->data = g_realloc(buffer->data, buffer->capacity);
80 }
81
82 while (cons != prod)
83 buffer->data[buffer->size++] = intf->out[
84 MASK_XENCONS_IDX(cons++, intf->out)];
85
86 xen_mb();
87 intf->out_cons = cons;
88 xen_device_notify_event_channel(XEN_DEVICE(con), con->event_channel, NULL);
89
90 if (buffer->max_capacity &&
91 buffer->size > buffer->max_capacity) {
92 /* Discard the middle of the data. */
93
94 size_t over = buffer->size - buffer->max_capacity;
95 uint8_t *maxpos = buffer->data + buffer->max_capacity;
96
97 memmove(maxpos - over, maxpos, over);
98 buffer->data = g_realloc(buffer->data, buffer->max_capacity);
99 buffer->size = buffer->capacity = buffer->max_capacity;
100
101 if (buffer->consumed > buffer->max_capacity - over)
102 buffer->consumed = buffer->max_capacity - over;
103 }
104 return true;
105 }
106
107 static void buffer_advance(struct buffer *buffer, size_t len)
108 {
109 buffer->consumed += len;
110 if (buffer->consumed == buffer->size) {
111 buffer->consumed = 0;
112 buffer->size = 0;
113 }
114 }
115
116 static int ring_free_bytes(XenConsole *con)
117 {
118 struct xencons_interface *intf = con->sring;
119 XENCONS_RING_IDX cons, prod, space;
120
121 cons = intf->in_cons;
122 prod = intf->in_prod;
123 xen_mb();
124
125 space = prod - cons;
126 if (space > sizeof(intf->in))
127 return 0; /* ring is screwed: ignore it */
128
129 return (sizeof(intf->in) - space);
130 }
131
132 static int xencons_can_receive(void *opaque)
133 {
134 XenConsole *con = opaque;
135 return ring_free_bytes(con);
136 }
137
138 static void xencons_receive(void *opaque, const uint8_t *buf, int len)
139 {
140 XenConsole *con = opaque;
141 struct xencons_interface *intf = con->sring;
142 XENCONS_RING_IDX prod;
143 int i, max;
144
145 max = ring_free_bytes(con);
146 /* The can_receive() func limits this, but check again anyway */
147 if (max < len)
148 len = max;
149
150 prod = intf->in_prod;
151 for (i = 0; i < len; i++) {
152 intf->in[MASK_XENCONS_IDX(prod++, intf->in)] =
153 buf[i];
154 }
155 xen_wmb();
156 intf->in_prod = prod;
157 xen_device_notify_event_channel(XEN_DEVICE(con), con->event_channel, NULL);
158 }
159
160 static bool xencons_send(XenConsole *con)
161 {
162 ssize_t len, size;
163
164 size = con->buffer.size - con->buffer.consumed;
165 if (qemu_chr_fe_backend_connected(&con->chr)) {
166 len = qemu_chr_fe_write(&con->chr,
167 con->buffer.data + con->buffer.consumed,
168 size);
169 } else {
170 len = size;
171 }
172 if (len < 1) {
173 if (!con->backlog) {
174 con->backlog = 1;
175 }
176 } else {
177 buffer_advance(&con->buffer, len);
178 if (con->backlog && len == size) {
179 con->backlog = 0;
180 }
181 }
182 return len > 0;
183 }
184
185 /* -------------------------------------------------------------------- */
186
187 static bool con_event(void *_xendev)
188 {
189 XenConsole *con = XEN_CONSOLE_DEVICE(_xendev);
190 bool done_something;
191
192 if (xen_device_backend_get_state(&con->xendev) != XenbusStateConnected) {
193 return false;
194 }
195
196 done_something = buffer_append(con);
197
198 if (con->buffer.size - con->buffer.consumed) {
199 done_something |= xencons_send(con);
200 }
201 return done_something;
202 }
203
204 /* -------------------------------------------------------------------- */
205
206 static bool xen_console_connect(XenDevice *xendev, Error **errp)
207 {
208 ERRP_GUARD();
209 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
210 unsigned int port, limit;
211
212 if (xen_device_frontend_scanf(xendev, "ring-ref", "%u",
213 &con->ring_ref) != 1) {
214 error_setg(errp, "failed to read ring-ref");
215 return false;
216 }
217
218 if (xen_device_frontend_scanf(xendev, "port", "%u", &port) != 1) {
219 error_setg(errp, "failed to read remote port");
220 return false;
221 }
222
223 if (xen_device_frontend_scanf(xendev, "limit", "%u", &limit) == 1) {
224 con->buffer.max_capacity = limit;
225 }
226
227 con->event_channel = xen_device_bind_event_channel(xendev, port,
228 con_event,
229 con,
230 errp);
231 if (!con->event_channel) {
232 return false;
233 }
234
235 switch (con->dev) {
236 case 0:
237 /*
238 * The primary console is special. For real Xen the ring-ref is
239 * actually a GFN which needs to be mapped as foreignmem.
240 */
241 if (xen_mode != XEN_EMULATE) {
242 xen_pfn_t mfn = (xen_pfn_t)con->ring_ref;
243 con->sring = qemu_xen_foreignmem_map(xendev->frontend_id, NULL,
244 PROT_READ | PROT_WRITE,
245 1, &mfn, NULL);
246 if (!con->sring) {
247 error_setg(errp, "failed to map console page");
248 return false;
249 }
250 break;
251 }
252
253 /*
254 * For Xen emulation, we still follow the convention of ring-ref
255 * holding the GFN, but we map the fixed GNTTAB_RESERVED_CONSOLE
256 * grant ref because there is no implementation of foreignmem
257 * operations for emulated mode. The emulation code which handles
258 * the guest-side page and event channel also needs to be informed
259 * of the backend event channel port, in order to reconnect to it
260 * after a soft reset.
261 */
262 xen_primary_console_set_be_port(
263 xen_event_channel_get_local_port(con->event_channel));
264 con->ring_ref = GNTTAB_RESERVED_CONSOLE;
265 /* fallthrough */
266 default:
267 con->sring = xen_device_map_grant_refs(xendev,
268 &con->ring_ref, 1,
269 PROT_READ | PROT_WRITE,
270 errp);
271 if (!con->sring) {
272 error_prepend(errp, "failed to map console grant ref: ");
273 return false;
274 }
275 break;
276 }
277
278 trace_xen_console_connect(con->dev, con->ring_ref, port,
279 con->buffer.max_capacity);
280
281 qemu_chr_fe_set_handlers(&con->chr, xencons_can_receive,
282 xencons_receive, NULL, NULL, con, NULL,
283 true);
284 return true;
285 }
286
287 static void xen_console_disconnect(XenDevice *xendev, Error **errp)
288 {
289 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
290
291 trace_xen_console_disconnect(con->dev);
292
293 qemu_chr_fe_set_handlers(&con->chr, NULL, NULL, NULL, NULL,
294 con, NULL, true);
295
296 if (con->event_channel) {
297 xen_device_unbind_event_channel(xendev, con->event_channel,
298 errp);
299 con->event_channel = NULL;
300
301 if (xen_mode == XEN_EMULATE && !con->dev) {
302 xen_primary_console_set_be_port(0);
303 }
304 }
305
306 if (con->sring) {
307 if (!con->dev && xen_mode != XEN_EMULATE) {
308 qemu_xen_foreignmem_unmap(con->sring, 1);
309 } else {
310 xen_device_unmap_grant_refs(xendev, con->sring,
311 &con->ring_ref, 1, errp);
312 }
313 con->sring = NULL;
314 }
315 }
316
317 static void xen_console_frontend_changed(XenDevice *xendev,
318 enum xenbus_state frontend_state,
319 Error **errp)
320 {
321 ERRP_GUARD();
322 enum xenbus_state backend_state = xen_device_backend_get_state(xendev);
323
324 switch (frontend_state) {
325 case XenbusStateInitialised:
326 case XenbusStateConnected:
327 if (backend_state == XenbusStateConnected) {
328 break;
329 }
330
331 xen_console_disconnect(xendev, errp);
332 if (*errp) {
333 break;
334 }
335
336 if (!xen_console_connect(xendev, errp)) {
337 xen_device_backend_set_state(xendev, XenbusStateClosing);
338 break;
339 }
340
341 xen_device_backend_set_state(xendev, XenbusStateConnected);
342 break;
343
344 case XenbusStateClosing:
345 xen_device_backend_set_state(xendev, XenbusStateClosing);
346 break;
347
348 case XenbusStateClosed:
349 case XenbusStateUnknown:
350 xen_console_disconnect(xendev, errp);
351 if (*errp) {
352 break;
353 }
354
355 xen_device_backend_set_state(xendev, XenbusStateClosed);
356 break;
357
358 default:
359 break;
360 }
361 }
362
363 static char *xen_console_get_name(XenDevice *xendev, Error **errp)
364 {
365 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
366
367 if (con->dev == -1) {
368 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
369 int idx = (xen_mode == XEN_EMULATE) ? 0 : 1;
370 Error *local_err = NULL;
371 char *value;
372
373 /* Theoretically we could go up to INT_MAX here but that's overkill */
374 while (idx < 100) {
375 if (!idx) {
376 value = xs_node_read(xenbus->xsh, XBT_NULL, NULL, &local_err,
377 "/local/domain/%u/console",
378 xendev->frontend_id);
379 } else {
380 value = xs_node_read(xenbus->xsh, XBT_NULL, NULL, &local_err,
381 "/local/domain/%u/device/console/%u",
382 xendev->frontend_id, idx);
383 }
384 if (!value) {
385 if (errno == ENOENT) {
386 con->dev = idx;
387 error_free(local_err);
388 goto found;
389 }
390 error_propagate(errp, local_err);
391 return NULL;
392 }
393 free(value);
394 idx++;
395 }
396 error_setg(errp, "cannot find device index for console device");
397 return NULL;
398 }
399 found:
400 return g_strdup_printf("%u", con->dev);
401 }
402
403 static void xen_console_unrealize(XenDevice *xendev)
404 {
405 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
406
407 trace_xen_console_unrealize(con->dev);
408
409 /* Disconnect from the frontend in case this has not already happened */
410 xen_console_disconnect(xendev, NULL);
411
412 qemu_chr_fe_deinit(&con->chr, false);
413 }
414
415 static void xen_console_realize(XenDevice *xendev, Error **errp)
416 {
417 ERRP_GUARD();
418 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
419 Chardev *cs = qemu_chr_fe_get_driver(&con->chr);
420 unsigned int u;
421 g_autofree char *pty_name = NULL;
422
423 if (!cs) {
424 error_setg(errp, "no backing character device");
425 return;
426 }
427
428 if (con->dev == -1) {
429 error_setg(errp, "no device index provided");
430 return;
431 }
432
433 /*
434 * The Xen primary console is special. The ring-ref is actually a GFN to
435 * be mapped directly as foreignmem (not a grant ref), and the guest port
436 * was allocated *for* the guest by the toolstack. The guest gets these
437 * through HVMOP_get_param and can use the console long before it's got
438 * XenStore up and running. We cannot create those for a true Xen guest,
439 * but we can for Xen emulation.
440 */
441 if (!con->dev) {
442 if (xen_mode == XEN_EMULATE) {
443 xen_primary_console_create();
444 } else if (xen_device_frontend_scanf(xendev, "ring-ref", "%u", &u)
445 != 1 ||
446 xen_device_frontend_scanf(xendev, "port", "%u", &u) != 1) {
447 error_setg(errp, "cannot create primary Xen console");
448 return;
449 }
450 }
451
452 trace_xen_console_realize(con->dev, object_get_typename(OBJECT(cs)));
453
454 pty_name = qemu_chr_get_pty_name(cs);
455 if (pty_name) {
456 xen_device_frontend_printf(xendev, "tty", "%s", pty_name);
457 }
458
459 /* No normal PV driver initialization for the primary console under Xen */
460 if (!con->dev && xen_mode != XEN_EMULATE) {
461 xen_console_connect(xendev, errp);
462 }
463 }
464
465 static char *console_frontend_path(struct qemu_xs_handle *xenstore,
466 unsigned int dom_id, unsigned int dev)
467 {
468 if (!dev) {
469 return g_strdup_printf("/local/domain/%u/console", dom_id);
470 } else {
471 return g_strdup_printf("/local/domain/%u/device/console/%u", dom_id,
472 dev);
473 }
474 }
475
476 static char *xen_console_get_frontend_path(XenDevice *xendev, Error **errp)
477 {
478 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
479 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
480 char *ret = console_frontend_path(xenbus->xsh, xendev->frontend_id,
481 con->dev);
482
483 if (!ret) {
484 error_setg(errp, "failed to create frontend path");
485 }
486 return ret;
487 }
488
489
490 static const Property xen_console_properties[] = {
491 DEFINE_PROP_CHR("chardev", XenConsole, chr),
492 DEFINE_PROP_INT32("idx", XenConsole, dev, -1),
493 };
494
495 static void xen_console_class_init(ObjectClass *class, const void *data)
496 {
497 DeviceClass *dev_class = DEVICE_CLASS(class);
498 XenDeviceClass *xendev_class = XEN_DEVICE_CLASS(class);
499
500 xendev_class->backend = "console";
501 xendev_class->device = "console";
502 xendev_class->get_name = xen_console_get_name;
503 xendev_class->realize = xen_console_realize;
504 xendev_class->frontend_changed = xen_console_frontend_changed;
505 xendev_class->unrealize = xen_console_unrealize;
506 xendev_class->get_frontend_path = xen_console_get_frontend_path;
507
508 device_class_set_props(dev_class, xen_console_properties);
509 }
510
511 static const TypeInfo xen_console_type_info = {
512 .name = TYPE_XEN_CONSOLE_DEVICE,
513 .parent = TYPE_XEN_DEVICE,
514 .instance_size = sizeof(XenConsole),
515 .class_init = xen_console_class_init,
516 };
517
518 static void xen_console_register_types(void)
519 {
520 type_register_static(&xen_console_type_info);
521 }
522
523 type_init(xen_console_register_types)
524
525 /* Called to instantiate a XenConsole when the backend is detected. */
526 static void xen_console_device_create(XenBackendInstance *backend,
527 QDict *opts, Error **errp)
528 {
529 ERRP_GUARD();
530 XenBus *xenbus = xen_backend_get_bus(backend);
531 const char *name = xen_backend_get_name(backend);
532 unsigned long number;
533 char *fe = NULL, *type = NULL, *output = NULL;
534 char label[32];
535 XenDevice *xendev = NULL;
536 XenConsole *con;
537 Chardev *cd = NULL;
538 struct qemu_xs_handle *xsh = xenbus->xsh;
539
540 if (qemu_strtoul(name, NULL, 10, &number) || number > INT_MAX) {
541 error_setg(errp, "failed to parse name '%s'", name);
542 goto fail;
543 }
544
545 trace_xen_console_device_create(number);
546
547 fe = console_frontend_path(xsh, xen_domid, number);
548 if (fe == NULL) {
549 error_setg(errp, "failed to generate frontend path");
550 goto fail;
551 }
552
553 type = xs_node_read(xsh, XBT_NULL, NULL, errp, "%s/%s", fe, "type");
554 if (!type) {
555 error_prepend(errp, "failed to read console device type: ");
556 goto fail;
557 }
558
559 if (strcmp(type, "ioemu")) {
560 error_setg(errp, "declining to handle console type '%s'",
561 type);
562 goto fail;
563 }
564
565 xendev = XEN_DEVICE(qdev_new(TYPE_XEN_CONSOLE_DEVICE));
566 con = XEN_CONSOLE_DEVICE(xendev);
567
568 con->dev = number;
569
570 snprintf(label, sizeof(label), "xencons%ld", number);
571
572 output = xs_node_read(xsh, XBT_NULL, NULL, errp, "%s/%s", fe, "output");
573 if (output) {
574 /*
575 * FIXME: sure we want to support implicit
576 * muxed monitors here?
577 */
578 cd = qemu_chr_new_mux_mon(label, output, NULL);
579 if (!cd) {
580 error_setg(errp, "console: No valid chardev found at '%s': ",
581 output);
582 goto fail;
583 }
584 } else if (errno != ENOENT) {
585 error_prepend(errp, "console: No valid chardev found: ");
586 goto fail;
587 } else {
588 error_free(*errp);
589 *errp = NULL;
590
591 if (number) {
592 cd = serial_hd(number);
593 if (!cd) {
594 error_setg(errp, "console: No serial device #%ld found",
595 number);
596 goto fail;
597 }
598 } else {
599 /* No 'output' node on primary console: use null. */
600 cd = qemu_chr_new(label, "null", NULL);
601 if (!cd) {
602 error_setg(errp, "console: failed to create null device");
603 goto fail;
604 }
605 }
606 }
607
608 if (!qemu_chr_fe_init(&con->chr, cd, errp)) {
609 error_prepend(errp, "console: failed to initialize backing chardev: ");
610 goto fail;
611 }
612
613 if (qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
614 xen_backend_set_device(backend, xendev);
615 goto done;
616 }
617
618 error_prepend(errp, "realization of console device %lu failed: ",
619 number);
620
621 fail:
622 if (xendev) {
623 object_unparent(OBJECT(xendev));
624 }
625 done:
626 g_free(fe);
627 free(type);
628 free(output);
629 }
630
631 static void xen_console_device_destroy(XenBackendInstance *backend,
632 Error **errp)
633 {
634 ERRP_GUARD();
635 XenDevice *xendev = xen_backend_get_device(backend);
636 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
637
638 trace_xen_console_device_destroy(con->dev);
639
640 object_unparent(OBJECT(xendev));
641 }
642
643 static const XenBackendInfo xen_console_backend_info = {
644 .type = "console",
645 .create = xen_console_device_create,
646 .destroy = xen_console_device_destroy,
647 };
648
649 static void xen_console_register_backend(void)
650 {
651 xen_backend_register(&xen_console_backend_info);
652 }
653
654 xen_backend_init(xen_console_register_backend);