PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tango/core/tools/Cpuid.d

https://bitbucket.org/luca/tango
D | 786 lines | 498 code | 53 blank | 235 comment | 115 complexity | a05747ff34b27a4d5d4cd1927af7b265 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /**
  2. Identify the characteristics of the host CPU, providing information
  3. about cache sizes and assembly optimisation hints.
  4. Some of this information was extremely difficult to track down. Some of the
  5. documents below were found only in cached versions stored by search engines!
  6. This code relies on information found in:
  7. - "Intel(R) 64 and IA-32 Architectures Software Developers Manual,
  8. Volume 2A: Instruction Set Reference, A-M" (2007).
  9. - "AMD CPUID Specification", Advanced Micro Devices, Rev 2.28 (2008).
  10. - "AMD Processor Recognition Application Note For Processors Prior to AMD
  11. Family 0Fh Processors", Advanced Micro Devices, Rev 3.13 (2005).
  12. - "AMD Geode(TM) GX Processors Data Book",
  13. Advanced Micro Devices, Publication ID 31505E, (2005).
  14. - "AMD K6 Processor Code Optimisation", Advanced Micro Devices, Rev D (2000).
  15. - "Application note 106: Software Customization for the 6x86 Family",
  16. Cyrix Corporation, Rev 1.5 (1998)
  17. - http://ftp.intron.ac/pub/document/cpu/cpuid.htm
  18. - "Geode(TM) GX1 Processor Series Low Power Integrated X86 Solution",
  19. National Semiconductor, (2002)
  20. - "The VIA Isaiah Architecture", G. Glenn Henry, Centaur Technology, Inc (2008).
  21. - http://www.sandpile.org/ia32/cpuid.htm
  22. - http://grafi.ii.pw.edu.pl/gbm/x86/cpuid.html
  23. - "What every programmer should know about memory",
  24. Ulrich Depper, Red Hat, Inc., (2007).
  25. - "CPU Identification by the Windows Kernel", G. Chappell (2009).
  26. http://www.geoffchappell.com/viewer.htm?doc=studies/windows/km/cpu/cx8.htm
  27. - "Intel(R) Processor Identification and the CPUID Instruction, Application
  28. Note 485" (2009).
  29. AUTHORS: Don Clugston,
  30. Tomas Lindquist Olsen <tomas@famolsen.dk>
  31. COPYRIGHT: Public Domain
  32. BUGS: Currently only works on x86 CPUs.
  33. Many processors have bugs in their microcode for the CPUID instruction,
  34. so sometimes the cache information may be incorrect.
  35. */
  36. module tango.core.tools.Cpuid;
  37. // If optimizing for a particular processor, it is generally better
  38. // to identify based on features rather than model. NOTE: Normally
  39. // it's only worthwhile to optimise for the latest Intel and AMD CPU,
  40. // with a backup for other CPUs.
  41. // Pentium -- preferPentium1()
  42. // PMMX -- + mmx()
  43. // PPro -- default
  44. // PII -- + mmx()
  45. // PIII -- + mmx() + sse()
  46. // PentiumM -- + mmx() + sse() + sse2()
  47. // Pentium4 -- preferPentium4()
  48. // PentiumD -- + isX86_64()
  49. // Core2 -- default + isX86_64()
  50. // AMD K5 -- preferPentium1()
  51. // AMD K6 -- + mmx()
  52. // AMD K6-II -- + mmx() + 3dnow()
  53. // AMD K7 -- preferAthlon()
  54. // AMD K8 -- + sse2()
  55. // AMD K10 -- + isX86_64()
  56. // Cyrix 6x86 -- preferPentium1()
  57. // 6x86MX -- + mmx()
  58. public:
  59. /// Cache size and behaviour
  60. struct CacheInfo
  61. {
  62. /// Size of the cache, in kilobytes, per CPU.
  63. /// For L1 unified (data + code) caches, this size is half the physical size.
  64. /// (we don't halve it for larger sizes, since normally
  65. /// data size is much greater than code size for critical loops).
  66. uint size;
  67. /// Number of ways of associativity, eg:
  68. /// 1 = direct mapped
  69. /// 2 = 2-way set associative
  70. /// 3 = 3-way set associative
  71. /// ubyte.max = fully associative
  72. ubyte associativity;
  73. /// Number of bytes read into the cache when a cache miss occurs.
  74. uint lineSize;
  75. }
  76. public:
  77. /// Returns vendor string, for display purposes only.
  78. /// Do NOT use this to determine features!
  79. /// Note that some CPUs have programmable vendorIDs.
  80. char[] vendor() {return cast(char[]) vendorID;}
  81. /// Returns processor string, for display purposes only
  82. char[] processor() {return processorName;}
  83. /// The data caches. If there are fewer than 5 physical caches levels,
  84. /// the remaining levels are set to uint.max (== entire memory space)
  85. CacheInfo[5] datacache;
  86. /// Does it have an x87 FPU on-chip?
  87. bool x87onChip() {return (features&FPU_BIT)!=0;}
  88. /// Is MMX supported?
  89. bool mmx() {return (features&MMX_BIT)!=0;}
  90. /// Is SSE supported?
  91. bool sse() {return (features&SSE_BIT)!=0;}
  92. /// Is SSE2 supported?
  93. bool sse2() {return (features&SSE2_BIT)!=0;}
  94. /// Is SSE3 supported?
  95. bool sse3() {return (miscfeatures&SSE3_BIT)!=0;}
  96. /// Is SSSE3 supported?
  97. bool ssse3() {return (miscfeatures&SSSE3_BIT)!=0;}
  98. /// Is SSE4.1 supported?
  99. bool sse41() {return (miscfeatures&SSE41_BIT)!=0;}
  100. /// Is SSE4.2 supported?
  101. bool sse42() {return (miscfeatures&SSE42_BIT)!=0;}
  102. /// Is SSE4a supported?
  103. bool sse4a() {return (amdmiscfeatures&SSE4A_BIT)!=0;}
  104. /// Is AMD 3DNOW supported?
  105. bool amd3dnow() {return (amdfeatures&AMD_3DNOW_BIT)!=0;}
  106. /// Is AMD 3DNOW Ext supported?
  107. bool amd3dnowExt() {return (amdfeatures&AMD_3DNOW_EXT_BIT)!=0;}
  108. /// Are AMD extensions to MMX supported?
  109. bool amdMmx() {return (amdfeatures&AMD_MMX_BIT)!=0;}
  110. /// Is fxsave/fxrstor supported?
  111. bool hasFxsr() {return (features&FXSR_BIT)!=0;}
  112. /// Is cmov supported?
  113. bool hasCmov() {return (features&CMOV_BIT)!=0;}
  114. /// Is rdtsc supported?
  115. bool hasRdtsc() {return (features&TIMESTAMP_BIT)!=0;}
  116. /// Is cmpxchg8b supported?
  117. bool hasCmpxchg8b() {return (features&CMPXCHG8B_BIT)!=0;}
  118. /// Is cmpxchg8b supported?
  119. bool hasCmpxchg16b() {return (miscfeatures&CMPXCHG16B_BIT)!=0;}
  120. /// Is SYSENTER/SYSEXIT supported?
  121. bool hasSysEnterSysExit() {
  122. // The SYSENTER/SYSEXIT features were buggy on Pentium Pro and early PentiumII.
  123. // (REF: www.geoffchappell.com).
  124. if (probablyIntel && (family < 6 || (family==6 && (model< 3 || (model==3 && stepping<3)))))
  125. return false;
  126. return (features & SYSENTERSYSEXIT_BIT)!=0;
  127. }
  128. /// Is 3DNow prefetch supported?
  129. bool has3dnowPrefetch()
  130. {return (amdmiscfeatures&AMD_3DNOW_PREFETCH_BIT)!=0;}
  131. /// Are LAHF and SAHF supported in 64-bit mode?
  132. bool hasLahfSahf() {return (amdmiscfeatures&LAHFSAHF_BIT)!=0;}
  133. /// Is POPCNT supported?
  134. bool hasPopcnt() {return (miscfeatures&POPCNT_BIT)!=0;}
  135. /// Is LZCNT supported?
  136. bool hasLzcnt() {return (amdmiscfeatures&LZCNT_BIT)!=0;}
  137. /// Is this an Intel64 or AMD 64?
  138. bool isX86_64() {return (amdfeatures&AMD64_BIT)!=0;}
  139. /// Is this an IA64 (Itanium) processor?
  140. bool isItanium() { return (features&IA64_BIT)!=0; }
  141. /// Is hyperthreading supported?
  142. bool hyperThreading() { return maxThreads>maxCores; }
  143. /// Returns number of threads per CPU
  144. uint threadsPerCPU() {return maxThreads;}
  145. /// Returns number of cores in CPU
  146. uint coresPerCPU() {return maxCores;}
  147. /// Optimisation hints for assembly code.
  148. /// For forward compatibility, the CPU is compared against different
  149. /// microarchitectures. For 32-bit X86, comparisons are made against
  150. /// the Intel PPro/PII/PIII/PM family.
  151. ///
  152. /// The major 32-bit x86 microarchitecture 'dynasties' have been:
  153. /// (1) Intel P6 (PentiumPro, PII, PIII, PM, Core, Core2).
  154. /// (2) AMD Athlon (K7, K8, K10).
  155. /// (3) Intel NetBurst (Pentium 4, Pentium D).
  156. /// (4) In-order Pentium (Pentium1, PMMX, Atom)
  157. /// Other early CPUs (Nx586, AMD K5, K6, Centaur C3, Transmeta,
  158. /// Cyrix, Rise) were mostly in-order.
  159. /// Some new processors do not fit into the existing categories:
  160. /// Intel Atom 230/330 (family 6, model 0x1C) is an in-order core.
  161. /// Centaur Isiah = VIA Nano (family 6, model F) is an out-of-order core.
  162. ///
  163. /// Within each dynasty, the optimisation techniques are largely
  164. /// identical (eg, use instruction pairing for group 4). Major
  165. /// instruction set improvements occur within each dynasty.
  166. /// Does this CPU perform better on AMD K7 code than PentiumPro..Core2 code?
  167. bool preferAthlon() { return probablyAMD && family >=6; }
  168. /// Does this CPU perform better on Pentium4 code than PentiumPro..Core2 code?
  169. bool preferPentium4() { return probablyIntel && family == 0xF; }
  170. /// Does this CPU perform better on Pentium I code than Pentium Pro code?
  171. bool preferPentium1() { return family < 6 || (family==6 && model < 0xF && !probablyIntel); }
  172. public:
  173. /// Processor type (vendor-dependent).
  174. /// This should be visible ONLY for display purposes.
  175. uint stepping, model, family;
  176. uint numCacheLevels = 1;
  177. private:
  178. bool probablyIntel; // true = _probably_ an Intel processor, might be faking
  179. bool probablyAMD; // true = _probably_ an AMD processor
  180. char[12] vendorID;
  181. char[] processorName;
  182. char[48] processorNameBuffer;
  183. uint features = 0; // mmx, sse, sse2, hyperthreading, etc
  184. uint miscfeatures = 0; // sse3, etc.
  185. uint amdfeatures = 0; // 3DNow!, mmxext, etc
  186. uint amdmiscfeatures = 0; // sse4a, sse5, svm, etc
  187. uint maxCores = 1;
  188. uint maxThreads = 1;
  189. // Note that this may indicate multi-core rather than hyperthreading.
  190. bool hyperThreadingBit() { return (features&HTT_BIT)!=0;}
  191. // feature flags CPUID1_EDX
  192. enum : uint
  193. {
  194. FPU_BIT = 1,
  195. TIMESTAMP_BIT = 1<<4, // rdtsc
  196. MDSR_BIT = 1<<5, // RDMSR/WRMSR
  197. CMPXCHG8B_BIT = 1<<8,
  198. SYSENTERSYSEXIT_BIT = 1<<11,
  199. CMOV_BIT = 1<<15,
  200. MMX_BIT = 1<<23,
  201. FXSR_BIT = 1<<24,
  202. SSE_BIT = 1<<25,
  203. SSE2_BIT = 1<<26,
  204. HTT_BIT = 1<<28,
  205. IA64_BIT = 1<<30
  206. }
  207. // feature flags misc CPUID1_ECX
  208. enum : uint
  209. {
  210. SSE3_BIT = 1,
  211. PCLMULQDQ_BIT = 1<<1, // from AVX
  212. MWAIT_BIT = 1<<3,
  213. SSSE3_BIT = 1<<9,
  214. FMA_BIT = 1<<12, // from AVX
  215. CMPXCHG16B_BIT = 1<<13,
  216. SSE41_BIT = 1<<19,
  217. SSE42_BIT = 1<<20,
  218. POPCNT_BIT = 1<<23,
  219. AES_BIT = 1<<25, // AES instructions from AVX
  220. OSXSAVE_BIT = 1<<27, // Used for AVX
  221. AVX_BIT = 1<<28
  222. }
  223. /+
  224. version(X86_64) {
  225. bool hasAVXinHardware() {
  226. // This only indicates hardware support, not OS support.
  227. return (miscfeatures&AVX_BIT) && (miscfeatures&OSXSAVE_BIT);
  228. }
  229. // Is AVX supported (in both hardware & OS)?
  230. bool Avx() {
  231. if (!hasAVXinHardware()) return false;
  232. // Check for OS support
  233. uint xfeatures;
  234. asm {mov ECX, 0; xgetbv; mov xfeatures, EAX; }
  235. return (xfeatures&0x6)==6;
  236. }
  237. bool hasAvxFma() {
  238. if (!AVX()) return false;
  239. return (features&FMA_BIT)!=0;
  240. }
  241. }
  242. +/
  243. // AMD feature flags CPUID80000001_EDX
  244. enum : uint
  245. {
  246. AMD_MMX_BIT = 1<<22,
  247. // FXR_OR_CYRIXMMX_BIT = 1<<24, // Cyrix/NS: 6x86MMX instructions.
  248. FFXSR_BIT = 1<<25,
  249. PAGE1GB_BIT = 1<<26, // support for 1GB pages
  250. RDTSCP_BIT = 1<<27,
  251. AMD64_BIT = 1<<29,
  252. AMD_3DNOW_EXT_BIT = 1<<30,
  253. AMD_3DNOW_BIT = 1<<31
  254. }
  255. // AMD misc feature flags CPUID80000001_ECX
  256. enum : uint
  257. {
  258. LAHFSAHF_BIT = 1,
  259. LZCNT_BIT = 1<<5,
  260. SSE4A_BIT = 1<<6,
  261. AMD_3DNOW_PREFETCH_BIT = 1<<8
  262. }
  263. version(GNU){
  264. // GDC is a filthy liar. It can't actually do inline asm.
  265. } else version(D_InlineAsm_X86) {
  266. version = Really_D_InlineAsm_X86;
  267. }
  268. version(Really_D_InlineAsm_X86) {
  269. // Note that this code will also work for Itanium in x86 mode.
  270. uint max_cpuid, max_extended_cpuid;
  271. // CPUID2: "cache and tlb information"
  272. void getcacheinfoCPUID2()
  273. {
  274. // CPUID2 is a dog's breakfast. What was Intel thinking???
  275. // We are only interested in the data caches
  276. void decipherCpuid2(ubyte x) {
  277. if (x==0) return;
  278. // Values from http://www.sandpile.org/ia32/cpuid.htm.
  279. // Includes Itanium and non-Intel CPUs.
  280. //
  281. static ubyte[63] ids = [
  282. 0x0A, 0x0C, 0x0D, 0x2C, 0x60, 0x0E, 0x66, 0x67, 0x68,
  283. // level 2 cache
  284. 0x41, 0x42, 0x43, 0x44, 0x45, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7F,
  285. 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x49, 0x4E,
  286. 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x48, 0x80, 0x81,
  287. // level 3 cache
  288. 0x22, 0x23, 0x25, 0x29, 0x46, 0x47, 0x4A, 0x4B, 0x4C, 0x4D,
  289. 0xD0, 0xD1, 0xD2, 0xD6, 0xD7, 0xD8, 0xDC, 0xDD, 0xDE,
  290. 0xE2, 0xE3, 0xE4, 0xEA, 0xEB, 0xEC
  291. ];
  292. static uint[63] sizes = [
  293. 8, 16, 16, 64, 16, 24, 8, 16, 32,
  294. 128, 256, 512, 1024, 2048, 1024, 128, 256, 512, 1024, 2048, 512,
  295. 256, 512, 1024, 2048, 512, 1024, 4096, 6*1024,
  296. 128, 192, 128, 256, 384, 512, 3072, 512, 128,
  297. 512, 1024, 2048, 4096, 4096, 8192, 6*1024, 8192, 12*1024, 16*1024,
  298. 512, 1024, 2048, 1024, 2048, 4096, 1024+512, 3*1024, 6*1024,
  299. 2*1024, 4*1024, 8*1024, 12*1024, 28*1024, 24*1024
  300. ];
  301. // CPUBUG: Pentium M reports 0x2C but tests show it is only 4-way associative
  302. static ubyte[63] ways = [
  303. 2, 4, 4, 8, 8, 6, 4, 4, 4,
  304. 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 2,
  305. 8, 8, 8, 8, 4, 8, 16, 24,
  306. 4, 6, 2, 4, 6, 4, 12, 8, 8,
  307. 4, 8, 8, 8, 4, 8, 12, 16, 12, 16,
  308. 4, 4, 4, 8, 8, 8, 12, 12, 12,
  309. 16, 16, 16, 24, 24, 24
  310. ];
  311. enum { FIRSTDATA2 = 8, FIRSTDATA3 = 28+9 }
  312. for (int i=0; i< ids.length; ++i) {
  313. if (x==ids[i]) {
  314. int level = i< FIRSTDATA2 ? 0: i<FIRSTDATA3 ? 1 : 2;
  315. if (x==0x49 && family==0xF && model==0x6) level=2;
  316. datacache[level].size=sizes[i];
  317. datacache[level].associativity=ways[i];
  318. if (level == 3 || x==0x2C || x==0x0D || (x>=0x48 && x<=0x80)
  319. || x==0x86 || x==0x87
  320. || (x>=0x66 && x<=0x68) || (x>=0x39 && x<=0x3E)){
  321. datacache[level].lineSize = 64;
  322. } else datacache[level].lineSize = 32;
  323. }
  324. }
  325. }
  326. uint[4] a;
  327. bool firstTime = true;
  328. // On a multi-core system, this could theoretically fail, but it's only used
  329. // for old single-core CPUs.
  330. uint numinfos = 1;
  331. do {
  332. asm {
  333. mov EAX, 2;
  334. cpuid;
  335. mov a, EAX;
  336. mov a+4, EBX;
  337. mov a+8, ECX;
  338. mov a+12, EDX;
  339. }
  340. if (firstTime) {
  341. if (a[0]==0x0000_7001 && a[3]==0x80 && a[1]==0 && a[2]==0) {
  342. // Cyrix MediaGX MMXEnhanced returns: EAX= 00007001, EDX=00000080.
  343. // These are NOT standard Intel values
  344. // (TLB = 32 entry, 4 way associative, 4K pages)
  345. // (L1 cache = 16K, 4way, linesize16)
  346. datacache[0].size=8;
  347. datacache[0].associativity=4;
  348. datacache[0].lineSize=16;
  349. return;
  350. }
  351. // lsb of a is how many times to loop.
  352. numinfos = a[0] & 0xFF;
  353. // and otherwise it should be ignored
  354. a[0] &= 0xFFFF_FF00;
  355. firstTime = false;
  356. }
  357. for (int c=0; c<4;++c) {
  358. // high bit set == no info.
  359. if (a[c] & 0x8000_0000) continue;
  360. decipherCpuid2(cast(ubyte)(a[c] & 0xFF));
  361. decipherCpuid2(cast(ubyte)((a[c]>>8) & 0xFF));
  362. decipherCpuid2(cast(ubyte)((a[c]>>16) & 0xFF));
  363. decipherCpuid2(cast(ubyte)((a[c]>>24) & 0xFF));
  364. }
  365. } while (--numinfos);
  366. }
  367. // CPUID4: "Deterministic cache parameters" leaf
  368. void getcacheinfoCPUID4()
  369. {
  370. int cachenum = 0;
  371. for(;;) {
  372. uint a, b, number_of_sets;
  373. asm {
  374. mov EAX, 4;
  375. mov ECX, cachenum;
  376. cpuid;
  377. mov a, EAX;
  378. mov b, EBX;
  379. mov number_of_sets, ECX;
  380. }
  381. ++cachenum;
  382. if ((a&0x1F)==0) break; // no more caches
  383. uint numthreads = ((a>>14) & 0xFFF) + 1;
  384. uint numcores = ((a>>26) & 0x3F) + 1;
  385. if (numcores > maxCores) maxCores = numcores;
  386. if ((a&0x1F)!=1 && ((a&0x1F)!=3)) continue; // we only want data & unified caches
  387. ++number_of_sets;
  388. ubyte level = cast(ubyte)(((a>>5)&7)-1);
  389. if (level > datacache.length) continue; // ignore deep caches
  390. datacache[level].associativity = a & 0x200 ? ubyte.max :cast(ubyte)((b>>22)+1);
  391. datacache[level].lineSize = (b & 0xFFF)+ 1; // system coherency line size
  392. uint line_partitions = ((b >> 12)& 0x3FF) + 1;
  393. // Size = number of sets * associativity * cachelinesize * linepartitions
  394. // and must convert to Kb, also dividing by the number of hyperthreads using this cache.
  395. ulong sz = (datacache[level].associativity< ubyte.max)? number_of_sets *
  396. datacache[level].associativity : number_of_sets;
  397. datacache[level].size = cast(uint)(
  398. (sz * datacache[level].lineSize * line_partitions ) / (numthreads *1024));
  399. if (level == 0 && (a&0xF)==3) {
  400. // Halve the size for unified L1 caches
  401. datacache[level].size/=2;
  402. }
  403. }
  404. }
  405. // CPUID8000_0005 & 6
  406. void getAMDcacheinfo()
  407. {
  408. uint c5, c6, d6;
  409. asm {
  410. mov EAX, 0x8000_0005; // L1 cache
  411. cpuid;
  412. // EAX has L1_TLB_4M.
  413. // EBX has L1_TLB_4K
  414. // EDX has L1 instruction cache
  415. mov c5, ECX;
  416. }
  417. datacache[0].size = ( (c5>>24) & 0xFF);
  418. datacache[0].associativity = cast(ubyte)( (c5 >> 16) & 0xFF);
  419. datacache[0].lineSize = c5 & 0xFF;
  420. if (max_extended_cpuid >= 0x8000_0006) {
  421. // AMD K6-III or K6-2+ or later.
  422. ubyte numcores = 1;
  423. if (max_extended_cpuid >=0x8000_0008) {
  424. asm {
  425. mov EAX, 0x8000_0008;
  426. cpuid;
  427. mov numcores, CL;
  428. }
  429. ++numcores;
  430. if (numcores>maxCores) maxCores = numcores;
  431. }
  432. asm {
  433. mov EAX, 0x8000_0006; // L2/L3 cache
  434. cpuid;
  435. mov c6, ECX; // L2 cache info
  436. mov d6, EDX; // L3 cache info
  437. }
  438. ubyte [] assocmap = [ 0, 1, 2, 0, 4, 0, 8, 0, 16, 0, 32, 48, 64, 96, 128, 0xFF ];
  439. datacache[1].size = (c6>>16) & 0xFFFF;
  440. datacache[1].associativity = assocmap[(c6>>12)&0xF];
  441. datacache[1].lineSize = c6 & 0xFF;
  442. // The L3 cache value is TOTAL, not per core.
  443. datacache[2].size = ((d6>>18)*512)/numcores; // could be up to 2 * this, -1.
  444. datacache[2].associativity = assocmap[(d6>>12)&0xF];
  445. datacache[2].lineSize = d6 & 0xFF;
  446. }
  447. }
  448. // For Intel CoreI7 and later, use function 0x0B
  449. // to determine number of processors.
  450. void getCpuInfo0B()
  451. {
  452. int level=0;
  453. uint a, b, c, d;
  454. do {
  455. asm {
  456. mov EAX, 0x0B;
  457. mov ECX, level;
  458. cpuid;
  459. mov a, EAX;
  460. mov b, EBX;
  461. mov c, ECX;
  462. mov d, EDX;
  463. }
  464. if (b!=0) {
  465. // I'm not sure about this. The docs state that there
  466. // are 2 hyperthreads per core if HT is factory enabled.
  467. if (level==0) maxThreads = b & 0xFFFF;
  468. else if (level==1) maxCores = b & 0xFFFF;
  469. }
  470. ++level;
  471. } while (a!=0 || b!=0);
  472. }
  473. void cpuidX86()
  474. {
  475. char * venptr = vendorID.ptr;
  476. uint a, b, c, d, a2;
  477. asm {
  478. mov EAX, 0;
  479. cpuid;
  480. mov a, EAX;
  481. mov EAX, venptr;
  482. mov [EAX], EBX;
  483. mov [EAX + 4], EDX;
  484. mov [EAX + 8], ECX;
  485. mov EAX, 0x8000_0000;
  486. cpuid;
  487. mov a2, EAX;
  488. }
  489. max_cpuid = a;
  490. max_extended_cpuid = a2;
  491. probablyIntel = vendorID == "GenuineIntel";
  492. probablyAMD = vendorID == "AuthenticAMD";
  493. uint apic = 0; // brand index, apic id
  494. asm {
  495. mov EAX, 1; // model, stepping
  496. cpuid;
  497. mov a, EAX;
  498. mov apic, EBX;
  499. mov c, ECX;
  500. mov d, EDX;
  501. }
  502. features = d;
  503. miscfeatures = c;
  504. amdfeatures = 0;
  505. amdmiscfeatures = 0;
  506. if (max_extended_cpuid >= 0x8000_0001) {
  507. asm {
  508. mov EAX, 0x8000_0001;
  509. cpuid;
  510. mov c, ECX;
  511. mov d, EDX;
  512. }
  513. amdmiscfeatures = c;
  514. amdfeatures = d;
  515. }
  516. // Try to detect fraudulent vendorIDs
  517. if (amd3dnow) probablyIntel = false;
  518. stepping = a & 0xF;
  519. uint fbase = (a >> 8) & 0xF;
  520. uint mbase = (a >> 4) & 0xF;
  521. family = ((fbase == 0xF) || (fbase == 0)) ? fbase + (a >> 20) & 0xFF : fbase;
  522. model = ((fbase == 0xF) || (fbase == 6 && probablyIntel) ) ?
  523. mbase + ((a >> 12) & 0xF0) : mbase;
  524. if (!probablyIntel && max_extended_cpuid >= 0x8000_0008) {
  525. // determine max number of cores for AMD
  526. asm {
  527. mov EAX, 0x8000_0008;
  528. cpuid;
  529. mov c, ECX;
  530. }
  531. uint apicsize = (c>>12) & 0xF;
  532. if (apicsize == 0) {
  533. // use legacy method
  534. if (hyperThreadingBit) maxCores = c & 0xFF;
  535. else maxCores = 1;
  536. } else {
  537. // maxcores = 2^ apicsize
  538. maxCores = 1;
  539. while (apicsize) { maxCores<<=1; --apicsize; }
  540. }
  541. }
  542. if (max_extended_cpuid >= 0x8000_0004) {
  543. char *procptr = processorNameBuffer.ptr;
  544. asm {
  545. push ESI;
  546. mov ESI, procptr;
  547. mov EAX, 0x8000_0002;
  548. cpuid;
  549. mov [ESI], EAX;
  550. mov [ESI+4], EBX;
  551. mov [ESI+8], ECX;
  552. mov [ESI+12], EDX;
  553. mov EAX, 0x8000_0003;
  554. cpuid;
  555. mov [ESI+16], EAX;
  556. mov [ESI+20], EBX;
  557. mov [ESI+24], ECX;
  558. mov [ESI+28], EDX;
  559. mov EAX, 0x8000_0004;
  560. cpuid;
  561. mov [ESI+32], EAX;
  562. mov [ESI+36], EBX;
  563. mov [ESI+40], ECX;
  564. mov [ESI+44], EDX;
  565. pop ESI;
  566. }
  567. // Intel P4 and PM pad at front with spaces.
  568. // Other CPUs pad at end with nulls.
  569. int start = 0, end = 0;
  570. while (processorNameBuffer[start] == ' ') { ++start; }
  571. while (processorNameBuffer[$-end-1] == 0) { ++end; }
  572. processorName = processorNameBuffer[start..$-end];
  573. } else {
  574. processorName = "Unknown CPU";
  575. }
  576. // Determine cache sizes
  577. // Intel docs specify that they return 0 for 0x8000_0005.
  578. // AMD docs do not specify the behaviour for 0004 and 0002.
  579. // Centaur/VIA and most other manufacturers use the AMD method,
  580. // except Cyrix MediaGX MMX Enhanced uses their OWN form of CPUID2!
  581. // NS Geode GX1 provides CyrixCPUID2 _and_ does the same wrong behaviour
  582. // for CPUID80000005. But Geode GX uses the AMD method
  583. // Deal with idiotic Geode GX1 - make it same as MediaGX MMX.
  584. if (max_extended_cpuid==0x8000_0005 && max_cpuid==2) {
  585. max_extended_cpuid = 0x8000_0004;
  586. }
  587. // Therefore, we try the AMD method unless it's an Intel chip.
  588. // If we still have no info, try the Intel methods.
  589. datacache[0].size = 0;
  590. if (max_cpuid<2 || !probablyIntel) {
  591. if (max_extended_cpuid >= 0x8000_0005) {
  592. getAMDcacheinfo();
  593. } else if (probablyAMD) {
  594. // According to AMDProcRecognitionAppNote, this means CPU
  595. // K5 model 0, or Am5x86 (model 4), or Am4x86DX4 (model 4)
  596. // Am5x86 has 16Kb 4-way unified data & code cache.
  597. datacache[0].size = 8;
  598. datacache[0].associativity = 4;
  599. datacache[0].lineSize = 32;
  600. } else {
  601. // Some obscure CPU.
  602. // Values for Cyrix 6x86MX (family 6, model 0)
  603. datacache[0].size = 64;
  604. datacache[0].associativity = 4;
  605. datacache[0].lineSize = 32;
  606. }
  607. }
  608. if ((datacache[0].size == 0) && max_cpuid>=4) {
  609. getcacheinfoCPUID4();
  610. }
  611. if ((datacache[0].size == 0) && max_cpuid>=2) {
  612. getcacheinfoCPUID2();
  613. }
  614. if (datacache[0].size == 0) {
  615. // Pentium, PMMX, late model 486, or an obscure CPU
  616. if (mmx) { // Pentium MMX. Also has 8kB code cache.
  617. datacache[0].size = 16;
  618. datacache[0].associativity = 4;
  619. datacache[0].lineSize = 32;
  620. } else { // Pentium 1 (which also has 8kB code cache)
  621. // or 486.
  622. // Cyrix 6x86: 16, 4way, 32 linesize
  623. datacache[0].size = 8;
  624. datacache[0].associativity = 2;
  625. datacache[0].lineSize = 32;
  626. }
  627. }
  628. if (max_cpuid >=0x0B) {
  629. // For Intel i7 and later, use function 0x0B to determine
  630. // cores and hyperthreads.
  631. getCpuInfo0B();
  632. } else {
  633. if (hyperThreadingBit) maxThreads = (apic>>>16) & 0xFF;
  634. else maxThreads = maxCores;
  635. }
  636. }
  637. // Return true if the cpuid instruction is supported.
  638. // BUG(WONTFIX): Returns false for Cyrix 6x86 and 6x86L. They will be treated as 486 machines.
  639. bool hasCPUID()
  640. {
  641. uint flags;
  642. asm {
  643. pushfd;
  644. pop EAX;
  645. mov flags, EAX;
  646. xor EAX, 0x0020_0000;
  647. push EAX;
  648. popfd;
  649. pushfd;
  650. pop EAX;
  651. xor flags, EAX;
  652. }
  653. return (flags & 0x0020_0000) !=0;
  654. }
  655. } else { // inline asm X86
  656. bool hasCPUID() { return false; }
  657. void cpuidX86()
  658. {
  659. datacache[0].size = 8;
  660. datacache[0].associativity = 2;
  661. datacache[0].lineSize = 32;
  662. }
  663. }
  664. // TODO: Implement this function with OS support
  665. void cpuidPPC()
  666. {
  667. enum :uint { PPC601, PPC603, PPC603E, PPC604,
  668. PPC604E, PPC620, PPCG3, PPCG4, PPCG5 };
  669. // TODO:
  670. // asm { mfpvr; } returns the CPU version but unfortunately it can
  671. // only be used in kernel mode. So OS support is required.
  672. uint cputype = PPC603;
  673. // 601 has a 8KB combined data & code L1 cache.
  674. uint[] sizes = [4, 8, 16, 16, 32, 32, 32, 32, 64];
  675. ubyte[] ways = [8, 2, 4, 4, 4, 8, 8, 8, 8];
  676. uint[] L2size= [0, 0, 0, 0, 0, 0, 0, 256, 512];
  677. uint[] L3size= [0, 0, 0, 0, 0, 0, 0, 2048, 0];
  678. datacache[0].size = sizes[cputype];
  679. datacache[0].associativity = ways[cputype];
  680. datacache[0].lineSize = (cputype==PPCG5)? 128u :
  681. (cputype == PPC620 || cputype == PPCG3)? 64u : 32u;
  682. datacache[1].size = L2size[cputype];
  683. datacache[2].size = L3size[cputype];
  684. datacache[1].lineSize = datacache[0].lineSize;
  685. datacache[2].lineSize = datacache[0].lineSize;
  686. }
  687. // TODO: Implement this function with OS support
  688. void cpuidSparc()
  689. {
  690. // UltaSparcIIi : L1 = 16, 2way. L2 = 512, 4 way.
  691. // UltraSparcIII : L1 = 64, 4way. L2= 4096 or 8192.
  692. // UltraSparcIIIi: L1 = 64, 4way. L2= 1024, 4 way
  693. // UltraSparcIV : L1 = 64, 4way. L2 = 16*1024.
  694. // UltraSparcIV+ : L1 = 64, 4way. L2 = 2048, L3=32*1024.
  695. // Sparc64V : L1 = 128, 2way. L2 = 4096 4way.
  696. }
  697. static this()
  698. {
  699. if (hasCPUID()) {
  700. cpuidX86();
  701. } else {
  702. // it's a 386 or 486, or a Cyrix 6x86.
  703. //Probably still has an external cache.
  704. }
  705. if (datacache[0].size==0) {
  706. // Guess same as Pentium 1.
  707. datacache[0].size = 8;
  708. datacache[0].associativity = 2;
  709. datacache[0].lineSize = 32;
  710. }
  711. numCacheLevels = 1;
  712. // And now fill up all the unused levels with full memory space.
  713. for (uint32 i=1; i< datacache.length; ++i) {
  714. if (datacache[i].size==0) {
  715. // Set all remaining levels of cache equal to full address space.
  716. datacache[i].size = uint.max/1024;
  717. datacache[i].associativity = 1;
  718. datacache[i].lineSize = datacache[i-1].lineSize;
  719. } else numCacheLevels = i+1;
  720. }
  721. }
  722. debug (Cpuid)
  723. {
  724. private import tango.io.Stdout;
  725. void main()
  726. {
  727. Stdout.formatln ("{}, {} threads, {} cores", processor, threadsPerCPU, coresPerCPU);
  728. }
  729. }