spapr: Remove support for NVIDIA V100 GPU with NVLink2
[qemu/kevin.git] / gdbstub / user.c
blob7ab6e5d975e0512922e7cd97ba2a06cd608d9e04
1 /*
2 * gdbstub user-mode helper routines.
4 * We know for user-mode we are using TCG so we can call stuff directly.
6 * Copyright (c) 2003-2005 Fabrice Bellard
7 * Copyright (c) 2022 Linaro Ltd
9 * SPDX-License-Identifier: LGPL-2.0+
12 #include "qemu/osdep.h"
13 #include "qemu/cutils.h"
14 #include "qemu/sockets.h"
15 #include "exec/hwaddr.h"
16 #include "exec/tb-flush.h"
17 #include "exec/gdbstub.h"
18 #include "gdbstub/syscalls.h"
19 #include "gdbstub/user.h"
20 #include "hw/core/cpu.h"
21 #include "trace.h"
22 #include "internals.h"
24 /* User-mode specific state */
25 typedef struct {
26 int fd;
27 char *socket_path;
28 int running_state;
29 } GDBUserState;
31 static GDBUserState gdbserver_user_state;
33 int gdb_get_char(void)
35 uint8_t ch;
36 int ret;
38 for (;;) {
39 ret = recv(gdbserver_user_state.fd, &ch, 1, 0);
40 if (ret < 0) {
41 if (errno == ECONNRESET) {
42 gdbserver_user_state.fd = -1;
44 if (errno != EINTR) {
45 return -1;
47 } else if (ret == 0) {
48 close(gdbserver_user_state.fd);
49 gdbserver_user_state.fd = -1;
50 return -1;
51 } else {
52 break;
55 return ch;
58 bool gdb_got_immediate_ack(void)
60 int i;
62 i = gdb_get_char();
63 if (i < 0) {
64 /* no response, continue anyway */
65 return true;
68 if (i == '+') {
69 /* received correctly, continue */
70 return true;
73 /* anything else, including '-' then try again */
74 return false;
77 void gdb_put_buffer(const uint8_t *buf, int len)
79 int ret;
81 while (len > 0) {
82 ret = send(gdbserver_user_state.fd, buf, len, 0);
83 if (ret < 0) {
84 if (errno != EINTR) {
85 return;
87 } else {
88 buf += ret;
89 len -= ret;
94 /* Tell the remote gdb that the process has exited. */
95 void gdb_exit(int code)
97 char buf[4];
99 if (!gdbserver_state.init) {
100 return;
102 if (gdbserver_user_state.socket_path) {
103 unlink(gdbserver_user_state.socket_path);
105 if (gdbserver_user_state.fd < 0) {
106 return;
109 trace_gdbstub_op_exiting((uint8_t)code);
111 if (gdbserver_state.allow_stop_reply) {
112 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
113 gdb_put_packet(buf);
114 gdbserver_state.allow_stop_reply = false;
118 int gdb_handlesig(CPUState *cpu, int sig)
120 char buf[256];
121 int n;
123 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
124 return sig;
127 /* disable single step if it was enabled */
128 cpu_single_step(cpu, 0);
129 tb_flush(cpu);
131 if (sig != 0) {
132 gdb_set_stop_cpu(cpu);
133 if (gdbserver_state.allow_stop_reply) {
134 g_string_printf(gdbserver_state.str_buf,
135 "T%02xthread:", gdb_target_signal_to_gdb(sig));
136 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
137 g_string_append_c(gdbserver_state.str_buf, ';');
138 gdb_put_strbuf();
139 gdbserver_state.allow_stop_reply = false;
143 * gdb_put_packet() might have detected that the peer terminated the
144 * connection.
146 if (gdbserver_user_state.fd < 0) {
147 return sig;
150 sig = 0;
151 gdbserver_state.state = RS_IDLE;
152 gdbserver_user_state.running_state = 0;
153 while (gdbserver_user_state.running_state == 0) {
154 n = read(gdbserver_user_state.fd, buf, 256);
155 if (n > 0) {
156 int i;
158 for (i = 0; i < n; i++) {
159 gdb_read_byte(buf[i]);
161 } else {
163 * XXX: Connection closed. Should probably wait for another
164 * connection before continuing.
166 if (n == 0) {
167 close(gdbserver_user_state.fd);
169 gdbserver_user_state.fd = -1;
170 return sig;
173 sig = gdbserver_state.signal;
174 gdbserver_state.signal = 0;
175 return sig;
178 /* Tell the remote gdb that the process has exited due to SIG. */
179 void gdb_signalled(CPUArchState *env, int sig)
181 char buf[4];
183 if (!gdbserver_state.init || gdbserver_user_state.fd < 0 ||
184 !gdbserver_state.allow_stop_reply) {
185 return;
188 snprintf(buf, sizeof(buf), "X%02x", gdb_target_signal_to_gdb(sig));
189 gdb_put_packet(buf);
190 gdbserver_state.allow_stop_reply = false;
193 static void gdb_accept_init(int fd)
195 gdb_init_gdbserver_state();
196 gdb_create_default_process(&gdbserver_state);
197 gdbserver_state.processes[0].attached = true;
198 gdbserver_state.c_cpu = gdb_first_attached_cpu();
199 gdbserver_state.g_cpu = gdbserver_state.c_cpu;
200 gdbserver_user_state.fd = fd;
203 static bool gdb_accept_socket(int gdb_fd)
205 int fd;
207 for (;;) {
208 fd = accept(gdb_fd, NULL, NULL);
209 if (fd < 0 && errno != EINTR) {
210 perror("accept socket");
211 return false;
212 } else if (fd >= 0) {
213 qemu_set_cloexec(fd);
214 break;
218 gdb_accept_init(fd);
219 return true;
222 static int gdbserver_open_socket(const char *path)
224 struct sockaddr_un sockaddr = {};
225 int fd, ret;
227 fd = socket(AF_UNIX, SOCK_STREAM, 0);
228 if (fd < 0) {
229 perror("create socket");
230 return -1;
233 sockaddr.sun_family = AF_UNIX;
234 pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path);
235 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
236 if (ret < 0) {
237 perror("bind socket");
238 close(fd);
239 return -1;
241 ret = listen(fd, 1);
242 if (ret < 0) {
243 perror("listen socket");
244 close(fd);
245 return -1;
248 return fd;
251 static bool gdb_accept_tcp(int gdb_fd)
253 struct sockaddr_in sockaddr = {};
254 socklen_t len;
255 int fd;
257 for (;;) {
258 len = sizeof(sockaddr);
259 fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
260 if (fd < 0 && errno != EINTR) {
261 perror("accept");
262 return false;
263 } else if (fd >= 0) {
264 qemu_set_cloexec(fd);
265 break;
269 /* set short latency */
270 if (socket_set_nodelay(fd)) {
271 perror("setsockopt");
272 close(fd);
273 return false;
276 gdb_accept_init(fd);
277 return true;
280 static int gdbserver_open_port(int port)
282 struct sockaddr_in sockaddr;
283 int fd, ret;
285 fd = socket(PF_INET, SOCK_STREAM, 0);
286 if (fd < 0) {
287 perror("socket");
288 return -1;
290 qemu_set_cloexec(fd);
292 socket_set_fast_reuse(fd);
294 sockaddr.sin_family = AF_INET;
295 sockaddr.sin_port = htons(port);
296 sockaddr.sin_addr.s_addr = 0;
297 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
298 if (ret < 0) {
299 perror("bind");
300 close(fd);
301 return -1;
303 ret = listen(fd, 1);
304 if (ret < 0) {
305 perror("listen");
306 close(fd);
307 return -1;
310 return fd;
313 int gdbserver_start(const char *port_or_path)
315 int port = g_ascii_strtoull(port_or_path, NULL, 10);
316 int gdb_fd;
318 if (port > 0) {
319 gdb_fd = gdbserver_open_port(port);
320 } else {
321 gdb_fd = gdbserver_open_socket(port_or_path);
324 if (gdb_fd < 0) {
325 return -1;
328 if (port > 0 && gdb_accept_tcp(gdb_fd)) {
329 return 0;
330 } else if (gdb_accept_socket(gdb_fd)) {
331 gdbserver_user_state.socket_path = g_strdup(port_or_path);
332 return 0;
335 /* gone wrong */
336 close(gdb_fd);
337 return -1;
340 /* Disable gdb stub for child processes. */
341 void gdbserver_fork(CPUState *cpu)
343 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
344 return;
346 close(gdbserver_user_state.fd);
347 gdbserver_user_state.fd = -1;
348 cpu_breakpoint_remove_all(cpu, BP_GDB);
349 /* no cpu_watchpoint_remove_all for user-mode */
353 * Execution state helpers
356 void gdb_handle_query_attached(GArray *params, void *user_ctx)
358 gdb_put_packet("0");
361 void gdb_continue(void)
363 gdbserver_user_state.running_state = 1;
364 trace_gdbstub_op_continue();
368 * Resume execution, for user-mode emulation it's equivalent to
369 * gdb_continue.
371 int gdb_continue_partial(char *newstates)
373 CPUState *cpu;
374 int res = 0;
376 * This is not exactly accurate, but it's an improvement compared to the
377 * previous situation, where only one CPU would be single-stepped.
379 CPU_FOREACH(cpu) {
380 if (newstates[cpu->cpu_index] == 's') {
381 trace_gdbstub_op_stepping(cpu->cpu_index);
382 cpu_single_step(cpu, gdbserver_state.sstep_flags);
385 gdbserver_user_state.running_state = 1;
386 return res;
390 * Memory access helpers
392 int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
393 uint8_t *buf, int len, bool is_write)
395 CPUClass *cc;
397 cc = CPU_GET_CLASS(cpu);
398 if (cc->memory_rw_debug) {
399 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
401 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
405 * cpu helpers
408 unsigned int gdb_get_max_cpus(void)
410 CPUState *cpu;
411 unsigned int max_cpus = 1;
413 CPU_FOREACH(cpu) {
414 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
417 return max_cpus;
420 /* replay not supported for user-mode */
421 bool gdb_can_reverse(void)
423 return false;
427 * Break/Watch point helpers
430 bool gdb_supports_guest_debug(void)
432 /* user-mode == TCG == supported */
433 return true;
436 int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
438 CPUState *cpu;
439 int err = 0;
441 switch (type) {
442 case GDB_BREAKPOINT_SW:
443 case GDB_BREAKPOINT_HW:
444 CPU_FOREACH(cpu) {
445 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
446 if (err) {
447 break;
450 return err;
451 default:
452 /* user-mode doesn't support watchpoints */
453 return -ENOSYS;
457 int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
459 CPUState *cpu;
460 int err = 0;
462 switch (type) {
463 case GDB_BREAKPOINT_SW:
464 case GDB_BREAKPOINT_HW:
465 CPU_FOREACH(cpu) {
466 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
467 if (err) {
468 break;
471 return err;
472 default:
473 /* user-mode doesn't support watchpoints */
474 return -ENOSYS;
478 void gdb_breakpoint_remove_all(CPUState *cs)
480 cpu_breakpoint_remove_all(cs, BP_GDB);
484 * For user-mode syscall support we send the system call immediately
485 * and then return control to gdb for it to process the syscall request.
486 * Since the protocol requires that gdb hands control back to us
487 * using a "here are the results" F packet, we don't need to check
488 * gdb_handlesig's return value (which is the signal to deliver if
489 * execution was resumed via a continue packet).
491 void gdb_syscall_handling(const char *syscall_packet)
493 gdb_put_packet(syscall_packet);
494 gdb_handlesig(gdbserver_state.c_cpu, 0);