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 <signal.h> 2362306a36Sopenharmony_ci#include <unistd.h> 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#include <altivec.h> 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci#include "utils.h" 2862306a36Sopenharmony_ci#include "tm.h" 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci#define MAX_ATTEMPT 500000 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci#define NV_GPR_REGS 18 /* Number of non-volatile GPR registers */ 3362306a36Sopenharmony_ci#define R14 14 /* First non-volatile register to check in r14-r31 subset */ 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_cilong tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss); 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_cistatic sig_atomic_t fail, broken; 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci/* Test only non-volatile general purpose registers, i.e. r14-r31 */ 4062306a36Sopenharmony_cistatic long gprs[] = { 4162306a36Sopenharmony_ci /* First context will be set with these values, i.e. non-speculative */ 4262306a36Sopenharmony_ci /* R14, R15, ... */ 4362306a36Sopenharmony_ci 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 4462306a36Sopenharmony_ci /* Second context will be set with these values, i.e. speculative */ 4562306a36Sopenharmony_ci /* R14, R15, ... */ 4662306a36Sopenharmony_ci -1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18 4762306a36Sopenharmony_ci}; 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_cistatic void signal_usr1(int signum, siginfo_t *info, void *uc) 5062306a36Sopenharmony_ci{ 5162306a36Sopenharmony_ci int i; 5262306a36Sopenharmony_ci ucontext_t *ucp = uc; 5362306a36Sopenharmony_ci ucontext_t *tm_ucp = ucp->uc_link; 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci /* Check first context. Print all mismatches. */ 5662306a36Sopenharmony_ci for (i = 0; i < NV_GPR_REGS; i++) { 5762306a36Sopenharmony_ci fail = (ucp->uc_mcontext.gp_regs[R14 + i] != gprs[i]); 5862306a36Sopenharmony_ci if (fail) { 5962306a36Sopenharmony_ci broken = 1; 6062306a36Sopenharmony_ci printf("GPR%d (1st context) == %lu instead of %lu (expected)\n", 6162306a36Sopenharmony_ci R14 + i, ucp->uc_mcontext.gp_regs[R14 + i], gprs[i]); 6262306a36Sopenharmony_ci } 6362306a36Sopenharmony_ci } 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci /* Check second context. Print all mismatches. */ 6662306a36Sopenharmony_ci for (i = 0; i < NV_GPR_REGS; i++) { 6762306a36Sopenharmony_ci fail = (tm_ucp->uc_mcontext.gp_regs[R14 + i] != gprs[NV_GPR_REGS + i]); 6862306a36Sopenharmony_ci if (fail) { 6962306a36Sopenharmony_ci broken = 1; 7062306a36Sopenharmony_ci printf("GPR%d (2nd context) == %lu instead of %lu (expected)\n", 7162306a36Sopenharmony_ci R14 + i, tm_ucp->uc_mcontext.gp_regs[R14 + i], gprs[NV_GPR_REGS + i]); 7262306a36Sopenharmony_ci } 7362306a36Sopenharmony_ci } 7462306a36Sopenharmony_ci} 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_cistatic int tm_signal_context_chk_gpr() 7762306a36Sopenharmony_ci{ 7862306a36Sopenharmony_ci struct sigaction act; 7962306a36Sopenharmony_ci int i; 8062306a36Sopenharmony_ci long rc; 8162306a36Sopenharmony_ci pid_t pid = getpid(); 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci SKIP_IF(!have_htm()); 8462306a36Sopenharmony_ci SKIP_IF(htm_is_synthetic()); 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci act.sa_sigaction = signal_usr1; 8762306a36Sopenharmony_ci sigemptyset(&act.sa_mask); 8862306a36Sopenharmony_ci act.sa_flags = SA_SIGINFO; 8962306a36Sopenharmony_ci if (sigaction(SIGUSR1, &act, NULL) < 0) { 9062306a36Sopenharmony_ci perror("sigaction sigusr1"); 9162306a36Sopenharmony_ci exit(1); 9262306a36Sopenharmony_ci } 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci i = 0; 9562306a36Sopenharmony_ci while (i < MAX_ATTEMPT && !broken) { 9662306a36Sopenharmony_ci /* 9762306a36Sopenharmony_ci * tm_signal_self_context_load will set both first and second 9862306a36Sopenharmony_ci * contexts accordingly to the values passed through non-NULL 9962306a36Sopenharmony_ci * array pointers to it, in that case 'gprs', and invoke the 10062306a36Sopenharmony_ci * signal handler installed for SIGUSR1. 10162306a36Sopenharmony_ci */ 10262306a36Sopenharmony_ci rc = tm_signal_self_context_load(pid, gprs, NULL, NULL, NULL); 10362306a36Sopenharmony_ci FAIL_IF(rc != pid); 10462306a36Sopenharmony_ci i++; 10562306a36Sopenharmony_ci } 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci return broken; 10862306a36Sopenharmony_ci} 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ciint main(void) 11162306a36Sopenharmony_ci{ 11262306a36Sopenharmony_ci return test_harness(tm_signal_context_chk_gpr, "tm_signal_context_chk_gpr"); 11362306a36Sopenharmony_ci} 114