PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/src/core/cpuid.d

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