Statistics
| Branch: | Revision:

root / gdbstub.c @ b3ce604e

History | View | Annotate | Download (74 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 fdf9b3e8 bellard
#elif defined (TARGET_SH4)
1159 6ef99fc5 ths
1160 6ef99fc5 ths
/* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1161 56aebc89 pbrook
/* FIXME: We should use XML for this.  */
1162 56aebc89 pbrook
1163 56aebc89 pbrook
#define NUM_CORE_REGS 59
1164 6ef99fc5 ths
1165 f3840919 Andreas Färber
static int cpu_gdb_read_register(CPUSH4State *env, uint8_t *mem_buf, int n)
1166 fdf9b3e8 bellard
{
1167 56aebc89 pbrook
    if (n < 8) {
1168 56aebc89 pbrook
        if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1169 56aebc89 pbrook
            GET_REGL(env->gregs[n + 16]);
1170 56aebc89 pbrook
        } else {
1171 56aebc89 pbrook
            GET_REGL(env->gregs[n]);
1172 56aebc89 pbrook
        }
1173 56aebc89 pbrook
    } else if (n < 16) {
1174 e192a45c takasi-y@ops.dti.ne.jp
        GET_REGL(env->gregs[n]);
1175 56aebc89 pbrook
    } else if (n >= 25 && n < 41) {
1176 56aebc89 pbrook
        GET_REGL(env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)]);
1177 56aebc89 pbrook
    } else if (n >= 43 && n < 51) {
1178 56aebc89 pbrook
        GET_REGL(env->gregs[n - 43]);
1179 56aebc89 pbrook
    } else if (n >= 51 && n < 59) {
1180 56aebc89 pbrook
        GET_REGL(env->gregs[n - (51 - 16)]);
1181 56aebc89 pbrook
    }
1182 56aebc89 pbrook
    switch (n) {
1183 56aebc89 pbrook
    case 16: GET_REGL(env->pc);
1184 56aebc89 pbrook
    case 17: GET_REGL(env->pr);
1185 56aebc89 pbrook
    case 18: GET_REGL(env->gbr);
1186 56aebc89 pbrook
    case 19: GET_REGL(env->vbr);
1187 56aebc89 pbrook
    case 20: GET_REGL(env->mach);
1188 56aebc89 pbrook
    case 21: GET_REGL(env->macl);
1189 56aebc89 pbrook
    case 22: GET_REGL(env->sr);
1190 56aebc89 pbrook
    case 23: GET_REGL(env->fpul);
1191 56aebc89 pbrook
    case 24: GET_REGL(env->fpscr);
1192 56aebc89 pbrook
    case 41: GET_REGL(env->ssr);
1193 56aebc89 pbrook
    case 42: GET_REGL(env->spc);
1194 56aebc89 pbrook
    }
1195 56aebc89 pbrook
1196 56aebc89 pbrook
    return 0;
1197 fdf9b3e8 bellard
}
1198 fdf9b3e8 bellard
1199 f3840919 Andreas Färber
static int cpu_gdb_write_register(CPUSH4State *env, uint8_t *mem_buf, int n)
1200 fdf9b3e8 bellard
{
1201 56aebc89 pbrook
    uint32_t tmp;
1202 56aebc89 pbrook
1203 56aebc89 pbrook
    tmp = ldl_p(mem_buf);
1204 56aebc89 pbrook
1205 56aebc89 pbrook
    if (n < 8) {
1206 56aebc89 pbrook
        if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1207 56aebc89 pbrook
            env->gregs[n + 16] = tmp;
1208 56aebc89 pbrook
        } else {
1209 56aebc89 pbrook
            env->gregs[n] = tmp;
1210 56aebc89 pbrook
        }
1211 56aebc89 pbrook
        return 4;
1212 56aebc89 pbrook
    } else if (n < 16) {
1213 e192a45c takasi-y@ops.dti.ne.jp
        env->gregs[n] = tmp;
1214 56aebc89 pbrook
        return 4;
1215 56aebc89 pbrook
    } else if (n >= 25 && n < 41) {
1216 56aebc89 pbrook
        env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)] = tmp;
1217 e192a45c takasi-y@ops.dti.ne.jp
        return 4;
1218 56aebc89 pbrook
    } else if (n >= 43 && n < 51) {
1219 56aebc89 pbrook
        env->gregs[n - 43] = tmp;
1220 56aebc89 pbrook
        return 4;
1221 56aebc89 pbrook
    } else if (n >= 51 && n < 59) {
1222 56aebc89 pbrook
        env->gregs[n - (51 - 16)] = tmp;
1223 56aebc89 pbrook
        return 4;
1224 56aebc89 pbrook
    }
1225 56aebc89 pbrook
    switch (n) {
1226 e192a45c takasi-y@ops.dti.ne.jp
    case 16: env->pc = tmp; break;
1227 e192a45c takasi-y@ops.dti.ne.jp
    case 17: env->pr = tmp; break;
1228 e192a45c takasi-y@ops.dti.ne.jp
    case 18: env->gbr = tmp; break;
1229 e192a45c takasi-y@ops.dti.ne.jp
    case 19: env->vbr = tmp; break;
1230 e192a45c takasi-y@ops.dti.ne.jp
    case 20: env->mach = tmp; break;
1231 e192a45c takasi-y@ops.dti.ne.jp
    case 21: env->macl = tmp; break;
1232 e192a45c takasi-y@ops.dti.ne.jp
    case 22: env->sr = tmp; break;
1233 e192a45c takasi-y@ops.dti.ne.jp
    case 23: env->fpul = tmp; break;
1234 e192a45c takasi-y@ops.dti.ne.jp
    case 24: env->fpscr = tmp; break;
1235 e192a45c takasi-y@ops.dti.ne.jp
    case 41: env->ssr = tmp; break;
1236 e192a45c takasi-y@ops.dti.ne.jp
    case 42: env->spc = tmp; break;
1237 56aebc89 pbrook
    default: return 0;
1238 56aebc89 pbrook
    }
1239 56aebc89 pbrook
1240 56aebc89 pbrook
    return 4;
1241 fdf9b3e8 bellard
}
1242 d74d6a99 Edgar E. Iglesias
#elif defined (TARGET_MICROBLAZE)
1243 d74d6a99 Edgar E. Iglesias
1244 d74d6a99 Edgar E. Iglesias
#define NUM_CORE_REGS (32 + 5)
1245 d74d6a99 Edgar E. Iglesias
1246 f3840919 Andreas Färber
static int cpu_gdb_read_register(CPUMBState *env, uint8_t *mem_buf, int n)
1247 d74d6a99 Edgar E. Iglesias
{
1248 d74d6a99 Edgar E. Iglesias
    if (n < 32) {
1249 d74d6a99 Edgar E. Iglesias
        GET_REG32(env->regs[n]);
1250 d74d6a99 Edgar E. Iglesias
    } else {
1251 d74d6a99 Edgar E. Iglesias
        GET_REG32(env->sregs[n - 32]);
1252 d74d6a99 Edgar E. Iglesias
    }
1253 d74d6a99 Edgar E. Iglesias
    return 0;
1254 d74d6a99 Edgar E. Iglesias
}
1255 d74d6a99 Edgar E. Iglesias
1256 f3840919 Andreas Färber
static int cpu_gdb_write_register(CPUMBState *env, uint8_t *mem_buf, int n)
1257 d74d6a99 Edgar E. Iglesias
{
1258 d74d6a99 Edgar E. Iglesias
    uint32_t tmp;
1259 d74d6a99 Edgar E. Iglesias
1260 d74d6a99 Edgar E. Iglesias
    if (n > NUM_CORE_REGS)
1261 d74d6a99 Edgar E. Iglesias
        return 0;
1262 d74d6a99 Edgar E. Iglesias
1263 d74d6a99 Edgar E. Iglesias
    tmp = ldl_p(mem_buf);
1264 d74d6a99 Edgar E. Iglesias
1265 d74d6a99 Edgar E. Iglesias
    if (n < 32) {
1266 d74d6a99 Edgar E. Iglesias
        env->regs[n] = tmp;
1267 d74d6a99 Edgar E. Iglesias
    } else {
1268 d74d6a99 Edgar E. Iglesias
        env->sregs[n - 32] = tmp;
1269 d74d6a99 Edgar E. Iglesias
    }
1270 d74d6a99 Edgar E. Iglesias
    return 4;
1271 d74d6a99 Edgar E. Iglesias
}
1272 f1ccf904 ths
#elif defined (TARGET_CRIS)
1273 f1ccf904 ths
1274 56aebc89 pbrook
#define NUM_CORE_REGS 49
1275 56aebc89 pbrook
1276 4a0b59fe Edgar E. Iglesias
static int
1277 f3840919 Andreas Färber
read_register_crisv10(CPUCRISState *env, uint8_t *mem_buf, int n)
1278 4a0b59fe Edgar E. Iglesias
{
1279 4a0b59fe Edgar E. Iglesias
    if (n < 15) {
1280 4a0b59fe Edgar E. Iglesias
        GET_REG32(env->regs[n]);
1281 4a0b59fe Edgar E. Iglesias
    }
1282 4a0b59fe Edgar E. Iglesias
1283 4a0b59fe Edgar E. Iglesias
    if (n == 15) {
1284 4a0b59fe Edgar E. Iglesias
        GET_REG32(env->pc);
1285 4a0b59fe Edgar E. Iglesias
    }
1286 4a0b59fe Edgar E. Iglesias
1287 4a0b59fe Edgar E. Iglesias
    if (n < 32) {
1288 4a0b59fe Edgar E. Iglesias
        switch (n) {
1289 4a0b59fe Edgar E. Iglesias
        case 16:
1290 4a0b59fe Edgar E. Iglesias
            GET_REG8(env->pregs[n - 16]);
1291 4a0b59fe Edgar E. Iglesias
            break;
1292 4a0b59fe Edgar E. Iglesias
        case 17:
1293 4a0b59fe Edgar E. Iglesias
            GET_REG8(env->pregs[n - 16]);
1294 4a0b59fe Edgar E. Iglesias
            break;
1295 4a0b59fe Edgar E. Iglesias
        case 20:
1296 4a0b59fe Edgar E. Iglesias
        case 21:
1297 4a0b59fe Edgar E. Iglesias
            GET_REG16(env->pregs[n - 16]);
1298 4a0b59fe Edgar E. Iglesias
            break;
1299 4a0b59fe Edgar E. Iglesias
        default:
1300 4a0b59fe Edgar E. Iglesias
            if (n >= 23) {
1301 4a0b59fe Edgar E. Iglesias
                GET_REG32(env->pregs[n - 16]);
1302 4a0b59fe Edgar E. Iglesias
            }
1303 4a0b59fe Edgar E. Iglesias
            break;
1304 4a0b59fe Edgar E. Iglesias
        }
1305 4a0b59fe Edgar E. Iglesias
    }
1306 4a0b59fe Edgar E. Iglesias
    return 0;
1307 4a0b59fe Edgar E. Iglesias
}
1308 4a0b59fe Edgar E. Iglesias
1309 f3840919 Andreas Färber
static int cpu_gdb_read_register(CPUCRISState *env, uint8_t *mem_buf, int n)
1310 f1ccf904 ths
{
1311 56aebc89 pbrook
    uint8_t srs;
1312 56aebc89 pbrook
1313 4a0b59fe Edgar E. Iglesias
    if (env->pregs[PR_VR] < 32)
1314 4a0b59fe Edgar E. Iglesias
        return read_register_crisv10(env, mem_buf, n);
1315 4a0b59fe Edgar E. Iglesias
1316 56aebc89 pbrook
    srs = env->pregs[PR_SRS];
1317 56aebc89 pbrook
    if (n < 16) {
1318 56aebc89 pbrook
        GET_REG32(env->regs[n]);
1319 56aebc89 pbrook
    }
1320 56aebc89 pbrook
1321 56aebc89 pbrook
    if (n >= 21 && n < 32) {
1322 56aebc89 pbrook
        GET_REG32(env->pregs[n - 16]);
1323 56aebc89 pbrook
    }
1324 56aebc89 pbrook
    if (n >= 33 && n < 49) {
1325 56aebc89 pbrook
        GET_REG32(env->sregs[srs][n - 33]);
1326 56aebc89 pbrook
    }
1327 56aebc89 pbrook
    switch (n) {
1328 56aebc89 pbrook
    case 16: GET_REG8(env->pregs[0]);
1329 56aebc89 pbrook
    case 17: GET_REG8(env->pregs[1]);
1330 56aebc89 pbrook
    case 18: GET_REG32(env->pregs[2]);
1331 56aebc89 pbrook
    case 19: GET_REG8(srs);
1332 56aebc89 pbrook
    case 20: GET_REG16(env->pregs[4]);
1333 56aebc89 pbrook
    case 32: GET_REG32(env->pc);
1334 56aebc89 pbrook
    }
1335 56aebc89 pbrook
1336 56aebc89 pbrook
    return 0;
1337 f1ccf904 ths
}
1338 56aebc89 pbrook
1339 f3840919 Andreas Färber
static int cpu_gdb_write_register(CPUCRISState *env, uint8_t *mem_buf, int n)
1340 f1ccf904 ths
{
1341 56aebc89 pbrook
    uint32_t tmp;
1342 56aebc89 pbrook
1343 56aebc89 pbrook
    if (n > 49)
1344 56aebc89 pbrook
        return 0;
1345 56aebc89 pbrook
1346 56aebc89 pbrook
    tmp = ldl_p(mem_buf);
1347 56aebc89 pbrook
1348 56aebc89 pbrook
    if (n < 16) {
1349 56aebc89 pbrook
        env->regs[n] = tmp;
1350 56aebc89 pbrook
    }
1351 56aebc89 pbrook
1352 d7b6967a edgar_igl
    if (n >= 21 && n < 32) {
1353 d7b6967a edgar_igl
        env->pregs[n - 16] = tmp;
1354 d7b6967a edgar_igl
    }
1355 d7b6967a edgar_igl
1356 d7b6967a edgar_igl
    /* FIXME: Should support function regs be writable?  */
1357 56aebc89 pbrook
    switch (n) {
1358 56aebc89 pbrook
    case 16: return 1;
1359 56aebc89 pbrook
    case 17: return 1;
1360 d7b6967a edgar_igl
    case 18: env->pregs[PR_PID] = tmp; break;
1361 56aebc89 pbrook
    case 19: return 1;
1362 56aebc89 pbrook
    case 20: return 2;
1363 56aebc89 pbrook
    case 32: env->pc = tmp; break;
1364 56aebc89 pbrook
    }
1365 56aebc89 pbrook
1366 56aebc89 pbrook
    return 4;
1367 f1ccf904 ths
}
1368 19bf517b aurel32
#elif defined (TARGET_ALPHA)
1369 19bf517b aurel32
1370 7c5a90dd Richard Henderson
#define NUM_CORE_REGS 67
1371 19bf517b aurel32
1372 f3840919 Andreas Färber
static int cpu_gdb_read_register(CPUAlphaState *env, uint8_t *mem_buf, int n)
1373 19bf517b aurel32
{
1374 7c5a90dd Richard Henderson
    uint64_t val;
1375 7c5a90dd Richard Henderson
    CPU_DoubleU d;
1376 19bf517b aurel32
1377 7c5a90dd Richard Henderson
    switch (n) {
1378 7c5a90dd Richard Henderson
    case 0 ... 30:
1379 7c5a90dd Richard Henderson
        val = env->ir[n];
1380 7c5a90dd Richard Henderson
        break;
1381 7c5a90dd Richard Henderson
    case 32 ... 62:
1382 7c5a90dd Richard Henderson
        d.d = env->fir[n - 32];
1383 7c5a90dd Richard Henderson
        val = d.ll;
1384 7c5a90dd Richard Henderson
        break;
1385 7c5a90dd Richard Henderson
    case 63:
1386 7c5a90dd Richard Henderson
        val = cpu_alpha_load_fpcr(env);
1387 7c5a90dd Richard Henderson
        break;
1388 7c5a90dd Richard Henderson
    case 64:
1389 7c5a90dd Richard Henderson
        val = env->pc;
1390 7c5a90dd Richard Henderson
        break;
1391 7c5a90dd Richard Henderson
    case 66:
1392 7c5a90dd Richard Henderson
        val = env->unique;
1393 7c5a90dd Richard Henderson
        break;
1394 7c5a90dd Richard Henderson
    case 31:
1395 7c5a90dd Richard Henderson
    case 65:
1396 7c5a90dd Richard Henderson
        /* 31 really is the zero register; 65 is unassigned in the
1397 7c5a90dd Richard Henderson
           gdb protocol, but is still required to occupy 8 bytes. */
1398 7c5a90dd Richard Henderson
        val = 0;
1399 7c5a90dd Richard Henderson
        break;
1400 7c5a90dd Richard Henderson
    default:
1401 7c5a90dd Richard Henderson
        return 0;
1402 19bf517b aurel32
    }
1403 7c5a90dd Richard Henderson
    GET_REGL(val);
1404 19bf517b aurel32
}
1405 19bf517b aurel32
1406 f3840919 Andreas Färber
static int cpu_gdb_write_register(CPUAlphaState *env, uint8_t *mem_buf, int n)
1407 19bf517b aurel32
{
1408 7c5a90dd Richard Henderson
    target_ulong tmp = ldtul_p(mem_buf);
1409 7c5a90dd Richard Henderson
    CPU_DoubleU d;
1410 19bf517b aurel32
1411 7c5a90dd Richard Henderson
    switch (n) {
1412 7c5a90dd Richard Henderson
    case 0 ... 30:
1413 19bf517b aurel32
        env->ir[n] = tmp;
1414 7c5a90dd Richard Henderson
        break;
1415 7c5a90dd Richard Henderson
    case 32 ... 62:
1416 7c5a90dd Richard Henderson
        d.ll = tmp;
1417 7c5a90dd Richard Henderson
        env->fir[n - 32] = d.d;
1418 7c5a90dd Richard Henderson
        break;
1419 7c5a90dd Richard Henderson
    case 63:
1420 7c5a90dd Richard Henderson
        cpu_alpha_store_fpcr(env, tmp);
1421 7c5a90dd Richard Henderson
        break;
1422 7c5a90dd Richard Henderson
    case 64:
1423 7c5a90dd Richard Henderson
        env->pc = tmp;
1424 7c5a90dd Richard Henderson
        break;
1425 7c5a90dd Richard Henderson
    case 66:
1426 7c5a90dd Richard Henderson
        env->unique = tmp;
1427 7c5a90dd Richard Henderson
        break;
1428 7c5a90dd Richard Henderson
    case 31:
1429 7c5a90dd Richard Henderson
    case 65:
1430 7c5a90dd Richard Henderson
        /* 31 really is the zero register; 65 is unassigned in the
1431 7c5a90dd Richard Henderson
           gdb protocol, but is still required to occupy 8 bytes. */
1432 7c5a90dd Richard Henderson
        break;
1433 7c5a90dd Richard Henderson
    default:
1434 7c5a90dd Richard Henderson
        return 0;
1435 19bf517b aurel32
    }
1436 19bf517b aurel32
    return 8;
1437 19bf517b aurel32
}
1438 afcb0e45 Alexander Graf
#elif defined (TARGET_S390X)
1439 afcb0e45 Alexander Graf
1440 afcb0e45 Alexander Graf
#define NUM_CORE_REGS S390_NUM_TOTAL_REGS
1441 afcb0e45 Alexander Graf
1442 f3840919 Andreas Färber
static int cpu_gdb_read_register(CPUS390XState *env, uint8_t *mem_buf, int n)
1443 afcb0e45 Alexander Graf
{
1444 afcb0e45 Alexander Graf
    switch (n) {
1445 afcb0e45 Alexander Graf
        case S390_PSWM_REGNUM: GET_REGL(env->psw.mask); break;
1446 afcb0e45 Alexander Graf
        case S390_PSWA_REGNUM: GET_REGL(env->psw.addr); break;
1447 afcb0e45 Alexander Graf
        case S390_R0_REGNUM ... S390_R15_REGNUM:
1448 afcb0e45 Alexander Graf
            GET_REGL(env->regs[n-S390_R0_REGNUM]); break;
1449 afcb0e45 Alexander Graf
        case S390_A0_REGNUM ... S390_A15_REGNUM:
1450 afcb0e45 Alexander Graf
            GET_REG32(env->aregs[n-S390_A0_REGNUM]); break;
1451 afcb0e45 Alexander Graf
        case S390_FPC_REGNUM: GET_REG32(env->fpc); break;
1452 afcb0e45 Alexander Graf
        case S390_F0_REGNUM ... S390_F15_REGNUM:
1453 afcb0e45 Alexander Graf
            /* XXX */
1454 afcb0e45 Alexander Graf
            break;
1455 afcb0e45 Alexander Graf
        case S390_PC_REGNUM: GET_REGL(env->psw.addr); break;
1456 59467bac Alexander Graf
        case S390_CC_REGNUM:
1457 59467bac Alexander Graf
            env->cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst,
1458 59467bac Alexander Graf
                                 env->cc_vr);
1459 59467bac Alexander Graf
            GET_REG32(env->cc_op);
1460 59467bac Alexander Graf
            break;
1461 afcb0e45 Alexander Graf
    }
1462 afcb0e45 Alexander Graf
1463 afcb0e45 Alexander Graf
    return 0;
1464 afcb0e45 Alexander Graf
}
1465 afcb0e45 Alexander Graf
1466 f3840919 Andreas Färber
static int cpu_gdb_write_register(CPUS390XState *env, uint8_t *mem_buf, int n)
1467 afcb0e45 Alexander Graf
{
1468 afcb0e45 Alexander Graf
    target_ulong tmpl;
1469 afcb0e45 Alexander Graf
    uint32_t tmp32;
1470 afcb0e45 Alexander Graf
    int r = 8;
1471 afcb0e45 Alexander Graf
    tmpl = ldtul_p(mem_buf);
1472 afcb0e45 Alexander Graf
    tmp32 = ldl_p(mem_buf);
1473 afcb0e45 Alexander Graf
1474 afcb0e45 Alexander Graf
    switch (n) {
1475 afcb0e45 Alexander Graf
        case S390_PSWM_REGNUM: env->psw.mask = tmpl; break;
1476 afcb0e45 Alexander Graf
        case S390_PSWA_REGNUM: env->psw.addr = tmpl; break;
1477 afcb0e45 Alexander Graf
        case S390_R0_REGNUM ... S390_R15_REGNUM:
1478 afcb0e45 Alexander Graf
            env->regs[n-S390_R0_REGNUM] = tmpl; break;
1479 afcb0e45 Alexander Graf
        case S390_A0_REGNUM ... S390_A15_REGNUM:
1480 afcb0e45 Alexander Graf
            env->aregs[n-S390_A0_REGNUM] = tmp32; r=4; break;
1481 afcb0e45 Alexander Graf
        case S390_FPC_REGNUM: env->fpc = tmp32; r=4; break;
1482 afcb0e45 Alexander Graf
        case S390_F0_REGNUM ... S390_F15_REGNUM:
1483 afcb0e45 Alexander Graf
            /* XXX */
1484 afcb0e45 Alexander Graf
            break;
1485 afcb0e45 Alexander Graf
        case S390_PC_REGNUM: env->psw.addr = tmpl; break;
1486 59467bac Alexander Graf
        case S390_CC_REGNUM: env->cc_op = tmp32; r=4; break;
1487 afcb0e45 Alexander Graf
    }
1488 afcb0e45 Alexander Graf
1489 afcb0e45 Alexander Graf
    return r;
1490 afcb0e45 Alexander Graf
}
1491 0c45d3d4 Michael Walle
#elif defined (TARGET_LM32)
1492 0c45d3d4 Michael Walle
1493 0c45d3d4 Michael Walle
#include "hw/lm32_pic.h"
1494 0c45d3d4 Michael Walle
#define NUM_CORE_REGS (32 + 7)
1495 0c45d3d4 Michael Walle
1496 f3840919 Andreas Färber
static int cpu_gdb_read_register(CPULM32State *env, uint8_t *mem_buf, int n)
1497 0c45d3d4 Michael Walle
{
1498 0c45d3d4 Michael Walle
    if (n < 32) {
1499 0c45d3d4 Michael Walle
        GET_REG32(env->regs[n]);
1500 0c45d3d4 Michael Walle
    } else {
1501 0c45d3d4 Michael Walle
        switch (n) {
1502 0c45d3d4 Michael Walle
        case 32:
1503 0c45d3d4 Michael Walle
            GET_REG32(env->pc);
1504 0c45d3d4 Michael Walle
            break;
1505 0c45d3d4 Michael Walle
        /* FIXME: put in right exception ID */
1506 0c45d3d4 Michael Walle
        case 33:
1507 0c45d3d4 Michael Walle
            GET_REG32(0);
1508 0c45d3d4 Michael Walle
            break;
1509 0c45d3d4 Michael Walle
        case 34:
1510 0c45d3d4 Michael Walle
            GET_REG32(env->eba);
1511 0c45d3d4 Michael Walle
            break;
1512 0c45d3d4 Michael Walle
        case 35:
1513 0c45d3d4 Michael Walle
            GET_REG32(env->deba);
1514 0c45d3d4 Michael Walle
            break;
1515 0c45d3d4 Michael Walle
        case 36:
1516 0c45d3d4 Michael Walle
            GET_REG32(env->ie);
1517 0c45d3d4 Michael Walle
            break;
1518 0c45d3d4 Michael Walle
        case 37:
1519 0c45d3d4 Michael Walle
            GET_REG32(lm32_pic_get_im(env->pic_state));
1520 0c45d3d4 Michael Walle
            break;
1521 0c45d3d4 Michael Walle
        case 38:
1522 0c45d3d4 Michael Walle
            GET_REG32(lm32_pic_get_ip(env->pic_state));
1523 0c45d3d4 Michael Walle
            break;
1524 0c45d3d4 Michael Walle
        }
1525 0c45d3d4 Michael Walle
    }
1526 0c45d3d4 Michael Walle
    return 0;
1527 0c45d3d4 Michael Walle
}
1528 0c45d3d4 Michael Walle
1529 f3840919 Andreas Färber
static int cpu_gdb_write_register(CPULM32State *env, uint8_t *mem_buf, int n)
1530 0c45d3d4 Michael Walle
{
1531 0c45d3d4 Michael Walle
    uint32_t tmp;
1532 0c45d3d4 Michael Walle
1533 0c45d3d4 Michael Walle
    if (n > NUM_CORE_REGS) {
1534 0c45d3d4 Michael Walle
        return 0;
1535 0c45d3d4 Michael Walle
    }
1536 0c45d3d4 Michael Walle
1537 0c45d3d4 Michael Walle
    tmp = ldl_p(mem_buf);
1538 0c45d3d4 Michael Walle
1539 0c45d3d4 Michael Walle
    if (n < 32) {
1540 0c45d3d4 Michael Walle
        env->regs[n] = tmp;
1541 0c45d3d4 Michael Walle
    } else {
1542 0c45d3d4 Michael Walle
        switch (n) {
1543 0c45d3d4 Michael Walle
        case 32:
1544 0c45d3d4 Michael Walle
            env->pc = tmp;
1545 0c45d3d4 Michael Walle
            break;
1546 0c45d3d4 Michael Walle
        case 34:
1547 0c45d3d4 Michael Walle
            env->eba = tmp;
1548 0c45d3d4 Michael Walle
            break;
1549 0c45d3d4 Michael Walle
        case 35:
1550 0c45d3d4 Michael Walle
            env->deba = tmp;
1551 0c45d3d4 Michael Walle
            break;
1552 0c45d3d4 Michael Walle
        case 36:
1553 0c45d3d4 Michael Walle
            env->ie = tmp;
1554 0c45d3d4 Michael Walle
            break;
1555 0c45d3d4 Michael Walle
        case 37:
1556 0c45d3d4 Michael Walle
            lm32_pic_set_im(env->pic_state, tmp);
1557 0c45d3d4 Michael Walle
            break;
1558 0c45d3d4 Michael Walle
        case 38:
1559 0c45d3d4 Michael Walle
            lm32_pic_set_ip(env->pic_state, tmp);
1560 0c45d3d4 Michael Walle
            break;
1561 0c45d3d4 Michael Walle
        }
1562 0c45d3d4 Michael Walle
    }
1563 0c45d3d4 Michael Walle
    return 4;
1564 0c45d3d4 Michael Walle
}
1565 ccfcaba6 Max Filippov
#elif defined(TARGET_XTENSA)
1566 ccfcaba6 Max Filippov
1567 ccfcaba6 Max Filippov
/* Use num_core_regs to see only non-privileged registers in an unmodified gdb.
1568 ccfcaba6 Max Filippov
 * Use num_regs to see all registers. gdb modification is required for that:
1569 ccfcaba6 Max Filippov
 * reset bit 0 in the 'flags' field of the registers definitions in the
1570 ccfcaba6 Max Filippov
 * gdb/xtensa-config.c inside gdb source tree or inside gdb overlay.
1571 ccfcaba6 Max Filippov
 */
1572 ccfcaba6 Max Filippov
#define NUM_CORE_REGS (env->config->gdb_regmap.num_regs)
1573 ccfcaba6 Max Filippov
#define num_g_regs NUM_CORE_REGS
1574 ccfcaba6 Max Filippov
1575 f3840919 Andreas Färber
static int cpu_gdb_read_register(CPUXtensaState *env, uint8_t *mem_buf, int n)
1576 ccfcaba6 Max Filippov
{
1577 ccfcaba6 Max Filippov
    const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
1578 ccfcaba6 Max Filippov
1579 ccfcaba6 Max Filippov
    if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
1580 ccfcaba6 Max Filippov
        return 0;
1581 ccfcaba6 Max Filippov
    }
1582 ccfcaba6 Max Filippov
1583 ccfcaba6 Max Filippov
    switch (reg->type) {
1584 ccfcaba6 Max Filippov
    case 9: /*pc*/
1585 ccfcaba6 Max Filippov
        GET_REG32(env->pc);
1586 ccfcaba6 Max Filippov
        break;
1587 ccfcaba6 Max Filippov
1588 ccfcaba6 Max Filippov
    case 1: /*ar*/
1589 ccfcaba6 Max Filippov
        xtensa_sync_phys_from_window(env);
1590 ccfcaba6 Max Filippov
        GET_REG32(env->phys_regs[(reg->targno & 0xff) % env->config->nareg]);
1591 ccfcaba6 Max Filippov
        break;
1592 ccfcaba6 Max Filippov
1593 ccfcaba6 Max Filippov
    case 2: /*SR*/
1594 ccfcaba6 Max Filippov
        GET_REG32(env->sregs[reg->targno & 0xff]);
1595 ccfcaba6 Max Filippov
        break;
1596 ccfcaba6 Max Filippov
1597 ccfcaba6 Max Filippov
    case 3: /*UR*/
1598 ccfcaba6 Max Filippov
        GET_REG32(env->uregs[reg->targno & 0xff]);
1599 ccfcaba6 Max Filippov
        break;
1600 ccfcaba6 Max Filippov
1601 ccfcaba6 Max Filippov
    case 8: /*a*/
1602 ccfcaba6 Max Filippov
        GET_REG32(env->regs[reg->targno & 0x0f]);
1603 ccfcaba6 Max Filippov
        break;
1604 ccfcaba6 Max Filippov
1605 ccfcaba6 Max Filippov
    default:
1606 ccfcaba6 Max Filippov
        qemu_log("%s from reg %d of unsupported type %d\n",
1607 ccfcaba6 Max Filippov
                __func__, n, reg->type);
1608 ccfcaba6 Max Filippov
        return 0;
1609 ccfcaba6 Max Filippov
    }
1610 ccfcaba6 Max Filippov
}
1611 ccfcaba6 Max Filippov
1612 f3840919 Andreas Färber
static int cpu_gdb_write_register(CPUXtensaState *env, uint8_t *mem_buf, int n)
1613 ccfcaba6 Max Filippov
{
1614 ccfcaba6 Max Filippov
    uint32_t tmp;
1615 ccfcaba6 Max Filippov
    const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
1616 ccfcaba6 Max Filippov
1617 ccfcaba6 Max Filippov
    if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
1618 ccfcaba6 Max Filippov
        return 0;
1619 ccfcaba6 Max Filippov
    }
1620 ccfcaba6 Max Filippov
1621 ccfcaba6 Max Filippov
    tmp = ldl_p(mem_buf);
1622 ccfcaba6 Max Filippov
1623 ccfcaba6 Max Filippov
    switch (reg->type) {
1624 ccfcaba6 Max Filippov
    case 9: /*pc*/
1625 ccfcaba6 Max Filippov
        env->pc = tmp;
1626 ccfcaba6 Max Filippov
        break;
1627 ccfcaba6 Max Filippov
1628 ccfcaba6 Max Filippov
    case 1: /*ar*/
1629 ccfcaba6 Max Filippov
        env->phys_regs[(reg->targno & 0xff) % env->config->nareg] = tmp;
1630 ccfcaba6 Max Filippov
        xtensa_sync_window_from_phys(env);
1631 ccfcaba6 Max Filippov
        break;
1632 ccfcaba6 Max Filippov
1633 ccfcaba6 Max Filippov
    case 2: /*SR*/
1634 ccfcaba6 Max Filippov
        env->sregs[reg->targno & 0xff] = tmp;
1635 ccfcaba6 Max Filippov
        break;
1636 ccfcaba6 Max Filippov
1637 ccfcaba6 Max Filippov
    case 3: /*UR*/
1638 ccfcaba6 Max Filippov
        env->uregs[reg->targno & 0xff] = tmp;
1639 ccfcaba6 Max Filippov
        break;
1640 ccfcaba6 Max Filippov
1641 ccfcaba6 Max Filippov
    case 8: /*a*/
1642 ccfcaba6 Max Filippov
        env->regs[reg->targno & 0x0f] = tmp;
1643 ccfcaba6 Max Filippov
        break;
1644 ccfcaba6 Max Filippov
1645 ccfcaba6 Max Filippov
    default:
1646 ccfcaba6 Max Filippov
        qemu_log("%s to reg %d of unsupported type %d\n",
1647 ccfcaba6 Max Filippov
                __func__, n, reg->type);
1648 ccfcaba6 Max Filippov
        return 0;
1649 ccfcaba6 Max Filippov
    }
1650 ccfcaba6 Max Filippov
1651 ccfcaba6 Max Filippov
    return 4;
1652 ccfcaba6 Max Filippov
}
1653 56aebc89 pbrook
#else
1654 56aebc89 pbrook
1655 56aebc89 pbrook
#define NUM_CORE_REGS 0
1656 56aebc89 pbrook
1657 9349b4f9 Andreas Färber
static int cpu_gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int n)
1658 f1ccf904 ths
{
1659 56aebc89 pbrook
    return 0;
1660 f1ccf904 ths
}
1661 f1ccf904 ths
1662 9349b4f9 Andreas Färber
static int cpu_gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int n)
1663 f1ccf904 ths
{
1664 56aebc89 pbrook
    return 0;
1665 56aebc89 pbrook
}
1666 f1ccf904 ths
1667 56aebc89 pbrook
#endif
1668 f1ccf904 ths
1669 ccfcaba6 Max Filippov
#if !defined(TARGET_XTENSA)
1670 56aebc89 pbrook
static int num_g_regs = NUM_CORE_REGS;
1671 ccfcaba6 Max Filippov
#endif
1672 f1ccf904 ths
1673 56aebc89 pbrook
#ifdef GDB_CORE_XML
1674 56aebc89 pbrook
/* Encode data using the encoding for 'x' packets.  */
1675 56aebc89 pbrook
static int memtox(char *buf, const char *mem, int len)
1676 56aebc89 pbrook
{
1677 56aebc89 pbrook
    char *p = buf;
1678 56aebc89 pbrook
    char c;
1679 56aebc89 pbrook
1680 56aebc89 pbrook
    while (len--) {
1681 56aebc89 pbrook
        c = *(mem++);
1682 56aebc89 pbrook
        switch (c) {
1683 56aebc89 pbrook
        case '#': case '$': case '*': case '}':
1684 56aebc89 pbrook
            *(p++) = '}';
1685 56aebc89 pbrook
            *(p++) = c ^ 0x20;
1686 56aebc89 pbrook
            break;
1687 56aebc89 pbrook
        default:
1688 56aebc89 pbrook
            *(p++) = c;
1689 56aebc89 pbrook
            break;
1690 56aebc89 pbrook
        }
1691 56aebc89 pbrook
    }
1692 56aebc89 pbrook
    return p - buf;
1693 56aebc89 pbrook
}
1694 f1ccf904 ths
1695 3faf778e aurel32
static const char *get_feature_xml(const char *p, const char **newp)
1696 56aebc89 pbrook
{
1697 56aebc89 pbrook
    size_t len;
1698 56aebc89 pbrook
    int i;
1699 56aebc89 pbrook
    const char *name;
1700 56aebc89 pbrook
    static char target_xml[1024];
1701 56aebc89 pbrook
1702 56aebc89 pbrook
    len = 0;
1703 56aebc89 pbrook
    while (p[len] && p[len] != ':')
1704 56aebc89 pbrook
        len++;
1705 56aebc89 pbrook
    *newp = p + len;
1706 56aebc89 pbrook
1707 56aebc89 pbrook
    name = NULL;
1708 56aebc89 pbrook
    if (strncmp(p, "target.xml", len) == 0) {
1709 56aebc89 pbrook
        /* Generate the XML description for this CPU.  */
1710 56aebc89 pbrook
        if (!target_xml[0]) {
1711 56aebc89 pbrook
            GDBRegisterState *r;
1712 56aebc89 pbrook
1713 5b3715bf blueswir1
            snprintf(target_xml, sizeof(target_xml),
1714 5b3715bf blueswir1
                     "<?xml version=\"1.0\"?>"
1715 5b3715bf blueswir1
                     "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1716 5b3715bf blueswir1
                     "<target>"
1717 5b3715bf blueswir1
                     "<xi:include href=\"%s\"/>",
1718 5b3715bf blueswir1
                     GDB_CORE_XML);
1719 56aebc89 pbrook
1720 880a7578 aliguori
            for (r = first_cpu->gdb_regs; r; r = r->next) {
1721 2dc766da blueswir1
                pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
1722 2dc766da blueswir1
                pstrcat(target_xml, sizeof(target_xml), r->xml);
1723 2dc766da blueswir1
                pstrcat(target_xml, sizeof(target_xml), "\"/>");
1724 56aebc89 pbrook
            }
1725 2dc766da blueswir1
            pstrcat(target_xml, sizeof(target_xml), "</target>");
1726 56aebc89 pbrook
        }
1727 56aebc89 pbrook
        return target_xml;
1728 56aebc89 pbrook
    }
1729 56aebc89 pbrook
    for (i = 0; ; i++) {
1730 56aebc89 pbrook
        name = xml_builtin[i][0];
1731 56aebc89 pbrook
        if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
1732 56aebc89 pbrook
            break;
1733 56aebc89 pbrook
    }
1734 56aebc89 pbrook
    return name ? xml_builtin[i][1] : NULL;
1735 56aebc89 pbrook
}
1736 56aebc89 pbrook
#endif
1737 f1ccf904 ths
1738 9349b4f9 Andreas Färber
static int gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int reg)
1739 56aebc89 pbrook
{
1740 56aebc89 pbrook
    GDBRegisterState *r;
1741 f1ccf904 ths
1742 56aebc89 pbrook
    if (reg < NUM_CORE_REGS)
1743 56aebc89 pbrook
        return cpu_gdb_read_register(env, mem_buf, reg);
1744 f1ccf904 ths
1745 56aebc89 pbrook
    for (r = env->gdb_regs; r; r = r->next) {
1746 56aebc89 pbrook
        if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1747 56aebc89 pbrook
            return r->get_reg(env, mem_buf, reg - r->base_reg);
1748 56aebc89 pbrook
        }
1749 56aebc89 pbrook
    }
1750 56aebc89 pbrook
    return 0;
1751 f1ccf904 ths
}
1752 f1ccf904 ths
1753 9349b4f9 Andreas Färber
static int gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int reg)
1754 f1ccf904 ths
{
1755 56aebc89 pbrook
    GDBRegisterState *r;
1756 f1ccf904 ths
1757 56aebc89 pbrook
    if (reg < NUM_CORE_REGS)
1758 56aebc89 pbrook
        return cpu_gdb_write_register(env, mem_buf, reg);
1759 56aebc89 pbrook
1760 56aebc89 pbrook
    for (r = env->gdb_regs; r; r = r->next) {
1761 56aebc89 pbrook
        if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1762 56aebc89 pbrook
            return r->set_reg(env, mem_buf, reg - r->base_reg);
1763 56aebc89 pbrook
        }
1764 56aebc89 pbrook
    }
1765 6da41eaf bellard
    return 0;
1766 6da41eaf bellard
}
1767 6da41eaf bellard
1768 ccfcaba6 Max Filippov
#if !defined(TARGET_XTENSA)
1769 56aebc89 pbrook
/* Register a supplemental set of CPU registers.  If g_pos is nonzero it
1770 56aebc89 pbrook
   specifies the first register number and these registers are included in
1771 56aebc89 pbrook
   a standard "g" packet.  Direction is relative to gdb, i.e. get_reg is
1772 56aebc89 pbrook
   gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1773 56aebc89 pbrook
 */
1774 56aebc89 pbrook
1775 9349b4f9 Andreas Färber
void gdb_register_coprocessor(CPUArchState * env,
1776 56aebc89 pbrook
                             gdb_reg_cb get_reg, gdb_reg_cb set_reg,
1777 56aebc89 pbrook
                             int num_regs, const char *xml, int g_pos)
1778 6da41eaf bellard
{
1779 56aebc89 pbrook
    GDBRegisterState *s;
1780 56aebc89 pbrook
    GDBRegisterState **p;
1781 56aebc89 pbrook
    static int last_reg = NUM_CORE_REGS;
1782 56aebc89 pbrook
1783 56aebc89 pbrook
    p = &env->gdb_regs;
1784 56aebc89 pbrook
    while (*p) {
1785 56aebc89 pbrook
        /* Check for duplicates.  */
1786 56aebc89 pbrook
        if (strcmp((*p)->xml, xml) == 0)
1787 56aebc89 pbrook
            return;
1788 56aebc89 pbrook
        p = &(*p)->next;
1789 56aebc89 pbrook
    }
1790 9643c25f Stefan Weil
1791 9643c25f Stefan Weil
    s = g_new0(GDBRegisterState, 1);
1792 9643c25f Stefan Weil
    s->base_reg = last_reg;
1793 9643c25f Stefan Weil
    s->num_regs = num_regs;
1794 9643c25f Stefan Weil
    s->get_reg = get_reg;
1795 9643c25f Stefan Weil
    s->set_reg = set_reg;
1796 9643c25f Stefan Weil
    s->xml = xml;
1797 9643c25f Stefan Weil
1798 56aebc89 pbrook
    /* Add to end of list.  */
1799 56aebc89 pbrook
    last_reg += num_regs;
1800 56aebc89 pbrook
    *p = s;
1801 56aebc89 pbrook
    if (g_pos) {
1802 56aebc89 pbrook
        if (g_pos != s->base_reg) {
1803 56aebc89 pbrook
            fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
1804 56aebc89 pbrook
                    "Expected %d got %d\n", xml, g_pos, s->base_reg);
1805 56aebc89 pbrook
        } else {
1806 56aebc89 pbrook
            num_g_regs = last_reg;
1807 56aebc89 pbrook
        }
1808 56aebc89 pbrook
    }
1809 6da41eaf bellard
}
1810 ccfcaba6 Max Filippov
#endif
1811 6da41eaf bellard
1812 a1d1bb31 aliguori
#ifndef CONFIG_USER_ONLY
1813 a1d1bb31 aliguori
static const int xlat_gdb_type[] = {
1814 a1d1bb31 aliguori
    [GDB_WATCHPOINT_WRITE]  = BP_GDB | BP_MEM_WRITE,
1815 a1d1bb31 aliguori
    [GDB_WATCHPOINT_READ]   = BP_GDB | BP_MEM_READ,
1816 a1d1bb31 aliguori
    [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
1817 a1d1bb31 aliguori
};
1818 a1d1bb31 aliguori
#endif
1819 a1d1bb31 aliguori
1820 880a7578 aliguori
static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
1821 a1d1bb31 aliguori
{
1822 9349b4f9 Andreas Färber
    CPUArchState *env;
1823 880a7578 aliguori
    int err = 0;
1824 880a7578 aliguori
1825 e22a25c9 aliguori
    if (kvm_enabled())
1826 e22a25c9 aliguori
        return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1827 e22a25c9 aliguori
1828 a1d1bb31 aliguori
    switch (type) {
1829 a1d1bb31 aliguori
    case GDB_BREAKPOINT_SW:
1830 a1d1bb31 aliguori
    case GDB_BREAKPOINT_HW:
1831 880a7578 aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1832 880a7578 aliguori
            err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
1833 880a7578 aliguori
            if (err)
1834 880a7578 aliguori
                break;
1835 880a7578 aliguori
        }
1836 880a7578 aliguori
        return err;
1837 a1d1bb31 aliguori
#ifndef CONFIG_USER_ONLY
1838 a1d1bb31 aliguori
    case GDB_WATCHPOINT_WRITE:
1839 a1d1bb31 aliguori
    case GDB_WATCHPOINT_READ:
1840 a1d1bb31 aliguori
    case GDB_WATCHPOINT_ACCESS:
1841 880a7578 aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1842 880a7578 aliguori
            err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
1843 880a7578 aliguori
                                        NULL);
1844 880a7578 aliguori
            if (err)
1845 880a7578 aliguori
                break;
1846 880a7578 aliguori
        }
1847 880a7578 aliguori
        return err;
1848 a1d1bb31 aliguori
#endif
1849 a1d1bb31 aliguori
    default:
1850 a1d1bb31 aliguori
        return -ENOSYS;
1851 a1d1bb31 aliguori
    }
1852 a1d1bb31 aliguori
}
1853 a1d1bb31 aliguori
1854 880a7578 aliguori
static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
1855 a1d1bb31 aliguori
{
1856 9349b4f9 Andreas Färber
    CPUArchState *env;
1857 880a7578 aliguori
    int err = 0;
1858 880a7578 aliguori
1859 e22a25c9 aliguori
    if (kvm_enabled())
1860 e22a25c9 aliguori
        return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1861 e22a25c9 aliguori
1862 a1d1bb31 aliguori
    switch (type) {
1863 a1d1bb31 aliguori
    case GDB_BREAKPOINT_SW:
1864 a1d1bb31 aliguori
    case GDB_BREAKPOINT_HW:
1865 880a7578 aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1866 880a7578 aliguori
            err = cpu_breakpoint_remove(env, addr, BP_GDB);
1867 880a7578 aliguori
            if (err)
1868 880a7578 aliguori
                break;
1869 880a7578 aliguori
        }
1870 880a7578 aliguori
        return err;
1871 a1d1bb31 aliguori
#ifndef CONFIG_USER_ONLY
1872 a1d1bb31 aliguori
    case GDB_WATCHPOINT_WRITE:
1873 a1d1bb31 aliguori
    case GDB_WATCHPOINT_READ:
1874 a1d1bb31 aliguori
    case GDB_WATCHPOINT_ACCESS:
1875 880a7578 aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1876 880a7578 aliguori
            err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
1877 880a7578 aliguori
            if (err)
1878 880a7578 aliguori
                break;
1879 880a7578 aliguori
        }
1880 880a7578 aliguori
        return err;
1881 a1d1bb31 aliguori
#endif
1882 a1d1bb31 aliguori
    default:
1883 a1d1bb31 aliguori
        return -ENOSYS;
1884 a1d1bb31 aliguori
    }
1885 a1d1bb31 aliguori
}
1886 a1d1bb31 aliguori
1887 880a7578 aliguori
static void gdb_breakpoint_remove_all(void)
1888 a1d1bb31 aliguori
{
1889 9349b4f9 Andreas Färber
    CPUArchState *env;
1890 880a7578 aliguori
1891 e22a25c9 aliguori
    if (kvm_enabled()) {
1892 e22a25c9 aliguori
        kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
1893 e22a25c9 aliguori
        return;
1894 e22a25c9 aliguori
    }
1895 e22a25c9 aliguori
1896 880a7578 aliguori
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
1897 880a7578 aliguori
        cpu_breakpoint_remove_all(env, BP_GDB);
1898 a1d1bb31 aliguori
#ifndef CONFIG_USER_ONLY
1899 880a7578 aliguori
        cpu_watchpoint_remove_all(env, BP_GDB);
1900 a1d1bb31 aliguori
#endif
1901 880a7578 aliguori
    }
1902 a1d1bb31 aliguori
}
1903 a1d1bb31 aliguori
1904 fab9d284 aurel32
static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1905 fab9d284 aurel32
{
1906 4c0960c0 Avi Kivity
    cpu_synchronize_state(s->c_cpu);
1907 a896d03b Peter Maydell
#if defined(TARGET_I386)
1908 fab9d284 aurel32
    s->c_cpu->eip = pc;
1909 fab9d284 aurel32
#elif defined (TARGET_PPC)
1910 fab9d284 aurel32
    s->c_cpu->nip = pc;
1911 fab9d284 aurel32
#elif defined (TARGET_SPARC)
1912 fab9d284 aurel32
    s->c_cpu->pc = pc;
1913 fab9d284 aurel32
    s->c_cpu->npc = pc + 4;
1914 fab9d284 aurel32
#elif defined (TARGET_ARM)
1915 fab9d284 aurel32
    s->c_cpu->regs[15] = pc;
1916 fab9d284 aurel32
#elif defined (TARGET_SH4)
1917 fab9d284 aurel32
    s->c_cpu->pc = pc;
1918 fab9d284 aurel32
#elif defined (TARGET_MIPS)
1919 ff1d1977 Nathan Froyd
    s->c_cpu->active_tc.PC = pc & ~(target_ulong)1;
1920 ff1d1977 Nathan Froyd
    if (pc & 1) {
1921 ff1d1977 Nathan Froyd
        s->c_cpu->hflags |= MIPS_HFLAG_M16;
1922 ff1d1977 Nathan Froyd
    } else {
1923 ff1d1977 Nathan Froyd
        s->c_cpu->hflags &= ~(MIPS_HFLAG_M16);
1924 ff1d1977 Nathan Froyd
    }
1925 d74d6a99 Edgar E. Iglesias
#elif defined (TARGET_MICROBLAZE)
1926 d74d6a99 Edgar E. Iglesias
    s->c_cpu->sregs[SR_PC] = pc;
1927 fab9d284 aurel32
#elif defined (TARGET_CRIS)
1928 fab9d284 aurel32
    s->c_cpu->pc = pc;
1929 fab9d284 aurel32
#elif defined (TARGET_ALPHA)
1930 fab9d284 aurel32
    s->c_cpu->pc = pc;
1931 afcb0e45 Alexander Graf
#elif defined (TARGET_S390X)
1932 afcb0e45 Alexander Graf
    s->c_cpu->psw.addr = pc;
1933 0c45d3d4 Michael Walle
#elif defined (TARGET_LM32)
1934 0c45d3d4 Michael Walle
    s->c_cpu->pc = pc;
1935 ccfcaba6 Max Filippov
#elif defined(TARGET_XTENSA)
1936 ccfcaba6 Max Filippov
    s->c_cpu->pc = pc;
1937 fab9d284 aurel32
#endif
1938 fab9d284 aurel32
}
1939 fab9d284 aurel32
1940 9349b4f9 Andreas Färber
static CPUArchState *find_cpu(uint32_t thread_id)
1941 1e9fa730 Nathan Froyd
{
1942 9349b4f9 Andreas Färber
    CPUArchState *env;
1943 1e9fa730 Nathan Froyd
1944 1e9fa730 Nathan Froyd
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
1945 68f4730c Wen Congyang
        if (cpu_index(env) == thread_id) {
1946 1e9fa730 Nathan Froyd
            return env;
1947 1e9fa730 Nathan Froyd
        }
1948 1e9fa730 Nathan Froyd
    }
1949 1e9fa730 Nathan Froyd
1950 1e9fa730 Nathan Froyd
    return NULL;
1951 1e9fa730 Nathan Froyd
}
1952 1e9fa730 Nathan Froyd
1953 880a7578 aliguori
static int gdb_handle_packet(GDBState *s, const char *line_buf)
1954 b4608c04 bellard
{
1955 9349b4f9 Andreas Färber
    CPUArchState *env;
1956 b4608c04 bellard
    const char *p;
1957 1e9fa730 Nathan Froyd
    uint32_t thread;
1958 1e9fa730 Nathan Froyd
    int ch, reg_size, type, res;
1959 56aebc89 pbrook
    char buf[MAX_PACKET_LENGTH];
1960 56aebc89 pbrook
    uint8_t mem_buf[MAX_PACKET_LENGTH];
1961 56aebc89 pbrook
    uint8_t *registers;
1962 9d9754a3 bellard
    target_ulong addr, len;
1963 3b46e624 ths
1964 858693c6 bellard
#ifdef DEBUG_GDB
1965 858693c6 bellard
    printf("command='%s'\n", line_buf);
1966 858693c6 bellard
#endif
1967 858693c6 bellard
    p = line_buf;
1968 858693c6 bellard
    ch = *p++;
1969 858693c6 bellard
    switch(ch) {
1970 858693c6 bellard
    case '?':
1971 1fddef4b bellard
        /* TODO: Make this return the correct value for user-mode.  */
1972 ca587a8e aurel32
        snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
1973 68f4730c Wen Congyang
                 cpu_index(s->c_cpu));
1974 858693c6 bellard
        put_packet(s, buf);
1975 7d03f82f edgar_igl
        /* Remove all the breakpoints when this query is issued,
1976 7d03f82f edgar_igl
         * because gdb is doing and initial connect and the state
1977 7d03f82f edgar_igl
         * should be cleaned up.
1978 7d03f82f edgar_igl
         */
1979 880a7578 aliguori
        gdb_breakpoint_remove_all();
1980 858693c6 bellard
        break;
1981 858693c6 bellard
    case 'c':
1982 858693c6 bellard
        if (*p != '\0') {
1983 9d9754a3 bellard
            addr = strtoull(p, (char **)&p, 16);
1984 fab9d284 aurel32
            gdb_set_cpu_pc(s, addr);
1985 858693c6 bellard
        }
1986 ca587a8e aurel32
        s->signal = 0;
1987 ba70a624 edgar_igl
        gdb_continue(s);
1988 41625033 bellard
        return RS_IDLE;
1989 1f487ee9 edgar_igl
    case 'C':
1990 ca587a8e aurel32
        s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1991 ca587a8e aurel32
        if (s->signal == -1)
1992 ca587a8e aurel32
            s->signal = 0;
1993 1f487ee9 edgar_igl
        gdb_continue(s);
1994 1f487ee9 edgar_igl
        return RS_IDLE;
1995 dd32aa10 Jan Kiszka
    case 'v':
1996 dd32aa10 Jan Kiszka
        if (strncmp(p, "Cont", 4) == 0) {
1997 dd32aa10 Jan Kiszka
            int res_signal, res_thread;
1998 dd32aa10 Jan Kiszka
1999 dd32aa10 Jan Kiszka
            p += 4;
2000 dd32aa10 Jan Kiszka
            if (*p == '?') {
2001 dd32aa10 Jan Kiszka
                put_packet(s, "vCont;c;C;s;S");
2002 dd32aa10 Jan Kiszka
                break;
2003 dd32aa10 Jan Kiszka
            }
2004 dd32aa10 Jan Kiszka
            res = 0;
2005 dd32aa10 Jan Kiszka
            res_signal = 0;
2006 dd32aa10 Jan Kiszka
            res_thread = 0;
2007 dd32aa10 Jan Kiszka
            while (*p) {
2008 dd32aa10 Jan Kiszka
                int action, signal;
2009 dd32aa10 Jan Kiszka
2010 dd32aa10 Jan Kiszka
                if (*p++ != ';') {
2011 dd32aa10 Jan Kiszka
                    res = 0;
2012 dd32aa10 Jan Kiszka
                    break;
2013 dd32aa10 Jan Kiszka
                }
2014 dd32aa10 Jan Kiszka
                action = *p++;
2015 dd32aa10 Jan Kiszka
                signal = 0;
2016 dd32aa10 Jan Kiszka
                if (action == 'C' || action == 'S') {
2017 dd32aa10 Jan Kiszka
                    signal = strtoul(p, (char **)&p, 16);
2018 dd32aa10 Jan Kiszka
                } else if (action != 'c' && action != 's') {
2019 dd32aa10 Jan Kiszka
                    res = 0;
2020 dd32aa10 Jan Kiszka
                    break;
2021 dd32aa10 Jan Kiszka
                }
2022 dd32aa10 Jan Kiszka
                thread = 0;
2023 dd32aa10 Jan Kiszka
                if (*p == ':') {
2024 dd32aa10 Jan Kiszka
                    thread = strtoull(p+1, (char **)&p, 16);
2025 dd32aa10 Jan Kiszka
                }
2026 dd32aa10 Jan Kiszka
                action = tolower(action);
2027 dd32aa10 Jan Kiszka
                if (res == 0 || (res == 'c' && action == 's')) {
2028 dd32aa10 Jan Kiszka
                    res = action;
2029 dd32aa10 Jan Kiszka
                    res_signal = signal;
2030 dd32aa10 Jan Kiszka
                    res_thread = thread;
2031 dd32aa10 Jan Kiszka
                }
2032 dd32aa10 Jan Kiszka
            }
2033 dd32aa10 Jan Kiszka
            if (res) {
2034 dd32aa10 Jan Kiszka
                if (res_thread != -1 && res_thread != 0) {
2035 dd32aa10 Jan Kiszka
                    env = find_cpu(res_thread);
2036 dd32aa10 Jan Kiszka
                    if (env == NULL) {
2037 dd32aa10 Jan Kiszka
                        put_packet(s, "E22");
2038 dd32aa10 Jan Kiszka
                        break;
2039 dd32aa10 Jan Kiszka
                    }
2040 dd32aa10 Jan Kiszka
                    s->c_cpu = env;
2041 dd32aa10 Jan Kiszka
                }
2042 dd32aa10 Jan Kiszka
                if (res == 's') {
2043 dd32aa10 Jan Kiszka
                    cpu_single_step(s->c_cpu, sstep_flags);
2044 dd32aa10 Jan Kiszka
                }
2045 dd32aa10 Jan Kiszka
                s->signal = res_signal;
2046 dd32aa10 Jan Kiszka
                gdb_continue(s);
2047 dd32aa10 Jan Kiszka
                return RS_IDLE;
2048 dd32aa10 Jan Kiszka
            }
2049 dd32aa10 Jan Kiszka
            break;
2050 dd32aa10 Jan Kiszka
        } else {
2051 dd32aa10 Jan Kiszka
            goto unknown_command;
2052 dd32aa10 Jan Kiszka
        }
2053 7d03f82f edgar_igl
    case 'k':
2054 00e94dbc Jan Kiszka
#ifdef CONFIG_USER_ONLY
2055 7d03f82f edgar_igl
        /* Kill the target */
2056 7d03f82f edgar_igl
        fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
2057 7d03f82f edgar_igl
        exit(0);
2058 00e94dbc Jan Kiszka
#endif
2059 7d03f82f edgar_igl
    case 'D':
2060 7d03f82f edgar_igl
        /* Detach packet */
2061 880a7578 aliguori
        gdb_breakpoint_remove_all();
2062 7ea06da3 Daniel Gutson
        gdb_syscall_mode = GDB_SYS_DISABLED;
2063 7d03f82f edgar_igl
        gdb_continue(s);
2064 7d03f82f edgar_igl
        put_packet(s, "OK");
2065 7d03f82f edgar_igl
        break;
2066 858693c6 bellard
    case 's':
2067 858693c6 bellard
        if (*p != '\0') {
2068 8fac5803 ths
            addr = strtoull(p, (char **)&p, 16);
2069 fab9d284 aurel32
            gdb_set_cpu_pc(s, addr);
2070 858693c6 bellard
        }
2071 880a7578 aliguori
        cpu_single_step(s->c_cpu, sstep_flags);
2072 ba70a624 edgar_igl
        gdb_continue(s);
2073 41625033 bellard
        return RS_IDLE;
2074 a2d1ebaf pbrook
    case 'F':
2075 a2d1ebaf pbrook
        {
2076 a2d1ebaf pbrook
            target_ulong ret;
2077 a2d1ebaf pbrook
            target_ulong err;
2078 a2d1ebaf pbrook
2079 a2d1ebaf pbrook
            ret = strtoull(p, (char **)&p, 16);
2080 a2d1ebaf pbrook
            if (*p == ',') {
2081 a2d1ebaf pbrook
                p++;
2082 a2d1ebaf pbrook
                err = strtoull(p, (char **)&p, 16);
2083 a2d1ebaf pbrook
            } else {
2084 a2d1ebaf pbrook
                err = 0;
2085 a2d1ebaf pbrook
            }
2086 a2d1ebaf pbrook
            if (*p == ',')
2087 a2d1ebaf pbrook
                p++;
2088 a2d1ebaf pbrook
            type = *p;
2089 cdb432b2 Meador Inge
            if (s->current_syscall_cb) {
2090 cdb432b2 Meador Inge
                s->current_syscall_cb(s->c_cpu, ret, err);
2091 cdb432b2 Meador Inge
                s->current_syscall_cb = NULL;
2092 cdb432b2 Meador Inge
            }
2093 a2d1ebaf pbrook
            if (type == 'C') {
2094 a2d1ebaf pbrook
                put_packet(s, "T02");
2095 a2d1ebaf pbrook
            } else {
2096 ba70a624 edgar_igl
                gdb_continue(s);
2097 a2d1ebaf pbrook
            }
2098 a2d1ebaf pbrook
        }
2099 a2d1ebaf pbrook
        break;
2100 858693c6 bellard
    case 'g':
2101 4c0960c0 Avi Kivity
        cpu_synchronize_state(s->g_cpu);
2102 ccfcaba6 Max Filippov
        env = s->g_cpu;
2103 56aebc89 pbrook
        len = 0;
2104 56aebc89 pbrook
        for (addr = 0; addr < num_g_regs; addr++) {
2105 880a7578 aliguori
            reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
2106 56aebc89 pbrook
            len += reg_size;
2107 56aebc89 pbrook
        }
2108 56aebc89 pbrook
        memtohex(buf, mem_buf, len);
2109 858693c6 bellard
        put_packet(s, buf);
2110 858693c6 bellard
        break;
2111 858693c6 bellard
    case 'G':
2112 4c0960c0 Avi Kivity
        cpu_synchronize_state(s->g_cpu);
2113 ccfcaba6 Max Filippov
        env = s->g_cpu;
2114 56aebc89 pbrook
        registers = mem_buf;
2115 858693c6 bellard
        len = strlen(p) / 2;
2116 858693c6 bellard
        hextomem((uint8_t *)registers, p, len);
2117 56aebc89 pbrook
        for (addr = 0; addr < num_g_regs && len > 0; addr++) {
2118 880a7578 aliguori
            reg_size = gdb_write_register(s->g_cpu, registers, addr);
2119 56aebc89 pbrook
            len -= reg_size;
2120 56aebc89 pbrook
            registers += reg_size;
2121 56aebc89 pbrook
        }
2122 858693c6 bellard
        put_packet(s, "OK");
2123 858693c6 bellard
        break;
2124 858693c6 bellard
    case 'm':
2125 9d9754a3 bellard
        addr = strtoull(p, (char **)&p, 16);
2126 858693c6 bellard
        if (*p == ',')
2127 858693c6 bellard
            p++;
2128 9d9754a3 bellard
        len = strtoull(p, NULL, 16);
2129 44520db1 Fabien Chouteau
        if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 0) != 0) {
2130 6f970bd9 bellard
            put_packet (s, "E14");
2131 6f970bd9 bellard
        } else {
2132 6f970bd9 bellard
            memtohex(buf, mem_buf, len);
2133 6f970bd9 bellard
            put_packet(s, buf);
2134 6f970bd9 bellard
        }
2135 858693c6 bellard
        break;
2136 858693c6 bellard
    case 'M':
2137 9d9754a3 bellard
        addr = strtoull(p, (char **)&p, 16);
2138 858693c6 bellard
        if (*p == ',')
2139 858693c6 bellard
            p++;
2140 9d9754a3 bellard
        len = strtoull(p, (char **)&p, 16);
2141 b328f873 bellard
        if (*p == ':')
2142 858693c6 bellard
            p++;
2143 858693c6 bellard
        hextomem(mem_buf, p, len);
2144 44520db1 Fabien Chouteau
        if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 1) != 0) {
2145 905f20b1 bellard
            put_packet(s, "E14");
2146 44520db1 Fabien Chouteau
        } else {
2147 858693c6 bellard
            put_packet(s, "OK");
2148 44520db1 Fabien Chouteau
        }
2149 858693c6 bellard
        break;
2150 56aebc89 pbrook
    case 'p':
2151 56aebc89 pbrook
        /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
2152 56aebc89 pbrook
           This works, but can be very slow.  Anything new enough to
2153 56aebc89 pbrook
           understand XML also knows how to use this properly.  */
2154 56aebc89 pbrook
        if (!gdb_has_xml)
2155 56aebc89 pbrook
            goto unknown_command;
2156 56aebc89 pbrook
        addr = strtoull(p, (char **)&p, 16);
2157 880a7578 aliguori
        reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
2158 56aebc89 pbrook
        if (reg_size) {
2159 56aebc89 pbrook
            memtohex(buf, mem_buf, reg_size);
2160 56aebc89 pbrook
            put_packet(s, buf);
2161 56aebc89 pbrook
        } else {
2162 56aebc89 pbrook
            put_packet(s, "E14");
2163 56aebc89 pbrook
        }
2164 56aebc89 pbrook
        break;
2165 56aebc89 pbrook
    case 'P':
2166 56aebc89 pbrook
        if (!gdb_has_xml)
2167 56aebc89 pbrook
            goto unknown_command;
2168 56aebc89 pbrook
        addr = strtoull(p, (char **)&p, 16);
2169 56aebc89 pbrook
        if (*p == '=')
2170 56aebc89 pbrook
            p++;
2171 56aebc89 pbrook
        reg_size = strlen(p) / 2;
2172 56aebc89 pbrook
        hextomem(mem_buf, p, reg_size);
2173 880a7578 aliguori
        gdb_write_register(s->g_cpu, mem_buf, addr);
2174 56aebc89 pbrook
        put_packet(s, "OK");
2175 56aebc89 pbrook
        break;
2176 858693c6 bellard
    case 'Z':
2177 858693c6 bellard
    case 'z':
2178 858693c6 bellard
        type = strtoul(p, (char **)&p, 16);
2179 858693c6 bellard
        if (*p == ',')
2180 858693c6 bellard
            p++;
2181 9d9754a3 bellard
        addr = strtoull(p, (char **)&p, 16);
2182 858693c6 bellard
        if (*p == ',')
2183 858693c6 bellard
            p++;
2184 9d9754a3 bellard
        len = strtoull(p, (char **)&p, 16);
2185 a1d1bb31 aliguori
        if (ch == 'Z')
2186 880a7578 aliguori
            res = gdb_breakpoint_insert(addr, len, type);
2187 a1d1bb31 aliguori
        else
2188 880a7578 aliguori
            res = gdb_breakpoint_remove(addr, len, type);
2189 a1d1bb31 aliguori
        if (res >= 0)
2190 a1d1bb31 aliguori
             put_packet(s, "OK");
2191 a1d1bb31 aliguori
        else if (res == -ENOSYS)
2192 0f459d16 pbrook
            put_packet(s, "");
2193 a1d1bb31 aliguori
        else
2194 a1d1bb31 aliguori
            put_packet(s, "E22");
2195 858693c6 bellard
        break;
2196 880a7578 aliguori
    case 'H':
2197 880a7578 aliguori
        type = *p++;
2198 880a7578 aliguori
        thread = strtoull(p, (char **)&p, 16);
2199 880a7578 aliguori
        if (thread == -1 || thread == 0) {
2200 880a7578 aliguori
            put_packet(s, "OK");
2201 880a7578 aliguori
            break;
2202 880a7578 aliguori
        }
2203 1e9fa730 Nathan Froyd
        env = find_cpu(thread);
2204 880a7578 aliguori
        if (env == NULL) {
2205 880a7578 aliguori
            put_packet(s, "E22");
2206 880a7578 aliguori
            break;
2207 880a7578 aliguori
        }
2208 880a7578 aliguori
        switch (type) {
2209 880a7578 aliguori
        case 'c':
2210 880a7578 aliguori
            s->c_cpu = env;
2211 880a7578 aliguori
            put_packet(s, "OK");
2212 880a7578 aliguori
            break;
2213 880a7578 aliguori
        case 'g':
2214 880a7578 aliguori
            s->g_cpu = env;
2215 880a7578 aliguori
            put_packet(s, "OK");
2216 880a7578 aliguori
            break;
2217 880a7578 aliguori
        default:
2218 880a7578 aliguori
             put_packet(s, "E22");
2219 880a7578 aliguori
             break;
2220 880a7578 aliguori
        }
2221 880a7578 aliguori
        break;
2222 880a7578 aliguori
    case 'T':
2223 880a7578 aliguori
        thread = strtoull(p, (char **)&p, 16);
2224 1e9fa730 Nathan Froyd
        env = find_cpu(thread);
2225 1e9fa730 Nathan Froyd
2226 1e9fa730 Nathan Froyd
        if (env != NULL) {
2227 1e9fa730 Nathan Froyd
            put_packet(s, "OK");
2228 1e9fa730 Nathan Froyd
        } else {
2229 880a7578 aliguori
            put_packet(s, "E22");
2230 1e9fa730 Nathan Froyd
        }
2231 880a7578 aliguori
        break;
2232 978efd6a pbrook
    case 'q':
2233 60897d36 edgar_igl
    case 'Q':
2234 60897d36 edgar_igl
        /* parse any 'q' packets here */
2235 60897d36 edgar_igl
        if (!strcmp(p,"qemu.sstepbits")) {
2236 60897d36 edgar_igl
            /* Query Breakpoint bit definitions */
2237 363a37d5 blueswir1
            snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
2238 363a37d5 blueswir1
                     SSTEP_ENABLE,
2239 363a37d5 blueswir1
                     SSTEP_NOIRQ,
2240 363a37d5 blueswir1
                     SSTEP_NOTIMER);
2241 60897d36 edgar_igl
            put_packet(s, buf);
2242 60897d36 edgar_igl
            break;
2243 60897d36 edgar_igl
        } else if (strncmp(p,"qemu.sstep",10) == 0) {
2244 60897d36 edgar_igl
            /* Display or change the sstep_flags */
2245 60897d36 edgar_igl
            p += 10;
2246 60897d36 edgar_igl
            if (*p != '=') {
2247 60897d36 edgar_igl
                /* Display current setting */
2248 363a37d5 blueswir1
                snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
2249 60897d36 edgar_igl
                put_packet(s, buf);
2250 60897d36 edgar_igl
                break;
2251 60897d36 edgar_igl
            }
2252 60897d36 edgar_igl
            p++;
2253 60897d36 edgar_igl
            type = strtoul(p, (char **)&p, 16);
2254 60897d36 edgar_igl
            sstep_flags = type;
2255 60897d36 edgar_igl
            put_packet(s, "OK");
2256 60897d36 edgar_igl
            break;
2257 880a7578 aliguori
        } else if (strcmp(p,"C") == 0) {
2258 880a7578 aliguori
            /* "Current thread" remains vague in the spec, so always return
2259 880a7578 aliguori
             *  the first CPU (gdb returns the first thread). */
2260 880a7578 aliguori
            put_packet(s, "QC1");
2261 880a7578 aliguori
            break;
2262 880a7578 aliguori
        } else if (strcmp(p,"fThreadInfo") == 0) {
2263 880a7578 aliguori
            s->query_cpu = first_cpu;
2264 880a7578 aliguori
            goto report_cpuinfo;
2265 880a7578 aliguori
        } else if (strcmp(p,"sThreadInfo") == 0) {
2266 880a7578 aliguori
        report_cpuinfo:
2267 880a7578 aliguori
            if (s->query_cpu) {
2268 68f4730c Wen Congyang
                snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
2269 880a7578 aliguori
                put_packet(s, buf);
2270 880a7578 aliguori
                s->query_cpu = s->query_cpu->next_cpu;
2271 880a7578 aliguori
            } else
2272 880a7578 aliguori
                put_packet(s, "l");
2273 880a7578 aliguori
            break;
2274 880a7578 aliguori
        } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
2275 880a7578 aliguori
            thread = strtoull(p+16, (char **)&p, 16);
2276 1e9fa730 Nathan Froyd
            env = find_cpu(thread);
2277 1e9fa730 Nathan Froyd
            if (env != NULL) {
2278 4c0960c0 Avi Kivity
                cpu_synchronize_state(env);
2279 1e9fa730 Nathan Froyd
                len = snprintf((char *)mem_buf, sizeof(mem_buf),
2280 1e9fa730 Nathan Froyd
                               "CPU#%d [%s]", env->cpu_index,
2281 1e9fa730 Nathan Froyd
                               env->halted ? "halted " : "running");
2282 1e9fa730 Nathan Froyd
                memtohex(buf, mem_buf, len);
2283 1e9fa730 Nathan Froyd
                put_packet(s, buf);
2284 1e9fa730 Nathan Froyd
            }
2285 880a7578 aliguori
            break;
2286 60897d36 edgar_igl
        }
2287 0b8a988c blueswir1
#ifdef CONFIG_USER_ONLY
2288 60897d36 edgar_igl
        else if (strncmp(p, "Offsets", 7) == 0) {
2289 880a7578 aliguori
            TaskState *ts = s->c_cpu->opaque;
2290 978efd6a pbrook
2291 363a37d5 blueswir1
            snprintf(buf, sizeof(buf),
2292 363a37d5 blueswir1
                     "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
2293 363a37d5 blueswir1
                     ";Bss=" TARGET_ABI_FMT_lx,
2294 363a37d5 blueswir1
                     ts->info->code_offset,
2295 363a37d5 blueswir1
                     ts->info->data_offset,
2296 363a37d5 blueswir1
                     ts->info->data_offset);
2297 978efd6a pbrook
            put_packet(s, buf);
2298 978efd6a pbrook
            break;
2299 978efd6a pbrook
        }
2300 0b8a988c blueswir1
#else /* !CONFIG_USER_ONLY */
2301 8a34a0fb aliguori
        else if (strncmp(p, "Rcmd,", 5) == 0) {
2302 8a34a0fb aliguori
            int len = strlen(p + 5);
2303 8a34a0fb aliguori
2304 8a34a0fb aliguori
            if ((len % 2) != 0) {
2305 8a34a0fb aliguori
                put_packet(s, "E01");
2306 8a34a0fb aliguori
                break;
2307 8a34a0fb aliguori
            }
2308 8a34a0fb aliguori
            hextomem(mem_buf, p + 5, len);
2309 8a34a0fb aliguori
            len = len / 2;
2310 8a34a0fb aliguori
            mem_buf[len++] = 0;
2311 fa5efccb Anthony Liguori
            qemu_chr_be_write(s->mon_chr, mem_buf, len);
2312 8a34a0fb aliguori
            put_packet(s, "OK");
2313 8a34a0fb aliguori
            break;
2314 8a34a0fb aliguori
        }
2315 0b8a988c blueswir1
#endif /* !CONFIG_USER_ONLY */
2316 56aebc89 pbrook
        if (strncmp(p, "Supported", 9) == 0) {
2317 5b3715bf blueswir1
            snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
2318 56aebc89 pbrook
#ifdef GDB_CORE_XML
2319 2dc766da blueswir1
            pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
2320 56aebc89 pbrook
#endif
2321 56aebc89 pbrook
            put_packet(s, buf);
2322 56aebc89 pbrook
            break;
2323 56aebc89 pbrook
        }
2324 56aebc89 pbrook
#ifdef GDB_CORE_XML
2325 56aebc89 pbrook
        if (strncmp(p, "Xfer:features:read:", 19) == 0) {
2326 56aebc89 pbrook
            const char *xml;
2327 56aebc89 pbrook
            target_ulong total_len;
2328 56aebc89 pbrook
2329 56aebc89 pbrook
            gdb_has_xml = 1;
2330 56aebc89 pbrook
            p += 19;
2331 880a7578 aliguori
            xml = get_feature_xml(p, &p);
2332 56aebc89 pbrook
            if (!xml) {
2333 5b3715bf blueswir1
                snprintf(buf, sizeof(buf), "E00");
2334 56aebc89 pbrook
                put_packet(s, buf);
2335 56aebc89 pbrook
                break;
2336 56aebc89 pbrook
            }
2337 56aebc89 pbrook
2338 56aebc89 pbrook
            if (*p == ':')
2339 56aebc89 pbrook
                p++;
2340 56aebc89 pbrook
            addr = strtoul(p, (char **)&p, 16);
2341 56aebc89 pbrook
            if (*p == ',')
2342 56aebc89 pbrook
                p++;
2343 56aebc89 pbrook
            len = strtoul(p, (char **)&p, 16);
2344 56aebc89 pbrook
2345 56aebc89 pbrook
            total_len = strlen(xml);
2346 56aebc89 pbrook
            if (addr > total_len) {
2347 5b3715bf blueswir1
                snprintf(buf, sizeof(buf), "E00");
2348 56aebc89 pbrook
                put_packet(s, buf);
2349 56aebc89 pbrook
                break;
2350 56aebc89 pbrook
            }
2351 56aebc89 pbrook
            if (len > (MAX_PACKET_LENGTH - 5) / 2)
2352 56aebc89 pbrook
                len = (MAX_PACKET_LENGTH - 5) / 2;
2353 56aebc89 pbrook
            if (len < total_len - addr) {
2354 56aebc89 pbrook
                buf[0] = 'm';
2355 56aebc89 pbrook
                len = memtox(buf + 1, xml + addr, len);
2356 56aebc89 pbrook
            } else {
2357 56aebc89 pbrook
                buf[0] = 'l';
2358 56aebc89 pbrook
                len = memtox(buf + 1, xml + addr, total_len - addr);
2359 56aebc89 pbrook
            }
2360 56aebc89 pbrook
            put_packet_binary(s, buf, len + 1);
2361 56aebc89 pbrook
            break;
2362 56aebc89 pbrook
        }
2363 56aebc89 pbrook
#endif
2364 56aebc89 pbrook
        /* Unrecognised 'q' command.  */
2365 56aebc89 pbrook
        goto unknown_command;
2366 56aebc89 pbrook
2367 858693c6 bellard
    default:
2368 56aebc89 pbrook
    unknown_command:
2369 858693c6 bellard
        /* put empty packet */
2370 858693c6 bellard
        buf[0] = '\0';
2371 858693c6 bellard
        put_packet(s, buf);
2372 858693c6 bellard
        break;
2373 858693c6 bellard
    }
2374 858693c6 bellard
    return RS_IDLE;
2375 858693c6 bellard
}
2376 858693c6 bellard
2377 9349b4f9 Andreas Färber
void gdb_set_stop_cpu(CPUArchState *env)
2378 880a7578 aliguori
{
2379 880a7578 aliguori
    gdbserver_state->c_cpu = env;
2380 880a7578 aliguori
    gdbserver_state->g_cpu = env;
2381 880a7578 aliguori
}
2382 880a7578 aliguori
2383 1fddef4b bellard
#ifndef CONFIG_USER_ONLY
2384 1dfb4dd9 Luiz Capitulino
static void gdb_vm_state_change(void *opaque, int running, RunState state)
2385 858693c6 bellard
{
2386 880a7578 aliguori
    GDBState *s = gdbserver_state;
2387 9349b4f9 Andreas Färber
    CPUArchState *env = s->c_cpu;
2388 858693c6 bellard
    char buf[256];
2389 d6fc1b39 aliguori
    const char *type;
2390 858693c6 bellard
    int ret;
2391 858693c6 bellard
2392 cdb432b2 Meador Inge
    if (running || s->state == RS_INACTIVE) {
2393 cdb432b2 Meador Inge
        return;
2394 cdb432b2 Meador Inge
    }
2395 cdb432b2 Meador Inge
    /* Is there a GDB syscall waiting to be sent?  */
2396 cdb432b2 Meador Inge
    if (s->current_syscall_cb) {
2397 cdb432b2 Meador Inge
        put_packet(s, s->syscall_buf);
2398 a2d1ebaf pbrook
        return;
2399 e07bbac5 Jan Kiszka
    }
2400 1dfb4dd9 Luiz Capitulino
    switch (state) {
2401 0461d5a6 Luiz Capitulino
    case RUN_STATE_DEBUG:
2402 880a7578 aliguori
        if (env->watchpoint_hit) {
2403 880a7578 aliguori
            switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
2404 a1d1bb31 aliguori
            case BP_MEM_READ:
2405 d6fc1b39 aliguori
                type = "r";
2406 d6fc1b39 aliguori
                break;
2407 a1d1bb31 aliguori
            case BP_MEM_ACCESS:
2408 d6fc1b39 aliguori
                type = "a";
2409 d6fc1b39 aliguori
                break;
2410 d6fc1b39 aliguori
            default:
2411 d6fc1b39 aliguori
                type = "";
2412 d6fc1b39 aliguori
                break;
2413 d6fc1b39 aliguori
            }
2414 880a7578 aliguori
            snprintf(buf, sizeof(buf),
2415 880a7578 aliguori
                     "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
2416 68f4730c Wen Congyang
                     GDB_SIGNAL_TRAP, cpu_index(env), type,
2417 880a7578 aliguori
                     env->watchpoint_hit->vaddr);
2418 880a7578 aliguori
            env->watchpoint_hit = NULL;
2419 425189a8 Jan Kiszka
            goto send_packet;
2420 6658ffb8 pbrook
        }
2421 425189a8 Jan Kiszka
        tb_flush(env);
2422 ca587a8e aurel32
        ret = GDB_SIGNAL_TRAP;
2423 425189a8 Jan Kiszka
        break;
2424 0461d5a6 Luiz Capitulino
    case RUN_STATE_PAUSED:
2425 9781e040 aliguori
        ret = GDB_SIGNAL_INT;
2426 425189a8 Jan Kiszka
        break;
2427 0461d5a6 Luiz Capitulino
    case RUN_STATE_SHUTDOWN:
2428 425189a8 Jan Kiszka
        ret = GDB_SIGNAL_QUIT;
2429 425189a8 Jan Kiszka
        break;
2430 0461d5a6 Luiz Capitulino
    case RUN_STATE_IO_ERROR:
2431 425189a8 Jan Kiszka
        ret = GDB_SIGNAL_IO;
2432 425189a8 Jan Kiszka
        break;
2433 0461d5a6 Luiz Capitulino
    case RUN_STATE_WATCHDOG:
2434 425189a8 Jan Kiszka
        ret = GDB_SIGNAL_ALRM;
2435 425189a8 Jan Kiszka
        break;
2436 0461d5a6 Luiz Capitulino
    case RUN_STATE_INTERNAL_ERROR:
2437 425189a8 Jan Kiszka
        ret = GDB_SIGNAL_ABRT;
2438 425189a8 Jan Kiszka
        break;
2439 0461d5a6 Luiz Capitulino
    case RUN_STATE_SAVE_VM:
2440 0461d5a6 Luiz Capitulino
    case RUN_STATE_RESTORE_VM:
2441 425189a8 Jan Kiszka
        return;
2442 0461d5a6 Luiz Capitulino
    case RUN_STATE_FINISH_MIGRATE:
2443 425189a8 Jan Kiszka
        ret = GDB_SIGNAL_XCPU;
2444 425189a8 Jan Kiszka
        break;
2445 425189a8 Jan Kiszka
    default:
2446 425189a8 Jan Kiszka
        ret = GDB_SIGNAL_UNKNOWN;
2447 425189a8 Jan Kiszka
        break;
2448 bbeb7b5c bellard
    }
2449 68f4730c Wen Congyang
    snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(env));
2450 425189a8 Jan Kiszka
2451 425189a8 Jan Kiszka
send_packet:
2452 858693c6 bellard
    put_packet(s, buf);
2453 425189a8 Jan Kiszka
2454 425189a8 Jan Kiszka
    /* disable single step if it was enabled */
2455 425189a8 Jan Kiszka
    cpu_single_step(env, 0);
2456 858693c6 bellard
}
2457 1fddef4b bellard
#endif
2458 858693c6 bellard
2459 a2d1ebaf pbrook
/* Send a gdb syscall request.
2460 a2d1ebaf pbrook
   This accepts limited printf-style format specifiers, specifically:
2461 a87295e8 pbrook
    %x  - target_ulong argument printed in hex.
2462 a87295e8 pbrook
    %lx - 64-bit argument printed in hex.
2463 a87295e8 pbrook
    %s  - string pointer (target_ulong) and length (int) pair.  */
2464 7ccfb2eb blueswir1
void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2465 a2d1ebaf pbrook
{
2466 a2d1ebaf pbrook
    va_list va;
2467 a2d1ebaf pbrook
    char *p;
2468 cdb432b2 Meador Inge
    char *p_end;
2469 a2d1ebaf pbrook
    target_ulong addr;
2470 a87295e8 pbrook
    uint64_t i64;
2471 a2d1ebaf pbrook
    GDBState *s;
2472 a2d1ebaf pbrook
2473 880a7578 aliguori
    s = gdbserver_state;
2474 a2d1ebaf pbrook
    if (!s)
2475 a2d1ebaf pbrook
        return;
2476 cdb432b2 Meador Inge
    s->current_syscall_cb = cb;
2477 a2d1ebaf pbrook
#ifndef CONFIG_USER_ONLY
2478 0461d5a6 Luiz Capitulino
    vm_stop(RUN_STATE_DEBUG);
2479 a2d1ebaf pbrook
#endif
2480 a2d1ebaf pbrook
    va_start(va, fmt);
2481 cdb432b2 Meador Inge
    p = s->syscall_buf;
2482 cdb432b2 Meador Inge
    p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
2483 a2d1ebaf pbrook
    *(p++) = 'F';
2484 a2d1ebaf pbrook
    while (*fmt) {
2485 a2d1ebaf pbrook
        if (*fmt == '%') {
2486 a2d1ebaf pbrook
            fmt++;
2487 a2d1ebaf pbrook
            switch (*fmt++) {
2488 a2d1ebaf pbrook
            case 'x':
2489 a2d1ebaf pbrook
                addr = va_arg(va, target_ulong);
2490 cdb432b2 Meador Inge
                p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
2491 a2d1ebaf pbrook
                break;
2492 a87295e8 pbrook
            case 'l':
2493 a87295e8 pbrook
                if (*(fmt++) != 'x')
2494 a87295e8 pbrook
                    goto bad_format;
2495 a87295e8 pbrook
                i64 = va_arg(va, uint64_t);
2496 cdb432b2 Meador Inge
                p += snprintf(p, p_end - p, "%" PRIx64, i64);
2497 a87295e8 pbrook
                break;
2498 a2d1ebaf pbrook
            case 's':
2499 a2d1ebaf pbrook
                addr = va_arg(va, target_ulong);
2500 cdb432b2 Meador Inge
                p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
2501 363a37d5 blueswir1
                              addr, va_arg(va, int));
2502 a2d1ebaf pbrook
                break;
2503 a2d1ebaf pbrook
            default:
2504 a87295e8 pbrook
            bad_format:
2505 a2d1ebaf pbrook
                fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
2506 a2d1ebaf pbrook
                        fmt - 1);
2507 a2d1ebaf pbrook
                break;
2508 a2d1ebaf pbrook
            }
2509 a2d1ebaf pbrook
        } else {
2510 a2d1ebaf pbrook
            *(p++) = *(fmt++);
2511 a2d1ebaf pbrook
        }
2512 a2d1ebaf pbrook
    }
2513 8a93e02a pbrook
    *p = 0;
2514 a2d1ebaf pbrook
    va_end(va);
2515 a2d1ebaf pbrook
#ifdef CONFIG_USER_ONLY
2516 cdb432b2 Meador Inge
    put_packet(s, s->syscall_buf);
2517 880a7578 aliguori
    gdb_handlesig(s->c_cpu, 0);
2518 a2d1ebaf pbrook
#else
2519 cdb432b2 Meador Inge
    /* In this case wait to send the syscall packet until notification that
2520 cdb432b2 Meador Inge
       the CPU has stopped.  This must be done because if the packet is sent
2521 cdb432b2 Meador Inge
       now the reply from the syscall request could be received while the CPU
2522 cdb432b2 Meador Inge
       is still in the running state, which can cause packets to be dropped
2523 cdb432b2 Meador Inge
       and state transition 'T' packets to be sent while the syscall is still
2524 cdb432b2 Meador Inge
       being processed.  */
2525 3098dba0 aurel32
    cpu_exit(s->c_cpu);
2526 a2d1ebaf pbrook
#endif
2527 a2d1ebaf pbrook
}
2528 a2d1ebaf pbrook
2529 6a00d601 bellard
static void gdb_read_byte(GDBState *s, int ch)
2530 858693c6 bellard
{
2531 858693c6 bellard
    int i, csum;
2532 60fe76f3 ths
    uint8_t reply;
2533 858693c6 bellard
2534 1fddef4b bellard
#ifndef CONFIG_USER_ONLY
2535 4046d913 pbrook
    if (s->last_packet_len) {
2536 4046d913 pbrook
        /* Waiting for a response to the last packet.  If we see the start
2537 4046d913 pbrook
           of a new command then abandon the previous response.  */
2538 4046d913 pbrook
        if (ch == '-') {
2539 4046d913 pbrook
#ifdef DEBUG_GDB
2540 4046d913 pbrook
            printf("Got NACK, retransmitting\n");
2541 4046d913 pbrook
#endif
2542 ffe8ab83 ths
            put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
2543 4046d913 pbrook
        }
2544 4046d913 pbrook
#ifdef DEBUG_GDB
2545 4046d913 pbrook
        else if (ch == '+')
2546 4046d913 pbrook
            printf("Got ACK\n");
2547 4046d913 pbrook
        else
2548 4046d913 pbrook
            printf("Got '%c' when expecting ACK/NACK\n", ch);
2549 4046d913 pbrook
#endif
2550 4046d913 pbrook
        if (ch == '+' || ch == '$')
2551 4046d913 pbrook
            s->last_packet_len = 0;
2552 4046d913 pbrook
        if (ch != '$')
2553 4046d913 pbrook
            return;
2554 4046d913 pbrook
    }
2555 1354869c Luiz Capitulino
    if (runstate_is_running()) {
2556 858693c6 bellard
        /* when the CPU is running, we cannot do anything except stop
2557 858693c6 bellard
           it when receiving a char */
2558 0461d5a6 Luiz Capitulino
        vm_stop(RUN_STATE_PAUSED);
2559 5fafdf24 ths
    } else
2560 1fddef4b bellard
#endif
2561 41625033 bellard
    {
2562 858693c6 bellard
        switch(s->state) {
2563 858693c6 bellard
        case RS_IDLE:
2564 858693c6 bellard
            if (ch == '$') {
2565 858693c6 bellard
                s->line_buf_index = 0;
2566 858693c6 bellard
                s->state = RS_GETLINE;
2567 c33a346e bellard
            }
2568 b4608c04 bellard
            break;
2569 858693c6 bellard
        case RS_GETLINE:
2570 858693c6 bellard
            if (ch == '#') {
2571 858693c6 bellard
            s->state = RS_CHKSUM1;
2572 858693c6 bellard
            } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2573 858693c6 bellard
                s->state = RS_IDLE;
2574 4c3a88a2 bellard
            } else {
2575 858693c6 bellard
            s->line_buf[s->line_buf_index++] = ch;
2576 4c3a88a2 bellard
            }
2577 4c3a88a2 bellard
            break;
2578 858693c6 bellard
        case RS_CHKSUM1:
2579 858693c6 bellard
            s->line_buf[s->line_buf_index] = '\0';
2580 858693c6 bellard
            s->line_csum = fromhex(ch) << 4;
2581 858693c6 bellard
            s->state = RS_CHKSUM2;
2582 858693c6 bellard
            break;
2583 858693c6 bellard
        case RS_CHKSUM2:
2584 858693c6 bellard
            s->line_csum |= fromhex(ch);
2585 858693c6 bellard
            csum = 0;
2586 858693c6 bellard
            for(i = 0; i < s->line_buf_index; i++) {
2587 858693c6 bellard
                csum += s->line_buf[i];
2588 858693c6 bellard
            }
2589 858693c6 bellard
            if (s->line_csum != (csum & 0xff)) {
2590 60fe76f3 ths
                reply = '-';
2591 60fe76f3 ths
                put_buffer(s, &reply, 1);
2592 858693c6 bellard
                s->state = RS_IDLE;
2593 4c3a88a2 bellard
            } else {
2594 60fe76f3 ths
                reply = '+';
2595 60fe76f3 ths
                put_buffer(s, &reply, 1);
2596 880a7578 aliguori
                s->state = gdb_handle_packet(s, s->line_buf);
2597 4c3a88a2 bellard
            }
2598 4c3a88a2 bellard
            break;
2599 a2d1ebaf pbrook
        default:
2600 a2d1ebaf pbrook
            abort();
2601 858693c6 bellard
        }
2602 858693c6 bellard
    }
2603 858693c6 bellard
}
2604 858693c6 bellard
2605 0e1c9c54 Paul Brook
/* Tell the remote gdb that the process has exited.  */
2606 9349b4f9 Andreas Färber
void gdb_exit(CPUArchState *env, int code)
2607 0e1c9c54 Paul Brook
{
2608 0e1c9c54 Paul Brook
  GDBState *s;
2609 0e1c9c54 Paul Brook
  char buf[4];
2610 0e1c9c54 Paul Brook
2611 0e1c9c54 Paul Brook
  s = gdbserver_state;
2612 0e1c9c54 Paul Brook
  if (!s) {
2613 0e1c9c54 Paul Brook
      return;
2614 0e1c9c54 Paul Brook
  }
2615 0e1c9c54 Paul Brook
#ifdef CONFIG_USER_ONLY
2616 0e1c9c54 Paul Brook
  if (gdbserver_fd < 0 || s->fd < 0) {
2617 0e1c9c54 Paul Brook
      return;
2618 0e1c9c54 Paul Brook
  }
2619 0e1c9c54 Paul Brook
#endif
2620 0e1c9c54 Paul Brook
2621 0e1c9c54 Paul Brook
  snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2622 0e1c9c54 Paul Brook
  put_packet(s, buf);
2623 e2af15b2 Fabien Chouteau
2624 e2af15b2 Fabien Chouteau
#ifndef CONFIG_USER_ONLY
2625 e2af15b2 Fabien Chouteau
  if (s->chr) {
2626 70f24fb6 Anthony Liguori
      qemu_chr_delete(s->chr);
2627 e2af15b2 Fabien Chouteau
  }
2628 e2af15b2 Fabien Chouteau
#endif
2629 0e1c9c54 Paul Brook
}
2630 0e1c9c54 Paul Brook
2631 1fddef4b bellard
#ifdef CONFIG_USER_ONLY
2632 1fddef4b bellard
int
2633 ca587a8e aurel32
gdb_queuesig (void)
2634 ca587a8e aurel32
{
2635 ca587a8e aurel32
    GDBState *s;
2636 ca587a8e aurel32
2637 ca587a8e aurel32
    s = gdbserver_state;
2638 ca587a8e aurel32
2639 ca587a8e aurel32
    if (gdbserver_fd < 0 || s->fd < 0)
2640 ca587a8e aurel32
        return 0;
2641 ca587a8e aurel32
    else
2642 ca587a8e aurel32
        return 1;
2643 ca587a8e aurel32
}
2644 ca587a8e aurel32
2645 ca587a8e aurel32
int
2646 9349b4f9 Andreas Färber
gdb_handlesig (CPUArchState *env, int sig)
2647 1fddef4b bellard
{
2648 1fddef4b bellard
  GDBState *s;
2649 1fddef4b bellard
  char buf[256];
2650 1fddef4b bellard
  int n;
2651 1fddef4b bellard
2652 880a7578 aliguori
  s = gdbserver_state;
2653 1f487ee9 edgar_igl
  if (gdbserver_fd < 0 || s->fd < 0)
2654 1f487ee9 edgar_igl
    return sig;
2655 1fddef4b bellard
2656 1fddef4b bellard
  /* disable single step if it was enabled */
2657 1fddef4b bellard
  cpu_single_step(env, 0);
2658 1fddef4b bellard
  tb_flush(env);
2659 1fddef4b bellard
2660 1fddef4b bellard
  if (sig != 0)
2661 1fddef4b bellard
    {
2662 ca587a8e aurel32
      snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb (sig));
2663 1fddef4b bellard
      put_packet(s, buf);
2664 1fddef4b bellard
    }
2665 1f487ee9 edgar_igl
  /* put_packet() might have detected that the peer terminated the 
2666 1f487ee9 edgar_igl
     connection.  */
2667 1f487ee9 edgar_igl
  if (s->fd < 0)
2668 1f487ee9 edgar_igl
      return sig;
2669 1fddef4b bellard
2670 1fddef4b bellard
  sig = 0;
2671 1fddef4b bellard
  s->state = RS_IDLE;
2672 41625033 bellard
  s->running_state = 0;
2673 41625033 bellard
  while (s->running_state == 0) {
2674 1fddef4b bellard
      n = read (s->fd, buf, 256);
2675 1fddef4b bellard
      if (n > 0)
2676 1fddef4b bellard
        {
2677 1fddef4b bellard
          int i;
2678 1fddef4b bellard
2679 1fddef4b bellard
          for (i = 0; i < n; i++)
2680 6a00d601 bellard
            gdb_read_byte (s, buf[i]);
2681 1fddef4b bellard
        }
2682 1fddef4b bellard
      else if (n == 0 || errno != EAGAIN)
2683 1fddef4b bellard
        {
2684 e7d81004 Stefan Weil
          /* XXX: Connection closed.  Should probably wait for another
2685 1fddef4b bellard
             connection before continuing.  */
2686 1fddef4b bellard
          return sig;
2687 1fddef4b bellard
        }
2688 41625033 bellard
  }
2689 1f487ee9 edgar_igl
  sig = s->signal;
2690 1f487ee9 edgar_igl
  s->signal = 0;
2691 1fddef4b bellard
  return sig;
2692 1fddef4b bellard
}
2693 e9009676 bellard
2694 ca587a8e aurel32
/* Tell the remote gdb that the process has exited due to SIG.  */
2695 9349b4f9 Andreas Färber
void gdb_signalled(CPUArchState *env, int sig)
2696 ca587a8e aurel32
{
2697 ca587a8e aurel32
  GDBState *s;
2698 ca587a8e aurel32
  char buf[4];
2699 ca587a8e aurel32
2700 ca587a8e aurel32
  s = gdbserver_state;
2701 ca587a8e aurel32
  if (gdbserver_fd < 0 || s->fd < 0)
2702 ca587a8e aurel32
    return;
2703 ca587a8e aurel32
2704 ca587a8e aurel32
  snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb (sig));
2705 ca587a8e aurel32
  put_packet(s, buf);
2706 ca587a8e aurel32
}
2707 1fddef4b bellard
2708 880a7578 aliguori
static void gdb_accept(void)
2709 858693c6 bellard
{
2710 858693c6 bellard
    GDBState *s;
2711 858693c6 bellard
    struct sockaddr_in sockaddr;
2712 858693c6 bellard
    socklen_t len;
2713 858693c6 bellard
    int val, fd;
2714 858693c6 bellard
2715 858693c6 bellard
    for(;;) {
2716 858693c6 bellard
        len = sizeof(sockaddr);
2717 858693c6 bellard
        fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2718 858693c6 bellard
        if (fd < 0 && errno != EINTR) {
2719 858693c6 bellard
            perror("accept");
2720 858693c6 bellard
            return;
2721 858693c6 bellard
        } else if (fd >= 0) {
2722 40ff6d7e Kevin Wolf
#ifndef _WIN32
2723 40ff6d7e Kevin Wolf
            fcntl(fd, F_SETFD, FD_CLOEXEC);
2724 40ff6d7e Kevin Wolf
#endif
2725 b4608c04 bellard
            break;
2726 b4608c04 bellard
        }
2727 b4608c04 bellard
    }
2728 858693c6 bellard
2729 858693c6 bellard
    /* set short latency */
2730 858693c6 bellard
    val = 1;
2731 8f447cc7 bellard
    setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2732 3b46e624 ths
2733 7267c094 Anthony Liguori
    s = g_malloc0(sizeof(GDBState));
2734 880a7578 aliguori
    s->c_cpu = first_cpu;
2735 880a7578 aliguori
    s->g_cpu = first_cpu;
2736 858693c6 bellard
    s->fd = fd;
2737 56aebc89 pbrook
    gdb_has_xml = 0;
2738 858693c6 bellard
2739 880a7578 aliguori
    gdbserver_state = s;
2740 a2d1ebaf pbrook
2741 858693c6 bellard
    fcntl(fd, F_SETFL, O_NONBLOCK);
2742 858693c6 bellard
}
2743 858693c6 bellard
2744 858693c6 bellard
static int gdbserver_open(int port)
2745 858693c6 bellard
{
2746 858693c6 bellard
    struct sockaddr_in sockaddr;
2747 858693c6 bellard
    int fd, val, ret;
2748 858693c6 bellard
2749 858693c6 bellard
    fd = socket(PF_INET, SOCK_STREAM, 0);
2750 858693c6 bellard
    if (fd < 0) {
2751 858693c6 bellard
        perror("socket");
2752 858693c6 bellard
        return -1;
2753 858693c6 bellard
    }
2754 40ff6d7e Kevin Wolf
#ifndef _WIN32
2755 40ff6d7e Kevin Wolf
    fcntl(fd, F_SETFD, FD_CLOEXEC);
2756 40ff6d7e Kevin Wolf
#endif
2757 858693c6 bellard
2758 858693c6 bellard
    /* allow fast reuse */
2759 858693c6 bellard
    val = 1;
2760 8f447cc7 bellard
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
2761 858693c6 bellard
2762 858693c6 bellard
    sockaddr.sin_family = AF_INET;
2763 858693c6 bellard
    sockaddr.sin_port = htons(port);
2764 858693c6 bellard
    sockaddr.sin_addr.s_addr = 0;
2765 858693c6 bellard
    ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2766 858693c6 bellard
    if (ret < 0) {
2767 858693c6 bellard
        perror("bind");
2768 bb16172c Peter Maydell
        close(fd);
2769 858693c6 bellard
        return -1;
2770 858693c6 bellard
    }
2771 858693c6 bellard
    ret = listen(fd, 0);
2772 858693c6 bellard
    if (ret < 0) {
2773 858693c6 bellard
        perror("listen");
2774 bb16172c Peter Maydell
        close(fd);
2775 858693c6 bellard
        return -1;
2776 858693c6 bellard
    }
2777 858693c6 bellard
    return fd;
2778 858693c6 bellard
}
2779 858693c6 bellard
2780 858693c6 bellard
int gdbserver_start(int port)
2781 858693c6 bellard
{
2782 858693c6 bellard
    gdbserver_fd = gdbserver_open(port);
2783 858693c6 bellard
    if (gdbserver_fd < 0)
2784 858693c6 bellard
        return -1;
2785 858693c6 bellard
    /* accept connections */
2786 880a7578 aliguori
    gdb_accept();
2787 4046d913 pbrook
    return 0;
2788 4046d913 pbrook
}
2789 2b1319c8 aurel32
2790 2b1319c8 aurel32
/* Disable gdb stub for child processes.  */
2791 9349b4f9 Andreas Färber
void gdbserver_fork(CPUArchState *env)
2792 2b1319c8 aurel32
{
2793 2b1319c8 aurel32
    GDBState *s = gdbserver_state;
2794 9f6164d6 edgar_igl
    if (gdbserver_fd < 0 || s->fd < 0)
2795 2b1319c8 aurel32
      return;
2796 2b1319c8 aurel32
    close(s->fd);
2797 2b1319c8 aurel32
    s->fd = -1;
2798 2b1319c8 aurel32
    cpu_breakpoint_remove_all(env, BP_GDB);
2799 2b1319c8 aurel32
    cpu_watchpoint_remove_all(env, BP_GDB);
2800 2b1319c8 aurel32
}
2801 1fddef4b bellard
#else
2802 aa1f17c1 ths
static int gdb_chr_can_receive(void *opaque)
2803 4046d913 pbrook
{
2804 56aebc89 pbrook
  /* We can handle an arbitrarily large amount of data.
2805 56aebc89 pbrook
   Pick the maximum packet size, which is as good as anything.  */
2806 56aebc89 pbrook
  return MAX_PACKET_LENGTH;
2807 4046d913 pbrook
}
2808 4046d913 pbrook
2809 aa1f17c1 ths
static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
2810 4046d913 pbrook
{
2811 4046d913 pbrook
    int i;
2812 4046d913 pbrook
2813 4046d913 pbrook
    for (i = 0; i < size; i++) {
2814 880a7578 aliguori
        gdb_read_byte(gdbserver_state, buf[i]);
2815 4046d913 pbrook
    }
2816 4046d913 pbrook
}
2817 4046d913 pbrook
2818 4046d913 pbrook
static void gdb_chr_event(void *opaque, int event)
2819 4046d913 pbrook
{
2820 4046d913 pbrook
    switch (event) {
2821 b6b8df56 Amit Shah
    case CHR_EVENT_OPENED:
2822 0461d5a6 Luiz Capitulino
        vm_stop(RUN_STATE_PAUSED);
2823 56aebc89 pbrook
        gdb_has_xml = 0;
2824 4046d913 pbrook
        break;
2825 4046d913 pbrook
    default:
2826 4046d913 pbrook
        break;
2827 4046d913 pbrook
    }
2828 4046d913 pbrook
}
2829 4046d913 pbrook
2830 8a34a0fb aliguori
static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2831 8a34a0fb aliguori
{
2832 8a34a0fb aliguori
    char buf[MAX_PACKET_LENGTH];
2833 8a34a0fb aliguori
2834 8a34a0fb aliguori
    buf[0] = 'O';
2835 8a34a0fb aliguori
    if (len > (MAX_PACKET_LENGTH/2) - 1)
2836 8a34a0fb aliguori
        len = (MAX_PACKET_LENGTH/2) - 1;
2837 8a34a0fb aliguori
    memtohex(buf + 1, (uint8_t *)msg, len);
2838 8a34a0fb aliguori
    put_packet(s, buf);
2839 8a34a0fb aliguori
}
2840 8a34a0fb aliguori
2841 8a34a0fb aliguori
static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
2842 8a34a0fb aliguori
{
2843 8a34a0fb aliguori
    const char *p = (const char *)buf;
2844 8a34a0fb aliguori
    int max_sz;
2845 8a34a0fb aliguori
2846 8a34a0fb aliguori
    max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2847 8a34a0fb aliguori
    for (;;) {
2848 8a34a0fb aliguori
        if (len <= max_sz) {
2849 8a34a0fb aliguori
            gdb_monitor_output(gdbserver_state, p, len);
2850 8a34a0fb aliguori
            break;
2851 8a34a0fb aliguori
        }
2852 8a34a0fb aliguori
        gdb_monitor_output(gdbserver_state, p, max_sz);
2853 8a34a0fb aliguori
        p += max_sz;
2854 8a34a0fb aliguori
        len -= max_sz;
2855 8a34a0fb aliguori
    }
2856 8a34a0fb aliguori
    return len;
2857 8a34a0fb aliguori
}
2858 8a34a0fb aliguori
2859 59030a8c aliguori
#ifndef _WIN32
2860 59030a8c aliguori
static void gdb_sigterm_handler(int signal)
2861 59030a8c aliguori
{
2862 1354869c Luiz Capitulino
    if (runstate_is_running()) {
2863 0461d5a6 Luiz Capitulino
        vm_stop(RUN_STATE_PAUSED);
2864 e07bbac5 Jan Kiszka
    }
2865 59030a8c aliguori
}
2866 59030a8c aliguori
#endif
2867 59030a8c aliguori
2868 59030a8c aliguori
int gdbserver_start(const char *device)
2869 4046d913 pbrook
{
2870 4046d913 pbrook
    GDBState *s;
2871 59030a8c aliguori
    char gdbstub_device_name[128];
2872 36556b20 aliguori
    CharDriverState *chr = NULL;
2873 36556b20 aliguori
    CharDriverState *mon_chr;
2874 cfc3475a pbrook
2875 59030a8c aliguori
    if (!device)
2876 59030a8c aliguori
        return -1;
2877 59030a8c aliguori
    if (strcmp(device, "none") != 0) {
2878 59030a8c aliguori
        if (strstart(device, "tcp:", NULL)) {
2879 59030a8c aliguori
            /* enforce required TCP attributes */
2880 59030a8c aliguori
            snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2881 59030a8c aliguori
                     "%s,nowait,nodelay,server", device);
2882 59030a8c aliguori
            device = gdbstub_device_name;
2883 36556b20 aliguori
        }
2884 59030a8c aliguori
#ifndef _WIN32
2885 59030a8c aliguori
        else if (strcmp(device, "stdio") == 0) {
2886 59030a8c aliguori
            struct sigaction act;
2887 4046d913 pbrook
2888 59030a8c aliguori
            memset(&act, 0, sizeof(act));
2889 59030a8c aliguori
            act.sa_handler = gdb_sigterm_handler;
2890 59030a8c aliguori
            sigaction(SIGINT, &act, NULL);
2891 59030a8c aliguori
        }
2892 59030a8c aliguori
#endif
2893 27143a44 Anthony Liguori
        chr = qemu_chr_new("gdb", device, NULL);
2894 36556b20 aliguori
        if (!chr)
2895 36556b20 aliguori
            return -1;
2896 36556b20 aliguori
2897 36556b20 aliguori
        qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
2898 36556b20 aliguori
                              gdb_chr_event, NULL);
2899 cfc3475a pbrook
    }
2900 cfc3475a pbrook
2901 36556b20 aliguori
    s = gdbserver_state;
2902 36556b20 aliguori
    if (!s) {
2903 7267c094 Anthony Liguori
        s = g_malloc0(sizeof(GDBState));
2904 36556b20 aliguori
        gdbserver_state = s;
2905 4046d913 pbrook
2906 36556b20 aliguori
        qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2907 36556b20 aliguori
2908 36556b20 aliguori
        /* Initialize a monitor terminal for gdb */
2909 7267c094 Anthony Liguori
        mon_chr = g_malloc0(sizeof(*mon_chr));
2910 36556b20 aliguori
        mon_chr->chr_write = gdb_monitor_write;
2911 36556b20 aliguori
        monitor_init(mon_chr, 0);
2912 36556b20 aliguori
    } else {
2913 36556b20 aliguori
        if (s->chr)
2914 70f24fb6 Anthony Liguori
            qemu_chr_delete(s->chr);
2915 36556b20 aliguori
        mon_chr = s->mon_chr;
2916 36556b20 aliguori
        memset(s, 0, sizeof(GDBState));
2917 36556b20 aliguori
    }
2918 880a7578 aliguori
    s->c_cpu = first_cpu;
2919 880a7578 aliguori
    s->g_cpu = first_cpu;
2920 4046d913 pbrook
    s->chr = chr;
2921 36556b20 aliguori
    s->state = chr ? RS_IDLE : RS_INACTIVE;
2922 36556b20 aliguori
    s->mon_chr = mon_chr;
2923 cdb432b2 Meador Inge
    s->current_syscall_cb = NULL;
2924 8a34a0fb aliguori
2925 b4608c04 bellard
    return 0;
2926 b4608c04 bellard
}
2927 4046d913 pbrook
#endif