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