x86cpuid.pl 11 KB

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