From 8ca5eea1e1173bc6c06ec00f2851d4ee5e4e6189 Mon Sep 17 00:00:00 2001
From: Li Lei
Date: Fri, 2 Nov 2018 10:08:56 -0700
Subject: [PATCH] Using leaf 0xb and subleaf 1 to get the cpu number
---
Pal/src/host/FreeBSD/db_main.c | 10 +++++++++-
Pal/src/host/Linux-SGX/db_main.c | 10 ++++++++--
Pal/src/host/Linux/db_main.c | 11 +++++++++--
3 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/Pal/src/host/FreeBSD/db_main.c b/Pal/src/host/FreeBSD/db_main.c
index fe2f6550..660dbe22 100644
--- a/Pal/src/host/FreeBSD/db_main.c
+++ b/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);
diff --git a/Pal/src/host/Linux-SGX/db_main.c b/Pal/src/host/Linux-SGX/db_main.c
index c7b8eccd..abc75a68 100644
--- a/Pal/src/host/Linux-SGX/db_main.c
+++ b/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) +
diff --git a/Pal/src/host/Linux/db_main.c b/Pal/src/host/Linux/db_main.c
index cef36f09..a5d7a007 100644
--- a/Pal/src/host/Linux/db_main.c
+++ b/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;