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

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