sparcv9cap.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright 2005-2021 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 <sys/time.h>
  15. #include <unistd.h>
  16. #include <openssl/bn.h>
  17. #include "internal/cryptlib.h"
  18. #include "crypto/sparc_arch.h"
  19. #if defined(__GNUC__) && defined(__linux)
  20. __attribute__ ((visibility("hidden")))
  21. #endif
  22. unsigned int OPENSSL_sparcv9cap_P[2] = { SPARCV9_TICK_PRIVILEGED, 0 };
  23. unsigned long _sparcv9_rdtick(void);
  24. void _sparcv9_vis1_probe(void);
  25. unsigned long _sparcv9_vis1_instrument(void);
  26. void _sparcv9_vis2_probe(void);
  27. void _sparcv9_fmadd_probe(void);
  28. unsigned long _sparcv9_rdcfr(void);
  29. void _sparcv9_vis3_probe(void);
  30. void _sparcv9_fjaesx_probe(void);
  31. unsigned long _sparcv9_random(void);
  32. size_t _sparcv9_vis1_instrument_bus(unsigned int *, size_t);
  33. size_t _sparcv9_vis1_instrument_bus2(unsigned int *, size_t, size_t);
  34. uint32_t OPENSSL_rdtsc(void)
  35. {
  36. if (OPENSSL_sparcv9cap_P[0] & SPARCV9_TICK_PRIVILEGED)
  37. #if defined(__sun) && defined(__SVR4)
  38. return gethrtime();
  39. #else
  40. return 0;
  41. #endif
  42. else
  43. return _sparcv9_rdtick();
  44. }
  45. size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
  46. {
  47. if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
  48. SPARCV9_BLK)
  49. return _sparcv9_vis1_instrument_bus(out, cnt);
  50. else
  51. return 0;
  52. }
  53. size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
  54. {
  55. if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
  56. SPARCV9_BLK)
  57. return _sparcv9_vis1_instrument_bus2(out, cnt, max);
  58. else
  59. return 0;
  60. }
  61. static sigjmp_buf common_jmp;
  62. static void common_handler(int sig)
  63. {
  64. siglongjmp(common_jmp, sig);
  65. }
  66. #if defined(__sun) && defined(__SVR4)
  67. # if defined(__GNUC__) && __GNUC__>=2
  68. extern unsigned int getisax(unsigned int vec[], unsigned int sz) __attribute__ ((weak));
  69. # elif defined(__SUNPRO_C)
  70. #pragma weak getisax
  71. extern unsigned int getisax(unsigned int vec[], unsigned int sz);
  72. # else
  73. static unsigned int (*getisax) (unsigned int vec[], unsigned int sz) = NULL;
  74. # endif
  75. #endif
  76. void OPENSSL_cpuid_setup(void)
  77. {
  78. char *e;
  79. struct sigaction common_act, ill_oact, bus_oact;
  80. sigset_t all_masked, oset;
  81. static int trigger = 0;
  82. if (trigger)
  83. return;
  84. trigger = 1;
  85. if ((e = getenv("OPENSSL_sparcv9cap"))) {
  86. OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
  87. if ((e = strchr(e, ':')))
  88. OPENSSL_sparcv9cap_P[1] = strtoul(e + 1, NULL, 0);
  89. return;
  90. }
  91. #if defined(__sun) && defined(__SVR4)
  92. if (getisax != NULL) {
  93. unsigned int vec[2] = { 0, 0 };
  94. if (getisax (vec,2)) {
  95. if (vec[0]&0x00020) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1;
  96. if (vec[0]&0x00040) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
  97. if (vec[0]&0x00080) OPENSSL_sparcv9cap_P[0] |= SPARCV9_BLK;
  98. if (vec[0]&0x00100) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
  99. if (vec[0]&0x00400) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
  100. if (vec[0]&0x01000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJHPCACE;
  101. if (vec[0]&0x02000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJDESX;
  102. if (vec[0]&0x08000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_IMA;
  103. if (vec[0]&0x10000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
  104. if (vec[1]&0x00008) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS4;
  105. /* reconstruct %cfr copy */
  106. OPENSSL_sparcv9cap_P[1] = (vec[0]>>17)&0x3ff;
  107. OPENSSL_sparcv9cap_P[1] |= (OPENSSL_sparcv9cap_P[1]&CFR_MONTMUL)<<1;
  108. if (vec[0]&0x20000000) OPENSSL_sparcv9cap_P[1] |= CFR_CRC32C;
  109. if (vec[1]&0x00000020) OPENSSL_sparcv9cap_P[1] |= CFR_XMPMUL;
  110. if (vec[1]&0x00000040)
  111. OPENSSL_sparcv9cap_P[1] |= CFR_XMONTMUL|CFR_XMONTSQR;
  112. /* Some heuristics */
  113. /* all known VIS2-capable CPUs have unprivileged tick counter */
  114. if (OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS2)
  115. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  116. OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU;
  117. /* detect UltraSPARC-Tx, see sparccpud.S for details... */
  118. if ((OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS1) &&
  119. _sparcv9_vis1_instrument() >= 12)
  120. OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
  121. }
  122. if (sizeof(size_t) == 8)
  123. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  124. return;
  125. }
  126. #endif
  127. /* Initial value, fits UltraSPARC-I&II... */
  128. OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
  129. sigfillset(&all_masked);
  130. sigdelset(&all_masked, SIGILL);
  131. sigdelset(&all_masked, SIGTRAP);
  132. # ifdef SIGEMT
  133. sigdelset(&all_masked, SIGEMT);
  134. # endif
  135. sigdelset(&all_masked, SIGFPE);
  136. sigdelset(&all_masked, SIGBUS);
  137. sigdelset(&all_masked, SIGSEGV);
  138. sigprocmask(SIG_SETMASK, &all_masked, &oset);
  139. memset(&common_act, 0, sizeof(common_act));
  140. common_act.sa_handler = common_handler;
  141. common_act.sa_mask = all_masked;
  142. sigaction(SIGILL, &common_act, &ill_oact);
  143. sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
  144. * Linux] */
  145. if (sigsetjmp(common_jmp, 1) == 0) {
  146. _sparcv9_rdtick();
  147. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  148. }
  149. if (sigsetjmp(common_jmp, 1) == 0) {
  150. _sparcv9_vis1_probe();
  151. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
  152. /* detect UltraSPARC-Tx, see sparccpud.S for details... */
  153. if (_sparcv9_vis1_instrument() >= 12)
  154. OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
  155. else {
  156. _sparcv9_vis2_probe();
  157. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
  158. }
  159. }
  160. if (sigsetjmp(common_jmp, 1) == 0) {
  161. _sparcv9_fmadd_probe();
  162. OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
  163. }
  164. /*
  165. * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
  166. * because VIS3 defines even integer instructions.
  167. */
  168. if (sigsetjmp(common_jmp, 1) == 0) {
  169. _sparcv9_vis3_probe();
  170. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
  171. }
  172. if (sigsetjmp(common_jmp, 1) == 0) {
  173. _sparcv9_fjaesx_probe();
  174. OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
  175. }
  176. /*
  177. * In wait for better solution _sparcv9_rdcfr is masked by
  178. * VIS3 flag, because it goes to uninterruptible endless
  179. * loop on UltraSPARC II running Solaris. Things might be
  180. * different on Linux...
  181. */
  182. if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) &&
  183. sigsetjmp(common_jmp, 1) == 0) {
  184. OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
  185. }
  186. sigaction(SIGBUS, &bus_oact, NULL);
  187. sigaction(SIGILL, &ill_oact, NULL);
  188. sigprocmask(SIG_SETMASK, &oset, NULL);
  189. if (sizeof(size_t) == 8)
  190. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  191. # ifdef __linux
  192. else {
  193. int ret = syscall(340);
  194. if (ret >= 0 && ret & 1)
  195. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  196. }
  197. # endif
  198. }