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