All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xen.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Sergey Dyasli" <sergey.dyasli@citrix.com>,
	"Wei Liu" <wei.liu2@citrix.com>,
	"Jan Beulich" <JBeulich@suse.com>,
	"Roger Pau Monné" <roger.pau@citrix.com>
Subject: [PATCH 1/4] libx86: Introduce x86_cpuid_lookup_vendor()
Date: Thu, 21 Mar 2019 12:21:03 +0000	[thread overview]
Message-ID: <1553170866-23812-2-git-send-email-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <1553170866-23812-1-git-send-email-andrew.cooper3@citrix.com>

Also introduce constants for the vendor strings in CPUID leaf 0.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: Wei Liu <wei.liu2@citrix.com>
CC: Roger Pau Monné <roger.pau@citrix.com>
CC: Sergey Dyasli <sergey.dyasli@citrix.com>
---
 tools/tests/cpu-policy/test-cpu-policy.c | 37 ++++++++++++++++++++++++++++++++
 xen/include/asm-x86/x86-vendors.h        | 21 ++++++++++++++++++
 xen/include/xen/lib/x86/cpuid.h          |  6 ++++++
 xen/lib/x86/cpuid.c                      | 32 +++++++++++++++++++++++++++
 xen/lib/x86/private.h                    |  1 +
 5 files changed, 97 insertions(+)

diff --git a/tools/tests/cpu-policy/test-cpu-policy.c b/tools/tests/cpu-policy/test-cpu-policy.c
index d13963e..beced5e 100644
--- a/tools/tests/cpu-policy/test-cpu-policy.c
+++ b/tools/tests/cpu-policy/test-cpu-policy.c
@@ -8,6 +8,7 @@
 #include <err.h>
 
 #include <xen-tools/libs.h>
+#include <xen/asm/x86-vendors.h>
 #include <xen/lib/x86/cpuid.h>
 #include <xen/lib/x86/msr.h>
 #include <xen/domctl.h>
@@ -19,6 +20,40 @@ static unsigned int nr_failures;
     printf(fmt, ##__VA_ARGS__);                 \
 })
 
+static void test_vendor_identification(void)
+{
+    static const struct test {
+        union {
+            char ident[12];
+            struct {
+                uint32_t b, d, c;
+            };
+        };
+        unsigned int vendor;
+    } tests[] = {
+        { { "GenuineIntel" }, X86_VENDOR_INTEL },
+        { { "AuthenticAMD" }, X86_VENDOR_AMD },
+        { { "CentaurHauls" }, X86_VENDOR_CENTAUR },
+        { { "  Shanghai  " }, X86_VENDOR_SHANGHAI },
+
+        { { ""             }, X86_VENDOR_UNKNOWN },
+        { { "            " }, X86_VENDOR_UNKNOWN },
+        { { "xxxxxxxxxxxx" }, X86_VENDOR_UNKNOWN },
+    };
+
+    printf("Testing CPU vendor identification:\n");
+
+    for ( size_t i = 0; i < ARRAY_SIZE(tests); ++i )
+    {
+        const struct test *t = &tests[i];
+        unsigned int vendor = x86_cpuid_lookup_vendor(t->b, t->c, t->d);
+
+        if ( vendor != t->vendor )
+            fail("  Test '%.12s', expected vendor %u, got %u\n",
+                 t->ident, t->vendor, vendor);
+    }
+}
+
 static void test_cpuid_serialise_success(void)
 {
     static const struct test {
@@ -243,6 +278,8 @@ int main(int argc, char **argv)
 {
     printf("CPU Policy unit tests\n");
 
+    test_vendor_identification();
+
     test_cpuid_serialise_success();
     test_msr_serialise_success();
 
diff --git a/xen/include/asm-x86/x86-vendors.h b/xen/include/asm-x86/x86-vendors.h
index 38a81c3..774ceac 100644
--- a/xen/include/asm-x86/x86-vendors.h
+++ b/xen/include/asm-x86/x86-vendors.h
@@ -3,12 +3,33 @@
 
 /*
  * CPU vendor IDs
+ *
+ * - X86_VENDOR_* are Xen-internal identifiers.  Values and order are
+ *   arbitrary.
+ * - X86_VENDOR_*_E?X are architectural information from CPUID leaf 0
  */
 #define X86_VENDOR_UNKNOWN 0
+
 #define X86_VENDOR_INTEL 1
+#define X86_VENDOR_INTEL_EBX 0x756e6547U /* "GenuineIntel" */
+#define X86_VENDOR_INTEL_ECX 0x6c65746eU
+#define X86_VENDOR_INTEL_EDX 0x49656e69U
+
 #define X86_VENDOR_AMD 2
+#define X86_VENDOR_AMD_EBX 0x68747541U /* "AuthenticAMD" */
+#define X86_VENDOR_AMD_ECX 0x444d4163U
+#define X86_VENDOR_AMD_EDX 0x69746e65U
+
 #define X86_VENDOR_CENTAUR 3
+#define X86_VENDOR_CENTAUR_EBX 0x746e6543U /* "CentaurHauls" */
+#define X86_VENDOR_CENTAUR_ECX 0x736c7561U
+#define X86_VENDOR_CENTAUR_EDX 0x48727561U
+
 #define X86_VENDOR_SHANGHAI 4
+#define X86_VENDOR_SHANGHAI_EBX 0x68532020U /* "  Shanghai  " */
+#define X86_VENDOR_SHANGHAI_ECX 0x20206961U
+#define X86_VENDOR_SHANGHAI_EDX 0x68676e61U
+
 #define X86_VENDOR_NUM 5
 
 #endif	/* __XEN_X86_VENDORS_H__ */
diff --git a/xen/include/xen/lib/x86/cpuid.h b/xen/include/xen/lib/x86/cpuid.h
index 95b37b6..f392c78 100644
--- a/xen/include/xen/lib/x86/cpuid.h
+++ b/xen/include/xen/lib/x86/cpuid.h
@@ -65,6 +65,12 @@ static inline void cpuid_count_leaf(
 #undef BX_CON
 #undef XCHG
 
+/**
+ * Given the vendor id from CPUID leaf 0, look up Xen's internal integer
+ * vendor ID.  Returns X86_VENDOR_UNKNOWN for any unknown vendor.
+ */
+unsigned int x86_cpuid_lookup_vendor(uint32_t ebx, uint32_t ecx, uint32_t edx);
+
 #define CPUID_GUEST_NR_BASIC      (0xdu + 1)
 #define CPUID_GUEST_NR_CACHE      (5u + 1)
 #define CPUID_GUEST_NR_FEAT       (0u + 1)
diff --git a/xen/lib/x86/cpuid.c b/xen/lib/x86/cpuid.c
index 6c60ba8..104a867 100644
--- a/xen/lib/x86/cpuid.c
+++ b/xen/lib/x86/cpuid.c
@@ -2,6 +2,38 @@
 
 #include <xen/lib/x86/cpuid.h>
 
+unsigned int x86_cpuid_lookup_vendor(uint32_t ebx, uint32_t ecx, uint32_t edx)
+{
+    switch ( ebx )
+    {
+    case X86_VENDOR_INTEL_EBX:
+        if ( ecx == X86_VENDOR_INTEL_ECX &&
+             edx == X86_VENDOR_INTEL_EDX )
+            return X86_VENDOR_INTEL;
+        break;
+
+    case X86_VENDOR_AMD_EBX:
+        if ( ecx == X86_VENDOR_AMD_ECX &&
+             edx == X86_VENDOR_AMD_EDX )
+            return X86_VENDOR_AMD;
+        break;
+
+    case X86_VENDOR_CENTAUR_EBX:
+        if ( ecx == X86_VENDOR_CENTAUR_ECX &&
+             edx == X86_VENDOR_CENTAUR_EDX )
+            return X86_VENDOR_CENTAUR;
+        break;
+
+    case X86_VENDOR_SHANGHAI_EBX:
+        if ( ecx == X86_VENDOR_SHANGHAI_ECX &&
+             edx == X86_VENDOR_SHANGHAI_EDX )
+            return X86_VENDOR_SHANGHAI;
+        break;
+    }
+
+    return X86_VENDOR_UNKNOWN;
+}
+
 void x86_cpuid_policy_fill_native(struct cpuid_policy *p)
 {
     unsigned int i;
diff --git a/xen/lib/x86/private.h b/xen/lib/x86/private.h
index 6fb5022..f5b195e 100644
--- a/xen/lib/x86/private.h
+++ b/xen/lib/x86/private.h
@@ -23,6 +23,7 @@
 #include <stddef.h>
 
 #include <xen/asm/msr-index.h>
+#include <xen/asm/x86-vendors.h>
 
 #include <xen-tools/libs.h>
 
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  reply	other threads:[~2019-03-21 12:21 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-21 12:21 [PATCH 0/4] x86/cpuid: Handling of synthetic cpuid_policy fields Andrew Cooper
2019-03-21 12:21 ` Andrew Cooper [this message]
2019-03-26 11:52   ` [PATCH 1/4] libx86: Introduce x86_cpuid_lookup_vendor() Jan Beulich
2019-03-26 13:11     ` Andrew Cooper
2019-03-26 14:07       ` Jan Beulich
2019-03-26 14:23         ` Juergen Gross
2019-03-26 14:39           ` Jan Beulich
2019-03-27 15:10             ` Andrew Cooper
     [not found]           ` <5C9A39B00200007800221F23@suse.com>
2019-03-26 14:47             ` Juergen Gross
2019-03-26 15:23               ` Jan Beulich
2019-03-21 12:21 ` [PATCH 2/4] x86/cpuid: Drop get_cpu_vendor() completely Andrew Cooper
2019-03-26 12:08   ` Jan Beulich
2019-03-26 16:41     ` Andrew Cooper
2019-03-21 12:21 ` [PATCH 3/4] tools/libxc: Use x86_cpuid_lookup_vendor() rather than opencoding the logic Andrew Cooper
2019-03-26 12:09   ` Jan Beulich
2019-03-21 12:21 ` [PATCH 4/4] libx86: Recalculate synthesised cpuid_policy fields when appropriate Andrew Cooper
2019-03-26 12:20   ` Jan Beulich
2019-03-26 12:34     ` Andrew Cooper
     [not found] <1553170866*23812*1*git*send*email*andrew.cooper3@citrix.com>
     [not found] ` <1553170866*23812*2*git*send*email*andrew.cooper3@citrix.com>
     [not found]   ` <5C9A12690200007800221E05@prv1*mh.provo.novell.com>
     [not found]     ` <c5d1df3d*038d*8c0a*97da*a71f8f3fd009@citrix.com>

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1553170866-23812-2-git-send-email-andrew.cooper3@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --cc=roger.pau@citrix.com \
    --cc=sergey.dyasli@citrix.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.