syscall-abi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2021 ARM Limited.
  4. */
  5. #include <errno.h>
  6. #include <stdbool.h>
  7. #include <stddef.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/auxv.h>
  13. #include <sys/prctl.h>
  14. #include <asm/hwcap.h>
  15. #include <asm/sigcontext.h>
  16. #include <asm/unistd.h>
  17. #include "../../kselftest.h"
  18. #include "syscall-abi.h"
  19. #define NUM_VL ((SVE_VQ_MAX - SVE_VQ_MIN) + 1)
  20. static int default_sme_vl;
  21. extern void do_syscall(int sve_vl, int sme_vl);
  22. static void fill_random(void *buf, size_t size)
  23. {
  24. int i;
  25. uint32_t *lbuf = buf;
  26. /* random() returns a 32 bit number regardless of the size of long */
  27. for (i = 0; i < size / sizeof(uint32_t); i++)
  28. lbuf[i] = random();
  29. }
  30. /*
  31. * We also repeat the test for several syscalls to try to expose different
  32. * behaviour.
  33. */
  34. static struct syscall_cfg {
  35. int syscall_nr;
  36. const char *name;
  37. } syscalls[] = {
  38. { __NR_getpid, "getpid()" },
  39. { __NR_sched_yield, "sched_yield()" },
  40. };
  41. #define NUM_GPR 31
  42. uint64_t gpr_in[NUM_GPR];
  43. uint64_t gpr_out[NUM_GPR];
  44. static void setup_gpr(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  45. uint64_t svcr)
  46. {
  47. fill_random(gpr_in, sizeof(gpr_in));
  48. gpr_in[8] = cfg->syscall_nr;
  49. memset(gpr_out, 0, sizeof(gpr_out));
  50. }
  51. static int check_gpr(struct syscall_cfg *cfg, int sve_vl, int sme_vl, uint64_t svcr)
  52. {
  53. int errors = 0;
  54. int i;
  55. /*
  56. * GPR x0-x7 may be clobbered, and all others should be preserved.
  57. */
  58. for (i = 9; i < ARRAY_SIZE(gpr_in); i++) {
  59. if (gpr_in[i] != gpr_out[i]) {
  60. ksft_print_msg("%s SVE VL %d mismatch in GPR %d: %llx != %llx\n",
  61. cfg->name, sve_vl, i,
  62. gpr_in[i], gpr_out[i]);
  63. errors++;
  64. }
  65. }
  66. return errors;
  67. }
  68. #define NUM_FPR 32
  69. uint64_t fpr_in[NUM_FPR * 2];
  70. uint64_t fpr_out[NUM_FPR * 2];
  71. static void setup_fpr(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  72. uint64_t svcr)
  73. {
  74. fill_random(fpr_in, sizeof(fpr_in));
  75. memset(fpr_out, 0, sizeof(fpr_out));
  76. }
  77. static int check_fpr(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  78. uint64_t svcr)
  79. {
  80. int errors = 0;
  81. int i;
  82. if (!sve_vl) {
  83. for (i = 0; i < ARRAY_SIZE(fpr_in); i++) {
  84. if (fpr_in[i] != fpr_out[i]) {
  85. ksft_print_msg("%s Q%d/%d mismatch %llx != %llx\n",
  86. cfg->name,
  87. i / 2, i % 2,
  88. fpr_in[i], fpr_out[i]);
  89. errors++;
  90. }
  91. }
  92. }
  93. return errors;
  94. }
  95. #define SVE_Z_SHARED_BYTES (128 / 8)
  96. static uint8_t z_zero[__SVE_ZREG_SIZE(SVE_VQ_MAX)];
  97. uint8_t z_in[SVE_NUM_ZREGS * __SVE_ZREG_SIZE(SVE_VQ_MAX)];
  98. uint8_t z_out[SVE_NUM_ZREGS * __SVE_ZREG_SIZE(SVE_VQ_MAX)];
  99. static void setup_z(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  100. uint64_t svcr)
  101. {
  102. fill_random(z_in, sizeof(z_in));
  103. fill_random(z_out, sizeof(z_out));
  104. }
  105. static int check_z(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  106. uint64_t svcr)
  107. {
  108. size_t reg_size = sve_vl;
  109. int errors = 0;
  110. int i;
  111. if (!sve_vl)
  112. return 0;
  113. for (i = 0; i < SVE_NUM_ZREGS; i++) {
  114. uint8_t *in = &z_in[reg_size * i];
  115. uint8_t *out = &z_out[reg_size * i];
  116. if (svcr & SVCR_SM_MASK) {
  117. /*
  118. * In streaming mode the whole register should
  119. * be cleared by the transition out of
  120. * streaming mode.
  121. */
  122. if (memcmp(z_zero, out, reg_size) != 0) {
  123. ksft_print_msg("%s SVE VL %d Z%d non-zero\n",
  124. cfg->name, sve_vl, i);
  125. errors++;
  126. }
  127. } else {
  128. /*
  129. * For standard SVE the low 128 bits should be
  130. * preserved and any additional bits cleared.
  131. */
  132. if (memcmp(in, out, SVE_Z_SHARED_BYTES) != 0) {
  133. ksft_print_msg("%s SVE VL %d Z%d low 128 bits changed\n",
  134. cfg->name, sve_vl, i);
  135. errors++;
  136. }
  137. if (reg_size > SVE_Z_SHARED_BYTES &&
  138. (memcmp(z_zero, out + SVE_Z_SHARED_BYTES,
  139. reg_size - SVE_Z_SHARED_BYTES) != 0)) {
  140. ksft_print_msg("%s SVE VL %d Z%d high bits non-zero\n",
  141. cfg->name, sve_vl, i);
  142. errors++;
  143. }
  144. }
  145. }
  146. return errors;
  147. }
  148. uint8_t p_in[SVE_NUM_PREGS * __SVE_PREG_SIZE(SVE_VQ_MAX)];
  149. uint8_t p_out[SVE_NUM_PREGS * __SVE_PREG_SIZE(SVE_VQ_MAX)];
  150. static void setup_p(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  151. uint64_t svcr)
  152. {
  153. fill_random(p_in, sizeof(p_in));
  154. fill_random(p_out, sizeof(p_out));
  155. }
  156. static int check_p(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  157. uint64_t svcr)
  158. {
  159. size_t reg_size = sve_vq_from_vl(sve_vl) * 2; /* 1 bit per VL byte */
  160. int errors = 0;
  161. int i;
  162. if (!sve_vl)
  163. return 0;
  164. /* After a syscall the P registers should be zeroed */
  165. for (i = 0; i < SVE_NUM_PREGS * reg_size; i++)
  166. if (p_out[i])
  167. errors++;
  168. if (errors)
  169. ksft_print_msg("%s SVE VL %d predicate registers non-zero\n",
  170. cfg->name, sve_vl);
  171. return errors;
  172. }
  173. uint8_t ffr_in[__SVE_PREG_SIZE(SVE_VQ_MAX)];
  174. uint8_t ffr_out[__SVE_PREG_SIZE(SVE_VQ_MAX)];
  175. static void setup_ffr(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  176. uint64_t svcr)
  177. {
  178. /*
  179. * If we are in streaming mode and do not have FA64 then FFR
  180. * is unavailable.
  181. */
  182. if ((svcr & SVCR_SM_MASK) &&
  183. !(getauxval(AT_HWCAP2) & HWCAP2_SME_FA64)) {
  184. memset(&ffr_in, 0, sizeof(ffr_in));
  185. return;
  186. }
  187. /*
  188. * It is only valid to set a contiguous set of bits starting
  189. * at 0. For now since we're expecting this to be cleared by
  190. * a syscall just set all bits.
  191. */
  192. memset(ffr_in, 0xff, sizeof(ffr_in));
  193. fill_random(ffr_out, sizeof(ffr_out));
  194. }
  195. static int check_ffr(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  196. uint64_t svcr)
  197. {
  198. size_t reg_size = sve_vq_from_vl(sve_vl) * 2; /* 1 bit per VL byte */
  199. int errors = 0;
  200. int i;
  201. if (!sve_vl)
  202. return 0;
  203. if ((svcr & SVCR_SM_MASK) &&
  204. !(getauxval(AT_HWCAP2) & HWCAP2_SME_FA64))
  205. return 0;
  206. /* After a syscall FFR should be zeroed */
  207. for (i = 0; i < reg_size; i++)
  208. if (ffr_out[i])
  209. errors++;
  210. if (errors)
  211. ksft_print_msg("%s SVE VL %d FFR non-zero\n",
  212. cfg->name, sve_vl);
  213. return errors;
  214. }
  215. uint64_t svcr_in, svcr_out;
  216. static void setup_svcr(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  217. uint64_t svcr)
  218. {
  219. svcr_in = svcr;
  220. }
  221. static int check_svcr(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  222. uint64_t svcr)
  223. {
  224. int errors = 0;
  225. if (svcr_out & SVCR_SM_MASK) {
  226. ksft_print_msg("%s Still in SM, SVCR %llx\n",
  227. cfg->name, svcr_out);
  228. errors++;
  229. }
  230. if ((svcr_in & SVCR_ZA_MASK) != (svcr_out & SVCR_ZA_MASK)) {
  231. ksft_print_msg("%s PSTATE.ZA changed, SVCR %llx != %llx\n",
  232. cfg->name, svcr_in, svcr_out);
  233. errors++;
  234. }
  235. return errors;
  236. }
  237. uint8_t za_in[SVE_NUM_PREGS * __SVE_ZREG_SIZE(SVE_VQ_MAX)];
  238. uint8_t za_out[SVE_NUM_PREGS * __SVE_ZREG_SIZE(SVE_VQ_MAX)];
  239. static void setup_za(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  240. uint64_t svcr)
  241. {
  242. fill_random(za_in, sizeof(za_in));
  243. memset(za_out, 0, sizeof(za_out));
  244. }
  245. static int check_za(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  246. uint64_t svcr)
  247. {
  248. size_t reg_size = sme_vl * sme_vl;
  249. int errors = 0;
  250. if (!(svcr & SVCR_ZA_MASK))
  251. return 0;
  252. if (memcmp(za_in, za_out, reg_size) != 0) {
  253. ksft_print_msg("SME VL %d ZA does not match\n", sme_vl);
  254. errors++;
  255. }
  256. return errors;
  257. }
  258. typedef void (*setup_fn)(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  259. uint64_t svcr);
  260. typedef int (*check_fn)(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  261. uint64_t svcr);
  262. /*
  263. * Each set of registers has a setup function which is called before
  264. * the syscall to fill values in a global variable for loading by the
  265. * test code and a check function which validates that the results are
  266. * as expected. Vector lengths are passed everywhere, a vector length
  267. * of 0 should be treated as do not test.
  268. */
  269. static struct {
  270. setup_fn setup;
  271. check_fn check;
  272. } regset[] = {
  273. { setup_gpr, check_gpr },
  274. { setup_fpr, check_fpr },
  275. { setup_z, check_z },
  276. { setup_p, check_p },
  277. { setup_ffr, check_ffr },
  278. { setup_svcr, check_svcr },
  279. { setup_za, check_za },
  280. };
  281. static bool do_test(struct syscall_cfg *cfg, int sve_vl, int sme_vl,
  282. uint64_t svcr)
  283. {
  284. int errors = 0;
  285. int i;
  286. for (i = 0; i < ARRAY_SIZE(regset); i++)
  287. regset[i].setup(cfg, sve_vl, sme_vl, svcr);
  288. do_syscall(sve_vl, sme_vl);
  289. for (i = 0; i < ARRAY_SIZE(regset); i++)
  290. errors += regset[i].check(cfg, sve_vl, sme_vl, svcr);
  291. return errors == 0;
  292. }
  293. static void test_one_syscall(struct syscall_cfg *cfg)
  294. {
  295. int sve_vq, sve_vl;
  296. int sme_vq, sme_vl;
  297. /* FPSIMD only case */
  298. ksft_test_result(do_test(cfg, 0, default_sme_vl, 0),
  299. "%s FPSIMD\n", cfg->name);
  300. if (!(getauxval(AT_HWCAP) & HWCAP_SVE))
  301. return;
  302. for (sve_vq = SVE_VQ_MAX; sve_vq > 0; --sve_vq) {
  303. sve_vl = prctl(PR_SVE_SET_VL, sve_vq * 16);
  304. if (sve_vl == -1)
  305. ksft_exit_fail_msg("PR_SVE_SET_VL failed: %s (%d)\n",
  306. strerror(errno), errno);
  307. sve_vl &= PR_SVE_VL_LEN_MASK;
  308. if (sve_vq != sve_vq_from_vl(sve_vl))
  309. sve_vq = sve_vq_from_vl(sve_vl);
  310. ksft_test_result(do_test(cfg, sve_vl, default_sme_vl, 0),
  311. "%s SVE VL %d\n", cfg->name, sve_vl);
  312. if (!(getauxval(AT_HWCAP2) & HWCAP2_SME))
  313. continue;
  314. for (sme_vq = SVE_VQ_MAX; sme_vq > 0; --sme_vq) {
  315. sme_vl = prctl(PR_SME_SET_VL, sme_vq * 16);
  316. if (sme_vl == -1)
  317. ksft_exit_fail_msg("PR_SME_SET_VL failed: %s (%d)\n",
  318. strerror(errno), errno);
  319. sme_vl &= PR_SME_VL_LEN_MASK;
  320. /* Found lowest VL */
  321. if (sve_vq_from_vl(sme_vl) > sme_vq)
  322. break;
  323. if (sme_vq != sve_vq_from_vl(sme_vl))
  324. sme_vq = sve_vq_from_vl(sme_vl);
  325. ksft_test_result(do_test(cfg, sve_vl, sme_vl,
  326. SVCR_ZA_MASK | SVCR_SM_MASK),
  327. "%s SVE VL %d/SME VL %d SM+ZA\n",
  328. cfg->name, sve_vl, sme_vl);
  329. ksft_test_result(do_test(cfg, sve_vl, sme_vl,
  330. SVCR_SM_MASK),
  331. "%s SVE VL %d/SME VL %d SM\n",
  332. cfg->name, sve_vl, sme_vl);
  333. ksft_test_result(do_test(cfg, sve_vl, sme_vl,
  334. SVCR_ZA_MASK),
  335. "%s SVE VL %d/SME VL %d ZA\n",
  336. cfg->name, sve_vl, sme_vl);
  337. }
  338. }
  339. }
  340. int sve_count_vls(void)
  341. {
  342. unsigned int vq;
  343. int vl_count = 0;
  344. int vl;
  345. if (!(getauxval(AT_HWCAP) & HWCAP_SVE))
  346. return 0;
  347. /*
  348. * Enumerate up to SVE_VQ_MAX vector lengths
  349. */
  350. for (vq = SVE_VQ_MAX; vq > 0; --vq) {
  351. vl = prctl(PR_SVE_SET_VL, vq * 16);
  352. if (vl == -1)
  353. ksft_exit_fail_msg("PR_SVE_SET_VL failed: %s (%d)\n",
  354. strerror(errno), errno);
  355. vl &= PR_SVE_VL_LEN_MASK;
  356. if (vq != sve_vq_from_vl(vl))
  357. vq = sve_vq_from_vl(vl);
  358. vl_count++;
  359. }
  360. return vl_count;
  361. }
  362. int sme_count_vls(void)
  363. {
  364. unsigned int vq;
  365. int vl_count = 0;
  366. int vl;
  367. if (!(getauxval(AT_HWCAP2) & HWCAP2_SME))
  368. return 0;
  369. /* Ensure we configure a SME VL, used to flag if SVCR is set */
  370. default_sme_vl = 16;
  371. /*
  372. * Enumerate up to SVE_VQ_MAX vector lengths
  373. */
  374. for (vq = SVE_VQ_MAX; vq > 0; --vq) {
  375. vl = prctl(PR_SME_SET_VL, vq * 16);
  376. if (vl == -1)
  377. ksft_exit_fail_msg("PR_SME_SET_VL failed: %s (%d)\n",
  378. strerror(errno), errno);
  379. vl &= PR_SME_VL_LEN_MASK;
  380. /* Found lowest VL */
  381. if (sve_vq_from_vl(vl) > vq)
  382. break;
  383. if (vq != sve_vq_from_vl(vl))
  384. vq = sve_vq_from_vl(vl);
  385. vl_count++;
  386. }
  387. return vl_count;
  388. }
  389. int main(void)
  390. {
  391. int i;
  392. int tests = 1; /* FPSIMD */
  393. srandom(getpid());
  394. ksft_print_header();
  395. tests += sve_count_vls();
  396. tests += (sve_count_vls() * sme_count_vls()) * 3;
  397. ksft_set_plan(ARRAY_SIZE(syscalls) * tests);
  398. if (getauxval(AT_HWCAP2) & HWCAP2_SME_FA64)
  399. ksft_print_msg("SME with FA64\n");
  400. else if (getauxval(AT_HWCAP2) & HWCAP2_SME)
  401. ksft_print_msg("SME without FA64\n");
  402. for (i = 0; i < ARRAY_SIZE(syscalls); i++)
  403. test_one_syscall(&syscalls[i]);
  404. ksft_print_cnts();
  405. return 0;
  406. }