a8ee09937715b96b2b2f47dc8abe250cbe74a157
[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         xor     %eax,%eax
54         cmp     \$0x756e6547,%ebx       # "Genu"
55         setne   %al
56         mov     %eax,%r9d
57         cmp     \$0x49656e69,%edx       # "ineI"
58         setne   %al
59         or      %eax,%r9d
60         cmp     \$0x6c65746e,%ecx       # "ntel"
61         setne   %al
62         or      %eax,%r9d               # 0 indicates Intel CPU
63         mov     \$1,%r10d               # "number of [AMD] cores"
64         jz      .Lintel
65
66         cmp     \$0x68747541,%ebx       # "Auth"
67         setne   %al
68         mov     %eax,%r10d
69         cmp     \$0x69746E65,%edx       # "enti"
70         setne   %al
71         or      %eax,%r10d
72         cmp     \$0x444D4163,%ecx       # "cAMD"
73         setne   %al
74         or      %eax,%r10d              # 0 indicates AMD CPU
75         jnz     .Lintel
76
77         mov     \$0x80000000,%eax
78         cpuid
79         cmp     \$0x80000008,%eax
80         mov     \$1,%r10d               # "number of [AMD] cores"
81         jb      .Lintel
82
83         mov     \$0x80000008,%eax
84         cpuid
85         movzb   %cl,%r10                # number of cores - 1
86         inc     %r10                    # number of cores
87
88 .Lintel:
89         mov     \$1,%eax
90         cpuid
91         cmp     \$0,%r9d
92         jne     .Lnotintel
93         or      \$0x00100000,%edx       # use reserved 20th bit to engage RC4_CHAR
94         and     \$15,%ah
95         cmp     \$15,%ah                # examine Family ID
96         je      .Lnotintel
97         or      \$0x40000000,%edx       # use reserved bit to skip unrolled loop
98 .Lnotintel:
99         bt      \$28,%edx               # test hyper-threading bit
100         jnc     .Ldone
101         shr     \$16,%ebx
102         cmp     %r10b,%bl               # see if cache is shared
103         ja      .Ldone
104         and     \$0xefffffff,%edx       # ~(1<<28)
105 .Ldone:
106         shl     \$32,%rcx
107         mov     %edx,%eax
108         mov     %r8,%rbx
109         or      %rcx,%rax
110         ret
111 .size   OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
112
113 .globl  OPENSSL_cleanse
114 .type   OPENSSL_cleanse,\@abi-omnipotent
115 .align  16
116 OPENSSL_cleanse:
117         xor     %rax,%rax
118         cmp     \$15,$arg2
119         jae     .Lot
120 .Little:
121         mov     %al,($arg1)
122         sub     \$1,$arg2
123         lea     1($arg1),$arg1
124         jnz     .Little
125         ret
126 .align  16
127 .Lot:
128         test    \$7,$arg1
129         jz      .Laligned
130         mov     %al,($arg1)
131         lea     -1($arg2),$arg2
132         lea     1($arg1),$arg1
133         jmp     .Lot
134 .Laligned:
135         mov     %rax,($arg1)
136         lea     -8($arg2),$arg2
137         test    \$-8,$arg2
138         lea     8($arg1),$arg1
139         jnz     .Laligned
140         cmp     \$0,$arg2
141         jne     .Little
142         ret
143 .size   OPENSSL_cleanse,.-OPENSSL_cleanse
144 ___
145
146 print<<___ if (!$win64);
147 .globl  OPENSSL_wipe_cpu
148 .type   OPENSSL_wipe_cpu,\@abi-omnipotent
149 .align  16
150 OPENSSL_wipe_cpu:
151         pxor    %xmm0,%xmm0
152         pxor    %xmm1,%xmm1
153         pxor    %xmm2,%xmm2
154         pxor    %xmm3,%xmm3
155         pxor    %xmm4,%xmm4
156         pxor    %xmm5,%xmm5
157         pxor    %xmm6,%xmm6
158         pxor    %xmm7,%xmm7
159         pxor    %xmm8,%xmm8
160         pxor    %xmm9,%xmm9
161         pxor    %xmm10,%xmm10
162         pxor    %xmm11,%xmm11
163         pxor    %xmm12,%xmm12
164         pxor    %xmm13,%xmm13
165         pxor    %xmm14,%xmm14
166         pxor    %xmm15,%xmm15
167         xorq    %rcx,%rcx
168         xorq    %rdx,%rdx
169         xorq    %rsi,%rsi
170         xorq    %rdi,%rdi
171         xorq    %r8,%r8
172         xorq    %r9,%r9
173         xorq    %r10,%r10
174         xorq    %r11,%r11
175         leaq    8(%rsp),%rax
176         ret
177 .size   OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
178 ___
179 print<<___ if ($win64);
180 .globl  OPENSSL_wipe_cpu
181 .type   OPENSSL_wipe_cpu,\@abi-omnipotent
182 .align  16
183 OPENSSL_wipe_cpu:
184         pxor    %xmm0,%xmm0
185         pxor    %xmm1,%xmm1
186         pxor    %xmm2,%xmm2
187         pxor    %xmm3,%xmm3
188         pxor    %xmm4,%xmm4
189         pxor    %xmm5,%xmm5
190         xorq    %rcx,%rcx
191         xorq    %rdx,%rdx
192         xorq    %r8,%r8
193         xorq    %r9,%r9
194         xorq    %r10,%r10
195         xorq    %r11,%r11
196         leaq    8(%rsp),%rax
197         ret
198 .size   OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
199 ___
200
201 close STDOUT;   # flush