hwcap.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2022 ARM Limited.
  4. */
  5. #include <errno.h>
  6. #include <signal.h>
  7. #include <stdbool.h>
  8. #include <stddef.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/auxv.h>
  14. #include <sys/prctl.h>
  15. #include <asm/hwcap.h>
  16. #include <asm/sigcontext.h>
  17. #include <asm/unistd.h>
  18. #include "../../kselftest.h"
  19. #define TESTS_PER_HWCAP 2
  20. /*
  21. * Function expected to generate SIGILL when the feature is not
  22. * supported and return when it is supported. If SIGILL is generated
  23. * then the handler must be able to skip over the instruction safely.
  24. *
  25. * Note that it is expected that for many architecture extensions
  26. * there are no specific traps due to no architecture state being
  27. * added so we may not fault if running on a kernel which doesn't know
  28. * to add the hwcap.
  29. */
  30. typedef void (*sigill_fn)(void);
  31. static void rng_sigill(void)
  32. {
  33. asm volatile("mrs x0, S3_3_C2_C4_0" : : : "x0");
  34. }
  35. static void sme_sigill(void)
  36. {
  37. /* RDSVL x0, #0 */
  38. asm volatile(".inst 0x04bf5800" : : : "x0");
  39. }
  40. static void sve_sigill(void)
  41. {
  42. /* RDVL x0, #0 */
  43. asm volatile(".inst 0x04bf5000" : : : "x0");
  44. }
  45. static void sve2_sigill(void)
  46. {
  47. /* SQABS Z0.b, P0/M, Z0.B */
  48. asm volatile(".inst 0x4408A000" : : : "z0");
  49. }
  50. static void sveaes_sigill(void)
  51. {
  52. /* AESD z0.b, z0.b, z0.b */
  53. asm volatile(".inst 0x4522e400" : : : "z0");
  54. }
  55. static void svepmull_sigill(void)
  56. {
  57. /* PMULLB Z0.Q, Z0.D, Z0.D */
  58. asm volatile(".inst 0x45006800" : : : "z0");
  59. }
  60. static void svebitperm_sigill(void)
  61. {
  62. /* BDEP Z0.B, Z0.B, Z0.B */
  63. asm volatile(".inst 0x4500b400" : : : "z0");
  64. }
  65. static void svesha3_sigill(void)
  66. {
  67. /* EOR3 Z0.D, Z0.D, Z0.D, Z0.D */
  68. asm volatile(".inst 0x4203800" : : : "z0");
  69. }
  70. static void svesm4_sigill(void)
  71. {
  72. /* SM4E Z0.S, Z0.S, Z0.S */
  73. asm volatile(".inst 0x4523e000" : : : "z0");
  74. }
  75. static void svei8mm_sigill(void)
  76. {
  77. /* USDOT Z0.S, Z0.B, Z0.B[0] */
  78. asm volatile(".inst 0x44a01800" : : : "z0");
  79. }
  80. static void svef32mm_sigill(void)
  81. {
  82. /* FMMLA Z0.S, Z0.S, Z0.S */
  83. asm volatile(".inst 0x64a0e400" : : : "z0");
  84. }
  85. static void svef64mm_sigill(void)
  86. {
  87. /* FMMLA Z0.D, Z0.D, Z0.D */
  88. asm volatile(".inst 0x64e0e400" : : : "z0");
  89. }
  90. static void svebf16_sigill(void)
  91. {
  92. /* BFCVT Z0.H, P0/M, Z0.S */
  93. asm volatile(".inst 0x658aa000" : : : "z0");
  94. }
  95. static const struct hwcap_data {
  96. const char *name;
  97. unsigned long at_hwcap;
  98. unsigned long hwcap_bit;
  99. const char *cpuinfo;
  100. sigill_fn sigill_fn;
  101. bool sigill_reliable;
  102. } hwcaps[] = {
  103. {
  104. .name = "RNG",
  105. .at_hwcap = AT_HWCAP2,
  106. .hwcap_bit = HWCAP2_RNG,
  107. .cpuinfo = "rng",
  108. .sigill_fn = rng_sigill,
  109. },
  110. {
  111. .name = "SME",
  112. .at_hwcap = AT_HWCAP2,
  113. .hwcap_bit = HWCAP2_SME,
  114. .cpuinfo = "sme",
  115. .sigill_fn = sme_sigill,
  116. .sigill_reliable = true,
  117. },
  118. {
  119. .name = "SVE",
  120. .at_hwcap = AT_HWCAP,
  121. .hwcap_bit = HWCAP_SVE,
  122. .cpuinfo = "sve",
  123. .sigill_fn = sve_sigill,
  124. .sigill_reliable = true,
  125. },
  126. {
  127. .name = "SVE 2",
  128. .at_hwcap = AT_HWCAP2,
  129. .hwcap_bit = HWCAP2_SVE2,
  130. .cpuinfo = "sve2",
  131. .sigill_fn = sve2_sigill,
  132. },
  133. {
  134. .name = "SVE AES",
  135. .at_hwcap = AT_HWCAP2,
  136. .hwcap_bit = HWCAP2_SVEAES,
  137. .cpuinfo = "sveaes",
  138. .sigill_fn = sveaes_sigill,
  139. },
  140. {
  141. .name = "SVE2 PMULL",
  142. .at_hwcap = AT_HWCAP2,
  143. .hwcap_bit = HWCAP2_SVEPMULL,
  144. .cpuinfo = "svepmull",
  145. .sigill_fn = svepmull_sigill,
  146. },
  147. {
  148. .name = "SVE2 BITPERM",
  149. .at_hwcap = AT_HWCAP2,
  150. .hwcap_bit = HWCAP2_SVEBITPERM,
  151. .cpuinfo = "svebitperm",
  152. .sigill_fn = svebitperm_sigill,
  153. },
  154. {
  155. .name = "SVE2 SHA3",
  156. .at_hwcap = AT_HWCAP2,
  157. .hwcap_bit = HWCAP2_SVESHA3,
  158. .cpuinfo = "svesha3",
  159. .sigill_fn = svesha3_sigill,
  160. },
  161. {
  162. .name = "SVE2 SM4",
  163. .at_hwcap = AT_HWCAP2,
  164. .hwcap_bit = HWCAP2_SVESM4,
  165. .cpuinfo = "svesm4",
  166. .sigill_fn = svesm4_sigill,
  167. },
  168. {
  169. .name = "SVE2 I8MM",
  170. .at_hwcap = AT_HWCAP2,
  171. .hwcap_bit = HWCAP2_SVEI8MM,
  172. .cpuinfo = "svei8mm",
  173. .sigill_fn = svei8mm_sigill,
  174. },
  175. {
  176. .name = "SVE2 F32MM",
  177. .at_hwcap = AT_HWCAP2,
  178. .hwcap_bit = HWCAP2_SVEF32MM,
  179. .cpuinfo = "svef32mm",
  180. .sigill_fn = svef32mm_sigill,
  181. },
  182. {
  183. .name = "SVE2 F64MM",
  184. .at_hwcap = AT_HWCAP2,
  185. .hwcap_bit = HWCAP2_SVEF64MM,
  186. .cpuinfo = "svef64mm",
  187. .sigill_fn = svef64mm_sigill,
  188. },
  189. {
  190. .name = "SVE2 BF16",
  191. .at_hwcap = AT_HWCAP2,
  192. .hwcap_bit = HWCAP2_SVEBF16,
  193. .cpuinfo = "svebf16",
  194. .sigill_fn = svebf16_sigill,
  195. },
  196. {
  197. .name = "SVE2 EBF16",
  198. .at_hwcap = AT_HWCAP2,
  199. .hwcap_bit = HWCAP2_SVE_EBF16,
  200. .cpuinfo = "sveebf16",
  201. },
  202. };
  203. static bool seen_sigill;
  204. static void handle_sigill(int sig, siginfo_t *info, void *context)
  205. {
  206. ucontext_t *uc = context;
  207. seen_sigill = true;
  208. /* Skip over the offending instruction */
  209. uc->uc_mcontext.pc += 4;
  210. }
  211. bool cpuinfo_present(const char *name)
  212. {
  213. FILE *f;
  214. char buf[2048], name_space[30], name_newline[30];
  215. char *s;
  216. /*
  217. * The feature should appear with a leading space and either a
  218. * trailing space or a newline.
  219. */
  220. snprintf(name_space, sizeof(name_space), " %s ", name);
  221. snprintf(name_newline, sizeof(name_newline), " %s\n", name);
  222. f = fopen("/proc/cpuinfo", "r");
  223. if (!f) {
  224. ksft_print_msg("Failed to open /proc/cpuinfo\n");
  225. return false;
  226. }
  227. while (fgets(buf, sizeof(buf), f)) {
  228. /* Features: line? */
  229. if (strncmp(buf, "Features\t:", strlen("Features\t:")) != 0)
  230. continue;
  231. /* All CPUs should be symmetric, don't read any more */
  232. fclose(f);
  233. s = strstr(buf, name_space);
  234. if (s)
  235. return true;
  236. s = strstr(buf, name_newline);
  237. if (s)
  238. return true;
  239. return false;
  240. }
  241. ksft_print_msg("Failed to find Features in /proc/cpuinfo\n");
  242. fclose(f);
  243. return false;
  244. }
  245. int main(void)
  246. {
  247. const struct hwcap_data *hwcap;
  248. int i, ret;
  249. bool have_cpuinfo, have_hwcap;
  250. struct sigaction sa;
  251. ksft_print_header();
  252. ksft_set_plan(ARRAY_SIZE(hwcaps) * TESTS_PER_HWCAP);
  253. memset(&sa, 0, sizeof(sa));
  254. sa.sa_sigaction = handle_sigill;
  255. sa.sa_flags = SA_RESTART | SA_SIGINFO;
  256. sigemptyset(&sa.sa_mask);
  257. ret = sigaction(SIGILL, &sa, NULL);
  258. if (ret < 0)
  259. ksft_exit_fail_msg("Failed to install SIGILL handler: %s (%d)\n",
  260. strerror(errno), errno);
  261. for (i = 0; i < ARRAY_SIZE(hwcaps); i++) {
  262. hwcap = &hwcaps[i];
  263. have_hwcap = getauxval(hwcap->at_hwcap) & hwcap->hwcap_bit;
  264. have_cpuinfo = cpuinfo_present(hwcap->cpuinfo);
  265. if (have_hwcap)
  266. ksft_print_msg("%s present\n", hwcap->name);
  267. ksft_test_result(have_hwcap == have_cpuinfo,
  268. "cpuinfo_match_%s\n", hwcap->name);
  269. if (hwcap->sigill_fn) {
  270. seen_sigill = false;
  271. hwcap->sigill_fn();
  272. if (have_hwcap) {
  273. /* Should be able to use the extension */
  274. ksft_test_result(!seen_sigill, "sigill_%s\n",
  275. hwcap->name);
  276. } else if (hwcap->sigill_reliable) {
  277. /* Guaranteed a SIGILL */
  278. ksft_test_result(seen_sigill, "sigill_%s\n",
  279. hwcap->name);
  280. } else {
  281. /* Missing SIGILL might be fine */
  282. ksft_print_msg("SIGILL %sreported for %s\n",
  283. seen_sigill ? "" : "not ",
  284. hwcap->name);
  285. ksft_test_result_skip("sigill_%s\n",
  286. hwcap->name);
  287. }
  288. } else {
  289. ksft_test_result_skip("sigill_%s\n",
  290. hwcap->name);
  291. }
  292. }
  293. ksft_print_cnts();
  294. return 0;
  295. }