Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / hw / char / xen_console.c
blob683c92aca1ce74f6af8f9639f02c8cd36f45a773
1 /*
2 * Copyright (C) International Business Machines Corp., 2005
3 * Author(s): Anthony Liguori <aliguori@us.ibm.com>
5 * Copyright (C) Red Hat 2007
7 * Xen Console
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.
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.
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/>.
22 #include "qemu/osdep.h"
23 #include "qemu/cutils.h"
24 #include <sys/select.h>
25 #include <termios.h>
27 #include "qapi/error.h"
28 #include "sysemu/sysemu.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/qdev-properties.h"
33 #include "hw/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"
40 struct buffer {
41 uint8_t *data;
42 size_t consumed;
43 size_t size;
44 size_t capacity;
45 size_t max_capacity;
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 CharBackend chr;
57 int backlog;
59 typedef struct XenConsole XenConsole;
61 #define TYPE_XEN_CONSOLE_DEVICE "xen-console"
62 OBJECT_DECLARE_SIMPLE_TYPE(XenConsole, XEN_CONSOLE_DEVICE)
64 static bool buffer_append(XenConsole *con)
66 struct buffer *buffer = &con->buffer;
67 XENCONS_RING_IDX cons, prod, size;
68 struct xencons_interface *intf = con->sring;
70 cons = intf->out_cons;
71 prod = intf->out_prod;
72 xen_mb();
74 size = prod - cons;
75 if ((size == 0) || (size > sizeof(intf->out)))
76 return false;
78 if ((buffer->capacity - buffer->size) < size) {
79 buffer->capacity += (size + 1024);
80 buffer->data = g_realloc(buffer->data, buffer->capacity);
83 while (cons != prod)
84 buffer->data[buffer->size++] = intf->out[
85 MASK_XENCONS_IDX(cons++, intf->out)];
87 xen_mb();
88 intf->out_cons = cons;
89 xen_device_notify_event_channel(XEN_DEVICE(con), con->event_channel, NULL);
91 if (buffer->max_capacity &&
92 buffer->size > buffer->max_capacity) {
93 /* Discard the middle of the data. */
95 size_t over = buffer->size - buffer->max_capacity;
96 uint8_t *maxpos = buffer->data + buffer->max_capacity;
98 memmove(maxpos - over, maxpos, over);
99 buffer->data = g_realloc(buffer->data, buffer->max_capacity);
100 buffer->size = buffer->capacity = buffer->max_capacity;
102 if (buffer->consumed > buffer->max_capacity - over)
103 buffer->consumed = buffer->max_capacity - over;
105 return true;
108 static void buffer_advance(struct buffer *buffer, size_t len)
110 buffer->consumed += len;
111 if (buffer->consumed == buffer->size) {
112 buffer->consumed = 0;
113 buffer->size = 0;
117 static int ring_free_bytes(XenConsole *con)
119 struct xencons_interface *intf = con->sring;
120 XENCONS_RING_IDX cons, prod, space;
122 cons = intf->in_cons;
123 prod = intf->in_prod;
124 xen_mb();
126 space = prod - cons;
127 if (space > sizeof(intf->in))
128 return 0; /* ring is screwed: ignore it */
130 return (sizeof(intf->in) - space);
133 static int xencons_can_receive(void *opaque)
135 XenConsole *con = opaque;
136 return ring_free_bytes(con);
139 static void xencons_receive(void *opaque, const uint8_t *buf, int len)
141 XenConsole *con = opaque;
142 struct xencons_interface *intf = con->sring;
143 XENCONS_RING_IDX prod;
144 int i, max;
146 max = ring_free_bytes(con);
147 /* The can_receive() func limits this, but check again anyway */
148 if (max < len)
149 len = max;
151 prod = intf->in_prod;
152 for (i = 0; i < len; i++) {
153 intf->in[MASK_XENCONS_IDX(prod++, intf->in)] =
154 buf[i];
156 xen_wmb();
157 intf->in_prod = prod;
158 xen_device_notify_event_channel(XEN_DEVICE(con), con->event_channel, NULL);
161 static bool xencons_send(XenConsole *con)
163 ssize_t len, size;
165 size = con->buffer.size - con->buffer.consumed;
166 if (qemu_chr_fe_backend_connected(&con->chr)) {
167 len = qemu_chr_fe_write(&con->chr,
168 con->buffer.data + con->buffer.consumed,
169 size);
170 } else {
171 len = size;
173 if (len < 1) {
174 if (!con->backlog) {
175 con->backlog = 1;
177 } else {
178 buffer_advance(&con->buffer, len);
179 if (con->backlog && len == size) {
180 con->backlog = 0;
183 return len > 0;
186 /* -------------------------------------------------------------------- */
188 static bool con_event(void *_xendev)
190 XenConsole *con = XEN_CONSOLE_DEVICE(_xendev);
191 bool done_something;
193 if (xen_device_backend_get_state(&con->xendev) != XenbusStateConnected) {
194 return false;
197 done_something = buffer_append(con);
199 if (con->buffer.size - con->buffer.consumed) {
200 done_something |= xencons_send(con);
202 return done_something;
205 /* -------------------------------------------------------------------- */
207 static bool xen_console_connect(XenDevice *xendev, Error **errp)
209 ERRP_GUARD();
210 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
211 unsigned int port, limit;
213 if (xen_device_frontend_scanf(xendev, "ring-ref", "%u",
214 &con->ring_ref) != 1) {
215 error_setg(errp, "failed to read ring-ref");
216 return false;
219 if (xen_device_frontend_scanf(xendev, "port", "%u", &port) != 1) {
220 error_setg(errp, "failed to read remote port");
221 return false;
224 if (xen_device_frontend_scanf(xendev, "limit", "%u", &limit) == 1) {
225 con->buffer.max_capacity = limit;
228 con->event_channel = xen_device_bind_event_channel(xendev, port,
229 con_event,
230 con,
231 errp);
232 if (!con->event_channel) {
233 return false;
236 switch (con->dev) {
237 case 0:
239 * The primary console is special. For real Xen the ring-ref is
240 * actually a GFN which needs to be mapped as foreignmem.
242 if (xen_mode != XEN_EMULATE) {
243 xen_pfn_t mfn = (xen_pfn_t)con->ring_ref;
244 con->sring = qemu_xen_foreignmem_map(xendev->frontend_id, NULL,
245 PROT_READ | PROT_WRITE,
246 1, &mfn, NULL);
247 if (!con->sring) {
248 error_setg(errp, "failed to map console page");
249 return false;
251 break;
255 * For Xen emulation, we still follow the convention of ring-ref
256 * holding the GFN, but we map the fixed GNTTAB_RESERVED_CONSOLE
257 * grant ref because there is no implementation of foreignmem
258 * operations for emulated mode. The emulation code which handles
259 * the guest-side page and event channel also needs to be informed
260 * of the backend event channel port, in order to reconnect to it
261 * after a soft reset.
263 xen_primary_console_set_be_port(
264 xen_event_channel_get_local_port(con->event_channel));
265 con->ring_ref = GNTTAB_RESERVED_CONSOLE;
266 /* fallthrough */
267 default:
268 con->sring = xen_device_map_grant_refs(xendev,
269 &con->ring_ref, 1,
270 PROT_READ | PROT_WRITE,
271 errp);
272 if (!con->sring) {
273 error_prepend(errp, "failed to map console grant ref: ");
274 return false;
276 break;
279 trace_xen_console_connect(con->dev, con->ring_ref, port,
280 con->buffer.max_capacity);
282 qemu_chr_fe_set_handlers(&con->chr, xencons_can_receive,
283 xencons_receive, NULL, NULL, con, NULL,
284 true);
285 return true;
288 static void xen_console_disconnect(XenDevice *xendev, Error **errp)
290 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
292 trace_xen_console_disconnect(con->dev);
294 qemu_chr_fe_set_handlers(&con->chr, NULL, NULL, NULL, NULL,
295 con, NULL, true);
297 if (con->event_channel) {
298 xen_device_unbind_event_channel(xendev, con->event_channel,
299 errp);
300 con->event_channel = NULL;
302 if (xen_mode == XEN_EMULATE && !con->dev) {
303 xen_primary_console_set_be_port(0);
307 if (con->sring) {
308 if (!con->dev && xen_mode != XEN_EMULATE) {
309 qemu_xen_foreignmem_unmap(con->sring, 1);
310 } else {
311 xen_device_unmap_grant_refs(xendev, con->sring,
312 &con->ring_ref, 1, errp);
314 con->sring = NULL;
318 static void xen_console_frontend_changed(XenDevice *xendev,
319 enum xenbus_state frontend_state,
320 Error **errp)
322 ERRP_GUARD();
323 enum xenbus_state backend_state = xen_device_backend_get_state(xendev);
325 switch (frontend_state) {
326 case XenbusStateInitialised:
327 case XenbusStateConnected:
328 if (backend_state == XenbusStateConnected) {
329 break;
332 xen_console_disconnect(xendev, errp);
333 if (*errp) {
334 break;
337 if (!xen_console_connect(xendev, errp)) {
338 xen_device_backend_set_state(xendev, XenbusStateClosing);
339 break;
342 xen_device_backend_set_state(xendev, XenbusStateConnected);
343 break;
345 case XenbusStateClosing:
346 xen_device_backend_set_state(xendev, XenbusStateClosing);
347 break;
349 case XenbusStateClosed:
350 case XenbusStateUnknown:
351 xen_console_disconnect(xendev, errp);
352 if (*errp) {
353 break;
356 xen_device_backend_set_state(xendev, XenbusStateClosed);
357 break;
359 default:
360 break;
364 static char *xen_console_get_name(XenDevice *xendev, Error **errp)
366 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
368 if (con->dev == -1) {
369 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
370 char fe_path[XENSTORE_ABS_PATH_MAX + 1];
371 int idx = (xen_mode == XEN_EMULATE) ? 0 : 1;
372 char *value;
374 /* Theoretically we could go up to INT_MAX here but that's overkill */
375 while (idx < 100) {
376 if (!idx) {
377 snprintf(fe_path, sizeof(fe_path),
378 "/local/domain/%u/console", xendev->frontend_id);
379 } else {
380 snprintf(fe_path, sizeof(fe_path),
381 "/local/domain/%u/device/console/%u",
382 xendev->frontend_id, idx);
384 value = qemu_xen_xs_read(xenbus->xsh, XBT_NULL, fe_path, NULL);
385 if (!value) {
386 if (errno == ENOENT) {
387 con->dev = idx;
388 goto found;
390 error_setg(errp, "cannot read %s: %s", fe_path,
391 strerror(errno));
392 return NULL;
394 free(value);
395 idx++;
397 error_setg(errp, "cannot find device index for console device");
398 return NULL;
400 found:
401 return g_strdup_printf("%u", con->dev);
404 static void xen_console_unrealize(XenDevice *xendev)
406 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
408 trace_xen_console_unrealize(con->dev);
410 /* Disconnect from the frontend in case this has not already happened */
411 xen_console_disconnect(xendev, NULL);
413 qemu_chr_fe_deinit(&con->chr, false);
416 static void xen_console_realize(XenDevice *xendev, Error **errp)
418 ERRP_GUARD();
419 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
420 Chardev *cs = qemu_chr_fe_get_driver(&con->chr);
421 unsigned int u;
423 if (!cs) {
424 error_setg(errp, "no backing character device");
425 return;
428 if (con->dev == -1) {
429 error_setg(errp, "no device index provided");
430 return;
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.
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;
452 trace_xen_console_realize(con->dev, object_get_typename(OBJECT(cs)));
454 if (CHARDEV_IS_PTY(cs)) {
455 /* Strip the leading 'pty:' */
456 xen_device_frontend_printf(xendev, "tty", "%s", cs->filename + 4);
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);
465 static char *console_frontend_path(struct qemu_xs_handle *xenstore,
466 unsigned int dom_id, unsigned int dev)
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);
476 static char *xen_console_get_frontend_path(XenDevice *xendev, Error **errp)
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);
483 if (!ret) {
484 error_setg(errp, "failed to create frontend path");
486 return ret;
490 static Property xen_console_properties[] = {
491 DEFINE_PROP_CHR("chardev", XenConsole, chr),
492 DEFINE_PROP_INT32("idx", XenConsole, dev, -1),
493 DEFINE_PROP_END_OF_LIST(),
496 static void xen_console_class_init(ObjectClass *class, void *data)
498 DeviceClass *dev_class = DEVICE_CLASS(class);
499 XenDeviceClass *xendev_class = XEN_DEVICE_CLASS(class);
501 xendev_class->backend = "console";
502 xendev_class->device = "console";
503 xendev_class->get_name = xen_console_get_name;
504 xendev_class->realize = xen_console_realize;
505 xendev_class->frontend_changed = xen_console_frontend_changed;
506 xendev_class->unrealize = xen_console_unrealize;
507 xendev_class->get_frontend_path = xen_console_get_frontend_path;
509 device_class_set_props(dev_class, xen_console_properties);
512 static const TypeInfo xen_console_type_info = {
513 .name = TYPE_XEN_CONSOLE_DEVICE,
514 .parent = TYPE_XEN_DEVICE,
515 .instance_size = sizeof(XenConsole),
516 .class_init = xen_console_class_init,
519 static void xen_console_register_types(void)
521 type_register_static(&xen_console_type_info);
524 type_init(xen_console_register_types)
526 /* Called to instantiate a XenConsole when the backend is detected. */
527 static void xen_console_device_create(XenBackendInstance *backend,
528 QDict *opts, Error **errp)
530 ERRP_GUARD();
531 XenBus *xenbus = xen_backend_get_bus(backend);
532 const char *name = xen_backend_get_name(backend);
533 unsigned long number;
534 char *fe = NULL, *type = NULL, *output = NULL;
535 char label[32];
536 XenDevice *xendev = NULL;
537 XenConsole *con;
538 Chardev *cd = NULL;
539 struct qemu_xs_handle *xsh = xenbus->xsh;
541 if (qemu_strtoul(name, NULL, 10, &number) || number > INT_MAX) {
542 error_setg(errp, "failed to parse name '%s'", name);
543 goto fail;
546 trace_xen_console_device_create(number);
548 fe = console_frontend_path(xsh, xen_domid, number);
549 if (fe == NULL) {
550 error_setg(errp, "failed to generate frontend path");
551 goto fail;
554 if (xs_node_scanf(xsh, XBT_NULL, fe, "type", errp, "%ms", &type) != 1) {
555 error_prepend(errp, "failed to read console device type: ");
556 goto fail;
559 if (strcmp(type, "ioemu")) {
560 error_setg(errp, "declining to handle console type '%s'",
561 type);
562 goto fail;
565 xendev = XEN_DEVICE(qdev_new(TYPE_XEN_CONSOLE_DEVICE));
566 con = XEN_CONSOLE_DEVICE(xendev);
568 con->dev = number;
570 snprintf(label, sizeof(label), "xencons%ld", number);
572 if (xs_node_scanf(xsh, XBT_NULL, fe, "output", NULL, "%ms", &output) == 1) {
574 * FIXME: sure we want to support implicit
575 * muxed monitors here?
577 cd = qemu_chr_new_mux_mon(label, output, NULL);
578 if (!cd) {
579 error_setg(errp, "console: No valid chardev found at '%s': ",
580 output);
581 goto fail;
583 } else if (number) {
584 cd = serial_hd(number);
585 if (!cd) {
586 error_prepend(errp, "console: No serial device #%ld found: ",
587 number);
588 goto fail;
590 } else {
591 /* No 'output' node on primary console: use null. */
592 cd = qemu_chr_new(label, "null", NULL);
593 if (!cd) {
594 error_setg(errp, "console: failed to create null device");
595 goto fail;
599 if (!qemu_chr_fe_init(&con->chr, cd, errp)) {
600 error_prepend(errp, "console: failed to initialize backing chardev: ");
601 goto fail;
604 if (qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
605 xen_backend_set_device(backend, xendev);
606 goto done;
609 error_prepend(errp, "realization of console device %lu failed: ",
610 number);
612 fail:
613 if (xendev) {
614 object_unparent(OBJECT(xendev));
616 done:
617 g_free(fe);
618 free(type);
619 free(output);
622 static void xen_console_device_destroy(XenBackendInstance *backend,
623 Error **errp)
625 ERRP_GUARD();
626 XenDevice *xendev = xen_backend_get_device(backend);
627 XenConsole *con = XEN_CONSOLE_DEVICE(xendev);
629 trace_xen_console_device_destroy(con->dev);
631 object_unparent(OBJECT(xendev));
634 static const XenBackendInfo xen_console_backend_info = {
635 .type = "console",
636 .create = xen_console_device_create,
637 .destroy = xen_console_device_destroy,
640 static void xen_console_register_backend(void)
642 xen_backend_register(&xen_console_backend_info);
645 xen_backend_init(xen_console_register_backend);