diff options
author | Gustavo Romero <gromero@linux.ibm.com> | 2019-09-04 00:55:29 -0400 |
---|---|---|
committer | Michael Ellerman <mpe@ellerman.id.au> | 2019-09-20 18:36:53 -0400 |
commit | a003365cab64b0f7988ac3ccb1da895ce0bece5e (patch) | |
tree | 01730b4b18c05380cf629fd4635f8d7b67088f69 /tools/testing/selftests | |
parent | 45824fc0da6e46cc5d563105e1eaaf3098a686f9 (diff) |
powerpc/tm: Add tm-poison test
Add TM selftest to check if FP or VEC register values from one process
can leak into another process when both run on the same CPU.
Signed-off-by: Gustavo Romero <gromero@linux.ibm.com>
Signed-off-by: Michael Neuling <mikey@neuling.org>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20190904045529.23002-3-gromero@linux.vnet.ibm.com
Diffstat (limited to 'tools/testing/selftests')
-rw-r--r-- | tools/testing/selftests/powerpc/tm/.gitignore | 1 | ||||
-rw-r--r-- | tools/testing/selftests/powerpc/tm/Makefile | 2 | ||||
-rw-r--r-- | tools/testing/selftests/powerpc/tm/tm-poison.c | 179 |
3 files changed, 181 insertions, 1 deletions
diff --git a/tools/testing/selftests/powerpc/tm/.gitignore b/tools/testing/selftests/powerpc/tm/.gitignore index 951fe855f7cd..98f2708d86cc 100644 --- a/tools/testing/selftests/powerpc/tm/.gitignore +++ b/tools/testing/selftests/powerpc/tm/.gitignore | |||
@@ -17,3 +17,4 @@ tm-vmx-unavail | |||
17 | tm-unavailable | 17 | tm-unavailable |
18 | tm-trap | 18 | tm-trap |
19 | tm-sigreturn | 19 | tm-sigreturn |
20 | tm-poison | ||
diff --git a/tools/testing/selftests/powerpc/tm/Makefile b/tools/testing/selftests/powerpc/tm/Makefile index c0734ed0ef56..b15a1a325bd0 100644 --- a/tools/testing/selftests/powerpc/tm/Makefile +++ b/tools/testing/selftests/powerpc/tm/Makefile | |||
@@ -5,7 +5,7 @@ SIGNAL_CONTEXT_CHK_TESTS := tm-signal-context-chk-gpr tm-signal-context-chk-fpu | |||
5 | TEST_GEN_PROGS := tm-resched-dscr tm-syscall tm-signal-msr-resv tm-signal-stack \ | 5 | TEST_GEN_PROGS := tm-resched-dscr tm-syscall tm-signal-msr-resv tm-signal-stack \ |
6 | tm-vmxcopy tm-fork tm-tar tm-tmspr tm-vmx-unavail tm-unavailable tm-trap \ | 6 | tm-vmxcopy tm-fork tm-tar tm-tmspr tm-vmx-unavail tm-unavailable tm-trap \ |
7 | $(SIGNAL_CONTEXT_CHK_TESTS) tm-sigreturn tm-signal-sigreturn-nt \ | 7 | $(SIGNAL_CONTEXT_CHK_TESTS) tm-sigreturn tm-signal-sigreturn-nt \ |
8 | tm-signal-context-force-tm | 8 | tm-signal-context-force-tm tm-poison |
9 | 9 | ||
10 | top_srcdir = ../../../../.. | 10 | top_srcdir = ../../../../.. |
11 | include ../../lib.mk | 11 | include ../../lib.mk |
diff --git a/tools/testing/selftests/powerpc/tm/tm-poison.c b/tools/testing/selftests/powerpc/tm/tm-poison.c new file mode 100644 index 000000000000..977558497c16 --- /dev/null +++ b/tools/testing/selftests/powerpc/tm/tm-poison.c | |||
@@ -0,0 +1,179 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0-only | ||
2 | /* | ||
3 | * Copyright 2019, Gustavo Romero, Michael Neuling, IBM Corp. | ||
4 | * | ||
5 | * This test will spawn two processes. Both will be attached to the same | ||
6 | * CPU (CPU 0). The child will be in a loop writing to FP register f31 and | ||
7 | * VMX/VEC/Altivec register vr31 a known value, called poison, calling | ||
8 | * sched_yield syscall after to allow the parent to switch on the CPU. | ||
9 | * Parent will set f31 and vr31 to 1 and in a loop will check if f31 and | ||
10 | * vr31 remain 1 as expected until a given timeout (2m). If the issue is | ||
11 | * present child's poison will leak into parent's f31 or vr31 registers, | ||
12 | * otherwise, poison will never leak into parent's f31 and vr31 registers. | ||
13 | */ | ||
14 | |||
15 | #define _GNU_SOURCE | ||
16 | #include <stdio.h> | ||
17 | #include <stdlib.h> | ||
18 | #include <unistd.h> | ||
19 | #include <inttypes.h> | ||
20 | #include <sched.h> | ||
21 | #include <sys/types.h> | ||
22 | #include <signal.h> | ||
23 | #include <inttypes.h> | ||
24 | |||
25 | #include "tm.h" | ||
26 | |||
27 | int tm_poison_test(void) | ||
28 | { | ||
29 | int pid; | ||
30 | cpu_set_t cpuset; | ||
31 | uint64_t poison = 0xdeadbeefc0dec0fe; | ||
32 | uint64_t unknown = 0; | ||
33 | bool fail_fp = false; | ||
34 | bool fail_vr = false; | ||
35 | |||
36 | SKIP_IF(!have_htm()); | ||
37 | |||
38 | /* Attach both Child and Parent to CPU 0 */ | ||
39 | CPU_ZERO(&cpuset); | ||
40 | CPU_SET(0, &cpuset); | ||
41 | sched_setaffinity(0, sizeof(cpuset), &cpuset); | ||
42 | |||
43 | pid = fork(); | ||
44 | if (!pid) { | ||
45 | /** | ||
46 | * child | ||
47 | */ | ||
48 | while (1) { | ||
49 | sched_yield(); | ||
50 | asm ( | ||
51 | "mtvsrd 31, %[poison];" // f31 = poison | ||
52 | "mtvsrd 63, %[poison];" // vr31 = poison | ||
53 | |||
54 | : : [poison] "r" (poison) : ); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * parent | ||
60 | */ | ||
61 | asm ( | ||
62 | /* | ||
63 | * Set r3, r4, and f31 to known value 1 before entering | ||
64 | * in transaction. They won't be written after that. | ||
65 | */ | ||
66 | " li 3, 0x1 ;" | ||
67 | " li 4, 0x1 ;" | ||
68 | " mtvsrd 31, 4 ;" | ||
69 | |||
70 | /* | ||
71 | * The Time Base (TB) is a 64-bit counter register that is | ||
72 | * independent of the CPU clock and which is incremented | ||
73 | * at a frequency of 512000000 Hz, so every 1.953125ns. | ||
74 | * So it's necessary 120s/0.000000001953125s = 61440000000 | ||
75 | * increments to get a 2 minutes timeout. Below we set that | ||
76 | * value in r5 and then use r6 to track initial TB value, | ||
77 | * updating TB values in r7 at every iteration and comparing it | ||
78 | * to r6. When r7 (current) - r6 (initial) > 61440000000 we bail | ||
79 | * out since for sure we spent already 2 minutes in the loop. | ||
80 | * SPR 268 is the TB register. | ||
81 | */ | ||
82 | " lis 5, 14 ;" | ||
83 | " ori 5, 5, 19996 ;" | ||
84 | " sldi 5, 5, 16 ;" // r5 = 61440000000 | ||
85 | |||
86 | " mfspr 6, 268 ;" // r6 (TB initial) | ||
87 | "1: mfspr 7, 268 ;" // r7 (TB current) | ||
88 | " subf 7, 6, 7 ;" // r7 - r6 > 61440000000 ? | ||
89 | " cmpd 7, 5 ;" | ||
90 | " bgt 3f ;" // yes, exit | ||
91 | |||
92 | /* | ||
93 | * Main loop to check f31 | ||
94 | */ | ||
95 | " tbegin. ;" // no, try again | ||
96 | " beq 1b ;" // restart if no timeout | ||
97 | " mfvsrd 3, 31 ;" // read f31 | ||
98 | " cmpd 3, 4 ;" // f31 == 1 ? | ||
99 | " bne 2f ;" // broken :-( | ||
100 | " tabort. 3 ;" // try another transaction | ||
101 | "2: tend. ;" // commit transaction | ||
102 | "3: mr %[unknown], 3 ;" // record r3 | ||
103 | |||
104 | : [unknown] "=r" (unknown) | ||
105 | : | ||
106 | : "cr0", "r3", "r4", "r5", "r6", "r7", "vs31" | ||
107 | |||
108 | ); | ||
109 | |||
110 | /* | ||
111 | * On leak 'unknown' will contain 'poison' value from child, | ||
112 | * otherwise (no leak) 'unknown' will contain the same value | ||
113 | * as r3 before entering in transactional mode, i.e. 0x1. | ||
114 | */ | ||
115 | fail_fp = unknown != 0x1; | ||
116 | if (fail_fp) | ||
117 | printf("Unknown value %#"PRIx64" leaked into f31!\n", unknown); | ||
118 | else | ||
119 | printf("Good, no poison or leaked value into FP registers\n"); | ||
120 | |||
121 | asm ( | ||
122 | /* | ||
123 | * Set r3, r4, and vr31 to known value 1 before entering | ||
124 | * in transaction. They won't be written after that. | ||
125 | */ | ||
126 | " li 3, 0x1 ;" | ||
127 | " li 4, 0x1 ;" | ||
128 | " mtvsrd 63, 4 ;" | ||
129 | |||
130 | " lis 5, 14 ;" | ||
131 | " ori 5, 5, 19996 ;" | ||
132 | " sldi 5, 5, 16 ;" // r5 = 61440000000 | ||
133 | |||
134 | " mfspr 6, 268 ;" // r6 (TB initial) | ||
135 | "1: mfspr 7, 268 ;" // r7 (TB current) | ||
136 | " subf 7, 6, 7 ;" // r7 - r6 > 61440000000 ? | ||
137 | " cmpd 7, 5 ;" | ||
138 | " bgt 3f ;" // yes, exit | ||
139 | |||
140 | /* | ||
141 | * Main loop to check vr31 | ||
142 | */ | ||
143 | " tbegin. ;" // no, try again | ||
144 | " beq 1b ;" // restart if no timeout | ||
145 | " mfvsrd 3, 63 ;" // read vr31 | ||
146 | " cmpd 3, 4 ;" // vr31 == 1 ? | ||
147 | " bne 2f ;" // broken :-( | ||
148 | " tabort. 3 ;" // try another transaction | ||
149 | "2: tend. ;" // commit transaction | ||
150 | "3: mr %[unknown], 3 ;" // record r3 | ||
151 | |||
152 | : [unknown] "=r" (unknown) | ||
153 | : | ||
154 | : "cr0", "r3", "r4", "r5", "r6", "r7", "vs63" | ||
155 | |||
156 | ); | ||
157 | |||
158 | /* | ||
159 | * On leak 'unknown' will contain 'poison' value from child, | ||
160 | * otherwise (no leak) 'unknown' will contain the same value | ||
161 | * as r3 before entering in transactional mode, i.e. 0x1. | ||
162 | */ | ||
163 | fail_vr = unknown != 0x1; | ||
164 | if (fail_vr) | ||
165 | printf("Unknown value %#"PRIx64" leaked into vr31!\n", unknown); | ||
166 | else | ||
167 | printf("Good, no poison or leaked value into VEC registers\n"); | ||
168 | |||
169 | kill(pid, SIGKILL); | ||
170 | |||
171 | return (fail_fp | fail_vr); | ||
172 | } | ||
173 | |||
174 | int main(int argc, char *argv[]) | ||
175 | { | ||
176 | /* Test completes in about 4m */ | ||
177 | test_harness_set_timeout(250); | ||
178 | return test_harness(tm_poison_test, "tm_poison_test"); | ||
179 | } | ||