console.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "cpu.h"
  19. #include "hw/semihosting/semihost.h"
  20. #include "hw/semihosting/console.h"
  21. #include "exec/gdbstub.h"
  22. #include "exec/exec-all.h"
  23. #include "qemu/log.h"
  24. #include "chardev/char.h"
  25. #include "chardev/char-fe.h"
  26. #include "sysemu/sysemu.h"
  27. #include "qemu/main-loop.h"
  28. #include "qapi/error.h"
  29. #include "qemu/fifo8.h"
  30. int qemu_semihosting_log_out(const char *s, int len)
  31. {
  32. Chardev *chardev = semihosting_get_chardev();
  33. if (chardev) {
  34. return qemu_chr_write_all(chardev, (uint8_t *) s, len);
  35. } else {
  36. return write(STDERR_FILENO, s, len);
  37. }
  38. }
  39. /*
  40. * A re-implementation of lock_user_string that we can use locally
  41. * instead of relying on softmmu-semi. Hopefully we can deprecate that
  42. * in time. Copy string until we find a 0 or address error.
  43. */
  44. static GString *copy_user_string(CPUArchState *env, target_ulong addr)
  45. {
  46. CPUState *cpu = env_cpu(env);
  47. GString *s = g_string_sized_new(128);
  48. uint8_t c;
  49. do {
  50. if (cpu_memory_rw_debug(cpu, addr++, &c, 1, 0) == 0) {
  51. if (c) {
  52. s = g_string_append_c(s, c);
  53. }
  54. } else {
  55. qemu_log_mask(LOG_GUEST_ERROR,
  56. "%s: passed inaccessible address " TARGET_FMT_lx,
  57. __func__, addr);
  58. break;
  59. }
  60. } while (c!=0);
  61. return s;
  62. }
  63. static void semihosting_cb(CPUState *cs, target_ulong ret, target_ulong err)
  64. {
  65. if (ret == (target_ulong) -1) {
  66. qemu_log("%s: gdb console output failed ("TARGET_FMT_ld")",
  67. __func__, err);
  68. }
  69. }
  70. int qemu_semihosting_console_outs(CPUArchState *env, target_ulong addr)
  71. {
  72. GString *s = copy_user_string(env, addr);
  73. int out = s->len;
  74. if (use_gdb_syscalls()) {
  75. gdb_do_syscall(semihosting_cb, "write,2,%x,%x", addr, s->len);
  76. } else {
  77. out = qemu_semihosting_log_out(s->str, s->len);
  78. }
  79. g_string_free(s, true);
  80. return out;
  81. }
  82. void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr)
  83. {
  84. CPUState *cpu = env_cpu(env);
  85. uint8_t c;
  86. if (cpu_memory_rw_debug(cpu, addr, &c, 1, 0) == 0) {
  87. if (use_gdb_syscalls()) {
  88. gdb_do_syscall(semihosting_cb, "write,2,%x,%x", addr, 1);
  89. } else {
  90. qemu_semihosting_log_out((const char *) &c, 1);
  91. }
  92. } else {
  93. qemu_log_mask(LOG_GUEST_ERROR,
  94. "%s: passed inaccessible address " TARGET_FMT_lx,
  95. __func__, addr);
  96. }
  97. }
  98. #define FIFO_SIZE 1024
  99. /* Access to this structure is protected by the BQL */
  100. typedef struct SemihostingConsole {
  101. CharBackend backend;
  102. GSList *sleeping_cpus;
  103. bool got;
  104. Fifo8 fifo;
  105. } SemihostingConsole;
  106. static SemihostingConsole console;
  107. static int console_can_read(void *opaque)
  108. {
  109. SemihostingConsole *c = opaque;
  110. int ret;
  111. g_assert(qemu_mutex_iothread_locked());
  112. ret = (int) fifo8_num_free(&c->fifo);
  113. return ret;
  114. }
  115. static void console_wake_up(gpointer data, gpointer user_data)
  116. {
  117. CPUState *cs = (CPUState *) data;
  118. /* cpu_handle_halt won't know we have work so just unbung here */
  119. cs->halted = 0;
  120. qemu_cpu_kick(cs);
  121. }
  122. static void console_read(void *opaque, const uint8_t *buf, int size)
  123. {
  124. SemihostingConsole *c = opaque;
  125. g_assert(qemu_mutex_iothread_locked());
  126. while (size-- && !fifo8_is_full(&c->fifo)) {
  127. fifo8_push(&c->fifo, *buf++);
  128. }
  129. g_slist_foreach(c->sleeping_cpus, console_wake_up, NULL);
  130. c->sleeping_cpus = NULL;
  131. }
  132. target_ulong qemu_semihosting_console_inc(CPUArchState *env)
  133. {
  134. uint8_t ch;
  135. SemihostingConsole *c = &console;
  136. g_assert(qemu_mutex_iothread_locked());
  137. g_assert(current_cpu);
  138. if (fifo8_is_empty(&c->fifo)) {
  139. c->sleeping_cpus = g_slist_prepend(c->sleeping_cpus, current_cpu);
  140. current_cpu->halted = 1;
  141. current_cpu->exception_index = EXCP_HALTED;
  142. cpu_loop_exit(current_cpu);
  143. /* never returns */
  144. }
  145. ch = fifo8_pop(&c->fifo);
  146. return (target_ulong) ch;
  147. }
  148. void qemu_semihosting_console_init(void)
  149. {
  150. Chardev *chr = semihosting_get_chardev();
  151. if (chr) {
  152. fifo8_create(&console.fifo, FIFO_SIZE);
  153. qemu_chr_fe_init(&console.backend, chr, &error_abort);
  154. qemu_chr_fe_set_handlers(&console.backend,
  155. console_can_read,
  156. console_read,
  157. NULL, NULL, &console,
  158. NULL, true);
  159. }
  160. }