1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright 2016, Cyril Bur, IBM Corp.
4 *
5 * Test the kernel's signal frame code.
6 *
7 * The kernel sets up two sets of ucontexts if the signal was to be
8 * delivered while the thread was in a transaction (referred too as
9 * first and second contexts).
10 * Expected behaviour is that the checkpointed state is in the user
11 * context passed to the signal handler (first context). The speculated
12 * state can be accessed with the uc_link pointer (second context).
13 *
14 * The rationale for this is that if TM unaware code (which linked
15 * against TM libs) installs a signal handler it will not know of the
16 * speculative nature of the 'live' registers and may infer the wrong
17 * thing.
18 */
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <signal.h>
23#include <unistd.h>
24
25#include <altivec.h>
26
27#include "utils.h"
28#include "tm.h"
29
30#define MAX_ATTEMPT 500000
31
32#define NV_GPR_REGS 18 /* Number of non-volatile GPR registers */
33#define R14 14 /* First non-volatile register to check in r14-r31 subset */
34
35long tm_signal_self_context_load(pid_t pid, long *gprs, double *fps, vector int *vms, vector int *vss);
36
37static sig_atomic_t fail, broken;
38
39/* Test only non-volatile general purpose registers, i.e. r14-r31 */
40static long gprs[] = {
41 /* First context will be set with these values, i.e. non-speculative */
42 /* R14, R15, ... */
43 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
44 /* Second context will be set with these values, i.e. speculative */
45 /* R14, R15, ... */
46 -1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18
47};
48
49static void signal_usr1(int signum, siginfo_t *info, void *uc)
50{
51 int i;
52 ucontext_t *ucp = uc;
53 ucontext_t *tm_ucp = ucp->uc_link;
54
55 /* Check first context. Print all mismatches. */
56 for (i = 0; i < NV_GPR_REGS; i++) {
57 fail = (ucp->uc_mcontext.gp_regs[R14 + i] != gprs[i]);
58 if (fail) {
59 broken = 1;
60 printf("GPR%d (1st context) == %lu instead of %lu (expected)\n",
61 R14 + i, ucp->uc_mcontext.gp_regs[R14 + i], gprs[i]);
62 }
63 }
64
65 /* Check second context. Print all mismatches. */
66 for (i = 0; i < NV_GPR_REGS; i++) {
67 fail = (tm_ucp->uc_mcontext.gp_regs[R14 + i] != gprs[NV_GPR_REGS + i]);
68 if (fail) {
69 broken = 1;
70 printf("GPR%d (2nd context) == %lu instead of %lu (expected)\n",
71 R14 + i, tm_ucp->uc_mcontext.gp_regs[R14 + i], gprs[NV_GPR_REGS + i]);
72 }
73 }
74}
75
76static int tm_signal_context_chk_gpr()
77{
78 struct sigaction act;
79 int i;
80 long rc;
81 pid_t pid = getpid();
82
83 SKIP_IF(!have_htm());
84 SKIP_IF(htm_is_synthetic());
85
86 act.sa_sigaction = signal_usr1;
87 sigemptyset(&act.sa_mask);
88 act.sa_flags = SA_SIGINFO;
89 if (sigaction(SIGUSR1, &act, NULL) < 0) {
90 perror("sigaction sigusr1");
91 exit(1);
92 }
93
94 i = 0;
95 while (i < MAX_ATTEMPT && !broken) {
96 /*
97 * tm_signal_self_context_load will set both first and second
98 * contexts accordingly to the values passed through non-NULL
99 * array pointers to it, in that case 'gprs', and invoke the
100 * signal handler installed for SIGUSR1.
101 */
102 rc = tm_signal_self_context_load(pid, gprs, NULL, NULL, NULL);
103 FAIL_IF(rc != pid);
104 i++;
105 }
106
107 return broken;
108}
109
110int main(void)
111{
112 return test_harness(tm_signal_context_chk_gpr, "tm_signal_context_chk_gpr");
113}
114

source code of linux/tools/testing/selftests/powerpc/tm/tm-signal-context-chk-gpr.c