tm-signal-context-chk-fpu.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2016, Cyril Bur, IBM Corp.
  4. *
  5. * Test the kernel's signal frame code.
  6. *
  7. * The kernel sets up two sets of ucontexts if the signal was to be
  8. * delivered while the thread was in a transaction (referred too as
  9. * first and second contexts).
  10. * Expected behaviour is that the checkpointed state is in the user
  11. * context passed to the signal handler (first context). The speculated
  12. * state can be accessed with the uc_link pointer (second context).
  13. *
  14. * The rationale for this is that if TM unaware code (which linked
  15. * against TM libs) installs a signal handler it will not know of the
  16. * speculative nature of the 'live' registers and may infer the wrong
  17. * thing.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <signal.h>
  22. #include <unistd.h>
  23. #include <altivec.h>
  24. #include "utils.h"
  25. #include "tm.h"
  26. #define MAX_ATTEMPT 500000
  27. #define NV_FPU_REGS 18 /* Number of non-volatile FP registers */
  28. #define FPR14 14 /* First non-volatile FP register to check in f14-31 subset */
  29. long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
  30. /* Test only non-volatile registers, i.e. 18 fpr registers from f14 to f31 */
  31. static double fps[] = {
  32. /* First context will be set with these values, i.e. non-speculative */
  33. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  34. /* Second context will be set with these values, i.e. speculative */
  35. -1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18
  36. };
  37. static sig_atomic_t fail, broken;
  38. static void signal_usr1(int signum, siginfo_t *info, void *uc)
  39. {
  40. int i;
  41. ucontext_t *ucp = uc;
  42. ucontext_t *tm_ucp = ucp->uc_link;
  43. for (i = 0; i < NV_FPU_REGS; i++) {
  44. /* Check first context. Print all mismatches. */
  45. fail = (ucp->uc_mcontext.fp_regs[FPR14 + i] != fps[i]);
  46. if (fail) {
  47. broken = 1;
  48. printf("FPR%d (1st context) == %g instead of %g (expected)\n",
  49. FPR14 + i, ucp->uc_mcontext.fp_regs[FPR14 + i], fps[i]);
  50. }
  51. }
  52. for (i = 0; i < NV_FPU_REGS; i++) {
  53. /* Check second context. Print all mismatches. */
  54. fail = (tm_ucp->uc_mcontext.fp_regs[FPR14 + i] != fps[NV_FPU_REGS + i]);
  55. if (fail) {
  56. broken = 1;
  57. printf("FPR%d (2nd context) == %g instead of %g (expected)\n",
  58. FPR14 + i, tm_ucp->uc_mcontext.fp_regs[FPR14 + i], fps[NV_FPU_REGS + i]);
  59. }
  60. }
  61. }
  62. static int tm_signal_context_chk_fpu()
  63. {
  64. struct sigaction act;
  65. int i;
  66. long rc;
  67. pid_t pid = getpid();
  68. SKIP_IF(!have_htm());
  69. SKIP_IF(htm_is_synthetic());
  70. act.sa_sigaction = signal_usr1;
  71. sigemptyset(&act.sa_mask);
  72. act.sa_flags = SA_SIGINFO;
  73. if (sigaction(SIGUSR1, &act, NULL) < 0) {
  74. perror("sigaction sigusr1");
  75. exit(1);
  76. }
  77. i = 0;
  78. while (i < MAX_ATTEMPT && !broken) {
  79. /*
  80. * tm_signal_self_context_load will set both first and second
  81. * contexts accordingly to the values passed through non-NULL
  82. * array pointers to it, in that case 'fps', and invoke the
  83. * signal handler installed for SIGUSR1.
  84. */
  85. rc = tm_signal_self_context_load(pid, NULL, fps, NULL, NULL);
  86. FAIL_IF(rc != pid);
  87. i++;
  88. }
  89. return (broken);
  90. }
  91. int main(void)
  92. {
  93. return test_harness(tm_signal_context_chk_fpu, "tm_signal_context_chk_fpu");
  94. }