gdb: move some commands into the tui namespace
[binutils-gdb.git] / gdbserver / linux-aarch64-low.cc
1 /* GNU/Linux/AArch64 specific low level interface, for the remote server for
2 GDB.
3
4 Copyright (C) 2009-2022 Free Software Foundation, Inc.
5 Contributed by ARM Ltd.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
22 #include "server.h"
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"
29 #include "ax.h"
30 #include "tracepoint.h"
31 #include "debug.h"
32
33 #include <signal.h>
34 #include <sys/user.h>
35 #include "nat/gdb_ptrace.h"
36 #include <asm/ptrace.h>
37 #include <inttypes.h>
38 #include <endian.h>
39 #include <sys/uio.h>
40
41 #include "gdb_proc_service.h"
42 #include "arch/aarch64.h"
43 #include "arch/aarch64-mte-linux.h"
44 #include "linux-aarch32-tdesc.h"
45 #include "linux-aarch64-tdesc.h"
46 #include "nat/aarch64-mte-linux-ptrace.h"
47 #include "nat/aarch64-sve-linux-ptrace.h"
48 #include "tdesc.h"
49
50 #ifdef HAVE_SYS_REG_H
51 #include <sys/reg.h>
52 #endif
53
54 #ifdef HAVE_GETAUXVAL
55 #include <sys/auxv.h>
56 #endif
57
58 /* Linux target op definitions for the AArch64 architecture. */
59
60 class aarch64_target : public linux_process_target
61 {
62 public:
63
64 const regs_info *get_regs_info () override;
65
66 int breakpoint_kind_from_pc (CORE_ADDR *pcptr) override;
67
68 int breakpoint_kind_from_current_state (CORE_ADDR *pcptr) override;
69
70 const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
71
72 bool supports_z_point_type (char z_type) override;
73
74 bool supports_tracepoints () override;
75
76 bool supports_fast_tracepoints () override;
77
78 int install_fast_tracepoint_jump_pad
79 (CORE_ADDR tpoint, CORE_ADDR tpaddr, CORE_ADDR collector,
80 CORE_ADDR lockaddr, ULONGEST orig_size, CORE_ADDR *jump_entry,
81 CORE_ADDR *trampoline, ULONGEST *trampoline_size,
82 unsigned char *jjump_pad_insn, ULONGEST *jjump_pad_insn_size,
83 CORE_ADDR *adjusted_insn_addr, CORE_ADDR *adjusted_insn_addr_end,
84 char *err) override;
85
86 int get_min_fast_tracepoint_insn_len () override;
87
88 struct emit_ops *emit_ops () override;
89
90 bool supports_memory_tagging () override;
91
92 bool fetch_memtags (CORE_ADDR address, size_t len,
93 gdb::byte_vector &tags, int type) override;
94
95 bool store_memtags (CORE_ADDR address, size_t len,
96 const gdb::byte_vector &tags, int type) override;
97
98 protected:
99
100 void low_arch_setup () override;
101
102 bool low_cannot_fetch_register (int regno) override;
103
104 bool low_cannot_store_register (int regno) override;
105
106 bool low_supports_breakpoints () override;
107
108 CORE_ADDR low_get_pc (regcache *regcache) override;
109
110 void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
111
112 bool low_breakpoint_at (CORE_ADDR pc) override;
113
114 int low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
115 int size, raw_breakpoint *bp) override;
116
117 int low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
118 int size, raw_breakpoint *bp) override;
119
120 bool low_stopped_by_watchpoint () override;
121
122 CORE_ADDR low_stopped_data_address () override;
123
124 bool low_siginfo_fixup (siginfo_t *native, gdb_byte *inf,
125 int direction) override;
126
127 arch_process_info *low_new_process () override;
128
129 void low_delete_process (arch_process_info *info) override;
130
131 void low_new_thread (lwp_info *) override;
132
133 void low_delete_thread (arch_lwp_info *) override;
134
135 void low_new_fork (process_info *parent, process_info *child) override;
136
137 void low_prepare_to_resume (lwp_info *lwp) override;
138
139 int low_get_thread_area (int lwpid, CORE_ADDR *addrp) override;
140
141 bool low_supports_range_stepping () override;
142
143 bool low_supports_catch_syscall () override;
144
145 void low_get_syscall_trapinfo (regcache *regcache, int *sysno) override;
146 };
147
148 /* The singleton target ops object. */
149
150 static aarch64_target the_aarch64_target;
151
152 bool
153 aarch64_target::low_cannot_fetch_register (int regno)
154 {
155 gdb_assert_not_reached ("linux target op low_cannot_fetch_register "
156 "is not implemented by the target");
157 }
158
159 bool
160 aarch64_target::low_cannot_store_register (int regno)
161 {
162 gdb_assert_not_reached ("linux target op low_cannot_store_register "
163 "is not implemented by the target");
164 }
165
166 void
167 aarch64_target::low_prepare_to_resume (lwp_info *lwp)
168 {
169 aarch64_linux_prepare_to_resume (lwp);
170 }
171
172 /* Per-process arch-specific data we want to keep. */
173
174 struct arch_process_info
175 {
176 /* Hardware breakpoint/watchpoint data.
177 The reason for them to be per-process rather than per-thread is
178 due to the lack of information in the gdbserver environment;
179 gdbserver is not told that whether a requested hardware
180 breakpoint/watchpoint is thread specific or not, so it has to set
181 each hw bp/wp for every thread in the current process. The
182 higher level bp/wp management in gdb will resume a thread if a hw
183 bp/wp trap is not expected for it. Since the hw bp/wp setting is
184 same for each thread, it is reasonable for the data to live here.
185 */
186 struct aarch64_debug_reg_state debug_reg_state;
187 };
188
189 /* Return true if the size of register 0 is 8 byte. */
190
191 static int
192 is_64bit_tdesc (void)
193 {
194 struct regcache *regcache = get_thread_regcache (current_thread, 0);
195
196 return register_size (regcache->tdesc, 0) == 8;
197 }
198
199 static void
200 aarch64_fill_gregset (struct regcache *regcache, void *buf)
201 {
202 struct user_pt_regs *regset = (struct user_pt_regs *) buf;
203 int i;
204
205 for (i = 0; i < AARCH64_X_REGS_NUM; i++)
206 collect_register (regcache, AARCH64_X0_REGNUM + i, &regset->regs[i]);
207 collect_register (regcache, AARCH64_SP_REGNUM, &regset->sp);
208 collect_register (regcache, AARCH64_PC_REGNUM, &regset->pc);
209 collect_register (regcache, AARCH64_CPSR_REGNUM, &regset->pstate);
210 }
211
212 static void
213 aarch64_store_gregset (struct regcache *regcache, const void *buf)
214 {
215 const struct user_pt_regs *regset = (const struct user_pt_regs *) buf;
216 int i;
217
218 for (i = 0; i < AARCH64_X_REGS_NUM; i++)
219 supply_register (regcache, AARCH64_X0_REGNUM + i, &regset->regs[i]);
220 supply_register (regcache, AARCH64_SP_REGNUM, &regset->sp);
221 supply_register (regcache, AARCH64_PC_REGNUM, &regset->pc);
222 supply_register (regcache, AARCH64_CPSR_REGNUM, &regset->pstate);
223 }
224
225 static void
226 aarch64_fill_fpregset (struct regcache *regcache, void *buf)
227 {
228 struct user_fpsimd_state *regset = (struct user_fpsimd_state *) buf;
229 int i;
230
231 for (i = 0; i < AARCH64_V_REGS_NUM; i++)
232 collect_register (regcache, AARCH64_V0_REGNUM + i, &regset->vregs[i]);
233 collect_register (regcache, AARCH64_FPSR_REGNUM, &regset->fpsr);
234 collect_register (regcache, AARCH64_FPCR_REGNUM, &regset->fpcr);
235 }
236
237 static void
238 aarch64_store_fpregset (struct regcache *regcache, const void *buf)
239 {
240 const struct user_fpsimd_state *regset
241 = (const struct user_fpsimd_state *) buf;
242 int i;
243
244 for (i = 0; i < AARCH64_V_REGS_NUM; i++)
245 supply_register (regcache, AARCH64_V0_REGNUM + i, &regset->vregs[i]);
246 supply_register (regcache, AARCH64_FPSR_REGNUM, &regset->fpsr);
247 supply_register (regcache, AARCH64_FPCR_REGNUM, &regset->fpcr);
248 }
249
250 /* Store the pauth registers to regcache. */
251
252 static void
253 aarch64_store_pauthregset (struct regcache *regcache, const void *buf)
254 {
255 uint64_t *pauth_regset = (uint64_t *) buf;
256 int pauth_base = find_regno (regcache->tdesc, "pauth_dmask");
257
258 if (pauth_base == 0)
259 return;
260
261 supply_register (regcache, AARCH64_PAUTH_DMASK_REGNUM (pauth_base),
262 &pauth_regset[0]);
263 supply_register (regcache, AARCH64_PAUTH_CMASK_REGNUM (pauth_base),
264 &pauth_regset[1]);
265 }
266
267 /* Fill BUF with the MTE registers from the regcache. */
268
269 static void
270 aarch64_fill_mteregset (struct regcache *regcache, void *buf)
271 {
272 uint64_t *mte_regset = (uint64_t *) buf;
273 int mte_base = find_regno (regcache->tdesc, "tag_ctl");
274
275 collect_register (regcache, mte_base, mte_regset);
276 }
277
278 /* Store the MTE registers to regcache. */
279
280 static void
281 aarch64_store_mteregset (struct regcache *regcache, const void *buf)
282 {
283 uint64_t *mte_regset = (uint64_t *) buf;
284 int mte_base = find_regno (regcache->tdesc, "tag_ctl");
285
286 /* Tag Control register */
287 supply_register (regcache, mte_base, mte_regset);
288 }
289
290 bool
291 aarch64_target::low_supports_breakpoints ()
292 {
293 return true;
294 }
295
296 /* Implementation of linux target ops method "low_get_pc". */
297
298 CORE_ADDR
299 aarch64_target::low_get_pc (regcache *regcache)
300 {
301 if (register_size (regcache->tdesc, 0) == 8)
302 return linux_get_pc_64bit (regcache);
303 else
304 return linux_get_pc_32bit (regcache);
305 }
306
307 /* Implementation of linux target ops method "low_set_pc". */
308
309 void
310 aarch64_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
311 {
312 if (register_size (regcache->tdesc, 0) == 8)
313 linux_set_pc_64bit (regcache, pc);
314 else
315 linux_set_pc_32bit (regcache, pc);
316 }
317
318 #define aarch64_breakpoint_len 4
319
320 /* AArch64 BRK software debug mode instruction.
321 This instruction needs to match gdb/aarch64-tdep.c
322 (aarch64_default_breakpoint). */
323 static const gdb_byte aarch64_breakpoint[] = {0x00, 0x00, 0x20, 0xd4};
324
325 /* Implementation of linux target ops method "low_breakpoint_at". */
326
327 bool
328 aarch64_target::low_breakpoint_at (CORE_ADDR where)
329 {
330 if (is_64bit_tdesc ())
331 {
332 gdb_byte insn[aarch64_breakpoint_len];
333
334 read_memory (where, (unsigned char *) &insn, aarch64_breakpoint_len);
335 if (memcmp (insn, aarch64_breakpoint, aarch64_breakpoint_len) == 0)
336 return true;
337
338 return false;
339 }
340 else
341 return arm_breakpoint_at (where);
342 }
343
344 static void
345 aarch64_init_debug_reg_state (struct aarch64_debug_reg_state *state)
346 {
347 int i;
348
349 for (i = 0; i < AARCH64_HBP_MAX_NUM; ++i)
350 {
351 state->dr_addr_bp[i] = 0;
352 state->dr_ctrl_bp[i] = 0;
353 state->dr_ref_count_bp[i] = 0;
354 }
355
356 for (i = 0; i < AARCH64_HWP_MAX_NUM; ++i)
357 {
358 state->dr_addr_wp[i] = 0;
359 state->dr_ctrl_wp[i] = 0;
360 state->dr_ref_count_wp[i] = 0;
361 }
362 }
363
364 /* Return the pointer to the debug register state structure in the
365 current process' arch-specific data area. */
366
367 struct aarch64_debug_reg_state *
368 aarch64_get_debug_reg_state (pid_t pid)
369 {
370 struct process_info *proc = find_process_pid (pid);
371
372 return &proc->priv->arch_private->debug_reg_state;
373 }
374
375 /* Implementation of target ops method "supports_z_point_type". */
376
377 bool
378 aarch64_target::supports_z_point_type (char z_type)
379 {
380 switch (z_type)
381 {
382 case Z_PACKET_SW_BP:
383 case Z_PACKET_HW_BP:
384 case Z_PACKET_WRITE_WP:
385 case Z_PACKET_READ_WP:
386 case Z_PACKET_ACCESS_WP:
387 return true;
388 default:
389 return false;
390 }
391 }
392
393 /* Implementation of linux target ops method "low_insert_point".
394
395 It actually only records the info of the to-be-inserted bp/wp;
396 the actual insertion will happen when threads are resumed. */
397
398 int
399 aarch64_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
400 int len, raw_breakpoint *bp)
401 {
402 int ret;
403 enum target_hw_bp_type targ_type;
404 struct aarch64_debug_reg_state *state
405 = aarch64_get_debug_reg_state (pid_of (current_thread));
406
407 if (show_debug_regs)
408 fprintf (stderr, "insert_point on entry (addr=0x%08lx, len=%d)\n",
409 (unsigned long) addr, len);
410
411 /* Determine the type from the raw breakpoint type. */
412 targ_type = raw_bkpt_type_to_target_hw_bp_type (type);
413
414 if (targ_type != hw_execute)
415 {
416 if (aarch64_region_ok_for_watchpoint (addr, len))
417 ret = aarch64_handle_watchpoint (targ_type, addr, len,
418 1 /* is_insert */,
419 current_lwp_ptid (), state);
420 else
421 ret = -1;
422 }
423 else
424 {
425 if (len == 3)
426 {
427 /* LEN is 3 means the breakpoint is set on a 32-bit thumb
428 instruction. Set it to 2 to correctly encode length bit
429 mask in hardware/watchpoint control register. */
430 len = 2;
431 }
432 ret = aarch64_handle_breakpoint (targ_type, addr, len,
433 1 /* is_insert */, current_lwp_ptid (),
434 state);
435 }
436
437 if (show_debug_regs)
438 aarch64_show_debug_reg_state (state, "insert_point", addr, len,
439 targ_type);
440
441 return ret;
442 }
443
444 /* Implementation of linux target ops method "low_remove_point".
445
446 It actually only records the info of the to-be-removed bp/wp,
447 the actual removal will be done when threads are resumed. */
448
449 int
450 aarch64_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
451 int len, raw_breakpoint *bp)
452 {
453 int ret;
454 enum target_hw_bp_type targ_type;
455 struct aarch64_debug_reg_state *state
456 = aarch64_get_debug_reg_state (pid_of (current_thread));
457
458 if (show_debug_regs)
459 fprintf (stderr, "remove_point on entry (addr=0x%08lx, len=%d)\n",
460 (unsigned long) addr, len);
461
462 /* Determine the type from the raw breakpoint type. */
463 targ_type = raw_bkpt_type_to_target_hw_bp_type (type);
464
465 /* Set up state pointers. */
466 if (targ_type != hw_execute)
467 ret =
468 aarch64_handle_watchpoint (targ_type, addr, len, 0 /* is_insert */,
469 current_lwp_ptid (), state);
470 else
471 {
472 if (len == 3)
473 {
474 /* LEN is 3 means the breakpoint is set on a 32-bit thumb
475 instruction. Set it to 2 to correctly encode length bit
476 mask in hardware/watchpoint control register. */
477 len = 2;
478 }
479 ret = aarch64_handle_breakpoint (targ_type, addr, len,
480 0 /* is_insert */, current_lwp_ptid (),
481 state);
482 }
483
484 if (show_debug_regs)
485 aarch64_show_debug_reg_state (state, "remove_point", addr, len,
486 targ_type);
487
488 return ret;
489 }
490
491 /* Return the address only having significant bits. This is used to ignore
492 the top byte (TBI). */
493
494 static CORE_ADDR
495 address_significant (CORE_ADDR addr)
496 {
497 /* Clear insignificant bits of a target address and sign extend resulting
498 address. */
499 int addr_bit = 56;
500
501 CORE_ADDR sign = (CORE_ADDR) 1 << (addr_bit - 1);
502 addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
503 addr = (addr ^ sign) - sign;
504
505 return addr;
506 }
507
508 /* Implementation of linux target ops method "low_stopped_data_address". */
509
510 CORE_ADDR
511 aarch64_target::low_stopped_data_address ()
512 {
513 siginfo_t siginfo;
514 int pid, i;
515 struct aarch64_debug_reg_state *state;
516
517 pid = lwpid_of (current_thread);
518
519 /* Get the siginfo. */
520 if (ptrace (PTRACE_GETSIGINFO, pid, NULL, &siginfo) != 0)
521 return (CORE_ADDR) 0;
522
523 /* Need to be a hardware breakpoint/watchpoint trap. */
524 if (siginfo.si_signo != SIGTRAP
525 || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
526 return (CORE_ADDR) 0;
527
528 /* Make sure to ignore the top byte, otherwise we may not recognize a
529 hardware watchpoint hit. The stopped data addresses coming from the
530 kernel can potentially be tagged addresses. */
531 const CORE_ADDR addr_trap
532 = address_significant ((CORE_ADDR) siginfo.si_addr);
533
534 /* Check if the address matches any watched address. */
535 state = aarch64_get_debug_reg_state (pid_of (current_thread));
536 for (i = aarch64_num_wp_regs - 1; i >= 0; --i)
537 {
538 const unsigned int offset
539 = aarch64_watchpoint_offset (state->dr_ctrl_wp[i]);
540 const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]);
541 const CORE_ADDR addr_watch = state->dr_addr_wp[i] + offset;
542 const CORE_ADDR addr_watch_aligned = align_down (state->dr_addr_wp[i], 8);
543 const CORE_ADDR addr_orig = state->dr_addr_orig_wp[i];
544
545 if (state->dr_ref_count_wp[i]
546 && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i])
547 && addr_trap >= addr_watch_aligned
548 && addr_trap < addr_watch + len)
549 {
550 /* ADDR_TRAP reports the first address of the memory range
551 accessed by the CPU, regardless of what was the memory
552 range watched. Thus, a large CPU access that straddles
553 the ADDR_WATCH..ADDR_WATCH+LEN range may result in an
554 ADDR_TRAP that is lower than the
555 ADDR_WATCH..ADDR_WATCH+LEN range. E.g.:
556
557 addr: | 4 | 5 | 6 | 7 | 8 |
558 |---- range watched ----|
559 |----------- range accessed ------------|
560
561 In this case, ADDR_TRAP will be 4.
562
563 To match a watchpoint known to GDB core, we must never
564 report *ADDR_P outside of any ADDR_WATCH..ADDR_WATCH+LEN
565 range. ADDR_WATCH <= ADDR_TRAP < ADDR_ORIG is a false
566 positive on kernels older than 4.10. See PR
567 external/20207. */
568 return addr_orig;
569 }
570 }
571
572 return (CORE_ADDR) 0;
573 }
574
575 /* Implementation of linux target ops method "low_stopped_by_watchpoint". */
576
577 bool
578 aarch64_target::low_stopped_by_watchpoint ()
579 {
580 return (low_stopped_data_address () != 0);
581 }
582
583 /* Fetch the thread-local storage pointer for libthread_db. */
584
585 ps_err_e
586 ps_get_thread_area (struct ps_prochandle *ph,
587 lwpid_t lwpid, int idx, void **base)
588 {
589 return aarch64_ps_get_thread_area (ph, lwpid, idx, base,
590 is_64bit_tdesc ());
591 }
592
593 /* Implementation of linux target ops method "low_siginfo_fixup". */
594
595 bool
596 aarch64_target::low_siginfo_fixup (siginfo_t *native, gdb_byte *inf,
597 int direction)
598 {
599 /* Is the inferior 32-bit? If so, then fixup the siginfo object. */
600 if (!is_64bit_tdesc ())
601 {
602 if (direction == 0)
603 aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo *) inf,
604 native);
605 else
606 aarch64_siginfo_from_compat_siginfo (native,
607 (struct compat_siginfo *) inf);
608
609 return true;
610 }
611
612 return false;
613 }
614
615 /* Implementation of linux target ops method "low_new_process". */
616
617 arch_process_info *
618 aarch64_target::low_new_process ()
619 {
620 struct arch_process_info *info = XCNEW (struct arch_process_info);
621
622 aarch64_init_debug_reg_state (&info->debug_reg_state);
623
624 return info;
625 }
626
627 /* Implementation of linux target ops method "low_delete_process". */
628
629 void
630 aarch64_target::low_delete_process (arch_process_info *info)
631 {
632 xfree (info);
633 }
634
635 void
636 aarch64_target::low_new_thread (lwp_info *lwp)
637 {
638 aarch64_linux_new_thread (lwp);
639 }
640
641 void
642 aarch64_target::low_delete_thread (arch_lwp_info *arch_lwp)
643 {
644 aarch64_linux_delete_thread (arch_lwp);
645 }
646
647 /* Implementation of linux target ops method "low_new_fork". */
648
649 void
650 aarch64_target::low_new_fork (process_info *parent,
651 process_info *child)
652 {
653 /* These are allocated by linux_add_process. */
654 gdb_assert (parent->priv != NULL
655 && parent->priv->arch_private != NULL);
656 gdb_assert (child->priv != NULL
657 && child->priv->arch_private != NULL);
658
659 /* Linux kernel before 2.6.33 commit
660 72f674d203cd230426437cdcf7dd6f681dad8b0d
661 will inherit hardware debug registers from parent
662 on fork/vfork/clone. Newer Linux kernels create such tasks with
663 zeroed debug registers.
664
665 GDB core assumes the child inherits the watchpoints/hw
666 breakpoints of the parent, and will remove them all from the
667 forked off process. Copy the debug registers mirrors into the
668 new process so that all breakpoints and watchpoints can be
669 removed together. The debug registers mirror will become zeroed
670 in the end before detaching the forked off process, thus making
671 this compatible with older Linux kernels too. */
672
673 *child->priv->arch_private = *parent->priv->arch_private;
674 }
675
676 /* Wrapper for aarch64_sve_regs_copy_to_reg_buf. */
677
678 static void
679 aarch64_sve_regs_copy_to_regcache (struct regcache *regcache, const void *buf)
680 {
681 return aarch64_sve_regs_copy_to_reg_buf (regcache, buf);
682 }
683
684 /* Wrapper for aarch64_sve_regs_copy_from_reg_buf. */
685
686 static void
687 aarch64_sve_regs_copy_from_regcache (struct regcache *regcache, void *buf)
688 {
689 return aarch64_sve_regs_copy_from_reg_buf (regcache, buf);
690 }
691
692 /* Array containing all the possible register sets for AArch64/Linux. During
693 architecture setup, these will be checked against the HWCAP/HWCAP2 bits for
694 validity and enabled/disabled accordingly.
695
696 Their sizes are set to 0 here, but they will be adjusted later depending
697 on whether each register set is available or not. */
698 static struct regset_info aarch64_regsets[] =
699 {
700 /* GPR registers. */
701 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
702 0, GENERAL_REGS,
703 aarch64_fill_gregset, aarch64_store_gregset },
704 /* Floating Point (FPU) registers. */
705 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET,
706 0, FP_REGS,
707 aarch64_fill_fpregset, aarch64_store_fpregset
708 },
709 /* Scalable Vector Extension (SVE) registers. */
710 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_ARM_SVE,
711 0, EXTENDED_REGS,
712 aarch64_sve_regs_copy_from_regcache, aarch64_sve_regs_copy_to_regcache
713 },
714 /* PAC registers. */
715 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_ARM_PAC_MASK,
716 0, OPTIONAL_REGS,
717 nullptr, aarch64_store_pauthregset },
718 /* Tagged address control / MTE registers. */
719 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_ARM_TAGGED_ADDR_CTRL,
720 0, OPTIONAL_REGS,
721 aarch64_fill_mteregset, aarch64_store_mteregset },
722 NULL_REGSET
723 };
724
725 static struct regsets_info aarch64_regsets_info =
726 {
727 aarch64_regsets, /* regsets */
728 0, /* num_regsets */
729 nullptr, /* disabled_regsets */
730 };
731
732 static struct regs_info regs_info_aarch64 =
733 {
734 nullptr, /* regset_bitmap */
735 nullptr, /* usrregs */
736 &aarch64_regsets_info,
737 };
738
739 /* Given FEATURES, adjust the available register sets by setting their
740 sizes. A size of 0 means the register set is disabled and won't be
741 used. */
742
743 static void
744 aarch64_adjust_register_sets (const struct aarch64_features &features)
745 {
746 struct regset_info *regset;
747
748 for (regset = aarch64_regsets; regset->size >= 0; regset++)
749 {
750 switch (regset->nt_type)
751 {
752 case NT_PRSTATUS:
753 /* General purpose registers are always present. */
754 regset->size = sizeof (struct user_pt_regs);
755 break;
756 case NT_FPREGSET:
757 /* This is unavailable when SVE is present. */
758 if (!features.sve)
759 regset->size = sizeof (struct user_fpsimd_state);
760 break;
761 case NT_ARM_SVE:
762 if (features.sve)
763 regset->size = SVE_PT_SIZE (AARCH64_MAX_SVE_VQ, SVE_PT_REGS_SVE);
764 break;
765 case NT_ARM_PAC_MASK:
766 if (features.pauth)
767 regset->size = AARCH64_PAUTH_REGS_SIZE;
768 break;
769 case NT_ARM_TAGGED_ADDR_CTRL:
770 if (features.mte)
771 regset->size = AARCH64_LINUX_SIZEOF_MTE;
772 break;
773 default:
774 gdb_assert_not_reached ("Unknown register set found.");
775 }
776 }
777 }
778
779 /* Matches HWCAP_PACA in kernel header arch/arm64/include/uapi/asm/hwcap.h. */
780 #define AARCH64_HWCAP_PACA (1 << 30)
781
782 /* Implementation of linux target ops method "low_arch_setup". */
783
784 void
785 aarch64_target::low_arch_setup ()
786 {
787 unsigned int machine;
788 int is_elf64;
789 int tid;
790
791 tid = lwpid_of (current_thread);
792
793 is_elf64 = linux_pid_exe_is_elf_64_file (tid, &machine);
794
795 if (is_elf64)
796 {
797 struct aarch64_features features;
798
799 uint64_t vq = aarch64_sve_get_vq (tid);
800 features.sve = (vq > 0);
801 /* A-profile PAC is 64-bit only. */
802 features.pauth = linux_get_hwcap (8) & AARCH64_HWCAP_PACA;
803 /* A-profile MTE is 64-bit only. */
804 features.mte = linux_get_hwcap2 (8) & HWCAP2_MTE;
805
806 current_process ()->tdesc
807 = aarch64_linux_read_description (vq, features.pauth, features.mte);
808
809 /* Adjust the register sets we should use for this particular set of
810 features. */
811 aarch64_adjust_register_sets (features);
812 }
813 else
814 current_process ()->tdesc = aarch32_linux_read_description ();
815
816 aarch64_linux_get_debug_reg_capacity (lwpid_of (current_thread));
817 }
818
819 /* Implementation of linux target ops method "get_regs_info". */
820
821 const regs_info *
822 aarch64_target::get_regs_info ()
823 {
824 if (!is_64bit_tdesc ())
825 return &regs_info_aarch32;
826
827 /* AArch64 64-bit registers. */
828 return &regs_info_aarch64;
829 }
830
831 /* Implementation of target ops method "supports_tracepoints". */
832
833 bool
834 aarch64_target::supports_tracepoints ()
835 {
836 if (current_thread == NULL)
837 return true;
838 else
839 {
840 /* We don't support tracepoints on aarch32 now. */
841 return is_64bit_tdesc ();
842 }
843 }
844
845 /* Implementation of linux target ops method "low_get_thread_area". */
846
847 int
848 aarch64_target::low_get_thread_area (int lwpid, CORE_ADDR *addrp)
849 {
850 struct iovec iovec;
851 uint64_t reg;
852
853 iovec.iov_base = &reg;
854 iovec.iov_len = sizeof (reg);
855
856 if (ptrace (PTRACE_GETREGSET, lwpid, NT_ARM_TLS, &iovec) != 0)
857 return -1;
858
859 *addrp = reg;
860
861 return 0;
862 }
863
864 bool
865 aarch64_target::low_supports_catch_syscall ()
866 {
867 return true;
868 }
869
870 /* Implementation of linux target ops method "low_get_syscall_trapinfo". */
871
872 void
873 aarch64_target::low_get_syscall_trapinfo (regcache *regcache, int *sysno)
874 {
875 int use_64bit = register_size (regcache->tdesc, 0) == 8;
876
877 if (use_64bit)
878 {
879 long l_sysno;
880
881 collect_register_by_name (regcache, "x8", &l_sysno);
882 *sysno = (int) l_sysno;
883 }
884 else
885 collect_register_by_name (regcache, "r7", sysno);
886 }
887
888 /* List of condition codes that we need. */
889
890 enum aarch64_condition_codes
891 {
892 EQ = 0x0,
893 NE = 0x1,
894 LO = 0x3,
895 GE = 0xa,
896 LT = 0xb,
897 GT = 0xc,
898 LE = 0xd,
899 };
900
901 enum aarch64_operand_type
902 {
903 OPERAND_IMMEDIATE,
904 OPERAND_REGISTER,
905 };
906
907 /* Representation of an operand. At this time, it only supports register
908 and immediate types. */
909
910 struct aarch64_operand
911 {
912 /* Type of the operand. */
913 enum aarch64_operand_type type;
914
915 /* Value of the operand according to the type. */
916 union
917 {
918 uint32_t imm;
919 struct aarch64_register reg;
920 };
921 };
922
923 /* List of registers that we are currently using, we can add more here as
924 we need to use them. */
925
926 /* General purpose scratch registers (64 bit). */
927 static const struct aarch64_register x0 = { 0, 1 };
928 static const struct aarch64_register x1 = { 1, 1 };
929 static const struct aarch64_register x2 = { 2, 1 };
930 static const struct aarch64_register x3 = { 3, 1 };
931 static const struct aarch64_register x4 = { 4, 1 };
932
933 /* General purpose scratch registers (32 bit). */
934 static const struct aarch64_register w0 = { 0, 0 };
935 static const struct aarch64_register w2 = { 2, 0 };
936
937 /* Intra-procedure scratch registers. */
938 static const struct aarch64_register ip0 = { 16, 1 };
939
940 /* Special purpose registers. */
941 static const struct aarch64_register fp = { 29, 1 };
942 static const struct aarch64_register lr = { 30, 1 };
943 static const struct aarch64_register sp = { 31, 1 };
944 static const struct aarch64_register xzr = { 31, 1 };
945
946 /* Dynamically allocate a new register. If we know the register
947 statically, we should make it a global as above instead of using this
948 helper function. */
949
950 static struct aarch64_register
951 aarch64_register (unsigned num, int is64)
952 {
953 return (struct aarch64_register) { num, is64 };
954 }
955
956 /* Helper function to create a register operand, for instructions with
957 different types of operands.
958
959 For example:
960 p += emit_mov (p, x0, register_operand (x1)); */
961
962 static struct aarch64_operand
963 register_operand (struct aarch64_register reg)
964 {
965 struct aarch64_operand operand;
966
967 operand.type = OPERAND_REGISTER;
968 operand.reg = reg;
969
970 return operand;
971 }
972
973 /* Helper function to create an immediate operand, for instructions with
974 different types of operands.
975
976 For example:
977 p += emit_mov (p, x0, immediate_operand (12)); */
978
979 static struct aarch64_operand
980 immediate_operand (uint32_t imm)
981 {
982 struct aarch64_operand operand;
983
984 operand.type = OPERAND_IMMEDIATE;
985 operand.imm = imm;
986
987 return operand;
988 }
989
990 /* Helper function to create an offset memory operand.
991
992 For example:
993 p += emit_ldr (p, x0, sp, offset_memory_operand (16)); */
994
995 static struct aarch64_memory_operand
996 offset_memory_operand (int32_t offset)
997 {
998 return (struct aarch64_memory_operand) { MEMORY_OPERAND_OFFSET, offset };
999 }
1000
1001 /* Helper function to create a pre-index memory operand.
1002
1003 For example:
1004 p += emit_ldr (p, x0, sp, preindex_memory_operand (16)); */
1005
1006 static struct aarch64_memory_operand
1007 preindex_memory_operand (int32_t index)
1008 {
1009 return (struct aarch64_memory_operand) { MEMORY_OPERAND_PREINDEX, index };
1010 }
1011
1012 /* Helper function to create a post-index memory operand.
1013
1014 For example:
1015 p += emit_ldr (p, x0, sp, postindex_memory_operand (16)); */
1016
1017 static struct aarch64_memory_operand
1018 postindex_memory_operand (int32_t index)
1019 {
1020 return (struct aarch64_memory_operand) { MEMORY_OPERAND_POSTINDEX, index };
1021 }
1022
1023 /* System control registers. These special registers can be written and
1024 read with the MRS and MSR instructions.
1025
1026 - NZCV: Condition flags. GDB refers to this register under the CPSR
1027 name.
1028 - FPSR: Floating-point status register.
1029 - FPCR: Floating-point control registers.
1030 - TPIDR_EL0: Software thread ID register. */
1031
1032 enum aarch64_system_control_registers
1033 {
1034 /* op0 op1 crn crm op2 */
1035 NZCV = (0x1 << 14) | (0x3 << 11) | (0x4 << 7) | (0x2 << 3) | 0x0,
1036 FPSR = (0x1 << 14) | (0x3 << 11) | (0x4 << 7) | (0x4 << 3) | 0x1,
1037 FPCR = (0x1 << 14) | (0x3 << 11) | (0x4 << 7) | (0x4 << 3) | 0x0,
1038 TPIDR_EL0 = (0x1 << 14) | (0x3 << 11) | (0xd << 7) | (0x0 << 3) | 0x2
1039 };
1040
1041 /* Write a BLR instruction into *BUF.
1042
1043 BLR rn
1044
1045 RN is the register to branch to. */
1046
1047 static int
1048 emit_blr (uint32_t *buf, struct aarch64_register rn)
1049 {
1050 return aarch64_emit_insn (buf, BLR | ENCODE (rn.num, 5, 5));
1051 }
1052
1053 /* Write a RET instruction into *BUF.
1054
1055 RET xn
1056
1057 RN is the register to branch to. */
1058
1059 static int
1060 emit_ret (uint32_t *buf, struct aarch64_register rn)
1061 {
1062 return aarch64_emit_insn (buf, RET | ENCODE (rn.num, 5, 5));
1063 }
1064
1065 static int
1066 emit_load_store_pair (uint32_t *buf, enum aarch64_opcodes opcode,
1067 struct aarch64_register rt,
1068 struct aarch64_register rt2,
1069 struct aarch64_register rn,
1070 struct aarch64_memory_operand operand)
1071 {
1072 uint32_t opc;
1073 uint32_t pre_index;
1074 uint32_t write_back;
1075
1076 if (rt.is64)
1077 opc = ENCODE (2, 2, 30);
1078 else
1079 opc = ENCODE (0, 2, 30);
1080
1081 switch (operand.type)
1082 {
1083 case MEMORY_OPERAND_OFFSET:
1084 {
1085 pre_index = ENCODE (1, 1, 24);
1086 write_back = ENCODE (0, 1, 23);
1087 break;
1088 }
1089 case MEMORY_OPERAND_POSTINDEX:
1090 {
1091 pre_index = ENCODE (0, 1, 24);
1092 write_back = ENCODE (1, 1, 23);
1093 break;
1094 }
1095 case MEMORY_OPERAND_PREINDEX:
1096 {
1097 pre_index = ENCODE (1, 1, 24);
1098 write_back = ENCODE (1, 1, 23);
1099 break;
1100 }
1101 default:
1102 return 0;
1103 }
1104
1105 return aarch64_emit_insn (buf, opcode | opc | pre_index | write_back
1106 | ENCODE (operand.index >> 3, 7, 15)
1107 | ENCODE (rt2.num, 5, 10)
1108 | ENCODE (rn.num, 5, 5) | ENCODE (rt.num, 5, 0));
1109 }
1110
1111 /* Write a STP instruction into *BUF.
1112
1113 STP rt, rt2, [rn, #offset]
1114 STP rt, rt2, [rn, #index]!
1115 STP rt, rt2, [rn], #index
1116
1117 RT and RT2 are the registers to store.
1118 RN is the base address register.
1119 OFFSET is the immediate to add to the base address. It is limited to a
1120 -512 .. 504 range (7 bits << 3). */
1121
1122 static int
1123 emit_stp (uint32_t *buf, struct aarch64_register rt,
1124 struct aarch64_register rt2, struct aarch64_register rn,
1125 struct aarch64_memory_operand operand)
1126 {
1127 return emit_load_store_pair (buf, STP, rt, rt2, rn, operand);
1128 }
1129
1130 /* Write a LDP instruction into *BUF.
1131
1132 LDP rt, rt2, [rn, #offset]
1133 LDP rt, rt2, [rn, #index]!
1134 LDP rt, rt2, [rn], #index
1135
1136 RT and RT2 are the registers to store.
1137 RN is the base address register.
1138 OFFSET is the immediate to add to the base address. It is limited to a
1139 -512 .. 504 range (7 bits << 3). */
1140
1141 static int
1142 emit_ldp (uint32_t *buf, struct aarch64_register rt,
1143 struct aarch64_register rt2, struct aarch64_register rn,
1144 struct aarch64_memory_operand operand)
1145 {
1146 return emit_load_store_pair (buf, LDP, rt, rt2, rn, operand);
1147 }
1148
1149 /* Write a LDP (SIMD&VFP) instruction using Q registers into *BUF.
1150
1151 LDP qt, qt2, [rn, #offset]
1152
1153 RT and RT2 are the Q registers to store.
1154 RN is the base address register.
1155 OFFSET is the immediate to add to the base address. It is limited to
1156 -1024 .. 1008 range (7 bits << 4). */
1157
1158 static int
1159 emit_ldp_q_offset (uint32_t *buf, unsigned rt, unsigned rt2,
1160 struct aarch64_register rn, int32_t offset)
1161 {
1162 uint32_t opc = ENCODE (2, 2, 30);
1163 uint32_t pre_index = ENCODE (1, 1, 24);
1164
1165 return aarch64_emit_insn (buf, LDP_SIMD_VFP | opc | pre_index
1166 | ENCODE (offset >> 4, 7, 15)
1167 | ENCODE (rt2, 5, 10)
1168 | ENCODE (rn.num, 5, 5) | ENCODE (rt, 5, 0));
1169 }
1170
1171 /* Write a STP (SIMD&VFP) instruction using Q registers into *BUF.
1172
1173 STP qt, qt2, [rn, #offset]
1174
1175 RT and RT2 are the Q registers to store.
1176 RN is the base address register.
1177 OFFSET is the immediate to add to the base address. It is limited to
1178 -1024 .. 1008 range (7 bits << 4). */
1179
1180 static int
1181 emit_stp_q_offset (uint32_t *buf, unsigned rt, unsigned rt2,
1182 struct aarch64_register rn, int32_t offset)
1183 {
1184 uint32_t opc = ENCODE (2, 2, 30);
1185 uint32_t pre_index = ENCODE (1, 1, 24);
1186
1187 return aarch64_emit_insn (buf, STP_SIMD_VFP | opc | pre_index
1188 | ENCODE (offset >> 4, 7, 15)
1189 | ENCODE (rt2, 5, 10)
1190 | ENCODE (rn.num, 5, 5) | ENCODE (rt, 5, 0));
1191 }
1192
1193 /* Write a LDRH instruction into *BUF.
1194
1195 LDRH wt, [xn, #offset]
1196 LDRH wt, [xn, #index]!
1197 LDRH wt, [xn], #index
1198
1199 RT is the register to store.
1200 RN is the base address register.
1201 OFFSET is the immediate to add to the base address. It is limited to
1202 0 .. 32760 range (12 bits << 3). */
1203
1204 static int
1205 emit_ldrh (uint32_t *buf, struct aarch64_register rt,
1206 struct aarch64_register rn,
1207 struct aarch64_memory_operand operand)
1208 {
1209 return aarch64_emit_load_store (buf, 1, LDR, rt, rn, operand);
1210 }
1211
1212 /* Write a LDRB instruction into *BUF.
1213
1214 LDRB wt, [xn, #offset]
1215 LDRB wt, [xn, #index]!
1216 LDRB wt, [xn], #index
1217
1218 RT is the register to store.
1219 RN is the base address register.
1220 OFFSET is the immediate to add to the base address. It is limited to
1221 0 .. 32760 range (12 bits << 3). */
1222
1223 static int
1224 emit_ldrb (uint32_t *buf, struct aarch64_register rt,
1225 struct aarch64_register rn,
1226 struct aarch64_memory_operand operand)
1227 {
1228 return aarch64_emit_load_store (buf, 0, LDR, rt, rn, operand);
1229 }
1230
1231
1232
1233 /* Write a STR instruction into *BUF.
1234
1235 STR rt, [rn, #offset]
1236 STR rt, [rn, #index]!
1237 STR rt, [rn], #index
1238
1239 RT is the register to store.
1240 RN is the base address register.
1241 OFFSET is the immediate to add to the base address. It is limited to
1242 0 .. 32760 range (12 bits << 3). */
1243
1244 static int
1245 emit_str (uint32_t *buf, struct aarch64_register rt,
1246 struct aarch64_register rn,
1247 struct aarch64_memory_operand operand)
1248 {
1249 return aarch64_emit_load_store (buf, rt.is64 ? 3 : 2, STR, rt, rn, operand);
1250 }
1251
1252 /* Helper function emitting an exclusive load or store instruction. */
1253
1254 static int
1255 emit_load_store_exclusive (uint32_t *buf, uint32_t size,
1256 enum aarch64_opcodes opcode,
1257 struct aarch64_register rs,
1258 struct aarch64_register rt,
1259 struct aarch64_register rt2,
1260 struct aarch64_register rn)
1261 {
1262 return aarch64_emit_insn (buf, opcode | ENCODE (size, 2, 30)
1263 | ENCODE (rs.num, 5, 16) | ENCODE (rt2.num, 5, 10)
1264 | ENCODE (rn.num, 5, 5) | ENCODE (rt.num, 5, 0));
1265 }
1266
1267 /* Write a LAXR instruction into *BUF.
1268
1269 LDAXR rt, [xn]
1270
1271 RT is the destination register.
1272 RN is the base address register. */
1273
1274 static int
1275 emit_ldaxr (uint32_t *buf, struct aarch64_register rt,
1276 struct aarch64_register rn)
1277 {
1278 return emit_load_store_exclusive (buf, rt.is64 ? 3 : 2, LDAXR, xzr, rt,
1279 xzr, rn);
1280 }
1281
1282 /* Write a STXR instruction into *BUF.
1283
1284 STXR ws, rt, [xn]
1285
1286 RS is the result register, it indicates if the store succeeded or not.
1287 RT is the destination register.
1288 RN is the base address register. */
1289
1290 static int
1291 emit_stxr (uint32_t *buf, struct aarch64_register rs,
1292 struct aarch64_register rt, struct aarch64_register rn)
1293 {
1294 return emit_load_store_exclusive (buf, rt.is64 ? 3 : 2, STXR, rs, rt,
1295 xzr, rn);
1296 }
1297
1298 /* Write a STLR instruction into *BUF.
1299
1300 STLR rt, [xn]
1301
1302 RT is the register to store.
1303 RN is the base address register. */
1304
1305 static int
1306 emit_stlr (uint32_t *buf, struct aarch64_register rt,
1307 struct aarch64_register rn)
1308 {
1309 return emit_load_store_exclusive (buf, rt.is64 ? 3 : 2, STLR, xzr, rt,
1310 xzr, rn);
1311 }
1312
1313 /* Helper function for data processing instructions with register sources. */
1314
1315 static int
1316 emit_data_processing_reg (uint32_t *buf, uint32_t opcode,
1317 struct aarch64_register rd,
1318 struct aarch64_register rn,
1319 struct aarch64_register rm)
1320 {
1321 uint32_t size = ENCODE (rd.is64, 1, 31);
1322
1323 return aarch64_emit_insn (buf, opcode | size | ENCODE (rm.num, 5, 16)
1324 | ENCODE (rn.num, 5, 5) | ENCODE (rd.num, 5, 0));
1325 }
1326
1327 /* Helper function for data processing instructions taking either a register
1328 or an immediate. */
1329
1330 static int
1331 emit_data_processing (uint32_t *buf, enum aarch64_opcodes opcode,
1332 struct aarch64_register rd,
1333 struct aarch64_register rn,
1334 struct aarch64_operand operand)
1335 {
1336 uint32_t size = ENCODE (rd.is64, 1, 31);
1337 /* The opcode is different for register and immediate source operands. */
1338 uint32_t operand_opcode;
1339
1340 if (operand.type == OPERAND_IMMEDIATE)
1341 {
1342 /* xxx1 000x xxxx xxxx xxxx xxxx xxxx xxxx */
1343 operand_opcode = ENCODE (8, 4, 25);
1344
1345 return aarch64_emit_insn (buf, opcode | operand_opcode | size
1346 | ENCODE (operand.imm, 12, 10)
1347 | ENCODE (rn.num, 5, 5)
1348 | ENCODE (rd.num, 5, 0));
1349 }
1350 else
1351 {
1352 /* xxx0 101x xxxx xxxx xxxx xxxx xxxx xxxx */
1353 operand_opcode = ENCODE (5, 4, 25);
1354
1355 return emit_data_processing_reg (buf, opcode | operand_opcode, rd,
1356 rn, operand.reg);
1357 }
1358 }
1359
1360 /* Write an ADD instruction into *BUF.
1361
1362 ADD rd, rn, #imm
1363 ADD rd, rn, rm
1364
1365 This function handles both an immediate and register add.
1366
1367 RD is the destination register.
1368 RN is the input register.
1369 OPERAND is the source operand, either of type OPERAND_IMMEDIATE or
1370 OPERAND_REGISTER. */
1371
1372 static int
1373 emit_add (uint32_t *buf, struct aarch64_register rd,
1374 struct aarch64_register rn, struct aarch64_operand operand)
1375 {
1376 return emit_data_processing (buf, ADD, rd, rn, operand);
1377 }
1378
1379 /* Write a SUB instruction into *BUF.
1380
1381 SUB rd, rn, #imm
1382 SUB rd, rn, rm
1383
1384 This function handles both an immediate and register sub.
1385
1386 RD is the destination register.
1387 RN is the input register.
1388 IMM is the immediate to substract to RN. */
1389
1390 static int
1391 emit_sub (uint32_t *buf, struct aarch64_register rd,
1392 struct aarch64_register rn, struct aarch64_operand operand)
1393 {
1394 return emit_data_processing (buf, SUB, rd, rn, operand);
1395 }
1396
1397 /* Write a MOV instruction into *BUF.
1398
1399 MOV rd, #imm
1400 MOV rd, rm
1401
1402 This function handles both a wide immediate move and a register move,
1403 with the condition that the source register is not xzr. xzr and the
1404 stack pointer share the same encoding and this function only supports
1405 the stack pointer.
1406
1407 RD is the destination register.
1408 OPERAND is the source operand, either of type OPERAND_IMMEDIATE or
1409 OPERAND_REGISTER. */
1410
1411 static int
1412 emit_mov (uint32_t *buf, struct aarch64_register rd,
1413 struct aarch64_operand operand)
1414 {
1415 if (operand.type == OPERAND_IMMEDIATE)
1416 {
1417 uint32_t size = ENCODE (rd.is64, 1, 31);
1418 /* Do not shift the immediate. */
1419 uint32_t shift = ENCODE (0, 2, 21);
1420
1421 return aarch64_emit_insn (buf, MOV | size | shift
1422 | ENCODE (operand.imm, 16, 5)
1423 | ENCODE (rd.num, 5, 0));
1424 }
1425 else
1426 return emit_add (buf, rd, operand.reg, immediate_operand (0));
1427 }
1428
1429 /* Write a MOVK instruction into *BUF.
1430
1431 MOVK rd, #imm, lsl #shift
1432
1433 RD is the destination register.
1434 IMM is the immediate.
1435 SHIFT is the logical shift left to apply to IMM. */
1436
1437 static int
1438 emit_movk (uint32_t *buf, struct aarch64_register rd, uint32_t imm,
1439 unsigned shift)
1440 {
1441 uint32_t size = ENCODE (rd.is64, 1, 31);
1442
1443 return aarch64_emit_insn (buf, MOVK | size | ENCODE (shift, 2, 21) |
1444 ENCODE (imm, 16, 5) | ENCODE (rd.num, 5, 0));
1445 }
1446
1447 /* Write instructions into *BUF in order to move ADDR into a register.
1448 ADDR can be a 64-bit value.
1449
1450 This function will emit a series of MOV and MOVK instructions, such as:
1451
1452 MOV xd, #(addr)
1453 MOVK xd, #(addr >> 16), lsl #16
1454 MOVK xd, #(addr >> 32), lsl #32
1455 MOVK xd, #(addr >> 48), lsl #48 */
1456
1457 static int
1458 emit_mov_addr (uint32_t *buf, struct aarch64_register rd, CORE_ADDR addr)
1459 {
1460 uint32_t *p = buf;
1461
1462 /* The MOV (wide immediate) instruction clears to top bits of the
1463 register. */
1464 p += emit_mov (p, rd, immediate_operand (addr & 0xffff));
1465
1466 if ((addr >> 16) != 0)
1467 p += emit_movk (p, rd, (addr >> 16) & 0xffff, 1);
1468 else
1469 return p - buf;
1470
1471 if ((addr >> 32) != 0)
1472 p += emit_movk (p, rd, (addr >> 32) & 0xffff, 2);
1473 else
1474 return p - buf;
1475
1476 if ((addr >> 48) != 0)
1477 p += emit_movk (p, rd, (addr >> 48) & 0xffff, 3);
1478
1479 return p - buf;
1480 }
1481
1482 /* Write a SUBS instruction into *BUF.
1483
1484 SUBS rd, rn, rm
1485
1486 This instruction update the condition flags.
1487
1488 RD is the destination register.
1489 RN and RM are the source registers. */
1490
1491 static int
1492 emit_subs (uint32_t *buf, struct aarch64_register rd,
1493 struct aarch64_register rn, struct aarch64_operand operand)
1494 {
1495 return emit_data_processing (buf, SUBS, rd, rn, operand);
1496 }
1497
1498 /* Write a CMP instruction into *BUF.
1499
1500 CMP rn, rm
1501
1502 This instruction is an alias of SUBS xzr, rn, rm.
1503
1504 RN and RM are the registers to compare. */
1505
1506 static int
1507 emit_cmp (uint32_t *buf, struct aarch64_register rn,
1508 struct aarch64_operand operand)
1509 {
1510 return emit_subs (buf, xzr, rn, operand);
1511 }
1512
1513 /* Write a AND instruction into *BUF.
1514
1515 AND rd, rn, rm
1516
1517 RD is the destination register.
1518 RN and RM are the source registers. */
1519
1520 static int
1521 emit_and (uint32_t *buf, struct aarch64_register rd,
1522 struct aarch64_register rn, struct aarch64_register rm)
1523 {
1524 return emit_data_processing_reg (buf, AND, rd, rn, rm);
1525 }
1526
1527 /* Write a ORR instruction into *BUF.
1528
1529 ORR rd, rn, rm
1530
1531 RD is the destination register.
1532 RN and RM are the source registers. */
1533
1534 static int
1535 emit_orr (uint32_t *buf, struct aarch64_register rd,
1536 struct aarch64_register rn, struct aarch64_register rm)
1537 {
1538 return emit_data_processing_reg (buf, ORR, rd, rn, rm);
1539 }
1540
1541 /* Write a ORN instruction into *BUF.
1542
1543 ORN rd, rn, rm
1544
1545 RD is the destination register.
1546 RN and RM are the source registers. */
1547
1548 static int
1549 emit_orn (uint32_t *buf, struct aarch64_register rd,
1550 struct aarch64_register rn, struct aarch64_register rm)
1551 {
1552 return emit_data_processing_reg (buf, ORN, rd, rn, rm);
1553 }
1554
1555 /* Write a EOR instruction into *BUF.
1556
1557 EOR rd, rn, rm
1558
1559 RD is the destination register.
1560 RN and RM are the source registers. */
1561
1562 static int
1563 emit_eor (uint32_t *buf, struct aarch64_register rd,
1564 struct aarch64_register rn, struct aarch64_register rm)
1565 {
1566 return emit_data_processing_reg (buf, EOR, rd, rn, rm);
1567 }
1568
1569 /* Write a MVN instruction into *BUF.
1570
1571 MVN rd, rm
1572
1573 This is an alias for ORN rd, xzr, rm.
1574
1575 RD is the destination register.
1576 RM is the source register. */
1577
1578 static int
1579 emit_mvn (uint32_t *buf, struct aarch64_register rd,
1580 struct aarch64_register rm)
1581 {
1582 return emit_orn (buf, rd, xzr, rm);
1583 }
1584
1585 /* Write a LSLV instruction into *BUF.
1586
1587 LSLV rd, rn, rm
1588
1589 RD is the destination register.
1590 RN and RM are the source registers. */
1591
1592 static int
1593 emit_lslv (uint32_t *buf, struct aarch64_register rd,
1594 struct aarch64_register rn, struct aarch64_register rm)
1595 {
1596 return emit_data_processing_reg (buf, LSLV, rd, rn, rm);
1597 }
1598
1599 /* Write a LSRV instruction into *BUF.
1600
1601 LSRV rd, rn, rm
1602
1603 RD is the destination register.
1604 RN and RM are the source registers. */
1605
1606 static int
1607 emit_lsrv (uint32_t *buf, struct aarch64_register rd,
1608 struct aarch64_register rn, struct aarch64_register rm)
1609 {
1610 return emit_data_processing_reg (buf, LSRV, rd, rn, rm);
1611 }
1612
1613 /* Write a ASRV instruction into *BUF.
1614
1615 ASRV rd, rn, rm
1616
1617 RD is the destination register.
1618 RN and RM are the source registers. */
1619
1620 static int
1621 emit_asrv (uint32_t *buf, struct aarch64_register rd,
1622 struct aarch64_register rn, struct aarch64_register rm)
1623 {
1624 return emit_data_processing_reg (buf, ASRV, rd, rn, rm);
1625 }
1626
1627 /* Write a MUL instruction into *BUF.
1628
1629 MUL rd, rn, rm
1630
1631 RD is the destination register.
1632 RN and RM are the source registers. */
1633
1634 static int
1635 emit_mul (uint32_t *buf, struct aarch64_register rd,
1636 struct aarch64_register rn, struct aarch64_register rm)
1637 {
1638 return emit_data_processing_reg (buf, MUL, rd, rn, rm);
1639 }
1640
1641 /* Write a MRS instruction into *BUF. The register size is 64-bit.
1642
1643 MRS xt, system_reg
1644
1645 RT is the destination register.
1646 SYSTEM_REG is special purpose register to read. */
1647
1648 static int
1649 emit_mrs (uint32_t *buf, struct aarch64_register rt,
1650 enum aarch64_system_control_registers system_reg)
1651 {
1652 return aarch64_emit_insn (buf, MRS | ENCODE (system_reg, 15, 5)
1653 | ENCODE (rt.num, 5, 0));
1654 }
1655
1656 /* Write a MSR instruction into *BUF. The register size is 64-bit.
1657
1658 MSR system_reg, xt
1659
1660 SYSTEM_REG is special purpose register to write.
1661 RT is the input register. */
1662
1663 static int
1664 emit_msr (uint32_t *buf, enum aarch64_system_control_registers system_reg,
1665 struct aarch64_register rt)
1666 {
1667 return aarch64_emit_insn (buf, MSR | ENCODE (system_reg, 15, 5)
1668 | ENCODE (rt.num, 5, 0));
1669 }
1670
1671 /* Write a SEVL instruction into *BUF.
1672
1673 This is a hint instruction telling the hardware to trigger an event. */
1674
1675 static int
1676 emit_sevl (uint32_t *buf)
1677 {
1678 return aarch64_emit_insn (buf, SEVL);
1679 }
1680
1681 /* Write a WFE instruction into *BUF.
1682
1683 This is a hint instruction telling the hardware to wait for an event. */
1684
1685 static int
1686 emit_wfe (uint32_t *buf)
1687 {
1688 return aarch64_emit_insn (buf, WFE);
1689 }
1690
1691 /* Write a SBFM instruction into *BUF.
1692
1693 SBFM rd, rn, #immr, #imms
1694
1695 This instruction moves the bits from #immr to #imms into the
1696 destination, sign extending the result.
1697
1698 RD is the destination register.
1699 RN is the source register.
1700 IMMR is the bit number to start at (least significant bit).
1701 IMMS is the bit number to stop at (most significant bit). */
1702
1703 static int
1704 emit_sbfm (uint32_t *buf, struct aarch64_register rd,
1705 struct aarch64_register rn, uint32_t immr, uint32_t imms)
1706 {
1707 uint32_t size = ENCODE (rd.is64, 1, 31);
1708 uint32_t n = ENCODE (rd.is64, 1, 22);
1709
1710 return aarch64_emit_insn (buf, SBFM | size | n | ENCODE (immr, 6, 16)
1711 | ENCODE (imms, 6, 10) | ENCODE (rn.num, 5, 5)
1712 | ENCODE (rd.num, 5, 0));
1713 }
1714
1715 /* Write a SBFX instruction into *BUF.
1716
1717 SBFX rd, rn, #lsb, #width
1718
1719 This instruction moves #width bits from #lsb into the destination, sign
1720 extending the result. This is an alias for:
1721
1722 SBFM rd, rn, #lsb, #(lsb + width - 1)
1723
1724 RD is the destination register.
1725 RN is the source register.
1726 LSB is the bit number to start at (least significant bit).
1727 WIDTH is the number of bits to move. */
1728
1729 static int
1730 emit_sbfx (uint32_t *buf, struct aarch64_register rd,
1731 struct aarch64_register rn, uint32_t lsb, uint32_t width)
1732 {
1733 return emit_sbfm (buf, rd, rn, lsb, lsb + width - 1);
1734 }
1735
1736 /* Write a UBFM instruction into *BUF.
1737
1738 UBFM rd, rn, #immr, #imms
1739
1740 This instruction moves the bits from #immr to #imms into the
1741 destination, extending the result with zeros.
1742
1743 RD is the destination register.
1744 RN is the source register.
1745 IMMR is the bit number to start at (least significant bit).
1746 IMMS is the bit number to stop at (most significant bit). */
1747
1748 static int
1749 emit_ubfm (uint32_t *buf, struct aarch64_register rd,
1750 struct aarch64_register rn, uint32_t immr, uint32_t imms)
1751 {
1752 uint32_t size = ENCODE (rd.is64, 1, 31);
1753 uint32_t n = ENCODE (rd.is64, 1, 22);
1754
1755 return aarch64_emit_insn (buf, UBFM | size | n | ENCODE (immr, 6, 16)
1756 | ENCODE (imms, 6, 10) | ENCODE (rn.num, 5, 5)
1757 | ENCODE (rd.num, 5, 0));
1758 }
1759
1760 /* Write a UBFX instruction into *BUF.
1761
1762 UBFX rd, rn, #lsb, #width
1763
1764 This instruction moves #width bits from #lsb into the destination,
1765 extending the result with zeros. This is an alias for:
1766
1767 UBFM rd, rn, #lsb, #(lsb + width - 1)
1768
1769 RD is the destination register.
1770 RN is the source register.
1771 LSB is the bit number to start at (least significant bit).
1772 WIDTH is the number of bits to move. */
1773
1774 static int
1775 emit_ubfx (uint32_t *buf, struct aarch64_register rd,
1776 struct aarch64_register rn, uint32_t lsb, uint32_t width)
1777 {
1778 return emit_ubfm (buf, rd, rn, lsb, lsb + width - 1);
1779 }
1780
1781 /* Write a CSINC instruction into *BUF.
1782
1783 CSINC rd, rn, rm, cond
1784
1785 This instruction conditionally increments rn or rm and places the result
1786 in rd. rn is chosen is the condition is true.
1787
1788 RD is the destination register.
1789 RN and RM are the source registers.
1790 COND is the encoded condition. */
1791
1792 static int
1793 emit_csinc (uint32_t *buf, struct aarch64_register rd,
1794 struct aarch64_register rn, struct aarch64_register rm,
1795 unsigned cond)
1796 {
1797 uint32_t size = ENCODE (rd.is64, 1, 31);
1798
1799 return aarch64_emit_insn (buf, CSINC | size | ENCODE (rm.num, 5, 16)
1800 | ENCODE (cond, 4, 12) | ENCODE (rn.num, 5, 5)
1801 | ENCODE (rd.num, 5, 0));
1802 }
1803
1804 /* Write a CSET instruction into *BUF.
1805
1806 CSET rd, cond
1807
1808 This instruction conditionally write 1 or 0 in the destination register.
1809 1 is written if the condition is true. This is an alias for:
1810
1811 CSINC rd, xzr, xzr, !cond
1812
1813 Note that the condition needs to be inverted.
1814
1815 RD is the destination register.
1816 RN and RM are the source registers.
1817 COND is the encoded condition. */
1818
1819 static int
1820 emit_cset (uint32_t *buf, struct aarch64_register rd, unsigned cond)
1821 {
1822 /* The least significant bit of the condition needs toggling in order to
1823 invert it. */
1824 return emit_csinc (buf, rd, xzr, xzr, cond ^ 0x1);
1825 }
1826
1827 /* Write LEN instructions from BUF into the inferior memory at *TO.
1828
1829 Note instructions are always little endian on AArch64, unlike data. */
1830
1831 static void
1832 append_insns (CORE_ADDR *to, size_t len, const uint32_t *buf)
1833 {
1834 size_t byte_len = len * sizeof (uint32_t);
1835 #if (__BYTE_ORDER == __BIG_ENDIAN)
1836 uint32_t *le_buf = (uint32_t *) xmalloc (byte_len);
1837 size_t i;
1838
1839 for (i = 0; i < len; i++)
1840 le_buf[i] = htole32 (buf[i]);
1841
1842 target_write_memory (*to, (const unsigned char *) le_buf, byte_len);
1843
1844 xfree (le_buf);
1845 #else
1846 target_write_memory (*to, (const unsigned char *) buf, byte_len);
1847 #endif
1848
1849 *to += byte_len;
1850 }
1851
1852 /* Sub-class of struct aarch64_insn_data, store information of
1853 instruction relocation for fast tracepoint. Visitor can
1854 relocate an instruction from BASE.INSN_ADDR to NEW_ADDR and save
1855 the relocated instructions in buffer pointed by INSN_PTR. */
1856
1857 struct aarch64_insn_relocation_data
1858 {
1859 struct aarch64_insn_data base;
1860
1861 /* The new address the instruction is relocated to. */
1862 CORE_ADDR new_addr;
1863 /* Pointer to the buffer of relocated instruction(s). */
1864 uint32_t *insn_ptr;
1865 };
1866
1867 /* Implementation of aarch64_insn_visitor method "b". */
1868
1869 static void
1870 aarch64_ftrace_insn_reloc_b (const int is_bl, const int32_t offset,
1871 struct aarch64_insn_data *data)
1872 {
1873 struct aarch64_insn_relocation_data *insn_reloc
1874 = (struct aarch64_insn_relocation_data *) data;
1875 int64_t new_offset
1876 = insn_reloc->base.insn_addr - insn_reloc->new_addr + offset;
1877
1878 if (can_encode_int32 (new_offset, 28))
1879 insn_reloc->insn_ptr += emit_b (insn_reloc->insn_ptr, is_bl, new_offset);
1880 }
1881
1882 /* Implementation of aarch64_insn_visitor method "b_cond". */
1883
1884 static void
1885 aarch64_ftrace_insn_reloc_b_cond (const unsigned cond, const int32_t offset,
1886 struct aarch64_insn_data *data)
1887 {
1888 struct aarch64_insn_relocation_data *insn_reloc
1889 = (struct aarch64_insn_relocation_data *) data;
1890 int64_t new_offset
1891 = insn_reloc->base.insn_addr - insn_reloc->new_addr + offset;
1892
1893 if (can_encode_int32 (new_offset, 21))
1894 {
1895 insn_reloc->insn_ptr += emit_bcond (insn_reloc->insn_ptr, cond,
1896 new_offset);
1897 }
1898 else if (can_encode_int32 (new_offset, 28))
1899 {
1900 /* The offset is out of range for a conditional branch
1901 instruction but not for a unconditional branch. We can use
1902 the following instructions instead:
1903
1904 B.COND TAKEN ; If cond is true, then jump to TAKEN.
1905 B NOT_TAKEN ; Else jump over TAKEN and continue.
1906 TAKEN:
1907 B #(offset - 8)
1908 NOT_TAKEN:
1909
1910 */
1911
1912 insn_reloc->insn_ptr += emit_bcond (insn_reloc->insn_ptr, cond, 8);
1913 insn_reloc->insn_ptr += emit_b (insn_reloc->insn_ptr, 0, 8);
1914 insn_reloc->insn_ptr += emit_b (insn_reloc->insn_ptr, 0, new_offset - 8);
1915 }
1916 }
1917
1918 /* Implementation of aarch64_insn_visitor method "cb". */
1919
1920 static void
1921 aarch64_ftrace_insn_reloc_cb (const int32_t offset, const int is_cbnz,
1922 const unsigned rn, int is64,
1923 struct aarch64_insn_data *data)
1924 {
1925 struct aarch64_insn_relocation_data *insn_reloc
1926 = (struct aarch64_insn_relocation_data *) data;
1927 int64_t new_offset
1928 = insn_reloc->base.insn_addr - insn_reloc->new_addr + offset;
1929
1930 if (can_encode_int32 (new_offset, 21))
1931 {
1932 insn_reloc->insn_ptr += emit_cb (insn_reloc->insn_ptr, is_cbnz,
1933 aarch64_register (rn, is64), new_offset);
1934 }
1935 else if (can_encode_int32 (new_offset, 28))
1936 {
1937 /* The offset is out of range for a compare and branch
1938 instruction but not for a unconditional branch. We can use
1939 the following instructions instead:
1940
1941 CBZ xn, TAKEN ; xn == 0, then jump to TAKEN.
1942 B NOT_TAKEN ; Else jump over TAKEN and continue.
1943 TAKEN:
1944 B #(offset - 8)
1945 NOT_TAKEN:
1946
1947 */
1948 insn_reloc->insn_ptr += emit_cb (insn_reloc->insn_ptr, is_cbnz,
1949 aarch64_register (rn, is64), 8);
1950 insn_reloc->insn_ptr += emit_b (insn_reloc->insn_ptr, 0, 8);
1951 insn_reloc->insn_ptr += emit_b (insn_reloc->insn_ptr, 0, new_offset - 8);
1952 }
1953 }
1954
1955 /* Implementation of aarch64_insn_visitor method "tb". */
1956
1957 static void
1958 aarch64_ftrace_insn_reloc_tb (const int32_t offset, int is_tbnz,
1959 const unsigned rt, unsigned bit,
1960 struct aarch64_insn_data *data)
1961 {
1962 struct aarch64_insn_relocation_data *insn_reloc
1963 = (struct aarch64_insn_relocation_data *) data;
1964 int64_t new_offset
1965 = insn_reloc->base.insn_addr - insn_reloc->new_addr + offset;
1966
1967 if (can_encode_int32 (new_offset, 16))
1968 {
1969 insn_reloc->insn_ptr += emit_tb (insn_reloc->insn_ptr, is_tbnz, bit,
1970 aarch64_register (rt, 1), new_offset);
1971 }
1972 else if (can_encode_int32 (new_offset, 28))
1973 {
1974 /* The offset is out of range for a test bit and branch
1975 instruction but not for a unconditional branch. We can use
1976 the following instructions instead:
1977
1978 TBZ xn, #bit, TAKEN ; xn[bit] == 0, then jump to TAKEN.
1979 B NOT_TAKEN ; Else jump over TAKEN and continue.
1980 TAKEN:
1981 B #(offset - 8)
1982 NOT_TAKEN:
1983
1984 */
1985 insn_reloc->insn_ptr += emit_tb (insn_reloc->insn_ptr, is_tbnz, bit,
1986 aarch64_register (rt, 1), 8);
1987 insn_reloc->insn_ptr += emit_b (insn_reloc->insn_ptr, 0, 8);
1988 insn_reloc->insn_ptr += emit_b (insn_reloc->insn_ptr, 0,
1989 new_offset - 8);
1990 }
1991 }
1992
1993 /* Implementation of aarch64_insn_visitor method "adr". */
1994
1995 static void
1996 aarch64_ftrace_insn_reloc_adr (const int32_t offset, const unsigned rd,
1997 const int is_adrp,
1998 struct aarch64_insn_data *data)
1999 {
2000 struct aarch64_insn_relocation_data *insn_reloc
2001 = (struct aarch64_insn_relocation_data *) data;
2002 /* We know exactly the address the ADR{P,} instruction will compute.
2003 We can just write it to the destination register. */
2004 CORE_ADDR address = data->insn_addr + offset;
2005
2006 if (is_adrp)
2007 {
2008 /* Clear the lower 12 bits of the offset to get the 4K page. */
2009 insn_reloc->insn_ptr += emit_mov_addr (insn_reloc->insn_ptr,
2010 aarch64_register (rd, 1),
2011 address & ~0xfff);
2012 }
2013 else
2014 insn_reloc->insn_ptr += emit_mov_addr (insn_reloc->insn_ptr,
2015 aarch64_register (rd, 1), address);
2016 }
2017
2018 /* Implementation of aarch64_insn_visitor method "ldr_literal". */
2019
2020 static void
2021 aarch64_ftrace_insn_reloc_ldr_literal (const int32_t offset, const int is_sw,
2022 const unsigned rt, const int is64,
2023 struct aarch64_insn_data *data)
2024 {
2025 struct aarch64_insn_relocation_data *insn_reloc
2026 = (struct aarch64_insn_relocation_data *) data;
2027 CORE_ADDR address = data->insn_addr + offset;
2028
2029 insn_reloc->insn_ptr += emit_mov_addr (insn_reloc->insn_ptr,
2030 aarch64_register (rt, 1), address);
2031
2032 /* We know exactly what address to load from, and what register we
2033 can use:
2034
2035 MOV xd, #(oldloc + offset)
2036 MOVK xd, #((oldloc + offset) >> 16), lsl #16
2037 ...
2038
2039 LDR xd, [xd] ; or LDRSW xd, [xd]
2040
2041 */
2042
2043 if (is_sw)
2044 insn_reloc->insn_ptr += emit_ldrsw (insn_reloc->insn_ptr,
2045 aarch64_register (rt, 1),
2046 aarch64_register (rt, 1),
2047 offset_memory_operand (0));
2048 else
2049 insn_reloc->insn_ptr += emit_ldr (insn_reloc->insn_ptr,
2050 aarch64_register (rt, is64),
2051 aarch64_register (rt, 1),
2052 offset_memory_operand (0));
2053 }
2054
2055 /* Implementation of aarch64_insn_visitor method "others". */
2056
2057 static void
2058 aarch64_ftrace_insn_reloc_others (const uint32_t insn,
2059 struct aarch64_insn_data *data)
2060 {
2061 struct aarch64_insn_relocation_data *insn_reloc
2062 = (struct aarch64_insn_relocation_data *) data;
2063
2064 /* The instruction is not PC relative. Just re-emit it at the new
2065 location. */
2066 insn_reloc->insn_ptr += aarch64_emit_insn (insn_reloc->insn_ptr, insn);
2067 }
2068
2069 static const struct aarch64_insn_visitor visitor =
2070 {
2071 aarch64_ftrace_insn_reloc_b,
2072 aarch64_ftrace_insn_reloc_b_cond,
2073 aarch64_ftrace_insn_reloc_cb,
2074 aarch64_ftrace_insn_reloc_tb,
2075 aarch64_ftrace_insn_reloc_adr,
2076 aarch64_ftrace_insn_reloc_ldr_literal,
2077 aarch64_ftrace_insn_reloc_others,
2078 };
2079
2080 bool
2081 aarch64_target::supports_fast_tracepoints ()
2082 {
2083 return true;
2084 }
2085
2086 /* Implementation of target ops method
2087 "install_fast_tracepoint_jump_pad". */
2088
2089 int
2090 aarch64_target::install_fast_tracepoint_jump_pad
2091 (CORE_ADDR tpoint, CORE_ADDR tpaddr, CORE_ADDR collector,
2092 CORE_ADDR lockaddr, ULONGEST orig_size, CORE_ADDR *jump_entry,
2093 CORE_ADDR *trampoline, ULONGEST *trampoline_size,
2094 unsigned char *jjump_pad_insn, ULONGEST *jjump_pad_insn_size,
2095 CORE_ADDR *adjusted_insn_addr, CORE_ADDR *adjusted_insn_addr_end,
2096 char *err)
2097 {
2098 uint32_t buf[256];
2099 uint32_t *p = buf;
2100 int64_t offset;
2101 int i;
2102 uint32_t insn;
2103 CORE_ADDR buildaddr = *jump_entry;
2104 struct aarch64_insn_relocation_data insn_data;
2105
2106 /* We need to save the current state on the stack both to restore it
2107 later and to collect register values when the tracepoint is hit.
2108
2109 The saved registers are pushed in a layout that needs to be in sync
2110 with aarch64_ft_collect_regmap (see linux-aarch64-ipa.c). Later on
2111 the supply_fast_tracepoint_registers function will fill in the
2112 register cache from a pointer to saved registers on the stack we build
2113 here.
2114
2115 For simplicity, we set the size of each cell on the stack to 16 bytes.
2116 This way one cell can hold any register type, from system registers
2117 to the 128 bit SIMD&FP registers. Furthermore, the stack pointer
2118 has to be 16 bytes aligned anyway.
2119
2120 Note that the CPSR register does not exist on AArch64. Instead we
2121 can access system bits describing the process state with the
2122 MRS/MSR instructions, namely the condition flags. We save them as
2123 if they are part of a CPSR register because that's how GDB
2124 interprets these system bits. At the moment, only the condition
2125 flags are saved in CPSR (NZCV).
2126
2127 Stack layout, each cell is 16 bytes (descending):
2128
2129 High *-------- SIMD&FP registers from 31 down to 0. --------*
2130 | q31 |
2131 . .
2132 . . 32 cells
2133 . .
2134 | q0 |
2135 *---- General purpose registers from 30 down to 0. ----*
2136 | x30 |
2137 . .
2138 . . 31 cells
2139 . .
2140 | x0 |
2141 *------------- Special purpose registers. -------------*
2142 | SP |
2143 | PC |
2144 | CPSR (NZCV) | 5 cells
2145 | FPSR |
2146 | FPCR | <- SP + 16
2147 *------------- collecting_t object --------------------*
2148 | TPIDR_EL0 | struct tracepoint * |
2149 Low *------------------------------------------------------*
2150
2151 After this stack is set up, we issue a call to the collector, passing
2152 it the saved registers at (SP + 16). */
2153
2154 /* Push SIMD&FP registers on the stack:
2155
2156 SUB sp, sp, #(32 * 16)
2157
2158 STP q30, q31, [sp, #(30 * 16)]
2159 ...
2160 STP q0, q1, [sp]
2161
2162 */
2163 p += emit_sub (p, sp, sp, immediate_operand (32 * 16));
2164 for (i = 30; i >= 0; i -= 2)
2165 p += emit_stp_q_offset (p, i, i + 1, sp, i * 16);
2166
2167 /* Push general purpose registers on the stack. Note that we do not need
2168 to push x31 as it represents the xzr register and not the stack
2169 pointer in a STR instruction.
2170
2171 SUB sp, sp, #(31 * 16)
2172
2173 STR x30, [sp, #(30 * 16)]
2174 ...
2175 STR x0, [sp]
2176
2177 */
2178 p += emit_sub (p, sp, sp, immediate_operand (31 * 16));
2179 for (i = 30; i >= 0; i -= 1)
2180 p += emit_str (p, aarch64_register (i, 1), sp,
2181 offset_memory_operand (i * 16));
2182
2183 /* Make space for 5 more cells.
2184
2185 SUB sp, sp, #(5 * 16)
2186
2187 */
2188 p += emit_sub (p, sp, sp, immediate_operand (5 * 16));
2189
2190
2191 /* Save SP:
2192
2193 ADD x4, sp, #((32 + 31 + 5) * 16)
2194 STR x4, [sp, #(4 * 16)]
2195
2196 */
2197 p += emit_add (p, x4, sp, immediate_operand ((32 + 31 + 5) * 16));
2198 p += emit_str (p, x4, sp, offset_memory_operand (4 * 16));
2199
2200 /* Save PC (tracepoint address):
2201
2202 MOV x3, #(tpaddr)
2203 ...
2204
2205 STR x3, [sp, #(3 * 16)]
2206
2207 */
2208
2209 p += emit_mov_addr (p, x3, tpaddr);
2210 p += emit_str (p, x3, sp, offset_memory_operand (3 * 16));
2211
2212 /* Save CPSR (NZCV), FPSR and FPCR:
2213
2214 MRS x2, nzcv
2215 MRS x1, fpsr
2216 MRS x0, fpcr
2217
2218 STR x2, [sp, #(2 * 16)]
2219 STR x1, [sp, #(1 * 16)]
2220 STR x0, [sp, #(0 * 16)]
2221
2222 */
2223 p += emit_mrs (p, x2, NZCV);
2224 p += emit_mrs (p, x1, FPSR);
2225 p += emit_mrs (p, x0, FPCR);
2226 p += emit_str (p, x2, sp, offset_memory_operand (2 * 16));
2227 p += emit_str (p, x1, sp, offset_memory_operand (1 * 16));
2228 p += emit_str (p, x0, sp, offset_memory_operand (0 * 16));
2229
2230 /* Push the collecting_t object. It consist of the address of the
2231 tracepoint and an ID for the current thread. We get the latter by
2232 reading the tpidr_el0 system register. It corresponds to the
2233 NT_ARM_TLS register accessible with ptrace.
2234
2235 MOV x0, #(tpoint)
2236 ...
2237
2238 MRS x1, tpidr_el0
2239
2240 STP x0, x1, [sp, #-16]!
2241
2242 */
2243
2244 p += emit_mov_addr (p, x0, tpoint);
2245 p += emit_mrs (p, x1, TPIDR_EL0);
2246 p += emit_stp (p, x0, x1, sp, preindex_memory_operand (-16));
2247
2248 /* Spin-lock:
2249
2250 The shared memory for the lock is at lockaddr. It will hold zero
2251 if no-one is holding the lock, otherwise it contains the address of
2252 the collecting_t object on the stack of the thread which acquired it.
2253
2254 At this stage, the stack pointer points to this thread's collecting_t
2255 object.
2256
2257 We use the following registers:
2258 - x0: Address of the lock.
2259 - x1: Pointer to collecting_t object.
2260 - x2: Scratch register.
2261
2262 MOV x0, #(lockaddr)
2263 ...
2264 MOV x1, sp
2265
2266 ; Trigger an event local to this core. So the following WFE
2267 ; instruction is ignored.
2268 SEVL
2269 again:
2270 ; Wait for an event. The event is triggered by either the SEVL
2271 ; or STLR instructions (store release).
2272 WFE
2273
2274 ; Atomically read at lockaddr. This marks the memory location as
2275 ; exclusive. This instruction also has memory constraints which
2276 ; make sure all previous data reads and writes are done before
2277 ; executing it.
2278 LDAXR x2, [x0]
2279
2280 ; Try again if another thread holds the lock.
2281 CBNZ x2, again
2282
2283 ; We can lock it! Write the address of the collecting_t object.
2284 ; This instruction will fail if the memory location is not marked
2285 ; as exclusive anymore. If it succeeds, it will remove the
2286 ; exclusive mark on the memory location. This way, if another
2287 ; thread executes this instruction before us, we will fail and try
2288 ; all over again.
2289 STXR w2, x1, [x0]
2290 CBNZ w2, again
2291
2292 */
2293
2294 p += emit_mov_addr (p, x0, lockaddr);
2295 p += emit_mov (p, x1, register_operand (sp));
2296
2297 p += emit_sevl (p);
2298 p += emit_wfe (p);
2299 p += emit_ldaxr (p, x2, x0);
2300 p += emit_cb (p, 1, w2, -2 * 4);
2301 p += emit_stxr (p, w2, x1, x0);
2302 p += emit_cb (p, 1, x2, -4 * 4);
2303
2304 /* Call collector (struct tracepoint *, unsigned char *):
2305
2306 MOV x0, #(tpoint)
2307 ...
2308
2309 ; Saved registers start after the collecting_t object.
2310 ADD x1, sp, #16
2311
2312 ; We use an intra-procedure-call scratch register.
2313 MOV ip0, #(collector)
2314 ...
2315
2316 ; And call back to C!
2317 BLR ip0
2318
2319 */
2320
2321 p += emit_mov_addr (p, x0, tpoint);
2322 p += emit_add (p, x1, sp, immediate_operand (16));
2323
2324 p += emit_mov_addr (p, ip0, collector);
2325 p += emit_blr (p, ip0);
2326
2327 /* Release the lock.
2328
2329 MOV x0, #(lockaddr)
2330 ...
2331
2332 ; This instruction is a normal store with memory ordering
2333 ; constraints. Thanks to this we do not have to put a data
2334 ; barrier instruction to make sure all data read and writes are done
2335 ; before this instruction is executed. Furthermore, this instruction
2336 ; will trigger an event, letting other threads know they can grab
2337 ; the lock.
2338 STLR xzr, [x0]
2339
2340 */
2341 p += emit_mov_addr (p, x0, lockaddr);
2342 p += emit_stlr (p, xzr, x0);
2343
2344 /* Free collecting_t object:
2345
2346 ADD sp, sp, #16
2347
2348 */
2349 p += emit_add (p, sp, sp, immediate_operand (16));
2350
2351 /* Restore CPSR (NZCV), FPSR and FPCR. And free all special purpose
2352 registers from the stack.
2353
2354 LDR x2, [sp, #(2 * 16)]
2355 LDR x1, [sp, #(1 * 16)]
2356 LDR x0, [sp, #(0 * 16)]
2357
2358 MSR NZCV, x2
2359 MSR FPSR, x1
2360 MSR FPCR, x0
2361
2362 ADD sp, sp #(5 * 16)
2363
2364 */
2365 p += emit_ldr (p, x2, sp, offset_memory_operand (2 * 16));
2366 p += emit_ldr (p, x1, sp, offset_memory_operand (1 * 16));
2367 p += emit_ldr (p, x0, sp, offset_memory_operand (0 * 16));
2368 p += emit_msr (p, NZCV, x2);
2369 p += emit_msr (p, FPSR, x1);
2370 p += emit_msr (p, FPCR, x0);
2371
2372 p += emit_add (p, sp, sp, immediate_operand (5 * 16));
2373
2374 /* Pop general purpose registers:
2375
2376 LDR x0, [sp]
2377 ...
2378 LDR x30, [sp, #(30 * 16)]
2379
2380 ADD sp, sp, #(31 * 16)
2381
2382 */
2383 for (i = 0; i <= 30; i += 1)
2384 p += emit_ldr (p, aarch64_register (i, 1), sp,
2385 offset_memory_operand (i * 16));
2386 p += emit_add (p, sp, sp, immediate_operand (31 * 16));
2387
2388 /* Pop SIMD&FP registers:
2389
2390 LDP q0, q1, [sp]
2391 ...
2392 LDP q30, q31, [sp, #(30 * 16)]
2393
2394 ADD sp, sp, #(32 * 16)
2395
2396 */
2397 for (i = 0; i <= 30; i += 2)
2398 p += emit_ldp_q_offset (p, i, i + 1, sp, i * 16);
2399 p += emit_add (p, sp, sp, immediate_operand (32 * 16));
2400
2401 /* Write the code into the inferior memory. */
2402 append_insns (&buildaddr, p - buf, buf);
2403
2404 /* Now emit the relocated instruction. */
2405 *adjusted_insn_addr = buildaddr;
2406 target_read_uint32 (tpaddr, &insn);
2407
2408 insn_data.base.insn_addr = tpaddr;
2409 insn_data.new_addr = buildaddr;
2410 insn_data.insn_ptr = buf;
2411
2412 aarch64_relocate_instruction (insn, &visitor,
2413 (struct aarch64_insn_data *) &insn_data);
2414
2415 /* We may not have been able to relocate the instruction. */
2416 if (insn_data.insn_ptr == buf)
2417 {
2418 sprintf (err,
2419 "E.Could not relocate instruction from %s to %s.",
2420 core_addr_to_string_nz (tpaddr),
2421 core_addr_to_string_nz (buildaddr));
2422 return 1;
2423 }
2424 else
2425 append_insns (&buildaddr, insn_data.insn_ptr - buf, buf);
2426 *adjusted_insn_addr_end = buildaddr;
2427
2428 /* Go back to the start of the buffer. */
2429 p = buf;
2430
2431 /* Emit a branch back from the jump pad. */
2432 offset = (tpaddr + orig_size - buildaddr);
2433 if (!can_encode_int32 (offset, 28))
2434 {
2435 sprintf (err,
2436 "E.Jump back from jump pad too far from tracepoint "
2437 "(offset 0x%" PRIx64 " cannot be encoded in 28 bits).",
2438 offset);
2439 return 1;
2440 }
2441
2442 p += emit_b (p, 0, offset);
2443 append_insns (&buildaddr, p - buf, buf);
2444
2445 /* Give the caller a branch instruction into the jump pad. */
2446 offset = (*jump_entry - tpaddr);
2447 if (!can_encode_int32 (offset, 28))
2448 {
2449 sprintf (err,
2450 "E.Jump pad too far from tracepoint "
2451 "(offset 0x%" PRIx64 " cannot be encoded in 28 bits).",
2452 offset);
2453 return 1;
2454 }
2455
2456 emit_b ((uint32_t *) jjump_pad_insn, 0, offset);
2457 *jjump_pad_insn_size = 4;
2458
2459 /* Return the end address of our pad. */
2460 *jump_entry = buildaddr;
2461
2462 return 0;
2463 }
2464
2465 /* Helper function writing LEN instructions from START into
2466 current_insn_ptr. */
2467
2468 static void
2469 emit_ops_insns (const uint32_t *start, int len)
2470 {
2471 CORE_ADDR buildaddr = current_insn_ptr;
2472
2473 threads_debug_printf ("Adding %d instrucions at %s",
2474 len, paddress (buildaddr));
2475
2476 append_insns (&buildaddr, len, start);
2477 current_insn_ptr = buildaddr;
2478 }
2479
2480 /* Pop a register from the stack. */
2481
2482 static int
2483 emit_pop (uint32_t *buf, struct aarch64_register rt)
2484 {
2485 return emit_ldr (buf, rt, sp, postindex_memory_operand (1 * 16));
2486 }
2487
2488 /* Push a register on the stack. */
2489
2490 static int
2491 emit_push (uint32_t *buf, struct aarch64_register rt)
2492 {
2493 return emit_str (buf, rt, sp, preindex_memory_operand (-1 * 16));
2494 }
2495
2496 /* Implementation of emit_ops method "emit_prologue". */
2497
2498 static void
2499 aarch64_emit_prologue (void)
2500 {
2501 uint32_t buf[16];
2502 uint32_t *p = buf;
2503
2504 /* This function emit a prologue for the following function prototype:
2505
2506 enum eval_result_type f (unsigned char *regs,
2507 ULONGEST *value);
2508
2509 The first argument is a buffer of raw registers. The second
2510 argument is the result of
2511 evaluating the expression, which will be set to whatever is on top of
2512 the stack at the end.
2513
2514 The stack set up by the prologue is as such:
2515
2516 High *------------------------------------------------------*
2517 | LR |
2518 | FP | <- FP
2519 | x1 (ULONGEST *value) |
2520 | x0 (unsigned char *regs) |
2521 Low *------------------------------------------------------*
2522
2523 As we are implementing a stack machine, each opcode can expand the
2524 stack so we never know how far we are from the data saved by this
2525 prologue. In order to be able refer to value and regs later, we save
2526 the current stack pointer in the frame pointer. This way, it is not
2527 clobbered when calling C functions.
2528
2529 Finally, throughout every operation, we are using register x0 as the
2530 top of the stack, and x1 as a scratch register. */
2531
2532 p += emit_stp (p, x0, x1, sp, preindex_memory_operand (-2 * 16));
2533 p += emit_str (p, lr, sp, offset_memory_operand (3 * 8));
2534 p += emit_str (p, fp, sp, offset_memory_operand (2 * 8));
2535
2536 p += emit_add (p, fp, sp, immediate_operand (2 * 8));
2537
2538
2539 emit_ops_insns (buf, p - buf);
2540 }
2541
2542 /* Implementation of emit_ops method "emit_epilogue". */
2543
2544 static void
2545 aarch64_emit_epilogue (void)
2546 {
2547 uint32_t buf[16];
2548 uint32_t *p = buf;
2549
2550 /* Store the result of the expression (x0) in *value. */
2551 p += emit_sub (p, x1, fp, immediate_operand (1 * 8));
2552 p += emit_ldr (p, x1, x1, offset_memory_operand (0));
2553 p += emit_str (p, x0, x1, offset_memory_operand (0));
2554
2555 /* Restore the previous state. */
2556 p += emit_add (p, sp, fp, immediate_operand (2 * 8));
2557 p += emit_ldp (p, fp, lr, fp, offset_memory_operand (0));
2558
2559 /* Return expr_eval_no_error. */
2560 p += emit_mov (p, x0, immediate_operand (expr_eval_no_error));
2561 p += emit_ret (p, lr);
2562
2563 emit_ops_insns (buf, p - buf);
2564 }
2565
2566 /* Implementation of emit_ops method "emit_add". */
2567
2568 static void
2569 aarch64_emit_add (void)
2570 {
2571 uint32_t buf[16];
2572 uint32_t *p = buf;
2573
2574 p += emit_pop (p, x1);
2575 p += emit_add (p, x0, x1, register_operand (x0));
2576
2577 emit_ops_insns (buf, p - buf);
2578 }
2579
2580 /* Implementation of emit_ops method "emit_sub". */
2581
2582 static void
2583 aarch64_emit_sub (void)
2584 {
2585 uint32_t buf[16];
2586 uint32_t *p = buf;
2587
2588 p += emit_pop (p, x1);
2589 p += emit_sub (p, x0, x1, register_operand (x0));
2590
2591 emit_ops_insns (buf, p - buf);
2592 }
2593
2594 /* Implementation of emit_ops method "emit_mul". */
2595
2596 static void
2597 aarch64_emit_mul (void)
2598 {
2599 uint32_t buf[16];
2600 uint32_t *p = buf;
2601
2602 p += emit_pop (p, x1);
2603 p += emit_mul (p, x0, x1, x0);
2604
2605 emit_ops_insns (buf, p - buf);
2606 }
2607
2608 /* Implementation of emit_ops method "emit_lsh". */
2609
2610 static void
2611 aarch64_emit_lsh (void)
2612 {
2613 uint32_t buf[16];
2614 uint32_t *p = buf;
2615
2616 p += emit_pop (p, x1);
2617 p += emit_lslv (p, x0, x1, x0);
2618
2619 emit_ops_insns (buf, p - buf);
2620 }
2621
2622 /* Implementation of emit_ops method "emit_rsh_signed". */
2623
2624 static void
2625 aarch64_emit_rsh_signed (void)
2626 {
2627 uint32_t buf[16];
2628 uint32_t *p = buf;
2629
2630 p += emit_pop (p, x1);
2631 p += emit_asrv (p, x0, x1, x0);
2632
2633 emit_ops_insns (buf, p - buf);
2634 }
2635
2636 /* Implementation of emit_ops method "emit_rsh_unsigned". */
2637
2638 static void
2639 aarch64_emit_rsh_unsigned (void)
2640 {
2641 uint32_t buf[16];
2642 uint32_t *p = buf;
2643
2644 p += emit_pop (p, x1);
2645 p += emit_lsrv (p, x0, x1, x0);
2646
2647 emit_ops_insns (buf, p - buf);
2648 }
2649
2650 /* Implementation of emit_ops method "emit_ext". */
2651
2652 static void
2653 aarch64_emit_ext (int arg)
2654 {
2655 uint32_t buf[16];
2656 uint32_t *p = buf;
2657
2658 p += emit_sbfx (p, x0, x0, 0, arg);
2659
2660 emit_ops_insns (buf, p - buf);
2661 }
2662
2663 /* Implementation of emit_ops method "emit_log_not". */
2664
2665 static void
2666 aarch64_emit_log_not (void)
2667 {
2668 uint32_t buf[16];
2669 uint32_t *p = buf;
2670
2671 /* If the top of the stack is 0, replace it with 1. Else replace it with
2672 0. */
2673
2674 p += emit_cmp (p, x0, immediate_operand (0));
2675 p += emit_cset (p, x0, EQ);
2676
2677 emit_ops_insns (buf, p - buf);
2678 }
2679
2680 /* Implementation of emit_ops method "emit_bit_and". */
2681
2682 static void
2683 aarch64_emit_bit_and (void)
2684 {
2685 uint32_t buf[16];
2686 uint32_t *p = buf;
2687
2688 p += emit_pop (p, x1);
2689 p += emit_and (p, x0, x0, x1);
2690
2691 emit_ops_insns (buf, p - buf);
2692 }
2693
2694 /* Implementation of emit_ops method "emit_bit_or". */
2695
2696 static void
2697 aarch64_emit_bit_or (void)
2698 {
2699 uint32_t buf[16];
2700 uint32_t *p = buf;
2701
2702 p += emit_pop (p, x1);
2703 p += emit_orr (p, x0, x0, x1);
2704
2705 emit_ops_insns (buf, p - buf);
2706 }
2707
2708 /* Implementation of emit_ops method "emit_bit_xor". */
2709
2710 static void
2711 aarch64_emit_bit_xor (void)
2712 {
2713 uint32_t buf[16];
2714 uint32_t *p = buf;
2715
2716 p += emit_pop (p, x1);
2717 p += emit_eor (p, x0, x0, x1);
2718
2719 emit_ops_insns (buf, p - buf);
2720 }
2721
2722 /* Implementation of emit_ops method "emit_bit_not". */
2723
2724 static void
2725 aarch64_emit_bit_not (void)
2726 {
2727 uint32_t buf[16];
2728 uint32_t *p = buf;
2729
2730 p += emit_mvn (p, x0, x0);
2731
2732 emit_ops_insns (buf, p - buf);
2733 }
2734
2735 /* Implementation of emit_ops method "emit_equal". */
2736
2737 static void
2738 aarch64_emit_equal (void)
2739 {
2740 uint32_t buf[16];
2741 uint32_t *p = buf;
2742
2743 p += emit_pop (p, x1);
2744 p += emit_cmp (p, x0, register_operand (x1));
2745 p += emit_cset (p, x0, EQ);
2746
2747 emit_ops_insns (buf, p - buf);
2748 }
2749
2750 /* Implementation of emit_ops method "emit_less_signed". */
2751
2752 static void
2753 aarch64_emit_less_signed (void)
2754 {
2755 uint32_t buf[16];
2756 uint32_t *p = buf;
2757
2758 p += emit_pop (p, x1);
2759 p += emit_cmp (p, x1, register_operand (x0));
2760 p += emit_cset (p, x0, LT);
2761
2762 emit_ops_insns (buf, p - buf);
2763 }
2764
2765 /* Implementation of emit_ops method "emit_less_unsigned". */
2766
2767 static void
2768 aarch64_emit_less_unsigned (void)
2769 {
2770 uint32_t buf[16];
2771 uint32_t *p = buf;
2772
2773 p += emit_pop (p, x1);
2774 p += emit_cmp (p, x1, register_operand (x0));
2775 p += emit_cset (p, x0, LO);
2776
2777 emit_ops_insns (buf, p - buf);
2778 }
2779
2780 /* Implementation of emit_ops method "emit_ref". */
2781
2782 static void
2783 aarch64_emit_ref (int size)
2784 {
2785 uint32_t buf[16];
2786 uint32_t *p = buf;
2787
2788 switch (size)
2789 {
2790 case 1:
2791 p += emit_ldrb (p, w0, x0, offset_memory_operand (0));
2792 break;
2793 case 2:
2794 p += emit_ldrh (p, w0, x0, offset_memory_operand (0));
2795 break;
2796 case 4:
2797 p += emit_ldr (p, w0, x0, offset_memory_operand (0));
2798 break;
2799 case 8:
2800 p += emit_ldr (p, x0, x0, offset_memory_operand (0));
2801 break;
2802 default:
2803 /* Unknown size, bail on compilation. */
2804 emit_error = 1;
2805 break;
2806 }
2807
2808 emit_ops_insns (buf, p - buf);
2809 }
2810
2811 /* Implementation of emit_ops method "emit_if_goto". */
2812
2813 static void
2814 aarch64_emit_if_goto (int *offset_p, int *size_p)
2815 {
2816 uint32_t buf[16];
2817 uint32_t *p = buf;
2818
2819 /* The Z flag is set or cleared here. */
2820 p += emit_cmp (p, x0, immediate_operand (0));
2821 /* This instruction must not change the Z flag. */
2822 p += emit_pop (p, x0);
2823 /* Branch over the next instruction if x0 == 0. */
2824 p += emit_bcond (p, EQ, 8);
2825
2826 /* The NOP instruction will be patched with an unconditional branch. */
2827 if (offset_p)
2828 *offset_p = (p - buf) * 4;
2829 if (size_p)
2830 *size_p = 4;
2831 p += emit_nop (p);
2832
2833 emit_ops_insns (buf, p - buf);
2834 }
2835
2836 /* Implementation of emit_ops method "emit_goto". */
2837
2838 static void
2839 aarch64_emit_goto (int *offset_p, int *size_p)
2840 {
2841 uint32_t buf[16];
2842 uint32_t *p = buf;
2843
2844 /* The NOP instruction will be patched with an unconditional branch. */
2845 if (offset_p)
2846 *offset_p = 0;
2847 if (size_p)
2848 *size_p = 4;
2849 p += emit_nop (p);
2850
2851 emit_ops_insns (buf, p - buf);
2852 }
2853
2854 /* Implementation of emit_ops method "write_goto_address". */
2855
2856 static void
2857 aarch64_write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
2858 {
2859 uint32_t insn;
2860
2861 emit_b (&insn, 0, to - from);
2862 append_insns (&from, 1, &insn);
2863 }
2864
2865 /* Implementation of emit_ops method "emit_const". */
2866
2867 static void
2868 aarch64_emit_const (LONGEST num)
2869 {
2870 uint32_t buf[16];
2871 uint32_t *p = buf;
2872
2873 p += emit_mov_addr (p, x0, num);
2874
2875 emit_ops_insns (buf, p - buf);
2876 }
2877
2878 /* Implementation of emit_ops method "emit_call". */
2879
2880 static void
2881 aarch64_emit_call (CORE_ADDR fn)
2882 {
2883 uint32_t buf[16];
2884 uint32_t *p = buf;
2885
2886 p += emit_mov_addr (p, ip0, fn);
2887 p += emit_blr (p, ip0);
2888
2889 emit_ops_insns (buf, p - buf);
2890 }
2891
2892 /* Implementation of emit_ops method "emit_reg". */
2893
2894 static void
2895 aarch64_emit_reg (int reg)
2896 {
2897 uint32_t buf[16];
2898 uint32_t *p = buf;
2899
2900 /* Set x0 to unsigned char *regs. */
2901 p += emit_sub (p, x0, fp, immediate_operand (2 * 8));
2902 p += emit_ldr (p, x0, x0, offset_memory_operand (0));
2903 p += emit_mov (p, x1, immediate_operand (reg));
2904
2905 emit_ops_insns (buf, p - buf);
2906
2907 aarch64_emit_call (get_raw_reg_func_addr ());
2908 }
2909
2910 /* Implementation of emit_ops method "emit_pop". */
2911
2912 static void
2913 aarch64_emit_pop (void)
2914 {
2915 uint32_t buf[16];
2916 uint32_t *p = buf;
2917
2918 p += emit_pop (p, x0);
2919
2920 emit_ops_insns (buf, p - buf);
2921 }
2922
2923 /* Implementation of emit_ops method "emit_stack_flush". */
2924
2925 static void
2926 aarch64_emit_stack_flush (void)
2927 {
2928 uint32_t buf[16];
2929 uint32_t *p = buf;
2930
2931 p += emit_push (p, x0);
2932
2933 emit_ops_insns (buf, p - buf);
2934 }
2935
2936 /* Implementation of emit_ops method "emit_zero_ext". */
2937
2938 static void
2939 aarch64_emit_zero_ext (int arg)
2940 {
2941 uint32_t buf[16];
2942 uint32_t *p = buf;
2943
2944 p += emit_ubfx (p, x0, x0, 0, arg);
2945
2946 emit_ops_insns (buf, p - buf);
2947 }
2948
2949 /* Implementation of emit_ops method "emit_swap". */
2950
2951 static void
2952 aarch64_emit_swap (void)
2953 {
2954 uint32_t buf[16];
2955 uint32_t *p = buf;
2956
2957 p += emit_ldr (p, x1, sp, offset_memory_operand (0 * 16));
2958 p += emit_str (p, x0, sp, offset_memory_operand (0 * 16));
2959 p += emit_mov (p, x0, register_operand (x1));
2960
2961 emit_ops_insns (buf, p - buf);
2962 }
2963
2964 /* Implementation of emit_ops method "emit_stack_adjust". */
2965
2966 static void
2967 aarch64_emit_stack_adjust (int n)
2968 {
2969 /* This is not needed with our design. */
2970 uint32_t buf[16];
2971 uint32_t *p = buf;
2972
2973 p += emit_add (p, sp, sp, immediate_operand (n * 16));
2974
2975 emit_ops_insns (buf, p - buf);
2976 }
2977
2978 /* Implementation of emit_ops method "emit_int_call_1". */
2979
2980 static void
2981 aarch64_emit_int_call_1 (CORE_ADDR fn, int arg1)
2982 {
2983 uint32_t buf[16];
2984 uint32_t *p = buf;
2985
2986 p += emit_mov (p, x0, immediate_operand (arg1));
2987
2988 emit_ops_insns (buf, p - buf);
2989
2990 aarch64_emit_call (fn);
2991 }
2992
2993 /* Implementation of emit_ops method "emit_void_call_2". */
2994
2995 static void
2996 aarch64_emit_void_call_2 (CORE_ADDR fn, int arg1)
2997 {
2998 uint32_t buf[16];
2999 uint32_t *p = buf;
3000
3001 /* Push x0 on the stack. */
3002 aarch64_emit_stack_flush ();
3003
3004 /* Setup arguments for the function call:
3005
3006 x0: arg1
3007 x1: top of the stack
3008
3009 MOV x1, x0
3010 MOV x0, #arg1 */
3011
3012 p += emit_mov (p, x1, register_operand (x0));
3013 p += emit_mov (p, x0, immediate_operand (arg1));
3014
3015 emit_ops_insns (buf, p - buf);
3016
3017 aarch64_emit_call (fn);
3018
3019 /* Restore x0. */
3020 aarch64_emit_pop ();
3021 }
3022
3023 /* Implementation of emit_ops method "emit_eq_goto". */
3024
3025 static void
3026 aarch64_emit_eq_goto (int *offset_p, int *size_p)
3027 {
3028 uint32_t buf[16];
3029 uint32_t *p = buf;
3030
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, NE, 8);
3035 /* The NOP instruction will be patched with an unconditional branch. */
3036 if (offset_p)
3037 *offset_p = (p - buf) * 4;
3038 if (size_p)
3039 *size_p = 4;
3040 p += emit_nop (p);
3041
3042 emit_ops_insns (buf, p - buf);
3043 }
3044
3045 /* Implementation of emit_ops method "emit_ne_goto". */
3046
3047 static void
3048 aarch64_emit_ne_goto (int *offset_p, int *size_p)
3049 {
3050 uint32_t buf[16];
3051 uint32_t *p = buf;
3052
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, EQ, 8);
3057 /* The NOP instruction will be patched with an unconditional branch. */
3058 if (offset_p)
3059 *offset_p = (p - buf) * 4;
3060 if (size_p)
3061 *size_p = 4;
3062 p += emit_nop (p);
3063
3064 emit_ops_insns (buf, p - buf);
3065 }
3066
3067 /* Implementation of emit_ops method "emit_lt_goto". */
3068
3069 static void
3070 aarch64_emit_lt_goto (int *offset_p, int *size_p)
3071 {
3072 uint32_t buf[16];
3073 uint32_t *p = buf;
3074
3075 p += emit_pop (p, x1);
3076 p += emit_cmp (p, x1, register_operand (x0));
3077 /* Branch over the next instruction if x0 >= x1. */
3078 p += emit_bcond (p, GE, 8);
3079 /* The NOP instruction will be patched with an unconditional branch. */
3080 if (offset_p)
3081 *offset_p = (p - buf) * 4;
3082 if (size_p)
3083 *size_p = 4;
3084 p += emit_nop (p);
3085
3086 emit_ops_insns (buf, p - buf);
3087 }
3088
3089 /* Implementation of emit_ops method "emit_le_goto". */
3090
3091 static void
3092 aarch64_emit_le_goto (int *offset_p, int *size_p)
3093 {
3094 uint32_t buf[16];
3095 uint32_t *p = buf;
3096
3097 p += emit_pop (p, x1);
3098 p += emit_cmp (p, x1, register_operand (x0));
3099 /* Branch over the next instruction if x0 > x1. */
3100 p += emit_bcond (p, GT, 8);
3101 /* The NOP instruction will be patched with an unconditional branch. */
3102 if (offset_p)
3103 *offset_p = (p - buf) * 4;
3104 if (size_p)
3105 *size_p = 4;
3106 p += emit_nop (p);
3107
3108 emit_ops_insns (buf, p - buf);
3109 }
3110
3111 /* Implementation of emit_ops method "emit_gt_goto". */
3112
3113 static void
3114 aarch64_emit_gt_goto (int *offset_p, int *size_p)
3115 {
3116 uint32_t buf[16];
3117 uint32_t *p = buf;
3118
3119 p += emit_pop (p, x1);
3120 p += emit_cmp (p, x1, register_operand (x0));
3121 /* Branch over the next instruction if x0 <= x1. */
3122 p += emit_bcond (p, LE, 8);
3123 /* The NOP instruction will be patched with an unconditional branch. */
3124 if (offset_p)
3125 *offset_p = (p - buf) * 4;
3126 if (size_p)
3127 *size_p = 4;
3128 p += emit_nop (p);
3129
3130 emit_ops_insns (buf, p - buf);
3131 }
3132
3133 /* Implementation of emit_ops method "emit_ge_got". */
3134
3135 static void
3136 aarch64_emit_ge_got (int *offset_p, int *size_p)
3137 {
3138 uint32_t buf[16];
3139 uint32_t *p = buf;
3140
3141 p += emit_pop (p, x1);
3142 p += emit_cmp (p, x1, register_operand (x0));
3143 /* Branch over the next instruction if x0 <= x1. */
3144 p += emit_bcond (p, LT, 8);
3145 /* The NOP instruction will be patched with an unconditional branch. */
3146 if (offset_p)
3147 *offset_p = (p - buf) * 4;
3148 if (size_p)
3149 *size_p = 4;
3150 p += emit_nop (p);
3151
3152 emit_ops_insns (buf, p - buf);
3153 }
3154
3155 static struct emit_ops aarch64_emit_ops_impl =
3156 {
3157 aarch64_emit_prologue,
3158 aarch64_emit_epilogue,
3159 aarch64_emit_add,
3160 aarch64_emit_sub,
3161 aarch64_emit_mul,
3162 aarch64_emit_lsh,
3163 aarch64_emit_rsh_signed,
3164 aarch64_emit_rsh_unsigned,
3165 aarch64_emit_ext,
3166 aarch64_emit_log_not,
3167 aarch64_emit_bit_and,
3168 aarch64_emit_bit_or,
3169 aarch64_emit_bit_xor,
3170 aarch64_emit_bit_not,
3171 aarch64_emit_equal,
3172 aarch64_emit_less_signed,
3173 aarch64_emit_less_unsigned,
3174 aarch64_emit_ref,
3175 aarch64_emit_if_goto,
3176 aarch64_emit_goto,
3177 aarch64_write_goto_address,
3178 aarch64_emit_const,
3179 aarch64_emit_call,
3180 aarch64_emit_reg,
3181 aarch64_emit_pop,
3182 aarch64_emit_stack_flush,
3183 aarch64_emit_zero_ext,
3184 aarch64_emit_swap,
3185 aarch64_emit_stack_adjust,
3186 aarch64_emit_int_call_1,
3187 aarch64_emit_void_call_2,
3188 aarch64_emit_eq_goto,
3189 aarch64_emit_ne_goto,
3190 aarch64_emit_lt_goto,
3191 aarch64_emit_le_goto,
3192 aarch64_emit_gt_goto,
3193 aarch64_emit_ge_got,
3194 };
3195
3196 /* Implementation of target ops method "emit_ops". */
3197
3198 emit_ops *
3199 aarch64_target::emit_ops ()
3200 {
3201 return &aarch64_emit_ops_impl;
3202 }
3203
3204 /* Implementation of target ops method
3205 "get_min_fast_tracepoint_insn_len". */
3206
3207 int
3208 aarch64_target::get_min_fast_tracepoint_insn_len ()
3209 {
3210 return 4;
3211 }
3212
3213 /* Implementation of linux target ops method "low_supports_range_stepping". */
3214
3215 bool
3216 aarch64_target::low_supports_range_stepping ()
3217 {
3218 return true;
3219 }
3220
3221 /* Implementation of target ops method "sw_breakpoint_from_kind". */
3222
3223 const gdb_byte *
3224 aarch64_target::sw_breakpoint_from_kind (int kind, int *size)
3225 {
3226 if (is_64bit_tdesc ())
3227 {
3228 *size = aarch64_breakpoint_len;
3229 return aarch64_breakpoint;
3230 }
3231 else
3232 return arm_sw_breakpoint_from_kind (kind, size);
3233 }
3234
3235 /* Implementation of target ops method "breakpoint_kind_from_pc". */
3236
3237 int
3238 aarch64_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
3239 {
3240 if (is_64bit_tdesc ())
3241 return aarch64_breakpoint_len;
3242 else
3243 return arm_breakpoint_kind_from_pc (pcptr);
3244 }
3245
3246 /* Implementation of the target ops method
3247 "breakpoint_kind_from_current_state". */
3248
3249 int
3250 aarch64_target::breakpoint_kind_from_current_state (CORE_ADDR *pcptr)
3251 {
3252 if (is_64bit_tdesc ())
3253 return aarch64_breakpoint_len;
3254 else
3255 return arm_breakpoint_kind_from_current_state (pcptr);
3256 }
3257
3258 /* Returns true if memory tagging is supported. */
3259 bool
3260 aarch64_target::supports_memory_tagging ()
3261 {
3262 if (current_thread == NULL)
3263 {
3264 /* We don't have any processes running, so don't attempt to
3265 use linux_get_hwcap2 as it will try to fetch the current
3266 thread id. Instead, just fetch the auxv from the self
3267 PID. */
3268 #ifdef HAVE_GETAUXVAL
3269 return (getauxval (AT_HWCAP2) & HWCAP2_MTE) != 0;
3270 #else
3271 return true;
3272 #endif
3273 }
3274
3275 return (linux_get_hwcap2 (8) & HWCAP2_MTE) != 0;
3276 }
3277
3278 bool
3279 aarch64_target::fetch_memtags (CORE_ADDR address, size_t len,
3280 gdb::byte_vector &tags, int type)
3281 {
3282 /* Allocation tags are per-process, so any tid is fine. */
3283 int tid = lwpid_of (current_thread);
3284
3285 /* Allocation tag? */
3286 if (type == static_cast <int> (aarch64_memtag_type::mte_allocation))
3287 return aarch64_mte_fetch_memtags (tid, address, len, tags);
3288
3289 return false;
3290 }
3291
3292 bool
3293 aarch64_target::store_memtags (CORE_ADDR address, size_t len,
3294 const gdb::byte_vector &tags, int type)
3295 {
3296 /* Allocation tags are per-process, so any tid is fine. */
3297 int tid = lwpid_of (current_thread);
3298
3299 /* Allocation tag? */
3300 if (type == static_cast <int> (aarch64_memtag_type::mte_allocation))
3301 return aarch64_mte_store_memtags (tid, address, len, tags);
3302
3303 return false;
3304 }
3305
3306 /* The linux target ops object. */
3307
3308 linux_process_target *the_linux_target = &the_aarch64_target;
3309
3310 void
3311 initialize_low_arch (void)
3312 {
3313 initialize_low_arch_aarch32 ();
3314
3315 initialize_regsets_info (&aarch64_regsets_info);
3316 }