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
[] = {
273 int riscv_cpu_gdb_read_register(CPUState
*cs
, GByteArray
*mem_buf
, int n
)
275 RISCVCPU
*cpu
= RISCV_CPU(cs
);
276 CPURISCVState
*env
= &cpu
->env
;
279 return gdb_get_regl(mem_buf
, env
->gpr
[n
]);
280 } else if (n
== 32) {
281 return gdb_get_regl(mem_buf
, env
->pc
);
286 int riscv_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
288 RISCVCPU
*cpu
= RISCV_CPU(cs
);
289 CPURISCVState
*env
= &cpu
->env
;
292 /* discard writes to x0 */
293 return sizeof(target_ulong
);
295 env
->gpr
[n
] = ldtul_p(mem_buf
);
296 return sizeof(target_ulong
);
297 } else if (n
== 32) {
298 env
->pc
= ldtul_p(mem_buf
);
299 return sizeof(target_ulong
);
304 static int riscv_gdb_get_fpu(CPURISCVState
*env
, GByteArray
*buf
, int n
)
307 if (env
->misa
& RVD
) {
308 return gdb_get_reg64(buf
, env
->fpr
[n
]);
310 if (env
->misa
& RVF
) {
311 return gdb_get_reg32(buf
, env
->fpr
[n
]);
313 /* there is hole between ft11 and fflags in fpu.xml */
314 } else if (n
< 36 && n
> 32) {
315 target_ulong val
= 0;
318 * CSR_FFLAGS is at index 8 in csr_register, and gdb says it is FP
319 * register 33, so we recalculate the map index.
320 * This also works for CSR_FRM and CSR_FCSR.
322 result
= riscv_csrrw_debug(env
, n
- 33 + csr_register_map
[8], &val
,
325 return gdb_get_regl(buf
, val
);
331 static int riscv_gdb_set_fpu(CPURISCVState
*env
, uint8_t *mem_buf
, int n
)
334 env
->fpr
[n
] = ldq_p(mem_buf
); /* always 64-bit */
335 return sizeof(uint64_t);
336 /* there is hole between ft11 and fflags in fpu.xml */
337 } else if (n
< 36 && n
> 32) {
338 target_ulong val
= ldtul_p(mem_buf
);
341 * CSR_FFLAGS is at index 8 in csr_register, and gdb says it is FP
342 * register 33, so we recalculate the map index.
343 * This also works for CSR_FRM and CSR_FCSR.
345 result
= riscv_csrrw_debug(env
, n
- 33 + csr_register_map
[8], NULL
,
348 return sizeof(target_ulong
);
354 static int riscv_gdb_get_csr(CPURISCVState
*env
, GByteArray
*buf
, int n
)
356 if (n
< ARRAY_SIZE(csr_register_map
)) {
357 target_ulong val
= 0;
360 result
= riscv_csrrw_debug(env
, csr_register_map
[n
], &val
, 0, 0);
362 return gdb_get_regl(buf
, val
);
368 static int riscv_gdb_set_csr(CPURISCVState
*env
, uint8_t *mem_buf
, int n
)
370 if (n
< ARRAY_SIZE(csr_register_map
)) {
371 target_ulong val
= ldtul_p(mem_buf
);
374 result
= riscv_csrrw_debug(env
, csr_register_map
[n
], NULL
, val
, -1);
376 return sizeof(target_ulong
);
382 static int riscv_gdb_get_virtual(CPURISCVState
*cs
, GByteArray
*buf
, int n
)
385 #ifdef CONFIG_USER_ONLY
386 return gdb_get_regl(buf
, 0);
388 return gdb_get_regl(buf
, cs
->priv
);
394 static int riscv_gdb_set_virtual(CPURISCVState
*cs
, uint8_t *mem_buf
, int n
)
397 #ifndef CONFIG_USER_ONLY
398 cs
->priv
= ldtul_p(mem_buf
) & 0x3;
399 if (cs
->priv
== PRV_H
) {
403 return sizeof(target_ulong
);
408 void riscv_cpu_register_gdb_regs_for_features(CPUState
*cs
)
410 RISCVCPU
*cpu
= RISCV_CPU(cs
);
411 CPURISCVState
*env
= &cpu
->env
;
412 if (env
->misa
& RVD
) {
413 gdb_register_coprocessor(cs
, riscv_gdb_get_fpu
, riscv_gdb_set_fpu
,
414 36, "riscv-64bit-fpu.xml", 0);
415 } else if (env
->misa
& RVF
) {
416 gdb_register_coprocessor(cs
, riscv_gdb_get_fpu
, riscv_gdb_set_fpu
,
417 36, "riscv-32bit-fpu.xml", 0);
419 #if defined(TARGET_RISCV32)
420 gdb_register_coprocessor(cs
, riscv_gdb_get_csr
, riscv_gdb_set_csr
,
421 240, "riscv-32bit-csr.xml", 0);
423 gdb_register_coprocessor(cs
, riscv_gdb_get_virtual
, riscv_gdb_set_virtual
,
424 1, "riscv-32bit-virtual.xml", 0);
425 #elif defined(TARGET_RISCV64)
426 gdb_register_coprocessor(cs
, riscv_gdb_get_csr
, riscv_gdb_set_csr
,
427 240, "riscv-64bit-csr.xml", 0);
429 gdb_register_coprocessor(cs
, riscv_gdb_get_virtual
, riscv_gdb_set_virtual
,
430 1, "riscv-64bit-virtual.xml", 0);