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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  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. "HASWELL",
  1158. };
  1159. static char *lowercpuname[] = {
  1160. "unknown",
  1161. "intel_unknown",
  1162. "umc_unknown",
  1163. "amd_unknown",
  1164. "cyrix_unknown",
  1165. "nexgen_unknown",
  1166. "centaur_unknown",
  1167. "rise_unknown",
  1168. "sis_unknown",
  1169. "transmeta_unknown",
  1170. "nsc_unknown",
  1171. "80386",
  1172. "80486",
  1173. "pentium",
  1174. "pentium2",
  1175. "pentium3",
  1176. "pentiumm",
  1177. "pentium4",
  1178. "core2",
  1179. "penryn",
  1180. "dunnington",
  1181. "nehalem",
  1182. "atom",
  1183. "itanium",
  1184. "itanium2",
  1185. "5x86",
  1186. "k6",
  1187. "athlon",
  1188. "duron",
  1189. "opteron",
  1190. "barcelona",
  1191. "shanghai",
  1192. "istanbul",
  1193. "cyrix5x86",
  1194. "cyrixm1",
  1195. "cyrixm2",
  1196. "nexgennx586",
  1197. "centaurc6",
  1198. "risemp6",
  1199. "sys55x",
  1200. "tms3x00",
  1201. "nsgeode",
  1202. "nano",
  1203. "sandybridge",
  1204. "bobcat",
  1205. "bulldozer",
  1206. "piledriver",
  1207. "haswell",
  1208. };
  1209. static char *corename[] = {
  1210. "UNKOWN",
  1211. "80486",
  1212. "P5",
  1213. "P6",
  1214. "KATMAI",
  1215. "COPPERMINE",
  1216. "NORTHWOOD",
  1217. "PRESCOTT",
  1218. "BANIAS",
  1219. "ATHLON",
  1220. "OPTERON",
  1221. "BARCELONA",
  1222. "VIAC3",
  1223. "YONAH",
  1224. "CORE2",
  1225. "PENRYN",
  1226. "DUNNINGTON",
  1227. "NEHALEM",
  1228. "ATOM",
  1229. "NANO",
  1230. "SANDYBRIDGE",
  1231. "BOBCAT",
  1232. "BULLDOZER",
  1233. "PILEDRIVER",
  1234. "HASWELL",
  1235. };
  1236. static char *corename_lower[] = {
  1237. "unknown",
  1238. "80486",
  1239. "p5",
  1240. "p6",
  1241. "katmai",
  1242. "coppermine",
  1243. "northwood",
  1244. "prescott",
  1245. "banias",
  1246. "athlon",
  1247. "opteron",
  1248. "barcelona",
  1249. "viac3",
  1250. "yonah",
  1251. "core2",
  1252. "penryn",
  1253. "dunnington",
  1254. "nehalem",
  1255. "atom",
  1256. "nano",
  1257. "sandybridge",
  1258. "bobcat",
  1259. "bulldozer",
  1260. "piledriver",
  1261. "haswell",
  1262. };
  1263. char *get_cpunamechar(void){
  1264. return cpuname[get_cpuname()];
  1265. }
  1266. char *get_lower_cpunamechar(void){
  1267. return lowercpuname[get_cpuname()];
  1268. }
  1269. int get_coretype(void){
  1270. int family, exfamily, model, exmodel, vendor;
  1271. if (!have_cpuid()) return CORE_80486;
  1272. family = get_cputype(GET_FAMILY);
  1273. exfamily = get_cputype(GET_EXFAMILY);
  1274. model = get_cputype(GET_MODEL);
  1275. exmodel = get_cputype(GET_EXMODEL);
  1276. vendor = get_vendor();
  1277. if (vendor == VENDOR_INTEL){
  1278. switch (family) {
  1279. case 4:
  1280. return CORE_80486;
  1281. case 5:
  1282. return CORE_P5;
  1283. case 6:
  1284. switch (exmodel) {
  1285. case 0:
  1286. switch (model) {
  1287. case 0:
  1288. case 1:
  1289. case 2:
  1290. case 3:
  1291. case 4:
  1292. case 5:
  1293. case 6:
  1294. return CORE_P6;
  1295. case 7:
  1296. return CORE_KATMAI;
  1297. case 8:
  1298. case 10:
  1299. case 11:
  1300. return CORE_COPPERMINE;
  1301. case 9:
  1302. case 13:
  1303. case 14:
  1304. return CORE_BANIAS;
  1305. case 15:
  1306. return CORE_CORE2;
  1307. }
  1308. break;
  1309. case 1:
  1310. switch (model) {
  1311. case 6:
  1312. return CORE_CORE2;
  1313. case 7:
  1314. return CORE_PENRYN;
  1315. case 10:
  1316. case 11:
  1317. case 14:
  1318. case 15:
  1319. return CORE_NEHALEM;
  1320. case 12:
  1321. return CORE_ATOM;
  1322. case 13:
  1323. return CORE_DUNNINGTON;
  1324. }
  1325. break;
  1326. case 2:
  1327. switch (model) {
  1328. case 5:
  1329. //Intel Core (Clarkdale) / Core (Arrandale)
  1330. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  1331. // Xeon (Clarkdale), 32nm
  1332. return CORE_NEHALEM;
  1333. case 10:
  1334. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  1335. if(support_avx())
  1336. return CORE_SANDYBRIDGE;
  1337. else
  1338. return CORE_NEHALEM; //OS doesn't support AVX
  1339. case 12:
  1340. //Xeon Processor 5600 (Westmere-EP)
  1341. return CORE_NEHALEM;
  1342. case 13:
  1343. //Intel Core i7-3000 / Xeon E5 (Sandy Bridge)
  1344. if(support_avx())
  1345. return CORE_SANDYBRIDGE;
  1346. else
  1347. return CORE_NEHALEM; //OS doesn't support AVX
  1348. case 14:
  1349. //Xeon E7540
  1350. case 15:
  1351. //Xeon Processor E7 (Westmere-EX)
  1352. return CORE_NEHALEM;
  1353. }
  1354. break;
  1355. case 3:
  1356. switch (model) {
  1357. case 10:
  1358. if(support_avx())
  1359. return CORE_SANDYBRIDGE;
  1360. else
  1361. return CORE_NEHALEM; //OS doesn't support AVX
  1362. case 12:
  1363. if(support_avx())
  1364. return CORE_HASWELL;
  1365. else
  1366. return CORE_NEHALEM;
  1367. }
  1368. break;
  1369. case 4:
  1370. switch (model) {
  1371. case 5:
  1372. if(support_avx())
  1373. return CORE_HASWELL;
  1374. else
  1375. return CORE_NEHALEM;
  1376. }
  1377. break;
  1378. }
  1379. break;
  1380. case 15:
  1381. if (model <= 0x2) return CORE_NORTHWOOD;
  1382. else return CORE_PRESCOTT;
  1383. }
  1384. }
  1385. if (vendor == VENDOR_AMD){
  1386. if (family <= 0x5) return CORE_80486;
  1387. if (family <= 0xe) return CORE_ATHLON;
  1388. if (family == 0xf){
  1389. if ((exfamily == 0) || (exfamily == 2)) return CORE_OPTERON;
  1390. else if (exfamily == 5) return CORE_BOBCAT;
  1391. else if (exfamily == 6) {
  1392. switch (model) {
  1393. case 1:
  1394. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1395. if(support_avx())
  1396. return CORE_BULLDOZER;
  1397. else
  1398. return CORE_BARCELONA; //OS don't support AVX.
  1399. case 2:
  1400. if(support_avx())
  1401. return CORE_PILEDRIVER;
  1402. else
  1403. return CORE_BARCELONA; //OS don't support AVX.
  1404. }
  1405. }else return CORE_BARCELONA;
  1406. }
  1407. }
  1408. if (vendor == VENDOR_CENTAUR) {
  1409. switch (family) {
  1410. case 0x6:
  1411. return CORE_NANO;
  1412. break;
  1413. }
  1414. return CORE_VIAC3;
  1415. }
  1416. return CORE_UNKNOWN;
  1417. }
  1418. void get_cpuconfig(void){
  1419. cache_info_t info;
  1420. int features;
  1421. printf("#define %s\n", cpuname[get_cpuname()]);
  1422. if (get_coretype() != CORE_P5) {
  1423. get_cacheinfo(CACHE_INFO_L1_I, &info);
  1424. if (info.size > 0) {
  1425. printf("#define L1_CODE_SIZE %d\n", info.size * 1024);
  1426. printf("#define L1_CODE_ASSOCIATIVE %d\n", info.associative);
  1427. printf("#define L1_CODE_LINESIZE %d\n", info.linesize);
  1428. }
  1429. get_cacheinfo(CACHE_INFO_L1_D, &info);
  1430. if (info.size > 0) {
  1431. printf("#define L1_DATA_SIZE %d\n", info.size * 1024);
  1432. printf("#define L1_DATA_ASSOCIATIVE %d\n", info.associative);
  1433. printf("#define L1_DATA_LINESIZE %d\n", info.linesize);
  1434. }
  1435. get_cacheinfo(CACHE_INFO_L2, &info);
  1436. if (info.size > 0) {
  1437. printf("#define L2_SIZE %d\n", info.size * 1024);
  1438. printf("#define L2_ASSOCIATIVE %d\n", info.associative);
  1439. printf("#define L2_LINESIZE %d\n", info.linesize);
  1440. }
  1441. get_cacheinfo(CACHE_INFO_L3, &info);
  1442. if (info.size > 0) {
  1443. printf("#define L3_SIZE %d\n", info.size * 1024);
  1444. printf("#define L3_ASSOCIATIVE %d\n", info.associative);
  1445. printf("#define L3_LINESIZE %d\n", info.linesize);
  1446. }
  1447. get_cacheinfo(CACHE_INFO_L1_ITB, &info);
  1448. if (info.size > 0) {
  1449. printf("#define ITB_SIZE %d\n", info.size * 1024);
  1450. printf("#define ITB_ASSOCIATIVE %d\n", info.associative);
  1451. printf("#define ITB_ENTRIES %d\n", info.linesize);
  1452. }
  1453. get_cacheinfo(CACHE_INFO_L1_DTB, &info);
  1454. if (info.size > 0) {
  1455. printf("#define DTB_SIZE %d\n", info.size * 1024);
  1456. printf("#define DTB_ASSOCIATIVE %d\n", info.associative);
  1457. printf("#define DTB_DEFAULT_ENTRIES %d\n", info.linesize);
  1458. } else {
  1459. //fall back for some virtual machines.
  1460. printf("#define DTB_DEFAULT_ENTRIES 32\n");
  1461. }
  1462. features = get_cputype(GET_FEATURE);
  1463. if (features & HAVE_CMOV ) printf("#define HAVE_CMOV\n");
  1464. if (features & HAVE_MMX ) printf("#define HAVE_MMX\n");
  1465. if (features & HAVE_SSE ) printf("#define HAVE_SSE\n");
  1466. if (features & HAVE_SSE2 ) printf("#define HAVE_SSE2\n");
  1467. if (features & HAVE_SSE3 ) printf("#define HAVE_SSE3\n");
  1468. if (features & HAVE_SSSE3) printf("#define HAVE_SSSE3\n");
  1469. if (features & HAVE_SSE4_1) printf("#define HAVE_SSE4_1\n");
  1470. if (features & HAVE_SSE4_2) printf("#define HAVE_SSE4_2\n");
  1471. if (features & HAVE_SSE4A) printf("#define HAVE_SSE4A\n");
  1472. if (features & HAVE_SSE5 ) printf("#define HAVE_SSSE5\n");
  1473. if (features & HAVE_AVX ) printf("#define HAVE_AVX\n");
  1474. if (features & HAVE_3DNOWEX) printf("#define HAVE_3DNOWEX\n");
  1475. if (features & HAVE_3DNOW) printf("#define HAVE_3DNOW\n");
  1476. if (features & HAVE_FMA4 ) printf("#define HAVE_FMA4\n");
  1477. if (features & HAVE_FMA3 ) printf("#define HAVE_FMA3\n");
  1478. if (features & HAVE_CFLUSH) printf("#define HAVE_CFLUSH\n");
  1479. if (features & HAVE_HIT) printf("#define HAVE_HIT 1\n");
  1480. if (features & HAVE_MISALIGNSSE) printf("#define HAVE_MISALIGNSSE\n");
  1481. if (features & HAVE_128BITFPU) printf("#define HAVE_128BITFPU\n");
  1482. if (features & HAVE_FASTMOVU) printf("#define HAVE_FASTMOVU\n");
  1483. printf("#define NUM_SHAREDCACHE %d\n", get_cputype(GET_NUMSHARE) + 1);
  1484. printf("#define NUM_CORES %d\n", get_cputype(GET_NUMCORES) + 1);
  1485. features = get_coretype();
  1486. if (features > 0) printf("#define CORE_%s\n", corename[features]);
  1487. } else {
  1488. printf("#define DTB_DEFAULT_ENTRIES 16\n");
  1489. printf("#define L1_CODE_SIZE 8192\n");
  1490. printf("#define L1_DATA_SIZE 8192\n");
  1491. printf("#define L2_SIZE 0\n");
  1492. }
  1493. }
  1494. void get_architecture(void){
  1495. #ifndef __64BIT__
  1496. printf("X86");
  1497. #else
  1498. printf("X86_64");
  1499. #endif
  1500. }
  1501. void get_subarchitecture(void){
  1502. printf("%s", get_cpunamechar());
  1503. }
  1504. void get_subdirname(void){
  1505. #ifndef __64BIT__
  1506. printf("x86");
  1507. #else
  1508. printf("x86_64");
  1509. #endif
  1510. }
  1511. char *get_corename(void){
  1512. return corename[get_coretype()];
  1513. }
  1514. void get_libname(void){
  1515. printf("%s", corename_lower[get_coretype()]);
  1516. }
  1517. /* This if for Makefile */
  1518. void get_sse(void){
  1519. int features;
  1520. features = get_cputype(GET_FEATURE);
  1521. if (features & HAVE_MMX ) printf("HAVE_MMX=1\n");
  1522. if (features & HAVE_SSE ) printf("HAVE_SSE=1\n");
  1523. if (features & HAVE_SSE2 ) printf("HAVE_SSE2=1\n");
  1524. if (features & HAVE_SSE3 ) printf("HAVE_SSE3=1\n");
  1525. if (features & HAVE_SSSE3) printf("HAVE_SSSE3=1\n");
  1526. if (features & HAVE_SSE4_1) printf("HAVE_SSE4_1=1\n");
  1527. if (features & HAVE_SSE4_2) printf("HAVE_SSE4_2=1\n");
  1528. if (features & HAVE_SSE4A) printf("HAVE_SSE4A=1\n");
  1529. if (features & HAVE_SSE5 ) printf("HAVE_SSSE5=1\n");
  1530. if (features & HAVE_AVX ) printf("HAVE_AVX=1\n");
  1531. if (features & HAVE_3DNOWEX) printf("HAVE_3DNOWEX=1\n");
  1532. if (features & HAVE_3DNOW) printf("HAVE_3DNOW=1\n");
  1533. if (features & HAVE_FMA4 ) printf("HAVE_FMA4=1\n");
  1534. if (features & HAVE_FMA3 ) printf("HAVE_FMA3=1\n");
  1535. }