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

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