2
0

x86cpuid.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #! /usr/bin/env perl
  2. # Copyright 2004-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  9. push(@INC, "${dir}perlasm", "perlasm");
  10. require "x86asm.pl";
  11. $output = pop and open STDOUT,">$output";
  12. &asm_init($ARGV[0]);
  13. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  14. &function_begin("OPENSSL_ia32_cpuid");
  15. &xor ("edx","edx");
  16. &pushf ();
  17. &pop ("eax");
  18. &mov ("ecx","eax");
  19. &xor ("eax",1<<21);
  20. &push ("eax");
  21. &popf ();
  22. &pushf ();
  23. &pop ("eax");
  24. &xor ("ecx","eax");
  25. &xor ("eax","eax");
  26. &mov ("esi",&wparam(0));
  27. &mov (&DWP(8,"esi"),"eax"); # clear extended feature flags
  28. &bt ("ecx",21);
  29. &jnc (&label("nocpuid"));
  30. &cpuid ();
  31. &mov ("edi","eax"); # max value for standard query level
  32. &xor ("eax","eax");
  33. &cmp ("ebx",0x756e6547); # "Genu"
  34. &setne (&LB("eax"));
  35. &mov ("ebp","eax");
  36. &cmp ("edx",0x49656e69); # "ineI"
  37. &setne (&LB("eax"));
  38. &or ("ebp","eax");
  39. &cmp ("ecx",0x6c65746e); # "ntel"
  40. &setne (&LB("eax"));
  41. &or ("ebp","eax"); # 0 indicates Intel CPU
  42. &jz (&label("intel"));
  43. &cmp ("ebx",0x68747541); # "Auth"
  44. &setne (&LB("eax"));
  45. &mov ("esi","eax");
  46. &cmp ("edx",0x69746E65); # "enti"
  47. &setne (&LB("eax"));
  48. &or ("esi","eax");
  49. &cmp ("ecx",0x444D4163); # "cAMD"
  50. &setne (&LB("eax"));
  51. &or ("esi","eax"); # 0 indicates AMD CPU
  52. &jnz (&label("intel"));
  53. # AMD specific
  54. &mov ("eax",0x80000000);
  55. &cpuid ();
  56. &cmp ("eax",0x80000001);
  57. &jb (&label("intel"));
  58. &mov ("esi","eax");
  59. &mov ("eax",0x80000001);
  60. &cpuid ();
  61. &or ("ebp","ecx");
  62. &and ("ebp",1<<11|1); # isolate XOP bit
  63. &cmp ("esi",0x80000008);
  64. &jb (&label("intel"));
  65. &mov ("eax",0x80000008);
  66. &cpuid ();
  67. &movz ("esi",&LB("ecx")); # number of cores - 1
  68. &inc ("esi"); # number of cores
  69. &mov ("eax",1);
  70. &xor ("ecx","ecx");
  71. &cpuid ();
  72. &bt ("edx",28);
  73. &jnc (&label("generic"));
  74. &shr ("ebx",16);
  75. &and ("ebx",0xff);
  76. &cmp ("ebx","esi");
  77. &ja (&label("generic"));
  78. &and ("edx",0xefffffff); # clear hyper-threading bit
  79. &jmp (&label("generic"));
  80. &set_label("intel");
  81. &cmp ("edi",4);
  82. &mov ("esi",-1);
  83. &jb (&label("nocacheinfo"));
  84. &mov ("eax",4);
  85. &mov ("ecx",0); # query L1D
  86. &cpuid ();
  87. &mov ("esi","eax");
  88. &shr ("esi",14);
  89. &and ("esi",0xfff); # number of cores -1 per L1D
  90. &set_label("nocacheinfo");
  91. &mov ("eax",1);
  92. &xor ("ecx","ecx");
  93. &cpuid ();
  94. &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
  95. &cmp ("ebp",0);
  96. &jne (&label("notintel"));
  97. &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
  98. &and (&HB("eax"),15); # family ID
  99. &cmp (&HB("eax"),15); # P4?
  100. &jne (&label("notintel"));
  101. &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
  102. &set_label("notintel");
  103. &bt ("edx",28); # test hyper-threading bit
  104. &jnc (&label("generic"));
  105. &and ("edx",0xefffffff);
  106. &cmp ("esi",0);
  107. &je (&label("generic"));
  108. &or ("edx",0x10000000);
  109. &shr ("ebx",16);
  110. &cmp (&LB("ebx"),1);
  111. &ja (&label("generic"));
  112. &and ("edx",0xefffffff); # clear hyper-threading bit if not
  113. &set_label("generic");
  114. &and ("ebp",1<<11); # isolate AMD XOP flag
  115. &and ("ecx",0xfffff7ff); # force 11th bit to 0
  116. &mov ("esi","edx"); # %ebp:%esi is copy of %ecx:%edx
  117. &or ("ebp","ecx"); # merge AMD XOP flag
  118. &cmp ("edi",7);
  119. &mov ("edi",&wparam(0));
  120. &jb (&label("no_extended_info"));
  121. &mov ("eax",7);
  122. &xor ("ecx","ecx");
  123. &cpuid ();
  124. &mov (&DWP(8,"edi"),"ebx"); # save extended feature flag
  125. &set_label("no_extended_info");
  126. &bt ("ebp",27); # check OSXSAVE bit
  127. &jnc (&label("clear_avx"));
  128. &xor ("ecx","ecx");
  129. &data_byte(0x0f,0x01,0xd0); # xgetbv
  130. &and ("eax",6);
  131. &cmp ("eax",6);
  132. &je (&label("done"));
  133. &cmp ("eax",2);
  134. &je (&label("clear_avx"));
  135. &set_label("clear_xmm");
  136. &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
  137. &and ("esi",0xfeffffff); # clear FXSR
  138. &set_label("clear_avx");
  139. &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
  140. &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2
  141. &set_label("done");
  142. &mov ("eax","esi");
  143. &mov ("edx","ebp");
  144. &set_label("nocpuid");
  145. &function_end("OPENSSL_ia32_cpuid");
  146. &external_label("OPENSSL_ia32cap_P");
  147. &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  148. &xor ("eax","eax");
  149. &xor ("edx","edx");
  150. &picmeup("ecx","OPENSSL_ia32cap_P");
  151. &bt (&DWP(0,"ecx"),4);
  152. &jnc (&label("notsc"));
  153. &rdtsc ();
  154. &set_label("notsc");
  155. &ret ();
  156. &function_end_B("OPENSSL_rdtsc");
  157. # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
  158. # but it's safe to call it on any [supported] 32-bit platform...
  159. # Just check for [non-]zero return value...
  160. &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  161. &picmeup("ecx","OPENSSL_ia32cap_P");
  162. &bt (&DWP(0,"ecx"),4);
  163. &jnc (&label("nohalt")); # no TSC
  164. &data_word(0x9058900e); # push %cs; pop %eax
  165. &and ("eax",3);
  166. &jnz (&label("nohalt")); # not enough privileges
  167. &pushf ();
  168. &pop ("eax");
  169. &bt ("eax",9);
  170. &jnc (&label("nohalt")); # interrupts are disabled
  171. &rdtsc ();
  172. &push ("edx");
  173. &push ("eax");
  174. &halt ();
  175. &rdtsc ();
  176. &sub ("eax",&DWP(0,"esp"));
  177. &sbb ("edx",&DWP(4,"esp"));
  178. &add ("esp",8);
  179. &ret ();
  180. &set_label("nohalt");
  181. &xor ("eax","eax");
  182. &xor ("edx","edx");
  183. &ret ();
  184. &function_end_B("OPENSSL_instrument_halt");
  185. # Essentially there is only one use for this function. Under DJGPP:
  186. #
  187. # #include <go32.h>
  188. # ...
  189. # i=OPENSSL_far_spin(_dos_ds,0x46c);
  190. # ...
  191. # to obtain the number of spins till closest timer interrupt.
  192. &function_begin_B("OPENSSL_far_spin");
  193. &pushf ();
  194. &pop ("eax");
  195. &bt ("eax",9);
  196. &jnc (&label("nospin")); # interrupts are disabled
  197. &mov ("eax",&DWP(4,"esp"));
  198. &mov ("ecx",&DWP(8,"esp"));
  199. &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
  200. &xor ("eax","eax");
  201. &mov ("edx",&DWP(0,"ecx"));
  202. &jmp (&label("spin"));
  203. &align (16);
  204. &set_label("spin");
  205. &inc ("eax");
  206. &cmp ("edx",&DWP(0,"ecx"));
  207. &je (&label("spin"));
  208. &data_word (0x1f909090); # pop %ds
  209. &ret ();
  210. &set_label("nospin");
  211. &xor ("eax","eax");
  212. &xor ("edx","edx");
  213. &ret ();
  214. &function_end_B("OPENSSL_far_spin");
  215. &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  216. &xor ("eax","eax");
  217. &xor ("edx","edx");
  218. &picmeup("ecx","OPENSSL_ia32cap_P");
  219. &mov ("ecx",&DWP(0,"ecx"));
  220. &bt (&DWP(0,"ecx"),1);
  221. &jnc (&label("no_x87"));
  222. if ($sse2) {
  223. &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
  224. &cmp ("ecx",1<<26|1<<24);
  225. &jne (&label("no_sse2"));
  226. &pxor ("xmm0","xmm0");
  227. &pxor ("xmm1","xmm1");
  228. &pxor ("xmm2","xmm2");
  229. &pxor ("xmm3","xmm3");
  230. &pxor ("xmm4","xmm4");
  231. &pxor ("xmm5","xmm5");
  232. &pxor ("xmm6","xmm6");
  233. &pxor ("xmm7","xmm7");
  234. &set_label("no_sse2");
  235. }
  236. # just a bunch of fldz to zap the fp/mm bank followed by finit...
  237. &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
  238. &set_label("no_x87");
  239. &lea ("eax",&DWP(4,"esp"));
  240. &ret ();
  241. &function_end_B("OPENSSL_wipe_cpu");
  242. &function_begin_B("OPENSSL_atomic_add");
  243. &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
  244. &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
  245. &push ("ebx");
  246. &nop ();
  247. &mov ("eax",&DWP(0,"edx"));
  248. &set_label("spin");
  249. &lea ("ebx",&DWP(0,"eax","ecx"));
  250. &nop ();
  251. &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is involved and is always reloaded
  252. &jne (&label("spin"));
  253. &mov ("eax","ebx"); # OpenSSL expects the new value
  254. &pop ("ebx");
  255. &ret ();
  256. &function_end_B("OPENSSL_atomic_add");
  257. &function_begin_B("OPENSSL_cleanse");
  258. &mov ("edx",&wparam(0));
  259. &mov ("ecx",&wparam(1));
  260. &xor ("eax","eax");
  261. &cmp ("ecx",7);
  262. &jae (&label("lot"));
  263. &cmp ("ecx",0);
  264. &je (&label("ret"));
  265. &set_label("little");
  266. &mov (&BP(0,"edx"),"al");
  267. &sub ("ecx",1);
  268. &lea ("edx",&DWP(1,"edx"));
  269. &jnz (&label("little"));
  270. &set_label("ret");
  271. &ret ();
  272. &set_label("lot",16);
  273. &test ("edx",3);
  274. &jz (&label("aligned"));
  275. &mov (&BP(0,"edx"),"al");
  276. &lea ("ecx",&DWP(-1,"ecx"));
  277. &lea ("edx",&DWP(1,"edx"));
  278. &jmp (&label("lot"));
  279. &set_label("aligned");
  280. &mov (&DWP(0,"edx"),"eax");
  281. &lea ("ecx",&DWP(-4,"ecx"));
  282. &test ("ecx",-4);
  283. &lea ("edx",&DWP(4,"edx"));
  284. &jnz (&label("aligned"));
  285. &cmp ("ecx",0);
  286. &jne (&label("little"));
  287. &ret ();
  288. &function_end_B("OPENSSL_cleanse");
  289. &function_begin_B("CRYPTO_memcmp");
  290. &push ("esi");
  291. &push ("edi");
  292. &mov ("esi",&wparam(0));
  293. &mov ("edi",&wparam(1));
  294. &mov ("ecx",&wparam(2));
  295. &xor ("eax","eax");
  296. &xor ("edx","edx");
  297. &cmp ("ecx",0);
  298. &je (&label("no_data"));
  299. &set_label("loop");
  300. &mov ("dl",&BP(0,"esi"));
  301. &lea ("esi",&DWP(1,"esi"));
  302. &xor ("dl",&BP(0,"edi"));
  303. &lea ("edi",&DWP(1,"edi"));
  304. &or ("al","dl");
  305. &dec ("ecx");
  306. &jnz (&label("loop"));
  307. &neg ("eax");
  308. &shr ("eax",31);
  309. &set_label("no_data");
  310. &pop ("edi");
  311. &pop ("esi");
  312. &ret ();
  313. &function_end_B("CRYPTO_memcmp");
  314. {
  315. my $lasttick = "esi";
  316. my $lastdiff = "ebx";
  317. my $out = "edi";
  318. my $cnt = "ecx";
  319. my $max = "ebp";
  320. &function_begin("OPENSSL_instrument_bus");
  321. &mov ("eax",0);
  322. if ($sse2) {
  323. &picmeup("edx","OPENSSL_ia32cap_P");
  324. &bt (&DWP(0,"edx"),4);
  325. &jnc (&label("nogo")); # no TSC
  326. &bt (&DWP(0,"edx"),19);
  327. &jnc (&label("nogo")); # no CLFLUSH
  328. &mov ($out,&wparam(0)); # load arguments
  329. &mov ($cnt,&wparam(1));
  330. # collect 1st tick
  331. &rdtsc ();
  332. &mov ($lasttick,"eax"); # lasttick = tick
  333. &mov ($lastdiff,0); # lastdiff = 0
  334. &clflush(&DWP(0,$out));
  335. &data_byte(0xf0); # lock
  336. &add (&DWP(0,$out),$lastdiff);
  337. &jmp (&label("loop"));
  338. &set_label("loop",16);
  339. &rdtsc ();
  340. &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
  341. &sub ("eax",$lasttick); # diff
  342. &mov ($lasttick,"edx"); # lasttick = tick
  343. &mov ($lastdiff,"eax"); # lastdiff = diff
  344. &clflush(&DWP(0,$out));
  345. &data_byte(0xf0); # lock
  346. &add (&DWP(0,$out),"eax"); # accumulate diff
  347. &lea ($out,&DWP(4,$out)); # ++$out
  348. &sub ($cnt,1); # --$cnt
  349. &jnz (&label("loop"));
  350. &mov ("eax",&wparam(1));
  351. &set_label("nogo");
  352. }
  353. &function_end("OPENSSL_instrument_bus");
  354. &function_begin("OPENSSL_instrument_bus2");
  355. &mov ("eax",0);
  356. if ($sse2) {
  357. &picmeup("edx","OPENSSL_ia32cap_P");
  358. &bt (&DWP(0,"edx"),4);
  359. &jnc (&label("nogo")); # no TSC
  360. &bt (&DWP(0,"edx"),19);
  361. &jnc (&label("nogo")); # no CLFLUSH
  362. &mov ($out,&wparam(0)); # load arguments
  363. &mov ($cnt,&wparam(1));
  364. &mov ($max,&wparam(2));
  365. &rdtsc (); # collect 1st tick
  366. &mov ($lasttick,"eax"); # lasttick = tick
  367. &mov ($lastdiff,0); # lastdiff = 0
  368. &clflush(&DWP(0,$out));
  369. &data_byte(0xf0); # lock
  370. &add (&DWP(0,$out),$lastdiff);
  371. &rdtsc (); # collect 1st diff
  372. &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
  373. &sub ("eax",$lasttick); # diff
  374. &mov ($lasttick,"edx"); # lasttick = tick
  375. &mov ($lastdiff,"eax"); # lastdiff = diff
  376. &jmp (&label("loop2"));
  377. &set_label("loop2",16);
  378. &clflush(&DWP(0,$out));
  379. &data_byte(0xf0); # lock
  380. &add (&DWP(0,$out),"eax"); # accumulate diff
  381. &sub ($max,1);
  382. &jz (&label("done2"));
  383. &rdtsc ();
  384. &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
  385. &sub ("eax",$lasttick); # diff
  386. &mov ($lasttick,"edx"); # lasttick = tick
  387. &cmp ("eax",$lastdiff);
  388. &mov ($lastdiff,"eax"); # lastdiff = diff
  389. &mov ("edx",0);
  390. &setne ("dl");
  391. &sub ($cnt,"edx"); # conditional --$cnt
  392. &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
  393. &jnz (&label("loop2"));
  394. &set_label("done2");
  395. &mov ("eax",&wparam(1));
  396. &sub ("eax",$cnt);
  397. &set_label("nogo");
  398. }
  399. &function_end("OPENSSL_instrument_bus2");
  400. }
  401. sub gen_random {
  402. my $rdop = shift;
  403. &function_begin_B("OPENSSL_ia32_${rdop}_bytes");
  404. &push ("edi");
  405. &push ("ebx");
  406. &xor ("eax","eax"); # return value
  407. &mov ("edi",&wparam(0));
  408. &mov ("ebx",&wparam(1));
  409. &cmp ("ebx",0);
  410. &je (&label("done"));
  411. &mov ("ecx",8);
  412. &set_label("loop");
  413. &${rdop}("edx");
  414. &jc (&label("break"));
  415. &loop (&label("loop"));
  416. &jmp (&label("done"));
  417. &set_label("break",16);
  418. &cmp ("ebx",4);
  419. &jb (&label("tail"));
  420. &mov (&DWP(0,"edi"),"edx");
  421. &lea ("edi",&DWP(4,"edi"));
  422. &add ("eax",4);
  423. &sub ("ebx",4);
  424. &jz (&label("done"));
  425. &mov ("ecx",8);
  426. &jmp (&label("loop"));
  427. &set_label("tail",16);
  428. &mov (&BP(0,"edi"),"dl");
  429. &lea ("edi",&DWP(1,"edi"));
  430. &inc ("eax");
  431. &shr ("edx",8);
  432. &dec ("ebx");
  433. &jnz (&label("tail"));
  434. &set_label("done");
  435. &xor ("edx","edx"); # Clear random value from registers
  436. &pop ("ebx");
  437. &pop ("edi");
  438. &ret ();
  439. &function_end_B("OPENSSL_ia32_${rdop}_bytes");
  440. }
  441. &gen_random("rdrand");
  442. &gen_random("rdseed");
  443. &initseg("OPENSSL_cpuid_setup");
  444. &hidden("OPENSSL_cpuid_setup");
  445. &hidden("OPENSSL_ia32cap_P");
  446. &asm_finish();
  447. close STDOUT or die "error closing STDOUT: $!";