1#! /usr/bin/env perl
2# Copyright 2015-2023 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
9
10# $output is the last argument if it looks like a file (it has an extension)
11# $flavour is the first argument if it doesn't look like a file
12$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
13$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
14
15$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
16( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
17( $xlate="${dir}perlasm/arm-xlate.pl" and -f $xlate) or
18die "can't locate arm-xlate.pl";
19
20open OUT,"| \"$^X\" $xlate $flavour \"$output\""
21    or die "can't call $xlate: $!";
22*STDOUT=*OUT;
23
24$code.=<<___;
25#include "arm_arch.h"
26
27#if defined(__thumb2__) && !defined(__APPLE__)
28.syntax	unified
29.thumb
30#else
31.code	32
32#undef	__thumb2__
33#endif
34
35.text
36
37.align	5
38.global	OPENSSL_atomic_add
39.type	OPENSSL_atomic_add,%function
40OPENSSL_atomic_add:
41#if __ARM_ARCH__>=6
42.Ladd:	ldrex	r2,[r0]
43	add	r3,r2,r1
44	strex	r2,r3,[r0]
45	cmp	r2,#0
46	bne	.Ladd
47	mov	r0,r3
48	bx	lr
49#else
50	stmdb	sp!,{r4-r6,lr}
51	ldr	r2,.Lspinlock
52	adr	r3,.Lspinlock
53	mov	r4,r0
54	mov	r5,r1
55	add	r6,r3,r2	@ &spinlock
56	b	.+8
57.Lspin:	bl	sched_yield
58	mov	r0,#-1
59	swp	r0,r0,[r6]
60	cmp	r0,#0
61	bne	.Lspin
62
63	ldr	r2,[r4]
64	add	r2,r2,r5
65	str	r2,[r4]
66	str	r0,[r6]		@ release spinlock
67	ldmia	sp!,{r4-r6,lr}
68	tst	lr,#1
69	moveq	pc,lr
70	.word	0xe12fff1e	@ bx	lr
71#endif
72.size	OPENSSL_atomic_add,.-OPENSSL_atomic_add
73
74.global	OPENSSL_cleanse
75.type	OPENSSL_cleanse,%function
76OPENSSL_cleanse:
77	eor	ip,ip,ip
78	cmp	r1,#7
79#ifdef	__thumb2__
80	itt	hs
81#endif
82	subhs	r1,r1,#4
83	bhs	.Lot
84	cmp	r1,#0
85	beq	.Lcleanse_done
86.Little:
87	strb	ip,[r0],#1
88	subs	r1,r1,#1
89	bhi	.Little
90	b	.Lcleanse_done
91
92.Lot:	tst	r0,#3
93	beq	.Laligned
94	strb	ip,[r0],#1
95	sub	r1,r1,#1
96	b	.Lot
97.Laligned:
98	str	ip,[r0],#4
99	subs	r1,r1,#4
100	bhs	.Laligned
101	adds	r1,r1,#4
102	bne	.Little
103.Lcleanse_done:
104#if __ARM_ARCH__>=5
105	bx	lr
106#else
107	tst	lr,#1
108	moveq	pc,lr
109	.word	0xe12fff1e	@ bx	lr
110#endif
111.size	OPENSSL_cleanse,.-OPENSSL_cleanse
112
113.global	CRYPTO_memcmp
114.type	CRYPTO_memcmp,%function
115.align	4
116CRYPTO_memcmp:
117	eor	ip,ip,ip
118	cmp	r2,#0
119	beq	.Lno_data
120	stmdb	sp!,{r4,r5}
121
122.Loop_cmp:
123	ldrb	r4,[r0],#1
124	ldrb	r5,[r1],#1
125	eor	r4,r4,r5
126	orr	ip,ip,r4
127	subs	r2,r2,#1
128	bne	.Loop_cmp
129
130	ldmia	sp!,{r4,r5}
131.Lno_data:
132	rsb	r0,ip,#0
133	mov	r0,r0,lsr#31
134#if __ARM_ARCH__>=5
135	bx	lr
136#else
137	tst	lr,#1
138	moveq	pc,lr
139	.word	0xe12fff1e	@ bx	lr
140#endif
141.size	CRYPTO_memcmp,.-CRYPTO_memcmp
142
143#if __ARM_MAX_ARCH__>=7
144.arch	armv7-a
145.fpu	neon
146
147.align	5
148.global	_armv7_neon_probe
149.type	_armv7_neon_probe,%function
150_armv7_neon_probe:
151	vorr	q0,q0,q0
152	bx	lr
153.size	_armv7_neon_probe,.-_armv7_neon_probe
154
155.global	_armv7_tick
156.type	_armv7_tick,%function
157_armv7_tick:
158#ifdef	__APPLE__
159	mrrc	p15,0,r0,r1,c14		@ CNTPCT
160#else
161	mrrc	p15,1,r0,r1,c14		@ CNTVCT
162#endif
163	bx	lr
164.size	_armv7_tick,.-_armv7_tick
165
166.global	_armv8_aes_probe
167.type	_armv8_aes_probe,%function
168_armv8_aes_probe:
169#if defined(__thumb2__) && !defined(__APPLE__)
170	.byte	0xb0,0xff,0x00,0x03	@ aese.8	q0,q0
171#else
172	.byte	0x00,0x03,0xb0,0xf3	@ aese.8	q0,q0
173#endif
174	bx	lr
175.size	_armv8_aes_probe,.-_armv8_aes_probe
176
177.global	_armv8_sha1_probe
178.type	_armv8_sha1_probe,%function
179_armv8_sha1_probe:
180#if defined(__thumb2__) && !defined(__APPLE__)
181	.byte	0x00,0xef,0x40,0x0c	@ sha1c.32	q0,q0,q0
182#else
183	.byte	0x40,0x0c,0x00,0xf2	@ sha1c.32	q0,q0,q0
184#endif
185	bx	lr
186.size	_armv8_sha1_probe,.-_armv8_sha1_probe
187
188.global	_armv8_sha256_probe
189.type	_armv8_sha256_probe,%function
190_armv8_sha256_probe:
191#if defined(__thumb2__) && !defined(__APPLE__)
192	.byte	0x00,0xff,0x40,0x0c	@ sha256h.32	q0,q0,q0
193#else
194	.byte	0x40,0x0c,0x00,0xf3	@ sha256h.32	q0,q0,q0
195#endif
196	bx	lr
197.size	_armv8_sha256_probe,.-_armv8_sha256_probe
198.global	_armv8_pmull_probe
199.type	_armv8_pmull_probe,%function
200_armv8_pmull_probe:
201#if defined(__thumb2__) && !defined(__APPLE__)
202	.byte	0xa0,0xef,0x00,0x0e	@ vmull.p64	q0,d0,d0
203#else
204	.byte	0x00,0x0e,0xa0,0xf2	@ vmull.p64	q0,d0,d0
205#endif
206	bx	lr
207.size	_armv8_pmull_probe,.-_armv8_pmull_probe
208#endif
209
210.global	OPENSSL_instrument_bus
211.type	OPENSSL_instrument_bus,%function
212OPENSSL_instrument_bus:
213	eor	r0,r0,r0
214#if __ARM_ARCH__>=5
215	bx	lr
216#else
217	tst	lr,#1
218	moveq	pc,lr
219	.word	0xe12fff1e	@ bx	lr
220#endif
221.size	OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
222
223.global	OPENSSL_instrument_bus2
224.type	OPENSSL_instrument_bus2,%function
225OPENSSL_instrument_bus2:
226	eor	r0,r0,r0
227#if __ARM_ARCH__>=5
228	bx	lr
229#else
230	tst	lr,#1
231	moveq	pc,lr
232	.word	0xe12fff1e	@ bx	lr
233#endif
234.size	OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
235
236.align	5
237#if __ARM_MAX_ARCH__>=7
238.LOPENSSL_armcap:
239.word	OPENSSL_armcap_P-.
240#endif
241#if __ARM_ARCH__>=6
242.align	5
243#else
244.Lspinlock:
245.word	atomic_add_spinlock-.Lspinlock
246.align	5
247
248.data
249.align	2
250atomic_add_spinlock:
251.word	0
252#endif
253
254.extern	OPENSSL_armcap_P
255.hidden	OPENSSL_armcap_P
256___
257
258print $code;
259close STDOUT or die "error closing STDOUT: $!";
260