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