master
c 480 lines 13.3 KB
Raw
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 "system/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. */
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 */
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
128 static void mux_chr_send_event(MuxChardev *d, unsigned int mux_nr,
129 QEMUChrEvent event)
130 {
131 CharFrontend *fe = d->frontends[mux_nr];
132
133 if (fe && fe->chr_event) {
134 fe->chr_event(fe->opaque, event);
135 }
136 }
137
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
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
199 static void mux_chr_accept_input(Chardev *chr)
200 {
201 MuxChardev *d = MUX_CHARDEV(chr);
202 int m = d->focus;
203 CharFrontend *fe = d->frontends[m];
204
205 while (fe && d->prod[m] != d->cons[m] &&
206 fe->chr_can_read && fe->chr_can_read(fe->opaque)) {
207 fe->chr_read(fe->opaque,
208 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
209 }
210 }
211
212 static int mux_chr_can_read(void *opaque)
213 {
214 MuxChardev *d = MUX_CHARDEV(opaque);
215 int m = d->focus;
216 CharFrontend *fe = d->frontends[m];
217
218 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) {
219 return 1;
220 }
221
222 if (fe && fe->chr_can_read) {
223 return fe->chr_can_read(fe->opaque);
224 }
225
226 return 0;
227 }
228
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 CharFrontend *fe = d->frontends[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 fe && fe->chr_can_read &&
243 fe->chr_can_read(fe->opaque)) {
244 fe->chr_read(fe->opaque, &buf[i], 1);
245 } else {
246 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
247 }
248 }
249 }
250
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
267 static void mux_chr_event(void *opaque, QEMUChrEvent event)
268 {
269 mux_chr_send_all_event(CHARDEV(opaque), event);
270 }
271
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
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 CharFrontend *be = d->frontends[bit];
293 be->chr = NULL;
294 d->frontends[bit] = NULL;
295 }
296 d->mux_bitset = 0;
297 qemu_chr_fe_deinit(&d->chr, false);
298 }
299
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
314 bool mux_chr_attach_frontend(MuxChardev *d, CharFrontend *c,
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->frontends[bit] = c;
332 *tag = bit;
333
334 return true;
335 }
336
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->frontends[tag] = NULL;
345
346 return true;
347 }
348
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->fe = d->frontends[focus];
361 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
362 }
363
364 static bool mux_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp)
365 {
366 ChardevMux *mux = backend->u.mux.data;
367 Chardev *drv;
368 MuxChardev *d = MUX_CHARDEV(chr);
369
370 drv = qemu_chr_find(mux->chardev);
371 if (drv == NULL) {
372 error_setg(errp, "mux: base chardev %s not found", mux->chardev);
373 return false;
374 }
375
376 d->focus = -1;
377 if (!qemu_chr_fe_init(&d->chr, drv, errp)) {
378 return false;
379 }
380
381 /*
382 * Only move to opened state if we've realized
383 * the initial set of muxes:
384 */
385 if (muxes_opened) {
386 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
387 }
388
389 return true;
390 }
391
392 static void mux_chr_parse(QemuOpts *opts, ChardevBackend *backend, Error **errp)
393 {
394 const char *chardev = qemu_opt_get(opts, "chardev");
395 ChardevMux *mux;
396
397 if (chardev == NULL) {
398 error_setg(errp, "chardev: mux: no chardev given");
399 return;
400 }
401 backend->type = CHARDEV_BACKEND_KIND_MUX;
402 mux = backend->u.mux.data = g_new0(ChardevMux, 1);
403 qemu_chr_parse_common(opts, qapi_ChardevMux_base(mux));
404 mux->chardev = g_strdup(chardev);
405 }
406
407 /**
408 * Called after processing of default and command-line-specified
409 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
410 * to a mux chardev. This is done here to ensure that
411 * output/prompts/banners are only displayed for the FE that has
412 * focus when initial command-line processing/machine init is
413 * completed.
414 *
415 * After this point, any new FE attached to any new or existing
416 * mux will receive CHR_EVENT_OPENED notifications for the BE
417 * immediately.
418 */
419 static void open_muxes(Chardev *chr)
420 {
421 /* send OPENED to all already-attached FEs */
422 mux_chr_send_all_event(chr, CHR_EVENT_OPENED);
423
424 /*
425 * mark mux as OPENED so any new FEs will immediately receive
426 * OPENED event
427 */
428 chr->be_open = 1;
429 }
430
431 void suspend_mux_open(void)
432 {
433 muxes_opened = false;
434 }
435
436 static int chardev_options_parsed_cb(Object *child, void *opaque)
437 {
438 Chardev *chr = (Chardev *)child;
439
440 if (!chr->be_open && CHARDEV_IS_MUX(chr)) {
441 open_muxes(chr);
442 }
443
444 return 0;
445 }
446
447 void resume_mux_open(void)
448 {
449 muxes_opened = true;
450 object_child_foreach(get_chardevs_root(),
451 chardev_options_parsed_cb, NULL);
452 }
453
454 static void char_mux_class_init(ObjectClass *oc, const void *data)
455 {
456 ChardevClass *cc = CHARDEV_CLASS(oc);
457
458 cc->chr_parse = mux_chr_parse;
459 cc->chr_open = mux_chr_open;
460 cc->chr_write = mux_chr_write;
461 cc->chr_accept_input = mux_chr_accept_input;
462 cc->chr_add_watch = mux_chr_add_watch;
463 cc->chr_be_event = mux_chr_be_event;
464 cc->chr_update_read_handler = mux_chr_update_read_handlers;
465 }
466
467 static const TypeInfo char_mux_type_info = {
468 .name = TYPE_CHARDEV_MUX,
469 .parent = TYPE_CHARDEV,
470 .class_init = char_mux_class_init,
471 .instance_size = sizeof(MuxChardev),
472 .instance_finalize = char_mux_finalize,
473 };
474
475 static void register_types(void)
476 {
477 type_register_static(&char_mux_type_info);
478 }
479
480 type_init(register_types);