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