x86cpuid.pl 12 KB

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