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
]);
79 /* Return target_desc to use for IPA, given the tdesc index passed by
82 const struct target_desc
*
83 get_ipa_tdesc (int idx
)
85 uint64_t xcr0
= x86_linux_tdesc_idx_to_xcr0 (idx
);
93 return amd64_linux_read_description (xcr0
, is_x32
);
96 /* Allocate buffer for the jump pads. The branch instruction has a
97 reach of +/- 31-bit, and the executable is loaded at low addresses.
99 64-bit: Use MAP_32BIT to allocate in the first 2GB. Shared
100 libraries, being allocated at the top, are unfortunately out of
103 x32: Since MAP_32BIT is 64-bit only, do the placement manually.
104 Try allocating at '0x80000000 - SIZE' initially, decreasing until
105 we hit a free area. This ensures the executable is fully covered,
106 and is as close as possible to the shared libraries, which are
107 usually mapped at the top of the first 4GB of the address space.
111 alloc_jump_pad_buffer (size_t size
)
117 pagesize
= sysconf (_SC_PAGE_SIZE
);
119 perror_with_name ("sysconf");
121 addr
= 0x80000000 - size
;
123 /* size should already be page-aligned, but this can't hurt. */
124 addr
&= ~(pagesize
- 1);
126 /* Search for a free area. If we hit 0, we're out of luck. */
127 for (; addr
; addr
-= pagesize
)
131 /* No MAP_FIXED - we don't want to zap someone's mapping. */
132 res
= mmap ((void *) addr
, size
,
133 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
134 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
136 /* If we got what we wanted, return. */
137 if ((uintptr_t) res
== addr
)
140 /* If we got a mapping, but at a wrong address, undo it. */
141 if (res
!= MAP_FAILED
)
147 void *res
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
148 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_32BIT
, -1, 0);
150 if (res
== MAP_FAILED
)
158 initialize_low_tracepoint (void)
160 #if defined __ILP32__
161 for (int i
= 0; i
< x86_linux_x32_tdesc_count (); i
++)
162 amd64_linux_read_description (x86_linux_tdesc_idx_to_xcr0 (i
), true);
164 for (int i
= 0; i
< x86_linux_amd64_tdesc_count (); i
++)
165 amd64_linux_read_description (x86_linux_tdesc_idx_to_xcr0 (i
), false);