PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/druntime/src/compiler/dmd/util/cpuid.d

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