xref: /netbsd-src/lib/libc/arch/arm/misc/arm_initfini.c (revision 66ad0cf771670312b70d06ef24da00bc8a18de1f)
1c9101679Smatt /*-
2c9101679Smatt  * Copyright (c) 2013 The NetBSD Foundation, Inc.
3c9101679Smatt  * All rights reserved.
4c9101679Smatt  *
5c9101679Smatt  * This code is derived from software contributed to The NetBSD Foundation
6c9101679Smatt  * by Matt Thomas of 3am Software Foundry.
7c9101679Smatt  *
8c9101679Smatt  * Redistribution and use in source and binary forms, with or without
9c9101679Smatt  * modification, are permitted provided that the following conditions
10c9101679Smatt  * are met:
11c9101679Smatt  * 1. Redistributions of source code must retain the above copyright
12c9101679Smatt  *    notice, this list of conditions and the following disclaimer.
13c9101679Smatt  * 2. Redistributions in binary form must reproduce the above copyright
14c9101679Smatt  *    notice, this list of conditions and the following disclaimer in the
15c9101679Smatt  *    documentation and/or other materials provided with the distribution.
16c9101679Smatt  *
17c9101679Smatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18c9101679Smatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19c9101679Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20c9101679Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21c9101679Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22c9101679Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23c9101679Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24c9101679Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25c9101679Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26c9101679Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27c9101679Smatt  * POSSIBILITY OF SUCH DAMAGE.
28c9101679Smatt  */
29c9101679Smatt 
30c9101679Smatt #include <sys/cdefs.h>
31c9101679Smatt 
32*66ad0cf7Smatt __RCSID("$NetBSD: arm_initfini.c,v 1.7 2013/09/08 13:15:53 matt Exp $");
33c026930aSskrll 
34c026930aSskrll #include "namespace.h"
35c9101679Smatt 
36c9101679Smatt /*
37c9101679Smatt  * To properly implement setjmp/longjmp for the ARM AAPCS ABI, it has to be
38c9101679Smatt  * aware of whether there is a FPU is present or not.  Regardless of whether
39c9101679Smatt  * the hard-float ABI is being used, setjmp needs to save D8-D15.  But it can
40c9101679Smatt  * only do this if those instructions won't cause an exception.
41c9101679Smatt  */
42c9101679Smatt 
43c9101679Smatt #include <sys/param.h>
44c9101679Smatt #include <sys/sysctl.h>
45c9101679Smatt 
46c9101679Smatt #include <stdbool.h>
478c98e4a9Smatt #include <stddef.h>
48c9101679Smatt 
499ba4e12cSmatt __dso_hidden int _libc_arm_fpu_present;
50*66ad0cf7Smatt #ifndef __ARM_ARCH_EXT_IDIV__
5198efa985Smatt __dso_hidden int _libc_arm_hwdiv_present;
52*66ad0cf7Smatt #endif
53c9101679Smatt static bool _libc_aapcs_initialized;
54c9101679Smatt 
55c9101679Smatt void	_libc_aapcs_init(void) __attribute__((__constructor__, __used__));
56c9101679Smatt 
578caf1030Smatt void __section(".text.startup")
_libc_aapcs_init(void)58c9101679Smatt _libc_aapcs_init(void)
59c9101679Smatt {
608c98e4a9Smatt 	if (!_libc_aapcs_initialized) {
61c9101679Smatt 		size_t len = sizeof(_libc_arm_fpu_present);
628c98e4a9Smatt 		_libc_aapcs_initialized = true;
638c98e4a9Smatt 		(void)sysctlbyname("machdep.fpu_present",
648c98e4a9Smatt 		    &_libc_arm_fpu_present, &len, NULL, 0);
65*66ad0cf7Smatt #ifndef __ARM_ARCH_EXT_IDIV__
6698efa985Smatt 		(void)sysctlbyname("machdep.hwdiv_present",
6798efa985Smatt 		    &_libc_arm_hwdiv_present, &len, NULL, 0);
68*66ad0cf7Smatt #endif
698c98e4a9Smatt 	}
70c9101679Smatt }
71