console.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/cpu-common.h"
  21. #include "exec/gdbstub.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. #include "hw/core/cpu.h"
  29. /* Access to this structure is protected by the BQL */
  30. typedef struct SemihostingConsole {
  31. CharBackend backend;
  32. Chardev *chr;
  33. GSList *sleeping_cpus;
  34. bool got;
  35. Fifo8 fifo;
  36. } SemihostingConsole;
  37. static SemihostingConsole console;
  38. #define FIFO_SIZE 1024
  39. static int console_can_read(void *opaque)
  40. {
  41. SemihostingConsole *c = opaque;
  42. g_assert(bql_locked());
  43. return (int)fifo8_num_free(&c->fifo);
  44. }
  45. static void console_wake_up(gpointer data, gpointer user_data)
  46. {
  47. CPUState *cs = (CPUState *) data;
  48. /* cpu_handle_halt won't know we have work so just unbung here */
  49. cs->halted = 0;
  50. qemu_cpu_kick(cs);
  51. }
  52. static void console_read(void *opaque, const uint8_t *buf, int size)
  53. {
  54. SemihostingConsole *c = opaque;
  55. g_assert(bql_locked());
  56. while (size-- && !fifo8_is_full(&c->fifo)) {
  57. fifo8_push(&c->fifo, *buf++);
  58. }
  59. g_slist_foreach(c->sleeping_cpus, console_wake_up, NULL);
  60. c->sleeping_cpus = NULL;
  61. }
  62. bool qemu_semihosting_console_ready(void)
  63. {
  64. SemihostingConsole *c = &console;
  65. g_assert(bql_locked());
  66. return !fifo8_is_empty(&c->fifo);
  67. }
  68. void qemu_semihosting_console_block_until_ready(CPUState *cs)
  69. {
  70. SemihostingConsole *c = &console;
  71. g_assert(bql_locked());
  72. /* Block if the fifo is completely empty. */
  73. if (fifo8_is_empty(&c->fifo)) {
  74. c->sleeping_cpus = g_slist_prepend(c->sleeping_cpus, cs);
  75. cs->halted = 1;
  76. cs->exception_index = EXCP_HALTED;
  77. cpu_loop_exit(cs);
  78. /* never returns */
  79. }
  80. }
  81. int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
  82. {
  83. SemihostingConsole *c = &console;
  84. int ret = 0;
  85. qemu_semihosting_console_block_until_ready(cs);
  86. /* Read until buffer full or fifo exhausted. */
  87. do {
  88. *(char *)(buf + ret) = fifo8_pop(&c->fifo);
  89. ret++;
  90. } while (ret < len && !fifo8_is_empty(&c->fifo));
  91. return ret;
  92. }
  93. int qemu_semihosting_console_write(void *buf, int len)
  94. {
  95. if (console.chr) {
  96. int r = qemu_chr_write_all(console.chr, (uint8_t *)buf, len);
  97. return r < 0 ? 0 : r;
  98. } else {
  99. return fwrite(buf, 1, len, stderr);
  100. }
  101. }
  102. void qemu_semihosting_console_init(Chardev *chr)
  103. {
  104. console.chr = chr;
  105. if (chr) {
  106. fifo8_create(&console.fifo, FIFO_SIZE);
  107. qemu_chr_fe_init(&console.backend, chr, &error_abort);
  108. qemu_chr_fe_set_handlers(&console.backend,
  109. console_can_read,
  110. console_read,
  111. NULL, NULL, &console,
  112. NULL, true);
  113. }
  114. qemu_semihosting_guestfd_init();
  115. }