2
0

x86cpuid.pl 5.7 KB

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