xref: /freebsd/lib/libc/arm/gen/arm_initfini.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 /*
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