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"
23 #include "gdbstub/helpers.h"
26 static int ppc_gdb_register_len_apple(int n
)
37 case 64 + 32: /* nip */
38 case 65 + 32: /* msr */
39 case 67 + 32: /* lr */
40 case 68 + 32: /* ctr */
41 case 70 + 32: /* fpscr */
43 case 66 + 32: /* cr */
44 case 69 + 32: /* xer */
51 static int ppc_gdb_register_len(int n
)
56 return sizeof(target_ulong
);
70 return sizeof(target_ulong
);
77 * We need to present the registers to gdb in the "current" memory
78 * ordering. For user-only mode we get this for free;
79 * TARGET_BIG_ENDIAN is set to the proper ordering for the
80 * binary, and cannot be changed. For system mode,
81 * TARGET_BIG_ENDIAN is always set, and we must check the current
82 * mode of the chip to see if we're running in little-endian.
84 void ppc_maybe_bswap_register(CPUPPCState
*env
, uint8_t *mem_buf
, int len
)
86 #ifndef CONFIG_USER_ONLY
87 if (!FIELD_EX64(env
->msr
, MSR
, LE
)) {
89 } else if (len
== 4) {
90 bswap32s((uint32_t *)mem_buf
);
91 } else if (len
== 8) {
92 bswap64s((uint64_t *)mem_buf
);
93 } else if (len
== 16) {
94 bswap128s((Int128
*)mem_buf
);
96 g_assert_not_reached();
102 * Old gdb always expects FP registers. Newer (xml-aware) gdb only
103 * expects whatever the target description contains. Due to a
104 * historical mishap the FP registers appear in between core integer
105 * regs and PC, MSR, CR, and so forth. We hack round this by giving
106 * the FP regs zero size when talking to a newer gdb.
109 int ppc_cpu_gdb_read_register(CPUState
*cs
, GByteArray
*buf
, int n
)
111 CPUPPCState
*env
= cpu_env(cs
);
113 int r
= ppc_gdb_register_len(n
);
121 gdb_get_regl(buf
, env
->gpr
[n
]);
125 gdb_get_regl(buf
, env
->nip
);
128 gdb_get_regl(buf
, env
->msr
);
132 uint32_t cr
= ppc_get_cr(env
);
133 gdb_get_reg32(buf
, cr
);
137 gdb_get_regl(buf
, env
->lr
);
140 gdb_get_regl(buf
, env
->ctr
);
143 gdb_get_reg32(buf
, cpu_read_xer(env
));
147 mem_buf
= buf
->data
+ buf
->len
- r
;
148 ppc_maybe_bswap_register(env
, mem_buf
, r
);
152 int ppc_cpu_gdb_read_register_apple(CPUState
*cs
, GByteArray
*buf
, int n
)
154 CPUPPCState
*env
= cpu_env(cs
);
156 int r
= ppc_gdb_register_len_apple(n
);
164 gdb_get_reg64(buf
, env
->gpr
[n
]);
167 gdb_get_reg64(buf
, *cpu_fpr_ptr(env
, n
- 32));
170 gdb_get_reg64(buf
, n
- 64);
171 gdb_get_reg64(buf
, 0);
175 gdb_get_reg64(buf
, env
->nip
);
178 gdb_get_reg64(buf
, env
->msr
);
182 uint32_t cr
= ppc_get_cr(env
);
183 gdb_get_reg32(buf
, cr
);
187 gdb_get_reg64(buf
, env
->lr
);
190 gdb_get_reg64(buf
, env
->ctr
);
193 gdb_get_reg32(buf
, cpu_read_xer(env
));
196 gdb_get_reg64(buf
, env
->fpscr
);
200 mem_buf
= buf
->data
+ buf
->len
- r
;
201 ppc_maybe_bswap_register(env
, mem_buf
, r
);
205 int ppc_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
207 CPUPPCState
*env
= cpu_env(cs
);
208 int r
= ppc_gdb_register_len(n
);
213 ppc_maybe_bswap_register(env
, mem_buf
, r
);
216 env
->gpr
[n
] = ldtul_p(mem_buf
);
219 *cpu_fpr_ptr(env
, n
- 32) = ldq_p(mem_buf
);
223 env
->nip
= ldtul_p(mem_buf
);
226 ppc_store_msr(env
, ldtul_p(mem_buf
));
230 uint32_t cr
= ldl_p(mem_buf
);
235 env
->lr
= ldtul_p(mem_buf
);
238 env
->ctr
= ldtul_p(mem_buf
);
241 cpu_write_xer(env
, ldl_p(mem_buf
));
245 ppc_store_fpscr(env
, ldtul_p(mem_buf
));
251 int ppc_cpu_gdb_write_register_apple(CPUState
*cs
, uint8_t *mem_buf
, int n
)
253 CPUPPCState
*env
= cpu_env(cs
);
254 int r
= ppc_gdb_register_len_apple(n
);
259 ppc_maybe_bswap_register(env
, mem_buf
, r
);
262 env
->gpr
[n
] = ldq_p(mem_buf
);
265 *cpu_fpr_ptr(env
, n
- 32) = ldq_p(mem_buf
);
269 env
->nip
= ldq_p(mem_buf
);
272 ppc_store_msr(env
, ldq_p(mem_buf
));
276 uint32_t cr
= ldl_p(mem_buf
);
281 env
->lr
= ldq_p(mem_buf
);
284 env
->ctr
= ldq_p(mem_buf
);
287 cpu_write_xer(env
, ldl_p(mem_buf
));
291 ppc_store_fpscr(env
, ldq_p(mem_buf
));
298 #ifndef CONFIG_USER_ONLY
299 static void gdb_gen_spr_feature(CPUState
*cs
)
301 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cs
);
302 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
303 CPUPPCState
*env
= &cpu
->env
;
304 GDBFeatureBuilder builder
;
305 unsigned int num_regs
= 0;
308 for (i
= 0; i
< ARRAY_SIZE(env
->spr_cb
); i
++) {
309 ppc_spr_t
*spr
= &env
->spr_cb
[i
];
316 * GDB identifies registers based on the order they are
317 * presented in the XML. These ids will not match QEMU's
318 * representation (which follows the PowerISA).
320 * Store the position of the current register description so
321 * we can make the correspondence later.
323 spr
->gdb_id
= num_regs
;
327 if (pcc
->gdb_spr
.xml
) {
331 gdb_feature_builder_init(&builder
, &pcc
->gdb_spr
,
332 "org.qemu.power.spr", "power-spr.xml",
335 for (i
= 0; i
< ARRAY_SIZE(env
->spr_cb
); i
++) {
336 ppc_spr_t
*spr
= &env
->spr_cb
[i
];
342 gdb_feature_builder_append_reg(&builder
, g_ascii_strdown(spr
->name
, -1),
343 TARGET_LONG_BITS
, spr
->gdb_id
,
347 gdb_feature_builder_end(&builder
);
351 #if !defined(CONFIG_USER_ONLY)
352 static int gdb_find_spr_idx(CPUPPCState
*env
, int n
)
356 for (i
= 0; i
< ARRAY_SIZE(env
->spr_cb
); i
++) {
357 ppc_spr_t
*spr
= &env
->spr_cb
[i
];
359 if (spr
->name
&& spr
->gdb_id
== n
) {
366 static int gdb_get_spr_reg(CPUState
*cs
, GByteArray
*buf
, int n
)
368 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
369 CPUPPCState
*env
= &cpu
->env
;
373 reg
= gdb_find_spr_idx(env
, n
);
378 len
= TARGET_LONG_SIZE
;
380 /* Handle those SPRs that are not part of the env->spr[] array */
383 #if defined(TARGET_PPC64)
389 val
= cpu_ppc_load_hdecr(env
);
392 val
= cpu_ppc_load_tbl(env
);
395 val
= cpu_ppc_load_tbu(env
);
398 val
= cpu_ppc_load_decr(env
);
403 gdb_get_regl(buf
, val
);
405 ppc_maybe_bswap_register(env
, gdb_get_reg_ptr(buf
, len
), len
);
409 static int gdb_set_spr_reg(CPUState
*cs
, uint8_t *mem_buf
, int n
)
411 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
412 CPUPPCState
*env
= &cpu
->env
;
416 reg
= gdb_find_spr_idx(env
, n
);
421 len
= TARGET_LONG_SIZE
;
422 ppc_maybe_bswap_register(env
, mem_buf
, len
);
424 /* Handle those SPRs that are not part of the env->spr[] array */
425 target_ulong val
= ldn_p(mem_buf
, len
);
427 #if defined(TARGET_PPC64)
440 static int gdb_get_float_reg(CPUState
*cs
, GByteArray
*buf
, int n
)
442 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
443 CPUPPCState
*env
= &cpu
->env
;
446 gdb_get_reg64(buf
, *cpu_fpr_ptr(env
, n
));
447 mem_buf
= gdb_get_reg_ptr(buf
, 8);
448 ppc_maybe_bswap_register(env
, mem_buf
, 8);
452 gdb_get_reg32(buf
, env
->fpscr
);
453 mem_buf
= gdb_get_reg_ptr(buf
, 4);
454 ppc_maybe_bswap_register(env
, mem_buf
, 4);
460 static int gdb_set_float_reg(CPUState
*cs
, uint8_t *mem_buf
, int n
)
462 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
463 CPUPPCState
*env
= &cpu
->env
;
466 ppc_maybe_bswap_register(env
, mem_buf
, 8);
467 *cpu_fpr_ptr(env
, n
) = ldq_p(mem_buf
);
471 ppc_maybe_bswap_register(env
, mem_buf
, 4);
472 ppc_store_fpscr(env
, ldl_p(mem_buf
));
478 static int gdb_get_avr_reg(CPUState
*cs
, GByteArray
*buf
, int n
)
480 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
481 CPUPPCState
*env
= &cpu
->env
;
485 ppc_avr_t
*avr
= cpu_avr_ptr(env
, n
);
486 gdb_get_reg128(buf
, avr
->VsrD(0), avr
->VsrD(1));
487 mem_buf
= gdb_get_reg_ptr(buf
, 16);
488 ppc_maybe_bswap_register(env
, mem_buf
, 16);
492 gdb_get_reg32(buf
, ppc_get_vscr(env
));
493 mem_buf
= gdb_get_reg_ptr(buf
, 4);
494 ppc_maybe_bswap_register(env
, mem_buf
, 4);
498 gdb_get_reg32(buf
, (uint32_t)env
->spr
[SPR_VRSAVE
]);
499 mem_buf
= gdb_get_reg_ptr(buf
, 4);
500 ppc_maybe_bswap_register(env
, mem_buf
, 4);
506 static int gdb_set_avr_reg(CPUState
*cs
, uint8_t *mem_buf
, int n
)
508 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
509 CPUPPCState
*env
= &cpu
->env
;
512 ppc_avr_t
*avr
= cpu_avr_ptr(env
, n
);
513 ppc_maybe_bswap_register(env
, mem_buf
, 16);
514 avr
->VsrD(0) = ldq_p(mem_buf
);
515 avr
->VsrD(1) = ldq_p(mem_buf
+ 8);
519 ppc_maybe_bswap_register(env
, mem_buf
, 4);
520 ppc_store_vscr(env
, ldl_p(mem_buf
));
524 ppc_maybe_bswap_register(env
, mem_buf
, 4);
525 env
->spr
[SPR_VRSAVE
] = (target_ulong
)ldl_p(mem_buf
);
531 static int gdb_get_spe_reg(CPUState
*cs
, GByteArray
*buf
, int n
)
533 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
534 CPUPPCState
*env
= &cpu
->env
;
537 #if defined(TARGET_PPC64)
538 gdb_get_reg32(buf
, env
->gpr
[n
] >> 32);
539 ppc_maybe_bswap_register(env
, gdb_get_reg_ptr(buf
, 4), 4);
541 gdb_get_reg32(buf
, env
->gprh
[n
]);
546 gdb_get_reg64(buf
, env
->spe_acc
);
547 ppc_maybe_bswap_register(env
, gdb_get_reg_ptr(buf
, 8), 8);
551 gdb_get_reg32(buf
, env
->spe_fscr
);
552 ppc_maybe_bswap_register(env
, gdb_get_reg_ptr(buf
, 4), 4);
558 static int gdb_set_spe_reg(CPUState
*cs
, uint8_t *mem_buf
, int n
)
560 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
561 CPUPPCState
*env
= &cpu
->env
;
564 #if defined(TARGET_PPC64)
565 target_ulong lo
= (uint32_t)env
->gpr
[n
];
568 ppc_maybe_bswap_register(env
, mem_buf
, 4);
570 hi
= (target_ulong
)ldl_p(mem_buf
) << 32;
571 env
->gpr
[n
] = lo
| hi
;
573 env
->gprh
[n
] = ldl_p(mem_buf
);
578 ppc_maybe_bswap_register(env
, mem_buf
, 8);
579 env
->spe_acc
= ldq_p(mem_buf
);
583 ppc_maybe_bswap_register(env
, mem_buf
, 4);
584 env
->spe_fscr
= ldl_p(mem_buf
);
590 static int gdb_get_vsx_reg(CPUState
*cs
, GByteArray
*buf
, int n
)
592 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
593 CPUPPCState
*env
= &cpu
->env
;
596 gdb_get_reg64(buf
, *cpu_vsrl_ptr(env
, n
));
597 ppc_maybe_bswap_register(env
, gdb_get_reg_ptr(buf
, 8), 8);
603 static int gdb_set_vsx_reg(CPUState
*cs
, uint8_t *mem_buf
, int n
)
605 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
606 CPUPPCState
*env
= &cpu
->env
;
609 ppc_maybe_bswap_register(env
, mem_buf
, 8);
610 *cpu_vsrl_ptr(env
, n
) = ldq_p(mem_buf
);
616 const gchar
*ppc_gdb_arch_name(CPUState
*cs
)
618 #if defined(TARGET_PPC64)
619 return "powerpc:common64";
621 return "powerpc:common";
625 void ppc_gdb_init(CPUState
*cs
, PowerPCCPUClass
*pcc
)
627 if (pcc
->insns_flags
& PPC_FLOAT
) {
628 gdb_register_coprocessor(cs
, gdb_get_float_reg
, gdb_set_float_reg
,
629 gdb_find_static_feature("power-fpu.xml"), 0);
631 if (pcc
->insns_flags
& PPC_ALTIVEC
) {
632 gdb_register_coprocessor(cs
, gdb_get_avr_reg
, gdb_set_avr_reg
,
633 gdb_find_static_feature("power-altivec.xml"),
636 if (pcc
->insns_flags
& PPC_SPE
) {
637 gdb_register_coprocessor(cs
, gdb_get_spe_reg
, gdb_set_spe_reg
,
638 gdb_find_static_feature("power-spe.xml"), 0);
640 if (pcc
->insns_flags2
& PPC2_VSX
) {
641 gdb_register_coprocessor(cs
, gdb_get_vsx_reg
, gdb_set_vsx_reg
,
642 gdb_find_static_feature("power-vsx.xml"), 0);
644 #ifndef CONFIG_USER_ONLY
645 gdb_gen_spr_feature(cs
);
646 gdb_register_coprocessor(cs
, gdb_get_spr_reg
, gdb_set_spr_reg
,