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. SKIP_IF(!is_ppc64le());
  25. for (i = 0; i < num_loops; i++)
  26. {
  27. uint64_t result = 0;
  28. asm __volatile__(
  29. "li 7, 1;"
  30. "mtspr %[tar], 7;" /* tar = 1 */
  31. "tbegin.;"
  32. "beq 3f;"
  33. "li 4, 0x7000;" /* Loop lots, to use time */
  34. "2:;" /* Start loop */
  35. "li 7, 2;"
  36. "mtspr %[tar], 7;" /* tar = 2 */
  37. "tsuspend.;"
  38. "li 7, 3;"
  39. "mtspr %[tar], 7;" /* tar = 3 */
  40. "tresume.;"
  41. "subi 4, 4, 1;"
  42. "cmpdi 4, 0;"
  43. "bne 2b;"
  44. "tend.;"
  45. /* Transaction sucess! TAR should be 3 */
  46. "mfspr 7, %[tar];"
  47. "ori %[res], 7, 4;" // res = 3|4 = 7
  48. "b 4f;"
  49. /* Abort handler. TAR should be rolled back to 1 */
  50. "3:;"
  51. "mfspr 7, %[tar];"
  52. "ori %[res], 7, 8;" // res = 1|8 = 9
  53. "4:;"
  54. : [res]"=r"(result)
  55. : [tar]"i"(SPRN_TAR)
  56. : "memory", "r0", "r4", "r7");
  57. /* If result is anything else other than 7 or 9, the tar
  58. * value must have been corrupted. */
  59. if ((result != 7) && (result != 9))
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. int main(int argc, char *argv[])
  65. {
  66. /* A low number of iterations (eg 100) can cause a false pass */
  67. if (argc > 1) {
  68. if (strcmp(argv[1], "-h") == 0) {
  69. printf("Syntax:\n\t%s [<num loops>]\n",
  70. argv[0]);
  71. return 1;
  72. } else {
  73. num_loops = atoi(argv[1]);
  74. }
  75. }
  76. printf("Starting, %d loops\n", num_loops);
  77. return test_harness(test_tar, "tm_tar");
  78. }