PageRenderTime 29ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/libdruntime/core/cpuid.d

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