crypto/ppccap.c: permit build with no-chacha and no-poly1305.
[openssl.git] / crypto / ppccpuid.pl
1 #! /usr/bin/env perl
2 # Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (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 $flavour = shift;
11
12 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
13 ( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or
14 ( $xlate="${dir}perlasm/ppc-xlate.pl" and -f $xlate) or
15 die "can't locate ppc-xlate.pl";
16
17 open STDOUT,"| $^X $xlate $flavour ".shift || die "can't call $xlate: $!";
18
19 if ($flavour=~/64/) {
20     $CMPLI="cmpldi";
21     $SHRLI="srdi";
22     $SIGNX="extsw";
23 } else {
24     $CMPLI="cmplwi";
25     $SHRLI="srwi";
26     $SIGNX="mr";
27 }
28
29 $code=<<___;
30 .machine        "any"
31 .text
32
33 .globl  .OPENSSL_fpu_probe
34 .align  4
35 .OPENSSL_fpu_probe:
36         fmr     f0,f0
37         blr
38         .long   0
39         .byte   0,12,0x14,0,0,0,0,0
40 .size   .OPENSSL_fpu_probe,.-.OPENSSL_fpu_probe
41 .globl  .OPENSSL_ppc64_probe
42 .align  4
43 .OPENSSL_ppc64_probe:
44         fcfid   f1,f1
45         extrdi  r0,r0,32,0
46         blr
47         .long   0
48         .byte   0,12,0x14,0,0,0,0,0
49 .size   .OPENSSL_ppc64_probe,.-.OPENSSL_ppc64_probe
50
51 .globl  .OPENSSL_altivec_probe
52 .align  4
53 .OPENSSL_altivec_probe:
54         .long   0x10000484      # vor   v0,v0,v0
55         blr
56         .long   0
57         .byte   0,12,0x14,0,0,0,0,0
58 .size   .OPENSSL_altivec_probe,.-..OPENSSL_altivec_probe
59
60 .globl  .OPENSSL_crypto207_probe
61 .align  4
62 .OPENSSL_crypto207_probe:
63         lvx_u   v0,0,r1
64         vcipher v0,v0,v0
65         blr
66         .long   0
67         .byte   0,12,0x14,0,0,0,0,0
68 .size   .OPENSSL_crypto207_probe,.-.OPENSSL_crypto207_probe
69
70 .globl  .OPENSSL_madd300_probe
71 .align  4
72 .OPENSSL_madd300_probe:
73         xor     r0,r0,r0
74         maddld  r3,r0,r0,r0
75         maddhdu r3,r0,r0,r0
76         blr
77         .long   0
78         .byte   0,12,0x14,0,0,0,0,0
79
80 .globl  .OPENSSL_wipe_cpu
81 .align  4
82 .OPENSSL_wipe_cpu:
83         xor     r0,r0,r0
84         fmr     f0,f31
85         fmr     f1,f31
86         fmr     f2,f31
87         mr      r3,r1
88         fmr     f3,f31
89         xor     r4,r4,r4
90         fmr     f4,f31
91         xor     r5,r5,r5
92         fmr     f5,f31
93         xor     r6,r6,r6
94         fmr     f6,f31
95         xor     r7,r7,r7
96         fmr     f7,f31
97         xor     r8,r8,r8
98         fmr     f8,f31
99         xor     r9,r9,r9
100         fmr     f9,f31
101         xor     r10,r10,r10
102         fmr     f10,f31
103         xor     r11,r11,r11
104         fmr     f11,f31
105         xor     r12,r12,r12
106         fmr     f12,f31
107         fmr     f13,f31
108         blr
109         .long   0
110         .byte   0,12,0x14,0,0,0,0,0
111 .size   .OPENSSL_wipe_cpu,.-.OPENSSL_wipe_cpu
112
113 .globl  .OPENSSL_atomic_add
114 .align  4
115 .OPENSSL_atomic_add:
116 Ladd:   lwarx   r5,0,r3
117         add     r0,r4,r5
118         stwcx.  r0,0,r3
119         bne-    Ladd
120         $SIGNX  r3,r0
121         blr
122         .long   0
123         .byte   0,12,0x14,0,0,0,2,0
124         .long   0
125 .size   .OPENSSL_atomic_add,.-.OPENSSL_atomic_add
126
127 .globl  .OPENSSL_rdtsc
128 .align  4
129 .OPENSSL_rdtsc:
130 ___
131 $code.=<<___    if ($flavour =~ /64/);
132         mftb    r3
133 ___
134 $code.=<<___    if ($flavour !~ /64/);
135 Loop_rdtsc:
136         mftbu   r5
137         mftb    r3
138         mftbu   r4
139         cmplw   r4,r5
140         bne     Loop_rdtsc
141 ___
142 $code.=<<___;
143         blr
144         .long   0
145         .byte   0,12,0x14,0,0,0,0,0
146 .size   .OPENSSL_rdtsc,.-.OPENSSL_rdtsc
147
148 .globl  .OPENSSL_cleanse
149 .align  4
150 .OPENSSL_cleanse:
151         $CMPLI  r4,7
152         li      r0,0
153         bge     Lot
154         $CMPLI  r4,0
155         beqlr-
156 Little: mtctr   r4
157         stb     r0,0(r3)
158         addi    r3,r3,1
159         bdnz    \$-8
160         blr
161 Lot:    andi.   r5,r3,3
162         beq     Laligned
163         stb     r0,0(r3)
164         subi    r4,r4,1
165         addi    r3,r3,1
166         b       Lot
167 Laligned:
168         $SHRLI  r5,r4,2
169         mtctr   r5
170         stw     r0,0(r3)
171         addi    r3,r3,4
172         bdnz    \$-8
173         andi.   r4,r4,3
174         bne     Little
175         blr
176         .long   0
177         .byte   0,12,0x14,0,0,0,2,0
178         .long   0
179 .size   .OPENSSL_cleanse,.-.OPENSSL_cleanse
180 ___
181 {
182 my ($out,$cnt,$max)=("r3","r4","r5");
183 my ($tick,$lasttick)=("r6","r7");
184 my ($diff,$lastdiff)=("r8","r9");
185
186 $code.=<<___;
187 .globl  .OPENSSL_instrument_bus
188 .align  4
189 .OPENSSL_instrument_bus:
190         mtctr   $cnt
191
192         mftb    $lasttick               # collect 1st tick
193         li      $diff,0
194
195         dcbf    0,$out                  # flush cache line
196         lwarx   $tick,0,$out            # load and lock
197         add     $tick,$tick,$diff
198         stwcx.  $tick,0,$out
199         stwx    $tick,0,$out
200
201 Loop:   mftb    $tick
202         sub     $diff,$tick,$lasttick
203         mr      $lasttick,$tick
204         dcbf    0,$out                  # flush cache line
205         lwarx   $tick,0,$out            # load and lock
206         add     $tick,$tick,$diff
207         stwcx.  $tick,0,$out
208         stwx    $tick,0,$out
209         addi    $out,$out,4             # ++$out
210         bdnz    Loop
211
212         mr      r3,$cnt
213         blr
214         .long   0
215         .byte   0,12,0x14,0,0,0,2,0
216         .long   0
217 .size   .OPENSSL_instrument_bus,.-.OPENSSL_instrument_bus
218
219 .globl  .OPENSSL_instrument_bus2
220 .align  4
221 .OPENSSL_instrument_bus2:
222         mr      r0,$cnt
223         slwi    $cnt,$cnt,2
224
225         mftb    $lasttick               # collect 1st tick
226         li      $diff,0
227
228         dcbf    0,$out                  # flush cache line
229         lwarx   $tick,0,$out            # load and lock
230         add     $tick,$tick,$diff
231         stwcx.  $tick,0,$out
232         stwx    $tick,0,$out
233
234         mftb    $tick                   # collect 1st diff
235         sub     $diff,$tick,$lasttick
236         mr      $lasttick,$tick
237         mr      $lastdiff,$diff
238 Loop2:
239         dcbf    0,$out                  # flush cache line
240         lwarx   $tick,0,$out            # load and lock
241         add     $tick,$tick,$diff
242         stwcx.  $tick,0,$out
243         stwx    $tick,0,$out
244
245         addic.  $max,$max,-1
246         beq     Ldone2
247
248         mftb    $tick
249         sub     $diff,$tick,$lasttick
250         mr      $lasttick,$tick
251         cmplw   7,$diff,$lastdiff
252         mr      $lastdiff,$diff
253
254         mfcr    $tick                   # pull cr
255         not     $tick,$tick             # flip bits
256         rlwinm  $tick,$tick,1,29,29     # isolate flipped eq bit and scale
257
258         sub.    $cnt,$cnt,$tick         # conditional --$cnt
259         add     $out,$out,$tick         # conditional ++$out
260         bne     Loop2
261
262 Ldone2:
263         srwi    $cnt,$cnt,2
264         sub     r3,r0,$cnt
265         blr
266         .long   0
267         .byte   0,12,0x14,0,0,0,3,0
268         .long   0
269 .size   .OPENSSL_instrument_bus2,.-.OPENSSL_instrument_bus2
270 ___
271 }
272
273 $code =~ s/\`([^\`]*)\`/eval $1/gem;
274 print $code;
275 close STDOUT;