You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpuid_x86.c 44 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include "cpuid.h"
  41. #if defined(_MSC_VER) && !defined(__clang__)
  42. #define C_INLINE __inline
  43. #else
  44. #define C_INLINE inline
  45. #endif
  46. /*
  47. #ifdef NO_AVX
  48. #define CPUTYPE_HASWELL CPUTYPE_NEHALEM
  49. #define CORE_HASWELL CORE_NEHALEM
  50. #define CPUTYPE_SANDYBRIDGE CPUTYPE_NEHALEM
  51. #define CORE_SANDYBRIDGE CORE_NEHALEM
  52. #define CPUTYPE_BULLDOZER CPUTYPE_BARCELONA
  53. #define CORE_BULLDOZER CORE_BARCELONA
  54. #define CPUTYPE_PILEDRIVER CPUTYPE_BARCELONA
  55. #define CORE_PILEDRIVER CORE_BARCELONA
  56. #endif
  57. */
  58. #if defined(_MSC_VER) && !defined(__clang__)
  59. void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx)
  60. {
  61. int cpuInfo[4] = {-1};
  62. __cpuid(cpuInfo, op);
  63. *eax = cpuInfo[0];
  64. *ebx = cpuInfo[1];
  65. *ecx = cpuInfo[2];
  66. *edx = cpuInfo[3];
  67. }
  68. #else
  69. #ifndef CPUIDEMU
  70. #if defined(__APPLE__) && defined(__i386__)
  71. void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx);
  72. #else
  73. static C_INLINE void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
  74. #if defined(__i386__) && defined(__PIC__)
  75. __asm__ __volatile__
  76. ("mov %%ebx, %%edi;"
  77. "cpuid;"
  78. "xchgl %%ebx, %%edi;"
  79. : "=a" (*eax), "=D" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (op) : "cc");
  80. #else
  81. __asm__ __volatile__
  82. ("cpuid": "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (op) : "cc");
  83. #endif
  84. }
  85. #endif
  86. #else
  87. typedef struct {
  88. unsigned int id, a, b, c, d;
  89. } idlist_t;
  90. typedef struct {
  91. char *vendor;
  92. char *name;
  93. int start, stop;
  94. } vendor_t;
  95. extern idlist_t idlist[];
  96. extern vendor_t vendor[];
  97. static int cv = VENDOR;
  98. void cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx){
  99. static int current = 0;
  100. int start = vendor[cv].start;
  101. int stop = vendor[cv].stop;
  102. int count = stop - start;
  103. if ((current < start) || (current > stop)) current = start;
  104. while ((count > 0) && (idlist[current].id != op)) {
  105. current ++;
  106. if (current > stop) current = start;
  107. count --;
  108. }
  109. *eax = idlist[current].a;
  110. *ebx = idlist[current].b;
  111. *ecx = idlist[current].c;
  112. *edx = idlist[current].d;
  113. }
  114. #endif
  115. #endif // _MSC_VER
  116. static C_INLINE int have_cpuid(void){
  117. int eax, ebx, ecx, edx;
  118. cpuid(0, &eax, &ebx, &ecx, &edx);
  119. return eax;
  120. }
  121. static C_INLINE int have_excpuid(void){
  122. int eax, ebx, ecx, edx;
  123. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  124. return eax & 0xffff;
  125. }
  126. #ifndef NO_AVX
  127. static C_INLINE void xgetbv(int op, int * eax, int * edx){
  128. //Use binary code for xgetbv
  129. #if defined(_MSC_VER) && !defined(__clang__)
  130. *eax = __xgetbv(op);
  131. #else
  132. __asm__ __volatile__
  133. (".byte 0x0f, 0x01, 0xd0": "=a" (*eax), "=d" (*edx) : "c" (op) : "cc");
  134. #endif
  135. }
  136. #endif
  137. int support_avx(){
  138. #ifndef NO_AVX
  139. int eax, ebx, ecx, edx;
  140. int ret=0;
  141. cpuid(1, &eax, &ebx, &ecx, &edx);
  142. if ((ecx & (1 << 28)) != 0 && (ecx & (1 << 27)) != 0 && (ecx & (1 << 26)) != 0){
  143. xgetbv(0, &eax, &edx);
  144. if((eax & 6) == 6){
  145. ret=1; //OS support AVX
  146. }
  147. }
  148. return ret;
  149. #else
  150. return 0;
  151. #endif
  152. }
  153. int get_vendor(void){
  154. int eax, ebx, ecx, edx;
  155. char vendor[13];
  156. cpuid(0, &eax, &ebx, &ecx, &edx);
  157. *(int *)(&vendor[0]) = ebx;
  158. *(int *)(&vendor[4]) = edx;
  159. *(int *)(&vendor[8]) = ecx;
  160. vendor[12] = (char)0;
  161. if (!strcmp(vendor, "GenuineIntel")) return VENDOR_INTEL;
  162. if (!strcmp(vendor, " UMC UMC UMC")) return VENDOR_UMC;
  163. if (!strcmp(vendor, "AuthenticAMD")) return VENDOR_AMD;
  164. if (!strcmp(vendor, "CyrixInstead")) return VENDOR_CYRIX;
  165. if (!strcmp(vendor, "NexGenDriven")) return VENDOR_NEXGEN;
  166. if (!strcmp(vendor, "CentaurHauls")) return VENDOR_CENTAUR;
  167. if (!strcmp(vendor, "RiseRiseRise")) return VENDOR_RISE;
  168. if (!strcmp(vendor, " SiS SiS SiS")) return VENDOR_SIS;
  169. if (!strcmp(vendor, "GenuineTMx86")) return VENDOR_TRANSMETA;
  170. if (!strcmp(vendor, "Geode by NSC")) return VENDOR_NSC;
  171. if ((eax == 0) || ((eax & 0x500) != 0)) return VENDOR_INTEL;
  172. return VENDOR_UNKNOWN;
  173. }
  174. int get_cputype(int gettype){
  175. int eax, ebx, ecx, edx;
  176. int extend_family, family;
  177. int extend_model, model;
  178. int type, stepping;
  179. int feature = 0;
  180. cpuid(1, &eax, &ebx, &ecx, &edx);
  181. switch (gettype) {
  182. case GET_EXFAMILY :
  183. return BITMASK(eax, 20, 0xff);
  184. case GET_EXMODEL :
  185. return BITMASK(eax, 16, 0x0f);
  186. case GET_TYPE :
  187. return BITMASK(eax, 12, 0x03);
  188. case GET_FAMILY :
  189. return BITMASK(eax, 8, 0x0f);
  190. case GET_MODEL :
  191. return BITMASK(eax, 4, 0x0f);
  192. case GET_APICID :
  193. return BITMASK(ebx, 24, 0x0f);
  194. case GET_LCOUNT :
  195. return BITMASK(ebx, 16, 0x0f);
  196. case GET_CHUNKS :
  197. return BITMASK(ebx, 8, 0x0f);
  198. case GET_STEPPING :
  199. return BITMASK(eax, 0, 0x0f);
  200. case GET_BLANDID :
  201. return BITMASK(ebx, 0, 0xff);
  202. case GET_NUMSHARE :
  203. if (have_cpuid() < 4) return 0;
  204. cpuid(4, &eax, &ebx, &ecx, &edx);
  205. return BITMASK(eax, 14, 0xfff);
  206. case GET_NUMCORES :
  207. if (have_cpuid() < 4) return 0;
  208. cpuid(4, &eax, &ebx, &ecx, &edx);
  209. return BITMASK(eax, 26, 0x3f);
  210. case GET_FEATURE :
  211. if ((edx & (1 << 3)) != 0) feature |= HAVE_PSE;
  212. if ((edx & (1 << 15)) != 0) feature |= HAVE_CMOV;
  213. if ((edx & (1 << 19)) != 0) feature |= HAVE_CFLUSH;
  214. if ((edx & (1 << 23)) != 0) feature |= HAVE_MMX;
  215. if ((edx & (1 << 25)) != 0) feature |= HAVE_SSE;
  216. if ((edx & (1 << 26)) != 0) feature |= HAVE_SSE2;
  217. if ((edx & (1 << 27)) != 0) {
  218. if (BITMASK(ebx, 16, 0x0f) > 0) feature |= HAVE_HIT;
  219. }
  220. if ((ecx & (1 << 0)) != 0) feature |= HAVE_SSE3;
  221. if ((ecx & (1 << 9)) != 0) feature |= HAVE_SSSE3;
  222. if ((ecx & (1 << 19)) != 0) feature |= HAVE_SSE4_1;
  223. if ((ecx & (1 << 20)) != 0) feature |= HAVE_SSE4_2;
  224. #ifndef NO_AVX
  225. if (support_avx()) feature |= HAVE_AVX;
  226. if ((ecx & (1 << 12)) != 0) feature |= HAVE_FMA3;
  227. #endif
  228. if (have_excpuid() >= 0x01) {
  229. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  230. if ((ecx & (1 << 6)) != 0) feature |= HAVE_SSE4A;
  231. if ((ecx & (1 << 7)) != 0) feature |= HAVE_MISALIGNSSE;
  232. #ifndef NO_AVX
  233. if ((ecx & (1 << 16)) != 0) feature |= HAVE_FMA4;
  234. #endif
  235. if ((edx & (1 << 30)) != 0) feature |= HAVE_3DNOWEX;
  236. if ((edx & (1 << 31)) != 0) feature |= HAVE_3DNOW;
  237. }
  238. if (have_excpuid() >= 0x1a) {
  239. cpuid(0x8000001a, &eax, &ebx, &ecx, &edx);
  240. if ((eax & (1 << 0)) != 0) feature |= HAVE_128BITFPU;
  241. if ((eax & (1 << 1)) != 0) feature |= HAVE_FASTMOVU;
  242. }
  243. }
  244. return feature;
  245. }
  246. int get_cacheinfo(int type, cache_info_t *cacheinfo){
  247. int eax, ebx, ecx, edx, cpuid_level;
  248. int info[15];
  249. int i;
  250. cache_info_t LC1, LD1, L2, L3,
  251. ITB, DTB, LITB, LDTB,
  252. L2ITB, L2DTB, L2LITB, L2LDTB;
  253. LC1.size = 0; LC1.associative = 0; LC1.linesize = 0; LC1.shared = 0;
  254. LD1.size = 0; LD1.associative = 0; LD1.linesize = 0; LD1.shared = 0;
  255. L2.size = 0; L2.associative = 0; L2.linesize = 0; L2.shared = 0;
  256. L3.size = 0; L3.associative = 0; L3.linesize = 0; L3.shared = 0;
  257. ITB.size = 0; ITB.associative = 0; ITB.linesize = 0; ITB.shared = 0;
  258. DTB.size = 0; DTB.associative = 0; DTB.linesize = 0; DTB.shared = 0;
  259. LITB.size = 0; LITB.associative = 0; LITB.linesize = 0; LITB.shared = 0;
  260. LDTB.size = 0; LDTB.associative = 0; LDTB.linesize = 0; LDTB.shared = 0;
  261. L2ITB.size = 0; L2ITB.associative = 0; L2ITB.linesize = 0; L2ITB.shared = 0;
  262. L2DTB.size = 0; L2DTB.associative = 0; L2DTB.linesize = 0; L2DTB.shared = 0;
  263. L2LITB.size = 0; L2LITB.associative = 0; L2LITB.linesize = 0; L2LITB.shared = 0;
  264. L2LDTB.size = 0; L2LDTB.associative = 0; L2LDTB.linesize = 0; L2LDTB.shared = 0;
  265. cpuid(0, &cpuid_level, &ebx, &ecx, &edx);
  266. if (cpuid_level > 1) {
  267. cpuid(2, &eax, &ebx, &ecx, &edx);
  268. info[ 0] = BITMASK(eax, 8, 0xff);
  269. info[ 1] = BITMASK(eax, 16, 0xff);
  270. info[ 2] = BITMASK(eax, 24, 0xff);
  271. info[ 3] = BITMASK(ebx, 0, 0xff);
  272. info[ 4] = BITMASK(ebx, 8, 0xff);
  273. info[ 5] = BITMASK(ebx, 16, 0xff);
  274. info[ 6] = BITMASK(ebx, 24, 0xff);
  275. info[ 7] = BITMASK(ecx, 0, 0xff);
  276. info[ 8] = BITMASK(ecx, 8, 0xff);
  277. info[ 9] = BITMASK(ecx, 16, 0xff);
  278. info[10] = BITMASK(ecx, 24, 0xff);
  279. info[11] = BITMASK(edx, 0, 0xff);
  280. info[12] = BITMASK(edx, 8, 0xff);
  281. info[13] = BITMASK(edx, 16, 0xff);
  282. info[14] = BITMASK(edx, 24, 0xff);
  283. for (i = 0; i < 15; i++){
  284. switch (info[i]){
  285. /* This table is from http://www.sandpile.org/ia32/cpuid.htm */
  286. case 0x01 :
  287. ITB.size = 4;
  288. ITB.associative = 4;
  289. ITB.linesize = 32;
  290. break;
  291. case 0x02 :
  292. LITB.size = 4096;
  293. LITB.associative = 0;
  294. LITB.linesize = 2;
  295. break;
  296. case 0x03 :
  297. DTB.size = 4;
  298. DTB.associative = 4;
  299. DTB.linesize = 64;
  300. break;
  301. case 0x04 :
  302. LDTB.size = 4096;
  303. LDTB.associative = 4;
  304. LDTB.linesize = 8;
  305. break;
  306. case 0x05 :
  307. LDTB.size = 4096;
  308. LDTB.associative = 4;
  309. LDTB.linesize = 32;
  310. break;
  311. case 0x06 :
  312. LC1.size = 8;
  313. LC1.associative = 4;
  314. LC1.linesize = 32;
  315. break;
  316. case 0x08 :
  317. LC1.size = 16;
  318. LC1.associative = 4;
  319. LC1.linesize = 32;
  320. break;
  321. case 0x09 :
  322. LC1.size = 32;
  323. LC1.associative = 4;
  324. LC1.linesize = 64;
  325. break;
  326. case 0x0a :
  327. LD1.size = 8;
  328. LD1.associative = 2;
  329. LD1.linesize = 32;
  330. break;
  331. case 0x0c :
  332. LD1.size = 16;
  333. LD1.associative = 4;
  334. LD1.linesize = 32;
  335. break;
  336. case 0x0d :
  337. LD1.size = 16;
  338. LD1.associative = 4;
  339. LD1.linesize = 64;
  340. break;
  341. case 0x0e :
  342. LD1.size = 24;
  343. LD1.associative = 6;
  344. LD1.linesize = 64;
  345. break;
  346. case 0x10 :
  347. LD1.size = 16;
  348. LD1.associative = 4;
  349. LD1.linesize = 32;
  350. break;
  351. case 0x15 :
  352. LC1.size = 16;
  353. LC1.associative = 4;
  354. LC1.linesize = 32;
  355. break;
  356. case 0x1a :
  357. L2.size = 96;
  358. L2.associative = 6;
  359. L2.linesize = 64;
  360. break;
  361. case 0x21 :
  362. L2.size = 256;
  363. L2.associative = 8;
  364. L2.linesize = 64;
  365. break;
  366. case 0x22 :
  367. L3.size = 512;
  368. L3.associative = 4;
  369. L3.linesize = 64;
  370. break;
  371. case 0x23 :
  372. L3.size = 1024;
  373. L3.associative = 8;
  374. L3.linesize = 64;
  375. break;
  376. case 0x25 :
  377. L3.size = 2048;
  378. L3.associative = 8;
  379. L3.linesize = 64;
  380. break;
  381. case 0x29 :
  382. L3.size = 4096;
  383. L3.associative = 8;
  384. L3.linesize = 64;
  385. break;
  386. case 0x2c :
  387. LD1.size = 32;
  388. LD1.associative = 8;
  389. LD1.linesize = 64;
  390. break;
  391. case 0x30 :
  392. LC1.size = 32;
  393. LC1.associative = 8;
  394. LC1.linesize = 64;
  395. break;
  396. case 0x39 :
  397. L2.size = 128;
  398. L2.associative = 4;
  399. L2.linesize = 64;
  400. break;
  401. case 0x3a :
  402. L2.size = 192;
  403. L2.associative = 6;
  404. L2.linesize = 64;
  405. break;
  406. case 0x3b :
  407. L2.size = 128;
  408. L2.associative = 2;
  409. L2.linesize = 64;
  410. break;
  411. case 0x3c :
  412. L2.size = 256;
  413. L2.associative = 4;
  414. L2.linesize = 64;
  415. break;
  416. case 0x3d :
  417. L2.size = 384;
  418. L2.associative = 6;
  419. L2.linesize = 64;
  420. break;
  421. case 0x3e :
  422. L2.size = 512;
  423. L2.associative = 4;
  424. L2.linesize = 64;
  425. break;
  426. case 0x41 :
  427. L2.size = 128;
  428. L2.associative = 4;
  429. L2.linesize = 32;
  430. break;
  431. case 0x42 :
  432. L2.size = 256;
  433. L2.associative = 4;
  434. L2.linesize = 32;
  435. break;
  436. case 0x43 :
  437. L2.size = 512;
  438. L2.associative = 4;
  439. L2.linesize = 32;
  440. break;
  441. case 0x44 :
  442. L2.size = 1024;
  443. L2.associative = 4;
  444. L2.linesize = 32;
  445. break;
  446. case 0x45 :
  447. L2.size = 2048;
  448. L2.associative = 4;
  449. L2.linesize = 32;
  450. break;
  451. case 0x46 :
  452. L3.size = 4096;
  453. L3.associative = 4;
  454. L3.linesize = 64;
  455. break;
  456. case 0x47 :
  457. L3.size = 8192;
  458. L3.associative = 8;
  459. L3.linesize = 64;
  460. break;
  461. case 0x48 :
  462. L2.size = 3184;
  463. L2.associative = 12;
  464. L2.linesize = 64;
  465. break;
  466. case 0x49 :
  467. if ((get_cputype(GET_FAMILY) == 0x0f) && (get_cputype(GET_MODEL) == 0x06)) {
  468. L3.size = 4096;
  469. L3.associative = 16;
  470. L3.linesize = 64;
  471. } else {
  472. L2.size = 4096;
  473. L2.associative = 16;
  474. L2.linesize = 64;
  475. }
  476. break;
  477. case 0x4a :
  478. L3.size = 6144;
  479. L3.associative = 12;
  480. L3.linesize = 64;
  481. break;
  482. case 0x4b :
  483. L3.size = 8192;
  484. L3.associative = 16;
  485. L3.linesize = 64;
  486. break;
  487. case 0x4c :
  488. L3.size = 12280;
  489. L3.associative = 12;
  490. L3.linesize = 64;
  491. break;
  492. case 0x4d :
  493. L3.size = 16384;
  494. L3.associative = 16;
  495. L3.linesize = 64;
  496. break;
  497. case 0x4e :
  498. L2.size = 6144;
  499. L2.associative = 24;
  500. L2.linesize = 64;
  501. break;
  502. case 0x4f :
  503. ITB.size = 4;
  504. ITB.associative = 0;
  505. ITB.linesize = 32;
  506. break;
  507. case 0x50 :
  508. ITB.size = 4;
  509. ITB.associative = 0;
  510. ITB.linesize = 64;
  511. LITB.size = 4096;
  512. LITB.associative = 0;
  513. LITB.linesize = 64;
  514. LITB.shared = 1;
  515. break;
  516. case 0x51 :
  517. ITB.size = 4;
  518. ITB.associative = 0;
  519. ITB.linesize = 128;
  520. LITB.size = 4096;
  521. LITB.associative = 0;
  522. LITB.linesize = 128;
  523. LITB.shared = 1;
  524. break;
  525. case 0x52 :
  526. ITB.size = 4;
  527. ITB.associative = 0;
  528. ITB.linesize = 256;
  529. LITB.size = 4096;
  530. LITB.associative = 0;
  531. LITB.linesize = 256;
  532. LITB.shared = 1;
  533. break;
  534. case 0x55 :
  535. LITB.size = 4096;
  536. LITB.associative = 0;
  537. LITB.linesize = 7;
  538. LITB.shared = 1;
  539. break;
  540. case 0x56 :
  541. LDTB.size = 4096;
  542. LDTB.associative = 4;
  543. LDTB.linesize = 16;
  544. break;
  545. case 0x57 :
  546. LDTB.size = 4096;
  547. LDTB.associative = 4;
  548. LDTB.linesize = 16;
  549. break;
  550. case 0x5b :
  551. DTB.size = 4;
  552. DTB.associative = 0;
  553. DTB.linesize = 64;
  554. LDTB.size = 4096;
  555. LDTB.associative = 0;
  556. LDTB.linesize = 64;
  557. LDTB.shared = 1;
  558. break;
  559. case 0x5c :
  560. DTB.size = 4;
  561. DTB.associative = 0;
  562. DTB.linesize = 128;
  563. LDTB.size = 4096;
  564. LDTB.associative = 0;
  565. LDTB.linesize = 128;
  566. LDTB.shared = 1;
  567. break;
  568. case 0x5d :
  569. DTB.size = 4;
  570. DTB.associative = 0;
  571. DTB.linesize = 256;
  572. LDTB.size = 4096;
  573. LDTB.associative = 0;
  574. LDTB.linesize = 256;
  575. LDTB.shared = 1;
  576. break;
  577. case 0x60 :
  578. LD1.size = 16;
  579. LD1.associative = 8;
  580. LD1.linesize = 64;
  581. break;
  582. case 0x66 :
  583. LD1.size = 8;
  584. LD1.associative = 4;
  585. LD1.linesize = 64;
  586. break;
  587. case 0x67 :
  588. LD1.size = 16;
  589. LD1.associative = 4;
  590. LD1.linesize = 64;
  591. break;
  592. case 0x68 :
  593. LD1.size = 32;
  594. LD1.associative = 4;
  595. LD1.linesize = 64;
  596. break;
  597. case 0x70 :
  598. LC1.size = 12;
  599. LC1.associative = 8;
  600. break;
  601. case 0x71 :
  602. LC1.size = 16;
  603. LC1.associative = 8;
  604. break;
  605. case 0x72 :
  606. LC1.size = 32;
  607. LC1.associative = 8;
  608. break;
  609. case 0x73 :
  610. LC1.size = 64;
  611. LC1.associative = 8;
  612. break;
  613. case 0x77 :
  614. LC1.size = 16;
  615. LC1.associative = 4;
  616. LC1.linesize = 64;
  617. break;
  618. case 0x78 :
  619. L2.size = 1024;
  620. L2.associative = 4;
  621. L2.linesize = 64;
  622. break;
  623. case 0x79 :
  624. L2.size = 128;
  625. L2.associative = 8;
  626. L2.linesize = 64;
  627. break;
  628. case 0x7a :
  629. L2.size = 256;
  630. L2.associative = 8;
  631. L2.linesize = 64;
  632. break;
  633. case 0x7b :
  634. L2.size = 512;
  635. L2.associative = 8;
  636. L2.linesize = 64;
  637. break;
  638. case 0x7c :
  639. L2.size = 1024;
  640. L2.associative = 8;
  641. L2.linesize = 64;
  642. break;
  643. case 0x7d :
  644. L2.size = 2048;
  645. L2.associative = 8;
  646. L2.linesize = 64;
  647. break;
  648. case 0x7e :
  649. L2.size = 256;
  650. L2.associative = 8;
  651. L2.linesize = 128;
  652. break;
  653. case 0x7f :
  654. L2.size = 512;
  655. L2.associative = 2;
  656. L2.linesize = 64;
  657. break;
  658. case 0x81 :
  659. L2.size = 128;
  660. L2.associative = 8;
  661. L2.linesize = 32;
  662. break;
  663. case 0x82 :
  664. L2.size = 256;
  665. L2.associative = 8;
  666. L2.linesize = 32;
  667. break;
  668. case 0x83 :
  669. L2.size = 512;
  670. L2.associative = 8;
  671. L2.linesize = 32;
  672. break;
  673. case 0x84 :
  674. L2.size = 1024;
  675. L2.associative = 8;
  676. L2.linesize = 32;
  677. break;
  678. case 0x85 :
  679. L2.size = 2048;
  680. L2.associative = 8;
  681. L2.linesize = 32;
  682. break;
  683. case 0x86 :
  684. L2.size = 512;
  685. L2.associative = 4;
  686. L2.linesize = 64;
  687. break;
  688. case 0x87 :
  689. L2.size = 1024;
  690. L2.associative = 8;
  691. L2.linesize = 64;
  692. break;
  693. case 0x88 :
  694. L3.size = 2048;
  695. L3.associative = 4;
  696. L3.linesize = 64;
  697. break;
  698. case 0x89 :
  699. L3.size = 4096;
  700. L3.associative = 4;
  701. L3.linesize = 64;
  702. break;
  703. case 0x8a :
  704. L3.size = 8192;
  705. L3.associative = 4;
  706. L3.linesize = 64;
  707. break;
  708. case 0x8d :
  709. L3.size = 3096;
  710. L3.associative = 12;
  711. L3.linesize = 128;
  712. break;
  713. case 0x90 :
  714. ITB.size = 4;
  715. ITB.associative = 0;
  716. ITB.linesize = 64;
  717. break;
  718. case 0x96 :
  719. DTB.size = 4;
  720. DTB.associative = 0;
  721. DTB.linesize = 32;
  722. break;
  723. case 0x9b :
  724. L2DTB.size = 4;
  725. L2DTB.associative = 0;
  726. L2DTB.linesize = 96;
  727. break;
  728. case 0xb0 :
  729. ITB.size = 4;
  730. ITB.associative = 4;
  731. ITB.linesize = 128;
  732. break;
  733. case 0xb1 :
  734. LITB.size = 4096;
  735. LITB.associative = 4;
  736. LITB.linesize = 4;
  737. break;
  738. case 0xb2 :
  739. ITB.size = 4;
  740. ITB.associative = 4;
  741. ITB.linesize = 64;
  742. break;
  743. case 0xb3 :
  744. DTB.size = 4;
  745. DTB.associative = 4;
  746. DTB.linesize = 128;
  747. break;
  748. case 0xb4 :
  749. DTB.size = 4;
  750. DTB.associative = 4;
  751. DTB.linesize = 256;
  752. break;
  753. case 0xba :
  754. DTB.size = 4;
  755. DTB.associative = 4;
  756. DTB.linesize = 64;
  757. break;
  758. case 0xd0 :
  759. L3.size = 512;
  760. L3.associative = 4;
  761. L3.linesize = 64;
  762. break;
  763. case 0xd1 :
  764. L3.size = 1024;
  765. L3.associative = 4;
  766. L3.linesize = 64;
  767. break;
  768. case 0xd2 :
  769. L3.size = 2048;
  770. L3.associative = 4;
  771. L3.linesize = 64;
  772. break;
  773. case 0xd6 :
  774. L3.size = 1024;
  775. L3.associative = 8;
  776. L3.linesize = 64;
  777. break;
  778. case 0xd7 :
  779. L3.size = 2048;
  780. L3.associative = 8;
  781. L3.linesize = 64;
  782. break;
  783. case 0xd8 :
  784. L3.size = 4096;
  785. L3.associative = 8;
  786. L3.linesize = 64;
  787. break;
  788. case 0xdc :
  789. L3.size = 2048;
  790. L3.associative = 12;
  791. L3.linesize = 64;
  792. break;
  793. case 0xdd :
  794. L3.size = 4096;
  795. L3.associative = 12;
  796. L3.linesize = 64;
  797. break;
  798. case 0xde :
  799. L3.size = 8192;
  800. L3.associative = 12;
  801. L3.linesize = 64;
  802. break;
  803. case 0xe2 :
  804. L3.size = 2048;
  805. L3.associative = 16;
  806. L3.linesize = 64;
  807. break;
  808. case 0xe3 :
  809. L3.size = 4096;
  810. L3.associative = 16;
  811. L3.linesize = 64;
  812. break;
  813. case 0xe4 :
  814. L3.size = 8192;
  815. L3.associative = 16;
  816. L3.linesize = 64;
  817. break;
  818. }
  819. }
  820. }
  821. if (get_vendor() == VENDOR_INTEL) {
  822. cpuid(0x80000000, &cpuid_level, &ebx, &ecx, &edx);
  823. if (cpuid_level >= 0x80000006) {
  824. if(L2.size<=0){
  825. //If we didn't detect L2 correctly before,
  826. cpuid(0x80000006, &eax, &ebx, &ecx, &edx);
  827. L2.size = BITMASK(ecx, 16, 0xffff);
  828. L2.associative = BITMASK(ecx, 12, 0x0f);
  829. switch (L2.associative){
  830. case 0x06:
  831. L2.associative = 8;
  832. break;
  833. case 0x08:
  834. L2.associative = 16;
  835. break;
  836. }
  837. L2.linesize = BITMASK(ecx, 0, 0xff);
  838. }
  839. }
  840. }
  841. if ((get_vendor() == VENDOR_AMD) || (get_vendor() == VENDOR_CENTAUR)) {
  842. cpuid(0x80000005, &eax, &ebx, &ecx, &edx);
  843. LDTB.size = 4096;
  844. LDTB.associative = BITMASK(eax, 24, 0xff);
  845. if (LDTB.associative == 0xff) LDTB.associative = 0;
  846. LDTB.linesize = BITMASK(eax, 16, 0xff);
  847. LITB.size = 4096;
  848. LITB.associative = BITMASK(eax, 8, 0xff);
  849. if (LITB.associative == 0xff) LITB.associative = 0;
  850. LITB.linesize = BITMASK(eax, 0, 0xff);
  851. DTB.size = 4;
  852. DTB.associative = BITMASK(ebx, 24, 0xff);
  853. if (DTB.associative == 0xff) DTB.associative = 0;
  854. DTB.linesize = BITMASK(ebx, 16, 0xff);
  855. ITB.size = 4;
  856. ITB.associative = BITMASK(ebx, 8, 0xff);
  857. if (ITB.associative == 0xff) ITB.associative = 0;
  858. ITB.linesize = BITMASK(ebx, 0, 0xff);
  859. LD1.size = BITMASK(ecx, 24, 0xff);
  860. LD1.associative = BITMASK(ecx, 16, 0xff);
  861. if (LD1.associative == 0xff) LD1.associative = 0;
  862. LD1.linesize = BITMASK(ecx, 0, 0xff);
  863. LC1.size = BITMASK(ecx, 24, 0xff);
  864. LC1.associative = BITMASK(ecx, 16, 0xff);
  865. if (LC1.associative == 0xff) LC1.associative = 0;
  866. LC1.linesize = BITMASK(ecx, 0, 0xff);
  867. cpuid(0x80000006, &eax, &ebx, &ecx, &edx);
  868. L2LDTB.size = 4096;
  869. L2LDTB.associative = BITMASK(eax, 24, 0xff);
  870. if (L2LDTB.associative == 0xff) L2LDTB.associative = 0;
  871. L2LDTB.linesize = BITMASK(eax, 16, 0xff);
  872. L2LITB.size = 4096;
  873. L2LITB.associative = BITMASK(eax, 8, 0xff);
  874. if (L2LITB.associative == 0xff) L2LITB.associative = 0;
  875. L2LITB.linesize = BITMASK(eax, 0, 0xff);
  876. L2DTB.size = 4;
  877. L2DTB.associative = BITMASK(ebx, 24, 0xff);
  878. if (L2DTB.associative == 0xff) L2DTB.associative = 0;
  879. L2DTB.linesize = BITMASK(ebx, 16, 0xff);
  880. L2ITB.size = 4;
  881. L2ITB.associative = BITMASK(ebx, 8, 0xff);
  882. if (L2ITB.associative == 0xff) L2ITB.associative = 0;
  883. L2ITB.linesize = BITMASK(ebx, 0, 0xff);
  884. if(L2.size <= 0){
  885. //If we didn't detect L2 correctly before,
  886. L2.size = BITMASK(ecx, 16, 0xffff);
  887. L2.associative = BITMASK(ecx, 12, 0xf);
  888. switch (L2.associative){
  889. case 0x06:
  890. L2.associative = 8;
  891. break;
  892. case 0x08:
  893. L2.associative = 16;
  894. break;
  895. }
  896. if (L2.associative == 0xff) L2.associative = 0;
  897. L2.linesize = BITMASK(ecx, 0, 0xff);
  898. }
  899. L3.size = BITMASK(edx, 18, 0x3fff) * 512;
  900. L3.associative = BITMASK(edx, 12, 0xf);
  901. if (L3.associative == 0xff) L2.associative = 0;
  902. L3.linesize = BITMASK(edx, 0, 0xff);
  903. }
  904. switch (type) {
  905. case CACHE_INFO_L1_I :
  906. *cacheinfo = LC1;
  907. break;
  908. case CACHE_INFO_L1_D :
  909. *cacheinfo = LD1;
  910. break;
  911. case CACHE_INFO_L2 :
  912. *cacheinfo = L2;
  913. break;
  914. case CACHE_INFO_L3 :
  915. *cacheinfo = L3;
  916. break;
  917. case CACHE_INFO_L1_DTB :
  918. *cacheinfo = DTB;
  919. break;
  920. case CACHE_INFO_L1_ITB :
  921. *cacheinfo = ITB;
  922. break;
  923. case CACHE_INFO_L1_LDTB :
  924. *cacheinfo = LDTB;
  925. break;
  926. case CACHE_INFO_L1_LITB :
  927. *cacheinfo = LITB;
  928. break;
  929. case CACHE_INFO_L2_DTB :
  930. *cacheinfo = L2DTB;
  931. break;
  932. case CACHE_INFO_L2_ITB :
  933. *cacheinfo = L2ITB;
  934. break;
  935. case CACHE_INFO_L2_LDTB :
  936. *cacheinfo = L2LDTB;
  937. break;
  938. case CACHE_INFO_L2_LITB :
  939. *cacheinfo = L2LITB;
  940. break;
  941. }
  942. return 0;
  943. }
  944. int get_cpuname(void){
  945. int family, exfamily, model, vendor, exmodel;
  946. if (!have_cpuid()) return CPUTYPE_80386;
  947. family = get_cputype(GET_FAMILY);
  948. exfamily = get_cputype(GET_EXFAMILY);
  949. model = get_cputype(GET_MODEL);
  950. exmodel = get_cputype(GET_EXMODEL);
  951. vendor = get_vendor();
  952. if (vendor == VENDOR_INTEL){
  953. switch (family) {
  954. case 0x4:
  955. return CPUTYPE_80486;
  956. case 0x5:
  957. return CPUTYPE_PENTIUM;
  958. case 0x6:
  959. switch (exmodel) {
  960. case 0:
  961. switch (model) {
  962. case 1:
  963. case 3:
  964. case 5:
  965. case 6:
  966. return CPUTYPE_PENTIUM2;
  967. case 7:
  968. case 8:
  969. case 10:
  970. case 11:
  971. return CPUTYPE_PENTIUM3;
  972. case 9:
  973. case 13:
  974. case 14:
  975. return CPUTYPE_PENTIUMM;
  976. case 15:
  977. return CPUTYPE_CORE2;
  978. }
  979. break;
  980. case 1:
  981. switch (model) {
  982. case 6:
  983. return CPUTYPE_CORE2;
  984. case 7:
  985. return CPUTYPE_PENRYN;
  986. case 10:
  987. case 11:
  988. case 14:
  989. case 15:
  990. return CPUTYPE_NEHALEM;
  991. case 12:
  992. return CPUTYPE_ATOM;
  993. case 13:
  994. return CPUTYPE_DUNNINGTON;
  995. }
  996. break;
  997. case 2:
  998. switch (model) {
  999. case 5:
  1000. //Intel Core (Clarkdale) / Core (Arrandale)
  1001. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  1002. // Xeon (Clarkdale), 32nm
  1003. return CPUTYPE_NEHALEM;
  1004. case 10:
  1005. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  1006. if(support_avx())
  1007. return CPUTYPE_SANDYBRIDGE;
  1008. else
  1009. return CPUTYPE_NEHALEM; //OS doesn't support AVX
  1010. case 12:
  1011. //Xeon Processor 5600 (Westmere-EP)
  1012. return CPUTYPE_NEHALEM;
  1013. case 13:
  1014. //Intel Core i7-3000 / Xeon E5 (Sandy Bridge)
  1015. if(support_avx())
  1016. return CPUTYPE_SANDYBRIDGE;
  1017. else
  1018. return CPUTYPE_NEHALEM;
  1019. case 14:
  1020. // Xeon E7540
  1021. case 15:
  1022. //Xeon Processor E7 (Westmere-EX)
  1023. return CPUTYPE_NEHALEM;
  1024. }
  1025. break;
  1026. case 3:
  1027. switch (model) {
  1028. case 10:
  1029. case 14:
  1030. // Ivy Bridge
  1031. if(support_avx())
  1032. return CPUTYPE_SANDYBRIDGE;
  1033. else
  1034. return CPUTYPE_NEHALEM;
  1035. case 12:
  1036. case 15:
  1037. if(support_avx())
  1038. #ifndef NO_AVX2
  1039. return CPUTYPE_HASWELL;
  1040. #else
  1041. return CPUTYPE_SANDYBRIDGE;
  1042. #endif
  1043. else
  1044. return CPUTYPE_NEHALEM;
  1045. case 13:
  1046. //Broadwell
  1047. if(support_avx())
  1048. #ifndef NO_AVX2
  1049. return CPUTYPE_HASWELL;
  1050. #else
  1051. return CPUTYPE_SANDYBRIDGE;
  1052. #endif
  1053. else
  1054. return CPUTYPE_NEHALEM;
  1055. }
  1056. break;
  1057. case 4:
  1058. switch (model) {
  1059. case 5:
  1060. case 6:
  1061. if(support_avx())
  1062. #ifndef NO_AVX2
  1063. return CPUTYPE_HASWELL;
  1064. #else
  1065. return CPUTYPE_SANDYBRIDGE;
  1066. #endif
  1067. else
  1068. return CPUTYPE_NEHALEM;
  1069. case 7:
  1070. case 15:
  1071. //Broadwell
  1072. if(support_avx())
  1073. #ifndef NO_AVX2
  1074. return CPUTYPE_HASWELL;
  1075. #else
  1076. return CPUTYPE_SANDYBRIDGE;
  1077. #endif
  1078. else
  1079. return CPUTYPE_NEHALEM;
  1080. case 14:
  1081. //Skylake
  1082. if(support_avx())
  1083. #ifndef NO_AVX2
  1084. return CPUTYPE_HASWELL;
  1085. #else
  1086. return CPUTYPE_SANDYBRIDGE;
  1087. #endif
  1088. else
  1089. return CPUTYPE_NEHALEM;
  1090. }
  1091. break;
  1092. case 5:
  1093. switch (model) {
  1094. case 6:
  1095. //Broadwell
  1096. if(support_avx())
  1097. #ifndef NO_AVX2
  1098. return CPUTYPE_HASWELL;
  1099. #else
  1100. return CPUTYPE_SANDYBRIDGE;
  1101. #endif
  1102. else
  1103. return CPUTYPE_NEHALEM;
  1104. case 5:
  1105. case 14:
  1106. // Skylake
  1107. if(support_avx())
  1108. #ifndef NO_AVX2
  1109. return CPUTYPE_HASWELL;
  1110. #else
  1111. return CPUTYPE_SANDYBRIDGE;
  1112. #endif
  1113. else
  1114. return CPUTYPE_NEHALEM;
  1115. }
  1116. break;
  1117. }
  1118. break;
  1119. case 0x7:
  1120. return CPUTYPE_ITANIUM;
  1121. case 0xf:
  1122. switch (exfamily) {
  1123. case 0 :
  1124. return CPUTYPE_PENTIUM4;
  1125. case 1 :
  1126. return CPUTYPE_ITANIUM;
  1127. }
  1128. break;
  1129. }
  1130. return CPUTYPE_INTEL_UNKNOWN;
  1131. }
  1132. if (vendor == VENDOR_AMD){
  1133. switch (family) {
  1134. case 0x4:
  1135. return CPUTYPE_AMD5X86;
  1136. case 0x5:
  1137. return CPUTYPE_AMDK6;
  1138. case 0x6:
  1139. return CPUTYPE_ATHLON;
  1140. case 0xf:
  1141. switch (exfamily) {
  1142. case 0:
  1143. case 2:
  1144. return CPUTYPE_OPTERON;
  1145. case 1:
  1146. case 3:
  1147. case 10:
  1148. return CPUTYPE_BARCELONA;
  1149. case 6:
  1150. switch (model) {
  1151. case 1:
  1152. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1153. if(support_avx())
  1154. return CPUTYPE_BULLDOZER;
  1155. else
  1156. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1157. case 2: //AMD Piledriver
  1158. case 3: //AMD Richland
  1159. if(support_avx())
  1160. return CPUTYPE_PILEDRIVER;
  1161. else
  1162. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1163. case 0:
  1164. switch(exmodel){
  1165. case 1: //AMD Trinity
  1166. if(support_avx())
  1167. return CPUTYPE_PILEDRIVER;
  1168. else
  1169. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1170. case 3:
  1171. if(support_avx())
  1172. return CPUTYPE_STEAMROLLER;
  1173. else
  1174. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1175. case 6:
  1176. if(support_avx())
  1177. return CPUTYPE_EXCAVATOR;
  1178. else
  1179. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1180. }
  1181. break;
  1182. }
  1183. break;
  1184. case 5:
  1185. return CPUTYPE_BOBCAT;
  1186. }
  1187. break;
  1188. }
  1189. return CPUTYPE_AMD_UNKNOWN;
  1190. }
  1191. if (vendor == VENDOR_CYRIX){
  1192. switch (family) {
  1193. case 0x4:
  1194. return CPUTYPE_CYRIX5X86;
  1195. case 0x5:
  1196. return CPUTYPE_CYRIXM1;
  1197. case 0x6:
  1198. return CPUTYPE_CYRIXM2;
  1199. }
  1200. return CPUTYPE_CYRIX_UNKNOWN;
  1201. }
  1202. if (vendor == VENDOR_NEXGEN){
  1203. switch (family) {
  1204. case 0x5:
  1205. return CPUTYPE_NEXGENNX586;
  1206. }
  1207. return CPUTYPE_NEXGEN_UNKNOWN;
  1208. }
  1209. if (vendor == VENDOR_CENTAUR){
  1210. switch (family) {
  1211. case 0x5:
  1212. return CPUTYPE_CENTAURC6;
  1213. break;
  1214. case 0x6:
  1215. return CPUTYPE_NANO;
  1216. break;
  1217. }
  1218. return CPUTYPE_VIAC3;
  1219. }
  1220. if (vendor == VENDOR_RISE){
  1221. switch (family) {
  1222. case 0x5:
  1223. return CPUTYPE_RISEMP6;
  1224. }
  1225. return CPUTYPE_RISE_UNKNOWN;
  1226. }
  1227. if (vendor == VENDOR_SIS){
  1228. switch (family) {
  1229. case 0x5:
  1230. return CPUTYPE_SYS55X;
  1231. }
  1232. return CPUTYPE_SIS_UNKNOWN;
  1233. }
  1234. if (vendor == VENDOR_TRANSMETA){
  1235. switch (family) {
  1236. case 0x5:
  1237. return CPUTYPE_CRUSOETM3X;
  1238. }
  1239. return CPUTYPE_TRANSMETA_UNKNOWN;
  1240. }
  1241. if (vendor == VENDOR_NSC){
  1242. switch (family) {
  1243. case 0x5:
  1244. return CPUTYPE_NSGEODE;
  1245. }
  1246. return CPUTYPE_NSC_UNKNOWN;
  1247. }
  1248. return CPUTYPE_UNKNOWN;
  1249. }
  1250. static char *cpuname[] = {
  1251. "UNKNOWN",
  1252. "INTEL_UNKNOWN",
  1253. "UMC_UNKNOWN",
  1254. "AMD_UNKNOWN",
  1255. "CYRIX_UNKNOWN",
  1256. "NEXGEN_UNKNOWN",
  1257. "CENTAUR_UNKNOWN",
  1258. "RISE_UNKNOWN",
  1259. "SIS_UNKNOWN",
  1260. "TRANSMETA_UNKNOWN",
  1261. "NSC_UNKNOWN",
  1262. "80386",
  1263. "80486",
  1264. "PENTIUM",
  1265. "PENTIUM2",
  1266. "PENTIUM3",
  1267. "PENTIUMM",
  1268. "PENTIUM4",
  1269. "CORE2",
  1270. "PENRYN",
  1271. "DUNNINGTON",
  1272. "NEHALEM",
  1273. "ATOM",
  1274. "ITANIUM",
  1275. "ITANIUM2",
  1276. "5X86",
  1277. "K6",
  1278. "ATHLON",
  1279. "DURON",
  1280. "OPTERON",
  1281. "BARCELONA",
  1282. "SHANGHAI",
  1283. "ISTANBUL",
  1284. "CYRIX5X86",
  1285. "CYRIXM1",
  1286. "CYRIXM2",
  1287. "NEXGENNX586",
  1288. "CENTAURC6",
  1289. "RISEMP6",
  1290. "SYS55X",
  1291. "TM3X00",
  1292. "NSGEODE",
  1293. "VIAC3",
  1294. "NANO",
  1295. "SANDYBRIDGE",
  1296. "BOBCAT",
  1297. "BULLDOZER",
  1298. "PILEDRIVER",
  1299. "HASWELL",
  1300. "STEAMROLLER",
  1301. "EXCAVATOR",
  1302. };
  1303. static char *lowercpuname[] = {
  1304. "unknown",
  1305. "intel_unknown",
  1306. "umc_unknown",
  1307. "amd_unknown",
  1308. "cyrix_unknown",
  1309. "nexgen_unknown",
  1310. "centaur_unknown",
  1311. "rise_unknown",
  1312. "sis_unknown",
  1313. "transmeta_unknown",
  1314. "nsc_unknown",
  1315. "80386",
  1316. "80486",
  1317. "pentium",
  1318. "pentium2",
  1319. "pentium3",
  1320. "pentiumm",
  1321. "pentium4",
  1322. "core2",
  1323. "penryn",
  1324. "dunnington",
  1325. "nehalem",
  1326. "atom",
  1327. "itanium",
  1328. "itanium2",
  1329. "5x86",
  1330. "k6",
  1331. "athlon",
  1332. "duron",
  1333. "opteron",
  1334. "barcelona",
  1335. "shanghai",
  1336. "istanbul",
  1337. "cyrix5x86",
  1338. "cyrixm1",
  1339. "cyrixm2",
  1340. "nexgennx586",
  1341. "centaurc6",
  1342. "risemp6",
  1343. "sys55x",
  1344. "tms3x00",
  1345. "nsgeode",
  1346. "nano",
  1347. "sandybridge",
  1348. "bobcat",
  1349. "bulldozer",
  1350. "piledriver",
  1351. "haswell",
  1352. "steamroller",
  1353. "excavator",
  1354. };
  1355. static char *corename[] = {
  1356. "UNKOWN",
  1357. "80486",
  1358. "P5",
  1359. "P6",
  1360. "KATMAI",
  1361. "COPPERMINE",
  1362. "NORTHWOOD",
  1363. "PRESCOTT",
  1364. "BANIAS",
  1365. "ATHLON",
  1366. "OPTERON",
  1367. "BARCELONA",
  1368. "VIAC3",
  1369. "YONAH",
  1370. "CORE2",
  1371. "PENRYN",
  1372. "DUNNINGTON",
  1373. "NEHALEM",
  1374. "ATOM",
  1375. "NANO",
  1376. "SANDYBRIDGE",
  1377. "BOBCAT",
  1378. "BULLDOZER",
  1379. "PILEDRIVER",
  1380. "HASWELL",
  1381. "STEAMROLLER",
  1382. "EXCAVATOR",
  1383. };
  1384. static char *corename_lower[] = {
  1385. "unknown",
  1386. "80486",
  1387. "p5",
  1388. "p6",
  1389. "katmai",
  1390. "coppermine",
  1391. "northwood",
  1392. "prescott",
  1393. "banias",
  1394. "athlon",
  1395. "opteron",
  1396. "barcelona",
  1397. "viac3",
  1398. "yonah",
  1399. "core2",
  1400. "penryn",
  1401. "dunnington",
  1402. "nehalem",
  1403. "atom",
  1404. "nano",
  1405. "sandybridge",
  1406. "bobcat",
  1407. "bulldozer",
  1408. "piledriver",
  1409. "haswell",
  1410. "steamroller",
  1411. "excavator",
  1412. };
  1413. char *get_cpunamechar(void){
  1414. return cpuname[get_cpuname()];
  1415. }
  1416. char *get_lower_cpunamechar(void){
  1417. return lowercpuname[get_cpuname()];
  1418. }
  1419. int get_coretype(void){
  1420. int family, exfamily, model, exmodel, vendor;
  1421. if (!have_cpuid()) return CORE_80486;
  1422. family = get_cputype(GET_FAMILY);
  1423. exfamily = get_cputype(GET_EXFAMILY);
  1424. model = get_cputype(GET_MODEL);
  1425. exmodel = get_cputype(GET_EXMODEL);
  1426. vendor = get_vendor();
  1427. if (vendor == VENDOR_INTEL){
  1428. switch (family) {
  1429. case 4:
  1430. return CORE_80486;
  1431. case 5:
  1432. return CORE_P5;
  1433. case 6:
  1434. switch (exmodel) {
  1435. case 0:
  1436. switch (model) {
  1437. case 0:
  1438. case 1:
  1439. case 2:
  1440. case 3:
  1441. case 4:
  1442. case 5:
  1443. case 6:
  1444. return CORE_P6;
  1445. case 7:
  1446. return CORE_KATMAI;
  1447. case 8:
  1448. case 10:
  1449. case 11:
  1450. return CORE_COPPERMINE;
  1451. case 9:
  1452. case 13:
  1453. case 14:
  1454. return CORE_BANIAS;
  1455. case 15:
  1456. return CORE_CORE2;
  1457. }
  1458. break;
  1459. case 1:
  1460. switch (model) {
  1461. case 6:
  1462. return CORE_CORE2;
  1463. case 7:
  1464. return CORE_PENRYN;
  1465. case 10:
  1466. case 11:
  1467. case 14:
  1468. case 15:
  1469. return CORE_NEHALEM;
  1470. case 12:
  1471. return CORE_ATOM;
  1472. case 13:
  1473. return CORE_DUNNINGTON;
  1474. }
  1475. break;
  1476. case 2:
  1477. switch (model) {
  1478. case 5:
  1479. //Intel Core (Clarkdale) / Core (Arrandale)
  1480. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  1481. // Xeon (Clarkdale), 32nm
  1482. return CORE_NEHALEM;
  1483. case 10:
  1484. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  1485. if(support_avx())
  1486. return CORE_SANDYBRIDGE;
  1487. else
  1488. return CORE_NEHALEM; //OS doesn't support AVX
  1489. case 12:
  1490. //Xeon Processor 5600 (Westmere-EP)
  1491. return CORE_NEHALEM;
  1492. case 13:
  1493. //Intel Core i7-3000 / Xeon E5 (Sandy Bridge)
  1494. if(support_avx())
  1495. return CORE_SANDYBRIDGE;
  1496. else
  1497. return CORE_NEHALEM; //OS doesn't support AVX
  1498. case 14:
  1499. //Xeon E7540
  1500. case 15:
  1501. //Xeon Processor E7 (Westmere-EX)
  1502. return CORE_NEHALEM;
  1503. }
  1504. break;
  1505. case 3:
  1506. switch (model) {
  1507. case 10:
  1508. case 14:
  1509. if(support_avx())
  1510. return CORE_SANDYBRIDGE;
  1511. else
  1512. return CORE_NEHALEM; //OS doesn't support AVX
  1513. case 12:
  1514. case 15:
  1515. if(support_avx())
  1516. #ifndef NO_AVX2
  1517. return CORE_HASWELL;
  1518. #else
  1519. return CORE_SANDYBRIDGE;
  1520. #endif
  1521. else
  1522. return CORE_NEHALEM;
  1523. case 13:
  1524. //broadwell
  1525. if(support_avx())
  1526. #ifndef NO_AVX2
  1527. return CORE_HASWELL;
  1528. #else
  1529. return CORE_SANDYBRIDGE;
  1530. #endif
  1531. else
  1532. return CORE_NEHALEM;
  1533. }
  1534. break;
  1535. case 4:
  1536. switch (model) {
  1537. case 5:
  1538. case 6:
  1539. if(support_avx())
  1540. #ifndef NO_AVX2
  1541. return CORE_HASWELL;
  1542. #else
  1543. return CORE_SANDYBRIDGE;
  1544. #endif
  1545. else
  1546. return CORE_NEHALEM;
  1547. case 7:
  1548. case 15:
  1549. //broadwell
  1550. if(support_avx())
  1551. #ifndef NO_AVX2
  1552. return CORE_HASWELL;
  1553. #else
  1554. return CORE_SANDYBRIDGE;
  1555. #endif
  1556. else
  1557. return CORE_NEHALEM;
  1558. case 14:
  1559. //Skylake
  1560. if(support_avx())
  1561. #ifndef NO_AVX2
  1562. return CORE_HASWELL;
  1563. #else
  1564. return CORE_SANDYBRIDGE;
  1565. #endif
  1566. else
  1567. return CORE_NEHALEM;
  1568. }
  1569. break;
  1570. case 5:
  1571. switch (model) {
  1572. case 6:
  1573. //broadwell
  1574. if(support_avx())
  1575. #ifndef NO_AVX2
  1576. return CORE_HASWELL;
  1577. #else
  1578. return CORE_SANDYBRIDGE;
  1579. #endif
  1580. else
  1581. return CORE_NEHALEM;
  1582. case 5:
  1583. case 14:
  1584. // Skylake
  1585. if(support_avx())
  1586. #ifndef NO_AVX2
  1587. return CORE_HASWELL;
  1588. #else
  1589. return CORE_SANDYBRIDGE;
  1590. #endif
  1591. else
  1592. return CORE_NEHALEM;
  1593. }
  1594. break;
  1595. }
  1596. break;
  1597. case 15:
  1598. if (model <= 0x2) return CORE_NORTHWOOD;
  1599. else return CORE_PRESCOTT;
  1600. }
  1601. }
  1602. if (vendor == VENDOR_AMD){
  1603. if (family <= 0x5) return CORE_80486;
  1604. if (family <= 0xe) return CORE_ATHLON;
  1605. if (family == 0xf){
  1606. if ((exfamily == 0) || (exfamily == 2)) return CORE_OPTERON;
  1607. else if (exfamily == 5) return CORE_BOBCAT;
  1608. else if (exfamily == 6) {
  1609. switch (model) {
  1610. case 1:
  1611. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1612. if(support_avx())
  1613. return CORE_BULLDOZER;
  1614. else
  1615. return CORE_BARCELONA; //OS don't support AVX.
  1616. case 2: //AMD Piledriver
  1617. case 3: //AMD Richland
  1618. if(support_avx())
  1619. return CORE_PILEDRIVER;
  1620. else
  1621. return CORE_BARCELONA; //OS don't support AVX.
  1622. case 0:
  1623. switch(exmodel){
  1624. case 1: //AMD Trinity
  1625. if(support_avx())
  1626. return CORE_PILEDRIVER;
  1627. else
  1628. return CORE_BARCELONA; //OS don't support AVX.
  1629. case 3:
  1630. if(support_avx())
  1631. return CORE_STEAMROLLER;
  1632. else
  1633. return CORE_BARCELONA; //OS don't support AVX.
  1634. case 6:
  1635. if(support_avx())
  1636. return CORE_EXCAVATOR;
  1637. else
  1638. return CORE_BARCELONA; //OS don't support AVX.
  1639. }
  1640. break;
  1641. }
  1642. }else return CORE_BARCELONA;
  1643. }
  1644. }
  1645. if (vendor == VENDOR_CENTAUR) {
  1646. switch (family) {
  1647. case 0x6:
  1648. return CORE_NANO;
  1649. break;
  1650. }
  1651. return CORE_VIAC3;
  1652. }
  1653. return CORE_UNKNOWN;
  1654. }
  1655. void get_cpuconfig(void){
  1656. cache_info_t info;
  1657. int features;
  1658. printf("#define %s\n", cpuname[get_cpuname()]);
  1659. if (get_coretype() != CORE_P5) {
  1660. get_cacheinfo(CACHE_INFO_L1_I, &info);
  1661. if (info.size > 0) {
  1662. printf("#define L1_CODE_SIZE %d\n", info.size * 1024);
  1663. printf("#define L1_CODE_ASSOCIATIVE %d\n", info.associative);
  1664. printf("#define L1_CODE_LINESIZE %d\n", info.linesize);
  1665. }
  1666. get_cacheinfo(CACHE_INFO_L1_D, &info);
  1667. if (info.size > 0) {
  1668. printf("#define L1_DATA_SIZE %d\n", info.size * 1024);
  1669. printf("#define L1_DATA_ASSOCIATIVE %d\n", info.associative);
  1670. printf("#define L1_DATA_LINESIZE %d\n", info.linesize);
  1671. }
  1672. get_cacheinfo(CACHE_INFO_L2, &info);
  1673. if (info.size > 0) {
  1674. printf("#define L2_SIZE %d\n", info.size * 1024);
  1675. printf("#define L2_ASSOCIATIVE %d\n", info.associative);
  1676. printf("#define L2_LINESIZE %d\n", info.linesize);
  1677. } else {
  1678. //fall back for some virtual machines.
  1679. printf("#define L2_SIZE 1048576\n");
  1680. printf("#define L2_ASSOCIATIVE 6\n");
  1681. printf("#define L2_LINESIZE 64\n");
  1682. }
  1683. get_cacheinfo(CACHE_INFO_L3, &info);
  1684. if (info.size > 0) {
  1685. printf("#define L3_SIZE %d\n", info.size * 1024);
  1686. printf("#define L3_ASSOCIATIVE %d\n", info.associative);
  1687. printf("#define L3_LINESIZE %d\n", info.linesize);
  1688. }
  1689. get_cacheinfo(CACHE_INFO_L1_ITB, &info);
  1690. if (info.size > 0) {
  1691. printf("#define ITB_SIZE %d\n", info.size * 1024);
  1692. printf("#define ITB_ASSOCIATIVE %d\n", info.associative);
  1693. printf("#define ITB_ENTRIES %d\n", info.linesize);
  1694. }
  1695. get_cacheinfo(CACHE_INFO_L1_DTB, &info);
  1696. if (info.size > 0) {
  1697. printf("#define DTB_SIZE %d\n", info.size * 1024);
  1698. printf("#define DTB_ASSOCIATIVE %d\n", info.associative);
  1699. printf("#define DTB_DEFAULT_ENTRIES %d\n", info.linesize);
  1700. } else {
  1701. //fall back for some virtual machines.
  1702. printf("#define DTB_DEFAULT_ENTRIES 32\n");
  1703. }
  1704. features = get_cputype(GET_FEATURE);
  1705. if (features & HAVE_CMOV ) printf("#define HAVE_CMOV\n");
  1706. if (features & HAVE_MMX ) printf("#define HAVE_MMX\n");
  1707. if (features & HAVE_SSE ) printf("#define HAVE_SSE\n");
  1708. if (features & HAVE_SSE2 ) printf("#define HAVE_SSE2\n");
  1709. if (features & HAVE_SSE3 ) printf("#define HAVE_SSE3\n");
  1710. if (features & HAVE_SSSE3) printf("#define HAVE_SSSE3\n");
  1711. if (features & HAVE_SSE4_1) printf("#define HAVE_SSE4_1\n");
  1712. if (features & HAVE_SSE4_2) printf("#define HAVE_SSE4_2\n");
  1713. if (features & HAVE_SSE4A) printf("#define HAVE_SSE4A\n");
  1714. if (features & HAVE_SSE5 ) printf("#define HAVE_SSSE5\n");
  1715. if (features & HAVE_AVX ) printf("#define HAVE_AVX\n");
  1716. if (features & HAVE_3DNOWEX) printf("#define HAVE_3DNOWEX\n");
  1717. if (features & HAVE_3DNOW) printf("#define HAVE_3DNOW\n");
  1718. if (features & HAVE_FMA4 ) printf("#define HAVE_FMA4\n");
  1719. if (features & HAVE_FMA3 ) printf("#define HAVE_FMA3\n");
  1720. if (features & HAVE_CFLUSH) printf("#define HAVE_CFLUSH\n");
  1721. if (features & HAVE_HIT) printf("#define HAVE_HIT 1\n");
  1722. if (features & HAVE_MISALIGNSSE) printf("#define HAVE_MISALIGNSSE\n");
  1723. if (features & HAVE_128BITFPU) printf("#define HAVE_128BITFPU\n");
  1724. if (features & HAVE_FASTMOVU) printf("#define HAVE_FASTMOVU\n");
  1725. printf("#define NUM_SHAREDCACHE %d\n", get_cputype(GET_NUMSHARE) + 1);
  1726. printf("#define NUM_CORES %d\n", get_cputype(GET_NUMCORES) + 1);
  1727. features = get_coretype();
  1728. if (features > 0) printf("#define CORE_%s\n", corename[features]);
  1729. } else {
  1730. printf("#define DTB_DEFAULT_ENTRIES 16\n");
  1731. printf("#define L1_CODE_SIZE 8192\n");
  1732. printf("#define L1_DATA_SIZE 8192\n");
  1733. printf("#define L2_SIZE 0\n");
  1734. }
  1735. }
  1736. void get_architecture(void){
  1737. #ifndef __64BIT__
  1738. printf("X86");
  1739. #else
  1740. printf("X86_64");
  1741. #endif
  1742. }
  1743. void get_subarchitecture(void){
  1744. printf("%s", get_cpunamechar());
  1745. }
  1746. void get_subdirname(void){
  1747. #ifndef __64BIT__
  1748. printf("x86");
  1749. #else
  1750. printf("x86_64");
  1751. #endif
  1752. }
  1753. char *get_corename(void){
  1754. return corename[get_coretype()];
  1755. }
  1756. void get_libname(void){
  1757. printf("%s", corename_lower[get_coretype()]);
  1758. }
  1759. /* This if for Makefile */
  1760. void get_sse(void){
  1761. int features;
  1762. features = get_cputype(GET_FEATURE);
  1763. if (features & HAVE_MMX ) printf("HAVE_MMX=1\n");
  1764. if (features & HAVE_SSE ) printf("HAVE_SSE=1\n");
  1765. if (features & HAVE_SSE2 ) printf("HAVE_SSE2=1\n");
  1766. if (features & HAVE_SSE3 ) printf("HAVE_SSE3=1\n");
  1767. if (features & HAVE_SSSE3) printf("HAVE_SSSE3=1\n");
  1768. if (features & HAVE_SSE4_1) printf("HAVE_SSE4_1=1\n");
  1769. if (features & HAVE_SSE4_2) printf("HAVE_SSE4_2=1\n");
  1770. if (features & HAVE_SSE4A) printf("HAVE_SSE4A=1\n");
  1771. if (features & HAVE_SSE5 ) printf("HAVE_SSSE5=1\n");
  1772. if (features & HAVE_AVX ) printf("HAVE_AVX=1\n");
  1773. if (features & HAVE_3DNOWEX) printf("HAVE_3DNOWEX=1\n");
  1774. if (features & HAVE_3DNOW) printf("HAVE_3DNOW=1\n");
  1775. if (features & HAVE_FMA4 ) printf("HAVE_FMA4=1\n");
  1776. if (features & HAVE_FMA3 ) printf("HAVE_FMA3=1\n");
  1777. }