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"
22 #include "exec/gdbstub.h"
25 static int ppc_gdb_register_len_apple(int n
)
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 */
42 case 66 + 32: /* cr */
43 case 69 + 32: /* xer */
50 static int ppc_gdb_register_len(int n
)
55 return sizeof(target_ulong
);
75 return sizeof(target_ulong
);
81 return sizeof(target_ulong
);
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_WORDS_BIGENDIAN is set to the proper ordering for the
91 * binary, and cannot be changed. For system mode,
92 * TARGET_WORDS_BIGENDIAN 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
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
);
107 g_assert_not_reached();
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
;
125 int r
= ppc_gdb_register_len(n
);
133 gdb_get_regl(buf
, env
->gpr
[n
]);
136 gdb_get_reg64(buf
, *cpu_fpr_ptr(env
, n
- 32));
140 gdb_get_regl(buf
, env
->nip
);
143 gdb_get_regl(buf
, env
->msr
);
149 for (i
= 0; i
< 8; i
++) {
150 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
152 gdb_get_reg32(buf
, cr
);
156 gdb_get_regl(buf
, env
->lr
);
159 gdb_get_regl(buf
, env
->ctr
);
162 gdb_get_reg32(buf
, env
->xer
);
165 gdb_get_reg32(buf
, env
->fpscr
);
169 mem_buf
= buf
->data
+ buf
->len
- r
;
170 ppc_maybe_bswap_register(env
, mem_buf
, 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
;
179 int r
= ppc_gdb_register_len_apple(n
);
187 gdb_get_reg64(buf
, env
->gpr
[n
]);
190 gdb_get_reg64(buf
, *cpu_fpr_ptr(env
, n
- 32));
193 gdb_get_reg64(buf
, n
- 64);
194 gdb_get_reg64(buf
, 0);
198 gdb_get_reg64(buf
, env
->nip
);
201 gdb_get_reg64(buf
, env
->msr
);
207 for (i
= 0; i
< 8; i
++) {
208 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
210 gdb_get_reg32(buf
, cr
);
214 gdb_get_reg64(buf
, env
->lr
);
217 gdb_get_reg64(buf
, env
->ctr
);
220 gdb_get_reg32(buf
, env
->xer
);
223 gdb_get_reg64(buf
, env
->fpscr
);
227 mem_buf
= buf
->data
+ buf
->len
- r
;
228 ppc_maybe_bswap_register(env
, mem_buf
, 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
);
241 ppc_maybe_bswap_register(env
, mem_buf
, r
);
244 env
->gpr
[n
] = ldtul_p(mem_buf
);
247 *cpu_fpr_ptr(env
, n
- 32) = ldq_p(mem_buf
);
251 env
->nip
= ldtul_p(mem_buf
);
254 ppc_store_msr(env
, ldtul_p(mem_buf
));
258 uint32_t cr
= ldl_p(mem_buf
);
260 for (i
= 0; i
< 8; i
++) {
261 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
266 env
->lr
= ldtul_p(mem_buf
);
269 env
->ctr
= ldtul_p(mem_buf
);
272 env
->xer
= ldl_p(mem_buf
);
276 ppc_store_fpscr(env
, ldtul_p(mem_buf
));
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
);
291 ppc_maybe_bswap_register(env
, mem_buf
, r
);
294 env
->gpr
[n
] = ldq_p(mem_buf
);
297 *cpu_fpr_ptr(env
, n
- 32) = ldq_p(mem_buf
);
301 env
->nip
= ldq_p(mem_buf
);
304 ppc_store_msr(env
, ldq_p(mem_buf
));
308 uint32_t cr
= ldl_p(mem_buf
);
310 for (i
= 0; i
< 8; i
++) {
311 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
316 env
->lr
= ldq_p(mem_buf
);
319 env
->ctr
= ldq_p(mem_buf
);
322 env
->xer
= ldl_p(mem_buf
);
326 ppc_store_fpscr(env
, ldq_p(mem_buf
));
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
;
340 unsigned int num_regs
= 0;
343 if (pcc
->gdb_spr_xml
) {
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
];
358 spr_name
= g_ascii_strdown(spr
->name
, -1);
359 g_string_append_printf(xml
, "<reg name=\"%s\"", 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
;
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
;
394 #if !defined(CONFIG_USER_ONLY)
395 static int gdb_find_spr_idx(CPUPPCState
*env
, int n
)
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
) {
409 static int gdb_get_spr_reg(CPUPPCState
*env
, GByteArray
*buf
, int n
)
414 reg
= gdb_find_spr_idx(env
, n
);
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
);
425 static int gdb_set_spr_reg(CPUPPCState
*env
, uint8_t *mem_buf
, int n
)
430 reg
= gdb_find_spr_idx(env
, n
);
435 len
= TARGET_LONG_SIZE
;
436 ppc_maybe_bswap_register(env
, mem_buf
, len
);
437 env
->spr
[reg
] = ldn_p(mem_buf
, len
);
443 static int gdb_get_float_reg(CPUPPCState
*env
, GByteArray
*buf
, int n
)
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);
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);
461 static int gdb_set_float_reg(CPUPPCState
*env
, uint8_t *mem_buf
, int n
)
464 ppc_maybe_bswap_register(env
, mem_buf
, 8);
465 *cpu_fpr_ptr(env
, n
) = ldq_p(mem_buf
);
469 ppc_maybe_bswap_register(env
, mem_buf
, 4);
470 ppc_store_fpscr(env
, ldl_p(mem_buf
));
476 static int gdb_get_avr_reg(CPUPPCState
*env
, GByteArray
*buf
, int n
)
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);
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);
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);
502 static int gdb_set_avr_reg(CPUPPCState
*env
, uint8_t *mem_buf
, int n
)
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);
512 ppc_maybe_bswap_register(env
, mem_buf
, 4);
513 ppc_store_vscr(env
, ldl_p(mem_buf
));
517 ppc_maybe_bswap_register(env
, mem_buf
, 4);
518 env
->spr
[SPR_VRSAVE
] = (target_ulong
)ldl_p(mem_buf
);
524 static int gdb_get_spe_reg(CPUPPCState
*env
, GByteArray
*buf
, int n
)
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);
531 gdb_get_reg32(buf
, env
->gprh
[n
]);
536 gdb_get_reg64(buf
, env
->spe_acc
);
537 ppc_maybe_bswap_register(env
, gdb_get_reg_ptr(buf
, 8), 8);
541 gdb_get_reg32(buf
, env
->spe_fscr
);
542 ppc_maybe_bswap_register(env
, gdb_get_reg_ptr(buf
, 4), 4);
548 static int gdb_set_spe_reg(CPUPPCState
*env
, uint8_t *mem_buf
, int n
)
551 #if defined(TARGET_PPC64)
552 target_ulong lo
= (uint32_t)env
->gpr
[n
];
555 ppc_maybe_bswap_register(env
, mem_buf
, 4);
557 hi
= (target_ulong
)ldl_p(mem_buf
) << 32;
558 env
->gpr
[n
] = lo
| hi
;
560 env
->gprh
[n
] = ldl_p(mem_buf
);
565 ppc_maybe_bswap_register(env
, mem_buf
, 8);
566 env
->spe_acc
= ldq_p(mem_buf
);
570 ppc_maybe_bswap_register(env
, mem_buf
, 4);
571 env
->spe_fscr
= ldl_p(mem_buf
);
577 static int gdb_get_vsx_reg(CPUPPCState
*env
, GByteArray
*buf
, int n
)
580 gdb_get_reg64(buf
, *cpu_vsrl_ptr(env
, n
));
581 ppc_maybe_bswap_register(env
, gdb_get_reg_ptr(buf
, 8), 8);
587 static int gdb_set_vsx_reg(CPUPPCState
*env
, uint8_t *mem_buf
, int n
)
590 ppc_maybe_bswap_register(env
, mem_buf
, 8);
591 *cpu_vsrl_ptr(env
, n
) = ldq_p(mem_buf
);
597 gchar
*ppc_gdb_arch_name(CPUState
*cs
)
599 #if defined(TARGET_PPC64)
600 return g_strdup("powerpc:common64");
602 return g_strdup("powerpc:common");
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);