1 /* GNU/Linux/x86-64 specific low level interface, for the in-process
4 Copyright (C) 2010-2024 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/>. */
22 #include "tracepoint.h"
23 #include "gdbsupport/x86-xstate.h"
24 #include "arch/amd64-linux-tdesc.h"
25 #include "arch/x86-linux-tdesc-features.h"
27 /* fast tracepoints collect registers. */
30 #define FT_CR_EFLAGS 1
48 static const int x86_64_ft_collect_regmap
[] = {
49 FT_CR_RAX
* 8, FT_CR_RBX
* 8, FT_CR_RCX
* 8, FT_CR_RDX
* 8,
50 FT_CR_RSI
* 8, FT_CR_RDI
* 8, FT_CR_RBP
* 8, FT_CR_RSP
* 8,
51 FT_CR_R8
* 8, FT_CR_R9
* 8, FT_CR_R10
* 8, FT_CR_R11
* 8,
52 FT_CR_R12
* 8, FT_CR_R13
* 8, FT_CR_R14
* 8, FT_CR_R15
* 8,
53 FT_CR_RIP
* 8, FT_CR_EFLAGS
* 8
56 #define X86_64_NUM_FT_COLLECT_GREGS \
57 (sizeof (x86_64_ft_collect_regmap) / sizeof(x86_64_ft_collect_regmap[0]))
60 supply_fast_tracepoint_registers (struct regcache
*regcache
,
61 const unsigned char *buf
)
65 for (i
= 0; i
< X86_64_NUM_FT_COLLECT_GREGS
; i
++)
66 supply_register (regcache
, i
,
67 ((char *) buf
) + x86_64_ft_collect_regmap
[i
]);
71 get_raw_reg (const unsigned char *raw_regs
, int regnum
)
73 if (regnum
>= X86_64_NUM_FT_COLLECT_GREGS
)
76 return *(ULONGEST
*) (raw_regs
+ x86_64_ft_collect_regmap
[regnum
]);
81 #include <ust/processor.h>
83 /* "struct registers" is the UST object type holding the registers at
84 the time of the static tracepoint marker call. This doesn't
85 contain RIP, but we know what it must have been (the marker
88 #define ST_REGENTRY(REG) \
90 offsetof (struct registers, REG), \
91 sizeof (((struct registers *) NULL)->REG) \
98 } x86_64_st_collect_regmap
[] =
122 #define X86_64_NUM_ST_COLLECT_GREGS \
123 (sizeof (x86_64_st_collect_regmap) / sizeof (x86_64_st_collect_regmap[0]))
125 /* GDB's RIP register number. */
126 #define AMD64_RIP_REGNUM 16
129 supply_static_tracepoint_registers (struct regcache
*regcache
,
130 const unsigned char *buf
,
134 unsigned long newpc
= pc
;
136 supply_register (regcache
, AMD64_RIP_REGNUM
, &newpc
);
138 for (i
= 0; i
< X86_64_NUM_ST_COLLECT_GREGS
; i
++)
139 if (x86_64_st_collect_regmap
[i
].offset
!= -1)
141 switch (x86_64_st_collect_regmap
[i
].size
)
144 supply_register (regcache
, i
,
146 + x86_64_st_collect_regmap
[i
].offset
);
151 = * (short *) (((char *) buf
)
152 + x86_64_st_collect_regmap
[i
].offset
);
154 supply_register (regcache
, i
, ®
);
158 internal_error ("unhandled register size: %d",
159 x86_64_st_collect_regmap
[i
].size
);
165 #endif /* HAVE_UST */
167 /* Return target_desc to use for IPA, given the tdesc index passed by
170 const struct target_desc
*
171 get_ipa_tdesc (int idx
)
173 uint64_t xcr0
= x86_linux_tdesc_idx_to_xcr0 (idx
);
175 #if defined __ILP32__
181 return amd64_linux_read_description (xcr0
, is_x32
);
184 /* Allocate buffer for the jump pads. The branch instruction has a
185 reach of +/- 31-bit, and the executable is loaded at low addresses.
187 64-bit: Use MAP_32BIT to allocate in the first 2GB. Shared
188 libraries, being allocated at the top, are unfortunately out of
191 x32: Since MAP_32BIT is 64-bit only, do the placement manually.
192 Try allocating at '0x80000000 - SIZE' initially, decreasing until
193 we hit a free area. This ensures the executable is fully covered,
194 and is as close as possible to the shared libraries, which are
195 usually mapped at the top of the first 4GB of the address space.
199 alloc_jump_pad_buffer (size_t size
)
205 pagesize
= sysconf (_SC_PAGE_SIZE
);
207 perror_with_name ("sysconf");
209 addr
= 0x80000000 - size
;
211 /* size should already be page-aligned, but this can't hurt. */
212 addr
&= ~(pagesize
- 1);
214 /* Search for a free area. If we hit 0, we're out of luck. */
215 for (; addr
; addr
-= pagesize
)
219 /* No MAP_FIXED - we don't want to zap someone's mapping. */
220 res
= mmap ((void *) addr
, size
,
221 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
222 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
224 /* If we got what we wanted, return. */
225 if ((uintptr_t) res
== addr
)
228 /* If we got a mapping, but at a wrong address, undo it. */
229 if (res
!= MAP_FAILED
)
235 void *res
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
236 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_32BIT
, -1, 0);
238 if (res
== MAP_FAILED
)
246 initialize_low_tracepoint (void)
248 #if defined __ILP32__
249 for (int i
= 0; i
< x86_linux_x32_tdesc_count (); i
++)
250 amd64_linux_read_description (x86_linux_tdesc_idx_to_xcr0 (i
), true);
252 for (int i
= 0; i
< x86_linux_amd64_tdesc_count (); i
++)
253 amd64_linux_read_description (x86_linux_tdesc_idx_to_xcr0 (i
), false);