x86cpuid.pl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. &cpuid ();
  22. &mov ("edi","eax"); # max value for standard query level
  23. &xor ("eax","eax");
  24. &cmp ("ebx",0x756e6547); # "Genu"
  25. &setne (&LB("eax"));
  26. &mov ("ebp","eax");
  27. &cmp ("edx",0x49656e69); # "ineI"
  28. &setne (&LB("eax"));
  29. &or ("ebp","eax");
  30. &cmp ("ecx",0x6c65746e); # "ntel"
  31. &setne (&LB("eax"));
  32. &or ("ebp","eax"); # 0 indicates Intel CPU
  33. &jz (&label("intel"));
  34. &cmp ("ebx",0x68747541); # "Auth"
  35. &setne (&LB("eax"));
  36. &mov ("esi","eax");
  37. &cmp ("edx",0x69746E65); # "enti"
  38. &setne (&LB("eax"));
  39. &or ("esi","eax");
  40. &cmp ("ecx",0x444D4163); # "cAMD"
  41. &setne (&LB("eax"));
  42. &or ("esi","eax"); # 0 indicates AMD CPU
  43. &jnz (&label("intel"));
  44. # AMD specific
  45. &mov ("eax",0x80000000);
  46. &cpuid ();
  47. &cmp ("eax",0x80000001);
  48. &jb (&label("intel"));
  49. &mov ("esi","eax");
  50. &mov ("eax",0x80000001);
  51. &cpuid ();
  52. &or ("ebp","ecx");
  53. &and ("ebp",1<<11|1); # isolate XOP bit
  54. &cmp ("esi",0x80000008);
  55. &jb (&label("intel"));
  56. &mov ("eax",0x80000008);
  57. &cpuid ();
  58. &movz ("esi",&LB("ecx")); # number of cores - 1
  59. &inc ("esi"); # number of cores
  60. &mov ("eax",1);
  61. &xor ("ecx","ecx");
  62. &cpuid ();
  63. &bt ("edx",28);
  64. &jnc (&label("generic"));
  65. &shr ("ebx",16);
  66. &and ("ebx",0xff);
  67. &cmp ("ebx","esi");
  68. &ja (&label("generic"));
  69. &and ("edx",0xefffffff); # clear hyper-threading bit
  70. &jmp (&label("generic"));
  71. &set_label("intel");
  72. &cmp ("edi",4);
  73. &mov ("edi",-1);
  74. &jb (&label("nocacheinfo"));
  75. &mov ("eax",4);
  76. &mov ("ecx",0); # query L1D
  77. &cpuid ();
  78. &mov ("edi","eax");
  79. &shr ("edi",14);
  80. &and ("edi",0xfff); # number of cores -1 per L1D
  81. &set_label("nocacheinfo");
  82. &mov ("eax",1);
  83. &xor ("ecx","ecx");
  84. &cpuid ();
  85. &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
  86. &cmp ("ebp",0);
  87. &jne (&label("notintel"));
  88. &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
  89. &and (&HB("eax"),15); # familiy ID
  90. &cmp (&HB("eax"),15); # P4?
  91. &jne (&label("notintel"));
  92. &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
  93. &set_label("notintel");
  94. &bt ("edx",28); # test hyper-threading bit
  95. &jnc (&label("generic"));
  96. &and ("edx",0xefffffff);
  97. &cmp ("edi",0);
  98. &je (&label("generic"));
  99. &or ("edx",0x10000000);
  100. &shr ("ebx",16);
  101. &cmp (&LB("ebx"),1);
  102. &ja (&label("generic"));
  103. &and ("edx",0xefffffff); # clear hyper-threading bit if not
  104. &set_label("generic");
  105. &and ("ebp",1<<11); # isolate AMD XOP flag
  106. &and ("ecx",0xfffff7ff); # force 11th bit to 0
  107. &mov ("esi","edx");
  108. &or ("ebp","ecx"); # merge AMD XOP flag
  109. &bt ("ecx",27); # check OSXSAVE bit
  110. &jnc (&label("clear_avx"));
  111. &xor ("ecx","ecx");
  112. &data_byte(0x0f,0x01,0xd0); # xgetbv
  113. &and ("eax",6);
  114. &cmp ("eax",6);
  115. &je (&label("done"));
  116. &cmp ("eax",2);
  117. &je (&label("clear_avx"));
  118. &set_label("clear_xmm");
  119. &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
  120. &and ("esi",0xfeffffff); # clear FXSR
  121. &set_label("clear_avx");
  122. &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
  123. &set_label("done");
  124. &mov ("eax","esi");
  125. &mov ("edx","ebp");
  126. &set_label("nocpuid");
  127. &function_end("OPENSSL_ia32_cpuid");
  128. &external_label("OPENSSL_ia32cap_P");
  129. &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  130. &xor ("eax","eax");
  131. &xor ("edx","edx");
  132. &picmeup("ecx","OPENSSL_ia32cap_P");
  133. &bt (&DWP(0,"ecx"),4);
  134. &jnc (&label("notsc"));
  135. &rdtsc ();
  136. &set_label("notsc");
  137. &ret ();
  138. &function_end_B("OPENSSL_rdtsc");
  139. # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
  140. # but it's safe to call it on any [supported] 32-bit platform...
  141. # Just check for [non-]zero return value...
  142. &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  143. &picmeup("ecx","OPENSSL_ia32cap_P");
  144. &bt (&DWP(0,"ecx"),4);
  145. &jnc (&label("nohalt")); # no TSC
  146. &data_word(0x9058900e); # push %cs; pop %eax
  147. &and ("eax",3);
  148. &jnz (&label("nohalt")); # not enough privileges
  149. &pushf ();
  150. &pop ("eax");
  151. &bt ("eax",9);
  152. &jnc (&label("nohalt")); # interrupts are disabled
  153. &rdtsc ();
  154. &push ("edx");
  155. &push ("eax");
  156. &halt ();
  157. &rdtsc ();
  158. &sub ("eax",&DWP(0,"esp"));
  159. &sbb ("edx",&DWP(4,"esp"));
  160. &add ("esp",8);
  161. &ret ();
  162. &set_label("nohalt");
  163. &xor ("eax","eax");
  164. &xor ("edx","edx");
  165. &ret ();
  166. &function_end_B("OPENSSL_instrument_halt");
  167. # Essentially there is only one use for this function. Under DJGPP:
  168. #
  169. # #include <go32.h>
  170. # ...
  171. # i=OPENSSL_far_spin(_dos_ds,0x46c);
  172. # ...
  173. # to obtain the number of spins till closest timer interrupt.
  174. &function_begin_B("OPENSSL_far_spin");
  175. &pushf ();
  176. &pop ("eax")
  177. &bt ("eax",9);
  178. &jnc (&label("nospin")); # interrupts are disabled
  179. &mov ("eax",&DWP(4,"esp"));
  180. &mov ("ecx",&DWP(8,"esp"));
  181. &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
  182. &xor ("eax","eax");
  183. &mov ("edx",&DWP(0,"ecx"));
  184. &jmp (&label("spin"));
  185. &align (16);
  186. &set_label("spin");
  187. &inc ("eax");
  188. &cmp ("edx",&DWP(0,"ecx"));
  189. &je (&label("spin"));
  190. &data_word (0x1f909090); # pop %ds
  191. &ret ();
  192. &set_label("nospin");
  193. &xor ("eax","eax");
  194. &xor ("edx","edx");
  195. &ret ();
  196. &function_end_B("OPENSSL_far_spin");
  197. &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
  198. &xor ("eax","eax");
  199. &xor ("edx","edx");
  200. &picmeup("ecx","OPENSSL_ia32cap_P");
  201. &mov ("ecx",&DWP(0,"ecx"));
  202. &bt (&DWP(0,"ecx"),1);
  203. &jnc (&label("no_x87"));
  204. if ($sse2) {
  205. &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
  206. &cmp ("ecx",1<<26|1<<24);
  207. &jne (&label("no_sse2"));
  208. &pxor ("xmm0","xmm0");
  209. &pxor ("xmm1","xmm1");
  210. &pxor ("xmm2","xmm2");
  211. &pxor ("xmm3","xmm3");
  212. &pxor ("xmm4","xmm4");
  213. &pxor ("xmm5","xmm5");
  214. &pxor ("xmm6","xmm6");
  215. &pxor ("xmm7","xmm7");
  216. &set_label("no_sse2");
  217. }
  218. # just a bunch of fldz to zap the fp/mm bank followed by finit...
  219. &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
  220. &set_label("no_x87");
  221. &lea ("eax",&DWP(4,"esp"));
  222. &ret ();
  223. &function_end_B("OPENSSL_wipe_cpu");
  224. &function_begin_B("OPENSSL_atomic_add");
  225. &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
  226. &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
  227. &push ("ebx");
  228. &nop ();
  229. &mov ("eax",&DWP(0,"edx"));
  230. &set_label("spin");
  231. &lea ("ebx",&DWP(0,"eax","ecx"));
  232. &nop ();
  233. &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
  234. &jne (&label("spin"));
  235. &mov ("eax","ebx"); # OpenSSL expects the new value
  236. &pop ("ebx");
  237. &ret ();
  238. &function_end_B("OPENSSL_atomic_add");
  239. # This function can become handy under Win32 in situations when
  240. # we don't know which calling convention, __stdcall or __cdecl(*),
  241. # indirect callee is using. In C it can be deployed as
  242. #
  243. #ifdef OPENSSL_CPUID_OBJ
  244. # type OPENSSL_indirect_call(void *f,...);
  245. # ...
  246. # OPENSSL_indirect_call(func,[up to $max arguments]);
  247. #endif
  248. #
  249. # (*) it's designed to work even for __fastcall if number of
  250. # arguments is 1 or 2!
  251. &function_begin_B("OPENSSL_indirect_call");
  252. {
  253. my ($max,$i)=(7,); # $max has to be chosen as 4*n-1
  254. # in order to preserve eventual
  255. # stack alignment
  256. &push ("ebp");
  257. &mov ("ebp","esp");
  258. &sub ("esp",$max*4);
  259. &mov ("ecx",&DWP(12,"ebp"));
  260. &mov (&DWP(0,"esp"),"ecx");
  261. &mov ("edx",&DWP(16,"ebp"));
  262. &mov (&DWP(4,"esp"),"edx");
  263. for($i=2;$i<$max;$i++)
  264. {
  265. # Some copies will be redundant/bogus...
  266. &mov ("eax",&DWP(12+$i*4,"ebp"));
  267. &mov (&DWP(0+$i*4,"esp"),"eax");
  268. }
  269. &call_ptr (&DWP(8,"ebp"));# make the call...
  270. &mov ("esp","ebp"); # ... and just restore the stack pointer
  271. # without paying attention to what we called,
  272. # (__cdecl *func) or (__stdcall *one).
  273. &pop ("ebp");
  274. &ret ();
  275. }
  276. &function_end_B("OPENSSL_indirect_call");
  277. &function_begin_B("OPENSSL_cleanse");
  278. &mov ("edx",&wparam(0));
  279. &mov ("ecx",&wparam(1));
  280. &xor ("eax","eax");
  281. &cmp ("ecx",7);
  282. &jae (&label("lot"));
  283. &cmp ("ecx",0);
  284. &je (&label("ret"));
  285. &set_label("little");
  286. &mov (&BP(0,"edx"),"al");
  287. &sub ("ecx",1);
  288. &lea ("edx",&DWP(1,"edx"));
  289. &jnz (&label("little"));
  290. &set_label("ret");
  291. &ret ();
  292. &set_label("lot",16);
  293. &test ("edx",3);
  294. &jz (&label("aligned"));
  295. &mov (&BP(0,"edx"),"al");
  296. &lea ("ecx",&DWP(-1,"ecx"));
  297. &lea ("edx",&DWP(1,"edx"));
  298. &jmp (&label("lot"));
  299. &set_label("aligned");
  300. &mov (&DWP(0,"edx"),"eax");
  301. &lea ("ecx",&DWP(-4,"ecx"));
  302. &test ("ecx",-4);
  303. &lea ("edx",&DWP(4,"edx"));
  304. &jnz (&label("aligned"));
  305. &cmp ("ecx",0);
  306. &jne (&label("little"));
  307. &ret ();
  308. &function_end_B("OPENSSL_cleanse");
  309. &function_begin_B("OPENSSL_ia32_rdrand");
  310. &mov ("ecx",8);
  311. &set_label("loop");
  312. &rdrand ("eax");
  313. &jc (&label("break"));
  314. &loop (&label("loop"));
  315. &set_label("break");
  316. &cmp ("eax",0);
  317. &cmove ("eax","ecx");
  318. &ret ();
  319. &function_end_B("OPENSSL_ia32_rdrand");
  320. &initseg("OPENSSL_cpuid_setup");
  321. &asm_finish();