]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/linux-crisv32-low.c
Refactor queries for hardware and software single stepping support in GDBServer.
[thirdparty/binutils-gdb.git] / gdb / gdbserver / linux-crisv32-low.c
CommitLineData
45b134e5 1/* GNU/Linux/CRIS specific low level interface, for the remote server for GDB.
32d0add0 2 Copyright (C) 1995-2015 Free Software Foundation, Inc.
45b134e5
OF
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
45b134e5
OF
9 (at your option) any later version.
10
11 This program 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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
45b134e5
OF
18
19#include "server.h"
20#include "linux-low.h"
5826e159 21#include "nat/gdb_ptrace.h"
45b134e5 22
d05b4ac3
UW
23/* Defined in auto-generated file reg-crisv32.c. */
24void init_registers_crisv32 (void);
3aee8918 25extern const struct target_desc *tdesc_crisv32;
d05b4ac3 26
45b134e5
OF
27/* CRISv32 */
28#define cris_num_regs 49
29
8eb3d7b6
RW
30#ifndef PTRACE_GET_THREAD_AREA
31#define PTRACE_GET_THREAD_AREA 25
32#endif
33
45b134e5
OF
34/* Note: Ignoring USP (having the stack pointer in two locations causes trouble
35 without any significant gain). */
36
37/* Locations need to match <include/asm/arch/ptrace.h>. */
38static int cris_regmap[] = {
39 1*4, 2*4, 3*4, 4*4,
40 5*4, 6*4, 7*4, 8*4,
41 9*4, 10*4, 11*4, 12*4,
42 13*4, 14*4, 24*4, 15*4,
43
44 -1, -1, -1, 16*4,
45 -1, 22*4, 23*4, 17*4,
46 -1, -1, 21*4, 20*4,
47 -1, 19*4, -1, 18*4,
48
49 25*4,
50
1b3f6016 51 26*4, -1, -1, 29*4,
45b134e5
OF
52 30*4, 31*4, 32*4, 33*4,
53 34*4, 35*4, 36*4, 37*4,
54 38*4, 39*4, 40*4, -1
1b3f6016 55
45b134e5
OF
56};
57
58extern int debug_threads;
59
60static CORE_ADDR
442ea881 61cris_get_pc (struct regcache *regcache)
45b134e5
OF
62{
63 unsigned long pc;
442ea881 64 collect_register_by_name (regcache, "pc", &pc);
45b134e5 65 if (debug_threads)
87ce2a04 66 debug_printf ("stop pc is %08lx\n", pc);
45b134e5
OF
67 return pc;
68}
69
70static void
442ea881 71cris_set_pc (struct regcache *regcache, CORE_ADDR pc)
45b134e5
OF
72{
73 unsigned long newpc = pc;
442ea881 74 supply_register_by_name (regcache, "pc", &newpc);
45b134e5
OF
75}
76
77static const unsigned short cris_breakpoint = 0xe938;
78#define cris_breakpoint_len 2
79
dd373349
AT
80/* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */
81
82static const gdb_byte *
83cris_sw_breakpoint_from_kind (int kind, int *size)
84{
85 *size = cris_breakpoint_len;
86 return (const gdb_byte *) &cris_breakpoint;
87}
88
45b134e5
OF
89static int
90cris_breakpoint_at (CORE_ADDR where)
91{
92 unsigned short insn;
93
f450004a
DJ
94 (*the_target->read_memory) (where, (unsigned char *) &insn,
95 cris_breakpoint_len);
45b134e5
OF
96 if (insn == cris_breakpoint)
97 return 1;
98
99 /* If necessary, recognize more trap instructions here. GDB only uses the
100 one. */
101 return 0;
102}
103
104/* We only place breakpoints in empty marker functions, and thread locking
105 is outside of the function. So rather than importing software single-step,
106 we can just run until exit. */
107
108/* FIXME: This function should not be needed, since we have PTRACE_SINGLESTEP
109 for CRISv32. Without it, td_ta_event_getmsg in thread_db_create_event
110 will fail when debugging multi-threaded applications. */
111
112static CORE_ADDR
113cris_reinsert_addr (void)
114{
0bfdf32f 115 struct regcache *regcache = get_thread_regcache (current_thread, 1);
45b134e5 116 unsigned long pc;
442ea881 117 collect_register_by_name (regcache, "srp", &pc);
45b134e5
OF
118 return pc;
119}
120
121static void
442ea881
PA
122cris_write_data_breakpoint (struct regcache *regcache,
123 int bp, unsigned long start, unsigned long end)
45b134e5
OF
124{
125 switch (bp)
126 {
127 case 0:
442ea881
PA
128 supply_register_by_name (regcache, "s3", &start);
129 supply_register_by_name (regcache, "s4", &end);
45b134e5
OF
130 break;
131 case 1:
442ea881
PA
132 supply_register_by_name (regcache, "s5", &start);
133 supply_register_by_name (regcache, "s6", &end);
45b134e5
OF
134 break;
135 case 2:
442ea881
PA
136 supply_register_by_name (regcache, "s7", &start);
137 supply_register_by_name (regcache, "s8", &end);
45b134e5
OF
138 break;
139 case 3:
442ea881
PA
140 supply_register_by_name (regcache, "s9", &start);
141 supply_register_by_name (regcache, "s10", &end);
45b134e5
OF
142 break;
143 case 4:
442ea881
PA
144 supply_register_by_name (regcache, "s11", &start);
145 supply_register_by_name (regcache, "s12", &end);
45b134e5
OF
146 break;
147 case 5:
442ea881
PA
148 supply_register_by_name (regcache, "s13", &start);
149 supply_register_by_name (regcache, "s14", &end);
45b134e5
OF
150 break;
151 }
152}
153
154static int
802e8e6d
PA
155cris_supports_z_point_type (char z_type)
156{
157 switch (z_type)
158 {
159 case Z_PACKET_WRITE_WP:
160 case Z_PACKET_READ_WP:
161 case Z_PACKET_ACCESS_WP:
162 return 1;
163 default:
164 return 0;
165 }
166}
167
168static int
169cris_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
170 int len, struct raw_breakpoint *bp)
45b134e5
OF
171{
172 int bp;
173 unsigned long bp_ctrl;
174 unsigned long start, end;
175 unsigned long ccs;
442ea881 176 struct regcache *regcache;
1b3f6016 177
0bfdf32f 178 regcache = get_thread_regcache (current_thread, 1);
442ea881 179
45b134e5
OF
180 /* Read watchpoints are set as access watchpoints, because of GDB's
181 inability to deal with pure read watchpoints. */
802e8e6d
PA
182 if (type == raw_bkpt_type_read_wp)
183 type = raw_bkpt_type_access_wp;
45b134e5
OF
184
185 /* Get the configuration register. */
442ea881 186 collect_register_by_name (regcache, "s0", &bp_ctrl);
45b134e5
OF
187
188 /* The watchpoint allocation scheme is the simplest possible.
189 For example, if a region is watched for read and
190 a write watch is requested, a new watchpoint will
191 be used. Also, if a watch for a region that is already
192 covered by one or more existing watchpoints, a new
193 watchpoint will be used. */
1b3f6016 194
45b134e5
OF
195 /* First, find a free data watchpoint. */
196 for (bp = 0; bp < 6; bp++)
197 {
198 /* Each data watchpoint's control registers occupy 2 bits
199 (hence the 3), starting at bit 2 for D0 (hence the 2)
200 with 4 bits between for each watchpoint (yes, the 4). */
1b3f6016 201 if (!(bp_ctrl & (0x3 << (2 + (bp * 4)))))
45b134e5
OF
202 break;
203 }
1b3f6016 204
45b134e5
OF
205 if (bp > 5)
206 {
207 /* We're out of watchpoints. */
208 return -1;
209 }
210
211 /* Configure the control register first. */
802e8e6d 212 if (type == raw_bkpt_type_read_wp || type == raw_bkpt_type_access_wp)
45b134e5
OF
213 {
214 /* Trigger on read. */
215 bp_ctrl |= (1 << (2 + bp * 4));
216 }
802e8e6d 217 if (type == raw_bkpt_type_write_wp || type == raw_bkpt_type_access_wp)
45b134e5
OF
218 {
219 /* Trigger on write. */
220 bp_ctrl |= (2 << (2 + bp * 4));
221 }
1b3f6016 222
45b134e5 223 /* Setup the configuration register. */
442ea881 224 supply_register_by_name (regcache, "s0", &bp_ctrl);
1b3f6016 225
45b134e5
OF
226 /* Setup the range. */
227 start = addr;
228 end = addr + len - 1;
229
230 /* Configure the watchpoint register. */
442ea881 231 cris_write_data_breakpoint (regcache, bp, start, end);
45b134e5 232
442ea881 233 collect_register_by_name (regcache, "ccs", &ccs);
45b134e5
OF
234 /* Set the S1 flag to enable watchpoints. */
235 ccs |= (1 << 19);
442ea881 236 supply_register_by_name (regcache, "ccs", &ccs);
45b134e5
OF
237
238 return 0;
239}
240
241static int
802e8e6d
PA
242cris_remove_point (enum raw_bkpt_type type, CORE_ADDR addr, int len,
243 struct raw_breakpoint *bp)
45b134e5
OF
244{
245 int bp;
246 unsigned long bp_ctrl;
247 unsigned long start, end;
442ea881 248 struct regcache *regcache;
07c04788 249 unsigned long bp_d_regs[12];
1b3f6016 250
0bfdf32f 251 regcache = get_thread_regcache (current_thread, 1);
442ea881 252
45b134e5
OF
253 /* Read watchpoints are set as access watchpoints, because of GDB's
254 inability to deal with pure read watchpoints. */
802e8e6d
PA
255 if (type == raw_bkpt_type_read_wp)
256 type = raw_bkpt_type_access_wp;
1b3f6016 257
45b134e5 258 /* Get the configuration register. */
442ea881 259 collect_register_by_name (regcache, "s0", &bp_ctrl);
45b134e5
OF
260
261 /* Try to find a watchpoint that is configured for the
262 specified range, then check that read/write also matches. */
1b3f6016 263
45b134e5
OF
264 /* Ugly pointer arithmetic, since I cannot rely on a
265 single switch (addr) as there may be several watchpoints with
266 the same start address for example. */
267
45b134e5 268 /* Get all range registers to simplify search. */
442ea881
PA
269 collect_register_by_name (regcache, "s3", &bp_d_regs[0]);
270 collect_register_by_name (regcache, "s4", &bp_d_regs[1]);
271 collect_register_by_name (regcache, "s5", &bp_d_regs[2]);
272 collect_register_by_name (regcache, "s6", &bp_d_regs[3]);
273 collect_register_by_name (regcache, "s7", &bp_d_regs[4]);
274 collect_register_by_name (regcache, "s8", &bp_d_regs[5]);
275 collect_register_by_name (regcache, "s9", &bp_d_regs[6]);
276 collect_register_by_name (regcache, "s10", &bp_d_regs[7]);
277 collect_register_by_name (regcache, "s11", &bp_d_regs[8]);
278 collect_register_by_name (regcache, "s12", &bp_d_regs[9]);
279 collect_register_by_name (regcache, "s13", &bp_d_regs[10]);
280 collect_register_by_name (regcache, "s14", &bp_d_regs[11]);
45b134e5 281
1b3f6016 282 for (bp = 0; bp < 6; bp++)
45b134e5 283 {
1b3f6016 284 if (bp_d_regs[bp * 2] == addr
45b134e5
OF
285 && bp_d_regs[bp * 2 + 1] == (addr + len - 1)) {
286 /* Matching range. */
287 int bitpos = 2 + bp * 4;
288 int rw_bits;
1b3f6016 289
45b134e5
OF
290 /* Read/write bits for this BP. */
291 rw_bits = (bp_ctrl & (0x3 << bitpos)) >> bitpos;
1b3f6016 292
802e8e6d
PA
293 if ((type == raw_bkpt_type_read_wp && rw_bits == 0x1)
294 || (type == raw_bkpt_type_write_wp && rw_bits == 0x2)
295 || (type == raw_bkpt_type_access_wp && rw_bits == 0x3))
45b134e5
OF
296 {
297 /* Read/write matched. */
298 break;
299 }
300 }
301 }
1b3f6016 302
45b134e5
OF
303 if (bp > 5)
304 {
305 /* No watchpoint matched. */
306 return -1;
307 }
1b3f6016 308
45b134e5
OF
309 /* Found a matching watchpoint. Now, deconfigure it by
310 both disabling read/write in bp_ctrl and zeroing its
311 start/end addresses. */
312 bp_ctrl &= ~(3 << (2 + (bp * 4)));
313 /* Setup the configuration register. */
442ea881 314 supply_register_by_name (regcache, "s0", &bp_ctrl);
45b134e5
OF
315
316 start = end = 0;
317 /* Configure the watchpoint register. */
442ea881 318 cris_write_data_breakpoint (regcache, bp, start, end);
45b134e5
OF
319
320 /* Note that we don't clear the S1 flag here. It's done when continuing. */
321 return 0;
322}
323
324static int
325cris_stopped_by_watchpoint (void)
326{
327 unsigned long exs;
0bfdf32f 328 struct regcache *regcache = get_thread_regcache (current_thread, 1);
45b134e5 329
07c04788 330 collect_register_by_name (regcache, "exs", &exs);
45b134e5
OF
331
332 return (((exs & 0xff00) >> 8) == 0xc);
333}
334
335static CORE_ADDR
336cris_stopped_data_address (void)
337{
338 unsigned long eda;
0bfdf32f 339 struct regcache *regcache = get_thread_regcache (current_thread, 1);
45b134e5 340
07c04788 341 collect_register_by_name (regcache, "eda", &eda);
45b134e5
OF
342
343 /* FIXME: Possibly adjust to match watched range. */
344 return eda;
345}
346
8eb3d7b6
RW
347ps_err_e
348ps_get_thread_area (const struct ps_prochandle *ph,
349 lwpid_t lwpid, int idx, void **base)
350{
351 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
352 return PS_ERR;
353
354 /* IDX is the bias from the thread pointer to the beginning of the
355 thread descriptor. It has to be subtracted due to implementation
356 quirks in libthread_db. */
357 *base = (void *) ((char *) *base - idx);
358 return PS_OK;
359}
360
45b134e5 361static void
07c04788 362cris_fill_gregset (struct regcache *regcache, void *buf)
45b134e5
OF
363{
364 int i;
365
366 for (i = 0; i < cris_num_regs; i++)
367 {
368 if (cris_regmap[i] != -1)
07c04788 369 collect_register (regcache, i, ((char *) buf) + cris_regmap[i]);
45b134e5
OF
370 }
371}
372
373static void
07c04788 374cris_store_gregset (struct regcache *regcache, const void *buf)
45b134e5
OF
375{
376 int i;
377
378 for (i = 0; i < cris_num_regs; i++)
379 {
380 if (cris_regmap[i] != -1)
07c04788 381 supply_register (regcache, i, ((char *) buf) + cris_regmap[i]);
45b134e5
OF
382 }
383}
384
3aee8918
PA
385static void
386cris_arch_setup (void)
387{
388 current_process ()->tdesc = tdesc_crisv32;
389}
390
7d00775e
AT
391/* Support for hardware single step. */
392
393static int
394cris_supports_hardware_single_step (void)
395{
396 return 1;
397}
398
3aee8918 399static struct regset_info cris_regsets[] = {
07c04788 400 { PTRACE_GETREGS, PTRACE_SETREGS, 0, cris_num_regs * 4,
45b134e5 401 GENERAL_REGS, cris_fill_gregset, cris_store_gregset },
50bc912a 402 NULL_REGSET
45b134e5
OF
403};
404
3aee8918
PA
405
406static struct regsets_info cris_regsets_info =
407 {
408 cris_regsets, /* regsets */
409 0, /* num_regsets */
410 NULL, /* disabled_regsets */
411 };
412
413static struct usrregs_info cris_usrregs_info =
414 {
415 cris_num_regs,
416 cris_regmap,
417 };
418
419static struct regs_info regs_info =
420 {
421 NULL, /* regset_bitmap */
422 &cris_usrregs_info,
423 &cris_regsets_info
424 };
425
426static const struct regs_info *
427cris_regs_info (void)
428{
429 return &regs_info;
430}
431
45b134e5 432struct linux_target_ops the_low_target = {
3aee8918
PA
433 cris_arch_setup,
434 cris_regs_info,
45b134e5 435 NULL,
1faeff08 436 NULL,
c14dfd32 437 NULL, /* fetch_register */
45b134e5
OF
438 cris_get_pc,
439 cris_set_pc,
dd373349
AT
440 NULL, /* breakpoint_kind_from_pc */
441 cris_sw_breakpoint_from_kind,
45b134e5
OF
442 cris_reinsert_addr,
443 0,
444 cris_breakpoint_at,
802e8e6d 445 cris_supports_z_point_type,
d993e290
PA
446 cris_insert_point,
447 cris_remove_point,
45b134e5
OF
448 cris_stopped_by_watchpoint,
449 cris_stopped_data_address,
7d00775e
AT
450 NULL, /* collect_ptrace_register */
451 NULL, /* supply_ptrace_register */
452 NULL, /* siginfo_fixup */
453 NULL, /* new_process */
454 NULL, /* new_thread */
455 NULL, /* new_fork */
456 NULL, /* prepare_to_resume */
457 NULL, /* process_qsupported */
458 NULL, /* supports_tracepoints */
459 NULL, /* get_thread_area */
460 NULL, /* install_fast_tracepoint_jump_pad */
461 NULL, /* emit_ops */
462 NULL, /* get_min_fast_tracepoint_insn_len */
463 NULL, /* supports_range_stepping */
464 NULL, /* breakpoint_kind_from_current_state */
465 cris_supports_hardware_single_step,
45b134e5 466};
3aee8918
PA
467
468void
469initialize_low_arch (void)
470{
eddddb9d 471 init_registers_crisv32 ();
3aee8918
PA
472
473 initialize_regsets_info (&cris_regsets_info);
474}