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