You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpuid_x86.c 40 kB

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