x86_64cpuid.pl 7.1 KB

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