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

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