瀏覽代碼

[Pal] support DkCpuIdRetrieve()

- Implements DkCpuIdRetrieve for Pal.
- move the definition WORD_E[ABCD]X to pal.h
  and eliminated duplicated deifnitions in each Pal.
- prefix PAL_CPUID_ to WORD_E[ABCD]X.

Signed-off-by: Isaku Yamahata <isaku.yamahata@gmail.com>
Isaku Yamahata 5 年之前
父節點
當前提交
024a185e29

+ 15 - 21
Pal/src/host/FreeBSD/db_main.c

@@ -267,20 +267,14 @@ done_init:
 
 /* the following code is borrowed from CPUID */
 
-#define WORD_EAX  0
-#define WORD_EBX  1
-#define WORD_ECX  2
-#define WORD_EDX  3
-#define WORD_NUM  4
-
 static void cpuid (int cpuid_fd, unsigned int reg,
                    unsigned int words[], unsigned int ecx)
 {
    asm("cpuid"
-       : "=a" (words[WORD_EAX]),
-         "=b" (words[WORD_EBX]),
-         "=c" (words[WORD_ECX]),
-         "=d" (words[WORD_EDX])
+       : "=a" (words[PAL_CPUID_WORD_EAX]),
+         "=b" (words[PAL_CPUID_WORD_EBX]),
+         "=c" (words[PAL_CPUID_WORD_ECX]),
+         "=d" (words[PAL_CPUID_WORD_EDX])
        : "a" (reg),
          "c" (ecx));
 }
@@ -338,33 +332,33 @@ static char * cpu_flags[]
 
 void _DkGetCPUInfo (PAL_CPU_INFO * ci)
 {
-    unsigned int words[WORD_NUM];
+    unsigned int words[PAL_CPUID_WORD_NUM];
 
     const size_t VENDOR_ID_SIZE = 13;
     char* vendor_id = malloc(VENDOR_ID_SIZE);
     cpuid(2, 0, words, 0);
 
-    FOUR_CHARS_VALUE(&vendor_id[0], words[WORD_EBX]);
-    FOUR_CHARS_VALUE(&vendor_id[4], words[WORD_EDX]);
-    FOUR_CHARS_VALUE(&vendor_id[8], words[WORD_ECX]);
+    FOUR_CHARS_VALUE(&vendor_id[0], words[PAL_CPUID_WORD_EBX]);
+    FOUR_CHARS_VALUE(&vendor_id[4], words[PAL_CPUID_WORD_EDX]);
+    FOUR_CHARS_VALUE(&vendor_id[8], words[PAL_CPUID_WORD_ECX]);
     vendor_id[VENDOR_ID_SIZE - 1] = '\0';
     ci->cpu_vendor = vendor_id;
 
     const size_t BRAND_SIZE = 49;
     char* brand = malloc(BRAND_SIZE);
     cpuid(-2, 0x80000002, words, 0);
-    memcpy(&brand[ 0], words, sizeof(unsigned int) * WORD_NUM);
+    memcpy(&brand[ 0], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
     cpuid(-2, 0x80000003, words, 0);
-    memcpy(&brand[16], words, sizeof(unsigned int) * WORD_NUM);
+    memcpy(&brand[16], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
     cpuid(-2, 0x80000004, words, 0);
-    memcpy(&brand[32], words, sizeof(unsigned int) * WORD_NUM);
+    memcpy(&brand[32], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
     brand[BRAND_SIZE - 1] = '\0';
     ci->cpu_brand = brand;
 
     cpuid(2, 1, words, 0);
-    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);
+    ci->cpu_family   = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX],  8, 12) + 1;
+    ci->cpu_model    = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX],  4,  8);
+    ci->cpu_stepping = BIT_EXTRACT_LE(words[PAL_CPUID_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
@@ -382,7 +376,7 @@ void _DkGetCPUInfo (PAL_CPU_INFO * ci)
         if (!cpu_flags[i])
             break;
 
-        if (BIT_EXTRACT_LE(words[WORD_EDX], i, i + 1)) {
+        if (BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EDX], i, i + 1)) {
             int len = strlen(cpu_flags[i]);
             if (flen + len + 1 > fmax) {
                 char * new_flags = malloc(fmax * 2);

+ 14 - 20
Pal/src/host/Linux-SGX/db_main.c

@@ -231,12 +231,6 @@ void pal_linux_main(const char ** arguments, const char ** environments,
 
 /* the following code is borrowed from CPUID */
 
-#define WORD_EAX  0
-#define WORD_EBX  1
-#define WORD_ECX  2
-#define WORD_EDX  3
-#define WORD_NUM  4
-
 static void cpuid (unsigned int leaf, unsigned int subleaf,
                    unsigned int words[])
 {
@@ -295,15 +289,15 @@ static char * cpu_flags[]
 
 void _DkGetCPUInfo (PAL_CPU_INFO * ci)
 {
-    unsigned int words[WORD_NUM];
+    unsigned int words[PAL_CPUID_WORD_NUM];
 
     const size_t VENDOR_ID_SIZE = 13;
     char* vendor_id = malloc(VENDOR_ID_SIZE);
     cpuid(0, 0, words);
 
-    FOUR_CHARS_VALUE(&vendor_id[0], words[WORD_EBX]);
-    FOUR_CHARS_VALUE(&vendor_id[4], words[WORD_EDX]);
-    FOUR_CHARS_VALUE(&vendor_id[8], words[WORD_ECX]);
+    FOUR_CHARS_VALUE(&vendor_id[0], words[PAL_CPUID_WORD_EBX]);
+    FOUR_CHARS_VALUE(&vendor_id[4], words[PAL_CPUID_WORD_EDX]);
+    FOUR_CHARS_VALUE(&vendor_id[8], words[PAL_CPUID_WORD_ECX]);
     vendor_id[VENDOR_ID_SIZE - 1] = '\0';
     ci->cpu_vendor = vendor_id;
     // Must be an Intel CPU
@@ -312,11 +306,11 @@ void _DkGetCPUInfo (PAL_CPU_INFO * ci)
     const size_t BRAND_SIZE = 49;
     char* brand = malloc(BRAND_SIZE);
     cpuid(0x80000002, 0, words);
-    memcpy(&brand[ 0], words, sizeof(unsigned int) * WORD_NUM);
+    memcpy(&brand[ 0], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
     cpuid(0x80000003, 0, words);
-    memcpy(&brand[16], words, sizeof(unsigned int) * WORD_NUM);
+    memcpy(&brand[16], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
     cpuid(0x80000004, 0, words);
-    memcpy(&brand[32], words, sizeof(unsigned int) * WORD_NUM);
+    memcpy(&brand[32], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
     brand[BRAND_SIZE - 1] = '\0';
     ci->cpu_brand = brand;
 
@@ -327,14 +321,14 @@ void _DkGetCPUInfo (PAL_CPU_INFO * ci)
      * 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);
+    ci->cpu_num      = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EBX], 0, 16);
 
     cpuid(1, 0, words);
-    ci->cpu_family   = BIT_EXTRACT_LE(words[WORD_EAX],  8, 12) +
-                       BIT_EXTRACT_LE(words[WORD_EAX], 20, 28);
-    ci->cpu_model    = BIT_EXTRACT_LE(words[WORD_EAX],  4,  8) +
-                      (BIT_EXTRACT_LE(words[WORD_EAX], 16, 20) << 4);
-    ci->cpu_stepping = BIT_EXTRACT_LE(words[WORD_EAX],  0,  4);
+    ci->cpu_family   = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX],  8, 12) +
+                       BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX], 20, 28);
+    ci->cpu_model    = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX],  4,  8) +
+                      (BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX], 16, 20) << 4);
+    ci->cpu_stepping = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX],  0,  4);
 
     int flen = 0, fmax = 80;
     char * flags = malloc(fmax);
@@ -343,7 +337,7 @@ void _DkGetCPUInfo (PAL_CPU_INFO * ci)
         if (!cpu_flags[i])
             continue;
 
-        if (BIT_EXTRACT_LE(words[WORD_EDX], i, i + 1)) {
+        if (BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EDX], i, i + 1)) {
             int len = strlen(cpu_flags[i]);
             if (flen + len + 1 > fmax) {
                 char * new_flags = malloc(fmax * 2);

+ 21 - 28
Pal/src/host/Linux/db_main.c

@@ -316,21 +316,14 @@ done_init:
 }
 
 /* the following code is borrowed from CPUID */
-
-#define WORD_EAX  0
-#define WORD_EBX  1
-#define WORD_ECX  2
-#define WORD_EDX  3
-#define WORD_NUM  4
-
-static void cpuid (unsigned int leaf, unsigned int subleaf,
-                   unsigned int words[])
+void cpuid (unsigned int leaf, unsigned int subleaf,
+            unsigned int words[])
 {
   __asm__ ("cpuid"
-      : "=a" (words[WORD_EAX]),
-        "=b" (words[WORD_EBX]),
-        "=c" (words[WORD_ECX]),
-        "=d" (words[WORD_EDX])
+      : "=a" (words[PAL_CPUID_WORD_EAX]),
+        "=b" (words[PAL_CPUID_WORD_EBX]),
+        "=c" (words[PAL_CPUID_WORD_ECX]),
+        "=d" (words[PAL_CPUID_WORD_EDX])
       : "a" (leaf),
         "c" (subleaf));
 }
@@ -387,26 +380,26 @@ static char * cpu_flags[]
 
 void _DkGetCPUInfo (PAL_CPU_INFO * ci)
 {
-    unsigned int words[WORD_NUM];
+    unsigned int words[PAL_CPUID_WORD_NUM];
 
     const size_t VENDOR_ID_SIZE = 13;
     char* vendor_id = malloc(VENDOR_ID_SIZE);
     cpuid(0, 0, words);
 
-    FOUR_CHARS_VALUE(&vendor_id[0], words[WORD_EBX]);
-    FOUR_CHARS_VALUE(&vendor_id[4], words[WORD_EDX]);
-    FOUR_CHARS_VALUE(&vendor_id[8], words[WORD_ECX]);
+    FOUR_CHARS_VALUE(&vendor_id[0], words[PAL_CPUID_WORD_EBX]);
+    FOUR_CHARS_VALUE(&vendor_id[4], words[PAL_CPUID_WORD_EDX]);
+    FOUR_CHARS_VALUE(&vendor_id[8], words[PAL_CPUID_WORD_ECX]);
     vendor_id[VENDOR_ID_SIZE - 1] = '\0';
     ci->cpu_vendor = vendor_id;
 
     const size_t BRAND_SIZE = 49;
     char* brand = malloc(BRAND_SIZE);
     cpuid(0x80000002, 0, words);
-    memcpy(&brand[ 0], words, sizeof(unsigned int) * WORD_NUM);
+    memcpy(&brand[ 0], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
     cpuid(0x80000003, 0, words);
-    memcpy(&brand[16], words, sizeof(unsigned int) * WORD_NUM);
+    memcpy(&brand[16], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
     cpuid(0x80000004, 0, words);
-    memcpy(&brand[32], words, sizeof(unsigned int) * WORD_NUM);
+    memcpy(&brand[32], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
     brand[BRAND_SIZE - 1] = '\0';
     ci->cpu_brand = brand;
 
@@ -419,23 +412,23 @@ void _DkGetCPUInfo (PAL_CPU_INFO * ci)
         * 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);
+        ci->cpu_num  = BIT_EXTRACT_LE(words[PAL_CPUID_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;
+        ci->cpu_num  = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX], 0, 8) + 1;
     } else {
         ci->cpu_num  = 1;
     }
 
     cpuid(1, 0, words);
-    ci->cpu_family   = BIT_EXTRACT_LE(words[WORD_EAX],  8, 12);
-    ci->cpu_model    = BIT_EXTRACT_LE(words[WORD_EAX],  4,  8);
-    ci->cpu_stepping = BIT_EXTRACT_LE(words[WORD_EAX],  0,  4);
+    ci->cpu_family   = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX],  8, 12);
+    ci->cpu_model    = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX],  4,  8);
+    ci->cpu_stepping = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX],  0,  4);
 
     if (!memcmp(vendor_id, "GenuineIntel", 12) ||
         !memcmp(vendor_id, "AuthenticAMD", 12)) {
-        ci->cpu_family += BIT_EXTRACT_LE(words[WORD_EAX], 20, 28);
-        ci->cpu_model  += BIT_EXTRACT_LE(words[WORD_EAX], 16, 20) << 4;
+        ci->cpu_family += BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX], 20, 28);
+        ci->cpu_model  += BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX], 16, 20) << 4;
     }
 
     int flen = 0, fmax = 80;
@@ -445,7 +438,7 @@ void _DkGetCPUInfo (PAL_CPU_INFO * ci)
         if (!cpu_flags[i])
             continue;
 
-        if (BIT_EXTRACT_LE(words[WORD_EDX], i, i + 1)) {
+        if (BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EDX], i, i + 1)) {
             int len = strlen(cpu_flags[i]);
             if (flen + len + 1 > fmax) {
                 char * new_flags = malloc(fmax * 2);

+ 2 - 1
Pal/src/host/Linux/db_misc.c

@@ -238,5 +238,6 @@ int _DkInstructionCacheFlush (const void * addr, int size)
 int _DkCpuIdRetrieve (unsigned int leaf, unsigned int subleaf,
                       unsigned int values[4])
 {
-    return -PAL_ERROR_NOTIMPLEMENTED;
+    cpuid(leaf, subleaf, values);
+    return 0;
 }

+ 1 - 0
Pal/src/host/Linux/pal.map

@@ -29,6 +29,7 @@ PAL {
 
         DkSystemTimeQuery; DkRandomBitsRead;
         DkInstructionCacheFlush;
+        DkCpuIdRetrieve;
         DkObjectClose;
         # objects checkpoint?
         # objects reload?

+ 2 - 0
Pal/src/host/Linux/pal_linux.h

@@ -163,6 +163,8 @@ int _DkMutexUnlock (struct mutex_handle * mut);
 void init_child_process (PAL_HANDLE * parent, PAL_HANDLE * exec,
                          PAL_HANDLE * manifest);
 
+void cpuid (unsigned int leaf, unsigned int subleaf,
+            unsigned int words[]);
 void signal_setup (void);
 
 unsigned long _DkSystemTimeQueryEarly (void);

+ 6 - 0
Pal/src/pal.h

@@ -506,6 +506,12 @@ DkPhysicalMemoryMap (PAL_HANDLE channel, PAL_NUM entries, PAL_PTR * addrs,
 
 PAL_NUM DkMemoryAvailableQuota (void);
 
+#define PAL_CPUID_WORD_EAX  0
+#define PAL_CPUID_WORD_EBX  1
+#define PAL_CPUID_WORD_ECX  2
+#define PAL_CPUID_WORD_EDX  3
+#define PAL_CPUID_WORD_NUM  4
+
 PAL_BOL
 DkCpuIdRetrieve (PAL_IDX leaf, PAL_IDX subleaf, PAL_IDX values[4]);