1 /* GNU/Linux/AArch64 specific low level interface, for the remote server for
4 Copyright (C) 2009-2020 Free Software Foundation, Inc.
5 Contributed by ARM Ltd.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "linux-low.h"
24 #include "nat/aarch64-linux.h"
25 #include "nat/aarch64-linux-hw-point.h"
26 #include "arch/aarch64-insn.h"
27 #include "linux-aarch32-low.h"
28 #include "elf/common.h"
30 #include "tracepoint.h"
35 #include "nat/gdb_ptrace.h"
36 #include <asm/ptrace.h>
41 #include "gdb_proc_service.h"
42 #include "arch/aarch64.h"
43 #include "linux-aarch32-tdesc.h"
44 #include "linux-aarch64-tdesc.h"
45 #include "nat/aarch64-sve-linux-ptrace.h"
52 /* Linux target op definitions for the AArch64 architecture. */
54 class aarch64_target
: public linux_process_target
58 const regs_info
*get_regs_info () override
;
60 int breakpoint_kind_from_pc (CORE_ADDR
*pcptr
) override
;
62 int breakpoint_kind_from_current_state (CORE_ADDR
*pcptr
) override
;
64 const gdb_byte
*sw_breakpoint_from_kind (int kind
, int *size
) override
;
66 bool supports_z_point_type (char z_type
) override
;
68 bool supports_tracepoints () override
;
70 bool supports_fast_tracepoints () override
;
72 int install_fast_tracepoint_jump_pad
73 (CORE_ADDR tpoint
, CORE_ADDR tpaddr
, CORE_ADDR collector
,
74 CORE_ADDR lockaddr
, ULONGEST orig_size
, CORE_ADDR
*jump_entry
,
75 CORE_ADDR
*trampoline
, ULONGEST
*trampoline_size
,
76 unsigned char *jjump_pad_insn
, ULONGEST
*jjump_pad_insn_size
,
77 CORE_ADDR
*adjusted_insn_addr
, CORE_ADDR
*adjusted_insn_addr_end
,
80 int get_min_fast_tracepoint_insn_len () override
;
82 struct emit_ops
*emit_ops () override
;
86 void low_arch_setup () override
;
88 bool low_cannot_fetch_register (int regno
) override
;
90 bool low_cannot_store_register (int regno
) override
;
92 bool low_supports_breakpoints () override
;
94 CORE_ADDR
low_get_pc (regcache
*regcache
) override
;
96 void low_set_pc (regcache
*regcache
, CORE_ADDR newpc
) override
;
98 bool low_breakpoint_at (CORE_ADDR pc
) override
;
100 int low_insert_point (raw_bkpt_type type
, CORE_ADDR addr
,
101 int size
, raw_breakpoint
*bp
) override
;
103 int low_remove_point (raw_bkpt_type type
, CORE_ADDR addr
,
104 int size
, raw_breakpoint
*bp
) override
;
106 bool low_stopped_by_watchpoint () override
;
108 CORE_ADDR
low_stopped_data_address () override
;
110 bool low_siginfo_fixup (siginfo_t
*native
, gdb_byte
*inf
,
111 int direction
) override
;
113 arch_process_info
*low_new_process () override
;
115 void low_delete_process (arch_process_info
*info
) override
;
117 void low_new_thread (lwp_info
*) override
;
119 void low_delete_thread (arch_lwp_info
*) override
;
121 void low_new_fork (process_info
*parent
, process_info
*child
) override
;
123 void low_prepare_to_resume (lwp_info
*lwp
) override
;
125 int low_get_thread_area (int lwpid
, CORE_ADDR
*addrp
) override
;
127 bool low_supports_range_stepping () override
;
129 bool low_supports_catch_syscall () override
;
131 void low_get_syscall_trapinfo (regcache
*regcache
, int *sysno
) override
;
134 /* The singleton target ops object. */
136 static aarch64_target the_aarch64_target
;
139 aarch64_target::low_cannot_fetch_register (int regno
)
141 gdb_assert_not_reached ("linux target op low_cannot_fetch_register "
142 "is not implemented by the target");
146 aarch64_target::low_cannot_store_register (int regno
)
148 gdb_assert_not_reached ("linux target op low_cannot_store_register "
149 "is not implemented by the target");
153 aarch64_target::low_prepare_to_resume (lwp_info
*lwp
)
155 aarch64_linux_prepare_to_resume (lwp
);
158 /* Per-process arch-specific data we want to keep. */
160 struct arch_process_info
162 /* Hardware breakpoint/watchpoint data.
163 The reason for them to be per-process rather than per-thread is
164 due to the lack of information in the gdbserver environment;
165 gdbserver is not told that whether a requested hardware
166 breakpoint/watchpoint is thread specific or not, so it has to set
167 each hw bp/wp for every thread in the current process. The
168 higher level bp/wp management in gdb will resume a thread if a hw
169 bp/wp trap is not expected for it. Since the hw bp/wp setting is
170 same for each thread, it is reasonable for the data to live here.
172 struct aarch64_debug_reg_state debug_reg_state
;
175 /* Return true if the size of register 0 is 8 byte. */
178 is_64bit_tdesc (void)
180 struct regcache
*regcache
= get_thread_regcache (current_thread
, 0);
182 return register_size (regcache
->tdesc
, 0) == 8;
185 /* Return true if the regcache contains the number of SVE registers. */
190 struct regcache
*regcache
= get_thread_regcache (current_thread
, 0);
192 return tdesc_contains_feature (regcache
->tdesc
, "org.gnu.gdb.aarch64.sve");
196 aarch64_fill_gregset (struct regcache
*regcache
, void *buf
)
198 struct user_pt_regs
*regset
= (struct user_pt_regs
*) buf
;
201 for (i
= 0; i
< AARCH64_X_REGS_NUM
; i
++)
202 collect_register (regcache
, AARCH64_X0_REGNUM
+ i
, ®set
->regs
[i
]);
203 collect_register (regcache
, AARCH64_SP_REGNUM
, ®set
->sp
);
204 collect_register (regcache
, AARCH64_PC_REGNUM
, ®set
->pc
);
205 collect_register (regcache
, AARCH64_CPSR_REGNUM
, ®set
->pstate
);
209 aarch64_store_gregset (struct regcache
*regcache
, const void *buf
)
211 const struct user_pt_regs
*regset
= (const struct user_pt_regs
*) buf
;
214 for (i
= 0; i
< AARCH64_X_REGS_NUM
; i
++)
215 supply_register (regcache
, AARCH64_X0_REGNUM
+ i
, ®set
->regs
[i
]);
216 supply_register (regcache
, AARCH64_SP_REGNUM
, ®set
->sp
);
217 supply_register (regcache
, AARCH64_PC_REGNUM
, ®set
->pc
);
218 supply_register (regcache
, AARCH64_CPSR_REGNUM
, ®set
->pstate
);
222 aarch64_fill_fpregset (struct regcache
*regcache
, void *buf
)
224 struct user_fpsimd_state
*regset
= (struct user_fpsimd_state
*) buf
;
227 for (i
= 0; i
< AARCH64_V_REGS_NUM
; i
++)
228 collect_register (regcache
, AARCH64_V0_REGNUM
+ i
, ®set
->vregs
[i
]);
229 collect_register (regcache
, AARCH64_FPSR_REGNUM
, ®set
->fpsr
);
230 collect_register (regcache
, AARCH64_FPCR_REGNUM
, ®set
->fpcr
);
234 aarch64_store_fpregset (struct regcache
*regcache
, const void *buf
)
236 const struct user_fpsimd_state
*regset
237 = (const struct user_fpsimd_state
*) buf
;
240 for (i
= 0; i
< AARCH64_V_REGS_NUM
; i
++)
241 supply_register (regcache
, AARCH64_V0_REGNUM
+ i
, ®set
->vregs
[i
]);
242 supply_register (regcache
, AARCH64_FPSR_REGNUM
, ®set
->fpsr
);
243 supply_register (regcache
, AARCH64_FPCR_REGNUM
, ®set
->fpcr
);
246 /* Store the pauth registers to regcache. */
249 aarch64_store_pauthregset (struct regcache
*regcache
, const void *buf
)
251 uint64_t *pauth_regset
= (uint64_t *) buf
;
252 int pauth_base
= find_regno (regcache
->tdesc
, "pauth_dmask");
257 supply_register (regcache
, AARCH64_PAUTH_DMASK_REGNUM (pauth_base
),
259 supply_register (regcache
, AARCH64_PAUTH_CMASK_REGNUM (pauth_base
),
264 aarch64_target::low_supports_breakpoints ()
269 /* Implementation of linux target ops method "low_get_pc". */
272 aarch64_target::low_get_pc (regcache
*regcache
)
274 if (register_size (regcache
->tdesc
, 0) == 8)
275 return linux_get_pc_64bit (regcache
);
277 return linux_get_pc_32bit (regcache
);
280 /* Implementation of linux target ops method "low_set_pc". */
283 aarch64_target::low_set_pc (regcache
*regcache
, CORE_ADDR pc
)
285 if (register_size (regcache
->tdesc
, 0) == 8)
286 linux_set_pc_64bit (regcache
, pc
);
288 linux_set_pc_32bit (regcache
, pc
);
291 #define aarch64_breakpoint_len 4
293 /* AArch64 BRK software debug mode instruction.
294 This instruction needs to match gdb/aarch64-tdep.c
295 (aarch64_default_breakpoint). */
296 static const gdb_byte aarch64_breakpoint
[] = {0x00, 0x00, 0x20, 0xd4};
298 /* Implementation of linux target ops method "low_breakpoint_at". */
301 aarch64_target::low_breakpoint_at (CORE_ADDR where
)
303 if (is_64bit_tdesc ())
305 gdb_byte insn
[aarch64_breakpoint_len
];
307 read_memory (where
, (unsigned char *) &insn
, aarch64_breakpoint_len
);
308 if (memcmp (insn
, aarch64_breakpoint
, aarch64_breakpoint_len
) == 0)
314 return arm_breakpoint_at (where
);
318 aarch64_init_debug_reg_state (struct aarch64_debug_reg_state
*state
)
322 for (i
= 0; i
< AARCH64_HBP_MAX_NUM
; ++i
)
324 state
->dr_addr_bp
[i
] = 0;
325 state
->dr_ctrl_bp
[i
] = 0;
326 state
->dr_ref_count_bp
[i
] = 0;
329 for (i
= 0; i
< AARCH64_HWP_MAX_NUM
; ++i
)
331 state
->dr_addr_wp
[i
] = 0;
332 state
->dr_ctrl_wp
[i
] = 0;
333 state
->dr_ref_count_wp
[i
] = 0;
337 /* Return the pointer to the debug register state structure in the
338 current process' arch-specific data area. */
340 struct aarch64_debug_reg_state
*
341 aarch64_get_debug_reg_state (pid_t pid
)
343 struct process_info
*proc
= find_process_pid (pid
);
345 return &proc
->priv
->arch_private
->debug_reg_state
;
348 /* Implementation of target ops method "supports_z_point_type". */
351 aarch64_target::supports_z_point_type (char z_type
)
357 case Z_PACKET_WRITE_WP
:
358 case Z_PACKET_READ_WP
:
359 case Z_PACKET_ACCESS_WP
:
366 /* Implementation of linux target ops method "low_insert_point".
368 It actually only records the info of the to-be-inserted bp/wp;
369 the actual insertion will happen when threads are resumed. */
372 aarch64_target::low_insert_point (raw_bkpt_type type
, CORE_ADDR addr
,
373 int len
, raw_breakpoint
*bp
)
376 enum target_hw_bp_type targ_type
;
377 struct aarch64_debug_reg_state
*state
378 = aarch64_get_debug_reg_state (pid_of (current_thread
));
381 fprintf (stderr
, "insert_point on entry (addr=0x%08lx, len=%d)\n",
382 (unsigned long) addr
, len
);
384 /* Determine the type from the raw breakpoint type. */
385 targ_type
= raw_bkpt_type_to_target_hw_bp_type (type
);
387 if (targ_type
!= hw_execute
)
389 if (aarch64_linux_region_ok_for_watchpoint (addr
, len
))
390 ret
= aarch64_handle_watchpoint (targ_type
, addr
, len
,
391 1 /* is_insert */, state
);
399 /* LEN is 3 means the breakpoint is set on a 32-bit thumb
400 instruction. Set it to 2 to correctly encode length bit
401 mask in hardware/watchpoint control register. */
404 ret
= aarch64_handle_breakpoint (targ_type
, addr
, len
,
405 1 /* is_insert */, state
);
409 aarch64_show_debug_reg_state (state
, "insert_point", addr
, len
,
415 /* Implementation of linux target ops method "low_remove_point".
417 It actually only records the info of the to-be-removed bp/wp,
418 the actual removal will be done when threads are resumed. */
421 aarch64_target::low_remove_point (raw_bkpt_type type
, CORE_ADDR addr
,
422 int len
, raw_breakpoint
*bp
)
425 enum target_hw_bp_type targ_type
;
426 struct aarch64_debug_reg_state
*state
427 = aarch64_get_debug_reg_state (pid_of (current_thread
));
430 fprintf (stderr
, "remove_point on entry (addr=0x%08lx, len=%d)\n",
431 (unsigned long) addr
, len
);
433 /* Determine the type from the raw breakpoint type. */
434 targ_type
= raw_bkpt_type_to_target_hw_bp_type (type
);
436 /* Set up state pointers. */
437 if (targ_type
!= hw_execute
)
439 aarch64_handle_watchpoint (targ_type
, addr
, len
, 0 /* is_insert */,
445 /* LEN is 3 means the breakpoint is set on a 32-bit thumb
446 instruction. Set it to 2 to correctly encode length bit
447 mask in hardware/watchpoint control register. */
450 ret
= aarch64_handle_breakpoint (targ_type
, addr
, len
,
451 0 /* is_insert */, state
);
455 aarch64_show_debug_reg_state (state
, "remove_point", addr
, len
,
461 /* Implementation of linux target ops method "low_stopped_data_address". */
464 aarch64_target::low_stopped_data_address ()
468 struct aarch64_debug_reg_state
*state
;
470 pid
= lwpid_of (current_thread
);
472 /* Get the siginfo. */
473 if (ptrace (PTRACE_GETSIGINFO
, pid
, NULL
, &siginfo
) != 0)
474 return (CORE_ADDR
) 0;
476 /* Need to be a hardware breakpoint/watchpoint trap. */
477 if (siginfo
.si_signo
!= SIGTRAP
478 || (siginfo
.si_code
& 0xffff) != 0x0004 /* TRAP_HWBKPT */)
479 return (CORE_ADDR
) 0;
481 /* Check if the address matches any watched address. */
482 state
= aarch64_get_debug_reg_state (pid_of (current_thread
));
483 for (i
= aarch64_num_wp_regs
- 1; i
>= 0; --i
)
485 const unsigned int offset
486 = aarch64_watchpoint_offset (state
->dr_ctrl_wp
[i
]);
487 const unsigned int len
= aarch64_watchpoint_length (state
->dr_ctrl_wp
[i
]);
488 const CORE_ADDR addr_trap
= (CORE_ADDR
) siginfo
.si_addr
;
489 const CORE_ADDR addr_watch
= state
->dr_addr_wp
[i
] + offset
;
490 const CORE_ADDR addr_watch_aligned
= align_down (state
->dr_addr_wp
[i
], 8);
491 const CORE_ADDR addr_orig
= state
->dr_addr_orig_wp
[i
];
493 if (state
->dr_ref_count_wp
[i
]
494 && DR_CONTROL_ENABLED (state
->dr_ctrl_wp
[i
])
495 && addr_trap
>= addr_watch_aligned
496 && addr_trap
< addr_watch
+ len
)
498 /* ADDR_TRAP reports the first address of the memory range
499 accessed by the CPU, regardless of what was the memory
500 range watched. Thus, a large CPU access that straddles
501 the ADDR_WATCH..ADDR_WATCH+LEN range may result in an
502 ADDR_TRAP that is lower than the
503 ADDR_WATCH..ADDR_WATCH+LEN range. E.g.:
505 addr: | 4 | 5 | 6 | 7 | 8 |
506 |---- range watched ----|
507 |----------- range accessed ------------|
509 In this case, ADDR_TRAP will be 4.
511 To match a watchpoint known to GDB core, we must never
512 report *ADDR_P outside of any ADDR_WATCH..ADDR_WATCH+LEN
513 range. ADDR_WATCH <= ADDR_TRAP < ADDR_ORIG is a false
514 positive on kernels older than 4.10. See PR
520 return (CORE_ADDR
) 0;
523 /* Implementation of linux target ops method "low_stopped_by_watchpoint". */
526 aarch64_target::low_stopped_by_watchpoint ()
528 return (low_stopped_data_address () != 0);
531 /* Fetch the thread-local storage pointer for libthread_db. */
534 ps_get_thread_area (struct ps_prochandle
*ph
,
535 lwpid_t lwpid
, int idx
, void **base
)
537 return aarch64_ps_get_thread_area (ph
, lwpid
, idx
, base
,
541 /* Implementation of linux target ops method "low_siginfo_fixup". */
544 aarch64_target::low_siginfo_fixup (siginfo_t
*native
, gdb_byte
*inf
,
547 /* Is the inferior 32-bit? If so, then fixup the siginfo object. */
548 if (!is_64bit_tdesc ())
551 aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo
*) inf
,
554 aarch64_siginfo_from_compat_siginfo (native
,
555 (struct compat_siginfo
*) inf
);
563 /* Implementation of linux target ops method "low_new_process". */
566 aarch64_target::low_new_process ()
568 struct arch_process_info
*info
= XCNEW (struct arch_process_info
);
570 aarch64_init_debug_reg_state (&info
->debug_reg_state
);
575 /* Implementation of linux target ops method "low_delete_process". */
578 aarch64_target::low_delete_process (arch_process_info
*info
)
584 aarch64_target::low_new_thread (lwp_info
*lwp
)
586 aarch64_linux_new_thread (lwp
);
590 aarch64_target::low_delete_thread (arch_lwp_info
*arch_lwp
)
592 aarch64_linux_delete_thread (arch_lwp
);
595 /* Implementation of linux target ops method "low_new_fork". */
598 aarch64_target::low_new_fork (process_info
*parent
,
601 /* These are allocated by linux_add_process. */
602 gdb_assert (parent
->priv
!= NULL
603 && parent
->priv
->arch_private
!= NULL
);
604 gdb_assert (child
->priv
!= NULL
605 && child
->priv
->arch_private
!= NULL
);
607 /* Linux kernel before 2.6.33 commit
608 72f674d203cd230426437cdcf7dd6f681dad8b0d
609 will inherit hardware debug registers from parent
610 on fork/vfork/clone. Newer Linux kernels create such tasks with
611 zeroed debug registers.
613 GDB core assumes the child inherits the watchpoints/hw
614 breakpoints of the parent, and will remove them all from the
615 forked off process. Copy the debug registers mirrors into the
616 new process so that all breakpoints and watchpoints can be
617 removed together. The debug registers mirror will become zeroed
618 in the end before detaching the forked off process, thus making
619 this compatible with older Linux kernels too. */
621 *child
->priv
->arch_private
= *parent
->priv
->arch_private
;
624 /* Matches HWCAP_PACA in kernel header arch/arm64/include/uapi/asm/hwcap.h. */
625 #define AARCH64_HWCAP_PACA (1 << 30)
627 /* Implementation of linux target ops method "low_arch_setup". */
630 aarch64_target::low_arch_setup ()
632 unsigned int machine
;
636 tid
= lwpid_of (current_thread
);
638 is_elf64
= linux_pid_exe_is_elf_64_file (tid
, &machine
);
642 uint64_t vq
= aarch64_sve_get_vq (tid
);
643 unsigned long hwcap
= linux_get_hwcap (8);
644 bool pauth_p
= hwcap
& AARCH64_HWCAP_PACA
;
646 current_process ()->tdesc
= aarch64_linux_read_description (vq
, pauth_p
);
649 current_process ()->tdesc
= aarch32_linux_read_description ();
651 aarch64_linux_get_debug_reg_capacity (lwpid_of (current_thread
));
654 /* Wrapper for aarch64_sve_regs_copy_to_reg_buf. */
657 aarch64_sve_regs_copy_to_regcache (struct regcache
*regcache
, const void *buf
)
659 return aarch64_sve_regs_copy_to_reg_buf (regcache
, buf
);
662 /* Wrapper for aarch64_sve_regs_copy_from_reg_buf. */
665 aarch64_sve_regs_copy_from_regcache (struct regcache
*regcache
, void *buf
)
667 return aarch64_sve_regs_copy_from_reg_buf (regcache
, buf
);
670 static struct regset_info aarch64_regsets
[] =
672 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_PRSTATUS
,
673 sizeof (struct user_pt_regs
), GENERAL_REGS
,
674 aarch64_fill_gregset
, aarch64_store_gregset
},
675 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_FPREGSET
,
676 sizeof (struct user_fpsimd_state
), FP_REGS
,
677 aarch64_fill_fpregset
, aarch64_store_fpregset
679 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_ARM_PAC_MASK
,
680 AARCH64_PAUTH_REGS_SIZE
, OPTIONAL_REGS
,
681 NULL
, aarch64_store_pauthregset
},
685 static struct regsets_info aarch64_regsets_info
=
687 aarch64_regsets
, /* regsets */
689 NULL
, /* disabled_regsets */
692 static struct regs_info regs_info_aarch64
=
694 NULL
, /* regset_bitmap */
696 &aarch64_regsets_info
,
699 static struct regset_info aarch64_sve_regsets
[] =
701 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_PRSTATUS
,
702 sizeof (struct user_pt_regs
), GENERAL_REGS
,
703 aarch64_fill_gregset
, aarch64_store_gregset
},
704 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_ARM_SVE
,
705 SVE_PT_SIZE (AARCH64_MAX_SVE_VQ
, SVE_PT_REGS_SVE
), EXTENDED_REGS
,
706 aarch64_sve_regs_copy_from_regcache
, aarch64_sve_regs_copy_to_regcache
708 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_ARM_PAC_MASK
,
709 AARCH64_PAUTH_REGS_SIZE
, OPTIONAL_REGS
,
710 NULL
, aarch64_store_pauthregset
},
714 static struct regsets_info aarch64_sve_regsets_info
=
716 aarch64_sve_regsets
, /* regsets. */
717 0, /* num_regsets. */
718 NULL
, /* disabled_regsets. */
721 static struct regs_info regs_info_aarch64_sve
=
723 NULL
, /* regset_bitmap. */
725 &aarch64_sve_regsets_info
,
728 /* Implementation of linux target ops method "get_regs_info". */
731 aarch64_target::get_regs_info ()
733 if (!is_64bit_tdesc ())
734 return ®s_info_aarch32
;
737 return ®s_info_aarch64_sve
;
739 return ®s_info_aarch64
;
742 /* Implementation of target ops method "supports_tracepoints". */
745 aarch64_target::supports_tracepoints ()
747 if (current_thread
== NULL
)
751 /* We don't support tracepoints on aarch32 now. */
752 return is_64bit_tdesc ();
756 /* Implementation of linux target ops method "low_get_thread_area". */
759 aarch64_target::low_get_thread_area (int lwpid
, CORE_ADDR
*addrp
)
764 iovec
.iov_base
= ®
;
765 iovec
.iov_len
= sizeof (reg
);
767 if (ptrace (PTRACE_GETREGSET
, lwpid
, NT_ARM_TLS
, &iovec
) != 0)
776 aarch64_target::low_supports_catch_syscall ()
781 /* Implementation of linux target ops method "low_get_syscall_trapinfo". */
784 aarch64_target::low_get_syscall_trapinfo (regcache
*regcache
, int *sysno
)
786 int use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
792 collect_register_by_name (regcache
, "x8", &l_sysno
);
793 *sysno
= (int) l_sysno
;
796 collect_register_by_name (regcache
, "r7", sysno
);
799 /* List of condition codes that we need. */
801 enum aarch64_condition_codes
812 enum aarch64_operand_type
818 /* Representation of an operand. At this time, it only supports register
819 and immediate types. */
821 struct aarch64_operand
823 /* Type of the operand. */
824 enum aarch64_operand_type type
;
826 /* Value of the operand according to the type. */
830 struct aarch64_register reg
;
834 /* List of registers that we are currently using, we can add more here as
835 we need to use them. */
837 /* General purpose scratch registers (64 bit). */
838 static const struct aarch64_register x0
= { 0, 1 };
839 static const struct aarch64_register x1
= { 1, 1 };
840 static const struct aarch64_register x2
= { 2, 1 };
841 static const struct aarch64_register x3
= { 3, 1 };
842 static const struct aarch64_register x4
= { 4, 1 };
844 /* General purpose scratch registers (32 bit). */
845 static const struct aarch64_register w0
= { 0, 0 };
846 static const struct aarch64_register w2
= { 2, 0 };
848 /* Intra-procedure scratch registers. */
849 static const struct aarch64_register ip0
= { 16, 1 };
851 /* Special purpose registers. */
852 static const struct aarch64_register fp
= { 29, 1 };
853 static const struct aarch64_register lr
= { 30, 1 };
854 static const struct aarch64_register sp
= { 31, 1 };
855 static const struct aarch64_register xzr
= { 31, 1 };
857 /* Dynamically allocate a new register. If we know the register
858 statically, we should make it a global as above instead of using this
861 static struct aarch64_register
862 aarch64_register (unsigned num
, int is64
)
864 return (struct aarch64_register
) { num
, is64
};
867 /* Helper function to create a register operand, for instructions with
868 different types of operands.
871 p += emit_mov (p, x0, register_operand (x1)); */
873 static struct aarch64_operand
874 register_operand (struct aarch64_register reg
)
876 struct aarch64_operand operand
;
878 operand
.type
= OPERAND_REGISTER
;
884 /* Helper function to create an immediate operand, for instructions with
885 different types of operands.
888 p += emit_mov (p, x0, immediate_operand (12)); */
890 static struct aarch64_operand
891 immediate_operand (uint32_t imm
)
893 struct aarch64_operand operand
;
895 operand
.type
= OPERAND_IMMEDIATE
;
901 /* Helper function to create an offset memory operand.
904 p += emit_ldr (p, x0, sp, offset_memory_operand (16)); */
906 static struct aarch64_memory_operand
907 offset_memory_operand (int32_t offset
)
909 return (struct aarch64_memory_operand
) { MEMORY_OPERAND_OFFSET
, offset
};
912 /* Helper function to create a pre-index memory operand.
915 p += emit_ldr (p, x0, sp, preindex_memory_operand (16)); */
917 static struct aarch64_memory_operand
918 preindex_memory_operand (int32_t index
)
920 return (struct aarch64_memory_operand
) { MEMORY_OPERAND_PREINDEX
, index
};
923 /* Helper function to create a post-index memory operand.
926 p += emit_ldr (p, x0, sp, postindex_memory_operand (16)); */
928 static struct aarch64_memory_operand
929 postindex_memory_operand (int32_t index
)
931 return (struct aarch64_memory_operand
) { MEMORY_OPERAND_POSTINDEX
, index
};
934 /* System control registers. These special registers can be written and
935 read with the MRS and MSR instructions.
937 - NZCV: Condition flags. GDB refers to this register under the CPSR
939 - FPSR: Floating-point status register.
940 - FPCR: Floating-point control registers.
941 - TPIDR_EL0: Software thread ID register. */
943 enum aarch64_system_control_registers
945 /* op0 op1 crn crm op2 */
946 NZCV
= (0x1 << 14) | (0x3 << 11) | (0x4 << 7) | (0x2 << 3) | 0x0,
947 FPSR
= (0x1 << 14) | (0x3 << 11) | (0x4 << 7) | (0x4 << 3) | 0x1,
948 FPCR
= (0x1 << 14) | (0x3 << 11) | (0x4 << 7) | (0x4 << 3) | 0x0,
949 TPIDR_EL0
= (0x1 << 14) | (0x3 << 11) | (0xd << 7) | (0x0 << 3) | 0x2
952 /* Write a BLR instruction into *BUF.
956 RN is the register to branch to. */
959 emit_blr (uint32_t *buf
, struct aarch64_register rn
)
961 return aarch64_emit_insn (buf
, BLR
| ENCODE (rn
.num
, 5, 5));
964 /* Write a RET instruction into *BUF.
968 RN is the register to branch to. */
971 emit_ret (uint32_t *buf
, struct aarch64_register rn
)
973 return aarch64_emit_insn (buf
, RET
| ENCODE (rn
.num
, 5, 5));
977 emit_load_store_pair (uint32_t *buf
, enum aarch64_opcodes opcode
,
978 struct aarch64_register rt
,
979 struct aarch64_register rt2
,
980 struct aarch64_register rn
,
981 struct aarch64_memory_operand operand
)
988 opc
= ENCODE (2, 2, 30);
990 opc
= ENCODE (0, 2, 30);
992 switch (operand
.type
)
994 case MEMORY_OPERAND_OFFSET
:
996 pre_index
= ENCODE (1, 1, 24);
997 write_back
= ENCODE (0, 1, 23);
1000 case MEMORY_OPERAND_POSTINDEX
:
1002 pre_index
= ENCODE (0, 1, 24);
1003 write_back
= ENCODE (1, 1, 23);
1006 case MEMORY_OPERAND_PREINDEX
:
1008 pre_index
= ENCODE (1, 1, 24);
1009 write_back
= ENCODE (1, 1, 23);
1016 return aarch64_emit_insn (buf
, opcode
| opc
| pre_index
| write_back
1017 | ENCODE (operand
.index
>> 3, 7, 15)
1018 | ENCODE (rt2
.num
, 5, 10)
1019 | ENCODE (rn
.num
, 5, 5) | ENCODE (rt
.num
, 5, 0));
1022 /* Write a STP instruction into *BUF.
1024 STP rt, rt2, [rn, #offset]
1025 STP rt, rt2, [rn, #index]!
1026 STP rt, rt2, [rn], #index
1028 RT and RT2 are the registers to store.
1029 RN is the base address register.
1030 OFFSET is the immediate to add to the base address. It is limited to a
1031 -512 .. 504 range (7 bits << 3). */
1034 emit_stp (uint32_t *buf
, struct aarch64_register rt
,
1035 struct aarch64_register rt2
, struct aarch64_register rn
,
1036 struct aarch64_memory_operand operand
)
1038 return emit_load_store_pair (buf
, STP
, rt
, rt2
, rn
, operand
);
1041 /* Write a LDP instruction into *BUF.
1043 LDP rt, rt2, [rn, #offset]
1044 LDP rt, rt2, [rn, #index]!
1045 LDP rt, rt2, [rn], #index
1047 RT and RT2 are the registers to store.
1048 RN is the base address register.
1049 OFFSET is the immediate to add to the base address. It is limited to a
1050 -512 .. 504 range (7 bits << 3). */
1053 emit_ldp (uint32_t *buf
, struct aarch64_register rt
,
1054 struct aarch64_register rt2
, struct aarch64_register rn
,
1055 struct aarch64_memory_operand operand
)
1057 return emit_load_store_pair (buf
, LDP
, rt
, rt2
, rn
, operand
);
1060 /* Write a LDP (SIMD&VFP) instruction using Q registers into *BUF.
1062 LDP qt, qt2, [rn, #offset]
1064 RT and RT2 are the Q registers to store.
1065 RN is the base address register.
1066 OFFSET is the immediate to add to the base address. It is limited to
1067 -1024 .. 1008 range (7 bits << 4). */
1070 emit_ldp_q_offset (uint32_t *buf
, unsigned rt
, unsigned rt2
,
1071 struct aarch64_register rn
, int32_t offset
)
1073 uint32_t opc
= ENCODE (2, 2, 30);
1074 uint32_t pre_index
= ENCODE (1, 1, 24);
1076 return aarch64_emit_insn (buf
, LDP_SIMD_VFP
| opc
| pre_index
1077 | ENCODE (offset
>> 4, 7, 15)
1078 | ENCODE (rt2
, 5, 10)
1079 | ENCODE (rn
.num
, 5, 5) | ENCODE (rt
, 5, 0));
1082 /* Write a STP (SIMD&VFP) instruction using Q registers into *BUF.
1084 STP qt, qt2, [rn, #offset]
1086 RT and RT2 are the Q registers to store.
1087 RN is the base address register.
1088 OFFSET is the immediate to add to the base address. It is limited to
1089 -1024 .. 1008 range (7 bits << 4). */
1092 emit_stp_q_offset (uint32_t *buf
, unsigned rt
, unsigned rt2
,
1093 struct aarch64_register rn
, int32_t offset
)
1095 uint32_t opc
= ENCODE (2, 2, 30);
1096 uint32_t pre_index
= ENCODE (1, 1, 24);
1098 return aarch64_emit_insn (buf
, STP_SIMD_VFP
| opc
| pre_index
1099 | ENCODE (offset
>> 4, 7, 15)
1100 | ENCODE (rt2
, 5, 10)
1101 | ENCODE (rn
.num
, 5, 5) | ENCODE (rt
, 5, 0));
1104 /* Write a LDRH instruction into *BUF.
1106 LDRH wt, [xn, #offset]
1107 LDRH wt, [xn, #index]!
1108 LDRH wt, [xn], #index
1110 RT is the register to store.
1111 RN is the base address register.
1112 OFFSET is the immediate to add to the base address. It is limited to
1113 0 .. 32760 range (12 bits << 3). */
1116 emit_ldrh (uint32_t *buf
, struct aarch64_register rt
,
1117 struct aarch64_register rn
,
1118 struct aarch64_memory_operand operand
)
1120 return aarch64_emit_load_store (buf
, 1, LDR
, rt
, rn
, operand
);
1123 /* Write a LDRB instruction into *BUF.
1125 LDRB wt, [xn, #offset]
1126 LDRB wt, [xn, #index]!
1127 LDRB wt, [xn], #index
1129 RT is the register to store.
1130 RN is the base address register.
1131 OFFSET is the immediate to add to the base address. It is limited to
1132 0 .. 32760 range (12 bits << 3). */
1135 emit_ldrb (uint32_t *buf
, struct aarch64_register rt
,
1136 struct aarch64_register rn
,
1137 struct aarch64_memory_operand operand
)
1139 return aarch64_emit_load_store (buf
, 0, LDR
, rt
, rn
, operand
);
1144 /* Write a STR instruction into *BUF.
1146 STR rt, [rn, #offset]
1147 STR rt, [rn, #index]!
1148 STR rt, [rn], #index
1150 RT is the register to store.
1151 RN is the base address register.
1152 OFFSET is the immediate to add to the base address. It is limited to
1153 0 .. 32760 range (12 bits << 3). */
1156 emit_str (uint32_t *buf
, struct aarch64_register rt
,
1157 struct aarch64_register rn
,
1158 struct aarch64_memory_operand operand
)
1160 return aarch64_emit_load_store (buf
, rt
.is64
? 3 : 2, STR
, rt
, rn
, operand
);
1163 /* Helper function emitting an exclusive load or store instruction. */
1166 emit_load_store_exclusive (uint32_t *buf
, uint32_t size
,
1167 enum aarch64_opcodes opcode
,
1168 struct aarch64_register rs
,
1169 struct aarch64_register rt
,
1170 struct aarch64_register rt2
,
1171 struct aarch64_register rn
)
1173 return aarch64_emit_insn (buf
, opcode
| ENCODE (size
, 2, 30)
1174 | ENCODE (rs
.num
, 5, 16) | ENCODE (rt2
.num
, 5, 10)
1175 | ENCODE (rn
.num
, 5, 5) | ENCODE (rt
.num
, 5, 0));
1178 /* Write a LAXR instruction into *BUF.
1182 RT is the destination register.
1183 RN is the base address register. */
1186 emit_ldaxr (uint32_t *buf
, struct aarch64_register rt
,
1187 struct aarch64_register rn
)
1189 return emit_load_store_exclusive (buf
, rt
.is64
? 3 : 2, LDAXR
, xzr
, rt
,
1193 /* Write a STXR instruction into *BUF.
1197 RS is the result register, it indicates if the store succeeded or not.
1198 RT is the destination register.
1199 RN is the base address register. */
1202 emit_stxr (uint32_t *buf
, struct aarch64_register rs
,
1203 struct aarch64_register rt
, struct aarch64_register rn
)
1205 return emit_load_store_exclusive (buf
, rt
.is64
? 3 : 2, STXR
, rs
, rt
,
1209 /* Write a STLR instruction into *BUF.
1213 RT is the register to store.
1214 RN is the base address register. */
1217 emit_stlr (uint32_t *buf
, struct aarch64_register rt
,
1218 struct aarch64_register rn
)
1220 return emit_load_store_exclusive (buf
, rt
.is64
? 3 : 2, STLR
, xzr
, rt
,
1224 /* Helper function for data processing instructions with register sources. */
1227 emit_data_processing_reg (uint32_t *buf
, uint32_t opcode
,
1228 struct aarch64_register rd
,
1229 struct aarch64_register rn
,
1230 struct aarch64_register rm
)
1232 uint32_t size
= ENCODE (rd
.is64
, 1, 31);
1234 return aarch64_emit_insn (buf
, opcode
| size
| ENCODE (rm
.num
, 5, 16)
1235 | ENCODE (rn
.num
, 5, 5) | ENCODE (rd
.num
, 5, 0));
1238 /* Helper function for data processing instructions taking either a register
1242 emit_data_processing (uint32_t *buf
, enum aarch64_opcodes opcode
,
1243 struct aarch64_register rd
,
1244 struct aarch64_register rn
,
1245 struct aarch64_operand operand
)
1247 uint32_t size
= ENCODE (rd
.is64
, 1, 31);
1248 /* The opcode is different for register and immediate source operands. */
1249 uint32_t operand_opcode
;
1251 if (operand
.type
== OPERAND_IMMEDIATE
)
1253 /* xxx1 000x xxxx xxxx xxxx xxxx xxxx xxxx */
1254 operand_opcode
= ENCODE (8, 4, 25);
1256 return aarch64_emit_insn (buf
, opcode
| operand_opcode
| size
1257 | ENCODE (operand
.imm
, 12, 10)
1258 | ENCODE (rn
.num
, 5, 5)
1259 | ENCODE (rd
.num
, 5, 0));
1263 /* xxx0 101x xxxx xxxx xxxx xxxx xxxx xxxx */
1264 operand_opcode
= ENCODE (5, 4, 25);
1266 return emit_data_processing_reg (buf
, opcode
| operand_opcode
, rd
,
1271 /* Write an ADD instruction into *BUF.
1276 This function handles both an immediate and register add.
1278 RD is the destination register.
1279 RN is the input register.
1280 OPERAND is the source operand, either of type OPERAND_IMMEDIATE or
1281 OPERAND_REGISTER. */
1284 emit_add (uint32_t *buf
, struct aarch64_register rd
,
1285 struct aarch64_register rn
, struct aarch64_operand operand
)
1287 return emit_data_processing (buf
, ADD
, rd
, rn
, operand
);
1290 /* Write a SUB instruction into *BUF.
1295 This function handles both an immediate and register sub.
1297 RD is the destination register.
1298 RN is the input register.
1299 IMM is the immediate to substract to RN. */
1302 emit_sub (uint32_t *buf
, struct aarch64_register rd
,
1303 struct aarch64_register rn
, struct aarch64_operand operand
)
1305 return emit_data_processing (buf
, SUB
, rd
, rn
, operand
);
1308 /* Write a MOV instruction into *BUF.
1313 This function handles both a wide immediate move and a register move,
1314 with the condition that the source register is not xzr. xzr and the
1315 stack pointer share the same encoding and this function only supports
1318 RD is the destination register.
1319 OPERAND is the source operand, either of type OPERAND_IMMEDIATE or
1320 OPERAND_REGISTER. */
1323 emit_mov (uint32_t *buf
, struct aarch64_register rd
,
1324 struct aarch64_operand operand
)
1326 if (operand
.type
== OPERAND_IMMEDIATE
)
1328 uint32_t size
= ENCODE (rd
.is64
, 1, 31);
1329 /* Do not shift the immediate. */
1330 uint32_t shift
= ENCODE (0, 2, 21);
1332 return aarch64_emit_insn (buf
, MOV
| size
| shift
1333 | ENCODE (operand
.imm
, 16, 5)
1334 | ENCODE (rd
.num
, 5, 0));
1337 return emit_add (buf
, rd
, operand
.reg
, immediate_operand (0));
1340 /* Write a MOVK instruction into *BUF.
1342 MOVK rd, #imm, lsl #shift
1344 RD is the destination register.
1345 IMM is the immediate.
1346 SHIFT is the logical shift left to apply to IMM. */
1349 emit_movk (uint32_t *buf
, struct aarch64_register rd
, uint32_t imm
,
1352 uint32_t size
= ENCODE (rd
.is64
, 1, 31);
1354 return aarch64_emit_insn (buf
, MOVK
| size
| ENCODE (shift
, 2, 21) |
1355 ENCODE (imm
, 16, 5) | ENCODE (rd
.num
, 5, 0));
1358 /* Write instructions into *BUF in order to move ADDR into a register.
1359 ADDR can be a 64-bit value.
1361 This function will emit a series of MOV and MOVK instructions, such as:
1364 MOVK xd, #(addr >> 16), lsl #16
1365 MOVK xd, #(addr >> 32), lsl #32
1366 MOVK xd, #(addr >> 48), lsl #48 */
1369 emit_mov_addr (uint32_t *buf
, struct aarch64_register rd
, CORE_ADDR addr
)
1373 /* The MOV (wide immediate) instruction clears to top bits of the
1375 p
+= emit_mov (p
, rd
, immediate_operand (addr
& 0xffff));
1377 if ((addr
>> 16) != 0)
1378 p
+= emit_movk (p
, rd
, (addr
>> 16) & 0xffff, 1);
1382 if ((addr
>> 32) != 0)
1383 p
+= emit_movk (p
, rd
, (addr
>> 32) & 0xffff, 2);
1387 if ((addr
>> 48) != 0)
1388 p
+= emit_movk (p
, rd
, (addr
>> 48) & 0xffff, 3);
1393 /* Write a SUBS instruction into *BUF.
1397 This instruction update the condition flags.
1399 RD is the destination register.
1400 RN and RM are the source registers. */
1403 emit_subs (uint32_t *buf
, struct aarch64_register rd
,
1404 struct aarch64_register rn
, struct aarch64_operand operand
)
1406 return emit_data_processing (buf
, SUBS
, rd
, rn
, operand
);
1409 /* Write a CMP instruction into *BUF.
1413 This instruction is an alias of SUBS xzr, rn, rm.
1415 RN and RM are the registers to compare. */
1418 emit_cmp (uint32_t *buf
, struct aarch64_register rn
,
1419 struct aarch64_operand operand
)
1421 return emit_subs (buf
, xzr
, rn
, operand
);
1424 /* Write a AND instruction into *BUF.
1428 RD is the destination register.
1429 RN and RM are the source registers. */
1432 emit_and (uint32_t *buf
, struct aarch64_register rd
,
1433 struct aarch64_register rn
, struct aarch64_register rm
)
1435 return emit_data_processing_reg (buf
, AND
, rd
, rn
, rm
);
1438 /* Write a ORR instruction into *BUF.
1442 RD is the destination register.
1443 RN and RM are the source registers. */
1446 emit_orr (uint32_t *buf
, struct aarch64_register rd
,
1447 struct aarch64_register rn
, struct aarch64_register rm
)
1449 return emit_data_processing_reg (buf
, ORR
, rd
, rn
, rm
);
1452 /* Write a ORN instruction into *BUF.
1456 RD is the destination register.
1457 RN and RM are the source registers. */
1460 emit_orn (uint32_t *buf
, struct aarch64_register rd
,
1461 struct aarch64_register rn
, struct aarch64_register rm
)
1463 return emit_data_processing_reg (buf
, ORN
, rd
, rn
, rm
);
1466 /* Write a EOR instruction into *BUF.
1470 RD is the destination register.
1471 RN and RM are the source registers. */
1474 emit_eor (uint32_t *buf
, struct aarch64_register rd
,
1475 struct aarch64_register rn
, struct aarch64_register rm
)
1477 return emit_data_processing_reg (buf
, EOR
, rd
, rn
, rm
);
1480 /* Write a MVN instruction into *BUF.
1484 This is an alias for ORN rd, xzr, rm.
1486 RD is the destination register.
1487 RM is the source register. */
1490 emit_mvn (uint32_t *buf
, struct aarch64_register rd
,
1491 struct aarch64_register rm
)
1493 return emit_orn (buf
, rd
, xzr
, rm
);
1496 /* Write a LSLV instruction into *BUF.
1500 RD is the destination register.
1501 RN and RM are the source registers. */
1504 emit_lslv (uint32_t *buf
, struct aarch64_register rd
,
1505 struct aarch64_register rn
, struct aarch64_register rm
)
1507 return emit_data_processing_reg (buf
, LSLV
, rd
, rn
, rm
);
1510 /* Write a LSRV instruction into *BUF.
1514 RD is the destination register.
1515 RN and RM are the source registers. */
1518 emit_lsrv (uint32_t *buf
, struct aarch64_register rd
,
1519 struct aarch64_register rn
, struct aarch64_register rm
)
1521 return emit_data_processing_reg (buf
, LSRV
, rd
, rn
, rm
);
1524 /* Write a ASRV instruction into *BUF.
1528 RD is the destination register.
1529 RN and RM are the source registers. */
1532 emit_asrv (uint32_t *buf
, struct aarch64_register rd
,
1533 struct aarch64_register rn
, struct aarch64_register rm
)
1535 return emit_data_processing_reg (buf
, ASRV
, rd
, rn
, rm
);
1538 /* Write a MUL instruction into *BUF.
1542 RD is the destination register.
1543 RN and RM are the source registers. */
1546 emit_mul (uint32_t *buf
, struct aarch64_register rd
,
1547 struct aarch64_register rn
, struct aarch64_register rm
)
1549 return emit_data_processing_reg (buf
, MUL
, rd
, rn
, rm
);
1552 /* Write a MRS instruction into *BUF. The register size is 64-bit.
1556 RT is the destination register.
1557 SYSTEM_REG is special purpose register to read. */
1560 emit_mrs (uint32_t *buf
, struct aarch64_register rt
,
1561 enum aarch64_system_control_registers system_reg
)
1563 return aarch64_emit_insn (buf
, MRS
| ENCODE (system_reg
, 15, 5)
1564 | ENCODE (rt
.num
, 5, 0));
1567 /* Write a MSR instruction into *BUF. The register size is 64-bit.
1571 SYSTEM_REG is special purpose register to write.
1572 RT is the input register. */
1575 emit_msr (uint32_t *buf
, enum aarch64_system_control_registers system_reg
,
1576 struct aarch64_register rt
)
1578 return aarch64_emit_insn (buf
, MSR
| ENCODE (system_reg
, 15, 5)
1579 | ENCODE (rt
.num
, 5, 0));
1582 /* Write a SEVL instruction into *BUF.
1584 This is a hint instruction telling the hardware to trigger an event. */
1587 emit_sevl (uint32_t *buf
)
1589 return aarch64_emit_insn (buf
, SEVL
);
1592 /* Write a WFE instruction into *BUF.
1594 This is a hint instruction telling the hardware to wait for an event. */
1597 emit_wfe (uint32_t *buf
)
1599 return aarch64_emit_insn (buf
, WFE
);
1602 /* Write a SBFM instruction into *BUF.
1604 SBFM rd, rn, #immr, #imms
1606 This instruction moves the bits from #immr to #imms into the
1607 destination, sign extending the result.
1609 RD is the destination register.
1610 RN is the source register.
1611 IMMR is the bit number to start at (least significant bit).
1612 IMMS is the bit number to stop at (most significant bit). */
1615 emit_sbfm (uint32_t *buf
, struct aarch64_register rd
,
1616 struct aarch64_register rn
, uint32_t immr
, uint32_t imms
)
1618 uint32_t size
= ENCODE (rd
.is64
, 1, 31);
1619 uint32_t n
= ENCODE (rd
.is64
, 1, 22);
1621 return aarch64_emit_insn (buf
, SBFM
| size
| n
| ENCODE (immr
, 6, 16)
1622 | ENCODE (imms
, 6, 10) | ENCODE (rn
.num
, 5, 5)
1623 | ENCODE (rd
.num
, 5, 0));
1626 /* Write a SBFX instruction into *BUF.
1628 SBFX rd, rn, #lsb, #width
1630 This instruction moves #width bits from #lsb into the destination, sign
1631 extending the result. This is an alias for:
1633 SBFM rd, rn, #lsb, #(lsb + width - 1)
1635 RD is the destination register.
1636 RN is the source register.
1637 LSB is the bit number to start at (least significant bit).
1638 WIDTH is the number of bits to move. */
1641 emit_sbfx (uint32_t *buf
, struct aarch64_register rd
,
1642 struct aarch64_register rn
, uint32_t lsb
, uint32_t width
)
1644 return emit_sbfm (buf
, rd
, rn
, lsb
, lsb
+ width
- 1);
1647 /* Write a UBFM instruction into *BUF.
1649 UBFM rd, rn, #immr, #imms
1651 This instruction moves the bits from #immr to #imms into the
1652 destination, extending the result with zeros.
1654 RD is the destination register.
1655 RN is the source register.
1656 IMMR is the bit number to start at (least significant bit).
1657 IMMS is the bit number to stop at (most significant bit). */
1660 emit_ubfm (uint32_t *buf
, struct aarch64_register rd
,
1661 struct aarch64_register rn
, uint32_t immr
, uint32_t imms
)
1663 uint32_t size
= ENCODE (rd
.is64
, 1, 31);
1664 uint32_t n
= ENCODE (rd
.is64
, 1, 22);
1666 return aarch64_emit_insn (buf
, UBFM
| size
| n
| ENCODE (immr
, 6, 16)
1667 | ENCODE (imms
, 6, 10) | ENCODE (rn
.num
, 5, 5)
1668 | ENCODE (rd
.num
, 5, 0));
1671 /* Write a UBFX instruction into *BUF.
1673 UBFX rd, rn, #lsb, #width
1675 This instruction moves #width bits from #lsb into the destination,
1676 extending the result with zeros. This is an alias for:
1678 UBFM rd, rn, #lsb, #(lsb + width - 1)
1680 RD is the destination register.
1681 RN is the source register.
1682 LSB is the bit number to start at (least significant bit).
1683 WIDTH is the number of bits to move. */
1686 emit_ubfx (uint32_t *buf
, struct aarch64_register rd
,
1687 struct aarch64_register rn
, uint32_t lsb
, uint32_t width
)
1689 return emit_ubfm (buf
, rd
, rn
, lsb
, lsb
+ width
- 1);
1692 /* Write a CSINC instruction into *BUF.
1694 CSINC rd, rn, rm, cond
1696 This instruction conditionally increments rn or rm and places the result
1697 in rd. rn is chosen is the condition is true.
1699 RD is the destination register.
1700 RN and RM are the source registers.
1701 COND is the encoded condition. */
1704 emit_csinc (uint32_t *buf
, struct aarch64_register rd
,
1705 struct aarch64_register rn
, struct aarch64_register rm
,
1708 uint32_t size
= ENCODE (rd
.is64
, 1, 31);
1710 return aarch64_emit_insn (buf
, CSINC
| size
| ENCODE (rm
.num
, 5, 16)
1711 | ENCODE (cond
, 4, 12) | ENCODE (rn
.num
, 5, 5)
1712 | ENCODE (rd
.num
, 5, 0));
1715 /* Write a CSET instruction into *BUF.
1719 This instruction conditionally write 1 or 0 in the destination register.
1720 1 is written if the condition is true. This is an alias for:
1722 CSINC rd, xzr, xzr, !cond
1724 Note that the condition needs to be inverted.
1726 RD is the destination register.
1727 RN and RM are the source registers.
1728 COND is the encoded condition. */
1731 emit_cset (uint32_t *buf
, struct aarch64_register rd
, unsigned cond
)
1733 /* The least significant bit of the condition needs toggling in order to
1735 return emit_csinc (buf
, rd
, xzr
, xzr
, cond
^ 0x1);
1738 /* Write LEN instructions from BUF into the inferior memory at *TO.
1740 Note instructions are always little endian on AArch64, unlike data. */
1743 append_insns (CORE_ADDR
*to
, size_t len
, const uint32_t *buf
)
1745 size_t byte_len
= len
* sizeof (uint32_t);
1746 #if (__BYTE_ORDER == __BIG_ENDIAN)
1747 uint32_t *le_buf
= (uint32_t *) xmalloc (byte_len
);
1750 for (i
= 0; i
< len
; i
++)
1751 le_buf
[i
] = htole32 (buf
[i
]);
1753 target_write_memory (*to
, (const unsigned char *) le_buf
, byte_len
);
1757 target_write_memory (*to
, (const unsigned char *) buf
, byte_len
);
1763 /* Sub-class of struct aarch64_insn_data, store information of
1764 instruction relocation for fast tracepoint. Visitor can
1765 relocate an instruction from BASE.INSN_ADDR to NEW_ADDR and save
1766 the relocated instructions in buffer pointed by INSN_PTR. */
1768 struct aarch64_insn_relocation_data
1770 struct aarch64_insn_data base
;
1772 /* The new address the instruction is relocated to. */
1774 /* Pointer to the buffer of relocated instruction(s). */
1778 /* Implementation of aarch64_insn_visitor method "b". */
1781 aarch64_ftrace_insn_reloc_b (const int is_bl
, const int32_t offset
,
1782 struct aarch64_insn_data
*data
)
1784 struct aarch64_insn_relocation_data
*insn_reloc
1785 = (struct aarch64_insn_relocation_data
*) data
;
1787 = insn_reloc
->base
.insn_addr
- insn_reloc
->new_addr
+ offset
;
1789 if (can_encode_int32 (new_offset
, 28))
1790 insn_reloc
->insn_ptr
+= emit_b (insn_reloc
->insn_ptr
, is_bl
, new_offset
);
1793 /* Implementation of aarch64_insn_visitor method "b_cond". */
1796 aarch64_ftrace_insn_reloc_b_cond (const unsigned cond
, const int32_t offset
,
1797 struct aarch64_insn_data
*data
)
1799 struct aarch64_insn_relocation_data
*insn_reloc
1800 = (struct aarch64_insn_relocation_data
*) data
;
1802 = insn_reloc
->base
.insn_addr
- insn_reloc
->new_addr
+ offset
;
1804 if (can_encode_int32 (new_offset
, 21))
1806 insn_reloc
->insn_ptr
+= emit_bcond (insn_reloc
->insn_ptr
, cond
,
1809 else if (can_encode_int32 (new_offset
, 28))
1811 /* The offset is out of range for a conditional branch
1812 instruction but not for a unconditional branch. We can use
1813 the following instructions instead:
1815 B.COND TAKEN ; If cond is true, then jump to TAKEN.
1816 B NOT_TAKEN ; Else jump over TAKEN and continue.
1823 insn_reloc
->insn_ptr
+= emit_bcond (insn_reloc
->insn_ptr
, cond
, 8);
1824 insn_reloc
->insn_ptr
+= emit_b (insn_reloc
->insn_ptr
, 0, 8);
1825 insn_reloc
->insn_ptr
+= emit_b (insn_reloc
->insn_ptr
, 0, new_offset
- 8);
1829 /* Implementation of aarch64_insn_visitor method "cb". */
1832 aarch64_ftrace_insn_reloc_cb (const int32_t offset
, const int is_cbnz
,
1833 const unsigned rn
, int is64
,
1834 struct aarch64_insn_data
*data
)
1836 struct aarch64_insn_relocation_data
*insn_reloc
1837 = (struct aarch64_insn_relocation_data
*) data
;
1839 = insn_reloc
->base
.insn_addr
- insn_reloc
->new_addr
+ offset
;
1841 if (can_encode_int32 (new_offset
, 21))
1843 insn_reloc
->insn_ptr
+= emit_cb (insn_reloc
->insn_ptr
, is_cbnz
,
1844 aarch64_register (rn
, is64
), new_offset
);
1846 else if (can_encode_int32 (new_offset
, 28))
1848 /* The offset is out of range for a compare and branch
1849 instruction but not for a unconditional branch. We can use
1850 the following instructions instead:
1852 CBZ xn, TAKEN ; xn == 0, then jump to TAKEN.
1853 B NOT_TAKEN ; Else jump over TAKEN and continue.
1859 insn_reloc
->insn_ptr
+= emit_cb (insn_reloc
->insn_ptr
, is_cbnz
,
1860 aarch64_register (rn
, is64
), 8);
1861 insn_reloc
->insn_ptr
+= emit_b (insn_reloc
->insn_ptr
, 0, 8);
1862 insn_reloc
->insn_ptr
+= emit_b (insn_reloc
->insn_ptr
, 0, new_offset
- 8);
1866 /* Implementation of aarch64_insn_visitor method "tb". */
1869 aarch64_ftrace_insn_reloc_tb (const int32_t offset
, int is_tbnz
,
1870 const unsigned rt
, unsigned bit
,
1871 struct aarch64_insn_data
*data
)
1873 struct aarch64_insn_relocation_data
*insn_reloc
1874 = (struct aarch64_insn_relocation_data
*) data
;
1876 = insn_reloc
->base
.insn_addr
- insn_reloc
->new_addr
+ offset
;
1878 if (can_encode_int32 (new_offset
, 16))
1880 insn_reloc
->insn_ptr
+= emit_tb (insn_reloc
->insn_ptr
, is_tbnz
, bit
,
1881 aarch64_register (rt
, 1), new_offset
);
1883 else if (can_encode_int32 (new_offset
, 28))
1885 /* The offset is out of range for a test bit and branch
1886 instruction but not for a unconditional branch. We can use
1887 the following instructions instead:
1889 TBZ xn, #bit, TAKEN ; xn[bit] == 0, then jump to TAKEN.
1890 B NOT_TAKEN ; Else jump over TAKEN and continue.
1896 insn_reloc
->insn_ptr
+= emit_tb (insn_reloc
->insn_ptr
, is_tbnz
, bit
,
1897 aarch64_register (rt
, 1), 8);
1898 insn_reloc
->insn_ptr
+= emit_b (insn_reloc
->insn_ptr
, 0, 8);
1899 insn_reloc
->insn_ptr
+= emit_b (insn_reloc
->insn_ptr
, 0,
1904 /* Implementation of aarch64_insn_visitor method "adr". */
1907 aarch64_ftrace_insn_reloc_adr (const int32_t offset
, const unsigned rd
,
1909 struct aarch64_insn_data
*data
)
1911 struct aarch64_insn_relocation_data
*insn_reloc
1912 = (struct aarch64_insn_relocation_data
*) data
;
1913 /* We know exactly the address the ADR{P,} instruction will compute.
1914 We can just write it to the destination register. */
1915 CORE_ADDR address
= data
->insn_addr
+ offset
;
1919 /* Clear the lower 12 bits of the offset to get the 4K page. */
1920 insn_reloc
->insn_ptr
+= emit_mov_addr (insn_reloc
->insn_ptr
,
1921 aarch64_register (rd
, 1),
1925 insn_reloc
->insn_ptr
+= emit_mov_addr (insn_reloc
->insn_ptr
,
1926 aarch64_register (rd
, 1), address
);
1929 /* Implementation of aarch64_insn_visitor method "ldr_literal". */
1932 aarch64_ftrace_insn_reloc_ldr_literal (const int32_t offset
, const int is_sw
,
1933 const unsigned rt
, const int is64
,
1934 struct aarch64_insn_data
*data
)
1936 struct aarch64_insn_relocation_data
*insn_reloc
1937 = (struct aarch64_insn_relocation_data
*) data
;
1938 CORE_ADDR address
= data
->insn_addr
+ offset
;
1940 insn_reloc
->insn_ptr
+= emit_mov_addr (insn_reloc
->insn_ptr
,
1941 aarch64_register (rt
, 1), address
);
1943 /* We know exactly what address to load from, and what register we
1946 MOV xd, #(oldloc + offset)
1947 MOVK xd, #((oldloc + offset) >> 16), lsl #16
1950 LDR xd, [xd] ; or LDRSW xd, [xd]
1955 insn_reloc
->insn_ptr
+= emit_ldrsw (insn_reloc
->insn_ptr
,
1956 aarch64_register (rt
, 1),
1957 aarch64_register (rt
, 1),
1958 offset_memory_operand (0));
1960 insn_reloc
->insn_ptr
+= emit_ldr (insn_reloc
->insn_ptr
,
1961 aarch64_register (rt
, is64
),
1962 aarch64_register (rt
, 1),
1963 offset_memory_operand (0));
1966 /* Implementation of aarch64_insn_visitor method "others". */
1969 aarch64_ftrace_insn_reloc_others (const uint32_t insn
,
1970 struct aarch64_insn_data
*data
)
1972 struct aarch64_insn_relocation_data
*insn_reloc
1973 = (struct aarch64_insn_relocation_data
*) data
;
1975 /* The instruction is not PC relative. Just re-emit it at the new
1977 insn_reloc
->insn_ptr
+= aarch64_emit_insn (insn_reloc
->insn_ptr
, insn
);
1980 static const struct aarch64_insn_visitor visitor
=
1982 aarch64_ftrace_insn_reloc_b
,
1983 aarch64_ftrace_insn_reloc_b_cond
,
1984 aarch64_ftrace_insn_reloc_cb
,
1985 aarch64_ftrace_insn_reloc_tb
,
1986 aarch64_ftrace_insn_reloc_adr
,
1987 aarch64_ftrace_insn_reloc_ldr_literal
,
1988 aarch64_ftrace_insn_reloc_others
,
1992 aarch64_target::supports_fast_tracepoints ()
1997 /* Implementation of target ops method
1998 "install_fast_tracepoint_jump_pad". */
2001 aarch64_target::install_fast_tracepoint_jump_pad
2002 (CORE_ADDR tpoint
, CORE_ADDR tpaddr
, CORE_ADDR collector
,
2003 CORE_ADDR lockaddr
, ULONGEST orig_size
, CORE_ADDR
*jump_entry
,
2004 CORE_ADDR
*trampoline
, ULONGEST
*trampoline_size
,
2005 unsigned char *jjump_pad_insn
, ULONGEST
*jjump_pad_insn_size
,
2006 CORE_ADDR
*adjusted_insn_addr
, CORE_ADDR
*adjusted_insn_addr_end
,
2014 CORE_ADDR buildaddr
= *jump_entry
;
2015 struct aarch64_insn_relocation_data insn_data
;
2017 /* We need to save the current state on the stack both to restore it
2018 later and to collect register values when the tracepoint is hit.
2020 The saved registers are pushed in a layout that needs to be in sync
2021 with aarch64_ft_collect_regmap (see linux-aarch64-ipa.c). Later on
2022 the supply_fast_tracepoint_registers function will fill in the
2023 register cache from a pointer to saved registers on the stack we build
2026 For simplicity, we set the size of each cell on the stack to 16 bytes.
2027 This way one cell can hold any register type, from system registers
2028 to the 128 bit SIMD&FP registers. Furthermore, the stack pointer
2029 has to be 16 bytes aligned anyway.
2031 Note that the CPSR register does not exist on AArch64. Instead we
2032 can access system bits describing the process state with the
2033 MRS/MSR instructions, namely the condition flags. We save them as
2034 if they are part of a CPSR register because that's how GDB
2035 interprets these system bits. At the moment, only the condition
2036 flags are saved in CPSR (NZCV).
2038 Stack layout, each cell is 16 bytes (descending):
2040 High *-------- SIMD&FP registers from 31 down to 0. --------*
2046 *---- General purpose registers from 30 down to 0. ----*
2052 *------------- Special purpose registers. -------------*
2055 | CPSR (NZCV) | 5 cells
2058 *------------- collecting_t object --------------------*
2059 | TPIDR_EL0 | struct tracepoint * |
2060 Low *------------------------------------------------------*
2062 After this stack is set up, we issue a call to the collector, passing
2063 it the saved registers at (SP + 16). */
2065 /* Push SIMD&FP registers on the stack:
2067 SUB sp, sp, #(32 * 16)
2069 STP q30, q31, [sp, #(30 * 16)]
2074 p
+= emit_sub (p
, sp
, sp
, immediate_operand (32 * 16));
2075 for (i
= 30; i
>= 0; i
-= 2)
2076 p
+= emit_stp_q_offset (p
, i
, i
+ 1, sp
, i
* 16);
2078 /* Push general purpose registers on the stack. Note that we do not need
2079 to push x31 as it represents the xzr register and not the stack
2080 pointer in a STR instruction.
2082 SUB sp, sp, #(31 * 16)
2084 STR x30, [sp, #(30 * 16)]
2089 p
+= emit_sub (p
, sp
, sp
, immediate_operand (31 * 16));
2090 for (i
= 30; i
>= 0; i
-= 1)
2091 p
+= emit_str (p
, aarch64_register (i
, 1), sp
,
2092 offset_memory_operand (i
* 16));
2094 /* Make space for 5 more cells.
2096 SUB sp, sp, #(5 * 16)
2099 p
+= emit_sub (p
, sp
, sp
, immediate_operand (5 * 16));
2104 ADD x4, sp, #((32 + 31 + 5) * 16)
2105 STR x4, [sp, #(4 * 16)]
2108 p
+= emit_add (p
, x4
, sp
, immediate_operand ((32 + 31 + 5) * 16));
2109 p
+= emit_str (p
, x4
, sp
, offset_memory_operand (4 * 16));
2111 /* Save PC (tracepoint address):
2116 STR x3, [sp, #(3 * 16)]
2120 p
+= emit_mov_addr (p
, x3
, tpaddr
);
2121 p
+= emit_str (p
, x3
, sp
, offset_memory_operand (3 * 16));
2123 /* Save CPSR (NZCV), FPSR and FPCR:
2129 STR x2, [sp, #(2 * 16)]
2130 STR x1, [sp, #(1 * 16)]
2131 STR x0, [sp, #(0 * 16)]
2134 p
+= emit_mrs (p
, x2
, NZCV
);
2135 p
+= emit_mrs (p
, x1
, FPSR
);
2136 p
+= emit_mrs (p
, x0
, FPCR
);
2137 p
+= emit_str (p
, x2
, sp
, offset_memory_operand (2 * 16));
2138 p
+= emit_str (p
, x1
, sp
, offset_memory_operand (1 * 16));
2139 p
+= emit_str (p
, x0
, sp
, offset_memory_operand (0 * 16));
2141 /* Push the collecting_t object. It consist of the address of the
2142 tracepoint and an ID for the current thread. We get the latter by
2143 reading the tpidr_el0 system register. It corresponds to the
2144 NT_ARM_TLS register accessible with ptrace.
2151 STP x0, x1, [sp, #-16]!
2155 p
+= emit_mov_addr (p
, x0
, tpoint
);
2156 p
+= emit_mrs (p
, x1
, TPIDR_EL0
);
2157 p
+= emit_stp (p
, x0
, x1
, sp
, preindex_memory_operand (-16));
2161 The shared memory for the lock is at lockaddr. It will hold zero
2162 if no-one is holding the lock, otherwise it contains the address of
2163 the collecting_t object on the stack of the thread which acquired it.
2165 At this stage, the stack pointer points to this thread's collecting_t
2168 We use the following registers:
2169 - x0: Address of the lock.
2170 - x1: Pointer to collecting_t object.
2171 - x2: Scratch register.
2177 ; Trigger an event local to this core. So the following WFE
2178 ; instruction is ignored.
2181 ; Wait for an event. The event is triggered by either the SEVL
2182 ; or STLR instructions (store release).
2185 ; Atomically read at lockaddr. This marks the memory location as
2186 ; exclusive. This instruction also has memory constraints which
2187 ; make sure all previous data reads and writes are done before
2191 ; Try again if another thread holds the lock.
2194 ; We can lock it! Write the address of the collecting_t object.
2195 ; This instruction will fail if the memory location is not marked
2196 ; as exclusive anymore. If it succeeds, it will remove the
2197 ; exclusive mark on the memory location. This way, if another
2198 ; thread executes this instruction before us, we will fail and try
2205 p
+= emit_mov_addr (p
, x0
, lockaddr
);
2206 p
+= emit_mov (p
, x1
, register_operand (sp
));
2210 p
+= emit_ldaxr (p
, x2
, x0
);
2211 p
+= emit_cb (p
, 1, w2
, -2 * 4);
2212 p
+= emit_stxr (p
, w2
, x1
, x0
);
2213 p
+= emit_cb (p
, 1, x2
, -4 * 4);
2215 /* Call collector (struct tracepoint *, unsigned char *):
2220 ; Saved registers start after the collecting_t object.
2223 ; We use an intra-procedure-call scratch register.
2224 MOV ip0, #(collector)
2227 ; And call back to C!
2232 p
+= emit_mov_addr (p
, x0
, tpoint
);
2233 p
+= emit_add (p
, x1
, sp
, immediate_operand (16));
2235 p
+= emit_mov_addr (p
, ip0
, collector
);
2236 p
+= emit_blr (p
, ip0
);
2238 /* Release the lock.
2243 ; This instruction is a normal store with memory ordering
2244 ; constraints. Thanks to this we do not have to put a data
2245 ; barrier instruction to make sure all data read and writes are done
2246 ; before this instruction is executed. Furthermore, this instruction
2247 ; will trigger an event, letting other threads know they can grab
2252 p
+= emit_mov_addr (p
, x0
, lockaddr
);
2253 p
+= emit_stlr (p
, xzr
, x0
);
2255 /* Free collecting_t object:
2260 p
+= emit_add (p
, sp
, sp
, immediate_operand (16));
2262 /* Restore CPSR (NZCV), FPSR and FPCR. And free all special purpose
2263 registers from the stack.
2265 LDR x2, [sp, #(2 * 16)]
2266 LDR x1, [sp, #(1 * 16)]
2267 LDR x0, [sp, #(0 * 16)]
2273 ADD sp, sp #(5 * 16)
2276 p
+= emit_ldr (p
, x2
, sp
, offset_memory_operand (2 * 16));
2277 p
+= emit_ldr (p
, x1
, sp
, offset_memory_operand (1 * 16));
2278 p
+= emit_ldr (p
, x0
, sp
, offset_memory_operand (0 * 16));
2279 p
+= emit_msr (p
, NZCV
, x2
);
2280 p
+= emit_msr (p
, FPSR
, x1
);
2281 p
+= emit_msr (p
, FPCR
, x0
);
2283 p
+= emit_add (p
, sp
, sp
, immediate_operand (5 * 16));
2285 /* Pop general purpose registers:
2289 LDR x30, [sp, #(30 * 16)]
2291 ADD sp, sp, #(31 * 16)
2294 for (i
= 0; i
<= 30; i
+= 1)
2295 p
+= emit_ldr (p
, aarch64_register (i
, 1), sp
,
2296 offset_memory_operand (i
* 16));
2297 p
+= emit_add (p
, sp
, sp
, immediate_operand (31 * 16));
2299 /* Pop SIMD&FP registers:
2303 LDP q30, q31, [sp, #(30 * 16)]
2305 ADD sp, sp, #(32 * 16)
2308 for (i
= 0; i
<= 30; i
+= 2)
2309 p
+= emit_ldp_q_offset (p
, i
, i
+ 1, sp
, i
* 16);
2310 p
+= emit_add (p
, sp
, sp
, immediate_operand (32 * 16));
2312 /* Write the code into the inferior memory. */
2313 append_insns (&buildaddr
, p
- buf
, buf
);
2315 /* Now emit the relocated instruction. */
2316 *adjusted_insn_addr
= buildaddr
;
2317 target_read_uint32 (tpaddr
, &insn
);
2319 insn_data
.base
.insn_addr
= tpaddr
;
2320 insn_data
.new_addr
= buildaddr
;
2321 insn_data
.insn_ptr
= buf
;
2323 aarch64_relocate_instruction (insn
, &visitor
,
2324 (struct aarch64_insn_data
*) &insn_data
);
2326 /* We may not have been able to relocate the instruction. */
2327 if (insn_data
.insn_ptr
== buf
)
2330 "E.Could not relocate instruction from %s to %s.",
2331 core_addr_to_string_nz (tpaddr
),
2332 core_addr_to_string_nz (buildaddr
));
2336 append_insns (&buildaddr
, insn_data
.insn_ptr
- buf
, buf
);
2337 *adjusted_insn_addr_end
= buildaddr
;
2339 /* Go back to the start of the buffer. */
2342 /* Emit a branch back from the jump pad. */
2343 offset
= (tpaddr
+ orig_size
- buildaddr
);
2344 if (!can_encode_int32 (offset
, 28))
2347 "E.Jump back from jump pad too far from tracepoint "
2348 "(offset 0x%" PRIx64
" cannot be encoded in 28 bits).",
2353 p
+= emit_b (p
, 0, offset
);
2354 append_insns (&buildaddr
, p
- buf
, buf
);
2356 /* Give the caller a branch instruction into the jump pad. */
2357 offset
= (*jump_entry
- tpaddr
);
2358 if (!can_encode_int32 (offset
, 28))
2361 "E.Jump pad too far from tracepoint "
2362 "(offset 0x%" PRIx64
" cannot be encoded in 28 bits).",
2367 emit_b ((uint32_t *) jjump_pad_insn
, 0, offset
);
2368 *jjump_pad_insn_size
= 4;
2370 /* Return the end address of our pad. */
2371 *jump_entry
= buildaddr
;
2376 /* Helper function writing LEN instructions from START into
2377 current_insn_ptr. */
2380 emit_ops_insns (const uint32_t *start
, int len
)
2382 CORE_ADDR buildaddr
= current_insn_ptr
;
2385 debug_printf ("Adding %d instrucions at %s\n",
2386 len
, paddress (buildaddr
));
2388 append_insns (&buildaddr
, len
, start
);
2389 current_insn_ptr
= buildaddr
;
2392 /* Pop a register from the stack. */
2395 emit_pop (uint32_t *buf
, struct aarch64_register rt
)
2397 return emit_ldr (buf
, rt
, sp
, postindex_memory_operand (1 * 16));
2400 /* Push a register on the stack. */
2403 emit_push (uint32_t *buf
, struct aarch64_register rt
)
2405 return emit_str (buf
, rt
, sp
, preindex_memory_operand (-1 * 16));
2408 /* Implementation of emit_ops method "emit_prologue". */
2411 aarch64_emit_prologue (void)
2416 /* This function emit a prologue for the following function prototype:
2418 enum eval_result_type f (unsigned char *regs,
2421 The first argument is a buffer of raw registers. The second
2422 argument is the result of
2423 evaluating the expression, which will be set to whatever is on top of
2424 the stack at the end.
2426 The stack set up by the prologue is as such:
2428 High *------------------------------------------------------*
2431 | x1 (ULONGEST *value) |
2432 | x0 (unsigned char *regs) |
2433 Low *------------------------------------------------------*
2435 As we are implementing a stack machine, each opcode can expand the
2436 stack so we never know how far we are from the data saved by this
2437 prologue. In order to be able refer to value and regs later, we save
2438 the current stack pointer in the frame pointer. This way, it is not
2439 clobbered when calling C functions.
2441 Finally, throughout every operation, we are using register x0 as the
2442 top of the stack, and x1 as a scratch register. */
2444 p
+= emit_stp (p
, x0
, x1
, sp
, preindex_memory_operand (-2 * 16));
2445 p
+= emit_str (p
, lr
, sp
, offset_memory_operand (3 * 8));
2446 p
+= emit_str (p
, fp
, sp
, offset_memory_operand (2 * 8));
2448 p
+= emit_add (p
, fp
, sp
, immediate_operand (2 * 8));
2451 emit_ops_insns (buf
, p
- buf
);
2454 /* Implementation of emit_ops method "emit_epilogue". */
2457 aarch64_emit_epilogue (void)
2462 /* Store the result of the expression (x0) in *value. */
2463 p
+= emit_sub (p
, x1
, fp
, immediate_operand (1 * 8));
2464 p
+= emit_ldr (p
, x1
, x1
, offset_memory_operand (0));
2465 p
+= emit_str (p
, x0
, x1
, offset_memory_operand (0));
2467 /* Restore the previous state. */
2468 p
+= emit_add (p
, sp
, fp
, immediate_operand (2 * 8));
2469 p
+= emit_ldp (p
, fp
, lr
, fp
, offset_memory_operand (0));
2471 /* Return expr_eval_no_error. */
2472 p
+= emit_mov (p
, x0
, immediate_operand (expr_eval_no_error
));
2473 p
+= emit_ret (p
, lr
);
2475 emit_ops_insns (buf
, p
- buf
);
2478 /* Implementation of emit_ops method "emit_add". */
2481 aarch64_emit_add (void)
2486 p
+= emit_pop (p
, x1
);
2487 p
+= emit_add (p
, x0
, x1
, register_operand (x0
));
2489 emit_ops_insns (buf
, p
- buf
);
2492 /* Implementation of emit_ops method "emit_sub". */
2495 aarch64_emit_sub (void)
2500 p
+= emit_pop (p
, x1
);
2501 p
+= emit_sub (p
, x0
, x1
, register_operand (x0
));
2503 emit_ops_insns (buf
, p
- buf
);
2506 /* Implementation of emit_ops method "emit_mul". */
2509 aarch64_emit_mul (void)
2514 p
+= emit_pop (p
, x1
);
2515 p
+= emit_mul (p
, x0
, x1
, x0
);
2517 emit_ops_insns (buf
, p
- buf
);
2520 /* Implementation of emit_ops method "emit_lsh". */
2523 aarch64_emit_lsh (void)
2528 p
+= emit_pop (p
, x1
);
2529 p
+= emit_lslv (p
, x0
, x1
, x0
);
2531 emit_ops_insns (buf
, p
- buf
);
2534 /* Implementation of emit_ops method "emit_rsh_signed". */
2537 aarch64_emit_rsh_signed (void)
2542 p
+= emit_pop (p
, x1
);
2543 p
+= emit_asrv (p
, x0
, x1
, x0
);
2545 emit_ops_insns (buf
, p
- buf
);
2548 /* Implementation of emit_ops method "emit_rsh_unsigned". */
2551 aarch64_emit_rsh_unsigned (void)
2556 p
+= emit_pop (p
, x1
);
2557 p
+= emit_lsrv (p
, x0
, x1
, x0
);
2559 emit_ops_insns (buf
, p
- buf
);
2562 /* Implementation of emit_ops method "emit_ext". */
2565 aarch64_emit_ext (int arg
)
2570 p
+= emit_sbfx (p
, x0
, x0
, 0, arg
);
2572 emit_ops_insns (buf
, p
- buf
);
2575 /* Implementation of emit_ops method "emit_log_not". */
2578 aarch64_emit_log_not (void)
2583 /* If the top of the stack is 0, replace it with 1. Else replace it with
2586 p
+= emit_cmp (p
, x0
, immediate_operand (0));
2587 p
+= emit_cset (p
, x0
, EQ
);
2589 emit_ops_insns (buf
, p
- buf
);
2592 /* Implementation of emit_ops method "emit_bit_and". */
2595 aarch64_emit_bit_and (void)
2600 p
+= emit_pop (p
, x1
);
2601 p
+= emit_and (p
, x0
, x0
, x1
);
2603 emit_ops_insns (buf
, p
- buf
);
2606 /* Implementation of emit_ops method "emit_bit_or". */
2609 aarch64_emit_bit_or (void)
2614 p
+= emit_pop (p
, x1
);
2615 p
+= emit_orr (p
, x0
, x0
, x1
);
2617 emit_ops_insns (buf
, p
- buf
);
2620 /* Implementation of emit_ops method "emit_bit_xor". */
2623 aarch64_emit_bit_xor (void)
2628 p
+= emit_pop (p
, x1
);
2629 p
+= emit_eor (p
, x0
, x0
, x1
);
2631 emit_ops_insns (buf
, p
- buf
);
2634 /* Implementation of emit_ops method "emit_bit_not". */
2637 aarch64_emit_bit_not (void)
2642 p
+= emit_mvn (p
, x0
, x0
);
2644 emit_ops_insns (buf
, p
- buf
);
2647 /* Implementation of emit_ops method "emit_equal". */
2650 aarch64_emit_equal (void)
2655 p
+= emit_pop (p
, x1
);
2656 p
+= emit_cmp (p
, x0
, register_operand (x1
));
2657 p
+= emit_cset (p
, x0
, EQ
);
2659 emit_ops_insns (buf
, p
- buf
);
2662 /* Implementation of emit_ops method "emit_less_signed". */
2665 aarch64_emit_less_signed (void)
2670 p
+= emit_pop (p
, x1
);
2671 p
+= emit_cmp (p
, x1
, register_operand (x0
));
2672 p
+= emit_cset (p
, x0
, LT
);
2674 emit_ops_insns (buf
, p
- buf
);
2677 /* Implementation of emit_ops method "emit_less_unsigned". */
2680 aarch64_emit_less_unsigned (void)
2685 p
+= emit_pop (p
, x1
);
2686 p
+= emit_cmp (p
, x1
, register_operand (x0
));
2687 p
+= emit_cset (p
, x0
, LO
);
2689 emit_ops_insns (buf
, p
- buf
);
2692 /* Implementation of emit_ops method "emit_ref". */
2695 aarch64_emit_ref (int size
)
2703 p
+= emit_ldrb (p
, w0
, x0
, offset_memory_operand (0));
2706 p
+= emit_ldrh (p
, w0
, x0
, offset_memory_operand (0));
2709 p
+= emit_ldr (p
, w0
, x0
, offset_memory_operand (0));
2712 p
+= emit_ldr (p
, x0
, x0
, offset_memory_operand (0));
2715 /* Unknown size, bail on compilation. */
2720 emit_ops_insns (buf
, p
- buf
);
2723 /* Implementation of emit_ops method "emit_if_goto". */
2726 aarch64_emit_if_goto (int *offset_p
, int *size_p
)
2731 /* The Z flag is set or cleared here. */
2732 p
+= emit_cmp (p
, x0
, immediate_operand (0));
2733 /* This instruction must not change the Z flag. */
2734 p
+= emit_pop (p
, x0
);
2735 /* Branch over the next instruction if x0 == 0. */
2736 p
+= emit_bcond (p
, EQ
, 8);
2738 /* The NOP instruction will be patched with an unconditional branch. */
2740 *offset_p
= (p
- buf
) * 4;
2745 emit_ops_insns (buf
, p
- buf
);
2748 /* Implementation of emit_ops method "emit_goto". */
2751 aarch64_emit_goto (int *offset_p
, int *size_p
)
2756 /* The NOP instruction will be patched with an unconditional branch. */
2763 emit_ops_insns (buf
, p
- buf
);
2766 /* Implementation of emit_ops method "write_goto_address". */
2769 aarch64_write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
2773 emit_b (&insn
, 0, to
- from
);
2774 append_insns (&from
, 1, &insn
);
2777 /* Implementation of emit_ops method "emit_const". */
2780 aarch64_emit_const (LONGEST num
)
2785 p
+= emit_mov_addr (p
, x0
, num
);
2787 emit_ops_insns (buf
, p
- buf
);
2790 /* Implementation of emit_ops method "emit_call". */
2793 aarch64_emit_call (CORE_ADDR fn
)
2798 p
+= emit_mov_addr (p
, ip0
, fn
);
2799 p
+= emit_blr (p
, ip0
);
2801 emit_ops_insns (buf
, p
- buf
);
2804 /* Implementation of emit_ops method "emit_reg". */
2807 aarch64_emit_reg (int reg
)
2812 /* Set x0 to unsigned char *regs. */
2813 p
+= emit_sub (p
, x0
, fp
, immediate_operand (2 * 8));
2814 p
+= emit_ldr (p
, x0
, x0
, offset_memory_operand (0));
2815 p
+= emit_mov (p
, x1
, immediate_operand (reg
));
2817 emit_ops_insns (buf
, p
- buf
);
2819 aarch64_emit_call (get_raw_reg_func_addr ());
2822 /* Implementation of emit_ops method "emit_pop". */
2825 aarch64_emit_pop (void)
2830 p
+= emit_pop (p
, x0
);
2832 emit_ops_insns (buf
, p
- buf
);
2835 /* Implementation of emit_ops method "emit_stack_flush". */
2838 aarch64_emit_stack_flush (void)
2843 p
+= emit_push (p
, x0
);
2845 emit_ops_insns (buf
, p
- buf
);
2848 /* Implementation of emit_ops method "emit_zero_ext". */
2851 aarch64_emit_zero_ext (int arg
)
2856 p
+= emit_ubfx (p
, x0
, x0
, 0, arg
);
2858 emit_ops_insns (buf
, p
- buf
);
2861 /* Implementation of emit_ops method "emit_swap". */
2864 aarch64_emit_swap (void)
2869 p
+= emit_ldr (p
, x1
, sp
, offset_memory_operand (0 * 16));
2870 p
+= emit_str (p
, x0
, sp
, offset_memory_operand (0 * 16));
2871 p
+= emit_mov (p
, x0
, register_operand (x1
));
2873 emit_ops_insns (buf
, p
- buf
);
2876 /* Implementation of emit_ops method "emit_stack_adjust". */
2879 aarch64_emit_stack_adjust (int n
)
2881 /* This is not needed with our design. */
2885 p
+= emit_add (p
, sp
, sp
, immediate_operand (n
* 16));
2887 emit_ops_insns (buf
, p
- buf
);
2890 /* Implementation of emit_ops method "emit_int_call_1". */
2893 aarch64_emit_int_call_1 (CORE_ADDR fn
, int arg1
)
2898 p
+= emit_mov (p
, x0
, immediate_operand (arg1
));
2900 emit_ops_insns (buf
, p
- buf
);
2902 aarch64_emit_call (fn
);
2905 /* Implementation of emit_ops method "emit_void_call_2". */
2908 aarch64_emit_void_call_2 (CORE_ADDR fn
, int arg1
)
2913 /* Push x0 on the stack. */
2914 aarch64_emit_stack_flush ();
2916 /* Setup arguments for the function call:
2919 x1: top of the stack
2924 p
+= emit_mov (p
, x1
, register_operand (x0
));
2925 p
+= emit_mov (p
, x0
, immediate_operand (arg1
));
2927 emit_ops_insns (buf
, p
- buf
);
2929 aarch64_emit_call (fn
);
2932 aarch64_emit_pop ();
2935 /* Implementation of emit_ops method "emit_eq_goto". */
2938 aarch64_emit_eq_goto (int *offset_p
, int *size_p
)
2943 p
+= emit_pop (p
, x1
);
2944 p
+= emit_cmp (p
, x1
, register_operand (x0
));
2945 /* Branch over the next instruction if x0 != x1. */
2946 p
+= emit_bcond (p
, NE
, 8);
2947 /* The NOP instruction will be patched with an unconditional branch. */
2949 *offset_p
= (p
- buf
) * 4;
2954 emit_ops_insns (buf
, p
- buf
);
2957 /* Implementation of emit_ops method "emit_ne_goto". */
2960 aarch64_emit_ne_goto (int *offset_p
, int *size_p
)
2965 p
+= emit_pop (p
, x1
);
2966 p
+= emit_cmp (p
, x1
, register_operand (x0
));
2967 /* Branch over the next instruction if x0 == x1. */
2968 p
+= emit_bcond (p
, EQ
, 8);
2969 /* The NOP instruction will be patched with an unconditional branch. */
2971 *offset_p
= (p
- buf
) * 4;
2976 emit_ops_insns (buf
, p
- buf
);
2979 /* Implementation of emit_ops method "emit_lt_goto". */
2982 aarch64_emit_lt_goto (int *offset_p
, int *size_p
)
2987 p
+= emit_pop (p
, x1
);
2988 p
+= emit_cmp (p
, x1
, register_operand (x0
));
2989 /* Branch over the next instruction if x0 >= x1. */
2990 p
+= emit_bcond (p
, GE
, 8);
2991 /* The NOP instruction will be patched with an unconditional branch. */
2993 *offset_p
= (p
- buf
) * 4;
2998 emit_ops_insns (buf
, p
- buf
);
3001 /* Implementation of emit_ops method "emit_le_goto". */
3004 aarch64_emit_le_goto (int *offset_p
, int *size_p
)
3009 p
+= emit_pop (p
, x1
);
3010 p
+= emit_cmp (p
, x1
, register_operand (x0
));
3011 /* Branch over the next instruction if x0 > x1. */
3012 p
+= emit_bcond (p
, GT
, 8);
3013 /* The NOP instruction will be patched with an unconditional branch. */
3015 *offset_p
= (p
- buf
) * 4;
3020 emit_ops_insns (buf
, p
- buf
);
3023 /* Implementation of emit_ops method "emit_gt_goto". */
3026 aarch64_emit_gt_goto (int *offset_p
, int *size_p
)
3031 p
+= emit_pop (p
, x1
);
3032 p
+= emit_cmp (p
, x1
, register_operand (x0
));
3033 /* Branch over the next instruction if x0 <= x1. */
3034 p
+= emit_bcond (p
, LE
, 8);
3035 /* The NOP instruction will be patched with an unconditional branch. */
3037 *offset_p
= (p
- buf
) * 4;
3042 emit_ops_insns (buf
, p
- buf
);
3045 /* Implementation of emit_ops method "emit_ge_got". */
3048 aarch64_emit_ge_got (int *offset_p
, int *size_p
)
3053 p
+= emit_pop (p
, x1
);
3054 p
+= emit_cmp (p
, x1
, register_operand (x0
));
3055 /* Branch over the next instruction if x0 <= x1. */
3056 p
+= emit_bcond (p
, LT
, 8);
3057 /* The NOP instruction will be patched with an unconditional branch. */
3059 *offset_p
= (p
- buf
) * 4;
3064 emit_ops_insns (buf
, p
- buf
);
3067 static struct emit_ops aarch64_emit_ops_impl
=
3069 aarch64_emit_prologue
,
3070 aarch64_emit_epilogue
,
3075 aarch64_emit_rsh_signed
,
3076 aarch64_emit_rsh_unsigned
,
3078 aarch64_emit_log_not
,
3079 aarch64_emit_bit_and
,
3080 aarch64_emit_bit_or
,
3081 aarch64_emit_bit_xor
,
3082 aarch64_emit_bit_not
,
3084 aarch64_emit_less_signed
,
3085 aarch64_emit_less_unsigned
,
3087 aarch64_emit_if_goto
,
3089 aarch64_write_goto_address
,
3094 aarch64_emit_stack_flush
,
3095 aarch64_emit_zero_ext
,
3097 aarch64_emit_stack_adjust
,
3098 aarch64_emit_int_call_1
,
3099 aarch64_emit_void_call_2
,
3100 aarch64_emit_eq_goto
,
3101 aarch64_emit_ne_goto
,
3102 aarch64_emit_lt_goto
,
3103 aarch64_emit_le_goto
,
3104 aarch64_emit_gt_goto
,
3105 aarch64_emit_ge_got
,
3108 /* Implementation of target ops method "emit_ops". */
3111 aarch64_target::emit_ops ()
3113 return &aarch64_emit_ops_impl
;
3116 /* Implementation of target ops method
3117 "get_min_fast_tracepoint_insn_len". */
3120 aarch64_target::get_min_fast_tracepoint_insn_len ()
3125 /* Implementation of linux target ops method "low_supports_range_stepping". */
3128 aarch64_target::low_supports_range_stepping ()
3133 /* Implementation of target ops method "sw_breakpoint_from_kind". */
3136 aarch64_target::sw_breakpoint_from_kind (int kind
, int *size
)
3138 if (is_64bit_tdesc ())
3140 *size
= aarch64_breakpoint_len
;
3141 return aarch64_breakpoint
;
3144 return arm_sw_breakpoint_from_kind (kind
, size
);
3147 /* Implementation of target ops method "breakpoint_kind_from_pc". */
3150 aarch64_target::breakpoint_kind_from_pc (CORE_ADDR
*pcptr
)
3152 if (is_64bit_tdesc ())
3153 return aarch64_breakpoint_len
;
3155 return arm_breakpoint_kind_from_pc (pcptr
);
3158 /* Implementation of the target ops method
3159 "breakpoint_kind_from_current_state". */
3162 aarch64_target::breakpoint_kind_from_current_state (CORE_ADDR
*pcptr
)
3164 if (is_64bit_tdesc ())
3165 return aarch64_breakpoint_len
;
3167 return arm_breakpoint_kind_from_current_state (pcptr
);
3170 /* The linux target ops object. */
3172 linux_process_target
*the_linux_target
= &the_aarch64_target
;
3175 initialize_low_arch (void)
3177 initialize_low_arch_aarch32 ();
3179 initialize_regsets_info (&aarch64_regsets_info
);
3180 initialize_regsets_info (&aarch64_sve_regsets_info
);