x86cpuid.pl 9.5 KB

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