syscalls.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * GDB Syscall Handling
  3. *
  4. * GDB can execute syscalls on the guests behalf, currently used by
  5. * the various semihosting extensions.
  6. *
  7. * Copyright (c) 2003-2005 Fabrice Bellard
  8. * Copyright (c) 2023 Linaro Ltd
  9. *
  10. * SPDX-License-Identifier: LGPL-2.0+
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qemu/error-report.h"
  14. #include "semihosting/semihost.h"
  15. #include "sysemu/runstate.h"
  16. #include "gdbstub/user.h"
  17. #include "gdbstub/syscalls.h"
  18. #include "trace.h"
  19. #include "internals.h"
  20. /* Syscall specific state */
  21. typedef struct {
  22. char syscall_buf[256];
  23. gdb_syscall_complete_cb current_syscall_cb;
  24. } GDBSyscallState;
  25. static GDBSyscallState gdbserver_syscall_state;
  26. /*
  27. * Return true if there is a GDB currently connected to the stub
  28. * and attached to a CPU
  29. */
  30. static bool gdb_attached(void)
  31. {
  32. return gdbserver_state.init && gdbserver_state.c_cpu;
  33. }
  34. static enum {
  35. GDB_SYS_UNKNOWN,
  36. GDB_SYS_ENABLED,
  37. GDB_SYS_DISABLED,
  38. } gdb_syscall_mode;
  39. /* Decide if either remote gdb syscalls or native file IO should be used. */
  40. int use_gdb_syscalls(void)
  41. {
  42. SemihostingTarget target = semihosting_get_target();
  43. if (target == SEMIHOSTING_TARGET_NATIVE) {
  44. /* -semihosting-config target=native */
  45. return false;
  46. } else if (target == SEMIHOSTING_TARGET_GDB) {
  47. /* -semihosting-config target=gdb */
  48. return true;
  49. }
  50. /* -semihosting-config target=auto */
  51. /* On the first call check if gdb is connected and remember. */
  52. if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
  53. gdb_syscall_mode = gdb_attached() ? GDB_SYS_ENABLED : GDB_SYS_DISABLED;
  54. }
  55. return gdb_syscall_mode == GDB_SYS_ENABLED;
  56. }
  57. /* called when the stub detaches */
  58. void gdb_disable_syscalls(void)
  59. {
  60. gdb_syscall_mode = GDB_SYS_DISABLED;
  61. }
  62. void gdb_syscall_reset(void)
  63. {
  64. gdbserver_syscall_state.current_syscall_cb = NULL;
  65. }
  66. bool gdb_handled_syscall(void)
  67. {
  68. if (gdbserver_syscall_state.current_syscall_cb) {
  69. gdb_put_packet(gdbserver_syscall_state.syscall_buf);
  70. return true;
  71. }
  72. return false;
  73. }
  74. /*
  75. * Send a gdb syscall request.
  76. * This accepts limited printf-style format specifiers, specifically:
  77. * %x - target_ulong argument printed in hex.
  78. * %lx - 64-bit argument printed in hex.
  79. * %s - string pointer (target_ulong) and length (int) pair.
  80. */
  81. void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
  82. {
  83. char *p, *p_end;
  84. va_list va;
  85. if (!gdb_attached()) {
  86. return;
  87. }
  88. gdbserver_syscall_state.current_syscall_cb = cb;
  89. va_start(va, fmt);
  90. p = gdbserver_syscall_state.syscall_buf;
  91. p_end = p + sizeof(gdbserver_syscall_state.syscall_buf);
  92. *(p++) = 'F';
  93. while (*fmt) {
  94. if (*fmt == '%') {
  95. uint64_t i64;
  96. uint32_t i32;
  97. fmt++;
  98. switch (*fmt++) {
  99. case 'x':
  100. i32 = va_arg(va, uint32_t);
  101. p += snprintf(p, p_end - p, "%" PRIx32, i32);
  102. break;
  103. case 'l':
  104. if (*(fmt++) != 'x') {
  105. goto bad_format;
  106. }
  107. i64 = va_arg(va, uint64_t);
  108. p += snprintf(p, p_end - p, "%" PRIx64, i64);
  109. break;
  110. case 's':
  111. i64 = va_arg(va, uint64_t);
  112. i32 = va_arg(va, uint32_t);
  113. p += snprintf(p, p_end - p, "%" PRIx64 "/%x" PRIx32, i64, i32);
  114. break;
  115. default:
  116. bad_format:
  117. error_report("gdbstub: Bad syscall format string '%s'",
  118. fmt - 1);
  119. break;
  120. }
  121. } else {
  122. *(p++) = *(fmt++);
  123. }
  124. }
  125. *p = 0;
  126. va_end(va);
  127. gdb_syscall_handling(gdbserver_syscall_state.syscall_buf);
  128. }
  129. /*
  130. * GDB Command Handlers
  131. */
  132. void gdb_handle_file_io(GArray *params, void *user_ctx)
  133. {
  134. if (params->len >= 1 && gdbserver_syscall_state.current_syscall_cb) {
  135. uint64_t ret;
  136. int err;
  137. ret = get_param(params, 0)->val_ull;
  138. if (params->len >= 2) {
  139. err = get_param(params, 1)->val_ull;
  140. } else {
  141. err = 0;
  142. }
  143. /* Convert GDB error numbers back to host error numbers. */
  144. #define E(X) case GDB_E##X: err = E##X; break
  145. switch (err) {
  146. case 0:
  147. break;
  148. E(PERM);
  149. E(NOENT);
  150. E(INTR);
  151. E(BADF);
  152. E(ACCES);
  153. E(FAULT);
  154. E(BUSY);
  155. E(EXIST);
  156. E(NODEV);
  157. E(NOTDIR);
  158. E(ISDIR);
  159. E(INVAL);
  160. E(NFILE);
  161. E(MFILE);
  162. E(FBIG);
  163. E(NOSPC);
  164. E(SPIPE);
  165. E(ROFS);
  166. E(NAMETOOLONG);
  167. default:
  168. err = EINVAL;
  169. break;
  170. }
  171. #undef E
  172. gdbserver_syscall_state.current_syscall_cb(gdbserver_state.c_cpu,
  173. ret, err);
  174. gdbserver_syscall_state.current_syscall_cb = NULL;
  175. }
  176. if (params->len >= 3 && get_param(params, 2)->opcode == (uint8_t)'C') {
  177. gdb_put_packet("T02");
  178. return;
  179. }
  180. gdb_continue();
  181. }