1221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <stdio.h>
2221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <stdlib.h>
3221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <string.h>
443c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom#include <setjmp.h>
543c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom#include <signal.h>
6221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <sys/time.h>
7221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/bn.h>
8221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
9221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#define SPARCV9_TICK_PRIVILEGED	(1<<0)
10221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#define SPARCV9_PREFER_FPU	(1<<1)
11221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#define SPARCV9_VIS1		(1<<2)
12221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#define SPARCV9_VIS2		(1<<3)	/* reserved */
13221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#define SPARCV9_FMADD		(1<<4)	/* reserved for SPARC64 V */
1443c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom
15221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int OPENSSL_sparcv9cap_P=SPARCV9_TICK_PRIVILEGED;
16221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
17221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromint bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num)
18221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
19221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num);
20221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num);
21221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
22392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom	if (num>=8 && !(num&1) &&
23392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom	    (OPENSSL_sparcv9cap_P&(SPARCV9_PREFER_FPU|SPARCV9_VIS1)) ==
24221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		(SPARCV9_PREFER_FPU|SPARCV9_VIS1))
25221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return bn_mul_mont_fpu(rp,ap,bp,np,n0,num);
26221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else
27221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return bn_mul_mont_int(rp,ap,bp,np,n0,num);
28221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
29221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
3043c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstromunsigned long	_sparcv9_rdtick(void);
3143c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstromvoid		_sparcv9_vis1_probe(void);
3243c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstromunsigned long	_sparcv9_vis1_instrument(void);
3343c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstromvoid		_sparcv9_vis2_probe(void);
3443c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstromvoid		_sparcv9_fmadd_probe(void);
3543c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom
36221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromunsigned long OPENSSL_rdtsc(void)
37221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
38221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (OPENSSL_sparcv9cap_P&SPARCV9_TICK_PRIVILEGED)
39221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#if defined(__sun) && defined(__SVR4)
40221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return gethrtime();
41221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#else
42221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
43221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#endif
44221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else
45221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return _sparcv9_rdtick();
46221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
47221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
4843c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom#if 0 && defined(__sun) && defined(__SVR4)
4943c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom/* This code path is disabled, because of incompatibility of
5043c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom * libdevinfo.so.1 and libmalloc.so.1 (see below for details)
5143c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom */
5243c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom#include <malloc.h>
53221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <dlfcn.h>
54221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <libdevinfo.h>
55221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <sys/systeminfo.h>
56221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
57221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromtypedef di_node_t (*di_init_t)(const char *,uint_t);
58221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromtypedef void      (*di_fini_t)(di_node_t);
59221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromtypedef char *    (*di_node_name_t)(di_node_t);
60221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromtypedef int       (*di_walk_node_t)(di_node_t,uint_t,di_node_name_t,int (*)(di_node_t,di_node_name_t));
61221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
62221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
63221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
64221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int walk_nodename(di_node_t node, di_node_name_t di_node_name)
65221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
66221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	char *name = (*di_node_name)(node);
67221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
68221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* This is expected to catch all UltraSPARC flavors prior T1 */
69221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!strcmp (name,"SUNW,UltraSPARC") ||
70221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	    !strncmp(name,"SUNW,UltraSPARC-I",17))  /* covers II,III,IV */
71221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
72221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1;
73221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
74221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		/* %tick is privileged only on UltraSPARC-I/II, but not IIe */
75221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (name[14]!='\0' && name[17]!='\0' && name[18]!='\0')
76221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
77221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
78221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return DI_WALK_TERMINATE;
79221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
80221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* This is expected to catch remaining UltraSPARCs, such as T1 */
81221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else if (!strncmp(name,"SUNW,UltraSPARC",15))
82221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
83221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
84221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
85221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return DI_WALK_TERMINATE;
86221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
87221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
88221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return DI_WALK_CONTINUE;
89221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
90221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
91221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromvoid OPENSSL_cpuid_setup(void)
92221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
93221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	void *h;
94221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	char *e,si[256];
95221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	static int trigger=0;
96221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
97221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (trigger) return;
98221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	trigger=1;
99221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
100221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if ((e=getenv("OPENSSL_sparcv9cap")))
101221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
102221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
103221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return;
104221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
105221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
106221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (sysinfo(SI_MACHINE,si,sizeof(si))>0)
107221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
108221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (strcmp(si,"sun4v"))
109221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			/* FPU is preferred for all CPUs, but US-T1/2 */
110221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU;
111221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
112221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
113221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (sysinfo(SI_ISALIST,si,sizeof(si))>0)
114221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
115221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (strstr(si,"+vis"))
116221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
117221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (strstr(si,"+vis2"))
118221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
119221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
120221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
121221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return;
122221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
123221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
12443c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom#ifdef M_KEEP
12543c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom	/*
12643c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom	 * Solaris libdevinfo.so.1 is effectively incomatible with
12743c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom	 * libmalloc.so.1. Specifically, if application is linked with
12843c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom	 * -lmalloc, it crashes upon startup with SIGSEGV in
12943c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom	 * free(3LIBMALLOC) called by di_fini. Prior call to
13043c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom	 * mallopt(M_KEEP,0) somehow helps... But not always...
13143c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom	 */
13243c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom	if ((h = dlopen(NULL,RTLD_LAZY)))
13343c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom		{
13443c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom		union { void *p; int (*f)(int,int); } sym;
13543c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom		if ((sym.p = dlsym(h,"mallopt"))) (*sym.f)(M_KEEP,0);
13643c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom		dlclose(h);
13743c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom		}
13843c12e3d4f9bbbbd4a8ba7b149686437514bc6b6Brian Carlstrom#endif
139221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if ((h = dlopen("libdevinfo.so.1",RTLD_LAZY))) do
140221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
141221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		di_init_t	di_init;
142221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		di_fini_t	di_fini;
143221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		di_walk_node_t	di_walk_node;
144221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		di_node_name_t	di_node_name;
145221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		di_node_t	root_node;
146221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
147221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (!DLLINK(h,di_init))		break;
148221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (!DLLINK(h,di_fini))		break;
149221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (!DLLINK(h,di_walk_node))	break;
150221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (!DLLINK(h,di_node_name))	break;
151221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
152221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if ((root_node = (*di_init)("/",DINFOSUBTREE))!=DI_NODE_NIL)
153221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
154221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			(*di_walk_node)(root_node,DI_WALK_SIBFIRST,
155221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					di_node_name,walk_nodename);
156