2
0

console.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Semihosting Console Support
  3. *
  4. * Copyright (c) 2015 Imagination Technologies
  5. * Copyright (c) 2019 Linaro Ltd
  6. *
  7. * This provides support for outputting to a semihosting console.
  8. *
  9. * While most semihosting implementations support reading and writing
  10. * to arbitrary file descriptors we treat the console as something
  11. * specifically for debugging interaction. This means messages can be
  12. * re-directed to gdb (if currently being used to debug) or even
  13. * re-directed elsewhere.
  14. *
  15. * SPDX-License-Identifier: GPL-2.0-or-later
  16. */
  17. #include "qemu/osdep.h"
  18. #include "semihosting/semihost.h"
  19. #include "semihosting/console.h"
  20. #include "exec/gdbstub.h"
  21. #include "exec/exec-all.h"
  22. #include "qemu/log.h"
  23. #include "chardev/char.h"
  24. #include "chardev/char-fe.h"
  25. #include "qemu/main-loop.h"
  26. #include "qapi/error.h"
  27. #include "qemu/fifo8.h"
  28. /* Access to this structure is protected by the BQL */
  29. typedef struct SemihostingConsole {
  30. CharBackend backend;
  31. Chardev *chr;
  32. GSList *sleeping_cpus;
  33. bool got;
  34. Fifo8 fifo;
  35. } SemihostingConsole;
  36. static SemihostingConsole console;
  37. #define FIFO_SIZE 1024
  38. static int console_can_read(void *opaque)
  39. {
  40. SemihostingConsole *c = opaque;
  41. g_assert(qemu_mutex_iothread_locked());
  42. return (int)fifo8_num_free(&c->fifo);
  43. }
  44. static void console_wake_up(gpointer data, gpointer user_data)
  45. {
  46. CPUState *cs = (CPUState *) data;
  47. /* cpu_handle_halt won't know we have work so just unbung here */
  48. cs->halted = 0;
  49. qemu_cpu_kick(cs);
  50. }
  51. static void console_read(void *opaque, const uint8_t *buf, int size)
  52. {
  53. SemihostingConsole *c = opaque;
  54. g_assert(qemu_mutex_iothread_locked());
  55. while (size-- && !fifo8_is_full(&c->fifo)) {
  56. fifo8_push(&c->fifo, *buf++);
  57. }
  58. g_slist_foreach(c->sleeping_cpus, console_wake_up, NULL);
  59. c->sleeping_cpus = NULL;
  60. }
  61. bool qemu_semihosting_console_ready(void)
  62. {
  63. SemihostingConsole *c = &console;
  64. g_assert(qemu_mutex_iothread_locked());
  65. return !fifo8_is_empty(&c->fifo);
  66. }
  67. void qemu_semihosting_console_block_until_ready(CPUState *cs)
  68. {
  69. SemihostingConsole *c = &console;
  70. g_assert(qemu_mutex_iothread_locked());
  71. /* Block if the fifo is completely empty. */
  72. if (fifo8_is_empty(&c->fifo)) {
  73. c->sleeping_cpus = g_slist_prepend(c->sleeping_cpus, cs);
  74. cs->halted = 1;
  75. cs->exception_index = EXCP_HALTED;
  76. cpu_loop_exit(cs);
  77. /* never returns */
  78. }
  79. }
  80. int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
  81. {
  82. SemihostingConsole *c = &console;
  83. int ret = 0;
  84. qemu_semihosting_console_block_until_ready(cs);
  85. /* Read until buffer full or fifo exhausted. */
  86. do {
  87. *(char *)(buf + ret) = fifo8_pop(&c->fifo);
  88. ret++;
  89. } while (ret < len && !fifo8_is_empty(&c->fifo));
  90. return ret;
  91. }
  92. int qemu_semihosting_console_write(void *buf, int len)
  93. {
  94. if (console.chr) {
  95. int r = qemu_chr_write_all(console.chr, (uint8_t *)buf, len);
  96. return r < 0 ? 0 : r;
  97. } else {
  98. return fwrite(buf, 1, len, stderr);
  99. }
  100. }
  101. void qemu_semihosting_console_init(Chardev *chr)
  102. {
  103. console.chr = chr;
  104. if (chr) {
  105. fifo8_create(&console.fifo, FIFO_SIZE);
  106. qemu_chr_fe_init(&console.backend, chr, &error_abort);
  107. qemu_chr_fe_set_handlers(&console.backend,
  108. console_can_read,
  109. console_read,
  110. NULL, NULL, &console,
  111. NULL, true);
  112. }
  113. qemu_semihosting_guestfd_init();
  114. }