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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_GPR_REGS 18 /* Number of non-volatile GPR registers */
  28. #define R14 14 /* First non-volatile register to check in r14-r31 subset */
  29. long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
  30. static sig_atomic_t fail, broken;
  31. /* Test only non-volatile general purpose registers, i.e. r14-r31 */
  32. static long gprs[] = {
  33. /* First context will be set with these values, i.e. non-speculative */
  34. /* R14, R15, ... */
  35. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  36. /* Second context will be set with these values, i.e. speculative */
  37. /* R14, R15, ... */
  38. -1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18
  39. };
  40. static void signal_usr1(int signum, siginfo_t *info, void *uc)
  41. {
  42. int i;
  43. ucontext_t *ucp = uc;
  44. ucontext_t *tm_ucp = ucp->uc_link;
  45. /* Check first context. Print all mismatches. */
  46. for (i = 0; i < NV_GPR_REGS; i++) {
  47. fail = (ucp->uc_mcontext.gp_regs[R14 + i] != gprs[i]);
  48. if (fail) {
  49. broken = 1;
  50. printf("GPR%d (1st context) == %lu instead of %lu (expected)\n",
  51. R14 + i, ucp->uc_mcontext.gp_regs[R14 + i], gprs[i]);
  52. }
  53. }
  54. /* Check second context. Print all mismatches. */
  55. for (i = 0; i < NV_GPR_REGS; i++) {
  56. fail = (tm_ucp->uc_mcontext.gp_regs[R14 + i] != gprs[NV_GPR_REGS + i]);
  57. if (fail) {
  58. broken = 1;
  59. printf("GPR%d (2nd context) == %lu instead of %lu (expected)\n",
  60. R14 + i, tm_ucp->uc_mcontext.gp_regs[R14 + i], gprs[NV_GPR_REGS + i]);
  61. }
  62. }
  63. }
  64. static int tm_signal_context_chk_gpr()
  65. {
  66. struct sigaction act;
  67. int i;
  68. long rc;
  69. pid_t pid = getpid();
  70. SKIP_IF(!have_htm());
  71. SKIP_IF(htm_is_synthetic());
  72. act.sa_sigaction = signal_usr1;
  73. sigemptyset(&act.sa_mask);
  74. act.sa_flags = SA_SIGINFO;
  75. if (sigaction(SIGUSR1, &act, NULL) < 0) {
  76. perror("sigaction sigusr1");
  77. exit(1);
  78. }
  79. i = 0;
  80. while (i < MAX_ATTEMPT && !broken) {
  81. /*
  82. * tm_signal_self_context_load will set both first and second
  83. * contexts accordingly to the values passed through non-NULL
  84. * array pointers to it, in that case 'gprs', and invoke the
  85. * signal handler installed for SIGUSR1.
  86. */
  87. rc = tm_signal_self_context_load(pid, gprs, NULL, NULL, NULL);
  88. FAIL_IF(rc != pid);
  89. i++;
  90. }
  91. return broken;
  92. }
  93. int main(void)
  94. {
  95. return test_harness(tm_signal_context_chk_gpr, "tm_signal_context_chk_gpr");
  96. }