sim: profile: implement --profile-file backend
[deliverable/binutils-gdb.git] / gdb / amd64-tdep.c
CommitLineData
e53bef9f 1/* Target-dependent code for AMD64.
ce0eebec 2
4c38e0a4 3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5ae96ec1
MK
4 Free Software Foundation, Inc.
5
6 Contributed by Jiri Smid, SuSE Labs.
53e95fcf
JS
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
53e95fcf
JS
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
53e95fcf
JS
22
23#include "defs.h"
35669430
DE
24#include "opcode/i386.h"
25#include "dis-asm.h"
c4f35dd8
MK
26#include "arch-utils.h"
27#include "block.h"
28#include "dummy-frame.h"
29#include "frame.h"
30#include "frame-base.h"
31#include "frame-unwind.h"
53e95fcf 32#include "inferior.h"
53e95fcf 33#include "gdbcmd.h"
c4f35dd8
MK
34#include "gdbcore.h"
35#include "objfiles.h"
53e95fcf 36#include "regcache.h"
2c261fae 37#include "regset.h"
53e95fcf 38#include "symfile.h"
c4f35dd8 39
82dbc5f7 40#include "gdb_assert.h"
c4f35dd8 41
9c1488cb 42#include "amd64-tdep.h"
c4f35dd8 43#include "i387-tdep.h"
53e95fcf 44
90884b2b 45#include "features/i386/amd64.c"
a055a187 46#include "features/i386/amd64-avx.c"
90884b2b 47
e53bef9f
MK
48/* Note that the AMD64 architecture was previously known as x86-64.
49 The latter is (forever) engraved into the canonical system name as
90f90721 50 returned by config.guess, and used as the name for the AMD64 port
e53bef9f
MK
51 of GNU/Linux. The BSD's have renamed their ports to amd64; they
52 don't like to shout. For GDB we prefer the amd64_-prefix over the
53 x86_64_-prefix since it's so much easier to type. */
54
402ecd56 55/* Register information. */
c4f35dd8 56
6707b003 57static const char *amd64_register_names[] =
de220d0f 58{
6707b003 59 "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp",
c4f35dd8
MK
60
61 /* %r8 is indeed register number 8. */
6707b003
UW
62 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
63 "rip", "eflags", "cs", "ss", "ds", "es", "fs", "gs",
c4f35dd8 64
af233647 65 /* %st0 is register number 24. */
6707b003
UW
66 "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7",
67 "fctrl", "fstat", "ftag", "fiseg", "fioff", "foseg", "fooff", "fop",
c4f35dd8 68
af233647 69 /* %xmm0 is register number 40. */
6707b003
UW
70 "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
71 "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
72 "mxcsr",
0e04a514
ML
73};
74
a055a187
L
75static const char *amd64_ymm_names[] =
76{
77 "ymm0", "ymm1", "ymm2", "ymm3",
78 "ymm4", "ymm5", "ymm6", "ymm7",
79 "ymm8", "ymm9", "ymm10", "ymm11",
80 "ymm12", "ymm13", "ymm14", "ymm15"
81};
82
83static const char *amd64_ymmh_names[] =
84{
85 "ymm0h", "ymm1h", "ymm2h", "ymm3h",
86 "ymm4h", "ymm5h", "ymm6h", "ymm7h",
87 "ymm8h", "ymm9h", "ymm10h", "ymm11h",
88 "ymm12h", "ymm13h", "ymm14h", "ymm15h"
89};
de220d0f 90
ba581dc1
JB
91/* The registers used to pass integer arguments during a function call. */
92static int amd64_dummy_call_integer_regs[] =
93{
94 AMD64_RDI_REGNUM, /* %rdi */
95 AMD64_RSI_REGNUM, /* %rsi */
96 AMD64_RDX_REGNUM, /* %rdx */
97 AMD64_RCX_REGNUM, /* %rcx */
98 8, /* %r8 */
99 9 /* %r9 */
100};
101
c4f35dd8
MK
102/* DWARF Register Number Mapping as defined in the System V psABI,
103 section 3.6. */
53e95fcf 104
e53bef9f 105static int amd64_dwarf_regmap[] =
0e04a514 106{
c4f35dd8 107 /* General Purpose Registers RAX, RDX, RCX, RBX, RSI, RDI. */
90f90721
MK
108 AMD64_RAX_REGNUM, AMD64_RDX_REGNUM,
109 AMD64_RCX_REGNUM, AMD64_RBX_REGNUM,
110 AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
c4f35dd8
MK
111
112 /* Frame Pointer Register RBP. */
90f90721 113 AMD64_RBP_REGNUM,
c4f35dd8
MK
114
115 /* Stack Pointer Register RSP. */
90f90721 116 AMD64_RSP_REGNUM,
c4f35dd8
MK
117
118 /* Extended Integer Registers 8 - 15. */
119 8, 9, 10, 11, 12, 13, 14, 15,
120
59207364 121 /* Return Address RA. Mapped to RIP. */
90f90721 122 AMD64_RIP_REGNUM,
c4f35dd8
MK
123
124 /* SSE Registers 0 - 7. */
90f90721
MK
125 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
126 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
127 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
128 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
c4f35dd8
MK
129
130 /* Extended SSE Registers 8 - 15. */
90f90721
MK
131 AMD64_XMM0_REGNUM + 8, AMD64_XMM0_REGNUM + 9,
132 AMD64_XMM0_REGNUM + 10, AMD64_XMM0_REGNUM + 11,
133 AMD64_XMM0_REGNUM + 12, AMD64_XMM0_REGNUM + 13,
134 AMD64_XMM0_REGNUM + 14, AMD64_XMM0_REGNUM + 15,
c4f35dd8
MK
135
136 /* Floating Point Registers 0-7. */
90f90721
MK
137 AMD64_ST0_REGNUM + 0, AMD64_ST0_REGNUM + 1,
138 AMD64_ST0_REGNUM + 2, AMD64_ST0_REGNUM + 3,
139 AMD64_ST0_REGNUM + 4, AMD64_ST0_REGNUM + 5,
c6f4c129
JB
140 AMD64_ST0_REGNUM + 6, AMD64_ST0_REGNUM + 7,
141
142 /* Control and Status Flags Register. */
143 AMD64_EFLAGS_REGNUM,
144
145 /* Selector Registers. */
146 AMD64_ES_REGNUM,
147 AMD64_CS_REGNUM,
148 AMD64_SS_REGNUM,
149 AMD64_DS_REGNUM,
150 AMD64_FS_REGNUM,
151 AMD64_GS_REGNUM,
152 -1,
153 -1,
154
155 /* Segment Base Address Registers. */
156 -1,
157 -1,
158 -1,
159 -1,
160
161 /* Special Selector Registers. */
162 -1,
163 -1,
164
165 /* Floating Point Control Registers. */
166 AMD64_MXCSR_REGNUM,
167 AMD64_FCTRL_REGNUM,
168 AMD64_FSTAT_REGNUM
c4f35dd8 169};
0e04a514 170
e53bef9f
MK
171static const int amd64_dwarf_regmap_len =
172 (sizeof (amd64_dwarf_regmap) / sizeof (amd64_dwarf_regmap[0]));
0e04a514 173
c4f35dd8
MK
174/* Convert DWARF register number REG to the appropriate register
175 number used by GDB. */
26abbdc4 176
c4f35dd8 177static int
d3f73121 178amd64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
53e95fcf 179{
a055a187
L
180 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
181 int ymm0_regnum = tdep->ymm0_regnum;
c4f35dd8 182 int regnum = -1;
53e95fcf 183
16aff9a6 184 if (reg >= 0 && reg < amd64_dwarf_regmap_len)
e53bef9f 185 regnum = amd64_dwarf_regmap[reg];
53e95fcf 186
c4f35dd8 187 if (regnum == -1)
8a3fe4f8 188 warning (_("Unmapped DWARF Register #%d encountered."), reg);
a055a187
L
189 else if (ymm0_regnum >= 0
190 && i386_xmm_regnum_p (gdbarch, regnum))
191 regnum += ymm0_regnum - I387_XMM0_REGNUM (tdep);
c4f35dd8
MK
192
193 return regnum;
53e95fcf 194}
d532c08f 195
35669430
DE
196/* Map architectural register numbers to gdb register numbers. */
197
198static const int amd64_arch_regmap[16] =
199{
200 AMD64_RAX_REGNUM, /* %rax */
201 AMD64_RCX_REGNUM, /* %rcx */
202 AMD64_RDX_REGNUM, /* %rdx */
203 AMD64_RBX_REGNUM, /* %rbx */
204 AMD64_RSP_REGNUM, /* %rsp */
205 AMD64_RBP_REGNUM, /* %rbp */
206 AMD64_RSI_REGNUM, /* %rsi */
207 AMD64_RDI_REGNUM, /* %rdi */
208 AMD64_R8_REGNUM, /* %r8 */
209 AMD64_R9_REGNUM, /* %r9 */
210 AMD64_R10_REGNUM, /* %r10 */
211 AMD64_R11_REGNUM, /* %r11 */
212 AMD64_R12_REGNUM, /* %r12 */
213 AMD64_R13_REGNUM, /* %r13 */
214 AMD64_R14_REGNUM, /* %r14 */
215 AMD64_R15_REGNUM /* %r15 */
216};
217
218static const int amd64_arch_regmap_len =
219 (sizeof (amd64_arch_regmap) / sizeof (amd64_arch_regmap[0]));
220
221/* Convert architectural register number REG to the appropriate register
222 number used by GDB. */
223
224static int
225amd64_arch_reg_to_regnum (int reg)
226{
227 gdb_assert (reg >= 0 && reg < amd64_arch_regmap_len);
228
229 return amd64_arch_regmap[reg];
230}
231
1ba53b71
L
232/* Register names for byte pseudo-registers. */
233
234static const char *amd64_byte_names[] =
235{
236 "al", "bl", "cl", "dl", "sil", "dil", "bpl", "spl",
fe01d668
L
237 "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l",
238 "ah", "bh", "ch", "dh"
1ba53b71
L
239};
240
fe01d668
L
241/* Number of lower byte registers. */
242#define AMD64_NUM_LOWER_BYTE_REGS 16
243
1ba53b71
L
244/* Register names for word pseudo-registers. */
245
246static const char *amd64_word_names[] =
247{
9cad29ac 248 "ax", "bx", "cx", "dx", "si", "di", "bp", "",
1ba53b71
L
249 "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
250};
251
252/* Register names for dword pseudo-registers. */
253
254static const char *amd64_dword_names[] =
255{
256 "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
257 "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d"
258};
259
a055a187
L
260/* Return the name of register REGNUM, or the empty string if it is
261 an anonymous register. */
262
263static const char *
264amd64_register_name (struct gdbarch *gdbarch, int regnum)
265{
266 /* Hide the upper YMM registers. */
267 if (i386_ymmh_regnum_p (gdbarch, regnum))
268 return "";
269
270 return tdesc_register_name (gdbarch, regnum);
271}
272
1ba53b71
L
273/* Return the name of register REGNUM. */
274
275static const char *
276amd64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
277{
278 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
279 if (i386_byte_regnum_p (gdbarch, regnum))
280 return amd64_byte_names[regnum - tdep->al_regnum];
a055a187
L
281 else if (i386_ymm_regnum_p (gdbarch, regnum))
282 return amd64_ymm_names[regnum - tdep->ymm0_regnum];
1ba53b71
L
283 else if (i386_word_regnum_p (gdbarch, regnum))
284 return amd64_word_names[regnum - tdep->ax_regnum];
285 else if (i386_dword_regnum_p (gdbarch, regnum))
286 return amd64_dword_names[regnum - tdep->eax_regnum];
287 else
288 return i386_pseudo_register_name (gdbarch, regnum);
289}
290
291static void
292amd64_pseudo_register_read (struct gdbarch *gdbarch,
293 struct regcache *regcache,
294 int regnum, gdb_byte *buf)
295{
296 gdb_byte raw_buf[MAX_REGISTER_SIZE];
297 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
298
299 if (i386_byte_regnum_p (gdbarch, regnum))
300 {
301 int gpnum = regnum - tdep->al_regnum;
302
303 /* Extract (always little endian). */
fe01d668
L
304 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
305 {
306 /* Special handling for AH, BH, CH, DH. */
307 regcache_raw_read (regcache,
308 gpnum - AMD64_NUM_LOWER_BYTE_REGS, raw_buf);
309 memcpy (buf, raw_buf + 1, 1);
310 }
311 else
312 {
313 regcache_raw_read (regcache, gpnum, raw_buf);
314 memcpy (buf, raw_buf, 1);
315 }
1ba53b71
L
316 }
317 else if (i386_dword_regnum_p (gdbarch, regnum))
318 {
319 int gpnum = regnum - tdep->eax_regnum;
320 /* Extract (always little endian). */
321 regcache_raw_read (regcache, gpnum, raw_buf);
322 memcpy (buf, raw_buf, 4);
323 }
324 else
325 i386_pseudo_register_read (gdbarch, regcache, regnum, buf);
326}
327
328static void
329amd64_pseudo_register_write (struct gdbarch *gdbarch,
330 struct regcache *regcache,
331 int regnum, const gdb_byte *buf)
332{
333 gdb_byte raw_buf[MAX_REGISTER_SIZE];
334 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
335
336 if (i386_byte_regnum_p (gdbarch, regnum))
337 {
338 int gpnum = regnum - tdep->al_regnum;
339
fe01d668
L
340 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
341 {
342 /* Read ... AH, BH, CH, DH. */
343 regcache_raw_read (regcache,
344 gpnum - AMD64_NUM_LOWER_BYTE_REGS, raw_buf);
345 /* ... Modify ... (always little endian). */
346 memcpy (raw_buf + 1, buf, 1);
347 /* ... Write. */
348 regcache_raw_write (regcache,
349 gpnum - AMD64_NUM_LOWER_BYTE_REGS, raw_buf);
350 }
351 else
352 {
353 /* Read ... */
354 regcache_raw_read (regcache, gpnum, raw_buf);
355 /* ... Modify ... (always little endian). */
356 memcpy (raw_buf, buf, 1);
357 /* ... Write. */
358 regcache_raw_write (regcache, gpnum, raw_buf);
359 }
1ba53b71
L
360 }
361 else if (i386_dword_regnum_p (gdbarch, regnum))
362 {
363 int gpnum = regnum - tdep->eax_regnum;
364
365 /* Read ... */
366 regcache_raw_read (regcache, gpnum, raw_buf);
367 /* ... Modify ... (always little endian). */
368 memcpy (raw_buf, buf, 4);
369 /* ... Write. */
370 regcache_raw_write (regcache, gpnum, raw_buf);
371 }
372 else
373 i386_pseudo_register_write (gdbarch, regcache, regnum, buf);
374}
375
53e95fcf
JS
376\f
377
efb1c01c
MK
378/* Return the union class of CLASS1 and CLASS2. See the psABI for
379 details. */
380
381static enum amd64_reg_class
382amd64_merge_classes (enum amd64_reg_class class1, enum amd64_reg_class class2)
383{
384 /* Rule (a): If both classes are equal, this is the resulting class. */
385 if (class1 == class2)
386 return class1;
387
388 /* Rule (b): If one of the classes is NO_CLASS, the resulting class
389 is the other class. */
390 if (class1 == AMD64_NO_CLASS)
391 return class2;
392 if (class2 == AMD64_NO_CLASS)
393 return class1;
394
395 /* Rule (c): If one of the classes is MEMORY, the result is MEMORY. */
396 if (class1 == AMD64_MEMORY || class2 == AMD64_MEMORY)
397 return AMD64_MEMORY;
398
399 /* Rule (d): If one of the classes is INTEGER, the result is INTEGER. */
400 if (class1 == AMD64_INTEGER || class2 == AMD64_INTEGER)
401 return AMD64_INTEGER;
402
403 /* Rule (e): If one of the classes is X87, X87UP, COMPLEX_X87 class,
404 MEMORY is used as class. */
405 if (class1 == AMD64_X87 || class1 == AMD64_X87UP
406 || class1 == AMD64_COMPLEX_X87 || class2 == AMD64_X87
407 || class2 == AMD64_X87UP || class2 == AMD64_COMPLEX_X87)
408 return AMD64_MEMORY;
409
410 /* Rule (f): Otherwise class SSE is used. */
411 return AMD64_SSE;
412}
413
79b1ab3d
MK
414/* Return non-zero if TYPE is a non-POD structure or union type. */
415
416static int
417amd64_non_pod_p (struct type *type)
418{
419 /* ??? A class with a base class certainly isn't POD, but does this
420 catch all non-POD structure types? */
421 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_N_BASECLASSES (type) > 0)
422 return 1;
423
424 return 0;
425}
426
efb1c01c
MK
427/* Classify TYPE according to the rules for aggregate (structures and
428 arrays) and union types, and store the result in CLASS. */
c4f35dd8
MK
429
430static void
efb1c01c 431amd64_classify_aggregate (struct type *type, enum amd64_reg_class class[2])
53e95fcf
JS
432{
433 int len = TYPE_LENGTH (type);
434
efb1c01c
MK
435 /* 1. If the size of an object is larger than two eightbytes, or in
436 C++, is a non-POD structure or union type, or contains
437 unaligned fields, it has class memory. */
79b1ab3d 438 if (len > 16 || amd64_non_pod_p (type))
53e95fcf 439 {
efb1c01c
MK
440 class[0] = class[1] = AMD64_MEMORY;
441 return;
53e95fcf 442 }
efb1c01c
MK
443
444 /* 2. Both eightbytes get initialized to class NO_CLASS. */
445 class[0] = class[1] = AMD64_NO_CLASS;
446
447 /* 3. Each field of an object is classified recursively so that
448 always two fields are considered. The resulting class is
449 calculated according to the classes of the fields in the
450 eightbyte: */
451
452 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
8ffd9b1b 453 {
efb1c01c
MK
454 struct type *subtype = check_typedef (TYPE_TARGET_TYPE (type));
455
456 /* All fields in an array have the same type. */
457 amd64_classify (subtype, class);
458 if (len > 8 && class[1] == AMD64_NO_CLASS)
459 class[1] = class[0];
8ffd9b1b 460 }
53e95fcf
JS
461 else
462 {
efb1c01c 463 int i;
53e95fcf 464
efb1c01c
MK
465 /* Structure or union. */
466 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
467 || TYPE_CODE (type) == TYPE_CODE_UNION);
468
469 for (i = 0; i < TYPE_NFIELDS (type); i++)
53e95fcf 470 {
efb1c01c
MK
471 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
472 int pos = TYPE_FIELD_BITPOS (type, i) / 64;
473 enum amd64_reg_class subclass[2];
e4e2711a
JB
474 int bitsize = TYPE_FIELD_BITSIZE (type, i);
475 int endpos;
476
477 if (bitsize == 0)
478 bitsize = TYPE_LENGTH (subtype) * 8;
479 endpos = (TYPE_FIELD_BITPOS (type, i) + bitsize - 1) / 64;
efb1c01c 480
562c50c2 481 /* Ignore static fields. */
d6a843b5 482 if (field_is_static (&TYPE_FIELD (type, i)))
562c50c2
MK
483 continue;
484
efb1c01c
MK
485 gdb_assert (pos == 0 || pos == 1);
486
487 amd64_classify (subtype, subclass);
488 class[pos] = amd64_merge_classes (class[pos], subclass[0]);
e4e2711a
JB
489 if (bitsize <= 64 && pos == 0 && endpos == 1)
490 /* This is a bit of an odd case: We have a field that would
491 normally fit in one of the two eightbytes, except that
492 it is placed in a way that this field straddles them.
493 This has been seen with a structure containing an array.
494
495 The ABI is a bit unclear in this case, but we assume that
496 this field's class (stored in subclass[0]) must also be merged
497 into class[1]. In other words, our field has a piece stored
498 in the second eight-byte, and thus its class applies to
499 the second eight-byte as well.
500
501 In the case where the field length exceeds 8 bytes,
502 it should not be necessary to merge the field class
503 into class[1]. As LEN > 8, subclass[1] is necessarily
504 different from AMD64_NO_CLASS. If subclass[1] is equal
505 to subclass[0], then the normal class[1]/subclass[1]
506 merging will take care of everything. For subclass[1]
507 to be different from subclass[0], I can only see the case
508 where we have a SSE/SSEUP or X87/X87UP pair, which both
509 use up all 16 bytes of the aggregate, and are already
510 handled just fine (because each portion sits on its own
511 8-byte). */
512 class[1] = amd64_merge_classes (class[1], subclass[0]);
efb1c01c
MK
513 if (pos == 0)
514 class[1] = amd64_merge_classes (class[1], subclass[1]);
53e95fcf 515 }
53e95fcf 516 }
efb1c01c
MK
517
518 /* 4. Then a post merger cleanup is done: */
519
520 /* Rule (a): If one of the classes is MEMORY, the whole argument is
521 passed in memory. */
522 if (class[0] == AMD64_MEMORY || class[1] == AMD64_MEMORY)
523 class[0] = class[1] = AMD64_MEMORY;
524
525 /* Rule (b): If SSEUP is not preceeded by SSE, it is converted to
526 SSE. */
527 if (class[0] == AMD64_SSEUP)
528 class[0] = AMD64_SSE;
529 if (class[1] == AMD64_SSEUP && class[0] != AMD64_SSE)
530 class[1] = AMD64_SSE;
531}
532
533/* Classify TYPE, and store the result in CLASS. */
534
ba581dc1 535void
efb1c01c
MK
536amd64_classify (struct type *type, enum amd64_reg_class class[2])
537{
538 enum type_code code = TYPE_CODE (type);
539 int len = TYPE_LENGTH (type);
540
541 class[0] = class[1] = AMD64_NO_CLASS;
542
543 /* Arguments of types (signed and unsigned) _Bool, char, short, int,
5a7225ed
JB
544 long, long long, and pointers are in the INTEGER class. Similarly,
545 range types, used by languages such as Ada, are also in the INTEGER
546 class. */
efb1c01c 547 if ((code == TYPE_CODE_INT || code == TYPE_CODE_ENUM
b929c77f 548 || code == TYPE_CODE_BOOL || code == TYPE_CODE_RANGE
9db13498 549 || code == TYPE_CODE_CHAR
efb1c01c
MK
550 || code == TYPE_CODE_PTR || code == TYPE_CODE_REF)
551 && (len == 1 || len == 2 || len == 4 || len == 8))
552 class[0] = AMD64_INTEGER;
553
5daa78cc
TJB
554 /* Arguments of types float, double, _Decimal32, _Decimal64 and __m64
555 are in class SSE. */
556 else if ((code == TYPE_CODE_FLT || code == TYPE_CODE_DECFLOAT)
557 && (len == 4 || len == 8))
efb1c01c
MK
558 /* FIXME: __m64 . */
559 class[0] = AMD64_SSE;
560
5daa78cc
TJB
561 /* Arguments of types __float128, _Decimal128 and __m128 are split into
562 two halves. The least significant ones belong to class SSE, the most
efb1c01c 563 significant one to class SSEUP. */
5daa78cc
TJB
564 else if (code == TYPE_CODE_DECFLOAT && len == 16)
565 /* FIXME: __float128, __m128. */
566 class[0] = AMD64_SSE, class[1] = AMD64_SSEUP;
efb1c01c
MK
567
568 /* The 64-bit mantissa of arguments of type long double belongs to
569 class X87, the 16-bit exponent plus 6 bytes of padding belongs to
570 class X87UP. */
571 else if (code == TYPE_CODE_FLT && len == 16)
572 /* Class X87 and X87UP. */
573 class[0] = AMD64_X87, class[1] = AMD64_X87UP;
574
575 /* Aggregates. */
576 else if (code == TYPE_CODE_ARRAY || code == TYPE_CODE_STRUCT
577 || code == TYPE_CODE_UNION)
578 amd64_classify_aggregate (type, class);
579}
580
581static enum return_value_convention
c055b101
CV
582amd64_return_value (struct gdbarch *gdbarch, struct type *func_type,
583 struct type *type, struct regcache *regcache,
42835c2b 584 gdb_byte *readbuf, const gdb_byte *writebuf)
efb1c01c 585{
ba581dc1 586 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
efb1c01c
MK
587 enum amd64_reg_class class[2];
588 int len = TYPE_LENGTH (type);
90f90721
MK
589 static int integer_regnum[] = { AMD64_RAX_REGNUM, AMD64_RDX_REGNUM };
590 static int sse_regnum[] = { AMD64_XMM0_REGNUM, AMD64_XMM1_REGNUM };
efb1c01c
MK
591 int integer_reg = 0;
592 int sse_reg = 0;
593 int i;
594
595 gdb_assert (!(readbuf && writebuf));
ba581dc1 596 gdb_assert (tdep->classify);
efb1c01c
MK
597
598 /* 1. Classify the return type with the classification algorithm. */
ba581dc1 599 tdep->classify (type, class);
efb1c01c
MK
600
601 /* 2. If the type has class MEMORY, then the caller provides space
6fa57a7d
MK
602 for the return value and passes the address of this storage in
603 %rdi as if it were the first argument to the function. In effect,
604 this address becomes a hidden first argument.
605
606 On return %rax will contain the address that has been passed in
607 by the caller in %rdi. */
efb1c01c 608 if (class[0] == AMD64_MEMORY)
6fa57a7d
MK
609 {
610 /* As indicated by the comment above, the ABI guarantees that we
611 can always find the return value just after the function has
612 returned. */
613
614 if (readbuf)
615 {
616 ULONGEST addr;
617
618 regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
619 read_memory (addr, readbuf, TYPE_LENGTH (type));
620 }
621
622 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
623 }
efb1c01c
MK
624
625 gdb_assert (class[1] != AMD64_MEMORY);
626 gdb_assert (len <= 16);
627
628 for (i = 0; len > 0; i++, len -= 8)
629 {
630 int regnum = -1;
631 int offset = 0;
632
633 switch (class[i])
634 {
635 case AMD64_INTEGER:
636 /* 3. If the class is INTEGER, the next available register
637 of the sequence %rax, %rdx is used. */
638 regnum = integer_regnum[integer_reg++];
639 break;
640
641 case AMD64_SSE:
642 /* 4. If the class is SSE, the next available SSE register
643 of the sequence %xmm0, %xmm1 is used. */
644 regnum = sse_regnum[sse_reg++];
645 break;
646
647 case AMD64_SSEUP:
648 /* 5. If the class is SSEUP, the eightbyte is passed in the
649 upper half of the last used SSE register. */
650 gdb_assert (sse_reg > 0);
651 regnum = sse_regnum[sse_reg - 1];
652 offset = 8;
653 break;
654
655 case AMD64_X87:
656 /* 6. If the class is X87, the value is returned on the X87
657 stack in %st0 as 80-bit x87 number. */
90f90721 658 regnum = AMD64_ST0_REGNUM;
efb1c01c
MK
659 if (writebuf)
660 i387_return_value (gdbarch, regcache);
661 break;
662
663 case AMD64_X87UP:
664 /* 7. If the class is X87UP, the value is returned together
665 with the previous X87 value in %st0. */
666 gdb_assert (i > 0 && class[0] == AMD64_X87);
90f90721 667 regnum = AMD64_ST0_REGNUM;
efb1c01c
MK
668 offset = 8;
669 len = 2;
670 break;
671
672 case AMD64_NO_CLASS:
673 continue;
674
675 default:
676 gdb_assert (!"Unexpected register class.");
677 }
678
679 gdb_assert (regnum != -1);
680
681 if (readbuf)
682 regcache_raw_read_part (regcache, regnum, offset, min (len, 8),
42835c2b 683 readbuf + i * 8);
efb1c01c
MK
684 if (writebuf)
685 regcache_raw_write_part (regcache, regnum, offset, min (len, 8),
42835c2b 686 writebuf + i * 8);
efb1c01c
MK
687 }
688
689 return RETURN_VALUE_REGISTER_CONVENTION;
53e95fcf
JS
690}
691\f
692
720aa428
MK
693static CORE_ADDR
694amd64_push_arguments (struct regcache *regcache, int nargs,
6470d250 695 struct value **args, CORE_ADDR sp, int struct_return)
720aa428 696{
80d19a06
JB
697 struct gdbarch *gdbarch = get_regcache_arch (regcache);
698 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
ba581dc1
JB
699 int *integer_regs = tdep->call_dummy_integer_regs;
700 int num_integer_regs = tdep->call_dummy_num_integer_regs;
701
720aa428
MK
702 static int sse_regnum[] =
703 {
704 /* %xmm0 ... %xmm7 */
90f90721
MK
705 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
706 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
707 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
708 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
720aa428
MK
709 };
710 struct value **stack_args = alloca (nargs * sizeof (struct value *));
80d19a06
JB
711 /* An array that mirrors the stack_args array. For all arguments
712 that are passed by MEMORY, if that argument's address also needs
713 to be stored in a register, the ARG_ADDR_REGNO array will contain
714 that register number (or a negative value otherwise). */
715 int *arg_addr_regno = alloca (nargs * sizeof (int));
720aa428
MK
716 int num_stack_args = 0;
717 int num_elements = 0;
718 int element = 0;
719 int integer_reg = 0;
720 int sse_reg = 0;
721 int i;
722
ba581dc1
JB
723 gdb_assert (tdep->classify);
724
6470d250
MK
725 /* Reserve a register for the "hidden" argument. */
726 if (struct_return)
727 integer_reg++;
728
720aa428
MK
729 for (i = 0; i < nargs; i++)
730 {
4991999e 731 struct type *type = value_type (args[i]);
720aa428
MK
732 int len = TYPE_LENGTH (type);
733 enum amd64_reg_class class[2];
734 int needed_integer_regs = 0;
735 int needed_sse_regs = 0;
736 int j;
737
738 /* Classify argument. */
ba581dc1 739 tdep->classify (type, class);
720aa428
MK
740
741 /* Calculate the number of integer and SSE registers needed for
742 this argument. */
743 for (j = 0; j < 2; j++)
744 {
745 if (class[j] == AMD64_INTEGER)
746 needed_integer_regs++;
747 else if (class[j] == AMD64_SSE)
748 needed_sse_regs++;
749 }
750
751 /* Check whether enough registers are available, and if the
752 argument should be passed in registers at all. */
ba581dc1 753 if (integer_reg + needed_integer_regs > num_integer_regs
720aa428
MK
754 || sse_reg + needed_sse_regs > ARRAY_SIZE (sse_regnum)
755 || (needed_integer_regs == 0 && needed_sse_regs == 0))
756 {
757 /* The argument will be passed on the stack. */
758 num_elements += ((len + 7) / 8);
80d19a06
JB
759 stack_args[num_stack_args] = args[i];
760 /* If this is an AMD64_MEMORY argument whose address must also
761 be passed in one of the integer registers, reserve that
762 register and associate this value to that register so that
763 we can store the argument address as soon as we know it. */
764 if (class[0] == AMD64_MEMORY
765 && tdep->memory_args_by_pointer
766 && integer_reg < tdep->call_dummy_num_integer_regs)
767 arg_addr_regno[num_stack_args] =
768 tdep->call_dummy_integer_regs[integer_reg++];
769 else
770 arg_addr_regno[num_stack_args] = -1;
771 num_stack_args++;
720aa428
MK
772 }
773 else
774 {
775 /* The argument will be passed in registers. */
d8de1ef7
MK
776 const gdb_byte *valbuf = value_contents (args[i]);
777 gdb_byte buf[8];
720aa428
MK
778
779 gdb_assert (len <= 16);
780
781 for (j = 0; len > 0; j++, len -= 8)
782 {
783 int regnum = -1;
784 int offset = 0;
785
786 switch (class[j])
787 {
788 case AMD64_INTEGER:
ba581dc1 789 regnum = integer_regs[integer_reg++];
720aa428
MK
790 break;
791
792 case AMD64_SSE:
793 regnum = sse_regnum[sse_reg++];
794 break;
795
796 case AMD64_SSEUP:
797 gdb_assert (sse_reg > 0);
798 regnum = sse_regnum[sse_reg - 1];
799 offset = 8;
800 break;
801
802 default:
803 gdb_assert (!"Unexpected register class.");
804 }
805
806 gdb_assert (regnum != -1);
807 memset (buf, 0, sizeof buf);
808 memcpy (buf, valbuf + j * 8, min (len, 8));
809 regcache_raw_write_part (regcache, regnum, offset, 8, buf);
810 }
811 }
812 }
813
814 /* Allocate space for the arguments on the stack. */
815 sp -= num_elements * 8;
816
817 /* The psABI says that "The end of the input argument area shall be
818 aligned on a 16 byte boundary." */
819 sp &= ~0xf;
820
821 /* Write out the arguments to the stack. */
822 for (i = 0; i < num_stack_args; i++)
823 {
4991999e 824 struct type *type = value_type (stack_args[i]);
d8de1ef7 825 const gdb_byte *valbuf = value_contents (stack_args[i]);
720aa428 826 int len = TYPE_LENGTH (type);
80d19a06
JB
827 CORE_ADDR arg_addr = sp + element * 8;
828
829 write_memory (arg_addr, valbuf, len);
830 if (arg_addr_regno[i] >= 0)
831 {
832 /* We also need to store the address of that argument in
833 the given register. */
834 gdb_byte buf[8];
835 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
836
837 store_unsigned_integer (buf, 8, byte_order, arg_addr);
838 regcache_cooked_write (regcache, arg_addr_regno[i], buf);
839 }
720aa428
MK
840 element += ((len + 7) / 8);
841 }
842
843 /* The psABI says that "For calls that may call functions that use
844 varargs or stdargs (prototype-less calls or calls to functions
845 containing ellipsis (...) in the declaration) %al is used as
846 hidden argument to specify the number of SSE registers used. */
90f90721 847 regcache_raw_write_unsigned (regcache, AMD64_RAX_REGNUM, sse_reg);
720aa428
MK
848 return sp;
849}
850
c4f35dd8 851static CORE_ADDR
7d9b040b 852amd64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
e53bef9f
MK
853 struct regcache *regcache, CORE_ADDR bp_addr,
854 int nargs, struct value **args, CORE_ADDR sp,
855 int struct_return, CORE_ADDR struct_addr)
53e95fcf 856{
e17a4113 857 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3af6ddfe 858 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
d8de1ef7 859 gdb_byte buf[8];
c4f35dd8
MK
860
861 /* Pass arguments. */
6470d250 862 sp = amd64_push_arguments (regcache, nargs, args, sp, struct_return);
c4f35dd8
MK
863
864 /* Pass "hidden" argument". */
865 if (struct_return)
866 {
ba581dc1
JB
867 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
868 /* The "hidden" argument is passed throught the first argument
869 register. */
870 const int arg_regnum = tdep->call_dummy_integer_regs[0];
871
e17a4113 872 store_unsigned_integer (buf, 8, byte_order, struct_addr);
ba581dc1 873 regcache_cooked_write (regcache, arg_regnum, buf);
c4f35dd8
MK
874 }
875
3af6ddfe
JB
876 /* Reserve some memory on the stack for the integer-parameter registers,
877 if required by the ABI. */
878 if (tdep->integer_param_regs_saved_in_caller_frame)
879 sp -= tdep->call_dummy_num_integer_regs * 8;
880
c4f35dd8
MK
881 /* Store return address. */
882 sp -= 8;
e17a4113 883 store_unsigned_integer (buf, 8, byte_order, bp_addr);
c4f35dd8
MK
884 write_memory (sp, buf, 8);
885
886 /* Finally, update the stack pointer... */
e17a4113 887 store_unsigned_integer (buf, 8, byte_order, sp);
90f90721 888 regcache_cooked_write (regcache, AMD64_RSP_REGNUM, buf);
c4f35dd8
MK
889
890 /* ...and fake a frame pointer. */
90f90721 891 regcache_cooked_write (regcache, AMD64_RBP_REGNUM, buf);
c4f35dd8 892
3e210248 893 return sp + 16;
53e95fcf 894}
c4f35dd8 895\f
35669430
DE
896/* Displaced instruction handling. */
897
898/* A partially decoded instruction.
899 This contains enough details for displaced stepping purposes. */
900
901struct amd64_insn
902{
903 /* The number of opcode bytes. */
904 int opcode_len;
905 /* The offset of the rex prefix or -1 if not present. */
906 int rex_offset;
907 /* The offset to the first opcode byte. */
908 int opcode_offset;
909 /* The offset to the modrm byte or -1 if not present. */
910 int modrm_offset;
911
912 /* The raw instruction. */
913 gdb_byte *raw_insn;
914};
915
916struct displaced_step_closure
917{
918 /* For rip-relative insns, saved copy of the reg we use instead of %rip. */
919 int tmp_used;
920 int tmp_regno;
921 ULONGEST tmp_save;
922
923 /* Details of the instruction. */
924 struct amd64_insn insn_details;
925
926 /* Amount of space allocated to insn_buf. */
927 int max_len;
928
929 /* The possibly modified insn.
930 This is a variable-length field. */
931 gdb_byte insn_buf[1];
932};
933
934/* WARNING: Keep onebyte_has_modrm, twobyte_has_modrm in sync with
935 ../opcodes/i386-dis.c (until libopcodes exports them, or an alternative,
936 at which point delete these in favor of libopcodes' versions). */
937
938static const unsigned char onebyte_has_modrm[256] = {
939 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
940 /* ------------------------------- */
941 /* 00 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 00 */
942 /* 10 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 10 */
943 /* 20 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 20 */
944 /* 30 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 30 */
945 /* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 40 */
946 /* 50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
947 /* 60 */ 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0, /* 60 */
948 /* 70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
949 /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 80 */
950 /* 90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
951 /* a0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* a0 */
952 /* b0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* b0 */
953 /* c0 */ 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0, /* c0 */
954 /* d0 */ 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, /* d0 */
955 /* e0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* e0 */
956 /* f0 */ 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1 /* f0 */
957 /* ------------------------------- */
958 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
959};
960
961static const unsigned char twobyte_has_modrm[256] = {
962 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
963 /* ------------------------------- */
964 /* 00 */ 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1, /* 0f */
965 /* 10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 1f */
966 /* 20 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 2f */
967 /* 30 */ 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0, /* 3f */
968 /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4f */
969 /* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
970 /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6f */
971 /* 70 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 7f */
972 /* 80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 8f */
973 /* 90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 9f */
974 /* a0 */ 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1, /* af */
975 /* b0 */ 1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1, /* bf */
976 /* c0 */ 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, /* cf */
977 /* d0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* df */
978 /* e0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ef */
979 /* f0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 /* ff */
980 /* ------------------------------- */
981 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
982};
983
984static int amd64_syscall_p (const struct amd64_insn *insn, int *lengthp);
985
986static int
987rex_prefix_p (gdb_byte pfx)
988{
989 return REX_PREFIX_P (pfx);
990}
991
992/* Skip the legacy instruction prefixes in INSN.
993 We assume INSN is properly sentineled so we don't have to worry
994 about falling off the end of the buffer. */
995
996static gdb_byte *
1903f0e6 997amd64_skip_prefixes (gdb_byte *insn)
35669430
DE
998{
999 while (1)
1000 {
1001 switch (*insn)
1002 {
1003 case DATA_PREFIX_OPCODE:
1004 case ADDR_PREFIX_OPCODE:
1005 case CS_PREFIX_OPCODE:
1006 case DS_PREFIX_OPCODE:
1007 case ES_PREFIX_OPCODE:
1008 case FS_PREFIX_OPCODE:
1009 case GS_PREFIX_OPCODE:
1010 case SS_PREFIX_OPCODE:
1011 case LOCK_PREFIX_OPCODE:
1012 case REPE_PREFIX_OPCODE:
1013 case REPNE_PREFIX_OPCODE:
1014 ++insn;
1015 continue;
1016 default:
1017 break;
1018 }
1019 break;
1020 }
1021
1022 return insn;
1023}
1024
1025/* fprintf-function for amd64_insn_length.
1026 This function is a nop, we don't want to print anything, we just want to
1027 compute the length of the insn. */
1028
1029static int ATTR_FORMAT (printf, 2, 3)
1030amd64_insn_length_fprintf (void *stream, const char *format, ...)
1031{
1032 return 0;
1033}
1034
1035/* Initialize a struct disassemble_info for amd64_insn_length. */
1036
1037static void
1038amd64_insn_length_init_dis (struct gdbarch *gdbarch,
1039 struct disassemble_info *di,
1040 const gdb_byte *insn, int max_len,
1041 CORE_ADDR addr)
1042{
1043 init_disassemble_info (di, NULL, amd64_insn_length_fprintf);
1044
1045 /* init_disassemble_info installs buffer_read_memory, etc.
1046 so we don't need to do that here.
1047 The cast is necessary until disassemble_info is const-ified. */
1048 di->buffer = (gdb_byte *) insn;
1049 di->buffer_length = max_len;
1050 di->buffer_vma = addr;
1051
1052 di->arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1053 di->mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1054 di->endian = gdbarch_byte_order (gdbarch);
1055 di->endian_code = gdbarch_byte_order_for_code (gdbarch);
1056
1057 disassemble_init_for_target (di);
1058}
1059
1060/* Return the length in bytes of INSN.
1061 MAX_LEN is the size of the buffer containing INSN.
1062 libopcodes currently doesn't export a utility to compute the
1063 instruction length, so use the disassembler until then. */
1064
1065static int
1066amd64_insn_length (struct gdbarch *gdbarch,
1067 const gdb_byte *insn, int max_len, CORE_ADDR addr)
1068{
1069 struct disassemble_info di;
1070
1071 amd64_insn_length_init_dis (gdbarch, &di, insn, max_len, addr);
1072
1073 return gdbarch_print_insn (gdbarch, addr, &di);
1074}
1075
1076/* Return an integer register (other than RSP) that is unused as an input
1077 operand in INSN.
1078 In order to not require adding a rex prefix if the insn doesn't already
1079 have one, the result is restricted to RAX ... RDI, sans RSP.
1080 The register numbering of the result follows architecture ordering,
1081 e.g. RDI = 7. */
1082
1083static int
1084amd64_get_unused_input_int_reg (const struct amd64_insn *details)
1085{
1086 /* 1 bit for each reg */
1087 int used_regs_mask = 0;
1088
1089 /* There can be at most 3 int regs used as inputs in an insn, and we have
1090 7 to choose from (RAX ... RDI, sans RSP).
1091 This allows us to take a conservative approach and keep things simple.
1092 E.g. By avoiding RAX, we don't have to specifically watch for opcodes
1093 that implicitly specify RAX. */
1094
1095 /* Avoid RAX. */
1096 used_regs_mask |= 1 << EAX_REG_NUM;
1097 /* Similarily avoid RDX, implicit operand in divides. */
1098 used_regs_mask |= 1 << EDX_REG_NUM;
1099 /* Avoid RSP. */
1100 used_regs_mask |= 1 << ESP_REG_NUM;
1101
1102 /* If the opcode is one byte long and there's no ModRM byte,
1103 assume the opcode specifies a register. */
1104 if (details->opcode_len == 1 && details->modrm_offset == -1)
1105 used_regs_mask |= 1 << (details->raw_insn[details->opcode_offset] & 7);
1106
1107 /* Mark used regs in the modrm/sib bytes. */
1108 if (details->modrm_offset != -1)
1109 {
1110 int modrm = details->raw_insn[details->modrm_offset];
1111 int mod = MODRM_MOD_FIELD (modrm);
1112 int reg = MODRM_REG_FIELD (modrm);
1113 int rm = MODRM_RM_FIELD (modrm);
1114 int have_sib = mod != 3 && rm == 4;
1115
1116 /* Assume the reg field of the modrm byte specifies a register. */
1117 used_regs_mask |= 1 << reg;
1118
1119 if (have_sib)
1120 {
1121 int base = SIB_BASE_FIELD (details->raw_insn[details->modrm_offset + 1]);
1122 int index = SIB_INDEX_FIELD (details->raw_insn[details->modrm_offset + 1]);
1123 used_regs_mask |= 1 << base;
1124 used_regs_mask |= 1 << index;
1125 }
1126 else
1127 {
1128 used_regs_mask |= 1 << rm;
1129 }
1130 }
1131
1132 gdb_assert (used_regs_mask < 256);
1133 gdb_assert (used_regs_mask != 255);
1134
1135 /* Finally, find a free reg. */
1136 {
1137 int i;
1138
1139 for (i = 0; i < 8; ++i)
1140 {
1141 if (! (used_regs_mask & (1 << i)))
1142 return i;
1143 }
1144
1145 /* We shouldn't get here. */
1146 internal_error (__FILE__, __LINE__, _("unable to find free reg"));
1147 }
1148}
1149
1150/* Extract the details of INSN that we need. */
1151
1152static void
1153amd64_get_insn_details (gdb_byte *insn, struct amd64_insn *details)
1154{
1155 gdb_byte *start = insn;
1156 int need_modrm;
1157
1158 details->raw_insn = insn;
1159
1160 details->opcode_len = -1;
1161 details->rex_offset = -1;
1162 details->opcode_offset = -1;
1163 details->modrm_offset = -1;
1164
1165 /* Skip legacy instruction prefixes. */
1903f0e6 1166 insn = amd64_skip_prefixes (insn);
35669430
DE
1167
1168 /* Skip REX instruction prefix. */
1169 if (rex_prefix_p (*insn))
1170 {
1171 details->rex_offset = insn - start;
1172 ++insn;
1173 }
1174
1175 details->opcode_offset = insn - start;
1176
1177 if (*insn == TWO_BYTE_OPCODE_ESCAPE)
1178 {
1179 /* Two or three-byte opcode. */
1180 ++insn;
1181 need_modrm = twobyte_has_modrm[*insn];
1182
1183 /* Check for three-byte opcode. */
1903f0e6 1184 switch (*insn)
35669430 1185 {
1903f0e6
DE
1186 case 0x24:
1187 case 0x25:
1188 case 0x38:
1189 case 0x3a:
1190 case 0x7a:
1191 case 0x7b:
35669430
DE
1192 ++insn;
1193 details->opcode_len = 3;
1903f0e6
DE
1194 break;
1195 default:
1196 details->opcode_len = 2;
1197 break;
35669430 1198 }
35669430
DE
1199 }
1200 else
1201 {
1202 /* One-byte opcode. */
1203 need_modrm = onebyte_has_modrm[*insn];
1204 details->opcode_len = 1;
1205 }
1206
1207 if (need_modrm)
1208 {
1209 ++insn;
1210 details->modrm_offset = insn - start;
1211 }
1212}
1213
1214/* Update %rip-relative addressing in INSN.
1215
1216 %rip-relative addressing only uses a 32-bit displacement.
1217 32 bits is not enough to be guaranteed to cover the distance between where
1218 the real instruction is and where its copy is.
1219 Convert the insn to use base+disp addressing.
1220 We set base = pc + insn_length so we can leave disp unchanged. */
c4f35dd8 1221
35669430
DE
1222static void
1223fixup_riprel (struct gdbarch *gdbarch, struct displaced_step_closure *dsc,
1224 CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1225{
e17a4113 1226 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
35669430
DE
1227 const struct amd64_insn *insn_details = &dsc->insn_details;
1228 int modrm_offset = insn_details->modrm_offset;
1229 gdb_byte *insn = insn_details->raw_insn + modrm_offset;
1230 CORE_ADDR rip_base;
1231 int32_t disp;
1232 int insn_length;
1233 int arch_tmp_regno, tmp_regno;
1234 ULONGEST orig_value;
1235
1236 /* %rip+disp32 addressing mode, displacement follows ModRM byte. */
1237 ++insn;
1238
1239 /* Compute the rip-relative address. */
e17a4113 1240 disp = extract_signed_integer (insn, sizeof (int32_t), byte_order);
35669430
DE
1241 insn_length = amd64_insn_length (gdbarch, dsc->insn_buf, dsc->max_len, from);
1242 rip_base = from + insn_length;
1243
1244 /* We need a register to hold the address.
1245 Pick one not used in the insn.
1246 NOTE: arch_tmp_regno uses architecture ordering, e.g. RDI = 7. */
1247 arch_tmp_regno = amd64_get_unused_input_int_reg (insn_details);
1248 tmp_regno = amd64_arch_reg_to_regnum (arch_tmp_regno);
1249
1250 /* REX.B should be unset as we were using rip-relative addressing,
1251 but ensure it's unset anyway, tmp_regno is not r8-r15. */
1252 if (insn_details->rex_offset != -1)
1253 dsc->insn_buf[insn_details->rex_offset] &= ~REX_B;
1254
1255 regcache_cooked_read_unsigned (regs, tmp_regno, &orig_value);
1256 dsc->tmp_regno = tmp_regno;
1257 dsc->tmp_save = orig_value;
1258 dsc->tmp_used = 1;
1259
1260 /* Convert the ModRM field to be base+disp. */
1261 dsc->insn_buf[modrm_offset] &= ~0xc7;
1262 dsc->insn_buf[modrm_offset] |= 0x80 + arch_tmp_regno;
1263
1264 regcache_cooked_write_unsigned (regs, tmp_regno, rip_base);
1265
1266 if (debug_displaced)
1267 fprintf_unfiltered (gdb_stdlog, "displaced: %%rip-relative addressing used.\n"
5af949e3
UW
1268 "displaced: using temp reg %d, old value %s, new value %s\n",
1269 dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save),
1270 paddress (gdbarch, rip_base));
35669430
DE
1271}
1272
1273static void
1274fixup_displaced_copy (struct gdbarch *gdbarch,
1275 struct displaced_step_closure *dsc,
1276 CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1277{
1278 const struct amd64_insn *details = &dsc->insn_details;
1279
1280 if (details->modrm_offset != -1)
1281 {
1282 gdb_byte modrm = details->raw_insn[details->modrm_offset];
1283
1284 if ((modrm & 0xc7) == 0x05)
1285 {
1286 /* The insn uses rip-relative addressing.
1287 Deal with it. */
1288 fixup_riprel (gdbarch, dsc, from, to, regs);
1289 }
1290 }
1291}
1292
1293struct displaced_step_closure *
1294amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
1295 CORE_ADDR from, CORE_ADDR to,
1296 struct regcache *regs)
1297{
1298 int len = gdbarch_max_insn_length (gdbarch);
1299 /* Extra space for sentinels so fixup_{riprel,displaced_copy don't have to
1300 continually watch for running off the end of the buffer. */
1301 int fixup_sentinel_space = len;
1302 struct displaced_step_closure *dsc =
1303 xmalloc (sizeof (*dsc) + len + fixup_sentinel_space);
1304 gdb_byte *buf = &dsc->insn_buf[0];
1305 struct amd64_insn *details = &dsc->insn_details;
1306
1307 dsc->tmp_used = 0;
1308 dsc->max_len = len + fixup_sentinel_space;
1309
1310 read_memory (from, buf, len);
1311
1312 /* Set up the sentinel space so we don't have to worry about running
1313 off the end of the buffer. An excessive number of leading prefixes
1314 could otherwise cause this. */
1315 memset (buf + len, 0, fixup_sentinel_space);
1316
1317 amd64_get_insn_details (buf, details);
1318
1319 /* GDB may get control back after the insn after the syscall.
1320 Presumably this is a kernel bug.
1321 If this is a syscall, make sure there's a nop afterwards. */
1322 {
1323 int syscall_length;
1324
1325 if (amd64_syscall_p (details, &syscall_length))
1326 buf[details->opcode_offset + syscall_length] = NOP_OPCODE;
1327 }
1328
1329 /* Modify the insn to cope with the address where it will be executed from.
1330 In particular, handle any rip-relative addressing. */
1331 fixup_displaced_copy (gdbarch, dsc, from, to, regs);
1332
1333 write_memory (to, buf, len);
1334
1335 if (debug_displaced)
1336 {
5af949e3
UW
1337 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
1338 paddress (gdbarch, from), paddress (gdbarch, to));
35669430
DE
1339 displaced_step_dump_bytes (gdb_stdlog, buf, len);
1340 }
1341
1342 return dsc;
1343}
1344
1345static int
1346amd64_absolute_jmp_p (const struct amd64_insn *details)
1347{
1348 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1349
1350 if (insn[0] == 0xff)
1351 {
1352 /* jump near, absolute indirect (/4) */
1353 if ((insn[1] & 0x38) == 0x20)
1354 return 1;
1355
1356 /* jump far, absolute indirect (/5) */
1357 if ((insn[1] & 0x38) == 0x28)
1358 return 1;
1359 }
1360
1361 return 0;
1362}
1363
1364static int
1365amd64_absolute_call_p (const struct amd64_insn *details)
1366{
1367 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1368
1369 if (insn[0] == 0xff)
1370 {
1371 /* Call near, absolute indirect (/2) */
1372 if ((insn[1] & 0x38) == 0x10)
1373 return 1;
1374
1375 /* Call far, absolute indirect (/3) */
1376 if ((insn[1] & 0x38) == 0x18)
1377 return 1;
1378 }
1379
1380 return 0;
1381}
1382
1383static int
1384amd64_ret_p (const struct amd64_insn *details)
1385{
1386 /* NOTE: gcc can emit "repz ; ret". */
1387 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1388
1389 switch (insn[0])
1390 {
1391 case 0xc2: /* ret near, pop N bytes */
1392 case 0xc3: /* ret near */
1393 case 0xca: /* ret far, pop N bytes */
1394 case 0xcb: /* ret far */
1395 case 0xcf: /* iret */
1396 return 1;
1397
1398 default:
1399 return 0;
1400 }
1401}
1402
1403static int
1404amd64_call_p (const struct amd64_insn *details)
1405{
1406 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1407
1408 if (amd64_absolute_call_p (details))
1409 return 1;
1410
1411 /* call near, relative */
1412 if (insn[0] == 0xe8)
1413 return 1;
1414
1415 return 0;
1416}
1417
35669430
DE
1418/* Return non-zero if INSN is a system call, and set *LENGTHP to its
1419 length in bytes. Otherwise, return zero. */
1420
1421static int
1422amd64_syscall_p (const struct amd64_insn *details, int *lengthp)
1423{
1424 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1425
1426 if (insn[0] == 0x0f && insn[1] == 0x05)
1427 {
1428 *lengthp = 2;
1429 return 1;
1430 }
1431
1432 return 0;
1433}
1434
1435/* Fix up the state of registers and memory after having single-stepped
1436 a displaced instruction. */
1437
1438void
1439amd64_displaced_step_fixup (struct gdbarch *gdbarch,
1440 struct displaced_step_closure *dsc,
1441 CORE_ADDR from, CORE_ADDR to,
1442 struct regcache *regs)
1443{
e17a4113 1444 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
35669430
DE
1445 /* The offset we applied to the instruction's address. */
1446 ULONGEST insn_offset = to - from;
1447 gdb_byte *insn = dsc->insn_buf;
1448 const struct amd64_insn *insn_details = &dsc->insn_details;
1449
1450 if (debug_displaced)
1451 fprintf_unfiltered (gdb_stdlog,
5af949e3 1452 "displaced: fixup (%s, %s), "
35669430 1453 "insn = 0x%02x 0x%02x ...\n",
5af949e3
UW
1454 paddress (gdbarch, from), paddress (gdbarch, to),
1455 insn[0], insn[1]);
35669430
DE
1456
1457 /* If we used a tmp reg, restore it. */
1458
1459 if (dsc->tmp_used)
1460 {
1461 if (debug_displaced)
5af949e3
UW
1462 fprintf_unfiltered (gdb_stdlog, "displaced: restoring reg %d to %s\n",
1463 dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save));
35669430
DE
1464 regcache_cooked_write_unsigned (regs, dsc->tmp_regno, dsc->tmp_save);
1465 }
1466
1467 /* The list of issues to contend with here is taken from
1468 resume_execution in arch/x86/kernel/kprobes.c, Linux 2.6.28.
1469 Yay for Free Software! */
1470
1471 /* Relocate the %rip back to the program's instruction stream,
1472 if necessary. */
1473
1474 /* Except in the case of absolute or indirect jump or call
1475 instructions, or a return instruction, the new rip is relative to
1476 the displaced instruction; make it relative to the original insn.
1477 Well, signal handler returns don't need relocation either, but we use the
1478 value of %rip to recognize those; see below. */
1479 if (! amd64_absolute_jmp_p (insn_details)
1480 && ! amd64_absolute_call_p (insn_details)
1481 && ! amd64_ret_p (insn_details))
1482 {
1483 ULONGEST orig_rip;
1484 int insn_len;
1485
1486 regcache_cooked_read_unsigned (regs, AMD64_RIP_REGNUM, &orig_rip);
1487
1488 /* A signal trampoline system call changes the %rip, resuming
1489 execution of the main program after the signal handler has
1490 returned. That makes them like 'return' instructions; we
1491 shouldn't relocate %rip.
1492
1493 But most system calls don't, and we do need to relocate %rip.
1494
1495 Our heuristic for distinguishing these cases: if stepping
1496 over the system call instruction left control directly after
1497 the instruction, the we relocate --- control almost certainly
1498 doesn't belong in the displaced copy. Otherwise, we assume
1499 the instruction has put control where it belongs, and leave
1500 it unrelocated. Goodness help us if there are PC-relative
1501 system calls. */
1502 if (amd64_syscall_p (insn_details, &insn_len)
1503 && orig_rip != to + insn_len
1504 /* GDB can get control back after the insn after the syscall.
1505 Presumably this is a kernel bug.
1506 Fixup ensures its a nop, we add one to the length for it. */
1507 && orig_rip != to + insn_len + 1)
1508 {
1509 if (debug_displaced)
1510 fprintf_unfiltered (gdb_stdlog,
1511 "displaced: syscall changed %%rip; "
1512 "not relocating\n");
1513 }
1514 else
1515 {
1516 ULONGEST rip = orig_rip - insn_offset;
1517
1903f0e6
DE
1518 /* If we just stepped over a breakpoint insn, we don't backup
1519 the pc on purpose; this is to match behaviour without
1520 stepping. */
35669430
DE
1521
1522 regcache_cooked_write_unsigned (regs, AMD64_RIP_REGNUM, rip);
1523
1524 if (debug_displaced)
1525 fprintf_unfiltered (gdb_stdlog,
1526 "displaced: "
5af949e3
UW
1527 "relocated %%rip from %s to %s\n",
1528 paddress (gdbarch, orig_rip),
1529 paddress (gdbarch, rip));
35669430
DE
1530 }
1531 }
1532
1533 /* If the instruction was PUSHFL, then the TF bit will be set in the
1534 pushed value, and should be cleared. We'll leave this for later,
1535 since GDB already messes up the TF flag when stepping over a
1536 pushfl. */
1537
1538 /* If the instruction was a call, the return address now atop the
1539 stack is the address following the copied instruction. We need
1540 to make it the address following the original instruction. */
1541 if (amd64_call_p (insn_details))
1542 {
1543 ULONGEST rsp;
1544 ULONGEST retaddr;
1545 const ULONGEST retaddr_len = 8;
1546
1547 regcache_cooked_read_unsigned (regs, AMD64_RSP_REGNUM, &rsp);
e17a4113 1548 retaddr = read_memory_unsigned_integer (rsp, retaddr_len, byte_order);
35669430 1549 retaddr = (retaddr - insn_offset) & 0xffffffffUL;
e17a4113 1550 write_memory_unsigned_integer (rsp, retaddr_len, byte_order, retaddr);
35669430
DE
1551
1552 if (debug_displaced)
1553 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
1554 "displaced: relocated return addr at %s "
1555 "to %s\n",
1556 paddress (gdbarch, rsp),
1557 paddress (gdbarch, retaddr));
35669430
DE
1558 }
1559}
1560\f
c4f35dd8 1561/* The maximum number of saved registers. This should include %rip. */
90f90721 1562#define AMD64_NUM_SAVED_REGS AMD64_NUM_GREGS
c4f35dd8 1563
e53bef9f 1564struct amd64_frame_cache
c4f35dd8
MK
1565{
1566 /* Base address. */
1567 CORE_ADDR base;
1568 CORE_ADDR sp_offset;
1569 CORE_ADDR pc;
1570
1571 /* Saved registers. */
e53bef9f 1572 CORE_ADDR saved_regs[AMD64_NUM_SAVED_REGS];
c4f35dd8 1573 CORE_ADDR saved_sp;
e0c62198 1574 int saved_sp_reg;
c4f35dd8
MK
1575
1576 /* Do we have a frame? */
1577 int frameless_p;
1578};
8dda9770 1579
d2449ee8 1580/* Initialize a frame cache. */
c4f35dd8 1581
d2449ee8
DJ
1582static void
1583amd64_init_frame_cache (struct amd64_frame_cache *cache)
8dda9770 1584{
c4f35dd8
MK
1585 int i;
1586
c4f35dd8
MK
1587 /* Base address. */
1588 cache->base = 0;
1589 cache->sp_offset = -8;
1590 cache->pc = 0;
1591
1592 /* Saved registers. We initialize these to -1 since zero is a valid
bba66b87
DE
1593 offset (that's where %rbp is supposed to be stored).
1594 The values start out as being offsets, and are later converted to
1595 addresses (at which point -1 is interpreted as an address, still meaning
1596 "invalid"). */
e53bef9f 1597 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
c4f35dd8
MK
1598 cache->saved_regs[i] = -1;
1599 cache->saved_sp = 0;
e0c62198 1600 cache->saved_sp_reg = -1;
c4f35dd8
MK
1601
1602 /* Frameless until proven otherwise. */
1603 cache->frameless_p = 1;
d2449ee8 1604}
c4f35dd8 1605
d2449ee8
DJ
1606/* Allocate and initialize a frame cache. */
1607
1608static struct amd64_frame_cache *
1609amd64_alloc_frame_cache (void)
1610{
1611 struct amd64_frame_cache *cache;
1612
1613 cache = FRAME_OBSTACK_ZALLOC (struct amd64_frame_cache);
1614 amd64_init_frame_cache (cache);
c4f35dd8 1615 return cache;
8dda9770 1616}
53e95fcf 1617
e0c62198
L
1618/* GCC 4.4 and later, can put code in the prologue to realign the
1619 stack pointer. Check whether PC points to such code, and update
1620 CACHE accordingly. Return the first instruction after the code
1621 sequence or CURRENT_PC, whichever is smaller. If we don't
1622 recognize the code, return PC. */
1623
1624static CORE_ADDR
1625amd64_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
1626 struct amd64_frame_cache *cache)
1627{
1628 /* There are 2 code sequences to re-align stack before the frame
1629 gets set up:
1630
1631 1. Use a caller-saved saved register:
1632
1633 leaq 8(%rsp), %reg
1634 andq $-XXX, %rsp
1635 pushq -8(%reg)
1636
1637 2. Use a callee-saved saved register:
1638
1639 pushq %reg
1640 leaq 16(%rsp), %reg
1641 andq $-XXX, %rsp
1642 pushq -8(%reg)
1643
1644 "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
1645
1646 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
1647 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
1648 */
1649
1650 gdb_byte buf[18];
1651 int reg, r;
1652 int offset, offset_and;
e0c62198
L
1653
1654 if (target_read_memory (pc, buf, sizeof buf))
1655 return pc;
1656
1657 /* Check caller-saved saved register. The first instruction has
1658 to be "leaq 8(%rsp), %reg". */
1659 if ((buf[0] & 0xfb) == 0x48
1660 && buf[1] == 0x8d
1661 && buf[3] == 0x24
1662 && buf[4] == 0x8)
1663 {
1664 /* MOD must be binary 10 and R/M must be binary 100. */
1665 if ((buf[2] & 0xc7) != 0x44)
1666 return pc;
1667
1668 /* REG has register number. */
1669 reg = (buf[2] >> 3) & 7;
1670
1671 /* Check the REX.R bit. */
1672 if (buf[0] == 0x4c)
1673 reg += 8;
1674
1675 offset = 5;
1676 }
1677 else
1678 {
1679 /* Check callee-saved saved register. The first instruction
1680 has to be "pushq %reg". */
1681 reg = 0;
1682 if ((buf[0] & 0xf8) == 0x50)
1683 offset = 0;
1684 else if ((buf[0] & 0xf6) == 0x40
1685 && (buf[1] & 0xf8) == 0x50)
1686 {
1687 /* Check the REX.B bit. */
1688 if ((buf[0] & 1) != 0)
1689 reg = 8;
1690
1691 offset = 1;
1692 }
1693 else
1694 return pc;
1695
1696 /* Get register. */
1697 reg += buf[offset] & 0x7;
1698
1699 offset++;
1700
1701 /* The next instruction has to be "leaq 16(%rsp), %reg". */
1702 if ((buf[offset] & 0xfb) != 0x48
1703 || buf[offset + 1] != 0x8d
1704 || buf[offset + 3] != 0x24
1705 || buf[offset + 4] != 0x10)
1706 return pc;
1707
1708 /* MOD must be binary 10 and R/M must be binary 100. */
1709 if ((buf[offset + 2] & 0xc7) != 0x44)
1710 return pc;
1711
1712 /* REG has register number. */
1713 r = (buf[offset + 2] >> 3) & 7;
1714
1715 /* Check the REX.R bit. */
1716 if (buf[offset] == 0x4c)
1717 r += 8;
1718
1719 /* Registers in pushq and leaq have to be the same. */
1720 if (reg != r)
1721 return pc;
1722
1723 offset += 5;
1724 }
1725
1726 /* Rigister can't be %rsp nor %rbp. */
1727 if (reg == 4 || reg == 5)
1728 return pc;
1729
1730 /* The next instruction has to be "andq $-XXX, %rsp". */
1731 if (buf[offset] != 0x48
1732 || buf[offset + 2] != 0xe4
1733 || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
1734 return pc;
1735
1736 offset_and = offset;
1737 offset += buf[offset + 1] == 0x81 ? 7 : 4;
1738
1739 /* The next instruction has to be "pushq -8(%reg)". */
1740 r = 0;
1741 if (buf[offset] == 0xff)
1742 offset++;
1743 else if ((buf[offset] & 0xf6) == 0x40
1744 && buf[offset + 1] == 0xff)
1745 {
1746 /* Check the REX.B bit. */
1747 if ((buf[offset] & 0x1) != 0)
1748 r = 8;
1749 offset += 2;
1750 }
1751 else
1752 return pc;
1753
1754 /* 8bit -8 is 0xf8. REG must be binary 110 and MOD must be binary
1755 01. */
1756 if (buf[offset + 1] != 0xf8
1757 || (buf[offset] & 0xf8) != 0x70)
1758 return pc;
1759
1760 /* R/M has register. */
1761 r += buf[offset] & 7;
1762
1763 /* Registers in leaq and pushq have to be the same. */
1764 if (reg != r)
1765 return pc;
1766
1767 if (current_pc > pc + offset_and)
35669430 1768 cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
e0c62198
L
1769
1770 return min (pc + offset + 2, current_pc);
1771}
1772
c4f35dd8
MK
1773/* Do a limited analysis of the prologue at PC and update CACHE
1774 accordingly. Bail out early if CURRENT_PC is reached. Return the
1775 address where the analysis stopped.
1776
1777 We will handle only functions beginning with:
1778
1779 pushq %rbp 0x55
1780 movq %rsp, %rbp 0x48 0x89 0xe5
1781
1782 Any function that doesn't start with this sequence will be assumed
1783 to have no prologue and thus no valid frame pointer in %rbp. */
1784
1785static CORE_ADDR
e17a4113
UW
1786amd64_analyze_prologue (struct gdbarch *gdbarch,
1787 CORE_ADDR pc, CORE_ADDR current_pc,
e53bef9f 1788 struct amd64_frame_cache *cache)
53e95fcf 1789{
e17a4113 1790 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
d8de1ef7
MK
1791 static gdb_byte proto[3] = { 0x48, 0x89, 0xe5 }; /* movq %rsp, %rbp */
1792 gdb_byte buf[3];
1793 gdb_byte op;
c4f35dd8
MK
1794
1795 if (current_pc <= pc)
1796 return current_pc;
1797
e0c62198
L
1798 pc = amd64_analyze_stack_align (pc, current_pc, cache);
1799
e17a4113 1800 op = read_memory_unsigned_integer (pc, 1, byte_order);
c4f35dd8
MK
1801
1802 if (op == 0x55) /* pushq %rbp */
1803 {
1804 /* Take into account that we've executed the `pushq %rbp' that
1805 starts this instruction sequence. */
90f90721 1806 cache->saved_regs[AMD64_RBP_REGNUM] = 0;
c4f35dd8
MK
1807 cache->sp_offset += 8;
1808
1809 /* If that's all, return now. */
1810 if (current_pc <= pc + 1)
1811 return current_pc;
1812
1813 /* Check for `movq %rsp, %rbp'. */
1814 read_memory (pc + 1, buf, 3);
1815 if (memcmp (buf, proto, 3) != 0)
1816 return pc + 1;
1817
1818 /* OK, we actually have a frame. */
1819 cache->frameless_p = 0;
1820 return pc + 4;
1821 }
1822
1823 return pc;
53e95fcf
JS
1824}
1825
c4f35dd8
MK
1826/* Return PC of first real instruction. */
1827
1828static CORE_ADDR
6093d2eb 1829amd64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
53e95fcf 1830{
e53bef9f 1831 struct amd64_frame_cache cache;
c4f35dd8
MK
1832 CORE_ADDR pc;
1833
d2449ee8 1834 amd64_init_frame_cache (&cache);
e17a4113
UW
1835 pc = amd64_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffLL,
1836 &cache);
c4f35dd8
MK
1837 if (cache.frameless_p)
1838 return start_pc;
1839
1840 return pc;
53e95fcf 1841}
c4f35dd8 1842\f
53e95fcf 1843
c4f35dd8
MK
1844/* Normal frames. */
1845
e53bef9f 1846static struct amd64_frame_cache *
10458914 1847amd64_frame_cache (struct frame_info *this_frame, void **this_cache)
6d686a84 1848{
e17a4113
UW
1849 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1850 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e53bef9f 1851 struct amd64_frame_cache *cache;
d8de1ef7 1852 gdb_byte buf[8];
6d686a84 1853 int i;
6d686a84 1854
c4f35dd8
MK
1855 if (*this_cache)
1856 return *this_cache;
6d686a84 1857
e53bef9f 1858 cache = amd64_alloc_frame_cache ();
c4f35dd8
MK
1859 *this_cache = cache;
1860
10458914 1861 cache->pc = get_frame_func (this_frame);
c4f35dd8 1862 if (cache->pc != 0)
e17a4113
UW
1863 amd64_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
1864 cache);
c4f35dd8 1865
e0c62198
L
1866 if (cache->saved_sp_reg != -1)
1867 {
1868 /* Stack pointer has been saved. */
1869 get_frame_register (this_frame, cache->saved_sp_reg, buf);
e17a4113 1870 cache->saved_sp = extract_unsigned_integer(buf, 8, byte_order);
e0c62198
L
1871 }
1872
c4f35dd8
MK
1873 if (cache->frameless_p)
1874 {
4a28816e
MK
1875 /* We didn't find a valid frame. If we're at the start of a
1876 function, or somewhere half-way its prologue, the function's
1877 frame probably hasn't been fully setup yet. Try to
1878 reconstruct the base address for the stack frame by looking
1879 at the stack pointer. For truly "frameless" functions this
1880 might work too. */
c4f35dd8 1881
e0c62198
L
1882 if (cache->saved_sp_reg != -1)
1883 {
1884 /* We're halfway aligning the stack. */
1885 cache->base = ((cache->saved_sp - 8) & 0xfffffffffffffff0LL) - 8;
1886 cache->saved_regs[AMD64_RIP_REGNUM] = cache->saved_sp - 8;
1887
1888 /* This will be added back below. */
1889 cache->saved_regs[AMD64_RIP_REGNUM] -= cache->base;
1890 }
1891 else
1892 {
1893 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
e17a4113
UW
1894 cache->base = extract_unsigned_integer (buf, 8, byte_order)
1895 + cache->sp_offset;
e0c62198 1896 }
c4f35dd8 1897 }
35883a3f
MK
1898 else
1899 {
10458914 1900 get_frame_register (this_frame, AMD64_RBP_REGNUM, buf);
e17a4113 1901 cache->base = extract_unsigned_integer (buf, 8, byte_order);
35883a3f 1902 }
c4f35dd8
MK
1903
1904 /* Now that we have the base address for the stack frame we can
1905 calculate the value of %rsp in the calling frame. */
1906 cache->saved_sp = cache->base + 16;
1907
35883a3f
MK
1908 /* For normal frames, %rip is stored at 8(%rbp). If we don't have a
1909 frame we find it at the same offset from the reconstructed base
e0c62198
L
1910 address. If we're halfway aligning the stack, %rip is handled
1911 differently (see above). */
1912 if (!cache->frameless_p || cache->saved_sp_reg == -1)
1913 cache->saved_regs[AMD64_RIP_REGNUM] = 8;
35883a3f 1914
c4f35dd8
MK
1915 /* Adjust all the saved registers such that they contain addresses
1916 instead of offsets. */
e53bef9f 1917 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
c4f35dd8
MK
1918 if (cache->saved_regs[i] != -1)
1919 cache->saved_regs[i] += cache->base;
1920
1921 return cache;
6d686a84
ML
1922}
1923
c4f35dd8 1924static void
10458914 1925amd64_frame_this_id (struct frame_info *this_frame, void **this_cache,
e53bef9f 1926 struct frame_id *this_id)
c4f35dd8 1927{
e53bef9f 1928 struct amd64_frame_cache *cache =
10458914 1929 amd64_frame_cache (this_frame, this_cache);
c4f35dd8
MK
1930
1931 /* This marks the outermost frame. */
1932 if (cache->base == 0)
1933 return;
1934
1935 (*this_id) = frame_id_build (cache->base + 16, cache->pc);
1936}
e76e1718 1937
10458914
DJ
1938static struct value *
1939amd64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1940 int regnum)
53e95fcf 1941{
10458914 1942 struct gdbarch *gdbarch = get_frame_arch (this_frame);
e53bef9f 1943 struct amd64_frame_cache *cache =
10458914 1944 amd64_frame_cache (this_frame, this_cache);
e76e1718 1945
c4f35dd8 1946 gdb_assert (regnum >= 0);
b1ab997b 1947
2ae02b47 1948 if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
10458914 1949 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
e76e1718 1950
e53bef9f 1951 if (regnum < AMD64_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
10458914
DJ
1952 return frame_unwind_got_memory (this_frame, regnum,
1953 cache->saved_regs[regnum]);
e76e1718 1954
10458914 1955 return frame_unwind_got_register (this_frame, regnum, regnum);
c4f35dd8 1956}
e76e1718 1957
e53bef9f 1958static const struct frame_unwind amd64_frame_unwind =
c4f35dd8
MK
1959{
1960 NORMAL_FRAME,
e53bef9f 1961 amd64_frame_this_id,
10458914
DJ
1962 amd64_frame_prev_register,
1963 NULL,
1964 default_frame_sniffer
c4f35dd8 1965};
c4f35dd8 1966\f
e76e1718 1967
c4f35dd8
MK
1968/* Signal trampolines. */
1969
1970/* FIXME: kettenis/20030419: Perhaps, we can unify the 32-bit and
1971 64-bit variants. This would require using identical frame caches
1972 on both platforms. */
1973
e53bef9f 1974static struct amd64_frame_cache *
10458914 1975amd64_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
c4f35dd8 1976{
e17a4113
UW
1977 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1978 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1979 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e53bef9f 1980 struct amd64_frame_cache *cache;
c4f35dd8 1981 CORE_ADDR addr;
d8de1ef7 1982 gdb_byte buf[8];
2b5e0749 1983 int i;
c4f35dd8
MK
1984
1985 if (*this_cache)
1986 return *this_cache;
1987
e53bef9f 1988 cache = amd64_alloc_frame_cache ();
c4f35dd8 1989
10458914 1990 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
e17a4113 1991 cache->base = extract_unsigned_integer (buf, 8, byte_order) - 8;
c4f35dd8 1992
10458914 1993 addr = tdep->sigcontext_addr (this_frame);
2b5e0749 1994 gdb_assert (tdep->sc_reg_offset);
e53bef9f 1995 gdb_assert (tdep->sc_num_regs <= AMD64_NUM_SAVED_REGS);
2b5e0749
MK
1996 for (i = 0; i < tdep->sc_num_regs; i++)
1997 if (tdep->sc_reg_offset[i] != -1)
1998 cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
c4f35dd8
MK
1999
2000 *this_cache = cache;
2001 return cache;
53e95fcf
JS
2002}
2003
c4f35dd8 2004static void
10458914 2005amd64_sigtramp_frame_this_id (struct frame_info *this_frame,
e53bef9f 2006 void **this_cache, struct frame_id *this_id)
c4f35dd8 2007{
e53bef9f 2008 struct amd64_frame_cache *cache =
10458914 2009 amd64_sigtramp_frame_cache (this_frame, this_cache);
c4f35dd8 2010
10458914 2011 (*this_id) = frame_id_build (cache->base + 16, get_frame_pc (this_frame));
c4f35dd8
MK
2012}
2013
10458914
DJ
2014static struct value *
2015amd64_sigtramp_frame_prev_register (struct frame_info *this_frame,
2016 void **this_cache, int regnum)
c4f35dd8
MK
2017{
2018 /* Make sure we've initialized the cache. */
10458914 2019 amd64_sigtramp_frame_cache (this_frame, this_cache);
c4f35dd8 2020
10458914 2021 return amd64_frame_prev_register (this_frame, this_cache, regnum);
c4f35dd8
MK
2022}
2023
10458914
DJ
2024static int
2025amd64_sigtramp_frame_sniffer (const struct frame_unwind *self,
2026 struct frame_info *this_frame,
2027 void **this_cache)
c4f35dd8 2028{
10458914 2029 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
911bc6ee
MK
2030
2031 /* We shouldn't even bother if we don't have a sigcontext_addr
2032 handler. */
2033 if (tdep->sigcontext_addr == NULL)
10458914 2034 return 0;
911bc6ee
MK
2035
2036 if (tdep->sigtramp_p != NULL)
2037 {
10458914
DJ
2038 if (tdep->sigtramp_p (this_frame))
2039 return 1;
911bc6ee 2040 }
c4f35dd8 2041
911bc6ee 2042 if (tdep->sigtramp_start != 0)
1c3545ae 2043 {
10458914 2044 CORE_ADDR pc = get_frame_pc (this_frame);
1c3545ae 2045
911bc6ee
MK
2046 gdb_assert (tdep->sigtramp_end != 0);
2047 if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
10458914 2048 return 1;
1c3545ae 2049 }
c4f35dd8 2050
10458914 2051 return 0;
c4f35dd8 2052}
10458914
DJ
2053
2054static const struct frame_unwind amd64_sigtramp_frame_unwind =
2055{
2056 SIGTRAMP_FRAME,
2057 amd64_sigtramp_frame_this_id,
2058 amd64_sigtramp_frame_prev_register,
2059 NULL,
2060 amd64_sigtramp_frame_sniffer
2061};
c4f35dd8
MK
2062\f
2063
2064static CORE_ADDR
10458914 2065amd64_frame_base_address (struct frame_info *this_frame, void **this_cache)
c4f35dd8 2066{
e53bef9f 2067 struct amd64_frame_cache *cache =
10458914 2068 amd64_frame_cache (this_frame, this_cache);
c4f35dd8
MK
2069
2070 return cache->base;
2071}
2072
e53bef9f 2073static const struct frame_base amd64_frame_base =
c4f35dd8 2074{
e53bef9f
MK
2075 &amd64_frame_unwind,
2076 amd64_frame_base_address,
2077 amd64_frame_base_address,
2078 amd64_frame_base_address
c4f35dd8
MK
2079};
2080
872761f4
MS
2081/* Normal frames, but in a function epilogue. */
2082
2083/* The epilogue is defined here as the 'ret' instruction, which will
2084 follow any instruction such as 'leave' or 'pop %ebp' that destroys
2085 the function's stack frame. */
2086
2087static int
2088amd64_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
2089{
2090 gdb_byte insn;
2091
2092 if (target_read_memory (pc, &insn, 1))
2093 return 0; /* Can't read memory at pc. */
2094
2095 if (insn != 0xc3) /* 'ret' instruction. */
2096 return 0;
2097
2098 return 1;
2099}
2100
2101static int
2102amd64_epilogue_frame_sniffer (const struct frame_unwind *self,
2103 struct frame_info *this_frame,
2104 void **this_prologue_cache)
2105{
2106 if (frame_relative_level (this_frame) == 0)
2107 return amd64_in_function_epilogue_p (get_frame_arch (this_frame),
2108 get_frame_pc (this_frame));
2109 else
2110 return 0;
2111}
2112
2113static struct amd64_frame_cache *
2114amd64_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
2115{
2116 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2117 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2118 struct amd64_frame_cache *cache;
6c10c06b 2119 gdb_byte buf[8];
872761f4
MS
2120
2121 if (*this_cache)
2122 return *this_cache;
2123
2124 cache = amd64_alloc_frame_cache ();
2125 *this_cache = cache;
2126
2127 /* Cache base will be %esp plus cache->sp_offset (-8). */
2128 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2129 cache->base = extract_unsigned_integer (buf, 8,
2130 byte_order) + cache->sp_offset;
2131
2132 /* Cache pc will be the frame func. */
2133 cache->pc = get_frame_pc (this_frame);
2134
2135 /* The saved %esp will be at cache->base plus 16. */
2136 cache->saved_sp = cache->base + 16;
2137
2138 /* The saved %eip will be at cache->base plus 8. */
2139 cache->saved_regs[AMD64_RIP_REGNUM] = cache->base + 8;
2140
2141 return cache;
2142}
2143
2144static void
2145amd64_epilogue_frame_this_id (struct frame_info *this_frame,
2146 void **this_cache,
2147 struct frame_id *this_id)
2148{
2149 struct amd64_frame_cache *cache = amd64_epilogue_frame_cache (this_frame,
2150 this_cache);
2151
2152 (*this_id) = frame_id_build (cache->base + 8, cache->pc);
2153}
2154
2155static const struct frame_unwind amd64_epilogue_frame_unwind =
2156{
2157 NORMAL_FRAME,
2158 amd64_epilogue_frame_this_id,
2159 amd64_frame_prev_register,
2160 NULL,
2161 amd64_epilogue_frame_sniffer
2162};
2163
166f4c7b 2164static struct frame_id
10458914 2165amd64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
166f4c7b 2166{
c4f35dd8
MK
2167 CORE_ADDR fp;
2168
10458914 2169 fp = get_frame_register_unsigned (this_frame, AMD64_RBP_REGNUM);
c4f35dd8 2170
10458914 2171 return frame_id_build (fp + 16, get_frame_pc (this_frame));
166f4c7b
ML
2172}
2173
8b148df9
AC
2174/* 16 byte align the SP per frame requirements. */
2175
2176static CORE_ADDR
e53bef9f 2177amd64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
8b148df9
AC
2178{
2179 return sp & -(CORE_ADDR)16;
2180}
473f17b0
MK
2181\f
2182
593adc23
MK
2183/* Supply register REGNUM from the buffer specified by FPREGS and LEN
2184 in the floating-point register set REGSET to register cache
2185 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
473f17b0
MK
2186
2187static void
e53bef9f
MK
2188amd64_supply_fpregset (const struct regset *regset, struct regcache *regcache,
2189 int regnum, const void *fpregs, size_t len)
473f17b0 2190{
9ea75c57 2191 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
473f17b0
MK
2192
2193 gdb_assert (len == tdep->sizeof_fpregset);
90f90721 2194 amd64_supply_fxsave (regcache, regnum, fpregs);
473f17b0 2195}
8b148df9 2196
593adc23
MK
2197/* Collect register REGNUM from the register cache REGCACHE and store
2198 it in the buffer specified by FPREGS and LEN as described by the
2199 floating-point register set REGSET. If REGNUM is -1, do this for
2200 all registers in REGSET. */
2201
2202static void
2203amd64_collect_fpregset (const struct regset *regset,
2204 const struct regcache *regcache,
2205 int regnum, void *fpregs, size_t len)
2206{
2207 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2208
2209 gdb_assert (len == tdep->sizeof_fpregset);
2210 amd64_collect_fxsave (regcache, regnum, fpregs);
2211}
2212
a055a187
L
2213/* Similar to amd64_supply_fpregset, but use XSAVE extended state. */
2214
2215static void
2216amd64_supply_xstateregset (const struct regset *regset,
2217 struct regcache *regcache, int regnum,
2218 const void *xstateregs, size_t len)
2219{
a055a187
L
2220 amd64_supply_xsave (regcache, regnum, xstateregs);
2221}
2222
2223/* Similar to amd64_collect_fpregset, but use XSAVE extended state. */
2224
2225static void
2226amd64_collect_xstateregset (const struct regset *regset,
2227 const struct regcache *regcache,
2228 int regnum, void *xstateregs, size_t len)
2229{
a055a187
L
2230 amd64_collect_xsave (regcache, regnum, xstateregs, 1);
2231}
2232
c6b33596
MK
2233/* Return the appropriate register set for the core section identified
2234 by SECT_NAME and SECT_SIZE. */
2235
2236static const struct regset *
e53bef9f
MK
2237amd64_regset_from_core_section (struct gdbarch *gdbarch,
2238 const char *sect_name, size_t sect_size)
c6b33596
MK
2239{
2240 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2241
2242 if (strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
2243 {
2244 if (tdep->fpregset == NULL)
593adc23
MK
2245 tdep->fpregset = regset_alloc (gdbarch, amd64_supply_fpregset,
2246 amd64_collect_fpregset);
c6b33596
MK
2247
2248 return tdep->fpregset;
2249 }
2250
a055a187
L
2251 if (strcmp (sect_name, ".reg-xstate") == 0)
2252 {
2253 if (tdep->xstateregset == NULL)
2254 tdep->xstateregset = regset_alloc (gdbarch,
2255 amd64_supply_xstateregset,
2256 amd64_collect_xstateregset);
2257
2258 return tdep->xstateregset;
2259 }
2260
c6b33596
MK
2261 return i386_regset_from_core_section (gdbarch, sect_name, sect_size);
2262}
2263\f
2264
436675d3
PA
2265/* Figure out where the longjmp will land. Slurp the jmp_buf out of
2266 %rdi. We expect its value to be a pointer to the jmp_buf structure
2267 from which we extract the address that we will land at. This
2268 address is copied into PC. This routine returns non-zero on
2269 success. */
2270
2271static int
2272amd64_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
2273{
2274 gdb_byte buf[8];
2275 CORE_ADDR jb_addr;
2276 struct gdbarch *gdbarch = get_frame_arch (frame);
2277 int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset;
0dfff4cb 2278 int len = TYPE_LENGTH (builtin_type (gdbarch)->builtin_func_ptr);
436675d3
PA
2279
2280 /* If JB_PC_OFFSET is -1, we have no way to find out where the
2281 longjmp will land. */
2282 if (jb_pc_offset == -1)
2283 return 0;
2284
2285 get_frame_register (frame, AMD64_RDI_REGNUM, buf);
0dfff4cb
UW
2286 jb_addr= extract_typed_address
2287 (buf, builtin_type (gdbarch)->builtin_data_ptr);
436675d3
PA
2288 if (target_read_memory (jb_addr + jb_pc_offset, buf, len))
2289 return 0;
2290
0dfff4cb 2291 *pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
436675d3
PA
2292
2293 return 1;
2294}
2295
cf648174
HZ
2296static const int amd64_record_regmap[] =
2297{
2298 AMD64_RAX_REGNUM, AMD64_RCX_REGNUM, AMD64_RDX_REGNUM, AMD64_RBX_REGNUM,
2299 AMD64_RSP_REGNUM, AMD64_RBP_REGNUM, AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
2300 AMD64_R8_REGNUM, AMD64_R9_REGNUM, AMD64_R10_REGNUM, AMD64_R11_REGNUM,
2301 AMD64_R12_REGNUM, AMD64_R13_REGNUM, AMD64_R14_REGNUM, AMD64_R15_REGNUM,
2302 AMD64_RIP_REGNUM, AMD64_EFLAGS_REGNUM, AMD64_CS_REGNUM, AMD64_SS_REGNUM,
2303 AMD64_DS_REGNUM, AMD64_ES_REGNUM, AMD64_FS_REGNUM, AMD64_GS_REGNUM
2304};
2305
2213a65d 2306void
90f90721 2307amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
53e95fcf 2308{
0c1a73d6 2309 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
90884b2b 2310 const struct target_desc *tdesc = info.target_desc;
53e95fcf 2311
473f17b0
MK
2312 /* AMD64 generally uses `fxsave' instead of `fsave' for saving its
2313 floating-point registers. */
2314 tdep->sizeof_fpregset = I387_SIZEOF_FXSAVE;
2315
90884b2b
L
2316 if (! tdesc_has_registers (tdesc))
2317 tdesc = tdesc_amd64;
2318 tdep->tdesc = tdesc;
2319
2320 tdep->num_core_regs = AMD64_NUM_GREGS + I387_NUM_REGS;
2321 tdep->register_names = amd64_register_names;
2322
a055a187
L
2323 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx") != NULL)
2324 {
2325 tdep->ymmh_register_names = amd64_ymmh_names;
2326 tdep->num_ymm_regs = 16;
2327 tdep->ymm0h_regnum = AMD64_YMM0H_REGNUM;
2328 }
2329
fe01d668 2330 tdep->num_byte_regs = 20;
1ba53b71
L
2331 tdep->num_word_regs = 16;
2332 tdep->num_dword_regs = 16;
2333 /* Avoid wiring in the MMX registers for now. */
2334 tdep->num_mmx_regs = 0;
2335
2336 set_gdbarch_pseudo_register_read (gdbarch,
2337 amd64_pseudo_register_read);
2338 set_gdbarch_pseudo_register_write (gdbarch,
2339 amd64_pseudo_register_write);
2340
2341 set_tdesc_pseudo_register_name (gdbarch, amd64_pseudo_register_name);
2342
a055a187
L
2343 set_gdbarch_register_name (gdbarch, amd64_register_name);
2344
5716833c 2345 /* AMD64 has an FPU and 16 SSE registers. */
90f90721 2346 tdep->st0_regnum = AMD64_ST0_REGNUM;
0c1a73d6 2347 tdep->num_xmm_regs = 16;
53e95fcf 2348
0c1a73d6 2349 /* This is what all the fuss is about. */
53e95fcf
JS
2350 set_gdbarch_long_bit (gdbarch, 64);
2351 set_gdbarch_long_long_bit (gdbarch, 64);
2352 set_gdbarch_ptr_bit (gdbarch, 64);
2353
e53bef9f
MK
2354 /* In contrast to the i386, on AMD64 a `long double' actually takes
2355 up 128 bits, even though it's still based on the i387 extended
2356 floating-point format which has only 80 significant bits. */
b83b026c
MK
2357 set_gdbarch_long_double_bit (gdbarch, 128);
2358
e53bef9f 2359 set_gdbarch_num_regs (gdbarch, AMD64_NUM_REGS);
b83b026c
MK
2360
2361 /* Register numbers of various important registers. */
90f90721
MK
2362 set_gdbarch_sp_regnum (gdbarch, AMD64_RSP_REGNUM); /* %rsp */
2363 set_gdbarch_pc_regnum (gdbarch, AMD64_RIP_REGNUM); /* %rip */
2364 set_gdbarch_ps_regnum (gdbarch, AMD64_EFLAGS_REGNUM); /* %eflags */
2365 set_gdbarch_fp0_regnum (gdbarch, AMD64_ST0_REGNUM); /* %st(0) */
b83b026c 2366
e53bef9f
MK
2367 /* The "default" register numbering scheme for AMD64 is referred to
2368 as the "DWARF Register Number Mapping" in the System V psABI.
2369 The preferred debugging format for all known AMD64 targets is
2370 actually DWARF2, and GCC doesn't seem to support DWARF (that is
2371 DWARF-1), but we provide the same mapping just in case. This
2372 mapping is also used for stabs, which GCC does support. */
2373 set_gdbarch_stab_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
e53bef9f 2374 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
de220d0f 2375
c4f35dd8 2376 /* We don't override SDB_REG_RO_REGNUM, since COFF doesn't seem to
e53bef9f 2377 be in use on any of the supported AMD64 targets. */
53e95fcf 2378
c4f35dd8 2379 /* Call dummy code. */
e53bef9f
MK
2380 set_gdbarch_push_dummy_call (gdbarch, amd64_push_dummy_call);
2381 set_gdbarch_frame_align (gdbarch, amd64_frame_align);
8b148df9 2382 set_gdbarch_frame_red_zone_size (gdbarch, 128);
ba581dc1
JB
2383 tdep->call_dummy_num_integer_regs =
2384 ARRAY_SIZE (amd64_dummy_call_integer_regs);
2385 tdep->call_dummy_integer_regs = amd64_dummy_call_integer_regs;
2386 tdep->classify = amd64_classify;
53e95fcf 2387
83acabca 2388 set_gdbarch_convert_register_p (gdbarch, i387_convert_register_p);
d532c08f
MK
2389 set_gdbarch_register_to_value (gdbarch, i387_register_to_value);
2390 set_gdbarch_value_to_register (gdbarch, i387_value_to_register);
2391
efb1c01c 2392 set_gdbarch_return_value (gdbarch, amd64_return_value);
53e95fcf 2393
e53bef9f 2394 set_gdbarch_skip_prologue (gdbarch, amd64_skip_prologue);
53e95fcf 2395
cf648174
HZ
2396 tdep->record_regmap = amd64_record_regmap;
2397
10458914 2398 set_gdbarch_dummy_id (gdbarch, amd64_dummy_id);
53e95fcf 2399
872761f4
MS
2400 /* Hook the function epilogue frame unwinder. This unwinder is
2401 appended to the list first, so that it supercedes the other
2402 unwinders in function epilogues. */
2403 frame_unwind_prepend_unwinder (gdbarch, &amd64_epilogue_frame_unwind);
2404
2405 /* Hook the prologue-based frame unwinders. */
10458914
DJ
2406 frame_unwind_append_unwinder (gdbarch, &amd64_sigtramp_frame_unwind);
2407 frame_unwind_append_unwinder (gdbarch, &amd64_frame_unwind);
e53bef9f 2408 frame_base_set_default (gdbarch, &amd64_frame_base);
c6b33596
MK
2409
2410 /* If we have a register mapping, enable the generic core file support. */
2411 if (tdep->gregset_reg_offset)
2412 set_gdbarch_regset_from_core_section (gdbarch,
e53bef9f 2413 amd64_regset_from_core_section);
436675d3
PA
2414
2415 set_gdbarch_get_longjmp_target (gdbarch, amd64_get_longjmp_target);
c4f35dd8 2416}
90884b2b
L
2417
2418/* Provide a prototype to silence -Wmissing-prototypes. */
2419void _initialize_amd64_tdep (void);
2420
2421void
2422_initialize_amd64_tdep (void)
2423{
2424 initialize_tdesc_amd64 ();
a055a187 2425 initialize_tdesc_amd64_avx ();
90884b2b 2426}
c4f35dd8
MK
2427\f
2428
41d041d6
MK
2429/* The 64-bit FXSAVE format differs from the 32-bit format in the
2430 sense that the instruction pointer and data pointer are simply
2431 64-bit offsets into the code segment and the data segment instead
2432 of a selector offset pair. The functions below store the upper 32
2433 bits of these pointers (instead of just the 16-bits of the segment
2434 selector). */
2435
2436/* Fill register REGNUM in REGCACHE with the appropriate
0485f6ad
MK
2437 floating-point or SSE register value from *FXSAVE. If REGNUM is
2438 -1, do this for all registers. This function masks off any of the
2439 reserved bits in *FXSAVE. */
c4f35dd8
MK
2440
2441void
90f90721 2442amd64_supply_fxsave (struct regcache *regcache, int regnum,
20a6ec49 2443 const void *fxsave)
c4f35dd8 2444{
20a6ec49
MD
2445 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2446 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2447
41d041d6 2448 i387_supply_fxsave (regcache, regnum, fxsave);
c4f35dd8 2449
20a6ec49 2450 if (fxsave && gdbarch_ptr_bit (gdbarch) == 64)
c4f35dd8 2451 {
d8de1ef7 2452 const gdb_byte *regs = fxsave;
41d041d6 2453
20a6ec49
MD
2454 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
2455 regcache_raw_supply (regcache, I387_FISEG_REGNUM (tdep), regs + 12);
2456 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
2457 regcache_raw_supply (regcache, I387_FOSEG_REGNUM (tdep), regs + 20);
c4f35dd8 2458 }
0c1a73d6
MK
2459}
2460
a055a187
L
2461/* Similar to amd64_supply_fxsave, but use XSAVE extended state. */
2462
2463void
2464amd64_supply_xsave (struct regcache *regcache, int regnum,
2465 const void *xsave)
2466{
2467 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2468 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2469
2470 i387_supply_xsave (regcache, regnum, xsave);
2471
2472 if (xsave && gdbarch_ptr_bit (gdbarch) == 64)
2473 {
2474 const gdb_byte *regs = xsave;
2475
2476 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
2477 regcache_raw_supply (regcache, I387_FISEG_REGNUM (tdep),
2478 regs + 12);
2479 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
2480 regcache_raw_supply (regcache, I387_FOSEG_REGNUM (tdep),
2481 regs + 20);
2482 }
2483}
2484
3c017e40
MK
2485/* Fill register REGNUM (if it is a floating-point or SSE register) in
2486 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
2487 all registers. This function doesn't touch any of the reserved
2488 bits in *FXSAVE. */
2489
2490void
2491amd64_collect_fxsave (const struct regcache *regcache, int regnum,
2492 void *fxsave)
2493{
20a6ec49
MD
2494 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2495 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
d8de1ef7 2496 gdb_byte *regs = fxsave;
3c017e40
MK
2497
2498 i387_collect_fxsave (regcache, regnum, fxsave);
2499
20a6ec49 2500 if (gdbarch_ptr_bit (gdbarch) == 64)
f0ef85a5 2501 {
20a6ec49
MD
2502 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
2503 regcache_raw_collect (regcache, I387_FISEG_REGNUM (tdep), regs + 12);
2504 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
2505 regcache_raw_collect (regcache, I387_FOSEG_REGNUM (tdep), regs + 20);
f0ef85a5 2506 }
3c017e40 2507}
a055a187
L
2508
2509/* Similar to amd64_collect_fxsave, but but use XSAVE extended state. */
2510
2511void
2512amd64_collect_xsave (const struct regcache *regcache, int regnum,
2513 void *xsave, int gcore)
2514{
2515 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2516 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2517 gdb_byte *regs = xsave;
2518
2519 i387_collect_xsave (regcache, regnum, xsave, gcore);
2520
2521 if (gdbarch_ptr_bit (gdbarch) == 64)
2522 {
2523 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
2524 regcache_raw_collect (regcache, I387_FISEG_REGNUM (tdep),
2525 regs + 12);
2526 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
2527 regcache_raw_collect (regcache, I387_FOSEG_REGNUM (tdep),
2528 regs + 20);
2529 }
2530}
This page took 0.753663 seconds and 4 git commands to generate.