PageRenderTime 121ms CodeModel.GetById 61ms RepoModel.GetById 1ms app.codeStats 0ms

/d/druntime/core/cpuid.d

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