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 "internals.h"
26 typedef struct RegisterSysregXmlParam
{
30 } RegisterSysregXmlParam
;
32 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
33 whatever the target description contains. Due to a historical mishap
34 the FPA registers appear in between core integer regs and the CPSR.
35 We hack round this by giving the FPA regs zero size when talking to a
38 int arm_cpu_gdb_read_register(CPUState
*cs
, GByteArray
*mem_buf
, int n
)
40 ARMCPU
*cpu
= ARM_CPU(cs
);
41 CPUARMState
*env
= &cpu
->env
;
44 /* Core integer register. */
45 return gdb_get_reg32(mem_buf
, env
->regs
[n
]);
52 return gdb_get_zeroes(mem_buf
, 12);
56 /* FPA status register. */
60 return gdb_get_reg32(mem_buf
, 0);
62 /* CPSR, or XPSR for M-profile */
63 if (arm_feature(env
, ARM_FEATURE_M
)) {
64 return gdb_get_reg32(mem_buf
, xpsr_read(env
));
66 return gdb_get_reg32(mem_buf
, cpsr_read(env
));
69 /* Unknown register. */
73 int arm_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
75 ARMCPU
*cpu
= ARM_CPU(cs
);
76 CPUARMState
*env
= &cpu
->env
;
82 * Mask out low bits of PC to workaround gdb bugs.
83 * This avoids an assert in thumb_tr_translate_insn, because it is
84 * architecturally impossible to misalign the pc.
85 * This will probably cause problems if we ever implement the
86 * Jazelle DBX extensions.
93 /* Core integer register. */
94 if (n
== 13 && arm_feature(env
, ARM_FEATURE_M
)) {
95 /* M profile SP low bits are always 0 */
101 if (n
< 24) { /* 16-23 */
102 /* FPA registers (ignored). */
110 /* FPA status register (ignored). */
116 /* CPSR, or XPSR for M-profile */
117 if (arm_feature(env
, ARM_FEATURE_M
)) {
119 * Don't allow writing to XPSR.Exception as it can cause
120 * a transition into or out of handler mode (it's not
121 * writable via the MSR insn so this is a reasonable
122 * restriction). Other fields are safe to update.
124 xpsr_write(env
, tmp
, ~XPSR_EXCP
);
126 cpsr_write(env
, tmp
, 0xffffffff, CPSRWriteByGDBStub
);
130 /* Unknown register. */
134 static int vfp_gdb_get_reg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
136 ARMCPU
*cpu
= env_archcpu(env
);
137 int nregs
= cpu_isar_feature(aa32_simd_r32
, cpu
) ? 32 : 16;
139 /* VFP data registers are always little-endian. */
141 return gdb_get_reg64(buf
, *aa32_vfp_dreg(env
, reg
));
143 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
144 /* Aliases for Q regs. */
147 uint64_t *q
= aa32_vfp_qreg(env
, reg
- 32);
148 return gdb_get_reg128(buf
, q
[0], q
[1]);
151 switch (reg
- nregs
) {
153 return gdb_get_reg32(buf
, vfp_get_fpscr(env
));
158 static int vfp_gdb_set_reg(CPUARMState
*env
, uint8_t *buf
, int reg
)
160 ARMCPU
*cpu
= env_archcpu(env
);
161 int nregs
= cpu_isar_feature(aa32_simd_r32
, cpu
) ? 32 : 16;
164 *aa32_vfp_dreg(env
, reg
) = ldq_le_p(buf
);
167 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
170 uint64_t *q
= aa32_vfp_qreg(env
, reg
- 32);
171 q
[0] = ldq_le_p(buf
);
172 q
[1] = ldq_le_p(buf
+ 8);
176 switch (reg
- nregs
) {
178 vfp_set_fpscr(env
, ldl_p(buf
));
184 static int vfp_gdb_get_sysreg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
188 return gdb_get_reg32(buf
, env
->vfp
.xregs
[ARM_VFP_FPSID
]);
190 return gdb_get_reg32(buf
, env
->vfp
.xregs
[ARM_VFP_FPEXC
]);
195 static int vfp_gdb_set_sysreg(CPUARMState
*env
, uint8_t *buf
, int reg
)
199 env
->vfp
.xregs
[ARM_VFP_FPSID
] = ldl_p(buf
);
202 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = ldl_p(buf
) & (1 << 30);
208 static int mve_gdb_get_reg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
212 return gdb_get_reg32(buf
, env
->v7m
.vpr
);
218 static int mve_gdb_set_reg(CPUARMState
*env
, uint8_t *buf
, int reg
)
222 env
->v7m
.vpr
= ldl_p(buf
);
230 * arm_get/set_gdb_*: get/set a gdb register
231 * @env: the CPU state
232 * @buf: a buffer to copy to/from
233 * @reg: register number (offset from start of group)
235 * We return the number of bytes copied
238 static int arm_gdb_get_sysreg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
240 ARMCPU
*cpu
= env_archcpu(env
);
241 const ARMCPRegInfo
*ri
;
244 key
= cpu
->dyn_sysreg_xml
.data
.cpregs
.keys
[reg
];
245 ri
= get_arm_cp_reginfo(cpu
->cp_regs
, key
);
247 if (cpreg_field_is_64bit(ri
)) {
248 return gdb_get_reg64(buf
, (uint64_t)read_raw_cp_reg(env
, ri
));
250 return gdb_get_reg32(buf
, (uint32_t)read_raw_cp_reg(env
, ri
));
256 static int arm_gdb_set_sysreg(CPUARMState
*env
, uint8_t *buf
, int reg
)
261 static void arm_gen_one_xml_sysreg_tag(GString
*s
, DynamicGDBXMLInfo
*dyn_xml
,
262 ARMCPRegInfo
*ri
, uint32_t ri_key
,
263 int bitsize
, int regnum
)
265 g_string_append_printf(s
, "<reg name=\"%s\"", ri
->name
);
266 g_string_append_printf(s
, " bitsize=\"%d\"", bitsize
);
267 g_string_append_printf(s
, " regnum=\"%d\"", regnum
);
268 g_string_append_printf(s
, " group=\"cp_regs\"/>");
269 dyn_xml
->data
.cpregs
.keys
[dyn_xml
->num
] = ri_key
;
273 static void arm_register_sysreg_for_xml(gpointer key
, gpointer value
,
276 uint32_t ri_key
= (uintptr_t)key
;
277 ARMCPRegInfo
*ri
= value
;
278 RegisterSysregXmlParam
*param
= (RegisterSysregXmlParam
*)p
;
279 GString
*s
= param
->s
;
280 ARMCPU
*cpu
= ARM_CPU(param
->cs
);
281 CPUARMState
*env
= &cpu
->env
;
282 DynamicGDBXMLInfo
*dyn_xml
= &cpu
->dyn_sysreg_xml
;
284 if (!(ri
->type
& (ARM_CP_NO_RAW
| ARM_CP_NO_GDB
))) {
285 if (arm_feature(env
, ARM_FEATURE_AARCH64
)) {
286 if (ri
->state
== ARM_CP_STATE_AA64
) {
287 arm_gen_one_xml_sysreg_tag(s
, dyn_xml
, ri
, ri_key
, 64,
291 if (ri
->state
== ARM_CP_STATE_AA32
) {
292 if (!arm_feature(env
, ARM_FEATURE_EL3
) &&
293 (ri
->secure
& ARM_CP_SECSTATE_S
)) {
296 if (ri
->type
& ARM_CP_64BIT
) {
297 arm_gen_one_xml_sysreg_tag(s
, dyn_xml
, ri
, ri_key
, 64,
300 arm_gen_one_xml_sysreg_tag(s
, dyn_xml
, ri
, ri_key
, 32,
308 int arm_gen_dynamic_sysreg_xml(CPUState
*cs
, int base_reg
)
310 ARMCPU
*cpu
= ARM_CPU(cs
);
311 GString
*s
= g_string_new(NULL
);
312 RegisterSysregXmlParam param
= {cs
, s
, base_reg
};
314 cpu
->dyn_sysreg_xml
.num
= 0;
315 cpu
->dyn_sysreg_xml
.data
.cpregs
.keys
= g_new(uint32_t, g_hash_table_size(cpu
->cp_regs
));
316 g_string_printf(s
, "<?xml version=\"1.0\"?>");
317 g_string_append_printf(s
, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
318 g_string_append_printf(s
, "<feature name=\"org.qemu.gdb.arm.sys.regs\">");
319 g_hash_table_foreach(cpu
->cp_regs
, arm_register_sysreg_for_xml
, ¶m
);
320 g_string_append_printf(s
, "</feature>");
321 cpu
->dyn_sysreg_xml
.desc
= g_string_free(s
, false);
322 return cpu
->dyn_sysreg_xml
.num
;
326 const char *gdb_type
;
328 const char sz
, suffix
;
331 static const struct TypeSize vec_lanes
[] = {
333 { "uint128", 128, 'q', 'u' },
334 { "int128", 128, 'q', 's' },
336 { "ieee_double", 64, 'd', 'f' },
337 { "uint64", 64, 'd', 'u' },
338 { "int64", 64, 'd', 's' },
340 { "ieee_single", 32, 's', 'f' },
341 { "uint32", 32, 's', 'u' },
342 { "int32", 32, 's', 's' },
344 { "ieee_half", 16, 'h', 'f' },
345 { "uint16", 16, 'h', 'u' },
346 { "int16", 16, 'h', 's' },
348 { "uint8", 8, 'b', 'u' },
349 { "int8", 8, 'b', 's' },
353 int arm_gen_dynamic_svereg_xml(CPUState
*cs
, int base_reg
)
355 ARMCPU
*cpu
= ARM_CPU(cs
);
356 GString
*s
= g_string_new(NULL
);
357 DynamicGDBXMLInfo
*info
= &cpu
->dyn_svereg_xml
;
358 g_autoptr(GString
) ts
= g_string_new("");
359 int i
, j
, bits
, reg_width
= (cpu
->sve_max_vq
* 128);
361 g_string_printf(s
, "<?xml version=\"1.0\"?>");
362 g_string_append_printf(s
, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
363 g_string_append_printf(s
, "<feature name=\"org.gnu.gdb.aarch64.sve\">");
365 /* First define types and totals in a whole VL */
366 for (i
= 0; i
< ARRAY_SIZE(vec_lanes
); i
++) {
367 int count
= reg_width
/ vec_lanes
[i
].size
;
368 g_string_printf(ts
, "svev%c%c", vec_lanes
[i
].sz
, vec_lanes
[i
].suffix
);
369 g_string_append_printf(s
,
370 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>",
371 ts
->str
, vec_lanes
[i
].gdb_type
, count
);
374 * Now define a union for each size group containing unsigned and
375 * signed and potentially float versions of each size from 128 to
378 for (bits
= 128, i
= 0; bits
>= 8; bits
/= 2, i
++) {
379 const char suf
[] = { 'q', 'd', 's', 'h', 'b' };
380 g_string_append_printf(s
, "<union id=\"svevn%c\">", suf
[i
]);
381 for (j
= 0; j
< ARRAY_SIZE(vec_lanes
); j
++) {
382 if (vec_lanes
[j
].size
== bits
) {
383 g_string_append_printf(s
, "<field name=\"%c\" type=\"svev%c%c\"/>",
385 vec_lanes
[j
].sz
, vec_lanes
[j
].suffix
);
388 g_string_append(s
, "</union>");
390 /* And now the final union of unions */
391 g_string_append(s
, "<union id=\"svev\">");
392 for (bits
= 128, i
= 0; bits
>= 8; bits
/= 2, i
++) {
393 const char suf
[] = { 'q', 'd', 's', 'h', 'b' };
394 g_string_append_printf(s
, "<field name=\"%c\" type=\"svevn%c\"/>",
397 g_string_append(s
, "</union>");
399 /* Finally the sve prefix type */
400 g_string_append_printf(s
,
401 "<vector id=\"svep\" type=\"uint8\" count=\"%d\"/>",
404 /* Then define each register in parts for each vq */
405 for (i
= 0; i
< 32; i
++) {
406 g_string_append_printf(s
,
407 "<reg name=\"z%d\" bitsize=\"%d\""
408 " regnum=\"%d\" type=\"svev\"/>",
409 i
, reg_width
, base_reg
++);
412 /* fpscr & status registers */
413 g_string_append_printf(s
, "<reg name=\"fpsr\" bitsize=\"32\""
414 " regnum=\"%d\" group=\"float\""
415 " type=\"int\"/>", base_reg
++);
416 g_string_append_printf(s
, "<reg name=\"fpcr\" bitsize=\"32\""
417 " regnum=\"%d\" group=\"float\""
418 " type=\"int\"/>", base_reg
++);
421 for (i
= 0; i
< 16; i
++) {
422 g_string_append_printf(s
,
423 "<reg name=\"p%d\" bitsize=\"%d\""
424 " regnum=\"%d\" type=\"svep\"/>",
425 i
, cpu
->sve_max_vq
* 16, base_reg
++);
428 g_string_append_printf(s
,
429 "<reg name=\"ffr\" bitsize=\"%d\""
430 " regnum=\"%d\" group=\"vector\""
432 cpu
->sve_max_vq
* 16, base_reg
++);
433 g_string_append_printf(s
,
434 "<reg name=\"vg\" bitsize=\"64\""
435 " regnum=\"%d\" type=\"int\"/>",
438 g_string_append_printf(s
, "</feature>");
439 cpu
->dyn_svereg_xml
.desc
= g_string_free(s
, false);
441 return cpu
->dyn_svereg_xml
.num
;
445 const char *arm_gdb_get_dynamic_xml(CPUState
*cs
, const char *xmlname
)
447 ARMCPU
*cpu
= ARM_CPU(cs
);
449 if (strcmp(xmlname
, "system-registers.xml") == 0) {
450 return cpu
->dyn_sysreg_xml
.desc
;
451 } else if (strcmp(xmlname
, "sve-registers.xml") == 0) {
452 return cpu
->dyn_svereg_xml
.desc
;
457 void arm_cpu_register_gdb_regs_for_features(ARMCPU
*cpu
)
459 CPUState
*cs
= CPU(cpu
);
460 CPUARMState
*env
= &cpu
->env
;
462 if (arm_feature(env
, ARM_FEATURE_AARCH64
)) {
464 * The lower part of each SVE register aliases to the FPU
465 * registers so we don't need to include both.
467 #ifdef TARGET_AARCH64
468 if (isar_feature_aa64_sve(&cpu
->isar
)) {
469 gdb_register_coprocessor(cs
, arm_gdb_get_svereg
, arm_gdb_set_svereg
,
470 arm_gen_dynamic_svereg_xml(cs
, cs
->gdb_num_regs
),
471 "sve-registers.xml", 0);
473 gdb_register_coprocessor(cs
, aarch64_fpu_gdb_get_reg
,
474 aarch64_fpu_gdb_set_reg
,
475 34, "aarch64-fpu.xml", 0);
479 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
480 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
481 49, "arm-neon.xml", 0);
482 } else if (cpu_isar_feature(aa32_simd_r32
, cpu
)) {
483 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
484 33, "arm-vfp3.xml", 0);
485 } else if (cpu_isar_feature(aa32_vfp_simd
, cpu
)) {
486 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
487 17, "arm-vfp.xml", 0);
489 if (!arm_feature(env
, ARM_FEATURE_M
)) {
491 * A and R profile have FP sysregs FPEXC and FPSID that we
494 gdb_register_coprocessor(cs
, vfp_gdb_get_sysreg
, vfp_gdb_set_sysreg
,
495 2, "arm-vfp-sysregs.xml", 0);
498 if (cpu_isar_feature(aa32_mve
, cpu
)) {
499 gdb_register_coprocessor(cs
, mve_gdb_get_reg
, mve_gdb_set_reg
,
500 1, "arm-m-profile-mve.xml", 0);
502 gdb_register_coprocessor(cs
, arm_gdb_get_sysreg
, arm_gdb_set_sysreg
,
503 arm_gen_dynamic_sysreg_xml(cs
, cs
->gdb_num_regs
),
504 "system-registers.xml", 0);