Debian Codegraph @

gdbserver/linux-aarch64-low.cc

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