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 43 kB

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