All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ammar Faizi <ammarfaizi2@gnuweeb.org>
To: "H. Peter Anvin" <hpa@zytor.com>, Xin Li <xin3.li@intel.com>
Cc: Ammar Faizi <ammarfaizi2@gnuweeb.org>,
	Dave Hansen <dave.hansen@intel.com>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Andrew Cooper <Andrew.Cooper3@citrix.com>,
	Brian Gerst <brgerst@gmail.com>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Peter Zijlstra <peterz@infradead.org>,
	Shuah Khan <shuah@kernel.org>, Ingo Molnar <mingo@kernel.org>,
	Andy Lutomirski <luto@kernel.org>,
	x86 Mailing List <x86@kernel.org>,
	"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>,
	Linux Kselftest Mailing List  <linux-kselftest@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: [RFC PATCH v4 1/2] selftests/x86: sysret_rip: Handle syscall in a FRED system
Date: Wed, 25 Jan 2023 10:22:39 +0700	[thread overview]
Message-ID: <20230125032240.728463-2-ammarfaizi2@gnuweeb.org> (raw)
In-Reply-To: <20230125032240.728463-1-ammarfaizi2@gnuweeb.org>

From: Ammar Faizi <ammarfaizi2@gnuweeb.org>

The current selftest asserts (%r11 == %rflags) after the 'syscall'
returns to user. Such an assertion doesn't apply to the FRED system
because in that system the 'syscall' instruction does not set
%r11=%rflags and %rcx=%rip.

Handle the FRED case. Now, test that:

  - "syscall" in a FRED system doesn't clobber %rcx and %r11.
  - "syscall" in a non-FRED system sets %rcx=%rip and %r11=%rflags.

The 'raise()' function from libc can't be used to control those
registers. Therefore, create a syscall wrapper in inline Assembly to
fully control them.

Fixes: 660602140103 ("selftests/x86: Add a selftest for SYSRET to noncanonical addresses")
Link: https://lore.kernel.org/lkml/25b96960-a07e-a952-5c23-786b55054126@zytor.com
Reported-by: Xin Li <xin3.li@intel.com>
Co-developed-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Signed-off-by: Ammar Faizi <ammarfaizi2@gnuweeb.org>
---
 tools/testing/selftests/x86/sysret_rip.c | 127 +++++++++++++++++++++--
 1 file changed, 120 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/x86/sysret_rip.c b/tools/testing/selftests/x86/sysret_rip.c
index 84d74be1d90207ab..86a31bbac9a85a88 100644
--- a/tools/testing/selftests/x86/sysret_rip.c
+++ b/tools/testing/selftests/x86/sysret_rip.c
@@ -39,6 +39,113 @@ asm (
 extern const char test_page[];
 static void const *current_test_page_addr = test_page;
 
+/* Arbitrary values */
+static const unsigned long r11_sentinel = 0xfeedfacedeadbeef;
+static const unsigned long rcx_sentinel = 0x5ca1ab1e0b57ac1e;
+
+/* An arbitrary *valid* RFLAGS value */
+static const unsigned long rflags_sentinel = 0x200a93;
+
+enum regs_ok {
+	REGS_UNDEFINED	= -2,	/* For consistency checker init, never returned */
+	REGS_ERROR	= -1,	/* Invalid register contents */
+	REGS_SAVED	=  0,	/* Registers properly preserved */
+	REGS_SYSRET	=  1	/* Registers match syscall/sysret */
+};
+
+/*
+ * Returns:
+ *  0 = %rcx and %r11 preserved.
+ *  1 = %rcx and %r11 set to %rflags and %rip.
+ * -1 = %rcx and/or %r11 set to any other values.
+ *
+ * @rbx should be set to the syscall return %rip.
+ */
+static enum regs_ok check_regs_result(unsigned long r11, unsigned long rcx,
+				      unsigned long rbx)
+{
+	if (r11 == r11_sentinel && rcx == rcx_sentinel)
+		return REGS_SAVED;
+
+	if (r11 == rflags_sentinel && rcx == rbx)
+		return REGS_SYSRET;
+
+	printf("[FAIL] check_regs_result\n");
+	printf("        r11_sentinel = %#lx; %%r11 = %#lx;\n", r11_sentinel, r11);
+	printf("        rcx_sentinel = %#lx; %%rcx = %#lx;\n", rcx_sentinel, rcx);
+	printf("        rflags_sentinel = %#lx\n", rflags_sentinel);
+	return REGS_ERROR;
+}
+
+static long do_syscall(long nr_syscall, unsigned long arg1, unsigned long arg2,
+		       unsigned long arg3, unsigned long arg4,
+		       unsigned long arg5, unsigned long arg6)
+{
+	static enum regs_ok regs_ok_state = REGS_UNDEFINED;
+	register unsigned long r11 asm("%r11");
+	register unsigned long r10 asm("%r10");
+	register unsigned long r8 asm("%r8");
+	register unsigned long r9 asm("%r9");
+	register void *rsp asm("%rsp");
+	unsigned long rcx, rbx;
+	enum regs_ok ret;
+
+	r11 = r11_sentinel;
+	rcx = rcx_sentinel;
+	r10 = arg4;
+	r8 = arg5;
+	r9 = arg6;
+
+	asm volatile (
+		"pushq	%[rflags_sentinel]\n\t"
+		"popf\n\t"
+		"leaq	1f(%%rip), %[rbx]\n\t"
+		"syscall\n"
+		"1:"
+
+		: "+a" (nr_syscall),
+		  "+r" (r11),
+		  "+c" (rcx),
+		  [rbx] "=b" (rbx),
+		  "+r" (rsp)	/* Clobber the redzone */
+
+		: [rflags_sentinel] "g" (rflags_sentinel),
+		  "D" (arg1),	/* %rdi */
+		  "S" (arg2),	/* %rsi */
+		  "d" (arg3),	/* %rdx */
+		  "r" (r10),
+		  "r" (r8),
+		  "r" (r9)
+
+		: "memory"
+	);
+
+	/*
+	 * Test that:
+	 *
+	 * - "syscall" in a FRED system doesn't clobber %rcx and %r11.
+	 * - "syscall" in a non-FRED system sets %rcx=%rip and %r11=%rflags.
+	 */
+	ret = check_regs_result(r11, rcx, rbx);
+	assert(ret != REGS_ERROR);
+
+	/*
+	 * Test that we don't get a mix of REGS_SAVED and REGS_SYSRET.
+	 * It needs at least calling do_syscall() twice to assert.
+	 */
+	if (regs_ok_state == REGS_UNDEFINED) {
+		/*
+		 * First time calling do_syscall().
+		 */
+		regs_ok_state = ret;
+		return ret;
+	} else {
+		assert(regs_ok_state == ret);
+	}
+
+	return nr_syscall;
+}
+
 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
 		       int flags)
 {
@@ -85,27 +192,33 @@ static void sigsegv_for_sigreturn_test(int sig, siginfo_t *info, void *ctx_void)
 static void sigusr1(int sig, siginfo_t *info, void *ctx_void)
 {
 	ucontext_t *ctx = (ucontext_t*)ctx_void;
+	enum regs_ok ret;
 
 	memcpy(&initial_regs, &ctx->uc_mcontext.gregs, sizeof(gregset_t));
 
+	ret = check_regs_result(ctx->uc_mcontext.gregs[REG_R11],
+				ctx->uc_mcontext.gregs[REG_RCX],
+				ctx->uc_mcontext.gregs[REG_RBX]);
+
+	assert(ret != REGS_ERROR);
+
 	/* Set IP and CX to match so that SYSRET can happen. */
 	ctx->uc_mcontext.gregs[REG_RIP] = rip;
 	ctx->uc_mcontext.gregs[REG_RCX] = rip;
-
-	/* R11 and EFLAGS should already match. */
-	assert(ctx->uc_mcontext.gregs[REG_EFL] ==
-	       ctx->uc_mcontext.gregs[REG_R11]);
-
 	sethandler(SIGSEGV, sigsegv_for_sigreturn_test, SA_RESETHAND);
 
-	return;
+}
+
+static void __raise(int sig)
+{
+	do_syscall(__NR_kill, getpid(), sig, 0, 0, 0, 0);
 }
 
 static void test_sigreturn_to(unsigned long ip)
 {
 	rip = ip;
 	printf("[RUN]\tsigreturn to 0x%lx\n", ip);
-	raise(SIGUSR1);
+	__raise(SIGUSR1);
 }
 
 static jmp_buf jmpbuf;
-- 
Ammar Faizi


  reply	other threads:[~2023-01-25  3:23 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <SA1PR11MB6734FA9139B9C9F6CC2ED123A8C59@SA1PR11MB6734.namprd11.prod.outlook.com>
2023-01-20 17:45 ` the x86 sysret_rip test fails on the Intel FRED architecture Dave Hansen
     [not found]   ` <eb81f7f2-d266-d999-b41a-e6eae086e731@citrix.com>
2023-01-20 20:50     ` H. Peter Anvin
2023-01-20 21:10       ` Andrew Cooper
2023-01-20 21:17         ` H. Peter Anvin
2023-01-20 21:29           ` Andrew Cooper
2023-01-21  4:59   ` H. Peter Anvin
2023-01-21 16:46     ` Dave Hansen
2023-01-21 21:47       ` Brian Gerst
2023-01-22  3:01         ` Li, Xin3
2023-01-22  3:28           ` H. Peter Anvin
2023-01-22  3:38     ` Li, Xin3
2023-01-22  4:34       ` Dave Hansen
2023-01-22  4:44         ` H. Peter Anvin
2023-01-22  8:22           ` Li, Xin3
2023-01-22  8:54             ` Ammar Faizi
2023-01-22  9:40               ` H. Peter Anvin
2023-01-22 23:45         ` H. Peter Anvin
2023-01-23  9:02           ` Ammar Faizi
2023-01-23 19:43             ` H. Peter Anvin
2023-01-23 23:43               ` Ammar Faizi
2023-01-23 23:58                 ` H. Peter Anvin
2023-01-24  0:26                   ` [RFC PATCH v1 0/2] selftests/x86: sysret_rip update for FRED system Ammar Faizi
2023-01-24  0:26                     ` [RFC PATCH v1 1/2] selftests/x86: sysret_rip: Handle syscall in a " Ammar Faizi
2023-01-24  1:40                       ` H. Peter Anvin
2023-01-24  2:31                         ` Ammar Faizi
2023-01-26 20:08                         ` Ammar Faizi
2023-02-15  9:17                           ` Andrew Cooper
2023-02-15 10:29                             ` Andrew Cooper
2023-02-15 10:44                               ` Ammar Faizi
2023-02-15 10:42                             ` Ammar Faizi
2023-01-26 20:16                         ` Ammar Faizi
2023-01-24  0:26                     ` [RFC PATCH v1 2/2] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-23 23:53             ` the x86 sysret_rip test fails on the Intel FRED architecture Andrew Cooper
2023-01-24  0:01               ` H. Peter Anvin
2023-01-24  2:27                 ` [RFC PATCH v2 0/2] selftests/x86: sysret_rip update for FRED system Ammar Faizi
2023-01-24  2:27                   ` [RFC PATCH v2 1/2] selftests/x86: sysret_rip: Handle syscall in a " Ammar Faizi
2023-01-24  5:44                     ` H. Peter Anvin
2023-01-24  2:27                   ` [RFC PATCH v2 2/2] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-24  6:16                     ` H. Peter Anvin
2023-01-24  6:41                       ` Ammar Faizi
2023-01-24  6:47                         ` Ammar Faizi
2023-01-24  9:07                         ` H. Peter Anvin
2023-01-24  9:12                           ` Ammar Faizi
2023-01-24 10:09                             ` [RFC PATCH v3 0/2] selftests/x86: sysret_rip update for FRED system Ammar Faizi
2023-01-24 10:09                               ` [RFC PATCH v3 1/2] selftests/x86: sysret_rip: Handle syscall in a " Ammar Faizi
2023-01-24 10:09                               ` [RFC PATCH v3 2/2] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-24 20:59                                 ` H. Peter Anvin
2023-01-25  3:29                                   ` Ammar Faizi
2023-01-24 21:32                               ` [RFC PATCH v3 0/2] selftests/x86: sysret_rip update for FRED system Li, Xin3
2023-01-24 21:37                                 ` H. Peter Anvin
2023-01-24 23:20                                   ` Li, Xin3
2023-01-25  3:27                                   ` Ammar Faizi
2023-01-24 21:51                                 ` Andrew Cooper
2023-01-24 23:58                                   ` Li, Xin3
2023-01-25  3:22                             ` [RFC PATCH v4 0/2] sysret_rip update for the Intel FRED architecture Ammar Faizi
2023-01-25  3:22                               ` Ammar Faizi [this message]
2023-01-25  3:37                                 ` [RFC PATCH v4 1/2] selftests/x86: sysret_rip: Handle syscall in a FRED system Ammar Faizi
2023-01-25  3:44                                   ` Ammar Faizi
2023-01-25  3:22                               ` [RFC PATCH v4 2/2] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-25  3:49                             ` [RFC PATCH v5 0/2] sysret_rip update for the Intel FRED architecture Ammar Faizi
2023-01-25  3:49                               ` [RFC PATCH v5 1/2] selftests/x86: sysret_rip: Handle syscall in a FRED system Ammar Faizi
2023-01-25  8:39                                 ` H. Peter Anvin
2023-01-25  8:53                                   ` Ammar Faizi
2023-01-25  9:57                                   ` Ammar Faizi
2023-01-25 10:01                                     ` Ammar Faizi
2023-01-25 10:17                                     ` H. Peter Anvin
2023-01-25 11:37                                       ` Ammar Faizi
2023-01-25 17:25                                         ` H. Peter Anvin
2023-01-25  3:49                               ` [RFC PATCH v5 2/2] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-25  8:22                               ` [RFC PATCH v5 0/2] sysret_rip update for the Intel FRED architecture Li, Xin3
2023-01-25  8:32                                 ` Ammar Faizi
2023-01-25 17:07                                   ` Li, Xin3
2023-01-25 17:24                                     ` H. Peter Anvin
2023-01-25 17:41                                       ` Ammar Faizi
2023-01-25 17:48                                         ` Li, Xin3
2023-02-15  7:42                                           ` Li, Xin3
2023-02-15  7:51                                             ` Ammar Faizi
2023-02-18  4:27                                             ` Ammar Faizi
2023-02-18  4:51                                               ` H. Peter Anvin
2023-01-25 21:17                             ` [RFC PATCH v6 0/3] " Ammar Faizi
2023-01-25 21:17                               ` [RFC PATCH v6 1/3] selftests/x86: sysret_rip: Handle syscall in a FRED system Ammar Faizi
2023-01-25 23:01                                 ` Ammar Faizi
2023-01-25 21:17                               ` [RFC PATCH v6 2/3] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-25 21:17                               ` [RFC PATCH v6 3/3] selftests/x86: sysret_rip: Test opportunistic SYSRET Ammar Faizi
2023-01-25 23:24                             ` [RFC PATCH v7 0/3] sysret_rip update for the Intel FRED architecture Ammar Faizi
2023-01-25 23:24                               ` [RFC PATCH v7 1/3] selftests/x86: sysret_rip: Handle syscall in a FRED system Ammar Faizi
2023-01-25 23:24                               ` [RFC PATCH v7 2/3] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-25 23:24                               ` [RFC PATCH v7 3/3] selftests/x86: sysret_rip: Test SYSRET with a signal handler Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230125032240.728463-2-ammarfaizi2@gnuweeb.org \
    --to=ammarfaizi2@gnuweeb.org \
    --cc=Andrew.Cooper3@citrix.com \
    --cc=bp@alien8.de \
    --cc=brgerst@gmail.com \
    --cc=dave.hansen@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=shuah@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    --cc=xin3.li@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.