blob: 342fed2b27b6947323f57f5761141025a702e503 [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
17#if defined(OPENSSL_ARM) && !defined(OPENSSL_STATIC_ARMCAP)
18
19#include <errno.h>
20#include <fcntl.h>
21#include <string.h>
22#include <sys/types.h>
23#include <unistd.h>
24
25#include <openssl/arm_arch.h>
26#include <openssl/buf.h>
27#include <openssl/mem.h>
28
29#include "internal.h"
30
31
32#define AT_HWCAP 16
33#define AT_HWCAP2 26
34
35#define HWCAP_NEON (1 << 12)
36
37/* See /usr/include/asm/hwcap.h on an ARM installation for the source of
38 * these values. */
39#define HWCAP2_AES (1 << 0)
40#define HWCAP2_PMULL (1 << 1)
41#define HWCAP2_SHA1 (1 << 2)
42#define HWCAP2_SHA2 (1 << 3)
43
44/* |getauxval| is not available on Android until API level 20. Link it as a weak
45 * symbol and use other methods as fallback. */
46unsigned long getauxval(unsigned long type) __attribute__((weak));
47
48static int open_eintr(const char *path, int flags) {
49 int ret;
50 do {
51 ret = open(path, flags);
52 } while (ret < 0 && errno == EINTR);
53 return ret;
54}
55
56static ssize_t read_eintr(int fd, void *out, size_t len) {
57 ssize_t ret;
58 do {
59 ret = read(fd, out, len);
60 } while (ret < 0 && errno == EINTR);
61 return ret;
62}
63
64/* read_full reads exactly |len| bytes from |fd| to |out|. On error or end of
65 * file, it returns zero. */
66static int read_full(int fd, void *out, size_t len) {
67 while (len > 0) {
68 ssize_t ret = read_eintr(fd, out, len);
69 if (ret <= 0) {
70 return 0;
71 }
72 out += ret;
73 len -= ret;
74 }
75 return 1;
76}
77
78/* read_file opens |path| and reads until end-of-file. On success, it returns
79 * one and sets |*out_ptr| and |*out_len| to a newly-allocated buffer with the
80 * contents. Otherwise, it returns zero. */
81static int read_file(char **out_ptr, size_t *out_len, const char *path) {
82 int fd = open_eintr(path, O_RDONLY);
83 if (fd < 0) {
84 return 0;
85 }
86
87 static const size_t kReadSize = 1024;
88 int ret = 0;
89 size_t cap = kReadSize, len = 0;
90 char *buf = OPENSSL_malloc(cap);
91 if (buf == NULL) {
92 goto err;
93 }
94
95 for (;;) {
96 if (cap - len < kReadSize) {
97 size_t new_cap = cap * 2;
98 if (new_cap < cap) {
99 goto err;
100 }
101 char *new_buf = OPENSSL_realloc(buf, new_cap);
102 if (new_buf == NULL) {
103 goto err;
104 }
105 buf = new_buf;
106 cap = new_cap;
107 }
108
109 ssize_t bytes_read = read_eintr(fd, buf + len, kReadSize);
110 if (bytes_read < 0) {
111 goto err;
112 }
113 if (bytes_read == 0) {
114 break;
115 }
116 len += bytes_read;
117 }
118
119 *out_ptr = buf;
120 *out_len = len;
121 ret = 1;
122 buf = NULL;
123
124err:
125 OPENSSL_free(buf);
126 close(fd);
127 return ret;
128}
129
130/* getauxval_proc behaves like |getauxval| but reads from /proc/self/auxv. */
131static unsigned long getauxval_proc(unsigned long type) {
132 int fd = open_eintr("/proc/self/auxv", O_RDONLY);
133 if (fd < 0) {
134 return 0;
135 }
136
137 struct {
138 unsigned long tag;
139 unsigned long value;
140 } entry;
141
142 for (;;) {
143 if (!read_full(fd, &entry, sizeof(entry)) ||
144 (entry.tag == 0 && entry.value == 0)) {
145 break;
146 }
147 if (entry.tag == type) {
148 close(fd);
149 return entry.value;
150 }
151 }
152 close(fd);
153 return 0;
154}
155
156typedef struct {
157 const char *data;
158 size_t len;
159} STRING_PIECE;
160
161static int STRING_PIECE_equals(const STRING_PIECE *a, const char *b) {
162 size_t b_len = strlen(b);
163 return a->len == b_len && memcmp(a->data, b, b_len) == 0;
164}
165
166/* STRING_PIECE_split finds the first occurence of |sep| in |in| and, if found,
167 * sets |*out_left| and |*out_right| to |in| split before and after it. It
168 * returns one if |sep| was found and zero otherwise. */
169static int STRING_PIECE_split(STRING_PIECE *out_left, STRING_PIECE *out_right,
170 const STRING_PIECE *in, char sep) {
171 const char *p = memchr(in->data, sep, in->len);
172 if (p == NULL) {
173 return 0;
174 }
175 /* |out_left| or |out_right| may alias |in|, so make a copy. */
176 STRING_PIECE in_copy = *in;
177 out_left->data = in_copy.data;
178 out_left->len = p - in_copy.data;
179 out_right->data = in_copy.data + out_left->len + 1;
180 out_right->len = in_copy.len - out_left->len - 1;
181 return 1;
182}
183
184/* STRING_PIECE_trim removes leading and trailing whitespace from |s|. */
185static void STRING_PIECE_trim(STRING_PIECE *s) {
186 while (s->len != 0 && (s->data[0] == ' ' || s->data[0] == '\t')) {
187 s->data++;
188 s->len--;
189 }
190 while (s->len != 0 &&
191 (s->data[s->len - 1] == ' ' || s->data[s->len - 1] == '\t')) {
192 s->len--;
193 }
194}
195
196/* extract_cpuinfo_field extracts a /proc/cpuinfo field named |field| from
197 * |in|. If found, it sets |*out| to the value and returns one. Otherwise, it
198 * returns zero. */
199static int extract_cpuinfo_field(STRING_PIECE *out, const STRING_PIECE *in,
200 const char *field) {
201 /* Process |in| one line at a time. */
202 STRING_PIECE remaining = *in, line;
203 while (STRING_PIECE_split(&line, &remaining, &remaining, '\n')) {
204 STRING_PIECE key, value;
205 if (!STRING_PIECE_split(&key, &value, &line, ':')) {
206 continue;
207 }
208 STRING_PIECE_trim(&key);
209 if (STRING_PIECE_equals(&key, field)) {
210 STRING_PIECE_trim(&value);
211 *out = value;
212 return 1;
213 }
214 }
215
216 return 0;
217}
218
219static int cpuinfo_field_equals(const STRING_PIECE *cpuinfo, const char *field,
220 const char *value) {
221 STRING_PIECE extracted;
222 return extract_cpuinfo_field(&extracted, cpuinfo, field) &&
223 STRING_PIECE_equals(&extracted, value);
224}
225
226/* has_list_item treats |list| as a space-separated list of items and returns
227 * one if |item| is contained in |list| and zero otherwise. */
228static int has_list_item(const STRING_PIECE *list, const char *item) {
229 STRING_PIECE remaining = *list, feature;
230 while (STRING_PIECE_split(&feature, &remaining, &remaining, ' ')) {
231 if (STRING_PIECE_equals(&feature, item)) {
232 return 1;
233 }
234 }
235 return 0;
236}
237
238static unsigned long get_hwcap_cpuinfo(const STRING_PIECE *cpuinfo) {
239 if (cpuinfo_field_equals(cpuinfo, "CPU architecture", "8")) {
240 /* This is a 32-bit ARM binary running on a 64-bit kernel. NEON is always
241 * available on ARMv8. Linux omits required features, so reading the
242 * "Features" line does not work. (For simplicity, use strict equality. We
243 * assume everything running on future ARM architectures will have a
244 * working |getauxval|.) */
245 return HWCAP_NEON;
246 }
247
248 STRING_PIECE features;
249 if (extract_cpuinfo_field(&features, cpuinfo, "Features") &&
250 has_list_item(&features, "neon")) {
251 return HWCAP_NEON;
252 }
253 return 0;
254}
255
David Benjamina2d4c0c2016-03-20 17:53:34 -0400256static unsigned long get_hwcap2_cpuinfo(const STRING_PIECE *cpuinfo) {
257 STRING_PIECE features;
258 if (!extract_cpuinfo_field(&features, cpuinfo, "Features")) {
259 return 0;
260 }
261
262 unsigned long ret = 0;
263 if (has_list_item(&features, "aes")) {
264 ret |= HWCAP2_AES;
265 }
266 if (has_list_item(&features, "pmull")) {
267 ret |= HWCAP2_PMULL;
268 }
269 if (has_list_item(&features, "sha1")) {
270 ret |= HWCAP2_SHA1;
271 }
272 if (has_list_item(&features, "sha2")) {
273 ret |= HWCAP2_SHA2;
274 }
275 return ret;
276}
277
David Benjamin054e1512016-03-01 17:35:47 -0500278/* has_broken_neon returns one if |in| matches a CPU known to have a broken
279 * NEON unit. See https://crbug.com/341598. */
280static int has_broken_neon(const STRING_PIECE *cpuinfo) {
281 return cpuinfo_field_equals(cpuinfo, "CPU implementer", "0x51") &&
282 cpuinfo_field_equals(cpuinfo, "CPU architecture", "7") &&
283 cpuinfo_field_equals(cpuinfo, "CPU variant", "0x1") &&
284 cpuinfo_field_equals(cpuinfo, "CPU part", "0x04d") &&
285 cpuinfo_field_equals(cpuinfo, "CPU revision", "0");
286}
287
288extern uint32_t OPENSSL_armcap_P;
289
290void OPENSSL_cpuid_setup(void) {
291 char *cpuinfo_data;
292 size_t cpuinfo_len;
293 if (!read_file(&cpuinfo_data, &cpuinfo_len, "/proc/cpuinfo")) {
294 return;
295 }
296 STRING_PIECE cpuinfo;
297 cpuinfo.data = cpuinfo_data;
298 cpuinfo.len = cpuinfo_len;
299
300 /* |getauxval| is not available on Android until API level 20. If it is
301 * unavailable, read from /proc/self/auxv as a fallback. This is unreadable
302 * on some versions of Android, so further fall back to /proc/cpuinfo.
303 *
304 * See
305 * https://android.googlesource.com/platform/ndk/+/882ac8f3392858991a0e1af33b4b7387ec856bd2
306 * and b/13679666 (Google-internal) for details. */
307 unsigned long hwcap = 0;
308 if (getauxval != NULL) {
309 hwcap = getauxval(AT_HWCAP);
310 }
311 if (hwcap == 0) {
312 hwcap = getauxval_proc(AT_HWCAP);
313 }
314 if (hwcap == 0) {
315 hwcap = get_hwcap_cpuinfo(&cpuinfo);
316 }
317
318 /* Clear NEON support if known broken. */
319 if (has_broken_neon(&cpuinfo)) {
320 hwcap &= ~HWCAP_NEON;
321 }
322
323 /* Matching OpenSSL, only report other features if NEON is present. */
324 if (hwcap & HWCAP_NEON) {
325 OPENSSL_armcap_P |= ARMV7_NEON;
326
David Benjamina2d4c0c2016-03-20 17:53:34 -0400327 /* Some ARMv8 Android devices don't expose AT_HWCAP2. Fall back to
328 * /proc/cpuinfo. See https://crbug.com/596156. */
329 unsigned long hwcap2 = 0;
David Benjamin054e1512016-03-01 17:35:47 -0500330 if (getauxval != NULL) {
David Benjamina2d4c0c2016-03-20 17:53:34 -0400331 hwcap2 = getauxval(AT_HWCAP2);
332 }
333 if (hwcap2 == 0) {
334 hwcap2 = get_hwcap2_cpuinfo(&cpuinfo);
335 }
David Benjamin054e1512016-03-01 17:35:47 -0500336
David Benjamina2d4c0c2016-03-20 17:53:34 -0400337 if (hwcap2 & HWCAP2_AES) {
338 OPENSSL_armcap_P |= ARMV8_AES;
339 }
340 if (hwcap2 & HWCAP2_PMULL) {
341 OPENSSL_armcap_P |= ARMV8_PMULL;
342 }
343 if (hwcap2 & HWCAP2_SHA1) {
344 OPENSSL_armcap_P |= ARMV8_SHA1;
345 }
346 if (hwcap2 & HWCAP2_SHA2) {
347 OPENSSL_armcap_P |= ARMV8_SHA256;
David Benjamin054e1512016-03-01 17:35:47 -0500348 }
349 }
350
351 OPENSSL_free(cpuinfo_data);
352}
353
354#endif /* OPENSSL_ARM && !OPENSSL_STATIC_ARMCAP */