console.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "qemu/log.h"
  23. #include "chardev/char.h"
  24. int qemu_semihosting_log_out(const char *s, int len)
  25. {
  26. Chardev *chardev = semihosting_get_chardev();
  27. if (chardev) {
  28. return qemu_chr_write_all(chardev, (uint8_t *) s, len);
  29. } else {
  30. return write(STDERR_FILENO, s, len);
  31. }
  32. }
  33. /*
  34. * A re-implementation of lock_user_string that we can use locally
  35. * instead of relying on softmmu-semi. Hopefully we can deprecate that
  36. * in time. Copy string until we find a 0 or address error.
  37. */
  38. static GString *copy_user_string(CPUArchState *env, target_ulong addr)
  39. {
  40. CPUState *cpu = env_cpu(env);
  41. GString *s = g_string_sized_new(128);
  42. uint8_t c;
  43. do {
  44. if (cpu_memory_rw_debug(cpu, addr++, &c, 1, 0) == 0) {
  45. s = g_string_append_c(s, c);
  46. } else {
  47. qemu_log_mask(LOG_GUEST_ERROR,
  48. "%s: passed inaccessible address " TARGET_FMT_lx,
  49. __func__, addr);
  50. break;
  51. }
  52. } while (c!=0);
  53. return s;
  54. }
  55. static void semihosting_cb(CPUState *cs, target_ulong ret, target_ulong err)
  56. {
  57. if (ret == (target_ulong) -1) {
  58. qemu_log("%s: gdb console output failed ("TARGET_FMT_ld")",
  59. __func__, err);
  60. }
  61. }
  62. int qemu_semihosting_console_outs(CPUArchState *env, target_ulong addr)
  63. {
  64. GString *s = copy_user_string(env, addr);
  65. int out = s->len;
  66. if (use_gdb_syscalls()) {
  67. gdb_do_syscall(semihosting_cb, "write,2,%x,%x", addr, s->len);
  68. } else {
  69. out = qemu_semihosting_log_out(s->str, s->len);
  70. }
  71. g_string_free(s, true);
  72. return out;
  73. }
  74. void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr)
  75. {
  76. CPUState *cpu = env_cpu(env);
  77. uint8_t c;
  78. if (cpu_memory_rw_debug(cpu, addr, &c, 1, 0) == 0) {
  79. if (use_gdb_syscalls()) {
  80. gdb_do_syscall(semihosting_cb, "write,2,%x,%x", addr, 1);
  81. } else {
  82. qemu_semihosting_log_out((const char *) &c, 1);
  83. }
  84. } else {
  85. qemu_log_mask(LOG_GUEST_ERROR,
  86. "%s: passed inaccessible address " TARGET_FMT_lx,
  87. __func__, addr);
  88. }
  89. }