]>
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 | |
17 | * License along with this library; if not, write to the Free Software | |
fad6cb1a | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA |
b4608c04 | 19 | */ |
978efd6a | 20 | #include "config.h" |
56aebc89 | 21 | #include "qemu-common.h" |
1fddef4b FB |
22 | #ifdef CONFIG_USER_ONLY |
23 | #include <stdlib.h> | |
24 | #include <stdio.h> | |
25 | #include <stdarg.h> | |
26 | #include <string.h> | |
27 | #include <errno.h> | |
28 | #include <unistd.h> | |
978efd6a | 29 | #include <fcntl.h> |
1fddef4b FB |
30 | |
31 | #include "qemu.h" | |
32 | #else | |
8a34a0fb | 33 | #include "monitor.h" |
87ecb68b PB |
34 | #include "qemu-char.h" |
35 | #include "sysemu.h" | |
36 | #include "gdbstub.h" | |
1fddef4b | 37 | #endif |
67b915a5 | 38 | |
56aebc89 PB |
39 | #define MAX_PACKET_LENGTH 4096 |
40 | ||
8f447cc7 | 41 | #include "qemu_socket.h" |
e22a25c9 | 42 | #include "kvm.h" |
ca587a8e AJ |
43 | |
44 | ||
45 | enum { | |
46 | GDB_SIGNAL_0 = 0, | |
47 | GDB_SIGNAL_INT = 2, | |
48 | GDB_SIGNAL_TRAP = 5, | |
49 | GDB_SIGNAL_UNKNOWN = 143 | |
50 | }; | |
51 | ||
52 | #ifdef CONFIG_USER_ONLY | |
53 | ||
54 | /* Map target signal numbers to GDB protocol signal numbers and vice | |
55 | * versa. For user emulation's currently supported systems, we can | |
56 | * assume most signals are defined. | |
57 | */ | |
58 | ||
59 | static int gdb_signal_table[] = { | |
60 | 0, | |
61 | TARGET_SIGHUP, | |
62 | TARGET_SIGINT, | |
63 | TARGET_SIGQUIT, | |
64 | TARGET_SIGILL, | |
65 | TARGET_SIGTRAP, | |
66 | TARGET_SIGABRT, | |
67 | -1, /* SIGEMT */ | |
68 | TARGET_SIGFPE, | |
69 | TARGET_SIGKILL, | |
70 | TARGET_SIGBUS, | |
71 | TARGET_SIGSEGV, | |
72 | TARGET_SIGSYS, | |
73 | TARGET_SIGPIPE, | |
74 | TARGET_SIGALRM, | |
75 | TARGET_SIGTERM, | |
76 | TARGET_SIGURG, | |
77 | TARGET_SIGSTOP, | |
78 | TARGET_SIGTSTP, | |
79 | TARGET_SIGCONT, | |
80 | TARGET_SIGCHLD, | |
81 | TARGET_SIGTTIN, | |
82 | TARGET_SIGTTOU, | |
83 | TARGET_SIGIO, | |
84 | TARGET_SIGXCPU, | |
85 | TARGET_SIGXFSZ, | |
86 | TARGET_SIGVTALRM, | |
87 | TARGET_SIGPROF, | |
88 | TARGET_SIGWINCH, | |
89 | -1, /* SIGLOST */ | |
90 | TARGET_SIGUSR1, | |
91 | TARGET_SIGUSR2, | |
c72d5bf8 | 92 | #ifdef TARGET_SIGPWR |
ca587a8e | 93 | TARGET_SIGPWR, |
c72d5bf8 BS |
94 | #else |
95 | -1, | |
96 | #endif | |
ca587a8e AJ |
97 | -1, /* SIGPOLL */ |
98 | -1, | |
99 | -1, | |
100 | -1, | |
101 | -1, | |
102 | -1, | |
103 | -1, | |
104 | -1, | |
105 | -1, | |
106 | -1, | |
107 | -1, | |
108 | -1, | |
c72d5bf8 | 109 | #ifdef __SIGRTMIN |
ca587a8e AJ |
110 | __SIGRTMIN + 1, |
111 | __SIGRTMIN + 2, | |
112 | __SIGRTMIN + 3, | |
113 | __SIGRTMIN + 4, | |
114 | __SIGRTMIN + 5, | |
115 | __SIGRTMIN + 6, | |
116 | __SIGRTMIN + 7, | |
117 | __SIGRTMIN + 8, | |
118 | __SIGRTMIN + 9, | |
119 | __SIGRTMIN + 10, | |
120 | __SIGRTMIN + 11, | |
121 | __SIGRTMIN + 12, | |
122 | __SIGRTMIN + 13, | |
123 | __SIGRTMIN + 14, | |
124 | __SIGRTMIN + 15, | |
125 | __SIGRTMIN + 16, | |
126 | __SIGRTMIN + 17, | |
127 | __SIGRTMIN + 18, | |
128 | __SIGRTMIN + 19, | |
129 | __SIGRTMIN + 20, | |
130 | __SIGRTMIN + 21, | |
131 | __SIGRTMIN + 22, | |
132 | __SIGRTMIN + 23, | |
133 | __SIGRTMIN + 24, | |
134 | __SIGRTMIN + 25, | |
135 | __SIGRTMIN + 26, | |
136 | __SIGRTMIN + 27, | |
137 | __SIGRTMIN + 28, | |
138 | __SIGRTMIN + 29, | |
139 | __SIGRTMIN + 30, | |
140 | __SIGRTMIN + 31, | |
141 | -1, /* SIGCANCEL */ | |
142 | __SIGRTMIN, | |
143 | __SIGRTMIN + 32, | |
144 | __SIGRTMIN + 33, | |
145 | __SIGRTMIN + 34, | |
146 | __SIGRTMIN + 35, | |
147 | __SIGRTMIN + 36, | |
148 | __SIGRTMIN + 37, | |
149 | __SIGRTMIN + 38, | |
150 | __SIGRTMIN + 39, | |
151 | __SIGRTMIN + 40, | |
152 | __SIGRTMIN + 41, | |
153 | __SIGRTMIN + 42, | |
154 | __SIGRTMIN + 43, | |
155 | __SIGRTMIN + 44, | |
156 | __SIGRTMIN + 45, | |
157 | __SIGRTMIN + 46, | |
158 | __SIGRTMIN + 47, | |
159 | __SIGRTMIN + 48, | |
160 | __SIGRTMIN + 49, | |
161 | __SIGRTMIN + 50, | |
162 | __SIGRTMIN + 51, | |
163 | __SIGRTMIN + 52, | |
164 | __SIGRTMIN + 53, | |
165 | __SIGRTMIN + 54, | |
166 | __SIGRTMIN + 55, | |
167 | __SIGRTMIN + 56, | |
168 | __SIGRTMIN + 57, | |
169 | __SIGRTMIN + 58, | |
170 | __SIGRTMIN + 59, | |
171 | __SIGRTMIN + 60, | |
172 | __SIGRTMIN + 61, | |
173 | __SIGRTMIN + 62, | |
174 | __SIGRTMIN + 63, | |
175 | __SIGRTMIN + 64, | |
176 | __SIGRTMIN + 65, | |
177 | __SIGRTMIN + 66, | |
178 | __SIGRTMIN + 67, | |
179 | __SIGRTMIN + 68, | |
180 | __SIGRTMIN + 69, | |
181 | __SIGRTMIN + 70, | |
182 | __SIGRTMIN + 71, | |
183 | __SIGRTMIN + 72, | |
184 | __SIGRTMIN + 73, | |
185 | __SIGRTMIN + 74, | |
186 | __SIGRTMIN + 75, | |
187 | __SIGRTMIN + 76, | |
188 | __SIGRTMIN + 77, | |
189 | __SIGRTMIN + 78, | |
190 | __SIGRTMIN + 79, | |
191 | __SIGRTMIN + 80, | |
192 | __SIGRTMIN + 81, | |
193 | __SIGRTMIN + 82, | |
194 | __SIGRTMIN + 83, | |
195 | __SIGRTMIN + 84, | |
196 | __SIGRTMIN + 85, | |
197 | __SIGRTMIN + 86, | |
198 | __SIGRTMIN + 87, | |
199 | __SIGRTMIN + 88, | |
200 | __SIGRTMIN + 89, | |
201 | __SIGRTMIN + 90, | |
202 | __SIGRTMIN + 91, | |
203 | __SIGRTMIN + 92, | |
204 | __SIGRTMIN + 93, | |
205 | __SIGRTMIN + 94, | |
206 | __SIGRTMIN + 95, | |
207 | -1, /* SIGINFO */ | |
208 | -1, /* UNKNOWN */ | |
209 | -1, /* DEFAULT */ | |
210 | -1, | |
211 | -1, | |
212 | -1, | |
213 | -1, | |
214 | -1, | |
215 | -1 | |
c72d5bf8 | 216 | #endif |
ca587a8e | 217 | }; |
8f447cc7 | 218 | #else |
ca587a8e AJ |
219 | /* In system mode we only need SIGINT and SIGTRAP; other signals |
220 | are not yet supported. */ | |
221 | ||
222 | enum { | |
223 | TARGET_SIGINT = 2, | |
224 | TARGET_SIGTRAP = 5 | |
225 | }; | |
226 | ||
227 | static int gdb_signal_table[] = { | |
228 | -1, | |
229 | -1, | |
230 | TARGET_SIGINT, | |
231 | -1, | |
232 | -1, | |
233 | TARGET_SIGTRAP | |
234 | }; | |
235 | #endif | |
236 | ||
237 | #ifdef CONFIG_USER_ONLY | |
238 | static int target_signal_to_gdb (int sig) | |
239 | { | |
240 | int i; | |
241 | for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++) | |
242 | if (gdb_signal_table[i] == sig) | |
243 | return i; | |
244 | return GDB_SIGNAL_UNKNOWN; | |
245 | } | |
8f447cc7 | 246 | #endif |
b4608c04 | 247 | |
ca587a8e AJ |
248 | static int gdb_signal_to_target (int sig) |
249 | { | |
250 | if (sig < ARRAY_SIZE (gdb_signal_table)) | |
251 | return gdb_signal_table[sig]; | |
252 | else | |
253 | return -1; | |
254 | } | |
255 | ||
4abe615b | 256 | //#define DEBUG_GDB |
b4608c04 | 257 | |
56aebc89 PB |
258 | typedef struct GDBRegisterState { |
259 | int base_reg; | |
260 | int num_regs; | |
261 | gdb_reg_cb get_reg; | |
262 | gdb_reg_cb set_reg; | |
263 | const char *xml; | |
264 | struct GDBRegisterState *next; | |
265 | } GDBRegisterState; | |
266 | ||
858693c6 | 267 | enum RSState { |
36556b20 | 268 | RS_INACTIVE, |
858693c6 FB |
269 | RS_IDLE, |
270 | RS_GETLINE, | |
271 | RS_CHKSUM1, | |
272 | RS_CHKSUM2, | |
a2d1ebaf | 273 | RS_SYSCALL, |
858693c6 | 274 | }; |
858693c6 | 275 | typedef struct GDBState { |
880a7578 AL |
276 | CPUState *c_cpu; /* current CPU for step/continue ops */ |
277 | CPUState *g_cpu; /* current CPU for other ops */ | |
278 | CPUState *query_cpu; /* for q{f|s}ThreadInfo */ | |
41625033 | 279 | enum RSState state; /* parsing state */ |
56aebc89 | 280 | char line_buf[MAX_PACKET_LENGTH]; |
858693c6 FB |
281 | int line_buf_index; |
282 | int line_csum; | |
56aebc89 | 283 | uint8_t last_packet[MAX_PACKET_LENGTH + 4]; |
4046d913 | 284 | int last_packet_len; |
1f487ee9 | 285 | int signal; |
41625033 | 286 | #ifdef CONFIG_USER_ONLY |
4046d913 | 287 | int fd; |
41625033 | 288 | int running_state; |
4046d913 PB |
289 | #else |
290 | CharDriverState *chr; | |
8a34a0fb | 291 | CharDriverState *mon_chr; |
41625033 | 292 | #endif |
858693c6 | 293 | } GDBState; |
b4608c04 | 294 | |
60897d36 EI |
295 | /* By default use no IRQs and no timers while single stepping so as to |
296 | * make single stepping like an ICE HW step. | |
297 | */ | |
298 | static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER; | |
299 | ||
880a7578 AL |
300 | static GDBState *gdbserver_state; |
301 | ||
56aebc89 PB |
302 | /* This is an ugly hack to cope with both new and old gdb. |
303 | If gdb sends qXfer:features:read then assume we're talking to a newish | |
304 | gdb that understands target descriptions. */ | |
305 | static int gdb_has_xml; | |
306 | ||
1fddef4b | 307 | #ifdef CONFIG_USER_ONLY |
4046d913 PB |
308 | /* XXX: This is not thread safe. Do we care? */ |
309 | static int gdbserver_fd = -1; | |
310 | ||
858693c6 | 311 | static int get_char(GDBState *s) |
b4608c04 FB |
312 | { |
313 | uint8_t ch; | |
314 | int ret; | |
315 | ||
316 | for(;;) { | |
8f447cc7 | 317 | ret = recv(s->fd, &ch, 1, 0); |
b4608c04 | 318 | if (ret < 0) { |
1f487ee9 EI |
319 | if (errno == ECONNRESET) |
320 | s->fd = -1; | |
b4608c04 FB |
321 | if (errno != EINTR && errno != EAGAIN) |
322 | return -1; | |
323 | } else if (ret == 0) { | |
1f487ee9 EI |
324 | close(s->fd); |
325 | s->fd = -1; | |
b4608c04 FB |
326 | return -1; |
327 | } else { | |
328 | break; | |
329 | } | |
330 | } | |
331 | return ch; | |
332 | } | |
4046d913 | 333 | #endif |
b4608c04 | 334 | |
a2d1ebaf PB |
335 | static gdb_syscall_complete_cb gdb_current_syscall_cb; |
336 | ||
337 | enum { | |
338 | GDB_SYS_UNKNOWN, | |
339 | GDB_SYS_ENABLED, | |
340 | GDB_SYS_DISABLED, | |
341 | } gdb_syscall_mode; | |
342 | ||
343 | /* If gdb is connected when the first semihosting syscall occurs then use | |
344 | remote gdb syscalls. Otherwise use native file IO. */ | |
345 | int use_gdb_syscalls(void) | |
346 | { | |
347 | if (gdb_syscall_mode == GDB_SYS_UNKNOWN) { | |
880a7578 AL |
348 | gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED |
349 | : GDB_SYS_DISABLED); | |
a2d1ebaf PB |
350 | } |
351 | return gdb_syscall_mode == GDB_SYS_ENABLED; | |
352 | } | |
353 | ||
ba70a624 EI |
354 | /* Resume execution. */ |
355 | static inline void gdb_continue(GDBState *s) | |
356 | { | |
357 | #ifdef CONFIG_USER_ONLY | |
358 | s->running_state = 1; | |
359 | #else | |
360 | vm_start(); | |
361 | #endif | |
362 | } | |
363 | ||
858693c6 | 364 | static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
b4608c04 | 365 | { |
4046d913 | 366 | #ifdef CONFIG_USER_ONLY |
b4608c04 FB |
367 | int ret; |
368 | ||
369 | while (len > 0) { | |
8f447cc7 | 370 | ret = send(s->fd, buf, len, 0); |
b4608c04 FB |
371 | if (ret < 0) { |
372 | if (errno != EINTR && errno != EAGAIN) | |
373 | return; | |
374 | } else { | |
375 | buf += ret; | |
376 | len -= ret; | |
377 | } | |
378 | } | |
4046d913 PB |
379 | #else |
380 | qemu_chr_write(s->chr, buf, len); | |
381 | #endif | |
b4608c04 FB |
382 | } |
383 | ||
384 | static inline int fromhex(int v) | |
385 | { | |
386 | if (v >= '0' && v <= '9') | |
387 | return v - '0'; | |
388 | else if (v >= 'A' && v <= 'F') | |
389 | return v - 'A' + 10; | |
390 | else if (v >= 'a' && v <= 'f') | |
391 | return v - 'a' + 10; | |
392 | else | |
393 | return 0; | |
394 | } | |
395 | ||
396 | static inline int tohex(int v) | |
397 | { | |
398 | if (v < 10) | |
399 | return v + '0'; | |
400 | else | |
401 | return v - 10 + 'a'; | |
402 | } | |
403 | ||
404 | static void memtohex(char *buf, const uint8_t *mem, int len) | |
405 | { | |
406 | int i, c; | |
407 | char *q; | |
408 | q = buf; | |
409 | for(i = 0; i < len; i++) { | |
410 | c = mem[i]; | |
411 | *q++ = tohex(c >> 4); | |
412 | *q++ = tohex(c & 0xf); | |
413 | } | |
414 | *q = '\0'; | |
415 | } | |
416 | ||
417 | static void hextomem(uint8_t *mem, const char *buf, int len) | |
418 | { | |
419 | int i; | |
420 | ||
421 | for(i = 0; i < len; i++) { | |
422 | mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); | |
423 | buf += 2; | |
424 | } | |
425 | } | |
426 | ||
b4608c04 | 427 | /* return -1 if error, 0 if OK */ |
56aebc89 | 428 | static int put_packet_binary(GDBState *s, const char *buf, int len) |
b4608c04 | 429 | { |
56aebc89 | 430 | int csum, i; |
60fe76f3 | 431 | uint8_t *p; |
b4608c04 | 432 | |
b4608c04 | 433 | for(;;) { |
4046d913 PB |
434 | p = s->last_packet; |
435 | *(p++) = '$'; | |
4046d913 PB |
436 | memcpy(p, buf, len); |
437 | p += len; | |
b4608c04 FB |
438 | csum = 0; |
439 | for(i = 0; i < len; i++) { | |
440 | csum += buf[i]; | |
441 | } | |
4046d913 PB |
442 | *(p++) = '#'; |
443 | *(p++) = tohex((csum >> 4) & 0xf); | |
444 | *(p++) = tohex((csum) & 0xf); | |
b4608c04 | 445 | |
4046d913 | 446 | s->last_packet_len = p - s->last_packet; |
ffe8ab83 | 447 | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
b4608c04 | 448 | |
4046d913 PB |
449 | #ifdef CONFIG_USER_ONLY |
450 | i = get_char(s); | |
451 | if (i < 0) | |
b4608c04 | 452 | return -1; |
4046d913 | 453 | if (i == '+') |
b4608c04 | 454 | break; |
4046d913 PB |
455 | #else |
456 | break; | |
457 | #endif | |
b4608c04 FB |
458 | } |
459 | return 0; | |
460 | } | |
461 | ||
56aebc89 PB |
462 | /* return -1 if error, 0 if OK */ |
463 | static int put_packet(GDBState *s, const char *buf) | |
464 | { | |
465 | #ifdef DEBUG_GDB | |
466 | printf("reply='%s'\n", buf); | |
467 | #endif | |
79808573 | 468 | |
56aebc89 PB |
469 | return put_packet_binary(s, buf, strlen(buf)); |
470 | } | |
471 | ||
472 | /* The GDB remote protocol transfers values in target byte order. This means | |
473 | we can use the raw memory access routines to access the value buffer. | |
474 | Conveniently, these also handle the case where the buffer is mis-aligned. | |
475 | */ | |
476 | #define GET_REG8(val) do { \ | |
477 | stb_p(mem_buf, val); \ | |
478 | return 1; \ | |
479 | } while(0) | |
480 | #define GET_REG16(val) do { \ | |
481 | stw_p(mem_buf, val); \ | |
482 | return 2; \ | |
483 | } while(0) | |
484 | #define GET_REG32(val) do { \ | |
485 | stl_p(mem_buf, val); \ | |
486 | return 4; \ | |
487 | } while(0) | |
488 | #define GET_REG64(val) do { \ | |
489 | stq_p(mem_buf, val); \ | |
490 | return 8; \ | |
491 | } while(0) | |
492 | ||
493 | #if TARGET_LONG_BITS == 64 | |
494 | #define GET_REGL(val) GET_REG64(val) | |
495 | #define ldtul_p(addr) ldq_p(addr) | |
496 | #else | |
497 | #define GET_REGL(val) GET_REG32(val) | |
498 | #define ldtul_p(addr) ldl_p(addr) | |
79808573 FB |
499 | #endif |
500 | ||
56aebc89 | 501 | #if defined(TARGET_I386) |
5ad265ee AZ |
502 | |
503 | #ifdef TARGET_X86_64 | |
56aebc89 PB |
504 | static const int gpr_map[16] = { |
505 | R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP, | |
506 | 8, 9, 10, 11, 12, 13, 14, 15 | |
507 | }; | |
79808573 | 508 | #else |
56aebc89 | 509 | static const int gpr_map[8] = {0, 1, 2, 3, 4, 5, 6, 7}; |
79808573 | 510 | #endif |
79808573 | 511 | |
56aebc89 PB |
512 | #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25) |
513 | ||
514 | static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n) | |
79808573 | 515 | { |
56aebc89 PB |
516 | if (n < CPU_NB_REGS) { |
517 | GET_REGL(env->regs[gpr_map[n]]); | |
518 | } else if (n >= CPU_NB_REGS + 8 && n < CPU_NB_REGS + 16) { | |
519 | /* FIXME: byteswap float values. */ | |
520 | #ifdef USE_X86LDOUBLE | |
521 | memcpy(mem_buf, &env->fpregs[n - (CPU_NB_REGS + 8)], 10); | |
79808573 | 522 | #else |
56aebc89 | 523 | memset(mem_buf, 0, 10); |
79808573 | 524 | #endif |
56aebc89 PB |
525 | return 10; |
526 | } else if (n >= CPU_NB_REGS + 24) { | |
527 | n -= CPU_NB_REGS + 24; | |
528 | if (n < CPU_NB_REGS) { | |
529 | stq_p(mem_buf, env->xmm_regs[n].XMM_Q(0)); | |
530 | stq_p(mem_buf + 8, env->xmm_regs[n].XMM_Q(1)); | |
531 | return 16; | |
532 | } else if (n == CPU_NB_REGS) { | |
533 | GET_REG32(env->mxcsr); | |
534 | } | |
535 | } else { | |
536 | n -= CPU_NB_REGS; | |
537 | switch (n) { | |
538 | case 0: GET_REGL(env->eip); | |
539 | case 1: GET_REG32(env->eflags); | |
540 | case 2: GET_REG32(env->segs[R_CS].selector); | |
541 | case 3: GET_REG32(env->segs[R_SS].selector); | |
542 | case 4: GET_REG32(env->segs[R_DS].selector); | |
543 | case 5: GET_REG32(env->segs[R_ES].selector); | |
544 | case 6: GET_REG32(env->segs[R_FS].selector); | |
545 | case 7: GET_REG32(env->segs[R_GS].selector); | |
546 | /* 8...15 x87 regs. */ | |
547 | case 16: GET_REG32(env->fpuc); | |
548 | case 17: GET_REG32((env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11); | |
549 | case 18: GET_REG32(0); /* ftag */ | |
550 | case 19: GET_REG32(0); /* fiseg */ | |
551 | case 20: GET_REG32(0); /* fioff */ | |
552 | case 21: GET_REG32(0); /* foseg */ | |
553 | case 22: GET_REG32(0); /* fooff */ | |
554 | case 23: GET_REG32(0); /* fop */ | |
555 | /* 24+ xmm regs. */ | |
556 | } | |
79808573 | 557 | } |
56aebc89 | 558 | return 0; |
6da41eaf FB |
559 | } |
560 | ||
56aebc89 | 561 | static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int i) |
6da41eaf | 562 | { |
56aebc89 | 563 | uint32_t tmp; |
6da41eaf | 564 | |
56aebc89 PB |
565 | if (i < CPU_NB_REGS) { |
566 | env->regs[gpr_map[i]] = ldtul_p(mem_buf); | |
567 | return sizeof(target_ulong); | |
568 | } else if (i >= CPU_NB_REGS + 8 && i < CPU_NB_REGS + 16) { | |
569 | i -= CPU_NB_REGS + 8; | |
570 | #ifdef USE_X86LDOUBLE | |
571 | memcpy(&env->fpregs[i], mem_buf, 10); | |
79808573 | 572 | #endif |
56aebc89 PB |
573 | return 10; |
574 | } else if (i >= CPU_NB_REGS + 24) { | |
575 | i -= CPU_NB_REGS + 24; | |
576 | if (i < CPU_NB_REGS) { | |
577 | env->xmm_regs[i].XMM_Q(0) = ldq_p(mem_buf); | |
578 | env->xmm_regs[i].XMM_Q(1) = ldq_p(mem_buf + 8); | |
579 | return 16; | |
580 | } else if (i == CPU_NB_REGS) { | |
581 | env->mxcsr = ldl_p(mem_buf); | |
582 | return 4; | |
79808573 | 583 | } |
56aebc89 PB |
584 | } else { |
585 | i -= CPU_NB_REGS; | |
586 | switch (i) { | |
587 | case 0: env->eip = ldtul_p(mem_buf); return sizeof(target_ulong); | |
588 | case 1: env->eflags = ldl_p(mem_buf); return 4; | |
589 | #if defined(CONFIG_USER_ONLY) | |
590 | #define LOAD_SEG(index, sreg)\ | |
591 | tmp = ldl_p(mem_buf);\ | |
592 | if (tmp != env->segs[sreg].selector)\ | |
593 | cpu_x86_load_seg(env, sreg, tmp); | |
594 | #else | |
595 | /* FIXME: Honor segment registers. Needs to avoid raising an exception | |
596 | when the selector is invalid. */ | |
597 | #define LOAD_SEG(index, sreg) do {} while(0) | |
6da41eaf | 598 | #endif |
56aebc89 PB |
599 | case 2: LOAD_SEG(10, R_CS); return 4; |
600 | case 3: LOAD_SEG(11, R_SS); return 4; | |
601 | case 4: LOAD_SEG(12, R_DS); return 4; | |
602 | case 5: LOAD_SEG(13, R_ES); return 4; | |
603 | case 6: LOAD_SEG(14, R_FS); return 4; | |
604 | case 7: LOAD_SEG(15, R_GS); return 4; | |
605 | /* 8...15 x87 regs. */ | |
606 | case 16: env->fpuc = ldl_p(mem_buf); return 4; | |
607 | case 17: | |
608 | tmp = ldl_p(mem_buf); | |
609 | env->fpstt = (tmp >> 11) & 7; | |
610 | env->fpus = tmp & ~0x3800; | |
611 | return 4; | |
612 | case 18: /* ftag */ return 4; | |
613 | case 19: /* fiseg */ return 4; | |
614 | case 20: /* fioff */ return 4; | |
615 | case 21: /* foseg */ return 4; | |
616 | case 22: /* fooff */ return 4; | |
617 | case 23: /* fop */ return 4; | |
618 | /* 24+ xmm regs. */ | |
79808573 | 619 | } |
79808573 | 620 | } |
56aebc89 PB |
621 | /* Unrecognised register. */ |
622 | return 0; | |
6da41eaf FB |
623 | } |
624 | ||
9e62fd7f | 625 | #elif defined (TARGET_PPC) |
9e62fd7f | 626 | |
e571cb47 AJ |
627 | /* Old gdb always expects FP registers. Newer (xml-aware) gdb only |
628 | expects whatever the target description contains. Due to a | |
629 | historical mishap the FP registers appear in between core integer | |
630 | regs and PC, MSR, CR, and so forth. We hack round this by giving the | |
631 | FP regs zero size when talking to a newer gdb. */ | |
56aebc89 | 632 | #define NUM_CORE_REGS 71 |
e571cb47 AJ |
633 | #if defined (TARGET_PPC64) |
634 | #define GDB_CORE_XML "power64-core.xml" | |
635 | #else | |
636 | #define GDB_CORE_XML "power-core.xml" | |
637 | #endif | |
9e62fd7f | 638 | |
56aebc89 | 639 | static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n) |
9e62fd7f | 640 | { |
56aebc89 PB |
641 | if (n < 32) { |
642 | /* gprs */ | |
643 | GET_REGL(env->gpr[n]); | |
644 | } else if (n < 64) { | |
645 | /* fprs */ | |
e571cb47 AJ |
646 | if (gdb_has_xml) |
647 | return 0; | |
8d4acf9b | 648 | stfq_p(mem_buf, env->fpr[n-32]); |
56aebc89 PB |
649 | return 8; |
650 | } else { | |
651 | switch (n) { | |
652 | case 64: GET_REGL(env->nip); | |
653 | case 65: GET_REGL(env->msr); | |
654 | case 66: | |
655 | { | |
656 | uint32_t cr = 0; | |
657 | int i; | |
658 | for (i = 0; i < 8; i++) | |
659 | cr |= env->crf[i] << (32 - ((i + 1) * 4)); | |
660 | GET_REG32(cr); | |
661 | } | |
662 | case 67: GET_REGL(env->lr); | |
663 | case 68: GET_REGL(env->ctr); | |
3d7b417e | 664 | case 69: GET_REGL(env->xer); |
e571cb47 AJ |
665 | case 70: |
666 | { | |
667 | if (gdb_has_xml) | |
668 | return 0; | |
669 | GET_REG32(0); /* fpscr */ | |
670 | } | |
56aebc89 PB |
671 | } |
672 | } | |
673 | return 0; | |
674 | } | |
9e62fd7f | 675 | |
56aebc89 PB |
676 | static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n) |
677 | { | |
678 | if (n < 32) { | |
679 | /* gprs */ | |
680 | env->gpr[n] = ldtul_p(mem_buf); | |
681 | return sizeof(target_ulong); | |
682 | } else if (n < 64) { | |
683 | /* fprs */ | |
e571cb47 AJ |
684 | if (gdb_has_xml) |
685 | return 0; | |
8d4acf9b | 686 | env->fpr[n-32] = ldfq_p(mem_buf); |
56aebc89 PB |
687 | return 8; |
688 | } else { | |
689 | switch (n) { | |
690 | case 64: | |
691 | env->nip = ldtul_p(mem_buf); | |
692 | return sizeof(target_ulong); | |
693 | case 65: | |
694 | ppc_store_msr(env, ldtul_p(mem_buf)); | |
695 | return sizeof(target_ulong); | |
696 | case 66: | |
697 | { | |
698 | uint32_t cr = ldl_p(mem_buf); | |
699 | int i; | |
700 | for (i = 0; i < 8; i++) | |
701 | env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF; | |
702 | return 4; | |
703 | } | |
704 | case 67: | |
705 | env->lr = ldtul_p(mem_buf); | |
706 | return sizeof(target_ulong); | |
707 | case 68: | |
708 | env->ctr = ldtul_p(mem_buf); | |
709 | return sizeof(target_ulong); | |
710 | case 69: | |
3d7b417e AJ |
711 | env->xer = ldtul_p(mem_buf); |
712 | return sizeof(target_ulong); | |
56aebc89 PB |
713 | case 70: |
714 | /* fpscr */ | |
e571cb47 AJ |
715 | if (gdb_has_xml) |
716 | return 0; | |
56aebc89 PB |
717 | return 4; |
718 | } | |
719 | } | |
720 | return 0; | |
e95c8d51 | 721 | } |
56aebc89 | 722 | |
e95c8d51 | 723 | #elif defined (TARGET_SPARC) |
56aebc89 PB |
724 | |
725 | #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) | |
726 | #define NUM_CORE_REGS 86 | |
96d19126 | 727 | #else |
5a377912 | 728 | #define NUM_CORE_REGS 72 |
96d19126 | 729 | #endif |
56aebc89 | 730 | |
96d19126 | 731 | #ifdef TARGET_ABI32 |
56aebc89 | 732 | #define GET_REGA(val) GET_REG32(val) |
96d19126 | 733 | #else |
56aebc89 | 734 | #define GET_REGA(val) GET_REGL(val) |
96d19126 | 735 | #endif |
e95c8d51 | 736 | |
56aebc89 PB |
737 | static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n) |
738 | { | |
739 | if (n < 8) { | |
740 | /* g0..g7 */ | |
741 | GET_REGA(env->gregs[n]); | |
e95c8d51 | 742 | } |
56aebc89 PB |
743 | if (n < 32) { |
744 | /* register window */ | |
745 | GET_REGA(env->regwptr[n - 8]); | |
e95c8d51 | 746 | } |
56aebc89 PB |
747 | #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64) |
748 | if (n < 64) { | |
749 | /* fprs */ | |
750 | GET_REG32(*((uint32_t *)&env->fpr[n - 32])); | |
e95c8d51 FB |
751 | } |
752 | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */ | |
56aebc89 PB |
753 | switch (n) { |
754 | case 64: GET_REGA(env->y); | |
755 | case 65: GET_REGA(GET_PSR(env)); | |
756 | case 66: GET_REGA(env->wim); | |
757 | case 67: GET_REGA(env->tbr); | |
758 | case 68: GET_REGA(env->pc); | |
759 | case 69: GET_REGA(env->npc); | |
760 | case 70: GET_REGA(env->fsr); | |
761 | case 71: GET_REGA(0); /* csr */ | |
5a377912 | 762 | default: GET_REGA(0); |
56aebc89 | 763 | } |
3475187d | 764 | #else |
56aebc89 PB |
765 | if (n < 64) { |
766 | /* f0-f31 */ | |
767 | GET_REG32(*((uint32_t *)&env->fpr[n - 32])); | |
768 | } | |
769 | if (n < 80) { | |
770 | /* f32-f62 (double width, even numbers only) */ | |
771 | uint64_t val; | |
9d9754a3 | 772 | |
56aebc89 PB |
773 | val = (uint64_t)*((uint32_t *)&env->fpr[(n - 64) * 2 + 32]) << 32; |
774 | val |= *((uint32_t *)&env->fpr[(n - 64) * 2 + 33]); | |
775 | GET_REG64(val); | |
3475187d | 776 | } |
56aebc89 PB |
777 | switch (n) { |
778 | case 80: GET_REGL(env->pc); | |
779 | case 81: GET_REGL(env->npc); | |
780 | case 82: GET_REGL(((uint64_t)GET_CCR(env) << 32) | | |
17d996e1 BS |
781 | ((env->asi & 0xff) << 24) | |
782 | ((env->pstate & 0xfff) << 8) | | |
783 | GET_CWP64(env)); | |
56aebc89 PB |
784 | case 83: GET_REGL(env->fsr); |
785 | case 84: GET_REGL(env->fprs); | |
786 | case 85: GET_REGL(env->y); | |
787 | } | |
3475187d | 788 | #endif |
56aebc89 | 789 | return 0; |
e95c8d51 FB |
790 | } |
791 | ||
56aebc89 | 792 | static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n) |
e95c8d51 | 793 | { |
56aebc89 PB |
794 | #if defined(TARGET_ABI32) |
795 | abi_ulong tmp; | |
796 | ||
797 | tmp = ldl_p(mem_buf); | |
96d19126 | 798 | #else |
56aebc89 PB |
799 | target_ulong tmp; |
800 | ||
801 | tmp = ldtul_p(mem_buf); | |
96d19126 | 802 | #endif |
e95c8d51 | 803 | |
56aebc89 PB |
804 | if (n < 8) { |
805 | /* g0..g7 */ | |
806 | env->gregs[n] = tmp; | |
807 | } else if (n < 32) { | |
808 | /* register window */ | |
809 | env->regwptr[n - 8] = tmp; | |
e95c8d51 | 810 | } |
56aebc89 PB |
811 | #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64) |
812 | else if (n < 64) { | |
813 | /* fprs */ | |
814 | *((uint32_t *)&env->fpr[n - 32]) = tmp; | |
815 | } else { | |
816 | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */ | |
817 | switch (n) { | |
818 | case 64: env->y = tmp; break; | |
819 | case 65: PUT_PSR(env, tmp); break; | |
820 | case 66: env->wim = tmp; break; | |
821 | case 67: env->tbr = tmp; break; | |
822 | case 68: env->pc = tmp; break; | |
823 | case 69: env->npc = tmp; break; | |
824 | case 70: env->fsr = tmp; break; | |
825 | default: return 0; | |
826 | } | |
e95c8d51 | 827 | } |
56aebc89 | 828 | return 4; |
3475187d | 829 | #else |
56aebc89 PB |
830 | else if (n < 64) { |
831 | /* f0-f31 */ | |
56aebc89 PB |
832 | env->fpr[n] = ldfl_p(mem_buf); |
833 | return 4; | |
834 | } else if (n < 80) { | |
835 | /* f32-f62 (double width, even numbers only) */ | |
836 | *((uint32_t *)&env->fpr[(n - 64) * 2 + 32]) = tmp >> 32; | |
837 | *((uint32_t *)&env->fpr[(n - 64) * 2 + 33]) = tmp; | |
838 | } else { | |
839 | switch (n) { | |
840 | case 80: env->pc = tmp; break; | |
841 | case 81: env->npc = tmp; break; | |
842 | case 82: | |
843 | PUT_CCR(env, tmp >> 32); | |
844 | env->asi = (tmp >> 24) & 0xff; | |
845 | env->pstate = (tmp >> 8) & 0xfff; | |
846 | PUT_CWP64(env, tmp & 0xff); | |
847 | break; | |
848 | case 83: env->fsr = tmp; break; | |
849 | case 84: env->fprs = tmp; break; | |
850 | case 85: env->y = tmp; break; | |
851 | default: return 0; | |
852 | } | |
17d996e1 | 853 | } |
56aebc89 | 854 | return 8; |
3475187d | 855 | #endif |
9e62fd7f | 856 | } |
1fddef4b | 857 | #elif defined (TARGET_ARM) |
6da41eaf | 858 | |
56aebc89 PB |
859 | /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect |
860 | whatever the target description contains. Due to a historical mishap | |
861 | the FPA registers appear in between core integer regs and the CPSR. | |
862 | We hack round this by giving the FPA regs zero size when talking to a | |
863 | newer gdb. */ | |
864 | #define NUM_CORE_REGS 26 | |
865 | #define GDB_CORE_XML "arm-core.xml" | |
e6e5906b | 866 | |
56aebc89 | 867 | static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n) |
e6e5906b | 868 | { |
56aebc89 PB |
869 | if (n < 16) { |
870 | /* Core integer register. */ | |
871 | GET_REG32(env->regs[n]); | |
872 | } | |
873 | if (n < 24) { | |
874 | /* FPA registers. */ | |
875 | if (gdb_has_xml) | |
876 | return 0; | |
877 | memset(mem_buf, 0, 12); | |
878 | return 12; | |
879 | } | |
880 | switch (n) { | |
881 | case 24: | |
882 | /* FPA status register. */ | |
883 | if (gdb_has_xml) | |
884 | return 0; | |
885 | GET_REG32(0); | |
886 | case 25: | |
887 | /* CPSR */ | |
888 | GET_REG32(cpsr_read(env)); | |
889 | } | |
890 | /* Unknown register. */ | |
891 | return 0; | |
e6e5906b | 892 | } |
6f970bd9 | 893 | |
56aebc89 PB |
894 | static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n) |
895 | { | |
896 | uint32_t tmp; | |
6f970bd9 | 897 | |
56aebc89 | 898 | tmp = ldl_p(mem_buf); |
6f970bd9 | 899 | |
56aebc89 PB |
900 | /* Mask out low bit of PC to workaround gdb bugs. This will probably |
901 | cause problems if we ever implement the Jazelle DBX extensions. */ | |
902 | if (n == 15) | |
903 | tmp &= ~1; | |
6f970bd9 | 904 | |
56aebc89 PB |
905 | if (n < 16) { |
906 | /* Core integer register. */ | |
907 | env->regs[n] = tmp; | |
908 | return 4; | |
909 | } | |
910 | if (n < 24) { /* 16-23 */ | |
911 | /* FPA registers (ignored). */ | |
912 | if (gdb_has_xml) | |
913 | return 0; | |
914 | return 12; | |
915 | } | |
916 | switch (n) { | |
917 | case 24: | |
918 | /* FPA status register (ignored). */ | |
919 | if (gdb_has_xml) | |
920 | return 0; | |
921 | return 4; | |
922 | case 25: | |
923 | /* CPSR */ | |
924 | cpsr_write (env, tmp, 0xffffffff); | |
925 | return 4; | |
926 | } | |
927 | /* Unknown register. */ | |
928 | return 0; | |
929 | } | |
6f970bd9 | 930 | |
56aebc89 | 931 | #elif defined (TARGET_M68K) |
6f970bd9 | 932 | |
56aebc89 | 933 | #define NUM_CORE_REGS 18 |
6f970bd9 | 934 | |
56aebc89 | 935 | #define GDB_CORE_XML "cf-core.xml" |
6f970bd9 | 936 | |
56aebc89 PB |
937 | static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n) |
938 | { | |
939 | if (n < 8) { | |
940 | /* D0-D7 */ | |
941 | GET_REG32(env->dregs[n]); | |
942 | } else if (n < 16) { | |
943 | /* A0-A7 */ | |
944 | GET_REG32(env->aregs[n - 8]); | |
945 | } else { | |
946 | switch (n) { | |
947 | case 16: GET_REG32(env->sr); | |
948 | case 17: GET_REG32(env->pc); | |
949 | } | |
950 | } | |
951 | /* FP registers not included here because they vary between | |
952 | ColdFire and m68k. Use XML bits for these. */ | |
953 | return 0; | |
954 | } | |
8e33c08c | 955 | |
56aebc89 PB |
956 | static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n) |
957 | { | |
958 | uint32_t tmp; | |
8e33c08c | 959 | |
56aebc89 | 960 | tmp = ldl_p(mem_buf); |
8e33c08c | 961 | |
56aebc89 PB |
962 | if (n < 8) { |
963 | /* D0-D7 */ | |
964 | env->dregs[n] = tmp; | |
965 | } else if (n < 8) { | |
966 | /* A0-A7 */ | |
967 | env->aregs[n - 8] = tmp; | |
968 | } else { | |
969 | switch (n) { | |
970 | case 16: env->sr = tmp; break; | |
971 | case 17: env->pc = tmp; break; | |
972 | default: return 0; | |
973 | } | |
974 | } | |
975 | return 4; | |
976 | } | |
977 | #elif defined (TARGET_MIPS) | |
7ac256b8 | 978 | |
56aebc89 | 979 | #define NUM_CORE_REGS 73 |
7ac256b8 | 980 | |
56aebc89 PB |
981 | static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n) |
982 | { | |
983 | if (n < 32) { | |
984 | GET_REGL(env->active_tc.gpr[n]); | |
985 | } | |
986 | if (env->CP0_Config1 & (1 << CP0C1_FP)) { | |
987 | if (n >= 38 && n < 70) { | |
988 | if (env->CP0_Status & (1 << CP0St_FR)) | |
989 | GET_REGL(env->active_fpu.fpr[n - 38].d); | |
990 | else | |
991 | GET_REGL(env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX]); | |
992 | } | |
993 | switch (n) { | |
994 | case 70: GET_REGL((int32_t)env->active_fpu.fcr31); | |
995 | case 71: GET_REGL((int32_t)env->active_fpu.fcr0); | |
996 | } | |
997 | } | |
998 | switch (n) { | |
999 | case 32: GET_REGL((int32_t)env->CP0_Status); | |
1000 | case 33: GET_REGL(env->active_tc.LO[0]); | |
1001 | case 34: GET_REGL(env->active_tc.HI[0]); | |
1002 | case 35: GET_REGL(env->CP0_BadVAddr); | |
1003 | case 36: GET_REGL((int32_t)env->CP0_Cause); | |
1004 | case 37: GET_REGL(env->active_tc.PC); | |
1005 | case 72: GET_REGL(0); /* fp */ | |
1006 | case 89: GET_REGL((int32_t)env->CP0_PRid); | |
1007 | } | |
1008 | if (n >= 73 && n <= 88) { | |
1009 | /* 16 embedded regs. */ | |
1010 | GET_REGL(0); | |
1011 | } | |
6f970bd9 | 1012 | |
56aebc89 | 1013 | return 0; |
6f970bd9 FB |
1014 | } |
1015 | ||
8e33c08c TS |
1016 | /* convert MIPS rounding mode in FCR31 to IEEE library */ |
1017 | static unsigned int ieee_rm[] = | |
1018 | { | |
1019 | float_round_nearest_even, | |
1020 | float_round_to_zero, | |
1021 | float_round_up, | |
1022 | float_round_down | |
1023 | }; | |
1024 | #define RESTORE_ROUNDING_MODE \ | |
f01be154 | 1025 | set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status) |
8e33c08c | 1026 | |
56aebc89 | 1027 | static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n) |
6f970bd9 | 1028 | { |
56aebc89 | 1029 | target_ulong tmp; |
6f970bd9 | 1030 | |
56aebc89 | 1031 | tmp = ldtul_p(mem_buf); |
6f970bd9 | 1032 | |
56aebc89 PB |
1033 | if (n < 32) { |
1034 | env->active_tc.gpr[n] = tmp; | |
1035 | return sizeof(target_ulong); | |
1036 | } | |
1037 | if (env->CP0_Config1 & (1 << CP0C1_FP) | |
1038 | && n >= 38 && n < 73) { | |
1039 | if (n < 70) { | |
7ac256b8 | 1040 | if (env->CP0_Status & (1 << CP0St_FR)) |
56aebc89 | 1041 | env->active_fpu.fpr[n - 38].d = tmp; |
7ac256b8 | 1042 | else |
56aebc89 PB |
1043 | env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX] = tmp; |
1044 | } | |
1045 | switch (n) { | |
1046 | case 70: | |
1047 | env->active_fpu.fcr31 = tmp & 0xFF83FFFF; | |
1048 | /* set rounding mode */ | |
1049 | RESTORE_ROUNDING_MODE; | |
8e33c08c | 1050 | #ifndef CONFIG_SOFTFLOAT |
56aebc89 PB |
1051 | /* no floating point exception for native float */ |
1052 | SET_FP_ENABLE(env->active_fpu.fcr31, 0); | |
8e33c08c | 1053 | #endif |
56aebc89 PB |
1054 | break; |
1055 | case 71: env->active_fpu.fcr0 = tmp; break; | |
1056 | } | |
1057 | return sizeof(target_ulong); | |
1058 | } | |
1059 | switch (n) { | |
1060 | case 32: env->CP0_Status = tmp; break; | |
1061 | case 33: env->active_tc.LO[0] = tmp; break; | |
1062 | case 34: env->active_tc.HI[0] = tmp; break; | |
1063 | case 35: env->CP0_BadVAddr = tmp; break; | |
1064 | case 36: env->CP0_Cause = tmp; break; | |
1065 | case 37: env->active_tc.PC = tmp; break; | |
1066 | case 72: /* fp, ignored */ break; | |
1067 | default: | |
1068 | if (n > 89) | |
1069 | return 0; | |
1070 | /* Other registers are readonly. Ignore writes. */ | |
1071 | break; | |
1072 | } | |
1073 | ||
1074 | return sizeof(target_ulong); | |
6f970bd9 | 1075 | } |
fdf9b3e8 | 1076 | #elif defined (TARGET_SH4) |
6ef99fc5 TS |
1077 | |
1078 | /* Hint: Use "set architecture sh4" in GDB to see fpu registers */ | |
56aebc89 PB |
1079 | /* FIXME: We should use XML for this. */ |
1080 | ||
1081 | #define NUM_CORE_REGS 59 | |
6ef99fc5 | 1082 | |
56aebc89 | 1083 | static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n) |
fdf9b3e8 | 1084 | { |
56aebc89 PB |
1085 | if (n < 8) { |
1086 | if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) { | |
1087 | GET_REGL(env->gregs[n + 16]); | |
1088 | } else { | |
1089 | GET_REGL(env->gregs[n]); | |
1090 | } | |
1091 | } else if (n < 16) { | |
1092 | GET_REGL(env->gregs[n - 8]); | |
1093 | } else if (n >= 25 && n < 41) { | |
1094 | GET_REGL(env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)]); | |
1095 | } else if (n >= 43 && n < 51) { | |
1096 | GET_REGL(env->gregs[n - 43]); | |
1097 | } else if (n >= 51 && n < 59) { | |
1098 | GET_REGL(env->gregs[n - (51 - 16)]); | |
1099 | } | |
1100 | switch (n) { | |
1101 | case 16: GET_REGL(env->pc); | |
1102 | case 17: GET_REGL(env->pr); | |
1103 | case 18: GET_REGL(env->gbr); | |
1104 | case 19: GET_REGL(env->vbr); | |
1105 | case 20: GET_REGL(env->mach); | |
1106 | case 21: GET_REGL(env->macl); | |
1107 | case 22: GET_REGL(env->sr); | |
1108 | case 23: GET_REGL(env->fpul); | |
1109 | case 24: GET_REGL(env->fpscr); | |
1110 | case 41: GET_REGL(env->ssr); | |
1111 | case 42: GET_REGL(env->spc); | |
1112 | } | |
1113 | ||
1114 | return 0; | |
fdf9b3e8 FB |
1115 | } |
1116 | ||
56aebc89 | 1117 | static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n) |
fdf9b3e8 | 1118 | { |
56aebc89 PB |
1119 | uint32_t tmp; |
1120 | ||
1121 | tmp = ldl_p(mem_buf); | |
1122 | ||
1123 | if (n < 8) { | |
1124 | if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) { | |
1125 | env->gregs[n + 16] = tmp; | |
1126 | } else { | |
1127 | env->gregs[n] = tmp; | |
1128 | } | |
1129 | return 4; | |
1130 | } else if (n < 16) { | |
1131 | env->gregs[n - 8] = tmp; | |
1132 | return 4; | |
1133 | } else if (n >= 25 && n < 41) { | |
1134 | env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)] = tmp; | |
1135 | } else if (n >= 43 && n < 51) { | |
1136 | env->gregs[n - 43] = tmp; | |
1137 | return 4; | |
1138 | } else if (n >= 51 && n < 59) { | |
1139 | env->gregs[n - (51 - 16)] = tmp; | |
1140 | return 4; | |
1141 | } | |
1142 | switch (n) { | |
1143 | case 16: env->pc = tmp; | |
1144 | case 17: env->pr = tmp; | |
1145 | case 18: env->gbr = tmp; | |
1146 | case 19: env->vbr = tmp; | |
1147 | case 20: env->mach = tmp; | |
1148 | case 21: env->macl = tmp; | |
1149 | case 22: env->sr = tmp; | |
1150 | case 23: env->fpul = tmp; | |
1151 | case 24: env->fpscr = tmp; | |
1152 | case 41: env->ssr = tmp; | |
1153 | case 42: env->spc = tmp; | |
1154 | default: return 0; | |
1155 | } | |
1156 | ||
1157 | return 4; | |
fdf9b3e8 | 1158 | } |
f1ccf904 TS |
1159 | #elif defined (TARGET_CRIS) |
1160 | ||
56aebc89 PB |
1161 | #define NUM_CORE_REGS 49 |
1162 | ||
1163 | static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n) | |
f1ccf904 | 1164 | { |
56aebc89 PB |
1165 | uint8_t srs; |
1166 | ||
1167 | srs = env->pregs[PR_SRS]; | |
1168 | if (n < 16) { | |
1169 | GET_REG32(env->regs[n]); | |
1170 | } | |
1171 | ||
1172 | if (n >= 21 && n < 32) { | |
1173 | GET_REG32(env->pregs[n - 16]); | |
1174 | } | |
1175 | if (n >= 33 && n < 49) { | |
1176 | GET_REG32(env->sregs[srs][n - 33]); | |
1177 | } | |
1178 | switch (n) { | |
1179 | case 16: GET_REG8(env->pregs[0]); | |
1180 | case 17: GET_REG8(env->pregs[1]); | |
1181 | case 18: GET_REG32(env->pregs[2]); | |
1182 | case 19: GET_REG8(srs); | |
1183 | case 20: GET_REG16(env->pregs[4]); | |
1184 | case 32: GET_REG32(env->pc); | |
1185 | } | |
1186 | ||
1187 | return 0; | |
f1ccf904 | 1188 | } |
56aebc89 PB |
1189 | |
1190 | static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n) | |
f1ccf904 | 1191 | { |
56aebc89 PB |
1192 | uint32_t tmp; |
1193 | ||
1194 | if (n > 49) | |
1195 | return 0; | |
1196 | ||
1197 | tmp = ldl_p(mem_buf); | |
1198 | ||
1199 | if (n < 16) { | |
1200 | env->regs[n] = tmp; | |
1201 | } | |
1202 | ||
d7b6967a EI |
1203 | if (n >= 21 && n < 32) { |
1204 | env->pregs[n - 16] = tmp; | |
1205 | } | |
1206 | ||
1207 | /* FIXME: Should support function regs be writable? */ | |
56aebc89 PB |
1208 | switch (n) { |
1209 | case 16: return 1; | |
1210 | case 17: return 1; | |
d7b6967a | 1211 | case 18: env->pregs[PR_PID] = tmp; break; |
56aebc89 PB |
1212 | case 19: return 1; |
1213 | case 20: return 2; | |
1214 | case 32: env->pc = tmp; break; | |
1215 | } | |
1216 | ||
1217 | return 4; | |
f1ccf904 | 1218 | } |
19bf517b AJ |
1219 | #elif defined (TARGET_ALPHA) |
1220 | ||
1221 | #define NUM_CORE_REGS 65 | |
1222 | ||
1223 | static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n) | |
1224 | { | |
1225 | if (n < 31) { | |
1226 | GET_REGL(env->ir[n]); | |
1227 | } | |
1228 | else if (n == 31) { | |
1229 | GET_REGL(0); | |
1230 | } | |
1231 | else if (n<63) { | |
1232 | uint64_t val; | |
1233 | ||
1234 | val=*((uint64_t *)&env->fir[n-32]); | |
1235 | GET_REGL(val); | |
1236 | } | |
1237 | else if (n==63) { | |
1238 | GET_REGL(env->fpcr); | |
1239 | } | |
1240 | else if (n==64) { | |
1241 | GET_REGL(env->pc); | |
1242 | } | |
1243 | else { | |
1244 | GET_REGL(0); | |
1245 | } | |
1246 | ||
1247 | return 0; | |
1248 | } | |
1249 | ||
1250 | static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n) | |
1251 | { | |
1252 | target_ulong tmp; | |
1253 | tmp = ldtul_p(mem_buf); | |
1254 | ||
1255 | if (n < 31) { | |
1256 | env->ir[n] = tmp; | |
1257 | } | |
1258 | ||
1259 | if (n > 31 && n < 63) { | |
1260 | env->fir[n - 32] = ldfl_p(mem_buf); | |
1261 | } | |
1262 | ||
1263 | if (n == 64 ) { | |
1264 | env->pc=tmp; | |
1265 | } | |
1266 | ||
1267 | return 8; | |
1268 | } | |
56aebc89 PB |
1269 | #else |
1270 | ||
1271 | #define NUM_CORE_REGS 0 | |
1272 | ||
1273 | static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n) | |
f1ccf904 | 1274 | { |
56aebc89 | 1275 | return 0; |
f1ccf904 TS |
1276 | } |
1277 | ||
56aebc89 | 1278 | static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n) |
f1ccf904 | 1279 | { |
56aebc89 PB |
1280 | return 0; |
1281 | } | |
f1ccf904 | 1282 | |
56aebc89 | 1283 | #endif |
f1ccf904 | 1284 | |
56aebc89 | 1285 | static int num_g_regs = NUM_CORE_REGS; |
f1ccf904 | 1286 | |
56aebc89 PB |
1287 | #ifdef GDB_CORE_XML |
1288 | /* Encode data using the encoding for 'x' packets. */ | |
1289 | static int memtox(char *buf, const char *mem, int len) | |
1290 | { | |
1291 | char *p = buf; | |
1292 | char c; | |
1293 | ||
1294 | while (len--) { | |
1295 | c = *(mem++); | |
1296 | switch (c) { | |
1297 | case '#': case '$': case '*': case '}': | |
1298 | *(p++) = '}'; | |
1299 | *(p++) = c ^ 0x20; | |
1300 | break; | |
1301 | default: | |
1302 | *(p++) = c; | |
1303 | break; | |
1304 | } | |
1305 | } | |
1306 | return p - buf; | |
1307 | } | |
f1ccf904 | 1308 | |
3faf778e | 1309 | static const char *get_feature_xml(const char *p, const char **newp) |
56aebc89 PB |
1310 | { |
1311 | extern const char *const xml_builtin[][2]; | |
1312 | size_t len; | |
1313 | int i; | |
1314 | const char *name; | |
1315 | static char target_xml[1024]; | |
1316 | ||
1317 | len = 0; | |
1318 | while (p[len] && p[len] != ':') | |
1319 | len++; | |
1320 | *newp = p + len; | |
1321 | ||
1322 | name = NULL; | |
1323 | if (strncmp(p, "target.xml", len) == 0) { | |
1324 | /* Generate the XML description for this CPU. */ | |
1325 | if (!target_xml[0]) { | |
1326 | GDBRegisterState *r; | |
1327 | ||
5b3715bf BS |
1328 | snprintf(target_xml, sizeof(target_xml), |
1329 | "<?xml version=\"1.0\"?>" | |
1330 | "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">" | |
1331 | "<target>" | |
1332 | "<xi:include href=\"%s\"/>", | |
1333 | GDB_CORE_XML); | |
56aebc89 | 1334 | |
880a7578 | 1335 | for (r = first_cpu->gdb_regs; r; r = r->next) { |
56aebc89 PB |
1336 | strcat(target_xml, "<xi:include href=\""); |
1337 | strcat(target_xml, r->xml); | |
1338 | strcat(target_xml, "\"/>"); | |
1339 | } | |
1340 | strcat(target_xml, "</target>"); | |
1341 | } | |
1342 | return target_xml; | |
1343 | } | |
1344 | for (i = 0; ; i++) { | |
1345 | name = xml_builtin[i][0]; | |
1346 | if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len)) | |
1347 | break; | |
1348 | } | |
1349 | return name ? xml_builtin[i][1] : NULL; | |
1350 | } | |
1351 | #endif | |
f1ccf904 | 1352 | |
56aebc89 PB |
1353 | static int gdb_read_register(CPUState *env, uint8_t *mem_buf, int reg) |
1354 | { | |
1355 | GDBRegisterState *r; | |
f1ccf904 | 1356 | |
56aebc89 PB |
1357 | if (reg < NUM_CORE_REGS) |
1358 | return cpu_gdb_read_register(env, mem_buf, reg); | |
f1ccf904 | 1359 | |
56aebc89 PB |
1360 | for (r = env->gdb_regs; r; r = r->next) { |
1361 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { | |
1362 | return r->get_reg(env, mem_buf, reg - r->base_reg); | |
1363 | } | |
1364 | } | |
1365 | return 0; | |
f1ccf904 TS |
1366 | } |
1367 | ||
56aebc89 | 1368 | static int gdb_write_register(CPUState *env, uint8_t *mem_buf, int reg) |
f1ccf904 | 1369 | { |
56aebc89 | 1370 | GDBRegisterState *r; |
f1ccf904 | 1371 | |
56aebc89 PB |
1372 | if (reg < NUM_CORE_REGS) |
1373 | return cpu_gdb_write_register(env, mem_buf, reg); | |
1374 | ||
1375 | for (r = env->gdb_regs; r; r = r->next) { | |
1376 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { | |
1377 | return r->set_reg(env, mem_buf, reg - r->base_reg); | |
1378 | } | |
1379 | } | |
6da41eaf FB |
1380 | return 0; |
1381 | } | |
1382 | ||
56aebc89 PB |
1383 | /* Register a supplemental set of CPU registers. If g_pos is nonzero it |
1384 | specifies the first register number and these registers are included in | |
1385 | a standard "g" packet. Direction is relative to gdb, i.e. get_reg is | |
1386 | gdb reading a CPU register, and set_reg is gdb modifying a CPU register. | |
1387 | */ | |
1388 | ||
1389 | void gdb_register_coprocessor(CPUState * env, | |
1390 | gdb_reg_cb get_reg, gdb_reg_cb set_reg, | |
1391 | int num_regs, const char *xml, int g_pos) | |
6da41eaf | 1392 | { |
56aebc89 PB |
1393 | GDBRegisterState *s; |
1394 | GDBRegisterState **p; | |
1395 | static int last_reg = NUM_CORE_REGS; | |
1396 | ||
1397 | s = (GDBRegisterState *)qemu_mallocz(sizeof(GDBRegisterState)); | |
1398 | s->base_reg = last_reg; | |
1399 | s->num_regs = num_regs; | |
1400 | s->get_reg = get_reg; | |
1401 | s->set_reg = set_reg; | |
1402 | s->xml = xml; | |
1403 | p = &env->gdb_regs; | |
1404 | while (*p) { | |
1405 | /* Check for duplicates. */ | |
1406 | if (strcmp((*p)->xml, xml) == 0) | |
1407 | return; | |
1408 | p = &(*p)->next; | |
1409 | } | |
1410 | /* Add to end of list. */ | |
1411 | last_reg += num_regs; | |
1412 | *p = s; | |
1413 | if (g_pos) { | |
1414 | if (g_pos != s->base_reg) { | |
1415 | fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n" | |
1416 | "Expected %d got %d\n", xml, g_pos, s->base_reg); | |
1417 | } else { | |
1418 | num_g_regs = last_reg; | |
1419 | } | |
1420 | } | |
6da41eaf FB |
1421 | } |
1422 | ||
a1d1bb31 AL |
1423 | #ifndef CONFIG_USER_ONLY |
1424 | static const int xlat_gdb_type[] = { | |
1425 | [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE, | |
1426 | [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ, | |
1427 | [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS, | |
1428 | }; | |
1429 | #endif | |
1430 | ||
880a7578 | 1431 | static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type) |
a1d1bb31 | 1432 | { |
880a7578 AL |
1433 | CPUState *env; |
1434 | int err = 0; | |
1435 | ||
e22a25c9 AL |
1436 | if (kvm_enabled()) |
1437 | return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type); | |
1438 | ||
a1d1bb31 AL |
1439 | switch (type) { |
1440 | case GDB_BREAKPOINT_SW: | |
1441 | case GDB_BREAKPOINT_HW: | |
880a7578 AL |
1442 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
1443 | err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL); | |
1444 | if (err) | |
1445 | break; | |
1446 | } | |
1447 | return err; | |
a1d1bb31 AL |
1448 | #ifndef CONFIG_USER_ONLY |
1449 | case GDB_WATCHPOINT_WRITE: | |
1450 | case GDB_WATCHPOINT_READ: | |
1451 | case GDB_WATCHPOINT_ACCESS: | |
880a7578 AL |
1452 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
1453 | err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type], | |
1454 | NULL); | |
1455 | if (err) | |
1456 | break; | |
1457 | } | |
1458 | return err; | |
a1d1bb31 AL |
1459 | #endif |
1460 | default: | |
1461 | return -ENOSYS; | |
1462 | } | |
1463 | } | |
1464 | ||
880a7578 | 1465 | static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type) |
a1d1bb31 | 1466 | { |
880a7578 AL |
1467 | CPUState *env; |
1468 | int err = 0; | |
1469 | ||
e22a25c9 AL |
1470 | if (kvm_enabled()) |
1471 | return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type); | |
1472 | ||
a1d1bb31 AL |
1473 | switch (type) { |
1474 | case GDB_BREAKPOINT_SW: | |
1475 | case GDB_BREAKPOINT_HW: | |
880a7578 AL |
1476 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
1477 | err = cpu_breakpoint_remove(env, addr, BP_GDB); | |
1478 | if (err) | |
1479 | break; | |
1480 | } | |
1481 | return err; | |
a1d1bb31 AL |
1482 | #ifndef CONFIG_USER_ONLY |
1483 | case GDB_WATCHPOINT_WRITE: | |
1484 | case GDB_WATCHPOINT_READ: | |
1485 | case GDB_WATCHPOINT_ACCESS: | |
880a7578 AL |
1486 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
1487 | err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]); | |
1488 | if (err) | |
1489 | break; | |
1490 | } | |
1491 | return err; | |
a1d1bb31 AL |
1492 | #endif |
1493 | default: | |
1494 | return -ENOSYS; | |
1495 | } | |
1496 | } | |
1497 | ||
880a7578 | 1498 | static void gdb_breakpoint_remove_all(void) |
a1d1bb31 | 1499 | { |
880a7578 AL |
1500 | CPUState *env; |
1501 | ||
e22a25c9 AL |
1502 | if (kvm_enabled()) { |
1503 | kvm_remove_all_breakpoints(gdbserver_state->c_cpu); | |
1504 | return; | |
1505 | } | |
1506 | ||
880a7578 AL |
1507 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
1508 | cpu_breakpoint_remove_all(env, BP_GDB); | |
a1d1bb31 | 1509 | #ifndef CONFIG_USER_ONLY |
880a7578 | 1510 | cpu_watchpoint_remove_all(env, BP_GDB); |
a1d1bb31 | 1511 | #endif |
880a7578 | 1512 | } |
a1d1bb31 AL |
1513 | } |
1514 | ||
fab9d284 AJ |
1515 | static void gdb_set_cpu_pc(GDBState *s, target_ulong pc) |
1516 | { | |
1517 | #if defined(TARGET_I386) | |
1518 | s->c_cpu->eip = pc; | |
1519 | cpu_synchronize_state(s->c_cpu, 1); | |
1520 | #elif defined (TARGET_PPC) | |
1521 | s->c_cpu->nip = pc; | |
1522 | #elif defined (TARGET_SPARC) | |
1523 | s->c_cpu->pc = pc; | |
1524 | s->c_cpu->npc = pc + 4; | |
1525 | #elif defined (TARGET_ARM) | |
1526 | s->c_cpu->regs[15] = pc; | |
1527 | #elif defined (TARGET_SH4) | |
1528 | s->c_cpu->pc = pc; | |
1529 | #elif defined (TARGET_MIPS) | |
1530 | s->c_cpu->active_tc.PC = pc; | |
1531 | #elif defined (TARGET_CRIS) | |
1532 | s->c_cpu->pc = pc; | |
1533 | #elif defined (TARGET_ALPHA) | |
1534 | s->c_cpu->pc = pc; | |
1535 | #endif | |
1536 | } | |
1537 | ||
880a7578 | 1538 | static int gdb_handle_packet(GDBState *s, const char *line_buf) |
b4608c04 | 1539 | { |
880a7578 | 1540 | CPUState *env; |
b4608c04 | 1541 | const char *p; |
880a7578 | 1542 | int ch, reg_size, type, res, thread; |
56aebc89 PB |
1543 | char buf[MAX_PACKET_LENGTH]; |
1544 | uint8_t mem_buf[MAX_PACKET_LENGTH]; | |
1545 | uint8_t *registers; | |
9d9754a3 | 1546 | target_ulong addr, len; |
3b46e624 | 1547 | |
858693c6 FB |
1548 | #ifdef DEBUG_GDB |
1549 | printf("command='%s'\n", line_buf); | |
1550 | #endif | |
1551 | p = line_buf; | |
1552 | ch = *p++; | |
1553 | switch(ch) { | |
1554 | case '?': | |
1fddef4b | 1555 | /* TODO: Make this return the correct value for user-mode. */ |
ca587a8e | 1556 | snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP, |
880a7578 | 1557 | s->c_cpu->cpu_index+1); |
858693c6 | 1558 | put_packet(s, buf); |
7d03f82f EI |
1559 | /* Remove all the breakpoints when this query is issued, |
1560 | * because gdb is doing and initial connect and the state | |
1561 | * should be cleaned up. | |
1562 | */ | |
880a7578 | 1563 | gdb_breakpoint_remove_all(); |
858693c6 FB |
1564 | break; |
1565 | case 'c': | |
1566 | if (*p != '\0') { | |
9d9754a3 | 1567 | addr = strtoull(p, (char **)&p, 16); |
fab9d284 | 1568 | gdb_set_cpu_pc(s, addr); |
858693c6 | 1569 | } |
ca587a8e | 1570 | s->signal = 0; |
ba70a624 | 1571 | gdb_continue(s); |
41625033 | 1572 | return RS_IDLE; |
1f487ee9 | 1573 | case 'C': |
ca587a8e AJ |
1574 | s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16)); |
1575 | if (s->signal == -1) | |
1576 | s->signal = 0; | |
1f487ee9 EI |
1577 | gdb_continue(s); |
1578 | return RS_IDLE; | |
7d03f82f EI |
1579 | case 'k': |
1580 | /* Kill the target */ | |
1581 | fprintf(stderr, "\nQEMU: Terminated via GDBstub\n"); | |
1582 | exit(0); | |
1583 | case 'D': | |
1584 | /* Detach packet */ | |
880a7578 | 1585 | gdb_breakpoint_remove_all(); |
7d03f82f EI |
1586 | gdb_continue(s); |
1587 | put_packet(s, "OK"); | |
1588 | break; | |
858693c6 FB |
1589 | case 's': |
1590 | if (*p != '\0') { | |
8fac5803 | 1591 | addr = strtoull(p, (char **)&p, 16); |
fab9d284 | 1592 | gdb_set_cpu_pc(s, addr); |
858693c6 | 1593 | } |
880a7578 | 1594 | cpu_single_step(s->c_cpu, sstep_flags); |
ba70a624 | 1595 | gdb_continue(s); |
41625033 | 1596 | return RS_IDLE; |
a2d1ebaf PB |
1597 | case 'F': |
1598 | { | |
1599 | target_ulong ret; | |
1600 | target_ulong err; | |
1601 | ||
1602 | ret = strtoull(p, (char **)&p, 16); | |
1603 | if (*p == ',') { | |
1604 | p++; | |
1605 | err = strtoull(p, (char **)&p, 16); | |
1606 | } else { | |
1607 | err = 0; | |
1608 | } | |
1609 | if (*p == ',') | |
1610 | p++; | |
1611 | type = *p; | |
1612 | if (gdb_current_syscall_cb) | |
880a7578 | 1613 | gdb_current_syscall_cb(s->c_cpu, ret, err); |
a2d1ebaf PB |
1614 | if (type == 'C') { |
1615 | put_packet(s, "T02"); | |
1616 | } else { | |
ba70a624 | 1617 | gdb_continue(s); |
a2d1ebaf PB |
1618 | } |
1619 | } | |
1620 | break; | |
858693c6 | 1621 | case 'g': |
e22a25c9 | 1622 | cpu_synchronize_state(s->g_cpu, 0); |
56aebc89 PB |
1623 | len = 0; |
1624 | for (addr = 0; addr < num_g_regs; addr++) { | |
880a7578 | 1625 | reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr); |
56aebc89 PB |
1626 | len += reg_size; |
1627 | } | |
1628 | memtohex(buf, mem_buf, len); | |
858693c6 FB |
1629 | put_packet(s, buf); |
1630 | break; | |
1631 | case 'G': | |
56aebc89 | 1632 | registers = mem_buf; |
858693c6 FB |
1633 | len = strlen(p) / 2; |
1634 | hextomem((uint8_t *)registers, p, len); | |
56aebc89 | 1635 | for (addr = 0; addr < num_g_regs && len > 0; addr++) { |
880a7578 | 1636 | reg_size = gdb_write_register(s->g_cpu, registers, addr); |
56aebc89 PB |
1637 | len -= reg_size; |
1638 | registers += reg_size; | |
1639 | } | |
e22a25c9 | 1640 | cpu_synchronize_state(s->g_cpu, 1); |
858693c6 FB |
1641 | put_packet(s, "OK"); |
1642 | break; | |
1643 | case 'm': | |
9d9754a3 | 1644 | addr = strtoull(p, (char **)&p, 16); |
858693c6 FB |
1645 | if (*p == ',') |
1646 | p++; | |
9d9754a3 | 1647 | len = strtoull(p, NULL, 16); |
880a7578 | 1648 | if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 0) != 0) { |
6f970bd9 FB |
1649 | put_packet (s, "E14"); |
1650 | } else { | |
1651 | memtohex(buf, mem_buf, len); | |
1652 | put_packet(s, buf); | |
1653 | } | |
858693c6 FB |
1654 | break; |
1655 | case 'M': | |
9d9754a3 | 1656 | addr = strtoull(p, (char **)&p, 16); |
858693c6 FB |
1657 | if (*p == ',') |
1658 | p++; | |
9d9754a3 | 1659 | len = strtoull(p, (char **)&p, 16); |
b328f873 | 1660 | if (*p == ':') |
858693c6 FB |
1661 | p++; |
1662 | hextomem(mem_buf, p, len); | |
880a7578 | 1663 | if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 1) != 0) |
905f20b1 | 1664 | put_packet(s, "E14"); |
858693c6 FB |
1665 | else |
1666 | put_packet(s, "OK"); | |
1667 | break; | |
56aebc89 PB |
1668 | case 'p': |
1669 | /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable. | |
1670 | This works, but can be very slow. Anything new enough to | |
1671 | understand XML also knows how to use this properly. */ | |
1672 | if (!gdb_has_xml) | |
1673 | goto unknown_command; | |
1674 | addr = strtoull(p, (char **)&p, 16); | |
880a7578 | 1675 | reg_size = gdb_read_register(s->g_cpu, mem_buf, addr); |
56aebc89 PB |
1676 | if (reg_size) { |
1677 | memtohex(buf, mem_buf, reg_size); | |
1678 | put_packet(s, buf); | |
1679 | } else { | |
1680 | put_packet(s, "E14"); | |
1681 | } | |
1682 | break; | |
1683 | case 'P': | |
1684 | if (!gdb_has_xml) | |
1685 | goto unknown_command; | |
1686 | addr = strtoull(p, (char **)&p, 16); | |
1687 | if (*p == '=') | |
1688 | p++; | |
1689 | reg_size = strlen(p) / 2; | |
1690 | hextomem(mem_buf, p, reg_size); | |
880a7578 | 1691 | gdb_write_register(s->g_cpu, mem_buf, addr); |
56aebc89 PB |
1692 | put_packet(s, "OK"); |
1693 | break; | |
858693c6 | 1694 | case 'Z': |
858693c6 FB |
1695 | case 'z': |
1696 | type = strtoul(p, (char **)&p, 16); | |
1697 | if (*p == ',') | |
1698 | p++; | |
9d9754a3 | 1699 | addr = strtoull(p, (char **)&p, 16); |
858693c6 FB |
1700 | if (*p == ',') |
1701 | p++; | |
9d9754a3 | 1702 | len = strtoull(p, (char **)&p, 16); |
a1d1bb31 | 1703 | if (ch == 'Z') |
880a7578 | 1704 | res = gdb_breakpoint_insert(addr, len, type); |
a1d1bb31 | 1705 | else |
880a7578 | 1706 | res = gdb_breakpoint_remove(addr, len, type); |
a1d1bb31 AL |
1707 | if (res >= 0) |
1708 | put_packet(s, "OK"); | |
1709 | else if (res == -ENOSYS) | |
0f459d16 | 1710 | put_packet(s, ""); |
a1d1bb31 AL |
1711 | else |
1712 | put_packet(s, "E22"); | |
858693c6 | 1713 | break; |
880a7578 AL |
1714 | case 'H': |
1715 | type = *p++; | |
1716 | thread = strtoull(p, (char **)&p, 16); | |
1717 | if (thread == -1 || thread == 0) { | |
1718 | put_packet(s, "OK"); | |
1719 | break; | |
1720 | } | |
1721 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
1722 | if (env->cpu_index + 1 == thread) | |
1723 | break; | |
1724 | if (env == NULL) { | |
1725 | put_packet(s, "E22"); | |
1726 | break; | |
1727 | } | |
1728 | switch (type) { | |
1729 | case 'c': | |
1730 | s->c_cpu = env; | |
1731 | put_packet(s, "OK"); | |
1732 | break; | |
1733 | case 'g': | |
1734 | s->g_cpu = env; | |
1735 | put_packet(s, "OK"); | |
1736 | break; | |
1737 | default: | |
1738 | put_packet(s, "E22"); | |
1739 | break; | |
1740 | } | |
1741 | break; | |
1742 | case 'T': | |
1743 | thread = strtoull(p, (char **)&p, 16); | |
1744 | #ifndef CONFIG_USER_ONLY | |
1745 | if (thread > 0 && thread < smp_cpus + 1) | |
1746 | #else | |
1747 | if (thread == 1) | |
1748 | #endif | |
1749 | put_packet(s, "OK"); | |
1750 | else | |
1751 | put_packet(s, "E22"); | |
1752 | break; | |
978efd6a | 1753 | case 'q': |
60897d36 EI |
1754 | case 'Q': |
1755 | /* parse any 'q' packets here */ | |
1756 | if (!strcmp(p,"qemu.sstepbits")) { | |
1757 | /* Query Breakpoint bit definitions */ | |
363a37d5 BS |
1758 | snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", |
1759 | SSTEP_ENABLE, | |
1760 | SSTEP_NOIRQ, | |
1761 | SSTEP_NOTIMER); | |
60897d36 EI |
1762 | put_packet(s, buf); |
1763 | break; | |
1764 | } else if (strncmp(p,"qemu.sstep",10) == 0) { | |
1765 | /* Display or change the sstep_flags */ | |
1766 | p += 10; | |
1767 | if (*p != '=') { | |
1768 | /* Display current setting */ | |
363a37d5 | 1769 | snprintf(buf, sizeof(buf), "0x%x", sstep_flags); |
60897d36 EI |
1770 | put_packet(s, buf); |
1771 | break; | |
1772 | } | |
1773 | p++; | |
1774 | type = strtoul(p, (char **)&p, 16); | |
1775 | sstep_flags = type; | |
1776 | put_packet(s, "OK"); | |
1777 | break; | |
880a7578 AL |
1778 | } else if (strcmp(p,"C") == 0) { |
1779 | /* "Current thread" remains vague in the spec, so always return | |
1780 | * the first CPU (gdb returns the first thread). */ | |
1781 | put_packet(s, "QC1"); | |
1782 | break; | |
1783 | } else if (strcmp(p,"fThreadInfo") == 0) { | |
1784 | s->query_cpu = first_cpu; | |
1785 | goto report_cpuinfo; | |
1786 | } else if (strcmp(p,"sThreadInfo") == 0) { | |
1787 | report_cpuinfo: | |
1788 | if (s->query_cpu) { | |
1789 | snprintf(buf, sizeof(buf), "m%x", s->query_cpu->cpu_index+1); | |
1790 | put_packet(s, buf); | |
1791 | s->query_cpu = s->query_cpu->next_cpu; | |
1792 | } else | |
1793 | put_packet(s, "l"); | |
1794 | break; | |
1795 | } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) { | |
1796 | thread = strtoull(p+16, (char **)&p, 16); | |
1797 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
1798 | if (env->cpu_index + 1 == thread) { | |
e22a25c9 | 1799 | cpu_synchronize_state(env, 0); |
880a7578 AL |
1800 | len = snprintf((char *)mem_buf, sizeof(mem_buf), |
1801 | "CPU#%d [%s]", env->cpu_index, | |
1802 | env->halted ? "halted " : "running"); | |
1803 | memtohex(buf, mem_buf, len); | |
1804 | put_packet(s, buf); | |
1805 | break; | |
1806 | } | |
1807 | break; | |
60897d36 | 1808 | } |
0b8a988c | 1809 | #ifdef CONFIG_USER_ONLY |
60897d36 | 1810 | else if (strncmp(p, "Offsets", 7) == 0) { |
880a7578 | 1811 | TaskState *ts = s->c_cpu->opaque; |
978efd6a | 1812 | |
363a37d5 BS |
1813 | snprintf(buf, sizeof(buf), |
1814 | "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx | |
1815 | ";Bss=" TARGET_ABI_FMT_lx, | |
1816 | ts->info->code_offset, | |
1817 | ts->info->data_offset, | |
1818 | ts->info->data_offset); | |
978efd6a PB |
1819 | put_packet(s, buf); |
1820 | break; | |
1821 | } | |
0b8a988c | 1822 | #else /* !CONFIG_USER_ONLY */ |
8a34a0fb AL |
1823 | else if (strncmp(p, "Rcmd,", 5) == 0) { |
1824 | int len = strlen(p + 5); | |
1825 | ||
1826 | if ((len % 2) != 0) { | |
1827 | put_packet(s, "E01"); | |
1828 | break; | |
1829 | } | |
1830 | hextomem(mem_buf, p + 5, len); | |
1831 | len = len / 2; | |
1832 | mem_buf[len++] = 0; | |
1833 | qemu_chr_read(s->mon_chr, mem_buf, len); | |
1834 | put_packet(s, "OK"); | |
1835 | break; | |
1836 | } | |
0b8a988c | 1837 | #endif /* !CONFIG_USER_ONLY */ |
56aebc89 | 1838 | if (strncmp(p, "Supported", 9) == 0) { |
5b3715bf | 1839 | snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH); |
56aebc89 PB |
1840 | #ifdef GDB_CORE_XML |
1841 | strcat(buf, ";qXfer:features:read+"); | |
1842 | #endif | |
1843 | put_packet(s, buf); | |
1844 | break; | |
1845 | } | |
1846 | #ifdef GDB_CORE_XML | |
1847 | if (strncmp(p, "Xfer:features:read:", 19) == 0) { | |
1848 | const char *xml; | |
1849 | target_ulong total_len; | |
1850 | ||
1851 | gdb_has_xml = 1; | |
1852 | p += 19; | |
880a7578 | 1853 | xml = get_feature_xml(p, &p); |
56aebc89 | 1854 | if (!xml) { |
5b3715bf | 1855 | snprintf(buf, sizeof(buf), "E00"); |
56aebc89 PB |
1856 | put_packet(s, buf); |
1857 | break; | |
1858 | } | |
1859 | ||
1860 | if (*p == ':') | |
1861 | p++; | |
1862 | addr = strtoul(p, (char **)&p, 16); | |
1863 | if (*p == ',') | |
1864 | p++; | |
1865 | len = strtoul(p, (char **)&p, 16); | |
1866 | ||
1867 | total_len = strlen(xml); | |
1868 | if (addr > total_len) { | |
5b3715bf | 1869 | snprintf(buf, sizeof(buf), "E00"); |
56aebc89 PB |
1870 | put_packet(s, buf); |
1871 | break; | |
1872 | } | |
1873 | if (len > (MAX_PACKET_LENGTH - 5) / 2) | |
1874 | len = (MAX_PACKET_LENGTH - 5) / 2; | |
1875 | if (len < total_len - addr) { | |
1876 | buf[0] = 'm'; | |
1877 | len = memtox(buf + 1, xml + addr, len); | |
1878 | } else { | |
1879 | buf[0] = 'l'; | |
1880 | len = memtox(buf + 1, xml + addr, total_len - addr); | |
1881 | } | |
1882 | put_packet_binary(s, buf, len + 1); | |
1883 | break; | |
1884 | } | |
1885 | #endif | |
1886 | /* Unrecognised 'q' command. */ | |
1887 | goto unknown_command; | |
1888 | ||
858693c6 | 1889 | default: |
56aebc89 | 1890 | unknown_command: |
858693c6 FB |
1891 | /* put empty packet */ |
1892 | buf[0] = '\0'; | |
1893 | put_packet(s, buf); | |
1894 | break; | |
1895 | } | |
1896 | return RS_IDLE; | |
1897 | } | |
1898 | ||
880a7578 AL |
1899 | void gdb_set_stop_cpu(CPUState *env) |
1900 | { | |
1901 | gdbserver_state->c_cpu = env; | |
1902 | gdbserver_state->g_cpu = env; | |
1903 | } | |
1904 | ||
1fddef4b | 1905 | #ifndef CONFIG_USER_ONLY |
9781e040 | 1906 | static void gdb_vm_state_change(void *opaque, int running, int reason) |
858693c6 | 1907 | { |
880a7578 AL |
1908 | GDBState *s = gdbserver_state; |
1909 | CPUState *env = s->c_cpu; | |
858693c6 | 1910 | char buf[256]; |
d6fc1b39 | 1911 | const char *type; |
858693c6 FB |
1912 | int ret; |
1913 | ||
9781e040 | 1914 | if (running || (reason != EXCP_DEBUG && reason != EXCP_INTERRUPT) || |
36556b20 | 1915 | s->state == RS_INACTIVE || s->state == RS_SYSCALL) |
a2d1ebaf PB |
1916 | return; |
1917 | ||
858693c6 | 1918 | /* disable single step if it was enable */ |
880a7578 | 1919 | cpu_single_step(env, 0); |
858693c6 | 1920 | |
e80cfcfc | 1921 | if (reason == EXCP_DEBUG) { |
880a7578 AL |
1922 | if (env->watchpoint_hit) { |
1923 | switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) { | |
a1d1bb31 | 1924 | case BP_MEM_READ: |
d6fc1b39 AL |
1925 | type = "r"; |
1926 | break; | |
a1d1bb31 | 1927 | case BP_MEM_ACCESS: |
d6fc1b39 AL |
1928 | type = "a"; |
1929 | break; | |
1930 | default: | |
1931 | type = ""; | |
1932 | break; | |
1933 | } | |
880a7578 AL |
1934 | snprintf(buf, sizeof(buf), |
1935 | "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";", | |
ca587a8e | 1936 | GDB_SIGNAL_TRAP, env->cpu_index+1, type, |
880a7578 | 1937 | env->watchpoint_hit->vaddr); |
6658ffb8 | 1938 | put_packet(s, buf); |
880a7578 | 1939 | env->watchpoint_hit = NULL; |
6658ffb8 PB |
1940 | return; |
1941 | } | |
880a7578 | 1942 | tb_flush(env); |
ca587a8e | 1943 | ret = GDB_SIGNAL_TRAP; |
bbeb7b5c | 1944 | } else { |
9781e040 | 1945 | ret = GDB_SIGNAL_INT; |
bbeb7b5c | 1946 | } |
880a7578 | 1947 | snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, env->cpu_index+1); |
858693c6 FB |
1948 | put_packet(s, buf); |
1949 | } | |
1fddef4b | 1950 | #endif |
858693c6 | 1951 | |
a2d1ebaf PB |
1952 | /* Send a gdb syscall request. |
1953 | This accepts limited printf-style format specifiers, specifically: | |
a87295e8 PB |
1954 | %x - target_ulong argument printed in hex. |
1955 | %lx - 64-bit argument printed in hex. | |
1956 | %s - string pointer (target_ulong) and length (int) pair. */ | |
7ccfb2eb | 1957 | void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) |
a2d1ebaf PB |
1958 | { |
1959 | va_list va; | |
1960 | char buf[256]; | |
1961 | char *p; | |
1962 | target_ulong addr; | |
a87295e8 | 1963 | uint64_t i64; |
a2d1ebaf PB |
1964 | GDBState *s; |
1965 | ||
880a7578 | 1966 | s = gdbserver_state; |
a2d1ebaf PB |
1967 | if (!s) |
1968 | return; | |
1969 | gdb_current_syscall_cb = cb; | |
1970 | s->state = RS_SYSCALL; | |
1971 | #ifndef CONFIG_USER_ONLY | |
1972 | vm_stop(EXCP_DEBUG); | |
1973 | #endif | |
1974 | s->state = RS_IDLE; | |
1975 | va_start(va, fmt); | |
1976 | p = buf; | |
1977 | *(p++) = 'F'; | |
1978 | while (*fmt) { | |
1979 | if (*fmt == '%') { | |
1980 | fmt++; | |
1981 | switch (*fmt++) { | |
1982 | case 'x': | |
1983 | addr = va_arg(va, target_ulong); | |
363a37d5 | 1984 | p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx, addr); |
a2d1ebaf | 1985 | break; |
a87295e8 PB |
1986 | case 'l': |
1987 | if (*(fmt++) != 'x') | |
1988 | goto bad_format; | |
1989 | i64 = va_arg(va, uint64_t); | |
363a37d5 | 1990 | p += snprintf(p, &buf[sizeof(buf)] - p, "%" PRIx64, i64); |
a87295e8 | 1991 | break; |
a2d1ebaf PB |
1992 | case 's': |
1993 | addr = va_arg(va, target_ulong); | |
363a37d5 BS |
1994 | p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx "/%x", |
1995 | addr, va_arg(va, int)); | |
a2d1ebaf PB |
1996 | break; |
1997 | default: | |
a87295e8 | 1998 | bad_format: |
a2d1ebaf PB |
1999 | fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n", |
2000 | fmt - 1); | |
2001 | break; | |
2002 | } | |
2003 | } else { | |
2004 | *(p++) = *(fmt++); | |
2005 | } | |
2006 | } | |
8a93e02a | 2007 | *p = 0; |
a2d1ebaf PB |
2008 | va_end(va); |
2009 | put_packet(s, buf); | |
2010 | #ifdef CONFIG_USER_ONLY | |
880a7578 | 2011 | gdb_handlesig(s->c_cpu, 0); |
a2d1ebaf | 2012 | #else |
3098dba0 | 2013 | cpu_exit(s->c_cpu); |
a2d1ebaf PB |
2014 | #endif |
2015 | } | |
2016 | ||
6a00d601 | 2017 | static void gdb_read_byte(GDBState *s, int ch) |
858693c6 FB |
2018 | { |
2019 | int i, csum; | |
60fe76f3 | 2020 | uint8_t reply; |
858693c6 | 2021 | |
1fddef4b | 2022 | #ifndef CONFIG_USER_ONLY |
4046d913 PB |
2023 | if (s->last_packet_len) { |
2024 | /* Waiting for a response to the last packet. If we see the start | |
2025 | of a new command then abandon the previous response. */ | |
2026 | if (ch == '-') { | |
2027 | #ifdef DEBUG_GDB | |
2028 | printf("Got NACK, retransmitting\n"); | |
2029 | #endif | |
ffe8ab83 | 2030 | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
4046d913 PB |
2031 | } |
2032 | #ifdef DEBUG_GDB | |
2033 | else if (ch == '+') | |
2034 | printf("Got ACK\n"); | |
2035 | else | |
2036 | printf("Got '%c' when expecting ACK/NACK\n", ch); | |
2037 | #endif | |
2038 | if (ch == '+' || ch == '$') | |
2039 | s->last_packet_len = 0; | |
2040 | if (ch != '$') | |
2041 | return; | |
2042 | } | |
858693c6 FB |
2043 | if (vm_running) { |
2044 | /* when the CPU is running, we cannot do anything except stop | |
2045 | it when receiving a char */ | |
2046 | vm_stop(EXCP_INTERRUPT); | |
5fafdf24 | 2047 | } else |
1fddef4b | 2048 | #endif |
41625033 | 2049 | { |
858693c6 FB |
2050 | switch(s->state) { |
2051 | case RS_IDLE: | |
2052 | if (ch == '$') { | |
2053 | s->line_buf_index = 0; | |
2054 | s->state = RS_GETLINE; | |
c33a346e | 2055 | } |
b4608c04 | 2056 | break; |
858693c6 FB |
2057 | case RS_GETLINE: |
2058 | if (ch == '#') { | |
2059 | s->state = RS_CHKSUM1; | |
2060 | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { | |
2061 | s->state = RS_IDLE; | |
4c3a88a2 | 2062 | } else { |
858693c6 | 2063 | s->line_buf[s->line_buf_index++] = ch; |
4c3a88a2 FB |
2064 | } |
2065 | break; | |
858693c6 FB |
2066 | case RS_CHKSUM1: |
2067 | s->line_buf[s->line_buf_index] = '\0'; | |
2068 | s->line_csum = fromhex(ch) << 4; | |
2069 | s->state = RS_CHKSUM2; | |
2070 | break; | |
2071 | case RS_CHKSUM2: | |
2072 | s->line_csum |= fromhex(ch); | |
2073 | csum = 0; | |
2074 | for(i = 0; i < s->line_buf_index; i++) { | |
2075 | csum += s->line_buf[i]; | |
2076 | } | |
2077 | if (s->line_csum != (csum & 0xff)) { | |
60fe76f3 TS |
2078 | reply = '-'; |
2079 | put_buffer(s, &reply, 1); | |
858693c6 | 2080 | s->state = RS_IDLE; |
4c3a88a2 | 2081 | } else { |
60fe76f3 TS |
2082 | reply = '+'; |
2083 | put_buffer(s, &reply, 1); | |
880a7578 | 2084 | s->state = gdb_handle_packet(s, s->line_buf); |
4c3a88a2 FB |
2085 | } |
2086 | break; | |
a2d1ebaf PB |
2087 | default: |
2088 | abort(); | |
858693c6 FB |
2089 | } |
2090 | } | |
2091 | } | |
2092 | ||
1fddef4b | 2093 | #ifdef CONFIG_USER_ONLY |
ca587a8e AJ |
2094 | int |
2095 | gdb_queuesig (void) | |
2096 | { | |
2097 | GDBState *s; | |
2098 | ||
2099 | s = gdbserver_state; | |
2100 | ||
2101 | if (gdbserver_fd < 0 || s->fd < 0) | |
2102 | return 0; | |
2103 | else | |
2104 | return 1; | |
2105 | } | |
2106 | ||
1fddef4b FB |
2107 | int |
2108 | gdb_handlesig (CPUState *env, int sig) | |
2109 | { | |
2110 | GDBState *s; | |
2111 | char buf[256]; | |
2112 | int n; | |
2113 | ||
880a7578 | 2114 | s = gdbserver_state; |
1f487ee9 EI |
2115 | if (gdbserver_fd < 0 || s->fd < 0) |
2116 | return sig; | |
1fddef4b FB |
2117 | |
2118 | /* disable single step if it was enabled */ | |
2119 | cpu_single_step(env, 0); | |
2120 | tb_flush(env); | |
2121 | ||
2122 | if (sig != 0) | |
2123 | { | |
ca587a8e | 2124 | snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb (sig)); |
1fddef4b FB |
2125 | put_packet(s, buf); |
2126 | } | |
1f487ee9 EI |
2127 | /* put_packet() might have detected that the peer terminated the |
2128 | connection. */ | |
2129 | if (s->fd < 0) | |
2130 | return sig; | |
1fddef4b | 2131 | |
1fddef4b FB |
2132 | sig = 0; |
2133 | s->state = RS_IDLE; | |
41625033 FB |
2134 | s->running_state = 0; |
2135 | while (s->running_state == 0) { | |
1fddef4b FB |
2136 | n = read (s->fd, buf, 256); |
2137 | if (n > 0) | |
2138 | { | |
2139 | int i; | |
2140 | ||
2141 | for (i = 0; i < n; i++) | |
6a00d601 | 2142 | gdb_read_byte (s, buf[i]); |
1fddef4b FB |
2143 | } |
2144 | else if (n == 0 || errno != EAGAIN) | |
2145 | { | |
2146 | /* XXX: Connection closed. Should probably wait for annother | |
2147 | connection before continuing. */ | |
2148 | return sig; | |
2149 | } | |
41625033 | 2150 | } |
1f487ee9 EI |
2151 | sig = s->signal; |
2152 | s->signal = 0; | |
1fddef4b FB |
2153 | return sig; |
2154 | } | |
e9009676 FB |
2155 | |
2156 | /* Tell the remote gdb that the process has exited. */ | |
2157 | void gdb_exit(CPUState *env, int code) | |
2158 | { | |
2159 | GDBState *s; | |
2160 | char buf[4]; | |
2161 | ||
880a7578 | 2162 | s = gdbserver_state; |
1f487ee9 EI |
2163 | if (gdbserver_fd < 0 || s->fd < 0) |
2164 | return; | |
e9009676 FB |
2165 | |
2166 | snprintf(buf, sizeof(buf), "W%02x", code); | |
2167 | put_packet(s, buf); | |
2168 | } | |
2169 | ||
ca587a8e AJ |
2170 | /* Tell the remote gdb that the process has exited due to SIG. */ |
2171 | void gdb_signalled(CPUState *env, int sig) | |
2172 | { | |
2173 | GDBState *s; | |
2174 | char buf[4]; | |
2175 | ||
2176 | s = gdbserver_state; | |
2177 | if (gdbserver_fd < 0 || s->fd < 0) | |
2178 | return; | |
2179 | ||
2180 | snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb (sig)); | |
2181 | put_packet(s, buf); | |
2182 | } | |
1fddef4b | 2183 | |
880a7578 | 2184 | static void gdb_accept(void) |
858693c6 FB |
2185 | { |
2186 | GDBState *s; | |
2187 | struct sockaddr_in sockaddr; | |
2188 | socklen_t len; | |
2189 | int val, fd; | |
2190 | ||
2191 | for(;;) { | |
2192 | len = sizeof(sockaddr); | |
2193 | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len); | |
2194 | if (fd < 0 && errno != EINTR) { | |
2195 | perror("accept"); | |
2196 | return; | |
2197 | } else if (fd >= 0) { | |
b4608c04 FB |
2198 | break; |
2199 | } | |
2200 | } | |
858693c6 FB |
2201 | |
2202 | /* set short latency */ | |
2203 | val = 1; | |
8f447cc7 | 2204 | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
3b46e624 | 2205 | |
880a7578 | 2206 | s = qemu_mallocz(sizeof(GDBState)); |
880a7578 AL |
2207 | s->c_cpu = first_cpu; |
2208 | s->g_cpu = first_cpu; | |
858693c6 | 2209 | s->fd = fd; |
56aebc89 | 2210 | gdb_has_xml = 0; |
858693c6 | 2211 | |
880a7578 | 2212 | gdbserver_state = s; |
a2d1ebaf | 2213 | |
858693c6 | 2214 | fcntl(fd, F_SETFL, O_NONBLOCK); |
858693c6 FB |
2215 | } |
2216 | ||
2217 | static int gdbserver_open(int port) | |
2218 | { | |
2219 | struct sockaddr_in sockaddr; | |
2220 | int fd, val, ret; | |
2221 | ||
2222 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
2223 | if (fd < 0) { | |
2224 | perror("socket"); | |
2225 | return -1; | |
2226 | } | |
2227 | ||
2228 | /* allow fast reuse */ | |
2229 | val = 1; | |
8f447cc7 | 2230 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); |
858693c6 FB |
2231 | |
2232 | sockaddr.sin_family = AF_INET; | |
2233 | sockaddr.sin_port = htons(port); | |
2234 | sockaddr.sin_addr.s_addr = 0; | |
2235 | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); | |
2236 | if (ret < 0) { | |
2237 | perror("bind"); | |
2238 | return -1; | |
2239 | } | |
2240 | ret = listen(fd, 0); | |
2241 | if (ret < 0) { | |
2242 | perror("listen"); | |
2243 | return -1; | |
2244 | } | |
858693c6 FB |
2245 | return fd; |
2246 | } | |
2247 | ||
2248 | int gdbserver_start(int port) | |
2249 | { | |
2250 | gdbserver_fd = gdbserver_open(port); | |
2251 | if (gdbserver_fd < 0) | |
2252 | return -1; | |
2253 | /* accept connections */ | |
880a7578 | 2254 | gdb_accept(); |
4046d913 PB |
2255 | return 0; |
2256 | } | |
2b1319c8 AJ |
2257 | |
2258 | /* Disable gdb stub for child processes. */ | |
2259 | void gdbserver_fork(CPUState *env) | |
2260 | { | |
2261 | GDBState *s = gdbserver_state; | |
9f6164d6 | 2262 | if (gdbserver_fd < 0 || s->fd < 0) |
2b1319c8 AJ |
2263 | return; |
2264 | close(s->fd); | |
2265 | s->fd = -1; | |
2266 | cpu_breakpoint_remove_all(env, BP_GDB); | |
2267 | cpu_watchpoint_remove_all(env, BP_GDB); | |
2268 | } | |
1fddef4b | 2269 | #else |
aa1f17c1 | 2270 | static int gdb_chr_can_receive(void *opaque) |
4046d913 | 2271 | { |
56aebc89 PB |
2272 | /* We can handle an arbitrarily large amount of data. |
2273 | Pick the maximum packet size, which is as good as anything. */ | |
2274 | return MAX_PACKET_LENGTH; | |
4046d913 PB |
2275 | } |
2276 | ||
aa1f17c1 | 2277 | static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) |
4046d913 | 2278 | { |
4046d913 PB |
2279 | int i; |
2280 | ||
2281 | for (i = 0; i < size; i++) { | |
880a7578 | 2282 | gdb_read_byte(gdbserver_state, buf[i]); |
4046d913 PB |
2283 | } |
2284 | } | |
2285 | ||
2286 | static void gdb_chr_event(void *opaque, int event) | |
2287 | { | |
2288 | switch (event) { | |
2289 | case CHR_EVENT_RESET: | |
2290 | vm_stop(EXCP_INTERRUPT); | |
56aebc89 | 2291 | gdb_has_xml = 0; |
4046d913 PB |
2292 | break; |
2293 | default: | |
2294 | break; | |
2295 | } | |
2296 | } | |
2297 | ||
8a34a0fb AL |
2298 | static void gdb_monitor_output(GDBState *s, const char *msg, int len) |
2299 | { | |
2300 | char buf[MAX_PACKET_LENGTH]; | |
2301 | ||
2302 | buf[0] = 'O'; | |
2303 | if (len > (MAX_PACKET_LENGTH/2) - 1) | |
2304 | len = (MAX_PACKET_LENGTH/2) - 1; | |
2305 | memtohex(buf + 1, (uint8_t *)msg, len); | |
2306 | put_packet(s, buf); | |
2307 | } | |
2308 | ||
2309 | static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len) | |
2310 | { | |
2311 | const char *p = (const char *)buf; | |
2312 | int max_sz; | |
2313 | ||
2314 | max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2; | |
2315 | for (;;) { | |
2316 | if (len <= max_sz) { | |
2317 | gdb_monitor_output(gdbserver_state, p, len); | |
2318 | break; | |
2319 | } | |
2320 | gdb_monitor_output(gdbserver_state, p, max_sz); | |
2321 | p += max_sz; | |
2322 | len -= max_sz; | |
2323 | } | |
2324 | return len; | |
2325 | } | |
2326 | ||
59030a8c AL |
2327 | #ifndef _WIN32 |
2328 | static void gdb_sigterm_handler(int signal) | |
2329 | { | |
2330 | if (vm_running) | |
2331 | vm_stop(EXCP_INTERRUPT); | |
2332 | } | |
2333 | #endif | |
2334 | ||
2335 | int gdbserver_start(const char *device) | |
4046d913 PB |
2336 | { |
2337 | GDBState *s; | |
59030a8c | 2338 | char gdbstub_device_name[128]; |
36556b20 AL |
2339 | CharDriverState *chr = NULL; |
2340 | CharDriverState *mon_chr; | |
cfc3475a | 2341 | |
59030a8c AL |
2342 | if (!device) |
2343 | return -1; | |
2344 | if (strcmp(device, "none") != 0) { | |
2345 | if (strstart(device, "tcp:", NULL)) { | |
2346 | /* enforce required TCP attributes */ | |
2347 | snprintf(gdbstub_device_name, sizeof(gdbstub_device_name), | |
2348 | "%s,nowait,nodelay,server", device); | |
2349 | device = gdbstub_device_name; | |
36556b20 | 2350 | } |
59030a8c AL |
2351 | #ifndef _WIN32 |
2352 | else if (strcmp(device, "stdio") == 0) { | |
2353 | struct sigaction act; | |
4046d913 | 2354 | |
59030a8c AL |
2355 | memset(&act, 0, sizeof(act)); |
2356 | act.sa_handler = gdb_sigterm_handler; | |
2357 | sigaction(SIGINT, &act, NULL); | |
2358 | } | |
2359 | #endif | |
2360 | chr = qemu_chr_open("gdb", device, NULL); | |
36556b20 AL |
2361 | if (!chr) |
2362 | return -1; | |
2363 | ||
2364 | qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive, | |
2365 | gdb_chr_event, NULL); | |
cfc3475a PB |
2366 | } |
2367 | ||
36556b20 AL |
2368 | s = gdbserver_state; |
2369 | if (!s) { | |
2370 | s = qemu_mallocz(sizeof(GDBState)); | |
2371 | gdbserver_state = s; | |
4046d913 | 2372 | |
36556b20 AL |
2373 | qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL); |
2374 | ||
2375 | /* Initialize a monitor terminal for gdb */ | |
2376 | mon_chr = qemu_mallocz(sizeof(*mon_chr)); | |
2377 | mon_chr->chr_write = gdb_monitor_write; | |
2378 | monitor_init(mon_chr, 0); | |
2379 | } else { | |
2380 | if (s->chr) | |
2381 | qemu_chr_close(s->chr); | |
2382 | mon_chr = s->mon_chr; | |
2383 | memset(s, 0, sizeof(GDBState)); | |
2384 | } | |
880a7578 AL |
2385 | s->c_cpu = first_cpu; |
2386 | s->g_cpu = first_cpu; | |
4046d913 | 2387 | s->chr = chr; |
36556b20 AL |
2388 | s->state = chr ? RS_IDLE : RS_INACTIVE; |
2389 | s->mon_chr = mon_chr; | |
8a34a0fb | 2390 | |
b4608c04 FB |
2391 | return 0; |
2392 | } | |
4046d913 | 2393 | #endif |