xref: /openbmc/qemu/target/arm/gdbstub64.c (revision abf1f1b0)
1 /*
2  * ARM gdb server stub: AArch64 specific functions.
3  *
4  * Copyright (c) 2013 SUSE LINUX Products GmbH
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 #include "qemu/log.h"
21 #include "cpu.h"
22 #include "internals.h"
23 #include "exec/gdbstub.h"
24 
25 int aarch64_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
26 {
27     ARMCPU *cpu = ARM_CPU(cs);
28     CPUARMState *env = &cpu->env;
29 
30     if (n < 31) {
31         /* Core integer register.  */
32         return gdb_get_reg64(mem_buf, env->xregs[n]);
33     }
34     switch (n) {
35     case 31:
36         return gdb_get_reg64(mem_buf, env->xregs[31]);
37     case 32:
38         return gdb_get_reg64(mem_buf, env->pc);
39     case 33:
40         return gdb_get_reg32(mem_buf, pstate_read(env));
41     }
42     /* Unknown register.  */
43     return 0;
44 }
45 
46 int aarch64_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
47 {
48     ARMCPU *cpu = ARM_CPU(cs);
49     CPUARMState *env = &cpu->env;
50     uint64_t tmp;
51 
52     tmp = ldq_p(mem_buf);
53 
54     if (n < 31) {
55         /* Core integer register.  */
56         env->xregs[n] = tmp;
57         return 8;
58     }
59     switch (n) {
60     case 31:
61         env->xregs[31] = tmp;
62         return 8;
63     case 32:
64         env->pc = tmp;
65         return 8;
66     case 33:
67         /* CPSR */
68         pstate_write(env, tmp);
69         return 4;
70     }
71     /* Unknown register.  */
72     return 0;
73 }
74 
75 int aarch64_gdb_get_fpu_reg(CPUARMState *env, GByteArray *buf, int reg)
76 {
77     switch (reg) {
78     case 0 ... 31:
79     {
80         /* 128 bit FP register - quads are in LE order */
81         uint64_t *q = aa64_vfp_qreg(env, reg);
82         return gdb_get_reg128(buf, q[1], q[0]);
83     }
84     case 32:
85         /* FPSR */
86         return gdb_get_reg32(buf, vfp_get_fpsr(env));
87     case 33:
88         /* FPCR */
89         return gdb_get_reg32(buf, vfp_get_fpcr(env));
90     default:
91         return 0;
92     }
93 }
94 
95 int aarch64_gdb_set_fpu_reg(CPUARMState *env, uint8_t *buf, int reg)
96 {
97     switch (reg) {
98     case 0 ... 31:
99         /* 128 bit FP register */
100         {
101             uint64_t *q = aa64_vfp_qreg(env, reg);
102             q[0] = ldq_le_p(buf);
103             q[1] = ldq_le_p(buf + 8);
104             return 16;
105         }
106     case 32:
107         /* FPSR */
108         vfp_set_fpsr(env, ldl_p(buf));
109         return 4;
110     case 33:
111         /* FPCR */
112         vfp_set_fpcr(env, ldl_p(buf));
113         return 4;
114     default:
115         return 0;
116     }
117 }
118 
119 int aarch64_gdb_get_sve_reg(CPUARMState *env, GByteArray *buf, int reg)
120 {
121     ARMCPU *cpu = env_archcpu(env);
122 
123     switch (reg) {
124     /* The first 32 registers are the zregs */
125     case 0 ... 31:
126     {
127         int vq, len = 0;
128         for (vq = 0; vq < cpu->sve_max_vq; vq++) {
129             len += gdb_get_reg128(buf,
130                                   env->vfp.zregs[reg].d[vq * 2 + 1],
131                                   env->vfp.zregs[reg].d[vq * 2]);
132         }
133         return len;
134     }
135     case 32:
136         return gdb_get_reg32(buf, vfp_get_fpsr(env));
137     case 33:
138         return gdb_get_reg32(buf, vfp_get_fpcr(env));
139     /* then 16 predicates and the ffr */
140     case 34 ... 50:
141     {
142         int preg = reg - 34;
143         int vq, len = 0;
144         for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
145             len += gdb_get_reg64(buf, env->vfp.pregs[preg].p[vq / 4]);
146         }
147         return len;
148     }
149     case 51:
150     {
151         /*
152          * We report in Vector Granules (VG) which is 64bit in a Z reg
153          * while the ZCR works in Vector Quads (VQ) which is 128bit chunks.
154          */
155         int vq = sve_vqm1_for_el(env, arm_current_el(env)) + 1;
156         return gdb_get_reg64(buf, vq * 2);
157     }
158     default:
159         /* gdbstub asked for something out our range */
160         qemu_log_mask(LOG_UNIMP, "%s: out of range register %d", __func__, reg);
161         break;
162     }
163 
164     return 0;
165 }
166 
167 int aarch64_gdb_set_sve_reg(CPUARMState *env, uint8_t *buf, int reg)
168 {
169     ARMCPU *cpu = env_archcpu(env);
170 
171     /* The first 32 registers are the zregs */
172     switch (reg) {
173     /* The first 32 registers are the zregs */
174     case 0 ... 31:
175     {
176         int vq, len = 0;
177         uint64_t *p = (uint64_t *) buf;
178         for (vq = 0; vq < cpu->sve_max_vq; vq++) {
179             env->vfp.zregs[reg].d[vq * 2 + 1] = *p++;
180             env->vfp.zregs[reg].d[vq * 2] = *p++;
181             len += 16;
182         }
183         return len;
184     }
185     case 32:
186         vfp_set_fpsr(env, *(uint32_t *)buf);
187         return 4;
188     case 33:
189         vfp_set_fpcr(env, *(uint32_t *)buf);
190         return 4;
191     case 34 ... 50:
192     {
193         int preg = reg - 34;
194         int vq, len = 0;
195         uint64_t *p = (uint64_t *) buf;
196         for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
197             env->vfp.pregs[preg].p[vq / 4] = *p++;
198             len += 8;
199         }
200         return len;
201     }
202     case 51:
203         /* cannot set vg via gdbstub */
204         return 0;
205     default:
206         /* gdbstub asked for something out our range */
207         break;
208     }
209 
210     return 0;
211 }
212 
213 static void output_vector_union_type(GString *s, int reg_width,
214                                      const char *name)
215 {
216     struct TypeSize {
217         const char *gdb_type;
218         short size;
219         char sz, suffix;
220     };
221 
222     static const struct TypeSize vec_lanes[] = {
223         /* quads */
224         { "uint128", 128, 'q', 'u' },
225         { "int128", 128, 'q', 's' },
226         /* 64 bit */
227         { "ieee_double", 64, 'd', 'f' },
228         { "uint64", 64, 'd', 'u' },
229         { "int64", 64, 'd', 's' },
230         /* 32 bit */
231         { "ieee_single", 32, 's', 'f' },
232         { "uint32", 32, 's', 'u' },
233         { "int32", 32, 's', 's' },
234         /* 16 bit */
235         { "ieee_half", 16, 'h', 'f' },
236         { "uint16", 16, 'h', 'u' },
237         { "int16", 16, 'h', 's' },
238         /* bytes */
239         { "uint8", 8, 'b', 'u' },
240         { "int8", 8, 'b', 's' },
241     };
242 
243     static const char suf[] = { 'b', 'h', 's', 'd', 'q' };
244     int i, j;
245 
246     /* First define types and totals in a whole VL */
247     for (i = 0; i < ARRAY_SIZE(vec_lanes); i++) {
248         g_string_append_printf(s,
249                                "<vector id=\"%s%c%c\" type=\"%s\" count=\"%d\"/>",
250                                name, vec_lanes[i].sz, vec_lanes[i].suffix,
251                                vec_lanes[i].gdb_type, reg_width / vec_lanes[i].size);
252     }
253 
254     /*
255      * Now define a union for each size group containing unsigned and
256      * signed and potentially float versions of each size from 128 to
257      * 8 bits.
258      */
259     for (i = 0; i < ARRAY_SIZE(suf); i++) {
260         int bits = 8 << i;
261 
262         g_string_append_printf(s, "<union id=\"%sn%c\">", name, suf[i]);
263         for (j = 0; j < ARRAY_SIZE(vec_lanes); j++) {
264             if (vec_lanes[j].size == bits) {
265                 g_string_append_printf(s, "<field name=\"%c\" type=\"%s%c%c\"/>",
266                                        vec_lanes[j].suffix, name,
267                                        vec_lanes[j].sz, vec_lanes[j].suffix);
268             }
269         }
270         g_string_append(s, "</union>");
271     }
272 
273     /* And now the final union of unions */
274     g_string_append_printf(s, "<union id=\"%s\">", name);
275     for (i = ARRAY_SIZE(suf) - 1; i >= 0; i--) {
276         g_string_append_printf(s, "<field name=\"%c\" type=\"%sn%c\"/>",
277                                suf[i], name, suf[i]);
278     }
279     g_string_append(s, "</union>");
280 }
281 
282 int arm_gen_dynamic_svereg_xml(CPUState *cs, int orig_base_reg)
283 {
284     ARMCPU *cpu = ARM_CPU(cs);
285     GString *s = g_string_new(NULL);
286     DynamicGDBXMLInfo *info = &cpu->dyn_svereg_xml;
287     int reg_width = cpu->sve_max_vq * 128;
288     int pred_width = cpu->sve_max_vq * 16;
289     int base_reg = orig_base_reg;
290     int i;
291 
292     g_string_printf(s, "<?xml version=\"1.0\"?>");
293     g_string_append_printf(s, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
294     g_string_append_printf(s, "<feature name=\"org.gnu.gdb.aarch64.sve\">");
295 
296     /* Create the vector union type. */
297     output_vector_union_type(s, reg_width, "svev");
298 
299     /* Create the predicate vector type. */
300     g_string_append_printf(s,
301                            "<vector id=\"svep\" type=\"uint8\" count=\"%d\"/>",
302                            pred_width / 8);
303 
304     /* Define the vector registers. */
305     for (i = 0; i < 32; i++) {
306         g_string_append_printf(s,
307                                "<reg name=\"z%d\" bitsize=\"%d\""
308                                " regnum=\"%d\" type=\"svev\"/>",
309                                i, reg_width, base_reg++);
310     }
311 
312     /* fpscr & status registers */
313     g_string_append_printf(s, "<reg name=\"fpsr\" bitsize=\"32\""
314                            " regnum=\"%d\" group=\"float\""
315                            " type=\"int\"/>", base_reg++);
316     g_string_append_printf(s, "<reg name=\"fpcr\" bitsize=\"32\""
317                            " regnum=\"%d\" group=\"float\""
318                            " type=\"int\"/>", base_reg++);
319 
320     /* Define the predicate registers. */
321     for (i = 0; i < 16; i++) {
322         g_string_append_printf(s,
323                                "<reg name=\"p%d\" bitsize=\"%d\""
324                                " regnum=\"%d\" type=\"svep\"/>",
325                                i, pred_width, base_reg++);
326     }
327     g_string_append_printf(s,
328                            "<reg name=\"ffr\" bitsize=\"%d\""
329                            " regnum=\"%d\" group=\"vector\""
330                            " type=\"svep\"/>",
331                            pred_width, base_reg++);
332 
333     /* Define the vector length pseudo-register. */
334     g_string_append_printf(s,
335                            "<reg name=\"vg\" bitsize=\"64\""
336                            " regnum=\"%d\" type=\"int\"/>",
337                            base_reg++);
338 
339     g_string_append_printf(s, "</feature>");
340 
341     info->desc = g_string_free(s, false);
342     info->num = base_reg - orig_base_reg;
343     return info->num;
344 }
345