cpu-features.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Initialize cpu feature data. s390x version.
  2. Copyright (C) 2023-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <cpu-features.h>
  16. #include <ldsodefs.h>
  17. #include <sys/auxv.h>
  18. #include <elf/dl-tunables.h>
  19. #include <ifunc-memcmp.h>
  20. #include <string.h>
  21. #include <dl-symbol-redir-ifunc.h>
  22. #include <dl-tunables-parse.h>
  23. #define S390_COPY_CPU_FEATURES(SRC_PTR, DEST_PTR) \
  24. (DEST_PTR)->hwcap = (SRC_PTR)->hwcap; \
  25. (DEST_PTR)->stfle_filtered = (SRC_PTR)->stfle_filtered;
  26. static void
  27. TUNABLE_CALLBACK (set_hwcaps) (tunable_val_t *valp)
  28. {
  29. /* The current IFUNC selection is always using the most recent
  30. features which are available via AT_HWCAP or STFLE-bits. But in
  31. some scenarios it is useful to adjust this selection.
  32. The environment variable:
  33. GLIBC_TUNABLES=glibc.cpu.hwcaps=-xxx,yyy,zzz,....
  34. can be used to enable HWCAP/STFLE feature yyy, disable HWCAP/STFLE feature
  35. xxx, where the feature name is case-sensitive and has to match the ones
  36. used below. Furthermore, the ARCH-level zzz can be used to set various
  37. HWCAP/STFLE features at once. */
  38. /* Copy the features from dl_s390_cpu_features, which contains the features
  39. provided by AT_HWCAP and stfle-instruction. */
  40. struct cpu_features *cpu_features = &GLRO(dl_s390_cpu_features);
  41. struct cpu_features cpu_features_orig;
  42. S390_COPY_CPU_FEATURES (cpu_features, &cpu_features_orig);
  43. struct cpu_features cpu_features_curr;
  44. S390_COPY_CPU_FEATURES (cpu_features, &cpu_features_curr);
  45. struct tunable_str_comma_state_t ts;
  46. tunable_str_comma_init (&ts, valp);
  47. struct tunable_str_comma_t t;
  48. while (tunable_str_comma_next (&ts, &t))
  49. {
  50. if (t.len == 0)
  51. continue;
  52. /* Handle only the features here which are really used in the
  53. IFUNC-resolvers. All others are ignored as the values are only used
  54. inside glibc. */
  55. bool reset_features = false;
  56. unsigned long int hwcap_mask = 0UL;
  57. unsigned long long stfle_bits0_mask = 0ULL;
  58. bool disable = t.disable;
  59. if (tunable_str_comma_strcmp_cte (&t, "zEC12")
  60. || tunable_str_comma_strcmp_cte (&t, "arch10"))
  61. {
  62. reset_features = true;
  63. disable = true;
  64. hwcap_mask = HWCAP_S390_VXRS | HWCAP_S390_VXRS_EXT
  65. | HWCAP_S390_VXRS_EXT2;
  66. stfle_bits0_mask = S390_STFLE_BIT61_ARCH13_MIE3;
  67. }
  68. else if (tunable_str_comma_strcmp_cte (&t, "z13")
  69. || tunable_str_comma_strcmp_cte (&t, "arch11"))
  70. {
  71. reset_features = true;
  72. disable = true;
  73. hwcap_mask = HWCAP_S390_VXRS_EXT | HWCAP_S390_VXRS_EXT2;
  74. stfle_bits0_mask = S390_STFLE_BIT61_ARCH13_MIE3;
  75. }
  76. else if (tunable_str_comma_strcmp_cte (&t, "z14")
  77. || tunable_str_comma_strcmp_cte (&t, "arch12"))
  78. {
  79. reset_features = true;
  80. disable = true;
  81. hwcap_mask = HWCAP_S390_VXRS_EXT2;
  82. stfle_bits0_mask = S390_STFLE_BIT61_ARCH13_MIE3;
  83. }
  84. else if (tunable_str_comma_strcmp_cte (&t, "z15")
  85. || tunable_str_comma_strcmp_cte (&t, "z16")
  86. || tunable_str_comma_strcmp_cte (&t, "z17")
  87. || tunable_str_comma_strcmp_cte (&t, "arch13")
  88. || tunable_str_comma_strcmp_cte (&t, "arch14")
  89. || tunable_str_comma_strcmp_cte (&t, "arch15"))
  90. {
  91. /* For z15 or newer we don't have to disable something, but we have
  92. to reset to the original values. */
  93. reset_features = true;
  94. }
  95. else if (tunable_str_comma_strcmp_cte (&t, "HWCAP_S390_VXRS"))
  96. {
  97. hwcap_mask = HWCAP_S390_VXRS;
  98. if (t.disable)
  99. hwcap_mask |= HWCAP_S390_VXRS_EXT | HWCAP_S390_VXRS_EXT2;
  100. }
  101. else if (tunable_str_comma_strcmp_cte (&t, "HWCAP_S390_VXRS_EXT"))
  102. {
  103. hwcap_mask = HWCAP_S390_VXRS_EXT;
  104. if (t.disable)
  105. hwcap_mask |= HWCAP_S390_VXRS_EXT2;
  106. else
  107. hwcap_mask |= HWCAP_S390_VXRS;
  108. }
  109. else if (tunable_str_comma_strcmp_cte (&t, "HWCAP_S390_VXRS_EXT2"))
  110. {
  111. hwcap_mask = HWCAP_S390_VXRS_EXT2;
  112. if (!t.disable)
  113. hwcap_mask |= HWCAP_S390_VXRS | HWCAP_S390_VXRS_EXT;
  114. }
  115. else if (tunable_str_comma_strcmp_cte (&t, "STFLE_MIE3"))
  116. stfle_bits0_mask = S390_STFLE_BIT61_ARCH13_MIE3;
  117. /* Perform the actions determined above. */
  118. if (reset_features)
  119. {
  120. S390_COPY_CPU_FEATURES (&cpu_features_orig, &cpu_features_curr);
  121. }
  122. if (hwcap_mask != 0UL)
  123. {
  124. if (disable)
  125. cpu_features_curr.hwcap &= ~hwcap_mask;
  126. else
  127. cpu_features_curr.hwcap |= hwcap_mask;
  128. }
  129. if (stfle_bits0_mask != 0ULL)
  130. {
  131. if (disable)
  132. cpu_features_curr.stfle_filtered &= ~stfle_bits0_mask;
  133. else
  134. cpu_features_curr.stfle_filtered |= stfle_bits0_mask;
  135. }
  136. }
  137. /* Copy back the features after checking that no unsupported features were
  138. enabled by user. */
  139. cpu_features->hwcap = cpu_features_curr.hwcap & cpu_features_orig.hwcap;
  140. cpu_features->stfle_filtered = cpu_features_curr.stfle_filtered
  141. & cpu_features_orig.stfle_filtered;
  142. }
  143. static inline void
  144. init_cpu_features_no_tunables (struct cpu_features *cpu_features)
  145. {
  146. /* Only initialize once. */
  147. if (cpu_features->hwcap != 0)
  148. return;
  149. /* Fill cpu_features as passed by kernel and machine. */
  150. cpu_features->hwcap = GLRO(dl_hwcap);
  151. /* We want just 1 double word to be returned. */
  152. if (__glibc_likely ((cpu_features->hwcap & HWCAP_S390_STFLE)
  153. && (cpu_features->hwcap & HWCAP_S390_ZARCH)
  154. && (cpu_features->hwcap & HWCAP_S390_HIGH_GPRS)))
  155. {
  156. unsigned long long stfle_bits[4] = { 0 };
  157. register unsigned long reg0 __asm__("0") = 3;
  158. __asm__ __volatile__(".machine push" "\n\t"
  159. ".machine \"z9-109\"" "\n\t"
  160. ".machinemode \"zarch_nohighgprs\"\n\t"
  161. "stfle %0" "\n\t"
  162. ".machine pop" "\n"
  163. : "=QS" (stfle_bits[0]),
  164. "+d" (reg0)
  165. : : "cc");
  166. unsigned long long internal_stfle_bits = 0;
  167. /* Facility bit 34: z10: General instructions extension. */
  168. if ((stfle_bits[0] & (1ULL << (63 - 34))) != 0)
  169. internal_stfle_bits |= S390_STFLE_BIT34_Z10;
  170. /* Facility bit 45: z196: Distinct operands, popcount, ... */
  171. if ((stfle_bits[0] & (1ULL << (63 - 45))) != 0)
  172. internal_stfle_bits |= S390_STFLE_BIT45_Z196;
  173. /* Facility bit 61: arch13/z15: Miscellaneous-Instruction-Extensions
  174. Facility 3, e.g. mvcrl. */
  175. if ((stfle_bits[0] & (1ULL << (63 - 61))) != 0)
  176. internal_stfle_bits |= S390_STFLE_BIT61_ARCH13_MIE3;
  177. /* Facility bit 84: arch15/z17: Miscellaneous-instruction-extensions 4 */
  178. if ((stfle_bits[1] & (1ULL << (127 - 84))) != 0)
  179. internal_stfle_bits |= S390_STFLE_BIT84_ARCH15_MIE4;
  180. /* Facility bit 198: arch15/z17: Vector-enhancements-facility 3 */
  181. if ((stfle_bits[3] & (1ULL << (255 - 198))) != 0)
  182. internal_stfle_bits |= S390_STFLE_BIT198_ARCH15_VXRS_EXT3;
  183. /* Facility bit 199: arch15/z17: Vector-Packed-Decimal-Enhancement 3 */
  184. if ((stfle_bits[3] & (1ULL << (255 - 199))) != 0)
  185. internal_stfle_bits |= S390_STFLE_BIT199_ARCH15_VXRS_PDE3;
  186. /* Facility bit 201: arch15/z17: CPU: Concurrent-Functions Facility */
  187. if ((stfle_bits[3] & (1ULL << (255 - 201))) != 0)
  188. internal_stfle_bits |= S390_STFLE_BIT201_ARCH15_CON;
  189. cpu_features->stfle_orig = internal_stfle_bits;
  190. cpu_features->stfle_filtered = internal_stfle_bits;
  191. }
  192. }
  193. static inline void
  194. init_cpu_features (struct cpu_features *cpu_features)
  195. {
  196. init_cpu_features_no_tunables (cpu_features);
  197. TUNABLE_GET (glibc, cpu, hwcaps, tunable_val_t *, TUNABLE_CALLBACK (set_hwcaps));
  198. }