cpu-features.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /* Initialize CPU feature data.
  2. This file is part of the GNU C Library.
  3. Copyright (C) 2008-2019 Free Software Foundation, Inc.
  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. <http://www.gnu.org/licenses/>. */
  15. #include <cpuid.h>
  16. #include <cpu-features.h>
  17. #include <dl-hwcap.h>
  18. #include <libc-pointer-arith.h>
  19. #if HAVE_TUNABLES
  20. # define TUNABLE_NAMESPACE cpu
  21. # include <unistd.h> /* Get STDOUT_FILENO for _dl_printf. */
  22. # include <elf/dl-tunables.h>
  23. extern void TUNABLE_CALLBACK (set_hwcaps) (tunable_val_t *)
  24. attribute_hidden;
  25. # if CET_ENABLED
  26. extern void TUNABLE_CALLBACK (set_x86_ibt) (tunable_val_t *)
  27. attribute_hidden;
  28. extern void TUNABLE_CALLBACK (set_x86_shstk) (tunable_val_t *)
  29. attribute_hidden;
  30. # endif
  31. #endif
  32. #if CET_ENABLED
  33. # include <dl-cet.h>
  34. # include <cet-tunables.h>
  35. #endif
  36. static void
  37. get_extended_indices (struct cpu_features *cpu_features)
  38. {
  39. unsigned int eax, ebx, ecx, edx;
  40. __cpuid (0x80000000, eax, ebx, ecx, edx);
  41. if (eax >= 0x80000001)
  42. __cpuid (0x80000001,
  43. cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].eax,
  44. cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].ebx,
  45. cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].ecx,
  46. cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].edx);
  47. if (eax >= 0x80000007)
  48. __cpuid (0x80000007,
  49. cpu_features->cpuid[COMMON_CPUID_INDEX_80000007].eax,
  50. cpu_features->cpuid[COMMON_CPUID_INDEX_80000007].ebx,
  51. cpu_features->cpuid[COMMON_CPUID_INDEX_80000007].ecx,
  52. cpu_features->cpuid[COMMON_CPUID_INDEX_80000007].edx);
  53. if (eax >= 0x80000008)
  54. __cpuid (0x80000008,
  55. cpu_features->cpuid[COMMON_CPUID_INDEX_80000008].eax,
  56. cpu_features->cpuid[COMMON_CPUID_INDEX_80000008].ebx,
  57. cpu_features->cpuid[COMMON_CPUID_INDEX_80000008].ecx,
  58. cpu_features->cpuid[COMMON_CPUID_INDEX_80000008].edx);
  59. }
  60. static void
  61. get_common_indices (struct cpu_features *cpu_features,
  62. unsigned int *family, unsigned int *model,
  63. unsigned int *extended_model, unsigned int *stepping)
  64. {
  65. if (family)
  66. {
  67. unsigned int eax;
  68. __cpuid (1, eax, cpu_features->cpuid[COMMON_CPUID_INDEX_1].ebx,
  69. cpu_features->cpuid[COMMON_CPUID_INDEX_1].ecx,
  70. cpu_features->cpuid[COMMON_CPUID_INDEX_1].edx);
  71. cpu_features->cpuid[COMMON_CPUID_INDEX_1].eax = eax;
  72. *family = (eax >> 8) & 0x0f;
  73. *model = (eax >> 4) & 0x0f;
  74. *extended_model = (eax >> 12) & 0xf0;
  75. *stepping = eax & 0x0f;
  76. if (*family == 0x0f)
  77. {
  78. *family += (eax >> 20) & 0xff;
  79. *model += *extended_model;
  80. }
  81. }
  82. if (cpu_features->basic.max_cpuid >= 7)
  83. __cpuid_count (7, 0,
  84. cpu_features->cpuid[COMMON_CPUID_INDEX_7].eax,
  85. cpu_features->cpuid[COMMON_CPUID_INDEX_7].ebx,
  86. cpu_features->cpuid[COMMON_CPUID_INDEX_7].ecx,
  87. cpu_features->cpuid[COMMON_CPUID_INDEX_7].edx);
  88. if (cpu_features->basic.max_cpuid >= 0xd)
  89. __cpuid_count (0xd, 1,
  90. cpu_features->cpuid[COMMON_CPUID_INDEX_D_ECX_1].eax,
  91. cpu_features->cpuid[COMMON_CPUID_INDEX_D_ECX_1].ebx,
  92. cpu_features->cpuid[COMMON_CPUID_INDEX_D_ECX_1].ecx,
  93. cpu_features->cpuid[COMMON_CPUID_INDEX_D_ECX_1].edx);
  94. /* Can we call xgetbv? */
  95. if (CPU_FEATURES_CPU_P (cpu_features, OSXSAVE))
  96. {
  97. unsigned int xcrlow;
  98. unsigned int xcrhigh;
  99. asm ("xgetbv" : "=a" (xcrlow), "=d" (xcrhigh) : "c" (0));
  100. /* Is YMM and XMM state usable? */
  101. if ((xcrlow & (bit_YMM_state | bit_XMM_state)) ==
  102. (bit_YMM_state | bit_XMM_state))
  103. {
  104. /* Determine if AVX is usable. */
  105. if (CPU_FEATURES_CPU_P (cpu_features, AVX))
  106. {
  107. cpu_features->feature[index_arch_AVX_Usable]
  108. |= bit_arch_AVX_Usable;
  109. /* The following features depend on AVX being usable. */
  110. /* Determine if AVX2 is usable. */
  111. if (CPU_FEATURES_CPU_P (cpu_features, AVX2))
  112. {
  113. cpu_features->feature[index_arch_AVX2_Usable]
  114. |= bit_arch_AVX2_Usable;
  115. /* Unaligned load with 256-bit AVX registers are faster on
  116. Intel/AMD processors with AVX2. */
  117. cpu_features->feature[index_arch_AVX_Fast_Unaligned_Load]
  118. |= bit_arch_AVX_Fast_Unaligned_Load;
  119. }
  120. /* Determine if FMA is usable. */
  121. if (CPU_FEATURES_CPU_P (cpu_features, FMA))
  122. cpu_features->feature[index_arch_FMA_Usable]
  123. |= bit_arch_FMA_Usable;
  124. /* Determine if VAES is usable. */
  125. if (CPU_FEATURES_CPU_P (cpu_features, VAES))
  126. cpu_features->feature[index_arch_VAES_Usable]
  127. |= bit_arch_VAES_Usable;
  128. /* Determine if VPCLMULQDQ is usable. */
  129. if (CPU_FEATURES_CPU_P (cpu_features, VPCLMULQDQ))
  130. cpu_features->feature[index_arch_VPCLMULQDQ_Usable]
  131. |= bit_arch_VPCLMULQDQ_Usable;
  132. /* Determine if XOP is usable. */
  133. if (CPU_FEATURES_CPU_P (cpu_features, XOP))
  134. cpu_features->feature[index_arch_XOP_Usable]
  135. |= bit_arch_XOP_Usable;
  136. }
  137. /* Check if OPMASK state, upper 256-bit of ZMM0-ZMM15 and
  138. ZMM16-ZMM31 state are enabled. */
  139. if ((xcrlow & (bit_Opmask_state | bit_ZMM0_15_state
  140. | bit_ZMM16_31_state)) ==
  141. (bit_Opmask_state | bit_ZMM0_15_state | bit_ZMM16_31_state))
  142. {
  143. /* Determine if AVX512F is usable. */
  144. if (CPU_FEATURES_CPU_P (cpu_features, AVX512F))
  145. {
  146. cpu_features->feature[index_arch_AVX512F_Usable]
  147. |= bit_arch_AVX512F_Usable;
  148. /* Determine if AVX512CD is usable. */
  149. if (CPU_FEATURES_CPU_P (cpu_features, AVX512CD))
  150. cpu_features->feature[index_arch_AVX512CD_Usable]
  151. |= bit_arch_AVX512CD_Usable;
  152. /* Determine if AVX512ER is usable. */
  153. if (CPU_FEATURES_CPU_P (cpu_features, AVX512ER))
  154. cpu_features->feature[index_arch_AVX512ER_Usable]
  155. |= bit_arch_AVX512ER_Usable;
  156. /* Determine if AVX512PF is usable. */
  157. if (CPU_FEATURES_CPU_P (cpu_features, AVX512PF))
  158. cpu_features->feature[index_arch_AVX512PF_Usable]
  159. |= bit_arch_AVX512PF_Usable;
  160. /* Determine if AVX512VL is usable. */
  161. if (CPU_FEATURES_CPU_P (cpu_features, AVX512VL))
  162. cpu_features->feature[index_arch_AVX512VL_Usable]
  163. |= bit_arch_AVX512VL_Usable;
  164. /* Determine if AVX512DQ is usable. */
  165. if (CPU_FEATURES_CPU_P (cpu_features, AVX512DQ))
  166. cpu_features->feature[index_arch_AVX512DQ_Usable]
  167. |= bit_arch_AVX512DQ_Usable;
  168. /* Determine if AVX512BW is usable. */
  169. if (CPU_FEATURES_CPU_P (cpu_features, AVX512BW))
  170. cpu_features->feature[index_arch_AVX512BW_Usable]
  171. |= bit_arch_AVX512BW_Usable;
  172. /* Determine if AVX512_4FMAPS is usable. */
  173. if (CPU_FEATURES_CPU_P (cpu_features, AVX512_4FMAPS))
  174. cpu_features->feature[index_arch_AVX512_4FMAPS_Usable]
  175. |= bit_arch_AVX512_4FMAPS_Usable;
  176. /* Determine if AVX512_4VNNIW is usable. */
  177. if (CPU_FEATURES_CPU_P (cpu_features, AVX512_4VNNIW))
  178. cpu_features->feature[index_arch_AVX512_4VNNIW_Usable]
  179. |= bit_arch_AVX512_4VNNIW_Usable;
  180. /* Determine if AVX512_BITALG is usable. */
  181. if (CPU_FEATURES_CPU_P (cpu_features, AVX512_BITALG))
  182. cpu_features->feature[index_arch_AVX512_BITALG_Usable]
  183. |= bit_arch_AVX512_BITALG_Usable;
  184. /* Determine if AVX512_IFMA is usable. */
  185. if (CPU_FEATURES_CPU_P (cpu_features, AVX512_IFMA))
  186. cpu_features->feature[index_arch_AVX512_IFMA_Usable]
  187. |= bit_arch_AVX512_IFMA_Usable;
  188. /* Determine if AVX512_VBMI is usable. */
  189. if (CPU_FEATURES_CPU_P (cpu_features, AVX512_VBMI))
  190. cpu_features->feature[index_arch_AVX512_VBMI_Usable]
  191. |= bit_arch_AVX512_VBMI_Usable;
  192. /* Determine if AVX512_VBMI2 is usable. */
  193. if (CPU_FEATURES_CPU_P (cpu_features, AVX512_VBMI2))
  194. cpu_features->feature[index_arch_AVX512_VBMI2_Usable]
  195. |= bit_arch_AVX512_VBMI2_Usable;
  196. /* Determine if is AVX512_VNNI usable. */
  197. if (CPU_FEATURES_CPU_P (cpu_features, AVX512_VNNI))
  198. cpu_features->feature[index_arch_AVX512_VNNI_Usable]
  199. |= bit_arch_AVX512_VNNI_Usable;
  200. /* Determine if AVX512_VPOPCNTDQ is usable. */
  201. if (CPU_FEATURES_CPU_P (cpu_features, AVX512_VPOPCNTDQ))
  202. cpu_features->feature[index_arch_AVX512_VPOPCNTDQ_Usable]
  203. |= bit_arch_AVX512_VPOPCNTDQ_Usable;
  204. }
  205. }
  206. }
  207. /* For _dl_runtime_resolve, set xsave_state_size to xsave area
  208. size + integer register save size and align it to 64 bytes. */
  209. if (cpu_features->basic.max_cpuid >= 0xd)
  210. {
  211. unsigned int eax, ebx, ecx, edx;
  212. __cpuid_count (0xd, 0, eax, ebx, ecx, edx);
  213. if (ebx != 0)
  214. {
  215. unsigned int xsave_state_full_size
  216. = ALIGN_UP (ebx + STATE_SAVE_OFFSET, 64);
  217. cpu_features->xsave_state_size
  218. = xsave_state_full_size;
  219. cpu_features->xsave_state_full_size
  220. = xsave_state_full_size;
  221. /* Check if XSAVEC is available. */
  222. if (CPU_FEATURES_CPU_P (cpu_features, XSAVEC))
  223. {
  224. unsigned int xstate_comp_offsets[32];
  225. unsigned int xstate_comp_sizes[32];
  226. unsigned int i;
  227. xstate_comp_offsets[0] = 0;
  228. xstate_comp_offsets[1] = 160;
  229. xstate_comp_offsets[2] = 576;
  230. xstate_comp_sizes[0] = 160;
  231. xstate_comp_sizes[1] = 256;
  232. for (i = 2; i < 32; i++)
  233. {
  234. if ((STATE_SAVE_MASK & (1 << i)) != 0)
  235. {
  236. __cpuid_count (0xd, i, eax, ebx, ecx, edx);
  237. xstate_comp_sizes[i] = eax;
  238. }
  239. else
  240. {
  241. ecx = 0;
  242. xstate_comp_sizes[i] = 0;
  243. }
  244. if (i > 2)
  245. {
  246. xstate_comp_offsets[i]
  247. = (xstate_comp_offsets[i - 1]
  248. + xstate_comp_sizes[i -1]);
  249. if ((ecx & (1 << 1)) != 0)
  250. xstate_comp_offsets[i]
  251. = ALIGN_UP (xstate_comp_offsets[i], 64);
  252. }
  253. }
  254. /* Use XSAVEC. */
  255. unsigned int size
  256. = xstate_comp_offsets[31] + xstate_comp_sizes[31];
  257. if (size)
  258. {
  259. cpu_features->xsave_state_size
  260. = ALIGN_UP (size + STATE_SAVE_OFFSET, 64);
  261. cpu_features->feature[index_arch_XSAVEC_Usable]
  262. |= bit_arch_XSAVEC_Usable;
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. _Static_assert (((index_arch_Fast_Unaligned_Load
  270. == index_arch_Fast_Unaligned_Copy)
  271. && (index_arch_Fast_Unaligned_Load
  272. == index_arch_Prefer_PMINUB_for_stringop)
  273. && (index_arch_Fast_Unaligned_Load
  274. == index_arch_Slow_SSE4_2)
  275. && (index_arch_Fast_Unaligned_Load
  276. == index_arch_Fast_Rep_String)
  277. && (index_arch_Fast_Unaligned_Load
  278. == index_arch_Fast_Copy_Backward)),
  279. "Incorrect index_arch_Fast_Unaligned_Load");
  280. static inline void
  281. init_cpu_features (struct cpu_features *cpu_features)
  282. {
  283. unsigned int ebx, ecx, edx;
  284. unsigned int family = 0;
  285. unsigned int model = 0;
  286. unsigned int stepping = 0;
  287. enum cpu_features_kind kind;
  288. #if !HAS_CPUID
  289. if (__get_cpuid_max (0, 0) == 0)
  290. {
  291. kind = arch_kind_other;
  292. goto no_cpuid;
  293. }
  294. #endif
  295. __cpuid (0, cpu_features->basic.max_cpuid, ebx, ecx, edx);
  296. /* This spells out "GenuineIntel". */
  297. if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
  298. {
  299. unsigned int extended_model;
  300. kind = arch_kind_intel;
  301. get_common_indices (cpu_features, &family, &model, &extended_model,
  302. &stepping);
  303. get_extended_indices (cpu_features);
  304. if (family == 0x06)
  305. {
  306. model += extended_model;
  307. switch (model)
  308. {
  309. case 0x1c:
  310. case 0x26:
  311. /* BSF is slow on Atom. */
  312. cpu_features->feature[index_arch_Slow_BSF]
  313. |= bit_arch_Slow_BSF;
  314. break;
  315. case 0x57:
  316. /* Knights Landing. Enable Silvermont optimizations. */
  317. case 0x5c:
  318. case 0x5f:
  319. /* Unaligned load versions are faster than SSSE3
  320. on Goldmont. */
  321. case 0x4c:
  322. /* Airmont is a die shrink of Silvermont. */
  323. case 0x37:
  324. case 0x4a:
  325. case 0x4d:
  326. case 0x5a:
  327. case 0x5d:
  328. /* Unaligned load versions are faster than SSSE3
  329. on Silvermont. */
  330. cpu_features->feature[index_arch_Fast_Unaligned_Load]
  331. |= (bit_arch_Fast_Unaligned_Load
  332. | bit_arch_Fast_Unaligned_Copy
  333. | bit_arch_Prefer_PMINUB_for_stringop
  334. | bit_arch_Slow_SSE4_2);
  335. break;
  336. default:
  337. /* Unknown family 0x06 processors. Assuming this is one
  338. of Core i3/i5/i7 processors if AVX is available. */
  339. if (!CPU_FEATURES_CPU_P (cpu_features, AVX))
  340. break;
  341. case 0x1a:
  342. case 0x1e:
  343. case 0x1f:
  344. case 0x25:
  345. case 0x2c:
  346. case 0x2e:
  347. case 0x2f:
  348. /* Rep string instructions, unaligned load, unaligned copy,
  349. and pminub are fast on Intel Core i3, i5 and i7. */
  350. cpu_features->feature[index_arch_Fast_Rep_String]
  351. |= (bit_arch_Fast_Rep_String
  352. | bit_arch_Fast_Unaligned_Load
  353. | bit_arch_Fast_Unaligned_Copy
  354. | bit_arch_Prefer_PMINUB_for_stringop);
  355. break;
  356. }
  357. /* Disable TSX on some Haswell processors to avoid TSX on kernels that
  358. weren't updated with the latest microcode package (which disables
  359. broken feature by default). */
  360. switch (model)
  361. {
  362. case 0x3f:
  363. /* Xeon E7 v3 with stepping >= 4 has working TSX. */
  364. if (stepping >= 4)
  365. break;
  366. case 0x3c:
  367. case 0x45:
  368. case 0x46:
  369. /* Disable Intel TSX on Haswell processors (except Xeon E7 v3
  370. with stepping >= 4) to avoid TSX on kernels that weren't
  371. updated with the latest microcode package (which disables
  372. broken feature by default). */
  373. cpu_features->cpuid[index_cpu_RTM].reg_RTM &= ~bit_cpu_RTM;
  374. break;
  375. }
  376. }
  377. /* Since AVX512ER is unique to Xeon Phi, set Prefer_No_VZEROUPPER
  378. if AVX512ER is available. Don't use AVX512 to avoid lower CPU
  379. frequency if AVX512ER isn't available. */
  380. if (CPU_FEATURES_CPU_P (cpu_features, AVX512ER))
  381. cpu_features->feature[index_arch_Prefer_No_VZEROUPPER]
  382. |= bit_arch_Prefer_No_VZEROUPPER;
  383. else
  384. cpu_features->feature[index_arch_Prefer_No_AVX512]
  385. |= bit_arch_Prefer_No_AVX512;
  386. }
  387. /* This spells out "AuthenticAMD" or "HygonGenuine". */
  388. else if ((ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
  389. || (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e))
  390. {
  391. unsigned int extended_model;
  392. kind = arch_kind_amd;
  393. get_common_indices (cpu_features, &family, &model, &extended_model,
  394. &stepping);
  395. get_extended_indices (cpu_features);
  396. ecx = cpu_features->cpuid[COMMON_CPUID_INDEX_1].ecx;
  397. if (HAS_ARCH_FEATURE (AVX_Usable))
  398. {
  399. /* Since the FMA4 bit is in COMMON_CPUID_INDEX_80000001 and
  400. FMA4 requires AVX, determine if FMA4 is usable here. */
  401. if (CPU_FEATURES_CPU_P (cpu_features, FMA4))
  402. cpu_features->feature[index_arch_FMA4_Usable]
  403. |= bit_arch_FMA4_Usable;
  404. }
  405. if (family == 0x15)
  406. {
  407. /* "Excavator" */
  408. if (model >= 0x60 && model <= 0x7f)
  409. {
  410. cpu_features->feature[index_arch_Fast_Unaligned_Load]
  411. |= (bit_arch_Fast_Unaligned_Load
  412. | bit_arch_Fast_Copy_Backward);
  413. /* Unaligned AVX loads are slower.*/
  414. cpu_features->feature[index_arch_AVX_Fast_Unaligned_Load]
  415. &= ~bit_arch_AVX_Fast_Unaligned_Load;
  416. }
  417. }
  418. }
  419. else
  420. {
  421. kind = arch_kind_other;
  422. get_common_indices (cpu_features, NULL, NULL, NULL, NULL);
  423. }
  424. /* Support i586 if CX8 is available. */
  425. if (CPU_FEATURES_CPU_P (cpu_features, CX8))
  426. cpu_features->feature[index_arch_I586] |= bit_arch_I586;
  427. /* Support i686 if CMOV is available. */
  428. if (CPU_FEATURES_CPU_P (cpu_features, CMOV))
  429. cpu_features->feature[index_arch_I686] |= bit_arch_I686;
  430. #if !HAS_CPUID
  431. no_cpuid:
  432. #endif
  433. cpu_features->basic.kind = kind;
  434. cpu_features->basic.family = family;
  435. cpu_features->basic.model = model;
  436. cpu_features->basic.stepping = stepping;
  437. #if HAVE_TUNABLES
  438. TUNABLE_GET (hwcaps, tunable_val_t *, TUNABLE_CALLBACK (set_hwcaps));
  439. cpu_features->non_temporal_threshold
  440. = TUNABLE_GET (x86_non_temporal_threshold, long int, NULL);
  441. cpu_features->data_cache_size
  442. = TUNABLE_GET (x86_data_cache_size, long int, NULL);
  443. cpu_features->shared_cache_size
  444. = TUNABLE_GET (x86_shared_cache_size, long int, NULL);
  445. #endif
  446. /* Reuse dl_platform, dl_hwcap and dl_hwcap_mask for x86. */
  447. #if !HAVE_TUNABLES && defined SHARED
  448. /* The glibc.cpu.hwcap_mask tunable is initialized already, so no need to do
  449. this. */
  450. GLRO(dl_hwcap_mask) = HWCAP_IMPORTANT;
  451. #endif
  452. #ifdef __x86_64__
  453. GLRO(dl_hwcap) = HWCAP_X86_64;
  454. if (cpu_features->basic.kind == arch_kind_intel)
  455. {
  456. const char *platform = NULL;
  457. if (CPU_FEATURES_ARCH_P (cpu_features, AVX512F_Usable)
  458. && CPU_FEATURES_CPU_P (cpu_features, AVX512CD))
  459. {
  460. if (CPU_FEATURES_CPU_P (cpu_features, AVX512ER))
  461. {
  462. if (CPU_FEATURES_CPU_P (cpu_features, AVX512PF))
  463. platform = "xeon_phi";
  464. }
  465. else
  466. {
  467. if (CPU_FEATURES_CPU_P (cpu_features, AVX512BW)
  468. && CPU_FEATURES_CPU_P (cpu_features, AVX512DQ)
  469. && CPU_FEATURES_CPU_P (cpu_features, AVX512VL))
  470. GLRO(dl_hwcap) |= HWCAP_X86_AVX512_1;
  471. }
  472. }
  473. if (platform == NULL
  474. && CPU_FEATURES_ARCH_P (cpu_features, AVX2_Usable)
  475. && CPU_FEATURES_ARCH_P (cpu_features, FMA_Usable)
  476. && CPU_FEATURES_CPU_P (cpu_features, BMI1)
  477. && CPU_FEATURES_CPU_P (cpu_features, BMI2)
  478. && CPU_FEATURES_CPU_P (cpu_features, LZCNT)
  479. && CPU_FEATURES_CPU_P (cpu_features, MOVBE)
  480. && CPU_FEATURES_CPU_P (cpu_features, POPCNT))
  481. platform = "haswell";
  482. if (platform != NULL)
  483. GLRO(dl_platform) = platform;
  484. }
  485. #else
  486. GLRO(dl_hwcap) = 0;
  487. if (CPU_FEATURES_CPU_P (cpu_features, SSE2))
  488. GLRO(dl_hwcap) |= HWCAP_X86_SSE2;
  489. if (CPU_FEATURES_ARCH_P (cpu_features, I686))
  490. GLRO(dl_platform) = "i686";
  491. else if (CPU_FEATURES_ARCH_P (cpu_features, I586))
  492. GLRO(dl_platform) = "i586";
  493. #endif
  494. #if CET_ENABLED
  495. # if HAVE_TUNABLES
  496. TUNABLE_GET (x86_ibt, tunable_val_t *,
  497. TUNABLE_CALLBACK (set_x86_ibt));
  498. TUNABLE_GET (x86_shstk, tunable_val_t *,
  499. TUNABLE_CALLBACK (set_x86_shstk));
  500. # endif
  501. /* Check CET status. */
  502. unsigned int cet_status = get_cet_status ();
  503. if (cet_status)
  504. {
  505. GL(dl_x86_feature_1)[0] = cet_status;
  506. # ifndef SHARED
  507. /* Check if IBT and SHSTK are enabled by kernel. */
  508. if ((cet_status & GNU_PROPERTY_X86_FEATURE_1_IBT)
  509. || (cet_status & GNU_PROPERTY_X86_FEATURE_1_SHSTK))
  510. {
  511. /* Disable IBT and/or SHSTK if they are enabled by kernel, but
  512. disabled by environment variable:
  513. GLIBC_TUNABLES=glibc.cpu.hwcaps=-IBT,-SHSTK
  514. */
  515. unsigned int cet_feature = 0;
  516. if (!HAS_CPU_FEATURE (IBT))
  517. cet_feature |= GNU_PROPERTY_X86_FEATURE_1_IBT;
  518. if (!HAS_CPU_FEATURE (SHSTK))
  519. cet_feature |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
  520. if (cet_feature)
  521. {
  522. int res = dl_cet_disable_cet (cet_feature);
  523. /* Clear the disabled bits in dl_x86_feature_1. */
  524. if (res == 0)
  525. GL(dl_x86_feature_1)[0] &= ~cet_feature;
  526. }
  527. /* Lock CET if IBT or SHSTK is enabled in executable. Don't
  528. lock CET if SHSTK is enabled permissively. */
  529. if (((GL(dl_x86_feature_1)[1] >> CET_MAX)
  530. & ((1 << CET_MAX) - 1))
  531. != CET_PERMISSIVE)
  532. dl_cet_lock_cet ();
  533. }
  534. # endif
  535. }
  536. #endif
  537. }