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