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