console.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. int qemu_semihosting_log_out(const char *s, int len)
  38. {
  39. if (console.chr) {
  40. return qemu_chr_write_all(console.chr, (uint8_t *) s, len);
  41. } else {
  42. return write(STDERR_FILENO, s, len);
  43. }
  44. }
  45. #define FIFO_SIZE 1024
  46. static int console_can_read(void *opaque)
  47. {
  48. SemihostingConsole *c = opaque;
  49. int ret;
  50. g_assert(qemu_mutex_iothread_locked());
  51. ret = (int) fifo8_num_free(&c->fifo);
  52. return ret;
  53. }
  54. static void console_wake_up(gpointer data, gpointer user_data)
  55. {
  56. CPUState *cs = (CPUState *) data;
  57. /* cpu_handle_halt won't know we have work so just unbung here */
  58. cs->halted = 0;
  59. qemu_cpu_kick(cs);
  60. }
  61. static void console_read(void *opaque, const uint8_t *buf, int size)
  62. {
  63. SemihostingConsole *c = opaque;
  64. g_assert(qemu_mutex_iothread_locked());
  65. while (size-- && !fifo8_is_full(&c->fifo)) {
  66. fifo8_push(&c->fifo, *buf++);
  67. }
  68. g_slist_foreach(c->sleeping_cpus, console_wake_up, NULL);
  69. c->sleeping_cpus = NULL;
  70. }
  71. int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
  72. {
  73. SemihostingConsole *c = &console;
  74. int ret = 0;
  75. g_assert(qemu_mutex_iothread_locked());
  76. /* Block if the fifo is completely empty. */
  77. if (fifo8_is_empty(&c->fifo)) {
  78. c->sleeping_cpus = g_slist_prepend(c->sleeping_cpus, cs);
  79. cs->halted = 1;
  80. cs->exception_index = EXCP_HALTED;
  81. cpu_loop_exit(cs);
  82. /* never returns */
  83. }
  84. /* Read until buffer full or fifo exhausted. */
  85. do {
  86. *(char *)(buf + ret) = fifo8_pop(&c->fifo);
  87. ret++;
  88. } while (ret < len && !fifo8_is_empty(&c->fifo));
  89. return ret;
  90. }
  91. int qemu_semihosting_console_write(void *buf, int len)
  92. {
  93. if (console.chr) {
  94. return qemu_chr_write_all(console.chr, (uint8_t *)buf, len);
  95. } else {
  96. return fwrite(buf, 1, len, stderr);
  97. }
  98. }
  99. void qemu_semihosting_console_init(Chardev *chr)
  100. {
  101. console.chr = chr;
  102. if (chr) {
  103. fifo8_create(&console.fifo, FIFO_SIZE);
  104. qemu_chr_fe_init(&console.backend, chr, &error_abort);
  105. qemu_chr_fe_set_handlers(&console.backend,
  106. console_can_read,
  107. console_read,
  108. NULL, NULL, &console,
  109. NULL, true);
  110. }
  111. qemu_semihosting_guestfd_init();
  112. }