amx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <err.h>
  4. #include <errno.h>
  5. #include <setjmp.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdbool.h>
  9. #include <unistd.h>
  10. #include <x86intrin.h>
  11. #include <sys/auxv.h>
  12. #include <sys/mman.h>
  13. #include <sys/shm.h>
  14. #include <sys/syscall.h>
  15. #include <sys/wait.h>
  16. #include "helpers.h"
  17. #include "xstate.h"
  18. #ifndef __x86_64__
  19. # error This test is 64-bit only
  20. #endif
  21. /* err() exits and will not return */
  22. #define fatal_error(msg, ...) err(1, "[FAIL]\t" msg, ##__VA_ARGS__)
  23. #define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG)
  24. #define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA)
  25. #define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA)
  26. struct xstate_info xtiledata;
  27. /* The helpers for managing XSAVE buffer and tile states: */
  28. struct xsave_buffer *stashed_xsave;
  29. static void init_stashed_xsave(void)
  30. {
  31. stashed_xsave = alloc_xbuf();
  32. if (!stashed_xsave)
  33. fatal_error("failed to allocate stashed_xsave\n");
  34. clear_xstate_header(stashed_xsave);
  35. }
  36. static void free_stashed_xsave(void)
  37. {
  38. free(stashed_xsave);
  39. }
  40. /* Work around printf() being unsafe in signals: */
  41. #define SIGNAL_BUF_LEN 1000
  42. char signal_message_buffer[SIGNAL_BUF_LEN];
  43. void sig_print(char *msg)
  44. {
  45. int left = SIGNAL_BUF_LEN - strlen(signal_message_buffer) - 1;
  46. strncat(signal_message_buffer, msg, left);
  47. }
  48. static volatile bool noperm_signaled;
  49. static int noperm_errs;
  50. /*
  51. * Signal handler for when AMX is used but
  52. * permission has not been obtained.
  53. */
  54. static void handle_noperm(int sig, siginfo_t *si, void *ctx_void)
  55. {
  56. ucontext_t *ctx = (ucontext_t *)ctx_void;
  57. void *xbuf = ctx->uc_mcontext.fpregs;
  58. struct _fpx_sw_bytes *sw_bytes;
  59. uint64_t features;
  60. /* Reset the signal message buffer: */
  61. signal_message_buffer[0] = '\0';
  62. sig_print("\tAt SIGILL handler,\n");
  63. if (si->si_code != ILL_ILLOPC) {
  64. noperm_errs++;
  65. sig_print("[FAIL]\tInvalid signal code.\n");
  66. } else {
  67. sig_print("[OK]\tValid signal code (ILL_ILLOPC).\n");
  68. }
  69. sw_bytes = get_fpx_sw_bytes(xbuf);
  70. /*
  71. * Without permission, the signal XSAVE buffer should not
  72. * have room for AMX register state (aka. xtiledata).
  73. * Check that the size does not overlap with where xtiledata
  74. * will reside.
  75. *
  76. * This also implies that no state components *PAST*
  77. * XTILEDATA (features >=19) can be present in the buffer.
  78. */
  79. if (sw_bytes->xstate_size <= xtiledata.xbuf_offset) {
  80. sig_print("[OK]\tValid xstate size\n");
  81. } else {
  82. noperm_errs++;
  83. sig_print("[FAIL]\tInvalid xstate size\n");
  84. }
  85. features = get_fpx_sw_bytes_features(xbuf);
  86. /*
  87. * Without permission, the XTILEDATA feature
  88. * bit should not be set.
  89. */
  90. if ((features & XFEATURE_MASK_XTILEDATA) == 0) {
  91. sig_print("[OK]\tValid xstate mask\n");
  92. } else {
  93. noperm_errs++;
  94. sig_print("[FAIL]\tInvalid xstate mask\n");
  95. }
  96. noperm_signaled = true;
  97. ctx->uc_mcontext.gregs[REG_RIP] += 3; /* Skip the faulting XRSTOR */
  98. }
  99. /* Return true if XRSTOR is successful; otherwise, false. */
  100. static inline bool xrstor_safe(struct xsave_buffer *xbuf, uint64_t mask)
  101. {
  102. noperm_signaled = false;
  103. xrstor(xbuf, mask);
  104. /* Print any messages produced by the signal code: */
  105. printf("%s", signal_message_buffer);
  106. /*
  107. * Reset the buffer to make sure any future printing
  108. * only outputs new messages:
  109. */
  110. signal_message_buffer[0] = '\0';
  111. if (noperm_errs)
  112. fatal_error("saw %d errors in noperm signal handler\n", noperm_errs);
  113. return !noperm_signaled;
  114. }
  115. /*
  116. * Use XRSTOR to populate the XTILEDATA registers with
  117. * random data.
  118. *
  119. * Return true if successful; otherwise, false.
  120. */
  121. static inline bool load_rand_tiledata(struct xsave_buffer *xbuf)
  122. {
  123. clear_xstate_header(xbuf);
  124. set_xstatebv(xbuf, XFEATURE_MASK_XTILEDATA);
  125. set_rand_data(&xtiledata, xbuf);
  126. return xrstor_safe(xbuf, XFEATURE_MASK_XTILEDATA);
  127. }
  128. enum expected_result { FAIL_EXPECTED, SUCCESS_EXPECTED };
  129. /* arch_prctl() and sigaltstack() test */
  130. #define ARCH_GET_XCOMP_SUPP 0x1021
  131. #define ARCH_GET_XCOMP_PERM 0x1022
  132. #define ARCH_REQ_XCOMP_PERM 0x1023
  133. static void req_xtiledata_perm(void)
  134. {
  135. syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_PERM, XFEATURE_XTILEDATA);
  136. }
  137. static void validate_req_xcomp_perm(enum expected_result exp)
  138. {
  139. unsigned long bitmask, expected_bitmask;
  140. long rc;
  141. rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_PERM, &bitmask);
  142. if (rc) {
  143. fatal_error("prctl(ARCH_GET_XCOMP_PERM) error: %ld", rc);
  144. } else if (!(bitmask & XFEATURE_MASK_XTILECFG)) {
  145. fatal_error("ARCH_GET_XCOMP_PERM returns XFEATURE_XTILECFG off.");
  146. }
  147. rc = syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_PERM, XFEATURE_XTILEDATA);
  148. if (exp == FAIL_EXPECTED) {
  149. if (rc) {
  150. printf("[OK]\tARCH_REQ_XCOMP_PERM saw expected failure..\n");
  151. return;
  152. }
  153. fatal_error("ARCH_REQ_XCOMP_PERM saw unexpected success.\n");
  154. } else if (rc) {
  155. fatal_error("ARCH_REQ_XCOMP_PERM saw unexpected failure.\n");
  156. }
  157. expected_bitmask = bitmask | XFEATURE_MASK_XTILEDATA;
  158. rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_PERM, &bitmask);
  159. if (rc) {
  160. fatal_error("prctl(ARCH_GET_XCOMP_PERM) error: %ld", rc);
  161. } else if (bitmask != expected_bitmask) {
  162. fatal_error("ARCH_REQ_XCOMP_PERM set a wrong bitmask: %lx, expected: %lx.\n",
  163. bitmask, expected_bitmask);
  164. } else {
  165. printf("\tARCH_REQ_XCOMP_PERM is successful.\n");
  166. }
  167. }
  168. static void validate_xcomp_perm(enum expected_result exp)
  169. {
  170. bool load_success = load_rand_tiledata(stashed_xsave);
  171. if (exp == FAIL_EXPECTED) {
  172. if (load_success) {
  173. noperm_errs++;
  174. printf("[FAIL]\tLoad tiledata succeeded.\n");
  175. } else {
  176. printf("[OK]\tLoad tiledata failed.\n");
  177. }
  178. } else if (exp == SUCCESS_EXPECTED) {
  179. if (load_success) {
  180. printf("[OK]\tLoad tiledata succeeded.\n");
  181. } else {
  182. noperm_errs++;
  183. printf("[FAIL]\tLoad tiledata failed.\n");
  184. }
  185. }
  186. }
  187. #ifndef AT_MINSIGSTKSZ
  188. # define AT_MINSIGSTKSZ 51
  189. #endif
  190. static void *alloc_altstack(unsigned int size)
  191. {
  192. void *altstack;
  193. altstack = mmap(NULL, size, PROT_READ | PROT_WRITE,
  194. MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
  195. if (altstack == MAP_FAILED)
  196. fatal_error("mmap() for altstack");
  197. return altstack;
  198. }
  199. static void setup_altstack(void *addr, unsigned long size, enum expected_result exp)
  200. {
  201. stack_t ss;
  202. int rc;
  203. memset(&ss, 0, sizeof(ss));
  204. ss.ss_size = size;
  205. ss.ss_sp = addr;
  206. rc = sigaltstack(&ss, NULL);
  207. if (exp == FAIL_EXPECTED) {
  208. if (rc) {
  209. printf("[OK]\tsigaltstack() failed.\n");
  210. } else {
  211. fatal_error("sigaltstack() succeeded unexpectedly.\n");
  212. }
  213. } else if (rc) {
  214. fatal_error("sigaltstack()");
  215. }
  216. }
  217. static void test_dynamic_sigaltstack(void)
  218. {
  219. unsigned int small_size, enough_size;
  220. unsigned long minsigstksz;
  221. void *altstack;
  222. minsigstksz = getauxval(AT_MINSIGSTKSZ);
  223. printf("\tAT_MINSIGSTKSZ = %lu\n", minsigstksz);
  224. /*
  225. * getauxval() itself can return 0 for failure or
  226. * success. But, in this case, AT_MINSIGSTKSZ
  227. * will always return a >=0 value if implemented.
  228. * Just check for 0.
  229. */
  230. if (minsigstksz == 0) {
  231. printf("no support for AT_MINSIGSTKSZ, skipping sigaltstack tests\n");
  232. return;
  233. }
  234. enough_size = minsigstksz * 2;
  235. altstack = alloc_altstack(enough_size);
  236. printf("\tAllocate memory for altstack (%u bytes).\n", enough_size);
  237. /*
  238. * Try setup_altstack() with a size which can not fit
  239. * XTILEDATA. ARCH_REQ_XCOMP_PERM should fail.
  240. */
  241. small_size = minsigstksz - xtiledata.size;
  242. printf("\tAfter sigaltstack() with small size (%u bytes).\n", small_size);
  243. setup_altstack(altstack, small_size, SUCCESS_EXPECTED);
  244. validate_req_xcomp_perm(FAIL_EXPECTED);
  245. /*
  246. * Try setup_altstack() with a size derived from
  247. * AT_MINSIGSTKSZ. It should be more than large enough
  248. * and thus ARCH_REQ_XCOMP_PERM should succeed.
  249. */
  250. printf("\tAfter sigaltstack() with enough size (%u bytes).\n", enough_size);
  251. setup_altstack(altstack, enough_size, SUCCESS_EXPECTED);
  252. validate_req_xcomp_perm(SUCCESS_EXPECTED);
  253. /*
  254. * Try to coerce setup_altstack() to again accept a
  255. * too-small altstack. This ensures that big-enough
  256. * sigaltstacks can not shrink to a too-small value
  257. * once XTILEDATA permission is established.
  258. */
  259. printf("\tThen, sigaltstack() with small size (%u bytes).\n", small_size);
  260. setup_altstack(altstack, small_size, FAIL_EXPECTED);
  261. }
  262. static void test_dynamic_state(void)
  263. {
  264. pid_t parent, child, grandchild;
  265. parent = fork();
  266. if (parent < 0) {
  267. /* fork() failed */
  268. fatal_error("fork");
  269. } else if (parent > 0) {
  270. int status;
  271. /* fork() succeeded. Now in the parent. */
  272. wait(&status);
  273. if (!WIFEXITED(status) || WEXITSTATUS(status))
  274. fatal_error("arch_prctl test parent exit");
  275. return;
  276. }
  277. /* fork() succeeded. Now in the child . */
  278. printf("[RUN]\tCheck ARCH_REQ_XCOMP_PERM around process fork() and sigaltack() test.\n");
  279. printf("\tFork a child.\n");
  280. child = fork();
  281. if (child < 0) {
  282. fatal_error("fork");
  283. } else if (child > 0) {
  284. int status;
  285. wait(&status);
  286. if (!WIFEXITED(status) || WEXITSTATUS(status))
  287. fatal_error("arch_prctl test child exit");
  288. _exit(0);
  289. }
  290. /*
  291. * The permission request should fail without an
  292. * XTILEDATA-compatible signal stack
  293. */
  294. printf("\tTest XCOMP_PERM at child.\n");
  295. validate_xcomp_perm(FAIL_EXPECTED);
  296. /*
  297. * Set up an XTILEDATA-compatible signal stack and
  298. * also obtain permission to populate XTILEDATA.
  299. */
  300. printf("\tTest dynamic sigaltstack at child:\n");
  301. test_dynamic_sigaltstack();
  302. /* Ensure that XTILEDATA can be populated. */
  303. printf("\tTest XCOMP_PERM again at child.\n");
  304. validate_xcomp_perm(SUCCESS_EXPECTED);
  305. printf("\tFork a grandchild.\n");
  306. grandchild = fork();
  307. if (grandchild < 0) {
  308. /* fork() failed */
  309. fatal_error("fork");
  310. } else if (!grandchild) {
  311. /* fork() succeeded. Now in the (grand)child. */
  312. printf("\tTest XCOMP_PERM at grandchild.\n");
  313. /*
  314. * Ensure that the grandchild inherited
  315. * permission and a compatible sigaltstack:
  316. */
  317. validate_xcomp_perm(SUCCESS_EXPECTED);
  318. } else {
  319. int status;
  320. /* fork() succeeded. Now in the parent. */
  321. wait(&status);
  322. if (!WIFEXITED(status) || WEXITSTATUS(status))
  323. fatal_error("fork test grandchild");
  324. }
  325. _exit(0);
  326. }
  327. static inline int __compare_tiledata_state(struct xsave_buffer *xbuf1, struct xsave_buffer *xbuf2)
  328. {
  329. return memcmp(&xbuf1->bytes[xtiledata.xbuf_offset],
  330. &xbuf2->bytes[xtiledata.xbuf_offset],
  331. xtiledata.size);
  332. }
  333. /*
  334. * Save current register state and compare it to @xbuf1.'
  335. *
  336. * Returns false if @xbuf1 matches the registers.
  337. * Returns true if @xbuf1 differs from the registers.
  338. */
  339. static inline bool __validate_tiledata_regs(struct xsave_buffer *xbuf1)
  340. {
  341. struct xsave_buffer *xbuf2;
  342. int ret;
  343. xbuf2 = alloc_xbuf();
  344. if (!xbuf2)
  345. fatal_error("failed to allocate XSAVE buffer\n");
  346. xsave(xbuf2, XFEATURE_MASK_XTILEDATA);
  347. ret = __compare_tiledata_state(xbuf1, xbuf2);
  348. free(xbuf2);
  349. if (ret == 0)
  350. return false;
  351. return true;
  352. }
  353. static inline void validate_tiledata_regs_changed(struct xsave_buffer *xbuf)
  354. {
  355. int ret = __validate_tiledata_regs(xbuf);
  356. if (ret == 0)
  357. fatal_error("TILEDATA registers did not change");
  358. }
  359. /* tiledata inheritance test */
  360. static void test_fork(void)
  361. {
  362. pid_t child, grandchild;
  363. child = fork();
  364. if (child < 0) {
  365. /* fork() failed */
  366. fatal_error("fork");
  367. } else if (child > 0) {
  368. /* fork() succeeded. Now in the parent. */
  369. int status;
  370. wait(&status);
  371. if (!WIFEXITED(status) || WEXITSTATUS(status))
  372. fatal_error("fork test child");
  373. return;
  374. }
  375. /* fork() succeeded. Now in the child. */
  376. printf("[RUN]\tCheck tile data inheritance.\n\tBefore fork(), load tiledata\n");
  377. load_rand_tiledata(stashed_xsave);
  378. grandchild = fork();
  379. if (grandchild < 0) {
  380. /* fork() failed */
  381. fatal_error("fork");
  382. } else if (grandchild > 0) {
  383. /* fork() succeeded. Still in the first child. */
  384. int status;
  385. wait(&status);
  386. if (!WIFEXITED(status) || WEXITSTATUS(status))
  387. fatal_error("fork test grand child");
  388. _exit(0);
  389. }
  390. /* fork() succeeded. Now in the (grand)child. */
  391. /*
  392. * TILEDATA registers are not preserved across fork().
  393. * Ensure that their value has changed:
  394. */
  395. validate_tiledata_regs_changed(stashed_xsave);
  396. _exit(0);
  397. }
  398. int main(void)
  399. {
  400. unsigned long features;
  401. long rc;
  402. rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_SUPP, &features);
  403. if (rc || (features & XFEATURE_MASK_XTILE) != XFEATURE_MASK_XTILE) {
  404. ksft_print_msg("no AMX support\n");
  405. return KSFT_SKIP;
  406. }
  407. xtiledata = get_xstate_info(XFEATURE_XTILEDATA);
  408. if (!xtiledata.size || !xtiledata.xbuf_offset) {
  409. fatal_error("xstate cpuid: invalid tile data size/offset: %d/%d",
  410. xtiledata.size, xtiledata.xbuf_offset);
  411. }
  412. init_stashed_xsave();
  413. sethandler(SIGILL, handle_noperm, 0);
  414. test_dynamic_state();
  415. /* Request permission for the following tests */
  416. req_xtiledata_perm();
  417. test_fork();
  418. /*
  419. * Perform generic xstate tests for context switching, ptrace,
  420. * and signal.
  421. */
  422. test_xstate(XFEATURE_XTILEDATA);
  423. clearhandler(SIGILL);
  424. free_stashed_xsave();
  425. return 0;
  426. }