tm-tmspr.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2015, Michael Neuling, IBM Corp.
  3. * Licensed under GPLv2.
  4. *
  5. * Original: Michael Neuling 3/4/2014
  6. * Modified: Rashmica Gupta 8/12/2015
  7. *
  8. * Check if any of the Transaction Memory SPRs get corrupted.
  9. * - TFIAR - stores address of location of transaction failure
  10. * - TFHAR - stores address of software failure handler (if transaction
  11. * fails)
  12. * - TEXASR - lots of info about the transacion(s)
  13. *
  14. * (1) create more threads than cpus
  15. * (2) in each thread:
  16. * (a) set TFIAR and TFHAR a unique value
  17. * (b) loop for awhile, continually checking to see if
  18. * either register has been corrupted.
  19. *
  20. * (3) Loop:
  21. * (a) begin transaction
  22. * (b) abort transaction
  23. * (c) check TEXASR to see if FS has been corrupted
  24. *
  25. */
  26. #define _GNU_SOURCE
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <pthread.h>
  31. #include <string.h>
  32. #include "utils.h"
  33. #include "tm.h"
  34. int num_loops = 10000;
  35. int passed = 1;
  36. void tfiar_tfhar(void *in)
  37. {
  38. int i, cpu;
  39. unsigned long tfhar, tfhar_rd, tfiar, tfiar_rd;
  40. cpu_set_t cpuset;
  41. CPU_ZERO(&cpuset);
  42. cpu = (unsigned long)in >> 1;
  43. CPU_SET(cpu, &cpuset);
  44. sched_setaffinity(0, sizeof(cpuset), &cpuset);
  45. /* TFIAR: Last bit has to be high so userspace can read register */
  46. tfiar = ((unsigned long)in) + 1;
  47. tfiar += 2;
  48. mtspr(SPRN_TFIAR, tfiar);
  49. /* TFHAR: Last two bits are reserved */
  50. tfhar = ((unsigned long)in);
  51. tfhar &= ~0x3UL;
  52. tfhar += 4;
  53. mtspr(SPRN_TFHAR, tfhar);
  54. for (i = 0; i < num_loops; i++) {
  55. tfhar_rd = mfspr(SPRN_TFHAR);
  56. tfiar_rd = mfspr(SPRN_TFIAR);
  57. if ( (tfhar != tfhar_rd) || (tfiar != tfiar_rd) ) {
  58. passed = 0;
  59. return;
  60. }
  61. }
  62. return;
  63. }
  64. void texasr(void *in)
  65. {
  66. unsigned long i;
  67. uint64_t result = 0;
  68. for (i = 0; i < num_loops; i++) {
  69. asm __volatile__(
  70. "tbegin.;"
  71. "beq 3f ;"
  72. "tabort. 0 ;"
  73. "tend.;"
  74. /* Abort handler */
  75. "3: ;"
  76. ::: "memory");
  77. /* Check the TEXASR */
  78. result = mfspr(SPRN_TEXASR);
  79. if ((result & TEXASR_FS) == 0) {
  80. passed = 0;
  81. return;
  82. }
  83. }
  84. return;
  85. }
  86. int test_tmspr()
  87. {
  88. pthread_t *thread;
  89. int thread_num;
  90. unsigned long i;
  91. SKIP_IF(!have_htm());
  92. /* To cause some context switching */
  93. thread_num = 10 * sysconf(_SC_NPROCESSORS_ONLN);
  94. thread = malloc(thread_num * sizeof(pthread_t));
  95. if (thread == NULL)
  96. return EXIT_FAILURE;
  97. /* Test TFIAR and TFHAR */
  98. for (i = 0; i < thread_num; i += 2) {
  99. if (pthread_create(&thread[i], NULL, (void *)tfiar_tfhar,
  100. (void *)i))
  101. return EXIT_FAILURE;
  102. }
  103. /* Test TEXASR */
  104. for (i = 1; i < thread_num; i += 2) {
  105. if (pthread_create(&thread[i], NULL, (void *)texasr, (void *)i))
  106. return EXIT_FAILURE;
  107. }
  108. for (i = 0; i < thread_num; i++) {
  109. if (pthread_join(thread[i], NULL) != 0)
  110. return EXIT_FAILURE;
  111. }
  112. free(thread);
  113. if (passed)
  114. return 0;
  115. else
  116. return 1;
  117. }
  118. int main(int argc, char *argv[])
  119. {
  120. if (argc > 1) {
  121. if (strcmp(argv[1], "-h") == 0) {
  122. printf("Syntax:\t [<num loops>]\n");
  123. return 0;
  124. } else {
  125. num_loops = atoi(argv[1]);
  126. }
  127. }
  128. return test_harness(test_tmspr, "tm_tmspr");
  129. }