1 /* 2 * ARM gdb server stub 3 * 4 * Copyright (c) 2003-2005 Fabrice Bellard 5 * Copyright (c) 2013 SUSE LINUX Products GmbH 6 * 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. 11 * 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. 16 * 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/>. 19 */ 20 #include "qemu/osdep.h" 21 #include "cpu.h" 22 #include "exec/gdbstub.h" 23 #include "internals.h" 24 #include "cpregs.h" 25 26 typedef struct RegisterSysregXmlParam { 27 CPUState *cs; 28 GString *s; 29 int n; 30 } RegisterSysregXmlParam; 31 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 36 newer gdb. */ 37 38 int arm_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n) 39 { 40 ARMCPU *cpu = ARM_CPU(cs); 41 CPUARMState *env = &cpu->env; 42 43 if (n < 16) { 44 /* Core integer register. */ 45 return gdb_get_reg32(mem_buf, env->regs[n]); 46 } 47 if (n < 24) { 48 /* FPA registers. */ 49 if (gdb_has_xml) { 50 return 0; 51 } 52 return gdb_get_zeroes(mem_buf, 12); 53 } 54 switch (n) { 55 case 24: 56 /* FPA status register. */ 57 if (gdb_has_xml) { 58 return 0; 59 } 60 return gdb_get_reg32(mem_buf, 0); 61 case 25: 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)); 65 } else { 66 return gdb_get_reg32(mem_buf, cpsr_read(env)); 67 } 68 } 69 /* Unknown register. */ 70 return 0; 71 } 72 73 int arm_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) 74 { 75 ARMCPU *cpu = ARM_CPU(cs); 76 CPUARMState *env = &cpu->env; 77 uint32_t tmp; 78 79 tmp = ldl_p(mem_buf); 80 81 /* 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. 87 */ 88 if (n == 15) { 89 tmp &= ~1; 90 } 91 92 if (n < 16) { 93 /* Core integer register. */ 94 if (n == 13 && arm_feature(env, ARM_FEATURE_M)) { 95 /* M profile SP low bits are always 0 */ 96 tmp &= ~3; 97 } 98 env->regs[n] = tmp; 99 return 4; 100 } 101 if (n < 24) { /* 16-23 */ 102 /* FPA registers (ignored). */ 103 if (gdb_has_xml) { 104 return 0; 105 } 106 return 12; 107 } 108 switch (n) { 109 case 24: 110 /* FPA status register (ignored). */ 111 if (gdb_has_xml) { 112 return 0; 113 } 114 return 4; 115 case 25: 116 /* CPSR, or XPSR for M-profile */ 117 if (arm_feature(env, ARM_FEATURE_M)) { 118 /* 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. 123 */ 124 xpsr_write(env, tmp, ~XPSR_EXCP); 125 } else { 126 cpsr_write(env, tmp, 0xffffffff, CPSRWriteByGDBStub); 127 } 128 return 4; 129 } 130 /* Unknown register. */ 131 return 0; 132 } 133 134 static int vfp_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg) 135 { 136 ARMCPU *cpu = env_archcpu(env); 137 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16; 138 139 /* VFP data registers are always little-endian. */ 140 if (reg < nregs) { 141 return gdb_get_reg64(buf, *aa32_vfp_dreg(env, reg)); 142 } 143 if (arm_feature(env, ARM_FEATURE_NEON)) { 144 /* Aliases for Q regs. */ 145 nregs += 16; 146 if (reg < nregs) { 147 uint64_t *q = aa32_vfp_qreg(env, reg - 32); 148 return gdb_get_reg128(buf, q[0], q[1]); 149 } 150 } 151 switch (reg - nregs) { 152 case 0: 153 return gdb_get_reg32(buf, vfp_get_fpscr(env)); 154 } 155 return 0; 156 } 157 158 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 159 { 160 ARMCPU *cpu = env_archcpu(env); 161 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16; 162 163 if (reg < nregs) { 164 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf); 165 return 8; 166 } 167 if (arm_feature(env, ARM_FEATURE_NEON)) { 168 nregs += 16; 169 if (reg < nregs) { 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); 173 return 16; 174 } 175 } 176 switch (reg - nregs) { 177 case 0: 178 vfp_set_fpscr(env, ldl_p(buf)); 179 return 4; 180 } 181 return 0; 182 } 183 184 static int vfp_gdb_get_sysreg(CPUARMState *env, GByteArray *buf, int reg) 185 { 186 switch (reg) { 187 case 0: 188 return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPSID]); 189 case 1: 190 return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPEXC]); 191 } 192 return 0; 193 } 194 195 static int vfp_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg) 196 { 197 switch (reg) { 198 case 0: 199 env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); 200 return 4; 201 case 1: 202 env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); 203 return 4; 204 } 205 return 0; 206 } 207 208 static int mve_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg) 209 { 210 switch (reg) { 211 case 0: 212 return gdb_get_reg32(buf, env->v7m.vpr); 213 default: 214 return 0; 215 } 216 } 217 218 static int mve_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 219 { 220 switch (reg) { 221 case 0: 222 env->v7m.vpr = ldl_p(buf); 223 return 4; 224 default: 225 return 0; 226 } 227 } 228 229 /** 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) 234 * 235 * We return the number of bytes copied 236 */ 237 238 static int arm_gdb_get_sysreg(CPUARMState *env, GByteArray *buf, int reg) 239 { 240 ARMCPU *cpu = env_archcpu(env); 241 const ARMCPRegInfo *ri; 242 uint32_t key; 243 244 key = cpu->dyn_sysreg_xml.data.cpregs.keys[reg]; 245 ri = get_arm_cp_reginfo(cpu->cp_regs, key); 246 if (ri) { 247 if (cpreg_field_is_64bit(ri)) { 248 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri)); 249 } else { 250 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri)); 251 } 252 } 253 return 0; 254 } 255 256 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg) 257 { 258 return 0; 259 } 260 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) 264 { 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; 270 dyn_xml->num++; 271 } 272 273 static void arm_register_sysreg_for_xml(gpointer key, gpointer value, 274 gpointer p) 275 { 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; 283 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, 288 param->n++); 289 } 290 } else { 291 if (ri->state == ARM_CP_STATE_AA32) { 292 if (!arm_feature(env, ARM_FEATURE_EL3) && 293 (ri->secure & ARM_CP_SECSTATE_S)) { 294 return; 295 } 296 if (ri->type & ARM_CP_64BIT) { 297 arm_gen_one_xml_sysreg_tag(s , dyn_xml, ri, ri_key, 64, 298 param->n++); 299 } else { 300 arm_gen_one_xml_sysreg_tag(s , dyn_xml, ri, ri_key, 32, 301 param->n++); 302 } 303 } 304 } 305 } 306 } 307 308 static int arm_gen_dynamic_sysreg_xml(CPUState *cs, int base_reg) 309 { 310 ARMCPU *cpu = ARM_CPU(cs); 311 GString *s = g_string_new(NULL); 312 RegisterSysregXmlParam param = {cs, s, base_reg}; 313 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; 323 } 324 325 const char *arm_gdb_get_dynamic_xml(CPUState *cs, const char *xmlname) 326 { 327 ARMCPU *cpu = ARM_CPU(cs); 328 329 if (strcmp(xmlname, "system-registers.xml") == 0) { 330 return cpu->dyn_sysreg_xml.desc; 331 } else if (strcmp(xmlname, "sve-registers.xml") == 0) { 332 return cpu->dyn_svereg_xml.desc; 333 } 334 return NULL; 335 } 336 337 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu) 338 { 339 CPUState *cs = CPU(cpu); 340 CPUARMState *env = &cpu->env; 341 342 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 343 /* 344 * The lower part of each SVE register aliases to the FPU 345 * registers so we don't need to include both. 346 */ 347 #ifdef TARGET_AARCH64 348 if (isar_feature_aa64_sve(&cpu->isar)) { 349 int nreg = arm_gen_dynamic_svereg_xml(cs, cs->gdb_num_regs); 350 gdb_register_coprocessor(cs, aarch64_gdb_get_sve_reg, 351 aarch64_gdb_set_sve_reg, nreg, 352 "sve-registers.xml", 0); 353 } else { 354 gdb_register_coprocessor(cs, aarch64_gdb_get_fpu_reg, 355 aarch64_gdb_set_fpu_reg, 356 34, "aarch64-fpu.xml", 0); 357 } 358 if (isar_feature_aa64_pauth(&cpu->isar)) { 359 gdb_register_coprocessor(cs, aarch64_gdb_get_pauth_reg, 360 aarch64_gdb_set_pauth_reg, 361 4, "aarch64-pauth.xml", 0); 362 } 363 #endif 364 } else { 365 if (arm_feature(env, ARM_FEATURE_NEON)) { 366 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 367 49, "arm-neon.xml", 0); 368 } else if (cpu_isar_feature(aa32_simd_r32, cpu)) { 369 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 370 33, "arm-vfp3.xml", 0); 371 } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) { 372 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 373 17, "arm-vfp.xml", 0); 374 } 375 if (!arm_feature(env, ARM_FEATURE_M)) { 376 /* 377 * A and R profile have FP sysregs FPEXC and FPSID that we 378 * expose to gdb. 379 */ 380 gdb_register_coprocessor(cs, vfp_gdb_get_sysreg, vfp_gdb_set_sysreg, 381 2, "arm-vfp-sysregs.xml", 0); 382 } 383 } 384 if (cpu_isar_feature(aa32_mve, cpu)) { 385 gdb_register_coprocessor(cs, mve_gdb_get_reg, mve_gdb_set_reg, 386 1, "arm-m-profile-mve.xml", 0); 387 } 388 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg, 389 arm_gen_dynamic_sysreg_xml(cs, cs->gdb_num_regs), 390 "system-registers.xml", 0); 391 392 } 393