x86_64cpuid.pl 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #! /usr/bin/env perl
  2. # Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. $flavour = shift;
  9. $output = shift;
  10. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  11. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  12. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  13. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  14. ( $xlate="${dir}perlasm/x86_64-xlate.pl" and -f $xlate) or
  15. die "can't locate x86_64-xlate.pl";
  16. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
  17. *STDOUT=*OUT;
  18. ($arg1,$arg2,$arg3,$arg4)=$win64?("%rcx","%rdx","%r8", "%r9") : # Win64 order
  19. ("%rdi","%rsi","%rdx","%rcx"); # Unix order
  20. print<<___;
  21. .extern OPENSSL_cpuid_setup
  22. .hidden OPENSSL_cpuid_setup
  23. .section .init
  24. call OPENSSL_cpuid_setup
  25. .hidden OPENSSL_ia32cap_P
  26. .comm OPENSSL_ia32cap_P,16,4
  27. .text
  28. .globl OPENSSL_atomic_add
  29. .type OPENSSL_atomic_add,\@abi-omnipotent
  30. .align 16
  31. OPENSSL_atomic_add:
  32. movl ($arg1),%eax
  33. .Lspin: leaq ($arg2,%rax),%r8
  34. .byte 0xf0 # lock
  35. cmpxchgl %r8d,($arg1)
  36. jne .Lspin
  37. movl %r8d,%eax
  38. .byte 0x48,0x98 # cltq/cdqe
  39. ret
  40. .size OPENSSL_atomic_add,.-OPENSSL_atomic_add
  41. .globl OPENSSL_rdtsc
  42. .type OPENSSL_rdtsc,\@abi-omnipotent
  43. .align 16
  44. OPENSSL_rdtsc:
  45. rdtsc
  46. shl \$32,%rdx
  47. or %rdx,%rax
  48. ret
  49. .size OPENSSL_rdtsc,.-OPENSSL_rdtsc
  50. .globl OPENSSL_ia32_cpuid
  51. .type OPENSSL_ia32_cpuid,\@function,1
  52. .align 16
  53. OPENSSL_ia32_cpuid:
  54. .cfi_startproc
  55. mov %rbx,%r8 # save %rbx
  56. .cfi_register %rbx,%r8
  57. xor %eax,%eax
  58. mov %eax,8(%rdi) # clear extended feature flags
  59. cpuid
  60. mov %eax,%r11d # max value for standard query level
  61. xor %eax,%eax
  62. cmp \$0x756e6547,%ebx # "Genu"
  63. setne %al
  64. mov %eax,%r9d
  65. cmp \$0x49656e69,%edx # "ineI"
  66. setne %al
  67. or %eax,%r9d
  68. cmp \$0x6c65746e,%ecx # "ntel"
  69. setne %al
  70. or %eax,%r9d # 0 indicates Intel CPU
  71. jz .Lintel
  72. cmp \$0x68747541,%ebx # "Auth"
  73. setne %al
  74. mov %eax,%r10d
  75. cmp \$0x69746E65,%edx # "enti"
  76. setne %al
  77. or %eax,%r10d
  78. cmp \$0x444D4163,%ecx # "cAMD"
  79. setne %al
  80. or %eax,%r10d # 0 indicates AMD CPU
  81. jnz .Lintel
  82. # AMD specific
  83. mov \$0x80000000,%eax
  84. cpuid
  85. cmp \$0x80000001,%eax
  86. jb .Lintel
  87. mov %eax,%r10d
  88. mov \$0x80000001,%eax
  89. cpuid
  90. or %ecx,%r9d
  91. and \$0x00000801,%r9d # isolate AMD XOP bit, 1<<11
  92. cmp \$0x80000008,%r10d
  93. jb .Lintel
  94. mov \$0x80000008,%eax
  95. cpuid
  96. movzb %cl,%r10 # number of cores - 1
  97. inc %r10 # number of cores
  98. mov \$1,%eax
  99. cpuid
  100. bt \$28,%edx # test hyper-threading bit
  101. jnc .Lgeneric
  102. shr \$16,%ebx # number of logical processors
  103. cmp %r10b,%bl
  104. ja .Lgeneric
  105. and \$0xefffffff,%edx # ~(1<<28)
  106. jmp .Lgeneric
  107. .Lintel:
  108. cmp \$4,%r11d
  109. mov \$-1,%r10d
  110. jb .Lnocacheinfo
  111. mov \$4,%eax
  112. mov \$0,%ecx # query L1D
  113. cpuid
  114. mov %eax,%r10d
  115. shr \$14,%r10d
  116. and \$0xfff,%r10d # number of cores -1 per L1D
  117. .Lnocacheinfo:
  118. mov \$1,%eax
  119. cpuid
  120. and \$0xbfefffff,%edx # force reserved bits to 0
  121. cmp \$0,%r9d
  122. jne .Lnotintel
  123. or \$0x40000000,%edx # set reserved bit#30 on Intel CPUs
  124. and \$15,%ah
  125. cmp \$15,%ah # examine Family ID
  126. jne .Lnotintel
  127. or \$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR
  128. .Lnotintel:
  129. bt \$28,%edx # test hyper-threading bit
  130. jnc .Lgeneric
  131. and \$0xefffffff,%edx # ~(1<<28)
  132. cmp \$0,%r10d
  133. je .Lgeneric
  134. or \$0x10000000,%edx # 1<<28
  135. shr \$16,%ebx
  136. cmp \$1,%bl # see if cache is shared
  137. ja .Lgeneric
  138. and \$0xefffffff,%edx # ~(1<<28)
  139. .Lgeneric:
  140. and \$0x00000800,%r9d # isolate AMD XOP flag
  141. and \$0xfffff7ff,%ecx
  142. or %ecx,%r9d # merge AMD XOP flag
  143. mov %edx,%r10d # %r9d:%r10d is copy of %ecx:%edx
  144. cmp \$7,%r11d
  145. jb .Lno_extended_info
  146. mov \$7,%eax
  147. xor %ecx,%ecx
  148. cpuid
  149. mov %ebx,8(%rdi) # save extended feature flags
  150. .Lno_extended_info:
  151. bt \$27,%r9d # check OSXSAVE bit
  152. jnc .Lclear_avx
  153. xor %ecx,%ecx # XCR0
  154. .byte 0x0f,0x01,0xd0 # xgetbv
  155. and \$0xe6,%eax # isolate XMM, YMM and ZMM state support
  156. cmp \$0xe6,%eax
  157. je .Ldone
  158. andl \$0xfffeffff,8(%rdi) # clear AVX512F, ~(1<<16)
  159. # note that we don't touch other AVX512
  160. # extensions, because they can be used
  161. # with YMM (without opmasking though)
  162. and \$6,%eax # isolate XMM and YMM state support
  163. cmp \$6,%eax
  164. je .Ldone
  165. .Lclear_avx:
  166. mov \$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11)
  167. and %eax,%r9d # clear AVX, FMA and AMD XOP bits
  168. mov \$0x3fdeffdf,%eax # ~(1<<31|1<<30|1<<21|1<<16|1<<5)
  169. and %eax,8(%rdi) # cleax AVX2 and AVX512* bits
  170. .Ldone:
  171. shl \$32,%r9
  172. mov %r10d,%eax
  173. mov %r8,%rbx # restore %rbx
  174. .cfi_restore %rbx
  175. or %r9,%rax
  176. ret
  177. .cfi_endproc
  178. .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
  179. .globl OPENSSL_cleanse
  180. .type OPENSSL_cleanse,\@abi-omnipotent
  181. .align 16
  182. OPENSSL_cleanse:
  183. xor %rax,%rax
  184. cmp \$15,$arg2
  185. jae .Lot
  186. cmp \$0,$arg2
  187. je .Lret
  188. .Little:
  189. mov %al,($arg1)
  190. sub \$1,$arg2
  191. lea 1($arg1),$arg1
  192. jnz .Little
  193. .Lret:
  194. ret
  195. .align 16
  196. .Lot:
  197. test \$7,$arg1
  198. jz .Laligned
  199. mov %al,($arg1)
  200. lea -1($arg2),$arg2
  201. lea 1($arg1),$arg1
  202. jmp .Lot
  203. .Laligned:
  204. mov %rax,($arg1)
  205. lea -8($arg2),$arg2
  206. test \$-8,$arg2
  207. lea 8($arg1),$arg1
  208. jnz .Laligned
  209. cmp \$0,$arg2
  210. jne .Little
  211. ret
  212. .size OPENSSL_cleanse,.-OPENSSL_cleanse
  213. .globl CRYPTO_memcmp
  214. .type CRYPTO_memcmp,\@abi-omnipotent
  215. .align 16
  216. CRYPTO_memcmp:
  217. xor %rax,%rax
  218. xor %r10,%r10
  219. cmp \$0,$arg3
  220. je .Lno_data
  221. .Loop_cmp:
  222. mov ($arg1),%r10b
  223. lea 1($arg1),$arg1
  224. xor ($arg2),%r10b
  225. lea 1($arg2),$arg2
  226. or %r10b,%al
  227. dec $arg3
  228. jnz .Loop_cmp
  229. neg %rax
  230. shr \$63,%rax
  231. .Lno_data:
  232. ret
  233. .size CRYPTO_memcmp,.-CRYPTO_memcmp
  234. ___
  235. print<<___ if (!$win64);
  236. .globl OPENSSL_wipe_cpu
  237. .type OPENSSL_wipe_cpu,\@abi-omnipotent
  238. .align 16
  239. OPENSSL_wipe_cpu:
  240. pxor %xmm0,%xmm0
  241. pxor %xmm1,%xmm1
  242. pxor %xmm2,%xmm2
  243. pxor %xmm3,%xmm3
  244. pxor %xmm4,%xmm4
  245. pxor %xmm5,%xmm5
  246. pxor %xmm6,%xmm6
  247. pxor %xmm7,%xmm7
  248. pxor %xmm8,%xmm8
  249. pxor %xmm9,%xmm9
  250. pxor %xmm10,%xmm10
  251. pxor %xmm11,%xmm11
  252. pxor %xmm12,%xmm12
  253. pxor %xmm13,%xmm13
  254. pxor %xmm14,%xmm14
  255. pxor %xmm15,%xmm15
  256. xorq %rcx,%rcx
  257. xorq %rdx,%rdx
  258. xorq %rsi,%rsi
  259. xorq %rdi,%rdi
  260. xorq %r8,%r8
  261. xorq %r9,%r9
  262. xorq %r10,%r10
  263. xorq %r11,%r11
  264. leaq 8(%rsp),%rax
  265. ret
  266. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  267. ___
  268. print<<___ if ($win64);
  269. .globl OPENSSL_wipe_cpu
  270. .type OPENSSL_wipe_cpu,\@abi-omnipotent
  271. .align 16
  272. OPENSSL_wipe_cpu:
  273. pxor %xmm0,%xmm0
  274. pxor %xmm1,%xmm1
  275. pxor %xmm2,%xmm2
  276. pxor %xmm3,%xmm3
  277. pxor %xmm4,%xmm4
  278. pxor %xmm5,%xmm5
  279. xorq %rcx,%rcx
  280. xorq %rdx,%rdx
  281. xorq %r8,%r8
  282. xorq %r9,%r9
  283. xorq %r10,%r10
  284. xorq %r11,%r11
  285. leaq 8(%rsp),%rax
  286. ret
  287. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  288. ___
  289. {
  290. my $out="%r10";
  291. my $cnt="%rcx";
  292. my $max="%r11";
  293. my $lasttick="%r8d";
  294. my $lastdiff="%r9d";
  295. my $redzone=win64?8:-8;
  296. print<<___;
  297. .globl OPENSSL_instrument_bus
  298. .type OPENSSL_instrument_bus,\@abi-omnipotent
  299. .align 16
  300. OPENSSL_instrument_bus:
  301. mov $arg1,$out # tribute to Win64
  302. mov $arg2,$cnt
  303. mov $arg2,$max
  304. rdtsc # collect 1st tick
  305. mov %eax,$lasttick # lasttick = tick
  306. mov \$0,$lastdiff # lastdiff = 0
  307. clflush ($out)
  308. .byte 0xf0 # lock
  309. add $lastdiff,($out)
  310. jmp .Loop
  311. .align 16
  312. .Loop: rdtsc
  313. mov %eax,%edx
  314. sub $lasttick,%eax
  315. mov %edx,$lasttick
  316. mov %eax,$lastdiff
  317. clflush ($out)
  318. .byte 0xf0 # lock
  319. add %eax,($out)
  320. lea 4($out),$out
  321. sub \$1,$cnt
  322. jnz .Loop
  323. mov $max,%rax
  324. ret
  325. .size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
  326. .globl OPENSSL_instrument_bus2
  327. .type OPENSSL_instrument_bus2,\@abi-omnipotent
  328. .align 16
  329. OPENSSL_instrument_bus2:
  330. mov $arg1,$out # tribute to Win64
  331. mov $arg2,$cnt
  332. mov $arg3,$max
  333. mov $cnt,$redzone(%rsp)
  334. rdtsc # collect 1st tick
  335. mov %eax,$lasttick # lasttick = tick
  336. mov \$0,$lastdiff # lastdiff = 0
  337. clflush ($out)
  338. .byte 0xf0 # lock
  339. add $lastdiff,($out)
  340. rdtsc # collect 1st diff
  341. mov %eax,%edx
  342. sub $lasttick,%eax # diff
  343. mov %edx,$lasttick # lasttick = tick
  344. mov %eax,$lastdiff # lastdiff = diff
  345. .Loop2:
  346. clflush ($out)
  347. .byte 0xf0 # lock
  348. add %eax,($out) # accumulate diff
  349. sub \$1,$max
  350. jz .Ldone2
  351. rdtsc
  352. mov %eax,%edx
  353. sub $lasttick,%eax # diff
  354. mov %edx,$lasttick # lasttick = tick
  355. cmp $lastdiff,%eax
  356. mov %eax,$lastdiff # lastdiff = diff
  357. mov \$0,%edx
  358. setne %dl
  359. sub %rdx,$cnt # conditional --$cnt
  360. lea ($out,%rdx,4),$out # conditional ++$out
  361. jnz .Loop2
  362. .Ldone2:
  363. mov $redzone(%rsp),%rax
  364. sub $cnt,%rax
  365. ret
  366. .size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
  367. ___
  368. }
  369. sub gen_random {
  370. my $rdop = shift;
  371. print<<___;
  372. .globl OPENSSL_ia32_${rdop}
  373. .type OPENSSL_ia32_${rdop},\@abi-omnipotent
  374. .align 16
  375. OPENSSL_ia32_${rdop}:
  376. mov \$8,%ecx
  377. .Loop_${rdop}:
  378. ${rdop} %rax
  379. jc .Lbreak_${rdop}
  380. loop .Loop_${rdop}
  381. .Lbreak_${rdop}:
  382. cmp \$0,%rax
  383. cmove %rcx,%rax
  384. ret
  385. .size OPENSSL_ia32_${rdop},.-OPENSSL_ia32_${rdop}
  386. .globl OPENSSL_ia32_${rdop}_bytes
  387. .type OPENSSL_ia32_${rdop}_bytes,\@abi-omnipotent
  388. .align 16
  389. OPENSSL_ia32_${rdop}_bytes:
  390. xor %rax, %rax # return value
  391. cmp \$0,$arg2
  392. je .Ldone_${rdop}_bytes
  393. mov \$8,%r11
  394. .Loop_${rdop}_bytes:
  395. ${rdop} %r10
  396. jc .Lbreak_${rdop}_bytes
  397. dec %r11
  398. jnz .Loop_${rdop}_bytes
  399. jmp .Ldone_${rdop}_bytes
  400. .align 16
  401. .Lbreak_${rdop}_bytes:
  402. cmp \$8,$arg2
  403. jb .Ltail_${rdop}_bytes
  404. mov %r10,($arg1)
  405. lea 8($arg1),$arg1
  406. add \$8,%rax
  407. sub \$8,$arg2
  408. jz .Ldone_${rdop}_bytes
  409. mov \$8,%r11
  410. jmp .Loop_${rdop}_bytes
  411. .align 16
  412. .Ltail_${rdop}_bytes:
  413. mov %r10b,($arg1)
  414. lea 1($arg1),$arg1
  415. inc %rax
  416. shr \$8,%r8
  417. dec $arg2
  418. jnz .Ltail_${rdop}_bytes
  419. .Ldone_${rdop}_bytes:
  420. ret
  421. .size OPENSSL_ia32_${rdop}_bytes,.-OPENSSL_ia32_${rdop}_bytes
  422. ___
  423. }
  424. gen_random("rdrand");
  425. gen_random("rdseed");
  426. close STDOUT; # flush