x86_64cpuid.pl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  8. ( $xlate="${dir}perlasm/x86_64-xlate.pl" and -f $xlate) or
  9. die "can't locate x86_64-xlate.pl";
  10. open OUT,"| \"$^X\" $xlate $flavour $output";
  11. *STDOUT=*OUT;
  12. ($arg1,$arg2,$arg3,$arg4)=$win64?("%rcx","%rdx","%r8", "%r9") : # Win64 order
  13. ("%rdi","%rsi","%rdx","%rcx"); # Unix order
  14. print<<___;
  15. .extern OPENSSL_cpuid_setup
  16. .hidden OPENSSL_cpuid_setup
  17. .section .init
  18. call OPENSSL_cpuid_setup
  19. .hidden OPENSSL_ia32cap_P
  20. .comm OPENSSL_ia32cap_P,8,4
  21. .text
  22. .globl OPENSSL_atomic_add
  23. .type OPENSSL_atomic_add,\@abi-omnipotent
  24. .align 16
  25. OPENSSL_atomic_add:
  26. movl ($arg1),%eax
  27. .Lspin: leaq ($arg2,%rax),%r8
  28. .byte 0xf0 # lock
  29. cmpxchgl %r8d,($arg1)
  30. jne .Lspin
  31. movl %r8d,%eax
  32. .byte 0x48,0x98 # cltq/cdqe
  33. ret
  34. .size OPENSSL_atomic_add,.-OPENSSL_atomic_add
  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. .globl OPENSSL_ia32_cpuid
  45. .type OPENSSL_ia32_cpuid,\@abi-omnipotent
  46. .align 16
  47. OPENSSL_ia32_cpuid:
  48. mov %rbx,%r8 # save %rbx
  49. xor %eax,%eax
  50. cpuid
  51. mov %eax,%r11d # max value for standard query level
  52. xor %eax,%eax
  53. cmp \$0x756e6547,%ebx # "Genu"
  54. setne %al
  55. mov %eax,%r9d
  56. cmp \$0x49656e69,%edx # "ineI"
  57. setne %al
  58. or %eax,%r9d
  59. cmp \$0x6c65746e,%ecx # "ntel"
  60. setne %al
  61. or %eax,%r9d # 0 indicates Intel CPU
  62. jz .Lintel
  63. cmp \$0x68747541,%ebx # "Auth"
  64. setne %al
  65. mov %eax,%r10d
  66. cmp \$0x69746E65,%edx # "enti"
  67. setne %al
  68. or %eax,%r10d
  69. cmp \$0x444D4163,%ecx # "cAMD"
  70. setne %al
  71. or %eax,%r10d # 0 indicates AMD CPU
  72. jnz .Lintel
  73. # AMD specific
  74. mov \$0x80000000,%eax
  75. cpuid
  76. cmp \$0x80000001,%eax
  77. jb .Lintel
  78. mov %eax,%r10d
  79. mov \$0x80000001,%eax
  80. cpuid
  81. or %ecx,%r9d
  82. and \$0x00000801,%r9d # isolate AMD XOP bit, 1<<11
  83. cmp \$0x80000008,%r10d
  84. jb .Lintel
  85. mov \$0x80000008,%eax
  86. cpuid
  87. movzb %cl,%r10 # number of cores - 1
  88. inc %r10 # number of cores
  89. mov \$1,%eax
  90. cpuid
  91. bt \$28,%edx # test hyper-threading bit
  92. jnc .Lgeneric
  93. shr \$16,%ebx # number of logical processors
  94. cmp %r10b,%bl
  95. ja .Lgeneric
  96. and \$0xefffffff,%edx # ~(1<<28)
  97. jmp .Lgeneric
  98. .Lintel:
  99. cmp \$4,%r11d
  100. mov \$-1,%r10d
  101. jb .Lnocacheinfo
  102. mov \$4,%eax
  103. mov \$0,%ecx # query L1D
  104. cpuid
  105. mov %eax,%r10d
  106. shr \$14,%r10d
  107. and \$0xfff,%r10d # number of cores -1 per L1D
  108. .Lnocacheinfo:
  109. mov \$1,%eax
  110. cpuid
  111. and \$0xbfefffff,%edx # force reserved bits to 0
  112. cmp \$0,%r9d
  113. jne .Lnotintel
  114. or \$0x40000000,%edx # set reserved bit#30 on Intel CPUs
  115. and \$15,%ah
  116. cmp \$15,%ah # examine Family ID
  117. jne .Lnotintel
  118. or \$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR
  119. .Lnotintel:
  120. bt \$28,%edx # test hyper-threading bit
  121. jnc .Lgeneric
  122. and \$0xefffffff,%edx # ~(1<<28)
  123. cmp \$0,%r10d
  124. je .Lgeneric
  125. or \$0x10000000,%edx # 1<<28
  126. shr \$16,%ebx
  127. cmp \$1,%bl # see if cache is shared
  128. ja .Lgeneric
  129. and \$0xefffffff,%edx # ~(1<<28)
  130. .Lgeneric:
  131. and \$0x00000800,%r9d # isolate AMD XOP flag
  132. and \$0xfffff7ff,%ecx
  133. or %ecx,%r9d # merge AMD XOP flag
  134. mov %edx,%r10d # %r9d:%r10d is copy of %ecx:%edx
  135. bt \$27,%r9d # check OSXSAVE bit
  136. jnc .Lclear_avx
  137. xor %ecx,%ecx # XCR0
  138. .byte 0x0f,0x01,0xd0 # xgetbv
  139. and \$6,%eax # isolate XMM and YMM state support
  140. cmp \$6,%eax
  141. je .Ldone
  142. .Lclear_avx:
  143. mov \$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11)
  144. and %eax,%r9d # clear AVX, FMA and AMD XOP bits
  145. .Ldone:
  146. shl \$32,%r9
  147. mov %r10d,%eax
  148. mov %r8,%rbx # restore %rbx
  149. or %r9,%rax
  150. ret
  151. .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
  152. .globl OPENSSL_cleanse
  153. .type OPENSSL_cleanse,\@abi-omnipotent
  154. .align 16
  155. OPENSSL_cleanse:
  156. xor %rax,%rax
  157. cmp \$15,$arg2
  158. jae .Lot
  159. cmp \$0,$arg2
  160. je .Lret
  161. .Little:
  162. mov %al,($arg1)
  163. sub \$1,$arg2
  164. lea 1($arg1),$arg1
  165. jnz .Little
  166. .Lret:
  167. ret
  168. .align 16
  169. .Lot:
  170. test \$7,$arg1
  171. jz .Laligned
  172. mov %al,($arg1)
  173. lea -1($arg2),$arg2
  174. lea 1($arg1),$arg1
  175. jmp .Lot
  176. .Laligned:
  177. mov %rax,($arg1)
  178. lea -8($arg2),$arg2
  179. test \$-8,$arg2
  180. lea 8($arg1),$arg1
  181. jnz .Laligned
  182. cmp \$0,$arg2
  183. jne .Little
  184. ret
  185. .size OPENSSL_cleanse,.-OPENSSL_cleanse
  186. ___
  187. print<<___ if (!$win64);
  188. .globl OPENSSL_wipe_cpu
  189. .type OPENSSL_wipe_cpu,\@abi-omnipotent
  190. .align 16
  191. OPENSSL_wipe_cpu:
  192. pxor %xmm0,%xmm0
  193. pxor %xmm1,%xmm1
  194. pxor %xmm2,%xmm2
  195. pxor %xmm3,%xmm3
  196. pxor %xmm4,%xmm4
  197. pxor %xmm5,%xmm5
  198. pxor %xmm6,%xmm6
  199. pxor %xmm7,%xmm7
  200. pxor %xmm8,%xmm8
  201. pxor %xmm9,%xmm9
  202. pxor %xmm10,%xmm10
  203. pxor %xmm11,%xmm11
  204. pxor %xmm12,%xmm12
  205. pxor %xmm13,%xmm13
  206. pxor %xmm14,%xmm14
  207. pxor %xmm15,%xmm15
  208. xorq %rcx,%rcx
  209. xorq %rdx,%rdx
  210. xorq %rsi,%rsi
  211. xorq %rdi,%rdi
  212. xorq %r8,%r8
  213. xorq %r9,%r9
  214. xorq %r10,%r10
  215. xorq %r11,%r11
  216. leaq 8(%rsp),%rax
  217. ret
  218. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  219. ___
  220. print<<___ if ($win64);
  221. .globl OPENSSL_wipe_cpu
  222. .type OPENSSL_wipe_cpu,\@abi-omnipotent
  223. .align 16
  224. OPENSSL_wipe_cpu:
  225. pxor %xmm0,%xmm0
  226. pxor %xmm1,%xmm1
  227. pxor %xmm2,%xmm2
  228. pxor %xmm3,%xmm3
  229. pxor %xmm4,%xmm4
  230. pxor %xmm5,%xmm5
  231. xorq %rcx,%rcx
  232. xorq %rdx,%rdx
  233. xorq %r8,%r8
  234. xorq %r9,%r9
  235. xorq %r10,%r10
  236. xorq %r11,%r11
  237. leaq 8(%rsp),%rax
  238. ret
  239. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  240. ___
  241. print<<___;
  242. .globl OPENSSL_ia32_rdrand
  243. .type OPENSSL_ia32_rdrand,\@abi-omnipotent
  244. .align 16
  245. OPENSSL_ia32_rdrand:
  246. mov \$8,%ecx
  247. .Loop_rdrand:
  248. rdrand %rax
  249. jc .Lbreak_rdrand
  250. loop .Loop_rdrand
  251. .Lbreak_rdrand:
  252. cmp \$0,%rax
  253. cmove %rcx,%rax
  254. ret
  255. .size OPENSSL_ia32_rdrand,.-OPENSSL_ia32_rdrand
  256. ___
  257. close STDOUT; # flush