x86cpuid.pl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. &mov ("edi","eax"); # max value for standard query level
  23. &xor ("eax","eax");
  24. &cmp ("ebx",0x756e6547); # "Genu"
  25. &setne (&LB("eax"));
  26. &mov ("ebp","eax");
  27. &cmp ("edx",0x49656e69); # "ineI"
  28. &setne (&LB("eax"));
  29. &or ("ebp","eax");
  30. &cmp ("ecx",0x6c65746e); # "ntel"
  31. &setne (&LB("eax"));
  32. &or ("ebp","eax"); # 0 indicates Intel CPU
  33. &jz (&label("intel"));
  34. &cmp ("ebx",0x68747541); # "Auth"
  35. &setne (&LB("eax"));
  36. &mov ("esi","eax");
  37. &cmp ("edx",0x69746E65); # "enti"
  38. &setne (&LB("eax"));
  39. &or ("esi","eax");
  40. &cmp ("ecx",0x444D4163); # "cAMD"
  41. &setne (&LB("eax"));
  42. &or ("esi","eax"); # 0 indicates AMD CPU
  43. &jnz (&label("intel"));
  44. # AMD specific
  45. &mov ("eax",0x80000000);
  46. &cpuid ();
  47. &cmp ("eax",0x80000008);
  48. &jb (&label("intel"));
  49. &mov ("eax",0x80000008);
  50. &cpuid ();
  51. &movz ("esi",&LB("ecx")); # number of cores - 1
  52. &inc ("esi"); # number of cores
  53. &mov ("eax",1);
  54. &cpuid ();
  55. &bt ("edx",28);
  56. &jnc (&label("done"));
  57. &shr ("ebx",16);
  58. &and ("ebx",0xff);
  59. &cmp ("ebx","esi");
  60. &ja (&label("done"));
  61. &and ("edx",0xefffffff); # clear hyper-threading bit
  62. &jmp (&label("done"));
  63. &set_label("intel");
  64. &cmp ("edi",4);
  65. &mov ("edi",-1);
  66. &jb (&label("nocacheinfo"));
  67. &mov ("eax",4);
  68. &mov ("ecx",0); # query L1D
  69. &cpuid ();
  70. &mov ("edi","eax");
  71. &shr ("edi",14);
  72. &and ("edi",0xfff); # number of cores -1 per L1D
  73. &set_label("nocacheinfo");
  74. &mov ("eax",1);
  75. &cpuid ();
  76. &cmp ("ebp",0);
  77. &jne (&label("notP4"));
  78. &and (&HB("eax"),15); # familiy ID
  79. &cmp (&HB("eax"),15); # P4?
  80. &jne (&label("notP4"));
  81. &or ("edx",1<<20); # use reserved bit to engage RC4_CHAR
  82. &set_label("notP4");
  83. &bt ("edx",28); # test hyper-threading bit
  84. &jnc (&label("done"));
  85. &and ("edx",0xefffffff);
  86. &cmp ("edi",0);
  87. &je (&label("done"));
  88. &or ("edx",0x10000000);
  89. &shr ("ebx",16);
  90. &cmp (&LB("ebx"),1);
  91. &ja (&label("done"));
  92. &and ("edx",0xefffffff); # clear hyper-threading bit if not
  93. &set_label("done");
  94. &mov ("eax","edx");
  95. &mov ("edx","ecx");
  96. &function_end("OPENSSL_ia32_cpuid");
  97. &external_label("OPENSSL_ia32cap_P");
  98. &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  99. &xor ("eax","eax");
  100. &xor ("edx","edx");
  101. &picmeup("ecx","OPENSSL_ia32cap_P");
  102. &bt (&DWP(0,"ecx"),4);
  103. &jnc (&label("notsc"));
  104. &rdtsc ();
  105. &set_label("notsc");
  106. &ret ();
  107. &function_end_B("OPENSSL_rdtsc");
  108. # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
  109. # but it's safe to call it on any [supported] 32-bit platform...
  110. # Just check for [non-]zero return value...
  111. &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  112. &picmeup("ecx","OPENSSL_ia32cap_P");
  113. &bt (&DWP(0,"ecx"),4);
  114. &jnc (&label("nohalt")); # no TSC
  115. &data_word(0x9058900e); # push %cs; pop %eax
  116. &and ("eax",3);
  117. &jnz (&label("nohalt")); # not enough privileges
  118. &pushf ();
  119. &pop ("eax")
  120. &bt ("eax",9);
  121. &jnc (&label("nohalt")); # interrupts are disabled
  122. &rdtsc ();
  123. &push ("edx");
  124. &push ("eax");
  125. &halt ();
  126. &rdtsc ();
  127. &sub ("eax",&DWP(0,"esp"));
  128. &sbb ("edx",&DWP(4,"esp"));
  129. &add ("esp",8);
  130. &ret ();
  131. &set_label("nohalt");
  132. &xor ("eax","eax");
  133. &xor ("edx","edx");
  134. &ret ();
  135. &function_end_B("OPENSSL_instrument_halt");
  136. # Essentially there is only one use for this function. Under DJGPP:
  137. #
  138. # #include <go32.h>
  139. # ...
  140. # i=OPENSSL_far_spin(_dos_ds,0x46c);
  141. # ...
  142. # to obtain the number of spins till closest timer interrupt.
  143. &function_begin_B("OPENSSL_far_spin");
  144. &pushf ();
  145. &pop ("eax")
  146. &bt ("eax",9);
  147. &jnc (&label("nospin")); # interrupts are disabled
  148. &mov ("eax",&DWP(4,"esp"));
  149. &mov ("ecx",&DWP(8,"esp"));
  150. &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
  151. &xor ("eax","eax");
  152. &mov ("edx",&DWP(0,"ecx"));
  153. &jmp (&label("spin"));
  154. &align (16);
  155. &set_label("spin");
  156. &inc ("eax");
  157. &cmp ("edx",&DWP(0,"ecx"));
  158. &je (&label("spin"));
  159. &data_word (0x1f909090); # pop %ds
  160. &ret ();
  161. &set_label("nospin");
  162. &xor ("eax","eax");
  163. &xor ("edx","edx");
  164. &ret ();
  165. &function_end_B("OPENSSL_far_spin");
  166. &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  167. &xor ("eax","eax");
  168. &xor ("edx","edx");
  169. &picmeup("ecx","OPENSSL_ia32cap_P");
  170. &mov ("ecx",&DWP(0,"ecx"));
  171. &bt (&DWP(0,"ecx"),1);
  172. &jnc (&label("no_x87"));
  173. if ($sse2) {
  174. &bt (&DWP(0,"ecx"),26);
  175. &jnc (&label("no_sse2"));
  176. &pxor ("xmm0","xmm0");
  177. &pxor ("xmm1","xmm1");
  178. &pxor ("xmm2","xmm2");
  179. &pxor ("xmm3","xmm3");
  180. &pxor ("xmm4","xmm4");
  181. &pxor ("xmm5","xmm5");
  182. &pxor ("xmm6","xmm6");
  183. &pxor ("xmm7","xmm7");
  184. &set_label("no_sse2");
  185. }
  186. # just a bunch of fldz to zap the fp/mm bank followed by finit...
  187. &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
  188. &set_label("no_x87");
  189. &lea ("eax",&DWP(4,"esp"));
  190. &ret ();
  191. &function_end_B("OPENSSL_wipe_cpu");
  192. &function_begin_B("OPENSSL_atomic_add");
  193. &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
  194. &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
  195. &push ("ebx");
  196. &nop ();
  197. &mov ("eax",&DWP(0,"edx"));
  198. &set_label("spin");
  199. &lea ("ebx",&DWP(0,"eax","ecx"));
  200. &nop ();
  201. &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
  202. &jne (&label("spin"));
  203. &mov ("eax","ebx"); # OpenSSL expects the new value
  204. &pop ("ebx");
  205. &ret ();
  206. &function_end_B("OPENSSL_atomic_add");
  207. # This function can become handy under Win32 in situations when
  208. # we don't know which calling convention, __stdcall or __cdecl(*),
  209. # indirect callee is using. In C it can be deployed as
  210. #
  211. #ifdef OPENSSL_CPUID_OBJ
  212. # type OPENSSL_indirect_call(void *f,...);
  213. # ...
  214. # OPENSSL_indirect_call(func,[up to $max arguments]);
  215. #endif
  216. #
  217. # (*) it's designed to work even for __fastcall if number of
  218. # arguments is 1 or 2!
  219. &function_begin_B("OPENSSL_indirect_call");
  220. {
  221. my $i,$max=7; # $max has to be chosen as 4*n-1
  222. # in order to preserve eventual
  223. # stack alignment
  224. &push ("ebp");
  225. &mov ("ebp","esp");
  226. &sub ("esp",$max*4);
  227. &mov ("ecx",&DWP(12,"ebp"));
  228. &mov (&DWP(0,"esp"),"ecx");
  229. &mov ("edx",&DWP(16,"ebp"));
  230. &mov (&DWP(4,"esp"),"edx");
  231. for($i=2;$i<$max;$i++)
  232. {
  233. # Some copies will be redundant/bogus...
  234. &mov ("eax",&DWP(12+$i*4,"ebp"));
  235. &mov (&DWP(0+$i*4,"esp"),"eax");
  236. }
  237. &call_ptr (&DWP(8,"ebp"));# make the call...
  238. &mov ("esp","ebp"); # ... and just restore the stack pointer
  239. # without paying attention to what we called,
  240. # (__cdecl *func) or (__stdcall *one).
  241. &pop ("ebp");
  242. &ret ();
  243. }
  244. &function_end_B("OPENSSL_indirect_call");
  245. &function_begin_B("OPENSSL_cleanse");
  246. &mov ("edx",&wparam(0));
  247. &mov ("ecx",&wparam(1));
  248. &xor ("eax","eax");
  249. &cmp ("ecx",7);
  250. &jae (&label("lot"));
  251. &set_label("little");
  252. &mov (&BP(0,"edx"),"al");
  253. &sub ("ecx",1);
  254. &lea ("edx",&DWP(1,"edx"));
  255. &jnz (&label("little"));
  256. &ret ();
  257. &set_label("lot",16);
  258. &test ("edx",3);
  259. &jz (&label("aligned"));
  260. &mov (&BP(0,"edx"),"al");
  261. &lea ("ecx",&DWP(-1,"ecx"));
  262. &lea ("edx",&DWP(1,"edx"));
  263. &jmp (&label("lot"));
  264. &set_label("aligned");
  265. &mov (&DWP(0,"edx"),"eax");
  266. &lea ("ecx",&DWP(-4,"ecx"));
  267. &test ("ecx",-4);
  268. &lea ("edx",&DWP(4,"edx"));
  269. &jnz (&label("aligned"));
  270. &cmp ("ecx",0);
  271. &jne (&label("little"));
  272. &ret ();
  273. &function_end_B("OPENSSL_cleanse");
  274. &initseg("OPENSSL_cpuid_setup");
  275. &asm_finish();