xref: /netbsd-src/lib/libc/arch/arm/misc/arm_initfini.c (revision 98efa985ed11e5c5c7f1cea2bc6313554daae87f)
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*98efa985Smatt __RCSID("$NetBSD: arm_initfini.c,v 1.6 2013/09/08 12:11:56 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*98efa985Smatt __dso_hidden int _libc_arm_hwdiv_present;
51c9101679Smatt static bool _libc_aapcs_initialized;
52c9101679Smatt 
53c9101679Smatt void	_libc_aapcs_init(void) __attribute__((__constructor__, __used__));
54c9101679Smatt 
558caf1030Smatt void __section(".text.startup")
56c9101679Smatt _libc_aapcs_init(void)
57c9101679Smatt {
588c98e4a9Smatt 	if (!_libc_aapcs_initialized) {
59c9101679Smatt 		size_t len = sizeof(_libc_arm_fpu_present);
608c98e4a9Smatt 		_libc_aapcs_initialized = true;
618c98e4a9Smatt 		(void)sysctlbyname("machdep.fpu_present",
628c98e4a9Smatt 		    &_libc_arm_fpu_present, &len, NULL, 0);
63*98efa985Smatt 		(void)sysctlbyname("machdep.hwdiv_present",
64*98efa985Smatt 		    &_libc_arm_hwdiv_present, &len, NULL, 0);
658c98e4a9Smatt 	}
66c9101679Smatt }
67