x86cpuid.pl 5.7 KB

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