GNU Linux-libre 4.19.245-gnu1
[releases.git] / tools / testing / selftests / powerpc / tm / tm-tar.c
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
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18
19 #include "tm.h"
20 #include "utils.h"
21
22 int     num_loops       = 10000;
23
24 int test_tar(void)
25 {
26         int i;
27
28         SKIP_IF(!have_htm());
29         SKIP_IF(!is_ppc64le());
30
31         for (i = 0; i < num_loops; i++)
32         {
33                 uint64_t result = 0;
34                 asm __volatile__(
35                         "li     7, 1;"
36                         "mtspr  %[tar], 7;"     /* tar = 1 */
37                         "tbegin.;"
38                         "beq    3f;"
39                         "li     4, 0x7000;"     /* Loop lots, to use time */
40                         "2:;"                   /* Start loop */
41                         "li     7, 2;"
42                         "mtspr  %[tar], 7;"     /* tar = 2 */
43                         "tsuspend.;"
44                         "li     7, 3;"
45                         "mtspr  %[tar], 7;"     /* tar = 3 */
46                         "tresume.;"
47                         "subi   4, 4, 1;"
48                         "cmpdi  4, 0;"
49                         "bne    2b;"
50                         "tend.;"
51
52                         /* Transaction sucess! TAR should be 3 */
53                         "mfspr  7, %[tar];"
54                         "ori    %[res], 7, 4;"  // res = 3|4 = 7
55                         "b      4f;"
56
57                         /* Abort handler. TAR should be rolled back to 1 */
58                         "3:;"
59                         "mfspr  7, %[tar];"
60                         "ori    %[res], 7, 8;"  // res = 1|8 = 9
61                         "4:;"
62
63                         : [res]"=r"(result)
64                         : [tar]"i"(SPRN_TAR)
65                            : "memory", "r0", "r4", "r7");
66
67                 /* If result is anything else other than 7 or 9, the tar
68                  * value must have been corrupted. */
69                 if ((result != 7) && (result != 9))
70                         return 1;
71         }
72         return 0;
73 }
74
75 int main(int argc, char *argv[])
76 {
77         /* A low number of iterations (eg 100) can cause a false pass */
78         if (argc > 1) {
79                 if (strcmp(argv[1], "-h") == 0) {
80                         printf("Syntax:\n\t%s [<num loops>]\n",
81                                argv[0]);
82                         return 1;
83                 } else {
84                         num_loops = atoi(argv[1]);
85                 }
86         }
87
88         printf("Starting, %d loops\n", num_loops);
89
90         return test_harness(test_tar, "tm_tar");
91 }