x86_64cpuid.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. open STDOUT,"| $^X ${dir}perlasm/x86_64-xlate.pl $flavour $output";
  8. if ($win64) { $arg1="%rcx"; $arg2="%rdx"; }
  9. else { $arg1="%rdi"; $arg2="%rsi"; }
  10. print<<___;
  11. .extern OPENSSL_cpuid_setup
  12. .section .init
  13. call OPENSSL_cpuid_setup
  14. .text
  15. .globl OPENSSL_atomic_add
  16. .type OPENSSL_atomic_add,\@abi-omnipotent
  17. .align 16
  18. OPENSSL_atomic_add:
  19. movl ($arg1),%eax
  20. .Lspin: leaq ($arg2,%rax),%r8
  21. .byte 0xf0 # lock
  22. cmpxchgl %r8d,($arg1)
  23. jne .Lspin
  24. movl %r8d,%eax
  25. .byte 0x48,0x98 # cltq/cdqe
  26. ret
  27. .size OPENSSL_atomic_add,.-OPENSSL_atomic_add
  28. .globl OPENSSL_rdtsc
  29. .type OPENSSL_rdtsc,\@abi-omnipotent
  30. .align 16
  31. OPENSSL_rdtsc:
  32. rdtsc
  33. shl \$32,%rdx
  34. or %rdx,%rax
  35. ret
  36. .size OPENSSL_rdtsc,.-OPENSSL_rdtsc
  37. .globl OPENSSL_ia32_cpuid
  38. .type OPENSSL_ia32_cpuid,\@abi-omnipotent
  39. .align 16
  40. OPENSSL_ia32_cpuid:
  41. mov %rbx,%r8
  42. xor %eax,%eax
  43. cpuid
  44. xor %eax,%eax
  45. cmp \$0x756e6547,%ebx # "Genu"
  46. setne %al
  47. mov %eax,%r9d
  48. cmp \$0x49656e69,%edx # "ineI"
  49. setne %al
  50. or %eax,%r9d
  51. cmp \$0x6c65746e,%ecx # "ntel"
  52. setne %al
  53. or %eax,%r9d
  54. mov \$1,%eax
  55. cpuid
  56. cmp \$0,%r9d
  57. jne .Lnotintel
  58. or \$0x00100000,%edx # use reserved 20th bit to engage RC4_CHAR
  59. and \$15,%ah
  60. cmp \$15,%ah # examine Family ID
  61. je .Lnotintel
  62. or \$0x40000000,%edx # use reserved bit to skip unrolled loop
  63. .Lnotintel:
  64. bt \$28,%edx # test hyper-threading bit
  65. jnc .Ldone
  66. shr \$16,%ebx
  67. cmp \$1,%bl # see if cache is shared
  68. ja .Ldone
  69. and \$0xefffffff,%edx # ~(1<<28)
  70. .Ldone:
  71. shl \$32,%rcx
  72. mov %edx,%eax
  73. mov %r8,%rbx
  74. or %rcx,%rax
  75. ret
  76. .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
  77. .globl OPENSSL_cleanse
  78. .type OPENSSL_cleanse,\@abi-omnipotent
  79. .align 16
  80. OPENSSL_cleanse:
  81. xor %rax,%rax
  82. cmp \$15,$arg2
  83. jae .Lot
  84. .Little:
  85. mov %al,($arg1)
  86. sub \$1,$arg2
  87. lea 1($arg1),$arg1
  88. jnz .Little
  89. ret
  90. .align 16
  91. .Lot:
  92. test \$7,$arg1
  93. jz .Laligned
  94. mov %al,($arg1)
  95. lea -1($arg2),$arg2
  96. lea 1($arg1),$arg1
  97. jmp .Lot
  98. .Laligned:
  99. mov %rax,($arg1)
  100. lea -8($arg2),$arg2
  101. test \$-8,$arg2
  102. lea 8($arg1),$arg1
  103. jnz .Laligned
  104. cmp \$0,$arg2
  105. jne .Little
  106. ret
  107. .size OPENSSL_cleanse,.-OPENSSL_cleanse
  108. ___
  109. print<<___ if (!$win64);
  110. .globl OPENSSL_wipe_cpu
  111. .type OPENSSL_wipe_cpu,\@abi-omnipotent
  112. .align 16
  113. OPENSSL_wipe_cpu:
  114. pxor %xmm0,%xmm0
  115. pxor %xmm1,%xmm1
  116. pxor %xmm2,%xmm2
  117. pxor %xmm3,%xmm3
  118. pxor %xmm4,%xmm4
  119. pxor %xmm5,%xmm5
  120. pxor %xmm6,%xmm6
  121. pxor %xmm7,%xmm7
  122. pxor %xmm8,%xmm8
  123. pxor %xmm9,%xmm9
  124. pxor %xmm10,%xmm10
  125. pxor %xmm11,%xmm11
  126. pxor %xmm12,%xmm12
  127. pxor %xmm13,%xmm13
  128. pxor %xmm14,%xmm14
  129. pxor %xmm15,%xmm15
  130. xorq %rcx,%rcx
  131. xorq %rdx,%rdx
  132. xorq %rsi,%rsi
  133. xorq %rdi,%rdi
  134. xorq %r8,%r8
  135. xorq %r9,%r9
  136. xorq %r10,%r10
  137. xorq %r11,%r11
  138. leaq 8(%rsp),%rax
  139. ret
  140. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  141. ___
  142. print<<___ if ($win64);
  143. .globl OPENSSL_wipe_cpu
  144. .type OPENSSL_wipe_cpu,\@abi-omnipotent
  145. .align 16
  146. OPENSSL_wipe_cpu:
  147. pxor %xmm0,%xmm0
  148. pxor %xmm1,%xmm1
  149. pxor %xmm2,%xmm2
  150. pxor %xmm3,%xmm3
  151. pxor %xmm4,%xmm4
  152. pxor %xmm5,%xmm5
  153. xorq %rcx,%rcx
  154. xorq %rdx,%rdx
  155. xorq %r8,%r8
  156. xorq %r9,%r9
  157. xorq %r10,%r10
  158. xorq %r11,%r11
  159. leaq 8(%rsp),%rax
  160. ret
  161. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  162. ___
  163. close STDOUT; # flush