x86_64cpuid.pl 10 KB

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