xref: /openbmc/qemu/target/loongarch/gdbstub.c (revision 44602af8)
1 /*
2  * LOONGARCH gdb server stub
3  *
4  * Copyright (c) 2021 Loongson Technology Corporation Limited
5  *
6  * SPDX-License-Identifier: LGPL-2.1+
7  */
8 
9 #include "qemu/osdep.h"
10 #include "cpu.h"
11 #include "internals.h"
12 #include "exec/gdbstub.h"
13 
14 int loongarch_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
15 {
16     LoongArchCPU *cpu = LOONGARCH_CPU(cs);
17     CPULoongArchState *env = &cpu->env;
18 
19     if (0 <= n && n < 32) {
20         return gdb_get_regl(mem_buf, env->gpr[n]);
21     } else if (n == 32) {
22         return gdb_get_regl(mem_buf, env->pc);
23     } else if (n == 33) {
24         return gdb_get_regl(mem_buf, env->CSR_BADV);
25     }
26     return 0;
27 }
28 
29 int loongarch_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
30 {
31     LoongArchCPU *cpu = LOONGARCH_CPU(cs);
32     CPULoongArchState *env = &cpu->env;
33     target_ulong tmp = ldtul_p(mem_buf);
34     int length = 0;
35 
36     if (0 <= n && n < 32) {
37         env->gpr[n] = tmp;
38         length = sizeof(target_ulong);
39     } else if (n == 32) {
40         env->pc = tmp;
41         length = sizeof(target_ulong);
42     }
43     return length;
44 }
45 
46 static int loongarch_gdb_get_fpu(CPULoongArchState *env,
47                                  GByteArray *mem_buf, int n)
48 {
49     if (0 <= n && n < 32) {
50         return gdb_get_reg64(mem_buf, env->fpr[n]);
51     } else if (32 <= n && n < 40) {
52         return gdb_get_reg8(mem_buf, env->cf[n - 32]);
53     } else if (n == 40) {
54         return gdb_get_reg32(mem_buf, env->fcsr0);
55     }
56     return 0;
57 }
58 
59 static int loongarch_gdb_set_fpu(CPULoongArchState *env,
60                                  uint8_t *mem_buf, int n)
61 {
62     int length = 0;
63 
64     if (0 <= n && n < 32) {
65         env->fpr[n] = ldq_p(mem_buf);
66         length = 8;
67     } else if (32 <= n && n < 40) {
68         env->cf[n - 32] = ldub_p(mem_buf);
69         length = 1;
70     } else if (n == 40) {
71         env->fcsr0 = ldl_p(mem_buf);
72         length = 4;
73     }
74     return length;
75 }
76 
77 void loongarch_cpu_register_gdb_regs_for_features(CPUState *cs)
78 {
79     gdb_register_coprocessor(cs, loongarch_gdb_get_fpu, loongarch_gdb_set_fpu,
80                              41, "loongarch-fpu64.xml", 0);
81 }
82