2
0

char-mux.c 13 KB

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