1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * This file contains bignum implementation code that
29*7c478bd9Sstevel@tonic-gate  * is specific to x86, but which is still more appropriate
30*7c478bd9Sstevel@tonic-gate  * to write in C, rather than assembly language.
31*7c478bd9Sstevel@tonic-gate  * bignum_i386_asm.s does all the assembly language code
32*7c478bd9Sstevel@tonic-gate  * for x86 specific bignum support.  The assembly language
33*7c478bd9Sstevel@tonic-gate  * source file has pure code, no data.  Let the C compiler
34*7c478bd9Sstevel@tonic-gate  * generate what is needed to handle the variations in
35*7c478bd9Sstevel@tonic-gate  * data representation and addressing, for example,
36*7c478bd9Sstevel@tonic-gate  * statically linked vs PIC.
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "bignum.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #if defined(_KERNEL)
42*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/disp.h>
44*7c478bd9Sstevel@tonic-gate #endif
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate extern uint32_t big_mul_set_vec_sse2(uint32_t *, uint32_t *, int, uint32_t);
47*7c478bd9Sstevel@tonic-gate extern uint32_t big_mul_add_vec_sse2(uint32_t *, uint32_t *, int, uint32_t);
48*7c478bd9Sstevel@tonic-gate extern void big_mul_vec_sse2(uint32_t *, uint32_t *, int, uint32_t *, int);
49*7c478bd9Sstevel@tonic-gate extern void big_sqr_vec_sse2(uint32_t *, uint32_t *, int);
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #if defined(MMX_MANAGE)
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate extern uint32_t big_mul_set_vec_sse2_nsv(uint32_t *, uint32_t *, int, uint32_t);
54*7c478bd9Sstevel@tonic-gate extern uint32_t big_mul_add_vec_sse2_nsv(uint32_t *, uint32_t *, int, uint32_t);
55*7c478bd9Sstevel@tonic-gate extern void big_mul_vec_sse2_nsv(uint32_t *, uint32_t *, int, uint32_t *, int);
56*7c478bd9Sstevel@tonic-gate extern void big_sqr_vec_sse2_nsv(uint32_t *, uint32_t *, int);
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #endif
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate extern uint32_t big_mul_set_vec_umul(uint32_t *, uint32_t *, int, uint32_t);
61*7c478bd9Sstevel@tonic-gate extern uint32_t big_mul_add_vec_umul(uint32_t *, uint32_t *, int, uint32_t);
62*7c478bd9Sstevel@tonic-gate extern void big_mul_vec_umul(uint32_t *, uint32_t *, int, uint32_t *, int);
63*7c478bd9Sstevel@tonic-gate extern void big_sqr_vec_umul(uint32_t *, uint32_t *, int);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate extern uint32_t bignum_use_sse2();
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate static void bignum_i386_init();
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate static uint32_t big_mul_set_vec_init(uint32_t *, uint32_t *, int, uint32_t);
70*7c478bd9Sstevel@tonic-gate static uint32_t big_mul_add_vec_init(uint32_t *, uint32_t *, int, uint32_t);
71*7c478bd9Sstevel@tonic-gate static void big_mul_vec_init(uint32_t *, uint32_t *, int, uint32_t *, int);
72*7c478bd9Sstevel@tonic-gate static void big_sqr_vec_init(uint32_t *, uint32_t *, int);
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate uint32_t
75*7c478bd9Sstevel@tonic-gate (*big_mul_set_vec_impl)(uint32_t *r, uint32_t *a, int len, uint32_t digit)
76*7c478bd9Sstevel@tonic-gate 	= &big_mul_set_vec_init;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate uint32_t
79*7c478bd9Sstevel@tonic-gate (*big_mul_add_vec_impl)(uint32_t *r, uint32_t *a, int len, uint32_t digit)
80*7c478bd9Sstevel@tonic-gate 	= &big_mul_add_vec_init;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate void
83*7c478bd9Sstevel@tonic-gate (*big_mul_vec_impl)(uint32_t *r, uint32_t *a, int alen, uint32_t *b, int blen)
84*7c478bd9Sstevel@tonic-gate 	= &big_mul_vec_init;
85*7c478bd9Sstevel@tonic-gate void
86*7c478bd9Sstevel@tonic-gate (*big_sqr_vec_impl)(uint32_t *r, uint32_t *a, int alen)
87*7c478bd9Sstevel@tonic-gate 	= &big_sqr_vec_init;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate static uint32_t
big_mul_set_vec_init(uint32_t * r,uint32_t * a,int len,uint32_t digit)90*7c478bd9Sstevel@tonic-gate big_mul_set_vec_init(uint32_t *r, uint32_t *a, int len, uint32_t digit)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	bignum_i386_init();
93*7c478bd9Sstevel@tonic-gate 	return ((*big_mul_set_vec_impl)(r, a, len, digit));
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate static uint32_t
big_mul_add_vec_init(uint32_t * r,uint32_t * a,int len,uint32_t digit)97*7c478bd9Sstevel@tonic-gate big_mul_add_vec_init(uint32_t *r, uint32_t *a, int len, uint32_t digit)
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate 	bignum_i386_init();
100*7c478bd9Sstevel@tonic-gate 	return ((*big_mul_add_vec_impl)(r, a, len, digit));
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate static void
big_mul_vec_init(uint32_t * r,uint32_t * a,int alen,uint32_t * b,int blen)104*7c478bd9Sstevel@tonic-gate big_mul_vec_init(uint32_t *r, uint32_t *a, int alen, uint32_t *b, int blen)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	bignum_i386_init();
107*7c478bd9Sstevel@tonic-gate 	(*big_mul_vec_impl)(r, a, alen, b, blen);
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate static void
big_sqr_vec_init(uint32_t * r,uint32_t * a,int alen)111*7c478bd9Sstevel@tonic-gate big_sqr_vec_init(uint32_t *r, uint32_t *a, int alen)
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate 	bignum_i386_init();
114*7c478bd9Sstevel@tonic-gate 	(*big_sqr_vec_impl)(r, a, alen);
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate static void
bignum_i386_init()118*7c478bd9Sstevel@tonic-gate bignum_i386_init()
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate 	if (bignum_use_sse2() != 0) {
121*7c478bd9Sstevel@tonic-gate 		big_mul_set_vec_impl = &big_mul_set_vec_sse2;
122*7c478bd9Sstevel@tonic-gate 		big_mul_add_vec_impl = &big_mul_add_vec_sse2;
123*7c478bd9Sstevel@tonic-gate 		big_mul_vec_impl = &big_mul_vec_sse2;
124*7c478bd9Sstevel@tonic-gate 		big_sqr_vec_impl = &big_sqr_vec_sse2;
125*7c478bd9Sstevel@tonic-gate 	} else {
126*7c478bd9Sstevel@tonic-gate 		big_mul_set_vec_impl = &big_mul_set_vec_umul;
127*7c478bd9Sstevel@tonic-gate 		big_mul_add_vec_impl = &big_mul_add_vec_umul;
128*7c478bd9Sstevel@tonic-gate 		big_mul_vec_impl = &big_mul_vec_umul;
129*7c478bd9Sstevel@tonic-gate 		big_sqr_vec_impl = &big_sqr_vec_umul;
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate void
big_mul_vec_umul(uint32_t * r,uint32_t * a,int alen,uint32_t * b,int blen)134*7c478bd9Sstevel@tonic-gate big_mul_vec_umul(uint32_t *r, uint32_t *a, int alen, uint32_t *b, int blen)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate 	int i;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	r[alen] = big_mul_set_vec_umul(r, a, alen, b[0]);
139*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < blen; ++i)
140*7c478bd9Sstevel@tonic-gate 		r[alen + i] = big_mul_add_vec_umul(r+i, a, alen, b[i]);
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate void
big_sqr_vec_umul(uint32_t * r,uint32_t * a,int alen)144*7c478bd9Sstevel@tonic-gate big_sqr_vec_umul(uint32_t *r, uint32_t *a, int alen)
145*7c478bd9Sstevel@tonic-gate {
146*7c478bd9Sstevel@tonic-gate 	int i;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	r[alen] = big_mul_set_vec_umul(r, a, alen, a[0]);
149*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < alen; ++i)
150*7c478bd9Sstevel@tonic-gate 		r[alen + i] = big_mul_add_vec_umul(r+i, a, alen, a[i]);
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate #if defined(_KERNEL)
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate void
kpr_disable()156*7c478bd9Sstevel@tonic-gate kpr_disable()
157*7c478bd9Sstevel@tonic-gate {
158*7c478bd9Sstevel@tonic-gate 	kpreempt_disable();
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate void
kpr_enable()162*7c478bd9Sstevel@tonic-gate kpr_enable()
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	kpreempt_enable();
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate #endif
168