aboutsummaryrefslogtreecommitdiff
path: root/tools/test/xregs_sig/xregs_sig.c
blob: a67379a0080e092a1675cd3257d8100840573040 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* $Id: avx_sig.c,v 1.12 2021/12/11 22:47:09 kostik Exp $ */
/*
 * Naive test to check that context switches and signal delivery do
 * not corrupt AVX registers file (%xmm).  Run until some
 * inconsistency detected, then aborts.
 *
 * FreeBSD:
 * ${CC} -Wall -Wextra -O -g -o avx_sig avx_sig.c -lpthread
 * Linux
 * ${CC} -D_GNU_SOURCE -Wall -Wextra -O -g -o avx_sig avx_sig.c -lbsd -lpthread
 */

#include <sys/param.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <errno.h>
#include <pthread.h>
#ifdef __FreeBSD__
#include <pthread_np.h>
#endif
#ifdef __linux__
#ifdef __GLIBC__
#include <gnu/libc-version.h>
#endif
#if !defined(__GLIBC__) || (__GLIBC__ * 100 + __GLIBC_MINOR__) < 236
#include <bsd/stdlib.h>
#endif
#endif
#include <signal.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* SIGALRM interval in seconds. */
#ifndef TIMO
#define	TIMO		5
#endif

#ifndef __unused
#define	__unused	__attribute__((__unused__))
#endif
#ifndef nitems
#define	nitems(x)	(sizeof((x)) / sizeof((x)[0]))
#endif

#if defined(__amd64__)
#define	XREGSRNAM	"xmm"
#define	NREGS		16
#elif defined(__aarch64__)
#define	XREGSRNAM	"q"
#define	NREGS		32
#endif

struct xregsreg {
	uint8_t xregs_bytes[16];
};

struct xregs {
	struct xregsreg xregsreg[NREGS];
};

void cpu_to_xregs(struct xregs *xregs);
void xregs_to_cpu(struct xregs *xregs);

static atomic_uint sigs;

static void
sigusr1_handler(int sig __unused, siginfo_t *si __unused, void *m __unused)
{
	atomic_fetch_add_explicit(&sigs, 1, memory_order_relaxed);
}

static void
sigalrm_handler(int sig __unused)
{
	struct rusage r;

	if (getrusage(RUSAGE_SELF, &r) == 0) {
		printf("%lu vctx %lu nvctx %lu nsigs %u SIGUSR1\n",
		    r.ru_nvcsw, r.ru_nivcsw, r.ru_nsignals, sigs);
	}
	alarm(TIMO);
}

static struct xregs zero_xregs = {};

static void
fill_xregs(struct xregs *xregs)
{
	arc4random_buf(xregs, sizeof(*xregs));
}

static void
dump_xregs(const struct xregsreg *r)
{
	unsigned k;

	for (k = 0; k < nitems(r->xregs_bytes); k++) {
		if (k != 0)
			printf(" ");
		printf("%02x", r->xregs_bytes[k]);
	}
	printf("\n");
}

static pthread_mutex_t show_lock;

static void
show_diff(const struct xregs *xregs1, const struct xregs *xregs2)
{
	const struct xregsreg *r1, *r2;
	unsigned i, j;

#if defined(__FreeBSD__)
	printf("thr %d\n", pthread_getthreadid_np());
#elif defined(__linux__)
	printf("thr %ld\n", syscall(SYS_gettid));
#endif
	for (i = 0; i < nitems(xregs1->xregsreg); i++) {
		r1 = &xregs1->xregsreg[i];
		r2 = &xregs2->xregsreg[i];
		for (j = 0; j < nitems(r1->xregs_bytes); j++) {
			if (r1->xregs_bytes[j] != r2->xregs_bytes[j]) {
				printf("%%%s%u\n", XREGSRNAM, i);
				dump_xregs(r1);
				dump_xregs(r2);
				break;
			}
		}
	}
}

static void
my_pause(void)
{
	usleep(0);
}

static void *
worker_thread(void *arg __unused)
{
	struct xregs xregs, xregs_cpu;

	fill_xregs(&xregs);
	for (;;) {
		xregs_to_cpu(&xregs);
		my_pause();
		cpu_to_xregs(&xregs_cpu);
		if (memcmp(&xregs, &xregs_cpu, sizeof(struct xregs)) != 0) {
			pthread_mutex_lock(&show_lock);
			show_diff(&xregs, &xregs_cpu);
			abort();
			pthread_mutex_unlock(&show_lock);
		}

		xregs_to_cpu(&zero_xregs);
		my_pause();
		cpu_to_xregs(&xregs_cpu);
		if (memcmp(&zero_xregs, &xregs_cpu, sizeof(struct xregs)) != 0) {
			pthread_mutex_lock(&show_lock);
			show_diff(&zero_xregs, &xregs_cpu);
			abort();
			pthread_mutex_unlock(&show_lock);
		}
	}
	return (NULL);
}

int
main(void)
{
	struct sigaction sa;
	int error, i, ncpu;

	bzero(&sa, sizeof(sa));
	sa.sa_handler = sigalrm_handler;
	if (sigaction(SIGALRM, &sa, NULL) == -1) {
		fprintf(stderr, "sigaction SIGALRM %s\n", strerror(errno));
		exit(1);
	}

	bzero(&sa, sizeof(sa));
	sa.sa_sigaction = sigusr1_handler;
	sa.sa_flags = SA_SIGINFO;
	if (sigaction(SIGUSR1, &sa, NULL) == -1) {
		fprintf(stderr, "sigaction SIGUSR1 %s\n", strerror(errno));
		exit(1);
	}

	error = pthread_mutex_init(&show_lock, NULL);
	if (error != 0) {
		fprintf(stderr, "pthread_mutex_init %s\n", strerror(error));
		exit(1);
	}

	ncpu = sysconf(_SC_NPROCESSORS_ONLN);
	ncpu *= 2;
	pthread_t wt[ncpu];
	for (i = 0; i < ncpu; i++) {
		error = pthread_create(&wt[i], NULL, worker_thread, NULL);
		if (error != 0) {
			fprintf(stderr, "pthread_create %s\n", strerror(error));
		}
	}

	alarm(TIMO);
	for (;;) {
		for (i = 0; i < ncpu; i++) {
			my_pause();
			pthread_kill(wt[i], SIGUSR1);
		}
	}
}