Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 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" |
Alex Bennée | 4ea5fe9 | 2023-03-02 18:57:56 -0800 | [diff] [blame] | 13 | #include "gdbstub/helpers.h" |
Song Gao | 008a3b1 | 2023-09-14 10:25:59 +0800 | [diff] [blame] | 14 | #include "vec.h" |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 15 | |
Song Gao | 2f149c7 | 2022-08-05 11:35:23 +0800 | [diff] [blame] | 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 | |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 34 | int loongarch_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n) |
| 35 | { |
Philippe Mathieu-Daudé | f3b603b | 2024-01-29 17:44:57 +0100 | [diff] [blame] | 36 | CPULoongArchState *env = cpu_env(cs); |
Jiajie Chen | ebda303 | 2023-08-21 14:59:59 +0200 | [diff] [blame] | 37 | |
| 38 | if (0 <= n && n <= 34) { |
Marc-André Lureau | 3cd804c | 2024-09-24 15:49:47 +0400 | [diff] [blame] | 39 | uint64_t val; |
| 40 | |
| 41 | if (n < 32) { |
| 42 | val = env->gpr[n]; |
| 43 | } else if (n == 32) { |
| 44 | /* orig_a0 */ |
| 45 | val = 0; |
| 46 | } else if (n == 33) { |
| 47 | val = env->pc; |
| 48 | } else /* if (n == 34) */ { |
| 49 | val = env->CSR_BADV; |
| 50 | } |
| 51 | |
Jiajie Chen | ebda303 | 2023-08-21 14:59:59 +0200 | [diff] [blame] | 52 | if (is_la64(env)) { |
| 53 | return gdb_get_reg64(mem_buf, val); |
| 54 | } else { |
| 55 | return gdb_get_reg32(mem_buf, val); |
| 56 | } |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 57 | } |
Marc-André Lureau | 3cd804c | 2024-09-24 15:49:47 +0400 | [diff] [blame] | 58 | |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int loongarch_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) |
| 63 | { |
Philippe Mathieu-Daudé | f3b603b | 2024-01-29 17:44:57 +0100 | [diff] [blame] | 64 | CPULoongArchState *env = cpu_env(cs); |
Jiajie Chen | ebda303 | 2023-08-21 14:59:59 +0200 | [diff] [blame] | 65 | target_ulong tmp; |
| 66 | int read_length; |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 67 | int length = 0; |
| 68 | |
Jiajie Chen | ebda303 | 2023-08-21 14:59:59 +0200 | [diff] [blame] | 69 | if (is_la64(env)) { |
| 70 | tmp = ldq_p(mem_buf); |
| 71 | read_length = 8; |
| 72 | } else { |
| 73 | tmp = ldl_p(mem_buf); |
| 74 | read_length = 4; |
| 75 | } |
| 76 | |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 77 | if (0 <= n && n < 32) { |
| 78 | env->gpr[n] = tmp; |
Jiajie Chen | ebda303 | 2023-08-21 14:59:59 +0200 | [diff] [blame] | 79 | length = read_length; |
Song Gao | 1fe8ac3 | 2022-08-05 11:35:19 +0800 | [diff] [blame] | 80 | } else if (n == 33) { |
Jiajie Chen | 2f6478f | 2023-08-22 09:13:54 +0200 | [diff] [blame] | 81 | set_pc(env, tmp); |
Jiajie Chen | ebda303 | 2023-08-21 14:59:59 +0200 | [diff] [blame] | 82 | length = read_length; |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 83 | } |
| 84 | return length; |
| 85 | } |
| 86 | |
Akihiko Odaki | 6626015 | 2024-02-27 14:43:16 +0000 | [diff] [blame] | 87 | static int loongarch_gdb_get_fpu(CPUState *cs, GByteArray *mem_buf, int n) |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 88 | { |
Akihiko Odaki | 6626015 | 2024-02-27 14:43:16 +0000 | [diff] [blame] | 89 | LoongArchCPU *cpu = LOONGARCH_CPU(cs); |
| 90 | CPULoongArchState *env = &cpu->env; |
| 91 | |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 92 | if (0 <= n && n < 32) { |
Song Gao | 16f5396 | 2023-05-04 20:27:27 +0800 | [diff] [blame] | 93 | return gdb_get_reg64(mem_buf, env->fpr[n].vreg.D(0)); |
Jiajie Chen | 17ffe33 | 2023-08-08 13:42:47 +0800 | [diff] [blame] | 94 | } else if (32 <= n && n < 40) { |
| 95 | return gdb_get_reg8(mem_buf, env->cf[n - 32]); |
| 96 | } else if (n == 40) { |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 97 | return gdb_get_reg32(mem_buf, env->fcsr0); |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
Akihiko Odaki | 6626015 | 2024-02-27 14:43:16 +0000 | [diff] [blame] | 102 | static int loongarch_gdb_set_fpu(CPUState *cs, uint8_t *mem_buf, int n) |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 103 | { |
Akihiko Odaki | 6626015 | 2024-02-27 14:43:16 +0000 | [diff] [blame] | 104 | LoongArchCPU *cpu = LOONGARCH_CPU(cs); |
| 105 | CPULoongArchState *env = &cpu->env; |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 106 | int length = 0; |
| 107 | |
| 108 | if (0 <= n && n < 32) { |
Song Gao | 16f5396 | 2023-05-04 20:27:27 +0800 | [diff] [blame] | 109 | env->fpr[n].vreg.D(0) = ldq_p(mem_buf); |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 110 | length = 8; |
Jiajie Chen | 17ffe33 | 2023-08-08 13:42:47 +0800 | [diff] [blame] | 111 | } else if (32 <= n && n < 40) { |
| 112 | env->cf[n - 32] = ldub_p(mem_buf); |
| 113 | length = 1; |
| 114 | } else if (n == 40) { |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 115 | env->fcsr0 = ldl_p(mem_buf); |
| 116 | length = 4; |
| 117 | } |
| 118 | return length; |
| 119 | } |
| 120 | |
Song Gao | 1c15dd6 | 2024-07-11 10:44:54 +0800 | [diff] [blame] | 121 | #define VREG_NUM 32 |
| 122 | #define REG64_LEN 64 |
| 123 | |
| 124 | static int loongarch_gdb_get_vec(CPUState *cs, GByteArray *mem_buf, int n, int vl) |
| 125 | { |
| 126 | LoongArchCPU *cpu = LOONGARCH_CPU(cs); |
| 127 | CPULoongArchState *env = &cpu->env; |
| 128 | int i, length = 0; |
| 129 | |
| 130 | if (0 <= n && n < VREG_NUM) { |
| 131 | for (i = 0; i < vl / REG64_LEN; i++) { |
| 132 | length += gdb_get_reg64(mem_buf, env->fpr[n].vreg.D(i)); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return length; |
| 137 | } |
| 138 | |
| 139 | static int loongarch_gdb_set_vec(CPUState *cs, uint8_t *mem_buf, int n, int vl) |
| 140 | { |
| 141 | LoongArchCPU *cpu = LOONGARCH_CPU(cs); |
| 142 | CPULoongArchState *env = &cpu->env; |
| 143 | int i, length = 0; |
| 144 | |
| 145 | if (0 <= n && n < VREG_NUM) { |
| 146 | for (i = 0; i < vl / REG64_LEN; i++) { |
| 147 | env->fpr[n].vreg.D(i) = ldq_le_p(mem_buf + 8 * i); |
| 148 | length += 8; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return length; |
| 153 | } |
| 154 | |
| 155 | static int loongarch_gdb_get_lsx(CPUState *cs, GByteArray *mem_buf, int n) |
| 156 | { |
| 157 | return loongarch_gdb_get_vec(cs, mem_buf, n, LSX_LEN); |
| 158 | } |
| 159 | |
| 160 | static int loongarch_gdb_set_lsx(CPUState *cs, uint8_t *mem_buf, int n) |
| 161 | { |
| 162 | return loongarch_gdb_set_vec(cs, mem_buf, n, LSX_LEN); |
| 163 | } |
| 164 | |
| 165 | static int loongarch_gdb_get_lasx(CPUState *cs, GByteArray *mem_buf, int n) |
| 166 | { |
| 167 | return loongarch_gdb_get_vec(cs, mem_buf, n, LASX_LEN); |
| 168 | } |
| 169 | |
| 170 | static int loongarch_gdb_set_lasx(CPUState *cs, uint8_t *mem_buf, int n) |
| 171 | { |
| 172 | return loongarch_gdb_set_vec(cs, mem_buf, n, LASX_LEN); |
| 173 | } |
| 174 | |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 175 | void loongarch_cpu_register_gdb_regs_for_features(CPUState *cs) |
| 176 | { |
Song Gao | 1c15dd6 | 2024-07-11 10:44:54 +0800 | [diff] [blame] | 177 | LoongArchCPU *cpu = LOONGARCH_CPU(cs); |
| 178 | CPULoongArchState *env = &cpu->env; |
| 179 | |
| 180 | if (FIELD_EX32(env->cpucfg[2], CPUCFG2, FP)) { |
| 181 | gdb_register_coprocessor(cs, loongarch_gdb_get_fpu, loongarch_gdb_set_fpu, |
| 182 | gdb_find_static_feature("loongarch-fpu.xml"), 0); |
| 183 | } |
| 184 | |
| 185 | if (FIELD_EX32(env->cpucfg[2], CPUCFG2, LSX)) { |
| 186 | gdb_register_coprocessor(cs, loongarch_gdb_get_lsx, loongarch_gdb_set_lsx, |
| 187 | gdb_find_static_feature("loongarch-lsx.xml"), 0); |
| 188 | } |
| 189 | |
| 190 | if (FIELD_EX32(env->cpucfg[2], CPUCFG2, LASX)) { |
| 191 | gdb_register_coprocessor(cs, loongarch_gdb_get_lasx, loongarch_gdb_set_lasx, |
| 192 | gdb_find_static_feature("loongarch-lasx.xml"), 0); |
| 193 | } |
Xiaojuan Yang | ca61e75 | 2022-06-06 20:43:31 +0800 | [diff] [blame] | 194 | } |