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_VMX_REGS 12 /* Number of non-volatile VMX registers */
3462306a36Sopenharmony_ci#define VMX20 20 /* First non-volatile register to check in vr20-31 subset */
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cilong tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic sig_atomic_t fail, broken;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/* Test only non-volatile registers, i.e. 12 vmx registers from vr20 to vr31 */
4162306a36Sopenharmony_civector int vms[] = {
4262306a36Sopenharmony_ci	/* First context will be set with these values, i.e. non-speculative */
4362306a36Sopenharmony_ci	/* VMX20     ,  VMX21      , ... */
4462306a36Sopenharmony_ci	{ 1, 2, 3, 4},{ 5, 6, 7, 8},{ 9,10,11,12},
4562306a36Sopenharmony_ci	{13,14,15,16},{17,18,19,20},{21,22,23,24},
4662306a36Sopenharmony_ci	{25,26,27,28},{29,30,31,32},{33,34,35,36},
4762306a36Sopenharmony_ci	{37,38,39,40},{41,42,43,44},{45,46,47,48},
4862306a36Sopenharmony_ci	/* Second context will be set with these values, i.e. speculative */
4962306a36Sopenharmony_ci	/* VMX20        , VMX21            , ... */
5062306a36Sopenharmony_ci	{ -1, -2, -3, -4},{ -5, -6, -7, -8},{ -9,-10,-11,-12},
5162306a36Sopenharmony_ci	{-13,-14,-15,-16},{-17,-18,-19,-20},{-21,-22,-23,-24},
5262306a36Sopenharmony_ci	{-25,-26,-27,-28},{-29,-30,-31,-32},{-33,-34,-35,-36},
5362306a36Sopenharmony_ci	{-37,-38,-39,-40},{-41,-42,-43,-44},{-45,-46,-47,-48}
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistatic void signal_usr1(int signum, siginfo_t *info, void *uc)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	int i, j;
5962306a36Sopenharmony_ci	ucontext_t *ucp = uc;
6062306a36Sopenharmony_ci	ucontext_t *tm_ucp = ucp->uc_link;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	for (i = 0; i < NV_VMX_REGS; i++) {
6362306a36Sopenharmony_ci		/* Check first context. Print all mismatches. */
6462306a36Sopenharmony_ci		fail = memcmp(ucp->uc_mcontext.v_regs->vrregs[VMX20 + i],
6562306a36Sopenharmony_ci				&vms[i], sizeof(vector int));
6662306a36Sopenharmony_ci		if (fail) {
6762306a36Sopenharmony_ci			broken = 1;
6862306a36Sopenharmony_ci			printf("VMX%d (1st context) == 0x", VMX20 + i);
6962306a36Sopenharmony_ci			/* Print actual value in first context. */
7062306a36Sopenharmony_ci			for (j = 0; j < 4; j++)
7162306a36Sopenharmony_ci				printf("%08x", ucp->uc_mcontext.v_regs->vrregs[VMX20 + i][j]);
7262306a36Sopenharmony_ci			printf(" instead of 0x");
7362306a36Sopenharmony_ci			/* Print expected value. */
7462306a36Sopenharmony_ci			for (j = 0; j < 4; j++)
7562306a36Sopenharmony_ci				printf("%08x", vms[i][j]);
7662306a36Sopenharmony_ci			printf(" (expected)\n");
7762306a36Sopenharmony_ci		}
7862306a36Sopenharmony_ci	}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	for (i = 0; i < NV_VMX_REGS; i++)  {
8162306a36Sopenharmony_ci		/* Check second context. Print all mismatches. */
8262306a36Sopenharmony_ci		fail = memcmp(tm_ucp->uc_mcontext.v_regs->vrregs[VMX20 + i],
8362306a36Sopenharmony_ci				&vms[NV_VMX_REGS + i], sizeof (vector int));
8462306a36Sopenharmony_ci		if (fail) {
8562306a36Sopenharmony_ci			broken = 1;
8662306a36Sopenharmony_ci			printf("VMX%d (2nd context) == 0x", NV_VMX_REGS + i);
8762306a36Sopenharmony_ci			/* Print actual value in second context. */
8862306a36Sopenharmony_ci			for (j = 0; j < 4; j++)
8962306a36Sopenharmony_ci				printf("%08x", tm_ucp->uc_mcontext.v_regs->vrregs[VMX20 + i][j]);
9062306a36Sopenharmony_ci			printf(" instead of 0x");
9162306a36Sopenharmony_ci			/* Print expected value. */
9262306a36Sopenharmony_ci			for (j = 0; j < 4; j++)
9362306a36Sopenharmony_ci				printf("%08x", vms[NV_VMX_REGS + i][j]);
9462306a36Sopenharmony_ci			printf(" (expected)\n");
9562306a36Sopenharmony_ci		}
9662306a36Sopenharmony_ci	}
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistatic int tm_signal_context_chk()
10062306a36Sopenharmony_ci{
10162306a36Sopenharmony_ci	struct sigaction act;
10262306a36Sopenharmony_ci	int i;
10362306a36Sopenharmony_ci	long rc;
10462306a36Sopenharmony_ci	pid_t pid = getpid();
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	SKIP_IF(!have_htm());
10762306a36Sopenharmony_ci	SKIP_IF(htm_is_synthetic());
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	act.sa_sigaction = signal_usr1;
11062306a36Sopenharmony_ci	sigemptyset(&act.sa_mask);
11162306a36Sopenharmony_ci	act.sa_flags = SA_SIGINFO;
11262306a36Sopenharmony_ci	if (sigaction(SIGUSR1, &act, NULL) < 0) {
11362306a36Sopenharmony_ci		perror("sigaction sigusr1");
11462306a36Sopenharmony_ci		exit(1);
11562306a36Sopenharmony_ci	}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	i = 0;
11862306a36Sopenharmony_ci	while (i < MAX_ATTEMPT && !broken) {
11962306a36Sopenharmony_ci		/*
12062306a36Sopenharmony_ci		 * tm_signal_self_context_load will set both first and second
12162306a36Sopenharmony_ci		 * contexts accordingly to the values passed through non-NULL
12262306a36Sopenharmony_ci		 * array pointers to it, in that case 'vms', and invoke the
12362306a36Sopenharmony_ci		 * signal handler installed for SIGUSR1.
12462306a36Sopenharmony_ci		 */
12562306a36Sopenharmony_ci		rc = tm_signal_self_context_load(pid, NULL, NULL, vms, NULL);
12662306a36Sopenharmony_ci		FAIL_IF(rc != pid);
12762306a36Sopenharmony_ci		i++;
12862306a36Sopenharmony_ci	}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	return (broken);
13162306a36Sopenharmony_ci}
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ciint main(void)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	return test_harness(tm_signal_context_chk, "tm_signal_context_chk_vmx");
13662306a36Sopenharmony_ci}
137