PageRenderTime 58ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/core/cpuid.d

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