sparcv9cap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <setjmp.h>
  5. #include <signal.h>
  6. #include <sys/time.h>
  7. #include <unistd.h>
  8. #include <openssl/bn.h>
  9. #include "sparc_arch.h"
  10. #if defined(__GNUC__) && defined(__linux)
  11. __attribute__ ((visibility("hidden")))
  12. #endif
  13. unsigned int OPENSSL_sparcv9cap_P[2] = { SPARCV9_TICK_PRIVILEGED, 0 };
  14. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  15. const BN_ULONG *np, const BN_ULONG *n0, int num)
  16. {
  17. int bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  18. const BN_ULONG *np, const BN_ULONG *n0, int num);
  19. int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  20. const BN_ULONG *np, const BN_ULONG *n0, int num);
  21. int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  22. const BN_ULONG *np, const BN_ULONG *n0, int num);
  23. if (!(num & 1) && num >= 6) {
  24. if ((num & 15) == 0 && num <= 64 &&
  25. (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
  26. (CFR_MONTMUL | CFR_MONTSQR)) {
  27. typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
  28. const BN_ULONG *bp,
  29. const BN_ULONG *np,
  30. const BN_ULONG *n0);
  31. int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap,
  32. const BN_ULONG *bp, const BN_ULONG *np,
  33. const BN_ULONG *n0);
  34. int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
  35. const BN_ULONG *bp, const BN_ULONG *np,
  36. const BN_ULONG *n0);
  37. int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
  38. const BN_ULONG *bp, const BN_ULONG *np,
  39. const BN_ULONG *n0);
  40. int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
  41. const BN_ULONG *bp, const BN_ULONG *np,
  42. const BN_ULONG *n0);
  43. static const bn_mul_mont_f funcs[4] = {
  44. bn_mul_mont_t4_8, bn_mul_mont_t4_16,
  45. bn_mul_mont_t4_24, bn_mul_mont_t4_32
  46. };
  47. bn_mul_mont_f worker = funcs[num / 16 - 1];
  48. if ((*worker) (rp, ap, bp, np, n0))
  49. return 1;
  50. /* retry once and fall back */
  51. if ((*worker) (rp, ap, bp, np, n0))
  52. return 1;
  53. return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
  54. }
  55. if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3))
  56. return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
  57. else if (num >= 8 &&
  58. (OPENSSL_sparcv9cap_P[0] &
  59. (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
  60. (SPARCV9_PREFER_FPU | SPARCV9_VIS1))
  61. return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
  62. }
  63. return bn_mul_mont_int(rp, ap, bp, np, n0, num);
  64. }
  65. unsigned long _sparcv9_rdtick(void);
  66. void _sparcv9_vis1_probe(void);
  67. unsigned long _sparcv9_vis1_instrument(void);
  68. void _sparcv9_vis2_probe(void);
  69. void _sparcv9_fmadd_probe(void);
  70. unsigned long _sparcv9_rdcfr(void);
  71. void _sparcv9_vis3_probe(void);
  72. unsigned long _sparcv9_random(void);
  73. size_t _sparcv9_vis1_instrument_bus(unsigned int *, size_t);
  74. size_t _sparcv9_vis1_instrument_bus2(unsigned int *, size_t, size_t);
  75. unsigned long OPENSSL_rdtsc(void)
  76. {
  77. if (OPENSSL_sparcv9cap_P[0] & SPARCV9_TICK_PRIVILEGED)
  78. #if defined(__sun) && defined(__SVR4)
  79. return gethrtime();
  80. #else
  81. return 0;
  82. #endif
  83. else
  84. return _sparcv9_rdtick();
  85. }
  86. size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
  87. {
  88. if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
  89. SPARCV9_BLK)
  90. return _sparcv9_vis1_instrument_bus(out, cnt);
  91. else
  92. return 0;
  93. }
  94. size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
  95. {
  96. if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
  97. SPARCV9_BLK)
  98. return _sparcv9_vis1_instrument_bus2(out, cnt, max);
  99. else
  100. return 0;
  101. }
  102. #if 0 && defined(__sun) && defined(__SVR4)
  103. /*
  104. * This code path is disabled, because of incompatibility of libdevinfo.so.1
  105. * and libmalloc.so.1 (see below for details)
  106. */
  107. # include <malloc.h>
  108. # include <dlfcn.h>
  109. # include <libdevinfo.h>
  110. # include <sys/systeminfo.h>
  111. typedef di_node_t(*di_init_t) (const char *, uint_t);
  112. typedef void (*di_fini_t) (di_node_t);
  113. typedef char *(*di_node_name_t) (di_node_t);
  114. typedef int (*di_walk_node_t) (di_node_t, uint_t, di_node_name_t,
  115. int (*)(di_node_t, di_node_name_t));
  116. # define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
  117. static int walk_nodename(di_node_t node, di_node_name_t di_node_name)
  118. {
  119. char *name = (*di_node_name) (node);
  120. /* This is expected to catch all UltraSPARC flavors prior T1 */
  121. if (!strcmp(name, "SUNW,UltraSPARC") ||
  122. /* covers II,III,IV */
  123. !strncmp(name, "SUNW,UltraSPARC-I", 17)) {
  124. OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU | SPARCV9_VIS1;
  125. /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
  126. if (name[14] != '\0' && name[17] != '\0' && name[18] != '\0')
  127. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  128. return DI_WALK_TERMINATE;
  129. }
  130. /* This is expected to catch remaining UltraSPARCs, such as T1 */
  131. else if (!strncmp(name, "SUNW,UltraSPARC", 15)) {
  132. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  133. return DI_WALK_TERMINATE;
  134. }
  135. return DI_WALK_CONTINUE;
  136. }
  137. void OPENSSL_cpuid_setup(void)
  138. {
  139. void *h;
  140. char *e, si[256];
  141. static int trigger = 0;
  142. if (trigger)
  143. return;
  144. trigger = 1;
  145. if ((e = getenv("OPENSSL_sparcv9cap"))) {
  146. OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
  147. return;
  148. }
  149. if (sysinfo(SI_MACHINE, si, sizeof(si)) > 0) {
  150. if (strcmp(si, "sun4v"))
  151. /* FPU is preferred for all CPUs, but US-T1/2 */
  152. OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU;
  153. }
  154. if (sysinfo(SI_ISALIST, si, sizeof(si)) > 0) {
  155. if (strstr(si, "+vis"))
  156. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
  157. if (strstr(si, "+vis2")) {
  158. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
  159. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  160. return;
  161. }
  162. }
  163. # ifdef M_KEEP
  164. /*
  165. * Solaris libdevinfo.so.1 is effectively incomatible with
  166. * libmalloc.so.1. Specifically, if application is linked with
  167. * -lmalloc, it crashes upon startup with SIGSEGV in
  168. * free(3LIBMALLOC) called by di_fini. Prior call to
  169. * mallopt(M_KEEP,0) somehow helps... But not always...
  170. */
  171. if ((h = dlopen(NULL, RTLD_LAZY))) {
  172. union {
  173. void *p;
  174. int (*f) (int, int);
  175. } sym;
  176. if ((sym.p = dlsym(h, "mallopt")))
  177. (*sym.f) (M_KEEP, 0);
  178. dlclose(h);
  179. }
  180. # endif
  181. if ((h = dlopen("libdevinfo.so.1", RTLD_LAZY)))
  182. do {
  183. di_init_t di_init;
  184. di_fini_t di_fini;
  185. di_walk_node_t di_walk_node;
  186. di_node_name_t di_node_name;
  187. di_node_t root_node;
  188. if (!DLLINK(h, di_init))
  189. break;
  190. if (!DLLINK(h, di_fini))
  191. break;
  192. if (!DLLINK(h, di_walk_node))
  193. break;
  194. if (!DLLINK(h, di_node_name))
  195. break;
  196. if ((root_node = (*di_init) ("/", DINFOSUBTREE)) != DI_NODE_NIL) {
  197. (*di_walk_node) (root_node, DI_WALK_SIBFIRST,
  198. di_node_name, walk_nodename);
  199. (*di_fini) (root_node);
  200. }
  201. } while (0);
  202. if (h)
  203. dlclose(h);
  204. }
  205. #else
  206. static sigjmp_buf common_jmp;
  207. static void common_handler(int sig)
  208. {
  209. siglongjmp(common_jmp, sig);
  210. }
  211. #if defined(__sun) && defined(__SVR4)
  212. # if defined(__GNUC__) && __GNUC__>=2
  213. extern unsigned int getisax(unsigned int vec[], unsigned int sz) __attribute__ ((weak));
  214. # elif defined(__SUNPRO_C)
  215. #pragma weak getisax
  216. extern unsigned int getisax(unsigned int vec[], unsigned int sz);
  217. # else
  218. static unsigned int (*getisax) (unsigned int vec[], unsigned int sz) = NULL;
  219. # endif
  220. #endif
  221. void OPENSSL_cpuid_setup(void)
  222. {
  223. char *e;
  224. struct sigaction common_act, ill_oact, bus_oact;
  225. sigset_t all_masked, oset;
  226. static int trigger = 0;
  227. if (trigger)
  228. return;
  229. trigger = 1;
  230. if ((e = getenv("OPENSSL_sparcv9cap"))) {
  231. OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
  232. if ((e = strchr(e, ':')))
  233. OPENSSL_sparcv9cap_P[1] = strtoul(e + 1, NULL, 0);
  234. return;
  235. }
  236. #if defined(__sun) && defined(__SVR4)
  237. if (getisax != NULL) {
  238. unsigned int vec[1];
  239. if (getisax (vec,1)) {
  240. if (vec[0]&0x0020) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1;
  241. if (vec[0]&0x0040) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
  242. if (vec[0]&0x0080) OPENSSL_sparcv9cap_P[0] |= SPARCV9_BLK;
  243. if (vec[0]&0x0100) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
  244. if (vec[0]&0x0400) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
  245. /* reconstruct %cfr copy */
  246. OPENSSL_sparcv9cap_P[1] = (vec[0]>>17)&0x3ff;
  247. OPENSSL_sparcv9cap_P[1] |= (OPENSSL_sparcv9cap_P[1]&CFR_MONTMUL)<<1;
  248. if (vec[0]&0x20000000) OPENSSL_sparcv9cap_P[1] |= CFR_CRC32C;
  249. /* Some heuristics */
  250. /* all known VIS2-capable CPUs have unprivileged tick counter */
  251. if (OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS2)
  252. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  253. OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU;
  254. /* detect UltraSPARC-Tx, see sparccpud.S for details... */
  255. if ((OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS1) &&
  256. _sparcv9_vis1_instrument() >= 12)
  257. OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
  258. }
  259. if (sizeof(size_t) == 8)
  260. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  261. return;
  262. }
  263. #endif
  264. /* Initial value, fits UltraSPARC-I&II... */
  265. OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
  266. sigfillset(&all_masked);
  267. sigdelset(&all_masked, SIGILL);
  268. sigdelset(&all_masked, SIGTRAP);
  269. # ifdef SIGEMT
  270. sigdelset(&all_masked, SIGEMT);
  271. # endif
  272. sigdelset(&all_masked, SIGFPE);
  273. sigdelset(&all_masked, SIGBUS);
  274. sigdelset(&all_masked, SIGSEGV);
  275. sigprocmask(SIG_SETMASK, &all_masked, &oset);
  276. memset(&common_act, 0, sizeof(common_act));
  277. common_act.sa_handler = common_handler;
  278. common_act.sa_mask = all_masked;
  279. sigaction(SIGILL, &common_act, &ill_oact);
  280. sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
  281. * Linux] */
  282. if (sigsetjmp(common_jmp, 1) == 0) {
  283. _sparcv9_rdtick();
  284. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  285. }
  286. if (sigsetjmp(common_jmp, 1) == 0) {
  287. _sparcv9_vis1_probe();
  288. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
  289. /* detect UltraSPARC-Tx, see sparccpud.S for details... */
  290. if (_sparcv9_vis1_instrument() >= 12)
  291. OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
  292. else {
  293. _sparcv9_vis2_probe();
  294. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
  295. }
  296. }
  297. if (sigsetjmp(common_jmp, 1) == 0) {
  298. _sparcv9_fmadd_probe();
  299. OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
  300. }
  301. /*
  302. * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
  303. * because VIS3 defines even integer instructions.
  304. */
  305. if (sigsetjmp(common_jmp, 1) == 0) {
  306. _sparcv9_vis3_probe();
  307. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
  308. }
  309. # if 0 /* was planned at some point but never
  310. * implemented in hardware */
  311. if (sigsetjmp(common_jmp, 1) == 0) {
  312. (void)_sparcv9_random();
  313. OPENSSL_sparcv9cap_P[0] |= SPARCV9_RANDOM;
  314. }
  315. # endif
  316. /*
  317. * In wait for better solution _sparcv9_rdcfr is masked by
  318. * VIS3 flag, because it goes to uninterruptable endless
  319. * loop on UltraSPARC II running Solaris. Things might be
  320. * different on Linux...
  321. */
  322. if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) &&
  323. sigsetjmp(common_jmp, 1) == 0) {
  324. OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
  325. }
  326. sigaction(SIGBUS, &bus_oact, NULL);
  327. sigaction(SIGILL, &ill_oact, NULL);
  328. sigprocmask(SIG_SETMASK, &oset, NULL);
  329. if (sizeof(size_t) == 8)
  330. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  331. # ifdef __linux
  332. else {
  333. int ret = syscall(340);
  334. if (ret >= 0 && ret & 1)
  335. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  336. }
  337. # endif
  338. }
  339. #endif