Statistics
| Branch: | Revision:

root / gdbstub.c @ ef5b2344

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