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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2016, Cyril Bur, IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. *
  10. * Test the kernel's signal frame code.
  11. *
  12. * The kernel sets up two sets of ucontexts if the signal was to be
  13. * delivered while the thread was in a transaction.
  14. * Expected behaviour is that the checkpointed state is in the user
  15. * context passed to the signal handler. The speculated state can be
  16. * accessed with the uc_link pointer.
  17. *
  18. * The rationale for this is that if TM unaware code (which linked
  19. * against TM libs) installs a signal handler it will not know of the
  20. * speculative nature of the 'live' registers and may infer the wrong
  21. * thing.
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <signal.h>
  26. #include <unistd.h>
  27. #include <altivec.h>
  28. #include "utils.h"
  29. #include "tm.h"
  30. #define MAX_ATTEMPT 500000
  31. #define NV_FPU_REGS 18
  32. long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
  33. /* Be sure there are 2x as many as there are NV FPU regs (2x18) */
  34. static double fps[] = {
  35. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  36. -1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18
  37. };
  38. static sig_atomic_t fail;
  39. static void signal_usr1(int signum, siginfo_t *info, void *uc)
  40. {
  41. int i;
  42. ucontext_t *ucp = uc;
  43. ucontext_t *tm_ucp = ucp->uc_link;
  44. for (i = 0; i < NV_FPU_REGS && !fail; i++) {
  45. fail = (ucp->uc_mcontext.fp_regs[i + 14] != fps[i]);
  46. fail |= (tm_ucp->uc_mcontext.fp_regs[i + 14] != fps[i + NV_FPU_REGS]);
  47. if (fail)
  48. printf("Failed on %d FP %g or %g\n", i, ucp->uc_mcontext.fp_regs[i + 14], tm_ucp->uc_mcontext.fp_regs[i + 14]);
  49. }
  50. }
  51. static int tm_signal_context_chk_fpu()
  52. {
  53. struct sigaction act;
  54. int i;
  55. long rc;
  56. pid_t pid = getpid();
  57. SKIP_IF(!have_htm());
  58. act.sa_sigaction = signal_usr1;
  59. sigemptyset(&act.sa_mask);
  60. act.sa_flags = SA_SIGINFO;
  61. if (sigaction(SIGUSR1, &act, NULL) < 0) {
  62. perror("sigaction sigusr1");
  63. exit(1);
  64. }
  65. i = 0;
  66. while (i < MAX_ATTEMPT && !fail) {
  67. rc = tm_signal_self_context_load(pid, NULL, fps, NULL, NULL);
  68. FAIL_IF(rc != pid);
  69. i++;
  70. }
  71. return fail;
  72. }
  73. int main(void)
  74. {
  75. return test_harness(tm_signal_context_chk_fpu, "tm_signal_context_chk_fpu");
  76. }