]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/linux-x86-low.c
Move duplicated Linux x86 code to nat/x86-linux.c
[thirdparty/binutils-gdb.git] / gdb / gdbserver / linux-x86-low.c
1 /* GNU/Linux/x86-64 specific low level interface, for the remote server
2 for GDB.
3 Copyright (C) 2002-2015 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "server.h"
21 #include <signal.h>
22 #include <limits.h>
23 #include <inttypes.h>
24 #include "linux-low.h"
25 #include "i387-fp.h"
26 #include "x86-low.h"
27 #include "x86-xstate.h"
28
29 #include "gdb_proc_service.h"
30 /* Don't include elf/common.h if linux/elf.h got included by
31 gdb_proc_service.h. */
32 #ifndef ELFMAG0
33 #include "elf/common.h"
34 #endif
35
36 #include "agent.h"
37 #include "tdesc.h"
38 #include "tracepoint.h"
39 #include "ax.h"
40 #include "nat/linux-nat.h"
41 #include "nat/x86-linux.h"
42 #include "nat/x86-linux-dregs.h"
43
44 #ifdef __x86_64__
45 /* Defined in auto-generated file amd64-linux.c. */
46 void init_registers_amd64_linux (void);
47 extern const struct target_desc *tdesc_amd64_linux;
48
49 /* Defined in auto-generated file amd64-avx-linux.c. */
50 void init_registers_amd64_avx_linux (void);
51 extern const struct target_desc *tdesc_amd64_avx_linux;
52
53 /* Defined in auto-generated file amd64-avx512-linux.c. */
54 void init_registers_amd64_avx512_linux (void);
55 extern const struct target_desc *tdesc_amd64_avx512_linux;
56
57 /* Defined in auto-generated file amd64-mpx-linux.c. */
58 void init_registers_amd64_mpx_linux (void);
59 extern const struct target_desc *tdesc_amd64_mpx_linux;
60
61 /* Defined in auto-generated file x32-linux.c. */
62 void init_registers_x32_linux (void);
63 extern const struct target_desc *tdesc_x32_linux;
64
65 /* Defined in auto-generated file x32-avx-linux.c. */
66 void init_registers_x32_avx_linux (void);
67 extern const struct target_desc *tdesc_x32_avx_linux;
68
69 /* Defined in auto-generated file x32-avx512-linux.c. */
70 void init_registers_x32_avx512_linux (void);
71 extern const struct target_desc *tdesc_x32_avx512_linux;
72
73 #endif
74
75 /* Defined in auto-generated file i386-linux.c. */
76 void init_registers_i386_linux (void);
77 extern const struct target_desc *tdesc_i386_linux;
78
79 /* Defined in auto-generated file i386-mmx-linux.c. */
80 void init_registers_i386_mmx_linux (void);
81 extern const struct target_desc *tdesc_i386_mmx_linux;
82
83 /* Defined in auto-generated file i386-avx-linux.c. */
84 void init_registers_i386_avx_linux (void);
85 extern const struct target_desc *tdesc_i386_avx_linux;
86
87 /* Defined in auto-generated file i386-avx512-linux.c. */
88 void init_registers_i386_avx512_linux (void);
89 extern const struct target_desc *tdesc_i386_avx512_linux;
90
91 /* Defined in auto-generated file i386-mpx-linux.c. */
92 void init_registers_i386_mpx_linux (void);
93 extern const struct target_desc *tdesc_i386_mpx_linux;
94
95 #ifdef __x86_64__
96 static struct target_desc *tdesc_amd64_linux_no_xml;
97 #endif
98 static struct target_desc *tdesc_i386_linux_no_xml;
99
100
101 static unsigned char jump_insn[] = { 0xe9, 0, 0, 0, 0 };
102 static unsigned char small_jump_insn[] = { 0x66, 0xe9, 0, 0 };
103
104 /* Backward compatibility for gdb without XML support. */
105
106 static const char *xmltarget_i386_linux_no_xml = "@<target>\
107 <architecture>i386</architecture>\
108 <osabi>GNU/Linux</osabi>\
109 </target>";
110
111 #ifdef __x86_64__
112 static const char *xmltarget_amd64_linux_no_xml = "@<target>\
113 <architecture>i386:x86-64</architecture>\
114 <osabi>GNU/Linux</osabi>\
115 </target>";
116 #endif
117
118 #include <sys/reg.h>
119 #include <sys/procfs.h>
120 #include <sys/ptrace.h>
121 #include <sys/uio.h>
122
123 #ifndef PTRACE_GETREGSET
124 #define PTRACE_GETREGSET 0x4204
125 #endif
126
127 #ifndef PTRACE_SETREGSET
128 #define PTRACE_SETREGSET 0x4205
129 #endif
130
131
132 #ifndef PTRACE_GET_THREAD_AREA
133 #define PTRACE_GET_THREAD_AREA 25
134 #endif
135
136 /* This definition comes from prctl.h, but some kernels may not have it. */
137 #ifndef PTRACE_ARCH_PRCTL
138 #define PTRACE_ARCH_PRCTL 30
139 #endif
140
141 /* The following definitions come from prctl.h, but may be absent
142 for certain configurations. */
143 #ifndef ARCH_GET_FS
144 #define ARCH_SET_GS 0x1001
145 #define ARCH_SET_FS 0x1002
146 #define ARCH_GET_FS 0x1003
147 #define ARCH_GET_GS 0x1004
148 #endif
149
150 /* Per-process arch-specific data we want to keep. */
151
152 struct arch_process_info
153 {
154 struct x86_debug_reg_state debug_reg_state;
155 };
156
157 #ifdef __x86_64__
158
159 /* Mapping between the general-purpose registers in `struct user'
160 format and GDB's register array layout.
161 Note that the transfer layout uses 64-bit regs. */
162 static /*const*/ int i386_regmap[] =
163 {
164 RAX * 8, RCX * 8, RDX * 8, RBX * 8,
165 RSP * 8, RBP * 8, RSI * 8, RDI * 8,
166 RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
167 DS * 8, ES * 8, FS * 8, GS * 8
168 };
169
170 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
171
172 /* So code below doesn't have to care, i386 or amd64. */
173 #define ORIG_EAX ORIG_RAX
174 #define REGSIZE 8
175
176 static const int x86_64_regmap[] =
177 {
178 RAX * 8, RBX * 8, RCX * 8, RDX * 8,
179 RSI * 8, RDI * 8, RBP * 8, RSP * 8,
180 R8 * 8, R9 * 8, R10 * 8, R11 * 8,
181 R12 * 8, R13 * 8, R14 * 8, R15 * 8,
182 RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
183 DS * 8, ES * 8, FS * 8, GS * 8,
184 -1, -1, -1, -1, -1, -1, -1, -1,
185 -1, -1, -1, -1, -1, -1, -1, -1,
186 -1, -1, -1, -1, -1, -1, -1, -1,
187 -1,
188 -1, -1, -1, -1, -1, -1, -1, -1,
189 ORIG_RAX * 8,
190 -1, -1, -1, -1, /* MPX registers BND0 ... BND3. */
191 -1, -1, /* MPX registers BNDCFGU, BNDSTATUS. */
192 -1, -1, -1, -1, -1, -1, -1, -1, /* xmm16 ... xmm31 (AVX512) */
193 -1, -1, -1, -1, -1, -1, -1, -1,
194 -1, -1, -1, -1, -1, -1, -1, -1, /* ymm16 ... ymm31 (AVX512) */
195 -1, -1, -1, -1, -1, -1, -1, -1,
196 -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512) */
197 -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm31 (AVX512) */
198 -1, -1, -1, -1, -1, -1, -1, -1,
199 -1, -1, -1, -1, -1, -1, -1, -1,
200 -1, -1, -1, -1, -1, -1, -1, -1
201 };
202
203 #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0]))
204 #define X86_64_USER_REGS (GS + 1)
205
206 #else /* ! __x86_64__ */
207
208 /* Mapping between the general-purpose registers in `struct user'
209 format and GDB's register array layout. */
210 static /*const*/ int i386_regmap[] =
211 {
212 EAX * 4, ECX * 4, EDX * 4, EBX * 4,
213 UESP * 4, EBP * 4, ESI * 4, EDI * 4,
214 EIP * 4, EFL * 4, CS * 4, SS * 4,
215 DS * 4, ES * 4, FS * 4, GS * 4
216 };
217
218 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
219
220 #define REGSIZE 4
221
222 #endif
223
224 #ifdef __x86_64__
225
226 /* Returns true if the current inferior belongs to a x86-64 process,
227 per the tdesc. */
228
229 static int
230 is_64bit_tdesc (void)
231 {
232 struct regcache *regcache = get_thread_regcache (current_thread, 0);
233
234 return register_size (regcache->tdesc, 0) == 8;
235 }
236
237 #endif
238
239 \f
240 /* Called by libthread_db. */
241
242 ps_err_e
243 ps_get_thread_area (const struct ps_prochandle *ph,
244 lwpid_t lwpid, int idx, void **base)
245 {
246 #ifdef __x86_64__
247 int use_64bit = is_64bit_tdesc ();
248
249 if (use_64bit)
250 {
251 switch (idx)
252 {
253 case FS:
254 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
255 return PS_OK;
256 break;
257 case GS:
258 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
259 return PS_OK;
260 break;
261 default:
262 return PS_BADADDR;
263 }
264 return PS_ERR;
265 }
266 #endif
267
268 {
269 unsigned int desc[4];
270
271 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
272 (void *) (intptr_t) idx, (unsigned long) &desc) < 0)
273 return PS_ERR;
274
275 /* Ensure we properly extend the value to 64-bits for x86_64. */
276 *base = (void *) (uintptr_t) desc[1];
277 return PS_OK;
278 }
279 }
280
281 /* Get the thread area address. This is used to recognize which
282 thread is which when tracing with the in-process agent library. We
283 don't read anything from the address, and treat it as opaque; it's
284 the address itself that we assume is unique per-thread. */
285
286 static int
287 x86_get_thread_area (int lwpid, CORE_ADDR *addr)
288 {
289 #ifdef __x86_64__
290 int use_64bit = is_64bit_tdesc ();
291
292 if (use_64bit)
293 {
294 void *base;
295 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, &base, ARCH_GET_FS) == 0)
296 {
297 *addr = (CORE_ADDR) (uintptr_t) base;
298 return 0;
299 }
300
301 return -1;
302 }
303 #endif
304
305 {
306 struct lwp_info *lwp = find_lwp_pid (pid_to_ptid (lwpid));
307 struct thread_info *thr = get_lwp_thread (lwp);
308 struct regcache *regcache = get_thread_regcache (thr, 1);
309 unsigned int desc[4];
310 ULONGEST gs = 0;
311 const int reg_thread_area = 3; /* bits to scale down register value. */
312 int idx;
313
314 collect_register_by_name (regcache, "gs", &gs);
315
316 idx = gs >> reg_thread_area;
317
318 if (ptrace (PTRACE_GET_THREAD_AREA,
319 lwpid_of (thr),
320 (void *) (long) idx, (unsigned long) &desc) < 0)
321 return -1;
322
323 *addr = desc[1];
324 return 0;
325 }
326 }
327
328
329 \f
330 static int
331 x86_cannot_store_register (int regno)
332 {
333 #ifdef __x86_64__
334 if (is_64bit_tdesc ())
335 return 0;
336 #endif
337
338 return regno >= I386_NUM_REGS;
339 }
340
341 static int
342 x86_cannot_fetch_register (int regno)
343 {
344 #ifdef __x86_64__
345 if (is_64bit_tdesc ())
346 return 0;
347 #endif
348
349 return regno >= I386_NUM_REGS;
350 }
351
352 static void
353 x86_fill_gregset (struct regcache *regcache, void *buf)
354 {
355 int i;
356
357 #ifdef __x86_64__
358 if (register_size (regcache->tdesc, 0) == 8)
359 {
360 for (i = 0; i < X86_64_NUM_REGS; i++)
361 if (x86_64_regmap[i] != -1)
362 collect_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
363 return;
364 }
365
366 /* 32-bit inferior registers need to be zero-extended.
367 Callers would read uninitialized memory otherwise. */
368 memset (buf, 0x00, X86_64_USER_REGS * 8);
369 #endif
370
371 for (i = 0; i < I386_NUM_REGS; i++)
372 collect_register (regcache, i, ((char *) buf) + i386_regmap[i]);
373
374 collect_register_by_name (regcache, "orig_eax",
375 ((char *) buf) + ORIG_EAX * REGSIZE);
376 }
377
378 static void
379 x86_store_gregset (struct regcache *regcache, const void *buf)
380 {
381 int i;
382
383 #ifdef __x86_64__
384 if (register_size (regcache->tdesc, 0) == 8)
385 {
386 for (i = 0; i < X86_64_NUM_REGS; i++)
387 if (x86_64_regmap[i] != -1)
388 supply_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
389 return;
390 }
391 #endif
392
393 for (i = 0; i < I386_NUM_REGS; i++)
394 supply_register (regcache, i, ((char *) buf) + i386_regmap[i]);
395
396 supply_register_by_name (regcache, "orig_eax",
397 ((char *) buf) + ORIG_EAX * REGSIZE);
398 }
399
400 static void
401 x86_fill_fpregset (struct regcache *regcache, void *buf)
402 {
403 #ifdef __x86_64__
404 i387_cache_to_fxsave (regcache, buf);
405 #else
406 i387_cache_to_fsave (regcache, buf);
407 #endif
408 }
409
410 static void
411 x86_store_fpregset (struct regcache *regcache, const void *buf)
412 {
413 #ifdef __x86_64__
414 i387_fxsave_to_cache (regcache, buf);
415 #else
416 i387_fsave_to_cache (regcache, buf);
417 #endif
418 }
419
420 #ifndef __x86_64__
421
422 static void
423 x86_fill_fpxregset (struct regcache *regcache, void *buf)
424 {
425 i387_cache_to_fxsave (regcache, buf);
426 }
427
428 static void
429 x86_store_fpxregset (struct regcache *regcache, const void *buf)
430 {
431 i387_fxsave_to_cache (regcache, buf);
432 }
433
434 #endif
435
436 static void
437 x86_fill_xstateregset (struct regcache *regcache, void *buf)
438 {
439 i387_cache_to_xsave (regcache, buf);
440 }
441
442 static void
443 x86_store_xstateregset (struct regcache *regcache, const void *buf)
444 {
445 i387_xsave_to_cache (regcache, buf);
446 }
447
448 /* ??? The non-biarch i386 case stores all the i387 regs twice.
449 Once in i387_.*fsave.* and once in i387_.*fxsave.*.
450 This is, presumably, to handle the case where PTRACE_[GS]ETFPXREGS
451 doesn't work. IWBN to avoid the duplication in the case where it
452 does work. Maybe the arch_setup routine could check whether it works
453 and update the supported regsets accordingly. */
454
455 static struct regset_info x86_regsets[] =
456 {
457 #ifdef HAVE_PTRACE_GETREGS
458 { PTRACE_GETREGS, PTRACE_SETREGS, 0, sizeof (elf_gregset_t),
459 GENERAL_REGS,
460 x86_fill_gregset, x86_store_gregset },
461 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_X86_XSTATE, 0,
462 EXTENDED_REGS, x86_fill_xstateregset, x86_store_xstateregset },
463 # ifndef __x86_64__
464 # ifdef HAVE_PTRACE_GETFPXREGS
465 { PTRACE_GETFPXREGS, PTRACE_SETFPXREGS, 0, sizeof (elf_fpxregset_t),
466 EXTENDED_REGS,
467 x86_fill_fpxregset, x86_store_fpxregset },
468 # endif
469 # endif
470 { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, sizeof (elf_fpregset_t),
471 FP_REGS,
472 x86_fill_fpregset, x86_store_fpregset },
473 #endif /* HAVE_PTRACE_GETREGS */
474 { 0, 0, 0, -1, -1, NULL, NULL }
475 };
476
477 static CORE_ADDR
478 x86_get_pc (struct regcache *regcache)
479 {
480 int use_64bit = register_size (regcache->tdesc, 0) == 8;
481
482 if (use_64bit)
483 {
484 unsigned long pc;
485 collect_register_by_name (regcache, "rip", &pc);
486 return (CORE_ADDR) pc;
487 }
488 else
489 {
490 unsigned int pc;
491 collect_register_by_name (regcache, "eip", &pc);
492 return (CORE_ADDR) pc;
493 }
494 }
495
496 static void
497 x86_set_pc (struct regcache *regcache, CORE_ADDR pc)
498 {
499 int use_64bit = register_size (regcache->tdesc, 0) == 8;
500
501 if (use_64bit)
502 {
503 unsigned long newpc = pc;
504 supply_register_by_name (regcache, "rip", &newpc);
505 }
506 else
507 {
508 unsigned int newpc = pc;
509 supply_register_by_name (regcache, "eip", &newpc);
510 }
511 }
512 \f
513 static const unsigned char x86_breakpoint[] = { 0xCC };
514 #define x86_breakpoint_len 1
515
516 static int
517 x86_breakpoint_at (CORE_ADDR pc)
518 {
519 unsigned char c;
520
521 (*the_target->read_memory) (pc, &c, 1);
522 if (c == 0xCC)
523 return 1;
524
525 return 0;
526 }
527 \f
528 /* Low-level function vector. */
529 struct x86_dr_low_type x86_dr_low =
530 {
531 x86_linux_dr_set_control,
532 x86_linux_dr_set_addr,
533 x86_linux_dr_get_addr,
534 x86_linux_dr_get_status,
535 x86_linux_dr_get_control,
536 sizeof (void *),
537 };
538 \f
539 /* Breakpoint/Watchpoint support. */
540
541 static int
542 x86_supports_z_point_type (char z_type)
543 {
544 switch (z_type)
545 {
546 case Z_PACKET_SW_BP:
547 case Z_PACKET_HW_BP:
548 case Z_PACKET_WRITE_WP:
549 case Z_PACKET_ACCESS_WP:
550 return 1;
551 default:
552 return 0;
553 }
554 }
555
556 static int
557 x86_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
558 int size, struct raw_breakpoint *bp)
559 {
560 struct process_info *proc = current_process ();
561
562 switch (type)
563 {
564 case raw_bkpt_type_sw:
565 return insert_memory_breakpoint (bp);
566
567 case raw_bkpt_type_hw:
568 case raw_bkpt_type_write_wp:
569 case raw_bkpt_type_access_wp:
570 {
571 enum target_hw_bp_type hw_type
572 = raw_bkpt_type_to_target_hw_bp_type (type);
573 struct x86_debug_reg_state *state
574 = &proc->priv->arch_private->debug_reg_state;
575
576 return x86_dr_insert_watchpoint (state, hw_type, addr, size);
577 }
578
579 default:
580 /* Unsupported. */
581 return 1;
582 }
583 }
584
585 static int
586 x86_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
587 int size, struct raw_breakpoint *bp)
588 {
589 struct process_info *proc = current_process ();
590
591 switch (type)
592 {
593 case raw_bkpt_type_sw:
594 return remove_memory_breakpoint (bp);
595
596 case raw_bkpt_type_hw:
597 case raw_bkpt_type_write_wp:
598 case raw_bkpt_type_access_wp:
599 {
600 enum target_hw_bp_type hw_type
601 = raw_bkpt_type_to_target_hw_bp_type (type);
602 struct x86_debug_reg_state *state
603 = &proc->priv->arch_private->debug_reg_state;
604
605 return x86_dr_remove_watchpoint (state, hw_type, addr, size);
606 }
607 default:
608 /* Unsupported. */
609 return 1;
610 }
611 }
612
613 static int
614 x86_stopped_by_watchpoint (void)
615 {
616 struct process_info *proc = current_process ();
617 return x86_dr_stopped_by_watchpoint (&proc->priv->arch_private->debug_reg_state);
618 }
619
620 static CORE_ADDR
621 x86_stopped_data_address (void)
622 {
623 struct process_info *proc = current_process ();
624 CORE_ADDR addr;
625 if (x86_dr_stopped_data_address (&proc->priv->arch_private->debug_reg_state,
626 &addr))
627 return addr;
628 return 0;
629 }
630 \f
631 /* Called when a new process is created. */
632
633 static struct arch_process_info *
634 x86_linux_new_process (void)
635 {
636 struct arch_process_info *info = XCNEW (struct arch_process_info);
637
638 x86_low_init_dregs (&info->debug_reg_state);
639
640 return info;
641 }
642
643 /* See nat/x86-dregs.h. */
644
645 struct x86_debug_reg_state *
646 x86_debug_reg_state (pid_t pid)
647 {
648 struct process_info *proc = find_process_pid (pid);
649
650 return &proc->priv->arch_private->debug_reg_state;
651 }
652 \f
653 /* When GDBSERVER is built as a 64-bit application on linux, the
654 PTRACE_GETSIGINFO data is always presented in 64-bit layout. Since
655 debugging a 32-bit inferior with a 64-bit GDBSERVER should look the same
656 as debugging it with a 32-bit GDBSERVER, we do the 32-bit <-> 64-bit
657 conversion in-place ourselves. */
658
659 /* These types below (compat_*) define a siginfo type that is layout
660 compatible with the siginfo type exported by the 32-bit userspace
661 support. */
662
663 #ifdef __x86_64__
664
665 typedef int compat_int_t;
666 typedef unsigned int compat_uptr_t;
667
668 typedef int compat_time_t;
669 typedef int compat_timer_t;
670 typedef int compat_clock_t;
671
672 struct compat_timeval
673 {
674 compat_time_t tv_sec;
675 int tv_usec;
676 };
677
678 typedef union compat_sigval
679 {
680 compat_int_t sival_int;
681 compat_uptr_t sival_ptr;
682 } compat_sigval_t;
683
684 typedef struct compat_siginfo
685 {
686 int si_signo;
687 int si_errno;
688 int si_code;
689
690 union
691 {
692 int _pad[((128 / sizeof (int)) - 3)];
693
694 /* kill() */
695 struct
696 {
697 unsigned int _pid;
698 unsigned int _uid;
699 } _kill;
700
701 /* POSIX.1b timers */
702 struct
703 {
704 compat_timer_t _tid;
705 int _overrun;
706 compat_sigval_t _sigval;
707 } _timer;
708
709 /* POSIX.1b signals */
710 struct
711 {
712 unsigned int _pid;
713 unsigned int _uid;
714 compat_sigval_t _sigval;
715 } _rt;
716
717 /* SIGCHLD */
718 struct
719 {
720 unsigned int _pid;
721 unsigned int _uid;
722 int _status;
723 compat_clock_t _utime;
724 compat_clock_t _stime;
725 } _sigchld;
726
727 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
728 struct
729 {
730 unsigned int _addr;
731 } _sigfault;
732
733 /* SIGPOLL */
734 struct
735 {
736 int _band;
737 int _fd;
738 } _sigpoll;
739 } _sifields;
740 } compat_siginfo_t;
741
742 /* For x32, clock_t in _sigchld is 64bit aligned at 4 bytes. */
743 typedef long __attribute__ ((__aligned__ (4))) compat_x32_clock_t;
744
745 typedef struct compat_x32_siginfo
746 {
747 int si_signo;
748 int si_errno;
749 int si_code;
750
751 union
752 {
753 int _pad[((128 / sizeof (int)) - 3)];
754
755 /* kill() */
756 struct
757 {
758 unsigned int _pid;
759 unsigned int _uid;
760 } _kill;
761
762 /* POSIX.1b timers */
763 struct
764 {
765 compat_timer_t _tid;
766 int _overrun;
767 compat_sigval_t _sigval;
768 } _timer;
769
770 /* POSIX.1b signals */
771 struct
772 {
773 unsigned int _pid;
774 unsigned int _uid;
775 compat_sigval_t _sigval;
776 } _rt;
777
778 /* SIGCHLD */
779 struct
780 {
781 unsigned int _pid;
782 unsigned int _uid;
783 int _status;
784 compat_x32_clock_t _utime;
785 compat_x32_clock_t _stime;
786 } _sigchld;
787
788 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
789 struct
790 {
791 unsigned int _addr;
792 } _sigfault;
793
794 /* SIGPOLL */
795 struct
796 {
797 int _band;
798 int _fd;
799 } _sigpoll;
800 } _sifields;
801 } compat_x32_siginfo_t __attribute__ ((__aligned__ (8)));
802
803 #define cpt_si_pid _sifields._kill._pid
804 #define cpt_si_uid _sifields._kill._uid
805 #define cpt_si_timerid _sifields._timer._tid
806 #define cpt_si_overrun _sifields._timer._overrun
807 #define cpt_si_status _sifields._sigchld._status
808 #define cpt_si_utime _sifields._sigchld._utime
809 #define cpt_si_stime _sifields._sigchld._stime
810 #define cpt_si_ptr _sifields._rt._sigval.sival_ptr
811 #define cpt_si_addr _sifields._sigfault._addr
812 #define cpt_si_band _sifields._sigpoll._band
813 #define cpt_si_fd _sifields._sigpoll._fd
814
815 /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
816 In their place is si_timer1,si_timer2. */
817 #ifndef si_timerid
818 #define si_timerid si_timer1
819 #endif
820 #ifndef si_overrun
821 #define si_overrun si_timer2
822 #endif
823
824 static void
825 compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from)
826 {
827 memset (to, 0, sizeof (*to));
828
829 to->si_signo = from->si_signo;
830 to->si_errno = from->si_errno;
831 to->si_code = from->si_code;
832
833 if (to->si_code == SI_TIMER)
834 {
835 to->cpt_si_timerid = from->si_timerid;
836 to->cpt_si_overrun = from->si_overrun;
837 to->cpt_si_ptr = (intptr_t) from->si_ptr;
838 }
839 else if (to->si_code == SI_USER)
840 {
841 to->cpt_si_pid = from->si_pid;
842 to->cpt_si_uid = from->si_uid;
843 }
844 else if (to->si_code < 0)
845 {
846 to->cpt_si_pid = from->si_pid;
847 to->cpt_si_uid = from->si_uid;
848 to->cpt_si_ptr = (intptr_t) from->si_ptr;
849 }
850 else
851 {
852 switch (to->si_signo)
853 {
854 case SIGCHLD:
855 to->cpt_si_pid = from->si_pid;
856 to->cpt_si_uid = from->si_uid;
857 to->cpt_si_status = from->si_status;
858 to->cpt_si_utime = from->si_utime;
859 to->cpt_si_stime = from->si_stime;
860 break;
861 case SIGILL:
862 case SIGFPE:
863 case SIGSEGV:
864 case SIGBUS:
865 to->cpt_si_addr = (intptr_t) from->si_addr;
866 break;
867 case SIGPOLL:
868 to->cpt_si_band = from->si_band;
869 to->cpt_si_fd = from->si_fd;
870 break;
871 default:
872 to->cpt_si_pid = from->si_pid;
873 to->cpt_si_uid = from->si_uid;
874 to->cpt_si_ptr = (intptr_t) from->si_ptr;
875 break;
876 }
877 }
878 }
879
880 static void
881 siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from)
882 {
883 memset (to, 0, sizeof (*to));
884
885 to->si_signo = from->si_signo;
886 to->si_errno = from->si_errno;
887 to->si_code = from->si_code;
888
889 if (to->si_code == SI_TIMER)
890 {
891 to->si_timerid = from->cpt_si_timerid;
892 to->si_overrun = from->cpt_si_overrun;
893 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
894 }
895 else if (to->si_code == SI_USER)
896 {
897 to->si_pid = from->cpt_si_pid;
898 to->si_uid = from->cpt_si_uid;
899 }
900 else if (to->si_code < 0)
901 {
902 to->si_pid = from->cpt_si_pid;
903 to->si_uid = from->cpt_si_uid;
904 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
905 }
906 else
907 {
908 switch (to->si_signo)
909 {
910 case SIGCHLD:
911 to->si_pid = from->cpt_si_pid;
912 to->si_uid = from->cpt_si_uid;
913 to->si_status = from->cpt_si_status;
914 to->si_utime = from->cpt_si_utime;
915 to->si_stime = from->cpt_si_stime;
916 break;
917 case SIGILL:
918 case SIGFPE:
919 case SIGSEGV:
920 case SIGBUS:
921 to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
922 break;
923 case SIGPOLL:
924 to->si_band = from->cpt_si_band;
925 to->si_fd = from->cpt_si_fd;
926 break;
927 default:
928 to->si_pid = from->cpt_si_pid;
929 to->si_uid = from->cpt_si_uid;
930 to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
931 break;
932 }
933 }
934 }
935
936 static void
937 compat_x32_siginfo_from_siginfo (compat_x32_siginfo_t *to,
938 siginfo_t *from)
939 {
940 memset (to, 0, sizeof (*to));
941
942 to->si_signo = from->si_signo;
943 to->si_errno = from->si_errno;
944 to->si_code = from->si_code;
945
946 if (to->si_code == SI_TIMER)
947 {
948 to->cpt_si_timerid = from->si_timerid;
949 to->cpt_si_overrun = from->si_overrun;
950 to->cpt_si_ptr = (intptr_t) from->si_ptr;
951 }
952 else if (to->si_code == SI_USER)
953 {
954 to->cpt_si_pid = from->si_pid;
955 to->cpt_si_uid = from->si_uid;
956 }
957 else if (to->si_code < 0)
958 {
959 to->cpt_si_pid = from->si_pid;
960 to->cpt_si_uid = from->si_uid;
961 to->cpt_si_ptr = (intptr_t) from->si_ptr;
962 }
963 else
964 {
965 switch (to->si_signo)
966 {
967 case SIGCHLD:
968 to->cpt_si_pid = from->si_pid;
969 to->cpt_si_uid = from->si_uid;
970 to->cpt_si_status = from->si_status;
971 to->cpt_si_utime = from->si_utime;
972 to->cpt_si_stime = from->si_stime;
973 break;
974 case SIGILL:
975 case SIGFPE:
976 case SIGSEGV:
977 case SIGBUS:
978 to->cpt_si_addr = (intptr_t) from->si_addr;
979 break;
980 case SIGPOLL:
981 to->cpt_si_band = from->si_band;
982 to->cpt_si_fd = from->si_fd;
983 break;
984 default:
985 to->cpt_si_pid = from->si_pid;
986 to->cpt_si_uid = from->si_uid;
987 to->cpt_si_ptr = (intptr_t) from->si_ptr;
988 break;
989 }
990 }
991 }
992
993 static void
994 siginfo_from_compat_x32_siginfo (siginfo_t *to,
995 compat_x32_siginfo_t *from)
996 {
997 memset (to, 0, sizeof (*to));
998
999 to->si_signo = from->si_signo;
1000 to->si_errno = from->si_errno;
1001 to->si_code = from->si_code;
1002
1003 if (to->si_code == SI_TIMER)
1004 {
1005 to->si_timerid = from->cpt_si_timerid;
1006 to->si_overrun = from->cpt_si_overrun;
1007 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
1008 }
1009 else if (to->si_code == SI_USER)
1010 {
1011 to->si_pid = from->cpt_si_pid;
1012 to->si_uid = from->cpt_si_uid;
1013 }
1014 else if (to->si_code < 0)
1015 {
1016 to->si_pid = from->cpt_si_pid;
1017 to->si_uid = from->cpt_si_uid;
1018 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
1019 }
1020 else
1021 {
1022 switch (to->si_signo)
1023 {
1024 case SIGCHLD:
1025 to->si_pid = from->cpt_si_pid;
1026 to->si_uid = from->cpt_si_uid;
1027 to->si_status = from->cpt_si_status;
1028 to->si_utime = from->cpt_si_utime;
1029 to->si_stime = from->cpt_si_stime;
1030 break;
1031 case SIGILL:
1032 case SIGFPE:
1033 case SIGSEGV:
1034 case SIGBUS:
1035 to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
1036 break;
1037 case SIGPOLL:
1038 to->si_band = from->cpt_si_band;
1039 to->si_fd = from->cpt_si_fd;
1040 break;
1041 default:
1042 to->si_pid = from->cpt_si_pid;
1043 to->si_uid = from->cpt_si_uid;
1044 to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
1045 break;
1046 }
1047 }
1048 }
1049
1050 #endif /* __x86_64__ */
1051
1052 /* Convert a native/host siginfo object, into/from the siginfo in the
1053 layout of the inferiors' architecture. Returns true if any
1054 conversion was done; false otherwise. If DIRECTION is 1, then copy
1055 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
1056 INF. */
1057
1058 static int
1059 x86_siginfo_fixup (siginfo_t *native, void *inf, int direction)
1060 {
1061 #ifdef __x86_64__
1062 unsigned int machine;
1063 int tid = lwpid_of (current_thread);
1064 int is_elf64 = linux_pid_exe_is_elf_64_file (tid, &machine);
1065
1066 /* Is the inferior 32-bit? If so, then fixup the siginfo object. */
1067 if (!is_64bit_tdesc ())
1068 {
1069 gdb_assert (sizeof (siginfo_t) == sizeof (compat_siginfo_t));
1070
1071 if (direction == 0)
1072 compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native);
1073 else
1074 siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf);
1075
1076 return 1;
1077 }
1078 /* No fixup for native x32 GDB. */
1079 else if (!is_elf64 && sizeof (void *) == 8)
1080 {
1081 gdb_assert (sizeof (siginfo_t) == sizeof (compat_x32_siginfo_t));
1082
1083 if (direction == 0)
1084 compat_x32_siginfo_from_siginfo ((struct compat_x32_siginfo *) inf,
1085 native);
1086 else
1087 siginfo_from_compat_x32_siginfo (native,
1088 (struct compat_x32_siginfo *) inf);
1089
1090 return 1;
1091 }
1092 #endif
1093
1094 return 0;
1095 }
1096 \f
1097 static int use_xml;
1098
1099 /* Format of XSAVE extended state is:
1100 struct
1101 {
1102 fxsave_bytes[0..463]
1103 sw_usable_bytes[464..511]
1104 xstate_hdr_bytes[512..575]
1105 avx_bytes[576..831]
1106 future_state etc
1107 };
1108
1109 Same memory layout will be used for the coredump NT_X86_XSTATE
1110 representing the XSAVE extended state registers.
1111
1112 The first 8 bytes of the sw_usable_bytes[464..467] is the OS enabled
1113 extended state mask, which is the same as the extended control register
1114 0 (the XFEATURE_ENABLED_MASK register), XCR0. We can use this mask
1115 together with the mask saved in the xstate_hdr_bytes to determine what
1116 states the processor/OS supports and what state, used or initialized,
1117 the process/thread is in. */
1118 #define I386_LINUX_XSAVE_XCR0_OFFSET 464
1119
1120 /* Does the current host support the GETFPXREGS request? The header
1121 file may or may not define it, and even if it is defined, the
1122 kernel will return EIO if it's running on a pre-SSE processor. */
1123 int have_ptrace_getfpxregs =
1124 #ifdef HAVE_PTRACE_GETFPXREGS
1125 -1
1126 #else
1127 0
1128 #endif
1129 ;
1130
1131 /* Does the current host support PTRACE_GETREGSET? */
1132 static int have_ptrace_getregset = -1;
1133
1134 /* Get Linux/x86 target description from running target. */
1135
1136 static const struct target_desc *
1137 x86_linux_read_description (void)
1138 {
1139 unsigned int machine;
1140 int is_elf64;
1141 int xcr0_features;
1142 int tid;
1143 static uint64_t xcr0;
1144 struct regset_info *regset;
1145
1146 tid = lwpid_of (current_thread);
1147
1148 is_elf64 = linux_pid_exe_is_elf_64_file (tid, &machine);
1149
1150 if (sizeof (void *) == 4)
1151 {
1152 if (is_elf64 > 0)
1153 error (_("Can't debug 64-bit process with 32-bit GDBserver"));
1154 #ifndef __x86_64__
1155 else if (machine == EM_X86_64)
1156 error (_("Can't debug x86-64 process with 32-bit GDBserver"));
1157 #endif
1158 }
1159
1160 #if !defined __x86_64__ && defined HAVE_PTRACE_GETFPXREGS
1161 if (machine == EM_386 && have_ptrace_getfpxregs == -1)
1162 {
1163 elf_fpxregset_t fpxregs;
1164
1165 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (long) &fpxregs) < 0)
1166 {
1167 have_ptrace_getfpxregs = 0;
1168 have_ptrace_getregset = 0;
1169 return tdesc_i386_mmx_linux;
1170 }
1171 else
1172 have_ptrace_getfpxregs = 1;
1173 }
1174 #endif
1175
1176 if (!use_xml)
1177 {
1178 x86_xcr0 = X86_XSTATE_SSE_MASK;
1179
1180 /* Don't use XML. */
1181 #ifdef __x86_64__
1182 if (machine == EM_X86_64)
1183 return tdesc_amd64_linux_no_xml;
1184 else
1185 #endif
1186 return tdesc_i386_linux_no_xml;
1187 }
1188
1189 if (have_ptrace_getregset == -1)
1190 {
1191 uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))];
1192 struct iovec iov;
1193
1194 iov.iov_base = xstateregs;
1195 iov.iov_len = sizeof (xstateregs);
1196
1197 /* Check if PTRACE_GETREGSET works. */
1198 if (ptrace (PTRACE_GETREGSET, tid,
1199 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
1200 have_ptrace_getregset = 0;
1201 else
1202 {
1203 have_ptrace_getregset = 1;
1204
1205 /* Get XCR0 from XSAVE extended state. */
1206 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
1207 / sizeof (uint64_t))];
1208
1209 /* Use PTRACE_GETREGSET if it is available. */
1210 for (regset = x86_regsets;
1211 regset->fill_function != NULL; regset++)
1212 if (regset->get_request == PTRACE_GETREGSET)
1213 regset->size = X86_XSTATE_SIZE (xcr0);
1214 else if (regset->type != GENERAL_REGS)
1215 regset->size = 0;
1216 }
1217 }
1218
1219 /* Check the native XCR0 only if PTRACE_GETREGSET is available. */
1220 xcr0_features = (have_ptrace_getregset
1221 && (xcr0 & X86_XSTATE_ALL_MASK));
1222
1223 if (xcr0_features)
1224 x86_xcr0 = xcr0;
1225
1226 if (machine == EM_X86_64)
1227 {
1228 #ifdef __x86_64__
1229 if (is_elf64)
1230 {
1231 if (xcr0_features)
1232 {
1233 switch (xcr0 & X86_XSTATE_ALL_MASK)
1234 {
1235 case X86_XSTATE_AVX512_MASK:
1236 return tdesc_amd64_avx512_linux;
1237
1238 case X86_XSTATE_MPX_MASK:
1239 return tdesc_amd64_mpx_linux;
1240
1241 case X86_XSTATE_AVX_MASK:
1242 return tdesc_amd64_avx_linux;
1243
1244 default:
1245 return tdesc_amd64_linux;
1246 }
1247 }
1248 else
1249 return tdesc_amd64_linux;
1250 }
1251 else
1252 {
1253 if (xcr0_features)
1254 {
1255 switch (xcr0 & X86_XSTATE_ALL_MASK)
1256 {
1257 case X86_XSTATE_AVX512_MASK:
1258 return tdesc_x32_avx512_linux;
1259
1260 case X86_XSTATE_MPX_MASK: /* No MPX on x32. */
1261 case X86_XSTATE_AVX_MASK:
1262 return tdesc_x32_avx_linux;
1263
1264 default:
1265 return tdesc_x32_linux;
1266 }
1267 }
1268 else
1269 return tdesc_x32_linux;
1270 }
1271 #endif
1272 }
1273 else
1274 {
1275 if (xcr0_features)
1276 {
1277 switch (xcr0 & X86_XSTATE_ALL_MASK)
1278 {
1279 case (X86_XSTATE_AVX512_MASK):
1280 return tdesc_i386_avx512_linux;
1281
1282 case (X86_XSTATE_MPX_MASK):
1283 return tdesc_i386_mpx_linux;
1284
1285 case (X86_XSTATE_AVX_MASK):
1286 return tdesc_i386_avx_linux;
1287
1288 default:
1289 return tdesc_i386_linux;
1290 }
1291 }
1292 else
1293 return tdesc_i386_linux;
1294 }
1295
1296 gdb_assert_not_reached ("failed to return tdesc");
1297 }
1298
1299 /* Callback for find_inferior. Stops iteration when a thread with a
1300 given PID is found. */
1301
1302 static int
1303 same_process_callback (struct inferior_list_entry *entry, void *data)
1304 {
1305 int pid = *(int *) data;
1306
1307 return (ptid_get_pid (entry->id) == pid);
1308 }
1309
1310 /* Callback for for_each_inferior. Calls the arch_setup routine for
1311 each process. */
1312
1313 static void
1314 x86_arch_setup_process_callback (struct inferior_list_entry *entry)
1315 {
1316 int pid = ptid_get_pid (entry->id);
1317
1318 /* Look up any thread of this processes. */
1319 current_thread
1320 = (struct thread_info *) find_inferior (&all_threads,
1321 same_process_callback, &pid);
1322
1323 the_low_target.arch_setup ();
1324 }
1325
1326 /* Update all the target description of all processes; a new GDB
1327 connected, and it may or not support xml target descriptions. */
1328
1329 static void
1330 x86_linux_update_xmltarget (void)
1331 {
1332 struct thread_info *saved_thread = current_thread;
1333
1334 /* Before changing the register cache's internal layout, flush the
1335 contents of the current valid caches back to the threads, and
1336 release the current regcache objects. */
1337 regcache_release ();
1338
1339 for_each_inferior (&all_processes, x86_arch_setup_process_callback);
1340
1341 current_thread = saved_thread;
1342 }
1343
1344 /* Process qSupported query, "xmlRegisters=". Update the buffer size for
1345 PTRACE_GETREGSET. */
1346
1347 static void
1348 x86_linux_process_qsupported (const char *query)
1349 {
1350 /* Return if gdb doesn't support XML. If gdb sends "xmlRegisters="
1351 with "i386" in qSupported query, it supports x86 XML target
1352 descriptions. */
1353 use_xml = 0;
1354 if (query != NULL && startswith (query, "xmlRegisters="))
1355 {
1356 char *copy = xstrdup (query + 13);
1357 char *p;
1358
1359 for (p = strtok (copy, ","); p != NULL; p = strtok (NULL, ","))
1360 {
1361 if (strcmp (p, "i386") == 0)
1362 {
1363 use_xml = 1;
1364 break;
1365 }
1366 }
1367
1368 free (copy);
1369 }
1370
1371 x86_linux_update_xmltarget ();
1372 }
1373
1374 /* Common for x86/x86-64. */
1375
1376 static struct regsets_info x86_regsets_info =
1377 {
1378 x86_regsets, /* regsets */
1379 0, /* num_regsets */
1380 NULL, /* disabled_regsets */
1381 };
1382
1383 #ifdef __x86_64__
1384 static struct regs_info amd64_linux_regs_info =
1385 {
1386 NULL, /* regset_bitmap */
1387 NULL, /* usrregs_info */
1388 &x86_regsets_info
1389 };
1390 #endif
1391 static struct usrregs_info i386_linux_usrregs_info =
1392 {
1393 I386_NUM_REGS,
1394 i386_regmap,
1395 };
1396
1397 static struct regs_info i386_linux_regs_info =
1398 {
1399 NULL, /* regset_bitmap */
1400 &i386_linux_usrregs_info,
1401 &x86_regsets_info
1402 };
1403
1404 const struct regs_info *
1405 x86_linux_regs_info (void)
1406 {
1407 #ifdef __x86_64__
1408 if (is_64bit_tdesc ())
1409 return &amd64_linux_regs_info;
1410 else
1411 #endif
1412 return &i386_linux_regs_info;
1413 }
1414
1415 /* Initialize the target description for the architecture of the
1416 inferior. */
1417
1418 static void
1419 x86_arch_setup (void)
1420 {
1421 current_process ()->tdesc = x86_linux_read_description ();
1422 }
1423
1424 static int
1425 x86_supports_tracepoints (void)
1426 {
1427 return 1;
1428 }
1429
1430 static void
1431 append_insns (CORE_ADDR *to, size_t len, const unsigned char *buf)
1432 {
1433 write_inferior_memory (*to, buf, len);
1434 *to += len;
1435 }
1436
1437 static int
1438 push_opcode (unsigned char *buf, char *op)
1439 {
1440 unsigned char *buf_org = buf;
1441
1442 while (1)
1443 {
1444 char *endptr;
1445 unsigned long ul = strtoul (op, &endptr, 16);
1446
1447 if (endptr == op)
1448 break;
1449
1450 *buf++ = ul;
1451 op = endptr;
1452 }
1453
1454 return buf - buf_org;
1455 }
1456
1457 #ifdef __x86_64__
1458
1459 /* Build a jump pad that saves registers and calls a collection
1460 function. Writes a jump instruction to the jump pad to
1461 JJUMPAD_INSN. The caller is responsible to write it in at the
1462 tracepoint address. */
1463
1464 static int
1465 amd64_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, CORE_ADDR tpaddr,
1466 CORE_ADDR collector,
1467 CORE_ADDR lockaddr,
1468 ULONGEST orig_size,
1469 CORE_ADDR *jump_entry,
1470 CORE_ADDR *trampoline,
1471 ULONGEST *trampoline_size,
1472 unsigned char *jjump_pad_insn,
1473 ULONGEST *jjump_pad_insn_size,
1474 CORE_ADDR *adjusted_insn_addr,
1475 CORE_ADDR *adjusted_insn_addr_end,
1476 char *err)
1477 {
1478 unsigned char buf[40];
1479 int i, offset;
1480 int64_t loffset;
1481
1482 CORE_ADDR buildaddr = *jump_entry;
1483
1484 /* Build the jump pad. */
1485
1486 /* First, do tracepoint data collection. Save registers. */
1487 i = 0;
1488 /* Need to ensure stack pointer saved first. */
1489 buf[i++] = 0x54; /* push %rsp */
1490 buf[i++] = 0x55; /* push %rbp */
1491 buf[i++] = 0x57; /* push %rdi */
1492 buf[i++] = 0x56; /* push %rsi */
1493 buf[i++] = 0x52; /* push %rdx */
1494 buf[i++] = 0x51; /* push %rcx */
1495 buf[i++] = 0x53; /* push %rbx */
1496 buf[i++] = 0x50; /* push %rax */
1497 buf[i++] = 0x41; buf[i++] = 0x57; /* push %r15 */
1498 buf[i++] = 0x41; buf[i++] = 0x56; /* push %r14 */
1499 buf[i++] = 0x41; buf[i++] = 0x55; /* push %r13 */
1500 buf[i++] = 0x41; buf[i++] = 0x54; /* push %r12 */
1501 buf[i++] = 0x41; buf[i++] = 0x53; /* push %r11 */
1502 buf[i++] = 0x41; buf[i++] = 0x52; /* push %r10 */
1503 buf[i++] = 0x41; buf[i++] = 0x51; /* push %r9 */
1504 buf[i++] = 0x41; buf[i++] = 0x50; /* push %r8 */
1505 buf[i++] = 0x9c; /* pushfq */
1506 buf[i++] = 0x48; /* movl <addr>,%rdi */
1507 buf[i++] = 0xbf;
1508 *((unsigned long *)(buf + i)) = (unsigned long) tpaddr;
1509 i += sizeof (unsigned long);
1510 buf[i++] = 0x57; /* push %rdi */
1511 append_insns (&buildaddr, i, buf);
1512
1513 /* Stack space for the collecting_t object. */
1514 i = 0;
1515 i += push_opcode (&buf[i], "48 83 ec 18"); /* sub $0x18,%rsp */
1516 i += push_opcode (&buf[i], "48 b8"); /* mov <tpoint>,%rax */
1517 memcpy (buf + i, &tpoint, 8);
1518 i += 8;
1519 i += push_opcode (&buf[i], "48 89 04 24"); /* mov %rax,(%rsp) */
1520 i += push_opcode (&buf[i],
1521 "64 48 8b 04 25 00 00 00 00"); /* mov %fs:0x0,%rax */
1522 i += push_opcode (&buf[i], "48 89 44 24 08"); /* mov %rax,0x8(%rsp) */
1523 append_insns (&buildaddr, i, buf);
1524
1525 /* spin-lock. */
1526 i = 0;
1527 i += push_opcode (&buf[i], "48 be"); /* movl <lockaddr>,%rsi */
1528 memcpy (&buf[i], (void *) &lockaddr, 8);
1529 i += 8;
1530 i += push_opcode (&buf[i], "48 89 e1"); /* mov %rsp,%rcx */
1531 i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */
1532 i += push_opcode (&buf[i], "f0 48 0f b1 0e"); /* lock cmpxchg %rcx,(%rsi) */
1533 i += push_opcode (&buf[i], "48 85 c0"); /* test %rax,%rax */
1534 i += push_opcode (&buf[i], "75 f4"); /* jne <again> */
1535 append_insns (&buildaddr, i, buf);
1536
1537 /* Set up the gdb_collect call. */
1538 /* At this point, (stack pointer + 0x18) is the base of our saved
1539 register block. */
1540
1541 i = 0;
1542 i += push_opcode (&buf[i], "48 89 e6"); /* mov %rsp,%rsi */
1543 i += push_opcode (&buf[i], "48 83 c6 18"); /* add $0x18,%rsi */
1544
1545 /* tpoint address may be 64-bit wide. */
1546 i += push_opcode (&buf[i], "48 bf"); /* movl <addr>,%rdi */
1547 memcpy (buf + i, &tpoint, 8);
1548 i += 8;
1549 append_insns (&buildaddr, i, buf);
1550
1551 /* The collector function being in the shared library, may be
1552 >31-bits away off the jump pad. */
1553 i = 0;
1554 i += push_opcode (&buf[i], "48 b8"); /* mov $collector,%rax */
1555 memcpy (buf + i, &collector, 8);
1556 i += 8;
1557 i += push_opcode (&buf[i], "ff d0"); /* callq *%rax */
1558 append_insns (&buildaddr, i, buf);
1559
1560 /* Clear the spin-lock. */
1561 i = 0;
1562 i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */
1563 i += push_opcode (&buf[i], "48 a3"); /* mov %rax, lockaddr */
1564 memcpy (buf + i, &lockaddr, 8);
1565 i += 8;
1566 append_insns (&buildaddr, i, buf);
1567
1568 /* Remove stack that had been used for the collect_t object. */
1569 i = 0;
1570 i += push_opcode (&buf[i], "48 83 c4 18"); /* add $0x18,%rsp */
1571 append_insns (&buildaddr, i, buf);
1572
1573 /* Restore register state. */
1574 i = 0;
1575 buf[i++] = 0x48; /* add $0x8,%rsp */
1576 buf[i++] = 0x83;
1577 buf[i++] = 0xc4;
1578 buf[i++] = 0x08;
1579 buf[i++] = 0x9d; /* popfq */
1580 buf[i++] = 0x41; buf[i++] = 0x58; /* pop %r8 */
1581 buf[i++] = 0x41; buf[i++] = 0x59; /* pop %r9 */
1582 buf[i++] = 0x41; buf[i++] = 0x5a; /* pop %r10 */
1583 buf[i++] = 0x41; buf[i++] = 0x5b; /* pop %r11 */
1584 buf[i++] = 0x41; buf[i++] = 0x5c; /* pop %r12 */
1585 buf[i++] = 0x41; buf[i++] = 0x5d; /* pop %r13 */
1586 buf[i++] = 0x41; buf[i++] = 0x5e; /* pop %r14 */
1587 buf[i++] = 0x41; buf[i++] = 0x5f; /* pop %r15 */
1588 buf[i++] = 0x58; /* pop %rax */
1589 buf[i++] = 0x5b; /* pop %rbx */
1590 buf[i++] = 0x59; /* pop %rcx */
1591 buf[i++] = 0x5a; /* pop %rdx */
1592 buf[i++] = 0x5e; /* pop %rsi */
1593 buf[i++] = 0x5f; /* pop %rdi */
1594 buf[i++] = 0x5d; /* pop %rbp */
1595 buf[i++] = 0x5c; /* pop %rsp */
1596 append_insns (&buildaddr, i, buf);
1597
1598 /* Now, adjust the original instruction to execute in the jump
1599 pad. */
1600 *adjusted_insn_addr = buildaddr;
1601 relocate_instruction (&buildaddr, tpaddr);
1602 *adjusted_insn_addr_end = buildaddr;
1603
1604 /* Finally, write a jump back to the program. */
1605
1606 loffset = (tpaddr + orig_size) - (buildaddr + sizeof (jump_insn));
1607 if (loffset > INT_MAX || loffset < INT_MIN)
1608 {
1609 sprintf (err,
1610 "E.Jump back from jump pad too far from tracepoint "
1611 "(offset 0x%" PRIx64 " > int32).", loffset);
1612 return 1;
1613 }
1614
1615 offset = (int) loffset;
1616 memcpy (buf, jump_insn, sizeof (jump_insn));
1617 memcpy (buf + 1, &offset, 4);
1618 append_insns (&buildaddr, sizeof (jump_insn), buf);
1619
1620 /* The jump pad is now built. Wire in a jump to our jump pad. This
1621 is always done last (by our caller actually), so that we can
1622 install fast tracepoints with threads running. This relies on
1623 the agent's atomic write support. */
1624 loffset = *jump_entry - (tpaddr + sizeof (jump_insn));
1625 if (loffset > INT_MAX || loffset < INT_MIN)
1626 {
1627 sprintf (err,
1628 "E.Jump pad too far from tracepoint "
1629 "(offset 0x%" PRIx64 " > int32).", loffset);
1630 return 1;
1631 }
1632
1633 offset = (int) loffset;
1634
1635 memcpy (buf, jump_insn, sizeof (jump_insn));
1636 memcpy (buf + 1, &offset, 4);
1637 memcpy (jjump_pad_insn, buf, sizeof (jump_insn));
1638 *jjump_pad_insn_size = sizeof (jump_insn);
1639
1640 /* Return the end address of our pad. */
1641 *jump_entry = buildaddr;
1642
1643 return 0;
1644 }
1645
1646 #endif /* __x86_64__ */
1647
1648 /* Build a jump pad that saves registers and calls a collection
1649 function. Writes a jump instruction to the jump pad to
1650 JJUMPAD_INSN. The caller is responsible to write it in at the
1651 tracepoint address. */
1652
1653 static int
1654 i386_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, CORE_ADDR tpaddr,
1655 CORE_ADDR collector,
1656 CORE_ADDR lockaddr,
1657 ULONGEST orig_size,
1658 CORE_ADDR *jump_entry,
1659 CORE_ADDR *trampoline,
1660 ULONGEST *trampoline_size,
1661 unsigned char *jjump_pad_insn,
1662 ULONGEST *jjump_pad_insn_size,
1663 CORE_ADDR *adjusted_insn_addr,
1664 CORE_ADDR *adjusted_insn_addr_end,
1665 char *err)
1666 {
1667 unsigned char buf[0x100];
1668 int i, offset;
1669 CORE_ADDR buildaddr = *jump_entry;
1670
1671 /* Build the jump pad. */
1672
1673 /* First, do tracepoint data collection. Save registers. */
1674 i = 0;
1675 buf[i++] = 0x60; /* pushad */
1676 buf[i++] = 0x68; /* push tpaddr aka $pc */
1677 *((int *)(buf + i)) = (int) tpaddr;
1678 i += 4;
1679 buf[i++] = 0x9c; /* pushf */
1680 buf[i++] = 0x1e; /* push %ds */
1681 buf[i++] = 0x06; /* push %es */
1682 buf[i++] = 0x0f; /* push %fs */
1683 buf[i++] = 0xa0;
1684 buf[i++] = 0x0f; /* push %gs */
1685 buf[i++] = 0xa8;
1686 buf[i++] = 0x16; /* push %ss */
1687 buf[i++] = 0x0e; /* push %cs */
1688 append_insns (&buildaddr, i, buf);
1689
1690 /* Stack space for the collecting_t object. */
1691 i = 0;
1692 i += push_opcode (&buf[i], "83 ec 08"); /* sub $0x8,%esp */
1693
1694 /* Build the object. */
1695 i += push_opcode (&buf[i], "b8"); /* mov <tpoint>,%eax */
1696 memcpy (buf + i, &tpoint, 4);
1697 i += 4;
1698 i += push_opcode (&buf[i], "89 04 24"); /* mov %eax,(%esp) */
1699
1700 i += push_opcode (&buf[i], "65 a1 00 00 00 00"); /* mov %gs:0x0,%eax */
1701 i += push_opcode (&buf[i], "89 44 24 04"); /* mov %eax,0x4(%esp) */
1702 append_insns (&buildaddr, i, buf);
1703
1704 /* spin-lock. Note this is using cmpxchg, which leaves i386 behind.
1705 If we cared for it, this could be using xchg alternatively. */
1706
1707 i = 0;
1708 i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */
1709 i += push_opcode (&buf[i], "f0 0f b1 25"); /* lock cmpxchg
1710 %esp,<lockaddr> */
1711 memcpy (&buf[i], (void *) &lockaddr, 4);
1712 i += 4;
1713 i += push_opcode (&buf[i], "85 c0"); /* test %eax,%eax */
1714 i += push_opcode (&buf[i], "75 f2"); /* jne <again> */
1715 append_insns (&buildaddr, i, buf);
1716
1717
1718 /* Set up arguments to the gdb_collect call. */
1719 i = 0;
1720 i += push_opcode (&buf[i], "89 e0"); /* mov %esp,%eax */
1721 i += push_opcode (&buf[i], "83 c0 08"); /* add $0x08,%eax */
1722 i += push_opcode (&buf[i], "89 44 24 fc"); /* mov %eax,-0x4(%esp) */
1723 append_insns (&buildaddr, i, buf);
1724
1725 i = 0;
1726 i += push_opcode (&buf[i], "83 ec 08"); /* sub $0x8,%esp */
1727 append_insns (&buildaddr, i, buf);
1728
1729 i = 0;
1730 i += push_opcode (&buf[i], "c7 04 24"); /* movl <addr>,(%esp) */
1731 memcpy (&buf[i], (void *) &tpoint, 4);
1732 i += 4;
1733 append_insns (&buildaddr, i, buf);
1734
1735 buf[0] = 0xe8; /* call <reladdr> */
1736 offset = collector - (buildaddr + sizeof (jump_insn));
1737 memcpy (buf + 1, &offset, 4);
1738 append_insns (&buildaddr, 5, buf);
1739 /* Clean up after the call. */
1740 buf[0] = 0x83; /* add $0x8,%esp */
1741 buf[1] = 0xc4;
1742 buf[2] = 0x08;
1743 append_insns (&buildaddr, 3, buf);
1744
1745
1746 /* Clear the spin-lock. This would need the LOCK prefix on older
1747 broken archs. */
1748 i = 0;
1749 i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */
1750 i += push_opcode (&buf[i], "a3"); /* mov %eax, lockaddr */
1751 memcpy (buf + i, &lockaddr, 4);
1752 i += 4;
1753 append_insns (&buildaddr, i, buf);
1754
1755
1756 /* Remove stack that had been used for the collect_t object. */
1757 i = 0;
1758 i += push_opcode (&buf[i], "83 c4 08"); /* add $0x08,%esp */
1759 append_insns (&buildaddr, i, buf);
1760
1761 i = 0;
1762 buf[i++] = 0x83; /* add $0x4,%esp (no pop of %cs, assume unchanged) */
1763 buf[i++] = 0xc4;
1764 buf[i++] = 0x04;
1765 buf[i++] = 0x17; /* pop %ss */
1766 buf[i++] = 0x0f; /* pop %gs */
1767 buf[i++] = 0xa9;
1768 buf[i++] = 0x0f; /* pop %fs */
1769 buf[i++] = 0xa1;
1770 buf[i++] = 0x07; /* pop %es */
1771 buf[i++] = 0x1f; /* pop %ds */
1772 buf[i++] = 0x9d; /* popf */
1773 buf[i++] = 0x83; /* add $0x4,%esp (pop of tpaddr aka $pc) */
1774 buf[i++] = 0xc4;
1775 buf[i++] = 0x04;
1776 buf[i++] = 0x61; /* popad */
1777 append_insns (&buildaddr, i, buf);
1778
1779 /* Now, adjust the original instruction to execute in the jump
1780 pad. */
1781 *adjusted_insn_addr = buildaddr;
1782 relocate_instruction (&buildaddr, tpaddr);
1783 *adjusted_insn_addr_end = buildaddr;
1784
1785 /* Write the jump back to the program. */
1786 offset = (tpaddr + orig_size) - (buildaddr + sizeof (jump_insn));
1787 memcpy (buf, jump_insn, sizeof (jump_insn));
1788 memcpy (buf + 1, &offset, 4);
1789 append_insns (&buildaddr, sizeof (jump_insn), buf);
1790
1791 /* The jump pad is now built. Wire in a jump to our jump pad. This
1792 is always done last (by our caller actually), so that we can
1793 install fast tracepoints with threads running. This relies on
1794 the agent's atomic write support. */
1795 if (orig_size == 4)
1796 {
1797 /* Create a trampoline. */
1798 *trampoline_size = sizeof (jump_insn);
1799 if (!claim_trampoline_space (*trampoline_size, trampoline))
1800 {
1801 /* No trampoline space available. */
1802 strcpy (err,
1803 "E.Cannot allocate trampoline space needed for fast "
1804 "tracepoints on 4-byte instructions.");
1805 return 1;
1806 }
1807
1808 offset = *jump_entry - (*trampoline + sizeof (jump_insn));
1809 memcpy (buf, jump_insn, sizeof (jump_insn));
1810 memcpy (buf + 1, &offset, 4);
1811 write_inferior_memory (*trampoline, buf, sizeof (jump_insn));
1812
1813 /* Use a 16-bit relative jump instruction to jump to the trampoline. */
1814 offset = (*trampoline - (tpaddr + sizeof (small_jump_insn))) & 0xffff;
1815 memcpy (buf, small_jump_insn, sizeof (small_jump_insn));
1816 memcpy (buf + 2, &offset, 2);
1817 memcpy (jjump_pad_insn, buf, sizeof (small_jump_insn));
1818 *jjump_pad_insn_size = sizeof (small_jump_insn);
1819 }
1820 else
1821 {
1822 /* Else use a 32-bit relative jump instruction. */
1823 offset = *jump_entry - (tpaddr + sizeof (jump_insn));
1824 memcpy (buf, jump_insn, sizeof (jump_insn));
1825 memcpy (buf + 1, &offset, 4);
1826 memcpy (jjump_pad_insn, buf, sizeof (jump_insn));
1827 *jjump_pad_insn_size = sizeof (jump_insn);
1828 }
1829
1830 /* Return the end address of our pad. */
1831 *jump_entry = buildaddr;
1832
1833 return 0;
1834 }
1835
1836 static int
1837 x86_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, CORE_ADDR tpaddr,
1838 CORE_ADDR collector,
1839 CORE_ADDR lockaddr,
1840 ULONGEST orig_size,
1841 CORE_ADDR *jump_entry,
1842 CORE_ADDR *trampoline,
1843 ULONGEST *trampoline_size,
1844 unsigned char *jjump_pad_insn,
1845 ULONGEST *jjump_pad_insn_size,
1846 CORE_ADDR *adjusted_insn_addr,
1847 CORE_ADDR *adjusted_insn_addr_end,
1848 char *err)
1849 {
1850 #ifdef __x86_64__
1851 if (is_64bit_tdesc ())
1852 return amd64_install_fast_tracepoint_jump_pad (tpoint, tpaddr,
1853 collector, lockaddr,
1854 orig_size, jump_entry,
1855 trampoline, trampoline_size,
1856 jjump_pad_insn,
1857 jjump_pad_insn_size,
1858 adjusted_insn_addr,
1859 adjusted_insn_addr_end,
1860 err);
1861 #endif
1862
1863 return i386_install_fast_tracepoint_jump_pad (tpoint, tpaddr,
1864 collector, lockaddr,
1865 orig_size, jump_entry,
1866 trampoline, trampoline_size,
1867 jjump_pad_insn,
1868 jjump_pad_insn_size,
1869 adjusted_insn_addr,
1870 adjusted_insn_addr_end,
1871 err);
1872 }
1873
1874 /* Return the minimum instruction length for fast tracepoints on x86/x86-64
1875 architectures. */
1876
1877 static int
1878 x86_get_min_fast_tracepoint_insn_len (void)
1879 {
1880 static int warned_about_fast_tracepoints = 0;
1881
1882 #ifdef __x86_64__
1883 /* On x86-64, 5-byte jump instructions with a 4-byte offset are always
1884 used for fast tracepoints. */
1885 if (is_64bit_tdesc ())
1886 return 5;
1887 #endif
1888
1889 if (agent_loaded_p ())
1890 {
1891 char errbuf[IPA_BUFSIZ];
1892
1893 errbuf[0] = '\0';
1894
1895 /* On x86, if trampolines are available, then 4-byte jump instructions
1896 with a 2-byte offset may be used, otherwise 5-byte jump instructions
1897 with a 4-byte offset are used instead. */
1898 if (have_fast_tracepoint_trampoline_buffer (errbuf))
1899 return 4;
1900 else
1901 {
1902 /* GDB has no channel to explain to user why a shorter fast
1903 tracepoint is not possible, but at least make GDBserver
1904 mention that something has gone awry. */
1905 if (!warned_about_fast_tracepoints)
1906 {
1907 warning ("4-byte fast tracepoints not available; %s\n", errbuf);
1908 warned_about_fast_tracepoints = 1;
1909 }
1910 return 5;
1911 }
1912 }
1913 else
1914 {
1915 /* Indicate that the minimum length is currently unknown since the IPA
1916 has not loaded yet. */
1917 return 0;
1918 }
1919 }
1920
1921 static void
1922 add_insns (unsigned char *start, int len)
1923 {
1924 CORE_ADDR buildaddr = current_insn_ptr;
1925
1926 if (debug_threads)
1927 debug_printf ("Adding %d bytes of insn at %s\n",
1928 len, paddress (buildaddr));
1929
1930 append_insns (&buildaddr, len, start);
1931 current_insn_ptr = buildaddr;
1932 }
1933
1934 /* Our general strategy for emitting code is to avoid specifying raw
1935 bytes whenever possible, and instead copy a block of inline asm
1936 that is embedded in the function. This is a little messy, because
1937 we need to keep the compiler from discarding what looks like dead
1938 code, plus suppress various warnings. */
1939
1940 #define EMIT_ASM(NAME, INSNS) \
1941 do \
1942 { \
1943 extern unsigned char start_ ## NAME, end_ ## NAME; \
1944 add_insns (&start_ ## NAME, &end_ ## NAME - &start_ ## NAME); \
1945 __asm__ ("jmp end_" #NAME "\n" \
1946 "\t" "start_" #NAME ":" \
1947 "\t" INSNS "\n" \
1948 "\t" "end_" #NAME ":"); \
1949 } while (0)
1950
1951 #ifdef __x86_64__
1952
1953 #define EMIT_ASM32(NAME,INSNS) \
1954 do \
1955 { \
1956 extern unsigned char start_ ## NAME, end_ ## NAME; \
1957 add_insns (&start_ ## NAME, &end_ ## NAME - &start_ ## NAME); \
1958 __asm__ (".code32\n" \
1959 "\t" "jmp end_" #NAME "\n" \
1960 "\t" "start_" #NAME ":\n" \
1961 "\t" INSNS "\n" \
1962 "\t" "end_" #NAME ":\n" \
1963 ".code64\n"); \
1964 } while (0)
1965
1966 #else
1967
1968 #define EMIT_ASM32(NAME,INSNS) EMIT_ASM(NAME,INSNS)
1969
1970 #endif
1971
1972 #ifdef __x86_64__
1973
1974 static void
1975 amd64_emit_prologue (void)
1976 {
1977 EMIT_ASM (amd64_prologue,
1978 "pushq %rbp\n\t"
1979 "movq %rsp,%rbp\n\t"
1980 "sub $0x20,%rsp\n\t"
1981 "movq %rdi,-8(%rbp)\n\t"
1982 "movq %rsi,-16(%rbp)");
1983 }
1984
1985
1986 static void
1987 amd64_emit_epilogue (void)
1988 {
1989 EMIT_ASM (amd64_epilogue,
1990 "movq -16(%rbp),%rdi\n\t"
1991 "movq %rax,(%rdi)\n\t"
1992 "xor %rax,%rax\n\t"
1993 "leave\n\t"
1994 "ret");
1995 }
1996
1997 static void
1998 amd64_emit_add (void)
1999 {
2000 EMIT_ASM (amd64_add,
2001 "add (%rsp),%rax\n\t"
2002 "lea 0x8(%rsp),%rsp");
2003 }
2004
2005 static void
2006 amd64_emit_sub (void)
2007 {
2008 EMIT_ASM (amd64_sub,
2009 "sub %rax,(%rsp)\n\t"
2010 "pop %rax");
2011 }
2012
2013 static void
2014 amd64_emit_mul (void)
2015 {
2016 emit_error = 1;
2017 }
2018
2019 static void
2020 amd64_emit_lsh (void)
2021 {
2022 emit_error = 1;
2023 }
2024
2025 static void
2026 amd64_emit_rsh_signed (void)
2027 {
2028 emit_error = 1;
2029 }
2030
2031 static void
2032 amd64_emit_rsh_unsigned (void)
2033 {
2034 emit_error = 1;
2035 }
2036
2037 static void
2038 amd64_emit_ext (int arg)
2039 {
2040 switch (arg)
2041 {
2042 case 8:
2043 EMIT_ASM (amd64_ext_8,
2044 "cbtw\n\t"
2045 "cwtl\n\t"
2046 "cltq");
2047 break;
2048 case 16:
2049 EMIT_ASM (amd64_ext_16,
2050 "cwtl\n\t"
2051 "cltq");
2052 break;
2053 case 32:
2054 EMIT_ASM (amd64_ext_32,
2055 "cltq");
2056 break;
2057 default:
2058 emit_error = 1;
2059 }
2060 }
2061
2062 static void
2063 amd64_emit_log_not (void)
2064 {
2065 EMIT_ASM (amd64_log_not,
2066 "test %rax,%rax\n\t"
2067 "sete %cl\n\t"
2068 "movzbq %cl,%rax");
2069 }
2070
2071 static void
2072 amd64_emit_bit_and (void)
2073 {
2074 EMIT_ASM (amd64_and,
2075 "and (%rsp),%rax\n\t"
2076 "lea 0x8(%rsp),%rsp");
2077 }
2078
2079 static void
2080 amd64_emit_bit_or (void)
2081 {
2082 EMIT_ASM (amd64_or,
2083 "or (%rsp),%rax\n\t"
2084 "lea 0x8(%rsp),%rsp");
2085 }
2086
2087 static void
2088 amd64_emit_bit_xor (void)
2089 {
2090 EMIT_ASM (amd64_xor,
2091 "xor (%rsp),%rax\n\t"
2092 "lea 0x8(%rsp),%rsp");
2093 }
2094
2095 static void
2096 amd64_emit_bit_not (void)
2097 {
2098 EMIT_ASM (amd64_bit_not,
2099 "xorq $0xffffffffffffffff,%rax");
2100 }
2101
2102 static void
2103 amd64_emit_equal (void)
2104 {
2105 EMIT_ASM (amd64_equal,
2106 "cmp %rax,(%rsp)\n\t"
2107 "je .Lamd64_equal_true\n\t"
2108 "xor %rax,%rax\n\t"
2109 "jmp .Lamd64_equal_end\n\t"
2110 ".Lamd64_equal_true:\n\t"
2111 "mov $0x1,%rax\n\t"
2112 ".Lamd64_equal_end:\n\t"
2113 "lea 0x8(%rsp),%rsp");
2114 }
2115
2116 static void
2117 amd64_emit_less_signed (void)
2118 {
2119 EMIT_ASM (amd64_less_signed,
2120 "cmp %rax,(%rsp)\n\t"
2121 "jl .Lamd64_less_signed_true\n\t"
2122 "xor %rax,%rax\n\t"
2123 "jmp .Lamd64_less_signed_end\n\t"
2124 ".Lamd64_less_signed_true:\n\t"
2125 "mov $1,%rax\n\t"
2126 ".Lamd64_less_signed_end:\n\t"
2127 "lea 0x8(%rsp),%rsp");
2128 }
2129
2130 static void
2131 amd64_emit_less_unsigned (void)
2132 {
2133 EMIT_ASM (amd64_less_unsigned,
2134 "cmp %rax,(%rsp)\n\t"
2135 "jb .Lamd64_less_unsigned_true\n\t"
2136 "xor %rax,%rax\n\t"
2137 "jmp .Lamd64_less_unsigned_end\n\t"
2138 ".Lamd64_less_unsigned_true:\n\t"
2139 "mov $1,%rax\n\t"
2140 ".Lamd64_less_unsigned_end:\n\t"
2141 "lea 0x8(%rsp),%rsp");
2142 }
2143
2144 static void
2145 amd64_emit_ref (int size)
2146 {
2147 switch (size)
2148 {
2149 case 1:
2150 EMIT_ASM (amd64_ref1,
2151 "movb (%rax),%al");
2152 break;
2153 case 2:
2154 EMIT_ASM (amd64_ref2,
2155 "movw (%rax),%ax");
2156 break;
2157 case 4:
2158 EMIT_ASM (amd64_ref4,
2159 "movl (%rax),%eax");
2160 break;
2161 case 8:
2162 EMIT_ASM (amd64_ref8,
2163 "movq (%rax),%rax");
2164 break;
2165 }
2166 }
2167
2168 static void
2169 amd64_emit_if_goto (int *offset_p, int *size_p)
2170 {
2171 EMIT_ASM (amd64_if_goto,
2172 "mov %rax,%rcx\n\t"
2173 "pop %rax\n\t"
2174 "cmp $0,%rcx\n\t"
2175 ".byte 0x0f, 0x85, 0x0, 0x0, 0x0, 0x0");
2176 if (offset_p)
2177 *offset_p = 10;
2178 if (size_p)
2179 *size_p = 4;
2180 }
2181
2182 static void
2183 amd64_emit_goto (int *offset_p, int *size_p)
2184 {
2185 EMIT_ASM (amd64_goto,
2186 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0");
2187 if (offset_p)
2188 *offset_p = 1;
2189 if (size_p)
2190 *size_p = 4;
2191 }
2192
2193 static void
2194 amd64_write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
2195 {
2196 int diff = (to - (from + size));
2197 unsigned char buf[sizeof (int)];
2198
2199 if (size != 4)
2200 {
2201 emit_error = 1;
2202 return;
2203 }
2204
2205 memcpy (buf, &diff, sizeof (int));
2206 write_inferior_memory (from, buf, sizeof (int));
2207 }
2208
2209 static void
2210 amd64_emit_const (LONGEST num)
2211 {
2212 unsigned char buf[16];
2213 int i;
2214 CORE_ADDR buildaddr = current_insn_ptr;
2215
2216 i = 0;
2217 buf[i++] = 0x48; buf[i++] = 0xb8; /* mov $<n>,%rax */
2218 memcpy (&buf[i], &num, sizeof (num));
2219 i += 8;
2220 append_insns (&buildaddr, i, buf);
2221 current_insn_ptr = buildaddr;
2222 }
2223
2224 static void
2225 amd64_emit_call (CORE_ADDR fn)
2226 {
2227 unsigned char buf[16];
2228 int i;
2229 CORE_ADDR buildaddr;
2230 LONGEST offset64;
2231
2232 /* The destination function being in the shared library, may be
2233 >31-bits away off the compiled code pad. */
2234
2235 buildaddr = current_insn_ptr;
2236
2237 offset64 = fn - (buildaddr + 1 /* call op */ + 4 /* 32-bit offset */);
2238
2239 i = 0;
2240
2241 if (offset64 > INT_MAX || offset64 < INT_MIN)
2242 {
2243 /* Offset is too large for a call. Use callq, but that requires
2244 a register, so avoid it if possible. Use r10, since it is
2245 call-clobbered, we don't have to push/pop it. */
2246 buf[i++] = 0x48; /* mov $fn,%r10 */
2247 buf[i++] = 0xba;
2248 memcpy (buf + i, &fn, 8);
2249 i += 8;
2250 buf[i++] = 0xff; /* callq *%r10 */
2251 buf[i++] = 0xd2;
2252 }
2253 else
2254 {
2255 int offset32 = offset64; /* we know we can't overflow here. */
2256 memcpy (buf + i, &offset32, 4);
2257 i += 4;
2258 }
2259
2260 append_insns (&buildaddr, i, buf);
2261 current_insn_ptr = buildaddr;
2262 }
2263
2264 static void
2265 amd64_emit_reg (int reg)
2266 {
2267 unsigned char buf[16];
2268 int i;
2269 CORE_ADDR buildaddr;
2270
2271 /* Assume raw_regs is still in %rdi. */
2272 buildaddr = current_insn_ptr;
2273 i = 0;
2274 buf[i++] = 0xbe; /* mov $<n>,%esi */
2275 memcpy (&buf[i], &reg, sizeof (reg));
2276 i += 4;
2277 append_insns (&buildaddr, i, buf);
2278 current_insn_ptr = buildaddr;
2279 amd64_emit_call (get_raw_reg_func_addr ());
2280 }
2281
2282 static void
2283 amd64_emit_pop (void)
2284 {
2285 EMIT_ASM (amd64_pop,
2286 "pop %rax");
2287 }
2288
2289 static void
2290 amd64_emit_stack_flush (void)
2291 {
2292 EMIT_ASM (amd64_stack_flush,
2293 "push %rax");
2294 }
2295
2296 static void
2297 amd64_emit_zero_ext (int arg)
2298 {
2299 switch (arg)
2300 {
2301 case 8:
2302 EMIT_ASM (amd64_zero_ext_8,
2303 "and $0xff,%rax");
2304 break;
2305 case 16:
2306 EMIT_ASM (amd64_zero_ext_16,
2307 "and $0xffff,%rax");
2308 break;
2309 case 32:
2310 EMIT_ASM (amd64_zero_ext_32,
2311 "mov $0xffffffff,%rcx\n\t"
2312 "and %rcx,%rax");
2313 break;
2314 default:
2315 emit_error = 1;
2316 }
2317 }
2318
2319 static void
2320 amd64_emit_swap (void)
2321 {
2322 EMIT_ASM (amd64_swap,
2323 "mov %rax,%rcx\n\t"
2324 "pop %rax\n\t"
2325 "push %rcx");
2326 }
2327
2328 static void
2329 amd64_emit_stack_adjust (int n)
2330 {
2331 unsigned char buf[16];
2332 int i;
2333 CORE_ADDR buildaddr = current_insn_ptr;
2334
2335 i = 0;
2336 buf[i++] = 0x48; /* lea $<n>(%rsp),%rsp */
2337 buf[i++] = 0x8d;
2338 buf[i++] = 0x64;
2339 buf[i++] = 0x24;
2340 /* This only handles adjustments up to 16, but we don't expect any more. */
2341 buf[i++] = n * 8;
2342 append_insns (&buildaddr, i, buf);
2343 current_insn_ptr = buildaddr;
2344 }
2345
2346 /* FN's prototype is `LONGEST(*fn)(int)'. */
2347
2348 static void
2349 amd64_emit_int_call_1 (CORE_ADDR fn, int arg1)
2350 {
2351 unsigned char buf[16];
2352 int i;
2353 CORE_ADDR buildaddr;
2354
2355 buildaddr = current_insn_ptr;
2356 i = 0;
2357 buf[i++] = 0xbf; /* movl $<n>,%edi */
2358 memcpy (&buf[i], &arg1, sizeof (arg1));
2359 i += 4;
2360 append_insns (&buildaddr, i, buf);
2361 current_insn_ptr = buildaddr;
2362 amd64_emit_call (fn);
2363 }
2364
2365 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
2366
2367 static void
2368 amd64_emit_void_call_2 (CORE_ADDR fn, int arg1)
2369 {
2370 unsigned char buf[16];
2371 int i;
2372 CORE_ADDR buildaddr;
2373
2374 buildaddr = current_insn_ptr;
2375 i = 0;
2376 buf[i++] = 0xbf; /* movl $<n>,%edi */
2377 memcpy (&buf[i], &arg1, sizeof (arg1));
2378 i += 4;
2379 append_insns (&buildaddr, i, buf);
2380 current_insn_ptr = buildaddr;
2381 EMIT_ASM (amd64_void_call_2_a,
2382 /* Save away a copy of the stack top. */
2383 "push %rax\n\t"
2384 /* Also pass top as the second argument. */
2385 "mov %rax,%rsi");
2386 amd64_emit_call (fn);
2387 EMIT_ASM (amd64_void_call_2_b,
2388 /* Restore the stack top, %rax may have been trashed. */
2389 "pop %rax");
2390 }
2391
2392 void
2393 amd64_emit_eq_goto (int *offset_p, int *size_p)
2394 {
2395 EMIT_ASM (amd64_eq,
2396 "cmp %rax,(%rsp)\n\t"
2397 "jne .Lamd64_eq_fallthru\n\t"
2398 "lea 0x8(%rsp),%rsp\n\t"
2399 "pop %rax\n\t"
2400 /* jmp, but don't trust the assembler to choose the right jump */
2401 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2402 ".Lamd64_eq_fallthru:\n\t"
2403 "lea 0x8(%rsp),%rsp\n\t"
2404 "pop %rax");
2405
2406 if (offset_p)
2407 *offset_p = 13;
2408 if (size_p)
2409 *size_p = 4;
2410 }
2411
2412 void
2413 amd64_emit_ne_goto (int *offset_p, int *size_p)
2414 {
2415 EMIT_ASM (amd64_ne,
2416 "cmp %rax,(%rsp)\n\t"
2417 "je .Lamd64_ne_fallthru\n\t"
2418 "lea 0x8(%rsp),%rsp\n\t"
2419 "pop %rax\n\t"
2420 /* jmp, but don't trust the assembler to choose the right jump */
2421 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2422 ".Lamd64_ne_fallthru:\n\t"
2423 "lea 0x8(%rsp),%rsp\n\t"
2424 "pop %rax");
2425
2426 if (offset_p)
2427 *offset_p = 13;
2428 if (size_p)
2429 *size_p = 4;
2430 }
2431
2432 void
2433 amd64_emit_lt_goto (int *offset_p, int *size_p)
2434 {
2435 EMIT_ASM (amd64_lt,
2436 "cmp %rax,(%rsp)\n\t"
2437 "jnl .Lamd64_lt_fallthru\n\t"
2438 "lea 0x8(%rsp),%rsp\n\t"
2439 "pop %rax\n\t"
2440 /* jmp, but don't trust the assembler to choose the right jump */
2441 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2442 ".Lamd64_lt_fallthru:\n\t"
2443 "lea 0x8(%rsp),%rsp\n\t"
2444 "pop %rax");
2445
2446 if (offset_p)
2447 *offset_p = 13;
2448 if (size_p)
2449 *size_p = 4;
2450 }
2451
2452 void
2453 amd64_emit_le_goto (int *offset_p, int *size_p)
2454 {
2455 EMIT_ASM (amd64_le,
2456 "cmp %rax,(%rsp)\n\t"
2457 "jnle .Lamd64_le_fallthru\n\t"
2458 "lea 0x8(%rsp),%rsp\n\t"
2459 "pop %rax\n\t"
2460 /* jmp, but don't trust the assembler to choose the right jump */
2461 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2462 ".Lamd64_le_fallthru:\n\t"
2463 "lea 0x8(%rsp),%rsp\n\t"
2464 "pop %rax");
2465
2466 if (offset_p)
2467 *offset_p = 13;
2468 if (size_p)
2469 *size_p = 4;
2470 }
2471
2472 void
2473 amd64_emit_gt_goto (int *offset_p, int *size_p)
2474 {
2475 EMIT_ASM (amd64_gt,
2476 "cmp %rax,(%rsp)\n\t"
2477 "jng .Lamd64_gt_fallthru\n\t"
2478 "lea 0x8(%rsp),%rsp\n\t"
2479 "pop %rax\n\t"
2480 /* jmp, but don't trust the assembler to choose the right jump */
2481 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2482 ".Lamd64_gt_fallthru:\n\t"
2483 "lea 0x8(%rsp),%rsp\n\t"
2484 "pop %rax");
2485
2486 if (offset_p)
2487 *offset_p = 13;
2488 if (size_p)
2489 *size_p = 4;
2490 }
2491
2492 void
2493 amd64_emit_ge_goto (int *offset_p, int *size_p)
2494 {
2495 EMIT_ASM (amd64_ge,
2496 "cmp %rax,(%rsp)\n\t"
2497 "jnge .Lamd64_ge_fallthru\n\t"
2498 ".Lamd64_ge_jump:\n\t"
2499 "lea 0x8(%rsp),%rsp\n\t"
2500 "pop %rax\n\t"
2501 /* jmp, but don't trust the assembler to choose the right jump */
2502 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2503 ".Lamd64_ge_fallthru:\n\t"
2504 "lea 0x8(%rsp),%rsp\n\t"
2505 "pop %rax");
2506
2507 if (offset_p)
2508 *offset_p = 13;
2509 if (size_p)
2510 *size_p = 4;
2511 }
2512
2513 struct emit_ops amd64_emit_ops =
2514 {
2515 amd64_emit_prologue,
2516 amd64_emit_epilogue,
2517 amd64_emit_add,
2518 amd64_emit_sub,
2519 amd64_emit_mul,
2520 amd64_emit_lsh,
2521 amd64_emit_rsh_signed,
2522 amd64_emit_rsh_unsigned,
2523 amd64_emit_ext,
2524 amd64_emit_log_not,
2525 amd64_emit_bit_and,
2526 amd64_emit_bit_or,
2527 amd64_emit_bit_xor,
2528 amd64_emit_bit_not,
2529 amd64_emit_equal,
2530 amd64_emit_less_signed,
2531 amd64_emit_less_unsigned,
2532 amd64_emit_ref,
2533 amd64_emit_if_goto,
2534 amd64_emit_goto,
2535 amd64_write_goto_address,
2536 amd64_emit_const,
2537 amd64_emit_call,
2538 amd64_emit_reg,
2539 amd64_emit_pop,
2540 amd64_emit_stack_flush,
2541 amd64_emit_zero_ext,
2542 amd64_emit_swap,
2543 amd64_emit_stack_adjust,
2544 amd64_emit_int_call_1,
2545 amd64_emit_void_call_2,
2546 amd64_emit_eq_goto,
2547 amd64_emit_ne_goto,
2548 amd64_emit_lt_goto,
2549 amd64_emit_le_goto,
2550 amd64_emit_gt_goto,
2551 amd64_emit_ge_goto
2552 };
2553
2554 #endif /* __x86_64__ */
2555
2556 static void
2557 i386_emit_prologue (void)
2558 {
2559 EMIT_ASM32 (i386_prologue,
2560 "push %ebp\n\t"
2561 "mov %esp,%ebp\n\t"
2562 "push %ebx");
2563 /* At this point, the raw regs base address is at 8(%ebp), and the
2564 value pointer is at 12(%ebp). */
2565 }
2566
2567 static void
2568 i386_emit_epilogue (void)
2569 {
2570 EMIT_ASM32 (i386_epilogue,
2571 "mov 12(%ebp),%ecx\n\t"
2572 "mov %eax,(%ecx)\n\t"
2573 "mov %ebx,0x4(%ecx)\n\t"
2574 "xor %eax,%eax\n\t"
2575 "pop %ebx\n\t"
2576 "pop %ebp\n\t"
2577 "ret");
2578 }
2579
2580 static void
2581 i386_emit_add (void)
2582 {
2583 EMIT_ASM32 (i386_add,
2584 "add (%esp),%eax\n\t"
2585 "adc 0x4(%esp),%ebx\n\t"
2586 "lea 0x8(%esp),%esp");
2587 }
2588
2589 static void
2590 i386_emit_sub (void)
2591 {
2592 EMIT_ASM32 (i386_sub,
2593 "subl %eax,(%esp)\n\t"
2594 "sbbl %ebx,4(%esp)\n\t"
2595 "pop %eax\n\t"
2596 "pop %ebx\n\t");
2597 }
2598
2599 static void
2600 i386_emit_mul (void)
2601 {
2602 emit_error = 1;
2603 }
2604
2605 static void
2606 i386_emit_lsh (void)
2607 {
2608 emit_error = 1;
2609 }
2610
2611 static void
2612 i386_emit_rsh_signed (void)
2613 {
2614 emit_error = 1;
2615 }
2616
2617 static void
2618 i386_emit_rsh_unsigned (void)
2619 {
2620 emit_error = 1;
2621 }
2622
2623 static void
2624 i386_emit_ext (int arg)
2625 {
2626 switch (arg)
2627 {
2628 case 8:
2629 EMIT_ASM32 (i386_ext_8,
2630 "cbtw\n\t"
2631 "cwtl\n\t"
2632 "movl %eax,%ebx\n\t"
2633 "sarl $31,%ebx");
2634 break;
2635 case 16:
2636 EMIT_ASM32 (i386_ext_16,
2637 "cwtl\n\t"
2638 "movl %eax,%ebx\n\t"
2639 "sarl $31,%ebx");
2640 break;
2641 case 32:
2642 EMIT_ASM32 (i386_ext_32,
2643 "movl %eax,%ebx\n\t"
2644 "sarl $31,%ebx");
2645 break;
2646 default:
2647 emit_error = 1;
2648 }
2649 }
2650
2651 static void
2652 i386_emit_log_not (void)
2653 {
2654 EMIT_ASM32 (i386_log_not,
2655 "or %ebx,%eax\n\t"
2656 "test %eax,%eax\n\t"
2657 "sete %cl\n\t"
2658 "xor %ebx,%ebx\n\t"
2659 "movzbl %cl,%eax");
2660 }
2661
2662 static void
2663 i386_emit_bit_and (void)
2664 {
2665 EMIT_ASM32 (i386_and,
2666 "and (%esp),%eax\n\t"
2667 "and 0x4(%esp),%ebx\n\t"
2668 "lea 0x8(%esp),%esp");
2669 }
2670
2671 static void
2672 i386_emit_bit_or (void)
2673 {
2674 EMIT_ASM32 (i386_or,
2675 "or (%esp),%eax\n\t"
2676 "or 0x4(%esp),%ebx\n\t"
2677 "lea 0x8(%esp),%esp");
2678 }
2679
2680 static void
2681 i386_emit_bit_xor (void)
2682 {
2683 EMIT_ASM32 (i386_xor,
2684 "xor (%esp),%eax\n\t"
2685 "xor 0x4(%esp),%ebx\n\t"
2686 "lea 0x8(%esp),%esp");
2687 }
2688
2689 static void
2690 i386_emit_bit_not (void)
2691 {
2692 EMIT_ASM32 (i386_bit_not,
2693 "xor $0xffffffff,%eax\n\t"
2694 "xor $0xffffffff,%ebx\n\t");
2695 }
2696
2697 static void
2698 i386_emit_equal (void)
2699 {
2700 EMIT_ASM32 (i386_equal,
2701 "cmpl %ebx,4(%esp)\n\t"
2702 "jne .Li386_equal_false\n\t"
2703 "cmpl %eax,(%esp)\n\t"
2704 "je .Li386_equal_true\n\t"
2705 ".Li386_equal_false:\n\t"
2706 "xor %eax,%eax\n\t"
2707 "jmp .Li386_equal_end\n\t"
2708 ".Li386_equal_true:\n\t"
2709 "mov $1,%eax\n\t"
2710 ".Li386_equal_end:\n\t"
2711 "xor %ebx,%ebx\n\t"
2712 "lea 0x8(%esp),%esp");
2713 }
2714
2715 static void
2716 i386_emit_less_signed (void)
2717 {
2718 EMIT_ASM32 (i386_less_signed,
2719 "cmpl %ebx,4(%esp)\n\t"
2720 "jl .Li386_less_signed_true\n\t"
2721 "jne .Li386_less_signed_false\n\t"
2722 "cmpl %eax,(%esp)\n\t"
2723 "jl .Li386_less_signed_true\n\t"
2724 ".Li386_less_signed_false:\n\t"
2725 "xor %eax,%eax\n\t"
2726 "jmp .Li386_less_signed_end\n\t"
2727 ".Li386_less_signed_true:\n\t"
2728 "mov $1,%eax\n\t"
2729 ".Li386_less_signed_end:\n\t"
2730 "xor %ebx,%ebx\n\t"
2731 "lea 0x8(%esp),%esp");
2732 }
2733
2734 static void
2735 i386_emit_less_unsigned (void)
2736 {
2737 EMIT_ASM32 (i386_less_unsigned,
2738 "cmpl %ebx,4(%esp)\n\t"
2739 "jb .Li386_less_unsigned_true\n\t"
2740 "jne .Li386_less_unsigned_false\n\t"
2741 "cmpl %eax,(%esp)\n\t"
2742 "jb .Li386_less_unsigned_true\n\t"
2743 ".Li386_less_unsigned_false:\n\t"
2744 "xor %eax,%eax\n\t"
2745 "jmp .Li386_less_unsigned_end\n\t"
2746 ".Li386_less_unsigned_true:\n\t"
2747 "mov $1,%eax\n\t"
2748 ".Li386_less_unsigned_end:\n\t"
2749 "xor %ebx,%ebx\n\t"
2750 "lea 0x8(%esp),%esp");
2751 }
2752
2753 static void
2754 i386_emit_ref (int size)
2755 {
2756 switch (size)
2757 {
2758 case 1:
2759 EMIT_ASM32 (i386_ref1,
2760 "movb (%eax),%al");
2761 break;
2762 case 2:
2763 EMIT_ASM32 (i386_ref2,
2764 "movw (%eax),%ax");
2765 break;
2766 case 4:
2767 EMIT_ASM32 (i386_ref4,
2768 "movl (%eax),%eax");
2769 break;
2770 case 8:
2771 EMIT_ASM32 (i386_ref8,
2772 "movl 4(%eax),%ebx\n\t"
2773 "movl (%eax),%eax");
2774 break;
2775 }
2776 }
2777
2778 static void
2779 i386_emit_if_goto (int *offset_p, int *size_p)
2780 {
2781 EMIT_ASM32 (i386_if_goto,
2782 "mov %eax,%ecx\n\t"
2783 "or %ebx,%ecx\n\t"
2784 "pop %eax\n\t"
2785 "pop %ebx\n\t"
2786 "cmpl $0,%ecx\n\t"
2787 /* Don't trust the assembler to choose the right jump */
2788 ".byte 0x0f, 0x85, 0x0, 0x0, 0x0, 0x0");
2789
2790 if (offset_p)
2791 *offset_p = 11; /* be sure that this matches the sequence above */
2792 if (size_p)
2793 *size_p = 4;
2794 }
2795
2796 static void
2797 i386_emit_goto (int *offset_p, int *size_p)
2798 {
2799 EMIT_ASM32 (i386_goto,
2800 /* Don't trust the assembler to choose the right jump */
2801 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0");
2802 if (offset_p)
2803 *offset_p = 1;
2804 if (size_p)
2805 *size_p = 4;
2806 }
2807
2808 static void
2809 i386_write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
2810 {
2811 int diff = (to - (from + size));
2812 unsigned char buf[sizeof (int)];
2813
2814 /* We're only doing 4-byte sizes at the moment. */
2815 if (size != 4)
2816 {
2817 emit_error = 1;
2818 return;
2819 }
2820
2821 memcpy (buf, &diff, sizeof (int));
2822 write_inferior_memory (from, buf, sizeof (int));
2823 }
2824
2825 static void
2826 i386_emit_const (LONGEST num)
2827 {
2828 unsigned char buf[16];
2829 int i, hi, lo;
2830 CORE_ADDR buildaddr = current_insn_ptr;
2831
2832 i = 0;
2833 buf[i++] = 0xb8; /* mov $<n>,%eax */
2834 lo = num & 0xffffffff;
2835 memcpy (&buf[i], &lo, sizeof (lo));
2836 i += 4;
2837 hi = ((num >> 32) & 0xffffffff);
2838 if (hi)
2839 {
2840 buf[i++] = 0xbb; /* mov $<n>,%ebx */
2841 memcpy (&buf[i], &hi, sizeof (hi));
2842 i += 4;
2843 }
2844 else
2845 {
2846 buf[i++] = 0x31; buf[i++] = 0xdb; /* xor %ebx,%ebx */
2847 }
2848 append_insns (&buildaddr, i, buf);
2849 current_insn_ptr = buildaddr;
2850 }
2851
2852 static void
2853 i386_emit_call (CORE_ADDR fn)
2854 {
2855 unsigned char buf[16];
2856 int i, offset;
2857 CORE_ADDR buildaddr;
2858
2859 buildaddr = current_insn_ptr;
2860 i = 0;
2861 buf[i++] = 0xe8; /* call <reladdr> */
2862 offset = ((int) fn) - (buildaddr + 5);
2863 memcpy (buf + 1, &offset, 4);
2864 append_insns (&buildaddr, 5, buf);
2865 current_insn_ptr = buildaddr;
2866 }
2867
2868 static void
2869 i386_emit_reg (int reg)
2870 {
2871 unsigned char buf[16];
2872 int i;
2873 CORE_ADDR buildaddr;
2874
2875 EMIT_ASM32 (i386_reg_a,
2876 "sub $0x8,%esp");
2877 buildaddr = current_insn_ptr;
2878 i = 0;
2879 buf[i++] = 0xb8; /* mov $<n>,%eax */
2880 memcpy (&buf[i], &reg, sizeof (reg));
2881 i += 4;
2882 append_insns (&buildaddr, i, buf);
2883 current_insn_ptr = buildaddr;
2884 EMIT_ASM32 (i386_reg_b,
2885 "mov %eax,4(%esp)\n\t"
2886 "mov 8(%ebp),%eax\n\t"
2887 "mov %eax,(%esp)");
2888 i386_emit_call (get_raw_reg_func_addr ());
2889 EMIT_ASM32 (i386_reg_c,
2890 "xor %ebx,%ebx\n\t"
2891 "lea 0x8(%esp),%esp");
2892 }
2893
2894 static void
2895 i386_emit_pop (void)
2896 {
2897 EMIT_ASM32 (i386_pop,
2898 "pop %eax\n\t"
2899 "pop %ebx");
2900 }
2901
2902 static void
2903 i386_emit_stack_flush (void)
2904 {
2905 EMIT_ASM32 (i386_stack_flush,
2906 "push %ebx\n\t"
2907 "push %eax");
2908 }
2909
2910 static void
2911 i386_emit_zero_ext (int arg)
2912 {
2913 switch (arg)
2914 {
2915 case 8:
2916 EMIT_ASM32 (i386_zero_ext_8,
2917 "and $0xff,%eax\n\t"
2918 "xor %ebx,%ebx");
2919 break;
2920 case 16:
2921 EMIT_ASM32 (i386_zero_ext_16,
2922 "and $0xffff,%eax\n\t"
2923 "xor %ebx,%ebx");
2924 break;
2925 case 32:
2926 EMIT_ASM32 (i386_zero_ext_32,
2927 "xor %ebx,%ebx");
2928 break;
2929 default:
2930 emit_error = 1;
2931 }
2932 }
2933
2934 static void
2935 i386_emit_swap (void)
2936 {
2937 EMIT_ASM32 (i386_swap,
2938 "mov %eax,%ecx\n\t"
2939 "mov %ebx,%edx\n\t"
2940 "pop %eax\n\t"
2941 "pop %ebx\n\t"
2942 "push %edx\n\t"
2943 "push %ecx");
2944 }
2945
2946 static void
2947 i386_emit_stack_adjust (int n)
2948 {
2949 unsigned char buf[16];
2950 int i;
2951 CORE_ADDR buildaddr = current_insn_ptr;
2952
2953 i = 0;
2954 buf[i++] = 0x8d; /* lea $<n>(%esp),%esp */
2955 buf[i++] = 0x64;
2956 buf[i++] = 0x24;
2957 buf[i++] = n * 8;
2958 append_insns (&buildaddr, i, buf);
2959 current_insn_ptr = buildaddr;
2960 }
2961
2962 /* FN's prototype is `LONGEST(*fn)(int)'. */
2963
2964 static void
2965 i386_emit_int_call_1 (CORE_ADDR fn, int arg1)
2966 {
2967 unsigned char buf[16];
2968 int i;
2969 CORE_ADDR buildaddr;
2970
2971 EMIT_ASM32 (i386_int_call_1_a,
2972 /* Reserve a bit of stack space. */
2973 "sub $0x8,%esp");
2974 /* Put the one argument on the stack. */
2975 buildaddr = current_insn_ptr;
2976 i = 0;
2977 buf[i++] = 0xc7; /* movl $<arg1>,(%esp) */
2978 buf[i++] = 0x04;
2979 buf[i++] = 0x24;
2980 memcpy (&buf[i], &arg1, sizeof (arg1));
2981 i += 4;
2982 append_insns (&buildaddr, i, buf);
2983 current_insn_ptr = buildaddr;
2984 i386_emit_call (fn);
2985 EMIT_ASM32 (i386_int_call_1_c,
2986 "mov %edx,%ebx\n\t"
2987 "lea 0x8(%esp),%esp");
2988 }
2989
2990 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
2991
2992 static void
2993 i386_emit_void_call_2 (CORE_ADDR fn, int arg1)
2994 {
2995 unsigned char buf[16];
2996 int i;
2997 CORE_ADDR buildaddr;
2998
2999 EMIT_ASM32 (i386_void_call_2_a,
3000 /* Preserve %eax only; we don't have to worry about %ebx. */
3001 "push %eax\n\t"
3002 /* Reserve a bit of stack space for arguments. */
3003 "sub $0x10,%esp\n\t"
3004 /* Copy "top" to the second argument position. (Note that
3005 we can't assume function won't scribble on its
3006 arguments, so don't try to restore from this.) */
3007 "mov %eax,4(%esp)\n\t"
3008 "mov %ebx,8(%esp)");
3009 /* Put the first argument on the stack. */
3010 buildaddr = current_insn_ptr;
3011 i = 0;
3012 buf[i++] = 0xc7; /* movl $<arg1>,(%esp) */
3013 buf[i++] = 0x04;
3014 buf[i++] = 0x24;
3015 memcpy (&buf[i], &arg1, sizeof (arg1));
3016 i += 4;
3017 append_insns (&buildaddr, i, buf);
3018 current_insn_ptr = buildaddr;
3019 i386_emit_call (fn);
3020 EMIT_ASM32 (i386_void_call_2_b,
3021 "lea 0x10(%esp),%esp\n\t"
3022 /* Restore original stack top. */
3023 "pop %eax");
3024 }
3025
3026
3027 void
3028 i386_emit_eq_goto (int *offset_p, int *size_p)
3029 {
3030 EMIT_ASM32 (eq,
3031 /* Check low half first, more likely to be decider */
3032 "cmpl %eax,(%esp)\n\t"
3033 "jne .Leq_fallthru\n\t"
3034 "cmpl %ebx,4(%esp)\n\t"
3035 "jne .Leq_fallthru\n\t"
3036 "lea 0x8(%esp),%esp\n\t"
3037 "pop %eax\n\t"
3038 "pop %ebx\n\t"
3039 /* jmp, but don't trust the assembler to choose the right jump */
3040 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3041 ".Leq_fallthru:\n\t"
3042 "lea 0x8(%esp),%esp\n\t"
3043 "pop %eax\n\t"
3044 "pop %ebx");
3045
3046 if (offset_p)
3047 *offset_p = 18;
3048 if (size_p)
3049 *size_p = 4;
3050 }
3051
3052 void
3053 i386_emit_ne_goto (int *offset_p, int *size_p)
3054 {
3055 EMIT_ASM32 (ne,
3056 /* Check low half first, more likely to be decider */
3057 "cmpl %eax,(%esp)\n\t"
3058 "jne .Lne_jump\n\t"
3059 "cmpl %ebx,4(%esp)\n\t"
3060 "je .Lne_fallthru\n\t"
3061 ".Lne_jump:\n\t"
3062 "lea 0x8(%esp),%esp\n\t"
3063 "pop %eax\n\t"
3064 "pop %ebx\n\t"
3065 /* jmp, but don't trust the assembler to choose the right jump */
3066 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3067 ".Lne_fallthru:\n\t"
3068 "lea 0x8(%esp),%esp\n\t"
3069 "pop %eax\n\t"
3070 "pop %ebx");
3071
3072 if (offset_p)
3073 *offset_p = 18;
3074 if (size_p)
3075 *size_p = 4;
3076 }
3077
3078 void
3079 i386_emit_lt_goto (int *offset_p, int *size_p)
3080 {
3081 EMIT_ASM32 (lt,
3082 "cmpl %ebx,4(%esp)\n\t"
3083 "jl .Llt_jump\n\t"
3084 "jne .Llt_fallthru\n\t"
3085 "cmpl %eax,(%esp)\n\t"
3086 "jnl .Llt_fallthru\n\t"
3087 ".Llt_jump:\n\t"
3088 "lea 0x8(%esp),%esp\n\t"
3089 "pop %eax\n\t"
3090 "pop %ebx\n\t"
3091 /* jmp, but don't trust the assembler to choose the right jump */
3092 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3093 ".Llt_fallthru:\n\t"
3094 "lea 0x8(%esp),%esp\n\t"
3095 "pop %eax\n\t"
3096 "pop %ebx");
3097
3098 if (offset_p)
3099 *offset_p = 20;
3100 if (size_p)
3101 *size_p = 4;
3102 }
3103
3104 void
3105 i386_emit_le_goto (int *offset_p, int *size_p)
3106 {
3107 EMIT_ASM32 (le,
3108 "cmpl %ebx,4(%esp)\n\t"
3109 "jle .Lle_jump\n\t"
3110 "jne .Lle_fallthru\n\t"
3111 "cmpl %eax,(%esp)\n\t"
3112 "jnle .Lle_fallthru\n\t"
3113 ".Lle_jump:\n\t"
3114 "lea 0x8(%esp),%esp\n\t"
3115 "pop %eax\n\t"
3116 "pop %ebx\n\t"
3117 /* jmp, but don't trust the assembler to choose the right jump */
3118 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3119 ".Lle_fallthru:\n\t"
3120 "lea 0x8(%esp),%esp\n\t"
3121 "pop %eax\n\t"
3122 "pop %ebx");
3123
3124 if (offset_p)
3125 *offset_p = 20;
3126 if (size_p)
3127 *size_p = 4;
3128 }
3129
3130 void
3131 i386_emit_gt_goto (int *offset_p, int *size_p)
3132 {
3133 EMIT_ASM32 (gt,
3134 "cmpl %ebx,4(%esp)\n\t"
3135 "jg .Lgt_jump\n\t"
3136 "jne .Lgt_fallthru\n\t"
3137 "cmpl %eax,(%esp)\n\t"
3138 "jng .Lgt_fallthru\n\t"
3139 ".Lgt_jump:\n\t"
3140 "lea 0x8(%esp),%esp\n\t"
3141 "pop %eax\n\t"
3142 "pop %ebx\n\t"
3143 /* jmp, but don't trust the assembler to choose the right jump */
3144 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3145 ".Lgt_fallthru:\n\t"
3146 "lea 0x8(%esp),%esp\n\t"
3147 "pop %eax\n\t"
3148 "pop %ebx");
3149
3150 if (offset_p)
3151 *offset_p = 20;
3152 if (size_p)
3153 *size_p = 4;
3154 }
3155
3156 void
3157 i386_emit_ge_goto (int *offset_p, int *size_p)
3158 {
3159 EMIT_ASM32 (ge,
3160 "cmpl %ebx,4(%esp)\n\t"
3161 "jge .Lge_jump\n\t"
3162 "jne .Lge_fallthru\n\t"
3163 "cmpl %eax,(%esp)\n\t"
3164 "jnge .Lge_fallthru\n\t"
3165 ".Lge_jump:\n\t"
3166 "lea 0x8(%esp),%esp\n\t"
3167 "pop %eax\n\t"
3168 "pop %ebx\n\t"
3169 /* jmp, but don't trust the assembler to choose the right jump */
3170 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3171 ".Lge_fallthru:\n\t"
3172 "lea 0x8(%esp),%esp\n\t"
3173 "pop %eax\n\t"
3174 "pop %ebx");
3175
3176 if (offset_p)
3177 *offset_p = 20;
3178 if (size_p)
3179 *size_p = 4;
3180 }
3181
3182 struct emit_ops i386_emit_ops =
3183 {
3184 i386_emit_prologue,
3185 i386_emit_epilogue,
3186 i386_emit_add,
3187 i386_emit_sub,
3188 i386_emit_mul,
3189 i386_emit_lsh,
3190 i386_emit_rsh_signed,
3191 i386_emit_rsh_unsigned,
3192 i386_emit_ext,
3193 i386_emit_log_not,
3194 i386_emit_bit_and,
3195 i386_emit_bit_or,
3196 i386_emit_bit_xor,
3197 i386_emit_bit_not,
3198 i386_emit_equal,
3199 i386_emit_less_signed,
3200 i386_emit_less_unsigned,
3201 i386_emit_ref,
3202 i386_emit_if_goto,
3203 i386_emit_goto,
3204 i386_write_goto_address,
3205 i386_emit_const,
3206 i386_emit_call,
3207 i386_emit_reg,
3208 i386_emit_pop,
3209 i386_emit_stack_flush,
3210 i386_emit_zero_ext,
3211 i386_emit_swap,
3212 i386_emit_stack_adjust,
3213 i386_emit_int_call_1,
3214 i386_emit_void_call_2,
3215 i386_emit_eq_goto,
3216 i386_emit_ne_goto,
3217 i386_emit_lt_goto,
3218 i386_emit_le_goto,
3219 i386_emit_gt_goto,
3220 i386_emit_ge_goto
3221 };
3222
3223
3224 static struct emit_ops *
3225 x86_emit_ops (void)
3226 {
3227 #ifdef __x86_64__
3228 if (is_64bit_tdesc ())
3229 return &amd64_emit_ops;
3230 else
3231 #endif
3232 return &i386_emit_ops;
3233 }
3234
3235 static int
3236 x86_supports_range_stepping (void)
3237 {
3238 return 1;
3239 }
3240
3241 /* This is initialized assuming an amd64 target.
3242 x86_arch_setup will correct it for i386 or amd64 targets. */
3243
3244 struct linux_target_ops the_low_target =
3245 {
3246 x86_arch_setup,
3247 x86_linux_regs_info,
3248 x86_cannot_fetch_register,
3249 x86_cannot_store_register,
3250 NULL, /* fetch_register */
3251 x86_get_pc,
3252 x86_set_pc,
3253 x86_breakpoint,
3254 x86_breakpoint_len,
3255 NULL,
3256 1,
3257 x86_breakpoint_at,
3258 x86_supports_z_point_type,
3259 x86_insert_point,
3260 x86_remove_point,
3261 x86_stopped_by_watchpoint,
3262 x86_stopped_data_address,
3263 /* collect_ptrace_register/supply_ptrace_register are not needed in the
3264 native i386 case (no registers smaller than an xfer unit), and are not
3265 used in the biarch case (HAVE_LINUX_USRREGS is not defined). */
3266 NULL,
3267 NULL,
3268 /* need to fix up i386 siginfo if host is amd64 */
3269 x86_siginfo_fixup,
3270 x86_linux_new_process,
3271 x86_linux_new_thread,
3272 x86_linux_prepare_to_resume,
3273 x86_linux_process_qsupported,
3274 x86_supports_tracepoints,
3275 x86_get_thread_area,
3276 x86_install_fast_tracepoint_jump_pad,
3277 x86_emit_ops,
3278 x86_get_min_fast_tracepoint_insn_len,
3279 x86_supports_range_stepping,
3280 };
3281
3282 void
3283 initialize_low_arch (void)
3284 {
3285 /* Initialize the Linux target descriptions. */
3286 #ifdef __x86_64__
3287 init_registers_amd64_linux ();
3288 init_registers_amd64_avx_linux ();
3289 init_registers_amd64_avx512_linux ();
3290 init_registers_amd64_mpx_linux ();
3291
3292 init_registers_x32_linux ();
3293 init_registers_x32_avx_linux ();
3294 init_registers_x32_avx512_linux ();
3295
3296 tdesc_amd64_linux_no_xml = xmalloc (sizeof (struct target_desc));
3297 copy_target_description (tdesc_amd64_linux_no_xml, tdesc_amd64_linux);
3298 tdesc_amd64_linux_no_xml->xmltarget = xmltarget_amd64_linux_no_xml;
3299 #endif
3300 init_registers_i386_linux ();
3301 init_registers_i386_mmx_linux ();
3302 init_registers_i386_avx_linux ();
3303 init_registers_i386_avx512_linux ();
3304 init_registers_i386_mpx_linux ();
3305
3306 tdesc_i386_linux_no_xml = xmalloc (sizeof (struct target_desc));
3307 copy_target_description (tdesc_i386_linux_no_xml, tdesc_i386_linux);
3308 tdesc_i386_linux_no_xml->xmltarget = xmltarget_i386_linux_no_xml;
3309
3310 initialize_regsets_info (&x86_regsets_info);
3311 }