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