blob: 962a4a5095c1aaeb35055f11509363fe507312d3 [file] [log] [blame]
David Benjamin054e1512016-03-01 17:35:47 -05001/* Copyright (c) 2016, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/cpu.h>
16
David Benjamin8cd61f72018-08-31 15:37:56 -050017#if defined(OPENSSL_ARM) && !defined(OPENSSL_STATIC_ARMCAP)
David Benjamin054e1512016-03-01 17:35:47 -050018#include <errno.h>
19#include <fcntl.h>
David Benjamin054e1512016-03-01 17:35:47 -050020#include <sys/types.h>
21#include <unistd.h>
22
23#include <openssl/arm_arch.h>
David Benjamin054e1512016-03-01 17:35:47 -050024#include <openssl/mem.h>
25
David Benjamin4f746a92018-11-20 18:32:12 -060026#include "cpu-arm-linux.h"
David Benjamin054e1512016-03-01 17:35:47 -050027
28#define AT_HWCAP 16
29#define AT_HWCAP2 26
30
David Benjamin808f8322017-08-18 14:06:02 -040031// |getauxval| is not available on Android until API level 20. Link it as a weak
32// symbol and use other methods as fallback.
David Benjamin054e1512016-03-01 17:35:47 -050033unsigned long getauxval(unsigned long type) __attribute__((weak));
34
35static int open_eintr(const char *path, int flags) {
36 int ret;
37 do {
38 ret = open(path, flags);
39 } while (ret < 0 && errno == EINTR);
40 return ret;
41}
42
43static ssize_t read_eintr(int fd, void *out, size_t len) {
44 ssize_t ret;
45 do {
46 ret = read(fd, out, len);
47 } while (ret < 0 && errno == EINTR);
48 return ret;
49}
50
David Benjamin808f8322017-08-18 14:06:02 -040051// read_full reads exactly |len| bytes from |fd| to |out|. On error or end of
52// file, it returns zero.
David Benjamin054e1512016-03-01 17:35:47 -050053static int read_full(int fd, void *out, size_t len) {
David Benjaminbfefc272016-03-29 19:00:33 -040054 char *outp = out;
David Benjamin054e1512016-03-01 17:35:47 -050055 while (len > 0) {
David Benjaminbfefc272016-03-29 19:00:33 -040056 ssize_t ret = read_eintr(fd, outp, len);
David Benjamin054e1512016-03-01 17:35:47 -050057 if (ret <= 0) {
58 return 0;
59 }
David Benjaminbfefc272016-03-29 19:00:33 -040060 outp += ret;
David Benjamin054e1512016-03-01 17:35:47 -050061 len -= ret;
62 }
63 return 1;
64}
65
David Benjamin808f8322017-08-18 14:06:02 -040066// read_file opens |path| and reads until end-of-file. On success, it returns
67// one and sets |*out_ptr| and |*out_len| to a newly-allocated buffer with the
68// contents. Otherwise, it returns zero.
David Benjamin054e1512016-03-01 17:35:47 -050069static int read_file(char **out_ptr, size_t *out_len, const char *path) {
70 int fd = open_eintr(path, O_RDONLY);
71 if (fd < 0) {
72 return 0;
73 }
74
75 static const size_t kReadSize = 1024;
76 int ret = 0;
77 size_t cap = kReadSize, len = 0;
78 char *buf = OPENSSL_malloc(cap);
79 if (buf == NULL) {
80 goto err;
81 }
82
83 for (;;) {
84 if (cap - len < kReadSize) {
85 size_t new_cap = cap * 2;
86 if (new_cap < cap) {
87 goto err;
88 }
89 char *new_buf = OPENSSL_realloc(buf, new_cap);
90 if (new_buf == NULL) {
91 goto err;
92 }
93 buf = new_buf;
94 cap = new_cap;
95 }
96
97 ssize_t bytes_read = read_eintr(fd, buf + len, kReadSize);
98 if (bytes_read < 0) {
99 goto err;
100 }
101 if (bytes_read == 0) {
102 break;
103 }
104 len += bytes_read;
105 }
106
107 *out_ptr = buf;
108 *out_len = len;
109 ret = 1;
110 buf = NULL;
111
112err:
113 OPENSSL_free(buf);
114 close(fd);
115 return ret;
116}
117
David Benjamin808f8322017-08-18 14:06:02 -0400118// getauxval_proc behaves like |getauxval| but reads from /proc/self/auxv.
David Benjamin054e1512016-03-01 17:35:47 -0500119static unsigned long getauxval_proc(unsigned long type) {
120 int fd = open_eintr("/proc/self/auxv", O_RDONLY);
121 if (fd < 0) {
122 return 0;
123 }
124
125 struct {
126 unsigned long tag;
127 unsigned long value;
128 } entry;
129
130 for (;;) {
131 if (!read_full(fd, &entry, sizeof(entry)) ||
132 (entry.tag == 0 && entry.value == 0)) {
133 break;
134 }
135 if (entry.tag == type) {
136 close(fd);
137 return entry.value;
138 }
139 }
140 close(fd);
141 return 0;
142}
143
David Benjamin054e1512016-03-01 17:35:47 -0500144extern uint32_t OPENSSL_armcap_P;
145
David Benjamin9a127b42017-09-15 20:30:07 -0400146static int g_has_broken_neon, g_needs_hwcap2_workaround;
David Benjamin0a63b962016-04-28 12:17:55 -0400147
David Benjamin054e1512016-03-01 17:35:47 -0500148void OPENSSL_cpuid_setup(void) {
David Benjamin9cf9d3e2020-05-08 18:28:07 -0400149 // We ignore the return value of |read_file| and proceed with an empty
150 // /proc/cpuinfo on error. If |getauxval| works, we will still detect
151 // capabilities. There may be a false positive due to
152 // |crypto_cpuinfo_has_broken_neon|, but this is now rare.
153 char *cpuinfo_data = NULL;
154 size_t cpuinfo_len = 0;
155 read_file(&cpuinfo_data, &cpuinfo_len, "/proc/cpuinfo");
David Benjamin054e1512016-03-01 17:35:47 -0500156 STRING_PIECE cpuinfo;
157 cpuinfo.data = cpuinfo_data;
158 cpuinfo.len = cpuinfo_len;
159
David Benjamin808f8322017-08-18 14:06:02 -0400160 // |getauxval| is not available on Android until API level 20. If it is
161 // unavailable, read from /proc/self/auxv as a fallback. This is unreadable
162 // on some versions of Android, so further fall back to /proc/cpuinfo.
163 //
164 // See
165 // https://android.googlesource.com/platform/ndk/+/882ac8f3392858991a0e1af33b4b7387ec856bd2
166 // and b/13679666 (Google-internal) for details.
David Benjamin054e1512016-03-01 17:35:47 -0500167 unsigned long hwcap = 0;
168 if (getauxval != NULL) {
169 hwcap = getauxval(AT_HWCAP);
170 }
171 if (hwcap == 0) {
172 hwcap = getauxval_proc(AT_HWCAP);
173 }
174 if (hwcap == 0) {
David Benjamin8cd61f72018-08-31 15:37:56 -0500175 hwcap = crypto_get_arm_hwcap_from_cpuinfo(&cpuinfo);
David Benjamin054e1512016-03-01 17:35:47 -0500176 }
177
David Benjaminf9bd4552021-02-09 16:13:28 -0500178 // Clear NEON support if known broken. Note, if NEON is available statically,
179 // the non-NEON code is dropped and this workaround is a no-op.
180 //
181 // TODO(davidben): The Android NDK now builds with NEON statically available
182 // by default. Cronet still has some consumers that support NEON-less devices
183 // (b/150371744). Get metrics on whether they still see this CPU and, if not,
184 // remove this check entirely.
David Benjamin8cd61f72018-08-31 15:37:56 -0500185 g_has_broken_neon = crypto_cpuinfo_has_broken_neon(&cpuinfo);
David Benjamin0a63b962016-04-28 12:17:55 -0400186 if (g_has_broken_neon) {
David Benjamin054e1512016-03-01 17:35:47 -0500187 hwcap &= ~HWCAP_NEON;
188 }
189
David Benjamin808f8322017-08-18 14:06:02 -0400190 // Matching OpenSSL, only report other features if NEON is present.
David Benjamin054e1512016-03-01 17:35:47 -0500191 if (hwcap & HWCAP_NEON) {
192 OPENSSL_armcap_P |= ARMV7_NEON;
193
David Benjamin808f8322017-08-18 14:06:02 -0400194 // Some ARMv8 Android devices don't expose AT_HWCAP2. Fall back to
David Benjaminf9bd4552021-02-09 16:13:28 -0500195 // /proc/cpuinfo. See https://crbug.com/boringssl/46. As of February 2021,
196 // this is now rare (see Chrome's Net.NeedsHWCAP2Workaround metric), but AES
197 // and PMULL extensions are very useful, so we still carry the workaround
198 // for now.
David Benjamina2d4c0c2016-03-20 17:53:34 -0400199 unsigned long hwcap2 = 0;
David Benjamin054e1512016-03-01 17:35:47 -0500200 if (getauxval != NULL) {
David Benjamina2d4c0c2016-03-20 17:53:34 -0400201 hwcap2 = getauxval(AT_HWCAP2);
202 }
203 if (hwcap2 == 0) {
David Benjamin8cd61f72018-08-31 15:37:56 -0500204 hwcap2 = crypto_get_arm_hwcap2_from_cpuinfo(&cpuinfo);
David Benjamin9a127b42017-09-15 20:30:07 -0400205 g_needs_hwcap2_workaround = hwcap2 != 0;
David Benjamina2d4c0c2016-03-20 17:53:34 -0400206 }
David Benjamin054e1512016-03-01 17:35:47 -0500207
David Benjamina2d4c0c2016-03-20 17:53:34 -0400208 if (hwcap2 & HWCAP2_AES) {
209 OPENSSL_armcap_P |= ARMV8_AES;
210 }
211 if (hwcap2 & HWCAP2_PMULL) {
212 OPENSSL_armcap_P |= ARMV8_PMULL;
213 }
214 if (hwcap2 & HWCAP2_SHA1) {
215 OPENSSL_armcap_P |= ARMV8_SHA1;
216 }
217 if (hwcap2 & HWCAP2_SHA2) {
218 OPENSSL_armcap_P |= ARMV8_SHA256;
David Benjamin054e1512016-03-01 17:35:47 -0500219 }
220 }
221
222 OPENSSL_free(cpuinfo_data);
223}
224
David Benjamin0a63b962016-04-28 12:17:55 -0400225int CRYPTO_has_broken_NEON(void) { return g_has_broken_neon; }
226
David Benjamin9a127b42017-09-15 20:30:07 -0400227int CRYPTO_needs_hwcap2_workaround(void) { return g_needs_hwcap2_workaround; }
228
David Benjamin808f8322017-08-18 14:06:02 -0400229#endif // OPENSSL_ARM && !OPENSSL_STATIC_ARMCAP