12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27bb0e7e3SCyril Bur /*
37bb0e7e3SCyril Bur * Copyright 2016, Cyril Bur, IBM Corp.
47bb0e7e3SCyril Bur *
57bb0e7e3SCyril Bur * Test the kernel's signal frame code.
67bb0e7e3SCyril Bur *
77bb0e7e3SCyril Bur * The kernel sets up two sets of ucontexts if the signal was to be
89d535e20SGustavo Romero * delivered while the thread was in a transaction (referred too as
99d535e20SGustavo Romero * first and second contexts).
107bb0e7e3SCyril Bur * Expected behaviour is that the checkpointed state is in the user
119d535e20SGustavo Romero * context passed to the signal handler (first context). The speculated
129d535e20SGustavo Romero * state can be accessed with the uc_link pointer (second context).
137bb0e7e3SCyril Bur *
147bb0e7e3SCyril Bur * The rationale for this is that if TM unaware code (which linked
157bb0e7e3SCyril Bur * against TM libs) installs a signal handler it will not know of the
167bb0e7e3SCyril Bur * speculative nature of the 'live' registers and may infer the wrong
177bb0e7e3SCyril Bur * thing.
187bb0e7e3SCyril Bur */
197bb0e7e3SCyril Bur
207bb0e7e3SCyril Bur #include <stdlib.h>
217bb0e7e3SCyril Bur #include <stdio.h>
227bb0e7e3SCyril Bur #include <string.h>
237bb0e7e3SCyril Bur #include <signal.h>
247bb0e7e3SCyril Bur #include <unistd.h>
257bb0e7e3SCyril Bur
267bb0e7e3SCyril Bur #include <altivec.h>
277bb0e7e3SCyril Bur
287bb0e7e3SCyril Bur #include "utils.h"
297bb0e7e3SCyril Bur #include "tm.h"
307bb0e7e3SCyril Bur
317bb0e7e3SCyril Bur #define MAX_ATTEMPT 500000
327bb0e7e3SCyril Bur
339d535e20SGustavo Romero #define NV_VMX_REGS 12 /* Number of non-volatile VMX registers */
349d535e20SGustavo Romero #define VMX20 20 /* First non-volatile register to check in vr20-31 subset */
357bb0e7e3SCyril Bur
367bb0e7e3SCyril Bur long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
377bb0e7e3SCyril Bur
389d535e20SGustavo Romero static sig_atomic_t fail, broken;
397bb0e7e3SCyril Bur
409d535e20SGustavo Romero /* Test only non-volatile registers, i.e. 12 vmx registers from vr20 to vr31 */
417bb0e7e3SCyril Bur vector int vms[] = {
429d535e20SGustavo Romero /* First context will be set with these values, i.e. non-speculative */
439d535e20SGustavo Romero /* VMX20 , VMX21 , ... */
447bb0e7e3SCyril Bur { 1, 2, 3, 4},{ 5, 6, 7, 8},{ 9,10,11,12},
457bb0e7e3SCyril Bur {13,14,15,16},{17,18,19,20},{21,22,23,24},
467bb0e7e3SCyril Bur {25,26,27,28},{29,30,31,32},{33,34,35,36},
477bb0e7e3SCyril Bur {37,38,39,40},{41,42,43,44},{45,46,47,48},
489d535e20SGustavo Romero /* Second context will be set with these values, i.e. speculative */
499d535e20SGustavo Romero /* VMX20 , VMX21 , ... */
507bb0e7e3SCyril Bur { -1, -2, -3, -4},{ -5, -6, -7, -8},{ -9,-10,-11,-12},
517bb0e7e3SCyril Bur {-13,-14,-15,-16},{-17,-18,-19,-20},{-21,-22,-23,-24},
527bb0e7e3SCyril Bur {-25,-26,-27,-28},{-29,-30,-31,-32},{-33,-34,-35,-36},
537bb0e7e3SCyril Bur {-37,-38,-39,-40},{-41,-42,-43,-44},{-45,-46,-47,-48}
547bb0e7e3SCyril Bur };
557bb0e7e3SCyril Bur
signal_usr1(int signum,siginfo_t * info,void * uc)567bb0e7e3SCyril Bur static void signal_usr1(int signum, siginfo_t *info, void *uc)
577bb0e7e3SCyril Bur {
589d535e20SGustavo Romero int i, j;
597bb0e7e3SCyril Bur ucontext_t *ucp = uc;
607bb0e7e3SCyril Bur ucontext_t *tm_ucp = ucp->uc_link;
617bb0e7e3SCyril Bur
629d535e20SGustavo Romero for (i = 0; i < NV_VMX_REGS; i++) {
639d535e20SGustavo Romero /* Check first context. Print all mismatches. */
649d535e20SGustavo Romero fail = memcmp(ucp->uc_mcontext.v_regs->vrregs[VMX20 + i],
657bb0e7e3SCyril Bur &vms[i], sizeof(vector int));
667bb0e7e3SCyril Bur if (fail) {
679d535e20SGustavo Romero broken = 1;
689d535e20SGustavo Romero printf("VMX%d (1st context) == 0x", VMX20 + i);
699d535e20SGustavo Romero /* Print actual value in first context. */
709d535e20SGustavo Romero for (j = 0; j < 4; j++)
719d535e20SGustavo Romero printf("%08x", ucp->uc_mcontext.v_regs->vrregs[VMX20 + i][j]);
729d535e20SGustavo Romero printf(" instead of 0x");
739d535e20SGustavo Romero /* Print expected value. */
749d535e20SGustavo Romero for (j = 0; j < 4; j++)
759d535e20SGustavo Romero printf("%08x", vms[i][j]);
769d535e20SGustavo Romero printf(" (expected)\n");
779d535e20SGustavo Romero }
789d535e20SGustavo Romero }
797bb0e7e3SCyril Bur
809d535e20SGustavo Romero for (i = 0; i < NV_VMX_REGS; i++) {
819d535e20SGustavo Romero /* Check second context. Print all mismatches. */
829d535e20SGustavo Romero fail = memcmp(tm_ucp->uc_mcontext.v_regs->vrregs[VMX20 + i],
839d535e20SGustavo Romero &vms[NV_VMX_REGS + i], sizeof (vector int));
849d535e20SGustavo Romero if (fail) {
859d535e20SGustavo Romero broken = 1;
869d535e20SGustavo Romero printf("VMX%d (2nd context) == 0x", NV_VMX_REGS + i);
879d535e20SGustavo Romero /* Print actual value in second context. */
887bb0e7e3SCyril Bur for (j = 0; j < 4; j++)
899d535e20SGustavo Romero printf("%08x", tm_ucp->uc_mcontext.v_regs->vrregs[VMX20 + i][j]);
909d535e20SGustavo Romero printf(" instead of 0x");
919d535e20SGustavo Romero /* Print expected value. */
927bb0e7e3SCyril Bur for (j = 0; j < 4; j++)
939d535e20SGustavo Romero printf("%08x", vms[NV_VMX_REGS + i][j]);
949d535e20SGustavo Romero printf(" (expected)\n");
957bb0e7e3SCyril Bur }
967bb0e7e3SCyril Bur }
977bb0e7e3SCyril Bur }
987bb0e7e3SCyril Bur
tm_signal_context_chk()997bb0e7e3SCyril Bur static int tm_signal_context_chk()
1007bb0e7e3SCyril Bur {
1017bb0e7e3SCyril Bur struct sigaction act;
1027bb0e7e3SCyril Bur int i;
1037bb0e7e3SCyril Bur long rc;
1047bb0e7e3SCyril Bur pid_t pid = getpid();
1057bb0e7e3SCyril Bur
1067bb0e7e3SCyril Bur SKIP_IF(!have_htm());
107*e42edf9bSJordan Niethe SKIP_IF(htm_is_synthetic());
1087bb0e7e3SCyril Bur
1097bb0e7e3SCyril Bur act.sa_sigaction = signal_usr1;
1107bb0e7e3SCyril Bur sigemptyset(&act.sa_mask);
1117bb0e7e3SCyril Bur act.sa_flags = SA_SIGINFO;
1127bb0e7e3SCyril Bur if (sigaction(SIGUSR1, &act, NULL) < 0) {
1137bb0e7e3SCyril Bur perror("sigaction sigusr1");
1147bb0e7e3SCyril Bur exit(1);
1157bb0e7e3SCyril Bur }
1167bb0e7e3SCyril Bur
1177bb0e7e3SCyril Bur i = 0;
1189d535e20SGustavo Romero while (i < MAX_ATTEMPT && !broken) {
1199d535e20SGustavo Romero /*
1209d535e20SGustavo Romero * tm_signal_self_context_load will set both first and second
1219d535e20SGustavo Romero * contexts accordingly to the values passed through non-NULL
1229d535e20SGustavo Romero * array pointers to it, in that case 'vms', and invoke the
1239d535e20SGustavo Romero * signal handler installed for SIGUSR1.
1249d535e20SGustavo Romero */
1257bb0e7e3SCyril Bur rc = tm_signal_self_context_load(pid, NULL, NULL, vms, NULL);
1267bb0e7e3SCyril Bur FAIL_IF(rc != pid);
1277bb0e7e3SCyril Bur i++;
1287bb0e7e3SCyril Bur }
1297bb0e7e3SCyril Bur
1309d535e20SGustavo Romero return (broken);
1317bb0e7e3SCyril Bur }
1327bb0e7e3SCyril Bur
main(void)1337bb0e7e3SCyril Bur int main(void)
1347bb0e7e3SCyril Bur {
1357bb0e7e3SCyril Bur return test_harness(tm_signal_context_chk, "tm_signal_context_chk_vmx");
1367bb0e7e3SCyril Bur }
137