target/i386: introduce insn_get_addr
[qemu.git] / target / ppc / gdbstub.c
blob1a0b9ca82c38371da1d6e4d46b0b498693d9a847
1 /*
2 * PowerPC gdb server stub
4 * Copyright (c) 2003-2005 Fabrice Bellard
5 * Copyright (c) 2013 SUSE LINUX Products GmbH
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "exec/gdbstub.h"
23 #include "internal.h"
25 static int ppc_gdb_register_len_apple(int n)
27 switch (n) {
28 case 0 ... 31:
29 /* gprs */
30 return 8;
31 case 32 ... 63:
32 /* fprs */
33 return 8;
34 case 64 ... 95:
35 return 16;
36 case 64 + 32: /* nip */
37 case 65 + 32: /* msr */
38 case 67 + 32: /* lr */
39 case 68 + 32: /* ctr */
40 case 70 + 32: /* fpscr */
41 return 8;
42 case 66 + 32: /* cr */
43 case 69 + 32: /* xer */
44 return 4;
45 default:
46 return 0;
50 static int ppc_gdb_register_len(int n)
52 switch (n) {
53 case 0 ... 31:
54 /* gprs */
55 return sizeof(target_ulong);
56 case 32 ... 63:
57 /* fprs */
58 if (gdb_has_xml) {
59 return 0;
61 return 8;
62 case 66:
63 /* cr */
64 case 69:
65 /* xer */
66 return 4;
67 case 64:
68 /* nip */
69 case 65:
70 /* msr */
71 case 67:
72 /* lr */
73 case 68:
74 /* ctr */
75 return sizeof(target_ulong);
76 case 70:
77 /* fpscr */
78 if (gdb_has_xml) {
79 return 0;
81 return sizeof(target_ulong);
82 default:
83 return 0;
88 * We need to present the registers to gdb in the "current" memory
89 * ordering. For user-only mode we get this for free;
90 * TARGET_BIG_ENDIAN is set to the proper ordering for the
91 * binary, and cannot be changed. For system mode,
92 * TARGET_BIG_ENDIAN is always set, and we must check the current
93 * mode of the chip to see if we're running in little-endian.
95 void ppc_maybe_bswap_register(CPUPPCState *env, uint8_t *mem_buf, int len)
97 #ifndef CONFIG_USER_ONLY
98 if (!FIELD_EX64(env->msr, MSR, LE)) {
99 /* do nothing */
100 } else if (len == 4) {
101 bswap32s((uint32_t *)mem_buf);
102 } else if (len == 8) {
103 bswap64s((uint64_t *)mem_buf);
104 } else if (len == 16) {
105 bswap128s((Int128 *)mem_buf);
106 } else {
107 g_assert_not_reached();
109 #endif
113 * Old gdb always expects FP registers. Newer (xml-aware) gdb only
114 * expects whatever the target description contains. Due to a
115 * historical mishap the FP registers appear in between core integer
116 * regs and PC, MSR, CR, and so forth. We hack round this by giving
117 * the FP regs zero size when talking to a newer gdb.
120 int ppc_cpu_gdb_read_register(CPUState *cs, GByteArray *buf, int n)
122 PowerPCCPU *cpu = POWERPC_CPU(cs);
123 CPUPPCState *env = &cpu->env;
124 uint8_t *mem_buf;
125 int r = ppc_gdb_register_len(n);
127 if (!r) {
128 return r;
131 if (n < 32) {
132 /* gprs */
133 gdb_get_regl(buf, env->gpr[n]);
134 } else if (n < 64) {
135 /* fprs */
136 gdb_get_reg64(buf, *cpu_fpr_ptr(env, n - 32));
137 } else {
138 switch (n) {
139 case 64:
140 gdb_get_regl(buf, env->nip);
141 break;
142 case 65:
143 gdb_get_regl(buf, env->msr);
144 break;
145 case 66:
147 uint32_t cr = 0;
148 int i;
149 for (i = 0; i < 8; i++) {
150 cr |= env->crf[i] << (32 - ((i + 1) * 4));
152 gdb_get_reg32(buf, cr);
153 break;
155 case 67:
156 gdb_get_regl(buf, env->lr);
157 break;
158 case 68:
159 gdb_get_regl(buf, env->ctr);
160 break;
161 case 69:
162 gdb_get_reg32(buf, cpu_read_xer(env));
163 break;
164 case 70:
165 gdb_get_reg32(buf, env->fpscr);
166 break;
169 mem_buf = buf->data + buf->len - r;
170 ppc_maybe_bswap_register(env, mem_buf, r);
171 return r;
174 int ppc_cpu_gdb_read_register_apple(CPUState *cs, GByteArray *buf, int n)
176 PowerPCCPU *cpu = POWERPC_CPU(cs);
177 CPUPPCState *env = &cpu->env;
178 uint8_t *mem_buf;
179 int r = ppc_gdb_register_len_apple(n);
181 if (!r) {
182 return r;
185 if (n < 32) {
186 /* gprs */
187 gdb_get_reg64(buf, env->gpr[n]);
188 } else if (n < 64) {
189 /* fprs */
190 gdb_get_reg64(buf, *cpu_fpr_ptr(env, n - 32));
191 } else if (n < 96) {
192 /* Altivec */
193 gdb_get_reg64(buf, n - 64);
194 gdb_get_reg64(buf, 0);
195 } else {
196 switch (n) {
197 case 64 + 32:
198 gdb_get_reg64(buf, env->nip);
199 break;
200 case 65 + 32:
201 gdb_get_reg64(buf, env->msr);
202 break;
203 case 66 + 32:
205 uint32_t cr = 0;
206 int i;
207 for (i = 0; i < 8; i++) {
208 cr |= env->crf[i] << (32 - ((i + 1) * 4));
210 gdb_get_reg32(buf, cr);
211 break;
213 case 67 + 32:
214 gdb_get_reg64(buf, env->lr);
215 break;
216 case 68 + 32:
217 gdb_get_reg64(buf, env->ctr);
218 break;
219 case 69 + 32:
220 gdb_get_reg32(buf, cpu_read_xer(env));
221 break;
222 case 70 + 32:
223 gdb_get_reg64(buf, env->fpscr);
224 break;
227 mem_buf = buf->data + buf->len - r;
228 ppc_maybe_bswap_register(env, mem_buf, r);
229 return r;
232 int ppc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
234 PowerPCCPU *cpu = POWERPC_CPU(cs);
235 CPUPPCState *env = &cpu->env;
236 int r = ppc_gdb_register_len(n);
238 if (!r) {
239 return r;
241 ppc_maybe_bswap_register(env, mem_buf, r);
242 if (n < 32) {
243 /* gprs */
244 env->gpr[n] = ldtul_p(mem_buf);
245 } else if (n < 64) {
246 /* fprs */
247 *cpu_fpr_ptr(env, n - 32) = ldq_p(mem_buf);
248 } else {
249 switch (n) {
250 case 64:
251 env->nip = ldtul_p(mem_buf);
252 break;
253 case 65:
254 ppc_store_msr(env, ldtul_p(mem_buf));
255 break;
256 case 66:
258 uint32_t cr = ldl_p(mem_buf);
259 int i;
260 for (i = 0; i < 8; i++) {
261 env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
263 break;
265 case 67:
266 env->lr = ldtul_p(mem_buf);
267 break;
268 case 68:
269 env->ctr = ldtul_p(mem_buf);
270 break;
271 case 69:
272 cpu_write_xer(env, ldl_p(mem_buf));
273 break;
274 case 70:
275 /* fpscr */
276 ppc_store_fpscr(env, ldtul_p(mem_buf));
277 break;
280 return r;
282 int ppc_cpu_gdb_write_register_apple(CPUState *cs, uint8_t *mem_buf, int n)
284 PowerPCCPU *cpu = POWERPC_CPU(cs);
285 CPUPPCState *env = &cpu->env;
286 int r = ppc_gdb_register_len_apple(n);
288 if (!r) {
289 return r;
291 ppc_maybe_bswap_register(env, mem_buf, r);
292 if (n < 32) {
293 /* gprs */
294 env->gpr[n] = ldq_p(mem_buf);
295 } else if (n < 64) {
296 /* fprs */
297 *cpu_fpr_ptr(env, n - 32) = ldq_p(mem_buf);
298 } else {
299 switch (n) {
300 case 64 + 32:
301 env->nip = ldq_p(mem_buf);
302 break;
303 case 65 + 32:
304 ppc_store_msr(env, ldq_p(mem_buf));
305 break;
306 case 66 + 32:
308 uint32_t cr = ldl_p(mem_buf);
309 int i;
310 for (i = 0; i < 8; i++) {
311 env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
313 break;
315 case 67 + 32:
316 env->lr = ldq_p(mem_buf);
317 break;
318 case 68 + 32:
319 env->ctr = ldq_p(mem_buf);
320 break;
321 case 69 + 32:
322 cpu_write_xer(env, ldl_p(mem_buf));
323 break;
324 case 70 + 32:
325 /* fpscr */
326 ppc_store_fpscr(env, ldq_p(mem_buf));
327 break;
330 return r;
333 #ifndef CONFIG_USER_ONLY
334 void ppc_gdb_gen_spr_xml(PowerPCCPU *cpu)
336 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
337 CPUPPCState *env = &cpu->env;
338 GString *xml;
339 char *spr_name;
340 unsigned int num_regs = 0;
341 int i;
343 if (pcc->gdb_spr_xml) {
344 return;
347 xml = g_string_new("<?xml version=\"1.0\"?>");
348 g_string_append(xml, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
349 g_string_append(xml, "<feature name=\"org.qemu.power.spr\">");
351 for (i = 0; i < ARRAY_SIZE(env->spr_cb); i++) {
352 ppc_spr_t *spr = &env->spr_cb[i];
354 if (!spr->name) {
355 continue;
358 spr_name = g_ascii_strdown(spr->name, -1);
359 g_string_append_printf(xml, "<reg name=\"%s\"", spr_name);
360 g_free(spr_name);
362 g_string_append_printf(xml, " bitsize=\"%d\"", TARGET_LONG_BITS);
363 g_string_append(xml, " group=\"spr\"/>");
366 * GDB identifies registers based on the order they are
367 * presented in the XML. These ids will not match QEMU's
368 * representation (which follows the PowerISA).
370 * Store the position of the current register description so
371 * we can make the correspondence later.
373 spr->gdb_id = num_regs;
374 num_regs++;
377 g_string_append(xml, "</feature>");
379 pcc->gdb_num_sprs = num_regs;
380 pcc->gdb_spr_xml = g_string_free(xml, false);
383 const char *ppc_gdb_get_dynamic_xml(CPUState *cs, const char *xml_name)
385 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
387 if (strcmp(xml_name, "power-spr.xml") == 0) {
388 return pcc->gdb_spr_xml;
390 return NULL;
392 #endif
394 #if !defined(CONFIG_USER_ONLY)
395 static int gdb_find_spr_idx(CPUPPCState *env, int n)
397 int i;
399 for (i = 0; i < ARRAY_SIZE(env->spr_cb); i++) {
400 ppc_spr_t *spr = &env->spr_cb[i];
402 if (spr->name && spr->gdb_id == n) {
403 return i;
406 return -1;
409 static int gdb_get_spr_reg(CPUPPCState *env, GByteArray *buf, int n)
411 int reg;
412 int len;
414 reg = gdb_find_spr_idx(env, n);
415 if (reg < 0) {
416 return 0;
419 len = TARGET_LONG_SIZE;
420 gdb_get_regl(buf, env->spr[reg]);
421 ppc_maybe_bswap_register(env, gdb_get_reg_ptr(buf, len), len);
422 return len;
425 static int gdb_set_spr_reg(CPUPPCState *env, uint8_t *mem_buf, int n)
427 int reg;
428 int len;
430 reg = gdb_find_spr_idx(env, n);
431 if (reg < 0) {
432 return 0;
435 len = TARGET_LONG_SIZE;
436 ppc_maybe_bswap_register(env, mem_buf, len);
437 env->spr[reg] = ldn_p(mem_buf, len);
439 return len;
441 #endif
443 static int gdb_get_float_reg(CPUPPCState *env, GByteArray *buf, int n)
445 uint8_t *mem_buf;
446 if (n < 32) {
447 gdb_get_reg64(buf, *cpu_fpr_ptr(env, n));
448 mem_buf = gdb_get_reg_ptr(buf, 8);
449 ppc_maybe_bswap_register(env, mem_buf, 8);
450 return 8;
452 if (n == 32) {
453 gdb_get_reg32(buf, env->fpscr);
454 mem_buf = gdb_get_reg_ptr(buf, 4);
455 ppc_maybe_bswap_register(env, mem_buf, 4);
456 return 4;
458 return 0;
461 static int gdb_set_float_reg(CPUPPCState *env, uint8_t *mem_buf, int n)
463 if (n < 32) {
464 ppc_maybe_bswap_register(env, mem_buf, 8);
465 *cpu_fpr_ptr(env, n) = ldq_p(mem_buf);
466 return 8;
468 if (n == 32) {
469 ppc_maybe_bswap_register(env, mem_buf, 4);
470 ppc_store_fpscr(env, ldl_p(mem_buf));
471 return 4;
473 return 0;
476 static int gdb_get_avr_reg(CPUPPCState *env, GByteArray *buf, int n)
478 uint8_t *mem_buf;
480 if (n < 32) {
481 ppc_avr_t *avr = cpu_avr_ptr(env, n);
482 gdb_get_reg128(buf, avr->VsrD(0), avr->VsrD(1));
483 mem_buf = gdb_get_reg_ptr(buf, 16);
484 ppc_maybe_bswap_register(env, mem_buf, 16);
485 return 16;
487 if (n == 32) {
488 gdb_get_reg32(buf, ppc_get_vscr(env));
489 mem_buf = gdb_get_reg_ptr(buf, 4);
490 ppc_maybe_bswap_register(env, mem_buf, 4);
491 return 4;
493 if (n == 33) {
494 gdb_get_reg32(buf, (uint32_t)env->spr[SPR_VRSAVE]);
495 mem_buf = gdb_get_reg_ptr(buf, 4);
496 ppc_maybe_bswap_register(env, mem_buf, 4);
497 return 4;
499 return 0;
502 static int gdb_set_avr_reg(CPUPPCState *env, uint8_t *mem_buf, int n)
504 if (n < 32) {
505 ppc_avr_t *avr = cpu_avr_ptr(env, n);
506 ppc_maybe_bswap_register(env, mem_buf, 16);
507 avr->VsrD(0) = ldq_p(mem_buf);
508 avr->VsrD(1) = ldq_p(mem_buf + 8);
509 return 16;
511 if (n == 32) {
512 ppc_maybe_bswap_register(env, mem_buf, 4);
513 ppc_store_vscr(env, ldl_p(mem_buf));
514 return 4;
516 if (n == 33) {
517 ppc_maybe_bswap_register(env, mem_buf, 4);
518 env->spr[SPR_VRSAVE] = (target_ulong)ldl_p(mem_buf);
519 return 4;
521 return 0;
524 static int gdb_get_spe_reg(CPUPPCState *env, GByteArray *buf, int n)
526 if (n < 32) {
527 #if defined(TARGET_PPC64)
528 gdb_get_reg32(buf, env->gpr[n] >> 32);
529 ppc_maybe_bswap_register(env, gdb_get_reg_ptr(buf, 4), 4);
530 #else
531 gdb_get_reg32(buf, env->gprh[n]);
532 #endif
533 return 4;
535 if (n == 32) {
536 gdb_get_reg64(buf, env->spe_acc);
537 ppc_maybe_bswap_register(env, gdb_get_reg_ptr(buf, 8), 8);
538 return 8;
540 if (n == 33) {
541 gdb_get_reg32(buf, env->spe_fscr);
542 ppc_maybe_bswap_register(env, gdb_get_reg_ptr(buf, 4), 4);
543 return 4;
545 return 0;
548 static int gdb_set_spe_reg(CPUPPCState *env, uint8_t *mem_buf, int n)
550 if (n < 32) {
551 #if defined(TARGET_PPC64)
552 target_ulong lo = (uint32_t)env->gpr[n];
553 target_ulong hi;
555 ppc_maybe_bswap_register(env, mem_buf, 4);
557 hi = (target_ulong)ldl_p(mem_buf) << 32;
558 env->gpr[n] = lo | hi;
559 #else
560 env->gprh[n] = ldl_p(mem_buf);
561 #endif
562 return 4;
564 if (n == 32) {
565 ppc_maybe_bswap_register(env, mem_buf, 8);
566 env->spe_acc = ldq_p(mem_buf);
567 return 8;
569 if (n == 33) {
570 ppc_maybe_bswap_register(env, mem_buf, 4);
571 env->spe_fscr = ldl_p(mem_buf);
572 return 4;
574 return 0;
577 static int gdb_get_vsx_reg(CPUPPCState *env, GByteArray *buf, int n)
579 if (n < 32) {
580 gdb_get_reg64(buf, *cpu_vsrl_ptr(env, n));
581 ppc_maybe_bswap_register(env, gdb_get_reg_ptr(buf, 8), 8);
582 return 8;
584 return 0;
587 static int gdb_set_vsx_reg(CPUPPCState *env, uint8_t *mem_buf, int n)
589 if (n < 32) {
590 ppc_maybe_bswap_register(env, mem_buf, 8);
591 *cpu_vsrl_ptr(env, n) = ldq_p(mem_buf);
592 return 8;
594 return 0;
597 gchar *ppc_gdb_arch_name(CPUState *cs)
599 #if defined(TARGET_PPC64)
600 return g_strdup("powerpc:common64");
601 #else
602 return g_strdup("powerpc:common");
603 #endif
606 void ppc_gdb_init(CPUState *cs, PowerPCCPUClass *pcc)
608 if (pcc->insns_flags & PPC_FLOAT) {
609 gdb_register_coprocessor(cs, gdb_get_float_reg, gdb_set_float_reg,
610 33, "power-fpu.xml", 0);
612 if (pcc->insns_flags & PPC_ALTIVEC) {
613 gdb_register_coprocessor(cs, gdb_get_avr_reg, gdb_set_avr_reg,
614 34, "power-altivec.xml", 0);
616 if (pcc->insns_flags & PPC_SPE) {
617 gdb_register_coprocessor(cs, gdb_get_spe_reg, gdb_set_spe_reg,
618 34, "power-spe.xml", 0);
620 if (pcc->insns_flags2 & PPC2_VSX) {
621 gdb_register_coprocessor(cs, gdb_get_vsx_reg, gdb_set_vsx_reg,
622 32, "power-vsx.xml", 0);
624 #ifndef CONFIG_USER_ONLY
625 gdb_register_coprocessor(cs, gdb_get_spr_reg, gdb_set_spr_reg,
626 pcc->gdb_num_sprs, "power-spr.xml", 0);
627 #endif