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_GPR_REGS 18 /* Number of non-volatile GPR registers */
0033 #define R14 14 /* First non-volatile register to check in r14-r31 subset */
0034 
0035 long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
0036 
0037 static sig_atomic_t fail, broken;
0038 
0039 /* Test only non-volatile general purpose registers, i.e. r14-r31 */
0040 static long gprs[] = {
0041     /* First context will be set with these values, i.e. non-speculative */
0042     /* R14, R15, ... */
0043      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
0044     /* Second context will be set with these values, i.e. speculative */
0045     /* R14, R15, ... */
0046     -1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18
0047 };
0048 
0049 static void signal_usr1(int signum, siginfo_t *info, void *uc)
0050 {
0051     int i;
0052     ucontext_t *ucp = uc;
0053     ucontext_t *tm_ucp = ucp->uc_link;
0054 
0055     /* Check first context. Print all mismatches. */
0056     for (i = 0; i < NV_GPR_REGS; i++) {
0057         fail = (ucp->uc_mcontext.gp_regs[R14 + i] != gprs[i]);
0058         if (fail) {
0059             broken = 1;
0060             printf("GPR%d (1st context) == %lu instead of %lu (expected)\n",
0061                 R14 + i, ucp->uc_mcontext.gp_regs[R14 + i], gprs[i]);
0062         }
0063     }
0064 
0065     /* Check second context. Print all mismatches. */
0066     for (i = 0; i < NV_GPR_REGS; i++) {
0067         fail = (tm_ucp->uc_mcontext.gp_regs[R14 + i] != gprs[NV_GPR_REGS + i]);
0068         if (fail) {
0069             broken = 1;
0070             printf("GPR%d (2nd context) == %lu instead of %lu (expected)\n",
0071                 R14 + i, tm_ucp->uc_mcontext.gp_regs[R14 + i], gprs[NV_GPR_REGS + i]);
0072         }
0073     }
0074 }
0075 
0076 static int tm_signal_context_chk_gpr()
0077 {
0078     struct sigaction act;
0079     int i;
0080     long rc;
0081     pid_t pid = getpid();
0082 
0083     SKIP_IF(!have_htm());
0084     SKIP_IF(htm_is_synthetic());
0085 
0086     act.sa_sigaction = signal_usr1;
0087     sigemptyset(&act.sa_mask);
0088     act.sa_flags = SA_SIGINFO;
0089     if (sigaction(SIGUSR1, &act, NULL) < 0) {
0090         perror("sigaction sigusr1");
0091         exit(1);
0092     }
0093 
0094     i = 0;
0095     while (i < MAX_ATTEMPT && !broken) {
0096                 /*
0097                  * tm_signal_self_context_load will set both first and second
0098                  * contexts accordingly to the values passed through non-NULL
0099                  * array pointers to it, in that case 'gprs', and invoke the
0100                  * signal handler installed for SIGUSR1.
0101                  */
0102         rc = tm_signal_self_context_load(pid, gprs, NULL, NULL, NULL);
0103         FAIL_IF(rc != pid);
0104         i++;
0105     }
0106 
0107     return broken;
0108 }
0109 
0110 int main(void)
0111 {
0112     return test_harness(tm_signal_context_chk_gpr, "tm_signal_context_chk_gpr");
0113 }