master
c 242 lines 5.71 KB
Raw
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-or-later
11 */
12
13 #include "qemu/osdep.h"
14 #include "qemu/error-report.h"
15 #include "semihosting/semihost.h"
16 #include "system/runstate.h"
17 #include "gdbstub/user.h"
18 #include "gdbstub/syscalls.h"
19 #include "gdbstub/commands.h"
20 #include "trace.h"
21 #include "internals.h"
22
23 /* Syscall specific state */
24 typedef struct {
25 char syscall_buf[256];
26 gdb_syscall_complete_cb current_syscall_cb;
27 } GDBSyscallState;
28
29 static GDBSyscallState gdbserver_syscall_state;
30
31 /*
32 * Return true if there is a GDB currently connected to the stub
33 * and attached to a CPU
34 */
35 static bool gdb_attached(void)
36 {
37 return gdbserver_state.init && gdbserver_state.c_cpu;
38 }
39
40 static enum {
41 GDB_SYS_UNKNOWN,
42 GDB_SYS_ENABLED,
43 GDB_SYS_DISABLED,
44 } gdb_syscall_mode;
45
46 /* Decide if either remote gdb syscalls or native file IO should be used. */
47 int use_gdb_syscalls(void)
48 {
49 SemihostingTarget target = semihosting_get_target();
50 if (target == SEMIHOSTING_TARGET_NATIVE) {
51 /* -semihosting-config target=native */
52 return false;
53 } else if (target == SEMIHOSTING_TARGET_GDB) {
54 /* -semihosting-config target=gdb */
55 return true;
56 }
57
58 /* -semihosting-config target=auto */
59 /* On the first call check if gdb is connected and remember. */
60 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
61 gdb_syscall_mode = gdb_attached() ? GDB_SYS_ENABLED : GDB_SYS_DISABLED;
62 }
63 return gdb_syscall_mode == GDB_SYS_ENABLED;
64 }
65
66 /* called when the stub detaches */
67 void gdb_disable_syscalls(void)
68 {
69 gdb_syscall_mode = GDB_SYS_DISABLED;
70 }
71
72 void gdb_syscall_reset(void)
73 {
74 gdbserver_syscall_state.current_syscall_cb = NULL;
75 }
76
77 bool gdb_handled_syscall(void)
78 {
79 if (gdbserver_syscall_state.current_syscall_cb) {
80 gdb_put_packet(gdbserver_syscall_state.syscall_buf);
81 return true;
82 }
83
84 return false;
85 }
86
87 /*
88 * Send a gdb syscall request.
89 * This accepts limited printf-style format specifiers, specifically:
90 * %x - target_ulong argument printed in hex.
91 * %lx - 64-bit argument printed in hex.
92 * %s - string pointer (target_ulong) and length (int) pair.
93 */
94 void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
95 {
96 char *p, *p_end;
97 va_list va;
98
99 if (!gdb_attached()) {
100 return;
101 }
102
103 gdbserver_syscall_state.current_syscall_cb = cb;
104 va_start(va, fmt);
105
106 p = gdbserver_syscall_state.syscall_buf;
107 p_end = p + sizeof(gdbserver_syscall_state.syscall_buf);
108 *(p++) = 'F';
109 while (*fmt) {
110 if (*fmt == '%') {
111 uint64_t i64;
112 uint32_t i32;
113
114 fmt++;
115 switch (*fmt++) {
116 case 'x':
117 i32 = va_arg(va, uint32_t);
118 p += snprintf(p, p_end - p, "%" PRIx32, i32);
119 break;
120 case 'l':
121 if (*(fmt++) != 'x') {
122 goto bad_format;
123 }
124 i64 = va_arg(va, uint64_t);
125 p += snprintf(p, p_end - p, "%" PRIx64, i64);
126 break;
127 case 's':
128 i64 = va_arg(va, uint64_t);
129 i32 = va_arg(va, uint32_t);
130 p += snprintf(p, p_end - p, "%" PRIx64 "/%" PRIx32, i64, i32);
131 break;
132 default:
133 bad_format:
134 error_report("gdbstub: Bad syscall format string '%s'",
135 fmt - 1);
136 break;
137 }
138 } else {
139 *(p++) = *(fmt++);
140 }
141 }
142 *p = 0;
143
144 va_end(va);
145 gdb_syscall_handling(gdbserver_syscall_state.syscall_buf);
146 }
147
148 /*
149 * Map host error numbers to their GDB protocol counterparts.
150 * For the list of GDB File-I/O supported error numbers, please consult:
151 * https://sourceware.org/gdb/current/onlinedocs/gdb.html/Errno-Values.html
152 */
153 int host_to_gdb_errno(int err)
154 {
155 #define E(X) case E##X: return GDB_E##X
156 switch (err) {
157 E(PERM);
158 E(NOENT);
159 E(INTR);
160 E(IO);
161 E(BADF);
162 E(ACCES);
163 E(FAULT);
164 E(BUSY);
165 E(EXIST);
166 E(NODEV);
167 E(NOTDIR);
168 E(ISDIR);
169 E(INVAL);
170 E(NFILE);
171 E(MFILE);
172 E(FBIG);
173 E(NOSPC);
174 E(SPIPE);
175 E(ROFS);
176 E(NOSYS);
177 E(NAMETOOLONG);
178 default:
179 return GDB_EUNKNOWN;
180 }
181 #undef E
182 }
183
184 /*
185 * GDB Command Handlers
186 */
187
188 void gdb_handle_file_io(GArray *params, void *user_ctx)
189 {
190 if (params->len >= 1 && gdbserver_syscall_state.current_syscall_cb) {
191 uint64_t ret;
192 int err;
193
194 ret = gdb_get_cmd_param(params, 0)->val_ull;
195 if (params->len >= 2) {
196 err = gdb_get_cmd_param(params, 1)->val_ull;
197 } else {
198 err = 0;
199 }
200
201 /* Convert GDB error numbers back to host error numbers. */
202 #define E(X) case GDB_E##X: err = E##X; break
203 switch (err) {
204 case 0:
205 break;
206 E(PERM);
207 E(NOENT);
208 E(INTR);
209 E(BADF);
210 E(ACCES);
211 E(FAULT);
212 E(BUSY);
213 E(EXIST);
214 E(NODEV);
215 E(NOTDIR);
216 E(ISDIR);
217 E(INVAL);
218 E(NFILE);
219 E(MFILE);
220 E(FBIG);
221 E(NOSPC);
222 E(SPIPE);
223 E(ROFS);
224 E(NAMETOOLONG);
225 default:
226 err = EINVAL;
227 break;
228 }
229 #undef E
230
231 gdbserver_syscall_state.current_syscall_cb(gdbserver_state.c_cpu,
232 ret, err);
233 gdbserver_syscall_state.current_syscall_cb = NULL;
234 }
235
236 if (params->len >= 3 && gdb_get_cmd_param(params, 2)->opcode == (uint8_t)'C') {
237 gdb_put_packet("T02");
238 return;
239 }
240
241 gdb_continue();
242 }