1 /* Initialize cpu feature data. s390x version.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <cpu-features.h>
21 #include <elf/dl-tunables.h>
22 #include <ifunc-memcmp.h>
24 #include <dl-symbol-redir-ifunc.h>
25 #include <dl-tunables-parse.h>
27 #define S390_COPY_CPU_FEATURES(SRC_PTR, DEST_PTR) \
28 (DEST_PTR)->hwcap = (SRC_PTR)->hwcap; \
29 (DEST_PTR)->stfle_bits[0] = (SRC_PTR)->stfle_bits[0];
32 TUNABLE_CALLBACK (set_hwcaps
) (tunable_val_t
*valp
)
34 /* The current IFUNC selection is always using the most recent
35 features which are available via AT_HWCAP or STFLE-bits. But in
36 some scenarios it is useful to adjust this selection.
38 The environment variable:
40 GLIBC_TUNABLES=glibc.cpu.hwcaps=-xxx,yyy,zzz,....
42 can be used to enable HWCAP/STFLE feature yyy, disable HWCAP/STFLE feature
43 xxx, where the feature name is case-sensitive and has to match the ones
44 used below. Furthermore, the ARCH-level zzz can be used to set various
45 HWCAP/STFLE features at once. */
47 /* Copy the features from dl_s390_cpu_features, which contains the features
48 provided by AT_HWCAP and stfle-instruction. */
49 struct cpu_features
*cpu_features
= &GLRO(dl_s390_cpu_features
);
50 struct cpu_features cpu_features_orig
;
51 S390_COPY_CPU_FEATURES (cpu_features
, &cpu_features_orig
);
52 struct cpu_features cpu_features_curr
;
53 S390_COPY_CPU_FEATURES (cpu_features
, &cpu_features_curr
);
55 struct tunable_str_comma_state_t ts
;
56 tunable_str_comma_init (&ts
, valp
);
58 struct tunable_str_comma_t t
;
59 while (tunable_str_comma_next (&ts
, &t
))
64 /* Handle only the features here which are really used in the
65 IFUNC-resolvers. All others are ignored as the values are only used
67 bool reset_features
= false;
68 unsigned long int hwcap_mask
= 0UL;
69 unsigned long long stfle_bits0_mask
= 0ULL;
70 bool disable
= t
.disable
;
72 if (tunable_str_comma_strcmp_cte (&t
, "zEC12")
73 || tunable_str_comma_strcmp_cte (&t
, "arch10"))
75 reset_features
= true;
77 hwcap_mask
= HWCAP_S390_VXRS
| HWCAP_S390_VXRS_EXT
78 | HWCAP_S390_VXRS_EXT2
;
79 stfle_bits0_mask
= S390_STFLE_MASK_ARCH13_MIE3
;
81 else if (tunable_str_comma_strcmp_cte (&t
, "z13")
82 || tunable_str_comma_strcmp_cte (&t
, "arch11"))
84 reset_features
= true;
86 hwcap_mask
= HWCAP_S390_VXRS_EXT
| HWCAP_S390_VXRS_EXT2
;
87 stfle_bits0_mask
= S390_STFLE_MASK_ARCH13_MIE3
;
89 else if (tunable_str_comma_strcmp_cte (&t
, "z14")
90 || tunable_str_comma_strcmp_cte (&t
, "arch12"))
92 reset_features
= true;
94 hwcap_mask
= HWCAP_S390_VXRS_EXT2
;
95 stfle_bits0_mask
= S390_STFLE_MASK_ARCH13_MIE3
;
97 else if (tunable_str_comma_strcmp_cte (&t
, "z15")
98 || tunable_str_comma_strcmp_cte (&t
, "z16")
99 || tunable_str_comma_strcmp_cte (&t
, "arch13")
100 || tunable_str_comma_strcmp_cte (&t
, "arch14"))
102 /* For z15 or newer we don't have to disable something, but we have
103 to reset to the original values. */
104 reset_features
= true;
106 else if (tunable_str_comma_strcmp_cte (&t
, "HWCAP_S390_VXRS"))
108 hwcap_mask
= HWCAP_S390_VXRS
;
110 hwcap_mask
|= HWCAP_S390_VXRS_EXT
| HWCAP_S390_VXRS_EXT2
;
112 else if (tunable_str_comma_strcmp_cte (&t
, "HWCAP_S390_VXRS_EXT"))
114 hwcap_mask
= HWCAP_S390_VXRS_EXT
;
116 hwcap_mask
|= HWCAP_S390_VXRS_EXT2
;
118 hwcap_mask
|= HWCAP_S390_VXRS
;
120 else if (tunable_str_comma_strcmp_cte (&t
, "HWCAP_S390_VXRS_EXT2"))
122 hwcap_mask
= HWCAP_S390_VXRS_EXT2
;
124 hwcap_mask
|= HWCAP_S390_VXRS
| HWCAP_S390_VXRS_EXT
;
126 else if (tunable_str_comma_strcmp_cte (&t
, "STFLE_MIE3"))
127 stfle_bits0_mask
= S390_STFLE_MASK_ARCH13_MIE3
;
129 /* Perform the actions determined above. */
132 S390_COPY_CPU_FEATURES (&cpu_features_orig
, &cpu_features_curr
);
135 if (hwcap_mask
!= 0UL)
138 cpu_features_curr
.hwcap
&= ~hwcap_mask
;
140 cpu_features_curr
.hwcap
|= hwcap_mask
;
143 if (stfle_bits0_mask
!= 0ULL)
146 cpu_features_curr
.stfle_bits
[0] &= ~stfle_bits0_mask
;
148 cpu_features_curr
.stfle_bits
[0] |= stfle_bits0_mask
;
152 /* Copy back the features after checking that no unsupported features were
154 cpu_features
->hwcap
= cpu_features_curr
.hwcap
& cpu_features_orig
.hwcap
;
155 cpu_features
->stfle_bits
[0] = cpu_features_curr
.stfle_bits
[0]
156 & cpu_features_orig
.stfle_bits
[0];
160 init_cpu_features (struct cpu_features
*cpu_features
)
162 /* Fill cpu_features as passed by kernel and machine. */
163 cpu_features
->hwcap
= GLRO(dl_hwcap
);
165 /* We want just 1 double word to be returned. */
166 if (__glibc_likely ((cpu_features
->hwcap
& HWCAP_S390_STFLE
)
167 && (cpu_features
->hwcap
& HWCAP_S390_ZARCH
)
168 && (cpu_features
->hwcap
& HWCAP_S390_HIGH_GPRS
)))
170 register unsigned long reg0
__asm__("0") = 0;
171 __asm__
__volatile__(".machine push" "\n\t"
172 ".machine \"z9-109\"" "\n\t"
173 ".machinemode \"zarch_nohighgprs\"\n\t"
176 : "=QS" (cpu_features
->stfle_bits
[0]),
182 cpu_features
->stfle_bits
[0] = 0ULL;
185 TUNABLE_GET (glibc
, cpu
, hwcaps
, tunable_val_t
*, TUNABLE_CALLBACK (set_hwcaps
));