Re: [RFC PATCH v1 1/2] selftests/x86: sysret_rip: Handle syscall in a FRED system

From: H. Peter Anvin
Date: Mon Jan 23 2023 - 20:43:12 EST


On 1/23/23 16:26, Ammar Faizi wrote:
+
+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)
+{
+ register unsigned long r11 asm("%r11");
+ register unsigned long r10 asm("%r10");
+ register unsigned long r8 asm("%r8");
+ register unsigned long r9 asm("%r9");
+ unsigned long rcx, rbx;
+
+ r11 = r11_sentinel;
+ rcx = rcx_sentinel;
+ r10 = arg4;
+ r8 = arg5;
+ r9 = arg6;
+
+ asm volatile (
+ "movq -8(%%rsp), %%r12\n\t" /* Don't clobber redzone. */
+ "pushq %[rflags_sentinel]\n\t"
+ "popf\n\t"
+ "movq %%r12, -8(%%rsp)\n\t"
+ "leaq 1f(%%rip), %[rbx]\n\t"
+ "syscall\n"
+ "1:"
+
+ : "+a" (nr_syscall),
+ "+r" (r11),
+ "+c" (rcx),
+ [rbx] "=b" (rbx)
+
+ : [rflags_sentinel] "g" (rflags_sentinel),
+ "D" (arg1), /* %rdi */
+ "S" (arg2), /* %rsi */
+ "d" (arg3), /* %rdx */
+ "r" (r10),
+ "r" (r8),
+ "r" (r9)
+
+ : "r12", "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.
+ *
+ */
+ assert(check_regs_result(r11, rcx, rbx) != REGS_ERROR);
+ return nr_syscall;
+}
+

So as per Andrew's comment, add:

register void * rsp asm("%rsp");

...

"+r" (rsp) /* clobber the redzone */

... as the right way to avoid redzone problems.

-hpa