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 + 8, &val
, 0, 0);
318 return gdb_get_regl(mem_buf
, val
);
324 static int riscv_gdb_set_fpu(CPURISCVState
*env
, uint8_t *mem_buf
, int n
)
327 env
->fpr
[n
] = ldq_p(mem_buf
); /* always 64-bit */
328 return sizeof(uint64_t);
329 /* there is hole between ft11 and fflags in fpu.xml */
330 } else if (n
< 36 && n
> 32) {
331 target_ulong val
= ldtul_p(mem_buf
);
334 * CSR_FFLAGS is at index 8 in csr_register, and gdb says it is FP
335 * register 33, so we recalculate the map index.
336 * This also works for CSR_FRM and CSR_FCSR.
338 result
= riscv_csrrw_debug(env
, n
- 33 + 8, NULL
, val
, -1);
340 return sizeof(target_ulong
);
346 static int riscv_gdb_get_csr(CPURISCVState
*env
, uint8_t *mem_buf
, int n
)
348 if (n
< ARRAY_SIZE(csr_register_map
)) {
349 target_ulong val
= 0;
352 result
= riscv_csrrw_debug(env
, csr_register_map
[n
], &val
, 0, 0);
354 return gdb_get_regl(mem_buf
, val
);
360 static int riscv_gdb_set_csr(CPURISCVState
*env
, uint8_t *mem_buf
, int n
)
362 if (n
< ARRAY_SIZE(csr_register_map
)) {
363 target_ulong val
= ldtul_p(mem_buf
);
366 result
= riscv_csrrw_debug(env
, csr_register_map
[n
], NULL
, val
, -1);
368 return sizeof(target_ulong
);
374 void riscv_cpu_register_gdb_regs_for_features(CPUState
*cs
)
376 RISCVCPU
*cpu
= RISCV_CPU(cs
);
377 CPURISCVState
*env
= &cpu
->env
;
378 #if defined(TARGET_RISCV32)
379 if (env
->misa
& RVF
) {
380 gdb_register_coprocessor(cs
, riscv_gdb_get_fpu
, riscv_gdb_set_fpu
,
381 36, "riscv-32bit-fpu.xml", 0);
384 gdb_register_coprocessor(cs
, riscv_gdb_get_csr
, riscv_gdb_set_csr
,
385 4096, "riscv-32bit-csr.xml", 0);
386 #elif defined(TARGET_RISCV64)
387 if (env
->misa
& RVF
) {
388 gdb_register_coprocessor(cs
, riscv_gdb_get_fpu
, riscv_gdb_set_fpu
,
389 36, "riscv-64bit-fpu.xml", 0);
392 gdb_register_coprocessor(cs
, riscv_gdb_get_csr
, riscv_gdb_set_csr
,
393 4096, "riscv-64bit-csr.xml", 0);