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 "internals.h"
23 #include "exec/gdbstub.h"
25 typedef struct RegisterSysregXmlParam
{
29 } RegisterSysregXmlParam
;
31 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
32 whatever the target description contains. Due to a historical mishap
33 the FPA registers appear in between core integer regs and the CPSR.
34 We hack round this by giving the FPA regs zero size when talking to a
37 int arm_cpu_gdb_read_register(CPUState
*cs
, GByteArray
*mem_buf
, int n
)
39 ARMCPU
*cpu
= ARM_CPU(cs
);
40 CPUARMState
*env
= &cpu
->env
;
43 /* Core integer register. */
44 return gdb_get_reg32(mem_buf
, env
->regs
[n
]);
51 return gdb_get_zeroes(mem_buf
, 12);
55 /* FPA status register. */
59 return gdb_get_reg32(mem_buf
, 0);
61 /* CPSR, or XPSR for M-profile */
62 if (arm_feature(env
, ARM_FEATURE_M
)) {
63 return gdb_get_reg32(mem_buf
, xpsr_read(env
));
65 return gdb_get_reg32(mem_buf
, cpsr_read(env
));
68 /* Unknown register. */
72 int arm_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
74 ARMCPU
*cpu
= ARM_CPU(cs
);
75 CPUARMState
*env
= &cpu
->env
;
80 /* Mask out low bit of PC to workaround gdb bugs. This will probably
81 cause problems if we ever implement the Jazelle DBX extensions. */
87 /* Core integer register. */
88 if (n
== 13 && arm_feature(env
, ARM_FEATURE_M
)) {
89 /* M profile SP low bits are always 0 */
95 if (n
< 24) { /* 16-23 */
96 /* FPA registers (ignored). */
104 /* FPA status register (ignored). */
110 /* CPSR, or XPSR for M-profile */
111 if (arm_feature(env
, ARM_FEATURE_M
)) {
113 * Don't allow writing to XPSR.Exception as it can cause
114 * a transition into or out of handler mode (it's not
115 * writeable via the MSR insn so this is a reasonable
116 * restriction). Other fields are safe to update.
118 xpsr_write(env
, tmp
, ~XPSR_EXCP
);
120 cpsr_write(env
, tmp
, 0xffffffff, CPSRWriteByGDBStub
);
124 /* Unknown register. */
128 static int vfp_gdb_get_reg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
130 ARMCPU
*cpu
= env_archcpu(env
);
131 int nregs
= cpu_isar_feature(aa32_simd_r32
, cpu
) ? 32 : 16;
133 /* VFP data registers are always little-endian. */
135 return gdb_get_reg64(buf
, *aa32_vfp_dreg(env
, reg
));
137 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
138 /* Aliases for Q regs. */
141 uint64_t *q
= aa32_vfp_qreg(env
, reg
- 32);
142 return gdb_get_reg128(buf
, q
[0], q
[1]);
145 switch (reg
- nregs
) {
147 return gdb_get_reg32(buf
, vfp_get_fpscr(env
));
152 static int vfp_gdb_set_reg(CPUARMState
*env
, uint8_t *buf
, int reg
)
154 ARMCPU
*cpu
= env_archcpu(env
);
155 int nregs
= cpu_isar_feature(aa32_simd_r32
, cpu
) ? 32 : 16;
158 *aa32_vfp_dreg(env
, reg
) = ldq_le_p(buf
);
161 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
164 uint64_t *q
= aa32_vfp_qreg(env
, reg
- 32);
165 q
[0] = ldq_le_p(buf
);
166 q
[1] = ldq_le_p(buf
+ 8);
170 switch (reg
- nregs
) {
172 vfp_set_fpscr(env
, ldl_p(buf
));
178 static int vfp_gdb_get_sysreg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
182 return gdb_get_reg32(buf
, env
->vfp
.xregs
[ARM_VFP_FPSID
]);
184 return gdb_get_reg32(buf
, env
->vfp
.xregs
[ARM_VFP_FPEXC
]);
189 static int vfp_gdb_set_sysreg(CPUARMState
*env
, uint8_t *buf
, int reg
)
193 env
->vfp
.xregs
[ARM_VFP_FPSID
] = ldl_p(buf
);
196 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = ldl_p(buf
) & (1 << 30);
203 * arm_get/set_gdb_*: get/set a gdb register
204 * @env: the CPU state
205 * @buf: a buffer to copy to/from
206 * @reg: register number (offset from start of group)
208 * We return the number of bytes copied
211 static int arm_gdb_get_sysreg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
213 ARMCPU
*cpu
= env_archcpu(env
);
214 const ARMCPRegInfo
*ri
;
217 key
= cpu
->dyn_sysreg_xml
.data
.cpregs
.keys
[reg
];
218 ri
= get_arm_cp_reginfo(cpu
->cp_regs
, key
);
220 if (cpreg_field_is_64bit(ri
)) {
221 return gdb_get_reg64(buf
, (uint64_t)read_raw_cp_reg(env
, ri
));
223 return gdb_get_reg32(buf
, (uint32_t)read_raw_cp_reg(env
, ri
));
229 static int arm_gdb_set_sysreg(CPUARMState
*env
, uint8_t *buf
, int reg
)
234 static void arm_gen_one_xml_sysreg_tag(GString
*s
, DynamicGDBXMLInfo
*dyn_xml
,
235 ARMCPRegInfo
*ri
, uint32_t ri_key
,
236 int bitsize
, int regnum
)
238 g_string_append_printf(s
, "<reg name=\"%s\"", ri
->name
);
239 g_string_append_printf(s
, " bitsize=\"%d\"", bitsize
);
240 g_string_append_printf(s
, " regnum=\"%d\"", regnum
);
241 g_string_append_printf(s
, " group=\"cp_regs\"/>");
242 dyn_xml
->data
.cpregs
.keys
[dyn_xml
->num
] = ri_key
;
246 static void arm_register_sysreg_for_xml(gpointer key
, gpointer value
,
249 uint32_t ri_key
= *(uint32_t *)key
;
250 ARMCPRegInfo
*ri
= value
;
251 RegisterSysregXmlParam
*param
= (RegisterSysregXmlParam
*)p
;
252 GString
*s
= param
->s
;
253 ARMCPU
*cpu
= ARM_CPU(param
->cs
);
254 CPUARMState
*env
= &cpu
->env
;
255 DynamicGDBXMLInfo
*dyn_xml
= &cpu
->dyn_sysreg_xml
;
257 if (!(ri
->type
& (ARM_CP_NO_RAW
| ARM_CP_NO_GDB
))) {
258 if (arm_feature(env
, ARM_FEATURE_AARCH64
)) {
259 if (ri
->state
== ARM_CP_STATE_AA64
) {
260 arm_gen_one_xml_sysreg_tag(s
, dyn_xml
, ri
, ri_key
, 64,
264 if (ri
->state
== ARM_CP_STATE_AA32
) {
265 if (!arm_feature(env
, ARM_FEATURE_EL3
) &&
266 (ri
->secure
& ARM_CP_SECSTATE_S
)) {
269 if (ri
->type
& ARM_CP_64BIT
) {
270 arm_gen_one_xml_sysreg_tag(s
, dyn_xml
, ri
, ri_key
, 64,
273 arm_gen_one_xml_sysreg_tag(s
, dyn_xml
, ri
, ri_key
, 32,
281 int arm_gen_dynamic_sysreg_xml(CPUState
*cs
, int base_reg
)
283 ARMCPU
*cpu
= ARM_CPU(cs
);
284 GString
*s
= g_string_new(NULL
);
285 RegisterSysregXmlParam param
= {cs
, s
, base_reg
};
287 cpu
->dyn_sysreg_xml
.num
= 0;
288 cpu
->dyn_sysreg_xml
.data
.cpregs
.keys
= g_new(uint32_t, g_hash_table_size(cpu
->cp_regs
));
289 g_string_printf(s
, "<?xml version=\"1.0\"?>");
290 g_string_append_printf(s
, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
291 g_string_append_printf(s
, "<feature name=\"org.qemu.gdb.arm.sys.regs\">");
292 g_hash_table_foreach(cpu
->cp_regs
, arm_register_sysreg_for_xml
, ¶m
);
293 g_string_append_printf(s
, "</feature>");
294 cpu
->dyn_sysreg_xml
.desc
= g_string_free(s
, false);
295 return cpu
->dyn_sysreg_xml
.num
;
299 const char *gdb_type
;
301 const char sz
, suffix
;
304 static const struct TypeSize vec_lanes
[] = {
306 { "uint128", 128, 'q', 'u' },
307 { "int128", 128, 'q', 's' },
309 { "ieee_double", 64, 'd', 'f' },
310 { "uint64", 64, 'd', 'u' },
311 { "int64", 64, 'd', 's' },
313 { "ieee_single", 32, 's', 'f' },
314 { "uint32", 32, 's', 'u' },
315 { "int32", 32, 's', 's' },
317 { "ieee_half", 16, 'h', 'f' },
318 { "uint16", 16, 'h', 'u' },
319 { "int16", 16, 'h', 's' },
321 { "uint8", 8, 'b', 'u' },
322 { "int8", 8, 'b', 's' },
326 int arm_gen_dynamic_svereg_xml(CPUState
*cs
, int base_reg
)
328 ARMCPU
*cpu
= ARM_CPU(cs
);
329 GString
*s
= g_string_new(NULL
);
330 DynamicGDBXMLInfo
*info
= &cpu
->dyn_svereg_xml
;
331 g_autoptr(GString
) ts
= g_string_new("");
332 int i
, j
, bits
, reg_width
= (cpu
->sve_max_vq
* 128);
334 g_string_printf(s
, "<?xml version=\"1.0\"?>");
335 g_string_append_printf(s
, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
336 g_string_append_printf(s
, "<feature name=\"org.gnu.gdb.aarch64.sve\">");
338 /* First define types and totals in a whole VL */
339 for (i
= 0; i
< ARRAY_SIZE(vec_lanes
); i
++) {
340 int count
= reg_width
/ vec_lanes
[i
].size
;
341 g_string_printf(ts
, "svev%c%c", vec_lanes
[i
].sz
, vec_lanes
[i
].suffix
);
342 g_string_append_printf(s
,
343 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>",
344 ts
->str
, vec_lanes
[i
].gdb_type
, count
);
347 * Now define a union for each size group containing unsigned and
348 * signed and potentially float versions of each size from 128 to
351 for (bits
= 128, i
= 0; bits
>= 8; bits
/= 2, i
++) {
352 const char suf
[] = { 'q', 'd', 's', 'h', 'b' };
353 g_string_append_printf(s
, "<union id=\"svevn%c\">", suf
[i
]);
354 for (j
= 0; j
< ARRAY_SIZE(vec_lanes
); j
++) {
355 if (vec_lanes
[j
].size
== bits
) {
356 g_string_append_printf(s
, "<field name=\"%c\" type=\"svev%c%c\"/>",
358 vec_lanes
[j
].sz
, vec_lanes
[j
].suffix
);
361 g_string_append(s
, "</union>");
363 /* And now the final union of unions */
364 g_string_append(s
, "<union id=\"svev\">");
365 for (bits
= 128, i
= 0; bits
>= 8; bits
/= 2, i
++) {
366 const char suf
[] = { 'q', 'd', 's', 'h', 'b' };
367 g_string_append_printf(s
, "<field name=\"%c\" type=\"svevn%c\"/>",
370 g_string_append(s
, "</union>");
372 /* Finally the sve prefix type */
373 g_string_append_printf(s
,
374 "<vector id=\"svep\" type=\"uint8\" count=\"%d\"/>",
377 /* Then define each register in parts for each vq */
378 for (i
= 0; i
< 32; i
++) {
379 g_string_append_printf(s
,
380 "<reg name=\"z%d\" bitsize=\"%d\""
381 " regnum=\"%d\" type=\"svev\"/>",
382 i
, reg_width
, base_reg
++);
385 /* fpscr & status registers */
386 g_string_append_printf(s
, "<reg name=\"fpsr\" bitsize=\"32\""
387 " regnum=\"%d\" group=\"float\""
388 " type=\"int\"/>", base_reg
++);
389 g_string_append_printf(s
, "<reg name=\"fpcr\" bitsize=\"32\""
390 " regnum=\"%d\" group=\"float\""
391 " type=\"int\"/>", base_reg
++);
394 for (i
= 0; i
< 16; i
++) {
395 g_string_append_printf(s
,
396 "<reg name=\"p%d\" bitsize=\"%d\""
397 " regnum=\"%d\" type=\"svep\"/>",
398 i
, cpu
->sve_max_vq
* 16, base_reg
++);
401 g_string_append_printf(s
,
402 "<reg name=\"ffr\" bitsize=\"%d\""
403 " regnum=\"%d\" group=\"vector\""
405 cpu
->sve_max_vq
* 16, base_reg
++);
406 g_string_append_printf(s
,
407 "<reg name=\"vg\" bitsize=\"64\""
408 " regnum=\"%d\" type=\"int\"/>",
411 g_string_append_printf(s
, "</feature>");
412 cpu
->dyn_svereg_xml
.desc
= g_string_free(s
, false);
414 return cpu
->dyn_svereg_xml
.num
;
418 const char *arm_gdb_get_dynamic_xml(CPUState
*cs
, const char *xmlname
)
420 ARMCPU
*cpu
= ARM_CPU(cs
);
422 if (strcmp(xmlname
, "system-registers.xml") == 0) {
423 return cpu
->dyn_sysreg_xml
.desc
;
424 } else if (strcmp(xmlname
, "sve-registers.xml") == 0) {
425 return cpu
->dyn_svereg_xml
.desc
;
430 void arm_cpu_register_gdb_regs_for_features(ARMCPU
*cpu
)
432 CPUState
*cs
= CPU(cpu
);
433 CPUARMState
*env
= &cpu
->env
;
435 if (arm_feature(env
, ARM_FEATURE_AARCH64
)) {
437 * The lower part of each SVE register aliases to the FPU
438 * registers so we don't need to include both.
440 #ifdef TARGET_AARCH64
441 if (isar_feature_aa64_sve(&cpu
->isar
)) {
442 gdb_register_coprocessor(cs
, arm_gdb_get_svereg
, arm_gdb_set_svereg
,
443 arm_gen_dynamic_svereg_xml(cs
, cs
->gdb_num_regs
),
444 "sve-registers.xml", 0);
446 gdb_register_coprocessor(cs
, aarch64_fpu_gdb_get_reg
,
447 aarch64_fpu_gdb_set_reg
,
448 34, "aarch64-fpu.xml", 0);
452 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
453 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
454 49, "arm-neon.xml", 0);
455 } else if (cpu_isar_feature(aa32_simd_r32
, cpu
)) {
456 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
457 33, "arm-vfp3.xml", 0);
458 } else if (cpu_isar_feature(aa32_vfp_simd
, cpu
)) {
459 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
460 17, "arm-vfp.xml", 0);
462 if (!arm_feature(env
, ARM_FEATURE_M
)) {
464 * A and R profile have FP sysregs FPEXC and FPSID that we
467 gdb_register_coprocessor(cs
, vfp_gdb_get_sysreg
, vfp_gdb_set_sysreg
,
468 2, "arm-vfp-sysregs.xml", 0);
471 gdb_register_coprocessor(cs
, arm_gdb_get_sysreg
, arm_gdb_set_sysreg
,
472 arm_gen_dynamic_sysreg_xml(cs
, cs
->gdb_num_regs
),
473 "system-registers.xml", 0);