tm-signal-context-chk-gpr.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_GPR_REGS 18
  32. long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
  33. static sig_atomic_t fail;
  34. static long gps[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  35. -1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18};
  36. static void signal_usr1(int signum, siginfo_t *info, void *uc)
  37. {
  38. int i;
  39. ucontext_t *ucp = uc;
  40. ucontext_t *tm_ucp = ucp->uc_link;
  41. for (i = 0; i < NV_GPR_REGS && !fail; i++) {
  42. fail = (ucp->uc_mcontext.gp_regs[i + 14] != gps[i]);
  43. fail |= (tm_ucp->uc_mcontext.gp_regs[i + 14] != gps[i + NV_GPR_REGS]);
  44. if (fail)
  45. printf("Failed on %d GPR %lu or %lu\n", i,
  46. ucp->uc_mcontext.gp_regs[i + 14], tm_ucp->uc_mcontext.gp_regs[i + 14]);
  47. }
  48. }
  49. static int tm_signal_context_chk_gpr()
  50. {
  51. struct sigaction act;
  52. int i;
  53. long rc;
  54. pid_t pid = getpid();
  55. SKIP_IF(!have_htm());
  56. act.sa_sigaction = signal_usr1;
  57. sigemptyset(&act.sa_mask);
  58. act.sa_flags = SA_SIGINFO;
  59. if (sigaction(SIGUSR1, &act, NULL) < 0) {
  60. perror("sigaction sigusr1");
  61. exit(1);
  62. }
  63. i = 0;
  64. while (i < MAX_ATTEMPT && !fail) {
  65. rc = tm_signal_self_context_load(pid, gps, NULL, NULL, NULL);
  66. FAIL_IF(rc != pid);
  67. i++;
  68. }
  69. return fail;
  70. }
  71. int main(void)
  72. {
  73. return test_harness(tm_signal_context_chk_gpr, "tm_signal_context_chk_gpr");
  74. }