A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

526 lines
13 KiB

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