| 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 | #include "gdbstub/helpers.h" |
| 14 | #include "vec.h" |
| 15 | |
| 16 | uint64_t read_fcc(CPULoongArchState *env) |
| 17 | { |
| 18 | uint64_t ret = 0; |
| 19 | |
| 20 | for (int i = 0; i < 8; ++i) { |
| 21 | ret |= (uint64_t)env->cf[i] << (i * 8); |
| 22 | } |
| 23 | |
| 24 | return ret; |
| 25 | } |
| 26 | |
| 27 | void write_fcc(CPULoongArchState *env, uint64_t val) |
| 28 | { |
| 29 | for (int i = 0; i < 8; ++i) { |
| 30 | env->cf[i] = (val >> (i * 8)) & 1; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | int loongarch_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n) |
| 35 | { |
| 36 | CPULoongArchState *env = cpu_env(cs); |
| 37 | CPUSysState *sys = env_sys(env); |
| 38 | |
| 39 | if (0 <= n && n <= 34) { |
| 40 | uint64_t val; |
| 41 | |
| 42 | if (n < 32) { |
| 43 | val = env->gpr[n]; |
| 44 | } else if (n == 32) { |
| 45 | /* orig_a0 */ |
| 46 | val = 0; |
| 47 | } else if (n == 33) { |
| 48 | val = env->pc; |
| 49 | } else /* if (n == 34) */ { |
| 50 | val = sys->CSR_BADV; |
| 51 | } |
| 52 | |
| 53 | if (is_la64(env)) { |
| 54 | return gdb_get_reg64(mem_buf, val); |
| 55 | } else { |
| 56 | return gdb_get_reg32(mem_buf, val); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int loongarch_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) |
| 64 | { |
| 65 | CPULoongArchState *env = cpu_env(cs); |
| 66 | uint64_t tmp; |
| 67 | int length = 0; |
| 68 | |
| 69 | if (n < 0 || n > 34) { |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | if (is_la64(env)) { |
| 74 | tmp = ldq_le_p(mem_buf); |
| 75 | length = 8; |
| 76 | } else { |
| 77 | tmp = ldl_le_p(mem_buf); |
| 78 | length = 4; |
| 79 | } |
| 80 | |
| 81 | if (0 <= n && n < 32) { |
| 82 | env->gpr[n] = tmp; |
| 83 | } else if (n == 33) { |
| 84 | set_pc(env, tmp); |
| 85 | } |
| 86 | return length; |
| 87 | } |
| 88 | |
| 89 | static int loongarch_gdb_get_fpu(CPUState *cs, GByteArray *mem_buf, int n) |
| 90 | { |
| 91 | LoongArchCPU *cpu = LOONGARCH_CPU(cs); |
| 92 | CPULoongArchState *env = &cpu->env; |
| 93 | |
| 94 | if (0 <= n && n < 32) { |
| 95 | return gdb_get_reg64(mem_buf, env->fpr[n].vreg.D(0)); |
| 96 | } else if (32 <= n && n < 40) { |
| 97 | return gdb_get_reg8(mem_buf, env->cf[n - 32]); |
| 98 | } else if (n == 40) { |
| 99 | return gdb_get_reg32(mem_buf, env->fcsr0); |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int loongarch_gdb_set_fpu(CPUState *cs, uint8_t *mem_buf, int n) |
| 105 | { |
| 106 | LoongArchCPU *cpu = LOONGARCH_CPU(cs); |
| 107 | CPULoongArchState *env = &cpu->env; |
| 108 | int length = 0; |
| 109 | |
| 110 | if (0 <= n && n < 32) { |
| 111 | env->fpr[n].vreg.D(0) = ldq_le_p(mem_buf); |
| 112 | length = 8; |
| 113 | } else if (32 <= n && n < 40) { |
| 114 | env->cf[n - 32] = ldub_p(mem_buf); |
| 115 | length = 1; |
| 116 | } else if (n == 40) { |
| 117 | env->fcsr0 = ldl_le_p(mem_buf); |
| 118 | length = 4; |
| 119 | } |
| 120 | return length; |
| 121 | } |
| 122 | |
| 123 | #define VREG_NUM 32 |
| 124 | #define REG64_LEN 64 |
| 125 | |
| 126 | static int loongarch_gdb_get_vec(CPUState *cs, GByteArray *mem_buf, int n, int vl) |
| 127 | { |
| 128 | LoongArchCPU *cpu = LOONGARCH_CPU(cs); |
| 129 | CPULoongArchState *env = &cpu->env; |
| 130 | int i, length = 0; |
| 131 | |
| 132 | if (0 <= n && n < VREG_NUM) { |
| 133 | for (i = 0; i < vl / REG64_LEN; i++) { |
| 134 | length += gdb_get_reg64(mem_buf, env->fpr[n].vreg.D(i)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return length; |
| 139 | } |
| 140 | |
| 141 | static int loongarch_gdb_set_vec(CPUState *cs, uint8_t *mem_buf, int n, int vl) |
| 142 | { |
| 143 | LoongArchCPU *cpu = LOONGARCH_CPU(cs); |
| 144 | CPULoongArchState *env = &cpu->env; |
| 145 | int i, length = 0; |
| 146 | |
| 147 | if (0 <= n && n < VREG_NUM) { |
| 148 | for (i = 0; i < vl / REG64_LEN; i++) { |
| 149 | env->fpr[n].vreg.D(i) = ldq_le_p(mem_buf + 8 * i); |
| 150 | length += 8; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return length; |
| 155 | } |
| 156 | |
| 157 | static int loongarch_gdb_get_lsx(CPUState *cs, GByteArray *mem_buf, int n) |
| 158 | { |
| 159 | return loongarch_gdb_get_vec(cs, mem_buf, n, LSX_LEN); |
| 160 | } |
| 161 | |
| 162 | static int loongarch_gdb_set_lsx(CPUState *cs, uint8_t *mem_buf, int n) |
| 163 | { |
| 164 | return loongarch_gdb_set_vec(cs, mem_buf, n, LSX_LEN); |
| 165 | } |
| 166 | |
| 167 | static int loongarch_gdb_get_lasx(CPUState *cs, GByteArray *mem_buf, int n) |
| 168 | { |
| 169 | return loongarch_gdb_get_vec(cs, mem_buf, n, LASX_LEN); |
| 170 | } |
| 171 | |
| 172 | static int loongarch_gdb_set_lasx(CPUState *cs, uint8_t *mem_buf, int n) |
| 173 | { |
| 174 | return loongarch_gdb_set_vec(cs, mem_buf, n, LASX_LEN); |
| 175 | } |
| 176 | |
| 177 | void loongarch_cpu_register_gdb_regs_for_features(CPUState *cs) |
| 178 | { |
| 179 | LoongArchCPU *cpu = LOONGARCH_CPU(cs); |
| 180 | CPULoongArchState *env = &cpu->env; |
| 181 | |
| 182 | if (FIELD_EX32(env->cpucfg[2], CPUCFG2, FP)) { |
| 183 | gdb_register_coprocessor(cs, loongarch_gdb_get_fpu, loongarch_gdb_set_fpu, |
| 184 | gdb_find_static_feature("loongarch-fpu.xml")); |
| 185 | } |
| 186 | |
| 187 | if (FIELD_EX32(env->cpucfg[2], CPUCFG2, LSX)) { |
| 188 | gdb_register_coprocessor(cs, loongarch_gdb_get_lsx, loongarch_gdb_set_lsx, |
| 189 | gdb_find_static_feature("loongarch-lsx.xml")); |
| 190 | } |
| 191 | |
| 192 | if (FIELD_EX32(env->cpucfg[2], CPUCFG2, LASX)) { |
| 193 | gdb_register_coprocessor(cs, loongarch_gdb_get_lasx, loongarch_gdb_set_lasx, |
| 194 | gdb_find_static_feature("loongarch-lasx.xml")); |
| 195 | } |
| 196 | } |