cpu-arm-linux.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* 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. #include <openssl/cpu.h>
  15. #if defined(OPENSSL_ARM) && !defined(OPENSSL_STATIC_ARMCAP)
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <sys/types.h>
  19. #include <unistd.h>
  20. #include <openssl/arm_arch.h>
  21. #include <openssl/buf.h>
  22. #include <openssl/mem.h>
  23. #include "cpu-arm-linux.h"
  24. #define AT_HWCAP 16
  25. #define AT_HWCAP2 26
  26. // |getauxval| is not available on Android until API level 20. Link it as a weak
  27. // symbol and use other methods as fallback.
  28. unsigned long getauxval(unsigned long type) __attribute__((weak));
  29. static int open_eintr(const char *path, int flags) {
  30. int ret;
  31. do {
  32. ret = open(path, flags);
  33. } while (ret < 0 && errno == EINTR);
  34. return ret;
  35. }
  36. static ssize_t read_eintr(int fd, void *out, size_t len) {
  37. ssize_t ret;
  38. do {
  39. ret = read(fd, out, len);
  40. } while (ret < 0 && errno == EINTR);
  41. return ret;
  42. }
  43. // read_full reads exactly |len| bytes from |fd| to |out|. On error or end of
  44. // file, it returns zero.
  45. static int read_full(int fd, void *out, size_t len) {
  46. char *outp = out;
  47. while (len > 0) {
  48. ssize_t ret = read_eintr(fd, outp, len);
  49. if (ret <= 0) {
  50. return 0;
  51. }
  52. outp += ret;
  53. len -= ret;
  54. }
  55. return 1;
  56. }
  57. // read_file opens |path| and reads until end-of-file. On success, it returns
  58. // one and sets |*out_ptr| and |*out_len| to a newly-allocated buffer with the
  59. // contents. Otherwise, it returns zero.
  60. static int read_file(char **out_ptr, size_t *out_len, const char *path) {
  61. int fd = open_eintr(path, O_RDONLY);
  62. if (fd < 0) {
  63. return 0;
  64. }
  65. static const size_t kReadSize = 1024;
  66. int ret = 0;
  67. size_t cap = kReadSize, len = 0;
  68. char *buf = OPENSSL_malloc(cap);
  69. if (buf == NULL) {
  70. goto err;
  71. }
  72. for (;;) {
  73. if (cap - len < kReadSize) {
  74. size_t new_cap = cap * 2;
  75. if (new_cap < cap) {
  76. goto err;
  77. }
  78. char *new_buf = OPENSSL_realloc(buf, new_cap);
  79. if (new_buf == NULL) {
  80. goto err;
  81. }
  82. buf = new_buf;
  83. cap = new_cap;
  84. }
  85. ssize_t bytes_read = read_eintr(fd, buf + len, kReadSize);
  86. if (bytes_read < 0) {
  87. goto err;
  88. }
  89. if (bytes_read == 0) {
  90. break;
  91. }
  92. len += bytes_read;
  93. }
  94. *out_ptr = buf;
  95. *out_len = len;
  96. ret = 1;
  97. buf = NULL;
  98. err:
  99. OPENSSL_free(buf);
  100. close(fd);
  101. return ret;
  102. }
  103. // getauxval_proc behaves like |getauxval| but reads from /proc/self/auxv.
  104. static unsigned long getauxval_proc(unsigned long type) {
  105. int fd = open_eintr("/proc/self/auxv", O_RDONLY);
  106. if (fd < 0) {
  107. return 0;
  108. }
  109. struct {
  110. unsigned long tag;
  111. unsigned long value;
  112. } entry;
  113. for (;;) {
  114. if (!read_full(fd, &entry, sizeof(entry)) ||
  115. (entry.tag == 0 && entry.value == 0)) {
  116. break;
  117. }
  118. if (entry.tag == type) {
  119. close(fd);
  120. return entry.value;
  121. }
  122. }
  123. close(fd);
  124. return 0;
  125. }
  126. extern uint32_t OPENSSL_armcap_P;
  127. static int g_has_broken_neon, g_needs_hwcap2_workaround;
  128. void OPENSSL_cpuid_setup(void) {
  129. char *cpuinfo_data;
  130. size_t cpuinfo_len;
  131. if (!read_file(&cpuinfo_data, &cpuinfo_len, "/proc/cpuinfo")) {
  132. return;
  133. }
  134. STRING_PIECE cpuinfo;
  135. cpuinfo.data = cpuinfo_data;
  136. cpuinfo.len = cpuinfo_len;
  137. // |getauxval| is not available on Android until API level 20. If it is
  138. // unavailable, read from /proc/self/auxv as a fallback. This is unreadable
  139. // on some versions of Android, so further fall back to /proc/cpuinfo.
  140. //
  141. // See
  142. // https://android.googlesource.com/platform/ndk/+/882ac8f3392858991a0e1af33b4b7387ec856bd2
  143. // and b/13679666 (Google-internal) for details.
  144. unsigned long hwcap = 0;
  145. if (getauxval != NULL) {
  146. hwcap = getauxval(AT_HWCAP);
  147. }
  148. if (hwcap == 0) {
  149. hwcap = getauxval_proc(AT_HWCAP);
  150. }
  151. if (hwcap == 0) {
  152. hwcap = crypto_get_arm_hwcap_from_cpuinfo(&cpuinfo);
  153. }
  154. // Clear NEON support if known broken.
  155. g_has_broken_neon = crypto_cpuinfo_has_broken_neon(&cpuinfo);
  156. if (g_has_broken_neon) {
  157. hwcap &= ~HWCAP_NEON;
  158. }
  159. // Matching OpenSSL, only report other features if NEON is present.
  160. if (hwcap & HWCAP_NEON) {
  161. OPENSSL_armcap_P |= ARMV7_NEON;
  162. // Some ARMv8 Android devices don't expose AT_HWCAP2. Fall back to
  163. // /proc/cpuinfo. See https://crbug.com/596156.
  164. unsigned long hwcap2 = 0;
  165. if (getauxval != NULL) {
  166. hwcap2 = getauxval(AT_HWCAP2);
  167. }
  168. if (hwcap2 == 0) {
  169. hwcap2 = crypto_get_arm_hwcap2_from_cpuinfo(&cpuinfo);
  170. g_needs_hwcap2_workaround = hwcap2 != 0;
  171. }
  172. if (hwcap2 & HWCAP2_AES) {
  173. OPENSSL_armcap_P |= ARMV8_AES;
  174. }
  175. if (hwcap2 & HWCAP2_PMULL) {
  176. OPENSSL_armcap_P |= ARMV8_PMULL;
  177. }
  178. if (hwcap2 & HWCAP2_SHA1) {
  179. OPENSSL_armcap_P |= ARMV8_SHA1;
  180. }
  181. if (hwcap2 & HWCAP2_SHA2) {
  182. OPENSSL_armcap_P |= ARMV8_SHA256;
  183. }
  184. }
  185. OPENSSL_free(cpuinfo_data);
  186. }
  187. int CRYPTO_has_broken_NEON(void) { return g_has_broken_neon; }
  188. int CRYPTO_needs_hwcap2_workaround(void) { return g_needs_hwcap2_workaround; }
  189. #endif // OPENSSL_ARM && !OPENSSL_STATIC_ARMCAP