2
0

x86_64cpuid.pl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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,16,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,\@function,1
  46. .align 16
  47. OPENSSL_ia32_cpuid:
  48. mov %rbx,%r8 # save %rbx
  49. xor %eax,%eax
  50. mov %eax,8(%rdi) # clear 3rd word
  51. cpuid
  52. mov %eax,%r11d # max value for standard query level
  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. jz .Lintel
  64. cmp \$0x68747541,%ebx # "Auth"
  65. setne %al
  66. mov %eax,%r10d
  67. cmp \$0x69746E65,%edx # "enti"
  68. setne %al
  69. or %eax,%r10d
  70. cmp \$0x444D4163,%ecx # "cAMD"
  71. setne %al
  72. or %eax,%r10d # 0 indicates AMD CPU
  73. jnz .Lintel
  74. # AMD specific
  75. mov \$0x80000000,%eax
  76. cpuid
  77. cmp \$0x80000001,%eax
  78. jb .Lintel
  79. mov %eax,%r10d
  80. mov \$0x80000001,%eax
  81. cpuid
  82. or %ecx,%r9d
  83. and \$0x00000801,%r9d # isolate AMD XOP bit, 1<<11
  84. cmp \$0x80000008,%r10d
  85. jb .Lintel
  86. mov \$0x80000008,%eax
  87. cpuid
  88. movzb %cl,%r10 # number of cores - 1
  89. inc %r10 # number of cores
  90. mov \$1,%eax
  91. cpuid
  92. bt \$28,%edx # test hyper-threading bit
  93. jnc .Lgeneric
  94. shr \$16,%ebx # number of logical processors
  95. cmp %r10b,%bl
  96. ja .Lgeneric
  97. and \$0xefffffff,%edx # ~(1<<28)
  98. jmp .Lgeneric
  99. .Lintel:
  100. cmp \$4,%r11d
  101. mov \$-1,%r10d
  102. jb .Lnocacheinfo
  103. mov \$4,%eax
  104. mov \$0,%ecx # query L1D
  105. cpuid
  106. mov %eax,%r10d
  107. shr \$14,%r10d
  108. and \$0xfff,%r10d # number of cores -1 per L1D
  109. cmp \$7,%r11d
  110. jb .Lnocacheinfo
  111. mov \$7,%eax
  112. xor %ecx,%ecx
  113. cpuid
  114. mov %ebx,8(%rdi)
  115. .Lnocacheinfo:
  116. mov \$1,%eax
  117. cpuid
  118. and \$0xbfefffff,%edx # force reserved bits to 0
  119. cmp \$0,%r9d
  120. jne .Lnotintel
  121. or \$0x40000000,%edx # set reserved bit#30 on Intel CPUs
  122. and \$15,%ah
  123. cmp \$15,%ah # examine Family ID
  124. jne .Lnotintel
  125. or \$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR
  126. .Lnotintel:
  127. bt \$28,%edx # test hyper-threading bit
  128. jnc .Lgeneric
  129. and \$0xefffffff,%edx # ~(1<<28)
  130. cmp \$0,%r10d
  131. je .Lgeneric
  132. or \$0x10000000,%edx # 1<<28
  133. shr \$16,%ebx
  134. cmp \$1,%bl # see if cache is shared
  135. ja .Lgeneric
  136. and \$0xefffffff,%edx # ~(1<<28)
  137. .Lgeneric:
  138. and \$0x00000800,%r9d # isolate AMD XOP flag
  139. and \$0xfffff7ff,%ecx
  140. or %ecx,%r9d # merge AMD XOP flag
  141. mov %edx,%r10d # %r9d:%r10d is copy of %ecx:%edx
  142. bt \$27,%r9d # check OSXSAVE bit
  143. jnc .Lclear_avx
  144. xor %ecx,%ecx # XCR0
  145. .byte 0x0f,0x01,0xd0 # xgetbv
  146. and \$6,%eax # isolate XMM and YMM state support
  147. cmp \$6,%eax
  148. je .Ldone
  149. .Lclear_avx:
  150. mov \$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11)
  151. and %eax,%r9d # clear AVX, FMA and AMD XOP bits
  152. andl \$0xffffffdf,8(%rdi) # cleax AVX2, ~(1<<5)
  153. .Ldone:
  154. shl \$32,%r9
  155. mov %r10d,%eax
  156. mov %r8,%rbx # restore %rbx
  157. or %r9,%rax
  158. ret
  159. .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
  160. .globl OPENSSL_cleanse
  161. .type OPENSSL_cleanse,\@abi-omnipotent
  162. .align 16
  163. OPENSSL_cleanse:
  164. xor %rax,%rax
  165. cmp \$15,$arg2
  166. jae .Lot
  167. cmp \$0,$arg2
  168. je .Lret
  169. .Little:
  170. mov %al,($arg1)
  171. sub \$1,$arg2
  172. lea 1($arg1),$arg1
  173. jnz .Little
  174. .Lret:
  175. ret
  176. .align 16
  177. .Lot:
  178. test \$7,$arg1
  179. jz .Laligned
  180. mov %al,($arg1)
  181. lea -1($arg2),$arg2
  182. lea 1($arg1),$arg1
  183. jmp .Lot
  184. .Laligned:
  185. mov %rax,($arg1)
  186. lea -8($arg2),$arg2
  187. test \$-8,$arg2
  188. lea 8($arg1),$arg1
  189. jnz .Laligned
  190. cmp \$0,$arg2
  191. jne .Little
  192. ret
  193. .size OPENSSL_cleanse,.-OPENSSL_cleanse
  194. ___
  195. print<<___ if (!$win64);
  196. .globl OPENSSL_wipe_cpu
  197. .type OPENSSL_wipe_cpu,\@abi-omnipotent
  198. .align 16
  199. OPENSSL_wipe_cpu:
  200. pxor %xmm0,%xmm0
  201. pxor %xmm1,%xmm1
  202. pxor %xmm2,%xmm2
  203. pxor %xmm3,%xmm3
  204. pxor %xmm4,%xmm4
  205. pxor %xmm5,%xmm5
  206. pxor %xmm6,%xmm6
  207. pxor %xmm7,%xmm7
  208. pxor %xmm8,%xmm8
  209. pxor %xmm9,%xmm9
  210. pxor %xmm10,%xmm10
  211. pxor %xmm11,%xmm11
  212. pxor %xmm12,%xmm12
  213. pxor %xmm13,%xmm13
  214. pxor %xmm14,%xmm14
  215. pxor %xmm15,%xmm15
  216. xorq %rcx,%rcx
  217. xorq %rdx,%rdx
  218. xorq %rsi,%rsi
  219. xorq %rdi,%rdi
  220. xorq %r8,%r8
  221. xorq %r9,%r9
  222. xorq %r10,%r10
  223. xorq %r11,%r11
  224. leaq 8(%rsp),%rax
  225. ret
  226. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  227. ___
  228. print<<___ if ($win64);
  229. .globl OPENSSL_wipe_cpu
  230. .type OPENSSL_wipe_cpu,\@abi-omnipotent
  231. .align 16
  232. OPENSSL_wipe_cpu:
  233. pxor %xmm0,%xmm0
  234. pxor %xmm1,%xmm1
  235. pxor %xmm2,%xmm2
  236. pxor %xmm3,%xmm3
  237. pxor %xmm4,%xmm4
  238. pxor %xmm5,%xmm5
  239. xorq %rcx,%rcx
  240. xorq %rdx,%rdx
  241. xorq %r8,%r8
  242. xorq %r9,%r9
  243. xorq %r10,%r10
  244. xorq %r11,%r11
  245. leaq 8(%rsp),%rax
  246. ret
  247. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  248. ___
  249. {
  250. my $out="%r10";
  251. my $cnt="%rcx";
  252. my $max="%r11";
  253. my $lasttick="%r8d";
  254. my $lastdiff="%r9d";
  255. my $redzone=win64?8:-8;
  256. print<<___;
  257. .globl OPENSSL_instrument_bus
  258. .type OPENSSL_instrument_bus,\@abi-omnipotent
  259. .align 16
  260. OPENSSL_instrument_bus:
  261. mov $arg1,$out # tribute to Win64
  262. mov $arg2,$cnt
  263. mov $arg2,$max
  264. rdtsc # collect 1st tick
  265. mov %eax,$lasttick # lasttick = tick
  266. mov \$0,$lastdiff # lastdiff = 0
  267. clflush ($out)
  268. .byte 0xf0 # lock
  269. add $lastdiff,($out)
  270. jmp .Loop
  271. .align 16
  272. .Loop: rdtsc
  273. mov %eax,%edx
  274. sub $lasttick,%eax
  275. mov %edx,$lasttick
  276. mov %eax,$lastdiff
  277. clflush ($out)
  278. .byte 0xf0 # lock
  279. add %eax,($out)
  280. lea 4($out),$out
  281. sub \$1,$cnt
  282. jnz .Loop
  283. mov $max,%rax
  284. ret
  285. .size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
  286. .globl OPENSSL_instrument_bus2
  287. .type OPENSSL_instrument_bus2,\@abi-omnipotent
  288. .align 16
  289. OPENSSL_instrument_bus2:
  290. mov $arg1,$out # tribute to Win64
  291. mov $arg2,$cnt
  292. mov $arg3,$max
  293. mov $cnt,$redzone(%rsp)
  294. rdtsc # collect 1st tick
  295. mov %eax,$lasttick # lasttick = tick
  296. mov \$0,$lastdiff # lastdiff = 0
  297. clflush ($out)
  298. .byte 0xf0 # lock
  299. add $lastdiff,($out)
  300. rdtsc # collect 1st diff
  301. mov %eax,%edx
  302. sub $lasttick,%eax # diff
  303. mov %edx,$lasttick # lasttick = tick
  304. mov %eax,$lastdiff # lastdiff = diff
  305. .Loop2:
  306. clflush ($out)
  307. .byte 0xf0 # lock
  308. add %eax,($out) # accumulate diff
  309. sub \$1,$max
  310. jz .Ldone2
  311. rdtsc
  312. mov %eax,%edx
  313. sub $lasttick,%eax # diff
  314. mov %edx,$lasttick # lasttick = tick
  315. cmp $lastdiff,%eax
  316. mov %eax,$lastdiff # lastdiff = diff
  317. mov \$0,%edx
  318. setne %dl
  319. sub %rdx,$cnt # conditional --$cnt
  320. lea ($out,%rdx,4),$out # conditional ++$out
  321. jnz .Loop2
  322. .Ldone2:
  323. mov $redzone(%rsp),%rax
  324. sub $cnt,%rax
  325. ret
  326. .size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
  327. ___
  328. }
  329. print<<___;
  330. .globl OPENSSL_ia32_rdrand
  331. .type OPENSSL_ia32_rdrand,\@abi-omnipotent
  332. .align 16
  333. OPENSSL_ia32_rdrand:
  334. mov \$8,%ecx
  335. .Loop_rdrand:
  336. rdrand %rax
  337. jc .Lbreak_rdrand
  338. loop .Loop_rdrand
  339. .Lbreak_rdrand:
  340. cmp \$0,%rax
  341. cmove %rcx,%rax
  342. ret
  343. .size OPENSSL_ia32_rdrand,.-OPENSSL_ia32_rdrand
  344. .globl OPENSSL_ia32_rdseed
  345. .type OPENSSL_ia32_rdseed,\@abi-omnipotent
  346. .align 16
  347. OPENSSL_ia32_rdseed:
  348. mov \$8,%ecx
  349. .Loop_rdseed:
  350. rdseed %rax
  351. jc .Lbreak_rdseed
  352. loop .Loop_rdseed
  353. .Lbreak_rdseed:
  354. cmp \$0,%rax
  355. cmove %rcx,%rax
  356. ret
  357. .size OPENSSL_ia32_rdseed,.-OPENSSL_ia32_rdseed
  358. ___
  359. close STDOUT; # flush