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