PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/core/cpuid.d

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