tm-tar.c 2.0 KB

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