blob: d13ac215c4dfa0a16996b31925ac72d34fdb18d2 [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
David Benjamin661266e2021-12-23 03:42:39 -050015#include "internal.h"
David Benjamin054e1512016-03-01 17:35:47 -050016
David Benjamin37faa932021-12-21 18:43:32 -050017#if defined(OPENSSL_ARM) && defined(OPENSSL_LINUX) && \
18 !defined(OPENSSL_STATIC_ARMCAP)
David Benjamin054e1512016-03-01 17:35:47 -050019#include <errno.h>
20#include <fcntl.h>
David Benjamin6ab4f0a2023-02-21 10:54:37 -050021#include <sys/auxv.h>
David Benjamin054e1512016-03-01 17:35:47 -050022#include <sys/types.h>
23#include <unistd.h>
24
25#include <openssl/arm_arch.h>
David Benjamin054e1512016-03-01 17:35:47 -050026#include <openssl/mem.h>
27
David Benjamin295b3132021-12-21 18:54:15 -050028#include "cpu_arm_linux.h"
David Benjamin054e1512016-03-01 17:35:47 -050029
David Benjamin054e1512016-03-01 17:35:47 -050030static int open_eintr(const char *path, int flags) {
31 int ret;
32 do {
33 ret = open(path, flags);
34 } while (ret < 0 && errno == EINTR);
35 return ret;
36}
37
38static ssize_t read_eintr(int fd, void *out, size_t len) {
39 ssize_t ret;
40 do {
41 ret = read(fd, out, len);
42 } while (ret < 0 && errno == EINTR);
43 return ret;
44}
45
David Benjamin808f8322017-08-18 14:06:02 -040046// read_file opens |path| and reads until end-of-file. On success, it returns
47// one and sets |*out_ptr| and |*out_len| to a newly-allocated buffer with the
48// contents. Otherwise, it returns zero.
David Benjamin054e1512016-03-01 17:35:47 -050049static int read_file(char **out_ptr, size_t *out_len, const char *path) {
50 int fd = open_eintr(path, O_RDONLY);
51 if (fd < 0) {
52 return 0;
53 }
54
55 static const size_t kReadSize = 1024;
56 int ret = 0;
57 size_t cap = kReadSize, len = 0;
58 char *buf = OPENSSL_malloc(cap);
59 if (buf == NULL) {
60 goto err;
61 }
62
63 for (;;) {
64 if (cap - len < kReadSize) {
65 size_t new_cap = cap * 2;
66 if (new_cap < cap) {
67 goto err;
68 }
69 char *new_buf = OPENSSL_realloc(buf, new_cap);
70 if (new_buf == NULL) {
71 goto err;
72 }
73 buf = new_buf;
74 cap = new_cap;
75 }
76
77 ssize_t bytes_read = read_eintr(fd, buf + len, kReadSize);
78 if (bytes_read < 0) {
79 goto err;
80 }
81 if (bytes_read == 0) {
82 break;
83 }
84 len += bytes_read;
85 }
86
87 *out_ptr = buf;
88 *out_len = len;
89 ret = 1;
90 buf = NULL;
91
92err:
93 OPENSSL_free(buf);
94 close(fd);
95 return ret;
96}
97
David Benjamin054e1512016-03-01 17:35:47 -050098extern uint32_t OPENSSL_armcap_P;
99
David Benjamin2c12ebd2023-02-01 10:39:50 -0500100static int g_needs_hwcap2_workaround;
David Benjamin0a63b962016-04-28 12:17:55 -0400101
David Benjamin054e1512016-03-01 17:35:47 -0500102void OPENSSL_cpuid_setup(void) {
David Benjamin9cf9d3e2020-05-08 18:28:07 -0400103 // We ignore the return value of |read_file| and proceed with an empty
104 // /proc/cpuinfo on error. If |getauxval| works, we will still detect
David Benjamin2c12ebd2023-02-01 10:39:50 -0500105 // capabilities.
David Benjamin9cf9d3e2020-05-08 18:28:07 -0400106 char *cpuinfo_data = NULL;
107 size_t cpuinfo_len = 0;
108 read_file(&cpuinfo_data, &cpuinfo_len, "/proc/cpuinfo");
David Benjamin054e1512016-03-01 17:35:47 -0500109 STRING_PIECE cpuinfo;
110 cpuinfo.data = cpuinfo_data;
111 cpuinfo.len = cpuinfo_len;
112
David Benjamin808f8322017-08-18 14:06:02 -0400113 // Matching OpenSSL, only report other features if NEON is present.
David Benjamin6ab4f0a2023-02-21 10:54:37 -0500114 unsigned long hwcap = getauxval(AT_HWCAP);
David Benjamin054e1512016-03-01 17:35:47 -0500115 if (hwcap & HWCAP_NEON) {
116 OPENSSL_armcap_P |= ARMV7_NEON;
117
David Benjamin808f8322017-08-18 14:06:02 -0400118 // Some ARMv8 Android devices don't expose AT_HWCAP2. Fall back to
David Benjaminf9bd4552021-02-09 16:13:28 -0500119 // /proc/cpuinfo. See https://crbug.com/boringssl/46. As of February 2021,
120 // this is now rare (see Chrome's Net.NeedsHWCAP2Workaround metric), but AES
121 // and PMULL extensions are very useful, so we still carry the workaround
122 // for now.
David Benjamin6ab4f0a2023-02-21 10:54:37 -0500123 unsigned long hwcap2 = getauxval(AT_HWCAP2);
David Benjamina2d4c0c2016-03-20 17:53:34 -0400124 if (hwcap2 == 0) {
David Benjamin8cd61f72018-08-31 15:37:56 -0500125 hwcap2 = crypto_get_arm_hwcap2_from_cpuinfo(&cpuinfo);
David Benjamin9a127b42017-09-15 20:30:07 -0400126 g_needs_hwcap2_workaround = hwcap2 != 0;
David Benjamina2d4c0c2016-03-20 17:53:34 -0400127 }
David Benjamin054e1512016-03-01 17:35:47 -0500128
David Benjamina2d4c0c2016-03-20 17:53:34 -0400129 if (hwcap2 & HWCAP2_AES) {
130 OPENSSL_armcap_P |= ARMV8_AES;
131 }
132 if (hwcap2 & HWCAP2_PMULL) {
133 OPENSSL_armcap_P |= ARMV8_PMULL;
134 }
135 if (hwcap2 & HWCAP2_SHA1) {
136 OPENSSL_armcap_P |= ARMV8_SHA1;
137 }
138 if (hwcap2 & HWCAP2_SHA2) {
139 OPENSSL_armcap_P |= ARMV8_SHA256;
David Benjamin054e1512016-03-01 17:35:47 -0500140 }
141 }
142
143 OPENSSL_free(cpuinfo_data);
144}
145
David Benjamin2c12ebd2023-02-01 10:39:50 -0500146int CRYPTO_has_broken_NEON(void) { return 0; }
David Benjamin0a63b962016-04-28 12:17:55 -0400147
David Benjamin9a127b42017-09-15 20:30:07 -0400148int CRYPTO_needs_hwcap2_workaround(void) { return g_needs_hwcap2_workaround; }
149
David Benjamin37faa932021-12-21 18:43:32 -0500150#endif // OPENSSL_ARM && OPENSSL_LINUX && !OPENSSL_STATIC_ARMCAP