1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#define _GNU_SOURCE
28#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <pthread.h>
32#include <string.h>
33
34#include "utils.h"
35#include "tm.h"
36
37int num_loops = 10000;
38int passed = 1;
39
40void tfiar_tfhar(void *in)
41{
42 int i, cpu;
43 unsigned long tfhar, tfhar_rd, tfiar, tfiar_rd;
44 cpu_set_t cpuset;
45
46 CPU_ZERO(&cpuset);
47 cpu = (unsigned long)in >> 1;
48 CPU_SET(cpu, &cpuset);
49 sched_setaffinity(0, sizeof(cpuset), &cpuset);
50
51
52 tfiar = ((unsigned long)in) + 1;
53 tfiar += 2;
54 mtspr(SPRN_TFIAR, tfiar);
55
56
57 tfhar = ((unsigned long)in);
58 tfhar &= ~0x3UL;
59 tfhar += 4;
60 mtspr(SPRN_TFHAR, tfhar);
61
62 for (i = 0; i < num_loops; i++) {
63 tfhar_rd = mfspr(SPRN_TFHAR);
64 tfiar_rd = mfspr(SPRN_TFIAR);
65 if ( (tfhar != tfhar_rd) || (tfiar != tfiar_rd) ) {
66 passed = 0;
67 return;
68 }
69 }
70 return;
71}
72
73void texasr(void *in)
74{
75 unsigned long i;
76 uint64_t result = 0;
77
78 for (i = 0; i < num_loops; i++) {
79 asm __volatile__(
80 "tbegin.;"
81 "beq 3f ;"
82 "tabort. 0 ;"
83 "tend.;"
84
85
86 "3: ;"
87 ::: "memory");
88
89
90 result = mfspr(SPRN_TEXASR);
91 if ((result & TEXASR_FS) == 0) {
92 passed = 0;
93 return;
94 }
95 }
96 return;
97}
98
99int test_tmspr()
100{
101 pthread_t thread;
102 int thread_num;
103 unsigned long i;
104
105 SKIP_IF(!have_htm());
106
107
108 thread_num = 10 * sysconf(_SC_NPROCESSORS_ONLN);
109
110
111 for (i = 0 ; i < thread_num ; i += 2){
112 if (pthread_create(&thread, NULL, (void*)tfiar_tfhar, (void *)i))
113 return EXIT_FAILURE;
114 }
115 if (pthread_join(thread, NULL) != 0)
116 return EXIT_FAILURE;
117
118
119 for (i = 0 ; i < thread_num ; i++){
120 if (pthread_create(&thread, NULL, (void*)texasr, (void *)i))
121 return EXIT_FAILURE;
122 }
123 if (pthread_join(thread, NULL) != 0)
124 return EXIT_FAILURE;
125
126 if (passed)
127 return 0;
128 else
129 return 1;
130}
131
132int main(int argc, char *argv[])
133{
134 if (argc > 1) {
135 if (strcmp(argv[1], "-h") == 0) {
136 printf("Syntax:\t [<num loops>]\n");
137 return 0;
138 } else {
139 num_loops = atoi(argv[1]);
140 }
141 }
142 return test_harness(test_tmspr, "tm_tmspr");
143}
144