1 | /*
|
---|
2 | * Copyright 2011-2017 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include <stdio.h>
|
---|
11 | #include <stdlib.h>
|
---|
12 | #include <string.h>
|
---|
13 | #include <setjmp.h>
|
---|
14 | #include <signal.h>
|
---|
15 | #include <openssl/crypto.h>
|
---|
16 |
|
---|
17 | #include "arm_arch.h"
|
---|
18 |
|
---|
19 | unsigned int OPENSSL_armcap_P = 0;
|
---|
20 |
|
---|
21 | #if __ARM_MAX_ARCH__<7
|
---|
22 | void OPENSSL_cpuid_setup(void)
|
---|
23 | {
|
---|
24 | }
|
---|
25 |
|
---|
26 | unsigned long OPENSSL_rdtsc(void)
|
---|
27 | {
|
---|
28 | return 0;
|
---|
29 | }
|
---|
30 | #else
|
---|
31 | static sigset_t all_masked;
|
---|
32 |
|
---|
33 | static sigjmp_buf ill_jmp;
|
---|
34 | static void ill_handler(int sig)
|
---|
35 | {
|
---|
36 | siglongjmp(ill_jmp, sig);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * Following subroutines could have been inlined, but it's not all
|
---|
41 | * ARM compilers support inline assembler...
|
---|
42 | */
|
---|
43 | void _armv7_neon_probe(void);
|
---|
44 | void _armv8_aes_probe(void);
|
---|
45 | void _armv8_sha1_probe(void);
|
---|
46 | void _armv8_sha256_probe(void);
|
---|
47 | void _armv8_pmull_probe(void);
|
---|
48 | unsigned long _armv7_tick(void);
|
---|
49 |
|
---|
50 | unsigned long OPENSSL_rdtsc(void)
|
---|
51 | {
|
---|
52 | if (OPENSSL_armcap_P & ARMV7_TICK)
|
---|
53 | return _armv7_tick();
|
---|
54 | else
|
---|
55 | return 0;
|
---|
56 | }
|
---|
57 |
|
---|
58 | # if defined(__GNUC__) && __GNUC__>=2
|
---|
59 | void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
|
---|
60 | # endif
|
---|
61 | /*
|
---|
62 | * Use a weak reference to getauxval() so we can use it if it is available but
|
---|
63 | * don't break the build if it is not.
|
---|
64 | */
|
---|
65 | # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
|
---|
66 | extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
|
---|
67 | # else
|
---|
68 | static unsigned long (*getauxval) (unsigned long) = NULL;
|
---|
69 | # endif
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
|
---|
73 | * AArch64 used AT_HWCAP.
|
---|
74 | */
|
---|
75 | # if defined(__arm__) || defined (__arm)
|
---|
76 | # define HWCAP 16
|
---|
77 | /* AT_HWCAP */
|
---|
78 | # define HWCAP_NEON (1 << 12)
|
---|
79 |
|
---|
80 | # define HWCAP_CE 26
|
---|
81 | /* AT_HWCAP2 */
|
---|
82 | # define HWCAP_CE_AES (1 << 0)
|
---|
83 | # define HWCAP_CE_PMULL (1 << 1)
|
---|
84 | # define HWCAP_CE_SHA1 (1 << 2)
|
---|
85 | # define HWCAP_CE_SHA256 (1 << 3)
|
---|
86 | # elif defined(__aarch64__)
|
---|
87 | # define HWCAP 16
|
---|
88 | /* AT_HWCAP */
|
---|
89 | # define HWCAP_NEON (1 << 1)
|
---|
90 |
|
---|
91 | # define HWCAP_CE HWCAP
|
---|
92 | # define HWCAP_CE_AES (1 << 3)
|
---|
93 | # define HWCAP_CE_PMULL (1 << 4)
|
---|
94 | # define HWCAP_CE_SHA1 (1 << 5)
|
---|
95 | # define HWCAP_CE_SHA256 (1 << 6)
|
---|
96 | # endif
|
---|
97 |
|
---|
98 | void OPENSSL_cpuid_setup(void)
|
---|
99 | {
|
---|
100 | char *e;
|
---|
101 | struct sigaction ill_oact, ill_act;
|
---|
102 | sigset_t oset;
|
---|
103 | static int trigger = 0;
|
---|
104 |
|
---|
105 | if (trigger)
|
---|
106 | return;
|
---|
107 | trigger = 1;
|
---|
108 |
|
---|
109 | if ((e = getenv("OPENSSL_armcap"))) {
|
---|
110 | OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
|
---|
111 | return;
|
---|
112 | }
|
---|
113 |
|
---|
114 | # if defined(__APPLE__) && !defined(__aarch64__)
|
---|
115 | /*
|
---|
116 | * Capability probing by catching SIGILL appears to be problematic
|
---|
117 | * on iOS. But since Apple universe is "monocultural", it's actually
|
---|
118 | * possible to simply set pre-defined processor capability mask.
|
---|
119 | */
|
---|
120 | if (1) {
|
---|
121 | OPENSSL_armcap_P = ARMV7_NEON;
|
---|
122 | return;
|
---|
123 | }
|
---|
124 | /*
|
---|
125 | * One could do same even for __aarch64__ iOS builds. It's not done
|
---|
126 | * exclusively for reasons of keeping code unified across platforms.
|
---|
127 | * Unified code works because it never triggers SIGILL on Apple
|
---|
128 | * devices...
|
---|
129 | */
|
---|
130 | # endif
|
---|
131 |
|
---|
132 | sigfillset(&all_masked);
|
---|
133 | sigdelset(&all_masked, SIGILL);
|
---|
134 | sigdelset(&all_masked, SIGTRAP);
|
---|
135 | sigdelset(&all_masked, SIGFPE);
|
---|
136 | sigdelset(&all_masked, SIGBUS);
|
---|
137 | sigdelset(&all_masked, SIGSEGV);
|
---|
138 |
|
---|
139 | OPENSSL_armcap_P = 0;
|
---|
140 |
|
---|
141 | memset(&ill_act, 0, sizeof(ill_act));
|
---|
142 | ill_act.sa_handler = ill_handler;
|
---|
143 | ill_act.sa_mask = all_masked;
|
---|
144 |
|
---|
145 | sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
|
---|
146 | sigaction(SIGILL, &ill_act, &ill_oact);
|
---|
147 |
|
---|
148 | if (getauxval != NULL) {
|
---|
149 | if (getauxval(HWCAP) & HWCAP_NEON) {
|
---|
150 | unsigned long hwcap = getauxval(HWCAP_CE);
|
---|
151 |
|
---|
152 | OPENSSL_armcap_P |= ARMV7_NEON;
|
---|
153 |
|
---|
154 | if (hwcap & HWCAP_CE_AES)
|
---|
155 | OPENSSL_armcap_P |= ARMV8_AES;
|
---|
156 |
|
---|
157 | if (hwcap & HWCAP_CE_PMULL)
|
---|
158 | OPENSSL_armcap_P |= ARMV8_PMULL;
|
---|
159 |
|
---|
160 | if (hwcap & HWCAP_CE_SHA1)
|
---|
161 | OPENSSL_armcap_P |= ARMV8_SHA1;
|
---|
162 |
|
---|
163 | if (hwcap & HWCAP_CE_SHA256)
|
---|
164 | OPENSSL_armcap_P |= ARMV8_SHA256;
|
---|
165 | }
|
---|
166 | } else if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
167 | _armv7_neon_probe();
|
---|
168 | OPENSSL_armcap_P |= ARMV7_NEON;
|
---|
169 | if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
170 | _armv8_pmull_probe();
|
---|
171 | OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
|
---|
172 | } else if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
173 | _armv8_aes_probe();
|
---|
174 | OPENSSL_armcap_P |= ARMV8_AES;
|
---|
175 | }
|
---|
176 | if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
177 | _armv8_sha1_probe();
|
---|
178 | OPENSSL_armcap_P |= ARMV8_SHA1;
|
---|
179 | }
|
---|
180 | if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
181 | _armv8_sha256_probe();
|
---|
182 | OPENSSL_armcap_P |= ARMV8_SHA256;
|
---|
183 | }
|
---|
184 | }
|
---|
185 | if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
186 | _armv7_tick();
|
---|
187 | OPENSSL_armcap_P |= ARMV7_TICK;
|
---|
188 | }
|
---|
189 |
|
---|
190 | sigaction(SIGILL, &ill_oact, NULL);
|
---|
191 | sigprocmask(SIG_SETMASK, &oset, NULL);
|
---|
192 | }
|
---|
193 | #endif
|
---|