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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include "cpuid.h"
  41. #if defined(_MSC_VER) && !defined(__clang__)
  42. #define C_INLINE __inline
  43. #else
  44. #define C_INLINE inline
  45. #endif
  46. /*
  47. #ifdef NO_AVX
  48. #define CPUTYPE_HASWELL CPUTYPE_NEHALEM
  49. #define CORE_HASWELL CORE_NEHALEM
  50. #define CPUTYPE_SKYLAKEX CPUTYPE_NEHALEM
  51. #define CORE_SKYLAKEX CORE_NEHALEM
  52. #define CPUTYPE_SANDYBRIDGE CPUTYPE_NEHALEM
  53. #define CORE_SANDYBRIDGE CORE_NEHALEM
  54. #define CPUTYPE_BULLDOZER CPUTYPE_BARCELONA
  55. #define CORE_BULLDOZER CORE_BARCELONA
  56. #define CPUTYPE_PILEDRIVER CPUTYPE_BARCELONA
  57. #define CORE_PILEDRIVER CORE_BARCELONA
  58. #endif
  59. */
  60. #if defined(_MSC_VER) && !defined(__clang__)
  61. void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx)
  62. {
  63. int cpuInfo[4] = {-1};
  64. __cpuid(cpuInfo, op);
  65. *eax = cpuInfo[0];
  66. *ebx = cpuInfo[1];
  67. *ecx = cpuInfo[2];
  68. *edx = cpuInfo[3];
  69. }
  70. void cpuid_count(int op, int count, int *eax, int *ebx, int *ecx, int *edx)
  71. {
  72. int cpuInfo[4] = {-1};
  73. __cpuidex(cpuInfo, op, count);
  74. *eax = cpuInfo[0];
  75. *ebx = cpuInfo[1];
  76. *ecx = cpuInfo[2];
  77. *edx = cpuInfo[3];
  78. }
  79. #else
  80. #ifndef CPUIDEMU
  81. #if defined(__APPLE__) && defined(__i386__)
  82. void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx);
  83. void cpuid_count(int op, int count, int *eax, int *ebx, int *ecx, int *edx);
  84. #else
  85. static C_INLINE void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
  86. #if defined(__i386__) && defined(__PIC__)
  87. __asm__ __volatile__
  88. ("mov %%ebx, %%edi;"
  89. "cpuid;"
  90. "xchgl %%ebx, %%edi;"
  91. : "=a" (*eax), "=D" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (op), "c" (0) : "cc");
  92. #else
  93. __asm__ __volatile__
  94. ("cpuid": "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (op) , "c" (0) : "cc");
  95. #endif
  96. }
  97. static C_INLINE void cpuid_count(int op, int count ,int *eax, int *ebx, int *ecx, int *edx){
  98. #if defined(__i386__) && defined(__PIC__)
  99. __asm__ __volatile__
  100. ("mov %%ebx, %%edi;"
  101. "cpuid;"
  102. "xchgl %%ebx, %%edi;"
  103. : "=a" (*eax), "=D" (*ebx), "=c" (*ecx), "=d" (*edx) : "0" (op), "2" (count) : "cc");
  104. #else
  105. __asm__ __volatile__
  106. ("cpuid": "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "0" (op), "2" (count) : "cc");
  107. #endif
  108. }
  109. #endif
  110. #else
  111. typedef struct {
  112. unsigned int id, a, b, c, d;
  113. } idlist_t;
  114. typedef struct {
  115. char *vendor;
  116. char *name;
  117. int start, stop;
  118. } vendor_t;
  119. extern idlist_t idlist[];
  120. extern vendor_t vendor[];
  121. static int cv = VENDOR;
  122. void cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx){
  123. static int current = 0;
  124. int start = vendor[cv].start;
  125. int stop = vendor[cv].stop;
  126. int count = stop - start;
  127. if ((current < start) || (current > stop)) current = start;
  128. while ((count > 0) && (idlist[current].id != op)) {
  129. current ++;
  130. if (current > stop) current = start;
  131. count --;
  132. }
  133. *eax = idlist[current].a;
  134. *ebx = idlist[current].b;
  135. *ecx = idlist[current].c;
  136. *edx = idlist[current].d;
  137. }
  138. void cpuid_count (unsigned int op, unsigned int count, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx) {
  139. return cpuid (op, eax, ebx, ecx, edx);
  140. }
  141. #endif
  142. #endif // _MSC_VER
  143. static C_INLINE int have_cpuid(void){
  144. int eax, ebx, ecx, edx;
  145. cpuid(0, &eax, &ebx, &ecx, &edx);
  146. return eax;
  147. }
  148. static C_INLINE int have_excpuid(void){
  149. int eax, ebx, ecx, edx;
  150. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  151. return eax & 0xffff;
  152. }
  153. #ifndef NO_AVX
  154. static C_INLINE void xgetbv(int op, int * eax, int * edx){
  155. //Use binary code for xgetbv
  156. #if defined(_MSC_VER) && !defined(__clang__)
  157. *eax = __xgetbv(op);
  158. #else
  159. __asm__ __volatile__
  160. (".byte 0x0f, 0x01, 0xd0": "=a" (*eax), "=d" (*edx) : "c" (op) : "cc");
  161. #endif
  162. }
  163. #endif
  164. int support_avx(){
  165. #ifndef NO_AVX
  166. int eax, ebx, ecx, edx;
  167. int ret=0;
  168. cpuid(1, &eax, &ebx, &ecx, &edx);
  169. if ((ecx & (1 << 28)) != 0 && (ecx & (1 << 27)) != 0 && (ecx & (1 << 26)) != 0){
  170. xgetbv(0, &eax, &edx);
  171. if((eax & 6) == 6){
  172. ret=1; //OS supports saving xmm and ymm registers (6 = (1<<1) | (1<<2))
  173. }
  174. }
  175. return ret;
  176. #else
  177. return 0;
  178. #endif
  179. }
  180. int support_avx2(){
  181. #ifndef NO_AVX2
  182. int eax, ebx, ecx=0, edx;
  183. int ret=0;
  184. if (!support_avx())
  185. return 0;
  186. cpuid(7, &eax, &ebx, &ecx, &edx);
  187. if((ebx & (1<<5)) != 0)
  188. ret=1; //CPU supports AVX2
  189. return ret;
  190. #else
  191. return 0;
  192. #endif
  193. }
  194. int support_avx512(){
  195. #if !defined(NO_AVX) && !defined(NO_AVX512)
  196. int eax, ebx, ecx, edx;
  197. int ret=0;
  198. if (!support_avx())
  199. return 0;
  200. cpuid(7, &eax, &ebx, &ecx, &edx);
  201. if((ebx & (1<<5)) == 0){
  202. ret=0; //cpu does not have avx2 flag
  203. }
  204. if((ebx & (1<<31)) != 0){ //AVX512VL flag
  205. xgetbv(0, &eax, &edx);
  206. if((eax & 0xe0) == 0xe0)
  207. ret=1; //OS supports saving zmm registers
  208. }
  209. return ret;
  210. #else
  211. return 0;
  212. #endif
  213. }
  214. int support_avx512_bf16(){
  215. #if !defined(NO_AVX) && !defined(NO_AVX512)
  216. int eax, ebx, ecx, edx;
  217. int ret=0;
  218. if (!support_avx512())
  219. return 0;
  220. cpuid_count(7, 1, &eax, &ebx, &ecx, &edx);
  221. if((eax & 32) == 32){
  222. ret=1; // CPUID.7.1:EAX[bit 5] indicates whether avx512_bf16 supported or not
  223. }
  224. return ret;
  225. #else
  226. return 0;
  227. #endif
  228. }
  229. int get_vendor(void){
  230. int eax, ebx, ecx, edx;
  231. char vendor[13];
  232. cpuid(0, &eax, &ebx, &ecx, &edx);
  233. *(int *)(&vendor[0]) = ebx;
  234. *(int *)(&vendor[4]) = edx;
  235. *(int *)(&vendor[8]) = ecx;
  236. vendor[12] = (char)0;
  237. if (!strcmp(vendor, "GenuineIntel")) return VENDOR_INTEL;
  238. if (!strcmp(vendor, " UMC UMC UMC")) return VENDOR_UMC;
  239. if (!strcmp(vendor, "AuthenticAMD")) return VENDOR_AMD;
  240. if (!strcmp(vendor, "CyrixInstead")) return VENDOR_CYRIX;
  241. if (!strcmp(vendor, "NexGenDriven")) return VENDOR_NEXGEN;
  242. if (!strcmp(vendor, "CentaurHauls")) return VENDOR_CENTAUR;
  243. if (!strcmp(vendor, "RiseRiseRise")) return VENDOR_RISE;
  244. if (!strcmp(vendor, " SiS SiS SiS")) return VENDOR_SIS;
  245. if (!strcmp(vendor, "GenuineTMx86")) return VENDOR_TRANSMETA;
  246. if (!strcmp(vendor, "Geode by NSC")) return VENDOR_NSC;
  247. if (!strcmp(vendor, "HygonGenuine")) return VENDOR_HYGON;
  248. if ((eax == 0) || ((eax & 0x500) != 0)) return VENDOR_INTEL;
  249. return VENDOR_UNKNOWN;
  250. }
  251. int get_cputype(int gettype){
  252. int eax, ebx, ecx, edx;
  253. int extend_family, family;
  254. int extend_model, model;
  255. int type, stepping;
  256. int feature = 0;
  257. cpuid(1, &eax, &ebx, &ecx, &edx);
  258. switch (gettype) {
  259. case GET_EXFAMILY :
  260. return BITMASK(eax, 20, 0xff);
  261. case GET_EXMODEL :
  262. return BITMASK(eax, 16, 0x0f);
  263. case GET_TYPE :
  264. return BITMASK(eax, 12, 0x03);
  265. case GET_FAMILY :
  266. return BITMASK(eax, 8, 0x0f);
  267. case GET_MODEL :
  268. return BITMASK(eax, 4, 0x0f);
  269. case GET_APICID :
  270. return BITMASK(ebx, 24, 0x0f);
  271. case GET_LCOUNT :
  272. return BITMASK(ebx, 16, 0x0f);
  273. case GET_CHUNKS :
  274. return BITMASK(ebx, 8, 0x0f);
  275. case GET_STEPPING :
  276. return BITMASK(eax, 0, 0x0f);
  277. case GET_BLANDID :
  278. return BITMASK(ebx, 0, 0xff);
  279. case GET_NUMSHARE :
  280. if (have_cpuid() < 4) return 0;
  281. cpuid(4, &eax, &ebx, &ecx, &edx);
  282. return BITMASK(eax, 14, 0xfff);
  283. case GET_NUMCORES :
  284. if (have_cpuid() < 4) return 0;
  285. cpuid(4, &eax, &ebx, &ecx, &edx);
  286. return BITMASK(eax, 26, 0x3f);
  287. case GET_FEATURE :
  288. if ((edx & (1 << 3)) != 0) feature |= HAVE_PSE;
  289. if ((edx & (1 << 15)) != 0) feature |= HAVE_CMOV;
  290. if ((edx & (1 << 19)) != 0) feature |= HAVE_CFLUSH;
  291. if ((edx & (1 << 23)) != 0) feature |= HAVE_MMX;
  292. if ((edx & (1 << 25)) != 0) feature |= HAVE_SSE;
  293. if ((edx & (1 << 26)) != 0) feature |= HAVE_SSE2;
  294. if ((edx & (1 << 27)) != 0) {
  295. if (BITMASK(ebx, 16, 0x0f) > 0) feature |= HAVE_HIT;
  296. }
  297. if ((ecx & (1 << 0)) != 0) feature |= HAVE_SSE3;
  298. if ((ecx & (1 << 9)) != 0) feature |= HAVE_SSSE3;
  299. if ((ecx & (1 << 19)) != 0) feature |= HAVE_SSE4_1;
  300. if ((ecx & (1 << 20)) != 0) feature |= HAVE_SSE4_2;
  301. #ifndef NO_AVX
  302. if (support_avx()) feature |= HAVE_AVX;
  303. if (support_avx2()) feature |= HAVE_AVX2;
  304. if (support_avx512()) feature |= HAVE_AVX512VL;
  305. if (support_avx512_bf16()) feature |= HAVE_AVX512BF16;
  306. if ((ecx & (1 << 12)) != 0) feature |= HAVE_FMA3;
  307. #endif
  308. if (have_excpuid() >= 0x01) {
  309. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  310. if ((ecx & (1 << 6)) != 0) feature |= HAVE_SSE4A;
  311. if ((ecx & (1 << 7)) != 0) feature |= HAVE_MISALIGNSSE;
  312. #ifndef NO_AVX
  313. if ((ecx & (1 << 16)) != 0) feature |= HAVE_FMA4;
  314. #endif
  315. if ((edx & (1 << 30)) != 0) feature |= HAVE_3DNOWEX;
  316. if ((edx & (1 << 31)) != 0) feature |= HAVE_3DNOW;
  317. }
  318. if (have_excpuid() >= 0x1a) {
  319. cpuid(0x8000001a, &eax, &ebx, &ecx, &edx);
  320. if ((eax & (1 << 0)) != 0) feature |= HAVE_128BITFPU;
  321. if ((eax & (1 << 1)) != 0) feature |= HAVE_FASTMOVU;
  322. }
  323. }
  324. return feature;
  325. }
  326. int get_cacheinfo(int type, cache_info_t *cacheinfo){
  327. int eax, ebx, ecx, edx, cpuid_level;
  328. int info[15];
  329. int i;
  330. cache_info_t LC1, LD1, L2, L3,
  331. ITB, DTB, LITB, LDTB,
  332. L2ITB, L2DTB, L2LITB, L2LDTB;
  333. LC1.size = 0; LC1.associative = 0; LC1.linesize = 0; LC1.shared = 0;
  334. LD1.size = 0; LD1.associative = 0; LD1.linesize = 0; LD1.shared = 0;
  335. L2.size = 0; L2.associative = 0; L2.linesize = 0; L2.shared = 0;
  336. L3.size = 0; L3.associative = 0; L3.linesize = 0; L3.shared = 0;
  337. ITB.size = 0; ITB.associative = 0; ITB.linesize = 0; ITB.shared = 0;
  338. DTB.size = 0; DTB.associative = 0; DTB.linesize = 0; DTB.shared = 0;
  339. LITB.size = 0; LITB.associative = 0; LITB.linesize = 0; LITB.shared = 0;
  340. LDTB.size = 0; LDTB.associative = 0; LDTB.linesize = 0; LDTB.shared = 0;
  341. L2ITB.size = 0; L2ITB.associative = 0; L2ITB.linesize = 0; L2ITB.shared = 0;
  342. L2DTB.size = 0; L2DTB.associative = 0; L2DTB.linesize = 0; L2DTB.shared = 0;
  343. L2LITB.size = 0; L2LITB.associative = 0; L2LITB.linesize = 0; L2LITB.shared = 0;
  344. L2LDTB.size = 0; L2LDTB.associative = 0; L2LDTB.linesize = 0; L2LDTB.shared = 0;
  345. cpuid(0, &cpuid_level, &ebx, &ecx, &edx);
  346. if (cpuid_level > 1) {
  347. int numcalls =0 ;
  348. cpuid(2, &eax, &ebx, &ecx, &edx);
  349. numcalls = BITMASK(eax, 0, 0xff); //FIXME some systems may require repeated calls to read all entries
  350. info[ 0] = BITMASK(eax, 8, 0xff);
  351. info[ 1] = BITMASK(eax, 16, 0xff);
  352. info[ 2] = BITMASK(eax, 24, 0xff);
  353. info[ 3] = BITMASK(ebx, 0, 0xff);
  354. info[ 4] = BITMASK(ebx, 8, 0xff);
  355. info[ 5] = BITMASK(ebx, 16, 0xff);
  356. info[ 6] = BITMASK(ebx, 24, 0xff);
  357. info[ 7] = BITMASK(ecx, 0, 0xff);
  358. info[ 8] = BITMASK(ecx, 8, 0xff);
  359. info[ 9] = BITMASK(ecx, 16, 0xff);
  360. info[10] = BITMASK(ecx, 24, 0xff);
  361. info[11] = BITMASK(edx, 0, 0xff);
  362. info[12] = BITMASK(edx, 8, 0xff);
  363. info[13] = BITMASK(edx, 16, 0xff);
  364. info[14] = BITMASK(edx, 24, 0xff);
  365. for (i = 0; i < 15; i++){
  366. switch (info[i]){
  367. /* This table is from http://www.sandpile.org/ia32/cpuid.htm */
  368. case 0x01 :
  369. ITB.size = 4;
  370. ITB.associative = 4;
  371. ITB.linesize = 32;
  372. break;
  373. case 0x02 :
  374. LITB.size = 4096;
  375. LITB.associative = 0;
  376. LITB.linesize = 2;
  377. break;
  378. case 0x03 :
  379. DTB.size = 4;
  380. DTB.associative = 4;
  381. DTB.linesize = 64;
  382. break;
  383. case 0x04 :
  384. LDTB.size = 4096;
  385. LDTB.associative = 4;
  386. LDTB.linesize = 8;
  387. break;
  388. case 0x05 :
  389. LDTB.size = 4096;
  390. LDTB.associative = 4;
  391. LDTB.linesize = 32;
  392. break;
  393. case 0x06 :
  394. LC1.size = 8;
  395. LC1.associative = 4;
  396. LC1.linesize = 32;
  397. break;
  398. case 0x08 :
  399. LC1.size = 16;
  400. LC1.associative = 4;
  401. LC1.linesize = 32;
  402. break;
  403. case 0x09 :
  404. LC1.size = 32;
  405. LC1.associative = 4;
  406. LC1.linesize = 64;
  407. break;
  408. case 0x0a :
  409. LD1.size = 8;
  410. LD1.associative = 2;
  411. LD1.linesize = 32;
  412. break;
  413. case 0x0c :
  414. LD1.size = 16;
  415. LD1.associative = 4;
  416. LD1.linesize = 32;
  417. break;
  418. case 0x0d :
  419. LD1.size = 16;
  420. LD1.associative = 4;
  421. LD1.linesize = 64;
  422. break;
  423. case 0x0e :
  424. LD1.size = 24;
  425. LD1.associative = 6;
  426. LD1.linesize = 64;
  427. break;
  428. case 0x10 :
  429. LD1.size = 16;
  430. LD1.associative = 4;
  431. LD1.linesize = 32;
  432. break;
  433. case 0x15 :
  434. LC1.size = 16;
  435. LC1.associative = 4;
  436. LC1.linesize = 32;
  437. break;
  438. case 0x1a :
  439. L2.size = 96;
  440. L2.associative = 6;
  441. L2.linesize = 64;
  442. break;
  443. case 0x21 :
  444. L2.size = 256;
  445. L2.associative = 8;
  446. L2.linesize = 64;
  447. break;
  448. case 0x22 :
  449. L3.size = 512;
  450. L3.associative = 4;
  451. L3.linesize = 64;
  452. break;
  453. case 0x23 :
  454. L3.size = 1024;
  455. L3.associative = 8;
  456. L3.linesize = 64;
  457. break;
  458. case 0x25 :
  459. L3.size = 2048;
  460. L3.associative = 8;
  461. L3.linesize = 64;
  462. break;
  463. case 0x29 :
  464. L3.size = 4096;
  465. L3.associative = 8;
  466. L3.linesize = 64;
  467. break;
  468. case 0x2c :
  469. LD1.size = 32;
  470. LD1.associative = 8;
  471. LD1.linesize = 64;
  472. break;
  473. case 0x30 :
  474. LC1.size = 32;
  475. LC1.associative = 8;
  476. LC1.linesize = 64;
  477. break;
  478. case 0x39 :
  479. L2.size = 128;
  480. L2.associative = 4;
  481. L2.linesize = 64;
  482. break;
  483. case 0x3a :
  484. L2.size = 192;
  485. L2.associative = 6;
  486. L2.linesize = 64;
  487. break;
  488. case 0x3b :
  489. L2.size = 128;
  490. L2.associative = 2;
  491. L2.linesize = 64;
  492. break;
  493. case 0x3c :
  494. L2.size = 256;
  495. L2.associative = 4;
  496. L2.linesize = 64;
  497. break;
  498. case 0x3d :
  499. L2.size = 384;
  500. L2.associative = 6;
  501. L2.linesize = 64;
  502. break;
  503. case 0x3e :
  504. L2.size = 512;
  505. L2.associative = 4;
  506. L2.linesize = 64;
  507. break;
  508. case 0x41 :
  509. L2.size = 128;
  510. L2.associative = 4;
  511. L2.linesize = 32;
  512. break;
  513. case 0x42 :
  514. L2.size = 256;
  515. L2.associative = 4;
  516. L2.linesize = 32;
  517. break;
  518. case 0x43 :
  519. L2.size = 512;
  520. L2.associative = 4;
  521. L2.linesize = 32;
  522. break;
  523. case 0x44 :
  524. L2.size = 1024;
  525. L2.associative = 4;
  526. L2.linesize = 32;
  527. break;
  528. case 0x45 :
  529. L2.size = 2048;
  530. L2.associative = 4;
  531. L2.linesize = 32;
  532. break;
  533. case 0x46 :
  534. L3.size = 4096;
  535. L3.associative = 4;
  536. L3.linesize = 64;
  537. break;
  538. case 0x47 :
  539. L3.size = 8192;
  540. L3.associative = 8;
  541. L3.linesize = 64;
  542. break;
  543. case 0x48 :
  544. L2.size = 3184;
  545. L2.associative = 12;
  546. L2.linesize = 64;
  547. break;
  548. case 0x49 :
  549. if ((get_cputype(GET_FAMILY) == 0x0f) && (get_cputype(GET_MODEL) == 0x06)) {
  550. L3.size = 4096;
  551. L3.associative = 16;
  552. L3.linesize = 64;
  553. } else {
  554. L2.size = 4096;
  555. L2.associative = 16;
  556. L2.linesize = 64;
  557. }
  558. break;
  559. case 0x4a :
  560. L3.size = 6144;
  561. L3.associative = 12;
  562. L3.linesize = 64;
  563. break;
  564. case 0x4b :
  565. L3.size = 8192;
  566. L3.associative = 16;
  567. L3.linesize = 64;
  568. break;
  569. case 0x4c :
  570. L3.size = 12280;
  571. L3.associative = 12;
  572. L3.linesize = 64;
  573. break;
  574. case 0x4d :
  575. L3.size = 16384;
  576. L3.associative = 16;
  577. L3.linesize = 64;
  578. break;
  579. case 0x4e :
  580. L2.size = 6144;
  581. L2.associative = 24;
  582. L2.linesize = 64;
  583. break;
  584. case 0x4f :
  585. ITB.size = 4;
  586. ITB.associative = 0;
  587. ITB.linesize = 32;
  588. break;
  589. case 0x50 :
  590. ITB.size = 4;
  591. ITB.associative = 0;
  592. ITB.linesize = 64;
  593. LITB.size = 4096;
  594. LITB.associative = 0;
  595. LITB.linesize = 64;
  596. LITB.shared = 1;
  597. break;
  598. case 0x51 :
  599. ITB.size = 4;
  600. ITB.associative = 0;
  601. ITB.linesize = 128;
  602. LITB.size = 4096;
  603. LITB.associative = 0;
  604. LITB.linesize = 128;
  605. LITB.shared = 1;
  606. break;
  607. case 0x52 :
  608. ITB.size = 4;
  609. ITB.associative = 0;
  610. ITB.linesize = 256;
  611. LITB.size = 4096;
  612. LITB.associative = 0;
  613. LITB.linesize = 256;
  614. LITB.shared = 1;
  615. break;
  616. case 0x55 :
  617. LITB.size = 4096;
  618. LITB.associative = 0;
  619. LITB.linesize = 7;
  620. LITB.shared = 1;
  621. break;
  622. case 0x56 :
  623. LDTB.size = 4096;
  624. LDTB.associative = 4;
  625. LDTB.linesize = 16;
  626. break;
  627. case 0x57 :
  628. LDTB.size = 4096;
  629. LDTB.associative = 4;
  630. LDTB.linesize = 16;
  631. break;
  632. case 0x5b :
  633. DTB.size = 4;
  634. DTB.associative = 0;
  635. DTB.linesize = 64;
  636. LDTB.size = 4096;
  637. LDTB.associative = 0;
  638. LDTB.linesize = 64;
  639. LDTB.shared = 1;
  640. break;
  641. case 0x5c :
  642. DTB.size = 4;
  643. DTB.associative = 0;
  644. DTB.linesize = 128;
  645. LDTB.size = 4096;
  646. LDTB.associative = 0;
  647. LDTB.linesize = 128;
  648. LDTB.shared = 1;
  649. break;
  650. case 0x5d :
  651. DTB.size = 4;
  652. DTB.associative = 0;
  653. DTB.linesize = 256;
  654. LDTB.size = 4096;
  655. LDTB.associative = 0;
  656. LDTB.linesize = 256;
  657. LDTB.shared = 1;
  658. break;
  659. case 0x60 :
  660. LD1.size = 16;
  661. LD1.associative = 8;
  662. LD1.linesize = 64;
  663. break;
  664. case 0x63 :
  665. DTB.size = 2048;
  666. DTB.associative = 4;
  667. DTB.linesize = 32;
  668. LDTB.size = 4096;
  669. LDTB.associative= 4;
  670. LDTB.linesize = 32;
  671. break;
  672. case 0x66 :
  673. LD1.size = 8;
  674. LD1.associative = 4;
  675. LD1.linesize = 64;
  676. break;
  677. case 0x67 :
  678. LD1.size = 16;
  679. LD1.associative = 4;
  680. LD1.linesize = 64;
  681. break;
  682. case 0x68 :
  683. LD1.size = 32;
  684. LD1.associative = 4;
  685. LD1.linesize = 64;
  686. break;
  687. case 0x70 :
  688. LC1.size = 12;
  689. LC1.associative = 8;
  690. break;
  691. case 0x71 :
  692. LC1.size = 16;
  693. LC1.associative = 8;
  694. break;
  695. case 0x72 :
  696. LC1.size = 32;
  697. LC1.associative = 8;
  698. break;
  699. case 0x73 :
  700. LC1.size = 64;
  701. LC1.associative = 8;
  702. break;
  703. case 0x76 :
  704. ITB.size = 2048;
  705. ITB.associative = 0;
  706. ITB.linesize = 8;
  707. LITB.size = 4096;
  708. LITB.associative= 0;
  709. LITB.linesize = 8;
  710. break;
  711. case 0x77 :
  712. LC1.size = 16;
  713. LC1.associative = 4;
  714. LC1.linesize = 64;
  715. break;
  716. case 0x78 :
  717. L2.size = 1024;
  718. L2.associative = 4;
  719. L2.linesize = 64;
  720. break;
  721. case 0x79 :
  722. L2.size = 128;
  723. L2.associative = 8;
  724. L2.linesize = 64;
  725. break;
  726. case 0x7a :
  727. L2.size = 256;
  728. L2.associative = 8;
  729. L2.linesize = 64;
  730. break;
  731. case 0x7b :
  732. L2.size = 512;
  733. L2.associative = 8;
  734. L2.linesize = 64;
  735. break;
  736. case 0x7c :
  737. L2.size = 1024;
  738. L2.associative = 8;
  739. L2.linesize = 64;
  740. break;
  741. case 0x7d :
  742. L2.size = 2048;
  743. L2.associative = 8;
  744. L2.linesize = 64;
  745. break;
  746. case 0x7e :
  747. L2.size = 256;
  748. L2.associative = 8;
  749. L2.linesize = 128;
  750. break;
  751. case 0x7f :
  752. L2.size = 512;
  753. L2.associative = 2;
  754. L2.linesize = 64;
  755. break;
  756. case 0x81 :
  757. L2.size = 128;
  758. L2.associative = 8;
  759. L2.linesize = 32;
  760. break;
  761. case 0x82 :
  762. L2.size = 256;
  763. L2.associative = 8;
  764. L2.linesize = 32;
  765. break;
  766. case 0x83 :
  767. L2.size = 512;
  768. L2.associative = 8;
  769. L2.linesize = 32;
  770. break;
  771. case 0x84 :
  772. L2.size = 1024;
  773. L2.associative = 8;
  774. L2.linesize = 32;
  775. break;
  776. case 0x85 :
  777. L2.size = 2048;
  778. L2.associative = 8;
  779. L2.linesize = 32;
  780. break;
  781. case 0x86 :
  782. L2.size = 512;
  783. L2.associative = 4;
  784. L2.linesize = 64;
  785. break;
  786. case 0x87 :
  787. L2.size = 1024;
  788. L2.associative = 8;
  789. L2.linesize = 64;
  790. break;
  791. case 0x88 :
  792. L3.size = 2048;
  793. L3.associative = 4;
  794. L3.linesize = 64;
  795. break;
  796. case 0x89 :
  797. L3.size = 4096;
  798. L3.associative = 4;
  799. L3.linesize = 64;
  800. break;
  801. case 0x8a :
  802. L3.size = 8192;
  803. L3.associative = 4;
  804. L3.linesize = 64;
  805. break;
  806. case 0x8d :
  807. L3.size = 3096;
  808. L3.associative = 12;
  809. L3.linesize = 128;
  810. break;
  811. case 0x90 :
  812. ITB.size = 4;
  813. ITB.associative = 0;
  814. ITB.linesize = 64;
  815. break;
  816. case 0x96 :
  817. DTB.size = 4;
  818. DTB.associative = 0;
  819. DTB.linesize = 32;
  820. break;
  821. case 0x9b :
  822. L2DTB.size = 4;
  823. L2DTB.associative = 0;
  824. L2DTB.linesize = 96;
  825. break;
  826. case 0xb0 :
  827. ITB.size = 4;
  828. ITB.associative = 4;
  829. ITB.linesize = 128;
  830. break;
  831. case 0xb1 :
  832. LITB.size = 4096;
  833. LITB.associative = 4;
  834. LITB.linesize = 4;
  835. break;
  836. case 0xb2 :
  837. ITB.size = 4;
  838. ITB.associative = 4;
  839. ITB.linesize = 64;
  840. break;
  841. case 0xb3 :
  842. DTB.size = 4;
  843. DTB.associative = 4;
  844. DTB.linesize = 128;
  845. break;
  846. case 0xb4 :
  847. DTB.size = 4;
  848. DTB.associative = 4;
  849. DTB.linesize = 256;
  850. break;
  851. case 0xba :
  852. DTB.size = 4;
  853. DTB.associative = 4;
  854. DTB.linesize = 64;
  855. break;
  856. case 0xd0 :
  857. L3.size = 512;
  858. L3.associative = 4;
  859. L3.linesize = 64;
  860. break;
  861. case 0xd1 :
  862. L3.size = 1024;
  863. L3.associative = 4;
  864. L3.linesize = 64;
  865. break;
  866. case 0xd2 :
  867. L3.size = 2048;
  868. L3.associative = 4;
  869. L3.linesize = 64;
  870. break;
  871. case 0xd6 :
  872. L3.size = 1024;
  873. L3.associative = 8;
  874. L3.linesize = 64;
  875. break;
  876. case 0xd7 :
  877. L3.size = 2048;
  878. L3.associative = 8;
  879. L3.linesize = 64;
  880. break;
  881. case 0xd8 :
  882. L3.size = 4096;
  883. L3.associative = 8;
  884. L3.linesize = 64;
  885. break;
  886. case 0xdc :
  887. L3.size = 2048;
  888. L3.associative = 12;
  889. L3.linesize = 64;
  890. break;
  891. case 0xdd :
  892. L3.size = 4096;
  893. L3.associative = 12;
  894. L3.linesize = 64;
  895. break;
  896. case 0xde :
  897. L3.size = 8192;
  898. L3.associative = 12;
  899. L3.linesize = 64;
  900. break;
  901. case 0xe2 :
  902. L3.size = 2048;
  903. L3.associative = 16;
  904. L3.linesize = 64;
  905. break;
  906. case 0xe3 :
  907. L3.size = 4096;
  908. L3.associative = 16;
  909. L3.linesize = 64;
  910. break;
  911. case 0xe4 :
  912. L3.size = 8192;
  913. L3.associative = 16;
  914. L3.linesize = 64;
  915. break;
  916. }
  917. }
  918. }
  919. if (get_vendor() == VENDOR_INTEL) {
  920. if(LD1.size<=0 || LC1.size<=0){
  921. //If we didn't detect L1 correctly before,
  922. int count;
  923. for (count=0;count <4;count++) {
  924. cpuid_count(4, count, &eax, &ebx, &ecx, &edx);
  925. switch (eax &0x1f) {
  926. case 0:
  927. continue;
  928. case 1:
  929. case 3:
  930. {
  931. switch ((eax >>5) &0x07)
  932. {
  933. case 1:
  934. {
  935. // fprintf(stderr,"L1 data cache...\n");
  936. int sets = ecx+1;
  937. int lines = (ebx & 0x0fff) +1;
  938. ebx>>=12;
  939. int part = (ebx&0x03ff)+1;
  940. ebx >>=10;
  941. int assoc = (ebx&0x03ff)+1;
  942. LD1.size = (assoc*part*lines*sets)/1024;
  943. LD1.associative = assoc;
  944. LD1.linesize= lines;
  945. break;
  946. }
  947. default:
  948. break;
  949. }
  950. break;
  951. }
  952. case 2:
  953. {
  954. switch ((eax >>5) &0x07)
  955. {
  956. case 1:
  957. {
  958. // fprintf(stderr,"L1 instruction cache...\n");
  959. int sets = ecx+1;
  960. int lines = (ebx & 0x0fff) +1;
  961. ebx>>=12;
  962. int part = (ebx&0x03ff)+1;
  963. ebx >>=10;
  964. int assoc = (ebx&0x03ff)+1;
  965. LC1.size = (assoc*part*lines*sets)/1024;
  966. LC1.associative = assoc;
  967. LC1.linesize= lines;
  968. break;
  969. }
  970. default:
  971. break;
  972. }
  973. break;
  974. }
  975. default:
  976. break;
  977. }
  978. }
  979. }
  980. cpuid(0x80000000, &cpuid_level, &ebx, &ecx, &edx);
  981. if (cpuid_level >= 0x80000006) {
  982. if(L2.size<=0){
  983. //If we didn't detect L2 correctly before,
  984. cpuid(0x80000006, &eax, &ebx, &ecx, &edx);
  985. L2.size = BITMASK(ecx, 16, 0xffff);
  986. L2.associative = BITMASK(ecx, 12, 0x0f);
  987. switch (L2.associative){
  988. case 0x06:
  989. L2.associative = 8;
  990. break;
  991. case 0x08:
  992. L2.associative = 16;
  993. break;
  994. }
  995. L2.linesize = BITMASK(ecx, 0, 0xff);
  996. }
  997. }
  998. }
  999. if ((get_vendor() == VENDOR_AMD) ||
  1000. (get_vendor() == VENDOR_HYGON) ||
  1001. (get_vendor() == VENDOR_CENTAUR)) {
  1002. cpuid(0x80000005, &eax, &ebx, &ecx, &edx);
  1003. LDTB.size = 4096;
  1004. LDTB.associative = BITMASK(eax, 24, 0xff);
  1005. if (LDTB.associative == 0xff) LDTB.associative = 0;
  1006. LDTB.linesize = BITMASK(eax, 16, 0xff);
  1007. LITB.size = 4096;
  1008. LITB.associative = BITMASK(eax, 8, 0xff);
  1009. if (LITB.associative == 0xff) LITB.associative = 0;
  1010. LITB.linesize = BITMASK(eax, 0, 0xff);
  1011. DTB.size = 4;
  1012. DTB.associative = BITMASK(ebx, 24, 0xff);
  1013. if (DTB.associative == 0xff) DTB.associative = 0;
  1014. DTB.linesize = BITMASK(ebx, 16, 0xff);
  1015. ITB.size = 4;
  1016. ITB.associative = BITMASK(ebx, 8, 0xff);
  1017. if (ITB.associative == 0xff) ITB.associative = 0;
  1018. ITB.linesize = BITMASK(ebx, 0, 0xff);
  1019. LD1.size = BITMASK(ecx, 24, 0xff);
  1020. LD1.associative = BITMASK(ecx, 16, 0xff);
  1021. if (LD1.associative == 0xff) LD1.associative = 0;
  1022. LD1.linesize = BITMASK(ecx, 0, 0xff);
  1023. LC1.size = BITMASK(ecx, 24, 0xff);
  1024. LC1.associative = BITMASK(ecx, 16, 0xff);
  1025. if (LC1.associative == 0xff) LC1.associative = 0;
  1026. LC1.linesize = BITMASK(ecx, 0, 0xff);
  1027. cpuid(0x80000006, &eax, &ebx, &ecx, &edx);
  1028. L2LDTB.size = 4096;
  1029. L2LDTB.associative = BITMASK(eax, 24, 0xff);
  1030. if (L2LDTB.associative == 0xff) L2LDTB.associative = 0;
  1031. L2LDTB.linesize = BITMASK(eax, 16, 0xff);
  1032. L2LITB.size = 4096;
  1033. L2LITB.associative = BITMASK(eax, 8, 0xff);
  1034. if (L2LITB.associative == 0xff) L2LITB.associative = 0;
  1035. L2LITB.linesize = BITMASK(eax, 0, 0xff);
  1036. L2DTB.size = 4;
  1037. L2DTB.associative = BITMASK(ebx, 24, 0xff);
  1038. if (L2DTB.associative == 0xff) L2DTB.associative = 0;
  1039. L2DTB.linesize = BITMASK(ebx, 16, 0xff);
  1040. L2ITB.size = 4;
  1041. L2ITB.associative = BITMASK(ebx, 8, 0xff);
  1042. if (L2ITB.associative == 0xff) L2ITB.associative = 0;
  1043. L2ITB.linesize = BITMASK(ebx, 0, 0xff);
  1044. if(L2.size <= 0){
  1045. //If we didn't detect L2 correctly before,
  1046. L2.size = BITMASK(ecx, 16, 0xffff);
  1047. L2.associative = BITMASK(ecx, 12, 0xf);
  1048. switch (L2.associative){
  1049. case 0x06:
  1050. L2.associative = 8;
  1051. break;
  1052. case 0x08:
  1053. L2.associative = 16;
  1054. break;
  1055. }
  1056. if (L2.associative == 0xff) L2.associative = 0;
  1057. L2.linesize = BITMASK(ecx, 0, 0xff);
  1058. }
  1059. L3.size = BITMASK(edx, 18, 0x3fff) * 512;
  1060. L3.associative = BITMASK(edx, 12, 0xf);
  1061. if (L3.associative == 0xff) L2.associative = 0;
  1062. L3.linesize = BITMASK(edx, 0, 0xff);
  1063. }
  1064. switch (type) {
  1065. case CACHE_INFO_L1_I :
  1066. *cacheinfo = LC1;
  1067. break;
  1068. case CACHE_INFO_L1_D :
  1069. *cacheinfo = LD1;
  1070. break;
  1071. case CACHE_INFO_L2 :
  1072. *cacheinfo = L2;
  1073. break;
  1074. case CACHE_INFO_L3 :
  1075. *cacheinfo = L3;
  1076. break;
  1077. case CACHE_INFO_L1_DTB :
  1078. *cacheinfo = DTB;
  1079. break;
  1080. case CACHE_INFO_L1_ITB :
  1081. *cacheinfo = ITB;
  1082. break;
  1083. case CACHE_INFO_L1_LDTB :
  1084. *cacheinfo = LDTB;
  1085. break;
  1086. case CACHE_INFO_L1_LITB :
  1087. *cacheinfo = LITB;
  1088. break;
  1089. case CACHE_INFO_L2_DTB :
  1090. *cacheinfo = L2DTB;
  1091. break;
  1092. case CACHE_INFO_L2_ITB :
  1093. *cacheinfo = L2ITB;
  1094. break;
  1095. case CACHE_INFO_L2_LDTB :
  1096. *cacheinfo = L2LDTB;
  1097. break;
  1098. case CACHE_INFO_L2_LITB :
  1099. *cacheinfo = L2LITB;
  1100. break;
  1101. }
  1102. return 0;
  1103. }
  1104. int get_cpuname(void){
  1105. int family, exfamily, model, vendor, exmodel;
  1106. if (!have_cpuid()) return CPUTYPE_80386;
  1107. family = get_cputype(GET_FAMILY);
  1108. exfamily = get_cputype(GET_EXFAMILY);
  1109. model = get_cputype(GET_MODEL);
  1110. exmodel = get_cputype(GET_EXMODEL);
  1111. vendor = get_vendor();
  1112. if (vendor == VENDOR_INTEL){
  1113. switch (family) {
  1114. case 0x4:
  1115. return CPUTYPE_80486;
  1116. case 0x5:
  1117. return CPUTYPE_PENTIUM;
  1118. case 0x6:
  1119. switch (exmodel) {
  1120. case 0:
  1121. switch (model) {
  1122. case 1:
  1123. case 3:
  1124. case 5:
  1125. case 6:
  1126. #if defined(__x86_64__) || defined(__amd64__)
  1127. return CPUTYPE_CORE2;
  1128. #else
  1129. return CPUTYPE_PENTIUM2;
  1130. #endif
  1131. case 7:
  1132. case 8:
  1133. case 10:
  1134. case 11:
  1135. return CPUTYPE_PENTIUM3;
  1136. case 9:
  1137. case 13:
  1138. case 14:
  1139. return CPUTYPE_PENTIUMM;
  1140. case 15:
  1141. return CPUTYPE_CORE2;
  1142. }
  1143. break;
  1144. case 1: // family 6 exmodel 1
  1145. switch (model) {
  1146. case 6:
  1147. return CPUTYPE_CORE2;
  1148. case 7:
  1149. return CPUTYPE_PENRYN;
  1150. case 10:
  1151. case 11:
  1152. case 14:
  1153. case 15:
  1154. return CPUTYPE_NEHALEM;
  1155. case 12:
  1156. return CPUTYPE_ATOM;
  1157. case 13:
  1158. return CPUTYPE_DUNNINGTON;
  1159. }
  1160. break;
  1161. case 2: // family 6 exmodel 2
  1162. switch (model) {
  1163. case 5:
  1164. //Intel Core (Clarkdale) / Core (Arrandale)
  1165. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  1166. // Xeon (Clarkdale), 32nm
  1167. return CPUTYPE_NEHALEM;
  1168. case 10:
  1169. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  1170. if(support_avx())
  1171. return CPUTYPE_SANDYBRIDGE;
  1172. else
  1173. return CPUTYPE_NEHALEM; //OS doesn't support AVX
  1174. case 12:
  1175. //Xeon Processor 5600 (Westmere-EP)
  1176. return CPUTYPE_NEHALEM;
  1177. case 13:
  1178. //Intel Core i7-3000 / Xeon E5 (Sandy Bridge)
  1179. if(support_avx())
  1180. return CPUTYPE_SANDYBRIDGE;
  1181. else
  1182. return CPUTYPE_NEHALEM;
  1183. case 14:
  1184. // Xeon E7540
  1185. case 15:
  1186. //Xeon Processor E7 (Westmere-EX)
  1187. return CPUTYPE_NEHALEM;
  1188. }
  1189. break;
  1190. case 3: // family 6 exmodel 3
  1191. switch (model) {
  1192. case 7:
  1193. // Bay Trail
  1194. return CPUTYPE_ATOM;
  1195. case 10:
  1196. case 14:
  1197. // Ivy Bridge
  1198. if(support_avx())
  1199. return CPUTYPE_SANDYBRIDGE;
  1200. else
  1201. return CPUTYPE_NEHALEM;
  1202. case 12:
  1203. case 15:
  1204. if(support_avx2())
  1205. return CPUTYPE_HASWELL;
  1206. if(support_avx())
  1207. return CPUTYPE_SANDYBRIDGE;
  1208. else
  1209. return CPUTYPE_NEHALEM;
  1210. case 13:
  1211. //Broadwell
  1212. if(support_avx2())
  1213. return CPUTYPE_HASWELL;
  1214. if(support_avx())
  1215. return CPUTYPE_SANDYBRIDGE;
  1216. else
  1217. return CPUTYPE_NEHALEM;
  1218. }
  1219. break;
  1220. case 4: // family 6 exmodel 4
  1221. switch (model) {
  1222. case 5:
  1223. case 6:
  1224. if(support_avx2())
  1225. return CPUTYPE_HASWELL;
  1226. if(support_avx())
  1227. return CPUTYPE_SANDYBRIDGE;
  1228. else
  1229. return CPUTYPE_NEHALEM;
  1230. case 7:
  1231. case 15:
  1232. //Broadwell
  1233. if(support_avx2())
  1234. return CPUTYPE_HASWELL;
  1235. if(support_avx())
  1236. return CPUTYPE_SANDYBRIDGE;
  1237. else
  1238. return CPUTYPE_NEHALEM;
  1239. case 14:
  1240. //Skylake
  1241. if(support_avx2())
  1242. return CPUTYPE_HASWELL;
  1243. if(support_avx())
  1244. return CPUTYPE_SANDYBRIDGE;
  1245. else
  1246. return CPUTYPE_NEHALEM;
  1247. case 12:
  1248. // Braswell
  1249. case 13:
  1250. // Avoton
  1251. return CPUTYPE_NEHALEM;
  1252. }
  1253. break;
  1254. case 5: // family 6 exmodel 5
  1255. switch (model) {
  1256. case 6:
  1257. //Broadwell
  1258. if(support_avx2())
  1259. return CPUTYPE_HASWELL;
  1260. if(support_avx())
  1261. return CPUTYPE_SANDYBRIDGE;
  1262. else
  1263. return CPUTYPE_NEHALEM;
  1264. case 5:
  1265. // Skylake X
  1266. if(support_avx512_bf16())
  1267. return CPUTYPE_COOPERLAKE;
  1268. if(support_avx512())
  1269. return CPUTYPE_SKYLAKEX;
  1270. if(support_avx2())
  1271. return CPUTYPE_HASWELL;
  1272. if(support_avx())
  1273. return CPUTYPE_SANDYBRIDGE;
  1274. else
  1275. return CPUTYPE_NEHALEM;
  1276. case 14:
  1277. // Skylake
  1278. if(support_avx2())
  1279. return CPUTYPE_HASWELL;
  1280. if(support_avx())
  1281. return CPUTYPE_SANDYBRIDGE;
  1282. else
  1283. return CPUTYPE_NEHALEM;
  1284. case 7:
  1285. // Xeon Phi Knights Landing
  1286. if(support_avx2())
  1287. return CPUTYPE_HASWELL;
  1288. if(support_avx())
  1289. return CPUTYPE_SANDYBRIDGE;
  1290. else
  1291. return CPUTYPE_NEHALEM;
  1292. case 12:
  1293. // Apollo Lake
  1294. case 15:
  1295. // Denverton
  1296. return CPUTYPE_NEHALEM;
  1297. }
  1298. break;
  1299. case 6: // family 6 exmodel 6
  1300. switch (model) {
  1301. case 6: // Cannon Lake
  1302. if(support_avx512())
  1303. return CPUTYPE_SKYLAKEX;
  1304. if(support_avx2())
  1305. return CPUTYPE_HASWELL;
  1306. if(support_avx())
  1307. return CPUTYPE_SANDYBRIDGE;
  1308. else
  1309. return CPUTYPE_NEHALEM;
  1310. case 10: // Ice Lake SP
  1311. if(support_avx512_bf16())
  1312. return CPUTYPE_COOPERLAKE;
  1313. if(support_avx512())
  1314. return CPUTYPE_SKYLAKEX;
  1315. if(support_avx2())
  1316. return CPUTYPE_HASWELL;
  1317. if(support_avx())
  1318. return CPUTYPE_SANDYBRIDGE;
  1319. else
  1320. return CPUTYPE_NEHALEM;
  1321. }
  1322. break;
  1323. case 7: // family 6 exmodel 7
  1324. switch (model) {
  1325. case 10: // Goldmont Plus
  1326. return CPUTYPE_NEHALEM;
  1327. case 14: // Ice Lake
  1328. if(support_avx512())
  1329. return CPUTYPE_SKYLAKEX;
  1330. if(support_avx2())
  1331. return CPUTYPE_HASWELL;
  1332. if(support_avx())
  1333. return CPUTYPE_SANDYBRIDGE;
  1334. else
  1335. return CPUTYPE_NEHALEM;
  1336. }
  1337. break;
  1338. case 9:
  1339. case 8:
  1340. switch (model) {
  1341. case 12: // Tiger Lake
  1342. if(support_avx512())
  1343. return CPUTYPE_SKYLAKEX;
  1344. if(support_avx2())
  1345. return CPUTYPE_HASWELL;
  1346. if(support_avx())
  1347. return CPUTYPE_SANDYBRIDGE;
  1348. else
  1349. return CPUTYPE_NEHALEM;
  1350. case 14: // Kaby Lake and refreshes
  1351. if(support_avx2())
  1352. return CPUTYPE_HASWELL;
  1353. if(support_avx())
  1354. return CPUTYPE_SANDYBRIDGE;
  1355. else
  1356. return CPUTYPE_NEHALEM;
  1357. }
  1358. case 10: //family 6 exmodel 10
  1359. switch (model) {
  1360. case 5: // Comet Lake H and S
  1361. case 6: // Comet Lake U
  1362. if(support_avx2())
  1363. return CPUTYPE_HASWELL;
  1364. if(support_avx())
  1365. return CPUTYPE_SANDYBRIDGE;
  1366. else
  1367. return CPUTYPE_NEHALEM;
  1368. case 7: // Rocket Lake
  1369. if(support_avx512())
  1370. return CPUTYPE_SKYLAKEX;
  1371. if(support_avx2())
  1372. return CPUTYPE_HASWELL;
  1373. if(support_avx())
  1374. return CPUTYPE_SANDYBRIDGE;
  1375. else
  1376. return CPUTYPE_NEHALEM;
  1377. }
  1378. break;
  1379. }
  1380. break;
  1381. case 0x7:
  1382. return CPUTYPE_ITANIUM;
  1383. case 0xf:
  1384. switch (exfamily) {
  1385. case 0 :
  1386. return CPUTYPE_PENTIUM4;
  1387. case 1 :
  1388. return CPUTYPE_ITANIUM;
  1389. }
  1390. break;
  1391. }
  1392. return CPUTYPE_INTEL_UNKNOWN;
  1393. }
  1394. if (vendor == VENDOR_AMD){
  1395. switch (family) {
  1396. case 0x4:
  1397. return CPUTYPE_AMD5X86;
  1398. case 0x5:
  1399. return CPUTYPE_AMDK6;
  1400. case 0x6:
  1401. #if defined(__x86_64__) || defined(__amd64__)
  1402. return CPUTYPE_BARCELONA;
  1403. #else
  1404. return CPUTYPE_ATHLON;
  1405. #endif
  1406. case 0xf:
  1407. switch (exfamily) {
  1408. case 0:
  1409. case 2:
  1410. return CPUTYPE_OPTERON;
  1411. case 1:
  1412. case 3:
  1413. // case 7:
  1414. // case 10:
  1415. return CPUTYPE_BARCELONA;
  1416. case 5:
  1417. case 7:
  1418. return CPUTYPE_BOBCAT;
  1419. case 6:
  1420. switch (model) {
  1421. case 1:
  1422. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1423. if(support_avx())
  1424. return CPUTYPE_BULLDOZER;
  1425. else
  1426. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1427. case 2: //AMD Piledriver
  1428. case 3: //AMD Richland
  1429. if(support_avx())
  1430. return CPUTYPE_PILEDRIVER;
  1431. else
  1432. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1433. case 5: // New EXCAVATOR CPUS
  1434. if(support_avx())
  1435. return CPUTYPE_EXCAVATOR;
  1436. else
  1437. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1438. case 0:
  1439. case 8:
  1440. switch(exmodel){
  1441. case 1: //AMD Trinity
  1442. if(support_avx())
  1443. return CPUTYPE_PILEDRIVER;
  1444. else
  1445. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1446. case 3:
  1447. if(support_avx())
  1448. return CPUTYPE_STEAMROLLER;
  1449. else
  1450. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1451. case 6:
  1452. if(support_avx())
  1453. return CPUTYPE_EXCAVATOR;
  1454. else
  1455. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1456. }
  1457. break;
  1458. }
  1459. break;
  1460. case 8:
  1461. switch (model) {
  1462. case 1:
  1463. // AMD Ryzen
  1464. case 8:
  1465. // AMD Ryzen2
  1466. default:
  1467. // Matisse/Renoir and other recent Ryzen2
  1468. if(support_avx())
  1469. #ifndef NO_AVX2
  1470. return CPUTYPE_ZEN;
  1471. #else
  1472. return CPUTYPE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  1473. #endif
  1474. else
  1475. return CPUTYPE_BARCELONA;
  1476. }
  1477. break;
  1478. case 10: // Zen3
  1479. if(support_avx())
  1480. #ifndef NO_AVX2
  1481. return CPUTYPE_ZEN;
  1482. #else
  1483. return CPUTYPE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  1484. #endif
  1485. else
  1486. return CPUTYPE_BARCELONA;
  1487. }
  1488. break;
  1489. }
  1490. return CPUTYPE_AMD_UNKNOWN;
  1491. }
  1492. if (vendor == VENDOR_HYGON){
  1493. switch (family) {
  1494. case 0xf:
  1495. switch (exfamily) {
  1496. case 9:
  1497. //Hygon Dhyana
  1498. if(support_avx())
  1499. #ifndef NO_AVX2
  1500. return CPUTYPE_ZEN;
  1501. #else
  1502. return CPUTYPE_SANDYBRIDGE; // closer in architecture to Sandy Bridge than to Excavator
  1503. #endif
  1504. else
  1505. return CPUTYPE_BARCELONA;
  1506. }
  1507. break;
  1508. }
  1509. return CPUTYPE_HYGON_UNKNOWN;
  1510. }
  1511. if (vendor == VENDOR_CYRIX){
  1512. switch (family) {
  1513. case 0x4:
  1514. return CPUTYPE_CYRIX5X86;
  1515. case 0x5:
  1516. return CPUTYPE_CYRIXM1;
  1517. case 0x6:
  1518. return CPUTYPE_CYRIXM2;
  1519. }
  1520. return CPUTYPE_CYRIX_UNKNOWN;
  1521. }
  1522. if (vendor == VENDOR_NEXGEN){
  1523. switch (family) {
  1524. case 0x5:
  1525. return CPUTYPE_NEXGENNX586;
  1526. }
  1527. return CPUTYPE_NEXGEN_UNKNOWN;
  1528. }
  1529. if (vendor == VENDOR_CENTAUR){
  1530. switch (family) {
  1531. case 0x5:
  1532. return CPUTYPE_CENTAURC6;
  1533. break;
  1534. case 0x6:
  1535. return CPUTYPE_NANO;
  1536. break;
  1537. }
  1538. return CPUTYPE_VIAC3;
  1539. }
  1540. if (vendor == VENDOR_RISE){
  1541. switch (family) {
  1542. case 0x5:
  1543. return CPUTYPE_RISEMP6;
  1544. }
  1545. return CPUTYPE_RISE_UNKNOWN;
  1546. }
  1547. if (vendor == VENDOR_SIS){
  1548. switch (family) {
  1549. case 0x5:
  1550. return CPUTYPE_SYS55X;
  1551. }
  1552. return CPUTYPE_SIS_UNKNOWN;
  1553. }
  1554. if (vendor == VENDOR_TRANSMETA){
  1555. switch (family) {
  1556. case 0x5:
  1557. return CPUTYPE_CRUSOETM3X;
  1558. }
  1559. return CPUTYPE_TRANSMETA_UNKNOWN;
  1560. }
  1561. if (vendor == VENDOR_NSC){
  1562. switch (family) {
  1563. case 0x5:
  1564. return CPUTYPE_NSGEODE;
  1565. }
  1566. return CPUTYPE_NSC_UNKNOWN;
  1567. }
  1568. return CPUTYPE_UNKNOWN;
  1569. }
  1570. static char *cpuname[] = {
  1571. "UNKNOWN",
  1572. "INTEL_UNKNOWN",
  1573. "UMC_UNKNOWN",
  1574. "AMD_UNKNOWN",
  1575. "CYRIX_UNKNOWN",
  1576. "NEXGEN_UNKNOWN",
  1577. "CENTAUR_UNKNOWN",
  1578. "RISE_UNKNOWN",
  1579. "SIS_UNKNOWN",
  1580. "TRANSMETA_UNKNOWN",
  1581. "NSC_UNKNOWN",
  1582. "80386",
  1583. "80486",
  1584. "PENTIUM",
  1585. "PENTIUM2",
  1586. "PENTIUM3",
  1587. "PENTIUMM",
  1588. "PENTIUM4",
  1589. "CORE2",
  1590. "PENRYN",
  1591. "DUNNINGTON",
  1592. "NEHALEM",
  1593. "ATOM",
  1594. "ITANIUM",
  1595. "ITANIUM2",
  1596. "5X86",
  1597. "K6",
  1598. "ATHLON",
  1599. "DURON",
  1600. "OPTERON",
  1601. "BARCELONA",
  1602. "SHANGHAI",
  1603. "ISTANBUL",
  1604. "CYRIX5X86",
  1605. "CYRIXM1",
  1606. "CYRIXM2",
  1607. "NEXGENNX586",
  1608. "CENTAURC6",
  1609. "RISEMP6",
  1610. "SYS55X",
  1611. "TM3X00",
  1612. "NSGEODE",
  1613. "VIAC3",
  1614. "NANO",
  1615. "SANDYBRIDGE",
  1616. "BOBCAT",
  1617. "BULLDOZER",
  1618. "PILEDRIVER",
  1619. "HASWELL",
  1620. "STEAMROLLER",
  1621. "EXCAVATOR",
  1622. "ZEN",
  1623. "SKYLAKEX",
  1624. "DHYANA",
  1625. "COOPERLAKE"
  1626. };
  1627. static char *lowercpuname[] = {
  1628. "unknown",
  1629. "intel_unknown",
  1630. "umc_unknown",
  1631. "amd_unknown",
  1632. "cyrix_unknown",
  1633. "nexgen_unknown",
  1634. "centaur_unknown",
  1635. "rise_unknown",
  1636. "sis_unknown",
  1637. "transmeta_unknown",
  1638. "nsc_unknown",
  1639. "80386",
  1640. "80486",
  1641. "pentium",
  1642. "pentium2",
  1643. "pentium3",
  1644. "pentiumm",
  1645. "pentium4",
  1646. "core2",
  1647. "penryn",
  1648. "dunnington",
  1649. "nehalem",
  1650. "atom",
  1651. "itanium",
  1652. "itanium2",
  1653. "5x86",
  1654. "k6",
  1655. "athlon",
  1656. "duron",
  1657. "opteron",
  1658. "barcelona",
  1659. "shanghai",
  1660. "istanbul",
  1661. "cyrix5x86",
  1662. "cyrixm1",
  1663. "cyrixm2",
  1664. "nexgennx586",
  1665. "centaurc6",
  1666. "risemp6",
  1667. "sys55x",
  1668. "tms3x00",
  1669. "nsgeode",
  1670. "nano",
  1671. "sandybridge",
  1672. "bobcat",
  1673. "bulldozer",
  1674. "piledriver",
  1675. "haswell",
  1676. "steamroller",
  1677. "excavator",
  1678. "zen",
  1679. "skylakex",
  1680. "dhyana",
  1681. "cooperlake"
  1682. };
  1683. static char *corename[] = {
  1684. "UNKNOWN",
  1685. "80486",
  1686. "P5",
  1687. "P6",
  1688. "KATMAI",
  1689. "COPPERMINE",
  1690. "NORTHWOOD",
  1691. "PRESCOTT",
  1692. "BANIAS",
  1693. "ATHLON",
  1694. "OPTERON",
  1695. "BARCELONA",
  1696. "VIAC3",
  1697. "YONAH",
  1698. "CORE2",
  1699. "PENRYN",
  1700. "DUNNINGTON",
  1701. "NEHALEM",
  1702. "ATOM",
  1703. "NANO",
  1704. "SANDYBRIDGE",
  1705. "BOBCAT",
  1706. "BULLDOZER",
  1707. "PILEDRIVER",
  1708. "HASWELL",
  1709. "STEAMROLLER",
  1710. "EXCAVATOR",
  1711. "ZEN",
  1712. "SKYLAKEX",
  1713. "DHYANA",
  1714. "COOPERLAKE"
  1715. };
  1716. static char *corename_lower[] = {
  1717. "unknown",
  1718. "80486",
  1719. "p5",
  1720. "p6",
  1721. "katmai",
  1722. "coppermine",
  1723. "northwood",
  1724. "prescott",
  1725. "banias",
  1726. "athlon",
  1727. "opteron",
  1728. "barcelona",
  1729. "viac3",
  1730. "yonah",
  1731. "core2",
  1732. "penryn",
  1733. "dunnington",
  1734. "nehalem",
  1735. "atom",
  1736. "nano",
  1737. "sandybridge",
  1738. "bobcat",
  1739. "bulldozer",
  1740. "piledriver",
  1741. "haswell",
  1742. "steamroller",
  1743. "excavator",
  1744. "zen",
  1745. "skylakex",
  1746. "dhyana",
  1747. "cooperlake"
  1748. };
  1749. char *get_cpunamechar(void){
  1750. return cpuname[get_cpuname()];
  1751. }
  1752. char *get_lower_cpunamechar(void){
  1753. return lowercpuname[get_cpuname()];
  1754. }
  1755. int get_coretype(void){
  1756. int family, exfamily, model, exmodel, vendor;
  1757. if (!have_cpuid()) return CORE_80486;
  1758. family = get_cputype(GET_FAMILY);
  1759. exfamily = get_cputype(GET_EXFAMILY);
  1760. model = get_cputype(GET_MODEL);
  1761. exmodel = get_cputype(GET_EXMODEL);
  1762. vendor = get_vendor();
  1763. if (vendor == VENDOR_INTEL){
  1764. switch (family) {
  1765. case 4:
  1766. return CORE_80486;
  1767. case 5:
  1768. return CORE_P5;
  1769. case 6:
  1770. switch (exmodel) {
  1771. case 0:
  1772. switch (model) {
  1773. case 0:
  1774. case 1:
  1775. case 2:
  1776. case 3:
  1777. case 4:
  1778. case 5:
  1779. case 6:
  1780. #if defined(__x86_64__) || defined(__amd64__)
  1781. return CORE_CORE2;
  1782. #else
  1783. return CORE_P6;
  1784. #endif
  1785. case 7:
  1786. return CORE_KATMAI;
  1787. case 8:
  1788. case 10:
  1789. case 11:
  1790. return CORE_COPPERMINE;
  1791. case 9:
  1792. case 13:
  1793. case 14:
  1794. return CORE_BANIAS;
  1795. case 15:
  1796. return CORE_CORE2;
  1797. }
  1798. break;
  1799. case 1:
  1800. switch (model) {
  1801. case 6:
  1802. return CORE_CORE2;
  1803. case 7:
  1804. return CORE_PENRYN;
  1805. case 10:
  1806. case 11:
  1807. case 14:
  1808. case 15:
  1809. return CORE_NEHALEM;
  1810. case 12:
  1811. return CORE_ATOM;
  1812. case 13:
  1813. return CORE_DUNNINGTON;
  1814. }
  1815. break;
  1816. case 2:
  1817. switch (model) {
  1818. case 5:
  1819. //Intel Core (Clarkdale) / Core (Arrandale)
  1820. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  1821. // Xeon (Clarkdale), 32nm
  1822. return CORE_NEHALEM;
  1823. case 10:
  1824. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  1825. if(support_avx())
  1826. return CORE_SANDYBRIDGE;
  1827. else
  1828. return CORE_NEHALEM; //OS doesn't support AVX
  1829. case 12:
  1830. //Xeon Processor 5600 (Westmere-EP)
  1831. return CORE_NEHALEM;
  1832. case 13:
  1833. //Intel Core i7-3000 / Xeon E5 (Sandy Bridge)
  1834. if(support_avx())
  1835. return CORE_SANDYBRIDGE;
  1836. else
  1837. return CORE_NEHALEM; //OS doesn't support AVX
  1838. case 14:
  1839. //Xeon E7540
  1840. case 15:
  1841. //Xeon Processor E7 (Westmere-EX)
  1842. return CORE_NEHALEM;
  1843. }
  1844. break;
  1845. case 3:
  1846. switch (model) {
  1847. case 7:
  1848. return CORE_ATOM;
  1849. case 10:
  1850. case 14:
  1851. if(support_avx())
  1852. return CORE_SANDYBRIDGE;
  1853. else
  1854. return CORE_NEHALEM; //OS doesn't support AVX
  1855. case 12:
  1856. case 15:
  1857. if(support_avx())
  1858. #ifndef NO_AVX2
  1859. return CORE_HASWELL;
  1860. #else
  1861. return CORE_SANDYBRIDGE;
  1862. #endif
  1863. else
  1864. return CORE_NEHALEM;
  1865. case 13:
  1866. //broadwell
  1867. if(support_avx())
  1868. #ifndef NO_AVX2
  1869. return CORE_HASWELL;
  1870. #else
  1871. return CORE_SANDYBRIDGE;
  1872. #endif
  1873. else
  1874. return CORE_NEHALEM;
  1875. }
  1876. break;
  1877. case 4:
  1878. switch (model) {
  1879. case 5:
  1880. case 6:
  1881. if(support_avx())
  1882. #ifndef NO_AVX2
  1883. return CORE_HASWELL;
  1884. #else
  1885. return CORE_SANDYBRIDGE;
  1886. #endif
  1887. else
  1888. return CORE_NEHALEM;
  1889. case 7:
  1890. case 15:
  1891. //broadwell
  1892. if(support_avx())
  1893. #ifndef NO_AVX2
  1894. return CORE_HASWELL;
  1895. #else
  1896. return CORE_SANDYBRIDGE;
  1897. #endif
  1898. else
  1899. return CORE_NEHALEM;
  1900. case 14:
  1901. //Skylake
  1902. if(support_avx())
  1903. #ifndef NO_AVX2
  1904. return CORE_HASWELL;
  1905. #else
  1906. return CORE_SANDYBRIDGE;
  1907. #endif
  1908. else
  1909. return CORE_NEHALEM;
  1910. case 12:
  1911. // Braswell
  1912. case 13:
  1913. // Avoton
  1914. return CORE_NEHALEM;
  1915. }
  1916. break;
  1917. case 10:
  1918. switch (model) {
  1919. case 5: // Comet Lake H and S
  1920. case 6: // Comet Lake U
  1921. if(support_avx())
  1922. #ifndef NO_AVX2
  1923. return CORE_HASWELL;
  1924. #else
  1925. return CORE_SANDYBRIDGE;
  1926. #endif
  1927. else
  1928. return CORE_NEHALEM;
  1929. case 7:// Rocket Lake
  1930. #ifndef NO_AVX512
  1931. if(support_avx512())
  1932. return CORE_SKYLAKEX;
  1933. #endif
  1934. #ifndef NO_AVX2
  1935. if(support_avx2())
  1936. return CORE_HASWELL;
  1937. #endif
  1938. if(support_avx())
  1939. return CORE_SANDYBRIDGE;
  1940. else
  1941. return CORE_NEHALEM;
  1942. }
  1943. case 5:
  1944. switch (model) {
  1945. case 6:
  1946. //broadwell
  1947. if(support_avx())
  1948. #ifndef NO_AVX2
  1949. return CORE_HASWELL;
  1950. #else
  1951. return CORE_SANDYBRIDGE;
  1952. #endif
  1953. else
  1954. return CORE_NEHALEM;
  1955. case 5:
  1956. // Skylake X
  1957. #ifndef NO_AVX512
  1958. if(support_avx512_bf16())
  1959. return CORE_COOPERLAKE;
  1960. return CORE_SKYLAKEX;
  1961. #else
  1962. if(support_avx())
  1963. #ifndef NO_AVX2
  1964. return CORE_HASWELL;
  1965. #else
  1966. return CORE_SANDYBRIDGE;
  1967. #endif
  1968. else
  1969. return CORE_NEHALEM;
  1970. #endif
  1971. case 14:
  1972. // Skylake
  1973. if(support_avx())
  1974. #ifndef NO_AVX2
  1975. return CORE_HASWELL;
  1976. #else
  1977. return CORE_SANDYBRIDGE;
  1978. #endif
  1979. else
  1980. return CORE_NEHALEM;
  1981. case 7:
  1982. // Phi Knights Landing
  1983. if(support_avx())
  1984. #ifndef NO_AVX2
  1985. return CORE_HASWELL;
  1986. #else
  1987. return CORE_SANDYBRIDGE;
  1988. #endif
  1989. else
  1990. return CORE_NEHALEM;
  1991. case 12:
  1992. // Apollo Lake
  1993. return CORE_NEHALEM;
  1994. }
  1995. break;
  1996. case 6:
  1997. if (model == 6)
  1998. #ifndef NO_AVX512
  1999. return CORE_SKYLAKEX;
  2000. #else
  2001. if(support_avx())
  2002. #ifndef NO_AVX2
  2003. return CORE_HASWELL;
  2004. #else
  2005. return CORE_SANDYBRIDGE;
  2006. #endif
  2007. else
  2008. return CORE_NEHALEM;
  2009. #endif
  2010. if (model == 10)
  2011. #ifndef NO_AVX512
  2012. if(support_avx512_bf16())
  2013. return CORE_COOPERLAKE;
  2014. return CORE_SKYLAKEX;
  2015. #else
  2016. if(support_avx())
  2017. #ifndef NO_AVX2
  2018. return CORE_HASWELL;
  2019. #else
  2020. return CORE_SANDYBRIDGE;
  2021. #endif
  2022. else
  2023. return CORE_NEHALEM;
  2024. #endif
  2025. break;
  2026. case 7:
  2027. if (model == 10)
  2028. return CORE_NEHALEM;
  2029. if (model == 14)
  2030. #ifndef NO_AVX512
  2031. return CORE_SKYLAKEX;
  2032. #else
  2033. if(support_avx())
  2034. #ifndef NO_AVX2
  2035. return CORE_HASWELL;
  2036. #else
  2037. return CORE_SANDYBRIDGE;
  2038. #endif
  2039. else
  2040. return CORE_NEHALEM;
  2041. #endif
  2042. break;
  2043. case 9:
  2044. case 8:
  2045. if (model == 12) { // Tiger Lake
  2046. if(support_avx512())
  2047. return CPUTYPE_SKYLAKEX;
  2048. if(support_avx2())
  2049. return CPUTYPE_HASWELL;
  2050. if(support_avx())
  2051. return CPUTYPE_SANDYBRIDGE;
  2052. else
  2053. return CPUTYPE_NEHALEM;
  2054. }
  2055. if (model == 14) { // Kaby Lake
  2056. if(support_avx())
  2057. #ifndef NO_AVX2
  2058. return CORE_HASWELL;
  2059. #else
  2060. return CORE_SANDYBRIDGE;
  2061. #endif
  2062. else
  2063. return CORE_NEHALEM;
  2064. }
  2065. }
  2066. break;
  2067. case 15:
  2068. if (model <= 0x2) return CORE_NORTHWOOD;
  2069. else return CORE_PRESCOTT;
  2070. }
  2071. }
  2072. if (vendor == VENDOR_AMD){
  2073. if (family <= 0x5) return CORE_80486;
  2074. #if defined(__x86_64__) || defined(__amd64__)
  2075. if (family <= 0xe) return CORE_BARCELONA;
  2076. #else
  2077. if (family <= 0xe) return CORE_ATHLON;
  2078. #endif
  2079. if (family == 0xf){
  2080. if ((exfamily == 0) || (exfamily == 2)) return CORE_OPTERON;
  2081. else if (exfamily == 5) return CORE_BOBCAT;
  2082. else if (exfamily == 6) {
  2083. switch (model) {
  2084. case 1:
  2085. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  2086. if(support_avx())
  2087. return CORE_BULLDOZER;
  2088. else
  2089. return CORE_BARCELONA; //OS don't support AVX.
  2090. case 2: //AMD Piledriver
  2091. case 3: //AMD Richland
  2092. if(support_avx())
  2093. return CORE_PILEDRIVER;
  2094. else
  2095. return CORE_BARCELONA; //OS don't support AVX.
  2096. case 5: // New EXCAVATOR
  2097. if(support_avx())
  2098. return CORE_EXCAVATOR;
  2099. else
  2100. return CORE_BARCELONA; //OS don't support AVX.
  2101. case 0:
  2102. case 8:
  2103. switch(exmodel){
  2104. case 1: //AMD Trinity
  2105. if(support_avx())
  2106. return CORE_PILEDRIVER;
  2107. else
  2108. return CORE_BARCELONA; //OS don't support AVX.
  2109. case 3:
  2110. if(support_avx())
  2111. return CORE_STEAMROLLER;
  2112. else
  2113. return CORE_BARCELONA; //OS don't support AVX.
  2114. case 6:
  2115. if(support_avx())
  2116. return CORE_EXCAVATOR;
  2117. else
  2118. return CORE_BARCELONA; //OS don't support AVX.
  2119. }
  2120. break;
  2121. }
  2122. } else if (exfamily == 8 || exfamily == 10) {
  2123. switch (model) {
  2124. case 1:
  2125. // AMD Ryzen
  2126. case 8:
  2127. // Ryzen 2
  2128. default:
  2129. // Matisse,Renoir Ryzen2 models
  2130. if(support_avx())
  2131. #ifndef NO_AVX2
  2132. return CORE_ZEN;
  2133. #else
  2134. return CORE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  2135. #endif
  2136. else
  2137. return CORE_BARCELONA;
  2138. }
  2139. } else {
  2140. return CORE_BARCELONA;
  2141. }
  2142. }
  2143. }
  2144. if (vendor == VENDOR_HYGON){
  2145. if (family == 0xf){
  2146. if (exfamily == 9) {
  2147. if(support_avx())
  2148. #ifndef NO_AVX2
  2149. return CORE_ZEN;
  2150. #else
  2151. return CORE_SANDYBRIDGE; // closer in architecture to Sandy Bridge than to Excavator
  2152. #endif
  2153. else
  2154. return CORE_BARCELONA;
  2155. } else {
  2156. return CORE_BARCELONA;
  2157. }
  2158. }
  2159. }
  2160. if (vendor == VENDOR_CENTAUR) {
  2161. switch (family) {
  2162. case 0x6:
  2163. return CORE_NANO;
  2164. break;
  2165. }
  2166. return CORE_VIAC3;
  2167. }
  2168. return CORE_UNKNOWN;
  2169. }
  2170. void get_cpuconfig(void){
  2171. cache_info_t info;
  2172. int features;
  2173. printf("#define %s\n", cpuname[get_cpuname()]);
  2174. if (get_coretype() != CORE_P5) {
  2175. get_cacheinfo(CACHE_INFO_L1_I, &info);
  2176. if (info.size > 0) {
  2177. printf("#define L1_CODE_SIZE %d\n", info.size * 1024);
  2178. printf("#define L1_CODE_ASSOCIATIVE %d\n", info.associative);
  2179. printf("#define L1_CODE_LINESIZE %d\n", info.linesize);
  2180. }
  2181. get_cacheinfo(CACHE_INFO_L1_D, &info);
  2182. if (info.size > 0) {
  2183. printf("#define L1_DATA_SIZE %d\n", info.size * 1024);
  2184. printf("#define L1_DATA_ASSOCIATIVE %d\n", info.associative);
  2185. printf("#define L1_DATA_LINESIZE %d\n", info.linesize);
  2186. }
  2187. get_cacheinfo(CACHE_INFO_L2, &info);
  2188. if (info.size > 0) {
  2189. printf("#define L2_SIZE %d\n", info.size * 1024);
  2190. printf("#define L2_ASSOCIATIVE %d\n", info.associative);
  2191. printf("#define L2_LINESIZE %d\n", info.linesize);
  2192. } else {
  2193. //fall back for some virtual machines.
  2194. printf("#define L2_SIZE 1048576\n");
  2195. printf("#define L2_ASSOCIATIVE 6\n");
  2196. printf("#define L2_LINESIZE 64\n");
  2197. }
  2198. get_cacheinfo(CACHE_INFO_L3, &info);
  2199. if (info.size > 0) {
  2200. printf("#define L3_SIZE %d\n", info.size * 1024);
  2201. printf("#define L3_ASSOCIATIVE %d\n", info.associative);
  2202. printf("#define L3_LINESIZE %d\n", info.linesize);
  2203. }
  2204. get_cacheinfo(CACHE_INFO_L1_ITB, &info);
  2205. if (info.size > 0) {
  2206. printf("#define ITB_SIZE %d\n", info.size * 1024);
  2207. printf("#define ITB_ASSOCIATIVE %d\n", info.associative);
  2208. printf("#define ITB_ENTRIES %d\n", info.linesize);
  2209. }
  2210. get_cacheinfo(CACHE_INFO_L1_DTB, &info);
  2211. if (info.size > 0) {
  2212. printf("#define DTB_SIZE %d\n", info.size * 1024);
  2213. printf("#define DTB_ASSOCIATIVE %d\n", info.associative);
  2214. printf("#define DTB_DEFAULT_ENTRIES %d\n", info.linesize);
  2215. } else {
  2216. //fall back for some virtual machines.
  2217. printf("#define DTB_DEFAULT_ENTRIES 32\n");
  2218. }
  2219. features = get_cputype(GET_FEATURE);
  2220. if (features & HAVE_CMOV ) printf("#define HAVE_CMOV\n");
  2221. if (features & HAVE_MMX ) printf("#define HAVE_MMX\n");
  2222. if (features & HAVE_SSE ) printf("#define HAVE_SSE\n");
  2223. if (features & HAVE_SSE2 ) printf("#define HAVE_SSE2\n");
  2224. if (features & HAVE_SSE3 ) printf("#define HAVE_SSE3\n");
  2225. if (features & HAVE_SSSE3) printf("#define HAVE_SSSE3\n");
  2226. if (features & HAVE_SSE4_1) printf("#define HAVE_SSE4_1\n");
  2227. if (features & HAVE_SSE4_2) printf("#define HAVE_SSE4_2\n");
  2228. if (features & HAVE_SSE4A) printf("#define HAVE_SSE4A\n");
  2229. if (features & HAVE_SSE5 ) printf("#define HAVE_SSSE5\n");
  2230. if (features & HAVE_AVX ) printf("#define HAVE_AVX\n");
  2231. if (features & HAVE_AVX2 ) printf("#define HAVE_AVX2\n");
  2232. if (features & HAVE_AVX512VL ) printf("#define HAVE_AVX512VL\n");
  2233. if (features & HAVE_AVX512BF16 ) printf("#define HAVE_AVX512BF16\n");
  2234. if (features & HAVE_3DNOWEX) printf("#define HAVE_3DNOWEX\n");
  2235. if (features & HAVE_3DNOW) printf("#define HAVE_3DNOW\n");
  2236. if (features & HAVE_FMA4 ) printf("#define HAVE_FMA4\n");
  2237. if (features & HAVE_FMA3 ) printf("#define HAVE_FMA3\n");
  2238. if (features & HAVE_CFLUSH) printf("#define HAVE_CFLUSH\n");
  2239. if (features & HAVE_HIT) printf("#define HAVE_HIT 1\n");
  2240. if (features & HAVE_MISALIGNSSE) printf("#define HAVE_MISALIGNSSE\n");
  2241. if (features & HAVE_128BITFPU) printf("#define HAVE_128BITFPU\n");
  2242. if (features & HAVE_FASTMOVU) printf("#define HAVE_FASTMOVU\n");
  2243. printf("#define NUM_SHAREDCACHE %d\n", get_cputype(GET_NUMSHARE) + 1);
  2244. printf("#define NUM_CORES %d\n", get_cputype(GET_NUMCORES) + 1);
  2245. features = get_coretype();
  2246. if (features > 0) printf("#define CORE_%s\n", corename[features]);
  2247. } else {
  2248. printf("#define DTB_DEFAULT_ENTRIES 16\n");
  2249. printf("#define L1_CODE_SIZE 8192\n");
  2250. printf("#define L1_DATA_SIZE 8192\n");
  2251. printf("#define L2_SIZE 0\n");
  2252. }
  2253. }
  2254. void get_architecture(void){
  2255. #ifndef __64BIT__
  2256. printf("X86");
  2257. #else
  2258. printf("X86_64");
  2259. #endif
  2260. }
  2261. void get_subarchitecture(void){
  2262. printf("%s", get_cpunamechar());
  2263. }
  2264. void get_subdirname(void){
  2265. #ifndef __64BIT__
  2266. printf("x86");
  2267. #else
  2268. printf("x86_64");
  2269. #endif
  2270. }
  2271. char *get_corename(void){
  2272. return corename[get_coretype()];
  2273. }
  2274. void get_libname(void){
  2275. printf("%s", corename_lower[get_coretype()]);
  2276. }
  2277. /* This if for Makefile */
  2278. void get_sse(void){
  2279. int features;
  2280. features = get_cputype(GET_FEATURE);
  2281. if (features & HAVE_MMX ) printf("HAVE_MMX=1\n");
  2282. if (features & HAVE_SSE ) printf("HAVE_SSE=1\n");
  2283. if (features & HAVE_SSE2 ) printf("HAVE_SSE2=1\n");
  2284. if (features & HAVE_SSE3 ) printf("HAVE_SSE3=1\n");
  2285. if (features & HAVE_SSSE3) printf("HAVE_SSSE3=1\n");
  2286. if (features & HAVE_SSE4_1) printf("HAVE_SSE4_1=1\n");
  2287. if (features & HAVE_SSE4_2) printf("HAVE_SSE4_2=1\n");
  2288. if (features & HAVE_SSE4A) printf("HAVE_SSE4A=1\n");
  2289. if (features & HAVE_SSE5 ) printf("HAVE_SSSE5=1\n");
  2290. if (features & HAVE_AVX ) printf("HAVE_AVX=1\n");
  2291. if (features & HAVE_AVX2 ) printf("HAVE_AVX2=1\n");
  2292. if (features & HAVE_AVX512VL ) printf("HAVE_AVX512VL=1\n");
  2293. if (features & HAVE_AVX512BF16 ) printf("HAVE_AVX512BF16=1\n");
  2294. if (features & HAVE_3DNOWEX) printf("HAVE_3DNOWEX=1\n");
  2295. if (features & HAVE_3DNOW) printf("HAVE_3DNOW=1\n");
  2296. if (features & HAVE_FMA4 ) printf("HAVE_FMA4=1\n");
  2297. if (features & HAVE_FMA3 ) printf("HAVE_FMA3=1\n");
  2298. }