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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  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. case 13:
  1025. //Broadwell
  1026. if(support_avx())
  1027. #ifndef NO_AVX2
  1028. return CPUTYPE_HASWELL;
  1029. #else
  1030. return CPUTYPE_SANDYBRIDGE;
  1031. #endif
  1032. else
  1033. return CPUTYPE_NEHALEM;
  1034. }
  1035. break;
  1036. case 4:
  1037. switch (model) {
  1038. case 5:
  1039. case 6:
  1040. if(support_avx())
  1041. #ifndef NO_AVX2
  1042. return CPUTYPE_HASWELL;
  1043. #else
  1044. return CPUTYPE_SANDYBRIDGE;
  1045. #endif
  1046. else
  1047. return CPUTYPE_NEHALEM;
  1048. case 7:
  1049. case 15:
  1050. //Broadwell
  1051. if(support_avx())
  1052. #ifndef NO_AVX2
  1053. return CPUTYPE_HASWELL;
  1054. #else
  1055. return CPUTYPE_SANDYBRIDGE;
  1056. #endif
  1057. else
  1058. return CPUTYPE_NEHALEM;
  1059. case 14:
  1060. //Skylake
  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. }
  1070. break;
  1071. case 5:
  1072. switch (model) {
  1073. case 6:
  1074. //Broadwell
  1075. if(support_avx())
  1076. #ifndef NO_AVX2
  1077. return CPUTYPE_HASWELL;
  1078. #else
  1079. return CPUTYPE_SANDYBRIDGE;
  1080. #endif
  1081. else
  1082. return CPUTYPE_NEHALEM;
  1083. case 5:
  1084. case 14:
  1085. // Skylake
  1086. if(support_avx())
  1087. #ifndef NO_AVX2
  1088. return CPUTYPE_HASWELL;
  1089. #else
  1090. return CPUTYPE_SANDYBRIDGE;
  1091. #endif
  1092. else
  1093. return CPUTYPE_NEHALEM;
  1094. }
  1095. break;
  1096. }
  1097. break;
  1098. case 0x7:
  1099. return CPUTYPE_ITANIUM;
  1100. case 0xf:
  1101. switch (exfamily) {
  1102. case 0 :
  1103. return CPUTYPE_PENTIUM4;
  1104. case 1 :
  1105. return CPUTYPE_ITANIUM;
  1106. }
  1107. break;
  1108. }
  1109. return CPUTYPE_INTEL_UNKNOWN;
  1110. }
  1111. if (vendor == VENDOR_AMD){
  1112. switch (family) {
  1113. case 0x4:
  1114. return CPUTYPE_AMD5X86;
  1115. case 0x5:
  1116. return CPUTYPE_AMDK6;
  1117. case 0x6:
  1118. return CPUTYPE_ATHLON;
  1119. case 0xf:
  1120. switch (exfamily) {
  1121. case 0:
  1122. case 2:
  1123. return CPUTYPE_OPTERON;
  1124. case 1:
  1125. case 10:
  1126. return CPUTYPE_BARCELONA;
  1127. case 6:
  1128. switch (model) {
  1129. case 1:
  1130. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1131. if(support_avx())
  1132. return CPUTYPE_BULLDOZER;
  1133. else
  1134. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1135. case 2:
  1136. if(support_avx())
  1137. return CPUTYPE_PILEDRIVER;
  1138. else
  1139. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1140. case 0:
  1141. switch(exmodel){
  1142. case 3:
  1143. if(support_avx())
  1144. return CPUTYPE_STEAMROLLER;
  1145. else
  1146. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1147. case 6:
  1148. if(support_avx())
  1149. return CPUTYPE_EXCAVATOR;
  1150. else
  1151. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1152. }
  1153. break;
  1154. }
  1155. break;
  1156. case 5:
  1157. return CPUTYPE_BOBCAT;
  1158. }
  1159. break;
  1160. }
  1161. return CPUTYPE_AMD_UNKNOWN;
  1162. }
  1163. if (vendor == VENDOR_CYRIX){
  1164. switch (family) {
  1165. case 0x4:
  1166. return CPUTYPE_CYRIX5X86;
  1167. case 0x5:
  1168. return CPUTYPE_CYRIXM1;
  1169. case 0x6:
  1170. return CPUTYPE_CYRIXM2;
  1171. }
  1172. return CPUTYPE_CYRIX_UNKNOWN;
  1173. }
  1174. if (vendor == VENDOR_NEXGEN){
  1175. switch (family) {
  1176. case 0x5:
  1177. return CPUTYPE_NEXGENNX586;
  1178. }
  1179. return CPUTYPE_NEXGEN_UNKNOWN;
  1180. }
  1181. if (vendor == VENDOR_CENTAUR){
  1182. switch (family) {
  1183. case 0x5:
  1184. return CPUTYPE_CENTAURC6;
  1185. break;
  1186. case 0x6:
  1187. return CPUTYPE_NANO;
  1188. break;
  1189. }
  1190. return CPUTYPE_VIAC3;
  1191. }
  1192. if (vendor == VENDOR_RISE){
  1193. switch (family) {
  1194. case 0x5:
  1195. return CPUTYPE_RISEMP6;
  1196. }
  1197. return CPUTYPE_RISE_UNKNOWN;
  1198. }
  1199. if (vendor == VENDOR_SIS){
  1200. switch (family) {
  1201. case 0x5:
  1202. return CPUTYPE_SYS55X;
  1203. }
  1204. return CPUTYPE_SIS_UNKNOWN;
  1205. }
  1206. if (vendor == VENDOR_TRANSMETA){
  1207. switch (family) {
  1208. case 0x5:
  1209. return CPUTYPE_CRUSOETM3X;
  1210. }
  1211. return CPUTYPE_TRANSMETA_UNKNOWN;
  1212. }
  1213. if (vendor == VENDOR_NSC){
  1214. switch (family) {
  1215. case 0x5:
  1216. return CPUTYPE_NSGEODE;
  1217. }
  1218. return CPUTYPE_NSC_UNKNOWN;
  1219. }
  1220. return CPUTYPE_UNKNOWN;
  1221. }
  1222. static char *cpuname[] = {
  1223. "UNKNOWN",
  1224. "INTEL_UNKNOWN",
  1225. "UMC_UNKNOWN",
  1226. "AMD_UNKNOWN",
  1227. "CYRIX_UNKNOWN",
  1228. "NEXGEN_UNKNOWN",
  1229. "CENTAUR_UNKNOWN",
  1230. "RISE_UNKNOWN",
  1231. "SIS_UNKNOWN",
  1232. "TRANSMETA_UNKNOWN",
  1233. "NSC_UNKNOWN",
  1234. "80386",
  1235. "80486",
  1236. "PENTIUM",
  1237. "PENTIUM2",
  1238. "PENTIUM3",
  1239. "PENTIUMM",
  1240. "PENTIUM4",
  1241. "CORE2",
  1242. "PENRYN",
  1243. "DUNNINGTON",
  1244. "NEHALEM",
  1245. "ATOM",
  1246. "ITANIUM",
  1247. "ITANIUM2",
  1248. "5X86",
  1249. "K6",
  1250. "ATHLON",
  1251. "DURON",
  1252. "OPTERON",
  1253. "BARCELONA",
  1254. "SHANGHAI",
  1255. "ISTANBUL",
  1256. "CYRIX5X86",
  1257. "CYRIXM1",
  1258. "CYRIXM2",
  1259. "NEXGENNX586",
  1260. "CENTAURC6",
  1261. "RISEMP6",
  1262. "SYS55X",
  1263. "TM3X00",
  1264. "NSGEODE",
  1265. "VIAC3",
  1266. "NANO",
  1267. "SANDYBRIDGE",
  1268. "BOBCAT",
  1269. "BULLDOZER",
  1270. "PILEDRIVER",
  1271. "HASWELL",
  1272. "STEAMROLLER",
  1273. "EXCAVATOR",
  1274. };
  1275. static char *lowercpuname[] = {
  1276. "unknown",
  1277. "intel_unknown",
  1278. "umc_unknown",
  1279. "amd_unknown",
  1280. "cyrix_unknown",
  1281. "nexgen_unknown",
  1282. "centaur_unknown",
  1283. "rise_unknown",
  1284. "sis_unknown",
  1285. "transmeta_unknown",
  1286. "nsc_unknown",
  1287. "80386",
  1288. "80486",
  1289. "pentium",
  1290. "pentium2",
  1291. "pentium3",
  1292. "pentiumm",
  1293. "pentium4",
  1294. "core2",
  1295. "penryn",
  1296. "dunnington",
  1297. "nehalem",
  1298. "atom",
  1299. "itanium",
  1300. "itanium2",
  1301. "5x86",
  1302. "k6",
  1303. "athlon",
  1304. "duron",
  1305. "opteron",
  1306. "barcelona",
  1307. "shanghai",
  1308. "istanbul",
  1309. "cyrix5x86",
  1310. "cyrixm1",
  1311. "cyrixm2",
  1312. "nexgennx586",
  1313. "centaurc6",
  1314. "risemp6",
  1315. "sys55x",
  1316. "tms3x00",
  1317. "nsgeode",
  1318. "nano",
  1319. "sandybridge",
  1320. "bobcat",
  1321. "bulldozer",
  1322. "piledriver",
  1323. "haswell",
  1324. "steamroller",
  1325. "excavator",
  1326. };
  1327. static char *corename[] = {
  1328. "UNKOWN",
  1329. "80486",
  1330. "P5",
  1331. "P6",
  1332. "KATMAI",
  1333. "COPPERMINE",
  1334. "NORTHWOOD",
  1335. "PRESCOTT",
  1336. "BANIAS",
  1337. "ATHLON",
  1338. "OPTERON",
  1339. "BARCELONA",
  1340. "VIAC3",
  1341. "YONAH",
  1342. "CORE2",
  1343. "PENRYN",
  1344. "DUNNINGTON",
  1345. "NEHALEM",
  1346. "ATOM",
  1347. "NANO",
  1348. "SANDYBRIDGE",
  1349. "BOBCAT",
  1350. "BULLDOZER",
  1351. "PILEDRIVER",
  1352. "HASWELL",
  1353. "STEAMROLLER",
  1354. "EXCAVATOR",
  1355. };
  1356. static char *corename_lower[] = {
  1357. "unknown",
  1358. "80486",
  1359. "p5",
  1360. "p6",
  1361. "katmai",
  1362. "coppermine",
  1363. "northwood",
  1364. "prescott",
  1365. "banias",
  1366. "athlon",
  1367. "opteron",
  1368. "barcelona",
  1369. "viac3",
  1370. "yonah",
  1371. "core2",
  1372. "penryn",
  1373. "dunnington",
  1374. "nehalem",
  1375. "atom",
  1376. "nano",
  1377. "sandybridge",
  1378. "bobcat",
  1379. "bulldozer",
  1380. "piledriver",
  1381. "haswell",
  1382. "steamroller",
  1383. "excavator",
  1384. };
  1385. char *get_cpunamechar(void){
  1386. return cpuname[get_cpuname()];
  1387. }
  1388. char *get_lower_cpunamechar(void){
  1389. return lowercpuname[get_cpuname()];
  1390. }
  1391. int get_coretype(void){
  1392. int family, exfamily, model, exmodel, vendor;
  1393. if (!have_cpuid()) return CORE_80486;
  1394. family = get_cputype(GET_FAMILY);
  1395. exfamily = get_cputype(GET_EXFAMILY);
  1396. model = get_cputype(GET_MODEL);
  1397. exmodel = get_cputype(GET_EXMODEL);
  1398. vendor = get_vendor();
  1399. if (vendor == VENDOR_INTEL){
  1400. switch (family) {
  1401. case 4:
  1402. return CORE_80486;
  1403. case 5:
  1404. return CORE_P5;
  1405. case 6:
  1406. switch (exmodel) {
  1407. case 0:
  1408. switch (model) {
  1409. case 0:
  1410. case 1:
  1411. case 2:
  1412. case 3:
  1413. case 4:
  1414. case 5:
  1415. case 6:
  1416. return CORE_P6;
  1417. case 7:
  1418. return CORE_KATMAI;
  1419. case 8:
  1420. case 10:
  1421. case 11:
  1422. return CORE_COPPERMINE;
  1423. case 9:
  1424. case 13:
  1425. case 14:
  1426. return CORE_BANIAS;
  1427. case 15:
  1428. return CORE_CORE2;
  1429. }
  1430. break;
  1431. case 1:
  1432. switch (model) {
  1433. case 6:
  1434. return CORE_CORE2;
  1435. case 7:
  1436. return CORE_PENRYN;
  1437. case 10:
  1438. case 11:
  1439. case 14:
  1440. case 15:
  1441. return CORE_NEHALEM;
  1442. case 12:
  1443. return CORE_ATOM;
  1444. case 13:
  1445. return CORE_DUNNINGTON;
  1446. }
  1447. break;
  1448. case 2:
  1449. switch (model) {
  1450. case 5:
  1451. //Intel Core (Clarkdale) / Core (Arrandale)
  1452. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  1453. // Xeon (Clarkdale), 32nm
  1454. return CORE_NEHALEM;
  1455. case 10:
  1456. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  1457. if(support_avx())
  1458. return CORE_SANDYBRIDGE;
  1459. else
  1460. return CORE_NEHALEM; //OS doesn't support AVX
  1461. case 12:
  1462. //Xeon Processor 5600 (Westmere-EP)
  1463. return CORE_NEHALEM;
  1464. case 13:
  1465. //Intel Core i7-3000 / Xeon E5 (Sandy Bridge)
  1466. if(support_avx())
  1467. return CORE_SANDYBRIDGE;
  1468. else
  1469. return CORE_NEHALEM; //OS doesn't support AVX
  1470. case 14:
  1471. //Xeon E7540
  1472. case 15:
  1473. //Xeon Processor E7 (Westmere-EX)
  1474. return CORE_NEHALEM;
  1475. }
  1476. break;
  1477. case 3:
  1478. switch (model) {
  1479. case 10:
  1480. case 14:
  1481. if(support_avx())
  1482. return CORE_SANDYBRIDGE;
  1483. else
  1484. return CORE_NEHALEM; //OS doesn't support AVX
  1485. case 12:
  1486. case 15:
  1487. if(support_avx())
  1488. #ifndef NO_AVX2
  1489. return CORE_HASWELL;
  1490. #else
  1491. return CORE_SANDYBRIDGE;
  1492. #endif
  1493. else
  1494. return CORE_NEHALEM;
  1495. case 13:
  1496. //broadwell
  1497. if(support_avx())
  1498. #ifndef NO_AVX2
  1499. return CORE_HASWELL;
  1500. #else
  1501. return CORE_SANDYBRIDGE;
  1502. #endif
  1503. else
  1504. return CORE_NEHALEM;
  1505. }
  1506. break;
  1507. case 4:
  1508. switch (model) {
  1509. case 5:
  1510. case 6:
  1511. if(support_avx())
  1512. #ifndef NO_AVX2
  1513. return CORE_HASWELL;
  1514. #else
  1515. return CORE_SANDYBRIDGE;
  1516. #endif
  1517. else
  1518. return CORE_NEHALEM;
  1519. case 7:
  1520. case 15:
  1521. //broadwell
  1522. if(support_avx())
  1523. #ifndef NO_AVX2
  1524. return CORE_HASWELL;
  1525. #else
  1526. return CORE_SANDYBRIDGE;
  1527. #endif
  1528. else
  1529. return CORE_NEHALEM;
  1530. case 14:
  1531. //Skylake
  1532. if(support_avx())
  1533. #ifndef NO_AVX2
  1534. return CORE_HASWELL;
  1535. #else
  1536. return CORE_SANDYBRIDGE;
  1537. #endif
  1538. else
  1539. return CORE_NEHALEM;
  1540. }
  1541. break;
  1542. case 5:
  1543. switch (model) {
  1544. case 6:
  1545. //broadwell
  1546. if(support_avx())
  1547. #ifndef NO_AVX2
  1548. return CORE_HASWELL;
  1549. #else
  1550. return CORE_SANDYBRIDGE;
  1551. #endif
  1552. else
  1553. return CORE_NEHALEM;
  1554. case 5:
  1555. case 14:
  1556. // Skylake
  1557. if(support_avx())
  1558. #ifndef NO_AVX2
  1559. return CORE_HASWELL;
  1560. #else
  1561. return CORE_SANDYBRIDGE;
  1562. #endif
  1563. else
  1564. return CORE_NEHALEM;
  1565. }
  1566. break;
  1567. }
  1568. break;
  1569. case 15:
  1570. if (model <= 0x2) return CORE_NORTHWOOD;
  1571. else return CORE_PRESCOTT;
  1572. }
  1573. }
  1574. if (vendor == VENDOR_AMD){
  1575. if (family <= 0x5) return CORE_80486;
  1576. if (family <= 0xe) return CORE_ATHLON;
  1577. if (family == 0xf){
  1578. if ((exfamily == 0) || (exfamily == 2)) return CORE_OPTERON;
  1579. else if (exfamily == 5) return CORE_BOBCAT;
  1580. else if (exfamily == 6) {
  1581. switch (model) {
  1582. case 1:
  1583. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1584. if(support_avx())
  1585. return CORE_BULLDOZER;
  1586. else
  1587. return CORE_BARCELONA; //OS don't support AVX.
  1588. case 2:
  1589. if(support_avx())
  1590. return CORE_PILEDRIVER;
  1591. else
  1592. return CORE_BARCELONA; //OS don't support AVX.
  1593. case 0:
  1594. switch(exmodel){
  1595. case 3:
  1596. if(support_avx())
  1597. return CORE_STEAMROLLER;
  1598. else
  1599. return CORE_BARCELONA; //OS don't support AVX.
  1600. case 6:
  1601. if(support_avx())
  1602. return CORE_EXCAVATOR;
  1603. else
  1604. return CORE_BARCELONA; //OS don't support AVX.
  1605. }
  1606. break;
  1607. }
  1608. }else return CORE_BARCELONA;
  1609. }
  1610. }
  1611. if (vendor == VENDOR_CENTAUR) {
  1612. switch (family) {
  1613. case 0x6:
  1614. return CORE_NANO;
  1615. break;
  1616. }
  1617. return CORE_VIAC3;
  1618. }
  1619. return CORE_UNKNOWN;
  1620. }
  1621. void get_cpuconfig(void){
  1622. cache_info_t info;
  1623. int features;
  1624. printf("#define %s\n", cpuname[get_cpuname()]);
  1625. if (get_coretype() != CORE_P5) {
  1626. get_cacheinfo(CACHE_INFO_L1_I, &info);
  1627. if (info.size > 0) {
  1628. printf("#define L1_CODE_SIZE %d\n", info.size * 1024);
  1629. printf("#define L1_CODE_ASSOCIATIVE %d\n", info.associative);
  1630. printf("#define L1_CODE_LINESIZE %d\n", info.linesize);
  1631. }
  1632. get_cacheinfo(CACHE_INFO_L1_D, &info);
  1633. if (info.size > 0) {
  1634. printf("#define L1_DATA_SIZE %d\n", info.size * 1024);
  1635. printf("#define L1_DATA_ASSOCIATIVE %d\n", info.associative);
  1636. printf("#define L1_DATA_LINESIZE %d\n", info.linesize);
  1637. }
  1638. get_cacheinfo(CACHE_INFO_L2, &info);
  1639. if (info.size > 0) {
  1640. printf("#define L2_SIZE %d\n", info.size * 1024);
  1641. printf("#define L2_ASSOCIATIVE %d\n", info.associative);
  1642. printf("#define L2_LINESIZE %d\n", info.linesize);
  1643. } else {
  1644. //fall back for some virtual machines.
  1645. printf("#define L2_SIZE 1048576\n");
  1646. printf("#define L2_ASSOCIATIVE 6\n");
  1647. printf("#define L2_LINESIZE 64\n");
  1648. }
  1649. get_cacheinfo(CACHE_INFO_L3, &info);
  1650. if (info.size > 0) {
  1651. printf("#define L3_SIZE %d\n", info.size * 1024);
  1652. printf("#define L3_ASSOCIATIVE %d\n", info.associative);
  1653. printf("#define L3_LINESIZE %d\n", info.linesize);
  1654. }
  1655. get_cacheinfo(CACHE_INFO_L1_ITB, &info);
  1656. if (info.size > 0) {
  1657. printf("#define ITB_SIZE %d\n", info.size * 1024);
  1658. printf("#define ITB_ASSOCIATIVE %d\n", info.associative);
  1659. printf("#define ITB_ENTRIES %d\n", info.linesize);
  1660. }
  1661. get_cacheinfo(CACHE_INFO_L1_DTB, &info);
  1662. if (info.size > 0) {
  1663. printf("#define DTB_SIZE %d\n", info.size * 1024);
  1664. printf("#define DTB_ASSOCIATIVE %d\n", info.associative);
  1665. printf("#define DTB_DEFAULT_ENTRIES %d\n", info.linesize);
  1666. } else {
  1667. //fall back for some virtual machines.
  1668. printf("#define DTB_DEFAULT_ENTRIES 32\n");
  1669. }
  1670. features = get_cputype(GET_FEATURE);
  1671. if (features & HAVE_CMOV ) printf("#define HAVE_CMOV\n");
  1672. if (features & HAVE_MMX ) printf("#define HAVE_MMX\n");
  1673. if (features & HAVE_SSE ) printf("#define HAVE_SSE\n");
  1674. if (features & HAVE_SSE2 ) printf("#define HAVE_SSE2\n");
  1675. if (features & HAVE_SSE3 ) printf("#define HAVE_SSE3\n");
  1676. if (features & HAVE_SSSE3) printf("#define HAVE_SSSE3\n");
  1677. if (features & HAVE_SSE4_1) printf("#define HAVE_SSE4_1\n");
  1678. if (features & HAVE_SSE4_2) printf("#define HAVE_SSE4_2\n");
  1679. if (features & HAVE_SSE4A) printf("#define HAVE_SSE4A\n");
  1680. if (features & HAVE_SSE5 ) printf("#define HAVE_SSSE5\n");
  1681. if (features & HAVE_AVX ) printf("#define HAVE_AVX\n");
  1682. if (features & HAVE_3DNOWEX) printf("#define HAVE_3DNOWEX\n");
  1683. if (features & HAVE_3DNOW) printf("#define HAVE_3DNOW\n");
  1684. if (features & HAVE_FMA4 ) printf("#define HAVE_FMA4\n");
  1685. if (features & HAVE_FMA3 ) printf("#define HAVE_FMA3\n");
  1686. if (features & HAVE_CFLUSH) printf("#define HAVE_CFLUSH\n");
  1687. if (features & HAVE_HIT) printf("#define HAVE_HIT 1\n");
  1688. if (features & HAVE_MISALIGNSSE) printf("#define HAVE_MISALIGNSSE\n");
  1689. if (features & HAVE_128BITFPU) printf("#define HAVE_128BITFPU\n");
  1690. if (features & HAVE_FASTMOVU) printf("#define HAVE_FASTMOVU\n");
  1691. printf("#define NUM_SHAREDCACHE %d\n", get_cputype(GET_NUMSHARE) + 1);
  1692. printf("#define NUM_CORES %d\n", get_cputype(GET_NUMCORES) + 1);
  1693. features = get_coretype();
  1694. if (features > 0) printf("#define CORE_%s\n", corename[features]);
  1695. } else {
  1696. printf("#define DTB_DEFAULT_ENTRIES 16\n");
  1697. printf("#define L1_CODE_SIZE 8192\n");
  1698. printf("#define L1_DATA_SIZE 8192\n");
  1699. printf("#define L2_SIZE 0\n");
  1700. }
  1701. }
  1702. void get_architecture(void){
  1703. #ifndef __64BIT__
  1704. printf("X86");
  1705. #else
  1706. printf("X86_64");
  1707. #endif
  1708. }
  1709. void get_subarchitecture(void){
  1710. printf("%s", get_cpunamechar());
  1711. }
  1712. void get_subdirname(void){
  1713. #ifndef __64BIT__
  1714. printf("x86");
  1715. #else
  1716. printf("x86_64");
  1717. #endif
  1718. }
  1719. char *get_corename(void){
  1720. return corename[get_coretype()];
  1721. }
  1722. void get_libname(void){
  1723. printf("%s", corename_lower[get_coretype()]);
  1724. }
  1725. /* This if for Makefile */
  1726. void get_sse(void){
  1727. int features;
  1728. features = get_cputype(GET_FEATURE);
  1729. if (features & HAVE_MMX ) printf("HAVE_MMX=1\n");
  1730. if (features & HAVE_SSE ) printf("HAVE_SSE=1\n");
  1731. if (features & HAVE_SSE2 ) printf("HAVE_SSE2=1\n");
  1732. if (features & HAVE_SSE3 ) printf("HAVE_SSE3=1\n");
  1733. if (features & HAVE_SSSE3) printf("HAVE_SSSE3=1\n");
  1734. if (features & HAVE_SSE4_1) printf("HAVE_SSE4_1=1\n");
  1735. if (features & HAVE_SSE4_2) printf("HAVE_SSE4_2=1\n");
  1736. if (features & HAVE_SSE4A) printf("HAVE_SSE4A=1\n");
  1737. if (features & HAVE_SSE5 ) printf("HAVE_SSSE5=1\n");
  1738. if (features & HAVE_AVX ) printf("HAVE_AVX=1\n");
  1739. if (features & HAVE_3DNOWEX) printf("HAVE_3DNOWEX=1\n");
  1740. if (features & HAVE_3DNOW) printf("HAVE_3DNOW=1\n");
  1741. if (features & HAVE_FMA4 ) printf("HAVE_FMA4=1\n");
  1742. if (features & HAVE_FMA3 ) printf("HAVE_FMA3=1\n");
  1743. }