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