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/*
37 * To properly implement setjmp/longjmp for the ARM AAPCS ABI, it has to be
38 * aware of whether there is a FPU is present or not.  Regardless of whether
39 * the hard-float ABI is being used, setjmp needs to save D8-D15.  But it can
40 * only do this if those instructions won't cause an exception.
41 */
42
43#include <sys/param.h>
44#include <sys/sysctl.h>
45
46#include <stdbool.h>
47#include <stddef.h>
48
49extern int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
50    void *newp, size_t newlen);
51
52int _libc_arm_fpu_present;
53static bool _libc_aapcs_initialized;
54
55void	_libc_aapcs_init(void) __attribute__((__constructor__, __used__));
56
57void
58_libc_aapcs_init(void)
59{
60	int mib[2];
61	size_t len;
62
63	if (_libc_aapcs_initialized)
64		return;
65
66	mib[0] = CTL_HW;
67	mib[1] = HW_FLOATINGPT;
68
69	len = sizeof(_libc_arm_fpu_present);
70	if (__sysctl(mib, 2, &_libc_arm_fpu_present, &len, NULL, 0) == -1 ||
71	    len != sizeof(_libc_arm_fpu_present)) {
72		/* sysctl failed, assume no vfp */
73		_libc_arm_fpu_present = 0;
74	}
75
76	_libc_aapcs_initialized = true;
77}
78