ce426f
Backport
ce426f
ce426f
37d350073888887637aa67dddf988d9c4b226032 \
ce426f
  aarch64: Re-implement setcontext without rt_sigreturn syscall
ce426f
03ea4d9b6916857e3c2a021f55d2a853cb837398 \
ce426f
  [AArch64] Simplify getcontext pstate initialization.
ce426f
6e445a3d2bdf152ebf57d1c92bfea1828e070743 \
ce426f
  [AArch64] Ensure getcontext() initializes PSTATE.
ce426f
ce426f
ce426f
diff --git glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/getcontext.S glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/getcontext.S
ce426f
index aff2e32..70b2e32 100644
ce426f
--- glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/getcontext.S
ce426f
+++ glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/getcontext.S
ce426f
@@ -1,6 +1,6 @@
ce426f
 /* Save current context.
ce426f
 
ce426f
-   Copyright (C) 2009-2012 Free Software Foundation, Inc.
ce426f
+   Copyright (C) 2009-2014 Free Software Foundation, Inc.
ce426f
 
ce426f
    This file is part of the GNU C Library.
ce426f
 
ce426f
@@ -53,6 +53,9 @@ ENTRY(__getcontext)
ce426f
 	mov	x2, sp
ce426f
 	str     x2, [x0, oSP]
ce426f
 
ce426f
+	/* Initialize the pstate.  */
ce426f
+	str	xzr, [x0, oPSTATE]
ce426f
+
ce426f
 	/* Figure out where to place the first context extension
ce426f
 	   block.  */
ce426f
 	add     x2, x0, #oEXTENSION
ce426f
diff --git glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/setcontext.S glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/setcontext.S
ce426f
index a98f67f..f45a655 100644
ce426f
--- glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/setcontext.S
ce426f
+++ glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/setcontext.S
ce426f
@@ -1,6 +1,6 @@
ce426f
 /* Set current context.
ce426f
 
ce426f
-   Copyright (C) 2009-2012 Free Software Foundation, Inc.
ce426f
+   Copyright (C) 2009-2014 Free Software Foundation, Inc.
ce426f
 
ce426f
    This file is part of the GNU C Library.
ce426f
 
ce426f
@@ -22,68 +22,108 @@
ce426f
 #include "ucontext_i.h"
ce426f
 #include "ucontext-internal.h"
ce426f
 
ce426f
-/* int setcontext (const ucontext_t *ucp) */
ce426f
+/*  int __setcontext (const ucontext_t *ucp)
ce426f
 
ce426f
-	.text
ce426f
-
ce426f
-ENTRY(__setcontext)
ce426f
-
ce426f
-	/* Create a signal frame on the stack:
ce426f
-
ce426f
-		fp
ce426f
-		lr
ce426f
-		...
ce426f
-	   sp-> rt_sigframe
ce426f
-	 */
ce426f
-
ce426f
-	stp     x29, x30, [sp, -16]!
ce426f
-	cfi_adjust_cfa_offset (16)
ce426f
-	cfi_rel_offset (x29, 0)
ce426f
-	cfi_rel_offset (x30, 8)
ce426f
-
ce426f
-        mov     x29, sp
ce426f
-	cfi_def_cfa_register (x29)
ce426f
-
ce426f
-	/* Allocate space for the sigcontext.  */
ce426f
-	mov	w3, #((RT_SIGFRAME_SIZE + SP_ALIGN_SIZE) & SP_ALIGN_MASK)
ce426f
-	sub	sp, sp,	x3
ce426f
+  Restores the machine context in UCP and thereby resumes execution
ce426f
+  in that context.
ce426f
 
ce426f
-	/* Compute the base address of the ucontext structure.  */
ce426f
-	add	x1, sp, #RT_SIGFRAME_UCONTEXT
ce426f
+  This implementation is intended to be used for *synchronous* context
ce426f
+  switches only.  Therefore, it does not have to restore anything
ce426f
+  other than the PRESERVED state.  */
ce426f
 
ce426f
-	/* Only ucontext is required in the frame, *copy* it in.  */
ce426f
-
ce426f
-#if UCONTEXT_SIZE % 16
ce426f
-#error The implementation of setcontext.S assumes sizeof(ucontext_t) % 16 == 0
ce426f
-#endif
ce426f
-
ce426f
-	mov	x2, #UCONTEXT_SIZE / 16
ce426f
-0:
ce426f
-	ldp	x3, x4, [x0], #16
ce426f
-	stp	x3, x4, [x1], #16
ce426f
-	sub	x2, x2, 1
ce426f
-	cbnz	x2, 0b
ce426f
+	.text
ce426f
 
ce426f
-	/* rt_sigreturn () -- no arguments, sp points to struct rt_sigframe.  */
ce426f
-	mov	x8, SYS_ify (rt_sigreturn)
ce426f
+ENTRY (__setcontext)
ce426f
+	/* Save a copy of UCP.  */
ce426f
+	mov	x9, x0
ce426f
+
ce426f
+	/* Set the signal mask with
ce426f
+	   rt_sigprocmask (SIG_SETMASK, mask, NULL, _NSIG/8).  */
ce426f
+	mov	x0, #SIG_SETMASK
ce426f
+	add	x1, x9, #UCONTEXT_SIGMASK
ce426f
+	mov	x2, #0
ce426f
+	mov	x3, #_NSIG8
ce426f
+	mov	x8, SYS_ify (rt_sigprocmask)
ce426f
 	svc	0
ce426f
-
ce426f
-	/* Ooops we failed.  Recover the stack */
ce426f
-
ce426f
-	mov	sp, x29
ce426f
-	cfi_def_cfa_register (sp)
ce426f
-
ce426f
-        ldp     x29, x30, [sp], 16
ce426f
-	cfi_adjust_cfa_offset (16)
ce426f
-	cfi_restore (x29)
ce426f
-	cfi_restore (x30)
ce426f
-	b	C_SYMBOL_NAME(__syscall_error)
ce426f
-
ce426f
+	cbz	x0, 1f
ce426f
+	b	C_SYMBOL_NAME (__syscall_error)
ce426f
+1:
ce426f
+	/* Restore the general purpose registers.  */
ce426f
+	mov	x0, x9
ce426f
+	cfi_def_cfa (x0, 0)
ce426f
+	cfi_offset (x18, oX0 + 18 * SZREG)
ce426f
+	cfi_offset (x19, oX0 + 19 * SZREG)
ce426f
+	cfi_offset (x20, oX0 + 20 * SZREG)
ce426f
+	cfi_offset (x21, oX0 + 21 * SZREG)
ce426f
+	cfi_offset (x22, oX0 + 22 * SZREG)
ce426f
+	cfi_offset (x23, oX0 + 23 * SZREG)
ce426f
+	cfi_offset (x24, oX0 + 24 * SZREG)
ce426f
+	cfi_offset (x25, oX0 + 25 * SZREG)
ce426f
+	cfi_offset (x26, oX0 + 26 * SZREG)
ce426f
+	cfi_offset (x27, oX0 + 27 * SZREG)
ce426f
+	cfi_offset (x28, oX0 + 28 * SZREG)
ce426f
+	cfi_offset (x29, oX0 + 29 * SZREG)
ce426f
+	cfi_offset (x30, oX0 + 30 * SZREG)
ce426f
+
ce426f
+	cfi_offset ( d8, oV0 + 8 * SZVREG)
ce426f
+	cfi_offset ( d9, oV0 + 9 * SZVREG)
ce426f
+	cfi_offset (d10, oV0 + 10 * SZVREG)
ce426f
+	cfi_offset (d11, oV0 + 11 * SZVREG)
ce426f
+	cfi_offset (d12, oV0 + 12 * SZVREG)
ce426f
+	cfi_offset (d13, oV0 + 13 * SZVREG)
ce426f
+	cfi_offset (d14, oV0 + 14 * SZVREG)
ce426f
+	cfi_offset (d15, oV0 + 15 * SZVREG)
ce426f
+	ldp	x18, x19, [x0, oX0 + 18 * SZREG]
ce426f
+	ldp	x20, x21, [x0, oX0 + 20 * SZREG]
ce426f
+	ldp	x22, x23, [x0, oX0 + 22 * SZREG]
ce426f
+	ldp	x24, x25, [x0, oX0 + 24 * SZREG]
ce426f
+	ldp	x26, x27, [x0, oX0 + 26 * SZREG]
ce426f
+	ldp	x28, x29, [x0, oX0 + 28 * SZREG]
ce426f
+	ldr     x30,      [x0, oX0 + 30 * SZREG]
ce426f
+	ldr     x2, [x0, oSP]
ce426f
+	mov	sp, x2
ce426f
+
ce426f
+	/* Check for FP SIMD context.  We don't support restoring
ce426f
+	   contexts created by the kernel, so this context must have
ce426f
+	   been created by getcontext.  Hence we can rely on the
ce426f
+	   first extension block being the FP SIMD context.  */
ce426f
+	add     x2, x0, #oEXTENSION
ce426f
+
ce426f
+	mov	w3, #(FPSIMD_MAGIC & 0xffff)
ce426f
+	movk	w3, #(FPSIMD_MAGIC >> 16), lsl #16
ce426f
+	ldr	w1, [x2, #oHEAD + oMAGIC]
ce426f
+	cmp	w1, w3
ce426f
+	b.ne	2f
ce426f
+
ce426f
+	/* Restore the FP SIMD context.  */
ce426f
+	add	x3, x2, #oV0 + 8 * SZVREG
ce426f
+	ldp	 d8,  d9, [x3], #2 * SZVREG
ce426f
+	ldp	d10, d11, [x3], #2 * SZVREG
ce426f
+	ldp	d12, d13, [x3], #2 * SZVREG
ce426f
+	ldp	d14, d15, [x3], #2 * SZVREG
ce426f
+
ce426f
+	add	x3, x2, oFPSR
ce426f
+
ce426f
+	ldr	w4, [x3]
ce426f
+	msr	fpsr, x4
ce426f
+
ce426f
+	ldr	w4, [x3, oFPCR - oFPSR]
ce426f
+	msr	fpcr, x4
ce426f
+
ce426f
+2:
ce426f
+	ldr     x16, [x0, oPC]
ce426f
+	/* Restore arg registers.  */
ce426f
+	ldp	x2, x3, [x0, oX0 + 2 * SZREG]
ce426f
+	ldp	x4, x5, [x0, oX0 + 4 * SZREG]
ce426f
+	ldp	x6, x7, [x0, oX0 + 6 * SZREG]
ce426f
+	ldp	x0, x1, [x0, oX0 + 0 * SZREG]
ce426f
+	/* Jump to the new pc value.  */
ce426f
+	br	x16
ce426f
 PSEUDO_END (__setcontext)
ce426f
 weak_alias (__setcontext, setcontext)
ce426f
 
ce426f
-ENTRY(__startcontext)
ce426f
+ENTRY (__startcontext)
ce426f
 	mov	x0, x19
ce426f
 	cbnz	x0, __setcontext
ce426f
-1:	b       HIDDEN_JUMPTARGET(_exit)
ce426f
-END(__startcontext)
ce426f
+1:	b       HIDDEN_JUMPTARGET (_exit)
ce426f
+END (__startcontext)
ce426f
diff --git glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/ucontext_i.sym glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/ucontext_i.sym
ce426f
index 1afff78..ab3930c 100644
ce426f
--- glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/ucontext_i.sym
ce426f
+++ glibc-2.17-c758a686/ports/sysdeps/unix/sysv/linux/aarch64/ucontext_i.sym
ce426f
@@ -37,6 +37,7 @@ STACK_FLAGS			stack (ss_flags)
ce426f
 oX0				mcontext (regs)
ce426f
 oSP				mcontext (sp)
ce426f
 oPC				mcontext (pc)
ce426f
+oPSTATE				mcontext (pstate)
ce426f
 oEXTENSION                      mcontext (__reserved)
ce426f
 
ce426f
 #define fpsimd_context(member)  offsetof (struct fpsimd_context, member)