x86_64cpuid.pl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #! /usr/bin/env perl
  2. # Copyright 2005-2018 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 %rax,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. movd %eax,%xmm0 # put aside processor id
  121. and \$0xbfefffff,%edx # force reserved bits to 0
  122. cmp \$0,%r9d
  123. jne .Lnotintel
  124. or \$0x40000000,%edx # set reserved bit#30 on Intel CPUs
  125. and \$15,%ah
  126. cmp \$15,%ah # examine Family ID
  127. jne .LnotP4
  128. or \$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR
  129. .LnotP4:
  130. cmp \$6,%ah
  131. jne .Lnotintel
  132. and \$0x0fff0ff0,%eax
  133. cmp \$0x00050670,%eax # Knights Landing
  134. je .Lknights
  135. cmp \$0x00080650,%eax # Knights Mill (according to sde)
  136. jne .Lnotintel
  137. .Lknights:
  138. and \$0xfbffffff,%ecx # clear XSAVE flag to mimic Silvermont
  139. .Lnotintel:
  140. bt \$28,%edx # test hyper-threading bit
  141. jnc .Lgeneric
  142. and \$0xefffffff,%edx # ~(1<<28)
  143. cmp \$0,%r10d
  144. je .Lgeneric
  145. or \$0x10000000,%edx # 1<<28
  146. shr \$16,%ebx
  147. cmp \$1,%bl # see if cache is shared
  148. ja .Lgeneric
  149. and \$0xefffffff,%edx # ~(1<<28)
  150. .Lgeneric:
  151. and \$0x00000800,%r9d # isolate AMD XOP flag
  152. and \$0xfffff7ff,%ecx
  153. or %ecx,%r9d # merge AMD XOP flag
  154. mov %edx,%r10d # %r9d:%r10d is copy of %ecx:%edx
  155. cmp \$7,%r11d
  156. jb .Lno_extended_info
  157. mov \$7,%eax
  158. xor %ecx,%ecx
  159. cpuid
  160. bt \$26,%r9d # check XSAVE bit, cleared on Knights
  161. jc .Lnotknights
  162. and \$0xfff7ffff,%ebx # clear ADCX/ADOX flag
  163. .Lnotknights:
  164. movd %xmm0,%eax # restore processor id
  165. and \$0x0fff0ff0,%eax
  166. cmp \$0x00050650,%eax # Skylake-X
  167. jne .Lnotskylakex
  168. and \$0xfffeffff,%ebx # ~(1<<16)
  169. # suppress AVX512F flag on Skylake-X
  170. .Lnotskylakex:
  171. mov %ebx,8(%rdi) # save extended feature flags
  172. mov %ecx,12(%rdi)
  173. .Lno_extended_info:
  174. bt \$27,%r9d # check OSXSAVE bit
  175. jnc .Lclear_avx
  176. xor %ecx,%ecx # XCR0
  177. .byte 0x0f,0x01,0xd0 # xgetbv
  178. and \$0xe6,%eax # isolate XMM, YMM and ZMM state support
  179. cmp \$0xe6,%eax
  180. je .Ldone
  181. andl \$0x3fdeffff,8(%rdi) # ~(1<<31|1<<30|1<<21|1<<16)
  182. # clear AVX512F+BW+VL+FIMA, all of
  183. # them are EVEX-encoded, which requires
  184. # ZMM state support even if one uses
  185. # only XMM and YMM :-(
  186. and \$6,%eax # isolate XMM and YMM state support
  187. cmp \$6,%eax
  188. je .Ldone
  189. .Lclear_avx:
  190. mov \$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11)
  191. and %eax,%r9d # clear AVX, FMA and AMD XOP bits
  192. mov \$0x3fdeffdf,%eax # ~(1<<31|1<<30|1<<21|1<<16|1<<5)
  193. and %eax,8(%rdi) # clear AVX2 and AVX512* bits
  194. .Ldone:
  195. shl \$32,%r9
  196. mov %r10d,%eax
  197. mov %r8,%rbx # restore %rbx
  198. .cfi_restore %rbx
  199. or %r9,%rax
  200. ret
  201. .cfi_endproc
  202. .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
  203. .globl OPENSSL_cleanse
  204. .type OPENSSL_cleanse,\@abi-omnipotent
  205. .align 16
  206. OPENSSL_cleanse:
  207. xor %rax,%rax
  208. cmp \$15,$arg2
  209. jae .Lot
  210. cmp \$0,$arg2
  211. je .Lret
  212. .Little:
  213. mov %al,($arg1)
  214. sub \$1,$arg2
  215. lea 1($arg1),$arg1
  216. jnz .Little
  217. .Lret:
  218. ret
  219. .align 16
  220. .Lot:
  221. test \$7,$arg1
  222. jz .Laligned
  223. mov %al,($arg1)
  224. lea -1($arg2),$arg2
  225. lea 1($arg1),$arg1
  226. jmp .Lot
  227. .Laligned:
  228. mov %rax,($arg1)
  229. lea -8($arg2),$arg2
  230. test \$-8,$arg2
  231. lea 8($arg1),$arg1
  232. jnz .Laligned
  233. cmp \$0,$arg2
  234. jne .Little
  235. ret
  236. .size OPENSSL_cleanse,.-OPENSSL_cleanse
  237. .globl CRYPTO_memcmp
  238. .type CRYPTO_memcmp,\@abi-omnipotent
  239. .align 16
  240. CRYPTO_memcmp:
  241. xor %rax,%rax
  242. xor %r10,%r10
  243. cmp \$0,$arg3
  244. je .Lno_data
  245. .Loop_cmp:
  246. mov ($arg1),%r10b
  247. lea 1($arg1),$arg1
  248. xor ($arg2),%r10b
  249. lea 1($arg2),$arg2
  250. or %r10b,%al
  251. dec $arg3
  252. jnz .Loop_cmp
  253. neg %rax
  254. shr \$63,%rax
  255. .Lno_data:
  256. ret
  257. .size CRYPTO_memcmp,.-CRYPTO_memcmp
  258. ___
  259. print<<___ if (!$win64);
  260. .globl OPENSSL_wipe_cpu
  261. .type OPENSSL_wipe_cpu,\@abi-omnipotent
  262. .align 16
  263. OPENSSL_wipe_cpu:
  264. pxor %xmm0,%xmm0
  265. pxor %xmm1,%xmm1
  266. pxor %xmm2,%xmm2
  267. pxor %xmm3,%xmm3
  268. pxor %xmm4,%xmm4
  269. pxor %xmm5,%xmm5
  270. pxor %xmm6,%xmm6
  271. pxor %xmm7,%xmm7
  272. pxor %xmm8,%xmm8
  273. pxor %xmm9,%xmm9
  274. pxor %xmm10,%xmm10
  275. pxor %xmm11,%xmm11
  276. pxor %xmm12,%xmm12
  277. pxor %xmm13,%xmm13
  278. pxor %xmm14,%xmm14
  279. pxor %xmm15,%xmm15
  280. xorq %rcx,%rcx
  281. xorq %rdx,%rdx
  282. xorq %rsi,%rsi
  283. xorq %rdi,%rdi
  284. xorq %r8,%r8
  285. xorq %r9,%r9
  286. xorq %r10,%r10
  287. xorq %r11,%r11
  288. leaq 8(%rsp),%rax
  289. ret
  290. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  291. ___
  292. print<<___ if ($win64);
  293. .globl OPENSSL_wipe_cpu
  294. .type OPENSSL_wipe_cpu,\@abi-omnipotent
  295. .align 16
  296. OPENSSL_wipe_cpu:
  297. pxor %xmm0,%xmm0
  298. pxor %xmm1,%xmm1
  299. pxor %xmm2,%xmm2
  300. pxor %xmm3,%xmm3
  301. pxor %xmm4,%xmm4
  302. pxor %xmm5,%xmm5
  303. xorq %rcx,%rcx
  304. xorq %rdx,%rdx
  305. xorq %r8,%r8
  306. xorq %r9,%r9
  307. xorq %r10,%r10
  308. xorq %r11,%r11
  309. leaq 8(%rsp),%rax
  310. ret
  311. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  312. ___
  313. {
  314. my $out="%r10";
  315. my $cnt="%rcx";
  316. my $max="%r11";
  317. my $lasttick="%r8d";
  318. my $lastdiff="%r9d";
  319. my $redzone=win64?8:-8;
  320. print<<___;
  321. .globl OPENSSL_instrument_bus
  322. .type OPENSSL_instrument_bus,\@abi-omnipotent
  323. .align 16
  324. OPENSSL_instrument_bus:
  325. mov $arg1,$out # tribute to Win64
  326. mov $arg2,$cnt
  327. mov $arg2,$max
  328. rdtsc # collect 1st tick
  329. mov %eax,$lasttick # lasttick = tick
  330. mov \$0,$lastdiff # lastdiff = 0
  331. clflush ($out)
  332. .byte 0xf0 # lock
  333. add $lastdiff,($out)
  334. jmp .Loop
  335. .align 16
  336. .Loop: rdtsc
  337. mov %eax,%edx
  338. sub $lasttick,%eax
  339. mov %edx,$lasttick
  340. mov %eax,$lastdiff
  341. clflush ($out)
  342. .byte 0xf0 # lock
  343. add %eax,($out)
  344. lea 4($out),$out
  345. sub \$1,$cnt
  346. jnz .Loop
  347. mov $max,%rax
  348. ret
  349. .size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
  350. .globl OPENSSL_instrument_bus2
  351. .type OPENSSL_instrument_bus2,\@abi-omnipotent
  352. .align 16
  353. OPENSSL_instrument_bus2:
  354. mov $arg1,$out # tribute to Win64
  355. mov $arg2,$cnt
  356. mov $arg3,$max
  357. mov $cnt,$redzone(%rsp)
  358. rdtsc # collect 1st tick
  359. mov %eax,$lasttick # lasttick = tick
  360. mov \$0,$lastdiff # lastdiff = 0
  361. clflush ($out)
  362. .byte 0xf0 # lock
  363. add $lastdiff,($out)
  364. rdtsc # collect 1st diff
  365. mov %eax,%edx
  366. sub $lasttick,%eax # diff
  367. mov %edx,$lasttick # lasttick = tick
  368. mov %eax,$lastdiff # lastdiff = diff
  369. .Loop2:
  370. clflush ($out)
  371. .byte 0xf0 # lock
  372. add %eax,($out) # accumulate diff
  373. sub \$1,$max
  374. jz .Ldone2
  375. rdtsc
  376. mov %eax,%edx
  377. sub $lasttick,%eax # diff
  378. mov %edx,$lasttick # lasttick = tick
  379. cmp $lastdiff,%eax
  380. mov %eax,$lastdiff # lastdiff = diff
  381. mov \$0,%edx
  382. setne %dl
  383. sub %rdx,$cnt # conditional --$cnt
  384. lea ($out,%rdx,4),$out # conditional ++$out
  385. jnz .Loop2
  386. .Ldone2:
  387. mov $redzone(%rsp),%rax
  388. sub $cnt,%rax
  389. ret
  390. .size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
  391. ___
  392. }
  393. sub gen_random {
  394. my $rdop = shift;
  395. print<<___;
  396. .globl OPENSSL_ia32_${rdop}_bytes
  397. .type OPENSSL_ia32_${rdop}_bytes,\@abi-omnipotent
  398. .align 16
  399. OPENSSL_ia32_${rdop}_bytes:
  400. xor %rax, %rax # return value
  401. cmp \$0,$arg2
  402. je .Ldone_${rdop}_bytes
  403. mov \$8,%r11
  404. .Loop_${rdop}_bytes:
  405. ${rdop} %r10
  406. jc .Lbreak_${rdop}_bytes
  407. dec %r11
  408. jnz .Loop_${rdop}_bytes
  409. jmp .Ldone_${rdop}_bytes
  410. .align 16
  411. .Lbreak_${rdop}_bytes:
  412. cmp \$8,$arg2
  413. jb .Ltail_${rdop}_bytes
  414. mov %r10,($arg1)
  415. lea 8($arg1),$arg1
  416. add \$8,%rax
  417. sub \$8,$arg2
  418. jz .Ldone_${rdop}_bytes
  419. mov \$8,%r11
  420. jmp .Loop_${rdop}_bytes
  421. .align 16
  422. .Ltail_${rdop}_bytes:
  423. mov %r10b,($arg1)
  424. lea 1($arg1),$arg1
  425. inc %rax
  426. shr \$8,%r10
  427. dec $arg2
  428. jnz .Ltail_${rdop}_bytes
  429. .Ldone_${rdop}_bytes:
  430. xor %r10,%r10 # Clear sensitive data from register
  431. ret
  432. .size OPENSSL_ia32_${rdop}_bytes,.-OPENSSL_ia32_${rdop}_bytes
  433. ___
  434. }
  435. gen_random("rdrand");
  436. gen_random("rdseed");
  437. close STDOUT; # flush