x86_64cpuid.pl 3.3 KB

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