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"
24 #include "sysemu/tcg.h"
25 #include "internals.h"
28 typedef struct RegisterSysregXmlParam
{
32 } RegisterSysregXmlParam
;
34 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
35 whatever the target description contains. Due to a historical mishap
36 the FPA registers appear in between core integer regs and the CPSR.
37 We hack round this by giving the FPA regs zero size when talking to a
40 int arm_cpu_gdb_read_register(CPUState
*cs
, GByteArray
*mem_buf
, int n
)
42 ARMCPU
*cpu
= ARM_CPU(cs
);
43 CPUARMState
*env
= &cpu
->env
;
46 /* Core integer register. */
47 return gdb_get_reg32(mem_buf
, env
->regs
[n
]);
54 return gdb_get_zeroes(mem_buf
, 12);
58 /* FPA status register. */
62 return gdb_get_reg32(mem_buf
, 0);
64 /* CPSR, or XPSR for M-profile */
65 if (arm_feature(env
, ARM_FEATURE_M
)) {
66 return gdb_get_reg32(mem_buf
, xpsr_read(env
));
68 return gdb_get_reg32(mem_buf
, cpsr_read(env
));
71 /* Unknown register. */
75 int arm_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
77 ARMCPU
*cpu
= ARM_CPU(cs
);
78 CPUARMState
*env
= &cpu
->env
;
84 * Mask out low bits of PC to workaround gdb bugs.
85 * This avoids an assert in thumb_tr_translate_insn, because it is
86 * architecturally impossible to misalign the pc.
87 * This will probably cause problems if we ever implement the
88 * Jazelle DBX extensions.
95 /* Core integer register. */
96 if (n
== 13 && arm_feature(env
, ARM_FEATURE_M
)) {
97 /* M profile SP low bits are always 0 */
103 if (n
< 24) { /* 16-23 */
104 /* FPA registers (ignored). */
112 /* FPA status register (ignored). */
118 /* CPSR, or XPSR for M-profile */
119 if (arm_feature(env
, ARM_FEATURE_M
)) {
121 * Don't allow writing to XPSR.Exception as it can cause
122 * a transition into or out of handler mode (it's not
123 * writable via the MSR insn so this is a reasonable
124 * restriction). Other fields are safe to update.
126 xpsr_write(env
, tmp
, ~XPSR_EXCP
);
128 cpsr_write(env
, tmp
, 0xffffffff, CPSRWriteByGDBStub
);
132 /* Unknown register. */
136 static int vfp_gdb_get_reg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
138 ARMCPU
*cpu
= env_archcpu(env
);
139 int nregs
= cpu_isar_feature(aa32_simd_r32
, cpu
) ? 32 : 16;
141 /* VFP data registers are always little-endian. */
143 return gdb_get_reg64(buf
, *aa32_vfp_dreg(env
, reg
));
145 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
146 /* Aliases for Q regs. */
149 uint64_t *q
= aa32_vfp_qreg(env
, reg
- 32);
150 return gdb_get_reg128(buf
, q
[0], q
[1]);
153 switch (reg
- nregs
) {
155 return gdb_get_reg32(buf
, vfp_get_fpscr(env
));
160 static int vfp_gdb_set_reg(CPUARMState
*env
, uint8_t *buf
, int reg
)
162 ARMCPU
*cpu
= env_archcpu(env
);
163 int nregs
= cpu_isar_feature(aa32_simd_r32
, cpu
) ? 32 : 16;
166 *aa32_vfp_dreg(env
, reg
) = ldq_le_p(buf
);
169 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
172 uint64_t *q
= aa32_vfp_qreg(env
, reg
- 32);
173 q
[0] = ldq_le_p(buf
);
174 q
[1] = ldq_le_p(buf
+ 8);
178 switch (reg
- nregs
) {
180 vfp_set_fpscr(env
, ldl_p(buf
));
186 static int vfp_gdb_get_sysreg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
190 return gdb_get_reg32(buf
, env
->vfp
.xregs
[ARM_VFP_FPSID
]);
192 return gdb_get_reg32(buf
, env
->vfp
.xregs
[ARM_VFP_FPEXC
]);
197 static int vfp_gdb_set_sysreg(CPUARMState
*env
, uint8_t *buf
, int reg
)
201 env
->vfp
.xregs
[ARM_VFP_FPSID
] = ldl_p(buf
);
204 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = ldl_p(buf
) & (1 << 30);
210 static int mve_gdb_get_reg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
214 return gdb_get_reg32(buf
, env
->v7m
.vpr
);
220 static int mve_gdb_set_reg(CPUARMState
*env
, uint8_t *buf
, int reg
)
224 env
->v7m
.vpr
= ldl_p(buf
);
232 * arm_get/set_gdb_*: get/set a gdb register
233 * @env: the CPU state
234 * @buf: a buffer to copy to/from
235 * @reg: register number (offset from start of group)
237 * We return the number of bytes copied
240 static int arm_gdb_get_sysreg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
242 ARMCPU
*cpu
= env_archcpu(env
);
243 const ARMCPRegInfo
*ri
;
246 key
= cpu
->dyn_sysreg_xml
.data
.cpregs
.keys
[reg
];
247 ri
= get_arm_cp_reginfo(cpu
->cp_regs
, key
);
249 if (cpreg_field_is_64bit(ri
)) {
250 return gdb_get_reg64(buf
, (uint64_t)read_raw_cp_reg(env
, ri
));
252 return gdb_get_reg32(buf
, (uint32_t)read_raw_cp_reg(env
, ri
));
258 static int arm_gdb_set_sysreg(CPUARMState
*env
, uint8_t *buf
, int reg
)
263 static void arm_gen_one_xml_sysreg_tag(GString
*s
, DynamicGDBXMLInfo
*dyn_xml
,
264 ARMCPRegInfo
*ri
, uint32_t ri_key
,
265 int bitsize
, int regnum
)
267 g_string_append_printf(s
, "<reg name=\"%s\"", ri
->name
);
268 g_string_append_printf(s
, " bitsize=\"%d\"", bitsize
);
269 g_string_append_printf(s
, " regnum=\"%d\"", regnum
);
270 g_string_append_printf(s
, " group=\"cp_regs\"/>");
271 dyn_xml
->data
.cpregs
.keys
[dyn_xml
->num
] = ri_key
;
275 static void arm_register_sysreg_for_xml(gpointer key
, gpointer value
,
278 uint32_t ri_key
= (uintptr_t)key
;
279 ARMCPRegInfo
*ri
= value
;
280 RegisterSysregXmlParam
*param
= (RegisterSysregXmlParam
*)p
;
281 GString
*s
= param
->s
;
282 ARMCPU
*cpu
= ARM_CPU(param
->cs
);
283 CPUARMState
*env
= &cpu
->env
;
284 DynamicGDBXMLInfo
*dyn_xml
= &cpu
->dyn_sysreg_xml
;
286 if (!(ri
->type
& (ARM_CP_NO_RAW
| ARM_CP_NO_GDB
))) {
287 if (arm_feature(env
, ARM_FEATURE_AARCH64
)) {
288 if (ri
->state
== ARM_CP_STATE_AA64
) {
289 arm_gen_one_xml_sysreg_tag(s
, dyn_xml
, ri
, ri_key
, 64,
293 if (ri
->state
== ARM_CP_STATE_AA32
) {
294 if (!arm_feature(env
, ARM_FEATURE_EL3
) &&
295 (ri
->secure
& ARM_CP_SECSTATE_S
)) {
298 if (ri
->type
& ARM_CP_64BIT
) {
299 arm_gen_one_xml_sysreg_tag(s
, dyn_xml
, ri
, ri_key
, 64,
302 arm_gen_one_xml_sysreg_tag(s
, dyn_xml
, ri
, ri_key
, 32,
310 static int arm_gen_dynamic_sysreg_xml(CPUState
*cs
, int base_reg
)
312 ARMCPU
*cpu
= ARM_CPU(cs
);
313 GString
*s
= g_string_new(NULL
);
314 RegisterSysregXmlParam param
= {cs
, s
, base_reg
};
316 cpu
->dyn_sysreg_xml
.num
= 0;
317 cpu
->dyn_sysreg_xml
.data
.cpregs
.keys
= g_new(uint32_t, g_hash_table_size(cpu
->cp_regs
));
318 g_string_printf(s
, "<?xml version=\"1.0\"?>");
319 g_string_append_printf(s
, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
320 g_string_append_printf(s
, "<feature name=\"org.qemu.gdb.arm.sys.regs\">");
321 g_hash_table_foreach(cpu
->cp_regs
, arm_register_sysreg_for_xml
, ¶m
);
322 g_string_append_printf(s
, "</feature>");
323 cpu
->dyn_sysreg_xml
.desc
= g_string_free(s
, false);
324 return cpu
->dyn_sysreg_xml
.num
;
338 static const struct {
342 [M_SYSREG_MSP
] = { "msp", ARM_FEATURE_M
},
343 [M_SYSREG_PSP
] = { "psp", ARM_FEATURE_M
},
344 [M_SYSREG_PRIMASK
] = { "primask", ARM_FEATURE_M
},
345 [M_SYSREG_CONTROL
] = { "control", ARM_FEATURE_M
},
346 [M_SYSREG_BASEPRI
] = { "basepri", ARM_FEATURE_M_MAIN
},
347 [M_SYSREG_FAULTMASK
] = { "faultmask", ARM_FEATURE_M_MAIN
},
348 [M_SYSREG_MSPLIM
] = { "msplim", ARM_FEATURE_V8
},
349 [M_SYSREG_PSPLIM
] = { "psplim", ARM_FEATURE_V8
},
352 static uint32_t *m_sysreg_ptr(CPUARMState
*env
, MProfileSysreg reg
, bool sec
)
358 ptr
= arm_v7m_get_sp_ptr(env
, sec
, false, true);
361 ptr
= arm_v7m_get_sp_ptr(env
, sec
, true, true);
363 case M_SYSREG_MSPLIM
:
364 ptr
= &env
->v7m
.msplim
[sec
];
366 case M_SYSREG_PSPLIM
:
367 ptr
= &env
->v7m
.psplim
[sec
];
369 case M_SYSREG_PRIMASK
:
370 ptr
= &env
->v7m
.primask
[sec
];
372 case M_SYSREG_BASEPRI
:
373 ptr
= &env
->v7m
.basepri
[sec
];
375 case M_SYSREG_FAULTMASK
:
376 ptr
= &env
->v7m
.faultmask
[sec
];
378 case M_SYSREG_CONTROL
:
379 ptr
= &env
->v7m
.control
[sec
];
384 return arm_feature(env
, m_sysreg_def
[reg
].feature
) ? ptr
: NULL
;
387 static int m_sysreg_get(CPUARMState
*env
, GByteArray
*buf
,
388 MProfileSysreg reg
, bool secure
)
390 uint32_t *ptr
= m_sysreg_ptr(env
, reg
, secure
);
395 return gdb_get_reg32(buf
, *ptr
);
398 static int arm_gdb_get_m_systemreg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
401 * Here, we emulate MRS instruction, where CONTROL has a mix of
402 * banked and non-banked bits.
404 if (reg
== M_SYSREG_CONTROL
) {
405 return gdb_get_reg32(buf
, arm_v7m_mrs_control(env
, env
->v7m
.secure
));
407 return m_sysreg_get(env
, buf
, reg
, env
->v7m
.secure
);
410 static int arm_gdb_set_m_systemreg(CPUARMState
*env
, uint8_t *buf
, int reg
)
415 static int arm_gen_dynamic_m_systemreg_xml(CPUState
*cs
, int orig_base_reg
)
417 ARMCPU
*cpu
= ARM_CPU(cs
);
418 CPUARMState
*env
= &cpu
->env
;
419 GString
*s
= g_string_new(NULL
);
420 int base_reg
= orig_base_reg
;
423 g_string_printf(s
, "<?xml version=\"1.0\"?>");
424 g_string_append_printf(s
, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
425 g_string_append_printf(s
, "<feature name=\"org.gnu.gdb.arm.m-system\">\n");
427 for (i
= 0; i
< ARRAY_SIZE(m_sysreg_def
); i
++) {
428 if (arm_feature(env
, m_sysreg_def
[i
].feature
)) {
429 g_string_append_printf(s
,
430 "<reg name=\"%s\" bitsize=\"32\" regnum=\"%d\"/>\n",
431 m_sysreg_def
[i
].name
, base_reg
++);
435 g_string_append_printf(s
, "</feature>");
436 cpu
->dyn_m_systemreg_xml
.desc
= g_string_free(s
, false);
437 cpu
->dyn_m_systemreg_xml
.num
= base_reg
- orig_base_reg
;
439 return cpu
->dyn_m_systemreg_xml
.num
;
442 #ifndef CONFIG_USER_ONLY
444 * For user-only, we see the non-secure registers via m_systemreg above.
445 * For secext, encode the non-secure view as even and secure view as odd.
447 static int arm_gdb_get_m_secextreg(CPUARMState
*env
, GByteArray
*buf
, int reg
)
449 return m_sysreg_get(env
, buf
, reg
>> 1, reg
& 1);
452 static int arm_gdb_set_m_secextreg(CPUARMState
*env
, uint8_t *buf
, int reg
)
457 static int arm_gen_dynamic_m_secextreg_xml(CPUState
*cs
, int orig_base_reg
)
459 ARMCPU
*cpu
= ARM_CPU(cs
);
460 GString
*s
= g_string_new(NULL
);
461 int base_reg
= orig_base_reg
;
464 g_string_printf(s
, "<?xml version=\"1.0\"?>");
465 g_string_append_printf(s
, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
466 g_string_append_printf(s
, "<feature name=\"org.gnu.gdb.arm.secext\">\n");
468 for (i
= 0; i
< ARRAY_SIZE(m_sysreg_def
); i
++) {
469 g_string_append_printf(s
,
470 "<reg name=\"%s_ns\" bitsize=\"32\" regnum=\"%d\"/>\n",
471 m_sysreg_def
[i
].name
, base_reg
++);
472 g_string_append_printf(s
,
473 "<reg name=\"%s_s\" bitsize=\"32\" regnum=\"%d\"/>\n",
474 m_sysreg_def
[i
].name
, base_reg
++);
477 g_string_append_printf(s
, "</feature>");
478 cpu
->dyn_m_secextreg_xml
.desc
= g_string_free(s
, false);
479 cpu
->dyn_m_secextreg_xml
.num
= base_reg
- orig_base_reg
;
481 return cpu
->dyn_m_secextreg_xml
.num
;
485 const char *arm_gdb_get_dynamic_xml(CPUState
*cs
, const char *xmlname
)
487 ARMCPU
*cpu
= ARM_CPU(cs
);
489 if (strcmp(xmlname
, "system-registers.xml") == 0) {
490 return cpu
->dyn_sysreg_xml
.desc
;
491 } else if (strcmp(xmlname
, "sve-registers.xml") == 0) {
492 return cpu
->dyn_svereg_xml
.desc
;
493 } else if (strcmp(xmlname
, "arm-m-system.xml") == 0) {
494 return cpu
->dyn_m_systemreg_xml
.desc
;
495 #ifndef CONFIG_USER_ONLY
496 } else if (strcmp(xmlname
, "arm-m-secext.xml") == 0) {
497 return cpu
->dyn_m_secextreg_xml
.desc
;
503 void arm_cpu_register_gdb_regs_for_features(ARMCPU
*cpu
)
505 CPUState
*cs
= CPU(cpu
);
506 CPUARMState
*env
= &cpu
->env
;
508 if (arm_feature(env
, ARM_FEATURE_AARCH64
)) {
510 * The lower part of each SVE register aliases to the FPU
511 * registers so we don't need to include both.
513 #ifdef TARGET_AARCH64
514 if (isar_feature_aa64_sve(&cpu
->isar
)) {
515 int nreg
= arm_gen_dynamic_svereg_xml(cs
, cs
->gdb_num_regs
);
516 gdb_register_coprocessor(cs
, aarch64_gdb_get_sve_reg
,
517 aarch64_gdb_set_sve_reg
, nreg
,
518 "sve-registers.xml", 0);
520 gdb_register_coprocessor(cs
, aarch64_gdb_get_fpu_reg
,
521 aarch64_gdb_set_fpu_reg
,
522 34, "aarch64-fpu.xml", 0);
525 * Note that we report pauth information via the feature name
526 * org.gnu.gdb.aarch64.pauth_v2, not org.gnu.gdb.aarch64.pauth.
527 * GDB versions 9 through 12 have a bug where they will crash
528 * if they see the latter XML from QEMU.
530 if (isar_feature_aa64_pauth(&cpu
->isar
)) {
531 gdb_register_coprocessor(cs
, aarch64_gdb_get_pauth_reg
,
532 aarch64_gdb_set_pauth_reg
,
533 4, "aarch64-pauth.xml", 0);
537 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
538 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
539 49, "arm-neon.xml", 0);
540 } else if (cpu_isar_feature(aa32_simd_r32
, cpu
)) {
541 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
542 33, "arm-vfp3.xml", 0);
543 } else if (cpu_isar_feature(aa32_vfp_simd
, cpu
)) {
544 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
545 17, "arm-vfp.xml", 0);
547 if (!arm_feature(env
, ARM_FEATURE_M
)) {
549 * A and R profile have FP sysregs FPEXC and FPSID that we
552 gdb_register_coprocessor(cs
, vfp_gdb_get_sysreg
, vfp_gdb_set_sysreg
,
553 2, "arm-vfp-sysregs.xml", 0);
556 if (cpu_isar_feature(aa32_mve
, cpu
) && tcg_enabled()) {
557 gdb_register_coprocessor(cs
, mve_gdb_get_reg
, mve_gdb_set_reg
,
558 1, "arm-m-profile-mve.xml", 0);
560 gdb_register_coprocessor(cs
, arm_gdb_get_sysreg
, arm_gdb_set_sysreg
,
561 arm_gen_dynamic_sysreg_xml(cs
, cs
->gdb_num_regs
),
562 "system-registers.xml", 0);
564 if (arm_feature(env
, ARM_FEATURE_M
) && tcg_enabled()) {
565 gdb_register_coprocessor(cs
,
566 arm_gdb_get_m_systemreg
, arm_gdb_set_m_systemreg
,
567 arm_gen_dynamic_m_systemreg_xml(cs
, cs
->gdb_num_regs
),
568 "arm-m-system.xml", 0);
569 #ifndef CONFIG_USER_ONLY
570 if (arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
571 gdb_register_coprocessor(cs
,
572 arm_gdb_get_m_secextreg
, arm_gdb_set_m_secextreg
,
573 arm_gen_dynamic_m_secextreg_xml(cs
, cs
->gdb_num_regs
),
574 "arm-m-secext.xml", 0);