Finish pulling in the NetBSD setjmp/longjmp updates on ARM.

Store/restore the VFP registers in setjmp/longjmp on ARM EABI if VFP is
enabled in the kernel. It checks the hw.floatingpoint sysctl to see if
floating-point is available and uses this to determine if it should store
them. If it does it uses a different magic value so longjmp is able to know
if it should load them.
This commit is contained in:
andrew 2013-06-07 22:01:06 +00:00
parent e2ff7d87d8
commit f0368ab7ce
4 changed files with 162 additions and 4 deletions

View File

@ -3,7 +3,8 @@
SRCS+= _ctx_start.S _setjmp.S _set_tp.c alloca.S fabs.c \
getcontextx.c infinity.c ldexp.c makecontext.c \
__aeabi_read_tp.S setjmp.S signalcontext.c sigsetjmp.S flt_rounds.c
__aeabi_read_tp.S setjmp.S signalcontext.c sigsetjmp.S flt_rounds.c \
arm_initfini.c
.if ${MK_ARM_EABI} == "no"
SRCS+= divsi3.S

View File

@ -36,7 +36,7 @@
#error FPA is not supported anymore
#endif
#ifdef __ARM_EABI__
#if defined(__ARM_EABI__) && !defined(_STANDALONE)
.fpu vfp
#endif
@ -61,6 +61,26 @@ __FBSDID("$FreeBSD$");
ENTRY(_setjmp)
ldr r1, .L_setjmp_magic
#if defined(__ARM_EABI__) && !defined(_STANDALONE)
ldr r2, .Lfpu_present
#ifdef PIC
GOT_INIT(r3, .L_setjmp_got, .L_setjmp_gotinit)
ldr r2, [r2, r3]
#else
ldr r2, [r2]
#endif
teq r2, #0 /* do we have a FPU? */
beq 1f /* no, don't save VFP registers */
orr r1, r1, #(_JB_MAGIC__SETJMP ^ _JB_MAGIC__SETJMP_VFP)
/* change magic to VFP magic */
add r2, r0, #(_JB_REG_D8 * 4)
vstmia r2, {d8-d15}
vmrs r2, fpscr
str r2, [r0, #(_JB_REG_FPSCR * 4)]
1:
#endif /* __ARM_EABI__ */
str r1, [r0]
add r0, r0, #(_JB_REG_R4 * 4)
@ -72,14 +92,31 @@ ENTRY(_setjmp)
.L_setjmp_magic:
.word _JB_MAGIC__SETJMP
#if defined(__ARM_EABI__) && !defined(_STANDALONE)
GOT_INITSYM(.L_setjmp_got, .L_setjmp_gotinit)
.Lfpu_present:
.word PIC_SYM(_libc_arm_fpu_present, GOTOFF)
#endif /* __ARM_EABI__ */
WEAK_ALIAS(___longjmp, _longjmp)
ENTRY(_longjmp)
ldr r2, [r0] /* get magic from jmp_buf */
bic r3, r2, #(_JB_MAGIC__SETJMP ^ _JB_MAGIC__SETJMP_VFP)
/* ignore VFP-ness of magic */
ldr ip, .L_setjmp_magic /* load magic */
teq ip, r2 /* magic correct? */
teq ip, r3 /* magic correct? */
bne botch /* no, botch */
#if defined(__ARM_EABI__) && !defined(_STANDALONE)
teq r3, r2 /* did magic change? */
beq 1f /* no, don't restore VFP */
add ip, r0, #(_JB_REG_D8 * 4)
vldmia ip, {d8-d15}
ldr ip, [r0, #(_JB_REG_FPSCR * 4)]
vmsr fpscr, ip
1:
#endif /* __ARM_EABI__ */
add r0, r0, #(_JB_REG_R4 * 4)
/* Restore integer registers */
ldmia r0, {r4-r14}

View File

@ -0,0 +1,78 @@
/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
* Copyright (c) 2013 Andrew Turner
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Matt Thomas of 3am Software Foundry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Bases on NetBSD lib/libc/arch/arm/misc/arm_initfini.c
* $NetBSD: arm_initfini.c,v 1.2 2013/01/31 06:47:55 matt Exp $
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* To properly implement setjmp/longjmp for the ARM AAPCS ABI, it has to be
* aware of whether there is a FPU is present or not. Regardless of whether
* the hard-float ABI is being used, setjmp needs to save D8-D15. But it can
* only do this if those instructions won't cause an exception.
*/
#include <sys/param.h>
#include <sys/sysctl.h>
#include <stdbool.h>
#include <stddef.h>
extern int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
void *newp, size_t newlen);
int _libc_arm_fpu_present;
static bool _libc_aapcs_initialized;
void _libc_aapcs_init(void) __attribute__((__constructor__, __used__));
void
_libc_aapcs_init(void)
{
int mib[2];
size_t len;
if (_libc_aapcs_initialized)
return;
mib[0] = CTL_HW;
mib[1] = HW_FLOATINGPT;
len = sizeof(_libc_arm_fpu_present);
if (__sysctl(mib, 2, &_libc_arm_fpu_present, &len, NULL, 0) == -1 ||
len != sizeof(_libc_arm_fpu_present)) {
/* sysctl failed, assume no vfp */
_libc_arm_fpu_present = 0;
}
_libc_aapcs_initialized = true;
}

View File

@ -36,6 +36,10 @@
#error FPA is not supported anymore
#endif
#ifdef __ARM_EABI__
.fpu vfp
#endif
#include <machine/asm.h>
#include <machine/setjmp.h>
@ -61,6 +65,27 @@ ENTRY(setjmp)
ldmfd sp!, {r0, r14}
ldr r1, .Lsetjmp_magic
#ifdef __ARM_EABI__
ldr r2, .Lfpu_present
#ifdef PIC
GOT_INIT(r3, .Lsetjmp_got, .Lsetjmp_gotinit)
ldr r2, [r2, r3]
#else
ldr r2, [r2]
#endif
teq r2, #0 /* do we have a FPU? */
beq 1f /* no, don't save VFP registers */
orr r1, r1, #(_JB_MAGIC_SETJMP ^ _JB_MAGIC_SETJMP_VFP)
/* change magic to VFP magic */
add r2, r0, #(_JB_REG_D8 * 4)
vstmia r2, {d8-d15}
vmrs r2, fpscr
str r2, [r0, #(_JB_REG_FPSCR * 4)]
1:
#endif /* __ARM_EABI__ */
str r1, [r0] /* store magic */
/* Store integer registers */
@ -71,6 +96,11 @@ ENTRY(setjmp)
.Lsetjmp_magic:
.word _JB_MAGIC_SETJMP
#ifdef __ARM_EABI__
GOT_INITSYM(.Lsetjmp_got, .Lsetjmp_gotinit)
.Lfpu_present:
.word PIC_SYM(_libc_arm_fpu_present, GOTOFF)
#endif /* __ARM_EABI__ */
.weak _C_LABEL(longjmp)
@ -78,7 +108,8 @@ ENTRY(setjmp)
ENTRY(__longjmp)
ldr r2, [r0]
ldr ip, .Lsetjmp_magic
teq r2, ip
bic r3, r2, #(_JB_MAGIC_SETJMP ^ _JB_MAGIC_SETJMP_VFP)
teq r3, ip
bne .Lbotch
/* Restore the signal mask. */
@ -89,6 +120,17 @@ ENTRY(__longjmp)
bl PIC_SYM(_C_LABEL(sigprocmask), PLT)
ldmfd sp!, {r0-r2, r14}
#ifdef __ARM_EABI__
tst r2, #(_JB_MAGIC_SETJMP ^ _JB_MAGIC_SETJMP_VFP)
/* is this a VFP magic? */
beq 1f /* no, don't restore VFP */
add ip, r0, #(_JB_REG_D8 * 4)
vldmia ip, {d8-d15}
ldr ip, [r0, #(_JB_REG_FPSCR * 4)]
vmsr fpscr, ip
1:
#endif /* __ARM_EABI__ */
add r0, r0, #(_JB_REG_R4 * 4)
/* Restore integer registers */
ldmia r0, {r4-r14}