18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2016, Cyril Bur, IBM Corp.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Test the kernel's signal frame code.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * The kernel sets up two sets of ucontexts if the signal was to be
88c2ecf20Sopenharmony_ci * delivered while the thread was in a transaction (referred too as
98c2ecf20Sopenharmony_ci * first and second contexts).
108c2ecf20Sopenharmony_ci * Expected behaviour is that the checkpointed state is in the user
118c2ecf20Sopenharmony_ci * context passed to the signal handler (first context). The speculated
128c2ecf20Sopenharmony_ci * state can be accessed with the uc_link pointer (second context).
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The rationale for this is that if TM unaware code (which linked
158c2ecf20Sopenharmony_ci * against TM libs) installs a signal handler it will not know of the
168c2ecf20Sopenharmony_ci * speculative nature of the 'live' registers and may infer the wrong
178c2ecf20Sopenharmony_ci * thing.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <stdlib.h>
218c2ecf20Sopenharmony_ci#include <stdio.h>
228c2ecf20Sopenharmony_ci#include <string.h>
238c2ecf20Sopenharmony_ci#include <signal.h>
248c2ecf20Sopenharmony_ci#include <unistd.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <altivec.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include "utils.h"
298c2ecf20Sopenharmony_ci#include "tm.h"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define MAX_ATTEMPT 500000
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define NV_VSX_REGS 12 /* Number of VSX registers to check. */
348c2ecf20Sopenharmony_ci#define VSX20 20 /* First VSX register to check in vsr20-vsr31 subset */
358c2ecf20Sopenharmony_ci#define FPR20 20 /* FPR20 overlaps VSX20 most significant doubleword */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cilong tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic sig_atomic_t fail, broken;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Test only 12 vsx registers from vsr20 to vsr31 */
428c2ecf20Sopenharmony_civector int vsxs[] = {
438c2ecf20Sopenharmony_ci	/* First context will be set with these values, i.e. non-speculative */
448c2ecf20Sopenharmony_ci	/* VSX20     ,  VSX21      , ... */
458c2ecf20Sopenharmony_ci	{ 1, 2, 3, 4},{ 5, 6, 7, 8},{ 9,10,11,12},
468c2ecf20Sopenharmony_ci	{13,14,15,16},{17,18,19,20},{21,22,23,24},
478c2ecf20Sopenharmony_ci	{25,26,27,28},{29,30,31,32},{33,34,35,36},
488c2ecf20Sopenharmony_ci	{37,38,39,40},{41,42,43,44},{45,46,47,48},
498c2ecf20Sopenharmony_ci	/* Second context will be set with these values, i.e. speculative */
508c2ecf20Sopenharmony_ci	/* VSX20         ,  VSX21          , ... */
518c2ecf20Sopenharmony_ci	{-1, -2, -3, -4 },{-5, -6, -7, -8 },{-9, -10,-11,-12},
528c2ecf20Sopenharmony_ci	{-13,-14,-15,-16},{-17,-18,-19,-20},{-21,-22,-23,-24},
538c2ecf20Sopenharmony_ci	{-25,-26,-27,-28},{-29,-30,-31,-32},{-33,-34,-35,-36},
548c2ecf20Sopenharmony_ci	{-37,-38,-39,-40},{-41,-42,-43,-44},{-45,-46,-47,-48}
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic void signal_usr1(int signum, siginfo_t *info, void *uc)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	int i, j;
608c2ecf20Sopenharmony_ci	uint8_t vsx[sizeof(vector int)];
618c2ecf20Sopenharmony_ci	uint8_t vsx_tm[sizeof(vector int)];
628c2ecf20Sopenharmony_ci	ucontext_t *ucp = uc;
638c2ecf20Sopenharmony_ci	ucontext_t *tm_ucp = ucp->uc_link;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	/*
668c2ecf20Sopenharmony_ci	 * FP registers and VMX registers overlap the VSX registers.
678c2ecf20Sopenharmony_ci	 *
688c2ecf20Sopenharmony_ci	 * FP registers (f0-31) overlap the most significant 64 bits of VSX
698c2ecf20Sopenharmony_ci	 * registers vsr0-31, whilst VMX registers vr0-31, being 128-bit like
708c2ecf20Sopenharmony_ci	 * the VSX registers, overlap fully the other half of VSX registers,
718c2ecf20Sopenharmony_ci	 * i.e. vr0-31 overlaps fully vsr32-63.
728c2ecf20Sopenharmony_ci	 *
738c2ecf20Sopenharmony_ci	 * Due to compatibility and historical reasons (VMX/Altivec support
748c2ecf20Sopenharmony_ci	 * appeared first on the architecture), VMX registers vr0-31 (so VSX
758c2ecf20Sopenharmony_ci	 * half vsr32-63 too) are stored right after the v_regs pointer, in an
768c2ecf20Sopenharmony_ci	 * area allocated for 'vmx_reverse' array (please see
778c2ecf20Sopenharmony_ci	 * arch/powerpc/include/uapi/asm/sigcontext.h for details about the
788c2ecf20Sopenharmony_ci	 * mcontext_t structure on Power).
798c2ecf20Sopenharmony_ci	 *
808c2ecf20Sopenharmony_ci	 * The other VSX half (vsr0-31) is hence stored below vr0-31/vsr32-63
818c2ecf20Sopenharmony_ci	 * registers, but only the least significant 64 bits of vsr0-31. The
828c2ecf20Sopenharmony_ci	 * most significant 64 bits of vsr0-31 (f0-31), as it overlaps the FP
838c2ecf20Sopenharmony_ci	 * registers, is kept in fp_regs.
848c2ecf20Sopenharmony_ci	 *
858c2ecf20Sopenharmony_ci	 * v_regs is a 16 byte aligned pointer at the start of vmx_reserve
868c2ecf20Sopenharmony_ci	 * (vmx_reserve may or may not be 16 aligned) where the v_regs structure
878c2ecf20Sopenharmony_ci	 * exists, so v_regs points to where vr0-31 / vsr32-63 registers are
888c2ecf20Sopenharmony_ci	 * fully stored. Since v_regs type is elf_vrregset_t, v_regs + 1
898c2ecf20Sopenharmony_ci	 * skips all the slots used to store vr0-31 / vsr32-64 and points to
908c2ecf20Sopenharmony_ci	 * part of one VSX half, i.e. v_regs + 1 points to the least significant
918c2ecf20Sopenharmony_ci	 * 64 bits of vsr0-31. The other part of this half (the most significant
928c2ecf20Sopenharmony_ci	 * part of vsr0-31) is stored in fp_regs.
938c2ecf20Sopenharmony_ci	 *
948c2ecf20Sopenharmony_ci	 */
958c2ecf20Sopenharmony_ci	/* Get pointer to least significant doubleword of vsr0-31 */
968c2ecf20Sopenharmony_ci	long *vsx_ptr = (long *)(ucp->uc_mcontext.v_regs + 1);
978c2ecf20Sopenharmony_ci	long *tm_vsx_ptr = (long *)(tm_ucp->uc_mcontext.v_regs + 1);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/* Check first context. Print all mismatches. */
1008c2ecf20Sopenharmony_ci	for (i = 0; i < NV_VSX_REGS; i++) {
1018c2ecf20Sopenharmony_ci		/*
1028c2ecf20Sopenharmony_ci		 * Copy VSX most significant doubleword from fp_regs and
1038c2ecf20Sopenharmony_ci		 * copy VSX least significant one from 64-bit slots below
1048c2ecf20Sopenharmony_ci		 * saved VMX registers.
1058c2ecf20Sopenharmony_ci		 */
1068c2ecf20Sopenharmony_ci		memcpy(vsx, &ucp->uc_mcontext.fp_regs[FPR20 + i], 8);
1078c2ecf20Sopenharmony_ci		memcpy(vsx + 8, &vsx_ptr[VSX20 + i], 8);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		fail = memcmp(vsx, &vsxs[i], sizeof(vector int));
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci		if (fail) {
1128c2ecf20Sopenharmony_ci			broken = 1;
1138c2ecf20Sopenharmony_ci			printf("VSX%d (1st context) == 0x", VSX20 + i);
1148c2ecf20Sopenharmony_ci			for (j = 0; j < 16; j++)
1158c2ecf20Sopenharmony_ci				printf("%02x", vsx[j]);
1168c2ecf20Sopenharmony_ci			printf(" instead of 0x");
1178c2ecf20Sopenharmony_ci			for (j = 0; j < 4; j++)
1188c2ecf20Sopenharmony_ci				printf("%08x", vsxs[i][j]);
1198c2ecf20Sopenharmony_ci			printf(" (expected)\n");
1208c2ecf20Sopenharmony_ci		}
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/* Check second context. Print all mismatches. */
1248c2ecf20Sopenharmony_ci	for (i = 0; i < NV_VSX_REGS; i++) {
1258c2ecf20Sopenharmony_ci		/*
1268c2ecf20Sopenharmony_ci		 * Copy VSX most significant doubleword from fp_regs and
1278c2ecf20Sopenharmony_ci		 * copy VSX least significant one from 64-bit slots below
1288c2ecf20Sopenharmony_ci		 * saved VMX registers.
1298c2ecf20Sopenharmony_ci		 */
1308c2ecf20Sopenharmony_ci		memcpy(vsx_tm, &tm_ucp->uc_mcontext.fp_regs[FPR20 + i], 8);
1318c2ecf20Sopenharmony_ci		memcpy(vsx_tm + 8, &tm_vsx_ptr[VSX20 + i], 8);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci		fail = memcmp(vsx_tm, &vsxs[NV_VSX_REGS + i], sizeof(vector int));
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci		if (fail) {
1368c2ecf20Sopenharmony_ci			broken = 1;
1378c2ecf20Sopenharmony_ci			printf("VSX%d (2nd context) == 0x", VSX20 + i);
1388c2ecf20Sopenharmony_ci			for (j = 0; j < 16; j++)
1398c2ecf20Sopenharmony_ci				printf("%02x", vsx_tm[j]);
1408c2ecf20Sopenharmony_ci			printf(" instead of 0x");
1418c2ecf20Sopenharmony_ci			for (j = 0; j < 4; j++)
1428c2ecf20Sopenharmony_ci				printf("%08x", vsxs[NV_VSX_REGS + i][j]);
1438c2ecf20Sopenharmony_ci			printf("(expected)\n");
1448c2ecf20Sopenharmony_ci		}
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic int tm_signal_context_chk()
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	struct sigaction act;
1518c2ecf20Sopenharmony_ci	int i;
1528c2ecf20Sopenharmony_ci	long rc;
1538c2ecf20Sopenharmony_ci	pid_t pid = getpid();
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	SKIP_IF(!have_htm());
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	act.sa_sigaction = signal_usr1;
1588c2ecf20Sopenharmony_ci	sigemptyset(&act.sa_mask);
1598c2ecf20Sopenharmony_ci	act.sa_flags = SA_SIGINFO;
1608c2ecf20Sopenharmony_ci	if (sigaction(SIGUSR1, &act, NULL) < 0) {
1618c2ecf20Sopenharmony_ci		perror("sigaction sigusr1");
1628c2ecf20Sopenharmony_ci		exit(1);
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	i = 0;
1668c2ecf20Sopenharmony_ci	while (i < MAX_ATTEMPT && !broken) {
1678c2ecf20Sopenharmony_ci               /*
1688c2ecf20Sopenharmony_ci                * tm_signal_self_context_load will set both first and second
1698c2ecf20Sopenharmony_ci                * contexts accordingly to the values passed through non-NULL
1708c2ecf20Sopenharmony_ci                * array pointers to it, in that case 'vsxs', and invoke the
1718c2ecf20Sopenharmony_ci                * signal handler installed for SIGUSR1.
1728c2ecf20Sopenharmony_ci                */
1738c2ecf20Sopenharmony_ci		rc = tm_signal_self_context_load(pid, NULL, NULL, NULL, vsxs);
1748c2ecf20Sopenharmony_ci		FAIL_IF(rc != pid);
1758c2ecf20Sopenharmony_ci		i++;
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	return (broken);
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ciint main(void)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	return test_harness(tm_signal_context_chk, "tm_signal_context_chk_vsx");
1848c2ecf20Sopenharmony_ci}
185