tm-tar.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2015, Michael Neuling, IBM Corp.
  3. * Licensed under GPLv2.
  4. * Original: Michael Neuling 19/7/2013
  5. * Edited: Rashmica Gupta 01/12/2015
  6. *
  7. * Do some transactions, see if the tar is corrupted.
  8. * If the transaction is aborted, the TAR should be rolled back to the
  9. * checkpointed value before the transaction began. The value written to
  10. * TAR in suspended mode should only remain in TAR if the transaction
  11. * completes.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <string.h>
  17. #include "tm.h"
  18. #include "utils.h"
  19. int num_loops = 10000;
  20. int test_tar(void)
  21. {
  22. int i;
  23. SKIP_IF(!have_htm());
  24. for (i = 0; i < num_loops; i++)
  25. {
  26. uint64_t result = 0;
  27. asm __volatile__(
  28. "li 7, 1;"
  29. "mtspr %[tar], 7;" /* tar = 1 */
  30. "tbegin.;"
  31. "beq 3f;"
  32. "li 4, 0x7000;" /* Loop lots, to use time */
  33. "2:;" /* Start loop */
  34. "li 7, 2;"
  35. "mtspr %[tar], 7;" /* tar = 2 */
  36. "tsuspend.;"
  37. "li 7, 3;"
  38. "mtspr %[tar], 7;" /* tar = 3 */
  39. "tresume.;"
  40. "subi 4, 4, 1;"
  41. "cmpdi 4, 0;"
  42. "bne 2b;"
  43. "tend.;"
  44. /* Transaction sucess! TAR should be 3 */
  45. "mfspr 7, %[tar];"
  46. "ori %[res], 7, 4;" // res = 3|4 = 7
  47. "b 4f;"
  48. /* Abort handler. TAR should be rolled back to 1 */
  49. "3:;"
  50. "mfspr 7, %[tar];"
  51. "ori %[res], 7, 8;" // res = 1|8 = 9
  52. "4:;"
  53. : [res]"=r"(result)
  54. : [tar]"i"(SPRN_TAR)
  55. : "memory", "r0", "r4", "r7");
  56. /* If result is anything else other than 7 or 9, the tar
  57. * value must have been corrupted. */
  58. if ((result != 7) && (result != 9))
  59. return 1;
  60. }
  61. return 0;
  62. }
  63. int main(int argc, char *argv[])
  64. {
  65. /* A low number of iterations (eg 100) can cause a false pass */
  66. if (argc > 1) {
  67. if (strcmp(argv[1], "-h") == 0) {
  68. printf("Syntax:\n\t%s [<num loops>]\n",
  69. argv[0]);
  70. return 1;
  71. } else {
  72. num_loops = atoi(argv[1]);
  73. }
  74. }
  75. printf("Starting, %d loops\n", num_loops);
  76. return test_harness(test_tar, "tm_tar");
  77. }