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