1 /* powerpc HWCAP/HWCAP2 and AT_PLATFORM data pre-processing.
2    Copyright (C) 2015-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <unistd.h>
20 #include <shlib-compat.h>
21 #include <dl-procinfo.h>
22 
23 tcbhead_t __tcb __attribute__ ((visibility ("hidden")));
24 
25 /* This function parses the HWCAP/HWCAP2 fields, adding the previous supported
26    ISA bits, as well as converting the AT_PLATFORM string to a number.  This
27    data is stored in two global variables that can be used later by the
28    powerpc-specific code to store it into the TCB.  */
29 void
__tcb_parse_hwcap_and_convert_at_platform(void)30 __tcb_parse_hwcap_and_convert_at_platform (void)
31 {
32 
33   uint64_t h1, h2;
34 
35   /* Read AT_PLATFORM string from auxv and convert it to a number.  */
36   __tcb.at_platform = _dl_string_platform (GLRO (dl_platform));
37 
38   /* Read HWCAP and HWCAP2 from auxv.  */
39   h1 = GLRO (dl_hwcap);
40   h2 = GLRO (dl_hwcap2);
41 
42   /* hwcap contains only the latest supported ISA, the code checks which is
43      and fills the previous supported ones.  */
44 
45   if (h2 & PPC_FEATURE2_ARCH_2_07)
46     h1 |= PPC_FEATURE_ARCH_2_06
47        | PPC_FEATURE_ARCH_2_05
48        | PPC_FEATURE_POWER5_PLUS
49        | PPC_FEATURE_POWER5
50        | PPC_FEATURE_POWER4;
51   else if (h1 & PPC_FEATURE_ARCH_2_06)
52     h1 |= PPC_FEATURE_ARCH_2_05
53        | PPC_FEATURE_POWER5_PLUS
54        | PPC_FEATURE_POWER5
55        | PPC_FEATURE_POWER4;
56   else if (h1 & PPC_FEATURE_ARCH_2_05)
57     h1 |= PPC_FEATURE_POWER5_PLUS
58        | PPC_FEATURE_POWER5
59        | PPC_FEATURE_POWER4;
60   else if (h1 & PPC_FEATURE_POWER5_PLUS)
61     h1 |= PPC_FEATURE_POWER5
62        | PPC_FEATURE_POWER4;
63   else if (h1 & PPC_FEATURE_POWER5)
64     h1 |= PPC_FEATURE_POWER4;
65 
66   /* Consolidate both HWCAP and HWCAP2 into a single doubleword so that
67      we can read both in a single load later.  */
68   __tcb.hwcap = (h1 << 32) | (h2 & 0xffffffff);
69 
70 }
71 #if IS_IN (rtld)
72 versioned_symbol (ld, __tcb_parse_hwcap_and_convert_at_platform, \
73 		  __parse_hwcap_and_convert_at_platform, GLIBC_2_23);
74 #endif
75 
76 /* Export __parse_hwcap_and_convert_at_platform in libc.a.  This is used by
77    GCC to make sure that the HWCAP/Platform bits are stored in the TCB when
78    using __builtin_cpu_is()/__builtin_cpu_supports() in the static case.  */
79 #ifndef SHARED
80 weak_alias (__tcb_parse_hwcap_and_convert_at_platform, \
81 	    __parse_hwcap_and_convert_at_platform);
82 #endif
83