1 /* hwf-ppc.c - Detect hardware features - PPC part
2  * Copyright (C) 2013,2019 Jussi Kivilinna <jussi.kivilinna@iki.fi>
3  * Copyright (C) 2019 Shawn Landden <shawn@git.icu>
4  *
5  * This file is part of Libgcrypt.
6  *
7  * Libgcrypt is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * Libgcrypt is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #if defined(HAVE_SYS_AUXV_H) && (defined(HAVE_GETAUXVAL) || \
29     defined(HAVE_ELF_AUX_INFO))
30 #include <sys/auxv.h>
31 #endif
32 
33 #include "g10lib.h"
34 #include "hwf-common.h"
35 
36 #if !defined (__powerpc__) && !defined (__powerpc64__)
37 # error Module build for wrong CPU.
38 #endif
39 
40 
41 #if defined(HAVE_SYS_AUXV_H) && defined(HAVE_ELF_AUX_INFO) && \
42     !defined(HAVE_GETAUXVAL) && defined(AT_HWCAP)
43 #define HAVE_GETAUXVAL
getauxval(unsigned long type)44 static unsigned long getauxval(unsigned long type)
45 {
46   unsigned long auxval = 0;
47   int err;
48 
49   /* FreeBSD provides 'elf_aux_info' function that does the same as
50    * 'getauxval' on Linux. */
51 
52   err = elf_aux_info (type, &auxval, sizeof(auxval));
53   if (err)
54     {
55       errno = err;
56       auxval = 0;
57     }
58 
59   return auxval;
60 }
61 #endif
62 
63 
64 #undef HAS_SYS_AT_HWCAP
65 #if defined(__linux__) || \
66     (defined(HAVE_SYS_AUXV_H) && defined(HAVE_GETAUXVAL))
67 #define HAS_SYS_AT_HWCAP 1
68 
69 struct feature_map_s
70   {
71     unsigned int hwcap_flag;
72     unsigned int hwcap2_flag;
73     unsigned int hwf_flag;
74   };
75 
76 #if defined(__powerpc__) || defined(__powerpc64__)
77 
78 /* Note: These macros have same values on Linux and FreeBSD. */
79 #ifndef AT_HWCAP
80 # define AT_HWCAP      16
81 #endif
82 #ifndef AT_HWCAP2
83 # define AT_HWCAP2     26
84 #endif
85 
86 #ifndef PPC_FEATURE2_ARCH_2_07
87 # define PPC_FEATURE2_ARCH_2_07     0x80000000
88 #endif
89 #ifndef PPC_FEATURE2_VEC_CRYPTO
90 # define PPC_FEATURE2_VEC_CRYPTO    0x02000000
91 #endif
92 #ifndef PPC_FEATURE2_ARCH_3_00
93 # define PPC_FEATURE2_ARCH_3_00     0x00800000
94 #endif
95 
96 static const struct feature_map_s ppc_features[] =
97   {
98     { 0, PPC_FEATURE2_ARCH_2_07, HWF_PPC_ARCH_2_07 },
99 #ifdef ENABLE_PPC_CRYPTO_SUPPORT
100     { 0, PPC_FEATURE2_VEC_CRYPTO, HWF_PPC_VCRYPTO },
101 #endif
102     { 0, PPC_FEATURE2_ARCH_3_00, HWF_PPC_ARCH_3_00 },
103   };
104 #endif
105 
106 static int
get_hwcap(unsigned int * hwcap,unsigned int * hwcap2)107 get_hwcap(unsigned int *hwcap, unsigned int *hwcap2)
108 {
109   struct { unsigned long a_type; unsigned long a_val; } auxv;
110   FILE *f;
111   int err = -1;
112   static int hwcap_initialized = 0;
113   static unsigned int stored_hwcap = 0;
114   static unsigned int stored_hwcap2 = 0;
115 
116   if (hwcap_initialized)
117     {
118       *hwcap = stored_hwcap;
119       *hwcap2 = stored_hwcap2;
120       return 0;
121     }
122 
123 #if 0 // TODO: configure.ac detection for __builtin_cpu_supports
124       // TODO: move to 'detect_ppc_builtin_cpu_supports'
125 #if defined(__GLIBC__) && defined(__GNUC__) && __GNUC__ >= 6
126   /* __builtin_cpu_supports returns 0 if glibc support doesn't exist, so
127    * we can only trust positive results. */
128 #ifdef ENABLE_PPC_CRYPTO_SUPPORT
129   if (__builtin_cpu_supports("vcrypto")) /* TODO: Configure.ac */
130     {
131       stored_hwcap2 |= PPC_FEATURE2_VEC_CRYPTO;
132       hwcap_initialized = 1;
133     }
134 #endif
135 
136   if (__builtin_cpu_supports("arch_3_00")) /* TODO: Configure.ac */
137     {
138       stored_hwcap2 |= PPC_FEATURE2_ARCH_3_00;
139       hwcap_initialized = 1;
140     }
141 #endif
142 #endif
143 
144 #if defined(HAVE_SYS_AUXV_H) && defined(HAVE_GETAUXVAL)
145   errno = 0;
146   auxv.a_val = getauxval (AT_HWCAP);
147   if (errno == 0)
148     {
149       stored_hwcap |= auxv.a_val;
150       hwcap_initialized = 1;
151     }
152 
153   if (AT_HWCAP2 >= 0)
154     {
155       errno = 0;
156       auxv.a_val = getauxval (AT_HWCAP2);
157       if (errno == 0)
158 	{
159 	  stored_hwcap2 |= auxv.a_val;
160 	  hwcap_initialized = 1;
161 	}
162     }
163 
164   if (hwcap_initialized && (stored_hwcap || stored_hwcap2))
165     {
166       *hwcap = stored_hwcap;
167       *hwcap2 = stored_hwcap2;
168       return 0;
169     }
170 #endif
171 
172   f = fopen("/proc/self/auxv", "r");
173   if (!f)
174     {
175       *hwcap = stored_hwcap;
176       *hwcap2 = stored_hwcap2;
177       return -1;
178     }
179 
180   while (fread(&auxv, sizeof(auxv), 1, f) > 0)
181     {
182       if (auxv.a_type == AT_HWCAP)
183         {
184           stored_hwcap |= auxv.a_val;
185           hwcap_initialized = 1;
186         }
187 
188       if (auxv.a_type == AT_HWCAP2)
189         {
190           stored_hwcap2 |= auxv.a_val;
191           hwcap_initialized = 1;
192         }
193     }
194 
195   if (hwcap_initialized)
196       err = 0;
197 
198   fclose(f);
199 
200   *hwcap = stored_hwcap;
201   *hwcap2 = stored_hwcap2;
202   return err;
203 }
204 
205 static unsigned int
detect_ppc_at_hwcap(void)206 detect_ppc_at_hwcap(void)
207 {
208   unsigned int hwcap;
209   unsigned int hwcap2;
210   unsigned int features = 0;
211   unsigned int i;
212 
213   if (get_hwcap(&hwcap, &hwcap2) < 0)
214       return features;
215 
216   for (i = 0; i < DIM(ppc_features); i++)
217     {
218       if (hwcap & ppc_features[i].hwcap_flag)
219         features |= ppc_features[i].hwf_flag;
220 
221       if (hwcap2 & ppc_features[i].hwcap2_flag)
222         features |= ppc_features[i].hwf_flag;
223     }
224 
225   return features;
226 }
227 
228 #endif
229 
230 unsigned int
_gcry_hwf_detect_ppc(void)231 _gcry_hwf_detect_ppc (void)
232 {
233   unsigned int ret = 0;
234   unsigned int broken_hwfs = 0;
235 
236 #if defined (HAS_SYS_AT_HWCAP)
237   ret |= detect_ppc_at_hwcap ();
238 #endif
239 
240   ret &= ~broken_hwfs;
241 
242   return ret;
243 }
244