x86_64cpuid.pl 10 KB

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