x86_64cpuid.pl 4.2 KB

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