Browse Source

Using leaf 0xb and subleaf 1 to get the cpu number

Li Lei 5 years ago
parent
commit
8ca5eea1e1
3 changed files with 26 additions and 5 deletions
  1. 9 1
      Pal/src/host/FreeBSD/db_main.c
  2. 8 2
      Pal/src/host/Linux-SGX/db_main.c
  3. 9 2
      Pal/src/host/Linux/db_main.c

+ 9 - 1
Pal/src/host/FreeBSD/db_main.c

@@ -358,11 +358,19 @@ void _DkGetCPUInfo (PAL_CPU_INFO * ci)
     ci->cpu_brand = brand;
 
     cpuid(2, 1, words, 0);
-    ci->cpu_num      = BIT_EXTRACT_LE(words[WORD_EBX], 16, 24);
     ci->cpu_family   = BIT_EXTRACT_LE(words[WORD_EAX],  8, 12) + 1;
     ci->cpu_model    = BIT_EXTRACT_LE(words[WORD_EAX],  4,  8);
     ci->cpu_stepping = BIT_EXTRACT_LE(words[WORD_EAX],  0,  4);
 
+    /* According to SDM: EBX[15:0] is to enumerate processor topology 
+     * of the system. However this value is intended for display/diagnostic
+     * purposes. The actual number of logical processors available to
+     * BIOS/OS/App may be different. We use this leaf for now as it's the 
+     * best option we have so far to get the cpu number  */
+
+    cpuid(0xb, 1, words, 0);
+    ci->cpu_num      = BIT_EXTRACT_LE(words[WORD_EBX],  0, 16);
+
     int flen = 0, fmax = 80;
     char * flags = malloc(fmax);
 

+ 8 - 2
Pal/src/host/Linux-SGX/db_main.c

@@ -311,8 +311,14 @@ void _DkGetCPUInfo (PAL_CPU_INFO * ci)
     memcpy(&brand[32], words, sizeof(unsigned int) * WORD_NUM);
     ci->cpu_brand = brand;
 
-    cpuid(4, 0, words);
-    ci->cpu_num      = BIT_EXTRACT_LE(words[WORD_EAX], 26, 32) + 1;
+    /* According to SDM: EBX[15:0] is to enumerate processor topology 
+     * of the system. However this value is intended for display/diagnostic
+     * purposes. The actual number of logical processors available to
+     * BIOS/OS/App may be different. We use this leaf for now as it's the 
+     * best option we have so far to get the cpu number  */
+
+    cpuid(0xb, 1, words);
+    ci->cpu_num      = BIT_EXTRACT_LE(words[WORD_EBX], 0, 16);
 
     cpuid(1, 0, words);
     ci->cpu_family   = BIT_EXTRACT_LE(words[WORD_EAX],  8, 12) +

+ 9 - 2
Pal/src/host/Linux/db_main.c

@@ -407,8 +407,15 @@ void _DkGetCPUInfo (PAL_CPU_INFO * ci)
     ci->cpu_brand = brand;
 
     if (!memcmp(vendor_id, "GenuineIntel", 12)) {
-        cpuid(4, 0, words);
-        ci->cpu_num  = BIT_EXTRACT_LE(words[WORD_EAX], 26, 32) + 1;
+
+       /* According to SDM: EBX[15:0] is to enumerate processor topology
+        * of the system. However this value is intended for display/diagnostic
+        * purposes. The actual number of logical processors available to
+        * BIOS/OS/App may be different. We use this leaf for now as it's the
+        * best option we have so far to get the cpu number  */
+
+        cpuid(0xb, 1, words);
+        ci->cpu_num  = BIT_EXTRACT_LE(words[WORD_EBX], 0, 16);
     } else if (!memcmp(vendor_id, "AuthenticAMD", 12)) {
         cpuid(0x8000008, 0, words);
         ci->cpu_num  = BIT_EXTRACT_LE(words[WORD_EAX], 0, 8) + 1;