Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2016, Cyril Bur, IBM Corp.
0004  *
0005  * Test the kernel's signal frame code.
0006  *
0007  * The kernel sets up two sets of ucontexts if the signal was to be
0008  * delivered while the thread was in a transaction (referred too as
0009  * first and second contexts).
0010  * Expected behaviour is that the checkpointed state is in the user
0011  * context passed to the signal handler (first context). The speculated
0012  * state can be accessed with the uc_link pointer (second context).
0013  *
0014  * The rationale for this is that if TM unaware code (which linked
0015  * against TM libs) installs a signal handler it will not know of the
0016  * speculative nature of the 'live' registers and may infer the wrong
0017  * thing.
0018  */
0019 
0020 #include <stdlib.h>
0021 #include <stdio.h>
0022 #include <signal.h>
0023 #include <unistd.h>
0024 
0025 #include <altivec.h>
0026 
0027 #include "utils.h"
0028 #include "tm.h"
0029 
0030 #define MAX_ATTEMPT 500000
0031 
0032 #define NV_FPU_REGS 18 /* Number of non-volatile FP registers */
0033 #define FPR14 14 /* First non-volatile FP register to check in f14-31 subset */
0034 
0035 long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
0036 
0037 /* Test only non-volatile registers, i.e. 18 fpr registers from f14 to f31 */
0038 static double fps[] = {
0039     /* First context will be set with these values, i.e. non-speculative */
0040      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
0041     /* Second context will be set with these values, i.e. speculative */
0042     -1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18
0043 };
0044 
0045 static sig_atomic_t fail, broken;
0046 
0047 static void signal_usr1(int signum, siginfo_t *info, void *uc)
0048 {
0049     int i;
0050     ucontext_t *ucp = uc;
0051     ucontext_t *tm_ucp = ucp->uc_link;
0052 
0053     for (i = 0; i < NV_FPU_REGS; i++) {
0054         /* Check first context. Print all mismatches. */
0055         fail = (ucp->uc_mcontext.fp_regs[FPR14 + i] != fps[i]);
0056         if (fail) {
0057             broken = 1;
0058             printf("FPR%d (1st context) == %g instead of %g (expected)\n",
0059                 FPR14 + i, ucp->uc_mcontext.fp_regs[FPR14 + i], fps[i]);
0060         }
0061     }
0062 
0063     for (i = 0; i < NV_FPU_REGS; i++) {
0064         /* Check second context. Print all mismatches. */
0065         fail = (tm_ucp->uc_mcontext.fp_regs[FPR14 + i] != fps[NV_FPU_REGS + i]);
0066         if (fail) {
0067             broken = 1;
0068             printf("FPR%d (2nd context) == %g instead of %g (expected)\n",
0069                 FPR14 + i, tm_ucp->uc_mcontext.fp_regs[FPR14 + i], fps[NV_FPU_REGS + i]);
0070         }
0071     }
0072 }
0073 
0074 static int tm_signal_context_chk_fpu()
0075 {
0076     struct sigaction act;
0077     int i;
0078     long rc;
0079     pid_t pid = getpid();
0080 
0081     SKIP_IF(!have_htm());
0082     SKIP_IF(htm_is_synthetic());
0083 
0084     act.sa_sigaction = signal_usr1;
0085     sigemptyset(&act.sa_mask);
0086     act.sa_flags = SA_SIGINFO;
0087     if (sigaction(SIGUSR1, &act, NULL) < 0) {
0088         perror("sigaction sigusr1");
0089         exit(1);
0090     }
0091 
0092     i = 0;
0093     while (i < MAX_ATTEMPT && !broken) {
0094         /*
0095          * tm_signal_self_context_load will set both first and second
0096          * contexts accordingly to the values passed through non-NULL
0097          * array pointers to it, in that case 'fps', and invoke the
0098          * signal handler installed for SIGUSR1.
0099          */
0100         rc = tm_signal_self_context_load(pid, NULL, fps, NULL, NULL);
0101         FAIL_IF(rc != pid);
0102         i++;
0103     }
0104 
0105     return (broken);
0106 }
0107 
0108 int main(void)
0109 {
0110     return test_harness(tm_signal_context_chk_fpu, "tm_signal_context_chk_fpu");
0111 }