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

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