1#! /usr/bin/env perl
2# Copyright 2012-2020 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
9while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
10open STDOUT,">$output";
11
12$code.=<<___;
13	.text
14
15	.if	.ASSEMBLER_VERSION<7000000
16	.asg	0,__TI_EABI__
17	.endif
18	.if	__TI_EABI__
19	.asg	OPENSSL_rdtsc,_OPENSSL_rdtsc
20	.asg	OPENSSL_cleanse,_OPENSSL_cleanse
21	.asg	CRYPTO_memcmp,_CRYPTO_memcmp
22	.asg	OPENSSL_atomic_add,_OPENSSL_atomic_add
23	.asg	OPENSSL_instrument_bus,_OPENSSL_instrument_bus
24	.asg	OPENSSL_instrument_bus2,_OPENSSL_instrument_bus2
25	.endif
26
27	.asg	B3,RA
28
29	.global	_OPENSSL_rdtsc
30_OPENSSL_rdtsc:
31	.asmfunc
32	B	RA
33	MVC	TSCL,B0
34	MVC	TSCH,B1
35  [!B0]	MVC	B0,TSCL		; start TSC
36	MV	B0,A4
37	MV	B1,A5
38	.endasmfunc
39
40	.global	_OPENSSL_cleanse
41_OPENSSL_cleanse:
42	.asmfunc
43	ZERO	A3:A2
44||	ZERO	B2
45||	SHRU	B4,3,B0		; is length >= 8
46||	ADD	1,A4,B6
47  [!B0]	BNOP	RA
48||	ZERO	A1
49||	ZERO	B1
50   [B0]	MVC	B0,ILC
51||[!B0]	CMPLT	0,B4,A1
52||[!B0]	CMPLT	1,B4,B1
53   [A1]	STB	A2,*A4++[2]
54|| [B1] STB	B2,*B6++[2]
55||[!B0]	CMPLT	2,B4,A1
56||[!B0]	CMPLT	3,B4,B1
57   [A1]	STB	A2,*A4++[2]
58|| [B1] STB	B2,*B6++[2]
59||[!B0]	CMPLT	4,B4,A1
60||[!B0]	CMPLT	5,B4,B1
61   [A1]	STB	A2,*A4++[2]
62|| [B1] STB	B2,*B6++[2]
63||[!B0]	CMPLT	6,B4,A1
64   [A1]	STB	A2,*A4++[2]
65
66	SPLOOP	1
67	STNDW	A3:A2,*A4++
68||	SUB	B4,8,B4
69	SPKERNEL
70
71	MV	B4,B0		; remaining bytes
72||	ADD	1,A4,B6
73||	BNOP	RA
74   [B0]	CMPLT	0,B0,A1
75|| [B0]	CMPLT	1,B0,B1
76   [A1]	STB	A2,*A4++[2]
77|| [B1] STB	B2,*B6++[2]
78|| [B0]	CMPLT	2,B0,A1
79|| [B0]	CMPLT	3,B0,B1
80   [A1]	STB	A2,*A4++[2]
81|| [B1] STB	B2,*B6++[2]
82|| [B0]	CMPLT	4,B0,A1
83|| [B0]	CMPLT	5,B0,B1
84   [A1]	STB	A2,*A4++[2]
85|| [B1] STB	B2,*B6++[2]
86|| [B0]	CMPLT	6,B0,A1
87   [A1]	STB	A2,*A4++[2]
88	.endasmfunc
89
90	.global	_CRYPTO_memcmp
91_CRYPTO_memcmp:
92	.asmfunc
93	MV	A6,B0
94  [!B0]	BNOP	RA
95||[!B0]	ZERO	A4
96   [B0]	MVC	B0,ILC
97|| [B0]	ZERO	A0
98	NOP	4
99
100	SPLOOP	1
101	LDBU	*A4++,A1
102||	LDBU	*B4++,B1
103	NOP	4
104	XOR.L	B1,A1,A2
105	SPKERNEL 1,0
106||	OR.S	A2,A0,A0
107
108	BNOP	RA,3
109	ZERO.L	A4
110  [A0]	MVK	1,A4
111	.endasmfunc
112
113	.global	_OPENSSL_atomic_add
114_OPENSSL_atomic_add:
115	.asmfunc
116	MV	A4,B0
117atomic_add?:
118	LL	*B0,B5
119	NOP	4
120	ADD	B4,B5,B5
121	SL	B5,*B0
122	CMTL	*B0,B1
123	NOP	4
124  [!B1]	B	atomic_add?
125   [B1]	BNOP	RA,4
126	MV	B5,A4
127	.endasmfunc
128
129	.global	_OPENSSL_wipe_cpu
130_OPENSSL_wipe_cpu:
131	.asmfunc
132	ZERO	A0
133||	ZERO	B0
134||	ZERO	A1
135||	ZERO	B1
136	ZERO	A3:A2
137||	MVD	B0,B2
138||	ZERO	A4
139||	ZERO	B4
140||	ZERO	A5
141||	ZERO	B5
142||	BNOP	RA
143	ZERO	A7:A6
144||	ZERO	B7:B6
145||	ZERO	A8
146||	ZERO	B8
147||	ZERO	A9
148||	ZERO	B9
149	ZERO	A17:A16
150||	ZERO	B17:B16
151||	ZERO	A18
152||	ZERO	B18
153||	ZERO	A19
154||	ZERO	B19
155	ZERO	A21:A20
156||	ZERO	B21:B20
157||	ZERO	A22
158||	ZERO	B22
159||	ZERO	A23
160||	ZERO	B23
161	ZERO	A25:A24
162||	ZERO	B25:B24
163||	ZERO	A26
164||	ZERO	B26
165||	ZERO	A27
166||	ZERO	B27
167	ZERO	A29:A28
168||	ZERO	B29:B28
169||	ZERO	A30
170||	ZERO	B30
171||	ZERO	A31
172||	ZERO	B31
173	.endasmfunc
174
175CLFLUSH	.macro	CONTROL,ADDR,LEN
176	B	passthrough?
177||	STW	ADDR,*CONTROL[0]
178	STW	LEN,*CONTROL[1]
179spinlock?:
180	LDW	*CONTROL[1],A0
181	NOP	3
182passthrough?:
183	NOP
184  [A0]	BNOP	spinlock?,5
185	.endm
186
187	.global	_OPENSSL_instrument_bus
188_OPENSSL_instrument_bus:
189	.asmfunc
190	MV	B4,B0			; reassign sizeof(output)
191||	MV	A4,B4			; reassign output
192||	MVK	0x00004030,A3
193	MV	B0,A4			; return value
194||	MVK	1,A1
195||	MVKH	0x01840000,A3		; L1DWIBAR
196	MVC	TSCL,B8			; collect 1st tick
197||	MVK	0x00004010,A5
198	MV	B8,B9			; lasttick = tick
199||	MVK	0,B7			; lastdiff = 0
200||	MVKH	0x01840000,A5		; L2WIBAR
201	CLFLUSH	A3,B4,A1		; write-back and invalidate L1D line
202	CLFLUSH	A5,B4,A1		; write-back and invalidate L2 line
203	LL	*B4,B5
204	NOP	4
205	ADD	B7,B5,B5
206	SL	B5,*B4
207	CMTL	*B4,B1
208	NOP	4
209	STW	B5,*B4
210bus_loop1?:
211	MVC	TSCL,B8
212|| [B0]	SUB	B0,1,B0
213	SUB	B8,B9,B7		; lastdiff = tick - lasttick
214||	MV	B8,B9			; lasttick = tick
215	CLFLUSH	A3,B4,A1		; write-back and invalidate L1D line
216	CLFLUSH	A5,B4,A1		; write-back and invalidate L2 line
217	LL	*B4,B5
218	NOP	4
219	ADD	B7,B5,B5
220	SL	B5,*B4
221	CMTL	*B4,B1
222	STW	B5,*B4			; [!B1] is removed to flatten samples
223||	ADDK	4,B4
224|| [B0]	BNOP	bus_loop1?,5
225
226	BNOP	RA,5
227	.endasmfunc
228
229	.global	_OPENSSL_instrument_bus2
230_OPENSSL_instrument_bus2:
231	.asmfunc
232	MV	A6,B0			; reassign max
233||	MV	B4,A6			; reassign sizeof(output)
234||	MVK	0x00004030,A3
235	MV	A4,B4			; reassign output
236||	MVK	0,A4			; return value
237||	MVK	1,A1
238||	MVKH	0x01840000,A3		; L1DWIBAR
239
240	MVC	TSCL,B8			; collect 1st tick
241||	MVK	0x00004010,A5
242	MV	B8,B9			; lasttick = tick
243||	MVK	0,B7			; lastdiff = 0
244||	MVKH	0x01840000,A5		; L2WIBAR
245	CLFLUSH	A3,B4,A1		; write-back and invalidate L1D line
246	CLFLUSH	A5,B4,A1		; write-back and invalidate L2 line
247	LL	*B4,B5
248	NOP	4
249	ADD	B7,B5,B5
250	SL	B5,*B4
251	CMTL	*B4,B1
252	NOP	4
253	STW	B5,*B4
254
255	MVC	TSCL,B8			; collect 1st diff
256	SUB	B8,B9,B7		; lastdiff = tick - lasttick
257||	MV	B8,B9			; lasttick = tick
258||	SUB	B0,1,B0
259bus_loop2?:
260	CLFLUSH	A3,B4,A1		; write-back and invalidate L1D line
261	CLFLUSH	A5,B4,A1		; write-back and invalidate L2 line
262	LL	*B4,B5
263	NOP	4
264	ADD	B7,B5,B5
265	SL	B5,*B4
266	CMTL	*B4,B1
267	STW	B5,*B4			; [!B1] is removed to flatten samples
268||[!B0]	BNOP	bus_loop2_done?,2
269||	SUB	B0,1,B0
270	MVC	TSCL,B8
271	SUB	B8,B9,B8
272||	MV	B8,B9
273	CMPEQ	B8,B7,B2
274||	MV	B8,B7
275  [!B2]	ADDAW	B4,1,B4
276||[!B2]	ADDK	1,A4
277	CMPEQ	A4,A6,A2
278  [!A2]	BNOP	bus_loop2?,5
279
280bus_loop2_done?:
281	BNOP	RA,5
282	.endasmfunc
283___
284
285print $code;
286close STDOUT or die "error closing STDOUT: $!";
287