gdbstub.c 26.9 KB
Newer Older
bellard's avatar
bellard committed
1
2
3
/*
 * gdb server stub
 * 
bellard's avatar
bellard committed
4
 * Copyright (c) 2003-2005 Fabrice Bellard
bellard's avatar
bellard committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
20
#include "config.h"
21
22
23
24
25
26
27
#ifdef CONFIG_USER_ONLY
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
28
#include <fcntl.h>
29
30
31

#include "qemu.h"
#else
bellard's avatar
bellard committed
32
#include "vl.h"
33
#endif
bellard's avatar
bellard committed
34

bellard's avatar
bellard committed
35
36
37
38
39
40
41
42
43
44
#include "qemu_socket.h"
#ifdef _WIN32
/* XXX: these constants may be independent of the host ones even for Unix */
#ifndef SIGTRAP
#define SIGTRAP 5
#endif
#ifndef SIGINT
#define SIGINT 2
#endif
#else
bellard's avatar
bellard committed
45
#include <signal.h>
bellard's avatar
bellard committed
46
#endif
bellard's avatar
bellard committed
47

bellard's avatar
bellard committed
48
//#define DEBUG_GDB
bellard's avatar
bellard committed
49

50
51
52
53
54
55
56
enum RSState {
    RS_IDLE,
    RS_GETLINE,
    RS_CHKSUM1,
    RS_CHKSUM2,
};
typedef struct GDBState {
bellard's avatar
bellard committed
57
    CPUState *env; /* current CPU */
bellard's avatar
bellard committed
58
    enum RSState state; /* parsing state */
59
60
61
    char line_buf[4096];
    int line_buf_index;
    int line_csum;
62
63
    char last_packet[4100];
    int last_packet_len;
bellard's avatar
bellard committed
64
#ifdef CONFIG_USER_ONLY
65
    int fd;
bellard's avatar
bellard committed
66
    int running_state;
67
68
#else
    CharDriverState *chr;
bellard's avatar
bellard committed
69
#endif
70
} GDBState;
bellard's avatar
bellard committed
71

72
#ifdef CONFIG_USER_ONLY
73
74
75
/* XXX: This is not thread safe.  Do we care?  */
static int gdbserver_fd = -1;

76
77
78
/* XXX: remove this hack.  */
static GDBState gdbserver_state;

79
static int get_char(GDBState *s)
bellard's avatar
bellard committed
80
81
82
83
84
{
    uint8_t ch;
    int ret;

    for(;;) {
bellard's avatar
bellard committed
85
        ret = recv(s->fd, &ch, 1, 0);
bellard's avatar
bellard committed
86
87
88
89
90
91
92
93
94
95
96
        if (ret < 0) {
            if (errno != EINTR && errno != EAGAIN)
                return -1;
        } else if (ret == 0) {
            return -1;
        } else {
            break;
        }
    }
    return ch;
}
97
#endif
bellard's avatar
bellard committed
98

99
static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellard's avatar
bellard committed
100
{
101
#ifdef CONFIG_USER_ONLY
bellard's avatar
bellard committed
102
103
104
    int ret;

    while (len > 0) {
bellard's avatar
bellard committed
105
        ret = send(s->fd, buf, len, 0);
bellard's avatar
bellard committed
106
107
108
109
110
111
112
113
        if (ret < 0) {
            if (errno != EINTR && errno != EAGAIN)
                return;
        } else {
            buf += ret;
            len -= ret;
        }
    }
114
115
116
#else
    qemu_chr_write(s->chr, buf, len);
#endif
bellard's avatar
bellard committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
}

static inline int fromhex(int v)
{
    if (v >= '0' && v <= '9')
        return v - '0';
    else if (v >= 'A' && v <= 'F')
        return v - 'A' + 10;
    else if (v >= 'a' && v <= 'f')
        return v - 'a' + 10;
    else
        return 0;
}

static inline int tohex(int v)
{
    if (v < 10)
        return v + '0';
    else
        return v - 10 + 'a';
}

static void memtohex(char *buf, const uint8_t *mem, int len)
{
    int i, c;
    char *q;
    q = buf;
    for(i = 0; i < len; i++) {
        c = mem[i];
        *q++ = tohex(c >> 4);
        *q++ = tohex(c & 0xf);
    }
    *q = '\0';
}

static void hextomem(uint8_t *mem, const char *buf, int len)
{
    int i;

    for(i = 0; i < len; i++) {
        mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
        buf += 2;
    }
}

/* return -1 if error, 0 if OK */
163
static int put_packet(GDBState *s, char *buf)
bellard's avatar
bellard committed
164
{
165
166
    int len, csum, i;
    char *p;
bellard's avatar
bellard committed
167
168
169
170
171
172

#ifdef DEBUG_GDB
    printf("reply='%s'\n", buf);
#endif

    for(;;) {
173
174
        p = s->last_packet;
        *(p++) = '$';
bellard's avatar
bellard committed
175
        len = strlen(buf);
176
177
        memcpy(p, buf, len);
        p += len;
bellard's avatar
bellard committed
178
179
180
181
        csum = 0;
        for(i = 0; i < len; i++) {
            csum += buf[i];
        }
182
183
184
        *(p++) = '#';
        *(p++) = tohex((csum >> 4) & 0xf);
        *(p++) = tohex((csum) & 0xf);
bellard's avatar
bellard committed
185

186
187
        s->last_packet_len = p - s->last_packet;
        put_buffer(s, s->last_packet, s->last_packet_len);
bellard's avatar
bellard committed
188

189
190
191
#ifdef CONFIG_USER_ONLY
        i = get_char(s);
        if (i < 0)
bellard's avatar
bellard committed
192
            return -1;
193
        if (i == '+')
bellard's avatar
bellard committed
194
            break;
195
196
197
#else
        break;
#endif
bellard's avatar
bellard committed
198
199
200
201
    }
    return 0;
}

bellard's avatar
bellard committed
202
203
204
205
#if defined(TARGET_I386)

static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
{
206
    uint32_t *registers = (uint32_t *)mem_buf;
bellard's avatar
bellard committed
207
208
209
    int i, fpus;

    for(i = 0; i < 8; i++) {
210
        registers[i] = env->regs[i];
bellard's avatar
bellard committed
211
    }
212
213
214
215
216
217
218
219
    registers[8] = env->eip;
    registers[9] = env->eflags;
    registers[10] = env->segs[R_CS].selector;
    registers[11] = env->segs[R_SS].selector;
    registers[12] = env->segs[R_DS].selector;
    registers[13] = env->segs[R_ES].selector;
    registers[14] = env->segs[R_FS].selector;
    registers[15] = env->segs[R_GS].selector;
bellard's avatar
bellard committed
220
221
222
223
    /* XXX: convert floats */
    for(i = 0; i < 8; i++) {
        memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10);
    }
224
    registers[36] = env->fpuc;
bellard's avatar
bellard committed
225
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
226
227
228
229
230
231
232
233
234
235
236
237
    registers[37] = fpus;
    registers[38] = 0; /* XXX: convert tags */
    registers[39] = 0; /* fiseg */
    registers[40] = 0; /* fioff */
    registers[41] = 0; /* foseg */
    registers[42] = 0; /* fooff */
    registers[43] = 0; /* fop */
    
    for(i = 0; i < 16; i++)
        tswapls(&registers[i]);
    for(i = 36; i < 44; i++)
        tswapls(&registers[i]);
bellard's avatar
bellard committed
238
239
240
241
242
243
244
245
246
247
248
    return 44 * 4;
}

static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
{
    uint32_t *registers = (uint32_t *)mem_buf;
    int i;

    for(i = 0; i < 8; i++) {
        env->regs[i] = tswapl(registers[i]);
    }
249
250
    env->eip = tswapl(registers[8]);
    env->eflags = tswapl(registers[9]);
bellard's avatar
bellard committed
251
252
253
254
255
256
257
258
259
260
261
262
263
#if defined(CONFIG_USER_ONLY)
#define LOAD_SEG(index, sreg)\
            if (tswapl(registers[index]) != env->segs[sreg].selector)\
                cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
            LOAD_SEG(10, R_CS);
            LOAD_SEG(11, R_SS);
            LOAD_SEG(12, R_DS);
            LOAD_SEG(13, R_ES);
            LOAD_SEG(14, R_FS);
            LOAD_SEG(15, R_GS);
#endif
}

bellard's avatar
bellard committed
264
265
266
#elif defined (TARGET_PPC)
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
{
267
    uint32_t *registers = (uint32_t *)mem_buf, tmp;
bellard's avatar
bellard committed
268
269
270
    int i;

    /* fill in gprs */
271
    for(i = 0; i < 32; i++) {
272
        registers[i] = tswapl(env->gpr[i]);
bellard's avatar
bellard committed
273
274
275
    }
    /* fill in fprs */
    for (i = 0; i < 32; i++) {
276
277
        registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
	registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1));
bellard's avatar
bellard committed
278
279
    }
    /* nip, msr, ccr, lnk, ctr, xer, mq */
280
    registers[96] = tswapl(env->nip);
281
    registers[97] = tswapl(do_load_msr(env));
bellard's avatar
bellard committed
282
283
    tmp = 0;
    for (i = 0; i < 8; i++)
284
        tmp |= env->crf[i] << (32 - ((i + 1) * 4));
285
286
287
    registers[98] = tswapl(tmp);
    registers[99] = tswapl(env->lr);
    registers[100] = tswapl(env->ctr);
288
    registers[101] = tswapl(do_load_xer(env));
289
    registers[102] = 0;
290
291

    return 103 * 4;
bellard's avatar
bellard committed
292
293
294
295
296
297
298
299
300
}

static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
{
    uint32_t *registers = (uint32_t *)mem_buf;
    int i;

    /* fill in gprs */
    for (i = 0; i < 32; i++) {
301
        env->gpr[i] = tswapl(registers[i]);
bellard's avatar
bellard committed
302
303
304
    }
    /* fill in fprs */
    for (i = 0; i < 32; i++) {
305
306
        *((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]);
	*((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]);
bellard's avatar
bellard committed
307
308
    }
    /* nip, msr, ccr, lnk, ctr, xer, mq */
309
    env->nip = tswapl(registers[96]);
310
    do_store_msr(env, tswapl(registers[97]));
311
    registers[98] = tswapl(registers[98]);
bellard's avatar
bellard committed
312
    for (i = 0; i < 8; i++)
313
        env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF;
314
315
    env->lr = tswapl(registers[99]);
    env->ctr = tswapl(registers[100]);
316
    do_store_xer(env, tswapl(registers[101]));
317
318
319
320
}
#elif defined (TARGET_SPARC)
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
{
bellard's avatar
bellard committed
321
    target_ulong *registers = (target_ulong *)mem_buf;
322
323
324
    int i;

    /* fill in g0..g7 */
bellard's avatar
bellard committed
325
    for(i = 0; i < 8; i++) {
326
327
328
329
330
331
        registers[i] = tswapl(env->gregs[i]);
    }
    /* fill in register window */
    for(i = 0; i < 24; i++) {
        registers[i + 8] = tswapl(env->regwptr[i]);
    }
332
#ifndef TARGET_SPARC64
333
334
335
336
337
338
    /* fill in fprs */
    for (i = 0; i < 32; i++) {
        registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
    }
    /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
    registers[64] = tswapl(env->y);
bellard's avatar
bellard committed
339
340
341
342
343
344
    {
	target_ulong tmp;

	tmp = GET_PSR(env);
	registers[65] = tswapl(tmp);
    }
345
346
347
348
349
350
351
    registers[66] = tswapl(env->wim);
    registers[67] = tswapl(env->tbr);
    registers[68] = tswapl(env->pc);
    registers[69] = tswapl(env->npc);
    registers[70] = tswapl(env->fsr);
    registers[71] = 0; /* csr */
    registers[72] = 0;
bellard's avatar
bellard committed
352
353
    return 73 * sizeof(target_ulong);
#else
354
355
356
357
358
359
360
    /* fill in fprs */
    for (i = 0; i < 64; i += 2) {
	uint64_t tmp;

        tmp = (uint64_t)tswap32(*((uint32_t *)&env->fpr[i])) << 32;
        tmp |= tswap32(*((uint32_t *)&env->fpr[i + 1]));
        registers[i/2 + 32] = tmp;
bellard's avatar
bellard committed
361
    }
362
363
364
365
366
367
368
    registers[64] = tswapl(env->pc);
    registers[65] = tswapl(env->npc);
    registers[66] = tswapl(env->tstate[env->tl]);
    registers[67] = tswapl(env->fsr);
    registers[68] = tswapl(env->fprs);
    registers[69] = tswapl(env->y);
    return 70 * sizeof(target_ulong);
bellard's avatar
bellard committed
369
#endif
370
371
372
373
}

static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
{
bellard's avatar
bellard committed
374
    target_ulong *registers = (target_ulong *)mem_buf;
375
376
377
378
379
380
381
382
    int i;

    /* fill in g0..g7 */
    for(i = 0; i < 7; i++) {
        env->gregs[i] = tswapl(registers[i]);
    }
    /* fill in register window */
    for(i = 0; i < 24; i++) {
bellard's avatar
bellard committed
383
        env->regwptr[i] = tswapl(registers[i + 8]);
384
    }
385
#ifndef TARGET_SPARC64
386
387
388
389
390
391
    /* fill in fprs */
    for (i = 0; i < 32; i++) {
        *((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
    }
    /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
    env->y = tswapl(registers[64]);
bellard's avatar
bellard committed
392
    PUT_PSR(env, tswapl(registers[65]));
393
394
395
396
397
    env->wim = tswapl(registers[66]);
    env->tbr = tswapl(registers[67]);
    env->pc = tswapl(registers[68]);
    env->npc = tswapl(registers[69]);
    env->fsr = tswapl(registers[70]);
bellard's avatar
bellard committed
398
#else
399
400
401
    for (i = 0; i < 64; i += 2) {
	*((uint32_t *)&env->fpr[i]) = tswap32(registers[i/2 + 32] >> 32);
	*((uint32_t *)&env->fpr[i + 1]) = tswap32(registers[i/2 + 32] & 0xffffffff);
bellard's avatar
bellard committed
402
    }
403
404
405
406
407
408
    env->pc = tswapl(registers[64]);
    env->npc = tswapl(registers[65]);
    env->tstate[env->tl] = tswapl(registers[66]);
    env->fsr = tswapl(registers[67]);
    env->fprs = tswapl(registers[68]);
    env->y = tswapl(registers[69]);
bellard's avatar
bellard committed
409
#endif
bellard's avatar
bellard committed
410
}
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#elif defined (TARGET_ARM)
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
{
    int i;
    uint8_t *ptr;

    ptr = mem_buf;
    /* 16 core integer registers (4 bytes each).  */
    for (i = 0; i < 16; i++)
      {
        *(uint32_t *)ptr = tswapl(env->regs[i]);
        ptr += 4;
      }
    /* 8 FPA registers (12 bytes each), FPS (4 bytes).
       Not yet implemented.  */
    memset (ptr, 0, 8 * 12 + 4);
    ptr += 8 * 12 + 4;
    /* CPSR (4 bytes).  */
bellard's avatar
bellard committed
429
    *(uint32_t *)ptr = tswapl (cpsr_read(env));
430
431
432
433
    ptr += 4;

    return ptr - mem_buf;
}
bellard's avatar
bellard committed
434

435
436
437
438
439
440
441
442
443
444
445
446
447
448
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
{
    int i;
    uint8_t *ptr;

    ptr = mem_buf;
    /* Core integer registers.  */
    for (i = 0; i < 16; i++)
      {
        env->regs[i] = tswapl(*(uint32_t *)ptr);
        ptr += 4;
      }
    /* Ignore FPA regs and scr.  */
    ptr += 8 * 12 + 4;
bellard's avatar
bellard committed
449
    cpsr_write (env, tswapl(*(uint32_t *)ptr), 0xffffffff);
450
}
pbrook's avatar
pbrook committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#elif defined (TARGET_M68K)
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
{
    int i;
    uint8_t *ptr;
    CPU_DoubleU u;

    ptr = mem_buf;
    /* D0-D7 */
    for (i = 0; i < 8; i++) {
        *(uint32_t *)ptr = tswapl(env->dregs[i]);
        ptr += 4;
    }
    /* A0-A7 */
    for (i = 0; i < 8; i++) {
        *(uint32_t *)ptr = tswapl(env->aregs[i]);
        ptr += 4;
    }
    *(uint32_t *)ptr = tswapl(env->sr);
    ptr += 4;
    *(uint32_t *)ptr = tswapl(env->pc);
    ptr += 4;
    /* F0-F7.  The 68881/68040 have 12-bit extended precision registers.
       ColdFire has 8-bit double precision registers.  */
    for (i = 0; i < 8; i++) {
        u.d = env->fregs[i];
        *(uint32_t *)ptr = tswap32(u.l.upper);
        *(uint32_t *)ptr = tswap32(u.l.lower);
    }
    /* FP control regs (not implemented).  */
    memset (ptr, 0, 3 * 4);
    ptr += 3 * 4;

    return ptr - mem_buf;
}

static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
{
    int i;
    uint8_t *ptr;
    CPU_DoubleU u;

    ptr = mem_buf;
    /* D0-D7 */
    for (i = 0; i < 8; i++) {
        env->dregs[i] = tswapl(*(uint32_t *)ptr);
        ptr += 4;
    }
    /* A0-A7 */
    for (i = 0; i < 8; i++) {
        env->aregs[i] = tswapl(*(uint32_t *)ptr);
        ptr += 4;
    }
    env->sr = tswapl(*(uint32_t *)ptr);
    ptr += 4;
    env->pc = tswapl(*(uint32_t *)ptr);
    ptr += 4;
    /* F0-F7.  The 68881/68040 have 12-bit extended precision registers.
       ColdFire has 8-bit double precision registers.  */
    for (i = 0; i < 8; i++) {
        u.l.upper = tswap32(*(uint32_t *)ptr); 
        u.l.lower = tswap32(*(uint32_t *)ptr);
        env->fregs[i] = u.d;
    }
    /* FP control regs (not implemented).  */
    ptr += 3 * 4;
}
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#elif defined (TARGET_MIPS)
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
{
    int i;
    uint8_t *ptr;

    ptr = mem_buf;
    for (i = 0; i < 32; i++)
      {
        *(uint32_t *)ptr = tswapl(env->gpr[i]);
        ptr += 4;
      }

    *(uint32_t *)ptr = tswapl(env->CP0_Status);
    ptr += 4;

    *(uint32_t *)ptr = tswapl(env->LO);
    ptr += 4;

    *(uint32_t *)ptr = tswapl(env->HI);
    ptr += 4;

    *(uint32_t *)ptr = tswapl(env->CP0_BadVAddr);
    ptr += 4;

    *(uint32_t *)ptr = tswapl(env->CP0_Cause);
    ptr += 4;

    *(uint32_t *)ptr = tswapl(env->PC);
    ptr += 4;

549
550
551
552
553
554
555
556
557
558
559
560
561
562
#ifdef MIPS_USES_FPU
    for (i = 0; i < 32; i++)
      {
        *(uint32_t *)ptr = tswapl(FPR_W (env, i));
        ptr += 4;
      }

    *(uint32_t *)ptr = tswapl(env->fcr31);
    ptr += 4;

    *(uint32_t *)ptr = tswapl(env->fcr0);
    ptr += 4;
#endif

563
    /* 32 FP registers, fsr, fir, fp.  Not yet implemented.  */
564
    /* what's 'fp' mean here?  */
565
566
567
568

    return ptr - mem_buf;
}

569
570
571
572
573
574
575
576
577
578
579
/* convert MIPS rounding mode in FCR31 to IEEE library */
static unsigned int ieee_rm[] =
  {
    float_round_nearest_even,
    float_round_to_zero,
    float_round_up,
    float_round_down
  };
#define RESTORE_ROUNDING_MODE \
    set_float_rounding_mode(ieee_rm[env->fcr31 & 3], &env->fp_status)

580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
{
    int i;
    uint8_t *ptr;

    ptr = mem_buf;
    for (i = 0; i < 32; i++)
      {
        env->gpr[i] = tswapl(*(uint32_t *)ptr);
        ptr += 4;
      }

    env->CP0_Status = tswapl(*(uint32_t *)ptr);
    ptr += 4;

    env->LO = tswapl(*(uint32_t *)ptr);
    ptr += 4;

    env->HI = tswapl(*(uint32_t *)ptr);
    ptr += 4;

    env->CP0_BadVAddr = tswapl(*(uint32_t *)ptr);
    ptr += 4;

    env->CP0_Cause = tswapl(*(uint32_t *)ptr);
    ptr += 4;

    env->PC = tswapl(*(uint32_t *)ptr);
    ptr += 4;
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630

#ifdef MIPS_USES_FPU
    for (i = 0; i < 32; i++)
      {
	FPR_W (env, i) = tswapl(*(uint32_t *)ptr);
        ptr += 4;
      }

    env->fcr31 = tswapl(*(uint32_t *)ptr) & 0x0183FFFF;
    ptr += 4;

    env->fcr0 = tswapl(*(uint32_t *)ptr);
    ptr += 4;

    /* set rounding mode */
    RESTORE_ROUNDING_MODE;

#ifndef CONFIG_SOFTFLOAT
    /* no floating point exception for native float */
    SET_FP_ENABLE(env->fcr31, 0);
#endif
#endif
631
}
bellard's avatar
bellard committed
632
633
634
635
636
637
638
#elif defined (TARGET_SH4)
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
{
  uint32_t *ptr = (uint32_t *)mem_buf;
  int i;

#define SAVE(x) *ptr++=tswapl(x)
pbrook's avatar
pbrook committed
639
640
641
642
643
644
  if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
      for (i = 0; i < 8; i++) SAVE(env->gregs[i + 16]);
  } else {
      for (i = 0; i < 8; i++) SAVE(env->gregs[i]);
  }
  for (i = 8; i < 16; i++) SAVE(env->gregs[i]);
bellard's avatar
bellard committed
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
  SAVE (env->pc);
  SAVE (env->pr);
  SAVE (env->gbr);
  SAVE (env->vbr);
  SAVE (env->mach);
  SAVE (env->macl);
  SAVE (env->sr);
  SAVE (0); /* TICKS */
  SAVE (0); /* STALLS */
  SAVE (0); /* CYCLES */
  SAVE (0); /* INSTS */
  SAVE (0); /* PLR */

  return ((uint8_t *)ptr - mem_buf);
}

static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
{
  uint32_t *ptr = (uint32_t *)mem_buf;
  int i;

#define LOAD(x) (x)=*ptr++;
pbrook's avatar
pbrook committed
667
668
669
670
671
672
  if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
      for (i = 0; i < 8; i++) LOAD(env->gregs[i + 16]);
  } else {
      for (i = 0; i < 8; i++) LOAD(env->gregs[i]);
  }
  for (i = 8; i < 16; i++) LOAD(env->gregs[i]);
bellard's avatar
bellard committed
673
674
675
676
677
678
679
680
  LOAD (env->pc);
  LOAD (env->pr);
  LOAD (env->gbr);
  LOAD (env->vbr);
  LOAD (env->mach);
  LOAD (env->macl);
  LOAD (env->sr);
}
681
#else
bellard's avatar
bellard committed
682
683
684
685
686
687
688
689
690
691
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
{
    return 0;
}

static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
{
}

#endif
bellard's avatar
bellard committed
692

693
static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf)
bellard's avatar
bellard committed
694
695
{
    const char *p;
696
    int ch, reg_size, type;
bellard's avatar
bellard committed
697
698
699
    char buf[4096];
    uint8_t mem_buf[2000];
    uint32_t *registers;
700
    target_ulong addr, len;
bellard's avatar
bellard committed
701
    
702
703
704
705
706
707
708
#ifdef DEBUG_GDB
    printf("command='%s'\n", line_buf);
#endif
    p = line_buf;
    ch = *p++;
    switch(ch) {
    case '?':
709
        /* TODO: Make this return the correct value for user-mode.  */
710
711
712
713
714
        snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
        put_packet(s, buf);
        break;
    case 'c':
        if (*p != '\0') {
715
            addr = strtoull(p, (char **)&p, 16);
bellard's avatar
bellard committed
716
#if defined(TARGET_I386)
717
            env->eip = addr;
bellard's avatar
ppc fix    
bellard committed
718
#elif defined (TARGET_PPC)
719
            env->nip = addr;
bellard's avatar
bellard committed
720
721
722
#elif defined (TARGET_SPARC)
            env->pc = addr;
            env->npc = addr + 4;
bellard's avatar
bellard committed
723
724
#elif defined (TARGET_ARM)
            env->regs[15] = addr;
bellard's avatar
bellard committed
725
726
#elif defined (TARGET_SH4)
	    env->pc = addr;
bellard's avatar
bellard committed
727
#endif
728
        }
bellard's avatar
bellard committed
729
730
731
732
733
734
#ifdef CONFIG_USER_ONLY
        s->running_state = 1;
#else
        vm_start();
#endif
	return RS_IDLE;
735
736
737
    case 's':
        if (*p != '\0') {
            addr = strtoul(p, (char **)&p, 16);
738
#if defined(TARGET_I386)
739
            env->eip = addr;
bellard's avatar
ppc fix    
bellard committed
740
#elif defined (TARGET_PPC)
741
            env->nip = addr;
bellard's avatar
bellard committed
742
743
744
#elif defined (TARGET_SPARC)
            env->pc = addr;
            env->npc = addr + 4;
bellard's avatar
bellard committed
745
746
#elif defined (TARGET_ARM)
            env->regs[15] = addr;
bellard's avatar
bellard committed
747
748
#elif defined (TARGET_SH4)
	    env->pc = addr;
749
#endif
750
751
        }
        cpu_single_step(env, 1);
bellard's avatar
bellard committed
752
753
754
755
756
757
#ifdef CONFIG_USER_ONLY
        s->running_state = 1;
#else
        vm_start();
#endif
	return RS_IDLE;
758
759
760
761
762
763
764
765
766
767
768
769
770
    case 'g':
        reg_size = cpu_gdb_read_registers(env, mem_buf);
        memtohex(buf, mem_buf, reg_size);
        put_packet(s, buf);
        break;
    case 'G':
        registers = (void *)mem_buf;
        len = strlen(p) / 2;
        hextomem((uint8_t *)registers, p, len);
        cpu_gdb_write_registers(env, mem_buf, len);
        put_packet(s, "OK");
        break;
    case 'm':
771
        addr = strtoull(p, (char **)&p, 16);
772
773
        if (*p == ',')
            p++;
774
        len = strtoull(p, NULL, 16);
775
776
777
778
779
780
        if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) {
            put_packet (s, "E14");
        } else {
            memtohex(buf, mem_buf, len);
            put_packet(s, buf);
        }
781
782
        break;
    case 'M':
783
        addr = strtoull(p, (char **)&p, 16);
784
785
        if (*p == ',')
            p++;
786
        len = strtoull(p, (char **)&p, 16);
787
        if (*p == ':')
788
789
790
            p++;
        hextomem(mem_buf, p, len);
        if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0)
791
            put_packet(s, "E14");
792
793
794
795
796
797
798
        else
            put_packet(s, "OK");
        break;
    case 'Z':
        type = strtoul(p, (char **)&p, 16);
        if (*p == ',')
            p++;
799
        addr = strtoull(p, (char **)&p, 16);
800
801
        if (*p == ',')
            p++;
802
        len = strtoull(p, (char **)&p, 16);
803
804
805
806
807
808
        if (type == 0 || type == 1) {
            if (cpu_breakpoint_insert(env, addr) < 0)
                goto breakpoint_error;
            put_packet(s, "OK");
        } else {
        breakpoint_error:
809
            put_packet(s, "E22");
810
811
812
813
814
815
        }
        break;
    case 'z':
        type = strtoul(p, (char **)&p, 16);
        if (*p == ',')
            p++;
816
        addr = strtoull(p, (char **)&p, 16);
817
818
        if (*p == ',')
            p++;
819
        len = strtoull(p, (char **)&p, 16);
820
821
822
823
824
825
826
        if (type == 0 || type == 1) {
            cpu_breakpoint_remove(env, addr);
            put_packet(s, "OK");
        } else {
            goto breakpoint_error;
        }
        break;
827
#ifdef CONFIG_LINUX_USER
828
829
830
831
832
833
834
835
836
837
838
    case 'q':
        if (strncmp(p, "Offsets", 7) == 0) {
            TaskState *ts = env->opaque;

            sprintf(buf, "Text=%x;Data=%x;Bss=%x", ts->info->code_offset,
                ts->info->data_offset, ts->info->data_offset);
            put_packet(s, buf);
            break;
        }
        /* Fall through.  */
#endif
839
840
841
842
843
844
845
846
847
848
    default:
        //        unknown_command:
        /* put empty packet */
        buf[0] = '\0';
        put_packet(s, buf);
        break;
    }
    return RS_IDLE;
}

bellard's avatar
bellard committed
849
850
extern void tb_flush(CPUState *env);

851
#ifndef CONFIG_USER_ONLY
852
853
854
855
856
857
858
static void gdb_vm_stopped(void *opaque, int reason)
{
    GDBState *s = opaque;
    char buf[256];
    int ret;

    /* disable single step if it was enable */
bellard's avatar
bellard committed
859
    cpu_single_step(s->env, 0);
860

bellard's avatar
bellard committed
861
    if (reason == EXCP_DEBUG) {
bellard's avatar
bellard committed
862
	tb_flush(s->env);
863
        ret = SIGTRAP;
bellard's avatar
bellard committed
864
865
866
    } else if (reason == EXCP_INTERRUPT) {
        ret = SIGINT;
    } else {
867
        ret = 0;
bellard's avatar
bellard committed
868
    }
869
870
871
    snprintf(buf, sizeof(buf), "S%02x", ret);
    put_packet(s, buf);
}
872
#endif
873

bellard's avatar
bellard committed
874
static void gdb_read_byte(GDBState *s, int ch)
875
{
bellard's avatar
bellard committed
876
    CPUState *env = s->env;
877
878
879
    int i, csum;
    char reply[1];

880
#ifndef CONFIG_USER_ONLY
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
    if (s->last_packet_len) {
        /* Waiting for a response to the last packet.  If we see the start
           of a new command then abandon the previous response.  */
        if (ch == '-') {
#ifdef DEBUG_GDB
            printf("Got NACK, retransmitting\n");
#endif
            put_buffer(s, s->last_packet, s->last_packet_len);
        }
#ifdef DEBUG_GDB
        else if (ch == '+')
            printf("Got ACK\n");
        else
            printf("Got '%c' when expecting ACK/NACK\n", ch);
#endif
        if (ch == '+' || ch == '$')
            s->last_packet_len = 0;
        if (ch != '$')
            return;
    }
901
902
903
904
    if (vm_running) {
        /* when the CPU is running, we cannot do anything except stop
           it when receiving a char */
        vm_stop(EXCP_INTERRUPT);
bellard's avatar
bellard committed
905
    } else 
906
#endif
bellard's avatar
bellard committed
907
    {
908
909
910
911
912
        switch(s->state) {
        case RS_IDLE:
            if (ch == '$') {
                s->line_buf_index = 0;
                s->state = RS_GETLINE;
913
            }
bellard's avatar
bellard committed
914
            break;
915
916
917
918
919
        case RS_GETLINE:
            if (ch == '#') {
            s->state = RS_CHKSUM1;
            } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
                s->state = RS_IDLE;
bellard's avatar
bellard committed
920
            } else {
921
            s->line_buf[s->line_buf_index++] = ch;
bellard's avatar
bellard committed
922
923
            }
            break;
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
        case RS_CHKSUM1:
            s->line_buf[s->line_buf_index] = '\0';
            s->line_csum = fromhex(ch) << 4;
            s->state = RS_CHKSUM2;
            break;
        case RS_CHKSUM2:
            s->line_csum |= fromhex(ch);
            csum = 0;
            for(i = 0; i < s->line_buf_index; i++) {
                csum += s->line_buf[i];
            }
            if (s->line_csum != (csum & 0xff)) {
                reply[0] = '-';
                put_buffer(s, reply, 1);
                s->state = RS_IDLE;
bellard's avatar
bellard committed
939
            } else {
940
941
                reply[0] = '+';
                put_buffer(s, reply, 1);
942
                s->state = gdb_handle_packet(s, env, s->line_buf);
bellard's avatar
bellard committed
943
944
            }
            break;
945
946
947
948
        }
    }
}

949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
#ifdef CONFIG_USER_ONLY
int
gdb_handlesig (CPUState *env, int sig)
{
  GDBState *s;
  char buf[256];
  int n;

  if (gdbserver_fd < 0)
    return sig;

  s = &gdbserver_state;

  /* disable single step if it was enabled */
  cpu_single_step(env, 0);
  tb_flush(env);

  if (sig != 0)
    {
      snprintf(buf, sizeof(buf), "S%02x", sig);
      put_packet(s, buf);
    }

  sig = 0;
  s->state = RS_IDLE;
bellard's avatar
bellard committed
974
975
  s->running_state = 0;
  while (s->running_state == 0) {
976
977
978
979
980
981
      n = read (s->fd, buf, 256);
      if (n > 0)
        {
          int i;

          for (i = 0; i < n; i++)
bellard's avatar
bellard committed
982
            gdb_read_byte (s, buf[i]);
983
984
985
986
987
988
989
        }
      else if (n == 0 || errno != EAGAIN)
        {
          /* XXX: Connection closed.  Should probably wait for annother
             connection before continuing.  */
          return sig;
        }
bellard's avatar
bellard committed
990
  }
991
992
  return sig;
}
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008

/* Tell the remote gdb that the process has exited.  */
void gdb_exit(CPUState *env, int code)
{
  GDBState *s;
  char buf[4];

  if (gdbserver_fd < 0)
    return;

  s = &gdbserver_state;

  snprintf(buf, sizeof(buf), "W%02x", code);
  put_packet(s, buf);
}

1009

bellard's avatar
bellard committed
1010
static void gdb_accept(void *opaque)
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
{
    GDBState *s;
    struct sockaddr_in sockaddr;
    socklen_t len;
    int val, fd;

    for(;;) {
        len = sizeof(sockaddr);
        fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
        if (fd < 0 && errno != EINTR) {
            perror("accept");
            return;
        } else if (fd >= 0) {
bellard's avatar
bellard committed
1024
1025
1026
            break;
        }
    }
1027
1028
1029

    /* set short latency */
    val = 1;
bellard's avatar
bellard committed
1030
    setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1031
    
1032
1033
    s = &gdbserver_state;
    memset (s, 0, sizeof (GDBState));
bellard's avatar
bellard committed
1034
    s->env = first_cpu; /* XXX: allow to change CPU */
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
    s->fd = fd;

    fcntl(fd, F_SETFL, O_NONBLOCK);
}

static int gdbserver_open(int port)
{
    struct sockaddr_in sockaddr;
    int fd, val, ret;

    fd = socket(PF_INET, SOCK_STREAM, 0);
    if (fd < 0) {
        perror("socket");
        return -1;
    }

    /* allow fast reuse */
    val = 1;
bellard's avatar
bellard committed
1053
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076

    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(port);
    sockaddr.sin_addr.s_addr = 0;
    ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
    if (ret < 0) {
        perror("bind");
        return -1;
    }
    ret = listen(fd, 0);
    if (ret < 0) {
        perror("listen");
        return -1;
    }
    return fd;
}

int gdbserver_start(int port)
{
    gdbserver_fd = gdbserver_open(port);
    if (gdbserver_fd < 0)
        return -1;
    /* accept connections */
bellard's avatar
bellard committed
1077
    gdb_accept (NULL);
1078
1079
    return 0;
}
1080
#else
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
static int gdb_chr_can_recieve(void *opaque)
{
  return 1;
}

static void gdb_chr_recieve(void *opaque, const uint8_t *buf, int size)
{
    GDBState *s = opaque;
    int i;

    for (i = 0; i < size; i++) {
        gdb_read_byte(s, buf[i]);
    }
}

static void gdb_chr_event(void *opaque, int event)
{
    switch (event) {
    case CHR_EVENT_RESET:
        vm_stop(EXCP_INTERRUPT);
        break;
    default:
        break;
    }
}

int gdbserver_start(CharDriverState *chr)
{
    GDBState *s;

    if (!chr)
        return -1;

    s = qemu_mallocz(sizeof(GDBState));
    if (!s) {
        return -1;
    }
    s->env = first_cpu; /* XXX: allow to change CPU */
    s->chr = chr;
    qemu_chr_add_handlers(chr, gdb_chr_can_recieve, gdb_chr_recieve,
                          gdb_chr_event, s);
    qemu_add_vm_stop_handler(gdb_vm_stopped, s);
bellard's avatar
bellard committed
1123
1124
    return 0;
}
1125
#endif