GoogleGit

blob: 151493ab9aa875f164f1ce23c30ca7baa7cf3976 [file] [log] [blame]
  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],"crypto/cpu-x86-asm");
  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. &function_begin_B("OPENSSL_ia32_rdrand");
  322. &mov ("ecx",8);
  323. &set_label("loop");
  324. &rdrand ("eax");
  325. &jc (&label("break"));
  326. &loop (&label("loop"));
  327. &set_label("break");
  328. &cmp ("eax",0);
  329. &cmove ("eax","ecx");
  330. &ret ();
  331. &function_end_B("OPENSSL_ia32_rdrand");
  332. &hidden("OPENSSL_ia32cap_P");
  333. &asm_finish();