Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / powerpc / tm / tm-tmspr.c
blobdf1d7d4b1c89f7e0dbcb003f05f5969782403d59
1 /*
2 * Copyright 2015, Michael Neuling, IBM Corp.
3 * Licensed under GPLv2.
5 * Original: Michael Neuling 3/4/2014
6 * Modified: Rashmica Gupta 8/12/2015
8 * Check if any of the Transaction Memory SPRs get corrupted.
9 * - TFIAR - stores address of location of transaction failure
10 * - TFHAR - stores address of software failure handler (if transaction
11 * fails)
12 * - TEXASR - lots of info about the transacion(s)
14 * (1) create more threads than cpus
15 * (2) in each thread:
16 * (a) set TFIAR and TFHAR a unique value
17 * (b) loop for awhile, continually checking to see if
18 * either register has been corrupted.
20 * (3) Loop:
21 * (a) begin transaction
22 * (b) abort transaction
23 * (c) check TEXASR to see if FS has been corrupted
27 #define _GNU_SOURCE
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <pthread.h>
32 #include <string.h>
34 #include "utils.h"
35 #include "tm.h"
37 int num_loops = 10000;
38 int passed = 1;
40 void tfiar_tfhar(void *in)
42 int i, cpu;
43 unsigned long tfhar, tfhar_rd, tfiar, tfiar_rd;
44 cpu_set_t cpuset;
46 CPU_ZERO(&cpuset);
47 cpu = (unsigned long)in >> 1;
48 CPU_SET(cpu, &cpuset);
49 sched_setaffinity(0, sizeof(cpuset), &cpuset);
51 /* TFIAR: Last bit has to be high so userspace can read register */
52 tfiar = ((unsigned long)in) + 1;
53 tfiar += 2;
54 mtspr(SPRN_TFIAR, tfiar);
56 /* TFHAR: Last two bits are reserved */
57 tfhar = ((unsigned long)in);
58 tfhar &= ~0x3UL;
59 tfhar += 4;
60 mtspr(SPRN_TFHAR, tfhar);
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;
70 return;
73 void texasr(void *in)
75 unsigned long i;
76 uint64_t result = 0;
78 for (i = 0; i < num_loops; i++) {
79 asm __volatile__(
80 "tbegin.;"
81 "beq 3f ;"
82 "tabort. 0 ;"
83 "tend.;"
85 /* Abort handler */
86 "3: ;"
87 ::: "memory");
89 /* Check the TEXASR */
90 result = mfspr(SPRN_TEXASR);
91 if ((result & TEXASR_FS) == 0) {
92 passed = 0;
93 return;
96 return;
99 int test_tmspr()
101 pthread_t *thread;
102 int thread_num;
103 unsigned long i;
105 SKIP_IF(!have_htm());
107 /* To cause some context switching */
108 thread_num = 10 * sysconf(_SC_NPROCESSORS_ONLN);
110 thread = malloc(thread_num * sizeof(pthread_t));
111 if (thread == NULL)
112 return EXIT_FAILURE;
114 /* Test TFIAR and TFHAR */
115 for (i = 0; i < thread_num; i += 2) {
116 if (pthread_create(&thread[i], NULL, (void *)tfiar_tfhar,
117 (void *)i))
118 return EXIT_FAILURE;
120 /* Test TEXASR */
121 for (i = 1; i < thread_num; i += 2) {
122 if (pthread_create(&thread[i], NULL, (void *)texasr, (void *)i))
123 return EXIT_FAILURE;
126 for (i = 0; i < thread_num; i++) {
127 if (pthread_join(thread[i], NULL) != 0)
128 return EXIT_FAILURE;
131 free(thread);
133 if (passed)
134 return 0;
135 else
136 return 1;
139 int main(int argc, char *argv[])
141 if (argc > 1) {
142 if (strcmp(argv[1], "-h") == 0) {
143 printf("Syntax:\t [<num loops>]\n");
144 return 0;
145 } else {
146 num_loops = atoi(argv[1]);
149 return test_harness(test_tmspr, "tm_tmspr");