Implement breakpoint_kind_from_pc and sw_breakpoint_from_kind for ARM in GDBServer.
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-arm-low.c
1 /* GNU/Linux/ARM specific low level interface, for the remote server for GDB.
2 Copyright (C) 1995-2015 Free Software Foundation, Inc.
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
8 the Free Software Foundation; either version 3 of the License, or
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
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "server.h"
20 #include "linux-low.h"
21 #include "arch/arm.h"
22 #include "linux-aarch32-low.h"
23
24 #include <sys/uio.h>
25 /* Don't include elf.h if linux/elf.h got included by gdb_proc_service.h.
26 On Bionic elf.h and linux/elf.h have conflicting definitions. */
27 #ifndef ELFMAG0
28 #include <elf.h>
29 #endif
30 #include "nat/gdb_ptrace.h"
31 #include <signal.h>
32
33 #include "arch/arm.h"
34
35 /* Defined in auto-generated files. */
36 void init_registers_arm (void);
37 extern const struct target_desc *tdesc_arm;
38
39 void init_registers_arm_with_iwmmxt (void);
40 extern const struct target_desc *tdesc_arm_with_iwmmxt;
41
42 void init_registers_arm_with_vfpv2 (void);
43 extern const struct target_desc *tdesc_arm_with_vfpv2;
44
45 void init_registers_arm_with_vfpv3 (void);
46 extern const struct target_desc *tdesc_arm_with_vfpv3;
47
48 #ifndef PTRACE_GET_THREAD_AREA
49 #define PTRACE_GET_THREAD_AREA 22
50 #endif
51
52 #ifndef PTRACE_GETWMMXREGS
53 # define PTRACE_GETWMMXREGS 18
54 # define PTRACE_SETWMMXREGS 19
55 #endif
56
57 #ifndef PTRACE_GETVFPREGS
58 # define PTRACE_GETVFPREGS 27
59 # define PTRACE_SETVFPREGS 28
60 #endif
61
62 #ifndef PTRACE_GETHBPREGS
63 #define PTRACE_GETHBPREGS 29
64 #define PTRACE_SETHBPREGS 30
65 #endif
66
67 /* Information describing the hardware breakpoint capabilities. */
68 static struct
69 {
70 unsigned char arch;
71 unsigned char max_wp_length;
72 unsigned char wp_count;
73 unsigned char bp_count;
74 } arm_linux_hwbp_cap;
75
76 /* Enum describing the different types of ARM hardware break-/watch-points. */
77 typedef enum
78 {
79 arm_hwbp_break = 0,
80 arm_hwbp_load = 1,
81 arm_hwbp_store = 2,
82 arm_hwbp_access = 3
83 } arm_hwbp_type;
84
85 /* Enum describing the different kinds of breakpoints. */
86 enum arm_breakpoint_kinds
87 {
88 ARM_BP_KIND_THUMB = 2,
89 ARM_BP_KIND_THUMB2 = 3,
90 ARM_BP_KIND_ARM = 4,
91 };
92
93 /* Type describing an ARM Hardware Breakpoint Control register value. */
94 typedef unsigned int arm_hwbp_control_t;
95
96 /* Structure used to keep track of hardware break-/watch-points. */
97 struct arm_linux_hw_breakpoint
98 {
99 /* Address to break on, or being watched. */
100 unsigned int address;
101 /* Control register for break-/watch- point. */
102 arm_hwbp_control_t control;
103 };
104
105 /* Since we cannot dynamically allocate subfields of arch_process_info,
106 assume a maximum number of supported break-/watchpoints. */
107 #define MAX_BPTS 32
108 #define MAX_WPTS 32
109
110 /* Per-process arch-specific data we want to keep. */
111 struct arch_process_info
112 {
113 /* Hardware breakpoints for this process. */
114 struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
115 /* Hardware watchpoints for this process. */
116 struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
117 };
118
119 /* Per-thread arch-specific data we want to keep. */
120 struct arch_lwp_info
121 {
122 /* Non-zero if our copy differs from what's recorded in the thread. */
123 char bpts_changed[MAX_BPTS];
124 char wpts_changed[MAX_WPTS];
125 /* Cached stopped data address. */
126 CORE_ADDR stopped_data_address;
127 };
128
129 /* These are in <asm/elf.h> in current kernels. */
130 #define HWCAP_VFP 64
131 #define HWCAP_IWMMXT 512
132 #define HWCAP_NEON 4096
133 #define HWCAP_VFPv3 8192
134 #define HWCAP_VFPv3D16 16384
135
136 #ifdef HAVE_SYS_REG_H
137 #include <sys/reg.h>
138 #endif
139
140 #define arm_num_regs 26
141
142 static int arm_regmap[] = {
143 0, 4, 8, 12, 16, 20, 24, 28,
144 32, 36, 40, 44, 48, 52, 56, 60,
145 -1, -1, -1, -1, -1, -1, -1, -1, -1,
146 64
147 };
148
149 static int
150 arm_cannot_store_register (int regno)
151 {
152 return (regno >= arm_num_regs);
153 }
154
155 static int
156 arm_cannot_fetch_register (int regno)
157 {
158 return (regno >= arm_num_regs);
159 }
160
161 static void
162 arm_fill_wmmxregset (struct regcache *regcache, void *buf)
163 {
164 int i;
165
166 if (regcache->tdesc != tdesc_arm_with_iwmmxt)
167 return;
168
169 for (i = 0; i < 16; i++)
170 collect_register (regcache, arm_num_regs + i, (char *) buf + i * 8);
171
172 /* We only have access to wcssf, wcasf, and wcgr0-wcgr3. */
173 for (i = 0; i < 6; i++)
174 collect_register (regcache, arm_num_regs + i + 16,
175 (char *) buf + 16 * 8 + i * 4);
176 }
177
178 static void
179 arm_store_wmmxregset (struct regcache *regcache, const void *buf)
180 {
181 int i;
182
183 if (regcache->tdesc != tdesc_arm_with_iwmmxt)
184 return;
185
186 for (i = 0; i < 16; i++)
187 supply_register (regcache, arm_num_regs + i, (char *) buf + i * 8);
188
189 /* We only have access to wcssf, wcasf, and wcgr0-wcgr3. */
190 for (i = 0; i < 6; i++)
191 supply_register (regcache, arm_num_regs + i + 16,
192 (char *) buf + 16 * 8 + i * 4);
193 }
194
195 static void
196 arm_fill_vfpregset (struct regcache *regcache, void *buf)
197 {
198 int num;
199
200 if (regcache->tdesc == tdesc_arm_with_neon
201 || regcache->tdesc == tdesc_arm_with_vfpv3)
202 num = 32;
203 else if (regcache->tdesc == tdesc_arm_with_vfpv2)
204 num = 16;
205 else
206 return;
207
208 arm_fill_vfpregset_num (regcache, buf, num);
209 }
210
211 static void
212 arm_store_vfpregset (struct regcache *regcache, const void *buf)
213 {
214 int num;
215
216 if (regcache->tdesc == tdesc_arm_with_neon
217 || regcache->tdesc == tdesc_arm_with_vfpv3)
218 num = 32;
219 else if (regcache->tdesc == tdesc_arm_with_vfpv2)
220 num = 16;
221 else
222 return;
223
224 arm_store_vfpregset_num (regcache, buf, num);
225 }
226
227 extern int debug_threads;
228
229 static CORE_ADDR
230 arm_get_pc (struct regcache *regcache)
231 {
232 unsigned long pc;
233 collect_register_by_name (regcache, "pc", &pc);
234 if (debug_threads)
235 debug_printf ("stop pc is %08lx\n", pc);
236 return pc;
237 }
238
239 static void
240 arm_set_pc (struct regcache *regcache, CORE_ADDR pc)
241 {
242 unsigned long newpc = pc;
243 supply_register_by_name (regcache, "pc", &newpc);
244 }
245
246 /* Correct in either endianness. */
247 static const unsigned long arm_breakpoint = 0xef9f0001;
248 #define arm_breakpoint_len 4
249 static const unsigned short thumb_breakpoint = 0xde01;
250 #define thumb_breakpoint_len 2
251 static const unsigned short thumb2_breakpoint[] = { 0xf7f0, 0xa000 };
252 #define thumb2_breakpoint_len 4
253
254 /* For new EABI binaries. We recognize it regardless of which ABI
255 is used for gdbserver, so single threaded debugging should work
256 OK, but for multi-threaded debugging we only insert the current
257 ABI's breakpoint instruction. For now at least. */
258 static const unsigned long arm_eabi_breakpoint = 0xe7f001f0;
259
260 static int
261 arm_breakpoint_at (CORE_ADDR where)
262 {
263 struct regcache *regcache = get_thread_regcache (current_thread, 1);
264 unsigned long cpsr;
265
266 collect_register_by_name (regcache, "cpsr", &cpsr);
267
268 if (cpsr & 0x20)
269 {
270 /* Thumb mode. */
271 unsigned short insn;
272
273 (*the_target->read_memory) (where, (unsigned char *) &insn, 2);
274 if (insn == thumb_breakpoint)
275 return 1;
276
277 if (insn == thumb2_breakpoint[0])
278 {
279 (*the_target->read_memory) (where + 2, (unsigned char *) &insn, 2);
280 if (insn == thumb2_breakpoint[1])
281 return 1;
282 }
283 }
284 else
285 {
286 /* ARM mode. */
287 unsigned long insn;
288
289 (*the_target->read_memory) (where, (unsigned char *) &insn, 4);
290 if (insn == arm_breakpoint)
291 return 1;
292
293 if (insn == arm_eabi_breakpoint)
294 return 1;
295 }
296
297 return 0;
298 }
299
300 /* We only place breakpoints in empty marker functions, and thread locking
301 is outside of the function. So rather than importing software single-step,
302 we can just run until exit. */
303 static CORE_ADDR
304 arm_reinsert_addr (void)
305 {
306 struct regcache *regcache = get_thread_regcache (current_thread, 1);
307 unsigned long pc;
308 collect_register_by_name (regcache, "lr", &pc);
309 return pc;
310 }
311
312 /* Fetch the thread-local storage pointer for libthread_db. */
313
314 ps_err_e
315 ps_get_thread_area (const struct ps_prochandle *ph,
316 lwpid_t lwpid, int idx, void **base)
317 {
318 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
319 return PS_ERR;
320
321 /* IDX is the bias from the thread pointer to the beginning of the
322 thread descriptor. It has to be subtracted due to implementation
323 quirks in libthread_db. */
324 *base = (void *) ((char *)*base - idx);
325
326 return PS_OK;
327 }
328
329
330 /* Query Hardware Breakpoint information for the target we are attached to
331 (using PID as ptrace argument) and set up arm_linux_hwbp_cap. */
332 static void
333 arm_linux_init_hwbp_cap (int pid)
334 {
335 unsigned int val;
336
337 if (ptrace (PTRACE_GETHBPREGS, pid, 0, &val) < 0)
338 return;
339
340 arm_linux_hwbp_cap.arch = (unsigned char)((val >> 24) & 0xff);
341 if (arm_linux_hwbp_cap.arch == 0)
342 return;
343
344 arm_linux_hwbp_cap.max_wp_length = (unsigned char)((val >> 16) & 0xff);
345 arm_linux_hwbp_cap.wp_count = (unsigned char)((val >> 8) & 0xff);
346 arm_linux_hwbp_cap.bp_count = (unsigned char)(val & 0xff);
347
348 if (arm_linux_hwbp_cap.wp_count > MAX_WPTS)
349 internal_error (__FILE__, __LINE__, "Unsupported number of watchpoints");
350 if (arm_linux_hwbp_cap.bp_count > MAX_BPTS)
351 internal_error (__FILE__, __LINE__, "Unsupported number of breakpoints");
352 }
353
354 /* How many hardware breakpoints are available? */
355 static int
356 arm_linux_get_hw_breakpoint_count (void)
357 {
358 return arm_linux_hwbp_cap.bp_count;
359 }
360
361 /* How many hardware watchpoints are available? */
362 static int
363 arm_linux_get_hw_watchpoint_count (void)
364 {
365 return arm_linux_hwbp_cap.wp_count;
366 }
367
368 /* Maximum length of area watched by hardware watchpoint. */
369 static int
370 arm_linux_get_hw_watchpoint_max_length (void)
371 {
372 return arm_linux_hwbp_cap.max_wp_length;
373 }
374
375 /* Initialize an ARM hardware break-/watch-point control register value.
376 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
377 type of break-/watch-point; ENABLE indicates whether the point is enabled.
378 */
379 static arm_hwbp_control_t
380 arm_hwbp_control_initialize (unsigned byte_address_select,
381 arm_hwbp_type hwbp_type,
382 int enable)
383 {
384 gdb_assert ((byte_address_select & ~0xffU) == 0);
385 gdb_assert (hwbp_type != arm_hwbp_break
386 || ((byte_address_select & 0xfU) != 0));
387
388 return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
389 }
390
391 /* Does the breakpoint control value CONTROL have the enable bit set? */
392 static int
393 arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
394 {
395 return control & 0x1;
396 }
397
398 /* Is the breakpoint control value CONTROL initialized? */
399 static int
400 arm_hwbp_control_is_initialized (arm_hwbp_control_t control)
401 {
402 return control != 0;
403 }
404
405 /* Change a breakpoint control word so that it is in the disabled state. */
406 static arm_hwbp_control_t
407 arm_hwbp_control_disable (arm_hwbp_control_t control)
408 {
409 return control & ~0x1;
410 }
411
412 /* Are two break-/watch-points equal? */
413 static int
414 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
415 const struct arm_linux_hw_breakpoint *p2)
416 {
417 return p1->address == p2->address && p1->control == p2->control;
418 }
419
420 /* Convert a raw breakpoint type to an enum arm_hwbp_type. */
421
422 static int
423 raw_bkpt_type_to_arm_hwbp_type (enum raw_bkpt_type raw_type)
424 {
425 switch (raw_type)
426 {
427 case raw_bkpt_type_hw:
428 return arm_hwbp_break;
429 case raw_bkpt_type_write_wp:
430 return arm_hwbp_store;
431 case raw_bkpt_type_read_wp:
432 return arm_hwbp_load;
433 case raw_bkpt_type_access_wp:
434 return arm_hwbp_access;
435 default:
436 gdb_assert_not_reached ("unhandled raw type");
437 }
438 }
439
440 /* Initialize the hardware breakpoint structure P for a breakpoint or
441 watchpoint at ADDR to LEN. The type of watchpoint is given in TYPE.
442 Returns -1 if TYPE is unsupported, or -2 if the particular combination
443 of ADDR and LEN cannot be implemented. Otherwise, returns 0 if TYPE
444 represents a breakpoint and 1 if type represents a watchpoint. */
445 static int
446 arm_linux_hw_point_initialize (enum raw_bkpt_type raw_type, CORE_ADDR addr,
447 int len, struct arm_linux_hw_breakpoint *p)
448 {
449 arm_hwbp_type hwbp_type;
450 unsigned mask;
451
452 hwbp_type = raw_bkpt_type_to_arm_hwbp_type (raw_type);
453
454 if (hwbp_type == arm_hwbp_break)
455 {
456 /* For breakpoints, the length field encodes the mode. */
457 switch (len)
458 {
459 case 2: /* 16-bit Thumb mode breakpoint */
460 case 3: /* 32-bit Thumb mode breakpoint */
461 mask = 0x3;
462 addr &= ~1;
463 break;
464 case 4: /* 32-bit ARM mode breakpoint */
465 mask = 0xf;
466 addr &= ~3;
467 break;
468 default:
469 /* Unsupported. */
470 return -2;
471 }
472 }
473 else
474 {
475 CORE_ADDR max_wp_length = arm_linux_get_hw_watchpoint_max_length ();
476 CORE_ADDR aligned_addr;
477
478 /* Can not set watchpoints for zero or negative lengths. */
479 if (len <= 0)
480 return -2;
481 /* The current ptrace interface can only handle watchpoints that are a
482 power of 2. */
483 if ((len & (len - 1)) != 0)
484 return -2;
485
486 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
487 range covered by a watchpoint. */
488 aligned_addr = addr & ~(max_wp_length - 1);
489 if (aligned_addr + max_wp_length < addr + len)
490 return -2;
491
492 mask = (1 << len) - 1;
493 }
494
495 p->address = (unsigned int) addr;
496 p->control = arm_hwbp_control_initialize (mask, hwbp_type, 1);
497
498 return hwbp_type != arm_hwbp_break;
499 }
500
501 /* Callback to mark a watch-/breakpoint to be updated in all threads of
502 the current process. */
503
504 struct update_registers_data
505 {
506 int watch;
507 int i;
508 };
509
510 static int
511 update_registers_callback (struct inferior_list_entry *entry, void *arg)
512 {
513 struct thread_info *thread = (struct thread_info *) entry;
514 struct lwp_info *lwp = get_thread_lwp (thread);
515 struct update_registers_data *data = (struct update_registers_data *) arg;
516
517 /* Only update the threads of the current process. */
518 if (pid_of (thread) == pid_of (current_thread))
519 {
520 /* The actual update is done later just before resuming the lwp,
521 we just mark that the registers need updating. */
522 if (data->watch)
523 lwp->arch_private->wpts_changed[data->i] = 1;
524 else
525 lwp->arch_private->bpts_changed[data->i] = 1;
526
527 /* If the lwp isn't stopped, force it to momentarily pause, so
528 we can update its breakpoint registers. */
529 if (!lwp->stopped)
530 linux_stop_lwp (lwp);
531 }
532
533 return 0;
534 }
535
536 static int
537 arm_supports_z_point_type (char z_type)
538 {
539 switch (z_type)
540 {
541 case Z_PACKET_HW_BP:
542 case Z_PACKET_WRITE_WP:
543 case Z_PACKET_READ_WP:
544 case Z_PACKET_ACCESS_WP:
545 return 1;
546 default:
547 /* Leave the handling of sw breakpoints with the gdb client. */
548 return 0;
549 }
550 }
551
552 /* Insert hardware break-/watchpoint. */
553 static int
554 arm_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
555 int len, struct raw_breakpoint *bp)
556 {
557 struct process_info *proc = current_process ();
558 struct arm_linux_hw_breakpoint p, *pts;
559 int watch, i, count;
560
561 watch = arm_linux_hw_point_initialize (type, addr, len, &p);
562 if (watch < 0)
563 {
564 /* Unsupported. */
565 return watch == -1 ? 1 : -1;
566 }
567
568 if (watch)
569 {
570 count = arm_linux_get_hw_watchpoint_count ();
571 pts = proc->priv->arch_private->wpts;
572 }
573 else
574 {
575 count = arm_linux_get_hw_breakpoint_count ();
576 pts = proc->priv->arch_private->bpts;
577 }
578
579 for (i = 0; i < count; i++)
580 if (!arm_hwbp_control_is_enabled (pts[i].control))
581 {
582 struct update_registers_data data = { watch, i };
583 pts[i] = p;
584 find_inferior (&all_threads, update_registers_callback, &data);
585 return 0;
586 }
587
588 /* We're out of watchpoints. */
589 return -1;
590 }
591
592 /* Remove hardware break-/watchpoint. */
593 static int
594 arm_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
595 int len, struct raw_breakpoint *bp)
596 {
597 struct process_info *proc = current_process ();
598 struct arm_linux_hw_breakpoint p, *pts;
599 int watch, i, count;
600
601 watch = arm_linux_hw_point_initialize (type, addr, len, &p);
602 if (watch < 0)
603 {
604 /* Unsupported. */
605 return -1;
606 }
607
608 if (watch)
609 {
610 count = arm_linux_get_hw_watchpoint_count ();
611 pts = proc->priv->arch_private->wpts;
612 }
613 else
614 {
615 count = arm_linux_get_hw_breakpoint_count ();
616 pts = proc->priv->arch_private->bpts;
617 }
618
619 for (i = 0; i < count; i++)
620 if (arm_linux_hw_breakpoint_equal (&p, pts + i))
621 {
622 struct update_registers_data data = { watch, i };
623 pts[i].control = arm_hwbp_control_disable (pts[i].control);
624 find_inferior (&all_threads, update_registers_callback, &data);
625 return 0;
626 }
627
628 /* No watchpoint matched. */
629 return -1;
630 }
631
632 /* Return whether current thread is stopped due to a watchpoint. */
633 static int
634 arm_stopped_by_watchpoint (void)
635 {
636 struct lwp_info *lwp = get_thread_lwp (current_thread);
637 siginfo_t siginfo;
638
639 /* We must be able to set hardware watchpoints. */
640 if (arm_linux_get_hw_watchpoint_count () == 0)
641 return 0;
642
643 /* Retrieve siginfo. */
644 errno = 0;
645 ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread), 0, &siginfo);
646 if (errno != 0)
647 return 0;
648
649 /* This must be a hardware breakpoint. */
650 if (siginfo.si_signo != SIGTRAP
651 || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
652 return 0;
653
654 /* If we are in a positive slot then we're looking at a breakpoint and not
655 a watchpoint. */
656 if (siginfo.si_errno >= 0)
657 return 0;
658
659 /* Cache stopped data address for use by arm_stopped_data_address. */
660 lwp->arch_private->stopped_data_address
661 = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
662
663 return 1;
664 }
665
666 /* Return data address that triggered watchpoint. Called only if
667 arm_stopped_by_watchpoint returned true. */
668 static CORE_ADDR
669 arm_stopped_data_address (void)
670 {
671 struct lwp_info *lwp = get_thread_lwp (current_thread);
672 return lwp->arch_private->stopped_data_address;
673 }
674
675 /* Called when a new process is created. */
676 static struct arch_process_info *
677 arm_new_process (void)
678 {
679 struct arch_process_info *info = XCNEW (struct arch_process_info);
680 return info;
681 }
682
683 /* Called when a new thread is detected. */
684 static void
685 arm_new_thread (struct lwp_info *lwp)
686 {
687 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
688 int i;
689
690 for (i = 0; i < MAX_BPTS; i++)
691 info->bpts_changed[i] = 1;
692 for (i = 0; i < MAX_WPTS; i++)
693 info->wpts_changed[i] = 1;
694
695 lwp->arch_private = info;
696 }
697
698 static void
699 arm_new_fork (struct process_info *parent, struct process_info *child)
700 {
701 struct arch_process_info *parent_proc_info = parent->priv->arch_private;
702 struct arch_process_info *child_proc_info = child->priv->arch_private;
703 struct lwp_info *child_lwp;
704 struct arch_lwp_info *child_lwp_info;
705 int i;
706
707 /* These are allocated by linux_add_process. */
708 gdb_assert (parent->priv != NULL
709 && parent->priv->arch_private != NULL);
710 gdb_assert (child->priv != NULL
711 && child->priv->arch_private != NULL);
712
713 /* Linux kernel before 2.6.33 commit
714 72f674d203cd230426437cdcf7dd6f681dad8b0d
715 will inherit hardware debug registers from parent
716 on fork/vfork/clone. Newer Linux kernels create such tasks with
717 zeroed debug registers.
718
719 GDB core assumes the child inherits the watchpoints/hw
720 breakpoints of the parent, and will remove them all from the
721 forked off process. Copy the debug registers mirrors into the
722 new process so that all breakpoints and watchpoints can be
723 removed together. The debug registers mirror will become zeroed
724 in the end before detaching the forked off process, thus making
725 this compatible with older Linux kernels too. */
726
727 *child_proc_info = *parent_proc_info;
728
729 /* Mark all the hardware breakpoints and watchpoints as changed to
730 make sure that the registers will be updated. */
731 child_lwp = find_lwp_pid (ptid_of (child));
732 child_lwp_info = child_lwp->arch_private;
733 for (i = 0; i < MAX_BPTS; i++)
734 child_lwp_info->bpts_changed[i] = 1;
735 for (i = 0; i < MAX_WPTS; i++)
736 child_lwp_info->wpts_changed[i] = 1;
737 }
738
739 /* Called when resuming a thread.
740 If the debug regs have changed, update the thread's copies. */
741 static void
742 arm_prepare_to_resume (struct lwp_info *lwp)
743 {
744 struct thread_info *thread = get_lwp_thread (lwp);
745 int pid = lwpid_of (thread);
746 struct process_info *proc = find_process_pid (pid_of (thread));
747 struct arch_process_info *proc_info = proc->priv->arch_private;
748 struct arch_lwp_info *lwp_info = lwp->arch_private;
749 int i;
750
751 for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
752 if (lwp_info->bpts_changed[i])
753 {
754 errno = 0;
755
756 if (arm_hwbp_control_is_enabled (proc_info->bpts[i].control))
757 if (ptrace (PTRACE_SETHBPREGS, pid,
758 (PTRACE_TYPE_ARG3) ((i << 1) + 1),
759 &proc_info->bpts[i].address) < 0)
760 perror_with_name ("Unexpected error setting breakpoint address");
761
762 if (arm_hwbp_control_is_initialized (proc_info->bpts[i].control))
763 if (ptrace (PTRACE_SETHBPREGS, pid,
764 (PTRACE_TYPE_ARG3) ((i << 1) + 2),
765 &proc_info->bpts[i].control) < 0)
766 perror_with_name ("Unexpected error setting breakpoint");
767
768 lwp_info->bpts_changed[i] = 0;
769 }
770
771 for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
772 if (lwp_info->wpts_changed[i])
773 {
774 errno = 0;
775
776 if (arm_hwbp_control_is_enabled (proc_info->wpts[i].control))
777 if (ptrace (PTRACE_SETHBPREGS, pid,
778 (PTRACE_TYPE_ARG3) -((i << 1) + 1),
779 &proc_info->wpts[i].address) < 0)
780 perror_with_name ("Unexpected error setting watchpoint address");
781
782 if (arm_hwbp_control_is_initialized (proc_info->wpts[i].control))
783 if (ptrace (PTRACE_SETHBPREGS, pid,
784 (PTRACE_TYPE_ARG3) -((i << 1) + 2),
785 &proc_info->wpts[i].control) < 0)
786 perror_with_name ("Unexpected error setting watchpoint");
787
788 lwp_info->wpts_changed[i] = 0;
789 }
790 }
791
792
793 static int
794 arm_get_hwcap (unsigned long *valp)
795 {
796 unsigned char *data = alloca (8);
797 int offset = 0;
798
799 while ((*the_target->read_auxv) (offset, data, 8) == 8)
800 {
801 unsigned int *data_p = (unsigned int *)data;
802 if (data_p[0] == AT_HWCAP)
803 {
804 *valp = data_p[1];
805 return 1;
806 }
807
808 offset += 8;
809 }
810
811 *valp = 0;
812 return 0;
813 }
814
815 static const struct target_desc *
816 arm_read_description (void)
817 {
818 int pid = lwpid_of (current_thread);
819 unsigned long arm_hwcap = 0;
820
821 /* Query hardware watchpoint/breakpoint capabilities. */
822 arm_linux_init_hwbp_cap (pid);
823
824 if (arm_get_hwcap (&arm_hwcap) == 0)
825 return tdesc_arm;
826
827 if (arm_hwcap & HWCAP_IWMMXT)
828 return tdesc_arm_with_iwmmxt;
829
830 if (arm_hwcap & HWCAP_VFP)
831 {
832 const struct target_desc *result;
833 char *buf;
834
835 /* NEON implies either no VFP, or VFPv3-D32. We only support
836 it with VFP. */
837 if (arm_hwcap & HWCAP_NEON)
838 result = tdesc_arm_with_neon;
839 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
840 result = tdesc_arm_with_vfpv3;
841 else
842 result = tdesc_arm_with_vfpv2;
843
844 /* Now make sure that the kernel supports reading these
845 registers. Support was added in 2.6.30. */
846 errno = 0;
847 buf = xmalloc (32 * 8 + 4);
848 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
849 && errno == EIO)
850 result = tdesc_arm;
851
852 free (buf);
853
854 return result;
855 }
856
857 /* The default configuration uses legacy FPA registers, probably
858 simulated. */
859 return tdesc_arm;
860 }
861
862 static void
863 arm_arch_setup (void)
864 {
865 int tid = lwpid_of (current_thread);
866 int gpregs[18];
867 struct iovec iov;
868
869 current_process ()->tdesc = arm_read_description ();
870
871 iov.iov_base = gpregs;
872 iov.iov_len = sizeof (gpregs);
873
874 /* Check if PTRACE_GETREGSET works. */
875 if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) == 0)
876 have_ptrace_getregset = 1;
877 else
878 have_ptrace_getregset = 0;
879 }
880
881 /* Register sets without using PTRACE_GETREGSET. */
882
883 static struct regset_info arm_regsets[] = {
884 { PTRACE_GETREGS, PTRACE_SETREGS, 0, 18 * 4,
885 GENERAL_REGS,
886 arm_fill_gregset, arm_store_gregset },
887 { PTRACE_GETWMMXREGS, PTRACE_SETWMMXREGS, 0, 16 * 8 + 6 * 4,
888 EXTENDED_REGS,
889 arm_fill_wmmxregset, arm_store_wmmxregset },
890 { PTRACE_GETVFPREGS, PTRACE_SETVFPREGS, 0, 32 * 8 + 4,
891 EXTENDED_REGS,
892 arm_fill_vfpregset, arm_store_vfpregset },
893 { 0, 0, 0, -1, -1, NULL, NULL }
894 };
895
896 static struct regsets_info arm_regsets_info =
897 {
898 arm_regsets, /* regsets */
899 0, /* num_regsets */
900 NULL, /* disabled_regsets */
901 };
902
903 static struct usrregs_info arm_usrregs_info =
904 {
905 arm_num_regs,
906 arm_regmap,
907 };
908
909 static struct regs_info regs_info_arm =
910 {
911 NULL, /* regset_bitmap */
912 &arm_usrregs_info,
913 &arm_regsets_info
914 };
915
916 static const struct regs_info *
917 arm_regs_info (void)
918 {
919 const struct target_desc *tdesc = current_process ()->tdesc;
920
921 if (have_ptrace_getregset == 1
922 && (tdesc == tdesc_arm_with_neon || tdesc == tdesc_arm_with_vfpv3))
923 return &regs_info_aarch32;
924 else
925 return &regs_info_arm;
926 }
927
928 /* Implementation of linux_target_ops method "breakpoint_kind_from_pc".
929
930 Determine the type and size of breakpoint to insert at PCPTR. Uses the
931 program counter value to determine whether a 16-bit or 32-bit breakpoint
932 should be used. It returns the breakpoint's kind, and adjusts the program
933 counter (if necessary) to point to the actual memory location where the
934 breakpoint should be inserted. */
935
936 static int
937 arm_breakpoint_kind_from_pc (CORE_ADDR *pcptr)
938 {
939 if (IS_THUMB_ADDR (*pcptr))
940 {
941 gdb_byte buf[2];
942
943 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
944
945 /* Check whether we are replacing a thumb2 32-bit instruction. */
946 if ((*the_target->read_memory) (*pcptr, buf, 2) == 0)
947 {
948 unsigned short inst1 = 0;
949
950 (*the_target->read_memory) (*pcptr, (gdb_byte *) &inst1, 2);
951 if (thumb_insn_size (inst1) == 4)
952 return ARM_BP_KIND_THUMB2;
953 }
954 return ARM_BP_KIND_THUMB;
955 }
956 else
957 return ARM_BP_KIND_ARM;
958 }
959
960 /* Implementation of the linux_target_ops method "sw_breakpoint_from_kind". */
961
962 static const gdb_byte *
963 arm_sw_breakpoint_from_kind (int kind , int *size)
964 {
965 *size = arm_breakpoint_len;
966 /* Define an ARM-mode breakpoint; we only set breakpoints in the C
967 library, which is most likely to be ARM. If the kernel supports
968 clone events, we will never insert a breakpoint, so even a Thumb
969 C library will work; so will mixing EABI/non-EABI gdbserver and
970 application. */
971 switch (kind)
972 {
973 case ARM_BP_KIND_THUMB:
974 *size = thumb_breakpoint_len;
975 return (gdb_byte *) &thumb_breakpoint;
976 case ARM_BP_KIND_THUMB2:
977 *size = thumb2_breakpoint_len;
978 return (gdb_byte *) &thumb2_breakpoint;
979 case ARM_BP_KIND_ARM:
980 *size = arm_breakpoint_len;
981 #ifndef __ARM_EABI__
982 return (const gdb_byte *) &arm_breakpoint;
983 #else
984 return (const gdb_byte *) &arm_eabi_breakpoint;
985 #endif
986 default:
987 return NULL;
988 }
989 return NULL;
990 }
991
992 struct linux_target_ops the_low_target = {
993 arm_arch_setup,
994 arm_regs_info,
995 arm_cannot_fetch_register,
996 arm_cannot_store_register,
997 NULL, /* fetch_register */
998 arm_get_pc,
999 arm_set_pc,
1000 arm_breakpoint_kind_from_pc,
1001 arm_sw_breakpoint_from_kind,
1002 arm_reinsert_addr,
1003 0,
1004 arm_breakpoint_at,
1005 arm_supports_z_point_type,
1006 arm_insert_point,
1007 arm_remove_point,
1008 arm_stopped_by_watchpoint,
1009 arm_stopped_data_address,
1010 NULL, /* collect_ptrace_register */
1011 NULL, /* supply_ptrace_register */
1012 NULL, /* siginfo_fixup */
1013 arm_new_process,
1014 arm_new_thread,
1015 arm_new_fork,
1016 arm_prepare_to_resume,
1017 };
1018
1019 void
1020 initialize_low_arch (void)
1021 {
1022 /* Initialize the Linux target descriptions. */
1023 init_registers_arm ();
1024 init_registers_arm_with_iwmmxt ();
1025 init_registers_arm_with_vfpv2 ();
1026 init_registers_arm_with_vfpv3 ();
1027
1028 initialize_low_arch_aarch32 ();
1029
1030 initialize_regsets_info (&arm_regsets_info);
1031 }
This page took 0.081979 seconds and 4 git commands to generate.