| 1 | /* |
| 2 | * gdbstub internals |
| 3 | * |
| 4 | * Copyright (c) 2022 Linaro Ltd |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #ifndef GDBSTUB_INTERNALS_H |
| 10 | #define GDBSTUB_INTERNALS_H |
| 11 | |
| 12 | #include "qemu/accel.h" |
| 13 | #include "exec/cpu-common.h" |
| 14 | #include "gdbstub/enums.h" |
| 15 | |
| 16 | /* |
| 17 | * Most "large" transfers (e.g. memory reads, feature XML |
| 18 | * transfer) have mechanisms in the gdb protocol for splitting |
| 19 | * them. However, register values in particular cannot currently |
| 20 | * be split. This packet size must therefore be at least big enough |
| 21 | * for the worst-case register size. Currently that is Arm SME |
| 22 | * ZA storage with a 256x256 byte value. We also must account |
| 23 | * for the conversion from raw data to hex in gdb_memtohex(), |
| 24 | * which writes 2 * size bytes, and for other protocol overhead |
| 25 | * including command, register number and checksum which add |
| 26 | * another 4 bytes of overhead. However, to be consistent with |
| 27 | * the changes made in gdbserver to address this same requirement, |
| 28 | * we add a total of 32 bytes to account for protocol overhead |
| 29 | * (unclear why specifically 32 bytes), bringing the value of |
| 30 | * MAX_PACKET_LENGTH to 2 * 256 * 256 + 32 = 131104. |
| 31 | * |
| 32 | * The commit making this change for gdbserver can be found here: |
| 33 | * https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h= |
| 34 | * b816042e88583f280ad186ff124ab84d31fb592b |
| 35 | */ |
| 36 | #define MAX_PACKET_LENGTH 131104 |
| 37 | |
| 38 | /* |
| 39 | * Shared structures and definitions |
| 40 | */ |
| 41 | |
| 42 | enum { |
| 43 | GDB_SIGNAL_0 = 0, |
| 44 | GDB_SIGNAL_INT = 2, |
| 45 | GDB_SIGNAL_QUIT = 3, |
| 46 | GDB_SIGNAL_TRAP = 5, |
| 47 | GDB_SIGNAL_ABRT = 6, |
| 48 | GDB_SIGNAL_ALRM = 14, |
| 49 | GDB_SIGNAL_STOP = 17, |
| 50 | GDB_SIGNAL_IO = 23, |
| 51 | GDB_SIGNAL_XCPU = 24, |
| 52 | GDB_SIGNAL_UNKNOWN = 143 |
| 53 | }; |
| 54 | |
| 55 | typedef struct GDBProcess { |
| 56 | uint32_t pid; |
| 57 | bool attached; |
| 58 | char *target_xml; |
| 59 | } GDBProcess; |
| 60 | |
| 61 | enum RSState { |
| 62 | RS_INACTIVE, |
| 63 | RS_IDLE, |
| 64 | RS_GETLINE, |
| 65 | RS_GETLINE_ESC, |
| 66 | RS_GETLINE_RLE, |
| 67 | RS_CHKSUM1, |
| 68 | RS_CHKSUM2, |
| 69 | }; |
| 70 | |
| 71 | typedef struct GDBState { |
| 72 | bool init; /* have we been initialised? */ |
| 73 | CPUState *c_cpu; /* current CPU for step/continue ops */ |
| 74 | CPUState *g_cpu; /* current CPU for other ops */ |
| 75 | CPUState *query_cpu; /* for q{f|s}ThreadInfo */ |
| 76 | enum RSState state; /* parsing state */ |
| 77 | char line_buf[MAX_PACKET_LENGTH]; |
| 78 | int line_buf_index; |
| 79 | int line_sum; /* running checksum */ |
| 80 | int line_csum; /* checksum at the end of the packet */ |
| 81 | GByteArray *last_packet; |
| 82 | int signal; |
| 83 | bool multiprocess; |
| 84 | GDBProcess *processes; |
| 85 | int process_num; |
| 86 | GString *str_buf; |
| 87 | GByteArray *mem_buf; |
| 88 | AccelGdbConfig accel_config; |
| 89 | int sstep_flags; |
| 90 | /* |
| 91 | * Whether we are allowed to send a stop reply packet at this moment. |
| 92 | * Must be set off after sending the stop reply itself. |
| 93 | */ |
| 94 | bool allow_stop_reply; |
| 95 | } GDBState; |
| 96 | |
| 97 | /* lives in main gdbstub.c */ |
| 98 | extern GDBState gdbserver_state; |
| 99 | |
| 100 | /* |
| 101 | * Inline utility function, convert from int to hex and back |
| 102 | */ |
| 103 | |
| 104 | static inline int fromhex(int v) |
| 105 | { |
| 106 | if (v >= '0' && v <= '9') { |
| 107 | return v - '0'; |
| 108 | } else if (v >= 'A' && v <= 'F') { |
| 109 | return v - 'A' + 10; |
| 110 | } else if (v >= 'a' && v <= 'f') { |
| 111 | return v - 'a' + 10; |
| 112 | } else { |
| 113 | return 0; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static inline int tohex(int v) |
| 118 | { |
| 119 | if (v < 10) { |
| 120 | return v + '0'; |
| 121 | } else { |
| 122 | return v - 10 + 'a'; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Connection helpers for both system and user backends |
| 128 | */ |
| 129 | |
| 130 | void gdb_put_strbuf(void); |
| 131 | int gdb_put_packet_binary(const char *buf, int len, bool dump); |
| 132 | void gdb_memtohex(GString *buf, const uint8_t *mem, int len); |
| 133 | void gdb_memtox(GString *buf, const char *mem, int len); |
| 134 | void gdb_read_byte(uint8_t ch); |
| 135 | |
| 136 | /* |
| 137 | * Packet acknowledgement - we handle this slightly differently |
| 138 | * between user and system mode, mainly to deal with the differences |
| 139 | * between the flexible chardev and the direct fd approaches. |
| 140 | * |
| 141 | * We currently don't support a negotiated QStartNoAckMode |
| 142 | */ |
| 143 | |
| 144 | /** |
| 145 | * gdb_got_immediate_ack() - check ok to continue |
| 146 | * |
| 147 | * Returns true to continue, false to re-transmit for user only, the |
| 148 | * system stub always returns true. |
| 149 | */ |
| 150 | bool gdb_got_immediate_ack(void); |
| 151 | /* utility helpers */ |
| 152 | GDBProcess *gdb_get_process(uint32_t pid); |
| 153 | CPUState *gdb_get_first_cpu_in_process(GDBProcess *process); |
| 154 | CPUState *gdb_first_attached_cpu(void); |
| 155 | void gdb_append_thread_id(CPUState *cpu, GString *buf); |
| 156 | int gdb_get_cpu_index(CPUState *cpu); |
| 157 | unsigned int gdb_get_max_cpus(void); /* both */ |
| 158 | int gdb_target_sigtrap(void); /* user */ |
| 159 | |
| 160 | void gdb_create_default_process(GDBState *s); |
| 161 | |
| 162 | /* signal mapping, common for system, specialised for user-mode */ |
| 163 | int gdb_signal_to_target(int sig); |
| 164 | int gdb_target_signal_to_gdb(int sig); |
| 165 | |
| 166 | int gdb_get_char(void); /* user only */ |
| 167 | |
| 168 | /** |
| 169 | * gdb_continue() - handle continue in mode specific way. |
| 170 | */ |
| 171 | void gdb_continue(void); |
| 172 | |
| 173 | /** |
| 174 | * gdb_continue_partial() - handle partial continue in mode specific way. |
| 175 | */ |
| 176 | int gdb_continue_partial(char *newstates); |
| 177 | |
| 178 | /* |
| 179 | * Helpers with separate system and user implementations |
| 180 | */ |
| 181 | void gdb_put_buffer(const uint8_t *buf, int len); |
| 182 | |
| 183 | /* |
| 184 | * Command handlers - either specialised or system or user only |
| 185 | */ |
| 186 | void gdb_init_gdbserver_state(void); |
| 187 | |
| 188 | void gdb_handle_query_rcmd(GArray *params, void *ctx); /* system */ |
| 189 | void gdb_handle_query_offsets(GArray *params, void *user_ctx); /* user */ |
| 190 | void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */ |
| 191 | void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx); /*user */ |
| 192 | void gdb_handle_v_file_open(GArray *params, void *user_ctx); /* user */ |
| 193 | void gdb_handle_v_file_close(GArray *params, void *user_ctx); /* user */ |
| 194 | void gdb_handle_v_file_pread(GArray *params, void *user_ctx); /* user */ |
| 195 | void gdb_handle_v_file_readlink(GArray *params, void *user_ctx); /* user */ |
| 196 | void gdb_handle_query_xfer_exec_file(GArray *params, void *user_ctx); /* user */ |
| 197 | void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx); /* user */ |
| 198 | void gdb_handle_query_supported_user(const char *gdb_supported); /* user */ |
| 199 | bool gdb_handle_set_thread_user(uint32_t pid, uint32_t tid); /* user */ |
| 200 | bool gdb_handle_detach_user(uint32_t pid); /* user */ |
| 201 | |
| 202 | void gdb_handle_query_attached(GArray *params, void *ctx); /* both */ |
| 203 | |
| 204 | /* system only */ |
| 205 | void gdb_handle_query_qemu_phy_mem_mode(GArray *params, void *ctx); |
| 206 | void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx); |
| 207 | |
| 208 | /* sycall handling */ |
| 209 | void gdb_handle_file_io(GArray *params, void *user_ctx); |
| 210 | bool gdb_handled_syscall(void); |
| 211 | void gdb_disable_syscalls(void); |
| 212 | void gdb_syscall_reset(void); |
| 213 | |
| 214 | /* user/system specific syscall handling */ |
| 215 | void gdb_syscall_handling(const char *syscall_packet); |
| 216 | |
| 217 | /* |
| 218 | * Break/Watch point support - there is an implementation for system |
| 219 | * and user mode. |
| 220 | */ |
| 221 | int gdb_breakpoint_insert(CPUState *cs, GdbBreakpointType type, |
| 222 | vaddr addr, vaddr len); |
| 223 | int gdb_breakpoint_remove(CPUState *cs, GdbBreakpointType type, |
| 224 | vaddr addr, vaddr len); |
| 225 | void gdb_breakpoint_remove_all(CPUState *cs); |
| 226 | |
| 227 | /** |
| 228 | * gdb_target_memory_rw_debug() - handle debug access to memory |
| 229 | * @cs: CPUState |
| 230 | * @addr: nominal address, could be an entire physical address |
| 231 | * @buf: data |
| 232 | * @len: length of access |
| 233 | * @is_write: is it a write operation |
| 234 | * |
| 235 | * This function is specialised depending on the mode we are running |
| 236 | * in. For system guests we can switch the interpretation of the |
| 237 | * address to a physical address. |
| 238 | */ |
| 239 | int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr, |
| 240 | uint8_t *buf, int len, bool is_write); |
| 241 | |
| 242 | /** |
| 243 | * gdb_build_stop_packet() - craft the stop packet |
| 244 | * @buf: GString buffer for building the packet |
| 245 | * @cs: CPUState |
| 246 | * |
| 247 | * Craft the Stop/Reply packet when we halt. |
| 248 | */ |
| 249 | |
| 250 | void gdb_build_stop_packet(GString *buf, CPUState *cs); |
| 251 | |
| 252 | #endif /* GDBSTUB_INTERNALS_H */ |