summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/x86/sigframe_fpu_portability.c
blob: 8377de05203266441c091b97165388fb403e7b06 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// SPDX-License-Identifier: GPL-2.0-only
#define _GNU_SOURCE
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/ucontext.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <cpuid.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <stddef.h>
#include <setjmp.h>

#include "helpers.h"
#include "xstate.h"

#ifndef FP_XSTATE_MAGIC2_SIZE
#define FP_XSTATE_MAGIC2_SIZE	sizeof(FP_XSTATE_MAGIC2)
#endif

/*
 * This test verifies the FPU portability and consistency of the signal frame.
 *
 * - test_valid_shrunk_xstate_size:
 *   Verifies that the kernel restores state from a frame with xstate_size
 *   shrunk to only include active features.
 *
 * - test_invalid_shrunk_xstate_size:
 *   Verifies that the kernel rejects a frame if xstate_size is too small for
 *   the features enabled in xfeatures.
 */

#define SIGFRAME_XSTATE_HDR_OFFSET	512
#define XSTATE_SSE_ONLY_SIZE	(SIGFRAME_XSTATE_HDR_OFFSET + XSAVE_HDR_SIZE)
#define XFEATURE_MASK_FPSSE	((1 << XFEATURE_FP) | (1 << XFEATURE_SSE))

static uint32_t ymm_offset;
static uint32_t xstate_size_ymm;
static pid_t self_pid;

/*
 * Load %ymm0 from @v, invoke SYS_kill to deliver @sig, and store the
 * restored %ymm0 state back into @v within a single inline assembly
 * block so the compiler cannot clobber %xmm0/%ymm0 between steps.
 */
__attribute__((target("avx")))
static void raise_with_ymm0(int sig, uint64_t *v)
{
	register long rax asm("rax") = SYS_kill;
	register long rdi asm("rdi") = self_pid;
	register long rsi asm("rsi") = sig;

	asm volatile ("vmovdqu %0, %%ymm0\n\t"
		      "syscall\n\t"
		      "vmovdqu %%ymm0, %0"
		      : "+m" (*(char (*)[32])v), "+r" (rax)
		      : "r" (rdi), "r" (rsi)
		      : "rcx", "r11", "ymm0", "memory");
}

/*
 * Avoid using printf() in signal handlers as it is not
 * async-signal-safe.
 */
#define SIGNAL_BUF_LEN 1024
static char sig_err_buf[SIGNAL_BUF_LEN];

static void sig_print(const char *msg)
{
	int left = SIGNAL_BUF_LEN - strlen(sig_err_buf) - 1;

	strncat(sig_err_buf, msg, left);
}

static void check_avx_support(void)
{
	uint32_t eax, ebx, ecx, edx;
	struct xstate_info xstate;

	/* Check CPUID.01H:ECX.OSXSAVE[bit 27] before calling xgetbv to avoid #UD */
	__cpuid(1, eax, ebx, ecx, edx);
	if (!(ecx & (1 << 27)))
		ksft_exit_skip("OSXSAVE not enabled by OS\n");

	/* Check XCR0[2] (YMM) is enabled by OS */
	if (!(xgetbv(0) & (1 << XFEATURE_YMM)))
		ksft_exit_skip("AVX (YMM) not enabled in XCR0\n");

	xstate = get_xstate_info(XFEATURE_YMM);
	if (!xstate.size)
		ksft_exit_skip("AVX not supported by hardware\n");

	ymm_offset = xstate.xbuf_offset;
	xstate_size_ymm = xstate.xbuf_offset + xstate.size;
}

#define TEST_YMMH_VAL (0x5656565656565656UL)

static void __handle_shrunk_xstate_size(int sig, siginfo_t *si, void *ucp, bool valid_size)
{
	uint64_t xfeatures, *ymmh_p;
	struct xsave_buffer *xbuf;
	struct _fpx_sw_bytes *sw;
	ucontext_t *uc = ucp;
	void *fp;

	fp = uc->uc_mcontext.fpregs;
	if (!fp) {
		sig_print("fpregs is NULL\n");
		return;
	}

	sw = get_fpx_sw_bytes(fp);
	if (sw->magic1 != FP_XSTATE_MAGIC1) {
		sig_print("magic1 is not valid\n");
		return;
	}

	xbuf = (struct xsave_buffer *)fp;

	/*
	 * Both test cases shrink the frame to contain only AVX (FP + SSE + YMM).
	 * If valid_size is true, set xstate_size to match the enabled features.
	 * If valid_size is false, set xstate_size too small (SSE only), which
	 * the kernel must reject.
	 */
	if (valid_size)
		sw->xstate_size = xstate_size_ymm;
	else
		sw->xstate_size = XSTATE_SSE_ONLY_SIZE;

	xfeatures = get_xstatebv(xbuf);
	xfeatures &= XFEATURE_MASK_FPSSE | (1 << XFEATURE_YMM);
	set_xstatebv(xbuf, xfeatures);
	set_fpx_sw_bytes_features(fp, xfeatures);

	*(uint32_t *)(fp + sw->xstate_size) = FP_XSTATE_MAGIC2;

	if (valid_size) {
		ymmh_p = (uint64_t *)(fp + ymm_offset);
		ymmh_p[0] = TEST_YMMH_VAL;
		ymmh_p[1] = TEST_YMMH_VAL + 1;
	}

	/* clear everything after MAGIC2. */
	if (sw->xstate_size + FP_XSTATE_MAGIC2_SIZE < sw->extended_size)
		memset(fp + sw->xstate_size + FP_XSTATE_MAGIC2_SIZE, 0,
		       sw->extended_size - sw->xstate_size - FP_XSTATE_MAGIC2_SIZE);
}

static void handle_valid_shrunk_xstate_size(int sig, siginfo_t *si, void *ucp)
{
	__handle_shrunk_xstate_size(sig, si, ucp, true);
}

static void handle_invalid_shrunk_xstate_size(int sig, siginfo_t *si, void *ucp)
{
	__handle_shrunk_xstate_size(sig, si, ucp, false);
}

static void test_valid_shrunk_xstate_size(void)
{
	uint64_t v[4];

	sig_err_buf[0] = 0;
	sethandler(SIGUSR1, handle_valid_shrunk_xstate_size, 0);

	v[0] = 0x1111111111111111ULL;
	v[1] = 0x2222222222222222ULL;
	v[2] = 0x3333333333333333ULL;
	v[3] = 0x4444444444444444ULL;
	raise_with_ymm0(SIGUSR1, v);

	if (sig_err_buf[0])
		ksft_test_result_fail("%s\n", sig_err_buf);
	else if (v[2] == TEST_YMMH_VAL && v[3] == (TEST_YMMH_VAL + 1))
		ksft_test_result_pass("YMM state restored correctly from shrunk frame\n");
	else
		ksft_test_result_fail(
				"Got upper bits: 0x%lx 0x%lx (expected %lx %lx)\n",
			       v[2], v[3], TEST_YMMH_VAL, TEST_YMMH_VAL + 1);

	clearhandler(SIGUSR1);
}

static sigjmp_buf segv_jmpbuf;

static void handle_segv(int sig, siginfo_t *si, void *ucp)
{
	siglongjmp(segv_jmpbuf, 1);
}

static void test_invalid_shrunk_xstate_size(void)
{
	uint64_t v[4];

	sig_err_buf[0] = 0;
	sethandler(SIGUSR1, handle_invalid_shrunk_xstate_size, 0);
	sethandler(SIGSEGV, handle_segv, 0);

	if (sigsetjmp(segv_jmpbuf, 1) == 0) {
		v[0] = 0x1111111111111111ULL;
		v[1] = 0x2222222222222222ULL;
		v[2] = 0x3333333333333333ULL;
		v[3] = 0x4444444444444444ULL;
		raise_with_ymm0(SIGUSR1, v);
		sig_print("Inconsistent size was NOT rejected\n");
	}

	clearhandler(SIGUSR1);
	clearhandler(SIGSEGV);

	if (sig_err_buf[0])
		ksft_test_result_fail("%s\n", sig_err_buf);
	else
		ksft_test_result_pass("Inconsistent size correctly rejected\n");
}

int main(void)
{
	ksft_print_header();
	ksft_set_plan(2);

	self_pid = getpid();

	check_avx_support();

	test_valid_shrunk_xstate_size();
	test_invalid_shrunk_xstate_size();

	ksft_finished();
	return 0;
}