1 /* GNU/Linux/x86-64 specific low level interface, for the remote server
3 Copyright (C) 2002-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
24 #include "linux-low.h"
27 #include "gdbsupport/x86-xstate.h"
28 #include "nat/gdb_ptrace.h"
31 #include "nat/amd64-linux-siginfo.h"
34 #include "gdb_proc_service.h"
35 /* Don't include elf/common.h if linux/elf.h got included by
36 gdb_proc_service.h. */
38 #include "elf/common.h"
41 #include "gdbsupport/agent.h"
43 #include "tracepoint.h"
45 #include "nat/linux-nat.h"
46 #include "nat/x86-linux.h"
47 #include "nat/x86-linux-dregs.h"
48 #include "linux-x86-tdesc.h"
51 static target_desc_up tdesc_amd64_linux_no_xml
;
53 static target_desc_up tdesc_i386_linux_no_xml
;
56 static unsigned char jump_insn
[] = { 0xe9, 0, 0, 0, 0 };
57 static unsigned char small_jump_insn
[] = { 0x66, 0xe9, 0, 0 };
59 /* Backward compatibility for gdb without XML support. */
61 static const char xmltarget_i386_linux_no_xml
[] = "@<target>\
62 <architecture>i386</architecture>\
63 <osabi>GNU/Linux</osabi>\
67 static const char xmltarget_amd64_linux_no_xml
[] = "@<target>\
68 <architecture>i386:x86-64</architecture>\
69 <osabi>GNU/Linux</osabi>\
74 #include <sys/procfs.h>
77 #ifndef PTRACE_GET_THREAD_AREA
78 #define PTRACE_GET_THREAD_AREA 25
81 /* This definition comes from prctl.h, but some kernels may not have it. */
82 #ifndef PTRACE_ARCH_PRCTL
83 #define PTRACE_ARCH_PRCTL 30
86 /* The following definitions come from prctl.h, but may be absent
87 for certain configurations. */
89 #define ARCH_SET_GS 0x1001
90 #define ARCH_SET_FS 0x1002
91 #define ARCH_GET_FS 0x1003
92 #define ARCH_GET_GS 0x1004
95 /* Linux target op definitions for the x86 architecture.
96 This is initialized assuming an amd64 target.
97 'low_arch_setup' will correct it for i386 or amd64 targets. */
99 class x86_target
: public linux_process_target
103 const regs_info
*get_regs_info () override
;
105 const gdb_byte
*sw_breakpoint_from_kind (int kind
, int *size
) override
;
107 bool supports_z_point_type (char z_type
) override
;
109 void process_qsupported (gdb::array_view
<const char * const> features
) override
;
111 bool supports_tracepoints () override
;
113 bool supports_fast_tracepoints () override
;
115 int install_fast_tracepoint_jump_pad
116 (CORE_ADDR tpoint
, CORE_ADDR tpaddr
, CORE_ADDR collector
,
117 CORE_ADDR lockaddr
, ULONGEST orig_size
, CORE_ADDR
*jump_entry
,
118 CORE_ADDR
*trampoline
, ULONGEST
*trampoline_size
,
119 unsigned char *jjump_pad_insn
, ULONGEST
*jjump_pad_insn_size
,
120 CORE_ADDR
*adjusted_insn_addr
, CORE_ADDR
*adjusted_insn_addr_end
,
123 int get_min_fast_tracepoint_insn_len () override
;
125 struct emit_ops
*emit_ops () override
;
127 int get_ipa_tdesc_idx () override
;
131 void low_arch_setup () override
;
133 bool low_cannot_fetch_register (int regno
) override
;
135 bool low_cannot_store_register (int regno
) override
;
137 bool low_supports_breakpoints () override
;
139 CORE_ADDR
low_get_pc (regcache
*regcache
) override
;
141 void low_set_pc (regcache
*regcache
, CORE_ADDR newpc
) override
;
143 int low_decr_pc_after_break () override
;
145 bool low_breakpoint_at (CORE_ADDR pc
) override
;
147 int low_insert_point (raw_bkpt_type type
, CORE_ADDR addr
,
148 int size
, raw_breakpoint
*bp
) override
;
150 int low_remove_point (raw_bkpt_type type
, CORE_ADDR addr
,
151 int size
, raw_breakpoint
*bp
) override
;
153 bool low_stopped_by_watchpoint () override
;
155 CORE_ADDR
low_stopped_data_address () override
;
157 /* collect_ptrace_register/supply_ptrace_register are not needed in the
158 native i386 case (no registers smaller than an xfer unit), and are not
159 used in the biarch case (HAVE_LINUX_USRREGS is not defined). */
161 /* Need to fix up i386 siginfo if host is amd64. */
162 bool low_siginfo_fixup (siginfo_t
*native
, gdb_byte
*inf
,
163 int direction
) override
;
165 arch_process_info
*low_new_process () override
;
167 void low_delete_process (arch_process_info
*info
) override
;
169 void low_new_thread (lwp_info
*) override
;
171 void low_delete_thread (arch_lwp_info
*) override
;
173 void low_new_fork (process_info
*parent
, process_info
*child
) override
;
175 void low_prepare_to_resume (lwp_info
*lwp
) override
;
177 int low_get_thread_area (int lwpid
, CORE_ADDR
*addrp
) override
;
179 bool low_supports_range_stepping () override
;
181 bool low_supports_catch_syscall () override
;
183 void low_get_syscall_trapinfo (regcache
*regcache
, int *sysno
) override
;
187 /* Update all the target description of all processes; a new GDB
188 connected, and it may or not support xml target descriptions. */
189 void update_xmltarget ();
192 /* The singleton target ops object. */
194 static x86_target the_x86_target
;
196 /* Per-process arch-specific data we want to keep. */
198 struct arch_process_info
200 struct x86_debug_reg_state debug_reg_state
;
205 /* Mapping between the general-purpose registers in `struct user'
206 format and GDB's register array layout.
207 Note that the transfer layout uses 64-bit regs. */
208 static /*const*/ int i386_regmap
[] =
210 RAX
* 8, RCX
* 8, RDX
* 8, RBX
* 8,
211 RSP
* 8, RBP
* 8, RSI
* 8, RDI
* 8,
212 RIP
* 8, EFLAGS
* 8, CS
* 8, SS
* 8,
213 DS
* 8, ES
* 8, FS
* 8, GS
* 8
216 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
218 /* So code below doesn't have to care, i386 or amd64. */
219 #define ORIG_EAX ORIG_RAX
222 static const int x86_64_regmap
[] =
224 RAX
* 8, RBX
* 8, RCX
* 8, RDX
* 8,
225 RSI
* 8, RDI
* 8, RBP
* 8, RSP
* 8,
226 R8
* 8, R9
* 8, R10
* 8, R11
* 8,
227 R12
* 8, R13
* 8, R14
* 8, R15
* 8,
228 RIP
* 8, EFLAGS
* 8, CS
* 8, SS
* 8,
229 DS
* 8, ES
* 8, FS
* 8, GS
* 8,
230 -1, -1, -1, -1, -1, -1, -1, -1,
231 -1, -1, -1, -1, -1, -1, -1, -1,
232 -1, -1, -1, -1, -1, -1, -1, -1,
234 -1, -1, -1, -1, -1, -1, -1, -1,
237 -1, -1, -1, -1, /* MPX registers BND0 ... BND3. */
238 -1, -1, /* MPX registers BNDCFGU, BNDSTATUS. */
239 -1, -1, -1, -1, -1, -1, -1, -1, /* xmm16 ... xmm31 (AVX512) */
240 -1, -1, -1, -1, -1, -1, -1, -1,
241 -1, -1, -1, -1, -1, -1, -1, -1, /* ymm16 ... ymm31 (AVX512) */
242 -1, -1, -1, -1, -1, -1, -1, -1,
243 -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512) */
244 -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm31 (AVX512) */
245 -1, -1, -1, -1, -1, -1, -1, -1,
246 -1, -1, -1, -1, -1, -1, -1, -1,
247 -1, -1, -1, -1, -1, -1, -1, -1,
251 #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0]))
252 #define X86_64_USER_REGS (GS + 1)
254 #else /* ! __x86_64__ */
256 /* Mapping between the general-purpose registers in `struct user'
257 format and GDB's register array layout. */
258 static /*const*/ int i386_regmap
[] =
260 EAX
* 4, ECX
* 4, EDX
* 4, EBX
* 4,
261 UESP
* 4, EBP
* 4, ESI
* 4, EDI
* 4,
262 EIP
* 4, EFL
* 4, CS
* 4, SS
* 4,
263 DS
* 4, ES
* 4, FS
* 4, GS
* 4
266 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
274 /* Returns true if the current inferior belongs to a x86-64 process,
278 is_64bit_tdesc (void)
280 struct regcache
*regcache
= get_thread_regcache (current_thread
, 0);
282 return register_size (regcache
->tdesc
, 0) == 8;
288 /* Called by libthread_db. */
291 ps_get_thread_area (struct ps_prochandle
*ph
,
292 lwpid_t lwpid
, int idx
, void **base
)
295 int use_64bit
= is_64bit_tdesc ();
302 if (ptrace (PTRACE_ARCH_PRCTL
, lwpid
, base
, ARCH_GET_FS
) == 0)
306 if (ptrace (PTRACE_ARCH_PRCTL
, lwpid
, base
, ARCH_GET_GS
) == 0)
317 unsigned int desc
[4];
319 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
,
320 (void *) (intptr_t) idx
, (unsigned long) &desc
) < 0)
323 /* Ensure we properly extend the value to 64-bits for x86_64. */
324 *base
= (void *) (uintptr_t) desc
[1];
329 /* Get the thread area address. This is used to recognize which
330 thread is which when tracing with the in-process agent library. We
331 don't read anything from the address, and treat it as opaque; it's
332 the address itself that we assume is unique per-thread. */
335 x86_target::low_get_thread_area (int lwpid
, CORE_ADDR
*addr
)
338 int use_64bit
= is_64bit_tdesc ();
343 if (ptrace (PTRACE_ARCH_PRCTL
, lwpid
, &base
, ARCH_GET_FS
) == 0)
345 *addr
= (CORE_ADDR
) (uintptr_t) base
;
354 struct lwp_info
*lwp
= find_lwp_pid (ptid_t (lwpid
));
355 struct thread_info
*thr
= get_lwp_thread (lwp
);
356 struct regcache
*regcache
= get_thread_regcache (thr
, 1);
357 unsigned int desc
[4];
359 const int reg_thread_area
= 3; /* bits to scale down register value. */
362 collect_register_by_name (regcache
, "gs", &gs
);
364 idx
= gs
>> reg_thread_area
;
366 if (ptrace (PTRACE_GET_THREAD_AREA
,
368 (void *) (long) idx
, (unsigned long) &desc
) < 0)
379 x86_target::low_cannot_store_register (int regno
)
382 if (is_64bit_tdesc ())
386 return regno
>= I386_NUM_REGS
;
390 x86_target::low_cannot_fetch_register (int regno
)
393 if (is_64bit_tdesc ())
397 return regno
>= I386_NUM_REGS
;
401 collect_register_i386 (struct regcache
*regcache
, int regno
, void *buf
)
403 collect_register (regcache
, regno
, buf
);
406 /* In case of x86_64 -m32, collect_register only writes 4 bytes, but the
407 space reserved in buf for the register is 8 bytes. Make sure the entire
408 reserved space is initialized. */
410 gdb_assert (register_size (regcache
->tdesc
, regno
) == 4);
414 /* Sign extend EAX value to avoid potential syscall restart
417 See amd64_linux_collect_native_gregset() in
418 gdb/amd64-linux-nat.c for a detailed explanation. */
419 *(int64_t *) buf
= *(int32_t *) buf
;
424 *(uint64_t *) buf
= *(uint32_t *) buf
;
430 x86_fill_gregset (struct regcache
*regcache
, void *buf
)
435 if (register_size (regcache
->tdesc
, 0) == 8)
437 for (i
= 0; i
< X86_64_NUM_REGS
; i
++)
438 if (x86_64_regmap
[i
] != -1)
439 collect_register (regcache
, i
, ((char *) buf
) + x86_64_regmap
[i
]);
445 for (i
= 0; i
< I386_NUM_REGS
; i
++)
446 collect_register_i386 (regcache
, i
, ((char *) buf
) + i386_regmap
[i
]);
448 /* Handle ORIG_EAX, which is not in i386_regmap. */
449 collect_register_i386 (regcache
, find_regno (regcache
->tdesc
, "orig_eax"),
450 ((char *) buf
) + ORIG_EAX
* REGSIZE
);
454 x86_store_gregset (struct regcache
*regcache
, const void *buf
)
459 if (register_size (regcache
->tdesc
, 0) == 8)
461 for (i
= 0; i
< X86_64_NUM_REGS
; i
++)
462 if (x86_64_regmap
[i
] != -1)
463 supply_register (regcache
, i
, ((char *) buf
) + x86_64_regmap
[i
]);
469 for (i
= 0; i
< I386_NUM_REGS
; i
++)
470 supply_register (regcache
, i
, ((char *) buf
) + i386_regmap
[i
]);
472 supply_register_by_name (regcache
, "orig_eax",
473 ((char *) buf
) + ORIG_EAX
* REGSIZE
);
477 x86_fill_fpregset (struct regcache
*regcache
, void *buf
)
480 i387_cache_to_fxsave (regcache
, buf
);
482 i387_cache_to_fsave (regcache
, buf
);
487 x86_store_fpregset (struct regcache
*regcache
, const void *buf
)
490 i387_fxsave_to_cache (regcache
, buf
);
492 i387_fsave_to_cache (regcache
, buf
);
499 x86_fill_fpxregset (struct regcache
*regcache
, void *buf
)
501 i387_cache_to_fxsave (regcache
, buf
);
505 x86_store_fpxregset (struct regcache
*regcache
, const void *buf
)
507 i387_fxsave_to_cache (regcache
, buf
);
513 x86_fill_xstateregset (struct regcache
*regcache
, void *buf
)
515 i387_cache_to_xsave (regcache
, buf
);
519 x86_store_xstateregset (struct regcache
*regcache
, const void *buf
)
521 i387_xsave_to_cache (regcache
, buf
);
524 /* ??? The non-biarch i386 case stores all the i387 regs twice.
525 Once in i387_.*fsave.* and once in i387_.*fxsave.*.
526 This is, presumably, to handle the case where PTRACE_[GS]ETFPXREGS
527 doesn't work. IWBN to avoid the duplication in the case where it
528 does work. Maybe the arch_setup routine could check whether it works
529 and update the supported regsets accordingly. */
531 static struct regset_info x86_regsets
[] =
533 #ifdef HAVE_PTRACE_GETREGS
534 { PTRACE_GETREGS
, PTRACE_SETREGS
, 0, sizeof (elf_gregset_t
),
536 x86_fill_gregset
, x86_store_gregset
},
537 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_X86_XSTATE
, 0,
538 EXTENDED_REGS
, x86_fill_xstateregset
, x86_store_xstateregset
},
540 # ifdef HAVE_PTRACE_GETFPXREGS
541 { PTRACE_GETFPXREGS
, PTRACE_SETFPXREGS
, 0, sizeof (elf_fpxregset_t
),
543 x86_fill_fpxregset
, x86_store_fpxregset
},
546 { PTRACE_GETFPREGS
, PTRACE_SETFPREGS
, 0, sizeof (elf_fpregset_t
),
548 x86_fill_fpregset
, x86_store_fpregset
},
549 #endif /* HAVE_PTRACE_GETREGS */
554 x86_target::low_supports_breakpoints ()
560 x86_target::low_get_pc (regcache
*regcache
)
562 int use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
568 collect_register_by_name (regcache
, "rip", &pc
);
569 return (CORE_ADDR
) pc
;
575 collect_register_by_name (regcache
, "eip", &pc
);
576 return (CORE_ADDR
) pc
;
581 x86_target::low_set_pc (regcache
*regcache
, CORE_ADDR pc
)
583 int use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
589 supply_register_by_name (regcache
, "rip", &newpc
);
595 supply_register_by_name (regcache
, "eip", &newpc
);
600 x86_target::low_decr_pc_after_break ()
606 static const gdb_byte x86_breakpoint
[] = { 0xCC };
607 #define x86_breakpoint_len 1
610 x86_target::low_breakpoint_at (CORE_ADDR pc
)
614 read_memory (pc
, &c
, 1);
621 /* Low-level function vector. */
622 struct x86_dr_low_type x86_dr_low
=
624 x86_linux_dr_set_control
,
625 x86_linux_dr_set_addr
,
626 x86_linux_dr_get_addr
,
627 x86_linux_dr_get_status
,
628 x86_linux_dr_get_control
,
632 /* Breakpoint/Watchpoint support. */
635 x86_target::supports_z_point_type (char z_type
)
641 case Z_PACKET_WRITE_WP
:
642 case Z_PACKET_ACCESS_WP
:
650 x86_target::low_insert_point (raw_bkpt_type type
, CORE_ADDR addr
,
651 int size
, raw_breakpoint
*bp
)
653 struct process_info
*proc
= current_process ();
657 case raw_bkpt_type_hw
:
658 case raw_bkpt_type_write_wp
:
659 case raw_bkpt_type_access_wp
:
661 enum target_hw_bp_type hw_type
662 = raw_bkpt_type_to_target_hw_bp_type (type
);
663 struct x86_debug_reg_state
*state
664 = &proc
->priv
->arch_private
->debug_reg_state
;
666 return x86_dr_insert_watchpoint (state
, hw_type
, addr
, size
);
676 x86_target::low_remove_point (raw_bkpt_type type
, CORE_ADDR addr
,
677 int size
, raw_breakpoint
*bp
)
679 struct process_info
*proc
= current_process ();
683 case raw_bkpt_type_hw
:
684 case raw_bkpt_type_write_wp
:
685 case raw_bkpt_type_access_wp
:
687 enum target_hw_bp_type hw_type
688 = raw_bkpt_type_to_target_hw_bp_type (type
);
689 struct x86_debug_reg_state
*state
690 = &proc
->priv
->arch_private
->debug_reg_state
;
692 return x86_dr_remove_watchpoint (state
, hw_type
, addr
, size
);
701 x86_target::low_stopped_by_watchpoint ()
703 struct process_info
*proc
= current_process ();
704 return x86_dr_stopped_by_watchpoint (&proc
->priv
->arch_private
->debug_reg_state
);
708 x86_target::low_stopped_data_address ()
710 struct process_info
*proc
= current_process ();
712 if (x86_dr_stopped_data_address (&proc
->priv
->arch_private
->debug_reg_state
,
718 /* Called when a new process is created. */
721 x86_target::low_new_process ()
723 struct arch_process_info
*info
= XCNEW (struct arch_process_info
);
725 x86_low_init_dregs (&info
->debug_reg_state
);
730 /* Called when a process is being deleted. */
733 x86_target::low_delete_process (arch_process_info
*info
)
739 x86_target::low_new_thread (lwp_info
*lwp
)
741 /* This comes from nat/. */
742 x86_linux_new_thread (lwp
);
746 x86_target::low_delete_thread (arch_lwp_info
*alwp
)
748 /* This comes from nat/. */
749 x86_linux_delete_thread (alwp
);
752 /* Target routine for new_fork. */
755 x86_target::low_new_fork (process_info
*parent
, process_info
*child
)
757 /* These are allocated by linux_add_process. */
758 gdb_assert (parent
->priv
!= NULL
759 && parent
->priv
->arch_private
!= NULL
);
760 gdb_assert (child
->priv
!= NULL
761 && child
->priv
->arch_private
!= NULL
);
763 /* Linux kernel before 2.6.33 commit
764 72f674d203cd230426437cdcf7dd6f681dad8b0d
765 will inherit hardware debug registers from parent
766 on fork/vfork/clone. Newer Linux kernels create such tasks with
767 zeroed debug registers.
769 GDB core assumes the child inherits the watchpoints/hw
770 breakpoints of the parent, and will remove them all from the
771 forked off process. Copy the debug registers mirrors into the
772 new process so that all breakpoints and watchpoints can be
773 removed together. The debug registers mirror will become zeroed
774 in the end before detaching the forked off process, thus making
775 this compatible with older Linux kernels too. */
777 *child
->priv
->arch_private
= *parent
->priv
->arch_private
;
781 x86_target::low_prepare_to_resume (lwp_info
*lwp
)
783 /* This comes from nat/. */
784 x86_linux_prepare_to_resume (lwp
);
787 /* See nat/x86-dregs.h. */
789 struct x86_debug_reg_state
*
790 x86_debug_reg_state (pid_t pid
)
792 struct process_info
*proc
= find_process_pid (pid
);
794 return &proc
->priv
->arch_private
->debug_reg_state
;
797 /* When GDBSERVER is built as a 64-bit application on linux, the
798 PTRACE_GETSIGINFO data is always presented in 64-bit layout. Since
799 debugging a 32-bit inferior with a 64-bit GDBSERVER should look the same
800 as debugging it with a 32-bit GDBSERVER, we do the 32-bit <-> 64-bit
801 conversion in-place ourselves. */
803 /* Convert a ptrace/host siginfo object, into/from the siginfo in the
804 layout of the inferiors' architecture. Returns true if any
805 conversion was done; false otherwise. If DIRECTION is 1, then copy
806 from INF to PTRACE. If DIRECTION is 0, copy from PTRACE to
810 x86_target::low_siginfo_fixup (siginfo_t
*ptrace
, gdb_byte
*inf
, int direction
)
813 unsigned int machine
;
814 int tid
= lwpid_of (current_thread
);
815 int is_elf64
= linux_pid_exe_is_elf_64_file (tid
, &machine
);
817 /* Is the inferior 32-bit? If so, then fixup the siginfo object. */
818 if (!is_64bit_tdesc ())
819 return amd64_linux_siginfo_fixup_common (ptrace
, inf
, direction
,
821 /* No fixup for native x32 GDB. */
822 else if (!is_elf64
&& sizeof (void *) == 8)
823 return amd64_linux_siginfo_fixup_common (ptrace
, inf
, direction
,
832 /* Format of XSAVE extended state is:
836 sw_usable_bytes[464..511]
837 xstate_hdr_bytes[512..575]
842 Same memory layout will be used for the coredump NT_X86_XSTATE
843 representing the XSAVE extended state registers.
845 The first 8 bytes of the sw_usable_bytes[464..467] is the OS enabled
846 extended state mask, which is the same as the extended control register
847 0 (the XFEATURE_ENABLED_MASK register), XCR0. We can use this mask
848 together with the mask saved in the xstate_hdr_bytes to determine what
849 states the processor/OS supports and what state, used or initialized,
850 the process/thread is in. */
851 #define I386_LINUX_XSAVE_XCR0_OFFSET 464
853 /* Does the current host support the GETFPXREGS request? The header
854 file may or may not define it, and even if it is defined, the
855 kernel will return EIO if it's running on a pre-SSE processor. */
856 int have_ptrace_getfpxregs
=
857 #ifdef HAVE_PTRACE_GETFPXREGS
864 /* Get Linux/x86 target description from running target. */
866 static const struct target_desc
*
867 x86_linux_read_description (void)
869 unsigned int machine
;
873 static uint64_t xcr0
;
874 struct regset_info
*regset
;
876 tid
= lwpid_of (current_thread
);
878 is_elf64
= linux_pid_exe_is_elf_64_file (tid
, &machine
);
880 if (sizeof (void *) == 4)
883 error (_("Can't debug 64-bit process with 32-bit GDBserver"));
885 else if (machine
== EM_X86_64
)
886 error (_("Can't debug x86-64 process with 32-bit GDBserver"));
890 #if !defined __x86_64__ && defined HAVE_PTRACE_GETFPXREGS
891 if (machine
== EM_386
&& have_ptrace_getfpxregs
== -1)
893 elf_fpxregset_t fpxregs
;
895 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (long) &fpxregs
) < 0)
897 have_ptrace_getfpxregs
= 0;
898 have_ptrace_getregset
= 0;
899 return i386_linux_read_description (X86_XSTATE_X87
);
902 have_ptrace_getfpxregs
= 1;
908 x86_xcr0
= X86_XSTATE_SSE_MASK
;
912 if (machine
== EM_X86_64
)
913 return tdesc_amd64_linux_no_xml
.get ();
916 return tdesc_i386_linux_no_xml
.get ();
919 if (have_ptrace_getregset
== -1)
921 uint64_t xstateregs
[(X86_XSTATE_SSE_SIZE
/ sizeof (uint64_t))];
924 iov
.iov_base
= xstateregs
;
925 iov
.iov_len
= sizeof (xstateregs
);
927 /* Check if PTRACE_GETREGSET works. */
928 if (ptrace (PTRACE_GETREGSET
, tid
,
929 (unsigned int) NT_X86_XSTATE
, (long) &iov
) < 0)
930 have_ptrace_getregset
= 0;
933 have_ptrace_getregset
= 1;
935 /* Get XCR0 from XSAVE extended state. */
936 xcr0
= xstateregs
[(I386_LINUX_XSAVE_XCR0_OFFSET
937 / sizeof (uint64_t))];
939 /* Use PTRACE_GETREGSET if it is available. */
940 for (regset
= x86_regsets
;
941 regset
->fill_function
!= NULL
; regset
++)
942 if (regset
->get_request
== PTRACE_GETREGSET
)
943 regset
->size
= X86_XSTATE_SIZE (xcr0
);
944 else if (regset
->type
!= GENERAL_REGS
)
949 /* Check the native XCR0 only if PTRACE_GETREGSET is available. */
950 xcr0_features
= (have_ptrace_getregset
951 && (xcr0
& X86_XSTATE_ALL_MASK
));
956 if (machine
== EM_X86_64
)
959 const target_desc
*tdesc
= NULL
;
963 tdesc
= amd64_linux_read_description (xcr0
& X86_XSTATE_ALL_MASK
,
968 tdesc
= amd64_linux_read_description (X86_XSTATE_SSE_MASK
, !is_elf64
);
974 const target_desc
*tdesc
= NULL
;
977 tdesc
= i386_linux_read_description (xcr0
& X86_XSTATE_ALL_MASK
);
980 tdesc
= i386_linux_read_description (X86_XSTATE_SSE
);
985 gdb_assert_not_reached ("failed to return tdesc");
988 /* Update all the target description of all processes; a new GDB
989 connected, and it may or not support xml target descriptions. */
992 x86_target::update_xmltarget ()
994 scoped_restore_current_thread restore_thread
;
996 /* Before changing the register cache's internal layout, flush the
997 contents of the current valid caches back to the threads, and
998 release the current regcache objects. */
1001 for_each_process ([this] (process_info
*proc
) {
1002 int pid
= proc
->pid
;
1004 /* Look up any thread of this process. */
1005 switch_to_thread (find_any_thread_of_pid (pid
));
1011 /* Process qSupported query, "xmlRegisters=". Update the buffer size for
1012 PTRACE_GETREGSET. */
1015 x86_target::process_qsupported (gdb::array_view
<const char * const> features
)
1017 /* Return if gdb doesn't support XML. If gdb sends "xmlRegisters="
1018 with "i386" in qSupported query, it supports x86 XML target
1022 for (const char *feature
: features
)
1024 if (startswith (feature
, "xmlRegisters="))
1026 char *copy
= xstrdup (feature
+ 13);
1029 for (char *p
= strtok_r (copy
, ",", &saveptr
);
1031 p
= strtok_r (NULL
, ",", &saveptr
))
1033 if (strcmp (p
, "i386") == 0)
1044 update_xmltarget ();
1047 /* Common for x86/x86-64. */
1049 static struct regsets_info x86_regsets_info
=
1051 x86_regsets
, /* regsets */
1052 0, /* num_regsets */
1053 NULL
, /* disabled_regsets */
1057 static struct regs_info amd64_linux_regs_info
=
1059 NULL
, /* regset_bitmap */
1060 NULL
, /* usrregs_info */
1064 static struct usrregs_info i386_linux_usrregs_info
=
1070 static struct regs_info i386_linux_regs_info
=
1072 NULL
, /* regset_bitmap */
1073 &i386_linux_usrregs_info
,
1078 x86_target::get_regs_info ()
1081 if (is_64bit_tdesc ())
1082 return &amd64_linux_regs_info
;
1085 return &i386_linux_regs_info
;
1088 /* Initialize the target description for the architecture of the
1092 x86_target::low_arch_setup ()
1094 current_process ()->tdesc
= x86_linux_read_description ();
1098 x86_target::low_supports_catch_syscall ()
1103 /* Fill *SYSNO and *SYSRET with the syscall nr trapped and the syscall return
1104 code. This should only be called if LWP got a SYSCALL_SIGTRAP. */
1107 x86_target::low_get_syscall_trapinfo (regcache
*regcache
, int *sysno
)
1109 int use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
1115 collect_register_by_name (regcache
, "orig_rax", &l_sysno
);
1116 *sysno
= (int) l_sysno
;
1119 collect_register_by_name (regcache
, "orig_eax", sysno
);
1123 x86_target::supports_tracepoints ()
1129 append_insns (CORE_ADDR
*to
, size_t len
, const unsigned char *buf
)
1131 target_write_memory (*to
, buf
, len
);
1136 push_opcode (unsigned char *buf
, const char *op
)
1138 unsigned char *buf_org
= buf
;
1143 unsigned long ul
= strtoul (op
, &endptr
, 16);
1152 return buf
- buf_org
;
1157 /* Build a jump pad that saves registers and calls a collection
1158 function. Writes a jump instruction to the jump pad to
1159 JJUMPAD_INSN. The caller is responsible to write it in at the
1160 tracepoint address. */
1163 amd64_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint
, CORE_ADDR tpaddr
,
1164 CORE_ADDR collector
,
1167 CORE_ADDR
*jump_entry
,
1168 CORE_ADDR
*trampoline
,
1169 ULONGEST
*trampoline_size
,
1170 unsigned char *jjump_pad_insn
,
1171 ULONGEST
*jjump_pad_insn_size
,
1172 CORE_ADDR
*adjusted_insn_addr
,
1173 CORE_ADDR
*adjusted_insn_addr_end
,
1176 unsigned char buf
[40];
1180 CORE_ADDR buildaddr
= *jump_entry
;
1182 /* Build the jump pad. */
1184 /* First, do tracepoint data collection. Save registers. */
1186 /* Need to ensure stack pointer saved first. */
1187 buf
[i
++] = 0x54; /* push %rsp */
1188 buf
[i
++] = 0x55; /* push %rbp */
1189 buf
[i
++] = 0x57; /* push %rdi */
1190 buf
[i
++] = 0x56; /* push %rsi */
1191 buf
[i
++] = 0x52; /* push %rdx */
1192 buf
[i
++] = 0x51; /* push %rcx */
1193 buf
[i
++] = 0x53; /* push %rbx */
1194 buf
[i
++] = 0x50; /* push %rax */
1195 buf
[i
++] = 0x41; buf
[i
++] = 0x57; /* push %r15 */
1196 buf
[i
++] = 0x41; buf
[i
++] = 0x56; /* push %r14 */
1197 buf
[i
++] = 0x41; buf
[i
++] = 0x55; /* push %r13 */
1198 buf
[i
++] = 0x41; buf
[i
++] = 0x54; /* push %r12 */
1199 buf
[i
++] = 0x41; buf
[i
++] = 0x53; /* push %r11 */
1200 buf
[i
++] = 0x41; buf
[i
++] = 0x52; /* push %r10 */
1201 buf
[i
++] = 0x41; buf
[i
++] = 0x51; /* push %r9 */
1202 buf
[i
++] = 0x41; buf
[i
++] = 0x50; /* push %r8 */
1203 buf
[i
++] = 0x9c; /* pushfq */
1204 buf
[i
++] = 0x48; /* movabs <addr>,%rdi */
1206 memcpy (buf
+ i
, &tpaddr
, 8);
1208 buf
[i
++] = 0x57; /* push %rdi */
1209 append_insns (&buildaddr
, i
, buf
);
1211 /* Stack space for the collecting_t object. */
1213 i
+= push_opcode (&buf
[i
], "48 83 ec 18"); /* sub $0x18,%rsp */
1214 i
+= push_opcode (&buf
[i
], "48 b8"); /* mov <tpoint>,%rax */
1215 memcpy (buf
+ i
, &tpoint
, 8);
1217 i
+= push_opcode (&buf
[i
], "48 89 04 24"); /* mov %rax,(%rsp) */
1218 i
+= push_opcode (&buf
[i
],
1219 "64 48 8b 04 25 00 00 00 00"); /* mov %fs:0x0,%rax */
1220 i
+= push_opcode (&buf
[i
], "48 89 44 24 08"); /* mov %rax,0x8(%rsp) */
1221 append_insns (&buildaddr
, i
, buf
);
1225 i
+= push_opcode (&buf
[i
], "48 be"); /* movl <lockaddr>,%rsi */
1226 memcpy (&buf
[i
], (void *) &lockaddr
, 8);
1228 i
+= push_opcode (&buf
[i
], "48 89 e1"); /* mov %rsp,%rcx */
1229 i
+= push_opcode (&buf
[i
], "31 c0"); /* xor %eax,%eax */
1230 i
+= push_opcode (&buf
[i
], "f0 48 0f b1 0e"); /* lock cmpxchg %rcx,(%rsi) */
1231 i
+= push_opcode (&buf
[i
], "48 85 c0"); /* test %rax,%rax */
1232 i
+= push_opcode (&buf
[i
], "75 f4"); /* jne <again> */
1233 append_insns (&buildaddr
, i
, buf
);
1235 /* Set up the gdb_collect call. */
1236 /* At this point, (stack pointer + 0x18) is the base of our saved
1240 i
+= push_opcode (&buf
[i
], "48 89 e6"); /* mov %rsp,%rsi */
1241 i
+= push_opcode (&buf
[i
], "48 83 c6 18"); /* add $0x18,%rsi */
1243 /* tpoint address may be 64-bit wide. */
1244 i
+= push_opcode (&buf
[i
], "48 bf"); /* movl <addr>,%rdi */
1245 memcpy (buf
+ i
, &tpoint
, 8);
1247 append_insns (&buildaddr
, i
, buf
);
1249 /* The collector function being in the shared library, may be
1250 >31-bits away off the jump pad. */
1252 i
+= push_opcode (&buf
[i
], "48 b8"); /* mov $collector,%rax */
1253 memcpy (buf
+ i
, &collector
, 8);
1255 i
+= push_opcode (&buf
[i
], "ff d0"); /* callq *%rax */
1256 append_insns (&buildaddr
, i
, buf
);
1258 /* Clear the spin-lock. */
1260 i
+= push_opcode (&buf
[i
], "31 c0"); /* xor %eax,%eax */
1261 i
+= push_opcode (&buf
[i
], "48 a3"); /* mov %rax, lockaddr */
1262 memcpy (buf
+ i
, &lockaddr
, 8);
1264 append_insns (&buildaddr
, i
, buf
);
1266 /* Remove stack that had been used for the collect_t object. */
1268 i
+= push_opcode (&buf
[i
], "48 83 c4 18"); /* add $0x18,%rsp */
1269 append_insns (&buildaddr
, i
, buf
);
1271 /* Restore register state. */
1273 buf
[i
++] = 0x48; /* add $0x8,%rsp */
1277 buf
[i
++] = 0x9d; /* popfq */
1278 buf
[i
++] = 0x41; buf
[i
++] = 0x58; /* pop %r8 */
1279 buf
[i
++] = 0x41; buf
[i
++] = 0x59; /* pop %r9 */
1280 buf
[i
++] = 0x41; buf
[i
++] = 0x5a; /* pop %r10 */
1281 buf
[i
++] = 0x41; buf
[i
++] = 0x5b; /* pop %r11 */
1282 buf
[i
++] = 0x41; buf
[i
++] = 0x5c; /* pop %r12 */
1283 buf
[i
++] = 0x41; buf
[i
++] = 0x5d; /* pop %r13 */
1284 buf
[i
++] = 0x41; buf
[i
++] = 0x5e; /* pop %r14 */
1285 buf
[i
++] = 0x41; buf
[i
++] = 0x5f; /* pop %r15 */
1286 buf
[i
++] = 0x58; /* pop %rax */
1287 buf
[i
++] = 0x5b; /* pop %rbx */
1288 buf
[i
++] = 0x59; /* pop %rcx */
1289 buf
[i
++] = 0x5a; /* pop %rdx */
1290 buf
[i
++] = 0x5e; /* pop %rsi */
1291 buf
[i
++] = 0x5f; /* pop %rdi */
1292 buf
[i
++] = 0x5d; /* pop %rbp */
1293 buf
[i
++] = 0x5c; /* pop %rsp */
1294 append_insns (&buildaddr
, i
, buf
);
1296 /* Now, adjust the original instruction to execute in the jump
1298 *adjusted_insn_addr
= buildaddr
;
1299 relocate_instruction (&buildaddr
, tpaddr
);
1300 *adjusted_insn_addr_end
= buildaddr
;
1302 /* Finally, write a jump back to the program. */
1304 loffset
= (tpaddr
+ orig_size
) - (buildaddr
+ sizeof (jump_insn
));
1305 if (loffset
> INT_MAX
|| loffset
< INT_MIN
)
1308 "E.Jump back from jump pad too far from tracepoint "
1309 "(offset 0x%" PRIx64
" > int32).", loffset
);
1313 offset
= (int) loffset
;
1314 memcpy (buf
, jump_insn
, sizeof (jump_insn
));
1315 memcpy (buf
+ 1, &offset
, 4);
1316 append_insns (&buildaddr
, sizeof (jump_insn
), buf
);
1318 /* The jump pad is now built. Wire in a jump to our jump pad. This
1319 is always done last (by our caller actually), so that we can
1320 install fast tracepoints with threads running. This relies on
1321 the agent's atomic write support. */
1322 loffset
= *jump_entry
- (tpaddr
+ sizeof (jump_insn
));
1323 if (loffset
> INT_MAX
|| loffset
< INT_MIN
)
1326 "E.Jump pad too far from tracepoint "
1327 "(offset 0x%" PRIx64
" > int32).", loffset
);
1331 offset
= (int) loffset
;
1333 memcpy (buf
, jump_insn
, sizeof (jump_insn
));
1334 memcpy (buf
+ 1, &offset
, 4);
1335 memcpy (jjump_pad_insn
, buf
, sizeof (jump_insn
));
1336 *jjump_pad_insn_size
= sizeof (jump_insn
);
1338 /* Return the end address of our pad. */
1339 *jump_entry
= buildaddr
;
1344 #endif /* __x86_64__ */
1346 /* Build a jump pad that saves registers and calls a collection
1347 function. Writes a jump instruction to the jump pad to
1348 JJUMPAD_INSN. The caller is responsible to write it in at the
1349 tracepoint address. */
1352 i386_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint
, CORE_ADDR tpaddr
,
1353 CORE_ADDR collector
,
1356 CORE_ADDR
*jump_entry
,
1357 CORE_ADDR
*trampoline
,
1358 ULONGEST
*trampoline_size
,
1359 unsigned char *jjump_pad_insn
,
1360 ULONGEST
*jjump_pad_insn_size
,
1361 CORE_ADDR
*adjusted_insn_addr
,
1362 CORE_ADDR
*adjusted_insn_addr_end
,
1365 unsigned char buf
[0x100];
1367 CORE_ADDR buildaddr
= *jump_entry
;
1369 /* Build the jump pad. */
1371 /* First, do tracepoint data collection. Save registers. */
1373 buf
[i
++] = 0x60; /* pushad */
1374 buf
[i
++] = 0x68; /* push tpaddr aka $pc */
1375 *((int *)(buf
+ i
)) = (int) tpaddr
;
1377 buf
[i
++] = 0x9c; /* pushf */
1378 buf
[i
++] = 0x1e; /* push %ds */
1379 buf
[i
++] = 0x06; /* push %es */
1380 buf
[i
++] = 0x0f; /* push %fs */
1382 buf
[i
++] = 0x0f; /* push %gs */
1384 buf
[i
++] = 0x16; /* push %ss */
1385 buf
[i
++] = 0x0e; /* push %cs */
1386 append_insns (&buildaddr
, i
, buf
);
1388 /* Stack space for the collecting_t object. */
1390 i
+= push_opcode (&buf
[i
], "83 ec 08"); /* sub $0x8,%esp */
1392 /* Build the object. */
1393 i
+= push_opcode (&buf
[i
], "b8"); /* mov <tpoint>,%eax */
1394 memcpy (buf
+ i
, &tpoint
, 4);
1396 i
+= push_opcode (&buf
[i
], "89 04 24"); /* mov %eax,(%esp) */
1398 i
+= push_opcode (&buf
[i
], "65 a1 00 00 00 00"); /* mov %gs:0x0,%eax */
1399 i
+= push_opcode (&buf
[i
], "89 44 24 04"); /* mov %eax,0x4(%esp) */
1400 append_insns (&buildaddr
, i
, buf
);
1402 /* spin-lock. Note this is using cmpxchg, which leaves i386 behind.
1403 If we cared for it, this could be using xchg alternatively. */
1406 i
+= push_opcode (&buf
[i
], "31 c0"); /* xor %eax,%eax */
1407 i
+= push_opcode (&buf
[i
], "f0 0f b1 25"); /* lock cmpxchg
1409 memcpy (&buf
[i
], (void *) &lockaddr
, 4);
1411 i
+= push_opcode (&buf
[i
], "85 c0"); /* test %eax,%eax */
1412 i
+= push_opcode (&buf
[i
], "75 f2"); /* jne <again> */
1413 append_insns (&buildaddr
, i
, buf
);
1416 /* Set up arguments to the gdb_collect call. */
1418 i
+= push_opcode (&buf
[i
], "89 e0"); /* mov %esp,%eax */
1419 i
+= push_opcode (&buf
[i
], "83 c0 08"); /* add $0x08,%eax */
1420 i
+= push_opcode (&buf
[i
], "89 44 24 fc"); /* mov %eax,-0x4(%esp) */
1421 append_insns (&buildaddr
, i
, buf
);
1424 i
+= push_opcode (&buf
[i
], "83 ec 08"); /* sub $0x8,%esp */
1425 append_insns (&buildaddr
, i
, buf
);
1428 i
+= push_opcode (&buf
[i
], "c7 04 24"); /* movl <addr>,(%esp) */
1429 memcpy (&buf
[i
], (void *) &tpoint
, 4);
1431 append_insns (&buildaddr
, i
, buf
);
1433 buf
[0] = 0xe8; /* call <reladdr> */
1434 offset
= collector
- (buildaddr
+ sizeof (jump_insn
));
1435 memcpy (buf
+ 1, &offset
, 4);
1436 append_insns (&buildaddr
, 5, buf
);
1437 /* Clean up after the call. */
1438 buf
[0] = 0x83; /* add $0x8,%esp */
1441 append_insns (&buildaddr
, 3, buf
);
1444 /* Clear the spin-lock. This would need the LOCK prefix on older
1447 i
+= push_opcode (&buf
[i
], "31 c0"); /* xor %eax,%eax */
1448 i
+= push_opcode (&buf
[i
], "a3"); /* mov %eax, lockaddr */
1449 memcpy (buf
+ i
, &lockaddr
, 4);
1451 append_insns (&buildaddr
, i
, buf
);
1454 /* Remove stack that had been used for the collect_t object. */
1456 i
+= push_opcode (&buf
[i
], "83 c4 08"); /* add $0x08,%esp */
1457 append_insns (&buildaddr
, i
, buf
);
1460 buf
[i
++] = 0x83; /* add $0x4,%esp (no pop of %cs, assume unchanged) */
1463 buf
[i
++] = 0x17; /* pop %ss */
1464 buf
[i
++] = 0x0f; /* pop %gs */
1466 buf
[i
++] = 0x0f; /* pop %fs */
1468 buf
[i
++] = 0x07; /* pop %es */
1469 buf
[i
++] = 0x1f; /* pop %ds */
1470 buf
[i
++] = 0x9d; /* popf */
1471 buf
[i
++] = 0x83; /* add $0x4,%esp (pop of tpaddr aka $pc) */
1474 buf
[i
++] = 0x61; /* popad */
1475 append_insns (&buildaddr
, i
, buf
);
1477 /* Now, adjust the original instruction to execute in the jump
1479 *adjusted_insn_addr
= buildaddr
;
1480 relocate_instruction (&buildaddr
, tpaddr
);
1481 *adjusted_insn_addr_end
= buildaddr
;
1483 /* Write the jump back to the program. */
1484 offset
= (tpaddr
+ orig_size
) - (buildaddr
+ sizeof (jump_insn
));
1485 memcpy (buf
, jump_insn
, sizeof (jump_insn
));
1486 memcpy (buf
+ 1, &offset
, 4);
1487 append_insns (&buildaddr
, sizeof (jump_insn
), buf
);
1489 /* The jump pad is now built. Wire in a jump to our jump pad. This
1490 is always done last (by our caller actually), so that we can
1491 install fast tracepoints with threads running. This relies on
1492 the agent's atomic write support. */
1495 /* Create a trampoline. */
1496 *trampoline_size
= sizeof (jump_insn
);
1497 if (!claim_trampoline_space (*trampoline_size
, trampoline
))
1499 /* No trampoline space available. */
1501 "E.Cannot allocate trampoline space needed for fast "
1502 "tracepoints on 4-byte instructions.");
1506 offset
= *jump_entry
- (*trampoline
+ sizeof (jump_insn
));
1507 memcpy (buf
, jump_insn
, sizeof (jump_insn
));
1508 memcpy (buf
+ 1, &offset
, 4);
1509 target_write_memory (*trampoline
, buf
, sizeof (jump_insn
));
1511 /* Use a 16-bit relative jump instruction to jump to the trampoline. */
1512 offset
= (*trampoline
- (tpaddr
+ sizeof (small_jump_insn
))) & 0xffff;
1513 memcpy (buf
, small_jump_insn
, sizeof (small_jump_insn
));
1514 memcpy (buf
+ 2, &offset
, 2);
1515 memcpy (jjump_pad_insn
, buf
, sizeof (small_jump_insn
));
1516 *jjump_pad_insn_size
= sizeof (small_jump_insn
);
1520 /* Else use a 32-bit relative jump instruction. */
1521 offset
= *jump_entry
- (tpaddr
+ sizeof (jump_insn
));
1522 memcpy (buf
, jump_insn
, sizeof (jump_insn
));
1523 memcpy (buf
+ 1, &offset
, 4);
1524 memcpy (jjump_pad_insn
, buf
, sizeof (jump_insn
));
1525 *jjump_pad_insn_size
= sizeof (jump_insn
);
1528 /* Return the end address of our pad. */
1529 *jump_entry
= buildaddr
;
1535 x86_target::supports_fast_tracepoints ()
1541 x86_target::install_fast_tracepoint_jump_pad (CORE_ADDR tpoint
,
1543 CORE_ADDR collector
,
1546 CORE_ADDR
*jump_entry
,
1547 CORE_ADDR
*trampoline
,
1548 ULONGEST
*trampoline_size
,
1549 unsigned char *jjump_pad_insn
,
1550 ULONGEST
*jjump_pad_insn_size
,
1551 CORE_ADDR
*adjusted_insn_addr
,
1552 CORE_ADDR
*adjusted_insn_addr_end
,
1556 if (is_64bit_tdesc ())
1557 return amd64_install_fast_tracepoint_jump_pad (tpoint
, tpaddr
,
1558 collector
, lockaddr
,
1559 orig_size
, jump_entry
,
1560 trampoline
, trampoline_size
,
1562 jjump_pad_insn_size
,
1564 adjusted_insn_addr_end
,
1568 return i386_install_fast_tracepoint_jump_pad (tpoint
, tpaddr
,
1569 collector
, lockaddr
,
1570 orig_size
, jump_entry
,
1571 trampoline
, trampoline_size
,
1573 jjump_pad_insn_size
,
1575 adjusted_insn_addr_end
,
1579 /* Return the minimum instruction length for fast tracepoints on x86/x86-64
1583 x86_target::get_min_fast_tracepoint_insn_len ()
1585 static int warned_about_fast_tracepoints
= 0;
1588 /* On x86-64, 5-byte jump instructions with a 4-byte offset are always
1589 used for fast tracepoints. */
1590 if (is_64bit_tdesc ())
1594 if (agent_loaded_p ())
1596 char errbuf
[IPA_BUFSIZ
];
1600 /* On x86, if trampolines are available, then 4-byte jump instructions
1601 with a 2-byte offset may be used, otherwise 5-byte jump instructions
1602 with a 4-byte offset are used instead. */
1603 if (have_fast_tracepoint_trampoline_buffer (errbuf
))
1607 /* GDB has no channel to explain to user why a shorter fast
1608 tracepoint is not possible, but at least make GDBserver
1609 mention that something has gone awry. */
1610 if (!warned_about_fast_tracepoints
)
1612 warning ("4-byte fast tracepoints not available; %s", errbuf
);
1613 warned_about_fast_tracepoints
= 1;
1620 /* Indicate that the minimum length is currently unknown since the IPA
1621 has not loaded yet. */
1627 add_insns (unsigned char *start
, int len
)
1629 CORE_ADDR buildaddr
= current_insn_ptr
;
1631 threads_debug_printf ("Adding %d bytes of insn at %s",
1632 len
, paddress (buildaddr
));
1634 append_insns (&buildaddr
, len
, start
);
1635 current_insn_ptr
= buildaddr
;
1638 /* Our general strategy for emitting code is to avoid specifying raw
1639 bytes whenever possible, and instead copy a block of inline asm
1640 that is embedded in the function. This is a little messy, because
1641 we need to keep the compiler from discarding what looks like dead
1642 code, plus suppress various warnings. */
1644 #define EMIT_ASM(NAME, INSNS) \
1647 extern unsigned char start_ ## NAME, end_ ## NAME; \
1648 add_insns (&start_ ## NAME, &end_ ## NAME - &start_ ## NAME); \
1649 __asm__ ("jmp end_" #NAME "\n" \
1650 "\t" "start_" #NAME ":" \
1652 "\t" "end_" #NAME ":"); \
1657 #define EMIT_ASM32(NAME,INSNS) \
1660 extern unsigned char start_ ## NAME, end_ ## NAME; \
1661 add_insns (&start_ ## NAME, &end_ ## NAME - &start_ ## NAME); \
1662 __asm__ (".code32\n" \
1663 "\t" "jmp end_" #NAME "\n" \
1664 "\t" "start_" #NAME ":\n" \
1666 "\t" "end_" #NAME ":\n" \
1672 #define EMIT_ASM32(NAME,INSNS) EMIT_ASM(NAME,INSNS)
1679 amd64_emit_prologue (void)
1681 EMIT_ASM (amd64_prologue
,
1683 "movq %rsp,%rbp\n\t"
1684 "sub $0x20,%rsp\n\t"
1685 "movq %rdi,-8(%rbp)\n\t"
1686 "movq %rsi,-16(%rbp)");
1691 amd64_emit_epilogue (void)
1693 EMIT_ASM (amd64_epilogue
,
1694 "movq -16(%rbp),%rdi\n\t"
1695 "movq %rax,(%rdi)\n\t"
1702 amd64_emit_add (void)
1704 EMIT_ASM (amd64_add
,
1705 "add (%rsp),%rax\n\t"
1706 "lea 0x8(%rsp),%rsp");
1710 amd64_emit_sub (void)
1712 EMIT_ASM (amd64_sub
,
1713 "sub %rax,(%rsp)\n\t"
1718 amd64_emit_mul (void)
1724 amd64_emit_lsh (void)
1730 amd64_emit_rsh_signed (void)
1736 amd64_emit_rsh_unsigned (void)
1742 amd64_emit_ext (int arg
)
1747 EMIT_ASM (amd64_ext_8
,
1753 EMIT_ASM (amd64_ext_16
,
1758 EMIT_ASM (amd64_ext_32
,
1767 amd64_emit_log_not (void)
1769 EMIT_ASM (amd64_log_not
,
1770 "test %rax,%rax\n\t"
1776 amd64_emit_bit_and (void)
1778 EMIT_ASM (amd64_and
,
1779 "and (%rsp),%rax\n\t"
1780 "lea 0x8(%rsp),%rsp");
1784 amd64_emit_bit_or (void)
1787 "or (%rsp),%rax\n\t"
1788 "lea 0x8(%rsp),%rsp");
1792 amd64_emit_bit_xor (void)
1794 EMIT_ASM (amd64_xor
,
1795 "xor (%rsp),%rax\n\t"
1796 "lea 0x8(%rsp),%rsp");
1800 amd64_emit_bit_not (void)
1802 EMIT_ASM (amd64_bit_not
,
1803 "xorq $0xffffffffffffffff,%rax");
1807 amd64_emit_equal (void)
1809 EMIT_ASM (amd64_equal
,
1810 "cmp %rax,(%rsp)\n\t"
1811 "je .Lamd64_equal_true\n\t"
1813 "jmp .Lamd64_equal_end\n\t"
1814 ".Lamd64_equal_true:\n\t"
1816 ".Lamd64_equal_end:\n\t"
1817 "lea 0x8(%rsp),%rsp");
1821 amd64_emit_less_signed (void)
1823 EMIT_ASM (amd64_less_signed
,
1824 "cmp %rax,(%rsp)\n\t"
1825 "jl .Lamd64_less_signed_true\n\t"
1827 "jmp .Lamd64_less_signed_end\n\t"
1828 ".Lamd64_less_signed_true:\n\t"
1830 ".Lamd64_less_signed_end:\n\t"
1831 "lea 0x8(%rsp),%rsp");
1835 amd64_emit_less_unsigned (void)
1837 EMIT_ASM (amd64_less_unsigned
,
1838 "cmp %rax,(%rsp)\n\t"
1839 "jb .Lamd64_less_unsigned_true\n\t"
1841 "jmp .Lamd64_less_unsigned_end\n\t"
1842 ".Lamd64_less_unsigned_true:\n\t"
1844 ".Lamd64_less_unsigned_end:\n\t"
1845 "lea 0x8(%rsp),%rsp");
1849 amd64_emit_ref (int size
)
1854 EMIT_ASM (amd64_ref1
,
1858 EMIT_ASM (amd64_ref2
,
1862 EMIT_ASM (amd64_ref4
,
1863 "movl (%rax),%eax");
1866 EMIT_ASM (amd64_ref8
,
1867 "movq (%rax),%rax");
1873 amd64_emit_if_goto (int *offset_p
, int *size_p
)
1875 EMIT_ASM (amd64_if_goto
,
1879 ".byte 0x0f, 0x85, 0x0, 0x0, 0x0, 0x0");
1887 amd64_emit_goto (int *offset_p
, int *size_p
)
1889 EMIT_ASM (amd64_goto
,
1890 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0");
1898 amd64_write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
1900 int diff
= (to
- (from
+ size
));
1901 unsigned char buf
[sizeof (int)];
1909 memcpy (buf
, &diff
, sizeof (int));
1910 target_write_memory (from
, buf
, sizeof (int));
1914 amd64_emit_const (LONGEST num
)
1916 unsigned char buf
[16];
1918 CORE_ADDR buildaddr
= current_insn_ptr
;
1921 buf
[i
++] = 0x48; buf
[i
++] = 0xb8; /* mov $<n>,%rax */
1922 memcpy (&buf
[i
], &num
, sizeof (num
));
1924 append_insns (&buildaddr
, i
, buf
);
1925 current_insn_ptr
= buildaddr
;
1929 amd64_emit_call (CORE_ADDR fn
)
1931 unsigned char buf
[16];
1933 CORE_ADDR buildaddr
;
1936 /* The destination function being in the shared library, may be
1937 >31-bits away off the compiled code pad. */
1939 buildaddr
= current_insn_ptr
;
1941 offset64
= fn
- (buildaddr
+ 1 /* call op */ + 4 /* 32-bit offset */);
1945 if (offset64
> INT_MAX
|| offset64
< INT_MIN
)
1947 /* Offset is too large for a call. Use callq, but that requires
1948 a register, so avoid it if possible. Use r10, since it is
1949 call-clobbered, we don't have to push/pop it. */
1950 buf
[i
++] = 0x48; /* mov $fn,%r10 */
1952 memcpy (buf
+ i
, &fn
, 8);
1954 buf
[i
++] = 0xff; /* callq *%r10 */
1959 int offset32
= offset64
; /* we know we can't overflow here. */
1961 buf
[i
++] = 0xe8; /* call <reladdr> */
1962 memcpy (buf
+ i
, &offset32
, 4);
1966 append_insns (&buildaddr
, i
, buf
);
1967 current_insn_ptr
= buildaddr
;
1971 amd64_emit_reg (int reg
)
1973 unsigned char buf
[16];
1975 CORE_ADDR buildaddr
;
1977 /* Assume raw_regs is still in %rdi. */
1978 buildaddr
= current_insn_ptr
;
1980 buf
[i
++] = 0xbe; /* mov $<n>,%esi */
1981 memcpy (&buf
[i
], ®
, sizeof (reg
));
1983 append_insns (&buildaddr
, i
, buf
);
1984 current_insn_ptr
= buildaddr
;
1985 amd64_emit_call (get_raw_reg_func_addr ());
1989 amd64_emit_pop (void)
1991 EMIT_ASM (amd64_pop
,
1996 amd64_emit_stack_flush (void)
1998 EMIT_ASM (amd64_stack_flush
,
2003 amd64_emit_zero_ext (int arg
)
2008 EMIT_ASM (amd64_zero_ext_8
,
2012 EMIT_ASM (amd64_zero_ext_16
,
2013 "and $0xffff,%rax");
2016 EMIT_ASM (amd64_zero_ext_32
,
2017 "mov $0xffffffff,%rcx\n\t"
2026 amd64_emit_swap (void)
2028 EMIT_ASM (amd64_swap
,
2035 amd64_emit_stack_adjust (int n
)
2037 unsigned char buf
[16];
2039 CORE_ADDR buildaddr
= current_insn_ptr
;
2042 buf
[i
++] = 0x48; /* lea $<n>(%rsp),%rsp */
2046 /* This only handles adjustments up to 16, but we don't expect any more. */
2048 append_insns (&buildaddr
, i
, buf
);
2049 current_insn_ptr
= buildaddr
;
2052 /* FN's prototype is `LONGEST(*fn)(int)'. */
2055 amd64_emit_int_call_1 (CORE_ADDR fn
, int arg1
)
2057 unsigned char buf
[16];
2059 CORE_ADDR buildaddr
;
2061 buildaddr
= current_insn_ptr
;
2063 buf
[i
++] = 0xbf; /* movl $<n>,%edi */
2064 memcpy (&buf
[i
], &arg1
, sizeof (arg1
));
2066 append_insns (&buildaddr
, i
, buf
);
2067 current_insn_ptr
= buildaddr
;
2068 amd64_emit_call (fn
);
2071 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
2074 amd64_emit_void_call_2 (CORE_ADDR fn
, int arg1
)
2076 unsigned char buf
[16];
2078 CORE_ADDR buildaddr
;
2080 buildaddr
= current_insn_ptr
;
2082 buf
[i
++] = 0xbf; /* movl $<n>,%edi */
2083 memcpy (&buf
[i
], &arg1
, sizeof (arg1
));
2085 append_insns (&buildaddr
, i
, buf
);
2086 current_insn_ptr
= buildaddr
;
2087 EMIT_ASM (amd64_void_call_2_a
,
2088 /* Save away a copy of the stack top. */
2090 /* Also pass top as the second argument. */
2092 amd64_emit_call (fn
);
2093 EMIT_ASM (amd64_void_call_2_b
,
2094 /* Restore the stack top, %rax may have been trashed. */
2099 amd64_emit_eq_goto (int *offset_p
, int *size_p
)
2102 "cmp %rax,(%rsp)\n\t"
2103 "jne .Lamd64_eq_fallthru\n\t"
2104 "lea 0x8(%rsp),%rsp\n\t"
2106 /* jmp, but don't trust the assembler to choose the right jump */
2107 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2108 ".Lamd64_eq_fallthru:\n\t"
2109 "lea 0x8(%rsp),%rsp\n\t"
2119 amd64_emit_ne_goto (int *offset_p
, int *size_p
)
2122 "cmp %rax,(%rsp)\n\t"
2123 "je .Lamd64_ne_fallthru\n\t"
2124 "lea 0x8(%rsp),%rsp\n\t"
2126 /* jmp, but don't trust the assembler to choose the right jump */
2127 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2128 ".Lamd64_ne_fallthru:\n\t"
2129 "lea 0x8(%rsp),%rsp\n\t"
2139 amd64_emit_lt_goto (int *offset_p
, int *size_p
)
2142 "cmp %rax,(%rsp)\n\t"
2143 "jnl .Lamd64_lt_fallthru\n\t"
2144 "lea 0x8(%rsp),%rsp\n\t"
2146 /* jmp, but don't trust the assembler to choose the right jump */
2147 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2148 ".Lamd64_lt_fallthru:\n\t"
2149 "lea 0x8(%rsp),%rsp\n\t"
2159 amd64_emit_le_goto (int *offset_p
, int *size_p
)
2162 "cmp %rax,(%rsp)\n\t"
2163 "jnle .Lamd64_le_fallthru\n\t"
2164 "lea 0x8(%rsp),%rsp\n\t"
2166 /* jmp, but don't trust the assembler to choose the right jump */
2167 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2168 ".Lamd64_le_fallthru:\n\t"
2169 "lea 0x8(%rsp),%rsp\n\t"
2179 amd64_emit_gt_goto (int *offset_p
, int *size_p
)
2182 "cmp %rax,(%rsp)\n\t"
2183 "jng .Lamd64_gt_fallthru\n\t"
2184 "lea 0x8(%rsp),%rsp\n\t"
2186 /* jmp, but don't trust the assembler to choose the right jump */
2187 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2188 ".Lamd64_gt_fallthru:\n\t"
2189 "lea 0x8(%rsp),%rsp\n\t"
2199 amd64_emit_ge_goto (int *offset_p
, int *size_p
)
2202 "cmp %rax,(%rsp)\n\t"
2203 "jnge .Lamd64_ge_fallthru\n\t"
2204 ".Lamd64_ge_jump:\n\t"
2205 "lea 0x8(%rsp),%rsp\n\t"
2207 /* jmp, but don't trust the assembler to choose the right jump */
2208 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2209 ".Lamd64_ge_fallthru:\n\t"
2210 "lea 0x8(%rsp),%rsp\n\t"
2219 static emit_ops amd64_emit_ops
=
2221 amd64_emit_prologue
,
2222 amd64_emit_epilogue
,
2227 amd64_emit_rsh_signed
,
2228 amd64_emit_rsh_unsigned
,
2236 amd64_emit_less_signed
,
2237 amd64_emit_less_unsigned
,
2241 amd64_write_goto_address
,
2246 amd64_emit_stack_flush
,
2247 amd64_emit_zero_ext
,
2249 amd64_emit_stack_adjust
,
2250 amd64_emit_int_call_1
,
2251 amd64_emit_void_call_2
,
2260 #endif /* __x86_64__ */
2263 i386_emit_prologue (void)
2265 EMIT_ASM32 (i386_prologue
,
2269 /* At this point, the raw regs base address is at 8(%ebp), and the
2270 value pointer is at 12(%ebp). */
2274 i386_emit_epilogue (void)
2276 EMIT_ASM32 (i386_epilogue
,
2277 "mov 12(%ebp),%ecx\n\t"
2278 "mov %eax,(%ecx)\n\t"
2279 "mov %ebx,0x4(%ecx)\n\t"
2287 i386_emit_add (void)
2289 EMIT_ASM32 (i386_add
,
2290 "add (%esp),%eax\n\t"
2291 "adc 0x4(%esp),%ebx\n\t"
2292 "lea 0x8(%esp),%esp");
2296 i386_emit_sub (void)
2298 EMIT_ASM32 (i386_sub
,
2299 "subl %eax,(%esp)\n\t"
2300 "sbbl %ebx,4(%esp)\n\t"
2306 i386_emit_mul (void)
2312 i386_emit_lsh (void)
2318 i386_emit_rsh_signed (void)
2324 i386_emit_rsh_unsigned (void)
2330 i386_emit_ext (int arg
)
2335 EMIT_ASM32 (i386_ext_8
,
2338 "movl %eax,%ebx\n\t"
2342 EMIT_ASM32 (i386_ext_16
,
2344 "movl %eax,%ebx\n\t"
2348 EMIT_ASM32 (i386_ext_32
,
2349 "movl %eax,%ebx\n\t"
2358 i386_emit_log_not (void)
2360 EMIT_ASM32 (i386_log_not
,
2362 "test %eax,%eax\n\t"
2369 i386_emit_bit_and (void)
2371 EMIT_ASM32 (i386_and
,
2372 "and (%esp),%eax\n\t"
2373 "and 0x4(%esp),%ebx\n\t"
2374 "lea 0x8(%esp),%esp");
2378 i386_emit_bit_or (void)
2380 EMIT_ASM32 (i386_or
,
2381 "or (%esp),%eax\n\t"
2382 "or 0x4(%esp),%ebx\n\t"
2383 "lea 0x8(%esp),%esp");
2387 i386_emit_bit_xor (void)
2389 EMIT_ASM32 (i386_xor
,
2390 "xor (%esp),%eax\n\t"
2391 "xor 0x4(%esp),%ebx\n\t"
2392 "lea 0x8(%esp),%esp");
2396 i386_emit_bit_not (void)
2398 EMIT_ASM32 (i386_bit_not
,
2399 "xor $0xffffffff,%eax\n\t"
2400 "xor $0xffffffff,%ebx\n\t");
2404 i386_emit_equal (void)
2406 EMIT_ASM32 (i386_equal
,
2407 "cmpl %ebx,4(%esp)\n\t"
2408 "jne .Li386_equal_false\n\t"
2409 "cmpl %eax,(%esp)\n\t"
2410 "je .Li386_equal_true\n\t"
2411 ".Li386_equal_false:\n\t"
2413 "jmp .Li386_equal_end\n\t"
2414 ".Li386_equal_true:\n\t"
2416 ".Li386_equal_end:\n\t"
2418 "lea 0x8(%esp),%esp");
2422 i386_emit_less_signed (void)
2424 EMIT_ASM32 (i386_less_signed
,
2425 "cmpl %ebx,4(%esp)\n\t"
2426 "jl .Li386_less_signed_true\n\t"
2427 "jne .Li386_less_signed_false\n\t"
2428 "cmpl %eax,(%esp)\n\t"
2429 "jl .Li386_less_signed_true\n\t"
2430 ".Li386_less_signed_false:\n\t"
2432 "jmp .Li386_less_signed_end\n\t"
2433 ".Li386_less_signed_true:\n\t"
2435 ".Li386_less_signed_end:\n\t"
2437 "lea 0x8(%esp),%esp");
2441 i386_emit_less_unsigned (void)
2443 EMIT_ASM32 (i386_less_unsigned
,
2444 "cmpl %ebx,4(%esp)\n\t"
2445 "jb .Li386_less_unsigned_true\n\t"
2446 "jne .Li386_less_unsigned_false\n\t"
2447 "cmpl %eax,(%esp)\n\t"
2448 "jb .Li386_less_unsigned_true\n\t"
2449 ".Li386_less_unsigned_false:\n\t"
2451 "jmp .Li386_less_unsigned_end\n\t"
2452 ".Li386_less_unsigned_true:\n\t"
2454 ".Li386_less_unsigned_end:\n\t"
2456 "lea 0x8(%esp),%esp");
2460 i386_emit_ref (int size
)
2465 EMIT_ASM32 (i386_ref1
,
2469 EMIT_ASM32 (i386_ref2
,
2473 EMIT_ASM32 (i386_ref4
,
2474 "movl (%eax),%eax");
2477 EMIT_ASM32 (i386_ref8
,
2478 "movl 4(%eax),%ebx\n\t"
2479 "movl (%eax),%eax");
2485 i386_emit_if_goto (int *offset_p
, int *size_p
)
2487 EMIT_ASM32 (i386_if_goto
,
2493 /* Don't trust the assembler to choose the right jump */
2494 ".byte 0x0f, 0x85, 0x0, 0x0, 0x0, 0x0");
2497 *offset_p
= 11; /* be sure that this matches the sequence above */
2503 i386_emit_goto (int *offset_p
, int *size_p
)
2505 EMIT_ASM32 (i386_goto
,
2506 /* Don't trust the assembler to choose the right jump */
2507 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0");
2515 i386_write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
2517 int diff
= (to
- (from
+ size
));
2518 unsigned char buf
[sizeof (int)];
2520 /* We're only doing 4-byte sizes at the moment. */
2527 memcpy (buf
, &diff
, sizeof (int));
2528 target_write_memory (from
, buf
, sizeof (int));
2532 i386_emit_const (LONGEST num
)
2534 unsigned char buf
[16];
2536 CORE_ADDR buildaddr
= current_insn_ptr
;
2539 buf
[i
++] = 0xb8; /* mov $<n>,%eax */
2540 lo
= num
& 0xffffffff;
2541 memcpy (&buf
[i
], &lo
, sizeof (lo
));
2543 hi
= ((num
>> 32) & 0xffffffff);
2546 buf
[i
++] = 0xbb; /* mov $<n>,%ebx */
2547 memcpy (&buf
[i
], &hi
, sizeof (hi
));
2552 buf
[i
++] = 0x31; buf
[i
++] = 0xdb; /* xor %ebx,%ebx */
2554 append_insns (&buildaddr
, i
, buf
);
2555 current_insn_ptr
= buildaddr
;
2559 i386_emit_call (CORE_ADDR fn
)
2561 unsigned char buf
[16];
2563 CORE_ADDR buildaddr
;
2565 buildaddr
= current_insn_ptr
;
2567 buf
[i
++] = 0xe8; /* call <reladdr> */
2568 offset
= ((int) fn
) - (buildaddr
+ 5);
2569 memcpy (buf
+ 1, &offset
, 4);
2570 append_insns (&buildaddr
, 5, buf
);
2571 current_insn_ptr
= buildaddr
;
2575 i386_emit_reg (int reg
)
2577 unsigned char buf
[16];
2579 CORE_ADDR buildaddr
;
2581 EMIT_ASM32 (i386_reg_a
,
2583 buildaddr
= current_insn_ptr
;
2585 buf
[i
++] = 0xb8; /* mov $<n>,%eax */
2586 memcpy (&buf
[i
], ®
, sizeof (reg
));
2588 append_insns (&buildaddr
, i
, buf
);
2589 current_insn_ptr
= buildaddr
;
2590 EMIT_ASM32 (i386_reg_b
,
2591 "mov %eax,4(%esp)\n\t"
2592 "mov 8(%ebp),%eax\n\t"
2594 i386_emit_call (get_raw_reg_func_addr ());
2595 EMIT_ASM32 (i386_reg_c
,
2597 "lea 0x8(%esp),%esp");
2601 i386_emit_pop (void)
2603 EMIT_ASM32 (i386_pop
,
2609 i386_emit_stack_flush (void)
2611 EMIT_ASM32 (i386_stack_flush
,
2617 i386_emit_zero_ext (int arg
)
2622 EMIT_ASM32 (i386_zero_ext_8
,
2623 "and $0xff,%eax\n\t"
2627 EMIT_ASM32 (i386_zero_ext_16
,
2628 "and $0xffff,%eax\n\t"
2632 EMIT_ASM32 (i386_zero_ext_32
,
2641 i386_emit_swap (void)
2643 EMIT_ASM32 (i386_swap
,
2653 i386_emit_stack_adjust (int n
)
2655 unsigned char buf
[16];
2657 CORE_ADDR buildaddr
= current_insn_ptr
;
2660 buf
[i
++] = 0x8d; /* lea $<n>(%esp),%esp */
2664 append_insns (&buildaddr
, i
, buf
);
2665 current_insn_ptr
= buildaddr
;
2668 /* FN's prototype is `LONGEST(*fn)(int)'. */
2671 i386_emit_int_call_1 (CORE_ADDR fn
, int arg1
)
2673 unsigned char buf
[16];
2675 CORE_ADDR buildaddr
;
2677 EMIT_ASM32 (i386_int_call_1_a
,
2678 /* Reserve a bit of stack space. */
2680 /* Put the one argument on the stack. */
2681 buildaddr
= current_insn_ptr
;
2683 buf
[i
++] = 0xc7; /* movl $<arg1>,(%esp) */
2686 memcpy (&buf
[i
], &arg1
, sizeof (arg1
));
2688 append_insns (&buildaddr
, i
, buf
);
2689 current_insn_ptr
= buildaddr
;
2690 i386_emit_call (fn
);
2691 EMIT_ASM32 (i386_int_call_1_c
,
2693 "lea 0x8(%esp),%esp");
2696 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
2699 i386_emit_void_call_2 (CORE_ADDR fn
, int arg1
)
2701 unsigned char buf
[16];
2703 CORE_ADDR buildaddr
;
2705 EMIT_ASM32 (i386_void_call_2_a
,
2706 /* Preserve %eax only; we don't have to worry about %ebx. */
2708 /* Reserve a bit of stack space for arguments. */
2709 "sub $0x10,%esp\n\t"
2710 /* Copy "top" to the second argument position. (Note that
2711 we can't assume function won't scribble on its
2712 arguments, so don't try to restore from this.) */
2713 "mov %eax,4(%esp)\n\t"
2714 "mov %ebx,8(%esp)");
2715 /* Put the first argument on the stack. */
2716 buildaddr
= current_insn_ptr
;
2718 buf
[i
++] = 0xc7; /* movl $<arg1>,(%esp) */
2721 memcpy (&buf
[i
], &arg1
, sizeof (arg1
));
2723 append_insns (&buildaddr
, i
, buf
);
2724 current_insn_ptr
= buildaddr
;
2725 i386_emit_call (fn
);
2726 EMIT_ASM32 (i386_void_call_2_b
,
2727 "lea 0x10(%esp),%esp\n\t"
2728 /* Restore original stack top. */
2734 i386_emit_eq_goto (int *offset_p
, int *size_p
)
2737 /* Check low half first, more likely to be decider */
2738 "cmpl %eax,(%esp)\n\t"
2739 "jne .Leq_fallthru\n\t"
2740 "cmpl %ebx,4(%esp)\n\t"
2741 "jne .Leq_fallthru\n\t"
2742 "lea 0x8(%esp),%esp\n\t"
2745 /* jmp, but don't trust the assembler to choose the right jump */
2746 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2747 ".Leq_fallthru:\n\t"
2748 "lea 0x8(%esp),%esp\n\t"
2759 i386_emit_ne_goto (int *offset_p
, int *size_p
)
2762 /* Check low half first, more likely to be decider */
2763 "cmpl %eax,(%esp)\n\t"
2765 "cmpl %ebx,4(%esp)\n\t"
2766 "je .Lne_fallthru\n\t"
2768 "lea 0x8(%esp),%esp\n\t"
2771 /* jmp, but don't trust the assembler to choose the right jump */
2772 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2773 ".Lne_fallthru:\n\t"
2774 "lea 0x8(%esp),%esp\n\t"
2785 i386_emit_lt_goto (int *offset_p
, int *size_p
)
2788 "cmpl %ebx,4(%esp)\n\t"
2790 "jne .Llt_fallthru\n\t"
2791 "cmpl %eax,(%esp)\n\t"
2792 "jnl .Llt_fallthru\n\t"
2794 "lea 0x8(%esp),%esp\n\t"
2797 /* jmp, but don't trust the assembler to choose the right jump */
2798 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2799 ".Llt_fallthru:\n\t"
2800 "lea 0x8(%esp),%esp\n\t"
2811 i386_emit_le_goto (int *offset_p
, int *size_p
)
2814 "cmpl %ebx,4(%esp)\n\t"
2816 "jne .Lle_fallthru\n\t"
2817 "cmpl %eax,(%esp)\n\t"
2818 "jnle .Lle_fallthru\n\t"
2820 "lea 0x8(%esp),%esp\n\t"
2823 /* jmp, but don't trust the assembler to choose the right jump */
2824 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2825 ".Lle_fallthru:\n\t"
2826 "lea 0x8(%esp),%esp\n\t"
2837 i386_emit_gt_goto (int *offset_p
, int *size_p
)
2840 "cmpl %ebx,4(%esp)\n\t"
2842 "jne .Lgt_fallthru\n\t"
2843 "cmpl %eax,(%esp)\n\t"
2844 "jng .Lgt_fallthru\n\t"
2846 "lea 0x8(%esp),%esp\n\t"
2849 /* jmp, but don't trust the assembler to choose the right jump */
2850 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2851 ".Lgt_fallthru:\n\t"
2852 "lea 0x8(%esp),%esp\n\t"
2863 i386_emit_ge_goto (int *offset_p
, int *size_p
)
2866 "cmpl %ebx,4(%esp)\n\t"
2868 "jne .Lge_fallthru\n\t"
2869 "cmpl %eax,(%esp)\n\t"
2870 "jnge .Lge_fallthru\n\t"
2872 "lea 0x8(%esp),%esp\n\t"
2875 /* jmp, but don't trust the assembler to choose the right jump */
2876 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2877 ".Lge_fallthru:\n\t"
2878 "lea 0x8(%esp),%esp\n\t"
2888 static emit_ops i386_emit_ops
=
2896 i386_emit_rsh_signed
,
2897 i386_emit_rsh_unsigned
,
2905 i386_emit_less_signed
,
2906 i386_emit_less_unsigned
,
2910 i386_write_goto_address
,
2915 i386_emit_stack_flush
,
2918 i386_emit_stack_adjust
,
2919 i386_emit_int_call_1
,
2920 i386_emit_void_call_2
,
2931 x86_target::emit_ops ()
2934 if (is_64bit_tdesc ())
2935 return &amd64_emit_ops
;
2938 return &i386_emit_ops
;
2941 /* Implementation of target ops method "sw_breakpoint_from_kind". */
2944 x86_target::sw_breakpoint_from_kind (int kind
, int *size
)
2946 *size
= x86_breakpoint_len
;
2947 return x86_breakpoint
;
2951 x86_target::low_supports_range_stepping ()
2957 x86_target::get_ipa_tdesc_idx ()
2959 struct regcache
*regcache
= get_thread_regcache (current_thread
, 0);
2960 const struct target_desc
*tdesc
= regcache
->tdesc
;
2963 return amd64_get_ipa_tdesc_idx (tdesc
);
2966 if (tdesc
== tdesc_i386_linux_no_xml
.get ())
2967 return X86_TDESC_SSE
;
2969 return i386_get_ipa_tdesc_idx (tdesc
);
2972 /* The linux target ops object. */
2974 linux_process_target
*the_linux_target
= &the_x86_target
;
2977 initialize_low_arch (void)
2979 /* Initialize the Linux target descriptions. */
2981 tdesc_amd64_linux_no_xml
= allocate_target_description ();
2982 copy_target_description (tdesc_amd64_linux_no_xml
.get (),
2983 amd64_linux_read_description (X86_XSTATE_SSE_MASK
,
2985 tdesc_amd64_linux_no_xml
->xmltarget
= xmltarget_amd64_linux_no_xml
;
2988 tdesc_i386_linux_no_xml
= allocate_target_description ();
2989 copy_target_description (tdesc_i386_linux_no_xml
.get (),
2990 i386_linux_read_description (X86_XSTATE_SSE_MASK
));
2991 tdesc_i386_linux_no_xml
->xmltarget
= xmltarget_i386_linux_no_xml
;
2993 initialize_regsets_info (&x86_regsets_info
);