1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/module.h"
28 #include "qemu/option.h"
29 #include "qemu/bitops.h"
30 #include "chardev/char.h"
31 #include "sysemu/block-backend.h"
32 #include "qapi/qapi-commands-control.h"
33 #include "chardev-internal.h"
34
35 /* MUX driver for serial I/O splitting */
36
37 /*
38 * Set to false by suspend_mux_open. Open events are delayed until
39 * resume_mux_open. Usually suspend_mux_open is called before
40 * command line processing and resume_mux_open afterwards.
41 */
42 static bool muxes_opened = true;
43
44 /* Called with chr_write_lock held. */
mux_chr_write(Chardev * chr,const uint8_t * buf,int len)45 static int mux_chr_write(Chardev *chr, const uint8_t *buf, int len)
46 {
47 MuxChardev *d = MUX_CHARDEV(chr);
48 int ret;
49 if (!d->timestamps) {
50 ret = qemu_chr_fe_write(&d->chr, buf, len);
51 } else {
52 int i;
53
54 ret = 0;
55 for (i = 0; i < len; i++) {
56 if (d->linestart) {
57 char buf1[64];
58 int64_t ti;
59 int secs;
60
61 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
62 if (d->timestamps_start == -1) {
63 d->timestamps_start = ti;
64 }
65 ti -= d->timestamps_start;
66 secs = ti / 1000;
67 snprintf(buf1, sizeof(buf1),
68 "[%02d:%02d:%02d.%03d] ",
69 secs / 3600,
70 (secs / 60) % 60,
71 secs % 60,
72 (int)(ti % 1000));
73 /* XXX this blocks entire thread. Rewrite to use
74 * qemu_chr_fe_write and background I/O callbacks */
75 qemu_chr_fe_write_all(&d->chr,
76 (uint8_t *)buf1, strlen(buf1));
77 d->linestart = false;
78 }
79 ret += qemu_chr_fe_write(&d->chr, buf + i, 1);
80 if (buf[i] == '\n') {
81 d->linestart = true;
82 }
83 }
84 }
85 return ret;
86 }
87
88 static const char * const mux_help[] = {
89 "% h print this help\n\r",
90 "% x exit emulator\n\r",
91 "% s save disk data back to file (if -snapshot)\n\r",
92 "% t toggle console timestamps\n\r",
93 "% b send break (magic sysrq)\n\r",
94 "% c switch between console and monitor\n\r",
95 "% % sends %\n\r",
96 NULL
97 };
98
99 int term_escape_char = 0x01; /* ctrl-a is used for escape */
mux_print_help(Chardev * chr)100 static void mux_print_help(Chardev *chr)
101 {
102 int i, j;
103 char ebuf[15] = "Escape-Char";
104 char cbuf[50] = "\n\r";
105
106 if (term_escape_char > 0 && term_escape_char < 26) {
107 snprintf(cbuf, sizeof(cbuf), "\n\r");
108 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
109 } else {
110 snprintf(cbuf, sizeof(cbuf),
111 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
112 term_escape_char);
113 }
114 /* XXX this blocks entire thread. Rewrite to use
115 * qemu_chr_fe_write and background I/O callbacks */
116 qemu_chr_write_all(chr, (uint8_t *)cbuf, strlen(cbuf));
117 for (i = 0; mux_help[i] != NULL; i++) {
118 for (j = 0; mux_help[i][j] != '\0'; j++) {
119 if (mux_help[i][j] == '%') {
120 qemu_chr_write_all(chr, (uint8_t *)ebuf, strlen(ebuf));
121 } else {
122 qemu_chr_write_all(chr, (uint8_t *)&mux_help[i][j], 1);
123 }
124 }
125 }
126 }
127
mux_chr_send_event(MuxChardev * d,unsigned int mux_nr,QEMUChrEvent event)128 static void mux_chr_send_event(MuxChardev *d, unsigned int mux_nr,
129 QEMUChrEvent event)
130 {
131 CharBackend *be = d->backends[mux_nr];
132
133 if (be && be->chr_event) {
134 be->chr_event(be->opaque, event);
135 }
136 }
137
mux_chr_be_event(Chardev * chr,QEMUChrEvent event)138 static void mux_chr_be_event(Chardev *chr, QEMUChrEvent event)
139 {
140 MuxChardev *d = MUX_CHARDEV(chr);
141
142 if (d->focus != -1) {
143 mux_chr_send_event(d, d->focus, event);
144 }
145 }
146
mux_proc_byte(Chardev * chr,MuxChardev * d,int ch)147 static int mux_proc_byte(Chardev *chr, MuxChardev *d, int ch)
148 {
149 if (d->term_got_escape) {
150 d->term_got_escape = false;
151 if (ch == term_escape_char) {
152 goto send_char;
153 }
154 switch (ch) {
155 case '?':
156 case 'h':
157 mux_print_help(chr);
158 break;
159 case 'x':
160 {
161 const char *term = "QEMU: Terminated\n\r";
162 qemu_chr_write_all(chr, (uint8_t *)term, strlen(term));
163 qmp_quit(NULL);
164 break;
165 }
166 case 's':
167 blk_commit_all();
168 break;
169 case 'b':
170 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
171 break;
172 case 'c': {
173 unsigned int bit;
174
175 /* Handler registered with first fe */
176 assert(d->mux_bitset != 0);
177 /* Switch to the next registered device */
178 bit = find_next_bit(&d->mux_bitset, MAX_MUX, d->focus + 1);
179 if (bit >= MAX_MUX) {
180 bit = find_next_bit(&d->mux_bitset, MAX_MUX, 0);
181 }
182 mux_set_focus(chr, bit);
183 break;
184 } case 't':
185 d->timestamps = !d->timestamps;
186 d->timestamps_start = -1;
187 d->linestart = false;
188 break;
189 }
190 } else if (ch == term_escape_char) {
191 d->term_got_escape = true;
192 } else {
193 send_char:
194 return 1;
195 }
196 return 0;
197 }
198
mux_chr_accept_input(Chardev * chr)199 static void mux_chr_accept_input(Chardev *chr)
200 {
201 MuxChardev *d = MUX_CHARDEV(chr);
202 int m = d->focus;
203 CharBackend *be = d->backends[m];
204
205 while (be && d->prod[m] != d->cons[m] &&
206 be->chr_can_read && be->chr_can_read(be->opaque)) {
207 be->chr_read(be->opaque,
208 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
209 }
210 }
211
mux_chr_can_read(void * opaque)212 static int mux_chr_can_read(void *opaque)
213 {
214 MuxChardev *d = MUX_CHARDEV(opaque);
215 int m = d->focus;
216 CharBackend *be = d->backends[m];
217
218 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) {
219 return 1;
220 }
221
222 if (be && be->chr_can_read) {
223 return be->chr_can_read(be->opaque);
224 }
225
226 return 0;
227 }
228
mux_chr_read(void * opaque,const uint8_t * buf,int size)229 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
230 {
231 Chardev *chr = CHARDEV(opaque);
232 MuxChardev *d = MUX_CHARDEV(opaque);
233 int m = d->focus;
234 CharBackend *be = d->backends[m];
235 int i;
236
237 mux_chr_accept_input(opaque);
238
239 for (i = 0; i < size; i++)
240 if (mux_proc_byte(chr, d, buf[i])) {
241 if (d->prod[m] == d->cons[m] &&
242 be && be->chr_can_read &&
243 be->chr_can_read(be->opaque)) {
244 be->chr_read(be->opaque, &buf[i], 1);
245 } else {
246 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
247 }
248 }
249 }
250
mux_chr_send_all_event(Chardev * chr,QEMUChrEvent event)251 void mux_chr_send_all_event(Chardev *chr, QEMUChrEvent event)
252 {
253 MuxChardev *d = MUX_CHARDEV(chr);
254 int bit;
255
256 if (!muxes_opened) {
257 return;
258 }
259
260 /* Send the event to all registered listeners */
261 bit = -1;
262 while ((bit = find_next_bit(&d->mux_bitset, MAX_MUX, bit + 1)) < MAX_MUX) {
263 mux_chr_send_event(d, bit, event);
264 }
265 }
266
mux_chr_event(void * opaque,QEMUChrEvent event)267 static void mux_chr_event(void *opaque, QEMUChrEvent event)
268 {
269 mux_chr_send_all_event(CHARDEV(opaque), event);
270 }
271
mux_chr_add_watch(Chardev * s,GIOCondition cond)272 static GSource *mux_chr_add_watch(Chardev *s, GIOCondition cond)
273 {
274 MuxChardev *d = MUX_CHARDEV(s);
275 Chardev *chr = qemu_chr_fe_get_driver(&d->chr);
276 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
277
278 if (!cc->chr_add_watch) {
279 return NULL;
280 }
281
282 return cc->chr_add_watch(chr, cond);
283 }
284
char_mux_finalize(Object * obj)285 static void char_mux_finalize(Object *obj)
286 {
287 MuxChardev *d = MUX_CHARDEV(obj);
288 int bit;
289
290 bit = -1;
291 while ((bit = find_next_bit(&d->mux_bitset, MAX_MUX, bit + 1)) < MAX_MUX) {
292 CharBackend *be = d->backends[bit];
293 be->chr = NULL;
294 d->backends[bit] = NULL;
295 }
296 d->mux_bitset = 0;
297 qemu_chr_fe_deinit(&d->chr, false);
298 }
299
mux_chr_update_read_handlers(Chardev * chr)300 static void mux_chr_update_read_handlers(Chardev *chr)
301 {
302 MuxChardev *d = MUX_CHARDEV(chr);
303
304 /* Fix up the real driver with mux routines */
305 qemu_chr_fe_set_handlers_full(&d->chr,
306 mux_chr_can_read,
307 mux_chr_read,
308 mux_chr_event,
309 NULL,
310 chr,
311 chr->gcontext, true, false);
312 }
313
mux_chr_attach_frontend(MuxChardev * d,CharBackend * b,unsigned int * tag,Error ** errp)314 bool mux_chr_attach_frontend(MuxChardev *d, CharBackend *b,
315 unsigned int *tag, Error **errp)
316 {
317 unsigned int bit;
318
319 QEMU_BUILD_BUG_ON(MAX_MUX > (sizeof(d->mux_bitset) * BITS_PER_BYTE));
320
321 bit = find_next_zero_bit(&d->mux_bitset, MAX_MUX, 0);
322 if (bit >= MAX_MUX) {
323 error_setg(errp,
324 "too many uses of multiplexed chardev '%s'"
325 " (maximum is " stringify(MAX_MUX) ")",
326 d->parent.label);
327 return false;
328 }
329
330 d->mux_bitset |= (1ul << bit);
331 d->backends[bit] = b;
332 *tag = bit;
333
334 return true;
335 }
336
mux_chr_detach_frontend(MuxChardev * d,unsigned int tag)337 bool mux_chr_detach_frontend(MuxChardev *d, unsigned int tag)
338 {
339 if (!(d->mux_bitset & (1ul << tag))) {
340 return false;
341 }
342
343 d->mux_bitset &= ~(1ul << tag);
344 d->backends[tag] = NULL;
345
346 return true;
347 }
348
mux_set_focus(Chardev * chr,unsigned int focus)349 void mux_set_focus(Chardev *chr, unsigned int focus)
350 {
351 MuxChardev *d = MUX_CHARDEV(chr);
352
353 assert(d->mux_bitset & (1ul << focus));
354
355 if (d->focus != -1) {
356 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
357 }
358
359 d->focus = focus;
360 chr->be = d->backends[focus];
361 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
362 }
363
qemu_chr_open_mux(Chardev * chr,ChardevBackend * backend,bool * be_opened,Error ** errp)364 static void qemu_chr_open_mux(Chardev *chr,
365 ChardevBackend *backend,
366 bool *be_opened,
367 Error **errp)
368 {
369 ChardevMux *mux = backend->u.mux.data;
370 Chardev *drv;
371 MuxChardev *d = MUX_CHARDEV(chr);
372
373 drv = qemu_chr_find(mux->chardev);
374 if (drv == NULL) {
375 error_setg(errp, "mux: base chardev %s not found", mux->chardev);
376 return;
377 }
378
379 d->focus = -1;
380 /* only default to opened state if we've realized the initial
381 * set of muxes
382 */
383 *be_opened = muxes_opened;
384 qemu_chr_fe_init(&d->chr, drv, errp);
385 }
386
qemu_chr_parse_mux(QemuOpts * opts,ChardevBackend * backend,Error ** errp)387 static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
388 Error **errp)
389 {
390 const char *chardev = qemu_opt_get(opts, "chardev");
391 ChardevMux *mux;
392
393 if (chardev == NULL) {
394 error_setg(errp, "chardev: mux: no chardev given");
395 return;
396 }
397 backend->type = CHARDEV_BACKEND_KIND_MUX;
398 mux = backend->u.mux.data = g_new0(ChardevMux, 1);
399 qemu_chr_parse_common(opts, qapi_ChardevMux_base(mux));
400 mux->chardev = g_strdup(chardev);
401 }
402
403 /**
404 * Called after processing of default and command-line-specified
405 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
406 * to a mux chardev. This is done here to ensure that
407 * output/prompts/banners are only displayed for the FE that has
408 * focus when initial command-line processing/machine init is
409 * completed.
410 *
411 * After this point, any new FE attached to any new or existing
412 * mux will receive CHR_EVENT_OPENED notifications for the BE
413 * immediately.
414 */
open_muxes(Chardev * chr)415 static void open_muxes(Chardev *chr)
416 {
417 /* send OPENED to all already-attached FEs */
418 mux_chr_send_all_event(chr, CHR_EVENT_OPENED);
419
420 /*
421 * mark mux as OPENED so any new FEs will immediately receive
422 * OPENED event
423 */
424 chr->be_open = 1;
425 }
426
suspend_mux_open(void)427 void suspend_mux_open(void)
428 {
429 muxes_opened = false;
430 }
431
chardev_options_parsed_cb(Object * child,void * opaque)432 static int chardev_options_parsed_cb(Object *child, void *opaque)
433 {
434 Chardev *chr = (Chardev *)child;
435
436 if (!chr->be_open && CHARDEV_IS_MUX(chr)) {
437 open_muxes(chr);
438 }
439
440 return 0;
441 }
442
resume_mux_open(void)443 void resume_mux_open(void)
444 {
445 muxes_opened = true;
446 object_child_foreach(get_chardevs_root(),
447 chardev_options_parsed_cb, NULL);
448 }
449
char_mux_class_init(ObjectClass * oc,void * data)450 static void char_mux_class_init(ObjectClass *oc, void *data)
451 {
452 ChardevClass *cc = CHARDEV_CLASS(oc);
453
454 cc->parse = qemu_chr_parse_mux;
455 cc->open = qemu_chr_open_mux;
456 cc->chr_write = mux_chr_write;
457 cc->chr_accept_input = mux_chr_accept_input;
458 cc->chr_add_watch = mux_chr_add_watch;
459 cc->chr_be_event = mux_chr_be_event;
460 cc->chr_update_read_handler = mux_chr_update_read_handlers;
461 }
462
463 static const TypeInfo char_mux_type_info = {
464 .name = TYPE_CHARDEV_MUX,
465 .parent = TYPE_CHARDEV,
466 .class_init = char_mux_class_init,
467 .instance_size = sizeof(MuxChardev),
468 .instance_finalize = char_mux_finalize,
469 };
470
register_types(void)471 static void register_types(void)
472 {
473 type_register_static(&char_mux_type_info);
474 }
475
476 type_init(register_types);
477