1f50a7f3dSThomas 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
21cde97f84SMichael Neuling #include "tm.h"
22cde97f84SMichael Neuling #include "utils.h"
23cde97f84SMichael Neuling
24cde97f84SMichael Neuling int passed;
25cde97f84SMichael Neuling
worker(void * unused)26cde97f84SMichael Neuling void *worker(void *unused)
27cde97f84SMichael Neuling {
28cde97f84SMichael Neuling __int128 vmx0;
29cde97f84SMichael Neuling uint64_t texasr;
30cde97f84SMichael Neuling
31cde97f84SMichael Neuling asm goto (
32cde97f84SMichael Neuling "li 3, 1;" /* Stick non-zero value in VMX0 */
33cde97f84SMichael Neuling "std 3, 0(%[vmx0_ptr]);"
34cde97f84SMichael Neuling "lvx 0, 0, %[vmx0_ptr];"
35cde97f84SMichael Neuling
36cde97f84SMichael Neuling /* Wait here a bit so we get scheduled out 255 times */
37cde97f84SMichael Neuling "lis 3, 0x3fff;"
38cde97f84SMichael Neuling "1: ;"
39cde97f84SMichael Neuling "addi 3, 3, -1;"
40cde97f84SMichael Neuling "cmpdi 3, 0;"
41cde97f84SMichael Neuling "bne 1b;"
42cde97f84SMichael Neuling
43cde97f84SMichael Neuling /* Kernel will hopefully turn VMX off now */
44cde97f84SMichael Neuling
45cde97f84SMichael Neuling "tbegin. ;"
46cde97f84SMichael Neuling "beq failure;"
47cde97f84SMichael Neuling
48cde97f84SMichael Neuling /* Cause VMX unavail. Any VMX instruction */
49cde97f84SMichael Neuling "vaddcuw 0,0,0;"
50cde97f84SMichael Neuling
51cde97f84SMichael Neuling "tend. ;"
52cde97f84SMichael Neuling "b %l[success];"
53cde97f84SMichael Neuling
54cde97f84SMichael Neuling /* Check VMX0 sanity after abort */
55cde97f84SMichael Neuling "failure: ;"
56cde97f84SMichael Neuling "lvx 1, 0, %[vmx0_ptr];"
57cde97f84SMichael Neuling "vcmpequb. 2, 0, 1;"
58cde97f84SMichael Neuling "bc 4, 24, %l[value_mismatch];"
59cde97f84SMichael Neuling "b %l[value_match];"
60cde97f84SMichael Neuling :
61cde97f84SMichael Neuling : [vmx0_ptr] "r"(&vmx0)
62cde97f84SMichael Neuling : "r3"
63cde97f84SMichael Neuling : success, value_match, value_mismatch
64cde97f84SMichael Neuling );
65cde97f84SMichael Neuling
66cde97f84SMichael Neuling /* HTM aborted and VMX0 is corrupted */
67cde97f84SMichael Neuling value_mismatch:
68cde97f84SMichael Neuling texasr = __builtin_get_texasr();
69cde97f84SMichael Neuling
70cde97f84SMichael Neuling printf("\n\n==============\n\n");
71cde97f84SMichael Neuling printf("Failure with error: %lx\n", _TEXASR_FAILURE_CODE(texasr));
72cde97f84SMichael Neuling printf("Summary error : %lx\n", _TEXASR_FAILURE_SUMMARY(texasr));
73cde97f84SMichael Neuling printf("TFIAR exact : %lx\n\n", _TEXASR_TFIAR_EXACT(texasr));
74cde97f84SMichael Neuling
75cde97f84SMichael Neuling passed = 0;
76cde97f84SMichael Neuling return NULL;
77cde97f84SMichael Neuling
78cde97f84SMichael Neuling /* HTM aborted but VMX0 is correct */
79cde97f84SMichael Neuling value_match:
80cde97f84SMichael Neuling // printf("!");
81cde97f84SMichael Neuling return NULL;
82cde97f84SMichael Neuling
83cde97f84SMichael Neuling success:
84cde97f84SMichael Neuling // printf(".");
85cde97f84SMichael Neuling return NULL;
86cde97f84SMichael Neuling }
87cde97f84SMichael Neuling
tm_vmx_unavail_test()88cde97f84SMichael Neuling int tm_vmx_unavail_test()
89cde97f84SMichael Neuling {
90cde97f84SMichael Neuling int threads;
91cde97f84SMichael Neuling pthread_t *thread;
92cde97f84SMichael Neuling
93cde97f84SMichael Neuling SKIP_IF(!have_htm());
94*e42edf9bSJordan Niethe SKIP_IF(htm_is_synthetic());
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
main(int argc,char ** argv)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