PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/d/druntime/core/cpuid.d

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