x86_64cpuid.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/env perl
  2. $output=shift;
  3. $masm=1 if ($output =~ /\.asm/);
  4. open STDOUT,">$output" || die "can't open $output: $!";
  5. print<<___ if(defined($masm));
  6. _TEXT SEGMENT
  7. PUBLIC OPENSSL_rdtsc
  8. PUBLIC OPENSSL_atomic_add
  9. ALIGN 16
  10. OPENSSL_atomic_add PROC
  11. mov eax,DWORD PTR[rcx]
  12. \$Lspin: lea r8,DWORD PTR[rdx+rax]
  13. lock cmpxchg DWORD PTR[rcx],r8d
  14. jne \$Lspin
  15. mov eax,r8d
  16. cdqe
  17. ret
  18. OPENSSL_atomic_add ENDP
  19. PUBLIC OPENSSL_wipe_cpu
  20. ALIGN 16
  21. OPENSSL_wipe_cpu PROC
  22. pxor xmm0,xmm0
  23. pxor xmm1,xmm1
  24. pxor xmm2,xmm2
  25. pxor xmm3,xmm3
  26. pxor xmm4,xmm4
  27. pxor xmm5,xmm5
  28. xor rcx,rcx
  29. xor rdx,rdx
  30. xor r8,r8
  31. xor r9,r9
  32. xor r10,r10
  33. xor r11,r11
  34. lea rax,QWORD PTR[rsp+8]
  35. ret
  36. OPENSSL_wipe_cpu ENDP
  37. _TEXT ENDS
  38. CRT\$XIU SEGMENT
  39. EXTRN OPENSSL_cpuid_setup:PROC
  40. DQ OPENSSL_cpuid_setup
  41. CRT\$XIU ENDS
  42. ___
  43. print<<___ if(!defined($masm));
  44. .text
  45. .globl OPENSSL_atomic_add
  46. .type OPENSSL_atomic_add,\@function
  47. .align 16
  48. OPENSSL_atomic_add:
  49. movl (%rdi),%eax
  50. .Lspin: leaq (%rsi,%rax),%r8
  51. lock; cmpxchgl %r8d,(%rdi)
  52. jne .Lspin
  53. movl %r8d,%eax
  54. .byte 0x48,0x98
  55. ret
  56. .size OPENSSL_atomic_add,.-OPENSSL_atomic_add
  57. .globl OPENSSL_wipe_cpu
  58. .type OPENSSL_wipe_cpu,\@function
  59. .align 16
  60. OPENSSL_wipe_cpu:
  61. pxor %xmm0,%xmm0
  62. pxor %xmm1,%xmm1
  63. pxor %xmm2,%xmm2
  64. pxor %xmm3,%xmm3
  65. pxor %xmm4,%xmm4
  66. pxor %xmm5,%xmm5
  67. pxor %xmm6,%xmm6
  68. pxor %xmm7,%xmm7
  69. pxor %xmm8,%xmm8
  70. pxor %xmm9,%xmm9
  71. pxor %xmm10,%xmm10
  72. pxor %xmm11,%xmm11
  73. pxor %xmm12,%xmm12
  74. pxor %xmm13,%xmm13
  75. pxor %xmm14,%xmm14
  76. pxor %xmm15,%xmm15
  77. xorq %rcx,%rcx
  78. xorq %rdx,%rdx
  79. xorq %rsi,%rsi
  80. xorq %rdi,%rdi
  81. xorq %r8,%r8
  82. xorq %r9,%r9
  83. xorq %r10,%r10
  84. xorq %r11,%r11
  85. leaq 8(%rsp),%rax
  86. ret
  87. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  88. .section .init
  89. call OPENSSL_cpuid_setup
  90. ___
  91. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  92. $dir = "." if $dir eq "";
  93. open STDOUT,"| $^X $dir/perlasm/x86_64-xlate.pl $output";
  94. print<<___;
  95. .text
  96. .globl OPENSSL_rdtsc
  97. .type OPENSSL_rdtsc,\@abi-omnipotent
  98. .align 16
  99. OPENSSL_rdtsc:
  100. rdtsc
  101. shl \$32,%rdx
  102. or %rdx,%rax
  103. ret
  104. .size OPENSSL_rdtsc,.-OPENSSL_rdtsc
  105. .globl OPENSSL_ia32_cpuid
  106. .type OPENSSL_ia32_cpuid,\@abi-omnipotent
  107. .align 16
  108. OPENSSL_ia32_cpuid:
  109. mov %rbx,%r8
  110. xor %eax,%eax
  111. cpuid
  112. xor %eax,%eax
  113. cmp \$0x756e6547,%ebx # "Genu"
  114. setne %al
  115. mov %eax,%r9d
  116. cmp \$0x49656e69,%edx # "ineI"
  117. setne %al
  118. or %eax,%r9d
  119. cmp \$0x6c65746e,%ecx # "ntel"
  120. setne %al
  121. or %eax,%r9d
  122. mov \$1,%eax
  123. cpuid
  124. cmp \$0,%r9d
  125. jne .Lnotintel
  126. or \$0x00100000,%edx # use reserved 20th bit to engage RC4_CHAR
  127. and \$15,%ah
  128. cmp \$15,%ah # examine Family ID
  129. je .Lnotintel
  130. or \$0x40000000,%edx # use reserved bit to skip unrolled loop
  131. .Lnotintel:
  132. bt \$28,%edx # test hyper-threading bit
  133. jnc .Ldone
  134. shr \$16,%ebx
  135. cmp \$1,%bl # see if cache is shared
  136. ja .Ldone
  137. and \$0xefffffff,%edx # ~(1<<28)
  138. .Ldone:
  139. shl \$32,%rcx
  140. mov %edx,%eax
  141. mov %r8,%rbx
  142. or %rcx,%rax
  143. ret
  144. .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
  145. .globl OPENSSL_cleanse
  146. .type OPENSSL_cleanse,\@function,2
  147. .align 16
  148. OPENSSL_cleanse:
  149. xor %rax,%rax
  150. cmp \$15,%rsi
  151. jae .Lot
  152. .Little:
  153. mov %al,(%rdi)
  154. sub \$1,%rsi
  155. lea 1(%rdi),%rdi
  156. jnz .Little
  157. ret
  158. .align 16
  159. .Lot:
  160. test \$7,%rdi
  161. jz .Laligned
  162. mov %al,(%rdi)
  163. lea -1(%rsi),%rsi
  164. lea 1(%rdi),%rdi
  165. jmp .Lot
  166. .Laligned:
  167. mov %rax,(%rdi)
  168. lea -8(%rsi),%rsi
  169. test \$-8,%rsi
  170. lea 8(%rdi),%rdi
  171. jnz .Laligned
  172. cmp \$0,%rsi
  173. jne .Little
  174. ret
  175. .size OPENSSL_cleanse,.-OPENSSL_cleanse
  176. ___
  177. close STDOUT; # flush