]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/s390/cpu-features.c
afeb9b56382efa96cf467a844ac798f1f28b4e47
[thirdparty/glibc.git] / sysdeps / s390 / cpu-features.c
1 /* Initialize cpu feature data. s390x version.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <cpu-features.h>
20
21 #if HAVE_TUNABLES
22 # include <elf/dl-tunables.h>
23 # include <ifunc-memcmp.h>
24 # include <string.h>
25 extern __typeof (memcmp) MEMCMP_DEFAULT;
26 #endif
27
28 #if HAVE_TUNABLES
29 # define S390_COPY_CPU_FEATURES(SRC_PTR, DEST_PTR) \
30 (DEST_PTR)->hwcap = (SRC_PTR)->hwcap; \
31 (DEST_PTR)->stfle_bits[0] = (SRC_PTR)->stfle_bits[0];
32
33 static void
34 TUNABLE_CALLBACK (set_hwcaps) (tunable_val_t *valp)
35 {
36 /* The current IFUNC selection is always using the most recent
37 features which are available via AT_HWCAP or STFLE-bits. But in
38 some scenarios it is useful to adjust this selection.
39
40 The environment variable:
41
42 GLIBC_TUNABLES=glibc.cpu.hwcaps=-xxx,yyy,zzz,....
43
44 can be used to enable HWCAP/STFLE feature yyy, disable HWCAP/STFLE feature
45 xxx, where the feature name is case-sensitive and has to match the ones
46 used below. Furthermore, the ARCH-level zzz can be used to set various
47 HWCAP/STFLE features at once. */
48
49 /* Copy the features from dl_s390_cpu_features, which contains the features
50 provided by AT_HWCAP and stfle-instruction. */
51 struct cpu_features *cpu_features = &GLRO(dl_s390_cpu_features);
52 struct cpu_features cpu_features_orig;
53 S390_COPY_CPU_FEATURES (cpu_features, &cpu_features_orig);
54 struct cpu_features cpu_features_curr;
55 S390_COPY_CPU_FEATURES (cpu_features, &cpu_features_curr);
56
57 const char *token = valp->strval;
58 do
59 {
60 const char *token_end, *feature;
61 bool disable;
62 size_t token_len;
63 size_t feature_len;
64
65 /* Find token separator or end of string. */
66 for (token_end = token; *token_end != ','; token_end++)
67 if (*token_end == '\0')
68 break;
69
70 /* Determine feature. */
71 token_len = token_end - token;
72 if (*token == '-')
73 {
74 disable = true;
75 feature = token + 1;
76 feature_len = token_len - 1;
77 }
78 else
79 {
80 disable = false;
81 feature = token;
82 feature_len = token_len;
83 }
84
85 /* Handle only the features here which are really used in the
86 IFUNC-resolvers. All others are ignored as the values are only used
87 inside glibc. */
88 bool reset_features = false;
89 unsigned long int hwcap_mask = 0UL;
90 unsigned long long stfle_bits0_mask = 0ULL;
91
92 if ((*feature == 'z' || *feature == 'a'))
93 {
94 if ((feature_len == 5 && *feature == 'z'
95 && MEMCMP_DEFAULT (feature, "zEC12", 5) == 0)
96 || (feature_len == 6 && *feature == 'a'
97 && MEMCMP_DEFAULT (feature, "arch10", 6) == 0))
98 {
99 reset_features = true;
100 disable = true;
101 hwcap_mask = HWCAP_S390_VXRS | HWCAP_S390_VXRS_EXT
102 | HWCAP_S390_VXRS_EXT2;
103 stfle_bits0_mask = S390_STFLE_MASK_ARCH13_MIE3;
104 }
105 else if ((feature_len == 3 && *feature == 'z'
106 && MEMCMP_DEFAULT (feature, "z13", 3) == 0)
107 || (feature_len == 6 && *feature == 'a'
108 && MEMCMP_DEFAULT (feature, "arch11", 6) == 0))
109 {
110 reset_features = true;
111 disable = true;
112 hwcap_mask = HWCAP_S390_VXRS_EXT | HWCAP_S390_VXRS_EXT2;
113 stfle_bits0_mask = S390_STFLE_MASK_ARCH13_MIE3;
114 }
115 else if ((feature_len == 3 && *feature == 'z'
116 && MEMCMP_DEFAULT (feature, "z14", 3) == 0)
117 || (feature_len == 6 && *feature == 'a'
118 && MEMCMP_DEFAULT (feature, "arch12", 6) == 0))
119 {
120 reset_features = true;
121 disable = true;
122 hwcap_mask = HWCAP_S390_VXRS_EXT2;
123 stfle_bits0_mask = S390_STFLE_MASK_ARCH13_MIE3;
124 }
125 else if ((feature_len == 3 && *feature == 'z'
126 && (MEMCMP_DEFAULT (feature, "z15", 3) == 0
127 || MEMCMP_DEFAULT (feature, "z16", 3) == 0))
128 || (feature_len == 6
129 && (MEMCMP_DEFAULT (feature, "arch13", 6) == 0
130 || MEMCMP_DEFAULT (feature, "arch14", 6) == 0)))
131 {
132 /* For z15 or newer we don't have to disable something,
133 but we have to reset to the original values. */
134 reset_features = true;
135 }
136 }
137 else if (*feature == 'H')
138 {
139 if (feature_len == 15
140 && MEMCMP_DEFAULT (feature, "HWCAP_S390_VXRS", 15) == 0)
141 {
142 hwcap_mask = HWCAP_S390_VXRS;
143 if (disable)
144 hwcap_mask |= HWCAP_S390_VXRS_EXT | HWCAP_S390_VXRS_EXT2;
145 }
146 else if (feature_len == 19
147 && MEMCMP_DEFAULT (feature, "HWCAP_S390_VXRS_EXT", 19) == 0)
148 {
149 hwcap_mask = HWCAP_S390_VXRS_EXT;
150 if (disable)
151 hwcap_mask |= HWCAP_S390_VXRS_EXT2;
152 else
153 hwcap_mask |= HWCAP_S390_VXRS;
154 }
155 else if (feature_len == 20
156 && MEMCMP_DEFAULT (feature, "HWCAP_S390_VXRS_EXT2", 20) == 0)
157 {
158 hwcap_mask = HWCAP_S390_VXRS_EXT2;
159 if (!disable)
160 hwcap_mask |= HWCAP_S390_VXRS | HWCAP_S390_VXRS_EXT;
161 }
162 }
163 else if (*feature == 'S')
164 {
165 if (feature_len == 10
166 && MEMCMP_DEFAULT (feature, "STFLE_MIE3", 10) == 0)
167 {
168 stfle_bits0_mask = S390_STFLE_MASK_ARCH13_MIE3;
169 }
170 }
171
172 /* Perform the actions determined above. */
173 if (reset_features)
174 {
175 S390_COPY_CPU_FEATURES (&cpu_features_orig, &cpu_features_curr);
176 }
177
178 if (hwcap_mask != 0UL)
179 {
180 if (disable)
181 cpu_features_curr.hwcap &= ~hwcap_mask;
182 else
183 cpu_features_curr.hwcap |= hwcap_mask;
184 }
185
186 if (stfle_bits0_mask != 0ULL)
187 {
188 if (disable)
189 cpu_features_curr.stfle_bits[0] &= ~stfle_bits0_mask;
190 else
191 cpu_features_curr.stfle_bits[0] |= stfle_bits0_mask;
192 }
193
194 /* Jump over current token ... */
195 token += token_len;
196
197 /* ... and skip token separator for next round. */
198 if (*token == ',') token++;
199 }
200 while (*token != '\0');
201
202 /* Copy back the features after checking that no unsupported features were
203 enabled by user. */
204 cpu_features->hwcap = cpu_features_curr.hwcap & cpu_features_orig.hwcap;
205 cpu_features->stfle_bits[0] = cpu_features_curr.stfle_bits[0]
206 & cpu_features_orig.stfle_bits[0];
207 }
208 #endif
209
210 static inline void
211 init_cpu_features (struct cpu_features *cpu_features)
212 {
213 /* Fill cpu_features as passed by kernel and machine. */
214 cpu_features->hwcap = GLRO(dl_hwcap);
215
216 /* We want just 1 double word to be returned. */
217 if (__glibc_likely ((cpu_features->hwcap & HWCAP_S390_STFLE)
218 && (cpu_features->hwcap & HWCAP_S390_ZARCH)
219 && (cpu_features->hwcap & HWCAP_S390_HIGH_GPRS)))
220 {
221 register unsigned long reg0 __asm__("0") = 0;
222 __asm__ __volatile__(".machine push" "\n\t"
223 ".machine \"z9-109\"" "\n\t"
224 ".machinemode \"zarch_nohighgprs\"\n\t"
225 "stfle %0" "\n\t"
226 ".machine pop" "\n"
227 : "=QS" (cpu_features->stfle_bits[0]),
228 "+d" (reg0)
229 : : "cc");
230 }
231 else
232 {
233 cpu_features->stfle_bits[0] = 0ULL;
234 }
235
236 #if HAVE_TUNABLES
237 TUNABLE_GET (glibc, cpu, hwcaps, tunable_val_t *, TUNABLE_CALLBACK (set_hwcaps));
238 #endif
239 }