162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2016, Cyril Bur, IBM Corp.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Test the kernel's signal frame code.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * The kernel sets up two sets of ucontexts if the signal was to be
862306a36Sopenharmony_ci * delivered while the thread was in a transaction (referred too as
962306a36Sopenharmony_ci * first and second contexts).
1062306a36Sopenharmony_ci * Expected behaviour is that the checkpointed state is in the user
1162306a36Sopenharmony_ci * context passed to the signal handler (first context). The speculated
1262306a36Sopenharmony_ci * state can be accessed with the uc_link pointer (second context).
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * The rationale for this is that if TM unaware code (which linked
1562306a36Sopenharmony_ci * against TM libs) installs a signal handler it will not know of the
1662306a36Sopenharmony_ci * speculative nature of the 'live' registers and may infer the wrong
1762306a36Sopenharmony_ci * thing.
1862306a36Sopenharmony_ci */
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#include <stdlib.h>
2162306a36Sopenharmony_ci#include <stdio.h>
2262306a36Sopenharmony_ci#include <string.h>
2362306a36Sopenharmony_ci#include <signal.h>
2462306a36Sopenharmony_ci#include <unistd.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#include <altivec.h>
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include "utils.h"
2962306a36Sopenharmony_ci#include "tm.h"
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#define MAX_ATTEMPT 500000
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define NV_VSX_REGS 12 /* Number of VSX registers to check. */
3462306a36Sopenharmony_ci#define VSX20 20 /* First VSX register to check in vsr20-vsr31 subset */
3562306a36Sopenharmony_ci#define FPR20 20 /* FPR20 overlaps VSX20 most significant doubleword */
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cilong tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cistatic sig_atomic_t fail, broken;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/* Test only 12 vsx registers from vsr20 to vsr31 */
4262306a36Sopenharmony_civector int vsxs[] = {
4362306a36Sopenharmony_ci	/* First context will be set with these values, i.e. non-speculative */
4462306a36Sopenharmony_ci	/* VSX20     ,  VSX21      , ... */
4562306a36Sopenharmony_ci	{ 1, 2, 3, 4},{ 5, 6, 7, 8},{ 9,10,11,12},
4662306a36Sopenharmony_ci	{13,14,15,16},{17,18,19,20},{21,22,23,24},
4762306a36Sopenharmony_ci	{25,26,27,28},{29,30,31,32},{33,34,35,36},
4862306a36Sopenharmony_ci	{37,38,39,40},{41,42,43,44},{45,46,47,48},
4962306a36Sopenharmony_ci	/* Second context will be set with these values, i.e. speculative */
5062306a36Sopenharmony_ci	/* VSX20         ,  VSX21          , ... */
5162306a36Sopenharmony_ci	{-1, -2, -3, -4 },{-5, -6, -7, -8 },{-9, -10,-11,-12},
5262306a36Sopenharmony_ci	{-13,-14,-15,-16},{-17,-18,-19,-20},{-21,-22,-23,-24},
5362306a36Sopenharmony_ci	{-25,-26,-27,-28},{-29,-30,-31,-32},{-33,-34,-35,-36},
5462306a36Sopenharmony_ci	{-37,-38,-39,-40},{-41,-42,-43,-44},{-45,-46,-47,-48}
5562306a36Sopenharmony_ci};
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistatic void signal_usr1(int signum, siginfo_t *info, void *uc)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	int i, j;
6062306a36Sopenharmony_ci	uint8_t vsx[sizeof(vector int)];
6162306a36Sopenharmony_ci	uint8_t vsx_tm[sizeof(vector int)];
6262306a36Sopenharmony_ci	ucontext_t *ucp = uc;
6362306a36Sopenharmony_ci	ucontext_t *tm_ucp = ucp->uc_link;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	/*
6662306a36Sopenharmony_ci	 * FP registers and VMX registers overlap the VSX registers.
6762306a36Sopenharmony_ci	 *
6862306a36Sopenharmony_ci	 * FP registers (f0-31) overlap the most significant 64 bits of VSX
6962306a36Sopenharmony_ci	 * registers vsr0-31, whilst VMX registers vr0-31, being 128-bit like
7062306a36Sopenharmony_ci	 * the VSX registers, overlap fully the other half of VSX registers,
7162306a36Sopenharmony_ci	 * i.e. vr0-31 overlaps fully vsr32-63.
7262306a36Sopenharmony_ci	 *
7362306a36Sopenharmony_ci	 * Due to compatibility and historical reasons (VMX/Altivec support
7462306a36Sopenharmony_ci	 * appeared first on the architecture), VMX registers vr0-31 (so VSX
7562306a36Sopenharmony_ci	 * half vsr32-63 too) are stored right after the v_regs pointer, in an
7662306a36Sopenharmony_ci	 * area allocated for 'vmx_reverse' array (please see
7762306a36Sopenharmony_ci	 * arch/powerpc/include/uapi/asm/sigcontext.h for details about the
7862306a36Sopenharmony_ci	 * mcontext_t structure on Power).
7962306a36Sopenharmony_ci	 *
8062306a36Sopenharmony_ci	 * The other VSX half (vsr0-31) is hence stored below vr0-31/vsr32-63
8162306a36Sopenharmony_ci	 * registers, but only the least significant 64 bits of vsr0-31. The
8262306a36Sopenharmony_ci	 * most significant 64 bits of vsr0-31 (f0-31), as it overlaps the FP
8362306a36Sopenharmony_ci	 * registers, is kept in fp_regs.
8462306a36Sopenharmony_ci	 *
8562306a36Sopenharmony_ci	 * v_regs is a 16 byte aligned pointer at the start of vmx_reserve
8662306a36Sopenharmony_ci	 * (vmx_reserve may or may not be 16 aligned) where the v_regs structure
8762306a36Sopenharmony_ci	 * exists, so v_regs points to where vr0-31 / vsr32-63 registers are
8862306a36Sopenharmony_ci	 * fully stored. Since v_regs type is elf_vrregset_t, v_regs + 1
8962306a36Sopenharmony_ci	 * skips all the slots used to store vr0-31 / vsr32-64 and points to
9062306a36Sopenharmony_ci	 * part of one VSX half, i.e. v_regs + 1 points to the least significant
9162306a36Sopenharmony_ci	 * 64 bits of vsr0-31. The other part of this half (the most significant
9262306a36Sopenharmony_ci	 * part of vsr0-31) is stored in fp_regs.
9362306a36Sopenharmony_ci	 *
9462306a36Sopenharmony_ci	 */
9562306a36Sopenharmony_ci	/* Get pointer to least significant doubleword of vsr0-31 */
9662306a36Sopenharmony_ci	long *vsx_ptr = (long *)(ucp->uc_mcontext.v_regs + 1);
9762306a36Sopenharmony_ci	long *tm_vsx_ptr = (long *)(tm_ucp->uc_mcontext.v_regs + 1);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	/* Check first context. Print all mismatches. */
10062306a36Sopenharmony_ci	for (i = 0; i < NV_VSX_REGS; i++) {
10162306a36Sopenharmony_ci		/*
10262306a36Sopenharmony_ci		 * Copy VSX most significant doubleword from fp_regs and
10362306a36Sopenharmony_ci		 * copy VSX least significant one from 64-bit slots below
10462306a36Sopenharmony_ci		 * saved VMX registers.
10562306a36Sopenharmony_ci		 */
10662306a36Sopenharmony_ci		memcpy(vsx, &ucp->uc_mcontext.fp_regs[FPR20 + i], 8);
10762306a36Sopenharmony_ci		memcpy(vsx + 8, &vsx_ptr[VSX20 + i], 8);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci		fail = memcmp(vsx, &vsxs[i], sizeof(vector int));
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci		if (fail) {
11262306a36Sopenharmony_ci			broken = 1;
11362306a36Sopenharmony_ci			printf("VSX%d (1st context) == 0x", VSX20 + i);
11462306a36Sopenharmony_ci			for (j = 0; j < 16; j++)
11562306a36Sopenharmony_ci				printf("%02x", vsx[j]);
11662306a36Sopenharmony_ci			printf(" instead of 0x");
11762306a36Sopenharmony_ci			for (j = 0; j < 4; j++)
11862306a36Sopenharmony_ci				printf("%08x", vsxs[i][j]);
11962306a36Sopenharmony_ci			printf(" (expected)\n");
12062306a36Sopenharmony_ci		}
12162306a36Sopenharmony_ci	}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	/* Check second context. Print all mismatches. */
12462306a36Sopenharmony_ci	for (i = 0; i < NV_VSX_REGS; i++) {
12562306a36Sopenharmony_ci		/*
12662306a36Sopenharmony_ci		 * Copy VSX most significant doubleword from fp_regs and
12762306a36Sopenharmony_ci		 * copy VSX least significant one from 64-bit slots below
12862306a36Sopenharmony_ci		 * saved VMX registers.
12962306a36Sopenharmony_ci		 */
13062306a36Sopenharmony_ci		memcpy(vsx_tm, &tm_ucp->uc_mcontext.fp_regs[FPR20 + i], 8);
13162306a36Sopenharmony_ci		memcpy(vsx_tm + 8, &tm_vsx_ptr[VSX20 + i], 8);
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci		fail = memcmp(vsx_tm, &vsxs[NV_VSX_REGS + i], sizeof(vector int));
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci		if (fail) {
13662306a36Sopenharmony_ci			broken = 1;
13762306a36Sopenharmony_ci			printf("VSX%d (2nd context) == 0x", VSX20 + i);
13862306a36Sopenharmony_ci			for (j = 0; j < 16; j++)
13962306a36Sopenharmony_ci				printf("%02x", vsx_tm[j]);
14062306a36Sopenharmony_ci			printf(" instead of 0x");
14162306a36Sopenharmony_ci			for (j = 0; j < 4; j++)
14262306a36Sopenharmony_ci				printf("%08x", vsxs[NV_VSX_REGS + i][j]);
14362306a36Sopenharmony_ci			printf("(expected)\n");
14462306a36Sopenharmony_ci		}
14562306a36Sopenharmony_ci	}
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_cistatic int tm_signal_context_chk()
14962306a36Sopenharmony_ci{
15062306a36Sopenharmony_ci	struct sigaction act;
15162306a36Sopenharmony_ci	int i;
15262306a36Sopenharmony_ci	long rc;
15362306a36Sopenharmony_ci	pid_t pid = getpid();
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	SKIP_IF(!have_htm());
15662306a36Sopenharmony_ci	SKIP_IF(htm_is_synthetic());
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	act.sa_sigaction = signal_usr1;
15962306a36Sopenharmony_ci	sigemptyset(&act.sa_mask);
16062306a36Sopenharmony_ci	act.sa_flags = SA_SIGINFO;
16162306a36Sopenharmony_ci	if (sigaction(SIGUSR1, &act, NULL) < 0) {
16262306a36Sopenharmony_ci		perror("sigaction sigusr1");
16362306a36Sopenharmony_ci		exit(1);
16462306a36Sopenharmony_ci	}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	i = 0;
16762306a36Sopenharmony_ci	while (i < MAX_ATTEMPT && !broken) {
16862306a36Sopenharmony_ci               /*
16962306a36Sopenharmony_ci                * tm_signal_self_context_load will set both first and second
17062306a36Sopenharmony_ci                * contexts accordingly to the values passed through non-NULL
17162306a36Sopenharmony_ci                * array pointers to it, in that case 'vsxs', and invoke the
17262306a36Sopenharmony_ci                * signal handler installed for SIGUSR1.
17362306a36Sopenharmony_ci                */
17462306a36Sopenharmony_ci		rc = tm_signal_self_context_load(pid, NULL, NULL, NULL, vsxs);
17562306a36Sopenharmony_ci		FAIL_IF(rc != pid);
17662306a36Sopenharmony_ci		i++;
17762306a36Sopenharmony_ci	}
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	return (broken);
18062306a36Sopenharmony_ci}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ciint main(void)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	return test_harness(tm_signal_context_chk, "tm_signal_context_chk_vsx");
18562306a36Sopenharmony_ci}
186