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