gdbserver/linux-low: turn 'supports_hardware_single_step' into a method
[deliverable/binutils-gdb.git] / gdbserver / linux-arm-low.cc
... / ...
CommitLineData
1/* GNU/Linux/ARM specific low level interface, for the remote server for GDB.
2 Copyright (C) 1995-2020 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 "arch/arm-linux.h"
23#include "arch/arm-get-next-pcs.h"
24#include "linux-aarch32-low.h"
25#include "linux-aarch32-tdesc.h"
26#include "linux-arm-tdesc.h"
27
28#include <sys/uio.h>
29/* Don't include elf.h if linux/elf.h got included by gdb_proc_service.h.
30 On Bionic elf.h and linux/elf.h have conflicting definitions. */
31#ifndef ELFMAG0
32#include <elf.h>
33#endif
34#include "nat/gdb_ptrace.h"
35#include <signal.h>
36#include <sys/syscall.h>
37
38#ifndef PTRACE_GET_THREAD_AREA
39#define PTRACE_GET_THREAD_AREA 22
40#endif
41
42#ifndef PTRACE_GETWMMXREGS
43# define PTRACE_GETWMMXREGS 18
44# define PTRACE_SETWMMXREGS 19
45#endif
46
47#ifndef PTRACE_GETVFPREGS
48# define PTRACE_GETVFPREGS 27
49# define PTRACE_SETVFPREGS 28
50#endif
51
52#ifndef PTRACE_GETHBPREGS
53#define PTRACE_GETHBPREGS 29
54#define PTRACE_SETHBPREGS 30
55#endif
56
57/* Linux target op definitions for the ARM architecture. */
58
59class arm_target : public linux_process_target
60{
61public:
62
63 const regs_info *get_regs_info () override;
64
65 int breakpoint_kind_from_pc (CORE_ADDR *pcptr) override;
66
67 int breakpoint_kind_from_current_state (CORE_ADDR *pcptr) override;
68
69 const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
70
71 bool supports_software_single_step () override;
72
73 bool supports_z_point_type (char z_type) override;
74
75 bool supports_hardware_single_step () override;
76
77protected:
78
79 void low_arch_setup () override;
80
81 bool low_cannot_fetch_register (int regno) override;
82
83 bool low_cannot_store_register (int regno) override;
84
85 bool low_supports_breakpoints () override;
86
87 CORE_ADDR low_get_pc (regcache *regcache) override;
88
89 void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
90
91 std::vector<CORE_ADDR> low_get_next_pcs (regcache *regcache) override;
92
93 bool low_breakpoint_at (CORE_ADDR pc) override;
94
95 int low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
96 int size, raw_breakpoint *bp) override;
97
98 int low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
99 int size, raw_breakpoint *bp) override;
100
101 bool low_stopped_by_watchpoint () override;
102
103 CORE_ADDR low_stopped_data_address () override;
104
105 arch_process_info *low_new_process () override;
106
107 void low_delete_process (arch_process_info *info) override;
108
109 void low_new_thread (lwp_info *) override;
110
111 void low_delete_thread (arch_lwp_info *) override;
112
113 void low_new_fork (process_info *parent, process_info *child) override;
114
115 void low_prepare_to_resume (lwp_info *lwp) override;
116};
117
118/* The singleton target ops object. */
119
120static arm_target the_arm_target;
121
122bool
123arm_target::low_supports_breakpoints ()
124{
125 return true;
126}
127
128CORE_ADDR
129arm_target::low_get_pc (regcache *regcache)
130{
131 return linux_get_pc_32bit (regcache);
132}
133
134void
135arm_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
136{
137 linux_set_pc_32bit (regcache, pc);
138}
139
140int
141arm_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
142{
143 return arm_breakpoint_kind_from_pc (pcptr);
144}
145
146int
147arm_target::breakpoint_kind_from_current_state (CORE_ADDR *pcptr)
148{
149 return arm_breakpoint_kind_from_current_state (pcptr);
150}
151
152const gdb_byte *
153arm_target::sw_breakpoint_from_kind (int kind, int *size)
154{
155 return arm_sw_breakpoint_from_kind (kind, size);
156}
157
158bool
159arm_target::low_breakpoint_at (CORE_ADDR pc)
160{
161 return arm_breakpoint_at (pc);
162}
163
164/* Information describing the hardware breakpoint capabilities. */
165static struct
166{
167 unsigned char arch;
168 unsigned char max_wp_length;
169 unsigned char wp_count;
170 unsigned char bp_count;
171} arm_linux_hwbp_cap;
172
173/* Enum describing the different types of ARM hardware break-/watch-points. */
174typedef enum
175{
176 arm_hwbp_break = 0,
177 arm_hwbp_load = 1,
178 arm_hwbp_store = 2,
179 arm_hwbp_access = 3
180} arm_hwbp_type;
181
182/* Type describing an ARM Hardware Breakpoint Control register value. */
183typedef unsigned int arm_hwbp_control_t;
184
185/* Structure used to keep track of hardware break-/watch-points. */
186struct arm_linux_hw_breakpoint
187{
188 /* Address to break on, or being watched. */
189 unsigned int address;
190 /* Control register for break-/watch- point. */
191 arm_hwbp_control_t control;
192};
193
194/* Since we cannot dynamically allocate subfields of arch_process_info,
195 assume a maximum number of supported break-/watchpoints. */
196#define MAX_BPTS 32
197#define MAX_WPTS 32
198
199/* Per-process arch-specific data we want to keep. */
200struct arch_process_info
201{
202 /* Hardware breakpoints for this process. */
203 struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
204 /* Hardware watchpoints for this process. */
205 struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
206};
207
208/* Per-thread arch-specific data we want to keep. */
209struct arch_lwp_info
210{
211 /* Non-zero if our copy differs from what's recorded in the thread. */
212 char bpts_changed[MAX_BPTS];
213 char wpts_changed[MAX_WPTS];
214 /* Cached stopped data address. */
215 CORE_ADDR stopped_data_address;
216};
217
218/* These are in <asm/elf.h> in current kernels. */
219#define HWCAP_VFP 64
220#define HWCAP_IWMMXT 512
221#define HWCAP_NEON 4096
222#define HWCAP_VFPv3 8192
223#define HWCAP_VFPv3D16 16384
224
225#ifdef HAVE_SYS_REG_H
226#include <sys/reg.h>
227#endif
228
229#define arm_num_regs 26
230
231static int arm_regmap[] = {
232 0, 4, 8, 12, 16, 20, 24, 28,
233 32, 36, 40, 44, 48, 52, 56, 60,
234 -1, -1, -1, -1, -1, -1, -1, -1, -1,
235 64
236};
237
238/* Forward declarations needed for get_next_pcs ops. */
239static ULONGEST get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr,
240 int len,
241 int byte_order);
242
243static CORE_ADDR get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
244 CORE_ADDR val);
245
246static CORE_ADDR get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
247
248static int get_next_pcs_is_thumb (struct arm_get_next_pcs *self);
249
250/* get_next_pcs operations. */
251static struct arm_get_next_pcs_ops get_next_pcs_ops = {
252 get_next_pcs_read_memory_unsigned_integer,
253 get_next_pcs_syscall_next_pc,
254 get_next_pcs_addr_bits_remove,
255 get_next_pcs_is_thumb,
256 arm_linux_get_next_pcs_fixup,
257};
258
259bool
260arm_target::low_cannot_store_register (int regno)
261{
262 return (regno >= arm_num_regs);
263}
264
265bool
266arm_target::low_cannot_fetch_register (int regno)
267{
268 return (regno >= arm_num_regs);
269}
270
271static void
272arm_fill_wmmxregset (struct regcache *regcache, void *buf)
273{
274 if (arm_linux_get_tdesc_fp_type (regcache->tdesc) != ARM_FP_TYPE_IWMMXT)
275 return;
276
277 for (int i = 0; i < 16; i++)
278 collect_register (regcache, arm_num_regs + i, (char *) buf + i * 8);
279
280 /* We only have access to wcssf, wcasf, and wcgr0-wcgr3. */
281 for (int i = 0; i < 6; i++)
282 collect_register (regcache, arm_num_regs + i + 16,
283 (char *) buf + 16 * 8 + i * 4);
284}
285
286static void
287arm_store_wmmxregset (struct regcache *regcache, const void *buf)
288{
289 if (arm_linux_get_tdesc_fp_type (regcache->tdesc) != ARM_FP_TYPE_IWMMXT)
290 return;
291
292 for (int i = 0; i < 16; i++)
293 supply_register (regcache, arm_num_regs + i, (char *) buf + i * 8);
294
295 /* We only have access to wcssf, wcasf, and wcgr0-wcgr3. */
296 for (int i = 0; i < 6; i++)
297 supply_register (regcache, arm_num_regs + i + 16,
298 (char *) buf + 16 * 8 + i * 4);
299}
300
301static void
302arm_fill_vfpregset (struct regcache *regcache, void *buf)
303{
304 int num;
305
306 if (is_aarch32_linux_description (regcache->tdesc))
307 num = 32;
308 else
309 {
310 arm_fp_type fp_type = arm_linux_get_tdesc_fp_type (regcache->tdesc);
311
312 if (fp_type == ARM_FP_TYPE_VFPV3)
313 num = 32;
314 else if (fp_type == ARM_FP_TYPE_VFPV2)
315 num = 16;
316 else
317 return;
318 }
319
320 arm_fill_vfpregset_num (regcache, buf, num);
321}
322
323/* Wrapper of UNMAKE_THUMB_ADDR for get_next_pcs. */
324static CORE_ADDR
325get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self, CORE_ADDR val)
326{
327 return UNMAKE_THUMB_ADDR (val);
328}
329
330static void
331arm_store_vfpregset (struct regcache *regcache, const void *buf)
332{
333 int num;
334
335 if (is_aarch32_linux_description (regcache->tdesc))
336 num = 32;
337 else
338 {
339 arm_fp_type fp_type = arm_linux_get_tdesc_fp_type (regcache->tdesc);
340
341 if (fp_type == ARM_FP_TYPE_VFPV3)
342 num = 32;
343 else if (fp_type == ARM_FP_TYPE_VFPV2)
344 num = 16;
345 else
346 return;
347 }
348
349 arm_store_vfpregset_num (regcache, buf, num);
350}
351
352/* Wrapper of arm_is_thumb_mode for get_next_pcs. */
353static int
354get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
355{
356 return arm_is_thumb_mode ();
357}
358
359/* Read memory from the inferior.
360 BYTE_ORDER is ignored and there to keep compatiblity with GDB's
361 read_memory_unsigned_integer. */
362static ULONGEST
363get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr,
364 int len,
365 int byte_order)
366{
367 ULONGEST res;
368
369 res = 0;
370 target_read_memory (memaddr, (unsigned char *) &res, len);
371
372 return res;
373}
374
375/* Fetch the thread-local storage pointer for libthread_db. */
376
377ps_err_e
378ps_get_thread_area (struct ps_prochandle *ph,
379 lwpid_t lwpid, int idx, void **base)
380{
381 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
382 return PS_ERR;
383
384 /* IDX is the bias from the thread pointer to the beginning of the
385 thread descriptor. It has to be subtracted due to implementation
386 quirks in libthread_db. */
387 *base = (void *) ((char *)*base - idx);
388
389 return PS_OK;
390}
391
392
393/* Query Hardware Breakpoint information for the target we are attached to
394 (using PID as ptrace argument) and set up arm_linux_hwbp_cap. */
395static void
396arm_linux_init_hwbp_cap (int pid)
397{
398 unsigned int val;
399
400 if (ptrace (PTRACE_GETHBPREGS, pid, 0, &val) < 0)
401 return;
402
403 arm_linux_hwbp_cap.arch = (unsigned char)((val >> 24) & 0xff);
404 if (arm_linux_hwbp_cap.arch == 0)
405 return;
406
407 arm_linux_hwbp_cap.max_wp_length = (unsigned char)((val >> 16) & 0xff);
408 arm_linux_hwbp_cap.wp_count = (unsigned char)((val >> 8) & 0xff);
409 arm_linux_hwbp_cap.bp_count = (unsigned char)(val & 0xff);
410
411 if (arm_linux_hwbp_cap.wp_count > MAX_WPTS)
412 internal_error (__FILE__, __LINE__, "Unsupported number of watchpoints");
413 if (arm_linux_hwbp_cap.bp_count > MAX_BPTS)
414 internal_error (__FILE__, __LINE__, "Unsupported number of breakpoints");
415}
416
417/* How many hardware breakpoints are available? */
418static int
419arm_linux_get_hw_breakpoint_count (void)
420{
421 return arm_linux_hwbp_cap.bp_count;
422}
423
424/* How many hardware watchpoints are available? */
425static int
426arm_linux_get_hw_watchpoint_count (void)
427{
428 return arm_linux_hwbp_cap.wp_count;
429}
430
431/* Maximum length of area watched by hardware watchpoint. */
432static int
433arm_linux_get_hw_watchpoint_max_length (void)
434{
435 return arm_linux_hwbp_cap.max_wp_length;
436}
437
438/* Initialize an ARM hardware break-/watch-point control register value.
439 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
440 type of break-/watch-point; ENABLE indicates whether the point is enabled.
441 */
442static arm_hwbp_control_t
443arm_hwbp_control_initialize (unsigned byte_address_select,
444 arm_hwbp_type hwbp_type,
445 int enable)
446{
447 gdb_assert ((byte_address_select & ~0xffU) == 0);
448 gdb_assert (hwbp_type != arm_hwbp_break
449 || ((byte_address_select & 0xfU) != 0));
450
451 return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
452}
453
454/* Does the breakpoint control value CONTROL have the enable bit set? */
455static int
456arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
457{
458 return control & 0x1;
459}
460
461/* Is the breakpoint control value CONTROL initialized? */
462static int
463arm_hwbp_control_is_initialized (arm_hwbp_control_t control)
464{
465 return control != 0;
466}
467
468/* Change a breakpoint control word so that it is in the disabled state. */
469static arm_hwbp_control_t
470arm_hwbp_control_disable (arm_hwbp_control_t control)
471{
472 return control & ~0x1;
473}
474
475/* Are two break-/watch-points equal? */
476static int
477arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
478 const struct arm_linux_hw_breakpoint *p2)
479{
480 return p1->address == p2->address && p1->control == p2->control;
481}
482
483/* Convert a raw breakpoint type to an enum arm_hwbp_type. */
484
485static arm_hwbp_type
486raw_bkpt_type_to_arm_hwbp_type (enum raw_bkpt_type raw_type)
487{
488 switch (raw_type)
489 {
490 case raw_bkpt_type_hw:
491 return arm_hwbp_break;
492 case raw_bkpt_type_write_wp:
493 return arm_hwbp_store;
494 case raw_bkpt_type_read_wp:
495 return arm_hwbp_load;
496 case raw_bkpt_type_access_wp:
497 return arm_hwbp_access;
498 default:
499 gdb_assert_not_reached ("unhandled raw type");
500 }
501}
502
503/* Initialize the hardware breakpoint structure P for a breakpoint or
504 watchpoint at ADDR to LEN. The type of watchpoint is given in TYPE.
505 Returns -1 if TYPE is unsupported, or -2 if the particular combination
506 of ADDR and LEN cannot be implemented. Otherwise, returns 0 if TYPE
507 represents a breakpoint and 1 if type represents a watchpoint. */
508static int
509arm_linux_hw_point_initialize (enum raw_bkpt_type raw_type, CORE_ADDR addr,
510 int len, struct arm_linux_hw_breakpoint *p)
511{
512 arm_hwbp_type hwbp_type;
513 unsigned mask;
514
515 hwbp_type = raw_bkpt_type_to_arm_hwbp_type (raw_type);
516
517 if (hwbp_type == arm_hwbp_break)
518 {
519 /* For breakpoints, the length field encodes the mode. */
520 switch (len)
521 {
522 case 2: /* 16-bit Thumb mode breakpoint */
523 case 3: /* 32-bit Thumb mode breakpoint */
524 mask = 0x3;
525 addr &= ~1;
526 break;
527 case 4: /* 32-bit ARM mode breakpoint */
528 mask = 0xf;
529 addr &= ~3;
530 break;
531 default:
532 /* Unsupported. */
533 return -2;
534 }
535 }
536 else
537 {
538 CORE_ADDR max_wp_length = arm_linux_get_hw_watchpoint_max_length ();
539 CORE_ADDR aligned_addr;
540
541 /* Can not set watchpoints for zero or negative lengths. */
542 if (len <= 0)
543 return -2;
544 /* The current ptrace interface can only handle watchpoints that are a
545 power of 2. */
546 if ((len & (len - 1)) != 0)
547 return -2;
548
549 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
550 range covered by a watchpoint. */
551 aligned_addr = addr & ~(max_wp_length - 1);
552 if (aligned_addr + max_wp_length < addr + len)
553 return -2;
554
555 mask = (1 << len) - 1;
556 }
557
558 p->address = (unsigned int) addr;
559 p->control = arm_hwbp_control_initialize (mask, hwbp_type, 1);
560
561 return hwbp_type != arm_hwbp_break;
562}
563
564/* Callback to mark a watch-/breakpoint to be updated in all threads of
565 the current process. */
566
567static void
568update_registers_callback (thread_info *thread, int watch, int i)
569{
570 struct lwp_info *lwp = get_thread_lwp (thread);
571
572 /* The actual update is done later just before resuming the lwp,
573 we just mark that the registers need updating. */
574 if (watch)
575 lwp->arch_private->wpts_changed[i] = 1;
576 else
577 lwp->arch_private->bpts_changed[i] = 1;
578
579 /* If the lwp isn't stopped, force it to momentarily pause, so
580 we can update its breakpoint registers. */
581 if (!lwp->stopped)
582 linux_stop_lwp (lwp);
583}
584
585bool
586arm_target::supports_z_point_type (char z_type)
587{
588 switch (z_type)
589 {
590 case Z_PACKET_SW_BP:
591 case Z_PACKET_HW_BP:
592 case Z_PACKET_WRITE_WP:
593 case Z_PACKET_READ_WP:
594 case Z_PACKET_ACCESS_WP:
595 return true;
596 default:
597 /* Leave the handling of sw breakpoints with the gdb client. */
598 return false;
599 }
600}
601
602/* Insert hardware break-/watchpoint. */
603int
604arm_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
605 int len, raw_breakpoint *bp)
606{
607 struct process_info *proc = current_process ();
608 struct arm_linux_hw_breakpoint p, *pts;
609 int watch, i, count;
610
611 watch = arm_linux_hw_point_initialize (type, addr, len, &p);
612 if (watch < 0)
613 {
614 /* Unsupported. */
615 return watch == -1 ? 1 : -1;
616 }
617
618 if (watch)
619 {
620 count = arm_linux_get_hw_watchpoint_count ();
621 pts = proc->priv->arch_private->wpts;
622 }
623 else
624 {
625 count = arm_linux_get_hw_breakpoint_count ();
626 pts = proc->priv->arch_private->bpts;
627 }
628
629 for (i = 0; i < count; i++)
630 if (!arm_hwbp_control_is_enabled (pts[i].control))
631 {
632 pts[i] = p;
633
634 /* Only update the threads of the current process. */
635 for_each_thread (current_thread->id.pid (), [&] (thread_info *thread)
636 {
637 update_registers_callback (thread, watch, i);
638 });
639
640 return 0;
641 }
642
643 /* We're out of watchpoints. */
644 return -1;
645}
646
647/* Remove hardware break-/watchpoint. */
648int
649arm_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
650 int len, raw_breakpoint *bp)
651{
652 struct process_info *proc = current_process ();
653 struct arm_linux_hw_breakpoint p, *pts;
654 int watch, i, count;
655
656 watch = arm_linux_hw_point_initialize (type, addr, len, &p);
657 if (watch < 0)
658 {
659 /* Unsupported. */
660 return -1;
661 }
662
663 if (watch)
664 {
665 count = arm_linux_get_hw_watchpoint_count ();
666 pts = proc->priv->arch_private->wpts;
667 }
668 else
669 {
670 count = arm_linux_get_hw_breakpoint_count ();
671 pts = proc->priv->arch_private->bpts;
672 }
673
674 for (i = 0; i < count; i++)
675 if (arm_linux_hw_breakpoint_equal (&p, pts + i))
676 {
677 pts[i].control = arm_hwbp_control_disable (pts[i].control);
678
679 /* Only update the threads of the current process. */
680 for_each_thread (current_thread->id.pid (), [&] (thread_info *thread)
681 {
682 update_registers_callback (thread, watch, i);
683 });
684
685 return 0;
686 }
687
688 /* No watchpoint matched. */
689 return -1;
690}
691
692/* Return whether current thread is stopped due to a watchpoint. */
693bool
694arm_target::low_stopped_by_watchpoint ()
695{
696 struct lwp_info *lwp = get_thread_lwp (current_thread);
697 siginfo_t siginfo;
698
699 /* We must be able to set hardware watchpoints. */
700 if (arm_linux_get_hw_watchpoint_count () == 0)
701 return false;
702
703 /* Retrieve siginfo. */
704 errno = 0;
705 ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread), 0, &siginfo);
706 if (errno != 0)
707 return false;
708
709 /* This must be a hardware breakpoint. */
710 if (siginfo.si_signo != SIGTRAP
711 || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
712 return false;
713
714 /* If we are in a positive slot then we're looking at a breakpoint and not
715 a watchpoint. */
716 if (siginfo.si_errno >= 0)
717 return false;
718
719 /* Cache stopped data address for use by arm_stopped_data_address. */
720 lwp->arch_private->stopped_data_address
721 = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
722
723 return true;
724}
725
726/* Return data address that triggered watchpoint. Called only if
727 low_stopped_by_watchpoint returned true. */
728CORE_ADDR
729arm_target::low_stopped_data_address ()
730{
731 struct lwp_info *lwp = get_thread_lwp (current_thread);
732 return lwp->arch_private->stopped_data_address;
733}
734
735/* Called when a new process is created. */
736arch_process_info *
737arm_target::low_new_process ()
738{
739 struct arch_process_info *info = XCNEW (struct arch_process_info);
740 return info;
741}
742
743/* Called when a process is being deleted. */
744
745void
746arm_target::low_delete_process (arch_process_info *info)
747{
748 xfree (info);
749}
750
751/* Called when a new thread is detected. */
752void
753arm_target::low_new_thread (lwp_info *lwp)
754{
755 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
756 int i;
757
758 for (i = 0; i < MAX_BPTS; i++)
759 info->bpts_changed[i] = 1;
760 for (i = 0; i < MAX_WPTS; i++)
761 info->wpts_changed[i] = 1;
762
763 lwp->arch_private = info;
764}
765
766/* Function to call when a thread is being deleted. */
767
768void
769arm_target::low_delete_thread (arch_lwp_info *arch_lwp)
770{
771 xfree (arch_lwp);
772}
773
774void
775arm_target::low_new_fork (process_info *parent, process_info *child)
776{
777 struct arch_process_info *parent_proc_info;
778 struct arch_process_info *child_proc_info;
779 struct lwp_info *child_lwp;
780 struct arch_lwp_info *child_lwp_info;
781 int i;
782
783 /* These are allocated by linux_add_process. */
784 gdb_assert (parent->priv != NULL
785 && parent->priv->arch_private != NULL);
786 gdb_assert (child->priv != NULL
787 && child->priv->arch_private != NULL);
788
789 parent_proc_info = parent->priv->arch_private;
790 child_proc_info = child->priv->arch_private;
791
792 /* Linux kernel before 2.6.33 commit
793 72f674d203cd230426437cdcf7dd6f681dad8b0d
794 will inherit hardware debug registers from parent
795 on fork/vfork/clone. Newer Linux kernels create such tasks with
796 zeroed debug registers.
797
798 GDB core assumes the child inherits the watchpoints/hw
799 breakpoints of the parent, and will remove them all from the
800 forked off process. Copy the debug registers mirrors into the
801 new process so that all breakpoints and watchpoints can be
802 removed together. The debug registers mirror will become zeroed
803 in the end before detaching the forked off process, thus making
804 this compatible with older Linux kernels too. */
805
806 *child_proc_info = *parent_proc_info;
807
808 /* Mark all the hardware breakpoints and watchpoints as changed to
809 make sure that the registers will be updated. */
810 child_lwp = find_lwp_pid (ptid_t (child->pid));
811 child_lwp_info = child_lwp->arch_private;
812 for (i = 0; i < MAX_BPTS; i++)
813 child_lwp_info->bpts_changed[i] = 1;
814 for (i = 0; i < MAX_WPTS; i++)
815 child_lwp_info->wpts_changed[i] = 1;
816}
817
818/* Called when resuming a thread.
819 If the debug regs have changed, update the thread's copies. */
820void
821arm_target::low_prepare_to_resume (lwp_info *lwp)
822{
823 struct thread_info *thread = get_lwp_thread (lwp);
824 int pid = lwpid_of (thread);
825 struct process_info *proc = find_process_pid (pid_of (thread));
826 struct arch_process_info *proc_info = proc->priv->arch_private;
827 struct arch_lwp_info *lwp_info = lwp->arch_private;
828 int i;
829
830 for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
831 if (lwp_info->bpts_changed[i])
832 {
833 errno = 0;
834
835 if (arm_hwbp_control_is_enabled (proc_info->bpts[i].control))
836 if (ptrace (PTRACE_SETHBPREGS, pid,
837 (PTRACE_TYPE_ARG3) ((i << 1) + 1),
838 &proc_info->bpts[i].address) < 0)
839 perror_with_name ("Unexpected error setting breakpoint address");
840
841 if (arm_hwbp_control_is_initialized (proc_info->bpts[i].control))
842 if (ptrace (PTRACE_SETHBPREGS, pid,
843 (PTRACE_TYPE_ARG3) ((i << 1) + 2),
844 &proc_info->bpts[i].control) < 0)
845 perror_with_name ("Unexpected error setting breakpoint");
846
847 lwp_info->bpts_changed[i] = 0;
848 }
849
850 for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
851 if (lwp_info->wpts_changed[i])
852 {
853 errno = 0;
854
855 if (arm_hwbp_control_is_enabled (proc_info->wpts[i].control))
856 if (ptrace (PTRACE_SETHBPREGS, pid,
857 (PTRACE_TYPE_ARG3) -((i << 1) + 1),
858 &proc_info->wpts[i].address) < 0)
859 perror_with_name ("Unexpected error setting watchpoint address");
860
861 if (arm_hwbp_control_is_initialized (proc_info->wpts[i].control))
862 if (ptrace (PTRACE_SETHBPREGS, pid,
863 (PTRACE_TYPE_ARG3) -((i << 1) + 2),
864 &proc_info->wpts[i].control) < 0)
865 perror_with_name ("Unexpected error setting watchpoint");
866
867 lwp_info->wpts_changed[i] = 0;
868 }
869}
870
871/* Find the next pc for a sigreturn or rt_sigreturn syscall. In
872 addition, set IS_THUMB depending on whether we will return to ARM
873 or Thumb code.
874 See arm-linux.h for stack layout details. */
875static CORE_ADDR
876arm_sigreturn_next_pc (struct regcache *regcache, int svc_number,
877 int *is_thumb)
878{
879 unsigned long sp;
880 unsigned long sp_data;
881 /* Offset of PC register. */
882 int pc_offset = 0;
883 CORE_ADDR next_pc = 0;
884 uint32_t cpsr;
885
886 gdb_assert (svc_number == __NR_sigreturn || svc_number == __NR_rt_sigreturn);
887
888 collect_register_by_name (regcache, "sp", &sp);
889 the_target->read_memory (sp, (unsigned char *) &sp_data, 4);
890
891 pc_offset = arm_linux_sigreturn_next_pc_offset
892 (sp, sp_data, svc_number, __NR_sigreturn == svc_number ? 1 : 0);
893
894 the_target->read_memory (sp + pc_offset, (unsigned char *) &next_pc, 4);
895
896 /* Set IS_THUMB according the CPSR saved on the stack. */
897 the_target->read_memory (sp + pc_offset + 4, (unsigned char *) &cpsr, 4);
898 *is_thumb = ((cpsr & CPSR_T) != 0);
899
900 return next_pc;
901}
902
903/* When PC is at a syscall instruction, return the PC of the next
904 instruction to be executed. */
905static CORE_ADDR
906get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
907{
908 CORE_ADDR next_pc = 0;
909 CORE_ADDR pc = regcache_read_pc (self->regcache);
910 int is_thumb = arm_is_thumb_mode ();
911 ULONGEST svc_number = 0;
912 struct regcache *regcache = self->regcache;
913
914 if (is_thumb)
915 {
916 collect_register (regcache, 7, &svc_number);
917 next_pc = pc + 2;
918 }
919 else
920 {
921 unsigned long this_instr;
922 unsigned long svc_operand;
923
924 target_read_memory (pc, (unsigned char *) &this_instr, 4);
925 svc_operand = (0x00ffffff & this_instr);
926
927 if (svc_operand) /* OABI. */
928 {
929 svc_number = svc_operand - 0x900000;
930 }
931 else /* EABI. */
932 {
933 collect_register (regcache, 7, &svc_number);
934 }
935
936 next_pc = pc + 4;
937 }
938
939 /* This is a sigreturn or sigreturn_rt syscall. */
940 if (svc_number == __NR_sigreturn || svc_number == __NR_rt_sigreturn)
941 {
942 /* SIGRETURN or RT_SIGRETURN may affect the arm thumb mode, so
943 update IS_THUMB. */
944 next_pc = arm_sigreturn_next_pc (regcache, svc_number, &is_thumb);
945 }
946
947 /* Addresses for calling Thumb functions have the bit 0 set. */
948 if (is_thumb)
949 next_pc = MAKE_THUMB_ADDR (next_pc);
950
951 return next_pc;
952}
953
954static const struct target_desc *
955arm_read_description (void)
956{
957 unsigned long arm_hwcap = linux_get_hwcap (4);
958
959 if (arm_hwcap & HWCAP_IWMMXT)
960 return arm_linux_read_description (ARM_FP_TYPE_IWMMXT);
961
962 if (arm_hwcap & HWCAP_VFP)
963 {
964 /* Make sure that the kernel supports reading VFP registers. Support was
965 added in 2.6.30. */
966 int pid = lwpid_of (current_thread);
967 errno = 0;
968 char *buf = (char *) alloca (ARM_VFP3_REGS_SIZE);
969 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0 && errno == EIO)
970 return arm_linux_read_description (ARM_FP_TYPE_NONE);
971
972 /* NEON implies either no VFP, or VFPv3-D32. We only support
973 it with VFP. */
974 if (arm_hwcap & HWCAP_NEON)
975 return aarch32_linux_read_description ();
976 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
977 return arm_linux_read_description (ARM_FP_TYPE_VFPV3);
978 else
979 return arm_linux_read_description (ARM_FP_TYPE_VFPV2);
980 }
981
982 /* The default configuration uses legacy FPA registers, probably
983 simulated. */
984 return arm_linux_read_description (ARM_FP_TYPE_NONE);
985}
986
987void
988arm_target::low_arch_setup ()
989{
990 int tid = lwpid_of (current_thread);
991 int gpregs[18];
992 struct iovec iov;
993
994 /* Query hardware watchpoint/breakpoint capabilities. */
995 arm_linux_init_hwbp_cap (tid);
996
997 current_process ()->tdesc = arm_read_description ();
998
999 iov.iov_base = gpregs;
1000 iov.iov_len = sizeof (gpregs);
1001
1002 /* Check if PTRACE_GETREGSET works. */
1003 if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) == 0)
1004 have_ptrace_getregset = 1;
1005 else
1006 have_ptrace_getregset = 0;
1007}
1008
1009bool
1010arm_target::supports_software_single_step ()
1011{
1012 return true;
1013}
1014
1015/* Fetch the next possible PCs after the current instruction executes. */
1016
1017std::vector<CORE_ADDR>
1018arm_target::low_get_next_pcs (regcache *regcache)
1019{
1020 struct arm_get_next_pcs next_pcs_ctx;
1021
1022 arm_get_next_pcs_ctor (&next_pcs_ctx,
1023 &get_next_pcs_ops,
1024 /* Byte order is ignored assumed as host. */
1025 0,
1026 0,
1027 1,
1028 regcache);
1029
1030 return arm_get_next_pcs (&next_pcs_ctx);
1031}
1032
1033/* Support for hardware single step. */
1034
1035bool
1036arm_target::supports_hardware_single_step ()
1037{
1038 return false;
1039}
1040
1041/* Implementation of linux_target_ops method "get_syscall_trapinfo". */
1042
1043static void
1044arm_get_syscall_trapinfo (struct regcache *regcache, int *sysno)
1045{
1046 if (arm_is_thumb_mode ())
1047 collect_register_by_name (regcache, "r7", sysno);
1048 else
1049 {
1050 unsigned long pc;
1051 unsigned long insn;
1052
1053 collect_register_by_name (regcache, "pc", &pc);
1054
1055 if (the_target->read_memory (pc - 4, (unsigned char *) &insn, 4))
1056 *sysno = UNKNOWN_SYSCALL;
1057 else
1058 {
1059 unsigned long svc_operand = (0x00ffffff & insn);
1060
1061 if (svc_operand)
1062 {
1063 /* OABI */
1064 *sysno = svc_operand - 0x900000;
1065 }
1066 else
1067 {
1068 /* EABI */
1069 collect_register_by_name (regcache, "r7", sysno);
1070 }
1071 }
1072 }
1073}
1074
1075/* Register sets without using PTRACE_GETREGSET. */
1076
1077static struct regset_info arm_regsets[] = {
1078 { PTRACE_GETREGS, PTRACE_SETREGS, 0,
1079 ARM_CORE_REGS_SIZE + ARM_INT_REGISTER_SIZE, GENERAL_REGS,
1080 arm_fill_gregset, arm_store_gregset },
1081 { PTRACE_GETWMMXREGS, PTRACE_SETWMMXREGS, 0, IWMMXT_REGS_SIZE, EXTENDED_REGS,
1082 arm_fill_wmmxregset, arm_store_wmmxregset },
1083 { PTRACE_GETVFPREGS, PTRACE_SETVFPREGS, 0, ARM_VFP3_REGS_SIZE, EXTENDED_REGS,
1084 arm_fill_vfpregset, arm_store_vfpregset },
1085 NULL_REGSET
1086};
1087
1088static struct regsets_info arm_regsets_info =
1089 {
1090 arm_regsets, /* regsets */
1091 0, /* num_regsets */
1092 NULL, /* disabled_regsets */
1093 };
1094
1095static struct usrregs_info arm_usrregs_info =
1096 {
1097 arm_num_regs,
1098 arm_regmap,
1099 };
1100
1101static struct regs_info regs_info_arm =
1102 {
1103 NULL, /* regset_bitmap */
1104 &arm_usrregs_info,
1105 &arm_regsets_info
1106 };
1107
1108const regs_info *
1109arm_target::get_regs_info ()
1110{
1111 const struct target_desc *tdesc = current_process ()->tdesc;
1112
1113 if (have_ptrace_getregset == 1
1114 && (is_aarch32_linux_description (tdesc)
1115 || arm_linux_get_tdesc_fp_type (tdesc) == ARM_FP_TYPE_VFPV3))
1116 return &regs_info_aarch32;
1117
1118 return &regs_info_arm;
1119}
1120
1121struct linux_target_ops the_low_target = {
1122 arm_get_syscall_trapinfo,
1123};
1124
1125/* The linux target ops object. */
1126
1127linux_process_target *the_linux_target = &the_arm_target;
1128
1129void
1130initialize_low_arch (void)
1131{
1132 initialize_low_arch_aarch32 ();
1133 initialize_regsets_info (&arm_regsets_info);
1134}
This page took 0.025641 seconds and 4 git commands to generate.