xref: /freebsd/lib/libc/arm/gen/arm_initfini.c (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
1 /*-
2  * Copyright (c) 2013 The NetBSD Foundation, Inc.
3  * Copyright (c) 2013 Andrew Turner
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Matt Thomas of 3am Software Foundry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * Bases on NetBSD lib/libc/arch/arm/misc/arm_initfini.c
31  * $NetBSD: arm_initfini.c,v 1.2 2013/01/31 06:47:55 matt Exp $
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * To properly implement setjmp/longjmp for the ARM AAPCS ABI, it has to be
39  * aware of whether there is a FPU is present or not.  Regardless of whether
40  * the hard-float ABI is being used, setjmp needs to save D8-D15.  But it can
41  * only do this if those instructions won't cause an exception.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/sysctl.h>
46 
47 #include <stdbool.h>
48 #include <stddef.h>
49 
50 extern int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
51     void *newp, size_t newlen);
52 
53 int _libc_arm_fpu_present;
54 static bool _libc_aapcs_initialized;
55 
56 void	_libc_aapcs_init(void) __attribute__((__constructor__, __used__));
57 
58 void
59 _libc_aapcs_init(void)
60 {
61 	int mib[2];
62 	size_t len;
63 
64 	if (_libc_aapcs_initialized)
65 		return;
66 
67 	mib[0] = CTL_HW;
68 	mib[1] = HW_FLOATINGPT;
69 
70 	len = sizeof(_libc_arm_fpu_present);
71 	if (__sysctl(mib, 2, &_libc_arm_fpu_present, &len, NULL, 0) == -1 ||
72 	    len != sizeof(_libc_arm_fpu_present)) {
73 		/* sysctl failed, assume no vfp */
74 		_libc_arm_fpu_present = 0;
75 	}
76 
77 	_libc_aapcs_initialized = true;
78 }
79