tm-signal-context-chk-vmx.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <string.h>
  22. #include <signal.h>
  23. #include <unistd.h>
  24. #include <altivec.h>
  25. #include "utils.h"
  26. #include "tm.h"
  27. #define MAX_ATTEMPT 500000
  28. #define NV_VMX_REGS 12 /* Number of non-volatile VMX registers */
  29. #define VMX20 20 /* First non-volatile register to check in vr20-31 subset */
  30. long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
  31. static sig_atomic_t fail, broken;
  32. /* Test only non-volatile registers, i.e. 12 vmx registers from vr20 to vr31 */
  33. vector int vms[] = {
  34. /* First context will be set with these values, i.e. non-speculative */
  35. /* VMX20 , VMX21 , ... */
  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. /* Second context will be set with these values, i.e. speculative */
  41. /* VMX20 , VMX21 , ... */
  42. { -1, -2, -3, -4},{ -5, -6, -7, -8},{ -9,-10,-11,-12},
  43. {-13,-14,-15,-16},{-17,-18,-19,-20},{-21,-22,-23,-24},
  44. {-25,-26,-27,-28},{-29,-30,-31,-32},{-33,-34,-35,-36},
  45. {-37,-38,-39,-40},{-41,-42,-43,-44},{-45,-46,-47,-48}
  46. };
  47. static void signal_usr1(int signum, siginfo_t *info, void *uc)
  48. {
  49. int i, j;
  50. ucontext_t *ucp = uc;
  51. ucontext_t *tm_ucp = ucp->uc_link;
  52. for (i = 0; i < NV_VMX_REGS; i++) {
  53. /* Check first context. Print all mismatches. */
  54. fail = memcmp(ucp->uc_mcontext.v_regs->vrregs[VMX20 + i],
  55. &vms[i], sizeof(vector int));
  56. if (fail) {
  57. broken = 1;
  58. printf("VMX%d (1st context) == 0x", VMX20 + i);
  59. /* Print actual value in first context. */
  60. for (j = 0; j < 4; j++)
  61. printf("%08x", ucp->uc_mcontext.v_regs->vrregs[VMX20 + i][j]);
  62. printf(" instead of 0x");
  63. /* Print expected value. */
  64. for (j = 0; j < 4; j++)
  65. printf("%08x", vms[i][j]);
  66. printf(" (expected)\n");
  67. }
  68. }
  69. for (i = 0; i < NV_VMX_REGS; i++) {
  70. /* Check second context. Print all mismatches. */
  71. fail = memcmp(tm_ucp->uc_mcontext.v_regs->vrregs[VMX20 + i],
  72. &vms[NV_VMX_REGS + i], sizeof (vector int));
  73. if (fail) {
  74. broken = 1;
  75. printf("VMX%d (2nd context) == 0x", NV_VMX_REGS + i);
  76. /* Print actual value in second context. */
  77. for (j = 0; j < 4; j++)
  78. printf("%08x", tm_ucp->uc_mcontext.v_regs->vrregs[VMX20 + i][j]);
  79. printf(" instead of 0x");
  80. /* Print expected value. */
  81. for (j = 0; j < 4; j++)
  82. printf("%08x", vms[NV_VMX_REGS + i][j]);
  83. printf(" (expected)\n");
  84. }
  85. }
  86. }
  87. static int tm_signal_context_chk()
  88. {
  89. struct sigaction act;
  90. int i;
  91. long rc;
  92. pid_t pid = getpid();
  93. SKIP_IF(!have_htm());
  94. SKIP_IF(htm_is_synthetic());
  95. act.sa_sigaction = signal_usr1;
  96. sigemptyset(&act.sa_mask);
  97. act.sa_flags = SA_SIGINFO;
  98. if (sigaction(SIGUSR1, &act, NULL) < 0) {
  99. perror("sigaction sigusr1");
  100. exit(1);
  101. }
  102. i = 0;
  103. while (i < MAX_ATTEMPT && !broken) {
  104. /*
  105. * tm_signal_self_context_load will set both first and second
  106. * contexts accordingly to the values passed through non-NULL
  107. * array pointers to it, in that case 'vms', and invoke the
  108. * signal handler installed for SIGUSR1.
  109. */
  110. rc = tm_signal_self_context_load(pid, NULL, NULL, vms, NULL);
  111. FAIL_IF(rc != pid);
  112. i++;
  113. }
  114. return (broken);
  115. }
  116. int main(void)
  117. {
  118. return test_harness(tm_signal_context_chk, "tm_signal_context_chk_vmx");
  119. }