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