xref: /linux/tools/testing/selftests/powerpc/tm/tm-vmx-unavail.c (revision f50a7f3d9225dd374455f28138f79ae3074a7a3d)
1*f50a7f3dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2cde97f84SMichael Neuling /*
3cde97f84SMichael Neuling  * Copyright 2017, Michael Neuling, IBM Corp.
4cde97f84SMichael Neuling  * Original: Breno Leitao <brenohl@br.ibm.com> &
5cde97f84SMichael Neuling  *           Gustavo Bueno Romero <gromero@br.ibm.com>
6cde97f84SMichael Neuling  * Edited: Michael Neuling
7cde97f84SMichael Neuling  *
8cde97f84SMichael Neuling  * Force VMX unavailable during a transaction and see if it corrupts
9cde97f84SMichael Neuling  * the checkpointed VMX register state after the abort.
10cde97f84SMichael Neuling  */
11cde97f84SMichael Neuling 
12cde97f84SMichael Neuling #include <inttypes.h>
13cde97f84SMichael Neuling #include <htmintrin.h>
14cde97f84SMichael Neuling #include <string.h>
15cde97f84SMichael Neuling #include <stdlib.h>
16cde97f84SMichael Neuling #include <stdio.h>
17cde97f84SMichael Neuling #include <pthread.h>
18cde97f84SMichael Neuling #include <sys/mman.h>
19cde97f84SMichael Neuling #include <unistd.h>
20cde97f84SMichael Neuling #include <pthread.h>
21cde97f84SMichael Neuling 
22cde97f84SMichael Neuling #include "tm.h"
23cde97f84SMichael Neuling #include "utils.h"
24cde97f84SMichael Neuling 
25cde97f84SMichael Neuling int passed;
26cde97f84SMichael Neuling 
27cde97f84SMichael Neuling void *worker(void *unused)
28cde97f84SMichael Neuling {
29cde97f84SMichael Neuling 	__int128 vmx0;
30cde97f84SMichael Neuling 	uint64_t texasr;
31cde97f84SMichael Neuling 
32cde97f84SMichael Neuling 	asm goto (
33cde97f84SMichael Neuling 		"li       3, 1;"  /* Stick non-zero value in VMX0 */
34cde97f84SMichael Neuling 		"std      3, 0(%[vmx0_ptr]);"
35cde97f84SMichael Neuling 		"lvx      0, 0, %[vmx0_ptr];"
36cde97f84SMichael Neuling 
37cde97f84SMichael Neuling 		/* Wait here a bit so we get scheduled out 255 times */
38cde97f84SMichael Neuling 		"lis      3, 0x3fff;"
39cde97f84SMichael Neuling 		"1: ;"
40cde97f84SMichael Neuling 		"addi     3, 3, -1;"
41cde97f84SMichael Neuling 		"cmpdi    3, 0;"
42cde97f84SMichael Neuling 		"bne      1b;"
43cde97f84SMichael Neuling 
44cde97f84SMichael Neuling 		/* Kernel will hopefully turn VMX off now */
45cde97f84SMichael Neuling 
46cde97f84SMichael Neuling 		"tbegin. ;"
47cde97f84SMichael Neuling 		"beq      failure;"
48cde97f84SMichael Neuling 
49cde97f84SMichael Neuling 		/* Cause VMX unavail. Any VMX instruction */
50cde97f84SMichael Neuling 		"vaddcuw  0,0,0;"
51cde97f84SMichael Neuling 
52cde97f84SMichael Neuling 		"tend. ;"
53cde97f84SMichael Neuling 		"b        %l[success];"
54cde97f84SMichael Neuling 
55cde97f84SMichael Neuling 		/* Check VMX0 sanity after abort */
56cde97f84SMichael Neuling 		"failure: ;"
57cde97f84SMichael Neuling 		"lvx       1,  0, %[vmx0_ptr];"
58cde97f84SMichael Neuling 		"vcmpequb. 2,  0, 1;"
59cde97f84SMichael Neuling 		"bc        4, 24, %l[value_mismatch];"
60cde97f84SMichael Neuling 		"b        %l[value_match];"
61cde97f84SMichael Neuling 		:
62cde97f84SMichael Neuling 		: [vmx0_ptr] "r"(&vmx0)
63cde97f84SMichael Neuling 		: "r3"
64cde97f84SMichael Neuling 		: success, value_match, value_mismatch
65cde97f84SMichael Neuling 		);
66cde97f84SMichael Neuling 
67cde97f84SMichael Neuling 	/* HTM aborted and VMX0 is corrupted */
68cde97f84SMichael Neuling value_mismatch:
69cde97f84SMichael Neuling 	texasr = __builtin_get_texasr();
70cde97f84SMichael Neuling 
71cde97f84SMichael Neuling 	printf("\n\n==============\n\n");
72cde97f84SMichael Neuling 	printf("Failure with error: %lx\n",   _TEXASR_FAILURE_CODE(texasr));
73cde97f84SMichael Neuling 	printf("Summary error     : %lx\n",   _TEXASR_FAILURE_SUMMARY(texasr));
74cde97f84SMichael Neuling 	printf("TFIAR exact       : %lx\n\n", _TEXASR_TFIAR_EXACT(texasr));
75cde97f84SMichael Neuling 
76cde97f84SMichael Neuling 	passed = 0;
77cde97f84SMichael Neuling 	return NULL;
78cde97f84SMichael Neuling 
79cde97f84SMichael Neuling 	/* HTM aborted but VMX0 is correct */
80cde97f84SMichael Neuling value_match:
81cde97f84SMichael Neuling //	printf("!");
82cde97f84SMichael Neuling 	return NULL;
83cde97f84SMichael Neuling 
84cde97f84SMichael Neuling success:
85cde97f84SMichael Neuling //	printf(".");
86cde97f84SMichael Neuling 	return NULL;
87cde97f84SMichael Neuling }
88cde97f84SMichael Neuling 
89cde97f84SMichael Neuling int tm_vmx_unavail_test()
90cde97f84SMichael Neuling {
91cde97f84SMichael Neuling 	int threads;
92cde97f84SMichael Neuling 	pthread_t *thread;
93cde97f84SMichael Neuling 
94cde97f84SMichael Neuling 	SKIP_IF(!have_htm());
95cde97f84SMichael Neuling 
96cde97f84SMichael Neuling 	passed = 1;
97cde97f84SMichael Neuling 
98cde97f84SMichael Neuling 	threads = sysconf(_SC_NPROCESSORS_ONLN) * 4;
99cde97f84SMichael Neuling 	thread = malloc(sizeof(pthread_t)*threads);
100cde97f84SMichael Neuling 	if (!thread)
101cde97f84SMichael Neuling 		return EXIT_FAILURE;
102cde97f84SMichael Neuling 
103cde97f84SMichael Neuling 	for (uint64_t i = 0; i < threads; i++)
104cde97f84SMichael Neuling 		pthread_create(&thread[i], NULL, &worker, NULL);
105cde97f84SMichael Neuling 
106cde97f84SMichael Neuling 	for (uint64_t i = 0; i < threads; i++)
107cde97f84SMichael Neuling 		pthread_join(thread[i], NULL);
108cde97f84SMichael Neuling 
109cde97f84SMichael Neuling 	free(thread);
110cde97f84SMichael Neuling 
111cde97f84SMichael Neuling 	return passed ? EXIT_SUCCESS : EXIT_FAILURE;
112cde97f84SMichael Neuling }
113cde97f84SMichael Neuling 
114cde97f84SMichael Neuling 
115cde97f84SMichael Neuling int main(int argc, char **argv)
116cde97f84SMichael Neuling {
117cde97f84SMichael Neuling 	return test_harness(tm_vmx_unavail_test, "tm_vmx_unavail_test");
118cde97f84SMichael Neuling }
119