Statistics
| Branch: | Revision:

root / gdbstub.c @ 59467bac

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