2 * ARM gdb server stub: AArch64 specific functions.
4 * Copyright (c) 2013 SUSE LINUX Products GmbH
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
22 #include "internals.h"
23 #include "gdbstub/helpers.h"
24 #include "gdbstub/commands.h"
25 #include "tcg/mte_helper.h"
26 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
27 #include <sys/prctl.h>
28 #include "mte_user_helper.h"
31 int aarch64_cpu_gdb_read_register(CPUState
*cs
, GByteArray
*mem_buf
, int n
)
33 ARMCPU
*cpu
= ARM_CPU(cs
);
34 CPUARMState
*env
= &cpu
->env
;
37 /* Core integer register. */
38 return gdb_get_reg64(mem_buf
, env
->xregs
[n
]);
42 return gdb_get_reg64(mem_buf
, env
->xregs
[31]);
44 return gdb_get_reg64(mem_buf
, env
->pc
);
46 return gdb_get_reg32(mem_buf
, pstate_read(env
));
48 /* Unknown register. */
52 int aarch64_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
54 ARMCPU
*cpu
= ARM_CPU(cs
);
55 CPUARMState
*env
= &cpu
->env
;
61 /* Core integer register. */
74 pstate_write(env
, tmp
);
77 /* Unknown register. */
81 int aarch64_gdb_get_fpu_reg(CPUState
*cs
, GByteArray
*buf
, int reg
)
83 ARMCPU
*cpu
= ARM_CPU(cs
);
84 CPUARMState
*env
= &cpu
->env
;
89 /* 128 bit FP register - quads are in LE order */
90 uint64_t *q
= aa64_vfp_qreg(env
, reg
);
91 return gdb_get_reg128(buf
, q
[1], q
[0]);
95 return gdb_get_reg32(buf
, vfp_get_fpsr(env
));
98 return gdb_get_reg32(buf
, vfp_get_fpcr(env
));
104 int aarch64_gdb_set_fpu_reg(CPUState
*cs
, uint8_t *buf
, int reg
)
106 ARMCPU
*cpu
= ARM_CPU(cs
);
107 CPUARMState
*env
= &cpu
->env
;
111 /* 128 bit FP register */
113 uint64_t *q
= aa64_vfp_qreg(env
, reg
);
114 q
[0] = ldq_le_p(buf
);
115 q
[1] = ldq_le_p(buf
+ 8);
120 vfp_set_fpsr(env
, ldl_p(buf
));
124 vfp_set_fpcr(env
, ldl_p(buf
));
131 int aarch64_gdb_get_sve_reg(CPUState
*cs
, GByteArray
*buf
, int reg
)
133 ARMCPU
*cpu
= ARM_CPU(cs
);
134 CPUARMState
*env
= &cpu
->env
;
137 /* The first 32 registers are the zregs */
141 for (vq
= 0; vq
< cpu
->sve_max_vq
; vq
++) {
142 len
+= gdb_get_reg128(buf
,
143 env
->vfp
.zregs
[reg
].d
[vq
* 2 + 1],
144 env
->vfp
.zregs
[reg
].d
[vq
* 2]);
149 return gdb_get_reg32(buf
, vfp_get_fpsr(env
));
151 return gdb_get_reg32(buf
, vfp_get_fpcr(env
));
152 /* then 16 predicates and the ffr */
157 for (vq
= 0; vq
< cpu
->sve_max_vq
; vq
= vq
+ 4) {
158 len
+= gdb_get_reg64(buf
, env
->vfp
.pregs
[preg
].p
[vq
/ 4]);
165 * We report in Vector Granules (VG) which is 64bit in a Z reg
166 * while the ZCR works in Vector Quads (VQ) which is 128bit chunks.
168 int vq
= sve_vqm1_for_el(env
, arm_current_el(env
)) + 1;
169 return gdb_get_reg64(buf
, vq
* 2);
172 /* gdbstub asked for something out our range */
173 qemu_log_mask(LOG_UNIMP
, "%s: out of range register %d", __func__
, reg
);
180 int aarch64_gdb_set_sve_reg(CPUState
*cs
, uint8_t *buf
, int reg
)
182 ARMCPU
*cpu
= ARM_CPU(cs
);
183 CPUARMState
*env
= &cpu
->env
;
185 /* The first 32 registers are the zregs */
187 /* The first 32 registers are the zregs */
191 uint64_t *p
= (uint64_t *) buf
;
192 for (vq
= 0; vq
< cpu
->sve_max_vq
; vq
++) {
193 env
->vfp
.zregs
[reg
].d
[vq
* 2 + 1] = *p
++;
194 env
->vfp
.zregs
[reg
].d
[vq
* 2] = *p
++;
200 vfp_set_fpsr(env
, *(uint32_t *)buf
);
203 vfp_set_fpcr(env
, *(uint32_t *)buf
);
209 uint64_t *p
= (uint64_t *) buf
;
210 for (vq
= 0; vq
< cpu
->sve_max_vq
; vq
= vq
+ 4) {
211 env
->vfp
.pregs
[preg
].p
[vq
/ 4] = *p
++;
217 /* cannot set vg via gdbstub */
220 /* gdbstub asked for something out our range */
227 int aarch64_gdb_get_pauth_reg(CPUState
*cs
, GByteArray
*buf
, int reg
)
229 ARMCPU
*cpu
= ARM_CPU(cs
);
230 CPUARMState
*env
= &cpu
->env
;
233 case 0: /* pauth_dmask */
234 case 1: /* pauth_cmask */
235 case 2: /* pauth_dmask_high */
236 case 3: /* pauth_cmask_high */
238 * Note that older versions of this feature only contained
239 * pauth_{d,c}mask, for use with Linux user processes, and
240 * thus exclusively in the low half of the address space.
242 * To support system mode, and to debug kernels, two new regs
243 * were added to cover the high half of the address space.
244 * For the purpose of pauth_ptr_mask, we can use any well-formed
245 * address within the address space half -- here, 0 and -1.
248 bool is_data
= !(reg
& 1);
249 bool is_high
= reg
& 2;
250 ARMMMUIdx mmu_idx
= arm_stage1_mmu_idx(env
);
251 ARMVAParameters param
;
253 param
= aa64_va_parameters(env
, -is_high
, mmu_idx
, is_data
, false);
254 return gdb_get_reg64(buf
, pauth_ptr_mask(param
));
261 int aarch64_gdb_set_pauth_reg(CPUState
*cs
, uint8_t *buf
, int reg
)
263 /* All pseudo registers are read-only. */
267 static void output_vector_union_type(GDBFeatureBuilder
*builder
, int reg_width
,
271 const char *gdb_type
;
276 static const struct TypeSize vec_lanes
[] = {
278 { "uint128", 128, 'q', 'u' },
279 { "int128", 128, 'q', 's' },
281 { "ieee_double", 64, 'd', 'f' },
282 { "uint64", 64, 'd', 'u' },
283 { "int64", 64, 'd', 's' },
285 { "ieee_single", 32, 's', 'f' },
286 { "uint32", 32, 's', 'u' },
287 { "int32", 32, 's', 's' },
289 { "ieee_half", 16, 'h', 'f' },
290 { "uint16", 16, 'h', 'u' },
291 { "int16", 16, 'h', 's' },
293 { "uint8", 8, 'b', 'u' },
294 { "int8", 8, 'b', 's' },
297 static const char suf
[] = { 'b', 'h', 's', 'd', 'q' };
300 /* First define types and totals in a whole VL */
301 for (i
= 0; i
< ARRAY_SIZE(vec_lanes
); i
++) {
302 gdb_feature_builder_append_tag(
303 builder
, "<vector id=\"%s%c%c\" type=\"%s\" count=\"%d\"/>",
304 name
, vec_lanes
[i
].sz
, vec_lanes
[i
].suffix
,
305 vec_lanes
[i
].gdb_type
, reg_width
/ vec_lanes
[i
].size
);
309 * Now define a union for each size group containing unsigned and
310 * signed and potentially float versions of each size from 128 to
313 for (i
= 0; i
< ARRAY_SIZE(suf
); i
++) {
316 gdb_feature_builder_append_tag(builder
, "<union id=\"%sn%c\">",
318 for (j
= 0; j
< ARRAY_SIZE(vec_lanes
); j
++) {
319 if (vec_lanes
[j
].size
== bits
) {
320 gdb_feature_builder_append_tag(
321 builder
, "<field name=\"%c\" type=\"%s%c%c\"/>",
322 vec_lanes
[j
].suffix
, name
,
323 vec_lanes
[j
].sz
, vec_lanes
[j
].suffix
);
326 gdb_feature_builder_append_tag(builder
, "</union>");
329 /* And now the final union of unions */
330 gdb_feature_builder_append_tag(builder
, "<union id=\"%s\">", name
);
331 for (i
= ARRAY_SIZE(suf
) - 1; i
>= 0; i
--) {
332 gdb_feature_builder_append_tag(builder
,
333 "<field name=\"%c\" type=\"%sn%c\"/>",
334 suf
[i
], name
, suf
[i
]);
336 gdb_feature_builder_append_tag(builder
, "</union>");
339 GDBFeature
*arm_gen_dynamic_svereg_feature(CPUState
*cs
, int base_reg
)
341 ARMCPU
*cpu
= ARM_CPU(cs
);
342 int reg_width
= cpu
->sve_max_vq
* 128;
343 int pred_width
= cpu
->sve_max_vq
* 16;
344 GDBFeatureBuilder builder
;
349 gdb_feature_builder_init(&builder
, &cpu
->dyn_svereg_feature
.desc
,
350 "org.gnu.gdb.aarch64.sve", "sve-registers.xml",
353 /* Create the vector union type. */
354 output_vector_union_type(&builder
, reg_width
, "svev");
356 /* Create the predicate vector type. */
357 gdb_feature_builder_append_tag(
358 &builder
, "<vector id=\"svep\" type=\"uint8\" count=\"%d\"/>",
361 /* Define the vector registers. */
362 for (i
= 0; i
< 32; i
++) {
363 name
= g_strdup_printf("z%d", i
);
364 gdb_feature_builder_append_reg(&builder
, name
, reg_width
, reg
++,
368 /* fpscr & status registers */
369 gdb_feature_builder_append_reg(&builder
, "fpsr", 32, reg
++,
371 gdb_feature_builder_append_reg(&builder
, "fpcr", 32, reg
++,
374 /* Define the predicate registers. */
375 for (i
= 0; i
< 16; i
++) {
376 name
= g_strdup_printf("p%d", i
);
377 gdb_feature_builder_append_reg(&builder
, name
, pred_width
, reg
++,
380 gdb_feature_builder_append_reg(&builder
, "ffr", pred_width
, reg
++,
383 /* Define the vector length pseudo-register. */
384 gdb_feature_builder_append_reg(&builder
, "vg", 64, reg
++, "int", NULL
);
386 gdb_feature_builder_end(&builder
);
388 return &cpu
->dyn_svereg_feature
.desc
;
391 #ifdef CONFIG_USER_ONLY
392 int aarch64_gdb_get_tag_ctl_reg(CPUState
*cs
, GByteArray
*buf
, int reg
)
394 ARMCPU
*cpu
= ARM_CPU(cs
);
395 CPUARMState
*env
= &cpu
->env
;
400 tcf0
= extract64(env
->cp15
.sctlr_el
[1], 38, 2);
402 return gdb_get_reg64(buf
, tcf0
);
405 int aarch64_gdb_set_tag_ctl_reg(CPUState
*cs
, uint8_t *buf
, int reg
)
407 #if defined(CONFIG_LINUX)
408 ARMCPU
*cpu
= ARM_CPU(cs
);
409 CPUARMState
*env
= &cpu
->env
;
415 tcf
= *buf
<< PR_MTE_TCF_SHIFT
;
422 * 'tag_ctl' register is actually a "pseudo-register" provided by GDB to
423 * expose options regarding the type of MTE fault that can be controlled at
426 arm_set_mte_tcf0(env
, tcf
);
433 #endif /* CONFIG_USER_ONLY */
436 static void handle_q_memtag(GArray
*params
, void *user_ctx
)
438 ARMCPU
*cpu
= ARM_CPU(user_ctx
);
439 CPUARMState
*env
= &cpu
->env
;
442 uint64_t addr
= gdb_get_cmd_param(params
, 0)->val_ull
;
443 uint64_t len
= gdb_get_cmd_param(params
, 1)->val_ul
;
444 int type
= gdb_get_cmd_param(params
, 2)->val_ul
;
449 g_autoptr(GString
) str_buf
= g_string_new(NULL
);
452 * GDB does not query multiple tags for a memory range on remote targets, so
453 * that's not supported either by gdbstub.
456 gdb_put_packet("E02");
459 /* GDB never queries a tag different from an allocation tag (type 1). */
461 gdb_put_packet("E03");
464 /* Find out the current translation regime for probe. */
465 mmu_index
= cpu_mmu_index(env_cpu(env
), false);
466 /* Note that tags are packed here (2 tags packed in one byte). */
467 tags
= allocation_tag_mem_probe(env
, mmu_index
, addr
, MMU_DATA_LOAD
, 1,
468 MMU_DATA_LOAD
, true, 0);
470 /* Address is not in a tagged region. */
471 gdb_put_packet("E04");
475 /* Unpack tag from byte. */
476 addr_tag
= load_tag1(addr
, tags
);
477 g_string_printf(str_buf
, "m%.2x", addr_tag
);
479 gdb_put_packet(str_buf
->str
);
482 static void handle_q_isaddresstagged(GArray
*params
, void *user_ctx
)
484 ARMCPU
*cpu
= ARM_CPU(user_ctx
);
485 CPUARMState
*env
= &cpu
->env
;
488 uint64_t addr
= gdb_get_cmd_param(params
, 0)->val_ull
;
493 /* Find out the current translation regime for probe. */
494 mmu_index
= cpu_mmu_index(env_cpu(env
), false);
495 tags
= allocation_tag_mem_probe(env
, mmu_index
, addr
, MMU_DATA_LOAD
, 1,
496 MMU_DATA_LOAD
, true, 0);
497 reply
= tags
? "01" : "00";
499 gdb_put_packet(reply
);
502 static void handle_Q_memtag(GArray
*params
, void *user_ctx
)
504 ARMCPU
*cpu
= ARM_CPU(user_ctx
);
505 CPUARMState
*env
= &cpu
->env
;
508 uint64_t start_addr
= gdb_get_cmd_param(params
, 0)->val_ull
;
509 uint64_t len
= gdb_get_cmd_param(params
, 1)->val_ul
;
510 int type
= gdb_get_cmd_param(params
, 2)->val_ul
;
511 char const *new_tags_str
= gdb_get_cmd_param(params
, 3)->data
;
518 g_autoptr(GByteArray
) new_tags
= g_byte_array_new();
521 * Only the allocation tag (i.e. type 1) can be set at the stub side.
524 gdb_put_packet("E02");
528 end_addr
= start_addr
+ (len
- 1); /* 'len' is always >= 1 */
529 /* Check if request's memory range does not cross page boundaries. */
530 if ((start_addr
^ end_addr
) & TARGET_PAGE_MASK
) {
531 gdb_put_packet("E03");
536 * Get all tags in the page starting from the tag of the start address.
537 * Note that there are two tags packed into a single byte here.
539 /* Find out the current translation regime for probe. */
540 mmu_index
= cpu_mmu_index(env_cpu(env
), false);
541 tags
= allocation_tag_mem_probe(env
, mmu_index
, start_addr
, MMU_DATA_STORE
,
542 1, MMU_DATA_STORE
, true, 0);
544 /* Address is not in a tagged region. */
545 gdb_put_packet("E04");
549 /* Convert tags provided by GDB, 2 hex digits per tag. */
550 num_new_tags
= strlen(new_tags_str
) / 2;
551 gdb_hextomem(new_tags
, new_tags_str
, num_new_tags
);
553 uint64_t address
= start_addr
;
554 int new_tag_index
= 0;
555 while (address
<= end_addr
) {
560 * Find packed tag index from unpacked tag index. There are two tags
561 * in one packed index (one tag per nibble).
563 packed_index
= new_tag_index
/ 2;
565 new_tag
= new_tags
->data
[new_tag_index
% num_new_tags
];
566 store_tag1(address
, tags
+ packed_index
, new_tag
);
568 address
+= TAG_GRANULE
;
572 gdb_put_packet("OK");
582 static const GdbCmdParseEntry cmd_handler_table
[NUM_CMDS
] = {
584 .handler
= handle_q_memtag
,
585 .cmd_startswith
= true,
588 .need_cpu_context
= true
590 [qIsAddressTagged
] = {
591 .handler
= handle_q_isaddresstagged
,
592 .cmd_startswith
= true,
593 .cmd
= "IsAddressTagged:",
595 .need_cpu_context
= true
598 .handler
= handle_Q_memtag
,
599 .cmd_startswith
= true,
601 .schema
= "L,l:l:s0",
602 .need_cpu_context
= true
605 #endif /* CONFIG_TCG */
607 void aarch64_cpu_register_gdb_commands(ARMCPU
*cpu
, GString
*qsupported
,
608 GPtrArray
*qtable
, GPtrArray
*stable
)
612 if (cpu_isar_feature(aa64_mte
, cpu
)) {
613 g_string_append(qsupported
, ";memory-tagging+");
615 g_ptr_array_add(qtable
, (gpointer
) &cmd_handler_table
[qMemTags
]);
616 g_ptr_array_add(qtable
, (gpointer
) &cmd_handler_table
[qIsAddressTagged
]);
617 g_ptr_array_add(stable
, (gpointer
) &cmd_handler_table
[QMemTags
]);