2
0

x86_64cpuid.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. open STDOUT,"| $^X perlasm/x86_64-xlate.pl $output";
  92. print<<___;
  93. .text
  94. .globl OPENSSL_rdtsc
  95. .type OPENSSL_rdtsc,\@abi-omnipotent
  96. .align 16
  97. OPENSSL_rdtsc:
  98. rdtsc
  99. shl \$32,%rdx
  100. or %rdx,%rax
  101. ret
  102. .size OPENSSL_rdtsc,.-OPENSSL_rdtsc
  103. .globl OPENSSL_ia32_cpuid
  104. .type OPENSSL_ia32_cpuid,\@abi-omnipotent
  105. .align 16
  106. OPENSSL_ia32_cpuid:
  107. mov %rbx,%r8
  108. xor %eax,%eax
  109. cpuid
  110. xor %eax,%eax
  111. cmp \$0x756e6547,%ebx # "Genu"
  112. setne %al
  113. mov %eax,%r9d
  114. cmp \$0x49656e69,%edx # "ineI"
  115. setne %al
  116. or %eax,%r9d
  117. cmp \$0x6c65746e,%ecx # "ntel"
  118. setne %al
  119. or %eax,%r9d
  120. mov \$1,%eax
  121. cpuid
  122. cmp \$0,%r9d
  123. jne .Lnotintel
  124. or \$0x00100000,%edx # use reserved 20th bit to engage RC4_CHAR
  125. and \$15,%ah
  126. cmp \$15,%ah # examine Family ID
  127. je .Lnotintel
  128. or \$0x40000000,%edx # use reserved 30th bit to skip unrolled loop
  129. .Lnotintel:
  130. bt \$28,%edx # test hyper-threading bit
  131. jnc .Ldone
  132. shr \$16,%ebx
  133. cmp \$1,%bl # see if cache is shared
  134. ja .Ldone
  135. and \$0xefffffff,%edx # ~(1<<28)
  136. .Ldone:
  137. shl \$32,%rcx
  138. mov %edx,%eax
  139. mov %r8,%rbx
  140. or %rcx,%rax
  141. ret
  142. .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
  143. ___
  144. close STDOUT; # flush