Skip to contentSkip to conversation

x86_64: test xmm/ymm register state after execve(2)

PATCH In review Alexey Dobriyan 2 participants 4 messages Dec 5, 2023 – Dec 11, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86_64: test xmm/ymm register state after execve(2)

Test that xmm/ymm registers are cleared immediately after execve(2).

It is opportunistically named "check_xmm_ymm_zmm" because I don't have AVX-512 machine but it will be trivial to extend without renaming stuff.

---

	I want to draw attention to executable sizes:

	-rwxr-xr-x.   1 ad ad 723920 Dec  5 12:28 check_initial_reg_state_32
	-rwxr-xr-x.   1 ad ad 825904 Dec  5 12:28 check_initial_reg_state_64
	-rw-r--r--.   1 ad ad   1901 Dec  5 12:18 check_initial_reg_state.c
	-rwxr-xr-x.   1 ad ad 827976 Dec  5 17:09 check_xmm_ymm_zmm_64
	-rw-r--r--.   1 ad ad   4705 Dec  5 17:09 check_xmm_ymm_zmm.c

	which are absolutely ridiculous!

	These tests should fit into 1 page, both!
 tools/testing/selftests/x86/Makefile            |    2 
 tools/testing/selftests/x86/check_xmm_ymm_zmm.c |  176 ++++++++++++++++++++++++
 2 files changed, 178 insertions(+)
tools/testing/selftests/x86/Makefile +2 -0
@@ -20,6 +20,7 @@ TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \
 			vdso_restorer
 TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
 			corrupt_xstate_header amx lam test_shadow_stack
+TARGETS_C_64BIT_ONLY += check_xmm_ymm_zmm
 # Some selftests require 32bit support enabled also on 64bit systems
 TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
 
@@ -110,6 +111,7 @@ $(OUTPUT)/test_syscall_vdso_32: thunks_32.S
 # state.
 $(OUTPUT)/check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static
 $(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static
+$(OUTPUT)/check_xmm_ymm_zmm_64: CFLAGS += -Wl,-ee_entry -static
 
 $(OUTPUT)/nx_stack_32: CFLAGS += -Wl,-z,noexecstack
 $(OUTPUT)/nx_stack_64: CFLAGS += -Wl,-z,noexecstack

new file mode 100644

tools/testing/selftests/x86/check_xmm_ymm_zmm.c new file +176 -0

176 lines not on this page · raw message

#1 lore raw
Quoted 4 lines · Alexey Dobriyan

Test that xmm/ymm registers are cleared immediately after execve(2).

It is opportunistically named "check_xmm_ymm_zmm" because I don't have AVX-512 machine but it will be trivial to extend without renaming stuff.

Hi Alexey,

This looks pretty useful. I know we've had bugs in this area in the past. Was there any recent motivation for this, though? Just curious.

Quoted 9 lines
tools/testing/selftests/x86/check_xmm_ymm_zmm.c new file +6 -0
@@ -0,0 +1,176 @@
+/*
+ * Copyright (c) 2023 Alexey Dobriyan <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.

tools/testing/selftests/x86/check_xmm_ymm_zmm.c line 16

Quoted 10 lines
tools/testing/selftests/x86/check_xmm_ymm_zmm.c new file +10 -0
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+/* Test that xmm, ymm registers are cleared immediately after execve(2). */

Can this be trimmed down to a nice SPDX header?

Quoted 47 lines
tools/testing/selftests/x86/check_xmm_ymm_zmm.c new file +47 -0
+#include <stdio.h>
+#include <string.h>
+
+#if   defined __amd64__
+#elif defined __i386__
+#error "fix register count, SSE2 detection"
+#else
+#error
+#endif
+
+#define array_size(a)	(sizeof(a) / sizeof(a[0]))
+
+typedef char xmm_t[16];
+static const xmm_t xmm_z;
+static xmm_t xmm[16];
+
+typedef char ymm_t[32];
+static const ymm_t ymm_z;
+static ymm_t ymm[16];
+
+enum {
+	TEST_XMM = 1,
+	TEST_YMM = 2,
+};
+static volatile char g_test;
+
+/*
+ * Homework: write and install #UD handler in assembly and just start
+ * executing AVX-512/AVX2/SSE2 instructions falling back SIMD ladder
+ * if necessary.
+ *
+ * jk, use cpuid instead like any normal programmer would do.
+ */
+asm (
+".pushsection .text;"
+".type e_entry,@function;"
+".global e_entry;"
+"e_entry:"
+	/* test AVX2 support */
+	"mov $7, %eax;"
+	"xor %ecx, %ecx;"
+	"cpuid;"
+	"bt $5, %ebx;"
+	"jnc .Ltest_xmm;"
+
+	"vmovdqu %ymm0, ymm + 32 * 0;"
+	"vmovdqu %ymm1, ymm + 32 * 1;

...

tools/testing/selftests/x86/check_xmm_ymm_zmm.c line 84

Quoted 4 lines

+".Ltest_xmm:"

tools/testing/selftests/x86/check_xmm_ymm_zmm.c new file +2 -0
+	"movdqu %xmm0, xmm + 16 * 0;"
+	"movdqu %xmm1, xmm + 16 * 1;"

Does this work on systems without XMMs? I know it's not common these days but it's possible, especially in VMs.

#2 lore raw
Quoted 10 lines · Dave Hansen
Quoted 4 lines · Alexey Dobriyan

Test that xmm/ymm registers are cleared immediately after execve(2).

It is opportunistically named "check_xmm_ymm_zmm" because I don't have AVX-512 machine but it will be trivial to extend without renaming stuff.

Hi Alexey,

This looks pretty useful. I know we've had bugs in this area in the past. Was there any recent motivation for this, though? Just curious.

I found check_initial_reg_state.c with cool assembly but only general registers being checked.

Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/x86/check_xmm_ymm_zmm.c new file +10 -0
@@ -0,0 +1,176 @@
+/*
+ * Copyright (c) 2023 Alexey Dobriyan <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
Quoted 8 lines

tools/testing/selftests/x86/check_xmm_ymm_zmm.c line 16

Quoted 6 lines
tools/testing/selftests/x86/check_xmm_ymm_zmm.c new file +6 -0
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+/* Test that xmm, ymm registers are cleared immediately after execve(2). */

Can this be trimmed down to a nice SPDX header?

I'd rather not.

This is standalone test program, not part of kernel proper, so if it's copied somewhere than copyrights are retained. Another project might not even use SPDX scheme.

Quoted 10 lines
Quoted 9 lines
tools/testing/selftests/x86/check_xmm_ymm_zmm.c new file +9 -0
+#include <stdio.h>
+#include <string.h>
+
+#if   defined __amd64__
+#elif defined __i386__
+#error "fix register count, SSE2 detection"
+#else
+#error
+#endif
Quoted 6 lines

tools/testing/selftests/x86/check_xmm_ymm_zmm.c line 84

Quoted 3 lines
tools/testing/selftests/x86/check_xmm_ymm_zmm.c new file +3 -0
+".Ltest_xmm:"
+	"movdqu %xmm0, xmm + 16 * 0;"
+	"movdqu %xmm1, xmm + 16 * 1;"

Does this work on systems without XMMs? I know it's not common these days but it's possible, especially in VMs.

No. But I think all x86_64 systems have SSE2. So it is up to whoever will port this test to i386.

#3 lore raw
Quoted 7 lines · Alexey Dobriyan
Quoted 5 lines

tools/testing/selftests/x86/check_xmm_ymm_zmm.c line 84

Quoted 3 lines

+".Ltest_xmm:"

tools/testing/selftests/x86/check_xmm_ymm_zmm.c new file +2 -0
+	"movdqu %xmm0, xmm + 16 * 0;"
+	"movdqu %xmm1, xmm + 16 * 1;"

Does this work on systems without XMMs? I know it's not common these days but it's possible, especially in VMs.

No. But I think all x86_64 systems have SSE2. So it is up to whoever will port this test to i386.

I somehow got it in my head that we can't do XMM's unless XSAVE is in the picture. But that's wrong. FXSAVE works on XMM's just fine and it's a requirement for FPU support, and 64-bit requires the FPU.

It would be nice to spell that out explicitly in the changelog if you get a chance to resend this.

Also, on the license... You as the copyright holder are, of course, free to distribute it under any license you want yourself. But for the copy in the kernel, it would be nice if you chose something you could stick an SPDX label on. That way, a maintainer knows immediately if the license is OK or not.