tm-tmspr.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /* Test TFIAR and TFHAR */
  95. for (i = 0 ; i < thread_num ; i += 2){
  96. if (pthread_create(&thread, NULL, (void*)tfiar_tfhar, (void *)i))
  97. return EXIT_FAILURE;
  98. }
  99. if (pthread_join(thread, NULL) != 0)
  100. return EXIT_FAILURE;
  101. /* Test TEXASR */
  102. for (i = 0 ; i < thread_num ; i++){
  103. if (pthread_create(&thread, NULL, (void*)texasr, (void *)i))
  104. return EXIT_FAILURE;
  105. }
  106. if (pthread_join(thread, NULL) != 0)
  107. return EXIT_FAILURE;
  108. if (passed)
  109. return 0;
  110. else
  111. return 1;
  112. }
  113. int main(int argc, char *argv[])
  114. {
  115. if (argc > 1) {
  116. if (strcmp(argv[1], "-h") == 0) {
  117. printf("Syntax:\t [<num loops>]\n");
  118. return 0;
  119. } else {
  120. num_loops = atoi(argv[1]);
  121. }
  122. }
  123. return test_harness(test_tmspr, "tm_tmspr");
  124. }