target/s390x: Implement LOAD/STORE TO REAL ADDRESS inline
[qemu/kevin.git] / chardev / char.c
blob7b6b2cb12300042c6adf257e188c14e4df1378bf
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu/cutils.h"
27 #include "monitor/monitor.h"
28 #include "sysemu/sysemu.h"
29 #include "qemu/config-file.h"
30 #include "qemu/error-report.h"
31 #include "qemu/qemu-print.h"
32 #include "chardev/char.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-commands-char.h"
35 #include "qapi/qmp/qerror.h"
36 #include "sysemu/replay.h"
37 #include "qemu/help_option.h"
38 #include "qemu/module.h"
39 #include "qemu/option.h"
41 #include "chardev/char-mux.h"
43 /***********************************************************/
44 /* character device */
46 static Object *get_chardevs_root(void)
48 return container_get(object_get_root(), "/chardevs");
51 static void chr_be_event(Chardev *s, int event)
53 CharBackend *be = s->be;
55 if (!be || !be->chr_event) {
56 return;
59 be->chr_event(be->opaque, event);
62 void qemu_chr_be_event(Chardev *s, int event)
64 /* Keep track if the char device is open */
65 switch (event) {
66 case CHR_EVENT_OPENED:
67 s->be_open = 1;
68 break;
69 case CHR_EVENT_CLOSED:
70 s->be_open = 0;
71 break;
74 CHARDEV_GET_CLASS(s)->chr_be_event(s, event);
77 /* Not reporting errors from writing to logfile, as logs are
78 * defined to be "best effort" only */
79 static void qemu_chr_write_log(Chardev *s, const uint8_t *buf, size_t len)
81 size_t done = 0;
82 ssize_t ret;
84 if (s->logfd < 0) {
85 return;
88 while (done < len) {
89 retry:
90 ret = write(s->logfd, buf + done, len - done);
91 if (ret == -1 && errno == EAGAIN) {
92 g_usleep(100);
93 goto retry;
96 if (ret <= 0) {
97 return;
99 done += ret;
103 static int qemu_chr_write_buffer(Chardev *s,
104 const uint8_t *buf, int len,
105 int *offset, bool write_all)
107 ChardevClass *cc = CHARDEV_GET_CLASS(s);
108 int res = 0;
109 *offset = 0;
111 qemu_mutex_lock(&s->chr_write_lock);
112 while (*offset < len) {
113 retry:
114 res = cc->chr_write(s, buf + *offset, len - *offset);
115 if (res < 0 && errno == EAGAIN && write_all) {
116 g_usleep(100);
117 goto retry;
120 if (res <= 0) {
121 break;
124 *offset += res;
125 if (!write_all) {
126 break;
129 if (*offset > 0) {
130 qemu_chr_write_log(s, buf, *offset);
132 qemu_mutex_unlock(&s->chr_write_lock);
134 return res;
137 int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all)
139 int offset = 0;
140 int res;
142 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
143 replay_char_write_event_load(&res, &offset);
144 assert(offset <= len);
145 qemu_chr_write_buffer(s, buf, offset, &offset, true);
146 return res;
149 res = qemu_chr_write_buffer(s, buf, len, &offset, write_all);
151 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
152 replay_char_write_event_save(res, offset);
155 if (res < 0) {
156 return res;
158 return offset;
161 int qemu_chr_be_can_write(Chardev *s)
163 CharBackend *be = s->be;
165 if (!be || !be->chr_can_read) {
166 return 0;
169 return be->chr_can_read(be->opaque);
172 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len)
174 CharBackend *be = s->be;
176 if (be && be->chr_read) {
177 be->chr_read(be->opaque, buf, len);
181 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len)
183 if (qemu_chr_replay(s)) {
184 if (replay_mode == REPLAY_MODE_PLAY) {
185 return;
187 replay_chr_be_write(s, buf, len);
188 } else {
189 qemu_chr_be_write_impl(s, buf, len);
193 void qemu_chr_be_update_read_handlers(Chardev *s,
194 GMainContext *context)
196 ChardevClass *cc = CHARDEV_GET_CLASS(s);
198 assert(qemu_chr_has_feature(s, QEMU_CHAR_FEATURE_GCONTEXT)
199 || !context);
200 s->gcontext = context;
201 if (cc->chr_update_read_handler) {
202 cc->chr_update_read_handler(s);
206 int qemu_chr_add_client(Chardev *s, int fd)
208 return CHARDEV_GET_CLASS(s)->chr_add_client ?
209 CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1;
212 static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
213 bool *be_opened, Error **errp)
215 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
216 /* Any ChardevCommon member would work */
217 ChardevCommon *common = backend ? backend->u.null.data : NULL;
219 if (common && common->has_logfile) {
220 int flags = O_WRONLY | O_CREAT;
221 if (common->has_logappend &&
222 common->logappend) {
223 flags |= O_APPEND;
224 } else {
225 flags |= O_TRUNC;
227 chr->logfd = qemu_open(common->logfile, flags, 0666);
228 if (chr->logfd < 0) {
229 error_setg_errno(errp, errno,
230 "Unable to open logfile %s",
231 common->logfile);
232 return;
236 if (cc->open) {
237 cc->open(chr, backend, be_opened, errp);
241 static void char_init(Object *obj)
243 Chardev *chr = CHARDEV(obj);
245 chr->logfd = -1;
246 qemu_mutex_init(&chr->chr_write_lock);
249 * Assume if chr_update_read_handler is implemented it will
250 * take the updated gcontext into account.
252 if (CHARDEV_GET_CLASS(chr)->chr_update_read_handler) {
253 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT);
258 static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
260 return len;
263 static void char_class_init(ObjectClass *oc, void *data)
265 ChardevClass *cc = CHARDEV_CLASS(oc);
267 cc->chr_write = null_chr_write;
268 cc->chr_be_event = chr_be_event;
271 static void char_finalize(Object *obj)
273 Chardev *chr = CHARDEV(obj);
275 if (chr->be) {
276 chr->be->chr = NULL;
278 g_free(chr->filename);
279 g_free(chr->label);
280 if (chr->logfd != -1) {
281 close(chr->logfd);
283 qemu_mutex_destroy(&chr->chr_write_lock);
286 static const TypeInfo char_type_info = {
287 .name = TYPE_CHARDEV,
288 .parent = TYPE_OBJECT,
289 .instance_size = sizeof(Chardev),
290 .instance_init = char_init,
291 .instance_finalize = char_finalize,
292 .abstract = true,
293 .class_size = sizeof(ChardevClass),
294 .class_init = char_class_init,
297 static int chardev_machine_done_notify_one(Object *child, void *opaque)
299 Chardev *chr = (Chardev *)child;
300 ChardevClass *class = CHARDEV_GET_CLASS(chr);
302 if (class->chr_machine_done) {
303 return class->chr_machine_done(chr);
306 return 0;
309 static void chardev_machine_done_hook(Notifier *notifier, void *unused)
311 int ret = object_child_foreach(get_chardevs_root(),
312 chardev_machine_done_notify_one, NULL);
314 if (ret) {
315 error_report("Failed to call chardev machine_done hooks");
316 exit(1);
320 static Notifier chardev_machine_done_notify = {
321 .notify = chardev_machine_done_hook,
324 static bool qemu_chr_is_busy(Chardev *s)
326 if (CHARDEV_IS_MUX(s)) {
327 MuxChardev *d = MUX_CHARDEV(s);
328 return d->mux_cnt >= 0;
329 } else {
330 return s->be != NULL;
334 int qemu_chr_wait_connected(Chardev *chr, Error **errp)
336 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
338 if (cc->chr_wait_connected) {
339 return cc->chr_wait_connected(chr, errp);
342 return 0;
345 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename,
346 bool permit_mux_mon)
348 char host[65], port[33], width[8], height[8];
349 int pos;
350 const char *p;
351 QemuOpts *opts;
352 Error *local_err = NULL;
354 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
355 if (local_err) {
356 error_report_err(local_err);
357 return NULL;
360 if (strstart(filename, "mon:", &p)) {
361 if (!permit_mux_mon) {
362 error_report("mon: isn't supported in this context");
363 return NULL;
365 filename = p;
366 qemu_opt_set(opts, "mux", "on", &error_abort);
367 if (strcmp(filename, "stdio") == 0) {
368 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
369 * but pass it to the guest. Handle this only for compat syntax,
370 * for -chardev syntax we have special option for this.
371 * This is what -nographic did, redirecting+muxing serial+monitor
372 * to stdio causing Ctrl+C to be passed to guest. */
373 qemu_opt_set(opts, "signal", "off", &error_abort);
377 if (strcmp(filename, "null") == 0 ||
378 strcmp(filename, "pty") == 0 ||
379 strcmp(filename, "msmouse") == 0 ||
380 strcmp(filename, "wctablet") == 0 ||
381 strcmp(filename, "braille") == 0 ||
382 strcmp(filename, "testdev") == 0 ||
383 strcmp(filename, "stdio") == 0) {
384 qemu_opt_set(opts, "backend", filename, &error_abort);
385 return opts;
387 if (strstart(filename, "vc", &p)) {
388 qemu_opt_set(opts, "backend", "vc", &error_abort);
389 if (*p == ':') {
390 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
391 /* pixels */
392 qemu_opt_set(opts, "width", width, &error_abort);
393 qemu_opt_set(opts, "height", height, &error_abort);
394 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
395 /* chars */
396 qemu_opt_set(opts, "cols", width, &error_abort);
397 qemu_opt_set(opts, "rows", height, &error_abort);
398 } else {
399 goto fail;
402 return opts;
404 if (strcmp(filename, "con:") == 0) {
405 qemu_opt_set(opts, "backend", "console", &error_abort);
406 return opts;
408 if (strstart(filename, "COM", NULL)) {
409 qemu_opt_set(opts, "backend", "serial", &error_abort);
410 qemu_opt_set(opts, "path", filename, &error_abort);
411 return opts;
413 if (strstart(filename, "file:", &p)) {
414 qemu_opt_set(opts, "backend", "file", &error_abort);
415 qemu_opt_set(opts, "path", p, &error_abort);
416 return opts;
418 if (strstart(filename, "pipe:", &p)) {
419 qemu_opt_set(opts, "backend", "pipe", &error_abort);
420 qemu_opt_set(opts, "path", p, &error_abort);
421 return opts;
423 if (strstart(filename, "tcp:", &p) ||
424 strstart(filename, "telnet:", &p) ||
425 strstart(filename, "tn3270:", &p) ||
426 strstart(filename, "websocket:", &p)) {
427 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
428 host[0] = 0;
429 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
430 goto fail;
432 qemu_opt_set(opts, "backend", "socket", &error_abort);
433 qemu_opt_set(opts, "host", host, &error_abort);
434 qemu_opt_set(opts, "port", port, &error_abort);
435 if (p[pos] == ',') {
436 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
437 if (local_err) {
438 error_report_err(local_err);
439 goto fail;
442 if (strstart(filename, "telnet:", &p)) {
443 qemu_opt_set(opts, "telnet", "on", &error_abort);
444 } else if (strstart(filename, "tn3270:", &p)) {
445 qemu_opt_set(opts, "tn3270", "on", &error_abort);
446 } else if (strstart(filename, "websocket:", &p)) {
447 qemu_opt_set(opts, "websocket", "on", &error_abort);
449 return opts;
451 if (strstart(filename, "udp:", &p)) {
452 qemu_opt_set(opts, "backend", "udp", &error_abort);
453 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
454 host[0] = 0;
455 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
456 goto fail;
459 qemu_opt_set(opts, "host", host, &error_abort);
460 qemu_opt_set(opts, "port", port, &error_abort);
461 if (p[pos] == '@') {
462 p += pos + 1;
463 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
464 host[0] = 0;
465 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
466 goto fail;
469 qemu_opt_set(opts, "localaddr", host, &error_abort);
470 qemu_opt_set(opts, "localport", port, &error_abort);
472 return opts;
474 if (strstart(filename, "unix:", &p)) {
475 qemu_opt_set(opts, "backend", "socket", &error_abort);
476 qemu_opts_do_parse(opts, p, "path", &local_err);
477 if (local_err) {
478 error_report_err(local_err);
479 goto fail;
481 return opts;
483 if (strstart(filename, "/dev/parport", NULL) ||
484 strstart(filename, "/dev/ppi", NULL)) {
485 qemu_opt_set(opts, "backend", "parallel", &error_abort);
486 qemu_opt_set(opts, "path", filename, &error_abort);
487 return opts;
489 if (strstart(filename, "/dev/", NULL)) {
490 qemu_opt_set(opts, "backend", "serial", &error_abort);
491 qemu_opt_set(opts, "path", filename, &error_abort);
492 return opts;
495 error_report("'%s' is not a valid char driver", filename);
497 fail:
498 qemu_opts_del(opts);
499 return NULL;
502 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend)
504 const char *logfile = qemu_opt_get(opts, "logfile");
506 backend->has_logfile = logfile != NULL;
507 backend->logfile = g_strdup(logfile);
509 backend->has_logappend = true;
510 backend->logappend = qemu_opt_get_bool(opts, "logappend", false);
513 static const ChardevClass *char_get_class(const char *driver, Error **errp)
515 ObjectClass *oc;
516 const ChardevClass *cc;
517 char *typename = g_strdup_printf("chardev-%s", driver);
519 oc = object_class_by_name(typename);
520 g_free(typename);
522 if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) {
523 error_setg(errp, "'%s' is not a valid char driver name", driver);
524 return NULL;
527 if (object_class_is_abstract(oc)) {
528 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
529 "abstract device type");
530 return NULL;
533 cc = CHARDEV_CLASS(oc);
534 if (cc->internal) {
535 error_setg(errp, "'%s' is not a valid char driver name", driver);
536 return NULL;
539 return cc;
542 static const struct ChardevAlias {
543 const char *typename;
544 const char *alias;
545 } chardev_alias_table[] = {
546 #ifdef HAVE_CHARDEV_PARPORT
547 { "parallel", "parport" },
548 #endif
549 #ifdef HAVE_CHARDEV_SERIAL
550 { "serial", "tty" },
551 #endif
554 typedef struct ChadevClassFE {
555 void (*fn)(const char *name, void *opaque);
556 void *opaque;
557 } ChadevClassFE;
559 static void
560 chardev_class_foreach(ObjectClass *klass, void *opaque)
562 ChadevClassFE *fe = opaque;
564 assert(g_str_has_prefix(object_class_get_name(klass), "chardev-"));
565 if (CHARDEV_CLASS(klass)->internal) {
566 return;
569 fe->fn(object_class_get_name(klass) + 8, fe->opaque);
572 static void
573 chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque)
575 ChadevClassFE fe = { .fn = fn, .opaque = opaque };
576 int i;
578 object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe);
580 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) {
581 fn(chardev_alias_table[i].alias, opaque);
585 static void
586 help_string_append(const char *name, void *opaque)
588 GString *str = opaque;
590 g_string_append_printf(str, "\n %s", name);
593 static const char *chardev_alias_translate(const char *name)
595 int i;
596 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) {
597 if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) {
598 return chardev_alias_table[i].typename;
601 return name;
604 ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp)
606 Error *local_err = NULL;
607 const ChardevClass *cc;
608 ChardevBackend *backend = NULL;
609 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend"));
611 if (name == NULL) {
612 error_setg(errp, "chardev: \"%s\" missing backend",
613 qemu_opts_id(opts));
614 return NULL;
617 cc = char_get_class(name, errp);
618 if (cc == NULL) {
619 return NULL;
622 backend = g_new0(ChardevBackend, 1);
623 backend->type = CHARDEV_BACKEND_KIND_NULL;
625 if (cc->parse) {
626 cc->parse(opts, backend, &local_err);
627 if (local_err) {
628 error_propagate(errp, local_err);
629 qapi_free_ChardevBackend(backend);
630 return NULL;
632 } else {
633 ChardevCommon *ccom = g_new0(ChardevCommon, 1);
634 qemu_chr_parse_common(opts, ccom);
635 backend->u.null.data = ccom; /* Any ChardevCommon member would work */
638 return backend;
641 Chardev *qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
642 Error **errp)
644 const ChardevClass *cc;
645 Chardev *chr = NULL;
646 ChardevBackend *backend = NULL;
647 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend"));
648 const char *id = qemu_opts_id(opts);
649 char *bid = NULL;
651 if (name && is_help_option(name)) {
652 GString *str = g_string_new("");
654 chardev_name_foreach(help_string_append, str);
656 qemu_printf("Available chardev backend types: %s\n", str->str);
657 g_string_free(str, true);
658 return NULL;
661 if (id == NULL) {
662 error_setg(errp, "chardev: no id specified");
663 return NULL;
666 backend = qemu_chr_parse_opts(opts, errp);
667 if (backend == NULL) {
668 return NULL;
671 cc = char_get_class(name, errp);
672 if (cc == NULL) {
673 goto out;
676 if (qemu_opt_get_bool(opts, "mux", 0)) {
677 bid = g_strdup_printf("%s-base", id);
680 chr = qemu_chardev_new(bid ? bid : id,
681 object_class_get_name(OBJECT_CLASS(cc)),
682 backend, context, errp);
684 if (chr == NULL) {
685 goto out;
688 if (bid) {
689 Chardev *mux;
690 qapi_free_ChardevBackend(backend);
691 backend = g_new0(ChardevBackend, 1);
692 backend->type = CHARDEV_BACKEND_KIND_MUX;
693 backend->u.mux.data = g_new0(ChardevMux, 1);
694 backend->u.mux.data->chardev = g_strdup(bid);
695 mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, context, errp);
696 if (mux == NULL) {
697 object_unparent(OBJECT(chr));
698 chr = NULL;
699 goto out;
701 chr = mux;
704 out:
705 qapi_free_ChardevBackend(backend);
706 g_free(bid);
707 return chr;
710 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
711 bool permit_mux_mon, GMainContext *context)
713 const char *p;
714 Chardev *chr;
715 QemuOpts *opts;
716 Error *err = NULL;
718 if (strstart(filename, "chardev:", &p)) {
719 return qemu_chr_find(p);
722 opts = qemu_chr_parse_compat(label, filename, permit_mux_mon);
723 if (!opts)
724 return NULL;
726 chr = qemu_chr_new_from_opts(opts, context, &err);
727 if (!chr) {
728 error_report_err(err);
729 goto out;
732 if (qemu_opt_get_bool(opts, "mux", 0)) {
733 assert(permit_mux_mon);
734 monitor_init_hmp(chr, true);
737 out:
738 qemu_opts_del(opts);
739 return chr;
742 static Chardev *qemu_chr_new_permit_mux_mon(const char *label,
743 const char *filename,
744 bool permit_mux_mon,
745 GMainContext *context)
747 Chardev *chr;
748 chr = qemu_chr_new_noreplay(label, filename, permit_mux_mon, context);
749 if (chr) {
750 if (replay_mode != REPLAY_MODE_NONE) {
751 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
753 if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) {
754 error_report("Replay: ioctl is not supported "
755 "for serial devices yet");
757 replay_register_char_driver(chr);
759 return chr;
762 Chardev *qemu_chr_new(const char *label, const char *filename,
763 GMainContext *context)
765 return qemu_chr_new_permit_mux_mon(label, filename, false, context);
768 Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename,
769 GMainContext *context)
771 return qemu_chr_new_permit_mux_mon(label, filename, true, context);
774 static int qmp_query_chardev_foreach(Object *obj, void *data)
776 Chardev *chr = CHARDEV(obj);
777 ChardevInfoList **list = data;
778 ChardevInfoList *info = g_malloc0(sizeof(*info));
780 info->value = g_malloc0(sizeof(*info->value));
781 info->value->label = g_strdup(chr->label);
782 info->value->filename = g_strdup(chr->filename);
783 info->value->frontend_open = chr->be && chr->be->fe_open;
785 info->next = *list;
786 *list = info;
788 return 0;
791 ChardevInfoList *qmp_query_chardev(Error **errp)
793 ChardevInfoList *chr_list = NULL;
795 object_child_foreach(get_chardevs_root(),
796 qmp_query_chardev_foreach, &chr_list);
798 return chr_list;
801 static void
802 qmp_prepend_backend(const char *name, void *opaque)
804 ChardevBackendInfoList **list = opaque;
805 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
807 info->value = g_malloc0(sizeof(*info->value));
808 info->value->name = g_strdup(name);
809 info->next = *list;
810 *list = info;
813 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
815 ChardevBackendInfoList *backend_list = NULL;
817 chardev_name_foreach(qmp_prepend_backend, &backend_list);
819 return backend_list;
822 Chardev *qemu_chr_find(const char *name)
824 Object *obj = object_resolve_path_component(get_chardevs_root(), name);
826 return obj ? CHARDEV(obj) : NULL;
829 QemuOptsList qemu_chardev_opts = {
830 .name = "chardev",
831 .implied_opt_name = "backend",
832 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
833 .desc = {
835 .name = "backend",
836 .type = QEMU_OPT_STRING,
838 .name = "path",
839 .type = QEMU_OPT_STRING,
841 .name = "host",
842 .type = QEMU_OPT_STRING,
844 .name = "port",
845 .type = QEMU_OPT_STRING,
847 .name = "fd",
848 .type = QEMU_OPT_STRING,
850 .name = "localaddr",
851 .type = QEMU_OPT_STRING,
853 .name = "localport",
854 .type = QEMU_OPT_STRING,
856 .name = "to",
857 .type = QEMU_OPT_NUMBER,
859 .name = "ipv4",
860 .type = QEMU_OPT_BOOL,
862 .name = "ipv6",
863 .type = QEMU_OPT_BOOL,
865 .name = "wait",
866 .type = QEMU_OPT_BOOL,
868 .name = "server",
869 .type = QEMU_OPT_BOOL,
871 .name = "delay",
872 .type = QEMU_OPT_BOOL,
874 .name = "reconnect",
875 .type = QEMU_OPT_NUMBER,
877 .name = "telnet",
878 .type = QEMU_OPT_BOOL,
880 .name = "tn3270",
881 .type = QEMU_OPT_BOOL,
883 .name = "tls-creds",
884 .type = QEMU_OPT_STRING,
886 .name = "tls-authz",
887 .type = QEMU_OPT_STRING,
889 .name = "websocket",
890 .type = QEMU_OPT_BOOL,
892 .name = "width",
893 .type = QEMU_OPT_NUMBER,
895 .name = "height",
896 .type = QEMU_OPT_NUMBER,
898 .name = "cols",
899 .type = QEMU_OPT_NUMBER,
901 .name = "rows",
902 .type = QEMU_OPT_NUMBER,
904 .name = "mux",
905 .type = QEMU_OPT_BOOL,
907 .name = "signal",
908 .type = QEMU_OPT_BOOL,
910 .name = "name",
911 .type = QEMU_OPT_STRING,
913 .name = "debug",
914 .type = QEMU_OPT_NUMBER,
916 .name = "size",
917 .type = QEMU_OPT_SIZE,
919 .name = "chardev",
920 .type = QEMU_OPT_STRING,
922 .name = "append",
923 .type = QEMU_OPT_BOOL,
925 .name = "logfile",
926 .type = QEMU_OPT_STRING,
928 .name = "logappend",
929 .type = QEMU_OPT_BOOL,
931 { /* end of list */ }
935 bool qemu_chr_has_feature(Chardev *chr,
936 ChardevFeature feature)
938 return test_bit(feature, chr->features);
941 void qemu_chr_set_feature(Chardev *chr,
942 ChardevFeature feature)
944 return set_bit(feature, chr->features);
947 Chardev *qemu_chardev_new(const char *id, const char *typename,
948 ChardevBackend *backend,
949 GMainContext *gcontext,
950 Error **errp)
952 Object *obj;
953 Chardev *chr = NULL;
954 Error *local_err = NULL;
955 bool be_opened = true;
957 assert(g_str_has_prefix(typename, "chardev-"));
959 obj = object_new(typename);
960 chr = CHARDEV(obj);
961 chr->label = g_strdup(id);
962 chr->gcontext = gcontext;
964 qemu_char_open(chr, backend, &be_opened, &local_err);
965 if (local_err) {
966 goto end;
969 if (!chr->filename) {
970 chr->filename = g_strdup(typename + 8);
972 if (be_opened) {
973 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
976 if (id) {
977 object_property_add_child(get_chardevs_root(), id, obj, &local_err);
978 if (local_err) {
979 goto end;
981 object_unref(obj);
984 end:
985 if (local_err) {
986 error_propagate(errp, local_err);
987 object_unref(obj);
988 return NULL;
991 return chr;
994 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
995 Error **errp)
997 const ChardevClass *cc;
998 ChardevReturn *ret;
999 Chardev *chr;
1001 cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
1002 if (!cc) {
1003 return NULL;
1006 chr = qemu_chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
1007 backend, NULL, errp);
1008 if (!chr) {
1009 return NULL;
1012 ret = g_new0(ChardevReturn, 1);
1013 if (CHARDEV_IS_PTY(chr)) {
1014 ret->pty = g_strdup(chr->filename + 4);
1015 ret->has_pty = true;
1018 return ret;
1021 ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
1022 Error **errp)
1024 CharBackend *be;
1025 const ChardevClass *cc;
1026 Chardev *chr, *chr_new;
1027 bool closed_sent = false;
1028 ChardevReturn *ret;
1030 chr = qemu_chr_find(id);
1031 if (!chr) {
1032 error_setg(errp, "Chardev '%s' does not exist", id);
1033 return NULL;
1036 if (CHARDEV_IS_MUX(chr)) {
1037 error_setg(errp, "Mux device hotswap not supported yet");
1038 return NULL;
1041 if (qemu_chr_replay(chr)) {
1042 error_setg(errp,
1043 "Chardev '%s' cannot be changed in record/replay mode", id);
1044 return NULL;
1047 be = chr->be;
1048 if (!be) {
1049 /* easy case */
1050 object_unparent(OBJECT(chr));
1051 return qmp_chardev_add(id, backend, errp);
1054 if (!be->chr_be_change) {
1055 error_setg(errp, "Chardev user does not support chardev hotswap");
1056 return NULL;
1059 cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
1060 if (!cc) {
1061 return NULL;
1064 chr_new = qemu_chardev_new(NULL, object_class_get_name(OBJECT_CLASS(cc)),
1065 backend, chr->gcontext, errp);
1066 if (!chr_new) {
1067 return NULL;
1069 chr_new->label = g_strdup(id);
1071 if (chr->be_open && !chr_new->be_open) {
1072 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1073 closed_sent = true;
1076 chr->be = NULL;
1077 qemu_chr_fe_init(be, chr_new, &error_abort);
1079 if (be->chr_be_change(be->opaque) < 0) {
1080 error_setg(errp, "Chardev '%s' change failed", chr_new->label);
1081 chr_new->be = NULL;
1082 qemu_chr_fe_init(be, chr, &error_abort);
1083 if (closed_sent) {
1084 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
1086 object_unref(OBJECT(chr_new));
1087 return NULL;
1090 object_unparent(OBJECT(chr));
1091 object_property_add_child(get_chardevs_root(), chr_new->label,
1092 OBJECT(chr_new), &error_abort);
1093 object_unref(OBJECT(chr_new));
1095 ret = g_new0(ChardevReturn, 1);
1096 if (CHARDEV_IS_PTY(chr_new)) {
1097 ret->pty = g_strdup(chr_new->filename + 4);
1098 ret->has_pty = true;
1101 return ret;
1104 void qmp_chardev_remove(const char *id, Error **errp)
1106 Chardev *chr;
1108 chr = qemu_chr_find(id);
1109 if (chr == NULL) {
1110 error_setg(errp, "Chardev '%s' not found", id);
1111 return;
1113 if (qemu_chr_is_busy(chr)) {
1114 error_setg(errp, "Chardev '%s' is busy", id);
1115 return;
1117 if (qemu_chr_replay(chr)) {
1118 error_setg(errp,
1119 "Chardev '%s' cannot be unplugged in record/replay mode", id);
1120 return;
1122 object_unparent(OBJECT(chr));
1125 void qmp_chardev_send_break(const char *id, Error **errp)
1127 Chardev *chr;
1129 chr = qemu_chr_find(id);
1130 if (chr == NULL) {
1131 error_setg(errp, "Chardev '%s' not found", id);
1132 return;
1134 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
1138 * Add a timeout callback for the chardev (in milliseconds), return
1139 * the GSource object created. Please use this to add timeout hook for
1140 * chardev instead of g_timeout_add() and g_timeout_add_seconds(), to
1141 * make sure the gcontext that the task bound to is correct.
1143 GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms,
1144 GSourceFunc func, void *private)
1146 GSource *source = g_timeout_source_new(ms);
1148 assert(func);
1149 g_source_set_callback(source, func, private, NULL);
1150 g_source_attach(source, chr->gcontext);
1152 return source;
1155 void qemu_chr_cleanup(void)
1157 object_unparent(get_chardevs_root());
1160 static void register_types(void)
1162 type_register_static(&char_type_info);
1164 /* this must be done after machine init, since we register FEs with muxes
1165 * as part of realize functions like serial_isa_realizefn when -nographic
1166 * is specified
1168 qemu_add_machine_init_done_notifier(&chardev_machine_done_notify);
1171 type_init(register_types);