PR: 2149
[oweals/openssl.git] / crypto / x86_64cpuid.pl
1 #!/usr/bin/env perl
2
3 $flavour = shift;
4 $output  = shift;
5 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
6
7 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
8
9 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
10 open STDOUT,"| $^X ${dir}perlasm/x86_64-xlate.pl $flavour $output";
11
12 if ($win64)     { $arg1="%rcx"; $arg2="%rdx"; }
13 else            { $arg1="%rdi"; $arg2="%rsi"; }
14 print<<___;
15 .extern         OPENSSL_cpuid_setup
16 .section        .init
17         call    OPENSSL_cpuid_setup
18
19 .text
20
21 .globl  OPENSSL_atomic_add
22 .type   OPENSSL_atomic_add,\@abi-omnipotent
23 .align  16
24 OPENSSL_atomic_add:
25         movl    ($arg1),%eax
26 .Lspin: leaq    ($arg2,%rax),%r8
27         .byte   0xf0            # lock
28         cmpxchgl        %r8d,($arg1)
29         jne     .Lspin
30         movl    %r8d,%eax
31         .byte   0x48,0x98       # cltq/cdqe
32         ret
33 .size   OPENSSL_atomic_add,.-OPENSSL_atomic_add
34
35 .globl  OPENSSL_rdtsc
36 .type   OPENSSL_rdtsc,\@abi-omnipotent
37 .align  16
38 OPENSSL_rdtsc:
39         rdtsc
40         shl     \$32,%rdx
41         or      %rdx,%rax
42         ret
43 .size   OPENSSL_rdtsc,.-OPENSSL_rdtsc
44
45 .globl  OPENSSL_ia32_cpuid
46 .type   OPENSSL_ia32_cpuid,\@abi-omnipotent
47 .align  16
48 OPENSSL_ia32_cpuid:
49         mov     %rbx,%r8
50
51         xor     %eax,%eax
52         cpuid
53         mov     %eax,%r11d              # max value for standard query level
54
55         xor     %eax,%eax
56         cmp     \$0x756e6547,%ebx       # "Genu"
57         setne   %al
58         mov     %eax,%r9d
59         cmp     \$0x49656e69,%edx       # "ineI"
60         setne   %al
61         or      %eax,%r9d
62         cmp     \$0x6c65746e,%ecx       # "ntel"
63         setne   %al
64         or      %eax,%r9d               # 0 indicates Intel CPU
65         jz      .Lintel
66
67         cmp     \$0x68747541,%ebx       # "Auth"
68         setne   %al
69         mov     %eax,%r10d
70         cmp     \$0x69746E65,%edx       # "enti"
71         setne   %al
72         or      %eax,%r10d
73         cmp     \$0x444D4163,%ecx       # "cAMD"
74         setne   %al
75         or      %eax,%r10d              # 0 indicates AMD CPU
76         jnz     .Lintel
77
78         # AMD specific
79         mov     \$0x80000000,%eax
80         cpuid
81         cmp     \$0x80000008,%eax
82         jb      .Lintel
83
84         mov     \$0x80000008,%eax
85         cpuid
86         movzb   %cl,%r10                # number of cores - 1
87         inc     %r10                    # number of cores
88
89         mov     \$1,%eax
90         cpuid
91         bt      \$28,%edx               # test hyper-threading bit
92         jnc     .Ldone
93         shr     \$16,%ebx               # number of logical processors
94         cmp     %r10b,%bl
95         ja      .Ldone
96         and     \$0xefffffff,%edx       # ~(1<<28)
97         jmp     .Ldone
98
99 .Lintel:
100         cmp     \$4,%r11d
101         mov     \$-1,%r10d
102         jb      .Lnocacheinfo
103
104         mov     \$4,%eax
105         mov     \$0,%ecx                # query L1D
106         cpuid
107         mov     %eax,%r10d
108         shr     \$14,%r10d
109         and     \$0xfff,%r10d           # number of cores -1 per L1D
110
111 .Lnocacheinfo:
112         mov     \$1,%eax
113         cpuid
114         cmp     \$0,%r9d
115         jne     .Lnotintel
116         or      \$0x00100000,%edx       # use reserved 20th bit to engage RC4_CHAR
117         and     \$15,%ah
118         cmp     \$15,%ah                # examine Family ID
119         je      .Lnotintel
120         or      \$0x40000000,%edx       # use reserved bit to skip unrolled loop
121 .Lnotintel:
122         bt      \$28,%edx               # test hyper-threading bit
123         jnc     .Ldone
124         and     \$0xefffffff,%edx       # ~(1<<28)
125         cmp     \$0,%r10d
126         je      .Ldone
127
128         or      \$0x10000000,%edx       # 1<<28
129         shr     \$16,%ebx
130         cmp     \$1,%bl                 # see if cache is shared
131         ja      .Ldone
132         and     \$0xefffffff,%edx       # ~(1<<28)
133 .Ldone:
134         shl     \$32,%rcx
135         mov     %edx,%eax
136         mov     %r8,%rbx
137         or      %rcx,%rax
138         ret
139 .size   OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
140
141 .globl  OPENSSL_cleanse
142 .type   OPENSSL_cleanse,\@abi-omnipotent
143 .align  16
144 OPENSSL_cleanse:
145         xor     %rax,%rax
146         cmp     \$15,$arg2
147         jae     .Lot
148         cmp     \$0,$arg2
149         je      .Lret
150 .Little:
151         mov     %al,($arg1)
152         sub     \$1,$arg2
153         lea     1($arg1),$arg1
154         jnz     .Little
155 .Lret:  ret
156 .align  16
157 .Lot:
158         test    \$7,$arg1
159         jz      .Laligned
160         mov     %al,($arg1)
161         lea     -1($arg2),$arg2
162         lea     1($arg1),$arg1
163         jmp     .Lot
164 .Laligned:
165         mov     %rax,($arg1)
166         lea     -8($arg2),$arg2
167         test    \$-8,$arg2
168         lea     8($arg1),$arg1
169         jnz     .Laligned
170         cmp     \$0,$arg2
171         jne     .Little
172         ret
173 .size   OPENSSL_cleanse,.-OPENSSL_cleanse
174 ___
175
176 print<<___ if (!$win64);
177 .globl  OPENSSL_wipe_cpu
178 .type   OPENSSL_wipe_cpu,\@abi-omnipotent
179 .align  16
180 OPENSSL_wipe_cpu:
181         pxor    %xmm0,%xmm0
182         pxor    %xmm1,%xmm1
183         pxor    %xmm2,%xmm2
184         pxor    %xmm3,%xmm3
185         pxor    %xmm4,%xmm4
186         pxor    %xmm5,%xmm5
187         pxor    %xmm6,%xmm6
188         pxor    %xmm7,%xmm7
189         pxor    %xmm8,%xmm8
190         pxor    %xmm9,%xmm9
191         pxor    %xmm10,%xmm10
192         pxor    %xmm11,%xmm11
193         pxor    %xmm12,%xmm12
194         pxor    %xmm13,%xmm13
195         pxor    %xmm14,%xmm14
196         pxor    %xmm15,%xmm15
197         xorq    %rcx,%rcx
198         xorq    %rdx,%rdx
199         xorq    %rsi,%rsi
200         xorq    %rdi,%rdi
201         xorq    %r8,%r8
202         xorq    %r9,%r9
203         xorq    %r10,%r10
204         xorq    %r11,%r11
205         leaq    8(%rsp),%rax
206         ret
207 .size   OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
208 ___
209 print<<___ if ($win64);
210 .globl  OPENSSL_wipe_cpu
211 .type   OPENSSL_wipe_cpu,\@abi-omnipotent
212 .align  16
213 OPENSSL_wipe_cpu:
214         pxor    %xmm0,%xmm0
215         pxor    %xmm1,%xmm1
216         pxor    %xmm2,%xmm2
217         pxor    %xmm3,%xmm3
218         pxor    %xmm4,%xmm4
219         pxor    %xmm5,%xmm5
220         xorq    %rcx,%rcx
221         xorq    %rdx,%rdx
222         xorq    %r8,%r8
223         xorq    %r9,%r9
224         xorq    %r10,%r10
225         xorq    %r11,%r11
226         leaq    8(%rsp),%rax
227         ret
228 .size   OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
229 ___
230
231 close STDOUT;   # flush