x86cpuid.pl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/usr/bin/env perl
  2. push(@INC,"perlasm");
  3. require "x86asm.pl";
  4. &asm_init($ARGV[0],"x86cpuid");
  5. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  6. &function_begin("OPENSSL_ia32_cpuid");
  7. &xor ("edx","edx");
  8. &pushf ();
  9. &pop ("eax");
  10. &mov ("ecx","eax");
  11. &xor ("eax",1<<21);
  12. &push ("eax");
  13. &popf ();
  14. &pushf ();
  15. &pop ("eax");
  16. &xor ("ecx","eax");
  17. &bt ("ecx",21);
  18. &jnc (&label("nocpuid"));
  19. &mov ("eax",1);
  20. &cpuid ();
  21. &set_label("nocpuid");
  22. &mov ("eax","edx");
  23. &mov ("edx","ecx");
  24. &function_end("OPENSSL_ia32_cpuid");
  25. &external_label("OPENSSL_ia32cap_P");
  26. &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  27. &xor ("eax","eax");
  28. &xor ("edx","edx");
  29. &picmeup("ecx","OPENSSL_ia32cap_P");
  30. &bt (&DWP(0,"ecx"),4);
  31. &jnc (&label("notsc"));
  32. &rdtsc ();
  33. &set_label("notsc");
  34. &ret ();
  35. &function_end_B("OPENSSL_rdtsc");
  36. # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
  37. # but it's safe to call it on any [supported] 32-bit platform...
  38. # Just check for [non-]zero return value...
  39. &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  40. &picmeup("ecx","OPENSSL_ia32cap_P");
  41. &bt (&DWP(0,"ecx"),4);
  42. &jnc (&label("nohalt")); # no TSC
  43. &data_word(0x9058900e); # push %cs; pop %eax
  44. &and ("eax",3);
  45. &jnz (&label("nohalt")); # not enough privileges
  46. &pushf ();
  47. &pop ("eax")
  48. &bt ("eax",9);
  49. &jnc (&label("nohalt")); # interrupts are disabled
  50. &rdtsc ();
  51. &push ("edx");
  52. &push ("eax");
  53. &halt ();
  54. &rdtsc ();
  55. &sub ("eax",&DWP(0,"esp"));
  56. &sbb ("edx",&DWP(4,"esp"));
  57. &add ("esp",8);
  58. &ret ();
  59. &set_label("nohalt");
  60. &xor ("eax","eax");
  61. &xor ("edx","edx");
  62. &ret ();
  63. &function_end_B("OPENSSL_instrument_halt");
  64. # Essentially there is only one use for this function. Under DJGPP:
  65. #
  66. # #include <go32.h>
  67. # ...
  68. # i=OPENSSL_far_spin(_dos_ds,0x46c);
  69. # ...
  70. # to obtain the number of spins till closest timer interrupt.
  71. &function_begin_B("OPENSSL_far_spin");
  72. &pushf ();
  73. &pop ("eax")
  74. &bt ("eax",9);
  75. &jnc (&label("nospin")); # interrupts are disabled
  76. &mov ("eax",&DWP(4,"esp"));
  77. &mov ("ecx",&DWP(8,"esp"));
  78. &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
  79. &xor ("eax","eax");
  80. &mov ("edx",&DWP(0,"ecx"));
  81. &jmp (&label("spin"));
  82. &align (16);
  83. &set_label("spin");
  84. &inc ("eax");
  85. &cmp ("edx",&DWP(0,"ecx"));
  86. &je (&label("spin"));
  87. &data_word (0x1f909090); # pop %ds
  88. &ret ();
  89. &set_label("nospin");
  90. &xor ("eax","eax");
  91. &xor ("edx","edx");
  92. &ret ();
  93. &function_end_B("OPENSSL_far_spin");
  94. &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  95. &xor ("eax","eax");
  96. &xor ("edx","edx");
  97. &picmeup("ecx","OPENSSL_ia32cap_P");
  98. &mov ("ecx",&DWP(0,"ecx"));
  99. &bt (&DWP(0,"ecx"),1);
  100. &jnc (&label("no_x87"));
  101. if ($sse2) {
  102. &bt (&DWP(0,"ecx"),26);
  103. &jnc (&label("no_sse2"));
  104. &pxor ("xmm0","xmm0");
  105. &pxor ("xmm1","xmm1");
  106. &pxor ("xmm2","xmm2");
  107. &pxor ("xmm3","xmm3");
  108. &pxor ("xmm4","xmm4");
  109. &pxor ("xmm5","xmm5");
  110. &pxor ("xmm6","xmm6");
  111. &pxor ("xmm7","xmm7");
  112. &set_label("no_sse2");
  113. }
  114. # just a bunch of fldz to zap the fp/mm bank followed by finit...
  115. &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
  116. &set_label("no_x87");
  117. &lea ("eax",&DWP(4,"esp"));
  118. &ret ();
  119. &function_end_B("OPENSSL_wipe_cpu");
  120. &function_begin_B("OPENSSL_atomic_add");
  121. &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
  122. &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
  123. &push ("ebx");
  124. &nop ();
  125. &mov ("eax",&DWP(0,"edx"));
  126. &set_label("spin");
  127. &lea ("ebx",&DWP(0,"eax","ecx"));
  128. &nop ();
  129. &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
  130. &jne (&label("spin"));
  131. &mov ("eax","ebx"); # OpenSSL expects the new value
  132. &pop ("ebx");
  133. &ret ();
  134. &function_end_B("OPENSSL_atomic_add");
  135. # This function can become handy under Win32 in situations when
  136. # we don't know which calling convention, __stdcall or __cdecl(*),
  137. # indirect callee is using. In C it can be deployed as
  138. #
  139. #ifdef OPENSSL_CPUID_OBJ
  140. # type OPENSSL_indirect_call(void *f,...);
  141. # ...
  142. # OPENSSL_indirect_call(func,[up to $max arguments]);
  143. #endif
  144. #
  145. # (*) it's designed to work even for __fastcall if number of
  146. # arguments is 1 or 2!
  147. &function_begin_B("OPENSSL_indirect_call");
  148. {
  149. my $i,$max=7; # $max has to be chosen as 4*n-1
  150. # in order to preserve eventual
  151. # stack alignment
  152. &push ("ebp");
  153. &mov ("ebp","esp");
  154. &sub ("esp",$max*4);
  155. &mov ("ecx",&DWP(12,"ebp"));
  156. &mov (&DWP(0,"esp"),"ecx");
  157. &mov ("edx",&DWP(16,"ebp"));
  158. &mov (&DWP(4,"esp"),"edx");
  159. for($i=2;$i<$max;$i++)
  160. {
  161. # Some copies will be redundant/bogus...
  162. &mov ("eax",&DWP(12+$i*4,"ebp"));
  163. &mov (&DWP(0+$i*4,"esp"),"eax");
  164. }
  165. &call_ptr (&DWP(8,"ebp"));# make the call...
  166. &mov ("esp","ebp"); # ... and just restore the stack pointer
  167. # without paying attention to what we called,
  168. # (__cdecl *func) or (__stdcall *one).
  169. &pop ("ebp");
  170. &ret ();
  171. }
  172. &function_end_B("OPENSSL_indirect_call");
  173. &initseg("OPENSSL_cpuid_setup");
  174. &asm_finish();