Add translations for various sub-directories
[binutils-gdb.git] / gdbserver / linux-amd64-ipa.cc
blob046636834fbef5655acb3ab2397916ccedbe0731
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 /* Return target_desc to use for IPA, given the tdesc index passed by
80 gdbserver. */
82 const struct target_desc *
83 get_ipa_tdesc (int idx)
85 uint64_t xcr0 = x86_linux_tdesc_idx_to_xcr0 (idx);
87 #if defined __ILP32__
88 bool is_x32 = true;
89 #else
90 bool is_x32 = false;
91 #endif
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
101 luck.
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.
110 void *
111 alloc_jump_pad_buffer (size_t size)
113 #if __ILP32__
114 uintptr_t addr;
115 int pagesize;
117 pagesize = sysconf (_SC_PAGE_SIZE);
118 if (pagesize == -1)
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)
129 void *res;
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)
138 return res;
140 /* If we got a mapping, but at a wrong address, undo it. */
141 if (res != MAP_FAILED)
142 munmap (res, size);
145 return NULL;
146 #else
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)
151 return NULL;
153 return res;
154 #endif
157 void
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);
163 #else
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);
166 #endif