Debian Codegraph @

gdbserver/linux-i386-ipa.cc

raw
1 /* GNU/Linux/x86 specific low level interface, for the in-process
2 agent library for GDB.
3
4 Copyright (C) 2010-2025 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21 #include <sys/mman.h>
22 #include "tracepoint.h"
23 #include "gdbsupport/x86-xstate.h"
24 #include "arch/i386-linux-tdesc.h"
25 #include "arch/x86-linux-tdesc-features.h"
26
27 /* GDB register numbers. */
28
29 enum i386_gdb_regnum
30 {
31 I386_EAX_REGNUM, /* %eax */
32 I386_ECX_REGNUM, /* %ecx */
33 I386_EDX_REGNUM, /* %edx */
34 I386_EBX_REGNUM, /* %ebx */
35 I386_ESP_REGNUM, /* %esp */
36 I386_EBP_REGNUM, /* %ebp */
37 I386_ESI_REGNUM, /* %esi */
38 I386_EDI_REGNUM, /* %edi */
39 I386_EIP_REGNUM, /* %eip */
40 I386_EFLAGS_REGNUM, /* %eflags */
41 I386_CS_REGNUM, /* %cs */
42 I386_SS_REGNUM, /* %ss */
43 I386_DS_REGNUM, /* %ds */
44 I386_ES_REGNUM, /* %es */
45 I386_FS_REGNUM, /* %fs */
46 I386_GS_REGNUM, /* %gs */
47 I386_ST0_REGNUM /* %st(0) */
48 };
49
50 #define i386_num_regs 16
51
52 #define FT_CR_EAX 15
53 #define FT_CR_ECX 14
54 #define FT_CR_EDX 13
55 #define FT_CR_EBX 12
56 #define FT_CR_UESP 11
57 #define FT_CR_EBP 10
58 #define FT_CR_ESI 9
59 #define FT_CR_EDI 8
60 #define FT_CR_EIP 7
61 #define FT_CR_EFL 6
62 #define FT_CR_DS 5
63 #define FT_CR_ES 4
64 #define FT_CR_FS 3
65 #define FT_CR_GS 2
66 #define FT_CR_SS 1
67 #define FT_CR_CS 0
68
69 /* Mapping between the general-purpose registers in jump tracepoint
70 format and GDB's register array layout. */
71
72 static const int i386_ft_collect_regmap[] =
73 {
74 FT_CR_EAX * 4, FT_CR_ECX * 4, FT_CR_EDX * 4, FT_CR_EBX * 4,
75 FT_CR_UESP * 4, FT_CR_EBP * 4, FT_CR_ESI * 4, FT_CR_EDI * 4,
76 FT_CR_EIP * 4, FT_CR_EFL * 4, FT_CR_CS * 4, FT_CR_SS * 4,
77 FT_CR_DS * 4, FT_CR_ES * 4, FT_CR_FS * 4, FT_CR_GS * 4
78 };
79
80 void
81 supply_fast_tracepoint_registers (struct regcache *regcache,
82 const unsigned char *buf)
83 {
84 int i;
85
86 for (i = 0; i < i386_num_regs; i++)
87 {
88 int regval;
89
90 if (i >= I386_CS_REGNUM && i <= I386_GS_REGNUM)
91 regval = *(short *) (((char *) buf) + i386_ft_collect_regmap[i]);
92 else
93 regval = *(int *) (((char *) buf) + i386_ft_collect_regmap[i]);
94
95 supply_register (regcache, i, &regval);
96 }
97 }
98
99 ULONGEST
100 get_raw_reg (const unsigned char *raw_regs, int regnum)
101 {
102 /* This should maybe be allowed to return an error code, or perhaps
103 better, have the emit_reg detect this, and emit a constant zero,
104 or something. */
105
106 if (regnum > i386_num_regs)
107 return 0;
108 else if (regnum >= I386_CS_REGNUM && regnum <= I386_GS_REGNUM)
109 return *(short *) (raw_regs + i386_ft_collect_regmap[regnum]);
110 else
111 return *(int *) (raw_regs + i386_ft_collect_regmap[regnum]);
112 }
113
114 /* This is only needed because reg-i386-linux-lib.o references it. We
115 may use it proper at some point. */
116 const char *gdbserver_xmltarget;
117
118 /* Attempt to allocate memory for trampolines in the first 64 KiB of
119 memory to enable smaller jump patches. */
120
121 static void
122 initialize_fast_tracepoint_trampoline_buffer (void)
123 {
124 const CORE_ADDR buffer_end = 64 * 1024;
125 /* Ensure that the buffer will be at least 1 KiB in size, which is
126 enough space for over 200 fast tracepoints. */
127 const int min_buffer_size = 1024;
128 char buf[IPA_BUFSIZ];
129 CORE_ADDR mmap_min_addr = buffer_end + 1;
130 ULONGEST buffer_size;
131 FILE *f = fopen ("/proc/sys/vm/mmap_min_addr", "r");
132
133 if (!f)
134 {
135 snprintf (buf, sizeof (buf), "mmap_min_addr open failed: %s",
136 safe_strerror (errno));
137 set_trampoline_buffer_space (0, 0, buf);
138 return;
139 }
140
141 if (fgets (buf, IPA_BUFSIZ, f))
142 sscanf (buf, "%llu", &mmap_min_addr);
143
144 fclose (f);
145
146 buffer_size = buffer_end - mmap_min_addr;
147
148 if (buffer_size >= min_buffer_size)
149 {
150 if (mmap ((void *) (uintptr_t) mmap_min_addr, buffer_size,
151 PROT_READ | PROT_EXEC | PROT_WRITE,
152 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
153 -1, 0)
154 != MAP_FAILED)
155 set_trampoline_buffer_space (mmap_min_addr, buffer_end, NULL);
156 else
157 {
158 snprintf (buf, IPA_BUFSIZ, "low-64K-buffer mmap() failed: %s",
159 safe_strerror (errno));
160 set_trampoline_buffer_space (0, 0, buf);
161 }
162 }
163 else
164 {
165 snprintf (buf, IPA_BUFSIZ, "mmap_min_addr is %d, must be %d or less",
166 (int) mmap_min_addr, (int) buffer_end - min_buffer_size);
167 set_trampoline_buffer_space (0, 0, buf);
168 }
169 }
170
171 /* Return target_desc to use for IPA, given the tdesc index passed by
172 gdbserver. */
173
174 const struct target_desc *
175 get_ipa_tdesc (int idx)
176 {
177 uint64_t xstate_bv = x86_linux_tdesc_idx_to_xstate_bv (idx);
178
179 return i386_linux_read_description (xstate_bv);
180 }
181
182 /* Allocate buffer for the jump pads. On i386, we can reach an arbitrary
183 address with a jump instruction, so just allocate normally. */
184
185 void *
186 alloc_jump_pad_buffer (size_t size)
187 {
188 void *res = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
189 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
190
191 if (res == MAP_FAILED)
192 return NULL;
193
194 return res;
195 }
196
197 void
198 initialize_low_tracepoint (void)
199 {
200 initialize_fast_tracepoint_trampoline_buffer ();
201 for (int i = 0; i < x86_linux_i386_tdesc_count (); i++)
202 i386_linux_read_description (x86_linux_tdesc_idx_to_xstate_bv (i));
203 }