1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2018, Breno Leitao, Gustavo Romero, IBM Corp.
5 * This test raises a SIGUSR1 signal, and toggle the MSR[TS]
6 * fields at the signal handler. With MSR[TS] being set, the kernel will
7 * force a recheckpoint, which may cause a segfault when returning to
8 * user space. Since the test needs to re-run, the segfault needs to be
11 * In order to continue the test even after a segfault, the context is
12 * saved prior to the signal being raised, and it is restored when there is
13 * a segmentation fault. This happens for COUNT_MAX times.
15 * This test never fails (as returning EXIT_FAILURE). It either succeeds,
16 * or crash the kernel (on a buggy kernel).
32 #define COUNT_MAX 5000 /* Number of interactions */
35 * This test only runs on 64 bits system. Unsetting MSR_TS_S to avoid
36 * compilation issue on 32 bits system. There is no side effect, since the
37 * whole test will be skipped if it is not running on 64 bits system.
44 /* Setting contexts because the test will crash and we want to recover */
45 ucontext_t init_context;
47 /* count is changed in the signal handler, so it must be volatile */
48 static volatile int count;
50 void usr_signal_handler(int signo, siginfo_t *si, void *uc)
56 * Allocating memory in a signal handler, and never freeing it on
57 * purpose, forcing the heap increase, so, the memory leak is what
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");
68 /* Forcing the page to be allocated in a page fault */
69 ret = madvise(ucp->uc_link, sizeof(ucontext_t), MADV_DONTNEED);
71 perror("madvise failed");
75 memcpy(&ucp->uc_link->uc_mcontext, &ucp->uc_mcontext,
76 sizeof(ucp->uc_mcontext));
78 /* Forcing to enable MSR[TM] */
79 UCONTEXT_MSR(ucp) |= MSR_TS_S;
82 * A fork inside a signal handler seems to be more efficient than a
83 * fork() prior to the signal being raised.
87 * Both child and parent will return, but, child returns
88 * with count set so it will exit in the next segfault.
89 * Parent will continue to loop.
95 * If the change above does not hit the bug, it will cause a
96 * segmentation fault, since the ck structures are NULL.
100 void seg_signal_handler(int signo, siginfo_t *si, void *uc)
104 /* Reexecute the test */
105 setcontext(&init_context);
108 void tm_trap_test(void)
110 struct sigaction usr_sa, seg_sa;
113 usr_sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
114 usr_sa.sa_sigaction = usr_signal_handler;
116 seg_sa.sa_flags = SA_SIGINFO;
117 seg_sa.sa_sigaction = seg_signal_handler;
120 * Set initial context. Will get back here from
121 * seg_signal_handler()
123 getcontext(&init_context);
125 while (count < COUNT_MAX) {
126 /* Allocated an alternative signal stack area */
127 ss.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
128 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
129 ss.ss_size = SIGSTKSZ;
132 if (ss.ss_sp == (void *)-1) {
133 perror("mmap error\n");
137 /* Force the allocation through a page fault */
138 if (madvise(ss.ss_sp, SIGSTKSZ, MADV_DONTNEED)) {
144 * Setting an alternative stack to generate a page fault when
145 * the signal is raised.
147 if (sigaltstack(&ss, NULL)) {
148 perror("sigaltstack\n");
152 /* The signal handler will enable MSR_TS */
153 sigaction(SIGUSR1, &usr_sa, NULL);
154 /* If it does not crash, it might segfault, avoid it to retest */
155 sigaction(SIGSEGV, &seg_sa, NULL);
162 int tm_signal_context_force_tm(void)
164 SKIP_IF(!have_htm());
166 * Skipping if not running on 64 bits system, since I think it is
167 * not possible to set mcontext's [MSR] with TS, due to it being 32
170 SKIP_IF(!is_ppc64le());
177 int main(int argc, char **argv)
179 test_harness(tm_signal_context_force_tm, "tm_signal_context_force_tm");