1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2016, Cyril Bur, IBM Corp.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Test the kernel's signal frame code.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * The kernel sets up two sets of ucontexts if the signal was to be
8*4882a593Smuzhiyun * delivered while the thread was in a transaction (referred too as
9*4882a593Smuzhiyun * first and second contexts).
10*4882a593Smuzhiyun * Expected behaviour is that the checkpointed state is in the user
11*4882a593Smuzhiyun * context passed to the signal handler (first context). The speculated
12*4882a593Smuzhiyun * state can be accessed with the uc_link pointer (second context).
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * The rationale for this is that if TM unaware code (which linked
15*4882a593Smuzhiyun * against TM libs) installs a signal handler it will not know of the
16*4882a593Smuzhiyun * speculative nature of the 'live' registers and may infer the wrong
17*4882a593Smuzhiyun * thing.
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include <stdlib.h>
21*4882a593Smuzhiyun #include <stdio.h>
22*4882a593Smuzhiyun #include <signal.h>
23*4882a593Smuzhiyun #include <unistd.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #include <altivec.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include "utils.h"
28*4882a593Smuzhiyun #include "tm.h"
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #define MAX_ATTEMPT 500000
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define NV_FPU_REGS 18 /* Number of non-volatile FP registers */
33*4882a593Smuzhiyun #define FPR14 14 /* First non-volatile FP register to check in f14-31 subset */
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* Test only non-volatile registers, i.e. 18 fpr registers from f14 to f31 */
38*4882a593Smuzhiyun static double fps[] = {
39*4882a593Smuzhiyun /* First context will be set with these values, i.e. non-speculative */
40*4882a593Smuzhiyun 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
41*4882a593Smuzhiyun /* Second context will be set with these values, i.e. speculative */
42*4882a593Smuzhiyun -1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun static sig_atomic_t fail, broken;
46*4882a593Smuzhiyun
signal_usr1(int signum,siginfo_t * info,void * uc)47*4882a593Smuzhiyun static void signal_usr1(int signum, siginfo_t *info, void *uc)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun int i;
50*4882a593Smuzhiyun ucontext_t *ucp = uc;
51*4882a593Smuzhiyun ucontext_t *tm_ucp = ucp->uc_link;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun for (i = 0; i < NV_FPU_REGS; i++) {
54*4882a593Smuzhiyun /* Check first context. Print all mismatches. */
55*4882a593Smuzhiyun fail = (ucp->uc_mcontext.fp_regs[FPR14 + i] != fps[i]);
56*4882a593Smuzhiyun if (fail) {
57*4882a593Smuzhiyun broken = 1;
58*4882a593Smuzhiyun printf("FPR%d (1st context) == %g instead of %g (expected)\n",
59*4882a593Smuzhiyun FPR14 + i, ucp->uc_mcontext.fp_regs[FPR14 + i], fps[i]);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun for (i = 0; i < NV_FPU_REGS; i++) {
64*4882a593Smuzhiyun /* Check second context. Print all mismatches. */
65*4882a593Smuzhiyun fail = (tm_ucp->uc_mcontext.fp_regs[FPR14 + i] != fps[NV_FPU_REGS + i]);
66*4882a593Smuzhiyun if (fail) {
67*4882a593Smuzhiyun broken = 1;
68*4882a593Smuzhiyun printf("FPR%d (2nd context) == %g instead of %g (expected)\n",
69*4882a593Smuzhiyun FPR14 + i, tm_ucp->uc_mcontext.fp_regs[FPR14 + i], fps[NV_FPU_REGS + i]);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
tm_signal_context_chk_fpu()74*4882a593Smuzhiyun static int tm_signal_context_chk_fpu()
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun struct sigaction act;
77*4882a593Smuzhiyun int i;
78*4882a593Smuzhiyun long rc;
79*4882a593Smuzhiyun pid_t pid = getpid();
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun SKIP_IF(!have_htm());
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun act.sa_sigaction = signal_usr1;
84*4882a593Smuzhiyun sigemptyset(&act.sa_mask);
85*4882a593Smuzhiyun act.sa_flags = SA_SIGINFO;
86*4882a593Smuzhiyun if (sigaction(SIGUSR1, &act, NULL) < 0) {
87*4882a593Smuzhiyun perror("sigaction sigusr1");
88*4882a593Smuzhiyun exit(1);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun i = 0;
92*4882a593Smuzhiyun while (i < MAX_ATTEMPT && !broken) {
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * tm_signal_self_context_load will set both first and second
95*4882a593Smuzhiyun * contexts accordingly to the values passed through non-NULL
96*4882a593Smuzhiyun * array pointers to it, in that case 'fps', and invoke the
97*4882a593Smuzhiyun * signal handler installed for SIGUSR1.
98*4882a593Smuzhiyun */
99*4882a593Smuzhiyun rc = tm_signal_self_context_load(pid, NULL, fps, NULL, NULL);
100*4882a593Smuzhiyun FAIL_IF(rc != pid);
101*4882a593Smuzhiyun i++;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun return (broken);
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
main(void)107*4882a593Smuzhiyun int main(void)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun return test_harness(tm_signal_context_chk_fpu, "tm_signal_context_chk_fpu");
110*4882a593Smuzhiyun }
111