1 /* GNU/Linux/x86-64 specific low level interface, for the in-process
4 Copyright (C) 2010-2023 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "tracepoint.h"
24 #include "linux-x86-tdesc.h"
25 #include "gdbsupport/x86-xstate.h"
27 /* Defined in auto-generated file amd64-linux.c. */
28 void init_registers_amd64_linux (void);
29 extern const struct target_desc
*tdesc_amd64_linux
;
31 /* fast tracepoints collect registers. */
34 #define FT_CR_EFLAGS 1
52 static const int x86_64_ft_collect_regmap
[] = {
53 FT_CR_RAX
* 8, FT_CR_RBX
* 8, FT_CR_RCX
* 8, FT_CR_RDX
* 8,
54 FT_CR_RSI
* 8, FT_CR_RDI
* 8, FT_CR_RBP
* 8, FT_CR_RSP
* 8,
55 FT_CR_R8
* 8, FT_CR_R9
* 8, FT_CR_R10
* 8, FT_CR_R11
* 8,
56 FT_CR_R12
* 8, FT_CR_R13
* 8, FT_CR_R14
* 8, FT_CR_R15
* 8,
57 FT_CR_RIP
* 8, FT_CR_EFLAGS
* 8
60 #define X86_64_NUM_FT_COLLECT_GREGS \
61 (sizeof (x86_64_ft_collect_regmap) / sizeof(x86_64_ft_collect_regmap[0]))
64 supply_fast_tracepoint_registers (struct regcache
*regcache
,
65 const unsigned char *buf
)
69 for (i
= 0; i
< X86_64_NUM_FT_COLLECT_GREGS
; i
++)
70 supply_register (regcache
, i
,
71 ((char *) buf
) + x86_64_ft_collect_regmap
[i
]);
75 get_raw_reg (const unsigned char *raw_regs
, int regnum
)
77 if (regnum
>= X86_64_NUM_FT_COLLECT_GREGS
)
80 return *(ULONGEST
*) (raw_regs
+ x86_64_ft_collect_regmap
[regnum
]);
85 #include <ust/processor.h>
87 /* "struct registers" is the UST object type holding the registers at
88 the time of the static tracepoint marker call. This doesn't
89 contain RIP, but we know what it must have been (the marker
92 #define ST_REGENTRY(REG) \
94 offsetof (struct registers, REG), \
95 sizeof (((struct registers *) NULL)->REG) \
102 } x86_64_st_collect_regmap
[] =
126 #define X86_64_NUM_ST_COLLECT_GREGS \
127 (sizeof (x86_64_st_collect_regmap) / sizeof (x86_64_st_collect_regmap[0]))
129 /* GDB's RIP register number. */
130 #define AMD64_RIP_REGNUM 16
133 supply_static_tracepoint_registers (struct regcache
*regcache
,
134 const unsigned char *buf
,
138 unsigned long newpc
= pc
;
140 supply_register (regcache
, AMD64_RIP_REGNUM
, &newpc
);
142 for (i
= 0; i
< X86_64_NUM_ST_COLLECT_GREGS
; i
++)
143 if (x86_64_st_collect_regmap
[i
].offset
!= -1)
145 switch (x86_64_st_collect_regmap
[i
].size
)
148 supply_register (regcache
, i
,
150 + x86_64_st_collect_regmap
[i
].offset
);
155 = * (short *) (((char *) buf
)
156 + x86_64_st_collect_regmap
[i
].offset
);
158 supply_register (regcache
, i
, ®
);
162 internal_error ("unhandled register size: %d",
163 x86_64_st_collect_regmap
[i
].size
);
169 #endif /* HAVE_UST */
171 #if !defined __ILP32__
172 /* Map the tdesc index to xcr0 mask. */
173 static uint64_t idx2mask
[X86_TDESC_LAST
] = {
178 X86_XSTATE_AVX_MPX_MASK
,
179 X86_XSTATE_AVX_AVX512_MASK
,
180 X86_XSTATE_AVX_MPX_AVX512_PKU_MASK
,
184 /* Return target_desc to use for IPA, given the tdesc index passed by
187 const struct target_desc
*
188 get_ipa_tdesc (int idx
)
190 if (idx
>= X86_TDESC_LAST
)
192 internal_error ("unknown ipa tdesc index: %d", idx
);
195 #if defined __ILP32__
199 return amd64_linux_read_description (X86_XSTATE_SSE_MASK
, true);
201 return amd64_linux_read_description (X86_XSTATE_AVX_MASK
, true);
202 case X86_TDESC_AVX_AVX512
:
203 return amd64_linux_read_description (X86_XSTATE_AVX_AVX512_MASK
, true);
208 return amd64_linux_read_description (idx2mask
[idx
], false);
211 internal_error ("unknown ipa tdesc index: %d", idx
);
214 /* Allocate buffer for the jump pads. The branch instruction has a
215 reach of +/- 31-bit, and the executable is loaded at low addresses.
217 64-bit: Use MAP_32BIT to allocate in the first 2GB. Shared
218 libraries, being allocated at the top, are unfortunately out of
221 x32: Since MAP_32BIT is 64-bit only, do the placement manually.
222 Try allocating at '0x80000000 - SIZE' initially, decreasing until
223 we hit a free area. This ensures the executable is fully covered,
224 and is as close as possible to the shared libraries, which are
225 usually mapped at the top of the first 4GB of the address space.
229 alloc_jump_pad_buffer (size_t size
)
235 pagesize
= sysconf (_SC_PAGE_SIZE
);
237 perror_with_name ("sysconf");
239 addr
= 0x80000000 - size
;
241 /* size should already be page-aligned, but this can't hurt. */
242 addr
&= ~(pagesize
- 1);
244 /* Search for a free area. If we hit 0, we're out of luck. */
245 for (; addr
; addr
-= pagesize
)
249 /* No MAP_FIXED - we don't want to zap someone's mapping. */
250 res
= mmap ((void *) addr
, size
,
251 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
252 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
254 /* If we got what we wanted, return. */
255 if ((uintptr_t) res
== addr
)
258 /* If we got a mapping, but at a wrong address, undo it. */
259 if (res
!= MAP_FAILED
)
265 void *res
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
266 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_32BIT
, -1, 0);
268 if (res
== MAP_FAILED
)
276 initialize_low_tracepoint (void)
278 #if defined __ILP32__
279 amd64_linux_read_description (X86_XSTATE_SSE_MASK
, true);
280 amd64_linux_read_description (X86_XSTATE_AVX_MASK
, true);
281 amd64_linux_read_description (X86_XSTATE_AVX_AVX512_MASK
, true);
283 for (auto i
= 0; i
< X86_TDESC_LAST
; i
++)
284 amd64_linux_read_description (idx2mask
[i
], false);