Statistics
| Branch: | Revision:

root / gdbstub.c @ 0c45d3d4

History | View | Annotate | Download (69.8 kB)

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