All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86_64,entry: Fix RCX for traced syscalls
@ 2014-06-26 19:08 Andy Lutomirski
  2014-06-26 19:56 ` Andi Kleen
  2014-11-04 23:43 ` Andy Lutomirski
  0 siblings, 2 replies; 9+ messages in thread
From: Andy Lutomirski @ 2014-06-26 19:08 UTC (permalink / raw)
  To: x86, H. Peter Anvin, Linux Kernel
  Cc: Borislav Petkov, Andi Kleen, Andy Lutomirski

The int_ret_from_sys_call and syscall tracing code disagrees with
the sysret path as to the value of RCX.

The Intel SDM, the AMD APM, and my laptop all agree that sysret
returns with RCX == RIP.  The syscall tracing code does not respect
this property.

For example, this program:

int main()
{
	extern const char syscall_rip[];
	unsigned long rcx = 1;
	unsigned long orig_rcx = rcx;
	asm ("mov $-1, %%eax\n\t"
	     "syscall\n\t"
	     "syscall_rip:"
	     : "+c" (rcx) : : "r11");
	printf("syscall: RCX = %lX  RIP = %lX  orig RCX = %lx\n",
	       rcx, (unsigned long)syscall_rip, orig_rcx);
	return 0;
}

prints:
syscall: RCX = 400556  RIP = 400556  orig RCX = 1

Running it under strace gives this instead:
syscall: RCX = FFFFFFFFFFFFFFFF  RIP = 400556  orig RCX = 1

This changes FIXUP_TOP_OF_STACK to match sysret, causing the test to
show RCX == RIP even under strace.

Signed-off-by: Andy Lutomirski <luto@amacapital.net>
---
 arch/x86/kernel/entry_64.S | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/entry_64.S b/arch/x86/kernel/entry_64.S
index b25ca96..6624e18 100644
--- a/arch/x86/kernel/entry_64.S
+++ b/arch/x86/kernel/entry_64.S
@@ -143,7 +143,8 @@ ENDPROC(native_usergs_sysret64)
 	movq \tmp,RSP+\offset(%rsp)
 	movq $__USER_DS,SS+\offset(%rsp)
 	movq $__USER_CS,CS+\offset(%rsp)
-	movq $-1,RCX+\offset(%rsp)
+	movq RIP+\offset(%rsp),\tmp  /* get rip */
+	movq \tmp,RCX+\offset(%rsp)  /* copy it to rcx as sysret would do */
 	movq R11+\offset(%rsp),\tmp  /* get eflags */
 	movq \tmp,EFLAGS+\offset(%rsp)
 	.endm
-- 
1.9.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2014-11-04 23:44 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-26 19:08 [PATCH] x86_64,entry: Fix RCX for traced syscalls Andy Lutomirski
2014-06-26 19:56 ` Andi Kleen
2014-06-26 19:59   ` Andy Lutomirski
2014-06-26 20:00     ` Andy Lutomirski
2014-06-26 20:12       ` H. Peter Anvin
2014-06-26 20:47         ` Andy Lutomirski
2014-06-28 17:07           ` Pavel Machek
2014-06-30 15:12             ` Andy Lutomirski
2014-11-04 23:43 ` Andy Lutomirski

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.