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