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"
24 * The GDB CSR xml files list them in documentation order, not numerical order,
25 * and are missing entries for unnamed CSRs. So we need to map the gdb numbers
26 * to the hardware numbers.
29 static int csr_register_map
[] = {
272 int riscv_cpu_gdb_read_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
274 RISCVCPU
*cpu
= RISCV_CPU(cs
);
275 CPURISCVState
*env
= &cpu
->env
;
278 return gdb_get_regl(mem_buf
, env
->gpr
[n
]);
279 } else if (n
== 32) {
280 return gdb_get_regl(mem_buf
, env
->pc
);
285 int riscv_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
287 RISCVCPU
*cpu
= RISCV_CPU(cs
);
288 CPURISCVState
*env
= &cpu
->env
;
291 /* discard writes to x0 */
292 return sizeof(target_ulong
);
294 env
->gpr
[n
] = ldtul_p(mem_buf
);
295 return sizeof(target_ulong
);
296 } else if (n
== 32) {
297 env
->pc
= ldtul_p(mem_buf
);
298 return sizeof(target_ulong
);
303 static int riscv_gdb_get_fpu(CPURISCVState
*env
, uint8_t *mem_buf
, int n
)
306 return gdb_get_reg64(mem_buf
, env
->fpr
[n
]);
307 /* there is hole between ft11 and fflags in fpu.xml */
308 } else if (n
< 36 && n
> 32) {
309 target_ulong val
= 0;
312 * CSR_FFLAGS is at index 8 in csr_register, and gdb says it is FP
313 * register 33, so we recalculate the map index.
314 * This also works for CSR_FRM and CSR_FCSR.
316 result
= riscv_csrrw_debug(env
, n
- 33 + csr_register_map
[8], &val
,
319 return gdb_get_regl(mem_buf
, val
);
325 static int riscv_gdb_set_fpu(CPURISCVState
*env
, uint8_t *mem_buf
, int n
)
328 env
->fpr
[n
] = ldq_p(mem_buf
); /* always 64-bit */
329 return sizeof(uint64_t);
330 /* there is hole between ft11 and fflags in fpu.xml */
331 } else if (n
< 36 && n
> 32) {
332 target_ulong val
= ldtul_p(mem_buf
);
335 * CSR_FFLAGS is at index 8 in csr_register, and gdb says it is FP
336 * register 33, so we recalculate the map index.
337 * This also works for CSR_FRM and CSR_FCSR.
339 result
= riscv_csrrw_debug(env
, n
- 33 + csr_register_map
[8], NULL
,
342 return sizeof(target_ulong
);
348 static int riscv_gdb_get_csr(CPURISCVState
*env
, uint8_t *mem_buf
, int n
)
350 if (n
< ARRAY_SIZE(csr_register_map
)) {
351 target_ulong val
= 0;
354 result
= riscv_csrrw_debug(env
, csr_register_map
[n
], &val
, 0, 0);
356 return gdb_get_regl(mem_buf
, val
);
362 static int riscv_gdb_set_csr(CPURISCVState
*env
, uint8_t *mem_buf
, int n
)
364 if (n
< ARRAY_SIZE(csr_register_map
)) {
365 target_ulong val
= ldtul_p(mem_buf
);
368 result
= riscv_csrrw_debug(env
, csr_register_map
[n
], NULL
, val
, -1);
370 return sizeof(target_ulong
);
376 void riscv_cpu_register_gdb_regs_for_features(CPUState
*cs
)
378 RISCVCPU
*cpu
= RISCV_CPU(cs
);
379 CPURISCVState
*env
= &cpu
->env
;
380 #if defined(TARGET_RISCV32)
381 if (env
->misa
& RVF
) {
382 gdb_register_coprocessor(cs
, riscv_gdb_get_fpu
, riscv_gdb_set_fpu
,
383 36, "riscv-32bit-fpu.xml", 0);
386 gdb_register_coprocessor(cs
, riscv_gdb_get_csr
, riscv_gdb_set_csr
,
387 4096, "riscv-32bit-csr.xml", 0);
388 #elif defined(TARGET_RISCV64)
389 if (env
->misa
& RVF
) {
390 gdb_register_coprocessor(cs
, riscv_gdb_get_fpu
, riscv_gdb_set_fpu
,
391 36, "riscv-64bit-fpu.xml", 0);
394 gdb_register_coprocessor(cs
, riscv_gdb_get_csr
, riscv_gdb_set_csr
,
395 4096, "riscv-64bit-csr.xml", 0);