x86cpuid.pl 5.6 KB

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