]>
Commit | Line | Data |
---|---|---|
b4608c04 FB |
1 | /* |
2 | * gdb server stub | |
5fafdf24 | 3 | * |
3475187d | 4 | * Copyright (c) 2003-2005 Fabrice Bellard |
b4608c04 FB |
5 | * |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
b4608c04 | 18 | */ |
978efd6a | 19 | #include "config.h" |
56aebc89 | 20 | #include "qemu-common.h" |
1fddef4b FB |
21 | #ifdef CONFIG_USER_ONLY |
22 | #include <stdlib.h> | |
23 | #include <stdio.h> | |
24 | #include <stdarg.h> | |
25 | #include <string.h> | |
26 | #include <errno.h> | |
27 | #include <unistd.h> | |
978efd6a | 28 | #include <fcntl.h> |
1fddef4b FB |
29 | |
30 | #include "qemu.h" | |
31 | #else | |
83c9089e | 32 | #include "monitor/monitor.h" |
dccfcd0e | 33 | #include "sysemu/char.h" |
9c17d615 | 34 | #include "sysemu/sysemu.h" |
022c62cb | 35 | #include "exec/gdbstub.h" |
1fddef4b | 36 | #endif |
67b915a5 | 37 | |
56aebc89 PB |
38 | #define MAX_PACKET_LENGTH 4096 |
39 | ||
2b41f10e | 40 | #include "cpu.h" |
1de7afc9 | 41 | #include "qemu/sockets.h" |
9c17d615 | 42 | #include "sysemu/kvm.h" |
6ee77b16 | 43 | #include "qemu/bitops.h" |
ca587a8e | 44 | |
f3659eee AF |
45 | static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr, |
46 | uint8_t *buf, int len, bool is_write) | |
44520db1 | 47 | { |
f3659eee AF |
48 | CPUClass *cc = CPU_GET_CLASS(cpu); |
49 | ||
50 | if (cc->memory_rw_debug) { | |
51 | return cc->memory_rw_debug(cpu, addr, buf, len, is_write); | |
52 | } | |
53 | return cpu_memory_rw_debug(cpu, addr, buf, len, is_write); | |
44520db1 | 54 | } |
ca587a8e AJ |
55 | |
56 | enum { | |
57 | GDB_SIGNAL_0 = 0, | |
58 | GDB_SIGNAL_INT = 2, | |
425189a8 | 59 | GDB_SIGNAL_QUIT = 3, |
ca587a8e | 60 | GDB_SIGNAL_TRAP = 5, |
425189a8 JK |
61 | GDB_SIGNAL_ABRT = 6, |
62 | GDB_SIGNAL_ALRM = 14, | |
63 | GDB_SIGNAL_IO = 23, | |
64 | GDB_SIGNAL_XCPU = 24, | |
ca587a8e AJ |
65 | GDB_SIGNAL_UNKNOWN = 143 |
66 | }; | |
67 | ||
68 | #ifdef CONFIG_USER_ONLY | |
69 | ||
70 | /* Map target signal numbers to GDB protocol signal numbers and vice | |
71 | * versa. For user emulation's currently supported systems, we can | |
72 | * assume most signals are defined. | |
73 | */ | |
74 | ||
75 | static int gdb_signal_table[] = { | |
76 | 0, | |
77 | TARGET_SIGHUP, | |
78 | TARGET_SIGINT, | |
79 | TARGET_SIGQUIT, | |
80 | TARGET_SIGILL, | |
81 | TARGET_SIGTRAP, | |
82 | TARGET_SIGABRT, | |
83 | -1, /* SIGEMT */ | |
84 | TARGET_SIGFPE, | |
85 | TARGET_SIGKILL, | |
86 | TARGET_SIGBUS, | |
87 | TARGET_SIGSEGV, | |
88 | TARGET_SIGSYS, | |
89 | TARGET_SIGPIPE, | |
90 | TARGET_SIGALRM, | |
91 | TARGET_SIGTERM, | |
92 | TARGET_SIGURG, | |
93 | TARGET_SIGSTOP, | |
94 | TARGET_SIGTSTP, | |
95 | TARGET_SIGCONT, | |
96 | TARGET_SIGCHLD, | |
97 | TARGET_SIGTTIN, | |
98 | TARGET_SIGTTOU, | |
99 | TARGET_SIGIO, | |
100 | TARGET_SIGXCPU, | |
101 | TARGET_SIGXFSZ, | |
102 | TARGET_SIGVTALRM, | |
103 | TARGET_SIGPROF, | |
104 | TARGET_SIGWINCH, | |
105 | -1, /* SIGLOST */ | |
106 | TARGET_SIGUSR1, | |
107 | TARGET_SIGUSR2, | |
c72d5bf8 | 108 | #ifdef TARGET_SIGPWR |
ca587a8e | 109 | TARGET_SIGPWR, |
c72d5bf8 BS |
110 | #else |
111 | -1, | |
112 | #endif | |
ca587a8e AJ |
113 | -1, /* SIGPOLL */ |
114 | -1, | |
115 | -1, | |
116 | -1, | |
117 | -1, | |
118 | -1, | |
119 | -1, | |
120 | -1, | |
121 | -1, | |
122 | -1, | |
123 | -1, | |
124 | -1, | |
c72d5bf8 | 125 | #ifdef __SIGRTMIN |
ca587a8e AJ |
126 | __SIGRTMIN + 1, |
127 | __SIGRTMIN + 2, | |
128 | __SIGRTMIN + 3, | |
129 | __SIGRTMIN + 4, | |
130 | __SIGRTMIN + 5, | |
131 | __SIGRTMIN + 6, | |
132 | __SIGRTMIN + 7, | |
133 | __SIGRTMIN + 8, | |
134 | __SIGRTMIN + 9, | |
135 | __SIGRTMIN + 10, | |
136 | __SIGRTMIN + 11, | |
137 | __SIGRTMIN + 12, | |
138 | __SIGRTMIN + 13, | |
139 | __SIGRTMIN + 14, | |
140 | __SIGRTMIN + 15, | |
141 | __SIGRTMIN + 16, | |
142 | __SIGRTMIN + 17, | |
143 | __SIGRTMIN + 18, | |
144 | __SIGRTMIN + 19, | |
145 | __SIGRTMIN + 20, | |
146 | __SIGRTMIN + 21, | |
147 | __SIGRTMIN + 22, | |
148 | __SIGRTMIN + 23, | |
149 | __SIGRTMIN + 24, | |
150 | __SIGRTMIN + 25, | |
151 | __SIGRTMIN + 26, | |
152 | __SIGRTMIN + 27, | |
153 | __SIGRTMIN + 28, | |
154 | __SIGRTMIN + 29, | |
155 | __SIGRTMIN + 30, | |
156 | __SIGRTMIN + 31, | |
157 | -1, /* SIGCANCEL */ | |
158 | __SIGRTMIN, | |
159 | __SIGRTMIN + 32, | |
160 | __SIGRTMIN + 33, | |
161 | __SIGRTMIN + 34, | |
162 | __SIGRTMIN + 35, | |
163 | __SIGRTMIN + 36, | |
164 | __SIGRTMIN + 37, | |
165 | __SIGRTMIN + 38, | |
166 | __SIGRTMIN + 39, | |
167 | __SIGRTMIN + 40, | |
168 | __SIGRTMIN + 41, | |
169 | __SIGRTMIN + 42, | |
170 | __SIGRTMIN + 43, | |
171 | __SIGRTMIN + 44, | |
172 | __SIGRTMIN + 45, | |
173 | __SIGRTMIN + 46, | |
174 | __SIGRTMIN + 47, | |
175 | __SIGRTMIN + 48, | |
176 | __SIGRTMIN + 49, | |
177 | __SIGRTMIN + 50, | |
178 | __SIGRTMIN + 51, | |
179 | __SIGRTMIN + 52, | |
180 | __SIGRTMIN + 53, | |
181 | __SIGRTMIN + 54, | |
182 | __SIGRTMIN + 55, | |
183 | __SIGRTMIN + 56, | |
184 | __SIGRTMIN + 57, | |
185 | __SIGRTMIN + 58, | |
186 | __SIGRTMIN + 59, | |
187 | __SIGRTMIN + 60, | |
188 | __SIGRTMIN + 61, | |
189 | __SIGRTMIN + 62, | |
190 | __SIGRTMIN + 63, | |
191 | __SIGRTMIN + 64, | |
192 | __SIGRTMIN + 65, | |
193 | __SIGRTMIN + 66, | |
194 | __SIGRTMIN + 67, | |
195 | __SIGRTMIN + 68, | |
196 | __SIGRTMIN + 69, | |
197 | __SIGRTMIN + 70, | |
198 | __SIGRTMIN + 71, | |
199 | __SIGRTMIN + 72, | |
200 | __SIGRTMIN + 73, | |
201 | __SIGRTMIN + 74, | |
202 | __SIGRTMIN + 75, | |
203 | __SIGRTMIN + 76, | |
204 | __SIGRTMIN + 77, | |
205 | __SIGRTMIN + 78, | |
206 | __SIGRTMIN + 79, | |
207 | __SIGRTMIN + 80, | |
208 | __SIGRTMIN + 81, | |
209 | __SIGRTMIN + 82, | |
210 | __SIGRTMIN + 83, | |
211 | __SIGRTMIN + 84, | |
212 | __SIGRTMIN + 85, | |
213 | __SIGRTMIN + 86, | |
214 | __SIGRTMIN + 87, | |
215 | __SIGRTMIN + 88, | |
216 | __SIGRTMIN + 89, | |
217 | __SIGRTMIN + 90, | |
218 | __SIGRTMIN + 91, | |
219 | __SIGRTMIN + 92, | |
220 | __SIGRTMIN + 93, | |
221 | __SIGRTMIN + 94, | |
222 | __SIGRTMIN + 95, | |
223 | -1, /* SIGINFO */ | |
224 | -1, /* UNKNOWN */ | |
225 | -1, /* DEFAULT */ | |
226 | -1, | |
227 | -1, | |
228 | -1, | |
229 | -1, | |
230 | -1, | |
231 | -1 | |
c72d5bf8 | 232 | #endif |
ca587a8e | 233 | }; |
8f447cc7 | 234 | #else |
ca587a8e AJ |
235 | /* In system mode we only need SIGINT and SIGTRAP; other signals |
236 | are not yet supported. */ | |
237 | ||
238 | enum { | |
239 | TARGET_SIGINT = 2, | |
240 | TARGET_SIGTRAP = 5 | |
241 | }; | |
242 | ||
243 | static int gdb_signal_table[] = { | |
244 | -1, | |
245 | -1, | |
246 | TARGET_SIGINT, | |
247 | -1, | |
248 | -1, | |
249 | TARGET_SIGTRAP | |
250 | }; | |
251 | #endif | |
252 | ||
253 | #ifdef CONFIG_USER_ONLY | |
254 | static int target_signal_to_gdb (int sig) | |
255 | { | |
256 | int i; | |
257 | for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++) | |
258 | if (gdb_signal_table[i] == sig) | |
259 | return i; | |
260 | return GDB_SIGNAL_UNKNOWN; | |
261 | } | |
8f447cc7 | 262 | #endif |
b4608c04 | 263 | |
ca587a8e AJ |
264 | static int gdb_signal_to_target (int sig) |
265 | { | |
266 | if (sig < ARRAY_SIZE (gdb_signal_table)) | |
267 | return gdb_signal_table[sig]; | |
268 | else | |
269 | return -1; | |
270 | } | |
271 | ||
4abe615b | 272 | //#define DEBUG_GDB |
b4608c04 | 273 | |
56aebc89 PB |
274 | typedef struct GDBRegisterState { |
275 | int base_reg; | |
276 | int num_regs; | |
277 | gdb_reg_cb get_reg; | |
278 | gdb_reg_cb set_reg; | |
279 | const char *xml; | |
280 | struct GDBRegisterState *next; | |
281 | } GDBRegisterState; | |
282 | ||
858693c6 | 283 | enum RSState { |
36556b20 | 284 | RS_INACTIVE, |
858693c6 FB |
285 | RS_IDLE, |
286 | RS_GETLINE, | |
287 | RS_CHKSUM1, | |
288 | RS_CHKSUM2, | |
289 | }; | |
858693c6 | 290 | typedef struct GDBState { |
2e0f2cfb AF |
291 | CPUState *c_cpu; /* current CPU for step/continue ops */ |
292 | CPUState *g_cpu; /* current CPU for other ops */ | |
52f34623 | 293 | CPUState *query_cpu; /* for q{f|s}ThreadInfo */ |
41625033 | 294 | enum RSState state; /* parsing state */ |
56aebc89 | 295 | char line_buf[MAX_PACKET_LENGTH]; |
858693c6 FB |
296 | int line_buf_index; |
297 | int line_csum; | |
56aebc89 | 298 | uint8_t last_packet[MAX_PACKET_LENGTH + 4]; |
4046d913 | 299 | int last_packet_len; |
1f487ee9 | 300 | int signal; |
41625033 | 301 | #ifdef CONFIG_USER_ONLY |
4046d913 | 302 | int fd; |
41625033 | 303 | int running_state; |
4046d913 PB |
304 | #else |
305 | CharDriverState *chr; | |
8a34a0fb | 306 | CharDriverState *mon_chr; |
41625033 | 307 | #endif |
cdb432b2 MI |
308 | char syscall_buf[256]; |
309 | gdb_syscall_complete_cb current_syscall_cb; | |
858693c6 | 310 | } GDBState; |
b4608c04 | 311 | |
60897d36 EI |
312 | /* By default use no IRQs and no timers while single stepping so as to |
313 | * make single stepping like an ICE HW step. | |
314 | */ | |
315 | static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER; | |
316 | ||
880a7578 AL |
317 | static GDBState *gdbserver_state; |
318 | ||
56aebc89 PB |
319 | /* This is an ugly hack to cope with both new and old gdb. |
320 | If gdb sends qXfer:features:read then assume we're talking to a newish | |
321 | gdb that understands target descriptions. */ | |
322 | static int gdb_has_xml; | |
323 | ||
1fddef4b | 324 | #ifdef CONFIG_USER_ONLY |
4046d913 PB |
325 | /* XXX: This is not thread safe. Do we care? */ |
326 | static int gdbserver_fd = -1; | |
327 | ||
858693c6 | 328 | static int get_char(GDBState *s) |
b4608c04 FB |
329 | { |
330 | uint8_t ch; | |
331 | int ret; | |
332 | ||
333 | for(;;) { | |
00aa0040 | 334 | ret = qemu_recv(s->fd, &ch, 1, 0); |
b4608c04 | 335 | if (ret < 0) { |
1f487ee9 EI |
336 | if (errno == ECONNRESET) |
337 | s->fd = -1; | |
b4608c04 FB |
338 | if (errno != EINTR && errno != EAGAIN) |
339 | return -1; | |
340 | } else if (ret == 0) { | |
1f487ee9 EI |
341 | close(s->fd); |
342 | s->fd = -1; | |
b4608c04 FB |
343 | return -1; |
344 | } else { | |
345 | break; | |
346 | } | |
347 | } | |
348 | return ch; | |
349 | } | |
4046d913 | 350 | #endif |
b4608c04 | 351 | |
654efcf3 | 352 | static enum { |
a2d1ebaf PB |
353 | GDB_SYS_UNKNOWN, |
354 | GDB_SYS_ENABLED, | |
355 | GDB_SYS_DISABLED, | |
356 | } gdb_syscall_mode; | |
357 | ||
358 | /* If gdb is connected when the first semihosting syscall occurs then use | |
359 | remote gdb syscalls. Otherwise use native file IO. */ | |
360 | int use_gdb_syscalls(void) | |
361 | { | |
362 | if (gdb_syscall_mode == GDB_SYS_UNKNOWN) { | |
880a7578 AL |
363 | gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED |
364 | : GDB_SYS_DISABLED); | |
a2d1ebaf PB |
365 | } |
366 | return gdb_syscall_mode == GDB_SYS_ENABLED; | |
367 | } | |
368 | ||
ba70a624 EI |
369 | /* Resume execution. */ |
370 | static inline void gdb_continue(GDBState *s) | |
371 | { | |
372 | #ifdef CONFIG_USER_ONLY | |
373 | s->running_state = 1; | |
374 | #else | |
bc7d0e66 PB |
375 | if (runstate_check(RUN_STATE_GUEST_PANICKED)) { |
376 | runstate_set(RUN_STATE_DEBUG); | |
377 | } | |
26ac7a31 | 378 | if (!runstate_needs_reset()) { |
87f25c12 PB |
379 | vm_start(); |
380 | } | |
ba70a624 EI |
381 | #endif |
382 | } | |
383 | ||
858693c6 | 384 | static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
b4608c04 | 385 | { |
4046d913 | 386 | #ifdef CONFIG_USER_ONLY |
b4608c04 FB |
387 | int ret; |
388 | ||
389 | while (len > 0) { | |
8f447cc7 | 390 | ret = send(s->fd, buf, len, 0); |
b4608c04 FB |
391 | if (ret < 0) { |
392 | if (errno != EINTR && errno != EAGAIN) | |
393 | return; | |
394 | } else { | |
395 | buf += ret; | |
396 | len -= ret; | |
397 | } | |
398 | } | |
4046d913 | 399 | #else |
2cc6e0a1 | 400 | qemu_chr_fe_write(s->chr, buf, len); |
4046d913 | 401 | #endif |
b4608c04 FB |
402 | } |
403 | ||
404 | static inline int fromhex(int v) | |
405 | { | |
406 | if (v >= '0' && v <= '9') | |
407 | return v - '0'; | |
408 | else if (v >= 'A' && v <= 'F') | |
409 | return v - 'A' + 10; | |
410 | else if (v >= 'a' && v <= 'f') | |
411 | return v - 'a' + 10; | |
412 | else | |
413 | return 0; | |
414 | } | |
415 | ||
416 | static inline int tohex(int v) | |
417 | { | |
418 | if (v < 10) | |
419 | return v + '0'; | |
420 | else | |
421 | return v - 10 + 'a'; | |
422 | } | |
423 | ||
424 | static void memtohex(char *buf, const uint8_t *mem, int len) | |
425 | { | |
426 | int i, c; | |
427 | char *q; | |
428 | q = buf; | |
429 | for(i = 0; i < len; i++) { | |
430 | c = mem[i]; | |
431 | *q++ = tohex(c >> 4); | |
432 | *q++ = tohex(c & 0xf); | |
433 | } | |
434 | *q = '\0'; | |
435 | } | |
436 | ||
437 | static void hextomem(uint8_t *mem, const char *buf, int len) | |
438 | { | |
439 | int i; | |
440 | ||
441 | for(i = 0; i < len; i++) { | |
442 | mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); | |
443 | buf += 2; | |
444 | } | |
445 | } | |
446 | ||
b4608c04 | 447 | /* return -1 if error, 0 if OK */ |
56aebc89 | 448 | static int put_packet_binary(GDBState *s, const char *buf, int len) |
b4608c04 | 449 | { |
56aebc89 | 450 | int csum, i; |
60fe76f3 | 451 | uint8_t *p; |
b4608c04 | 452 | |
b4608c04 | 453 | for(;;) { |
4046d913 PB |
454 | p = s->last_packet; |
455 | *(p++) = '$'; | |
4046d913 PB |
456 | memcpy(p, buf, len); |
457 | p += len; | |
b4608c04 FB |
458 | csum = 0; |
459 | for(i = 0; i < len; i++) { | |
460 | csum += buf[i]; | |
461 | } | |
4046d913 PB |
462 | *(p++) = '#'; |
463 | *(p++) = tohex((csum >> 4) & 0xf); | |
464 | *(p++) = tohex((csum) & 0xf); | |
b4608c04 | 465 | |
4046d913 | 466 | s->last_packet_len = p - s->last_packet; |
ffe8ab83 | 467 | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
b4608c04 | 468 | |
4046d913 PB |
469 | #ifdef CONFIG_USER_ONLY |
470 | i = get_char(s); | |
471 | if (i < 0) | |
b4608c04 | 472 | return -1; |
4046d913 | 473 | if (i == '+') |
b4608c04 | 474 | break; |
4046d913 PB |
475 | #else |
476 | break; | |
477 | #endif | |
b4608c04 FB |
478 | } |
479 | return 0; | |
480 | } | |
481 | ||
56aebc89 PB |
482 | /* return -1 if error, 0 if OK */ |
483 | static int put_packet(GDBState *s, const char *buf) | |
484 | { | |
485 | #ifdef DEBUG_GDB | |
486 | printf("reply='%s'\n", buf); | |
487 | #endif | |
79808573 | 488 | |
56aebc89 PB |
489 | return put_packet_binary(s, buf, strlen(buf)); |
490 | } | |
491 | ||
492 | /* The GDB remote protocol transfers values in target byte order. This means | |
493 | we can use the raw memory access routines to access the value buffer. | |
494 | Conveniently, these also handle the case where the buffer is mis-aligned. | |
495 | */ | |
496 | #define GET_REG8(val) do { \ | |
497 | stb_p(mem_buf, val); \ | |
498 | return 1; \ | |
499 | } while(0) | |
500 | #define GET_REG16(val) do { \ | |
501 | stw_p(mem_buf, val); \ | |
502 | return 2; \ | |
503 | } while(0) | |
504 | #define GET_REG32(val) do { \ | |
505 | stl_p(mem_buf, val); \ | |
506 | return 4; \ | |
507 | } while(0) | |
508 | #define GET_REG64(val) do { \ | |
509 | stq_p(mem_buf, val); \ | |
510 | return 8; \ | |
511 | } while(0) | |
512 | ||
513 | #if TARGET_LONG_BITS == 64 | |
514 | #define GET_REGL(val) GET_REG64(val) | |
515 | #define ldtul_p(addr) ldq_p(addr) | |
516 | #else | |
517 | #define GET_REGL(val) GET_REG32(val) | |
518 | #define ldtul_p(addr) ldl_p(addr) | |
79808573 FB |
519 | #endif |
520 | ||
56aebc89 | 521 | #if defined(TARGET_I386) |
5ad265ee AZ |
522 | |
523 | #ifdef TARGET_X86_64 | |
56aebc89 PB |
524 | static const int gpr_map[16] = { |
525 | R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP, | |
526 | 8, 9, 10, 11, 12, 13, 14, 15 | |
527 | }; | |
79808573 | 528 | #else |
5f30fa18 | 529 | #define gpr_map gpr_map32 |
79808573 | 530 | #endif |
5f30fa18 | 531 | static const int gpr_map32[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; |
79808573 | 532 | |
56aebc89 PB |
533 | #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25) |
534 | ||
b1631e7a JK |
535 | #define IDX_IP_REG CPU_NB_REGS |
536 | #define IDX_FLAGS_REG (IDX_IP_REG + 1) | |
537 | #define IDX_SEG_REGS (IDX_FLAGS_REG + 1) | |
538 | #define IDX_FP_REGS (IDX_SEG_REGS + 6) | |
539 | #define IDX_XMM_REGS (IDX_FP_REGS + 16) | |
540 | #define IDX_MXCSR_REG (IDX_XMM_REGS + CPU_NB_REGS) | |
541 | ||
f3840919 | 542 | static int cpu_gdb_read_register(CPUX86State *env, uint8_t *mem_buf, int n) |
79808573 | 543 | { |
56aebc89 | 544 | if (n < CPU_NB_REGS) { |
5f30fa18 JK |
545 | if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) { |
546 | GET_REG64(env->regs[gpr_map[n]]); | |
547 | } else if (n < CPU_NB_REGS32) { | |
548 | GET_REG32(env->regs[gpr_map32[n]]); | |
549 | } | |
b1631e7a | 550 | } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) { |
56aebc89 | 551 | #ifdef USE_X86LDOUBLE |
b1631e7a JK |
552 | /* FIXME: byteswap float values - after fixing fpregs layout. */ |
553 | memcpy(mem_buf, &env->fpregs[n - IDX_FP_REGS], 10); | |
79808573 | 554 | #else |
56aebc89 | 555 | memset(mem_buf, 0, 10); |
79808573 | 556 | #endif |
56aebc89 | 557 | return 10; |
b1631e7a JK |
558 | } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) { |
559 | n -= IDX_XMM_REGS; | |
5f30fa18 JK |
560 | if (n < CPU_NB_REGS32 || |
561 | (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) { | |
562 | stq_p(mem_buf, env->xmm_regs[n].XMM_Q(0)); | |
563 | stq_p(mem_buf + 8, env->xmm_regs[n].XMM_Q(1)); | |
564 | return 16; | |
565 | } | |
56aebc89 | 566 | } else { |
56aebc89 | 567 | switch (n) { |
5f30fa18 JK |
568 | case IDX_IP_REG: |
569 | if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) { | |
570 | GET_REG64(env->eip); | |
571 | } else { | |
572 | GET_REG32(env->eip); | |
573 | } | |
b1631e7a JK |
574 | case IDX_FLAGS_REG: GET_REG32(env->eflags); |
575 | ||
576 | case IDX_SEG_REGS: GET_REG32(env->segs[R_CS].selector); | |
577 | case IDX_SEG_REGS + 1: GET_REG32(env->segs[R_SS].selector); | |
578 | case IDX_SEG_REGS + 2: GET_REG32(env->segs[R_DS].selector); | |
579 | case IDX_SEG_REGS + 3: GET_REG32(env->segs[R_ES].selector); | |
580 | case IDX_SEG_REGS + 4: GET_REG32(env->segs[R_FS].selector); | |
581 | case IDX_SEG_REGS + 5: GET_REG32(env->segs[R_GS].selector); | |
582 | ||
583 | case IDX_FP_REGS + 8: GET_REG32(env->fpuc); | |
584 | case IDX_FP_REGS + 9: GET_REG32((env->fpus & ~0x3800) | | |
585 | (env->fpstt & 0x7) << 11); | |
586 | case IDX_FP_REGS + 10: GET_REG32(0); /* ftag */ | |
587 | case IDX_FP_REGS + 11: GET_REG32(0); /* fiseg */ | |
588 | case IDX_FP_REGS + 12: GET_REG32(0); /* fioff */ | |
589 | case IDX_FP_REGS + 13: GET_REG32(0); /* foseg */ | |
590 | case IDX_FP_REGS + 14: GET_REG32(0); /* fooff */ | |
591 | case IDX_FP_REGS + 15: GET_REG32(0); /* fop */ | |
592 | ||
593 | case IDX_MXCSR_REG: GET_REG32(env->mxcsr); | |
56aebc89 | 594 | } |
79808573 | 595 | } |
56aebc89 | 596 | return 0; |
6da41eaf FB |
597 | } |
598 | ||
f3840919 | 599 | static int cpu_x86_gdb_load_seg(CPUX86State *env, int sreg, uint8_t *mem_buf) |
84273177 JK |
600 | { |
601 | uint16_t selector = ldl_p(mem_buf); | |
602 | ||
603 | if (selector != env->segs[sreg].selector) { | |
604 | #if defined(CONFIG_USER_ONLY) | |
605 | cpu_x86_load_seg(env, sreg, selector); | |
606 | #else | |
607 | unsigned int limit, flags; | |
608 | target_ulong base; | |
609 | ||
610 | if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) { | |
611 | base = selector << 4; | |
612 | limit = 0xffff; | |
613 | flags = 0; | |
614 | } else { | |
615 | if (!cpu_x86_get_descr_debug(env, selector, &base, &limit, &flags)) | |
616 | return 4; | |
617 | } | |
618 | cpu_x86_load_seg_cache(env, sreg, selector, base, limit, flags); | |
619 | #endif | |
620 | } | |
621 | return 4; | |
622 | } | |
623 | ||
f3840919 | 624 | static int cpu_gdb_write_register(CPUX86State *env, uint8_t *mem_buf, int n) |
6da41eaf | 625 | { |
56aebc89 | 626 | uint32_t tmp; |
6da41eaf | 627 | |
b1631e7a | 628 | if (n < CPU_NB_REGS) { |
5f30fa18 JK |
629 | if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) { |
630 | env->regs[gpr_map[n]] = ldtul_p(mem_buf); | |
631 | return sizeof(target_ulong); | |
632 | } else if (n < CPU_NB_REGS32) { | |
633 | n = gpr_map32[n]; | |
634 | env->regs[n] &= ~0xffffffffUL; | |
635 | env->regs[n] |= (uint32_t)ldl_p(mem_buf); | |
636 | return 4; | |
637 | } | |
b1631e7a | 638 | } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) { |
56aebc89 | 639 | #ifdef USE_X86LDOUBLE |
b1631e7a JK |
640 | /* FIXME: byteswap float values - after fixing fpregs layout. */ |
641 | memcpy(&env->fpregs[n - IDX_FP_REGS], mem_buf, 10); | |
79808573 | 642 | #endif |
56aebc89 | 643 | return 10; |
b1631e7a JK |
644 | } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) { |
645 | n -= IDX_XMM_REGS; | |
5f30fa18 JK |
646 | if (n < CPU_NB_REGS32 || |
647 | (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) { | |
648 | env->xmm_regs[n].XMM_Q(0) = ldq_p(mem_buf); | |
649 | env->xmm_regs[n].XMM_Q(1) = ldq_p(mem_buf + 8); | |
650 | return 16; | |
651 | } | |
56aebc89 | 652 | } else { |
b1631e7a JK |
653 | switch (n) { |
654 | case IDX_IP_REG: | |
5f30fa18 JK |
655 | if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) { |
656 | env->eip = ldq_p(mem_buf); | |
657 | return 8; | |
658 | } else { | |
659 | env->eip &= ~0xffffffffUL; | |
660 | env->eip |= (uint32_t)ldl_p(mem_buf); | |
661 | return 4; | |
662 | } | |
b1631e7a JK |
663 | case IDX_FLAGS_REG: |
664 | env->eflags = ldl_p(mem_buf); | |
665 | return 4; | |
666 | ||
84273177 JK |
667 | case IDX_SEG_REGS: return cpu_x86_gdb_load_seg(env, R_CS, mem_buf); |
668 | case IDX_SEG_REGS + 1: return cpu_x86_gdb_load_seg(env, R_SS, mem_buf); | |
669 | case IDX_SEG_REGS + 2: return cpu_x86_gdb_load_seg(env, R_DS, mem_buf); | |
670 | case IDX_SEG_REGS + 3: return cpu_x86_gdb_load_seg(env, R_ES, mem_buf); | |
671 | case IDX_SEG_REGS + 4: return cpu_x86_gdb_load_seg(env, R_FS, mem_buf); | |
672 | case IDX_SEG_REGS + 5: return cpu_x86_gdb_load_seg(env, R_GS, mem_buf); | |
b1631e7a JK |
673 | |
674 | case IDX_FP_REGS + 8: | |
675 | env->fpuc = ldl_p(mem_buf); | |
676 | return 4; | |
677 | case IDX_FP_REGS + 9: | |
678 | tmp = ldl_p(mem_buf); | |
679 | env->fpstt = (tmp >> 11) & 7; | |
680 | env->fpus = tmp & ~0x3800; | |
681 | return 4; | |
682 | case IDX_FP_REGS + 10: /* ftag */ return 4; | |
683 | case IDX_FP_REGS + 11: /* fiseg */ return 4; | |
684 | case IDX_FP_REGS + 12: /* fioff */ return 4; | |
685 | case IDX_FP_REGS + 13: /* foseg */ return 4; | |
686 | case IDX_FP_REGS + 14: /* fooff */ return 4; | |
687 | case IDX_FP_REGS + 15: /* fop */ return 4; | |
688 | ||
689 | case IDX_MXCSR_REG: | |
690 | env->mxcsr = ldl_p(mem_buf); | |
691 | return 4; | |
79808573 | 692 | } |
79808573 | 693 | } |
56aebc89 PB |
694 | /* Unrecognised register. */ |
695 | return 0; | |
6da41eaf FB |
696 | } |
697 | ||
9e62fd7f | 698 | #elif defined (TARGET_PPC) |
9e62fd7f | 699 | |
e571cb47 AJ |
700 | /* Old gdb always expects FP registers. Newer (xml-aware) gdb only |
701 | expects whatever the target description contains. Due to a | |
702 | historical mishap the FP registers appear in between core integer | |
703 | regs and PC, MSR, CR, and so forth. We hack round this by giving the | |
704 | FP regs zero size when talking to a newer gdb. */ | |
56aebc89 | 705 | #define NUM_CORE_REGS 71 |
e571cb47 AJ |
706 | #if defined (TARGET_PPC64) |
707 | #define GDB_CORE_XML "power64-core.xml" | |
708 | #else | |
709 | #define GDB_CORE_XML "power-core.xml" | |
710 | #endif | |
9e62fd7f | 711 | |
f3840919 | 712 | static int cpu_gdb_read_register(CPUPPCState *env, uint8_t *mem_buf, int n) |
9e62fd7f | 713 | { |
56aebc89 PB |
714 | if (n < 32) { |
715 | /* gprs */ | |
716 | GET_REGL(env->gpr[n]); | |
717 | } else if (n < 64) { | |
718 | /* fprs */ | |
e571cb47 AJ |
719 | if (gdb_has_xml) |
720 | return 0; | |
8d4acf9b | 721 | stfq_p(mem_buf, env->fpr[n-32]); |
56aebc89 PB |
722 | return 8; |
723 | } else { | |
724 | switch (n) { | |
725 | case 64: GET_REGL(env->nip); | |
726 | case 65: GET_REGL(env->msr); | |
727 | case 66: | |
728 | { | |
729 | uint32_t cr = 0; | |
730 | int i; | |
731 | for (i = 0; i < 8; i++) | |
732 | cr |= env->crf[i] << (32 - ((i + 1) * 4)); | |
733 | GET_REG32(cr); | |
734 | } | |
735 | case 67: GET_REGL(env->lr); | |
736 | case 68: GET_REGL(env->ctr); | |
3d7b417e | 737 | case 69: GET_REGL(env->xer); |
e571cb47 AJ |
738 | case 70: |
739 | { | |
740 | if (gdb_has_xml) | |
741 | return 0; | |
5a576fb3 | 742 | GET_REG32(env->fpscr); |
e571cb47 | 743 | } |
56aebc89 PB |
744 | } |
745 | } | |
746 | return 0; | |
747 | } | |
9e62fd7f | 748 | |
f3840919 | 749 | static int cpu_gdb_write_register(CPUPPCState *env, uint8_t *mem_buf, int n) |
56aebc89 PB |
750 | { |
751 | if (n < 32) { | |
752 | /* gprs */ | |
753 | env->gpr[n] = ldtul_p(mem_buf); | |
754 | return sizeof(target_ulong); | |
755 | } else if (n < 64) { | |
756 | /* fprs */ | |
e571cb47 AJ |
757 | if (gdb_has_xml) |
758 | return 0; | |
8d4acf9b | 759 | env->fpr[n-32] = ldfq_p(mem_buf); |
56aebc89 PB |
760 | return 8; |
761 | } else { | |
762 | switch (n) { | |
763 | case 64: | |
764 | env->nip = ldtul_p(mem_buf); | |
765 | return sizeof(target_ulong); | |
766 | case 65: | |
767 | ppc_store_msr(env, ldtul_p(mem_buf)); | |
768 | return sizeof(target_ulong); | |
769 | case 66: | |
770 | { | |
771 | uint32_t cr = ldl_p(mem_buf); | |
772 | int i; | |
773 | for (i = 0; i < 8; i++) | |
774 | env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF; | |
775 | return 4; | |
776 | } | |
777 | case 67: | |
778 | env->lr = ldtul_p(mem_buf); | |
779 | return sizeof(target_ulong); | |
780 | case 68: | |
781 | env->ctr = ldtul_p(mem_buf); | |
782 | return sizeof(target_ulong); | |
783 | case 69: | |
3d7b417e AJ |
784 | env->xer = ldtul_p(mem_buf); |
785 | return sizeof(target_ulong); | |
56aebc89 PB |
786 | case 70: |
787 | /* fpscr */ | |
e571cb47 AJ |
788 | if (gdb_has_xml) |
789 | return 0; | |
d6478bc7 FC |
790 | store_fpscr(env, ldtul_p(mem_buf), 0xffffffff); |
791 | return sizeof(target_ulong); | |
56aebc89 PB |
792 | } |
793 | } | |
794 | return 0; | |
e95c8d51 | 795 | } |
56aebc89 | 796 | |
e95c8d51 | 797 | #elif defined (TARGET_SPARC) |
56aebc89 PB |
798 | |
799 | #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) | |
800 | #define NUM_CORE_REGS 86 | |
96d19126 | 801 | #else |
5a377912 | 802 | #define NUM_CORE_REGS 72 |
96d19126 | 803 | #endif |
56aebc89 | 804 | |
96d19126 | 805 | #ifdef TARGET_ABI32 |
56aebc89 | 806 | #define GET_REGA(val) GET_REG32(val) |
96d19126 | 807 | #else |
56aebc89 | 808 | #define GET_REGA(val) GET_REGL(val) |
96d19126 | 809 | #endif |
e95c8d51 | 810 | |
f3840919 | 811 | static int cpu_gdb_read_register(CPUSPARCState *env, uint8_t *mem_buf, int n) |
56aebc89 PB |
812 | { |
813 | if (n < 8) { | |
814 | /* g0..g7 */ | |
815 | GET_REGA(env->gregs[n]); | |
e95c8d51 | 816 | } |
56aebc89 PB |
817 | if (n < 32) { |
818 | /* register window */ | |
819 | GET_REGA(env->regwptr[n - 8]); | |
e95c8d51 | 820 | } |
56aebc89 PB |
821 | #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64) |
822 | if (n < 64) { | |
823 | /* fprs */ | |
30038fd8 RH |
824 | if (n & 1) { |
825 | GET_REG32(env->fpr[(n - 32) / 2].l.lower); | |
826 | } else { | |
827 | GET_REG32(env->fpr[(n - 32) / 2].l.upper); | |
828 | } | |
e95c8d51 FB |
829 | } |
830 | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */ | |
56aebc89 PB |
831 | switch (n) { |
832 | case 64: GET_REGA(env->y); | |
5a834bb4 | 833 | case 65: GET_REGA(cpu_get_psr(env)); |
56aebc89 PB |
834 | case 66: GET_REGA(env->wim); |
835 | case 67: GET_REGA(env->tbr); | |
836 | case 68: GET_REGA(env->pc); | |
837 | case 69: GET_REGA(env->npc); | |
838 | case 70: GET_REGA(env->fsr); | |
839 | case 71: GET_REGA(0); /* csr */ | |
5a377912 | 840 | default: GET_REGA(0); |
56aebc89 | 841 | } |
3475187d | 842 | #else |
56aebc89 PB |
843 | if (n < 64) { |
844 | /* f0-f31 */ | |
30038fd8 RH |
845 | if (n & 1) { |
846 | GET_REG32(env->fpr[(n - 32) / 2].l.lower); | |
847 | } else { | |
848 | GET_REG32(env->fpr[(n - 32) / 2].l.upper); | |
849 | } | |
56aebc89 PB |
850 | } |
851 | if (n < 80) { | |
852 | /* f32-f62 (double width, even numbers only) */ | |
30038fd8 | 853 | GET_REG64(env->fpr[(n - 32) / 2].ll); |
3475187d | 854 | } |
56aebc89 PB |
855 | switch (n) { |
856 | case 80: GET_REGL(env->pc); | |
857 | case 81: GET_REGL(env->npc); | |
5a834bb4 BS |
858 | case 82: GET_REGL((cpu_get_ccr(env) << 32) | |
859 | ((env->asi & 0xff) << 24) | | |
860 | ((env->pstate & 0xfff) << 8) | | |
861 | cpu_get_cwp64(env)); | |
56aebc89 PB |
862 | case 83: GET_REGL(env->fsr); |
863 | case 84: GET_REGL(env->fprs); | |
864 | case 85: GET_REGL(env->y); | |
865 | } | |
3475187d | 866 | #endif |
56aebc89 | 867 | return 0; |
e95c8d51 FB |
868 | } |
869 | ||
f3840919 | 870 | static int cpu_gdb_write_register(CPUSPARCState *env, uint8_t *mem_buf, int n) |
e95c8d51 | 871 | { |
56aebc89 PB |
872 | #if defined(TARGET_ABI32) |
873 | abi_ulong tmp; | |
874 | ||
875 | tmp = ldl_p(mem_buf); | |
96d19126 | 876 | #else |
56aebc89 PB |
877 | target_ulong tmp; |
878 | ||
879 | tmp = ldtul_p(mem_buf); | |
96d19126 | 880 | #endif |
e95c8d51 | 881 | |
56aebc89 PB |
882 | if (n < 8) { |
883 | /* g0..g7 */ | |
884 | env->gregs[n] = tmp; | |
885 | } else if (n < 32) { | |
886 | /* register window */ | |
887 | env->regwptr[n - 8] = tmp; | |
e95c8d51 | 888 | } |
56aebc89 PB |
889 | #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64) |
890 | else if (n < 64) { | |
891 | /* fprs */ | |
30038fd8 RH |
892 | /* f0-f31 */ |
893 | if (n & 1) { | |
894 | env->fpr[(n - 32) / 2].l.lower = tmp; | |
895 | } else { | |
896 | env->fpr[(n - 32) / 2].l.upper = tmp; | |
897 | } | |
56aebc89 PB |
898 | } else { |
899 | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */ | |
900 | switch (n) { | |
901 | case 64: env->y = tmp; break; | |
5a834bb4 | 902 | case 65: cpu_put_psr(env, tmp); break; |
56aebc89 PB |
903 | case 66: env->wim = tmp; break; |
904 | case 67: env->tbr = tmp; break; | |
905 | case 68: env->pc = tmp; break; | |
906 | case 69: env->npc = tmp; break; | |
907 | case 70: env->fsr = tmp; break; | |
908 | default: return 0; | |
909 | } | |
e95c8d51 | 910 | } |
56aebc89 | 911 | return 4; |
3475187d | 912 | #else |
56aebc89 PB |
913 | else if (n < 64) { |
914 | /* f0-f31 */ | |
30038fd8 RH |
915 | tmp = ldl_p(mem_buf); |
916 | if (n & 1) { | |
917 | env->fpr[(n - 32) / 2].l.lower = tmp; | |
918 | } else { | |
919 | env->fpr[(n - 32) / 2].l.upper = tmp; | |
920 | } | |
56aebc89 PB |
921 | return 4; |
922 | } else if (n < 80) { | |
923 | /* f32-f62 (double width, even numbers only) */ | |
30038fd8 | 924 | env->fpr[(n - 32) / 2].ll = tmp; |
56aebc89 PB |
925 | } else { |
926 | switch (n) { | |
927 | case 80: env->pc = tmp; break; | |
928 | case 81: env->npc = tmp; break; | |
929 | case 82: | |
5a834bb4 | 930 | cpu_put_ccr(env, tmp >> 32); |
56aebc89 PB |
931 | env->asi = (tmp >> 24) & 0xff; |
932 | env->pstate = (tmp >> 8) & 0xfff; | |
5a834bb4 | 933 | cpu_put_cwp64(env, tmp & 0xff); |
56aebc89 PB |
934 | break; |
935 | case 83: env->fsr = tmp; break; | |
936 | case 84: env->fprs = tmp; break; | |
937 | case 85: env->y = tmp; break; | |
938 | default: return 0; | |
939 | } | |
17d996e1 | 940 | } |
56aebc89 | 941 | return 8; |
3475187d | 942 | #endif |
9e62fd7f | 943 | } |
1fddef4b | 944 | #elif defined (TARGET_ARM) |
6da41eaf | 945 | |
56aebc89 PB |
946 | /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect |
947 | whatever the target description contains. Due to a historical mishap | |
948 | the FPA registers appear in between core integer regs and the CPSR. | |
949 | We hack round this by giving the FPA regs zero size when talking to a | |
950 | newer gdb. */ | |
951 | #define NUM_CORE_REGS 26 | |
952 | #define GDB_CORE_XML "arm-core.xml" | |
e6e5906b | 953 | |
f3840919 | 954 | static int cpu_gdb_read_register(CPUARMState *env, uint8_t *mem_buf, int n) |
e6e5906b | 955 | { |
56aebc89 PB |
956 | if (n < 16) { |
957 | /* Core integer register. */ | |
958 | GET_REG32(env->regs[n]); | |
959 | } | |
960 | if (n < 24) { | |
961 | /* FPA registers. */ | |
962 | if (gdb_has_xml) | |
963 | return 0; | |
964 | memset(mem_buf, 0, 12); | |
965 | return 12; | |
966 | } | |
967 | switch (n) { | |
968 | case 24: | |
969 | /* FPA status register. */ | |
970 | if (gdb_has_xml) | |
971 | return 0; | |
972 | GET_REG32(0); | |
973 | case 25: | |
974 | /* CPSR */ | |
975 | GET_REG32(cpsr_read(env)); | |
976 | } | |
977 | /* Unknown register. */ | |
978 | return 0; | |
e6e5906b | 979 | } |
6f970bd9 | 980 | |
f3840919 | 981 | static int cpu_gdb_write_register(CPUARMState *env, uint8_t *mem_buf, int n) |
56aebc89 PB |
982 | { |
983 | uint32_t tmp; | |
6f970bd9 | 984 | |
56aebc89 | 985 | tmp = ldl_p(mem_buf); |
6f970bd9 | 986 | |
56aebc89 PB |
987 | /* Mask out low bit of PC to workaround gdb bugs. This will probably |
988 | cause problems if we ever implement the Jazelle DBX extensions. */ | |
989 | if (n == 15) | |
990 | tmp &= ~1; | |
6f970bd9 | 991 | |
56aebc89 PB |
992 | if (n < 16) { |
993 | /* Core integer register. */ | |
994 | env->regs[n] = tmp; | |
995 | return 4; | |
996 | } | |
997 | if (n < 24) { /* 16-23 */ | |
998 | /* FPA registers (ignored). */ | |
999 | if (gdb_has_xml) | |
1000 | return 0; | |
1001 | return 12; | |
1002 | } | |
1003 | switch (n) { | |
1004 | case 24: | |
1005 | /* FPA status register (ignored). */ | |
1006 | if (gdb_has_xml) | |
1007 | return 0; | |
1008 | return 4; | |
1009 | case 25: | |
1010 | /* CPSR */ | |
1011 | cpsr_write (env, tmp, 0xffffffff); | |
1012 | return 4; | |
1013 | } | |
1014 | /* Unknown register. */ | |
1015 | return 0; | |
1016 | } | |
6f970bd9 | 1017 | |
56aebc89 | 1018 | #elif defined (TARGET_M68K) |
6f970bd9 | 1019 | |
56aebc89 | 1020 | #define NUM_CORE_REGS 18 |
6f970bd9 | 1021 | |
56aebc89 | 1022 | #define GDB_CORE_XML "cf-core.xml" |
6f970bd9 | 1023 | |
f3840919 | 1024 | static int cpu_gdb_read_register(CPUM68KState *env, uint8_t *mem_buf, int n) |
56aebc89 PB |
1025 | { |
1026 | if (n < 8) { | |
1027 | /* D0-D7 */ | |
1028 | GET_REG32(env->dregs[n]); | |
1029 | } else if (n < 16) { | |
1030 | /* A0-A7 */ | |
1031 | GET_REG32(env->aregs[n - 8]); | |
1032 | } else { | |
1033 | switch (n) { | |
1034 | case 16: GET_REG32(env->sr); | |
1035 | case 17: GET_REG32(env->pc); | |
1036 | } | |
1037 | } | |
1038 | /* FP registers not included here because they vary between | |
1039 | ColdFire and m68k. Use XML bits for these. */ | |
1040 | return 0; | |
1041 | } | |
8e33c08c | 1042 | |
f3840919 | 1043 | static int cpu_gdb_write_register(CPUM68KState *env, uint8_t *mem_buf, int n) |
56aebc89 PB |
1044 | { |
1045 | uint32_t tmp; | |
8e33c08c | 1046 | |
56aebc89 | 1047 | tmp = ldl_p(mem_buf); |
8e33c08c | 1048 | |
56aebc89 PB |
1049 | if (n < 8) { |
1050 | /* D0-D7 */ | |
1051 | env->dregs[n] = tmp; | |
b3d6b959 | 1052 | } else if (n < 16) { |
56aebc89 PB |
1053 | /* A0-A7 */ |
1054 | env->aregs[n - 8] = tmp; | |
1055 | } else { | |
1056 | switch (n) { | |
1057 | case 16: env->sr = tmp; break; | |
1058 | case 17: env->pc = tmp; break; | |
1059 | default: return 0; | |
1060 | } | |
1061 | } | |
1062 | return 4; | |
1063 | } | |
1064 | #elif defined (TARGET_MIPS) | |
7ac256b8 | 1065 | |
56aebc89 | 1066 | #define NUM_CORE_REGS 73 |
7ac256b8 | 1067 | |
f3840919 | 1068 | static int cpu_gdb_read_register(CPUMIPSState *env, uint8_t *mem_buf, int n) |
56aebc89 PB |
1069 | { |
1070 | if (n < 32) { | |
1071 | GET_REGL(env->active_tc.gpr[n]); | |
1072 | } | |
1073 | if (env->CP0_Config1 & (1 << CP0C1_FP)) { | |
1074 | if (n >= 38 && n < 70) { | |
1075 | if (env->CP0_Status & (1 << CP0St_FR)) | |
1076 | GET_REGL(env->active_fpu.fpr[n - 38].d); | |
1077 | else | |
1078 | GET_REGL(env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX]); | |
1079 | } | |
1080 | switch (n) { | |
1081 | case 70: GET_REGL((int32_t)env->active_fpu.fcr31); | |
1082 | case 71: GET_REGL((int32_t)env->active_fpu.fcr0); | |
1083 | } | |
1084 | } | |
1085 | switch (n) { | |
1086 | case 32: GET_REGL((int32_t)env->CP0_Status); | |
1087 | case 33: GET_REGL(env->active_tc.LO[0]); | |
1088 | case 34: GET_REGL(env->active_tc.HI[0]); | |
1089 | case 35: GET_REGL(env->CP0_BadVAddr); | |
1090 | case 36: GET_REGL((int32_t)env->CP0_Cause); | |
ff1d1977 | 1091 | case 37: GET_REGL(env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16)); |
56aebc89 PB |
1092 | case 72: GET_REGL(0); /* fp */ |
1093 | case 89: GET_REGL((int32_t)env->CP0_PRid); | |
1094 | } | |
1095 | if (n >= 73 && n <= 88) { | |
1096 | /* 16 embedded regs. */ | |
1097 | GET_REGL(0); | |
1098 | } | |
6f970bd9 | 1099 | |
56aebc89 | 1100 | return 0; |
6f970bd9 FB |
1101 | } |
1102 | ||
8e33c08c TS |
1103 | /* convert MIPS rounding mode in FCR31 to IEEE library */ |
1104 | static unsigned int ieee_rm[] = | |
1105 | { | |
1106 | float_round_nearest_even, | |
1107 | float_round_to_zero, | |
1108 | float_round_up, | |
1109 | float_round_down | |
1110 | }; | |
1111 | #define RESTORE_ROUNDING_MODE \ | |
f01be154 | 1112 | set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status) |
8e33c08c | 1113 | |
f3840919 | 1114 | static int cpu_gdb_write_register(CPUMIPSState *env, uint8_t *mem_buf, int n) |
6f970bd9 | 1115 | { |
56aebc89 | 1116 | target_ulong tmp; |
6f970bd9 | 1117 | |
56aebc89 | 1118 | tmp = ldtul_p(mem_buf); |
6f970bd9 | 1119 | |
56aebc89 PB |
1120 | if (n < 32) { |
1121 | env->active_tc.gpr[n] = tmp; | |
1122 | return sizeof(target_ulong); | |
1123 | } | |
1124 | if (env->CP0_Config1 & (1 << CP0C1_FP) | |
1125 | && n >= 38 && n < 73) { | |
1126 | if (n < 70) { | |
7ac256b8 | 1127 | if (env->CP0_Status & (1 << CP0St_FR)) |
56aebc89 | 1128 | env->active_fpu.fpr[n - 38].d = tmp; |
7ac256b8 | 1129 | else |
56aebc89 PB |
1130 | env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX] = tmp; |
1131 | } | |
1132 | switch (n) { | |
1133 | case 70: | |
1134 | env->active_fpu.fcr31 = tmp & 0xFF83FFFF; | |
1135 | /* set rounding mode */ | |
1136 | RESTORE_ROUNDING_MODE; | |
56aebc89 PB |
1137 | break; |
1138 | case 71: env->active_fpu.fcr0 = tmp; break; | |
1139 | } | |
1140 | return sizeof(target_ulong); | |
1141 | } | |
1142 | switch (n) { | |
1143 | case 32: env->CP0_Status = tmp; break; | |
1144 | case 33: env->active_tc.LO[0] = tmp; break; | |
1145 | case 34: env->active_tc.HI[0] = tmp; break; | |
1146 | case 35: env->CP0_BadVAddr = tmp; break; | |
1147 | case 36: env->CP0_Cause = tmp; break; | |
ff1d1977 NF |
1148 | case 37: |
1149 | env->active_tc.PC = tmp & ~(target_ulong)1; | |
1150 | if (tmp & 1) { | |
1151 | env->hflags |= MIPS_HFLAG_M16; | |
1152 | } else { | |
1153 | env->hflags &= ~(MIPS_HFLAG_M16); | |
1154 | } | |
1155 | break; | |
56aebc89 PB |
1156 | case 72: /* fp, ignored */ break; |
1157 | default: | |
1158 | if (n > 89) | |
1159 | return 0; | |
1160 | /* Other registers are readonly. Ignore writes. */ | |
1161 | break; | |
1162 | } | |
1163 | ||
1164 | return sizeof(target_ulong); | |
6f970bd9 | 1165 | } |
fc04355b JL |
1166 | #elif defined(TARGET_OPENRISC) |
1167 | ||
1168 | #define NUM_CORE_REGS (32 + 3) | |
1169 | ||
1170 | static int cpu_gdb_read_register(CPUOpenRISCState *env, uint8_t *mem_buf, int n) | |
1171 | { | |
1172 | if (n < 32) { | |
1173 | GET_REG32(env->gpr[n]); | |
1174 | } else { | |
1175 | switch (n) { | |
1176 | case 32: /* PPC */ | |
1177 | GET_REG32(env->ppc); | |
1178 | break; | |
1179 | ||
1180 | case 33: /* NPC */ | |
1181 | GET_REG32(env->npc); | |
1182 | break; | |
1183 | ||
1184 | case 34: /* SR */ | |
1185 | GET_REG32(env->sr); | |
1186 | break; | |
1187 | ||
1188 | default: | |
1189 | break; | |
1190 | } | |
1191 | } | |
1192 | return 0; | |
1193 | } | |
1194 | ||
1195 | static int cpu_gdb_write_register(CPUOpenRISCState *env, | |
1196 | uint8_t *mem_buf, int n) | |
1197 | { | |
1198 | uint32_t tmp; | |
1199 | ||
1200 | if (n > NUM_CORE_REGS) { | |
1201 | return 0; | |
1202 | } | |
1203 | ||
1204 | tmp = ldl_p(mem_buf); | |
1205 | ||
1206 | if (n < 32) { | |
1207 | env->gpr[n] = tmp; | |
1208 | } else { | |
1209 | switch (n) { | |
1210 | case 32: /* PPC */ | |
1211 | env->ppc = tmp; | |
1212 | break; | |
1213 | ||
1214 | case 33: /* NPC */ | |
1215 | env->npc = tmp; | |
1216 | break; | |
1217 | ||
1218 | case 34: /* SR */ | |
1219 | env->sr = tmp; | |
1220 | break; | |
1221 | ||
1222 | default: | |
1223 | break; | |
1224 | } | |
1225 | } | |
1226 | return 4; | |
1227 | } | |
fdf9b3e8 | 1228 | #elif defined (TARGET_SH4) |
6ef99fc5 TS |
1229 | |
1230 | /* Hint: Use "set architecture sh4" in GDB to see fpu registers */ | |
56aebc89 PB |
1231 | /* FIXME: We should use XML for this. */ |
1232 | ||
1233 | #define NUM_CORE_REGS 59 | |
6ef99fc5 | 1234 | |
f3840919 | 1235 | static int cpu_gdb_read_register(CPUSH4State *env, uint8_t *mem_buf, int n) |
fdf9b3e8 | 1236 | { |
eca5c303 AJ |
1237 | switch (n) { |
1238 | case 0 ... 7: | |
56aebc89 PB |
1239 | if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) { |
1240 | GET_REGL(env->gregs[n + 16]); | |
1241 | } else { | |
1242 | GET_REGL(env->gregs[n]); | |
1243 | } | |
eca5c303 | 1244 | case 8 ... 15: |
e192a45c | 1245 | GET_REGL(env->gregs[n]); |
eca5c303 AJ |
1246 | case 16: |
1247 | GET_REGL(env->pc); | |
1248 | case 17: | |
1249 | GET_REGL(env->pr); | |
1250 | case 18: | |
1251 | GET_REGL(env->gbr); | |
1252 | case 19: | |
1253 | GET_REGL(env->vbr); | |
1254 | case 20: | |
1255 | GET_REGL(env->mach); | |
1256 | case 21: | |
1257 | GET_REGL(env->macl); | |
1258 | case 22: | |
1259 | GET_REGL(env->sr); | |
1260 | case 23: | |
1261 | GET_REGL(env->fpul); | |
1262 | case 24: | |
1263 | GET_REGL(env->fpscr); | |
1264 | case 25 ... 40: | |
1265 | if (env->fpscr & FPSCR_FR) { | |
1266 | stfl_p(mem_buf, env->fregs[n - 9]); | |
1267 | } else { | |
1268 | stfl_p(mem_buf, env->fregs[n - 25]); | |
1269 | } | |
1270 | return 4; | |
1271 | case 41: | |
1272 | GET_REGL(env->ssr); | |
1273 | case 42: | |
1274 | GET_REGL(env->spc); | |
1275 | case 43 ... 50: | |
1276 | GET_REGL(env->gregs[n - 43]); | |
1277 | case 51 ... 58: | |
1278 | GET_REGL(env->gregs[n - (51 - 16)]); | |
56aebc89 PB |
1279 | } |
1280 | ||
1281 | return 0; | |
fdf9b3e8 FB |
1282 | } |
1283 | ||
f3840919 | 1284 | static int cpu_gdb_write_register(CPUSH4State *env, uint8_t *mem_buf, int n) |
fdf9b3e8 | 1285 | { |
eca5c303 AJ |
1286 | switch (n) { |
1287 | case 0 ... 7: | |
56aebc89 | 1288 | if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) { |
eca5c303 | 1289 | env->gregs[n + 16] = ldl_p(mem_buf); |
56aebc89 | 1290 | } else { |
eca5c303 | 1291 | env->gregs[n] = ldl_p(mem_buf); |
56aebc89 | 1292 | } |
eca5c303 AJ |
1293 | break; |
1294 | case 8 ... 15: | |
1295 | env->gregs[n] = ldl_p(mem_buf); | |
1296 | break; | |
1297 | case 16: | |
1298 | env->pc = ldl_p(mem_buf); | |
1299 | break; | |
1300 | case 17: | |
1301 | env->pr = ldl_p(mem_buf); | |
1302 | break; | |
1303 | case 18: | |
1304 | env->gbr = ldl_p(mem_buf); | |
1305 | break; | |
1306 | case 19: | |
1307 | env->vbr = ldl_p(mem_buf); | |
1308 | break; | |
1309 | case 20: | |
1310 | env->mach = ldl_p(mem_buf); | |
1311 | break; | |
1312 | case 21: | |
1313 | env->macl = ldl_p(mem_buf); | |
1314 | break; | |
1315 | case 22: | |
1316 | env->sr = ldl_p(mem_buf); | |
1317 | break; | |
1318 | case 23: | |
1319 | env->fpul = ldl_p(mem_buf); | |
1320 | break; | |
1321 | case 24: | |
1322 | env->fpscr = ldl_p(mem_buf); | |
1323 | break; | |
1324 | case 25 ... 40: | |
1325 | if (env->fpscr & FPSCR_FR) { | |
1326 | env->fregs[n - 9] = ldfl_p(mem_buf); | |
1327 | } else { | |
1328 | env->fregs[n - 25] = ldfl_p(mem_buf); | |
1329 | } | |
1330 | break; | |
1331 | case 41: | |
1332 | env->ssr = ldl_p(mem_buf); | |
1333 | break; | |
1334 | case 42: | |
1335 | env->spc = ldl_p(mem_buf); | |
1336 | break; | |
1337 | case 43 ... 50: | |
1338 | env->gregs[n - 43] = ldl_p(mem_buf); | |
1339 | break; | |
1340 | case 51 ... 58: | |
1341 | env->gregs[n - (51 - 16)] = ldl_p(mem_buf); | |
1342 | break; | |
56aebc89 PB |
1343 | default: return 0; |
1344 | } | |
1345 | ||
1346 | return 4; | |
fdf9b3e8 | 1347 | } |
d74d6a99 EI |
1348 | #elif defined (TARGET_MICROBLAZE) |
1349 | ||
1350 | #define NUM_CORE_REGS (32 + 5) | |
1351 | ||
f3840919 | 1352 | static int cpu_gdb_read_register(CPUMBState *env, uint8_t *mem_buf, int n) |
d74d6a99 EI |
1353 | { |
1354 | if (n < 32) { | |
1355 | GET_REG32(env->regs[n]); | |
1356 | } else { | |
1357 | GET_REG32(env->sregs[n - 32]); | |
1358 | } | |
1359 | return 0; | |
1360 | } | |
1361 | ||
f3840919 | 1362 | static int cpu_gdb_write_register(CPUMBState *env, uint8_t *mem_buf, int n) |
d74d6a99 EI |
1363 | { |
1364 | uint32_t tmp; | |
1365 | ||
1366 | if (n > NUM_CORE_REGS) | |
1367 | return 0; | |
1368 | ||
1369 | tmp = ldl_p(mem_buf); | |
1370 | ||
1371 | if (n < 32) { | |
1372 | env->regs[n] = tmp; | |
1373 | } else { | |
1374 | env->sregs[n - 32] = tmp; | |
1375 | } | |
1376 | return 4; | |
1377 | } | |
f1ccf904 TS |
1378 | #elif defined (TARGET_CRIS) |
1379 | ||
56aebc89 PB |
1380 | #define NUM_CORE_REGS 49 |
1381 | ||
4a0b59fe | 1382 | static int |
f3840919 | 1383 | read_register_crisv10(CPUCRISState *env, uint8_t *mem_buf, int n) |
4a0b59fe EI |
1384 | { |
1385 | if (n < 15) { | |
1386 | GET_REG32(env->regs[n]); | |
1387 | } | |
1388 | ||
1389 | if (n == 15) { | |
1390 | GET_REG32(env->pc); | |
1391 | } | |
1392 | ||
1393 | if (n < 32) { | |
1394 | switch (n) { | |
1395 | case 16: | |
1396 | GET_REG8(env->pregs[n - 16]); | |
1397 | break; | |
1398 | case 17: | |
1399 | GET_REG8(env->pregs[n - 16]); | |
1400 | break; | |
1401 | case 20: | |
1402 | case 21: | |
1403 | GET_REG16(env->pregs[n - 16]); | |
1404 | break; | |
1405 | default: | |
1406 | if (n >= 23) { | |
1407 | GET_REG32(env->pregs[n - 16]); | |
1408 | } | |
1409 | break; | |
1410 | } | |
1411 | } | |
1412 | return 0; | |
1413 | } | |
1414 | ||
f3840919 | 1415 | static int cpu_gdb_read_register(CPUCRISState *env, uint8_t *mem_buf, int n) |
f1ccf904 | 1416 | { |
56aebc89 PB |
1417 | uint8_t srs; |
1418 | ||
4a0b59fe EI |
1419 | if (env->pregs[PR_VR] < 32) |
1420 | return read_register_crisv10(env, mem_buf, n); | |
1421 | ||
56aebc89 PB |
1422 | srs = env->pregs[PR_SRS]; |
1423 | if (n < 16) { | |
1424 | GET_REG32(env->regs[n]); | |
1425 | } | |
1426 | ||
1427 | if (n >= 21 && n < 32) { | |
1428 | GET_REG32(env->pregs[n - 16]); | |
1429 | } | |
1430 | if (n >= 33 && n < 49) { | |
1431 | GET_REG32(env->sregs[srs][n - 33]); | |
1432 | } | |
1433 | switch (n) { | |
1434 | case 16: GET_REG8(env->pregs[0]); | |
1435 | case 17: GET_REG8(env->pregs[1]); | |
1436 | case 18: GET_REG32(env->pregs[2]); | |
1437 | case 19: GET_REG8(srs); | |
1438 | case 20: GET_REG16(env->pregs[4]); | |
1439 | case 32: GET_REG32(env->pc); | |
1440 | } | |
1441 | ||
1442 | return 0; | |
f1ccf904 | 1443 | } |
56aebc89 | 1444 | |
f3840919 | 1445 | static int cpu_gdb_write_register(CPUCRISState *env, uint8_t *mem_buf, int n) |
f1ccf904 | 1446 | { |
56aebc89 PB |
1447 | uint32_t tmp; |
1448 | ||
1449 | if (n > 49) | |
1450 | return 0; | |
1451 | ||
1452 | tmp = ldl_p(mem_buf); | |
1453 | ||
1454 | if (n < 16) { | |
1455 | env->regs[n] = tmp; | |
1456 | } | |
1457 | ||
d7b6967a EI |
1458 | if (n >= 21 && n < 32) { |
1459 | env->pregs[n - 16] = tmp; | |
1460 | } | |
1461 | ||
1462 | /* FIXME: Should support function regs be writable? */ | |
56aebc89 PB |
1463 | switch (n) { |
1464 | case 16: return 1; | |
1465 | case 17: return 1; | |
d7b6967a | 1466 | case 18: env->pregs[PR_PID] = tmp; break; |
56aebc89 PB |
1467 | case 19: return 1; |
1468 | case 20: return 2; | |
1469 | case 32: env->pc = tmp; break; | |
1470 | } | |
1471 | ||
1472 | return 4; | |
f1ccf904 | 1473 | } |
19bf517b AJ |
1474 | #elif defined (TARGET_ALPHA) |
1475 | ||
7c5a90dd | 1476 | #define NUM_CORE_REGS 67 |
19bf517b | 1477 | |
f3840919 | 1478 | static int cpu_gdb_read_register(CPUAlphaState *env, uint8_t *mem_buf, int n) |
19bf517b | 1479 | { |
7c5a90dd RH |
1480 | uint64_t val; |
1481 | CPU_DoubleU d; | |
19bf517b | 1482 | |
7c5a90dd RH |
1483 | switch (n) { |
1484 | case 0 ... 30: | |
1485 | val = env->ir[n]; | |
1486 | break; | |
1487 | case 32 ... 62: | |
1488 | d.d = env->fir[n - 32]; | |
1489 | val = d.ll; | |
1490 | break; | |
1491 | case 63: | |
1492 | val = cpu_alpha_load_fpcr(env); | |
1493 | break; | |
1494 | case 64: | |
1495 | val = env->pc; | |
1496 | break; | |
1497 | case 66: | |
1498 | val = env->unique; | |
1499 | break; | |
1500 | case 31: | |
1501 | case 65: | |
1502 | /* 31 really is the zero register; 65 is unassigned in the | |
1503 | gdb protocol, but is still required to occupy 8 bytes. */ | |
1504 | val = 0; | |
1505 | break; | |
1506 | default: | |
1507 | return 0; | |
19bf517b | 1508 | } |
7c5a90dd | 1509 | GET_REGL(val); |
19bf517b AJ |
1510 | } |
1511 | ||
f3840919 | 1512 | static int cpu_gdb_write_register(CPUAlphaState *env, uint8_t *mem_buf, int n) |
19bf517b | 1513 | { |
7c5a90dd RH |
1514 | target_ulong tmp = ldtul_p(mem_buf); |
1515 | CPU_DoubleU d; | |
19bf517b | 1516 | |
7c5a90dd RH |
1517 | switch (n) { |
1518 | case 0 ... 30: | |
19bf517b | 1519 | env->ir[n] = tmp; |
7c5a90dd RH |
1520 | break; |
1521 | case 32 ... 62: | |
1522 | d.ll = tmp; | |
1523 | env->fir[n - 32] = d.d; | |
1524 | break; | |
1525 | case 63: | |
1526 | cpu_alpha_store_fpcr(env, tmp); | |
1527 | break; | |
1528 | case 64: | |
1529 | env->pc = tmp; | |
1530 | break; | |
1531 | case 66: | |
1532 | env->unique = tmp; | |
1533 | break; | |
1534 | case 31: | |
1535 | case 65: | |
1536 | /* 31 really is the zero register; 65 is unassigned in the | |
1537 | gdb protocol, but is still required to occupy 8 bytes. */ | |
1538 | break; | |
1539 | default: | |
1540 | return 0; | |
19bf517b | 1541 | } |
19bf517b AJ |
1542 | return 8; |
1543 | } | |
afcb0e45 AG |
1544 | #elif defined (TARGET_S390X) |
1545 | ||
6ee77b16 | 1546 | #define NUM_CORE_REGS S390_NUM_REGS |
afcb0e45 | 1547 | |
f3840919 | 1548 | static int cpu_gdb_read_register(CPUS390XState *env, uint8_t *mem_buf, int n) |
afcb0e45 | 1549 | { |
6ee77b16 RH |
1550 | uint64_t val; |
1551 | int cc_op; | |
1552 | ||
afcb0e45 | 1553 | switch (n) { |
6ee77b16 RH |
1554 | case S390_PSWM_REGNUM: |
1555 | cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst, env->cc_vr); | |
1556 | val = deposit64(env->psw.mask, 44, 2, cc_op); | |
1557 | GET_REGL(val); | |
1558 | break; | |
1559 | case S390_PSWA_REGNUM: | |
1560 | GET_REGL(env->psw.addr); | |
1561 | break; | |
1562 | case S390_R0_REGNUM ... S390_R15_REGNUM: | |
1563 | GET_REGL(env->regs[n-S390_R0_REGNUM]); | |
1564 | break; | |
1565 | case S390_A0_REGNUM ... S390_A15_REGNUM: | |
1566 | GET_REG32(env->aregs[n-S390_A0_REGNUM]); | |
1567 | break; | |
1568 | case S390_FPC_REGNUM: | |
1569 | GET_REG32(env->fpc); | |
1570 | break; | |
1571 | case S390_F0_REGNUM ... S390_F15_REGNUM: | |
1572 | GET_REG64(env->fregs[n-S390_F0_REGNUM].ll); | |
1573 | break; | |
afcb0e45 AG |
1574 | } |
1575 | ||
1576 | return 0; | |
1577 | } | |
1578 | ||
f3840919 | 1579 | static int cpu_gdb_write_register(CPUS390XState *env, uint8_t *mem_buf, int n) |
afcb0e45 AG |
1580 | { |
1581 | target_ulong tmpl; | |
1582 | uint32_t tmp32; | |
1583 | int r = 8; | |
1584 | tmpl = ldtul_p(mem_buf); | |
1585 | tmp32 = ldl_p(mem_buf); | |
1586 | ||
1587 | switch (n) { | |
6ee77b16 RH |
1588 | case S390_PSWM_REGNUM: |
1589 | env->psw.mask = tmpl; | |
1590 | env->cc_op = extract64(tmpl, 44, 2); | |
1591 | break; | |
1592 | case S390_PSWA_REGNUM: | |
1593 | env->psw.addr = tmpl; | |
1594 | break; | |
1595 | case S390_R0_REGNUM ... S390_R15_REGNUM: | |
1596 | env->regs[n-S390_R0_REGNUM] = tmpl; | |
1597 | break; | |
1598 | case S390_A0_REGNUM ... S390_A15_REGNUM: | |
1599 | env->aregs[n-S390_A0_REGNUM] = tmp32; | |
1600 | r = 4; | |
1601 | break; | |
1602 | case S390_FPC_REGNUM: | |
1603 | env->fpc = tmp32; | |
1604 | r = 4; | |
1605 | break; | |
1606 | case S390_F0_REGNUM ... S390_F15_REGNUM: | |
1607 | env->fregs[n-S390_F0_REGNUM].ll = tmpl; | |
1608 | break; | |
1609 | default: | |
1610 | return 0; | |
afcb0e45 | 1611 | } |
afcb0e45 AG |
1612 | return r; |
1613 | } | |
0c45d3d4 MW |
1614 | #elif defined (TARGET_LM32) |
1615 | ||
0d09e41a | 1616 | #include "hw/lm32/lm32_pic.h" |
0c45d3d4 MW |
1617 | #define NUM_CORE_REGS (32 + 7) |
1618 | ||
f3840919 | 1619 | static int cpu_gdb_read_register(CPULM32State *env, uint8_t *mem_buf, int n) |
0c45d3d4 MW |
1620 | { |
1621 | if (n < 32) { | |
1622 | GET_REG32(env->regs[n]); | |
1623 | } else { | |
1624 | switch (n) { | |
1625 | case 32: | |
1626 | GET_REG32(env->pc); | |
1627 | break; | |
1628 | /* FIXME: put in right exception ID */ | |
1629 | case 33: | |
1630 | GET_REG32(0); | |
1631 | break; | |
1632 | case 34: | |
1633 | GET_REG32(env->eba); | |
1634 | break; | |
1635 | case 35: | |
1636 | GET_REG32(env->deba); | |
1637 | break; | |
1638 | case 36: | |
1639 | GET_REG32(env->ie); | |
1640 | break; | |
1641 | case 37: | |
1642 | GET_REG32(lm32_pic_get_im(env->pic_state)); | |
1643 | break; | |
1644 | case 38: | |
1645 | GET_REG32(lm32_pic_get_ip(env->pic_state)); | |
1646 | break; | |
1647 | } | |
1648 | } | |
1649 | return 0; | |
1650 | } | |
1651 | ||
f3840919 | 1652 | static int cpu_gdb_write_register(CPULM32State *env, uint8_t *mem_buf, int n) |
0c45d3d4 MW |
1653 | { |
1654 | uint32_t tmp; | |
1655 | ||
1656 | if (n > NUM_CORE_REGS) { | |
1657 | return 0; | |
1658 | } | |
1659 | ||
1660 | tmp = ldl_p(mem_buf); | |
1661 | ||
1662 | if (n < 32) { | |
1663 | env->regs[n] = tmp; | |
1664 | } else { | |
1665 | switch (n) { | |
1666 | case 32: | |
1667 | env->pc = tmp; | |
1668 | break; | |
1669 | case 34: | |
1670 | env->eba = tmp; | |
1671 | break; | |
1672 | case 35: | |
1673 | env->deba = tmp; | |
1674 | break; | |
1675 | case 36: | |
1676 | env->ie = tmp; | |
1677 | break; | |
1678 | case 37: | |
1679 | lm32_pic_set_im(env->pic_state, tmp); | |
1680 | break; | |
1681 | case 38: | |
1682 | lm32_pic_set_ip(env->pic_state, tmp); | |
1683 | break; | |
1684 | } | |
1685 | } | |
1686 | return 4; | |
1687 | } | |
ccfcaba6 MF |
1688 | #elif defined(TARGET_XTENSA) |
1689 | ||
1690 | /* Use num_core_regs to see only non-privileged registers in an unmodified gdb. | |
1691 | * Use num_regs to see all registers. gdb modification is required for that: | |
1692 | * reset bit 0 in the 'flags' field of the registers definitions in the | |
1693 | * gdb/xtensa-config.c inside gdb source tree or inside gdb overlay. | |
1694 | */ | |
1695 | #define NUM_CORE_REGS (env->config->gdb_regmap.num_regs) | |
1696 | #define num_g_regs NUM_CORE_REGS | |
1697 | ||
f3840919 | 1698 | static int cpu_gdb_read_register(CPUXtensaState *env, uint8_t *mem_buf, int n) |
ccfcaba6 MF |
1699 | { |
1700 | const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n; | |
1701 | ||
1702 | if (n < 0 || n >= env->config->gdb_regmap.num_regs) { | |
1703 | return 0; | |
1704 | } | |
1705 | ||
1706 | switch (reg->type) { | |
1707 | case 9: /*pc*/ | |
1708 | GET_REG32(env->pc); | |
1709 | break; | |
1710 | ||
1711 | case 1: /*ar*/ | |
1712 | xtensa_sync_phys_from_window(env); | |
1713 | GET_REG32(env->phys_regs[(reg->targno & 0xff) % env->config->nareg]); | |
1714 | break; | |
1715 | ||
1716 | case 2: /*SR*/ | |
1717 | GET_REG32(env->sregs[reg->targno & 0xff]); | |
1718 | break; | |
1719 | ||
1720 | case 3: /*UR*/ | |
1721 | GET_REG32(env->uregs[reg->targno & 0xff]); | |
1722 | break; | |
1723 | ||
dd519cbe MF |
1724 | case 4: /*f*/ |
1725 | GET_REG32(float32_val(env->fregs[reg->targno & 0x0f])); | |
1726 | break; | |
1727 | ||
ccfcaba6 MF |
1728 | case 8: /*a*/ |
1729 | GET_REG32(env->regs[reg->targno & 0x0f]); | |
1730 | break; | |
1731 | ||
1732 | default: | |
1733 | qemu_log("%s from reg %d of unsupported type %d\n", | |
1734 | __func__, n, reg->type); | |
1735 | return 0; | |
1736 | } | |
1737 | } | |
1738 | ||
f3840919 | 1739 | static int cpu_gdb_write_register(CPUXtensaState *env, uint8_t *mem_buf, int n) |
ccfcaba6 MF |
1740 | { |
1741 | uint32_t tmp; | |
1742 | const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n; | |
1743 | ||
1744 | if (n < 0 || n >= env->config->gdb_regmap.num_regs) { | |
1745 | return 0; | |
1746 | } | |
1747 | ||
1748 | tmp = ldl_p(mem_buf); | |
1749 | ||
1750 | switch (reg->type) { | |
1751 | case 9: /*pc*/ | |
1752 | env->pc = tmp; | |
1753 | break; | |
1754 | ||
1755 | case 1: /*ar*/ | |
1756 | env->phys_regs[(reg->targno & 0xff) % env->config->nareg] = tmp; | |
1757 | xtensa_sync_window_from_phys(env); | |
1758 | break; | |
1759 | ||
1760 | case 2: /*SR*/ | |
1761 | env->sregs[reg->targno & 0xff] = tmp; | |
1762 | break; | |
1763 | ||
1764 | case 3: /*UR*/ | |
1765 | env->uregs[reg->targno & 0xff] = tmp; | |
1766 | break; | |
1767 | ||
dd519cbe MF |
1768 | case 4: /*f*/ |
1769 | env->fregs[reg->targno & 0x0f] = make_float32(tmp); | |
1770 | break; | |
1771 | ||
ccfcaba6 MF |
1772 | case 8: /*a*/ |
1773 | env->regs[reg->targno & 0x0f] = tmp; | |
1774 | break; | |
1775 | ||
1776 | default: | |
1777 | qemu_log("%s to reg %d of unsupported type %d\n", | |
1778 | __func__, n, reg->type); | |
1779 | return 0; | |
1780 | } | |
1781 | ||
1782 | return 4; | |
1783 | } | |
56aebc89 PB |
1784 | #else |
1785 | ||
1786 | #define NUM_CORE_REGS 0 | |
1787 | ||
9349b4f9 | 1788 | static int cpu_gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int n) |
f1ccf904 | 1789 | { |
56aebc89 | 1790 | return 0; |
f1ccf904 TS |
1791 | } |
1792 | ||
9349b4f9 | 1793 | static int cpu_gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int n) |
f1ccf904 | 1794 | { |
56aebc89 PB |
1795 | return 0; |
1796 | } | |
f1ccf904 | 1797 | |
56aebc89 | 1798 | #endif |
f1ccf904 | 1799 | |
ccfcaba6 | 1800 | #if !defined(TARGET_XTENSA) |
56aebc89 | 1801 | static int num_g_regs = NUM_CORE_REGS; |
ccfcaba6 | 1802 | #endif |
f1ccf904 | 1803 | |
56aebc89 PB |
1804 | #ifdef GDB_CORE_XML |
1805 | /* Encode data using the encoding for 'x' packets. */ | |
1806 | static int memtox(char *buf, const char *mem, int len) | |
1807 | { | |
1808 | char *p = buf; | |
1809 | char c; | |
1810 | ||
1811 | while (len--) { | |
1812 | c = *(mem++); | |
1813 | switch (c) { | |
1814 | case '#': case '$': case '*': case '}': | |
1815 | *(p++) = '}'; | |
1816 | *(p++) = c ^ 0x20; | |
1817 | break; | |
1818 | default: | |
1819 | *(p++) = c; | |
1820 | break; | |
1821 | } | |
1822 | } | |
1823 | return p - buf; | |
1824 | } | |
f1ccf904 | 1825 | |
3faf778e | 1826 | static const char *get_feature_xml(const char *p, const char **newp) |
56aebc89 | 1827 | { |
56aebc89 PB |
1828 | size_t len; |
1829 | int i; | |
1830 | const char *name; | |
1831 | static char target_xml[1024]; | |
1832 | ||
1833 | len = 0; | |
1834 | while (p[len] && p[len] != ':') | |
1835 | len++; | |
1836 | *newp = p + len; | |
1837 | ||
1838 | name = NULL; | |
1839 | if (strncmp(p, "target.xml", len) == 0) { | |
1840 | /* Generate the XML description for this CPU. */ | |
1841 | if (!target_xml[0]) { | |
1842 | GDBRegisterState *r; | |
182735ef | 1843 | CPUArchState *env = first_cpu->env_ptr; |
56aebc89 | 1844 | |
5b3715bf BS |
1845 | snprintf(target_xml, sizeof(target_xml), |
1846 | "<?xml version=\"1.0\"?>" | |
1847 | "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">" | |
1848 | "<target>" | |
1849 | "<xi:include href=\"%s\"/>", | |
1850 | GDB_CORE_XML); | |
56aebc89 | 1851 | |
182735ef | 1852 | for (r = env->gdb_regs; r; r = r->next) { |
2dc766da BS |
1853 | pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\""); |
1854 | pstrcat(target_xml, sizeof(target_xml), r->xml); | |
1855 | pstrcat(target_xml, sizeof(target_xml), "\"/>"); | |
56aebc89 | 1856 | } |
2dc766da | 1857 | pstrcat(target_xml, sizeof(target_xml), "</target>"); |
56aebc89 PB |
1858 | } |
1859 | return target_xml; | |
1860 | } | |
1861 | for (i = 0; ; i++) { | |
1862 | name = xml_builtin[i][0]; | |
1863 | if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len)) | |
1864 | break; | |
1865 | } | |
1866 | return name ? xml_builtin[i][1] : NULL; | |
1867 | } | |
1868 | #endif | |
f1ccf904 | 1869 | |
385b9f0e | 1870 | static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
56aebc89 | 1871 | { |
385b9f0e | 1872 | CPUArchState *env = cpu->env_ptr; |
56aebc89 | 1873 | GDBRegisterState *r; |
f1ccf904 | 1874 | |
56aebc89 PB |
1875 | if (reg < NUM_CORE_REGS) |
1876 | return cpu_gdb_read_register(env, mem_buf, reg); | |
f1ccf904 | 1877 | |
56aebc89 PB |
1878 | for (r = env->gdb_regs; r; r = r->next) { |
1879 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { | |
1880 | return r->get_reg(env, mem_buf, reg - r->base_reg); | |
1881 | } | |
1882 | } | |
1883 | return 0; | |
f1ccf904 TS |
1884 | } |
1885 | ||
385b9f0e | 1886 | static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
f1ccf904 | 1887 | { |
385b9f0e | 1888 | CPUArchState *env = cpu->env_ptr; |
56aebc89 | 1889 | GDBRegisterState *r; |
f1ccf904 | 1890 | |
56aebc89 PB |
1891 | if (reg < NUM_CORE_REGS) |
1892 | return cpu_gdb_write_register(env, mem_buf, reg); | |
1893 | ||
1894 | for (r = env->gdb_regs; r; r = r->next) { | |
1895 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { | |
1896 | return r->set_reg(env, mem_buf, reg - r->base_reg); | |
1897 | } | |
1898 | } | |
6da41eaf FB |
1899 | return 0; |
1900 | } | |
1901 | ||
ccfcaba6 | 1902 | #if !defined(TARGET_XTENSA) |
56aebc89 PB |
1903 | /* Register a supplemental set of CPU registers. If g_pos is nonzero it |
1904 | specifies the first register number and these registers are included in | |
1905 | a standard "g" packet. Direction is relative to gdb, i.e. get_reg is | |
1906 | gdb reading a CPU register, and set_reg is gdb modifying a CPU register. | |
1907 | */ | |
1908 | ||
9349b4f9 | 1909 | void gdb_register_coprocessor(CPUArchState * env, |
56aebc89 PB |
1910 | gdb_reg_cb get_reg, gdb_reg_cb set_reg, |
1911 | int num_regs, const char *xml, int g_pos) | |
6da41eaf | 1912 | { |
56aebc89 PB |
1913 | GDBRegisterState *s; |
1914 | GDBRegisterState **p; | |
1915 | static int last_reg = NUM_CORE_REGS; | |
1916 | ||
56aebc89 PB |
1917 | p = &env->gdb_regs; |
1918 | while (*p) { | |
1919 | /* Check for duplicates. */ | |
1920 | if (strcmp((*p)->xml, xml) == 0) | |
1921 | return; | |
1922 | p = &(*p)->next; | |
1923 | } | |
9643c25f SW |
1924 | |
1925 | s = g_new0(GDBRegisterState, 1); | |
1926 | s->base_reg = last_reg; | |
1927 | s->num_regs = num_regs; | |
1928 | s->get_reg = get_reg; | |
1929 | s->set_reg = set_reg; | |
1930 | s->xml = xml; | |
1931 | ||
56aebc89 PB |
1932 | /* Add to end of list. */ |
1933 | last_reg += num_regs; | |
1934 | *p = s; | |
1935 | if (g_pos) { | |
1936 | if (g_pos != s->base_reg) { | |
1937 | fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n" | |
1938 | "Expected %d got %d\n", xml, g_pos, s->base_reg); | |
1939 | } else { | |
1940 | num_g_regs = last_reg; | |
1941 | } | |
1942 | } | |
6da41eaf | 1943 | } |
ccfcaba6 | 1944 | #endif |
6da41eaf | 1945 | |
a1d1bb31 AL |
1946 | #ifndef CONFIG_USER_ONLY |
1947 | static const int xlat_gdb_type[] = { | |
1948 | [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE, | |
1949 | [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ, | |
1950 | [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS, | |
1951 | }; | |
1952 | #endif | |
1953 | ||
880a7578 | 1954 | static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type) |
a1d1bb31 | 1955 | { |
182735ef | 1956 | CPUState *cpu; |
9349b4f9 | 1957 | CPUArchState *env; |
880a7578 AL |
1958 | int err = 0; |
1959 | ||
62278814 | 1960 | if (kvm_enabled()) { |
2e0f2cfb | 1961 | return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type); |
62278814 | 1962 | } |
e22a25c9 | 1963 | |
a1d1bb31 AL |
1964 | switch (type) { |
1965 | case GDB_BREAKPOINT_SW: | |
1966 | case GDB_BREAKPOINT_HW: | |
182735ef AF |
1967 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
1968 | env = cpu->env_ptr; | |
880a7578 AL |
1969 | err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL); |
1970 | if (err) | |
1971 | break; | |
1972 | } | |
1973 | return err; | |
a1d1bb31 AL |
1974 | #ifndef CONFIG_USER_ONLY |
1975 | case GDB_WATCHPOINT_WRITE: | |
1976 | case GDB_WATCHPOINT_READ: | |
1977 | case GDB_WATCHPOINT_ACCESS: | |
182735ef AF |
1978 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
1979 | env = cpu->env_ptr; | |
880a7578 AL |
1980 | err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type], |
1981 | NULL); | |
1982 | if (err) | |
1983 | break; | |
1984 | } | |
1985 | return err; | |
a1d1bb31 AL |
1986 | #endif |
1987 | default: | |
1988 | return -ENOSYS; | |
1989 | } | |
1990 | } | |
1991 | ||
880a7578 | 1992 | static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type) |
a1d1bb31 | 1993 | { |
182735ef | 1994 | CPUState *cpu; |
9349b4f9 | 1995 | CPUArchState *env; |
880a7578 AL |
1996 | int err = 0; |
1997 | ||
62278814 | 1998 | if (kvm_enabled()) { |
2e0f2cfb | 1999 | return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type); |
62278814 | 2000 | } |
e22a25c9 | 2001 | |
a1d1bb31 AL |
2002 | switch (type) { |
2003 | case GDB_BREAKPOINT_SW: | |
2004 | case GDB_BREAKPOINT_HW: | |
182735ef AF |
2005 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
2006 | env = cpu->env_ptr; | |
880a7578 AL |
2007 | err = cpu_breakpoint_remove(env, addr, BP_GDB); |
2008 | if (err) | |
2009 | break; | |
2010 | } | |
2011 | return err; | |
a1d1bb31 AL |
2012 | #ifndef CONFIG_USER_ONLY |
2013 | case GDB_WATCHPOINT_WRITE: | |
2014 | case GDB_WATCHPOINT_READ: | |
2015 | case GDB_WATCHPOINT_ACCESS: | |
182735ef AF |
2016 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
2017 | env = cpu->env_ptr; | |
880a7578 AL |
2018 | err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]); |
2019 | if (err) | |
2020 | break; | |
2021 | } | |
2022 | return err; | |
a1d1bb31 AL |
2023 | #endif |
2024 | default: | |
2025 | return -ENOSYS; | |
2026 | } | |
2027 | } | |
2028 | ||
880a7578 | 2029 | static void gdb_breakpoint_remove_all(void) |
a1d1bb31 | 2030 | { |
182735ef | 2031 | CPUState *cpu; |
9349b4f9 | 2032 | CPUArchState *env; |
880a7578 | 2033 | |
e22a25c9 | 2034 | if (kvm_enabled()) { |
2e0f2cfb | 2035 | kvm_remove_all_breakpoints(gdbserver_state->c_cpu); |
e22a25c9 AL |
2036 | return; |
2037 | } | |
2038 | ||
182735ef AF |
2039 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
2040 | env = cpu->env_ptr; | |
880a7578 | 2041 | cpu_breakpoint_remove_all(env, BP_GDB); |
a1d1bb31 | 2042 | #ifndef CONFIG_USER_ONLY |
880a7578 | 2043 | cpu_watchpoint_remove_all(env, BP_GDB); |
a1d1bb31 | 2044 | #endif |
880a7578 | 2045 | } |
a1d1bb31 AL |
2046 | } |
2047 | ||
fab9d284 AJ |
2048 | static void gdb_set_cpu_pc(GDBState *s, target_ulong pc) |
2049 | { | |
2e0f2cfb | 2050 | CPUState *cpu = s->c_cpu; |
f45748f1 AF |
2051 | CPUClass *cc = CPU_GET_CLASS(cpu); |
2052 | ||
2053 | cpu_synchronize_state(cpu); | |
2054 | if (cc->set_pc) { | |
2055 | cc->set_pc(cpu, pc); | |
ff1d1977 | 2056 | } |
fab9d284 AJ |
2057 | } |
2058 | ||
2e0f2cfb | 2059 | static CPUState *find_cpu(uint32_t thread_id) |
1e9fa730 | 2060 | { |
0d34282f | 2061 | CPUState *cpu; |
1e9fa730 | 2062 | |
182735ef | 2063 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
aa48dd93 | 2064 | if (cpu_index(cpu) == thread_id) { |
2e0f2cfb | 2065 | return cpu; |
aa48dd93 | 2066 | } |
1e9fa730 | 2067 | } |
aa48dd93 AF |
2068 | |
2069 | return NULL; | |
1e9fa730 NF |
2070 | } |
2071 | ||
880a7578 | 2072 | static int gdb_handle_packet(GDBState *s, const char *line_buf) |
b4608c04 | 2073 | { |
2e0f2cfb | 2074 | #ifdef TARGET_XTENSA |
9349b4f9 | 2075 | CPUArchState *env; |
2e0f2cfb AF |
2076 | #endif |
2077 | CPUState *cpu; | |
b4608c04 | 2078 | const char *p; |
1e9fa730 NF |
2079 | uint32_t thread; |
2080 | int ch, reg_size, type, res; | |
56aebc89 PB |
2081 | char buf[MAX_PACKET_LENGTH]; |
2082 | uint8_t mem_buf[MAX_PACKET_LENGTH]; | |
2083 | uint8_t *registers; | |
9d9754a3 | 2084 | target_ulong addr, len; |
3b46e624 | 2085 | |
858693c6 FB |
2086 | #ifdef DEBUG_GDB |
2087 | printf("command='%s'\n", line_buf); | |
2088 | #endif | |
2089 | p = line_buf; | |
2090 | ch = *p++; | |
2091 | switch(ch) { | |
2092 | case '?': | |
1fddef4b | 2093 | /* TODO: Make this return the correct value for user-mode. */ |
ca587a8e | 2094 | snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP, |
2e0f2cfb | 2095 | cpu_index(s->c_cpu)); |
858693c6 | 2096 | put_packet(s, buf); |
7d03f82f EI |
2097 | /* Remove all the breakpoints when this query is issued, |
2098 | * because gdb is doing and initial connect and the state | |
2099 | * should be cleaned up. | |
2100 | */ | |
880a7578 | 2101 | gdb_breakpoint_remove_all(); |
858693c6 FB |
2102 | break; |
2103 | case 'c': | |
2104 | if (*p != '\0') { | |
9d9754a3 | 2105 | addr = strtoull(p, (char **)&p, 16); |
fab9d284 | 2106 | gdb_set_cpu_pc(s, addr); |
858693c6 | 2107 | } |
ca587a8e | 2108 | s->signal = 0; |
ba70a624 | 2109 | gdb_continue(s); |
41625033 | 2110 | return RS_IDLE; |
1f487ee9 | 2111 | case 'C': |
ca587a8e AJ |
2112 | s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16)); |
2113 | if (s->signal == -1) | |
2114 | s->signal = 0; | |
1f487ee9 EI |
2115 | gdb_continue(s); |
2116 | return RS_IDLE; | |
dd32aa10 JK |
2117 | case 'v': |
2118 | if (strncmp(p, "Cont", 4) == 0) { | |
2119 | int res_signal, res_thread; | |
2120 | ||
2121 | p += 4; | |
2122 | if (*p == '?') { | |
2123 | put_packet(s, "vCont;c;C;s;S"); | |
2124 | break; | |
2125 | } | |
2126 | res = 0; | |
2127 | res_signal = 0; | |
2128 | res_thread = 0; | |
2129 | while (*p) { | |
2130 | int action, signal; | |
2131 | ||
2132 | if (*p++ != ';') { | |
2133 | res = 0; | |
2134 | break; | |
2135 | } | |
2136 | action = *p++; | |
2137 | signal = 0; | |
2138 | if (action == 'C' || action == 'S') { | |
2139 | signal = strtoul(p, (char **)&p, 16); | |
2140 | } else if (action != 'c' && action != 's') { | |
2141 | res = 0; | |
2142 | break; | |
2143 | } | |
2144 | thread = 0; | |
2145 | if (*p == ':') { | |
2146 | thread = strtoull(p+1, (char **)&p, 16); | |
2147 | } | |
2148 | action = tolower(action); | |
2149 | if (res == 0 || (res == 'c' && action == 's')) { | |
2150 | res = action; | |
2151 | res_signal = signal; | |
2152 | res_thread = thread; | |
2153 | } | |
2154 | } | |
2155 | if (res) { | |
2156 | if (res_thread != -1 && res_thread != 0) { | |
2e0f2cfb AF |
2157 | cpu = find_cpu(res_thread); |
2158 | if (cpu == NULL) { | |
dd32aa10 JK |
2159 | put_packet(s, "E22"); |
2160 | break; | |
2161 | } | |
2e0f2cfb | 2162 | s->c_cpu = cpu; |
dd32aa10 JK |
2163 | } |
2164 | if (res == 's') { | |
2e0f2cfb | 2165 | cpu_single_step(s->c_cpu, sstep_flags); |
dd32aa10 JK |
2166 | } |
2167 | s->signal = res_signal; | |
2168 | gdb_continue(s); | |
2169 | return RS_IDLE; | |
2170 | } | |
2171 | break; | |
2172 | } else { | |
2173 | goto unknown_command; | |
2174 | } | |
7d03f82f | 2175 | case 'k': |
00e94dbc | 2176 | #ifdef CONFIG_USER_ONLY |
7d03f82f EI |
2177 | /* Kill the target */ |
2178 | fprintf(stderr, "\nQEMU: Terminated via GDBstub\n"); | |
2179 | exit(0); | |
00e94dbc | 2180 | #endif |
7d03f82f EI |
2181 | case 'D': |
2182 | /* Detach packet */ | |
880a7578 | 2183 | gdb_breakpoint_remove_all(); |
7ea06da3 | 2184 | gdb_syscall_mode = GDB_SYS_DISABLED; |
7d03f82f EI |
2185 | gdb_continue(s); |
2186 | put_packet(s, "OK"); | |
2187 | break; | |
858693c6 FB |
2188 | case 's': |
2189 | if (*p != '\0') { | |
8fac5803 | 2190 | addr = strtoull(p, (char **)&p, 16); |
fab9d284 | 2191 | gdb_set_cpu_pc(s, addr); |
858693c6 | 2192 | } |
2e0f2cfb | 2193 | cpu_single_step(s->c_cpu, sstep_flags); |
ba70a624 | 2194 | gdb_continue(s); |
41625033 | 2195 | return RS_IDLE; |
a2d1ebaf PB |
2196 | case 'F': |
2197 | { | |
2198 | target_ulong ret; | |
2199 | target_ulong err; | |
2200 | ||
2201 | ret = strtoull(p, (char **)&p, 16); | |
2202 | if (*p == ',') { | |
2203 | p++; | |
2204 | err = strtoull(p, (char **)&p, 16); | |
2205 | } else { | |
2206 | err = 0; | |
2207 | } | |
2208 | if (*p == ',') | |
2209 | p++; | |
2210 | type = *p; | |
cdb432b2 | 2211 | if (s->current_syscall_cb) { |
2e0f2cfb | 2212 | s->current_syscall_cb(s->c_cpu, ret, err); |
cdb432b2 MI |
2213 | s->current_syscall_cb = NULL; |
2214 | } | |
a2d1ebaf PB |
2215 | if (type == 'C') { |
2216 | put_packet(s, "T02"); | |
2217 | } else { | |
ba70a624 | 2218 | gdb_continue(s); |
a2d1ebaf PB |
2219 | } |
2220 | } | |
2221 | break; | |
858693c6 | 2222 | case 'g': |
2e0f2cfb AF |
2223 | cpu_synchronize_state(s->g_cpu); |
2224 | #ifdef TARGET_XTENSA | |
2225 | env = s->g_cpu->env_ptr; | |
2226 | #endif | |
56aebc89 PB |
2227 | len = 0; |
2228 | for (addr = 0; addr < num_g_regs; addr++) { | |
2e0f2cfb | 2229 | reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr); |
56aebc89 PB |
2230 | len += reg_size; |
2231 | } | |
2232 | memtohex(buf, mem_buf, len); | |
858693c6 FB |
2233 | put_packet(s, buf); |
2234 | break; | |
2235 | case 'G': | |
2e0f2cfb AF |
2236 | cpu_synchronize_state(s->g_cpu); |
2237 | #ifdef TARGET_XTENSA | |
2238 | env = s->g_cpu->env_ptr; | |
2239 | #endif | |
56aebc89 | 2240 | registers = mem_buf; |
858693c6 FB |
2241 | len = strlen(p) / 2; |
2242 | hextomem((uint8_t *)registers, p, len); | |
56aebc89 | 2243 | for (addr = 0; addr < num_g_regs && len > 0; addr++) { |
2e0f2cfb | 2244 | reg_size = gdb_write_register(s->g_cpu, registers, addr); |
56aebc89 PB |
2245 | len -= reg_size; |
2246 | registers += reg_size; | |
2247 | } | |
858693c6 FB |
2248 | put_packet(s, "OK"); |
2249 | break; | |
2250 | case 'm': | |
9d9754a3 | 2251 | addr = strtoull(p, (char **)&p, 16); |
858693c6 FB |
2252 | if (*p == ',') |
2253 | p++; | |
9d9754a3 | 2254 | len = strtoull(p, NULL, 16); |
2e0f2cfb | 2255 | if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) { |
6f970bd9 FB |
2256 | put_packet (s, "E14"); |
2257 | } else { | |
2258 | memtohex(buf, mem_buf, len); | |
2259 | put_packet(s, buf); | |
2260 | } | |
858693c6 FB |
2261 | break; |
2262 | case 'M': | |
9d9754a3 | 2263 | addr = strtoull(p, (char **)&p, 16); |
858693c6 FB |
2264 | if (*p == ',') |
2265 | p++; | |
9d9754a3 | 2266 | len = strtoull(p, (char **)&p, 16); |
b328f873 | 2267 | if (*p == ':') |
858693c6 FB |
2268 | p++; |
2269 | hextomem(mem_buf, p, len); | |
2e0f2cfb | 2270 | if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, |
f3659eee | 2271 | true) != 0) { |
905f20b1 | 2272 | put_packet(s, "E14"); |
44520db1 | 2273 | } else { |
858693c6 | 2274 | put_packet(s, "OK"); |
44520db1 | 2275 | } |
858693c6 | 2276 | break; |
56aebc89 PB |
2277 | case 'p': |
2278 | /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable. | |
2279 | This works, but can be very slow. Anything new enough to | |
2280 | understand XML also knows how to use this properly. */ | |
2281 | if (!gdb_has_xml) | |
2282 | goto unknown_command; | |
2283 | addr = strtoull(p, (char **)&p, 16); | |
2e0f2cfb | 2284 | reg_size = gdb_read_register(s->g_cpu, mem_buf, addr); |
56aebc89 PB |
2285 | if (reg_size) { |
2286 | memtohex(buf, mem_buf, reg_size); | |
2287 | put_packet(s, buf); | |
2288 | } else { | |
2289 | put_packet(s, "E14"); | |
2290 | } | |
2291 | break; | |
2292 | case 'P': | |
2293 | if (!gdb_has_xml) | |
2294 | goto unknown_command; | |
2295 | addr = strtoull(p, (char **)&p, 16); | |
2296 | if (*p == '=') | |
2297 | p++; | |
2298 | reg_size = strlen(p) / 2; | |
2299 | hextomem(mem_buf, p, reg_size); | |
2e0f2cfb | 2300 | gdb_write_register(s->g_cpu, mem_buf, addr); |
56aebc89 PB |
2301 | put_packet(s, "OK"); |
2302 | break; | |
858693c6 | 2303 | case 'Z': |
858693c6 FB |
2304 | case 'z': |
2305 | type = strtoul(p, (char **)&p, 16); | |
2306 | if (*p == ',') | |
2307 | p++; | |
9d9754a3 | 2308 | addr = strtoull(p, (char **)&p, 16); |
858693c6 FB |
2309 | if (*p == ',') |
2310 | p++; | |
9d9754a3 | 2311 | len = strtoull(p, (char **)&p, 16); |
a1d1bb31 | 2312 | if (ch == 'Z') |
880a7578 | 2313 | res = gdb_breakpoint_insert(addr, len, type); |
a1d1bb31 | 2314 | else |
880a7578 | 2315 | res = gdb_breakpoint_remove(addr, len, type); |
a1d1bb31 AL |
2316 | if (res >= 0) |
2317 | put_packet(s, "OK"); | |
2318 | else if (res == -ENOSYS) | |
0f459d16 | 2319 | put_packet(s, ""); |
a1d1bb31 AL |
2320 | else |
2321 | put_packet(s, "E22"); | |
858693c6 | 2322 | break; |
880a7578 AL |
2323 | case 'H': |
2324 | type = *p++; | |
2325 | thread = strtoull(p, (char **)&p, 16); | |
2326 | if (thread == -1 || thread == 0) { | |
2327 | put_packet(s, "OK"); | |
2328 | break; | |
2329 | } | |
2e0f2cfb AF |
2330 | cpu = find_cpu(thread); |
2331 | if (cpu == NULL) { | |
880a7578 AL |
2332 | put_packet(s, "E22"); |
2333 | break; | |
2334 | } | |
2335 | switch (type) { | |
2336 | case 'c': | |
2e0f2cfb | 2337 | s->c_cpu = cpu; |
880a7578 AL |
2338 | put_packet(s, "OK"); |
2339 | break; | |
2340 | case 'g': | |
2e0f2cfb | 2341 | s->g_cpu = cpu; |
880a7578 AL |
2342 | put_packet(s, "OK"); |
2343 | break; | |
2344 | default: | |
2345 | put_packet(s, "E22"); | |
2346 | break; | |
2347 | } | |
2348 | break; | |
2349 | case 'T': | |
2350 | thread = strtoull(p, (char **)&p, 16); | |
2e0f2cfb | 2351 | cpu = find_cpu(thread); |
1e9fa730 | 2352 | |
2e0f2cfb | 2353 | if (cpu != NULL) { |
1e9fa730 NF |
2354 | put_packet(s, "OK"); |
2355 | } else { | |
880a7578 | 2356 | put_packet(s, "E22"); |
1e9fa730 | 2357 | } |
880a7578 | 2358 | break; |
978efd6a | 2359 | case 'q': |
60897d36 EI |
2360 | case 'Q': |
2361 | /* parse any 'q' packets here */ | |
2362 | if (!strcmp(p,"qemu.sstepbits")) { | |
2363 | /* Query Breakpoint bit definitions */ | |
363a37d5 BS |
2364 | snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", |
2365 | SSTEP_ENABLE, | |
2366 | SSTEP_NOIRQ, | |
2367 | SSTEP_NOTIMER); | |
60897d36 EI |
2368 | put_packet(s, buf); |
2369 | break; | |
2370 | } else if (strncmp(p,"qemu.sstep",10) == 0) { | |
2371 | /* Display or change the sstep_flags */ | |
2372 | p += 10; | |
2373 | if (*p != '=') { | |
2374 | /* Display current setting */ | |
363a37d5 | 2375 | snprintf(buf, sizeof(buf), "0x%x", sstep_flags); |
60897d36 EI |
2376 | put_packet(s, buf); |
2377 | break; | |
2378 | } | |
2379 | p++; | |
2380 | type = strtoul(p, (char **)&p, 16); | |
2381 | sstep_flags = type; | |
2382 | put_packet(s, "OK"); | |
2383 | break; | |
880a7578 AL |
2384 | } else if (strcmp(p,"C") == 0) { |
2385 | /* "Current thread" remains vague in the spec, so always return | |
2386 | * the first CPU (gdb returns the first thread). */ | |
2387 | put_packet(s, "QC1"); | |
2388 | break; | |
2389 | } else if (strcmp(p,"fThreadInfo") == 0) { | |
52f34623 | 2390 | s->query_cpu = first_cpu; |
880a7578 AL |
2391 | goto report_cpuinfo; |
2392 | } else if (strcmp(p,"sThreadInfo") == 0) { | |
2393 | report_cpuinfo: | |
2394 | if (s->query_cpu) { | |
52f34623 | 2395 | snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu)); |
880a7578 | 2396 | put_packet(s, buf); |
52f34623 | 2397 | s->query_cpu = s->query_cpu->next_cpu; |
880a7578 AL |
2398 | } else |
2399 | put_packet(s, "l"); | |
2400 | break; | |
2401 | } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) { | |
2402 | thread = strtoull(p+16, (char **)&p, 16); | |
2e0f2cfb AF |
2403 | cpu = find_cpu(thread); |
2404 | if (cpu != NULL) { | |
cb446eca | 2405 | cpu_synchronize_state(cpu); |
1e9fa730 | 2406 | len = snprintf((char *)mem_buf, sizeof(mem_buf), |
55e5c285 | 2407 | "CPU#%d [%s]", cpu->cpu_index, |
259186a7 | 2408 | cpu->halted ? "halted " : "running"); |
1e9fa730 NF |
2409 | memtohex(buf, mem_buf, len); |
2410 | put_packet(s, buf); | |
2411 | } | |
880a7578 | 2412 | break; |
60897d36 | 2413 | } |
0b8a988c | 2414 | #ifdef CONFIG_USER_ONLY |
60897d36 | 2415 | else if (strncmp(p, "Offsets", 7) == 0) { |
2e0f2cfb AF |
2416 | CPUArchState *env = s->c_cpu->env_ptr; |
2417 | TaskState *ts = env->opaque; | |
978efd6a | 2418 | |
363a37d5 BS |
2419 | snprintf(buf, sizeof(buf), |
2420 | "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx | |
2421 | ";Bss=" TARGET_ABI_FMT_lx, | |
2422 | ts->info->code_offset, | |
2423 | ts->info->data_offset, | |
2424 | ts->info->data_offset); | |
978efd6a PB |
2425 | put_packet(s, buf); |
2426 | break; | |
2427 | } | |
0b8a988c | 2428 | #else /* !CONFIG_USER_ONLY */ |
8a34a0fb AL |
2429 | else if (strncmp(p, "Rcmd,", 5) == 0) { |
2430 | int len = strlen(p + 5); | |
2431 | ||
2432 | if ((len % 2) != 0) { | |
2433 | put_packet(s, "E01"); | |
2434 | break; | |
2435 | } | |
2436 | hextomem(mem_buf, p + 5, len); | |
2437 | len = len / 2; | |
2438 | mem_buf[len++] = 0; | |
fa5efccb | 2439 | qemu_chr_be_write(s->mon_chr, mem_buf, len); |
8a34a0fb AL |
2440 | put_packet(s, "OK"); |
2441 | break; | |
2442 | } | |
0b8a988c | 2443 | #endif /* !CONFIG_USER_ONLY */ |
56aebc89 | 2444 | if (strncmp(p, "Supported", 9) == 0) { |
5b3715bf | 2445 | snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH); |
56aebc89 | 2446 | #ifdef GDB_CORE_XML |
2dc766da | 2447 | pstrcat(buf, sizeof(buf), ";qXfer:features:read+"); |
56aebc89 PB |
2448 | #endif |
2449 | put_packet(s, buf); | |
2450 | break; | |
2451 | } | |
2452 | #ifdef GDB_CORE_XML | |
2453 | if (strncmp(p, "Xfer:features:read:", 19) == 0) { | |
2454 | const char *xml; | |
2455 | target_ulong total_len; | |
2456 | ||
2457 | gdb_has_xml = 1; | |
2458 | p += 19; | |
880a7578 | 2459 | xml = get_feature_xml(p, &p); |
56aebc89 | 2460 | if (!xml) { |
5b3715bf | 2461 | snprintf(buf, sizeof(buf), "E00"); |
56aebc89 PB |
2462 | put_packet(s, buf); |
2463 | break; | |
2464 | } | |
2465 | ||
2466 | if (*p == ':') | |
2467 | p++; | |
2468 | addr = strtoul(p, (char **)&p, 16); | |
2469 | if (*p == ',') | |
2470 | p++; | |
2471 | len = strtoul(p, (char **)&p, 16); | |
2472 | ||
2473 | total_len = strlen(xml); | |
2474 | if (addr > total_len) { | |
5b3715bf | 2475 | snprintf(buf, sizeof(buf), "E00"); |
56aebc89 PB |
2476 | put_packet(s, buf); |
2477 | break; | |
2478 | } | |
2479 | if (len > (MAX_PACKET_LENGTH - 5) / 2) | |
2480 | len = (MAX_PACKET_LENGTH - 5) / 2; | |
2481 | if (len < total_len - addr) { | |
2482 | buf[0] = 'm'; | |
2483 | len = memtox(buf + 1, xml + addr, len); | |
2484 | } else { | |
2485 | buf[0] = 'l'; | |
2486 | len = memtox(buf + 1, xml + addr, total_len - addr); | |
2487 | } | |
2488 | put_packet_binary(s, buf, len + 1); | |
2489 | break; | |
2490 | } | |
2491 | #endif | |
2492 | /* Unrecognised 'q' command. */ | |
2493 | goto unknown_command; | |
2494 | ||
858693c6 | 2495 | default: |
56aebc89 | 2496 | unknown_command: |
858693c6 FB |
2497 | /* put empty packet */ |
2498 | buf[0] = '\0'; | |
2499 | put_packet(s, buf); | |
2500 | break; | |
2501 | } | |
2502 | return RS_IDLE; | |
2503 | } | |
2504 | ||
64f6b346 | 2505 | void gdb_set_stop_cpu(CPUState *cpu) |
880a7578 | 2506 | { |
2e0f2cfb AF |
2507 | gdbserver_state->c_cpu = cpu; |
2508 | gdbserver_state->g_cpu = cpu; | |
880a7578 AL |
2509 | } |
2510 | ||
1fddef4b | 2511 | #ifndef CONFIG_USER_ONLY |
1dfb4dd9 | 2512 | static void gdb_vm_state_change(void *opaque, int running, RunState state) |
858693c6 | 2513 | { |
880a7578 | 2514 | GDBState *s = gdbserver_state; |
2e0f2cfb AF |
2515 | CPUArchState *env = s->c_cpu->env_ptr; |
2516 | CPUState *cpu = s->c_cpu; | |
858693c6 | 2517 | char buf[256]; |
d6fc1b39 | 2518 | const char *type; |
858693c6 FB |
2519 | int ret; |
2520 | ||
cdb432b2 MI |
2521 | if (running || s->state == RS_INACTIVE) { |
2522 | return; | |
2523 | } | |
2524 | /* Is there a GDB syscall waiting to be sent? */ | |
2525 | if (s->current_syscall_cb) { | |
2526 | put_packet(s, s->syscall_buf); | |
a2d1ebaf | 2527 | return; |
e07bbac5 | 2528 | } |
1dfb4dd9 | 2529 | switch (state) { |
0461d5a6 | 2530 | case RUN_STATE_DEBUG: |
880a7578 AL |
2531 | if (env->watchpoint_hit) { |
2532 | switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) { | |
a1d1bb31 | 2533 | case BP_MEM_READ: |
d6fc1b39 AL |
2534 | type = "r"; |
2535 | break; | |
a1d1bb31 | 2536 | case BP_MEM_ACCESS: |
d6fc1b39 AL |
2537 | type = "a"; |
2538 | break; | |
2539 | default: | |
2540 | type = ""; | |
2541 | break; | |
2542 | } | |
880a7578 AL |
2543 | snprintf(buf, sizeof(buf), |
2544 | "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";", | |
0d34282f | 2545 | GDB_SIGNAL_TRAP, cpu_index(cpu), type, |
880a7578 | 2546 | env->watchpoint_hit->vaddr); |
880a7578 | 2547 | env->watchpoint_hit = NULL; |
425189a8 | 2548 | goto send_packet; |
6658ffb8 | 2549 | } |
425189a8 | 2550 | tb_flush(env); |
ca587a8e | 2551 | ret = GDB_SIGNAL_TRAP; |
425189a8 | 2552 | break; |
0461d5a6 | 2553 | case RUN_STATE_PAUSED: |
9781e040 | 2554 | ret = GDB_SIGNAL_INT; |
425189a8 | 2555 | break; |
0461d5a6 | 2556 | case RUN_STATE_SHUTDOWN: |
425189a8 JK |
2557 | ret = GDB_SIGNAL_QUIT; |
2558 | break; | |
0461d5a6 | 2559 | case RUN_STATE_IO_ERROR: |
425189a8 JK |
2560 | ret = GDB_SIGNAL_IO; |
2561 | break; | |
0461d5a6 | 2562 | case RUN_STATE_WATCHDOG: |
425189a8 JK |
2563 | ret = GDB_SIGNAL_ALRM; |
2564 | break; | |
0461d5a6 | 2565 | case RUN_STATE_INTERNAL_ERROR: |
425189a8 JK |
2566 | ret = GDB_SIGNAL_ABRT; |
2567 | break; | |
0461d5a6 LC |
2568 | case RUN_STATE_SAVE_VM: |
2569 | case RUN_STATE_RESTORE_VM: | |
425189a8 | 2570 | return; |
0461d5a6 | 2571 | case RUN_STATE_FINISH_MIGRATE: |
425189a8 JK |
2572 | ret = GDB_SIGNAL_XCPU; |
2573 | break; | |
2574 | default: | |
2575 | ret = GDB_SIGNAL_UNKNOWN; | |
2576 | break; | |
bbeb7b5c | 2577 | } |
0d34282f | 2578 | snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu)); |
425189a8 JK |
2579 | |
2580 | send_packet: | |
858693c6 | 2581 | put_packet(s, buf); |
425189a8 JK |
2582 | |
2583 | /* disable single step if it was enabled */ | |
3825b28f | 2584 | cpu_single_step(cpu, 0); |
858693c6 | 2585 | } |
1fddef4b | 2586 | #endif |
858693c6 | 2587 | |
a2d1ebaf PB |
2588 | /* Send a gdb syscall request. |
2589 | This accepts limited printf-style format specifiers, specifically: | |
a87295e8 PB |
2590 | %x - target_ulong argument printed in hex. |
2591 | %lx - 64-bit argument printed in hex. | |
2592 | %s - string pointer (target_ulong) and length (int) pair. */ | |
7ccfb2eb | 2593 | void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) |
a2d1ebaf PB |
2594 | { |
2595 | va_list va; | |
a2d1ebaf | 2596 | char *p; |
cdb432b2 | 2597 | char *p_end; |
a2d1ebaf | 2598 | target_ulong addr; |
a87295e8 | 2599 | uint64_t i64; |
a2d1ebaf PB |
2600 | GDBState *s; |
2601 | ||
880a7578 | 2602 | s = gdbserver_state; |
a2d1ebaf PB |
2603 | if (!s) |
2604 | return; | |
cdb432b2 | 2605 | s->current_syscall_cb = cb; |
a2d1ebaf | 2606 | #ifndef CONFIG_USER_ONLY |
0461d5a6 | 2607 | vm_stop(RUN_STATE_DEBUG); |
a2d1ebaf | 2608 | #endif |
a2d1ebaf | 2609 | va_start(va, fmt); |
cdb432b2 MI |
2610 | p = s->syscall_buf; |
2611 | p_end = &s->syscall_buf[sizeof(s->syscall_buf)]; | |
a2d1ebaf PB |
2612 | *(p++) = 'F'; |
2613 | while (*fmt) { | |
2614 | if (*fmt == '%') { | |
2615 | fmt++; | |
2616 | switch (*fmt++) { | |
2617 | case 'x': | |
2618 | addr = va_arg(va, target_ulong); | |
cdb432b2 | 2619 | p += snprintf(p, p_end - p, TARGET_FMT_lx, addr); |
a2d1ebaf | 2620 | break; |
a87295e8 PB |
2621 | case 'l': |
2622 | if (*(fmt++) != 'x') | |
2623 | goto bad_format; | |
2624 | i64 = va_arg(va, uint64_t); | |
cdb432b2 | 2625 | p += snprintf(p, p_end - p, "%" PRIx64, i64); |
a87295e8 | 2626 | break; |
a2d1ebaf PB |
2627 | case 's': |
2628 | addr = va_arg(va, target_ulong); | |
cdb432b2 | 2629 | p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x", |
363a37d5 | 2630 | addr, va_arg(va, int)); |
a2d1ebaf PB |
2631 | break; |
2632 | default: | |
a87295e8 | 2633 | bad_format: |
a2d1ebaf PB |
2634 | fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n", |
2635 | fmt - 1); | |
2636 | break; | |
2637 | } | |
2638 | } else { | |
2639 | *(p++) = *(fmt++); | |
2640 | } | |
2641 | } | |
8a93e02a | 2642 | *p = 0; |
a2d1ebaf | 2643 | va_end(va); |
a2d1ebaf | 2644 | #ifdef CONFIG_USER_ONLY |
cdb432b2 | 2645 | put_packet(s, s->syscall_buf); |
2e0f2cfb | 2646 | gdb_handlesig(s->c_cpu, 0); |
a2d1ebaf | 2647 | #else |
cdb432b2 MI |
2648 | /* In this case wait to send the syscall packet until notification that |
2649 | the CPU has stopped. This must be done because if the packet is sent | |
2650 | now the reply from the syscall request could be received while the CPU | |
2651 | is still in the running state, which can cause packets to be dropped | |
2652 | and state transition 'T' packets to be sent while the syscall is still | |
2653 | being processed. */ | |
2e0f2cfb | 2654 | cpu_exit(s->c_cpu); |
a2d1ebaf PB |
2655 | #endif |
2656 | } | |
2657 | ||
6a00d601 | 2658 | static void gdb_read_byte(GDBState *s, int ch) |
858693c6 FB |
2659 | { |
2660 | int i, csum; | |
60fe76f3 | 2661 | uint8_t reply; |
858693c6 | 2662 | |
1fddef4b | 2663 | #ifndef CONFIG_USER_ONLY |
4046d913 PB |
2664 | if (s->last_packet_len) { |
2665 | /* Waiting for a response to the last packet. If we see the start | |
2666 | of a new command then abandon the previous response. */ | |
2667 | if (ch == '-') { | |
2668 | #ifdef DEBUG_GDB | |
2669 | printf("Got NACK, retransmitting\n"); | |
2670 | #endif | |
ffe8ab83 | 2671 | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
4046d913 PB |
2672 | } |
2673 | #ifdef DEBUG_GDB | |
2674 | else if (ch == '+') | |
2675 | printf("Got ACK\n"); | |
2676 | else | |
2677 | printf("Got '%c' when expecting ACK/NACK\n", ch); | |
2678 | #endif | |
2679 | if (ch == '+' || ch == '$') | |
2680 | s->last_packet_len = 0; | |
2681 | if (ch != '$') | |
2682 | return; | |
2683 | } | |
1354869c | 2684 | if (runstate_is_running()) { |
858693c6 FB |
2685 | /* when the CPU is running, we cannot do anything except stop |
2686 | it when receiving a char */ | |
0461d5a6 | 2687 | vm_stop(RUN_STATE_PAUSED); |
5fafdf24 | 2688 | } else |
1fddef4b | 2689 | #endif |
41625033 | 2690 | { |
858693c6 FB |
2691 | switch(s->state) { |
2692 | case RS_IDLE: | |
2693 | if (ch == '$') { | |
2694 | s->line_buf_index = 0; | |
2695 | s->state = RS_GETLINE; | |
c33a346e | 2696 | } |
b4608c04 | 2697 | break; |
858693c6 FB |
2698 | case RS_GETLINE: |
2699 | if (ch == '#') { | |
2700 | s->state = RS_CHKSUM1; | |
2701 | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { | |
2702 | s->state = RS_IDLE; | |
4c3a88a2 | 2703 | } else { |
858693c6 | 2704 | s->line_buf[s->line_buf_index++] = ch; |
4c3a88a2 FB |
2705 | } |
2706 | break; | |
858693c6 FB |
2707 | case RS_CHKSUM1: |
2708 | s->line_buf[s->line_buf_index] = '\0'; | |
2709 | s->line_csum = fromhex(ch) << 4; | |
2710 | s->state = RS_CHKSUM2; | |
2711 | break; | |
2712 | case RS_CHKSUM2: | |
2713 | s->line_csum |= fromhex(ch); | |
2714 | csum = 0; | |
2715 | for(i = 0; i < s->line_buf_index; i++) { | |
2716 | csum += s->line_buf[i]; | |
2717 | } | |
2718 | if (s->line_csum != (csum & 0xff)) { | |
60fe76f3 TS |
2719 | reply = '-'; |
2720 | put_buffer(s, &reply, 1); | |
858693c6 | 2721 | s->state = RS_IDLE; |
4c3a88a2 | 2722 | } else { |
60fe76f3 TS |
2723 | reply = '+'; |
2724 | put_buffer(s, &reply, 1); | |
880a7578 | 2725 | s->state = gdb_handle_packet(s, s->line_buf); |
4c3a88a2 FB |
2726 | } |
2727 | break; | |
a2d1ebaf PB |
2728 | default: |
2729 | abort(); | |
858693c6 FB |
2730 | } |
2731 | } | |
2732 | } | |
2733 | ||
0e1c9c54 | 2734 | /* Tell the remote gdb that the process has exited. */ |
9349b4f9 | 2735 | void gdb_exit(CPUArchState *env, int code) |
0e1c9c54 PB |
2736 | { |
2737 | GDBState *s; | |
2738 | char buf[4]; | |
2739 | ||
2740 | s = gdbserver_state; | |
2741 | if (!s) { | |
2742 | return; | |
2743 | } | |
2744 | #ifdef CONFIG_USER_ONLY | |
2745 | if (gdbserver_fd < 0 || s->fd < 0) { | |
2746 | return; | |
2747 | } | |
2748 | #endif | |
2749 | ||
2750 | snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); | |
2751 | put_packet(s, buf); | |
e2af15b2 FC |
2752 | |
2753 | #ifndef CONFIG_USER_ONLY | |
2754 | if (s->chr) { | |
70f24fb6 | 2755 | qemu_chr_delete(s->chr); |
e2af15b2 FC |
2756 | } |
2757 | #endif | |
0e1c9c54 PB |
2758 | } |
2759 | ||
1fddef4b | 2760 | #ifdef CONFIG_USER_ONLY |
ca587a8e AJ |
2761 | int |
2762 | gdb_queuesig (void) | |
2763 | { | |
2764 | GDBState *s; | |
2765 | ||
2766 | s = gdbserver_state; | |
2767 | ||
2768 | if (gdbserver_fd < 0 || s->fd < 0) | |
2769 | return 0; | |
2770 | else | |
2771 | return 1; | |
2772 | } | |
2773 | ||
1fddef4b | 2774 | int |
db6b81d4 | 2775 | gdb_handlesig(CPUState *cpu, int sig) |
1fddef4b | 2776 | { |
db6b81d4 | 2777 | CPUArchState *env = cpu->env_ptr; |
5ca666c7 AF |
2778 | GDBState *s; |
2779 | char buf[256]; | |
2780 | int n; | |
1fddef4b | 2781 | |
5ca666c7 AF |
2782 | s = gdbserver_state; |
2783 | if (gdbserver_fd < 0 || s->fd < 0) { | |
2784 | return sig; | |
2785 | } | |
1fddef4b | 2786 | |
5ca666c7 | 2787 | /* disable single step if it was enabled */ |
3825b28f | 2788 | cpu_single_step(cpu, 0); |
5ca666c7 | 2789 | tb_flush(env); |
1fddef4b | 2790 | |
5ca666c7 AF |
2791 | if (sig != 0) { |
2792 | snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig)); | |
2793 | put_packet(s, buf); | |
2794 | } | |
2795 | /* put_packet() might have detected that the peer terminated the | |
2796 | connection. */ | |
2797 | if (s->fd < 0) { | |
2798 | return sig; | |
2799 | } | |
1fddef4b | 2800 | |
5ca666c7 AF |
2801 | sig = 0; |
2802 | s->state = RS_IDLE; | |
2803 | s->running_state = 0; | |
2804 | while (s->running_state == 0) { | |
2805 | n = read(s->fd, buf, 256); | |
2806 | if (n > 0) { | |
2807 | int i; | |
2808 | ||
2809 | for (i = 0; i < n; i++) { | |
2810 | gdb_read_byte(s, buf[i]); | |
2811 | } | |
2812 | } else if (n == 0 || errno != EAGAIN) { | |
2813 | /* XXX: Connection closed. Should probably wait for another | |
2814 | connection before continuing. */ | |
2815 | return sig; | |
1fddef4b | 2816 | } |
5ca666c7 AF |
2817 | } |
2818 | sig = s->signal; | |
2819 | s->signal = 0; | |
2820 | return sig; | |
1fddef4b | 2821 | } |
e9009676 | 2822 | |
ca587a8e | 2823 | /* Tell the remote gdb that the process has exited due to SIG. */ |
9349b4f9 | 2824 | void gdb_signalled(CPUArchState *env, int sig) |
ca587a8e | 2825 | { |
5ca666c7 AF |
2826 | GDBState *s; |
2827 | char buf[4]; | |
ca587a8e | 2828 | |
5ca666c7 AF |
2829 | s = gdbserver_state; |
2830 | if (gdbserver_fd < 0 || s->fd < 0) { | |
2831 | return; | |
2832 | } | |
ca587a8e | 2833 | |
5ca666c7 AF |
2834 | snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig)); |
2835 | put_packet(s, buf); | |
ca587a8e | 2836 | } |
1fddef4b | 2837 | |
880a7578 | 2838 | static void gdb_accept(void) |
858693c6 FB |
2839 | { |
2840 | GDBState *s; | |
2841 | struct sockaddr_in sockaddr; | |
2842 | socklen_t len; | |
bf1c852a | 2843 | int fd; |
858693c6 FB |
2844 | |
2845 | for(;;) { | |
2846 | len = sizeof(sockaddr); | |
2847 | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len); | |
2848 | if (fd < 0 && errno != EINTR) { | |
2849 | perror("accept"); | |
2850 | return; | |
2851 | } else if (fd >= 0) { | |
40ff6d7e KW |
2852 | #ifndef _WIN32 |
2853 | fcntl(fd, F_SETFD, FD_CLOEXEC); | |
2854 | #endif | |
b4608c04 FB |
2855 | break; |
2856 | } | |
2857 | } | |
858693c6 FB |
2858 | |
2859 | /* set short latency */ | |
bf1c852a | 2860 | socket_set_nodelay(fd); |
3b46e624 | 2861 | |
7267c094 | 2862 | s = g_malloc0(sizeof(GDBState)); |
2e0f2cfb AF |
2863 | s->c_cpu = first_cpu; |
2864 | s->g_cpu = first_cpu; | |
858693c6 | 2865 | s->fd = fd; |
56aebc89 | 2866 | gdb_has_xml = 0; |
858693c6 | 2867 | |
880a7578 | 2868 | gdbserver_state = s; |
a2d1ebaf | 2869 | |
858693c6 | 2870 | fcntl(fd, F_SETFL, O_NONBLOCK); |
858693c6 FB |
2871 | } |
2872 | ||
2873 | static int gdbserver_open(int port) | |
2874 | { | |
2875 | struct sockaddr_in sockaddr; | |
2876 | int fd, val, ret; | |
2877 | ||
2878 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
2879 | if (fd < 0) { | |
2880 | perror("socket"); | |
2881 | return -1; | |
2882 | } | |
40ff6d7e KW |
2883 | #ifndef _WIN32 |
2884 | fcntl(fd, F_SETFD, FD_CLOEXEC); | |
2885 | #endif | |
858693c6 FB |
2886 | |
2887 | /* allow fast reuse */ | |
2888 | val = 1; | |
9957fc7f | 2889 | qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); |
858693c6 FB |
2890 | |
2891 | sockaddr.sin_family = AF_INET; | |
2892 | sockaddr.sin_port = htons(port); | |
2893 | sockaddr.sin_addr.s_addr = 0; | |
2894 | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); | |
2895 | if (ret < 0) { | |
2896 | perror("bind"); | |
bb16172c | 2897 | close(fd); |
858693c6 FB |
2898 | return -1; |
2899 | } | |
2900 | ret = listen(fd, 0); | |
2901 | if (ret < 0) { | |
2902 | perror("listen"); | |
bb16172c | 2903 | close(fd); |
858693c6 FB |
2904 | return -1; |
2905 | } | |
858693c6 FB |
2906 | return fd; |
2907 | } | |
2908 | ||
2909 | int gdbserver_start(int port) | |
2910 | { | |
2911 | gdbserver_fd = gdbserver_open(port); | |
2912 | if (gdbserver_fd < 0) | |
2913 | return -1; | |
2914 | /* accept connections */ | |
880a7578 | 2915 | gdb_accept(); |
4046d913 PB |
2916 | return 0; |
2917 | } | |
2b1319c8 AJ |
2918 | |
2919 | /* Disable gdb stub for child processes. */ | |
9349b4f9 | 2920 | void gdbserver_fork(CPUArchState *env) |
2b1319c8 AJ |
2921 | { |
2922 | GDBState *s = gdbserver_state; | |
9f6164d6 | 2923 | if (gdbserver_fd < 0 || s->fd < 0) |
2b1319c8 AJ |
2924 | return; |
2925 | close(s->fd); | |
2926 | s->fd = -1; | |
2927 | cpu_breakpoint_remove_all(env, BP_GDB); | |
2928 | cpu_watchpoint_remove_all(env, BP_GDB); | |
2929 | } | |
1fddef4b | 2930 | #else |
aa1f17c1 | 2931 | static int gdb_chr_can_receive(void *opaque) |
4046d913 | 2932 | { |
56aebc89 PB |
2933 | /* We can handle an arbitrarily large amount of data. |
2934 | Pick the maximum packet size, which is as good as anything. */ | |
2935 | return MAX_PACKET_LENGTH; | |
4046d913 PB |
2936 | } |
2937 | ||
aa1f17c1 | 2938 | static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) |
4046d913 | 2939 | { |
4046d913 PB |
2940 | int i; |
2941 | ||
2942 | for (i = 0; i < size; i++) { | |
880a7578 | 2943 | gdb_read_byte(gdbserver_state, buf[i]); |
4046d913 PB |
2944 | } |
2945 | } | |
2946 | ||
2947 | static void gdb_chr_event(void *opaque, int event) | |
2948 | { | |
2949 | switch (event) { | |
b6b8df56 | 2950 | case CHR_EVENT_OPENED: |
0461d5a6 | 2951 | vm_stop(RUN_STATE_PAUSED); |
56aebc89 | 2952 | gdb_has_xml = 0; |
4046d913 PB |
2953 | break; |
2954 | default: | |
2955 | break; | |
2956 | } | |
2957 | } | |
2958 | ||
8a34a0fb AL |
2959 | static void gdb_monitor_output(GDBState *s, const char *msg, int len) |
2960 | { | |
2961 | char buf[MAX_PACKET_LENGTH]; | |
2962 | ||
2963 | buf[0] = 'O'; | |
2964 | if (len > (MAX_PACKET_LENGTH/2) - 1) | |
2965 | len = (MAX_PACKET_LENGTH/2) - 1; | |
2966 | memtohex(buf + 1, (uint8_t *)msg, len); | |
2967 | put_packet(s, buf); | |
2968 | } | |
2969 | ||
2970 | static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len) | |
2971 | { | |
2972 | const char *p = (const char *)buf; | |
2973 | int max_sz; | |
2974 | ||
2975 | max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2; | |
2976 | for (;;) { | |
2977 | if (len <= max_sz) { | |
2978 | gdb_monitor_output(gdbserver_state, p, len); | |
2979 | break; | |
2980 | } | |
2981 | gdb_monitor_output(gdbserver_state, p, max_sz); | |
2982 | p += max_sz; | |
2983 | len -= max_sz; | |
2984 | } | |
2985 | return len; | |
2986 | } | |
2987 | ||
59030a8c AL |
2988 | #ifndef _WIN32 |
2989 | static void gdb_sigterm_handler(int signal) | |
2990 | { | |
1354869c | 2991 | if (runstate_is_running()) { |
0461d5a6 | 2992 | vm_stop(RUN_STATE_PAUSED); |
e07bbac5 | 2993 | } |
59030a8c AL |
2994 | } |
2995 | #endif | |
2996 | ||
2997 | int gdbserver_start(const char *device) | |
4046d913 PB |
2998 | { |
2999 | GDBState *s; | |
59030a8c | 3000 | char gdbstub_device_name[128]; |
36556b20 AL |
3001 | CharDriverState *chr = NULL; |
3002 | CharDriverState *mon_chr; | |
cfc3475a | 3003 | |
59030a8c AL |
3004 | if (!device) |
3005 | return -1; | |
3006 | if (strcmp(device, "none") != 0) { | |
3007 | if (strstart(device, "tcp:", NULL)) { | |
3008 | /* enforce required TCP attributes */ | |
3009 | snprintf(gdbstub_device_name, sizeof(gdbstub_device_name), | |
3010 | "%s,nowait,nodelay,server", device); | |
3011 | device = gdbstub_device_name; | |
36556b20 | 3012 | } |
59030a8c AL |
3013 | #ifndef _WIN32 |
3014 | else if (strcmp(device, "stdio") == 0) { | |
3015 | struct sigaction act; | |
4046d913 | 3016 | |
59030a8c AL |
3017 | memset(&act, 0, sizeof(act)); |
3018 | act.sa_handler = gdb_sigterm_handler; | |
3019 | sigaction(SIGINT, &act, NULL); | |
3020 | } | |
3021 | #endif | |
27143a44 | 3022 | chr = qemu_chr_new("gdb", device, NULL); |
36556b20 AL |
3023 | if (!chr) |
3024 | return -1; | |
3025 | ||
456d6069 | 3026 | qemu_chr_fe_claim_no_fail(chr); |
36556b20 AL |
3027 | qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive, |
3028 | gdb_chr_event, NULL); | |
cfc3475a PB |
3029 | } |
3030 | ||
36556b20 AL |
3031 | s = gdbserver_state; |
3032 | if (!s) { | |
7267c094 | 3033 | s = g_malloc0(sizeof(GDBState)); |
36556b20 | 3034 | gdbserver_state = s; |
4046d913 | 3035 | |
36556b20 AL |
3036 | qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL); |
3037 | ||
3038 | /* Initialize a monitor terminal for gdb */ | |
7267c094 | 3039 | mon_chr = g_malloc0(sizeof(*mon_chr)); |
36556b20 AL |
3040 | mon_chr->chr_write = gdb_monitor_write; |
3041 | monitor_init(mon_chr, 0); | |
3042 | } else { | |
3043 | if (s->chr) | |
70f24fb6 | 3044 | qemu_chr_delete(s->chr); |
36556b20 AL |
3045 | mon_chr = s->mon_chr; |
3046 | memset(s, 0, sizeof(GDBState)); | |
3047 | } | |
2e0f2cfb AF |
3048 | s->c_cpu = first_cpu; |
3049 | s->g_cpu = first_cpu; | |
4046d913 | 3050 | s->chr = chr; |
36556b20 AL |
3051 | s->state = chr ? RS_IDLE : RS_INACTIVE; |
3052 | s->mon_chr = mon_chr; | |
cdb432b2 | 3053 | s->current_syscall_cb = NULL; |
8a34a0fb | 3054 | |
b4608c04 FB |
3055 | return 0; |
3056 | } | |
4046d913 | 3057 | #endif |