x86cpuid.pl 12 KB

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