hw/arm/virt: Disable pl011 clock migration if needed
[qemu/ar7.git] / target / riscv / gdbstub.c
blob5f96b7ea2a9523b81d3eadb1dd43a1bfc56483b4
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 "cpu.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;
28 if (n < 32) {
29 return gdb_get_regl(mem_buf, env->gpr[n]);
30 } else if (n == 32) {
31 return gdb_get_regl(mem_buf, env->pc);
33 return 0;
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;
41 if (n == 0) {
42 /* discard writes to x0 */
43 return sizeof(target_ulong);
44 } else if (n < 32) {
45 env->gpr[n] = ldtul_p(mem_buf);
46 return sizeof(target_ulong);
47 } else if (n == 32) {
48 env->pc = ldtul_p(mem_buf);
49 return sizeof(target_ulong);
51 return 0;
54 static int riscv_gdb_get_fpu(CPURISCVState *env, GByteArray *buf, int n)
56 if (n < 32) {
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) {
65 target_ulong val = 0;
66 int result;
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,
73 0, 0);
74 if (result == 0) {
75 return gdb_get_regl(buf, val);
78 return 0;
81 static int riscv_gdb_set_fpu(CPURISCVState *env, uint8_t *mem_buf, int n)
83 if (n < 32) {
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);
89 int result;
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,
96 val, -1);
97 if (result == 0) {
98 return sizeof(target_ulong);
101 return 0;
104 static int riscv_gdb_get_csr(CPURISCVState *env, GByteArray *buf, int n)
106 if (n < CSR_TABLE_SIZE) {
107 target_ulong val = 0;
108 int result;
110 result = riscv_csrrw_debug(env, n, &val, 0, 0);
111 if (result == 0) {
112 return gdb_get_regl(buf, val);
115 return 0;
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);
122 int result;
124 result = riscv_csrrw_debug(env, n, NULL, val, -1);
125 if (result == 0) {
126 return sizeof(target_ulong);
129 return 0;
132 static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n)
134 if (n == 0) {
135 #ifdef CONFIG_USER_ONLY
136 return gdb_get_regl(buf, 0);
137 #else
138 return gdb_get_regl(buf, cs->priv);
139 #endif
141 return 0;
144 static int riscv_gdb_set_virtual(CPURISCVState *cs, uint8_t *mem_buf, int n)
146 if (n == 0) {
147 #ifndef CONFIG_USER_ONLY
148 cs->priv = ldtul_p(mem_buf) & 0x3;
149 if (cs->priv == PRV_H) {
150 cs->priv = PRV_S;
152 #endif
153 return sizeof(target_ulong);
155 return 0;
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;
165 int i;
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)) {
174 if (csr_ops[i].name) {
175 g_string_append_printf(s, "<reg name=\"%s\"", csr_ops[i].name);
176 } else {
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);
207 #endif
209 gdb_register_coprocessor(cs, riscv_gdb_get_csr, riscv_gdb_set_csr,
210 riscv_gen_dynamic_csr_xml(cs, cs->gdb_num_regs),
211 "riscv-csr.xml", 0);