]> git.ozlabs.org Git - ccan/blobdiff - ccan/cpuid/cpuid.c
cpuid: better parser for processor info
[ccan] / ccan / cpuid / cpuid.c
index 026875dcae86b3188115b5fc3513e6a78dfbf6ff..bc44fcf2f9d7c264071cbc3cf67cf2e31022670c 100644 (file)
@@ -29,6 +29,7 @@
 #include "cpuid.h"
 
 #include <string.h>
+#include <stdio.h>
 
 enum {
        CPU_PROC_BRAND_STRING_INTERNAL0                 = 0x80000003,
@@ -92,6 +93,8 @@ static struct {
 
 bool cpuid_is_supported(void)
 {
+       int ret = 0;
+#if defined(__GNUC__) || defined(__clang__)
        /* The following assembly code uses EAX as the return value,
         * but we store the value of EAX into ret since GCC uses EAX
         * as the return register for every C function.  That's a double
@@ -124,7 +127,6 @@ bool cpuid_is_supported(void)
 #define ASM_PUSHECX    "pushl %%ecx\n\t"
 #endif
 
-       int ret = 0;
        asm volatile(
                ASM_PUSHF
                ASM_POPEAX
@@ -147,7 +149,26 @@ bool cpuid_is_supported(void)
 #undef ASM_PUSHEAX
 #undef ASM_POPEAX
 #undef ASM_PUSHECX
-
+#elif defined _MSC_VER
+       __asm {
+               pushfd
+               pop eax
+               mov ecx, eax
+               xor eax, 0x200000
+               push eax
+               popfd
+
+               pushfd
+               pop eax
+               xor eax, ecx
+               shr eax, 0x21
+               and eax, 0x1
+               push ecx
+               popfd
+
+               mov eax, ret
+       };
+#endif
        return !!ret;
 }
 
@@ -235,11 +256,19 @@ uint32_t cpuid_highest_ext_func_supported(void)
        static uint32_t highest;
 
        if (!highest) {
+#if defined(__GNUC__) || defined(__clang__)
                asm volatile(
                        "cpuid\n\t"
                        : "=a" (highest)
                        : "a" (CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED)
                );
+#elif defined _MSC_VER
+               __asm {
+                       mov eax, CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED
+                       cpuid
+                       mov highest, eax
+               };
+#endif
        }
 
        return highest;
@@ -281,10 +310,15 @@ void cpuid(cpuid_t info, uint32_t *buf)
                        buf[2] = ecx;
                        break;
                case CPU_PROCINFO_AND_FEATUREBITS:
-                       buf[0] = eax;   /* The so called "signature" of the CPU.  */
-                       buf[1] = edx;   /* Feature flags #1.  */
-                       buf[2] = ecx;   /* Feature flags #2.  */
-                       buf[3] = ebx;   /* Additional feature information.  */
+                       buf[0] = (eax & 0x0F);          /* Stepping  */
+                       buf[1] = (eax >> 4)  & 0x0F;    /* Model  */
+                       buf[2] = (eax >> 8)  & 0x0F;    /* Family  */
+                       buf[3] = (eax >> 16) & 0x0F;    /* Extended Model.  */
+                       buf[4] = (eax >> 24) & 0x0F;    /* Extended Family.  */
+
+                       buf[5] = edx;                   /* Feature flags #1.  */
+                       buf[6] = ecx;                   /* Feature flags #2.  */
+                       buf[7] = ebx;                   /* Additional feature information.  */
                        break;
                case CPU_CACHE_AND_TLBD_INFO:
                        buf[0] = eax;