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 <string.h>
0023 #include <signal.h>
0024 #include <unistd.h>
0025 
0026 #include <altivec.h>
0027 
0028 #include "utils.h"
0029 #include "tm.h"
0030 
0031 #define MAX_ATTEMPT 500000
0032 
0033 #define NV_VMX_REGS 12 /* Number of non-volatile VMX registers */
0034 #define VMX20 20 /* First non-volatile register to check in vr20-31 subset */
0035 
0036 long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
0037 
0038 static sig_atomic_t fail, broken;
0039 
0040 /* Test only non-volatile registers, i.e. 12 vmx registers from vr20 to vr31 */
0041 vector int vms[] = {
0042     /* First context will be set with these values, i.e. non-speculative */
0043     /* VMX20     ,  VMX21      , ... */
0044     { 1, 2, 3, 4},{ 5, 6, 7, 8},{ 9,10,11,12},
0045     {13,14,15,16},{17,18,19,20},{21,22,23,24},
0046     {25,26,27,28},{29,30,31,32},{33,34,35,36},
0047     {37,38,39,40},{41,42,43,44},{45,46,47,48},
0048     /* Second context will be set with these values, i.e. speculative */
0049     /* VMX20        , VMX21            , ... */
0050     { -1, -2, -3, -4},{ -5, -6, -7, -8},{ -9,-10,-11,-12},
0051     {-13,-14,-15,-16},{-17,-18,-19,-20},{-21,-22,-23,-24},
0052     {-25,-26,-27,-28},{-29,-30,-31,-32},{-33,-34,-35,-36},
0053     {-37,-38,-39,-40},{-41,-42,-43,-44},{-45,-46,-47,-48}
0054 };
0055 
0056 static void signal_usr1(int signum, siginfo_t *info, void *uc)
0057 {
0058     int i, j;
0059     ucontext_t *ucp = uc;
0060     ucontext_t *tm_ucp = ucp->uc_link;
0061 
0062     for (i = 0; i < NV_VMX_REGS; i++) {
0063         /* Check first context. Print all mismatches. */
0064         fail = memcmp(ucp->uc_mcontext.v_regs->vrregs[VMX20 + i],
0065                 &vms[i], sizeof(vector int));
0066         if (fail) {
0067             broken = 1;
0068             printf("VMX%d (1st context) == 0x", VMX20 + i);
0069             /* Print actual value in first context. */
0070             for (j = 0; j < 4; j++)
0071                 printf("%08x", ucp->uc_mcontext.v_regs->vrregs[VMX20 + i][j]);
0072             printf(" instead of 0x");
0073             /* Print expected value. */
0074             for (j = 0; j < 4; j++)
0075                 printf("%08x", vms[i][j]);
0076             printf(" (expected)\n");
0077         }
0078     }
0079 
0080     for (i = 0; i < NV_VMX_REGS; i++)  {
0081         /* Check second context. Print all mismatches. */
0082         fail = memcmp(tm_ucp->uc_mcontext.v_regs->vrregs[VMX20 + i],
0083                 &vms[NV_VMX_REGS + i], sizeof (vector int));
0084         if (fail) {
0085             broken = 1;
0086             printf("VMX%d (2nd context) == 0x", NV_VMX_REGS + i);
0087             /* Print actual value in second context. */
0088             for (j = 0; j < 4; j++)
0089                 printf("%08x", tm_ucp->uc_mcontext.v_regs->vrregs[VMX20 + i][j]);
0090             printf(" instead of 0x");
0091             /* Print expected value. */
0092             for (j = 0; j < 4; j++)
0093                 printf("%08x", vms[NV_VMX_REGS + i][j]);
0094             printf(" (expected)\n");
0095         }
0096     }
0097 }
0098 
0099 static int tm_signal_context_chk()
0100 {
0101     struct sigaction act;
0102     int i;
0103     long rc;
0104     pid_t pid = getpid();
0105 
0106     SKIP_IF(!have_htm());
0107     SKIP_IF(htm_is_synthetic());
0108 
0109     act.sa_sigaction = signal_usr1;
0110     sigemptyset(&act.sa_mask);
0111     act.sa_flags = SA_SIGINFO;
0112     if (sigaction(SIGUSR1, &act, NULL) < 0) {
0113         perror("sigaction sigusr1");
0114         exit(1);
0115     }
0116 
0117     i = 0;
0118     while (i < MAX_ATTEMPT && !broken) {
0119         /*
0120          * tm_signal_self_context_load will set both first and second
0121          * contexts accordingly to the values passed through non-NULL
0122          * array pointers to it, in that case 'vms', and invoke the
0123          * signal handler installed for SIGUSR1.
0124          */
0125         rc = tm_signal_self_context_load(pid, NULL, NULL, vms, NULL);
0126         FAIL_IF(rc != pid);
0127         i++;
0128     }
0129 
0130     return (broken);
0131 }
0132 
0133 int main(void)
0134 {
0135     return test_harness(tm_signal_context_chk, "tm_signal_context_chk_vmx");
0136 }