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

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