]> git.proxmox.com Git - mirror_qemu.git/blame - gdbstub.c
Fix initialisation of serial/parallel ports, spotted by Stefan Weil.
[mirror_qemu.git] / gdbstub.c
CommitLineData
b4608c04
FB
1/*
2 * gdb server stub
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
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
978efd6a 20#include "config.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
67b915a5 32#include "vl.h"
1fddef4b 33#endif
67b915a5 34
8f447cc7
FB
35#include "qemu_socket.h"
36#ifdef _WIN32
37/* XXX: these constants may be independent of the host ones even for Unix */
38#ifndef SIGTRAP
39#define SIGTRAP 5
40#endif
41#ifndef SIGINT
42#define SIGINT 2
43#endif
44#else
b4608c04 45#include <signal.h>
8f447cc7 46#endif
b4608c04 47
4abe615b 48//#define DEBUG_GDB
b4608c04 49
858693c6
FB
50enum RSState {
51 RS_IDLE,
52 RS_GETLINE,
53 RS_CHKSUM1,
54 RS_CHKSUM2,
a2d1ebaf 55 RS_SYSCALL,
858693c6 56};
858693c6 57typedef struct GDBState {
6a00d601 58 CPUState *env; /* current CPU */
41625033 59 enum RSState state; /* parsing state */
858693c6
FB
60 char line_buf[4096];
61 int line_buf_index;
62 int line_csum;
4046d913
PB
63 char last_packet[4100];
64 int last_packet_len;
41625033 65#ifdef CONFIG_USER_ONLY
4046d913 66 int fd;
41625033 67 int running_state;
4046d913
PB
68#else
69 CharDriverState *chr;
41625033 70#endif
858693c6 71} GDBState;
b4608c04 72
1fddef4b 73#ifdef CONFIG_USER_ONLY
4046d913
PB
74/* XXX: This is not thread safe. Do we care? */
75static int gdbserver_fd = -1;
76
1fddef4b
FB
77/* XXX: remove this hack. */
78static GDBState gdbserver_state;
1fddef4b 79
858693c6 80static int get_char(GDBState *s)
b4608c04
FB
81{
82 uint8_t ch;
83 int ret;
84
85 for(;;) {
8f447cc7 86 ret = recv(s->fd, &ch, 1, 0);
b4608c04
FB
87 if (ret < 0) {
88 if (errno != EINTR && errno != EAGAIN)
89 return -1;
90 } else if (ret == 0) {
91 return -1;
92 } else {
93 break;
94 }
95 }
96 return ch;
97}
4046d913 98#endif
b4608c04 99
a2d1ebaf
PB
100/* GDB stub state for use by semihosting syscalls. */
101static GDBState *gdb_syscall_state;
102static gdb_syscall_complete_cb gdb_current_syscall_cb;
103
104enum {
105 GDB_SYS_UNKNOWN,
106 GDB_SYS_ENABLED,
107 GDB_SYS_DISABLED,
108} gdb_syscall_mode;
109
110/* If gdb is connected when the first semihosting syscall occurs then use
111 remote gdb syscalls. Otherwise use native file IO. */
112int use_gdb_syscalls(void)
113{
114 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
115 gdb_syscall_mode = (gdb_syscall_state ? GDB_SYS_ENABLED
116 : GDB_SYS_DISABLED);
117 }
118 return gdb_syscall_mode == GDB_SYS_ENABLED;
119}
120
858693c6 121static void put_buffer(GDBState *s, const uint8_t *buf, int len)
b4608c04 122{
4046d913 123#ifdef CONFIG_USER_ONLY
b4608c04
FB
124 int ret;
125
126 while (len > 0) {
8f447cc7 127 ret = send(s->fd, buf, len, 0);
b4608c04
FB
128 if (ret < 0) {
129 if (errno != EINTR && errno != EAGAIN)
130 return;
131 } else {
132 buf += ret;
133 len -= ret;
134 }
135 }
4046d913
PB
136#else
137 qemu_chr_write(s->chr, buf, len);
138#endif
b4608c04
FB
139}
140
141static inline int fromhex(int v)
142{
143 if (v >= '0' && v <= '9')
144 return v - '0';
145 else if (v >= 'A' && v <= 'F')
146 return v - 'A' + 10;
147 else if (v >= 'a' && v <= 'f')
148 return v - 'a' + 10;
149 else
150 return 0;
151}
152
153static inline int tohex(int v)
154{
155 if (v < 10)
156 return v + '0';
157 else
158 return v - 10 + 'a';
159}
160
161static void memtohex(char *buf, const uint8_t *mem, int len)
162{
163 int i, c;
164 char *q;
165 q = buf;
166 for(i = 0; i < len; i++) {
167 c = mem[i];
168 *q++ = tohex(c >> 4);
169 *q++ = tohex(c & 0xf);
170 }
171 *q = '\0';
172}
173
174static void hextomem(uint8_t *mem, const char *buf, int len)
175{
176 int i;
177
178 for(i = 0; i < len; i++) {
179 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
180 buf += 2;
181 }
182}
183
b4608c04 184/* return -1 if error, 0 if OK */
858693c6 185static int put_packet(GDBState *s, char *buf)
b4608c04 186{
4046d913
PB
187 int len, csum, i;
188 char *p;
b4608c04
FB
189
190#ifdef DEBUG_GDB
191 printf("reply='%s'\n", buf);
192#endif
193
194 for(;;) {
4046d913
PB
195 p = s->last_packet;
196 *(p++) = '$';
b4608c04 197 len = strlen(buf);
4046d913
PB
198 memcpy(p, buf, len);
199 p += len;
b4608c04
FB
200 csum = 0;
201 for(i = 0; i < len; i++) {
202 csum += buf[i];
203 }
4046d913
PB
204 *(p++) = '#';
205 *(p++) = tohex((csum >> 4) & 0xf);
206 *(p++) = tohex((csum) & 0xf);
b4608c04 207
4046d913
PB
208 s->last_packet_len = p - s->last_packet;
209 put_buffer(s, s->last_packet, s->last_packet_len);
b4608c04 210
4046d913
PB
211#ifdef CONFIG_USER_ONLY
212 i = get_char(s);
213 if (i < 0)
b4608c04 214 return -1;
4046d913 215 if (i == '+')
b4608c04 216 break;
4046d913
PB
217#else
218 break;
219#endif
b4608c04
FB
220 }
221 return 0;
222}
223
6da41eaf
FB
224#if defined(TARGET_I386)
225
6da41eaf
FB
226static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
227{
e95c8d51 228 uint32_t *registers = (uint32_t *)mem_buf;
6da41eaf
FB
229 int i, fpus;
230
231 for(i = 0; i < 8; i++) {
e95c8d51 232 registers[i] = env->regs[i];
6da41eaf 233 }
e95c8d51
FB
234 registers[8] = env->eip;
235 registers[9] = env->eflags;
236 registers[10] = env->segs[R_CS].selector;
237 registers[11] = env->segs[R_SS].selector;
238 registers[12] = env->segs[R_DS].selector;
239 registers[13] = env->segs[R_ES].selector;
240 registers[14] = env->segs[R_FS].selector;
241 registers[15] = env->segs[R_GS].selector;
6da41eaf
FB
242 /* XXX: convert floats */
243 for(i = 0; i < 8; i++) {
244 memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10);
245 }
e95c8d51 246 registers[36] = env->fpuc;
6da41eaf 247 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
e95c8d51
FB
248 registers[37] = fpus;
249 registers[38] = 0; /* XXX: convert tags */
250 registers[39] = 0; /* fiseg */
251 registers[40] = 0; /* fioff */
252 registers[41] = 0; /* foseg */
253 registers[42] = 0; /* fooff */
254 registers[43] = 0; /* fop */
255
256 for(i = 0; i < 16; i++)
257 tswapls(&registers[i]);
258 for(i = 36; i < 44; i++)
259 tswapls(&registers[i]);
6da41eaf
FB
260 return 44 * 4;
261}
262
263static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
264{
265 uint32_t *registers = (uint32_t *)mem_buf;
266 int i;
267
268 for(i = 0; i < 8; i++) {
269 env->regs[i] = tswapl(registers[i]);
270 }
e95c8d51
FB
271 env->eip = tswapl(registers[8]);
272 env->eflags = tswapl(registers[9]);
6da41eaf
FB
273#if defined(CONFIG_USER_ONLY)
274#define LOAD_SEG(index, sreg)\
275 if (tswapl(registers[index]) != env->segs[sreg].selector)\
276 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
277 LOAD_SEG(10, R_CS);
278 LOAD_SEG(11, R_SS);
279 LOAD_SEG(12, R_DS);
280 LOAD_SEG(13, R_ES);
281 LOAD_SEG(14, R_FS);
282 LOAD_SEG(15, R_GS);
283#endif
284}
285
9e62fd7f 286#elif defined (TARGET_PPC)
9e62fd7f
FB
287static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
288{
a541f297 289 uint32_t *registers = (uint32_t *)mem_buf, tmp;
9e62fd7f
FB
290 int i;
291
292 /* fill in gprs */
a541f297 293 for(i = 0; i < 32; i++) {
e95c8d51 294 registers[i] = tswapl(env->gpr[i]);
9e62fd7f
FB
295 }
296 /* fill in fprs */
297 for (i = 0; i < 32; i++) {
e95c8d51
FB
298 registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
299 registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1));
9e62fd7f
FB
300 }
301 /* nip, msr, ccr, lnk, ctr, xer, mq */
e95c8d51 302 registers[96] = tswapl(env->nip);
3fc6c082 303 registers[97] = tswapl(do_load_msr(env));
9e62fd7f
FB
304 tmp = 0;
305 for (i = 0; i < 8; i++)
a541f297 306 tmp |= env->crf[i] << (32 - ((i + 1) * 4));
e95c8d51
FB
307 registers[98] = tswapl(tmp);
308 registers[99] = tswapl(env->lr);
309 registers[100] = tswapl(env->ctr);
3fc6c082 310 registers[101] = tswapl(do_load_xer(env));
e95c8d51 311 registers[102] = 0;
a541f297
FB
312
313 return 103 * 4;
9e62fd7f
FB
314}
315
316static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
317{
318 uint32_t *registers = (uint32_t *)mem_buf;
319 int i;
320
321 /* fill in gprs */
322 for (i = 0; i < 32; i++) {
e95c8d51 323 env->gpr[i] = tswapl(registers[i]);
9e62fd7f
FB
324 }
325 /* fill in fprs */
326 for (i = 0; i < 32; i++) {
e95c8d51
FB
327 *((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]);
328 *((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]);
9e62fd7f
FB
329 }
330 /* nip, msr, ccr, lnk, ctr, xer, mq */
e95c8d51 331 env->nip = tswapl(registers[96]);
3fc6c082 332 do_store_msr(env, tswapl(registers[97]));
e95c8d51 333 registers[98] = tswapl(registers[98]);
9e62fd7f 334 for (i = 0; i < 8; i++)
a541f297 335 env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF;
e95c8d51
FB
336 env->lr = tswapl(registers[99]);
337 env->ctr = tswapl(registers[100]);
3fc6c082 338 do_store_xer(env, tswapl(registers[101]));
e95c8d51
FB
339}
340#elif defined (TARGET_SPARC)
341static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
342{
3475187d 343 target_ulong *registers = (target_ulong *)mem_buf;
e95c8d51
FB
344 int i;
345
346 /* fill in g0..g7 */
48b2c193 347 for(i = 0; i < 8; i++) {
e95c8d51
FB
348 registers[i] = tswapl(env->gregs[i]);
349 }
350 /* fill in register window */
351 for(i = 0; i < 24; i++) {
352 registers[i + 8] = tswapl(env->regwptr[i]);
353 }
9d9754a3 354#ifndef TARGET_SPARC64
e95c8d51
FB
355 /* fill in fprs */
356 for (i = 0; i < 32; i++) {
357 registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
358 }
359 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
360 registers[64] = tswapl(env->y);
3475187d
FB
361 {
362 target_ulong tmp;
363
364 tmp = GET_PSR(env);
365 registers[65] = tswapl(tmp);
366 }
e95c8d51
FB
367 registers[66] = tswapl(env->wim);
368 registers[67] = tswapl(env->tbr);
369 registers[68] = tswapl(env->pc);
370 registers[69] = tswapl(env->npc);
371 registers[70] = tswapl(env->fsr);
372 registers[71] = 0; /* csr */
373 registers[72] = 0;
3475187d
FB
374 return 73 * sizeof(target_ulong);
375#else
9d9754a3
FB
376 /* fill in fprs */
377 for (i = 0; i < 64; i += 2) {
378 uint64_t tmp;
379
380 tmp = (uint64_t)tswap32(*((uint32_t *)&env->fpr[i])) << 32;
381 tmp |= tswap32(*((uint32_t *)&env->fpr[i + 1]));
382 registers[i/2 + 32] = tmp;
3475187d 383 }
9d9754a3
FB
384 registers[64] = tswapl(env->pc);
385 registers[65] = tswapl(env->npc);
386 registers[66] = tswapl(env->tstate[env->tl]);
387 registers[67] = tswapl(env->fsr);
388 registers[68] = tswapl(env->fprs);
389 registers[69] = tswapl(env->y);
390 return 70 * sizeof(target_ulong);
3475187d 391#endif
e95c8d51
FB
392}
393
394static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
395{
3475187d 396 target_ulong *registers = (target_ulong *)mem_buf;
e95c8d51
FB
397 int i;
398
399 /* fill in g0..g7 */
400 for(i = 0; i < 7; i++) {
401 env->gregs[i] = tswapl(registers[i]);
402 }
403 /* fill in register window */
404 for(i = 0; i < 24; i++) {
3475187d 405 env->regwptr[i] = tswapl(registers[i + 8]);
e95c8d51 406 }
9d9754a3 407#ifndef TARGET_SPARC64
e95c8d51
FB
408 /* fill in fprs */
409 for (i = 0; i < 32; i++) {
410 *((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
411 }
412 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
413 env->y = tswapl(registers[64]);
e80cfcfc 414 PUT_PSR(env, tswapl(registers[65]));
e95c8d51
FB
415 env->wim = tswapl(registers[66]);
416 env->tbr = tswapl(registers[67]);
417 env->pc = tswapl(registers[68]);
418 env->npc = tswapl(registers[69]);
419 env->fsr = tswapl(registers[70]);
3475187d 420#else
9d9754a3
FB
421 for (i = 0; i < 64; i += 2) {
422 *((uint32_t *)&env->fpr[i]) = tswap32(registers[i/2 + 32] >> 32);
423 *((uint32_t *)&env->fpr[i + 1]) = tswap32(registers[i/2 + 32] & 0xffffffff);
3475187d 424 }
9d9754a3
FB
425 env->pc = tswapl(registers[64]);
426 env->npc = tswapl(registers[65]);
427 env->tstate[env->tl] = tswapl(registers[66]);
428 env->fsr = tswapl(registers[67]);
429 env->fprs = tswapl(registers[68]);
430 env->y = tswapl(registers[69]);
3475187d 431#endif
9e62fd7f 432}
1fddef4b
FB
433#elif defined (TARGET_ARM)
434static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
435{
436 int i;
437 uint8_t *ptr;
438
439 ptr = mem_buf;
440 /* 16 core integer registers (4 bytes each). */
441 for (i = 0; i < 16; i++)
442 {
443 *(uint32_t *)ptr = tswapl(env->regs[i]);
444 ptr += 4;
445 }
446 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
447 Not yet implemented. */
448 memset (ptr, 0, 8 * 12 + 4);
449 ptr += 8 * 12 + 4;
450 /* CPSR (4 bytes). */
b5ff1b31 451 *(uint32_t *)ptr = tswapl (cpsr_read(env));
1fddef4b
FB
452 ptr += 4;
453
454 return ptr - mem_buf;
455}
6da41eaf 456
1fddef4b
FB
457static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
458{
459 int i;
460 uint8_t *ptr;
461
462 ptr = mem_buf;
463 /* Core integer registers. */
464 for (i = 0; i < 16; i++)
465 {
466 env->regs[i] = tswapl(*(uint32_t *)ptr);
467 ptr += 4;
468 }
469 /* Ignore FPA regs and scr. */
470 ptr += 8 * 12 + 4;
b5ff1b31 471 cpsr_write (env, tswapl(*(uint32_t *)ptr), 0xffffffff);
1fddef4b 472}
e6e5906b
PB
473#elif defined (TARGET_M68K)
474static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
475{
476 int i;
477 uint8_t *ptr;
478 CPU_DoubleU u;
479
480 ptr = mem_buf;
481 /* D0-D7 */
482 for (i = 0; i < 8; i++) {
483 *(uint32_t *)ptr = tswapl(env->dregs[i]);
484 ptr += 4;
485 }
486 /* A0-A7 */
487 for (i = 0; i < 8; i++) {
488 *(uint32_t *)ptr = tswapl(env->aregs[i]);
489 ptr += 4;
490 }
491 *(uint32_t *)ptr = tswapl(env->sr);
492 ptr += 4;
493 *(uint32_t *)ptr = tswapl(env->pc);
494 ptr += 4;
495 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
496 ColdFire has 8-bit double precision registers. */
497 for (i = 0; i < 8; i++) {
498 u.d = env->fregs[i];
499 *(uint32_t *)ptr = tswap32(u.l.upper);
500 *(uint32_t *)ptr = tswap32(u.l.lower);
501 }
502 /* FP control regs (not implemented). */
503 memset (ptr, 0, 3 * 4);
504 ptr += 3 * 4;
505
506 return ptr - mem_buf;
507}
508
509static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
510{
511 int i;
512 uint8_t *ptr;
513 CPU_DoubleU u;
514
515 ptr = mem_buf;
516 /* D0-D7 */
517 for (i = 0; i < 8; i++) {
518 env->dregs[i] = tswapl(*(uint32_t *)ptr);
519 ptr += 4;
520 }
521 /* A0-A7 */
522 for (i = 0; i < 8; i++) {
523 env->aregs[i] = tswapl(*(uint32_t *)ptr);
524 ptr += 4;
525 }
526 env->sr = tswapl(*(uint32_t *)ptr);
527 ptr += 4;
528 env->pc = tswapl(*(uint32_t *)ptr);
529 ptr += 4;
530 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
531 ColdFire has 8-bit double precision registers. */
532 for (i = 0; i < 8; i++) {
533 u.l.upper = tswap32(*(uint32_t *)ptr);
534 u.l.lower = tswap32(*(uint32_t *)ptr);
535 env->fregs[i] = u.d;
536 }
537 /* FP control regs (not implemented). */
538 ptr += 3 * 4;
539}
6f970bd9
FB
540#elif defined (TARGET_MIPS)
541static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
542{
543 int i;
544 uint8_t *ptr;
545
546 ptr = mem_buf;
547 for (i = 0; i < 32; i++)
548 {
549 *(uint32_t *)ptr = tswapl(env->gpr[i]);
550 ptr += 4;
551 }
552
553 *(uint32_t *)ptr = tswapl(env->CP0_Status);
554 ptr += 4;
555
556 *(uint32_t *)ptr = tswapl(env->LO);
557 ptr += 4;
558
559 *(uint32_t *)ptr = tswapl(env->HI);
560 ptr += 4;
561
562 *(uint32_t *)ptr = tswapl(env->CP0_BadVAddr);
563 ptr += 4;
564
565 *(uint32_t *)ptr = tswapl(env->CP0_Cause);
566 ptr += 4;
567
568 *(uint32_t *)ptr = tswapl(env->PC);
569 ptr += 4;
570
8e33c08c
TS
571#ifdef MIPS_USES_FPU
572 for (i = 0; i < 32; i++)
573 {
574 *(uint32_t *)ptr = tswapl(FPR_W (env, i));
575 ptr += 4;
576 }
577
578 *(uint32_t *)ptr = tswapl(env->fcr31);
579 ptr += 4;
580
581 *(uint32_t *)ptr = tswapl(env->fcr0);
582 ptr += 4;
583#endif
584
6f970bd9 585 /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
8e33c08c 586 /* what's 'fp' mean here? */
6f970bd9
FB
587
588 return ptr - mem_buf;
589}
590
8e33c08c
TS
591/* convert MIPS rounding mode in FCR31 to IEEE library */
592static unsigned int ieee_rm[] =
593 {
594 float_round_nearest_even,
595 float_round_to_zero,
596 float_round_up,
597 float_round_down
598 };
599#define RESTORE_ROUNDING_MODE \
600 set_float_rounding_mode(ieee_rm[env->fcr31 & 3], &env->fp_status)
601
6f970bd9
FB
602static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
603{
604 int i;
605 uint8_t *ptr;
606
607 ptr = mem_buf;
608 for (i = 0; i < 32; i++)
609 {
610 env->gpr[i] = tswapl(*(uint32_t *)ptr);
611 ptr += 4;
612 }
613
614 env->CP0_Status = tswapl(*(uint32_t *)ptr);
615 ptr += 4;
616
617 env->LO = tswapl(*(uint32_t *)ptr);
618 ptr += 4;
619
620 env->HI = tswapl(*(uint32_t *)ptr);
621 ptr += 4;
622
623 env->CP0_BadVAddr = tswapl(*(uint32_t *)ptr);
624 ptr += 4;
625
626 env->CP0_Cause = tswapl(*(uint32_t *)ptr);
627 ptr += 4;
628
629 env->PC = tswapl(*(uint32_t *)ptr);
630 ptr += 4;
8e33c08c
TS
631
632#ifdef MIPS_USES_FPU
633 for (i = 0; i < 32; i++)
634 {
635 FPR_W (env, i) = tswapl(*(uint32_t *)ptr);
636 ptr += 4;
637 }
638
639 env->fcr31 = tswapl(*(uint32_t *)ptr) & 0x0183FFFF;
640 ptr += 4;
641
642 env->fcr0 = tswapl(*(uint32_t *)ptr);
643 ptr += 4;
644
645 /* set rounding mode */
646 RESTORE_ROUNDING_MODE;
647
648#ifndef CONFIG_SOFTFLOAT
649 /* no floating point exception for native float */
650 SET_FP_ENABLE(env->fcr31, 0);
651#endif
652#endif
6f970bd9 653}
fdf9b3e8
FB
654#elif defined (TARGET_SH4)
655static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
656{
657 uint32_t *ptr = (uint32_t *)mem_buf;
658 int i;
659
660#define SAVE(x) *ptr++=tswapl(x)
9c2a9ea1
PB
661 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
662 for (i = 0; i < 8; i++) SAVE(env->gregs[i + 16]);
663 } else {
664 for (i = 0; i < 8; i++) SAVE(env->gregs[i]);
665 }
666 for (i = 8; i < 16; i++) SAVE(env->gregs[i]);
fdf9b3e8
FB
667 SAVE (env->pc);
668 SAVE (env->pr);
669 SAVE (env->gbr);
670 SAVE (env->vbr);
671 SAVE (env->mach);
672 SAVE (env->macl);
673 SAVE (env->sr);
674 SAVE (0); /* TICKS */
675 SAVE (0); /* STALLS */
676 SAVE (0); /* CYCLES */
677 SAVE (0); /* INSTS */
678 SAVE (0); /* PLR */
679
680 return ((uint8_t *)ptr - mem_buf);
681}
682
683static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
684{
685 uint32_t *ptr = (uint32_t *)mem_buf;
686 int i;
687
688#define LOAD(x) (x)=*ptr++;
9c2a9ea1
PB
689 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
690 for (i = 0; i < 8; i++) LOAD(env->gregs[i + 16]);
691 } else {
692 for (i = 0; i < 8; i++) LOAD(env->gregs[i]);
693 }
694 for (i = 8; i < 16; i++) LOAD(env->gregs[i]);
fdf9b3e8
FB
695 LOAD (env->pc);
696 LOAD (env->pr);
697 LOAD (env->gbr);
698 LOAD (env->vbr);
699 LOAD (env->mach);
700 LOAD (env->macl);
701 LOAD (env->sr);
702}
1fddef4b 703#else
6da41eaf
FB
704static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
705{
706 return 0;
707}
708
709static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
710{
711}
712
713#endif
b4608c04 714
1fddef4b 715static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf)
b4608c04 716{
b4608c04 717 const char *p;
858693c6 718 int ch, reg_size, type;
b4608c04
FB
719 char buf[4096];
720 uint8_t mem_buf[2000];
721 uint32_t *registers;
9d9754a3 722 target_ulong addr, len;
b4608c04 723
858693c6
FB
724#ifdef DEBUG_GDB
725 printf("command='%s'\n", line_buf);
726#endif
727 p = line_buf;
728 ch = *p++;
729 switch(ch) {
730 case '?':
1fddef4b 731 /* TODO: Make this return the correct value for user-mode. */
858693c6
FB
732 snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
733 put_packet(s, buf);
734 break;
735 case 'c':
736 if (*p != '\0') {
9d9754a3 737 addr = strtoull(p, (char **)&p, 16);
4c3a88a2 738#if defined(TARGET_I386)
858693c6 739 env->eip = addr;
5be1a8e0 740#elif defined (TARGET_PPC)
858693c6 741 env->nip = addr;
8d5f07fa
FB
742#elif defined (TARGET_SPARC)
743 env->pc = addr;
744 env->npc = addr + 4;
b5ff1b31
FB
745#elif defined (TARGET_ARM)
746 env->regs[15] = addr;
fdf9b3e8
FB
747#elif defined (TARGET_SH4)
748 env->pc = addr;
4c3a88a2 749#endif
858693c6 750 }
41625033
FB
751#ifdef CONFIG_USER_ONLY
752 s->running_state = 1;
753#else
754 vm_start();
755#endif
756 return RS_IDLE;
858693c6
FB
757 case 's':
758 if (*p != '\0') {
759 addr = strtoul(p, (char **)&p, 16);
c33a346e 760#if defined(TARGET_I386)
858693c6 761 env->eip = addr;
5be1a8e0 762#elif defined (TARGET_PPC)
858693c6 763 env->nip = addr;
8d5f07fa
FB
764#elif defined (TARGET_SPARC)
765 env->pc = addr;
766 env->npc = addr + 4;
b5ff1b31
FB
767#elif defined (TARGET_ARM)
768 env->regs[15] = addr;
fdf9b3e8
FB
769#elif defined (TARGET_SH4)
770 env->pc = addr;
c33a346e 771#endif
858693c6
FB
772 }
773 cpu_single_step(env, 1);
41625033
FB
774#ifdef CONFIG_USER_ONLY
775 s->running_state = 1;
776#else
777 vm_start();
778#endif
779 return RS_IDLE;
a2d1ebaf
PB
780 case 'F':
781 {
782 target_ulong ret;
783 target_ulong err;
784
785 ret = strtoull(p, (char **)&p, 16);
786 if (*p == ',') {
787 p++;
788 err = strtoull(p, (char **)&p, 16);
789 } else {
790 err = 0;
791 }
792 if (*p == ',')
793 p++;
794 type = *p;
795 if (gdb_current_syscall_cb)
796 gdb_current_syscall_cb(s->env, ret, err);
797 if (type == 'C') {
798 put_packet(s, "T02");
799 } else {
800#ifdef CONFIG_USER_ONLY
801 s->running_state = 1;
802#else
803 vm_start();
804#endif
805 }
806 }
807 break;
858693c6
FB
808 case 'g':
809 reg_size = cpu_gdb_read_registers(env, mem_buf);
810 memtohex(buf, mem_buf, reg_size);
811 put_packet(s, buf);
812 break;
813 case 'G':
814 registers = (void *)mem_buf;
815 len = strlen(p) / 2;
816 hextomem((uint8_t *)registers, p, len);
817 cpu_gdb_write_registers(env, mem_buf, len);
818 put_packet(s, "OK");
819 break;
820 case 'm':
9d9754a3 821 addr = strtoull(p, (char **)&p, 16);
858693c6
FB
822 if (*p == ',')
823 p++;
9d9754a3 824 len = strtoull(p, NULL, 16);
6f970bd9
FB
825 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) {
826 put_packet (s, "E14");
827 } else {
828 memtohex(buf, mem_buf, len);
829 put_packet(s, buf);
830 }
858693c6
FB
831 break;
832 case 'M':
9d9754a3 833 addr = strtoull(p, (char **)&p, 16);
858693c6
FB
834 if (*p == ',')
835 p++;
9d9754a3 836 len = strtoull(p, (char **)&p, 16);
b328f873 837 if (*p == ':')
858693c6
FB
838 p++;
839 hextomem(mem_buf, p, len);
840 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0)
905f20b1 841 put_packet(s, "E14");
858693c6
FB
842 else
843 put_packet(s, "OK");
844 break;
845 case 'Z':
846 type = strtoul(p, (char **)&p, 16);
847 if (*p == ',')
848 p++;
9d9754a3 849 addr = strtoull(p, (char **)&p, 16);
858693c6
FB
850 if (*p == ',')
851 p++;
9d9754a3 852 len = strtoull(p, (char **)&p, 16);
858693c6
FB
853 if (type == 0 || type == 1) {
854 if (cpu_breakpoint_insert(env, addr) < 0)
855 goto breakpoint_error;
856 put_packet(s, "OK");
857 } else {
858 breakpoint_error:
905f20b1 859 put_packet(s, "E22");
858693c6
FB
860 }
861 break;
862 case 'z':
863 type = strtoul(p, (char **)&p, 16);
864 if (*p == ',')
865 p++;
9d9754a3 866 addr = strtoull(p, (char **)&p, 16);
858693c6
FB
867 if (*p == ',')
868 p++;
9d9754a3 869 len = strtoull(p, (char **)&p, 16);
858693c6
FB
870 if (type == 0 || type == 1) {
871 cpu_breakpoint_remove(env, addr);
872 put_packet(s, "OK");
873 } else {
874 goto breakpoint_error;
875 }
876 break;
831b7825 877#ifdef CONFIG_LINUX_USER
978efd6a
PB
878 case 'q':
879 if (strncmp(p, "Offsets", 7) == 0) {
880 TaskState *ts = env->opaque;
881
882 sprintf(buf, "Text=%x;Data=%x;Bss=%x", ts->info->code_offset,
883 ts->info->data_offset, ts->info->data_offset);
884 put_packet(s, buf);
885 break;
886 }
887 /* Fall through. */
888#endif
858693c6
FB
889 default:
890 // unknown_command:
891 /* put empty packet */
892 buf[0] = '\0';
893 put_packet(s, buf);
894 break;
895 }
896 return RS_IDLE;
897}
898
612458f5
FB
899extern void tb_flush(CPUState *env);
900
1fddef4b 901#ifndef CONFIG_USER_ONLY
858693c6
FB
902static void gdb_vm_stopped(void *opaque, int reason)
903{
904 GDBState *s = opaque;
905 char buf[256];
906 int ret;
907
a2d1ebaf
PB
908 if (s->state == RS_SYSCALL)
909 return;
910
858693c6 911 /* disable single step if it was enable */
6a00d601 912 cpu_single_step(s->env, 0);
858693c6 913
e80cfcfc 914 if (reason == EXCP_DEBUG) {
6a00d601 915 tb_flush(s->env);
858693c6 916 ret = SIGTRAP;
bbeb7b5c
FB
917 } else if (reason == EXCP_INTERRUPT) {
918 ret = SIGINT;
919 } else {
858693c6 920 ret = 0;
bbeb7b5c 921 }
858693c6
FB
922 snprintf(buf, sizeof(buf), "S%02x", ret);
923 put_packet(s, buf);
924}
1fddef4b 925#endif
858693c6 926
a2d1ebaf
PB
927/* Send a gdb syscall request.
928 This accepts limited printf-style format specifiers, specifically:
929 %x - target_ulong argument printed in hex.
930 %s - string pointer (target_ulong) and length (int) pair. */
931void gdb_do_syscall(gdb_syscall_complete_cb cb, char *fmt, ...)
932{
933 va_list va;
934 char buf[256];
935 char *p;
936 target_ulong addr;
937 GDBState *s;
938
939 s = gdb_syscall_state;
940 if (!s)
941 return;
942 gdb_current_syscall_cb = cb;
943 s->state = RS_SYSCALL;
944#ifndef CONFIG_USER_ONLY
945 vm_stop(EXCP_DEBUG);
946#endif
947 s->state = RS_IDLE;
948 va_start(va, fmt);
949 p = buf;
950 *(p++) = 'F';
951 while (*fmt) {
952 if (*fmt == '%') {
953 fmt++;
954 switch (*fmt++) {
955 case 'x':
956 addr = va_arg(va, target_ulong);
957 p += sprintf(p, TARGET_FMT_lx, addr);
958 break;
959 case 's':
960 addr = va_arg(va, target_ulong);
961 p += sprintf(p, TARGET_FMT_lx "/%x", addr, va_arg(va, int));
962 break;
963 default:
964 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
965 fmt - 1);
966 break;
967 }
968 } else {
969 *(p++) = *(fmt++);
970 }
971 }
972 va_end(va);
973 put_packet(s, buf);
974#ifdef CONFIG_USER_ONLY
975 gdb_handlesig(s->env, 0);
976#else
977 cpu_interrupt(s->env, CPU_INTERRUPT_EXIT);
978#endif
979}
980
6a00d601 981static void gdb_read_byte(GDBState *s, int ch)
858693c6 982{
6a00d601 983 CPUState *env = s->env;
858693c6
FB
984 int i, csum;
985 char reply[1];
986
1fddef4b 987#ifndef CONFIG_USER_ONLY
4046d913
PB
988 if (s->last_packet_len) {
989 /* Waiting for a response to the last packet. If we see the start
990 of a new command then abandon the previous response. */
991 if (ch == '-') {
992#ifdef DEBUG_GDB
993 printf("Got NACK, retransmitting\n");
994#endif
995 put_buffer(s, s->last_packet, s->last_packet_len);
996 }
997#ifdef DEBUG_GDB
998 else if (ch == '+')
999 printf("Got ACK\n");
1000 else
1001 printf("Got '%c' when expecting ACK/NACK\n", ch);
1002#endif
1003 if (ch == '+' || ch == '$')
1004 s->last_packet_len = 0;
1005 if (ch != '$')
1006 return;
1007 }
858693c6
FB
1008 if (vm_running) {
1009 /* when the CPU is running, we cannot do anything except stop
1010 it when receiving a char */
1011 vm_stop(EXCP_INTERRUPT);
41625033 1012 } else
1fddef4b 1013#endif
41625033 1014 {
858693c6
FB
1015 switch(s->state) {
1016 case RS_IDLE:
1017 if (ch == '$') {
1018 s->line_buf_index = 0;
1019 s->state = RS_GETLINE;
c33a346e 1020 }
b4608c04 1021 break;
858693c6
FB
1022 case RS_GETLINE:
1023 if (ch == '#') {
1024 s->state = RS_CHKSUM1;
1025 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1026 s->state = RS_IDLE;
4c3a88a2 1027 } else {
858693c6 1028 s->line_buf[s->line_buf_index++] = ch;
4c3a88a2
FB
1029 }
1030 break;
858693c6
FB
1031 case RS_CHKSUM1:
1032 s->line_buf[s->line_buf_index] = '\0';
1033 s->line_csum = fromhex(ch) << 4;
1034 s->state = RS_CHKSUM2;
1035 break;
1036 case RS_CHKSUM2:
1037 s->line_csum |= fromhex(ch);
1038 csum = 0;
1039 for(i = 0; i < s->line_buf_index; i++) {
1040 csum += s->line_buf[i];
1041 }
1042 if (s->line_csum != (csum & 0xff)) {
1043 reply[0] = '-';
1044 put_buffer(s, reply, 1);
1045 s->state = RS_IDLE;
4c3a88a2 1046 } else {
858693c6
FB
1047 reply[0] = '+';
1048 put_buffer(s, reply, 1);
1fddef4b 1049 s->state = gdb_handle_packet(s, env, s->line_buf);
4c3a88a2
FB
1050 }
1051 break;
a2d1ebaf
PB
1052 default:
1053 abort();
858693c6
FB
1054 }
1055 }
1056}
1057
1fddef4b
FB
1058#ifdef CONFIG_USER_ONLY
1059int
1060gdb_handlesig (CPUState *env, int sig)
1061{
1062 GDBState *s;
1063 char buf[256];
1064 int n;
1065
1066 if (gdbserver_fd < 0)
1067 return sig;
1068
1069 s = &gdbserver_state;
1070
1071 /* disable single step if it was enabled */
1072 cpu_single_step(env, 0);
1073 tb_flush(env);
1074
1075 if (sig != 0)
1076 {
1077 snprintf(buf, sizeof(buf), "S%02x", sig);
1078 put_packet(s, buf);
1079 }
1080
1fddef4b
FB
1081 sig = 0;
1082 s->state = RS_IDLE;
41625033
FB
1083 s->running_state = 0;
1084 while (s->running_state == 0) {
1fddef4b
FB
1085 n = read (s->fd, buf, 256);
1086 if (n > 0)
1087 {
1088 int i;
1089
1090 for (i = 0; i < n; i++)
6a00d601 1091 gdb_read_byte (s, buf[i]);
1fddef4b
FB
1092 }
1093 else if (n == 0 || errno != EAGAIN)
1094 {
1095 /* XXX: Connection closed. Should probably wait for annother
1096 connection before continuing. */
1097 return sig;
1098 }
41625033 1099 }
1fddef4b
FB
1100 return sig;
1101}
e9009676
FB
1102
1103/* Tell the remote gdb that the process has exited. */
1104void gdb_exit(CPUState *env, int code)
1105{
1106 GDBState *s;
1107 char buf[4];
1108
1109 if (gdbserver_fd < 0)
1110 return;
1111
1112 s = &gdbserver_state;
1113
1114 snprintf(buf, sizeof(buf), "W%02x", code);
1115 put_packet(s, buf);
1116}
1117
1fddef4b 1118
7c9d8e07 1119static void gdb_accept(void *opaque)
858693c6
FB
1120{
1121 GDBState *s;
1122 struct sockaddr_in sockaddr;
1123 socklen_t len;
1124 int val, fd;
1125
1126 for(;;) {
1127 len = sizeof(sockaddr);
1128 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1129 if (fd < 0 && errno != EINTR) {
1130 perror("accept");
1131 return;
1132 } else if (fd >= 0) {
b4608c04
FB
1133 break;
1134 }
1135 }
858693c6
FB
1136
1137 /* set short latency */
1138 val = 1;
8f447cc7 1139 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
858693c6 1140
1fddef4b
FB
1141 s = &gdbserver_state;
1142 memset (s, 0, sizeof (GDBState));
6a00d601 1143 s->env = first_cpu; /* XXX: allow to change CPU */
858693c6
FB
1144 s->fd = fd;
1145
a2d1ebaf
PB
1146 gdb_syscall_state = s;
1147
858693c6 1148 fcntl(fd, F_SETFL, O_NONBLOCK);
858693c6
FB
1149}
1150
1151static int gdbserver_open(int port)
1152{
1153 struct sockaddr_in sockaddr;
1154 int fd, val, ret;
1155
1156 fd = socket(PF_INET, SOCK_STREAM, 0);
1157 if (fd < 0) {
1158 perror("socket");
1159 return -1;
1160 }
1161
1162 /* allow fast reuse */
1163 val = 1;
8f447cc7 1164 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
858693c6
FB
1165
1166 sockaddr.sin_family = AF_INET;
1167 sockaddr.sin_port = htons(port);
1168 sockaddr.sin_addr.s_addr = 0;
1169 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1170 if (ret < 0) {
1171 perror("bind");
1172 return -1;
1173 }
1174 ret = listen(fd, 0);
1175 if (ret < 0) {
1176 perror("listen");
1177 return -1;
1178 }
858693c6
FB
1179 return fd;
1180}
1181
1182int gdbserver_start(int port)
1183{
1184 gdbserver_fd = gdbserver_open(port);
1185 if (gdbserver_fd < 0)
1186 return -1;
1187 /* accept connections */
7c9d8e07 1188 gdb_accept (NULL);
4046d913
PB
1189 return 0;
1190}
1fddef4b 1191#else
4046d913
PB
1192static int gdb_chr_can_recieve(void *opaque)
1193{
1194 return 1;
1195}
1196
1197static void gdb_chr_recieve(void *opaque, const uint8_t *buf, int size)
1198{
1199 GDBState *s = opaque;
1200 int i;
1201
1202 for (i = 0; i < size; i++) {
1203 gdb_read_byte(s, buf[i]);
1204 }
1205}
1206
1207static void gdb_chr_event(void *opaque, int event)
1208{
1209 switch (event) {
1210 case CHR_EVENT_RESET:
1211 vm_stop(EXCP_INTERRUPT);
a2d1ebaf 1212 gdb_syscall_state = opaque;
4046d913
PB
1213 break;
1214 default:
1215 break;
1216 }
1217}
1218
1219int gdbserver_start(CharDriverState *chr)
1220{
1221 GDBState *s;
1222
1223 if (!chr)
1224 return -1;
1225
1226 s = qemu_mallocz(sizeof(GDBState));
1227 if (!s) {
1228 return -1;
1229 }
1230 s->env = first_cpu; /* XXX: allow to change CPU */
1231 s->chr = chr;
1232 qemu_chr_add_handlers(chr, gdb_chr_can_recieve, gdb_chr_recieve,
1233 gdb_chr_event, s);
1234 qemu_add_vm_stop_handler(gdb_vm_stopped, s);
b4608c04
FB
1235 return 0;
1236}
c636bb66
FB
1237
1238int gdbserver_start_port(int port)
1239{
1240 CharDriverState *chr;
1241 char gdbstub_port_name[128];
1242
1243 snprintf(gdbstub_port_name, sizeof(gdbstub_port_name),
1244 "tcp::%d,nowait,nodelay,server", port);
1245 chr = qemu_chr_open(gdbstub_port_name);
1246 if (!chr)
1247 return -EIO;
1248 return gdbserver_start(chr);
1249}
1250
4046d913 1251#endif