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
45ucontext_t init_context;
46
47
48static volatile int count;
49
50void usr_signal_handler(int signo, siginfo_t *si, void *uc)
51{
52 ucontext_t *ucp = uc;
53 int ret;
54
55
56
57
58
59
60 ucp->uc_link = mmap(NULL, sizeof(ucontext_t),
61 PROT_READ | PROT_WRITE,
62 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
63 if (ucp->uc_link == (void *)-1) {
64 perror("Mmap failed");
65 exit(-1);
66 }
67
68
69 ret = madvise(ucp->uc_link, sizeof(ucontext_t), MADV_DONTNEED);
70 if (ret) {
71 perror("madvise failed");
72 exit(-1);
73 }
74
75 memcpy(&ucp->uc_link->uc_mcontext, &ucp->uc_mcontext,
76 sizeof(ucp->uc_mcontext));
77
78
79 UCONTEXT_MSR(ucp) |= MSR_TS_S;
80
81
82
83
84
85 if (fork() == 0) {
86
87
88
89
90
91 count = COUNT_MAX;
92 }
93
94
95
96
97
98}
99
100void seg_signal_handler(int signo, siginfo_t *si, void *uc)
101{
102 count++;
103
104
105 setcontext(&init_context);
106}
107
108void tm_trap_test(void)
109{
110 struct sigaction usr_sa, seg_sa;
111 stack_t ss;
112
113 usr_sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
114 usr_sa.sa_sigaction = usr_signal_handler;
115
116 seg_sa.sa_flags = SA_SIGINFO;
117 seg_sa.sa_sigaction = seg_signal_handler;
118
119
120
121
122
123 getcontext(&init_context);
124
125 while (count < COUNT_MAX) {
126
127 ss.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
128 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
129 ss.ss_size = SIGSTKSZ;
130 ss.ss_flags = 0;
131
132 if (ss.ss_sp == (void *)-1) {
133 perror("mmap error\n");
134 exit(-1);
135 }
136
137
138 if (madvise(ss.ss_sp, SIGSTKSZ, MADV_DONTNEED)) {
139 perror("madvise\n");
140 exit(-1);
141 }
142
143
144
145
146
147 if (sigaltstack(&ss, NULL)) {
148 perror("sigaltstack\n");
149 exit(-1);
150 }
151
152
153 sigaction(SIGUSR1, &usr_sa, NULL);
154
155 sigaction(SIGSEGV, &seg_sa, NULL);
156
157 raise(SIGUSR1);
158 count++;
159 }
160}
161
162int tm_signal_context_force_tm(void)
163{
164 SKIP_IF(!have_htm());
165
166
167
168
169
170 SKIP_IF(!is_ppc64le());
171
172 tm_trap_test();
173
174 return EXIT_SUCCESS;
175}
176
177int main(int argc, char **argv)
178{
179 test_harness(tm_signal_context_force_tm, "tm_signal_context_force_tm");
180}
181