Commit | Line | Data |
---|---|---|
58dbd541 YQ |
1 | /* Target dependent code for GDB on TI C6x systems. |
2 | ||
61baf725 | 3 | Copyright (C) 2010-2017 Free Software Foundation, Inc. |
58dbd541 YQ |
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 | ||
5826e159 | 25 | #include "nat/gdb_ptrace.h" |
58dbd541 YQ |
26 | #include <endian.h> |
27 | ||
28 | #include "gdb_proc_service.h" | |
29 | ||
30 | #ifndef PTRACE_GET_THREAD_AREA | |
31 | #define PTRACE_GET_THREAD_AREA 25 | |
32 | #endif | |
33 | ||
34 | /* There are at most 69 registers accessible in ptrace. */ | |
35 | #define TIC6X_NUM_REGS 69 | |
36 | ||
37 | #include <asm/ptrace.h> | |
38 | ||
39 | /* Defined in auto-generated file tic6x-c64xp-linux.c. */ | |
40 | void init_registers_tic6x_c64xp_linux (void); | |
3aee8918 PA |
41 | extern const struct target_desc *tdesc_tic6x_c64xp_linux; |
42 | ||
58dbd541 YQ |
43 | /* Defined in auto-generated file tic6x-c64x-linux.c. */ |
44 | void init_registers_tic6x_c64x_linux (void); | |
3aee8918 PA |
45 | extern const struct target_desc *tdesc_tic6x_c64x_linux; |
46 | ||
58dbd541 YQ |
47 | /* Defined in auto-generated file tic62x-c6xp-linux.c. */ |
48 | void init_registers_tic6x_c62x_linux (void); | |
3aee8918 | 49 | extern const struct target_desc *tdesc_tic6x_c62x_linux; |
58dbd541 YQ |
50 | |
51 | union tic6x_register | |
52 | { | |
53 | unsigned char buf[4]; | |
54 | ||
55 | int reg32; | |
56 | }; | |
57 | ||
58 | /* Return the ptrace ``address'' of register REGNO. */ | |
59 | ||
60 | #if __BYTE_ORDER == __BIG_ENDIAN | |
61 | static int tic6x_regmap_c64xp[] = { | |
62 | /* A0 - A15 */ | |
63 | 53, 52, 55, 54, 57, 56, 59, 58, | |
64 | 61, 60, 63, 62, 65, 64, 67, 66, | |
65 | /* B0 - B15 */ | |
66 | 23, 22, 25, 24, 27, 26, 29, 28, | |
67 | 31, 30, 33, 32, 35, 34, 69, 68, | |
68 | /* CSR PC */ | |
69 | 5, 4, | |
70 | /* A16 - A31 */ | |
71 | 37, 36, 39, 38, 41, 40, 43, 42, | |
72 | 45, 44, 47, 46, 49, 48, 51, 50, | |
73 | /* B16 - B31 */ | |
74 | 7, 6, 9, 8, 11, 10, 13, 12, | |
75 | 15, 14, 17, 16, 19, 18, 21, 20, | |
76 | /* TSR, ILC, RILC */ | |
77 | 1, 2, 3 | |
78 | }; | |
79 | ||
80 | static int tic6x_regmap_c64x[] = { | |
81 | /* A0 - A15 */ | |
82 | 51, 50, 53, 52, 55, 54, 57, 56, | |
83 | 59, 58, 61, 60, 63, 62, 65, 64, | |
84 | /* B0 - B15 */ | |
85 | 21, 20, 23, 22, 25, 24, 27, 26, | |
86 | 29, 28, 31, 30, 33, 32, 67, 66, | |
87 | /* CSR PC */ | |
88 | 3, 2, | |
89 | /* A16 - A31 */ | |
90 | 35, 34, 37, 36, 39, 38, 41, 40, | |
91 | 43, 42, 45, 44, 47, 46, 49, 48, | |
92 | /* B16 - B31 */ | |
93 | 5, 4, 7, 6, 9, 8, 11, 10, | |
94 | 13, 12, 15, 14, 17, 16, 19, 18, | |
95 | -1, -1, -1 | |
96 | }; | |
97 | ||
98 | static int tic6x_regmap_c62x[] = { | |
99 | /* A0 - A15 */ | |
100 | 19, 18, 21, 20, 23, 22, 25, 24, | |
101 | 27, 26, 29, 28, 31, 30, 33, 32, | |
102 | /* B0 - B15 */ | |
103 | 5, 4, 7, 6, 9, 8, 11, 10, | |
104 | 13, 12, 15, 14, 17, 16, 35, 34, | |
105 | /* CSR, PC */ | |
106 | 3, 2, | |
107 | -1, -1, -1, -1, -1, -1, -1, -1, | |
108 | -1, -1, -1, -1, -1, -1, -1, -1, | |
109 | -1, -1, -1, -1, -1, -1, -1, -1, | |
110 | -1, -1, -1, -1, -1, -1, -1, -1, | |
111 | -1, -1, -1 | |
112 | }; | |
113 | ||
114 | #else | |
115 | static int tic6x_regmap_c64xp[] = { | |
116 | /* A0 - A15 */ | |
117 | 52, 53, 54, 55, 56, 57, 58, 59, | |
118 | 60, 61, 62, 63, 64, 65, 66, 67, | |
119 | /* B0 - B15 */ | |
120 | 22, 23, 24, 25, 26, 27, 28, 29, | |
121 | 30, 31, 32, 33, 34, 35, 68, 69, | |
122 | /* CSR PC */ | |
123 | 4, 5, | |
124 | /* A16 - A31 */ | |
125 | 36, 37, 38, 39, 40, 41, 42, 43, | |
126 | 44, 45, 46, 47, 48, 49, 50, 51, | |
127 | /* B16 -B31 */ | |
128 | 6, 7, 8, 9, 10, 11, 12, 13, | |
129 | 14, 15, 16, 17, 18, 19, 20, 31, | |
130 | /* TSR, ILC, RILC */ | |
131 | 0, 3, 2 | |
132 | }; | |
133 | ||
134 | static int tic6x_regmap_c64x[] = { | |
135 | /* A0 - A15 */ | |
136 | 50, 51, 52, 53, 54, 55, 56, 57, | |
137 | 58, 59, 60, 61, 62, 63, 64, 65, | |
138 | /* B0 - B15 */ | |
139 | 20, 21, 22, 23, 24, 25, 26, 27, | |
140 | 28, 29, 30, 31, 32, 33, 66, 67, | |
141 | /* CSR PC */ | |
142 | 2, 3, | |
143 | /* A16 - A31 */ | |
144 | 34, 35, 36, 37, 38, 39, 40, 41, | |
145 | 42, 43, 44, 45, 46, 47, 48, 49, | |
146 | /* B16 - B31 */ | |
147 | 4, 5, 6, 7, 8, 9, 10, 11, | |
148 | 12, 13, 14, 15, 16, 17, 18, 19, | |
149 | -1, -1, -1 | |
150 | }; | |
151 | ||
152 | static int tic6x_regmap_c62x[] = { | |
153 | /* A0 - A15 */ | |
154 | 18, 19, 20, 21, 22, 23, 24, 25, | |
155 | 26, 27, 28, 29, 30, 31, 32, 33, | |
156 | /* B0 - B15 */ | |
157 | 4, 5, 6, 7, 8, 9, 10, 11, | |
158 | 12, 13, 14, 15, 16, 17, 34, 35, | |
159 | /* CSR PC */ | |
160 | 2, 3, | |
161 | -1, -1, -1, -1, -1, -1, -1, -1, | |
162 | -1, -1, -1, -1, -1, -1, -1, -1, | |
163 | -1, -1, -1, -1, -1, -1, -1, -1, | |
164 | -1, -1, -1, -1, -1, -1, -1, -1, | |
165 | -1, -1, -1 | |
166 | }; | |
167 | ||
168 | #endif | |
169 | ||
170 | extern struct linux_target_ops the_low_target; | |
171 | ||
172 | static int *tic6x_regmap; | |
173 | static unsigned int tic6x_breakpoint; | |
dd373349 AT |
174 | #define tic6x_breakpoint_len 4 |
175 | ||
176 | /* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */ | |
177 | ||
178 | static const gdb_byte * | |
179 | tic6x_sw_breakpoint_from_kind (int kind, int *size) | |
180 | { | |
181 | *size = tic6x_breakpoint_len; | |
182 | return (const gdb_byte *) &tic6x_breakpoint; | |
183 | } | |
58dbd541 | 184 | |
3491a34c YQ |
185 | static struct usrregs_info tic6x_usrregs_info = |
186 | { | |
187 | TIC6X_NUM_REGS, | |
188 | NULL, /* Set in tic6x_read_description. */ | |
189 | }; | |
3aee8918 PA |
190 | |
191 | static const struct target_desc * | |
192 | tic6x_read_description (void) | |
58dbd541 YQ |
193 | { |
194 | register unsigned int csr asm ("B2"); | |
195 | unsigned int cpuid; | |
3aee8918 | 196 | const struct target_desc *tdesc; |
58dbd541 YQ |
197 | |
198 | /* Determine the CPU we're running on to find the register order. */ | |
199 | __asm__ ("MVC .S2 CSR,%0" : "=r" (csr) :); | |
200 | cpuid = csr >> 24; | |
201 | switch (cpuid) | |
202 | { | |
203 | case 0x00: /* C62x */ | |
204 | case 0x02: /* C67x */ | |
205 | tic6x_regmap = tic6x_regmap_c62x; | |
206 | tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */ | |
3aee8918 | 207 | tdesc = tdesc_tic6x_c62x_linux; |
58dbd541 YQ |
208 | break; |
209 | case 0x03: /* C67x+ */ | |
210 | tic6x_regmap = tic6x_regmap_c64x; | |
211 | tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */ | |
3aee8918 | 212 | tdesc = tdesc_tic6x_c64x_linux; |
58dbd541 YQ |
213 | break; |
214 | case 0x0c: /* C64x */ | |
215 | tic6x_regmap = tic6x_regmap_c64x; | |
216 | tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */ | |
3aee8918 | 217 | tdesc = tdesc_tic6x_c64x_linux; |
58dbd541 YQ |
218 | break; |
219 | case 0x10: /* C64x+ */ | |
220 | case 0x14: /* C674x */ | |
221 | case 0x15: /* C66x */ | |
222 | tic6x_regmap = tic6x_regmap_c64xp; | |
223 | tic6x_breakpoint = 0x56454314; /* illegal opcode */ | |
3aee8918 | 224 | tdesc = tdesc_tic6x_c64xp_linux; |
58dbd541 YQ |
225 | break; |
226 | default: | |
227 | error ("Unknown CPU ID 0x%02x", cpuid); | |
228 | } | |
3aee8918 PA |
229 | tic6x_usrregs_info.regmap = tic6x_regmap; |
230 | return tdesc; | |
58dbd541 YQ |
231 | } |
232 | ||
233 | static int | |
234 | tic6x_cannot_fetch_register (int regno) | |
235 | { | |
236 | return (tic6x_regmap[regno] == -1); | |
237 | } | |
238 | ||
239 | static int | |
240 | tic6x_cannot_store_register (int regno) | |
241 | { | |
242 | return (tic6x_regmap[regno] == -1); | |
243 | } | |
244 | ||
245 | static CORE_ADDR | |
246 | tic6x_get_pc (struct regcache *regcache) | |
247 | { | |
248 | union tic6x_register pc; | |
249 | ||
250 | collect_register_by_name (regcache, "PC", pc.buf); | |
251 | return pc.reg32; | |
252 | } | |
253 | ||
254 | static void | |
255 | tic6x_set_pc (struct regcache *regcache, CORE_ADDR pc) | |
256 | { | |
257 | union tic6x_register newpc; | |
258 | ||
259 | newpc.reg32 = pc; | |
260 | supply_register_by_name (regcache, "PC", newpc.buf); | |
261 | } | |
262 | ||
58dbd541 YQ |
263 | static int |
264 | tic6x_breakpoint_at (CORE_ADDR where) | |
265 | { | |
266 | unsigned int insn; | |
267 | ||
268 | (*the_target->read_memory) (where, (unsigned char *) &insn, 4); | |
269 | if (insn == tic6x_breakpoint) | |
270 | return 1; | |
271 | ||
272 | /* If necessary, recognize more trap instructions here. GDB only uses the | |
273 | one. */ | |
274 | return 0; | |
275 | } | |
276 | ||
277 | /* Fetch the thread-local storage pointer for libthread_db. */ | |
278 | ||
279 | ps_err_e | |
754653a7 | 280 | ps_get_thread_area (struct ps_prochandle *ph, |
58dbd541 YQ |
281 | lwpid_t lwpid, int idx, void **base) |
282 | { | |
283 | if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0) | |
284 | return PS_ERR; | |
285 | ||
286 | /* IDX is the bias from the thread pointer to the beginning of the | |
287 | thread descriptor. It has to be subtracted due to implementation | |
288 | quirks in libthread_db. */ | |
289 | *base = (void *) ((char *) *base - idx); | |
290 | ||
291 | return PS_OK; | |
292 | } | |
293 | ||
294 | static void | |
295 | tic6x_collect_register (struct regcache *regcache, int regno, | |
296 | union tic6x_register *reg) | |
297 | { | |
298 | union tic6x_register tmp_reg; | |
299 | ||
300 | collect_register (regcache, regno, &tmp_reg.reg32); | |
301 | reg->reg32 = tmp_reg.reg32; | |
302 | } | |
303 | ||
304 | static void | |
305 | tic6x_supply_register (struct regcache *regcache, int regno, | |
306 | const union tic6x_register *reg) | |
307 | { | |
308 | int offset = 0; | |
309 | ||
310 | supply_register (regcache, regno, reg->buf + offset); | |
311 | } | |
312 | ||
313 | static void | |
314 | tic6x_fill_gregset (struct regcache *regcache, void *buf) | |
315 | { | |
3491a34c | 316 | auto regset = static_cast<union tic6x_register *> (buf); |
58dbd541 YQ |
317 | int i; |
318 | ||
319 | for (i = 0; i < TIC6X_NUM_REGS; i++) | |
320 | if (tic6x_regmap[i] != -1) | |
321 | tic6x_collect_register (regcache, i, regset + tic6x_regmap[i]); | |
322 | } | |
323 | ||
324 | static void | |
325 | tic6x_store_gregset (struct regcache *regcache, const void *buf) | |
326 | { | |
3491a34c | 327 | const auto regset = static_cast<const union tic6x_register *> (buf); |
58dbd541 YQ |
328 | int i; |
329 | ||
330 | for (i = 0; i < TIC6X_NUM_REGS; i++) | |
331 | if (tic6x_regmap[i] != -1) | |
332 | tic6x_supply_register (regcache, i, regset + tic6x_regmap[i]); | |
333 | } | |
334 | ||
3aee8918 | 335 | static struct regset_info tic6x_regsets[] = { |
58dbd541 YQ |
336 | { PTRACE_GETREGS, PTRACE_SETREGS, 0, TIC6X_NUM_REGS * 4, GENERAL_REGS, |
337 | tic6x_fill_gregset, tic6x_store_gregset }, | |
50bc912a | 338 | NULL_REGSET |
58dbd541 YQ |
339 | }; |
340 | ||
3aee8918 PA |
341 | static void |
342 | tic6x_arch_setup (void) | |
343 | { | |
344 | current_process ()->tdesc = tic6x_read_description (); | |
345 | } | |
346 | ||
7d00775e AT |
347 | /* Support for hardware single step. */ |
348 | ||
349 | static int | |
350 | tic6x_supports_hardware_single_step (void) | |
351 | { | |
352 | return 1; | |
353 | } | |
354 | ||
3aee8918 PA |
355 | static struct regsets_info tic6x_regsets_info = |
356 | { | |
357 | tic6x_regsets, /* regsets */ | |
358 | 0, /* num_regsets */ | |
359 | NULL, /* disabled_regsets */ | |
360 | }; | |
361 | ||
3aee8918 PA |
362 | static struct regs_info regs_info = |
363 | { | |
364 | NULL, /* regset_bitmap */ | |
365 | &tic6x_usrregs_info, | |
366 | &tic6x_regsets_info | |
367 | }; | |
368 | ||
369 | static const struct regs_info * | |
370 | tic6x_regs_info (void) | |
371 | { | |
372 | return ®s_info; | |
373 | } | |
374 | ||
58dbd541 YQ |
375 | struct linux_target_ops the_low_target = { |
376 | tic6x_arch_setup, | |
3aee8918 | 377 | tic6x_regs_info, |
58dbd541 YQ |
378 | tic6x_cannot_fetch_register, |
379 | tic6x_cannot_store_register, | |
c14dfd32 | 380 | NULL, /* fetch_register */ |
58dbd541 YQ |
381 | tic6x_get_pc, |
382 | tic6x_set_pc, | |
dd373349 AT |
383 | NULL, /* breakpoint_kind_from_pc */ |
384 | tic6x_sw_breakpoint_from_kind, | |
58dbd541 YQ |
385 | NULL, |
386 | 0, | |
387 | tic6x_breakpoint_at, | |
7d00775e AT |
388 | NULL, /* supports_z_point_type */ |
389 | NULL, /* insert_point */ | |
390 | NULL, /* remove_point */ | |
391 | NULL, /* stopped_by_watchpoint */ | |
392 | NULL, /* stopped_data_address */ | |
393 | NULL, /* collect_ptrace_register */ | |
394 | NULL, /* supply_ptrace_register */ | |
395 | NULL, /* siginfo_fixup */ | |
396 | NULL, /* new_process */ | |
04ec7890 | 397 | NULL, /* delete_process */ |
7d00775e | 398 | NULL, /* new_thread */ |
466eecee | 399 | NULL, /* delete_thread */ |
7d00775e AT |
400 | NULL, /* new_fork */ |
401 | NULL, /* prepare_to_resume */ | |
402 | NULL, /* process_qsupported */ | |
403 | NULL, /* supports_tracepoints */ | |
404 | NULL, /* get_thread_area */ | |
405 | NULL, /* install_fast_tracepoint_jump_pad */ | |
406 | NULL, /* emit_ops */ | |
407 | NULL, /* get_min_fast_tracepoint_insn_len */ | |
408 | NULL, /* supports_range_stepping */ | |
409 | NULL, /* breakpoint_kind_from_current_state */ | |
410 | tic6x_supports_hardware_single_step, | |
58dbd541 | 411 | }; |
3aee8918 PA |
412 | |
413 | void | |
414 | initialize_low_arch (void) | |
415 | { | |
416 | /* Initialize the Linux target descriptions. */ | |
417 | init_registers_tic6x_c64xp_linux (); | |
418 | init_registers_tic6x_c64x_linux (); | |
419 | init_registers_tic6x_c62x_linux (); | |
420 | ||
421 | initialize_regsets_info (&tic6x_regsets_info); | |
422 | } |