Updated Malay translation for the bfd sub-directory
[binutils-gdb.git] / gdbserver / linux-amd64-ipa.cc
blobc53758a7dbe9f69d006bd9ee2fc815609fe4edf4
1 /* GNU/Linux/x86-64 specific low level interface, for the in-process
2 agent library for GDB.
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/>. */
21 #include <sys/mman.h>
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. */
29 #define FT_CR_RIP 0
30 #define FT_CR_EFLAGS 1
31 #define FT_CR_R8 2
32 #define FT_CR_R9 3
33 #define FT_CR_R10 4
34 #define FT_CR_R11 5
35 #define FT_CR_R12 6
36 #define FT_CR_R13 7
37 #define FT_CR_R14 8
38 #define FT_CR_R15 9
39 #define FT_CR_RAX 10
40 #define FT_CR_RBX 11
41 #define FT_CR_RCX 12
42 #define FT_CR_RDX 13
43 #define FT_CR_RSI 14
44 #define FT_CR_RDI 15
45 #define FT_CR_RBP 16
46 #define FT_CR_RSP 17
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]))
59 void
60 supply_fast_tracepoint_registers (struct regcache *regcache,
61 const unsigned char *buf)
63 int i;
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]);
70 ULONGEST
71 get_raw_reg (const unsigned char *raw_regs, int regnum)
73 if (regnum >= X86_64_NUM_FT_COLLECT_GREGS)
74 return 0;
76 return *(ULONGEST *) (raw_regs + x86_64_ft_collect_regmap[regnum]);
79 #ifdef HAVE_UST
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
86 address). */
88 #define ST_REGENTRY(REG) \
89 { \
90 offsetof (struct registers, REG), \
91 sizeof (((struct registers *) NULL)->REG) \
94 static struct
96 int offset;
97 int size;
98 } x86_64_st_collect_regmap[] =
100 ST_REGENTRY(rax),
101 ST_REGENTRY(rbx),
102 ST_REGENTRY(rcx),
103 ST_REGENTRY(rdx),
104 ST_REGENTRY(rsi),
105 ST_REGENTRY(rdi),
106 ST_REGENTRY(rbp),
107 ST_REGENTRY(rsp),
108 ST_REGENTRY(r8),
109 ST_REGENTRY(r9),
110 ST_REGENTRY(r10),
111 ST_REGENTRY(r11),
112 ST_REGENTRY(r12),
113 ST_REGENTRY(r13),
114 ST_REGENTRY(r14),
115 ST_REGENTRY(r15),
116 { -1, 0 },
117 ST_REGENTRY(rflags),
118 ST_REGENTRY(cs),
119 ST_REGENTRY(ss),
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
128 void
129 supply_static_tracepoint_registers (struct regcache *regcache,
130 const unsigned char *buf,
131 CORE_ADDR pc)
133 int i;
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)
143 case 8:
144 supply_register (regcache, i,
145 ((char *) buf)
146 + x86_64_st_collect_regmap[i].offset);
147 break;
148 case 2:
150 unsigned long reg
151 = * (short *) (((char *) buf)
152 + x86_64_st_collect_regmap[i].offset);
153 reg &= 0xffff;
154 supply_register (regcache, i, &reg);
156 break;
157 default:
158 internal_error ("unhandled register size: %d",
159 x86_64_st_collect_regmap[i].size);
160 break;
165 #endif /* HAVE_UST */
167 /* Return target_desc to use for IPA, given the tdesc index passed by
168 gdbserver. */
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__
176 bool is_x32 = true;
177 #else
178 bool is_x32 = false;
179 #endif
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
189 luck.
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.
198 void *
199 alloc_jump_pad_buffer (size_t size)
201 #if __ILP32__
202 uintptr_t addr;
203 int pagesize;
205 pagesize = sysconf (_SC_PAGE_SIZE);
206 if (pagesize == -1)
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)
217 void *res;
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)
226 return res;
228 /* If we got a mapping, but at a wrong address, undo it. */
229 if (res != MAP_FAILED)
230 munmap (res, size);
233 return NULL;
234 #else
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)
239 return NULL;
241 return res;
242 #endif
245 void
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);
251 #else
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);
254 #endif