RISC-V: Don't report warnings when linking different privileged spec objects.
[binutils-gdb.git] / gdb / arch / x86-linux-tdesc-features.c
blobf6eb112f3122e3c2728ca433e180092eea00496a
1 /* Target description related code for GNU/Linux x86 (i386 and x86-64).
3 Copyright (C) 2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "arch/x86-linux-tdesc-features.h"
22 /* A structure used to describe a single xstate feature bit that might, or
23 might not, be checked for when creating a target description for one of
24 i386, amd64, or x32.
26 The different CPU/ABI types check for different xstate features when
27 creating a target description.
29 We want to cache target descriptions, and this is currently done in
30 three separate caches, one each for i386, amd64, and x32. Additionally,
31 the caching we're discussing here is Linux only, and for Linux, the only
32 thing that has an impact on target description creation is the xcr0
33 value.
35 In order to ensure the cache functions correctly we need to filter out
36 only those xcr0 feature bits that are relevant, we can then cache target
37 descriptions based on the relevant feature bits. Two xcr0 values might
38 be different, but have the same relevant feature bits. In this case we
39 would expect the two xcr0 values to map to the same cache entry. */
41 struct x86_xstate_feature {
42 /* The xstate feature mask. This is a mask against an xcr0 value. */
43 uint64_t feature;
45 /* Is this feature checked when creating an i386 target description. */
46 bool is_i386;
48 /* Is this feature checked when creating an amd64 target description. */
49 bool is_amd64;
51 /* Is this feature checked when creating an x32 target description. */
52 bool is_x32;
55 /* A constant table that describes all of the xstate features that are
56 checked when building a target description for i386, amd64, or x32.
58 If in the future, due to simplifications or refactoring, this table ever
59 ends up with 'true' for every xcr0 feature on every target type, then this
60 is an indication that this table should probably be removed, and that the
61 rest of the code in this file can be simplified. */
63 static constexpr x86_xstate_feature x86_linux_all_xstate_features[] = {
64 /* Feature, i386, amd64, x32. */
65 { X86_XSTATE_PKRU, true, true, true },
66 { X86_XSTATE_AVX512, true, true, true },
67 { X86_XSTATE_AVX, true, true, true },
68 { X86_XSTATE_SSE, true, false, false },
69 { X86_XSTATE_X87, true, false, false }
72 /* Return a compile time constant which is a mask of all the xstate features
73 that are checked for when building an i386 target description. */
75 static constexpr uint64_t
76 x86_linux_i386_xcr0_feature_mask_1 ()
78 uint64_t mask = 0;
80 for (const auto &entry : x86_linux_all_xstate_features)
81 if (entry.is_i386)
82 mask |= entry.feature;
84 return mask;
87 /* Return a compile time constant which is a mask of all the xstate features
88 that are checked for when building an amd64 target description. */
90 static constexpr uint64_t
91 x86_linux_amd64_xcr0_feature_mask_1 ()
93 uint64_t mask = 0;
95 for (const auto &entry : x86_linux_all_xstate_features)
96 if (entry.is_amd64)
97 mask |= entry.feature;
99 return mask;
102 /* Return a compile time constant which is a mask of all the xstate features
103 that are checked for when building an x32 target description. */
105 static constexpr uint64_t
106 x86_linux_x32_xcr0_feature_mask_1 ()
108 uint64_t mask = 0;
110 for (const auto &entry : x86_linux_all_xstate_features)
111 if (entry.is_x32)
112 mask |= entry.feature;
114 return mask;
117 /* See arch/x86-linux-tdesc-features.h. */
119 uint64_t
120 x86_linux_i386_xcr0_feature_mask ()
122 return x86_linux_i386_xcr0_feature_mask_1 ();
125 /* See arch/x86-linux-tdesc-features.h. */
127 uint64_t
128 x86_linux_amd64_xcr0_feature_mask ()
130 return x86_linux_amd64_xcr0_feature_mask_1 ();
133 /* See arch/x86-linux-tdesc-features.h. */
135 uint64_t
136 x86_linux_x32_xcr0_feature_mask ()
138 return x86_linux_x32_xcr0_feature_mask_1 ();
141 #ifdef GDBSERVER
143 /* See arch/x86-linux-tdesc-features.h. */
146 x86_linux_xcr0_to_tdesc_idx (uint64_t xcr0)
148 /* The following table shows which features are checked for when creating
149 the target descriptions (see nat/x86-linux-tdesc.c), the feature order
150 represents the bit order within the generated index number.
152 i386 | x87 sse avx avx512 pkru
153 amd64 | avx avx512 pkru
154 i32 | avx avx512 pkru
156 The features are ordered so that for each mode (i386, amd64, i32) the
157 generated index will form a continuous range. */
159 int idx = 0;
161 for (int i = 0; i < ARRAY_SIZE (x86_linux_all_xstate_features); ++i)
163 if ((xcr0 & x86_linux_all_xstate_features[i].feature)
164 == x86_linux_all_xstate_features[i].feature)
165 idx |= (1 << i);
168 return idx;
171 #endif /* GDBSERVER */
173 #ifdef IN_PROCESS_AGENT
175 /* Return a compile time constant which is a count of the number of xstate
176 features that are checked for when building an i386 target description. */
178 static constexpr int
179 x86_linux_i386_tdesc_count_1 ()
181 uint64_t count = 0;
183 for (const auto &entry : x86_linux_all_xstate_features)
184 if (entry.is_i386)
185 ++count;
187 gdb_assert (count > 0);
189 return (1 << count);
192 /* Return a compile time constant which is a count of the number of xstate
193 features that are checked for when building an amd64 target description. */
195 static constexpr int
196 x86_linux_amd64_tdesc_count_1 ()
198 uint64_t count = 0;
200 for (const auto &entry : x86_linux_all_xstate_features)
201 if (entry.is_amd64)
202 ++count;
204 gdb_assert (count > 0);
206 return (1 << count);
209 /* Return a compile time constant which is a count of the number of xstate
210 features that are checked for when building an x32 target description. */
212 static constexpr int
213 x86_linux_x32_tdesc_count_1 ()
215 uint64_t count = 0;
217 for (const auto &entry : x86_linux_all_xstate_features)
218 if (entry.is_x32)
219 ++count;
221 gdb_assert (count > 0);
223 return (1 << count);
226 /* See arch/x86-linux-tdesc-features.h. */
229 x86_linux_amd64_tdesc_count ()
231 return x86_linux_amd64_tdesc_count_1 ();
234 /* See arch/x86-linux-tdesc-features.h. */
237 x86_linux_x32_tdesc_count ()
239 return x86_linux_x32_tdesc_count_1 ();
242 /* See arch/x86-linux-tdesc-features.h. */
245 x86_linux_i386_tdesc_count ()
247 return x86_linux_i386_tdesc_count_1 ();
250 /* See arch/x86-linux-tdesc-features.h. */
252 uint64_t
253 x86_linux_tdesc_idx_to_xcr0 (int idx)
255 uint64_t xcr0 = 0;
257 for (int i = 0; i < ARRAY_SIZE (x86_linux_all_xstate_features); ++i)
259 if ((idx & (1 << i)) != 0)
260 xcr0 |= x86_linux_all_xstate_features[i].feature;
263 return xcr0;
266 #endif /* IN_PROCESS_AGENT */