public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/2] x86-64/setcontext: Pop the pointer into %rdx after syscall
@ 2018-04-26 12:17 H.J. Lu
  2018-04-29 21:03 ` Florian Weimer
  0 siblings, 1 reply; 4+ messages in thread
From: H.J. Lu @ 2018-04-26 12:17 UTC (permalink / raw)
  To: GNU C Library

To prepare for shadow stack support, pop the pointer into %rdx after
syscall and use %rdx, instead of %rsi, to restore context.  There is
no functional change.

Any comments?

H.J.
----
	* sysdeps/unix/sysv/linux/x86_64/setcontext.S (__setcontext):
	Pop the pointer into %rdx after syscall and use %rdx, instead
	of %rsi, to restore context.
---
 sysdeps/unix/sysv/linux/x86_64/setcontext.S | 39 +++++++++++++++--------------
 1 file changed, 20 insertions(+), 19 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/x86_64/setcontext.S b/sysdeps/unix/sysv/linux/x86_64/setcontext.S
index 4a9b662074..5a1bdd8de6 100644
--- a/sysdeps/unix/sysv/linux/x86_64/setcontext.S
+++ b/sysdeps/unix/sysv/linux/x86_64/setcontext.S
@@ -44,21 +44,22 @@ ENTRY(__setcontext)
 	movl	$_NSIG8,%r10d
 	movl	$__NR_rt_sigprocmask, %eax
 	syscall
-	popq	%rdi			/* Reload %rdi, adjust stack.  */
+	/* Pop the pointer into RDX which is preserved by the syscall.  */
+	popq	%rdx
 	cfi_adjust_cfa_offset(-8)
 	cmpq	$-4095, %rax		/* Check %rax for error.  */
 	jae	SYSCALL_ERROR_LABEL	/* Jump to error handler if error.  */
 
 	/* Restore the floating-point context.  Not the registers, only the
 	   rest.  */
-	movq	oFPREGS(%rdi), %rcx
+	movq	oFPREGS(%rdx), %rcx
 	fldenv	(%rcx)
-	ldmxcsr oMXCSR(%rdi)
+	ldmxcsr oMXCSR(%rdx)
 
 
 	/* Load the new stack pointer, the preserved registers and
 	   registers used for passing args.  */
-	cfi_def_cfa(%rdi, 0)
+	cfi_def_cfa(%rdx, 0)
 	cfi_offset(%rbx,oRBX)
 	cfi_offset(%rbp,oRBP)
 	cfi_offset(%r12,oR12)
@@ -68,27 +69,27 @@ ENTRY(__setcontext)
 	cfi_offset(%rsp,oRSP)
 	cfi_offset(%rip,oRIP)
 
-	movq	oRSP(%rdi), %rsp
-	movq	oRBX(%rdi), %rbx
-	movq	oRBP(%rdi), %rbp
-	movq	oR12(%rdi), %r12
-	movq	oR13(%rdi), %r13
-	movq	oR14(%rdi), %r14
-	movq	oR15(%rdi), %r15
+	movq	oRSP(%rdx), %rsp
+	movq	oRBX(%rdx), %rbx
+	movq	oRBP(%rdx), %rbp
+	movq	oR12(%rdx), %r12
+	movq	oR13(%rdx), %r13
+	movq	oR14(%rdx), %r14
+	movq	oR15(%rdx), %r15
 
 	/* The following ret should return to the address set with
 	getcontext.  Therefore push the address on the stack.  */
-	movq	oRIP(%rdi), %rcx
+	movq	oRIP(%rdx), %rcx
 	pushq	%rcx
 
-	movq	oRSI(%rdi), %rsi
-	movq	oRDX(%rdi), %rdx
-	movq	oRCX(%rdi), %rcx
-	movq	oR8(%rdi), %r8
-	movq	oR9(%rdi), %r9
+	movq	oRSI(%rdx), %rsi
+	movq	oRDI(%rdx), %rdi
+	movq	oRCX(%rdx), %rcx
+	movq	oR8(%rdx), %r8
+	movq	oR9(%rdx), %r9
 
-	/* Setup finally  %rdi.  */
-	movq	oRDI(%rdi), %rdi
+	/* Setup finally %rdx.  */
+	movq	oRDX(%rdx), %rdx
 
 	/* End FDE here, we fall into another context.  */
 	cfi_endproc
-- 
2.14.3

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

* Re: [PATCH 1/2] x86-64/setcontext: Pop the pointer into %rdx after syscall
  2018-04-26 12:17 [PATCH 1/2] x86-64/setcontext: Pop the pointer into %rdx after syscall H.J. Lu
@ 2018-04-29 21:03 ` Florian Weimer
  2018-04-29 22:07   ` H.J. Lu
  2018-05-02  5:03   ` Carlos O'Donell
  0 siblings, 2 replies; 4+ messages in thread
From: Florian Weimer @ 2018-04-29 21:03 UTC (permalink / raw)
  To: H.J. Lu, GNU C Library

On 04/26/2018 02:17 PM, H.J. Lu wrote:
> +	/* Pop the pointer into RDX which is preserved by the syscall.  */
> +	popq	%rdx

Sorry, the comment is a bit unclear.  Why do you need to restore %rdx if 
it is preserved by the system call?  Maybe it becomes clearer with a 
future change?

Thanks,
Florian

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

* Re: [PATCH 1/2] x86-64/setcontext: Pop the pointer into %rdx after syscall
  2018-04-29 21:03 ` Florian Weimer
@ 2018-04-29 22:07   ` H.J. Lu
  2018-05-02  5:03   ` Carlos O'Donell
  1 sibling, 0 replies; 4+ messages in thread
From: H.J. Lu @ 2018-04-29 22:07 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On Sun, Apr 29, 2018 at 2:03 PM, Florian Weimer <fweimer@redhat.com> wrote:
> On 04/26/2018 02:17 PM, H.J. Lu wrote:
>>
>> +       /* Pop the pointer into RDX which is preserved by the syscall.  */
>> +       popq    %rdx
>
>
> Sorry, the comment is a bit unclear.  Why do you need to restore %rdx if it
> is preserved by the system call?  Maybe it becomes clearer with a future
> change?
>

Here is the patch to use it:

https://github.com/hjl-tools/glibc/commit/182ce0987aada98b2d606276d4aa4fbd2527031b


-- 
H.J.

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

* Re: [PATCH 1/2] x86-64/setcontext: Pop the pointer into %rdx after syscall
  2018-04-29 21:03 ` Florian Weimer
  2018-04-29 22:07   ` H.J. Lu
@ 2018-05-02  5:03   ` Carlos O'Donell
  1 sibling, 0 replies; 4+ messages in thread
From: Carlos O'Donell @ 2018-05-02  5:03 UTC (permalink / raw)
  To: Florian Weimer, H.J. Lu, GNU C Library

On 04/29/2018 05:03 PM, Florian Weimer wrote:
> On 04/26/2018 02:17 PM, H.J. Lu wrote:
>> +    /* Pop the pointer into RDX which is preserved by the syscall.
>> */ +    popq    %rdx
> 
> Sorry, the comment is a bit unclear.  Why do you need to restore %rdx
> if it is preserved by the system call?  Maybe it becomes clearer with
> a future change?

I agree with Florian, there is no reason to mention RDX is preserved.
It doesn't matter here.

The reason you use RDX is because later on you want to call arch_prctl
and need to use RDI + RSI to pass arguments, and so it's simple to
already be using RDX for the register offset loads.

If anything a better comment is:

/* Pop the pointer into RDX. The choice is arbitrary, but leaving
   RDI and RSI available for use later can avoid shuffling values.  */

Otherwise OK with that comment change.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>

-- 
Cheers,
Carlos.

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

end of thread, other threads:[~2018-05-02  5:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-26 12:17 [PATCH 1/2] x86-64/setcontext: Pop the pointer into %rdx after syscall H.J. Lu
2018-04-29 21:03 ` Florian Weimer
2018-04-29 22:07   ` H.J. Lu
2018-05-02  5:03   ` Carlos O'Donell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).