ppccap.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright 2009-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include <unistd.h>
  15. #if defined(__linux) || defined(_AIX)
  16. # include <sys/utsname.h>
  17. #endif
  18. #if defined(_AIX53) /* defined even on post-5.3 */
  19. # include <sys/systemcfg.h>
  20. # if !defined(__power_set)
  21. # define __power_set(a) (_system_configuration.implementation & (a))
  22. # endif
  23. #endif
  24. #if defined(__APPLE__) && defined(__MACH__)
  25. # include <sys/types.h>
  26. # include <sys/sysctl.h>
  27. #endif
  28. #include <openssl/crypto.h>
  29. #include "internal/cryptlib.h"
  30. #include "crypto/ppc_arch.h"
  31. unsigned int OPENSSL_ppccap_P = 0;
  32. static sigset_t all_masked;
  33. static sigjmp_buf ill_jmp;
  34. static void ill_handler(int sig)
  35. {
  36. siglongjmp(ill_jmp, sig);
  37. }
  38. void OPENSSL_fpu_probe(void);
  39. void OPENSSL_ppc64_probe(void);
  40. void OPENSSL_altivec_probe(void);
  41. void OPENSSL_crypto207_probe(void);
  42. void OPENSSL_madd300_probe(void);
  43. void OPENSSL_brd31_probe(void);
  44. long OPENSSL_rdtsc_mftb(void);
  45. long OPENSSL_rdtsc_mfspr268(void);
  46. uint32_t OPENSSL_rdtsc(void)
  47. {
  48. if (OPENSSL_ppccap_P & PPC_MFTB)
  49. return OPENSSL_rdtsc_mftb();
  50. else if (OPENSSL_ppccap_P & PPC_MFSPR268)
  51. return OPENSSL_rdtsc_mfspr268();
  52. else
  53. return 0;
  54. }
  55. size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
  56. size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
  57. size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
  58. {
  59. if (OPENSSL_ppccap_P & PPC_MFTB)
  60. return OPENSSL_instrument_bus_mftb(out, cnt);
  61. else if (OPENSSL_ppccap_P & PPC_MFSPR268)
  62. return OPENSSL_instrument_bus_mfspr268(out, cnt);
  63. else
  64. return 0;
  65. }
  66. size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
  67. size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
  68. size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
  69. {
  70. if (OPENSSL_ppccap_P & PPC_MFTB)
  71. return OPENSSL_instrument_bus2_mftb(out, cnt, max);
  72. else if (OPENSSL_ppccap_P & PPC_MFSPR268)
  73. return OPENSSL_instrument_bus2_mfspr268(out, cnt, max);
  74. else
  75. return 0;
  76. }
  77. #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
  78. # if __GLIBC_PREREQ(2, 16)
  79. # include <sys/auxv.h>
  80. # define OSSL_IMPLEMENT_GETAUXVAL
  81. # elif defined(__ANDROID_API__)
  82. /* see https://developer.android.google.cn/ndk/guides/cpu-features */
  83. # if __ANDROID_API__ >= 18
  84. # include <sys/auxv.h>
  85. # define OSSL_IMPLEMENT_GETAUXVAL
  86. # endif
  87. # endif
  88. #endif
  89. #if defined(__FreeBSD__)
  90. # include <sys/param.h>
  91. # if __FreeBSD_version >= 1200000
  92. # include <sys/auxv.h>
  93. # define OSSL_IMPLEMENT_GETAUXVAL
  94. static unsigned long getauxval(unsigned long key)
  95. {
  96. unsigned long val = 0ul;
  97. if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
  98. return 0ul;
  99. return val;
  100. }
  101. # endif
  102. #endif
  103. /* I wish <sys/auxv.h> was universally available */
  104. #ifndef AT_HWCAP
  105. # define AT_HWCAP 16 /* AT_HWCAP */
  106. #endif
  107. #define HWCAP_PPC64 (1U << 30)
  108. #define HWCAP_ALTIVEC (1U << 28)
  109. #define HWCAP_FPU (1U << 27)
  110. #define HWCAP_POWER6_EXT (1U << 9)
  111. #define HWCAP_VSX (1U << 7)
  112. #ifndef AT_HWCAP2
  113. # define AT_HWCAP2 26 /* AT_HWCAP2 */
  114. #endif
  115. #define HWCAP_VEC_CRYPTO (1U << 25)
  116. #define HWCAP_ARCH_3_00 (1U << 23)
  117. #define HWCAP_ARCH_3_1 (1U << 18)
  118. # if defined(__GNUC__) && __GNUC__>=2
  119. __attribute__ ((constructor))
  120. # endif
  121. void OPENSSL_cpuid_setup(void)
  122. {
  123. char *e;
  124. struct sigaction ill_oact, ill_act;
  125. sigset_t oset;
  126. static int trigger = 0;
  127. if (trigger)
  128. return;
  129. trigger = 1;
  130. if ((e = getenv("OPENSSL_ppccap"))) {
  131. OPENSSL_ppccap_P = strtoul(e, NULL, 0);
  132. return;
  133. }
  134. OPENSSL_ppccap_P = 0;
  135. #if defined(_AIX)
  136. OPENSSL_ppccap_P |= PPC_FPU;
  137. if (sizeof(size_t) == 4) {
  138. struct utsname uts;
  139. # if defined(_SC_AIX_KERNEL_BITMODE)
  140. if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
  141. return;
  142. # endif
  143. if (uname(&uts) != 0 || atoi(uts.version) < 6)
  144. return;
  145. }
  146. # if defined(__power_set)
  147. /*
  148. * Value used in __power_set is a single-bit 1<<n one denoting
  149. * specific processor class. Incidentally 0xffffffff<<n can be
  150. * used to denote specific processor and its successors.
  151. */
  152. if (sizeof(size_t) == 4) {
  153. /* In 32-bit case PPC_FPU64 is always fastest [if option] */
  154. if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
  155. OPENSSL_ppccap_P |= PPC_FPU64;
  156. } else {
  157. /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
  158. if (__power_set(0x1U<<14)) /* POWER6 */
  159. OPENSSL_ppccap_P |= PPC_FPU64;
  160. }
  161. if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
  162. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  163. if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
  164. OPENSSL_ppccap_P |= PPC_CRYPTO207;
  165. if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
  166. OPENSSL_ppccap_P |= PPC_MADD300;
  167. if (__power_set(0xffffffffU<<18)) /* POWER10 and later */
  168. OPENSSL_ppccap_P |= PPC_BRD31;
  169. return;
  170. # endif
  171. #endif
  172. #if defined(__APPLE__) && defined(__MACH__)
  173. OPENSSL_ppccap_P |= PPC_FPU;
  174. {
  175. int val;
  176. size_t len = sizeof(val);
  177. if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
  178. if (val)
  179. OPENSSL_ppccap_P |= PPC_FPU64;
  180. }
  181. len = sizeof(val);
  182. if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
  183. if (val)
  184. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  185. }
  186. return;
  187. }
  188. #endif
  189. #ifdef OSSL_IMPLEMENT_GETAUXVAL
  190. {
  191. unsigned long hwcap = getauxval(AT_HWCAP);
  192. unsigned long hwcap2 = getauxval(AT_HWCAP2);
  193. if (hwcap & HWCAP_FPU) {
  194. OPENSSL_ppccap_P |= PPC_FPU;
  195. if (sizeof(size_t) == 4) {
  196. /* In 32-bit case PPC_FPU64 is always fastest [if option] */
  197. if (hwcap & HWCAP_PPC64)
  198. OPENSSL_ppccap_P |= PPC_FPU64;
  199. } else {
  200. /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
  201. if (hwcap & HWCAP_POWER6_EXT)
  202. OPENSSL_ppccap_P |= PPC_FPU64;
  203. }
  204. }
  205. if (hwcap & HWCAP_ALTIVEC) {
  206. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  207. if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
  208. OPENSSL_ppccap_P |= PPC_CRYPTO207;
  209. }
  210. if (hwcap2 & HWCAP_ARCH_3_00) {
  211. OPENSSL_ppccap_P |= PPC_MADD300;
  212. }
  213. if (hwcap2 & HWCAP_ARCH_3_1) {
  214. OPENSSL_ppccap_P |= PPC_BRD31;
  215. }
  216. }
  217. #endif
  218. sigfillset(&all_masked);
  219. sigdelset(&all_masked, SIGILL);
  220. sigdelset(&all_masked, SIGTRAP);
  221. #ifdef SIGEMT
  222. sigdelset(&all_masked, SIGEMT);
  223. #endif
  224. sigdelset(&all_masked, SIGFPE);
  225. sigdelset(&all_masked, SIGBUS);
  226. sigdelset(&all_masked, SIGSEGV);
  227. memset(&ill_act, 0, sizeof(ill_act));
  228. ill_act.sa_handler = ill_handler;
  229. ill_act.sa_mask = all_masked;
  230. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  231. sigaction(SIGILL, &ill_act, &ill_oact);
  232. #ifndef OSSL_IMPLEMENT_GETAUXVAL
  233. if (sigsetjmp(ill_jmp, 1) == 0) {
  234. OPENSSL_fpu_probe();
  235. OPENSSL_ppccap_P |= PPC_FPU;
  236. if (sizeof(size_t) == 4) {
  237. # ifdef __linux
  238. struct utsname uts;
  239. if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
  240. # endif
  241. if (sigsetjmp(ill_jmp, 1) == 0) {
  242. OPENSSL_ppc64_probe();
  243. OPENSSL_ppccap_P |= PPC_FPU64;
  244. }
  245. } else {
  246. /*
  247. * Wanted code detecting POWER6 CPU and setting PPC_FPU64
  248. */
  249. }
  250. }
  251. if (sigsetjmp(ill_jmp, 1) == 0) {
  252. OPENSSL_altivec_probe();
  253. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  254. if (sigsetjmp(ill_jmp, 1) == 0) {
  255. OPENSSL_crypto207_probe();
  256. OPENSSL_ppccap_P |= PPC_CRYPTO207;
  257. }
  258. }
  259. if (sigsetjmp(ill_jmp, 1) == 0) {
  260. OPENSSL_madd300_probe();
  261. OPENSSL_ppccap_P |= PPC_MADD300;
  262. }
  263. #endif
  264. if (sigsetjmp(ill_jmp, 1) == 0) {
  265. OPENSSL_rdtsc_mftb();
  266. OPENSSL_ppccap_P |= PPC_MFTB;
  267. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  268. OPENSSL_rdtsc_mfspr268();
  269. OPENSSL_ppccap_P |= PPC_MFSPR268;
  270. }
  271. sigaction(SIGILL, &ill_oact, NULL);
  272. sigprocmask(SIG_SETMASK, &oset, NULL);
  273. }