tm-signal-context-chk-vsx.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <string.h>
  26. #include <signal.h>
  27. #include <unistd.h>
  28. #include <altivec.h>
  29. #include "utils.h"
  30. #include "tm.h"
  31. #define MAX_ATTEMPT 500000
  32. #define NV_VSX_REGS 12
  33. long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
  34. static sig_atomic_t fail;
  35. vector int vss[] = {
  36. {1, 2, 3, 4 },{5, 6, 7, 8 },{9, 10,11,12},
  37. {13,14,15,16},{17,18,19,20},{21,22,23,24},
  38. {25,26,27,28},{29,30,31,32},{33,34,35,36},
  39. {37,38,39,40},{41,42,43,44},{45,46,47,48},
  40. {-1, -2, -3, -4 },{-5, -6, -7, -8 },{-9, -10,-11,-12},
  41. {-13,-14,-15,-16},{-17,-18,-19,-20},{-21,-22,-23,-24},
  42. {-25,-26,-27,-28},{-29,-30,-31,-32},{-33,-34,-35,-36},
  43. {-37,-38,-39,-40},{-41,-42,-43,-44},{-45,-46,-47,-48}
  44. };
  45. static void signal_usr1(int signum, siginfo_t *info, void *uc)
  46. {
  47. int i;
  48. uint8_t vsc[sizeof(vector int)];
  49. uint8_t vst[sizeof(vector int)];
  50. ucontext_t *ucp = uc;
  51. ucontext_t *tm_ucp = ucp->uc_link;
  52. /*
  53. * The other half of the VSX regs will be after v_regs.
  54. *
  55. * In short, vmx_reserve array holds everything. v_regs is a 16
  56. * byte aligned pointer at the start of vmx_reserve (vmx_reserve
  57. * may or may not be 16 aligned) where the v_regs structure exists.
  58. * (half of) The VSX regsters are directly after v_regs so the
  59. * easiest way to find them below.
  60. */
  61. long *vsx_ptr = (long *)(ucp->uc_mcontext.v_regs + 1);
  62. long *tm_vsx_ptr = (long *)(tm_ucp->uc_mcontext.v_regs + 1);
  63. for (i = 0; i < NV_VSX_REGS && !fail; i++) {
  64. memcpy(vsc, &ucp->uc_mcontext.fp_regs[i + 20], 8);
  65. memcpy(vsc + 8, &vsx_ptr[20 + i], 8);
  66. fail = memcmp(vsc, &vss[i], sizeof(vector int));
  67. memcpy(vst, &tm_ucp->uc_mcontext.fp_regs[i + 20], 8);
  68. memcpy(vst + 8, &tm_vsx_ptr[20 + i], 8);
  69. fail |= memcmp(vst, &vss[i + NV_VSX_REGS], sizeof(vector int));
  70. if (fail) {
  71. int j;
  72. fprintf(stderr, "Failed on %d vsx 0x", i);
  73. for (j = 0; j < 16; j++)
  74. fprintf(stderr, "%02x", vsc[j]);
  75. fprintf(stderr, " vs 0x");
  76. for (j = 0; j < 16; j++)
  77. fprintf(stderr, "%02x", vst[j]);
  78. fprintf(stderr, "\n");
  79. }
  80. }
  81. }
  82. static int tm_signal_context_chk()
  83. {
  84. struct sigaction act;
  85. int i;
  86. long rc;
  87. pid_t pid = getpid();
  88. SKIP_IF(!have_htm());
  89. act.sa_sigaction = signal_usr1;
  90. sigemptyset(&act.sa_mask);
  91. act.sa_flags = SA_SIGINFO;
  92. if (sigaction(SIGUSR1, &act, NULL) < 0) {
  93. perror("sigaction sigusr1");
  94. exit(1);
  95. }
  96. i = 0;
  97. while (i < MAX_ATTEMPT && !fail) {
  98. rc = tm_signal_self_context_load(pid, NULL, NULL, NULL, vss);
  99. FAIL_IF(rc != pid);
  100. i++;
  101. }
  102. return fail;
  103. }
  104. int main(void)
  105. {
  106. return test_harness(tm_signal_context_chk, "tm_signal_context_chk_vsx");
  107. }