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
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
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. */
45 /* Is this feature checked when creating an i386 target description. */
48 /* Is this feature checked when creating an amd64 target description. */
51 /* Is this feature checked when creating an x32 target description. */
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 ()
80 for (const auto &entry
: x86_linux_all_xstate_features
)
82 mask
|= entry
.feature
;
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 ()
95 for (const auto &entry
: x86_linux_all_xstate_features
)
97 mask
|= entry
.feature
;
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 ()
110 for (const auto &entry
: x86_linux_all_xstate_features
)
112 mask
|= entry
.feature
;
117 /* See arch/x86-linux-tdesc-features.h. */
120 x86_linux_i386_xcr0_feature_mask ()
122 return x86_linux_i386_xcr0_feature_mask_1 ();
125 /* See arch/x86-linux-tdesc-features.h. */
128 x86_linux_amd64_xcr0_feature_mask ()
130 return x86_linux_amd64_xcr0_feature_mask_1 ();
133 /* See arch/x86-linux-tdesc-features.h. */
136 x86_linux_x32_xcr0_feature_mask ()
138 return x86_linux_x32_xcr0_feature_mask_1 ();
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. */
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
)
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. */
179 x86_linux_i386_tdesc_count_1 ()
183 for (const auto &entry
: x86_linux_all_xstate_features
)
187 gdb_assert (count
> 0);
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. */
196 x86_linux_amd64_tdesc_count_1 ()
200 for (const auto &entry
: x86_linux_all_xstate_features
)
204 gdb_assert (count
> 0);
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. */
213 x86_linux_x32_tdesc_count_1 ()
217 for (const auto &entry
: x86_linux_all_xstate_features
)
221 gdb_assert (count
> 0);
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. */
253 x86_linux_tdesc_idx_to_xcr0 (int idx
)
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
;
266 #endif /* IN_PROCESS_AGENT */