2
0

x86cpuid.pl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #!/usr/bin/env perl
  2. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  3. push(@INC, "${dir}perlasm", "perlasm");
  4. require "x86asm.pl";
  5. &asm_init($ARGV[0],"x86cpuid");
  6. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  7. &function_begin("OPENSSL_ia32_cpuid");
  8. &xor ("edx","edx");
  9. &pushf ();
  10. &pop ("eax");
  11. &mov ("ecx","eax");
  12. &xor ("eax",1<<21);
  13. &push ("eax");
  14. &popf ();
  15. &pushf ();
  16. &pop ("eax");
  17. &xor ("ecx","eax");
  18. &bt ("ecx",21);
  19. &jnc (&label("done"));
  20. &xor ("eax","eax");
  21. &cpuid ();
  22. &xor ("eax","eax");
  23. &cmp ("ebx",0x756e6547); # "Genu"
  24. &setne (&LB("eax"));
  25. &mov ("ebp","eax");
  26. &cmp ("edx",0x49656e69); # "ineI"
  27. &setne (&LB("eax"));
  28. &or ("ebp","eax");
  29. &cmp ("ecx",0x6c65746e); # "ntel"
  30. &setne (&LB("eax"));
  31. &or ("ebp","eax");
  32. &mov ("eax",1);
  33. &cpuid ();
  34. &cmp ("ebp",0);
  35. &jne (&label("notP4"));
  36. &and (&HB("eax"),15); # familiy ID
  37. &cmp (&HB("eax"),15); # 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. &cmp (&LB("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. &function_begin_B("OPENSSL_cleanse");
  202. &mov ("edx",&wparam(0));
  203. &mov ("ecx",&wparam(1));
  204. &xor ("eax","eax");
  205. &cmp ("ecx",7);
  206. &jae (&label("lot"));
  207. &set_label("little");
  208. &mov (&BP(0,"edx"),"al");
  209. &sub ("ecx",1);
  210. &lea ("edx",&DWP(1,"edx"));
  211. &jnz (&label("little"));
  212. &ret ();
  213. &set_label("lot",16);
  214. &test ("edx",3);
  215. &jz (&label("aligned"));
  216. &mov (&BP(0,"edx"),"al");
  217. &lea ("ecx",&DWP(-1,"ecx"));
  218. &lea ("edx",&DWP(1,"edx"));
  219. &jmp (&label("lot"));
  220. &set_label("aligned");
  221. &mov (&DWP(0,"edx"),"eax");
  222. &lea ("ecx",&DWP(-4,"ecx"));
  223. &test ("ecx",-4);
  224. &lea ("edx",&DWP(4,"edx"));
  225. &jnz (&label("aligned"));
  226. &cmp ("ecx",0);
  227. &jne (&label("little"));
  228. &ret ();
  229. &function_end_B("OPENSSL_cleanse");
  230. &initseg("OPENSSL_cpuid_setup");
  231. &asm_finish();