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

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