qapi: Require descriptions and tagged sections to be indented
[qemu/kevin.git] / target / riscv / gdbstub.c
blobca9b71f7bbce5ae5986c6ef5d9e71d67a625e9c0
1 /*
2 * RISC-V GDB Server Stub
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "exec/gdbstub.h"
21 #include "gdbstub/helpers.h"
22 #include "cpu.h"
24 struct TypeSize {
25 const char *gdb_type;
26 const char *id;
27 int size;
28 const char suffix;
31 static const struct TypeSize vec_lanes[] = {
32 /* quads */
33 { "uint128", "quads", 128, 'q' },
34 /* 64 bit */
35 { "uint64", "longs", 64, 'l' },
36 /* 32 bit */
37 { "uint32", "words", 32, 'w' },
38 /* 16 bit */
39 { "uint16", "shorts", 16, 's' },
41 * TODO: currently there is no reliable way of telling
42 * if the remote gdb actually understands ieee_half so
43 * we don't expose it in the target description for now.
44 * { "ieee_half", 16, 'h', 'f' },
46 /* bytes */
47 { "uint8", "bytes", 8, 'b' },
50 int riscv_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
52 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cs);
53 RISCVCPU *cpu = RISCV_CPU(cs);
54 CPURISCVState *env = &cpu->env;
55 target_ulong tmp;
57 if (n < 32) {
58 tmp = env->gpr[n];
59 } else if (n == 32) {
60 tmp = env->pc;
61 } else {
62 return 0;
65 switch (mcc->misa_mxl_max) {
66 case MXL_RV32:
67 return gdb_get_reg32(mem_buf, tmp);
68 case MXL_RV64:
69 case MXL_RV128:
70 return gdb_get_reg64(mem_buf, tmp);
71 default:
72 g_assert_not_reached();
74 return 0;
77 int riscv_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
79 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cs);
80 RISCVCPU *cpu = RISCV_CPU(cs);
81 CPURISCVState *env = &cpu->env;
82 int length = 0;
83 target_ulong tmp;
85 switch (mcc->misa_mxl_max) {
86 case MXL_RV32:
87 tmp = (int32_t)ldl_p(mem_buf);
88 length = 4;
89 break;
90 case MXL_RV64:
91 case MXL_RV128:
92 if (env->xl < MXL_RV64) {
93 tmp = (int32_t)ldq_p(mem_buf);
94 } else {
95 tmp = ldq_p(mem_buf);
97 length = 8;
98 break;
99 default:
100 g_assert_not_reached();
102 if (n > 0 && n < 32) {
103 env->gpr[n] = tmp;
104 } else if (n == 32) {
105 env->pc = tmp;
108 return length;
111 static int riscv_gdb_get_fpu(CPURISCVState *env, GByteArray *buf, int n)
113 if (n < 32) {
114 if (env->misa_ext & RVD) {
115 return gdb_get_reg64(buf, env->fpr[n]);
117 if (env->misa_ext & RVF) {
118 return gdb_get_reg32(buf, env->fpr[n]);
121 return 0;
124 static int riscv_gdb_set_fpu(CPURISCVState *env, uint8_t *mem_buf, int n)
126 if (n < 32) {
127 env->fpr[n] = ldq_p(mem_buf); /* always 64-bit */
128 return sizeof(uint64_t);
130 return 0;
133 static int riscv_gdb_get_vector(CPURISCVState *env, GByteArray *buf, int n)
135 uint16_t vlenb = riscv_cpu_cfg(env)->vlenb;
136 if (n < 32) {
137 int i;
138 int cnt = 0;
139 for (i = 0; i < vlenb; i += 8) {
140 cnt += gdb_get_reg64(buf,
141 env->vreg[(n * vlenb + i) / 8]);
143 return cnt;
146 return 0;
149 static int riscv_gdb_set_vector(CPURISCVState *env, uint8_t *mem_buf, int n)
151 uint16_t vlenb = riscv_cpu_cfg(env)->vlenb;
152 if (n < 32) {
153 int i;
154 for (i = 0; i < vlenb; i += 8) {
155 env->vreg[(n * vlenb + i) / 8] = ldq_p(mem_buf + i);
157 return vlenb;
160 return 0;
163 static int riscv_gdb_get_csr(CPURISCVState *env, GByteArray *buf, int n)
165 if (n < CSR_TABLE_SIZE) {
166 target_ulong val = 0;
167 int result;
169 result = riscv_csrrw_debug(env, n, &val, 0, 0);
170 if (result == RISCV_EXCP_NONE) {
171 return gdb_get_regl(buf, val);
174 return 0;
177 static int riscv_gdb_set_csr(CPURISCVState *env, uint8_t *mem_buf, int n)
179 if (n < CSR_TABLE_SIZE) {
180 target_ulong val = ldtul_p(mem_buf);
181 int result;
183 result = riscv_csrrw_debug(env, n, NULL, val, -1);
184 if (result == RISCV_EXCP_NONE) {
185 return sizeof(target_ulong);
188 return 0;
191 static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n)
193 if (n == 0) {
194 #ifdef CONFIG_USER_ONLY
195 return gdb_get_regl(buf, 0);
196 #else
197 return gdb_get_regl(buf, cs->priv);
198 #endif
200 return 0;
203 static int riscv_gdb_set_virtual(CPURISCVState *cs, uint8_t *mem_buf, int n)
205 if (n == 0) {
206 #ifndef CONFIG_USER_ONLY
207 cs->priv = ldtul_p(mem_buf) & 0x3;
208 if (cs->priv == PRV_RESERVED) {
209 cs->priv = PRV_S;
211 #endif
212 return sizeof(target_ulong);
214 return 0;
217 static int riscv_gen_dynamic_csr_xml(CPUState *cs, int base_reg)
219 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cs);
220 RISCVCPU *cpu = RISCV_CPU(cs);
221 CPURISCVState *env = &cpu->env;
222 GString *s = g_string_new(NULL);
223 riscv_csr_predicate_fn predicate;
224 int bitsize = riscv_cpu_max_xlen(mcc);
225 int i;
227 #if !defined(CONFIG_USER_ONLY)
228 env->debugger = true;
229 #endif
231 /* Until gdb knows about 128-bit registers */
232 if (bitsize > 64) {
233 bitsize = 64;
236 g_string_printf(s, "<?xml version=\"1.0\"?>");
237 g_string_append_printf(s, "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">");
238 g_string_append_printf(s, "<feature name=\"org.gnu.gdb.riscv.csr\">");
240 for (i = 0; i < CSR_TABLE_SIZE; i++) {
241 if (env->priv_ver < csr_ops[i].min_priv_ver) {
242 continue;
244 predicate = csr_ops[i].predicate;
245 if (predicate && (predicate(env, i) == RISCV_EXCP_NONE)) {
246 if (csr_ops[i].name) {
247 g_string_append_printf(s, "<reg name=\"%s\"", csr_ops[i].name);
248 } else {
249 g_string_append_printf(s, "<reg name=\"csr%03x\"", i);
251 g_string_append_printf(s, " bitsize=\"%d\"", bitsize);
252 g_string_append_printf(s, " regnum=\"%d\"/>", base_reg + i);
256 g_string_append_printf(s, "</feature>");
258 cpu->dyn_csr_xml = g_string_free(s, false);
260 #if !defined(CONFIG_USER_ONLY)
261 env->debugger = false;
262 #endif
264 return CSR_TABLE_SIZE;
267 static int ricsv_gen_dynamic_vector_xml(CPUState *cs, int base_reg)
269 RISCVCPU *cpu = RISCV_CPU(cs);
270 GString *s = g_string_new(NULL);
271 g_autoptr(GString) ts = g_string_new("");
272 int reg_width = cpu->cfg.vlenb << 3;
273 int num_regs = 0;
274 int i;
276 g_string_printf(s, "<?xml version=\"1.0\"?>");
277 g_string_append_printf(s, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
278 g_string_append_printf(s, "<feature name=\"org.gnu.gdb.riscv.vector\">");
280 /* First define types and totals in a whole VL */
281 for (i = 0; i < ARRAY_SIZE(vec_lanes); i++) {
282 int count = reg_width / vec_lanes[i].size;
283 g_string_printf(ts, "%s", vec_lanes[i].id);
284 g_string_append_printf(s,
285 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>",
286 ts->str, vec_lanes[i].gdb_type, count);
289 /* Define unions */
290 g_string_append_printf(s, "<union id=\"riscv_vector\">");
291 for (i = 0; i < ARRAY_SIZE(vec_lanes); i++) {
292 g_string_append_printf(s, "<field name=\"%c\" type=\"%s\"/>",
293 vec_lanes[i].suffix,
294 vec_lanes[i].id);
296 g_string_append(s, "</union>");
298 /* Define vector registers */
299 for (i = 0; i < 32; i++) {
300 g_string_append_printf(s,
301 "<reg name=\"v%d\" bitsize=\"%d\""
302 " regnum=\"%d\" group=\"vector\""
303 " type=\"riscv_vector\"/>",
304 i, reg_width, base_reg++);
305 num_regs++;
308 g_string_append_printf(s, "</feature>");
310 cpu->dyn_vreg_xml = g_string_free(s, false);
311 return num_regs;
314 void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
316 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cs);
317 RISCVCPU *cpu = RISCV_CPU(cs);
318 CPURISCVState *env = &cpu->env;
319 if (env->misa_ext & RVD) {
320 gdb_register_coprocessor(cs, riscv_gdb_get_fpu, riscv_gdb_set_fpu,
321 32, "riscv-64bit-fpu.xml", 0);
322 } else if (env->misa_ext & RVF) {
323 gdb_register_coprocessor(cs, riscv_gdb_get_fpu, riscv_gdb_set_fpu,
324 32, "riscv-32bit-fpu.xml", 0);
326 if (env->misa_ext & RVV) {
327 int base_reg = cs->gdb_num_regs;
328 gdb_register_coprocessor(cs, riscv_gdb_get_vector,
329 riscv_gdb_set_vector,
330 ricsv_gen_dynamic_vector_xml(cs, base_reg),
331 "riscv-vector.xml", 0);
333 switch (mcc->misa_mxl_max) {
334 case MXL_RV32:
335 gdb_register_coprocessor(cs, riscv_gdb_get_virtual,
336 riscv_gdb_set_virtual,
337 1, "riscv-32bit-virtual.xml", 0);
338 break;
339 case MXL_RV64:
340 case MXL_RV128:
341 gdb_register_coprocessor(cs, riscv_gdb_get_virtual,
342 riscv_gdb_set_virtual,
343 1, "riscv-64bit-virtual.xml", 0);
344 break;
345 default:
346 g_assert_not_reached();
349 if (cpu->cfg.ext_zicsr) {
350 int base_reg = cs->gdb_num_regs;
351 gdb_register_coprocessor(cs, riscv_gdb_get_csr, riscv_gdb_set_csr,
352 riscv_gen_dynamic_csr_xml(cs, base_reg),
353 "riscv-csr.xml", 0);