PageRenderTime 30ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/kruntime/src/core/cpuid.d

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