xref: /openbmc/linux/tools/testing/selftests/powerpc/signal/signal_tm.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ef186331SCyril Bur /*
3ef186331SCyril Bur  * Copyright 2016, Cyril Bur, IBM Corp.
4ef186331SCyril Bur  *
5ef186331SCyril Bur  * Sending one self a signal should always get delivered.
6ef186331SCyril Bur  */
7ef186331SCyril Bur 
8ef186331SCyril Bur #include <errno.h>
9ef186331SCyril Bur #include <stdlib.h>
10ef186331SCyril Bur #include <stdio.h>
11ef186331SCyril Bur #include <string.h>
12ef186331SCyril Bur #include <signal.h>
13ef186331SCyril Bur #include <unistd.h>
14ef186331SCyril Bur 
15ef186331SCyril Bur #include <altivec.h>
16ef186331SCyril Bur 
17ef186331SCyril Bur #include "utils.h"
18ef186331SCyril Bur #include "../tm/tm.h"
19ef186331SCyril Bur 
20ef186331SCyril Bur #define MAX_ATTEMPT 500000
21ef186331SCyril Bur #define TIMEOUT 10
22ef186331SCyril Bur 
23ef186331SCyril Bur extern long tm_signal_self(pid_t pid, int sig, long *ret);
24ef186331SCyril Bur 
25ef186331SCyril Bur static sig_atomic_t signaled;
26ef186331SCyril Bur static sig_atomic_t fail;
27ef186331SCyril Bur 
signal_handler(int sig)28ef186331SCyril Bur static void signal_handler(int sig)
29ef186331SCyril Bur {
30ef186331SCyril Bur 	if (tcheck_active()) {
31ef186331SCyril Bur 		fail = 2;
32ef186331SCyril Bur 		return;
33ef186331SCyril Bur 	}
34ef186331SCyril Bur 
35ef186331SCyril Bur 	if (sig == SIGUSR1)
36ef186331SCyril Bur 		signaled = 1;
37ef186331SCyril Bur 	else
38ef186331SCyril Bur 		fail = 1;
39ef186331SCyril Bur }
40ef186331SCyril Bur 
test_signal_tm()41ef186331SCyril Bur static int test_signal_tm()
42ef186331SCyril Bur {
43ef186331SCyril Bur 	int i;
44ef186331SCyril Bur 	struct sigaction act;
45ef186331SCyril Bur 
46ef186331SCyril Bur 	act.sa_handler = signal_handler;
47ef186331SCyril Bur 	act.sa_flags = 0;
48ef186331SCyril Bur 	sigemptyset(&act.sa_mask);
49ef186331SCyril Bur 	if (sigaction(SIGUSR1, &act, NULL) < 0) {
50ef186331SCyril Bur 		perror("sigaction SIGUSR1");
51ef186331SCyril Bur 		exit(1);
52ef186331SCyril Bur 	}
53ef186331SCyril Bur 	if (sigaction(SIGALRM, &act, NULL) < 0) {
54ef186331SCyril Bur 		perror("sigaction SIGALRM");
55ef186331SCyril Bur 		exit(1);
56ef186331SCyril Bur 	}
57ef186331SCyril Bur 
58ef186331SCyril Bur 	SKIP_IF(!have_htm());
59*e42edf9bSJordan Niethe 	SKIP_IF(htm_is_synthetic());
60ef186331SCyril Bur 
61ef186331SCyril Bur 	for (i = 0; i < MAX_ATTEMPT; i++) {
62ef186331SCyril Bur 		/*
63ef186331SCyril Bur 		 * If anything bad happens in ASM and we fail to set ret
64ef186331SCyril Bur 		 * because *handwave* TM this will cause failure
65ef186331SCyril Bur 		 */
66ef186331SCyril Bur 		long ret = 0xdead;
67ef186331SCyril Bur 		long rc = 0xbeef;
68ef186331SCyril Bur 
69ef186331SCyril Bur 		alarm(0); /* Disable any pending */
70ef186331SCyril Bur 		signaled = 0;
71ef186331SCyril Bur 		alarm(TIMEOUT);
72ef186331SCyril Bur 		FAIL_IF(tcheck_transactional());
73ef186331SCyril Bur 		rc = tm_signal_self(getpid(), SIGUSR1, &ret);
74ef186331SCyril Bur 		if (ret == 0xdead)
75ef186331SCyril Bur 			/*
76ef186331SCyril Bur 			 * This basically means the transaction aborted before we
77ef186331SCyril Bur 			 * even got to the suspend... this is crazy but it
78ef186331SCyril Bur 			 * happens.
79ef186331SCyril Bur 			 * Yes this also means we might never make forward
80ef186331SCyril Bur 			 * progress... the alarm() will trip eventually...
81ef186331SCyril Bur 			 */
82ef186331SCyril Bur 			continue;
83ef186331SCyril Bur 
84ef186331SCyril Bur 		if (rc || ret) {
85ef186331SCyril Bur 			/* Ret is actually an errno */
86ef186331SCyril Bur 			printf("TEXASR 0x%016lx, TFIAR 0x%016lx\n",
87ef186331SCyril Bur 					__builtin_get_texasr(), __builtin_get_tfiar());
88ef186331SCyril Bur 			fprintf(stderr, "(%d) Fail reason: %d rc=0x%lx ret=0x%lx\n",
89ef186331SCyril Bur 					i, fail, rc, ret);
90ef186331SCyril Bur 			FAIL_IF(ret);
91ef186331SCyril Bur 		}
92ef186331SCyril Bur 		while(!signaled && !fail)
93ef186331SCyril Bur 			asm volatile("": : :"memory");
94ef186331SCyril Bur 		if (!signaled) {
95ef186331SCyril Bur 			fprintf(stderr, "(%d) Fail reason: %d rc=0x%lx ret=0x%lx\n",
96ef186331SCyril Bur 					i, fail, rc, ret);
97ef186331SCyril Bur 			FAIL_IF(fail); /* For the line number */
98ef186331SCyril Bur 		}
99ef186331SCyril Bur 	}
100ef186331SCyril Bur 
101ef186331SCyril Bur 	return 0;
102ef186331SCyril Bur }
103ef186331SCyril Bur 
main(void)104ef186331SCyril Bur int main(void)
105ef186331SCyril Bur {
106ef186331SCyril Bur 	return test_harness(test_signal_tm, "signal_tm");
107ef186331SCyril Bur }
108