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

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