Debian Codegraph @

gdbserver/linux-x86-low.cc

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