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
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"
23 int riscv_cpu_gdb_read_register(CPUState
*cs
, GByteArray
*mem_buf
, int n
)
25 RISCVCPU
*cpu
= RISCV_CPU(cs
);
26 CPURISCVState
*env
= &cpu
->env
;
29 return gdb_get_regl(mem_buf
, env
->gpr
[n
]);
31 return gdb_get_regl(mem_buf
, env
->pc
);
36 int riscv_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
38 RISCVCPU
*cpu
= RISCV_CPU(cs
);
39 CPURISCVState
*env
= &cpu
->env
;
42 /* discard writes to x0 */
43 return sizeof(target_ulong
);
45 env
->gpr
[n
] = ldtul_p(mem_buf
);
46 return sizeof(target_ulong
);
48 env
->pc
= ldtul_p(mem_buf
);
49 return sizeof(target_ulong
);
54 static int riscv_gdb_get_fpu(CPURISCVState
*env
, GByteArray
*buf
, int n
)
57 if (env
->misa
& RVD
) {
58 return gdb_get_reg64(buf
, env
->fpr
[n
]);
60 if (env
->misa
& RVF
) {
61 return gdb_get_reg32(buf
, env
->fpr
[n
]);
63 /* there is hole between ft11 and fflags in fpu.xml */
64 } else if (n
< 36 && n
> 32) {
68 * CSR_FFLAGS is at index 1 in csr_register, and gdb says it is FP
69 * register 33, so we recalculate the map index.
70 * This also works for CSR_FRM and CSR_FCSR.
72 result
= riscv_csrrw_debug(env
, n
- 32, &val
,
74 if (result
== RISCV_EXCP_NONE
) {
75 return gdb_get_regl(buf
, val
);
81 static int riscv_gdb_set_fpu(CPURISCVState
*env
, uint8_t *mem_buf
, int n
)
84 env
->fpr
[n
] = ldq_p(mem_buf
); /* always 64-bit */
85 return sizeof(uint64_t);
86 /* there is hole between ft11 and fflags in fpu.xml */
87 } else if (n
< 36 && n
> 32) {
88 target_ulong val
= ldtul_p(mem_buf
);
91 * CSR_FFLAGS is at index 1 in csr_register, and gdb says it is FP
92 * register 33, so we recalculate the map index.
93 * This also works for CSR_FRM and CSR_FCSR.
95 result
= riscv_csrrw_debug(env
, n
- 32, NULL
,
97 if (result
== RISCV_EXCP_NONE
) {
98 return sizeof(target_ulong
);
104 static int riscv_gdb_get_csr(CPURISCVState
*env
, GByteArray
*buf
, int n
)
106 if (n
< CSR_TABLE_SIZE
) {
107 target_ulong val
= 0;
110 result
= riscv_csrrw_debug(env
, n
, &val
, 0, 0);
111 if (result
== RISCV_EXCP_NONE
) {
112 return gdb_get_regl(buf
, val
);
118 static int riscv_gdb_set_csr(CPURISCVState
*env
, uint8_t *mem_buf
, int n
)
120 if (n
< CSR_TABLE_SIZE
) {
121 target_ulong val
= ldtul_p(mem_buf
);
124 result
= riscv_csrrw_debug(env
, n
, NULL
, val
, -1);
125 if (result
== RISCV_EXCP_NONE
) {
126 return sizeof(target_ulong
);
132 static int riscv_gdb_get_virtual(CPURISCVState
*cs
, GByteArray
*buf
, int n
)
135 #ifdef CONFIG_USER_ONLY
136 return gdb_get_regl(buf
, 0);
138 return gdb_get_regl(buf
, cs
->priv
);
144 static int riscv_gdb_set_virtual(CPURISCVState
*cs
, uint8_t *mem_buf
, int n
)
147 #ifndef CONFIG_USER_ONLY
148 cs
->priv
= ldtul_p(mem_buf
) & 0x3;
149 if (cs
->priv
== PRV_H
) {
153 return sizeof(target_ulong
);
158 static int riscv_gen_dynamic_csr_xml(CPUState
*cs
, int base_reg
)
160 RISCVCPU
*cpu
= RISCV_CPU(cs
);
161 CPURISCVState
*env
= &cpu
->env
;
162 GString
*s
= g_string_new(NULL
);
163 riscv_csr_predicate_fn predicate
;
164 int bitsize
= riscv_cpu_is_32bit(env
) ? 32 : 64;
167 g_string_printf(s
, "<?xml version=\"1.0\"?>");
168 g_string_append_printf(s
, "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">");
169 g_string_append_printf(s
, "<feature name=\"org.gnu.gdb.riscv.csr\">");
171 for (i
= 0; i
< CSR_TABLE_SIZE
; i
++) {
172 predicate
= csr_ops
[i
].predicate
;
173 if (predicate
&& (predicate(env
, i
) == RISCV_EXCP_NONE
)) {
174 if (csr_ops
[i
].name
) {
175 g_string_append_printf(s
, "<reg name=\"%s\"", csr_ops
[i
].name
);
177 g_string_append_printf(s
, "<reg name=\"csr%03x\"", i
);
179 g_string_append_printf(s
, " bitsize=\"%d\"", bitsize
);
180 g_string_append_printf(s
, " regnum=\"%d\"/>", base_reg
+ i
);
184 g_string_append_printf(s
, "</feature>");
186 cpu
->dyn_csr_xml
= g_string_free(s
, false);
187 return CSR_TABLE_SIZE
;
190 void riscv_cpu_register_gdb_regs_for_features(CPUState
*cs
)
192 RISCVCPU
*cpu
= RISCV_CPU(cs
);
193 CPURISCVState
*env
= &cpu
->env
;
194 if (env
->misa
& RVD
) {
195 gdb_register_coprocessor(cs
, riscv_gdb_get_fpu
, riscv_gdb_set_fpu
,
196 36, "riscv-64bit-fpu.xml", 0);
197 } else if (env
->misa
& RVF
) {
198 gdb_register_coprocessor(cs
, riscv_gdb_get_fpu
, riscv_gdb_set_fpu
,
199 36, "riscv-32bit-fpu.xml", 0);
201 #if defined(TARGET_RISCV32)
202 gdb_register_coprocessor(cs
, riscv_gdb_get_virtual
, riscv_gdb_set_virtual
,
203 1, "riscv-32bit-virtual.xml", 0);
204 #elif defined(TARGET_RISCV64)
205 gdb_register_coprocessor(cs
, riscv_gdb_get_virtual
, riscv_gdb_set_virtual
,
206 1, "riscv-64bit-virtual.xml", 0);
209 gdb_register_coprocessor(cs
, riscv_gdb_get_csr
, riscv_gdb_set_csr
,
210 riscv_gen_dynamic_csr_xml(cs
, cs
->gdb_num_regs
),