This source file includes following definitions.
- usr_signal_handler
- seg_signal_handler
- tm_trap_test
- tm_signal_context_force_tm
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #define _GNU_SOURCE
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <ucontext.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27
28 #include "tm.h"
29 #include "utils.h"
30 #include "reg.h"
31
32 #define COUNT_MAX 5000
33
34
35
36
37
38
39 #ifndef __powerpc64__
40 #undef MSR_TS_S
41 #define MSR_TS_S 0
42 #endif
43
44
45 ucontext_t init_context, main_context;
46
47 static int count, first_time;
48
49 void usr_signal_handler(int signo, siginfo_t *si, void *uc)
50 {
51 ucontext_t *ucp = uc;
52 int ret;
53
54
55
56
57
58
59 ucp->uc_link = mmap(NULL, sizeof(ucontext_t),
60 PROT_READ | PROT_WRITE,
61 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
62 if (ucp->uc_link == (void *)-1) {
63 perror("Mmap failed");
64 exit(-1);
65 }
66
67
68 ret = madvise(ucp->uc_link, sizeof(ucontext_t), MADV_DONTNEED);
69 if (ret) {
70 perror("madvise failed");
71 exit(-1);
72 }
73
74 memcpy(&ucp->uc_link->uc_mcontext, &ucp->uc_mcontext,
75 sizeof(ucp->uc_mcontext));
76
77
78 UCONTEXT_MSR(ucp) |= MSR_TS_S;
79
80
81
82
83
84 if (fork() == 0) {
85
86
87
88
89
90 count = COUNT_MAX;
91 }
92
93
94
95
96
97 }
98
99 void seg_signal_handler(int signo, siginfo_t *si, void *uc)
100 {
101 if (count == COUNT_MAX) {
102
103 setcontext(&main_context);
104 }
105
106 count++;
107
108
109 setcontext(&init_context);
110 }
111
112 void tm_trap_test(void)
113 {
114 struct sigaction usr_sa, seg_sa;
115 stack_t ss;
116
117 usr_sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
118 usr_sa.sa_sigaction = usr_signal_handler;
119
120 seg_sa.sa_flags = SA_SIGINFO;
121 seg_sa.sa_sigaction = seg_signal_handler;
122
123
124
125
126
127 getcontext(&init_context);
128
129
130 ss.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
131 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
132 ss.ss_size = SIGSTKSZ;
133 ss.ss_flags = 0;
134
135 if (ss.ss_sp == (void *)-1) {
136 perror("mmap error\n");
137 exit(-1);
138 }
139
140
141 if (madvise(ss.ss_sp, SIGSTKSZ, MADV_DONTNEED)) {
142 perror("madvise\n");
143 exit(-1);
144 }
145
146
147
148
149 if (sigaltstack(&ss, NULL)) {
150 perror("sigaltstack\n");
151 exit(-1);
152 }
153
154
155 sigaction(SIGUSR1, &usr_sa, NULL);
156
157 sigaction(SIGSEGV, &seg_sa, NULL);
158
159 raise(SIGUSR1);
160 }
161
162 int tm_signal_context_force_tm(void)
163 {
164 SKIP_IF(!have_htm());
165
166
167
168
169
170 SKIP_IF(!is_ppc64le());
171
172
173 getcontext(&main_context);
174
175 if (!first_time++)
176 tm_trap_test();
177
178 return EXIT_SUCCESS;
179 }
180
181 int main(int argc, char **argv)
182 {
183 test_harness(tm_signal_context_force_tm, "tm_signal_context_force_tm");
184 }