gdbserver/linux-low: start turning linux target ops into methods
[binutils-gdb.git] / gdbserver / linux-tic6x-low.cc
1 /* Target dependent code for GDB on TI C6x systems.
2
3 Copyright (C) 2010-2020 Free Software Foundation, Inc.
4 Contributed by Andrew Jenner <andrew@codesourcery.com>
5 Contributed by Yao Qi <yao@codesourcery.com>
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "server.h"
23 #include "linux-low.h"
24 #include "arch/tic6x.h"
25 #include "tdesc.h"
26
27 #include "nat/gdb_ptrace.h"
28 #include <endian.h>
29
30 #include "gdb_proc_service.h"
31
32 #ifndef PTRACE_GET_THREAD_AREA
33 #define PTRACE_GET_THREAD_AREA 25
34 #endif
35
36 /* There are at most 69 registers accessible in ptrace. */
37 #define TIC6X_NUM_REGS 69
38
39 #include <asm/ptrace.h>
40
41 /* Linux target op definitions for the TI C6x architecture. */
42
43 class tic6x_target : public linux_process_target
44 {
45 public:
46
47 };
48
49 /* The singleton target ops object. */
50
51 static tic6x_target the_tic6x_target;
52
53 /* Defined in auto-generated file tic6x-c64xp-linux.c. */
54 void init_registers_tic6x_c64xp_linux (void);
55 extern const struct target_desc *tdesc_tic6x_c64xp_linux;
56
57 /* Defined in auto-generated file tic6x-c64x-linux.c. */
58 void init_registers_tic6x_c64x_linux (void);
59 extern const struct target_desc *tdesc_tic6x_c64x_linux;
60
61 /* Defined in auto-generated file tic62x-c6xp-linux.c. */
62 void init_registers_tic6x_c62x_linux (void);
63 extern const struct target_desc *tdesc_tic6x_c62x_linux;
64
65 union tic6x_register
66 {
67 unsigned char buf[4];
68
69 int reg32;
70 };
71
72 /* Return the ptrace ``address'' of register REGNO. */
73
74 #if __BYTE_ORDER == __BIG_ENDIAN
75 static int tic6x_regmap_c64xp[] = {
76 /* A0 - A15 */
77 53, 52, 55, 54, 57, 56, 59, 58,
78 61, 60, 63, 62, 65, 64, 67, 66,
79 /* B0 - B15 */
80 23, 22, 25, 24, 27, 26, 29, 28,
81 31, 30, 33, 32, 35, 34, 69, 68,
82 /* CSR PC */
83 5, 4,
84 /* A16 - A31 */
85 37, 36, 39, 38, 41, 40, 43, 42,
86 45, 44, 47, 46, 49, 48, 51, 50,
87 /* B16 - B31 */
88 7, 6, 9, 8, 11, 10, 13, 12,
89 15, 14, 17, 16, 19, 18, 21, 20,
90 /* TSR, ILC, RILC */
91 1, 2, 3
92 };
93
94 static int tic6x_regmap_c64x[] = {
95 /* A0 - A15 */
96 51, 50, 53, 52, 55, 54, 57, 56,
97 59, 58, 61, 60, 63, 62, 65, 64,
98 /* B0 - B15 */
99 21, 20, 23, 22, 25, 24, 27, 26,
100 29, 28, 31, 30, 33, 32, 67, 66,
101 /* CSR PC */
102 3, 2,
103 /* A16 - A31 */
104 35, 34, 37, 36, 39, 38, 41, 40,
105 43, 42, 45, 44, 47, 46, 49, 48,
106 /* B16 - B31 */
107 5, 4, 7, 6, 9, 8, 11, 10,
108 13, 12, 15, 14, 17, 16, 19, 18,
109 -1, -1, -1
110 };
111
112 static int tic6x_regmap_c62x[] = {
113 /* A0 - A15 */
114 19, 18, 21, 20, 23, 22, 25, 24,
115 27, 26, 29, 28, 31, 30, 33, 32,
116 /* B0 - B15 */
117 5, 4, 7, 6, 9, 8, 11, 10,
118 13, 12, 15, 14, 17, 16, 35, 34,
119 /* CSR, PC */
120 3, 2,
121 -1, -1, -1, -1, -1, -1, -1, -1,
122 -1, -1, -1, -1, -1, -1, -1, -1,
123 -1, -1, -1, -1, -1, -1, -1, -1,
124 -1, -1, -1, -1, -1, -1, -1, -1,
125 -1, -1, -1
126 };
127
128 #else
129 static int tic6x_regmap_c64xp[] = {
130 /* A0 - A15 */
131 52, 53, 54, 55, 56, 57, 58, 59,
132 60, 61, 62, 63, 64, 65, 66, 67,
133 /* B0 - B15 */
134 22, 23, 24, 25, 26, 27, 28, 29,
135 30, 31, 32, 33, 34, 35, 68, 69,
136 /* CSR PC */
137 4, 5,
138 /* A16 - A31 */
139 36, 37, 38, 39, 40, 41, 42, 43,
140 44, 45, 46, 47, 48, 49, 50, 51,
141 /* B16 -B31 */
142 6, 7, 8, 9, 10, 11, 12, 13,
143 14, 15, 16, 17, 18, 19, 20, 31,
144 /* TSR, ILC, RILC */
145 0, 3, 2
146 };
147
148 static int tic6x_regmap_c64x[] = {
149 /* A0 - A15 */
150 50, 51, 52, 53, 54, 55, 56, 57,
151 58, 59, 60, 61, 62, 63, 64, 65,
152 /* B0 - B15 */
153 20, 21, 22, 23, 24, 25, 26, 27,
154 28, 29, 30, 31, 32, 33, 66, 67,
155 /* CSR PC */
156 2, 3,
157 /* A16 - A31 */
158 34, 35, 36, 37, 38, 39, 40, 41,
159 42, 43, 44, 45, 46, 47, 48, 49,
160 /* B16 - B31 */
161 4, 5, 6, 7, 8, 9, 10, 11,
162 12, 13, 14, 15, 16, 17, 18, 19,
163 -1, -1, -1
164 };
165
166 static int tic6x_regmap_c62x[] = {
167 /* A0 - A15 */
168 18, 19, 20, 21, 22, 23, 24, 25,
169 26, 27, 28, 29, 30, 31, 32, 33,
170 /* B0 - B15 */
171 4, 5, 6, 7, 8, 9, 10, 11,
172 12, 13, 14, 15, 16, 17, 34, 35,
173 /* CSR PC */
174 2, 3,
175 -1, -1, -1, -1, -1, -1, -1, -1,
176 -1, -1, -1, -1, -1, -1, -1, -1,
177 -1, -1, -1, -1, -1, -1, -1, -1,
178 -1, -1, -1, -1, -1, -1, -1, -1,
179 -1, -1, -1
180 };
181
182 #endif
183
184 extern struct linux_target_ops the_low_target;
185
186 static int *tic6x_regmap;
187 static unsigned int tic6x_breakpoint;
188 #define tic6x_breakpoint_len 4
189
190 /* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */
191
192 static const gdb_byte *
193 tic6x_sw_breakpoint_from_kind (int kind, int *size)
194 {
195 *size = tic6x_breakpoint_len;
196 return (const gdb_byte *) &tic6x_breakpoint;
197 }
198
199 static struct usrregs_info tic6x_usrregs_info =
200 {
201 TIC6X_NUM_REGS,
202 NULL, /* Set in tic6x_read_description. */
203 };
204
205 static const struct target_desc *
206 tic6x_read_description (enum c6x_feature feature)
207 {
208 static target_desc *tdescs[C6X_LAST] = { };
209 struct target_desc **tdesc = &tdescs[feature];
210
211 if (*tdesc == NULL)
212 {
213 *tdesc = tic6x_create_target_description (feature);
214 static const char *expedite_regs[] = { "A15", "PC", NULL };
215 init_target_desc (*tdesc, expedite_regs);
216 }
217
218 return *tdesc;
219 }
220
221 static int
222 tic6x_cannot_fetch_register (int regno)
223 {
224 return (tic6x_regmap[regno] == -1);
225 }
226
227 static int
228 tic6x_cannot_store_register (int regno)
229 {
230 return (tic6x_regmap[regno] == -1);
231 }
232
233 static CORE_ADDR
234 tic6x_get_pc (struct regcache *regcache)
235 {
236 union tic6x_register pc;
237
238 collect_register_by_name (regcache, "PC", pc.buf);
239 return pc.reg32;
240 }
241
242 static void
243 tic6x_set_pc (struct regcache *regcache, CORE_ADDR pc)
244 {
245 union tic6x_register newpc;
246
247 newpc.reg32 = pc;
248 supply_register_by_name (regcache, "PC", newpc.buf);
249 }
250
251 static int
252 tic6x_breakpoint_at (CORE_ADDR where)
253 {
254 unsigned int insn;
255
256 the_target->read_memory (where, (unsigned char *) &insn, 4);
257 if (insn == tic6x_breakpoint)
258 return 1;
259
260 /* If necessary, recognize more trap instructions here. GDB only uses the
261 one. */
262 return 0;
263 }
264
265 /* Fetch the thread-local storage pointer for libthread_db. */
266
267 ps_err_e
268 ps_get_thread_area (struct ps_prochandle *ph,
269 lwpid_t lwpid, int idx, void **base)
270 {
271 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
272 return PS_ERR;
273
274 /* IDX is the bias from the thread pointer to the beginning of the
275 thread descriptor. It has to be subtracted due to implementation
276 quirks in libthread_db. */
277 *base = (void *) ((char *) *base - idx);
278
279 return PS_OK;
280 }
281
282 static void
283 tic6x_collect_register (struct regcache *regcache, int regno,
284 union tic6x_register *reg)
285 {
286 union tic6x_register tmp_reg;
287
288 collect_register (regcache, regno, &tmp_reg.reg32);
289 reg->reg32 = tmp_reg.reg32;
290 }
291
292 static void
293 tic6x_supply_register (struct regcache *regcache, int regno,
294 const union tic6x_register *reg)
295 {
296 int offset = 0;
297
298 supply_register (regcache, regno, reg->buf + offset);
299 }
300
301 static void
302 tic6x_fill_gregset (struct regcache *regcache, void *buf)
303 {
304 auto regset = static_cast<union tic6x_register *> (buf);
305 int i;
306
307 for (i = 0; i < TIC6X_NUM_REGS; i++)
308 if (tic6x_regmap[i] != -1)
309 tic6x_collect_register (regcache, i, regset + tic6x_regmap[i]);
310 }
311
312 static void
313 tic6x_store_gregset (struct regcache *regcache, const void *buf)
314 {
315 const auto regset = static_cast<const union tic6x_register *> (buf);
316 int i;
317
318 for (i = 0; i < TIC6X_NUM_REGS; i++)
319 if (tic6x_regmap[i] != -1)
320 tic6x_supply_register (regcache, i, regset + tic6x_regmap[i]);
321 }
322
323 static struct regset_info tic6x_regsets[] = {
324 { PTRACE_GETREGS, PTRACE_SETREGS, 0, TIC6X_NUM_REGS * 4, GENERAL_REGS,
325 tic6x_fill_gregset, tic6x_store_gregset },
326 NULL_REGSET
327 };
328
329 static void
330 tic6x_arch_setup (void)
331 {
332 register unsigned int csr asm ("B2");
333 unsigned int cpuid;
334 enum c6x_feature feature = C6X_CORE;
335
336 /* Determine the CPU we're running on to find the register order. */
337 __asm__ ("MVC .S2 CSR,%0" : "=r" (csr) :);
338 cpuid = csr >> 24;
339 switch (cpuid)
340 {
341 case 0x00: /* C62x */
342 case 0x02: /* C67x */
343 tic6x_regmap = tic6x_regmap_c62x;
344 tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */
345 feature = C6X_CORE;
346 break;
347 case 0x03: /* C67x+ */
348 tic6x_regmap = tic6x_regmap_c64x;
349 tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */
350 feature = C6X_GP;
351 break;
352 case 0x0c: /* C64x */
353 tic6x_regmap = tic6x_regmap_c64x;
354 tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */
355 feature = C6X_GP;
356 break;
357 case 0x10: /* C64x+ */
358 case 0x14: /* C674x */
359 case 0x15: /* C66x */
360 tic6x_regmap = tic6x_regmap_c64xp;
361 tic6x_breakpoint = 0x56454314; /* illegal opcode */
362 feature = C6X_C6XP;
363 break;
364 default:
365 error ("Unknown CPU ID 0x%02x", cpuid);
366 }
367 tic6x_usrregs_info.regmap = tic6x_regmap;
368
369 current_process ()->tdesc = tic6x_read_description (feature);
370 }
371
372 /* Support for hardware single step. */
373
374 static int
375 tic6x_supports_hardware_single_step (void)
376 {
377 return 1;
378 }
379
380 static struct regsets_info tic6x_regsets_info =
381 {
382 tic6x_regsets, /* regsets */
383 0, /* num_regsets */
384 NULL, /* disabled_regsets */
385 };
386
387 static struct regs_info regs_info =
388 {
389 NULL, /* regset_bitmap */
390 &tic6x_usrregs_info,
391 &tic6x_regsets_info
392 };
393
394 static const struct regs_info *
395 tic6x_regs_info (void)
396 {
397 return &regs_info;
398 }
399
400 struct linux_target_ops the_low_target = {
401 tic6x_arch_setup,
402 tic6x_regs_info,
403 tic6x_cannot_fetch_register,
404 tic6x_cannot_store_register,
405 NULL, /* fetch_register */
406 tic6x_get_pc,
407 tic6x_set_pc,
408 NULL, /* breakpoint_kind_from_pc */
409 tic6x_sw_breakpoint_from_kind,
410 NULL,
411 0,
412 tic6x_breakpoint_at,
413 NULL, /* supports_z_point_type */
414 NULL, /* insert_point */
415 NULL, /* remove_point */
416 NULL, /* stopped_by_watchpoint */
417 NULL, /* stopped_data_address */
418 NULL, /* collect_ptrace_register */
419 NULL, /* supply_ptrace_register */
420 NULL, /* siginfo_fixup */
421 NULL, /* new_process */
422 NULL, /* delete_process */
423 NULL, /* new_thread */
424 NULL, /* delete_thread */
425 NULL, /* new_fork */
426 NULL, /* prepare_to_resume */
427 NULL, /* process_qsupported */
428 NULL, /* supports_tracepoints */
429 NULL, /* get_thread_area */
430 NULL, /* install_fast_tracepoint_jump_pad */
431 NULL, /* emit_ops */
432 NULL, /* get_min_fast_tracepoint_insn_len */
433 NULL, /* supports_range_stepping */
434 NULL, /* breakpoint_kind_from_current_state */
435 tic6x_supports_hardware_single_step,
436 };
437
438 #if GDB_SELF_TEST
439 #include "gdbsupport/selftest.h"
440
441 namespace selftests {
442 namespace tdesc {
443 static void
444 tic6x_tdesc_test ()
445 {
446 SELF_CHECK (*tdesc_tic6x_c62x_linux == *tic6x_read_description (C6X_CORE));
447 SELF_CHECK (*tdesc_tic6x_c64x_linux == *tic6x_read_description (C6X_GP));
448 SELF_CHECK (*tdesc_tic6x_c64xp_linux == *tic6x_read_description (C6X_C6XP));
449 }
450 }
451 }
452 #endif
453
454 /* The linux target ops object. */
455
456 linux_process_target *the_linux_target = &the_tic6x_target;
457
458 void
459 initialize_low_arch (void)
460 {
461 #if GDB_SELF_TEST
462 /* Initialize the Linux target descriptions. */
463 init_registers_tic6x_c64xp_linux ();
464 init_registers_tic6x_c64x_linux ();
465 init_registers_tic6x_c62x_linux ();
466
467 selftests::register_test ("tic6x-tdesc", selftests::tdesc::tic6x_tdesc_test);
468 #endif
469
470 initialize_regsets_info (&tic6x_regsets_info);
471 }