xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xen.org>
Cc: "Wei Liu" <wei.liu2@citrix.com>,
	"Andrew Cooper" <andrew.cooper3@citrix.com>,
	"David Wang" <davidwang@zhaoxin.com>,
	"Jan Beulich" <JBeulich@suse.com>, "Pu Wen" <puwen@hygon.cn>,
	"Roger Pau Monné" <roger.pau@citrix.com>
Subject: [PATCH 2/5] x86/cpu: Introduce x86_cpuid_vendor_to_str() and drop cpu_dev.c_vendor[]
Date: Thu, 4 Apr 2019 21:26:29 +0100	[thread overview]
Message-ID: <1554409592-28572-3-git-send-email-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <1554409592-28572-1-git-send-email-andrew.cooper3@citrix.com>

cpu_dev.c_vendor[] is a char[8] array which is printed using %s in two
locations.  This leads to subtle lack-of-NUL bugs when using an 8 character
vendor name.

Introduce x86_cpuid_vendor_to_str() to turn an x86_vendor into a printable
string, use it in the two locations that c_vendor is used, and drop c_vendor.

This drops the final user of X86_VENDOR_NUM, so drop that as well.

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: David Wang <davidwang@zhaoxin.com>
CC: Pu Wen <puwen@hygon.cn>

David: Same question as Wei asked.  Presuambly, " Shang" is a mistake, and
"Shanghai" is the correct term to use?
---
 xen/arch/x86/cpu/amd.c            |  1 -
 xen/arch/x86/cpu/centaur.c        |  1 -
 xen/arch/x86/cpu/common.c         | 11 +++--------
 xen/arch/x86/cpu/cpu.h            |  2 --
 xen/arch/x86/cpu/intel.c          |  1 -
 xen/arch/x86/cpu/shanghai.c       |  1 -
 xen/include/asm-x86/x86-vendors.h |  2 --
 xen/include/xen/lib/x86/cpuid.h   |  6 ++++++
 xen/lib/x86/cpuid.c               | 12 ++++++++++++
 9 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/xen/arch/x86/cpu/amd.c b/xen/arch/x86/cpu/amd.c
index d58952b..e19a5ea 100644
--- a/xen/arch/x86/cpu/amd.c
+++ b/xen/arch/x86/cpu/amd.c
@@ -793,7 +793,6 @@ static void init_amd(struct cpuinfo_x86 *c)
 }
 
 const struct cpu_dev amd_cpu_dev = {
-	.c_vendor	= "AMD",
 	.c_early_init	= early_init_amd,
 	.c_init		= init_amd,
 };
diff --git a/xen/arch/x86/cpu/centaur.c b/xen/arch/x86/cpu/centaur.c
index 268f4d4..34a5bfc 100644
--- a/xen/arch/x86/cpu/centaur.c
+++ b/xen/arch/x86/cpu/centaur.c
@@ -55,6 +55,5 @@ static void init_centaur(struct cpuinfo_x86 *c)
 }
 
 const struct cpu_dev centaur_cpu_dev = {
-	.c_vendor	= "Centaur",
 	.c_init		= init_centaur,
 };
diff --git a/xen/arch/x86/cpu/common.c b/xen/arch/x86/cpu/common.c
index 4cf9ec2..7cc45fe 100644
--- a/xen/arch/x86/cpu/common.c
+++ b/xen/arch/x86/cpu/common.c
@@ -102,7 +102,6 @@ static void default_init(struct cpuinfo_x86 * c)
 
 static const struct cpu_dev default_cpu = {
 	.c_init	= default_init,
-	.c_vendor = "Unknown",
 };
 static const struct cpu_dev *this_cpu = &default_cpu;
 
@@ -306,7 +305,7 @@ static void __init early_cpu_detect(void)
 
 	printk(XENLOG_INFO
 	       "CPU Vendor: %s, Family %u (%#x), Model %u (%#x), Stepping %u (raw %08x)\n",
-	       this_cpu->c_vendor, c->x86, c->x86,
+	       x86_cpuid_vendor_to_str(c->x86_vendor), c->x86, c->x86,
 	       c->x86_model, c->x86_model, c->x86_mask, eax);
 
 	eax = cpuid_eax(0x80000000);
@@ -661,12 +660,8 @@ void print_cpu_info(unsigned int cpu)
 
 	printk("CPU%u: ", cpu);
 
-	if (c->x86_vendor < X86_VENDOR_NUM)
-		vendor = this_cpu->c_vendor;
-	else
-		vendor = c->x86_vendor_id;
-
-	if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
+	vendor = x86_cpuid_vendor_to_str(c->x86_vendor);
+	if (strncmp(c->x86_model_id, vendor, strlen(vendor)))
 		printk("%s ", vendor);
 
 	if (!c->x86_model_id[0])
diff --git a/xen/arch/x86/cpu/cpu.h b/xen/arch/x86/cpu/cpu.h
index 62e4b03..54bd0d3 100644
--- a/xen/arch/x86/cpu/cpu.h
+++ b/xen/arch/x86/cpu/cpu.h
@@ -1,7 +1,5 @@
 /* attempt to consolidate cpu attributes */
 struct cpu_dev {
-	char	c_vendor[8];
-
 	void		(*c_early_init)(struct cpuinfo_x86 *c);
 	void		(*c_init)(struct cpuinfo_x86 * c);
 };
diff --git a/xen/arch/x86/cpu/intel.c b/xen/arch/x86/cpu/intel.c
index fcb3708..0dd8f98 100644
--- a/xen/arch/x86/cpu/intel.c
+++ b/xen/arch/x86/cpu/intel.c
@@ -349,7 +349,6 @@ static void init_intel(struct cpuinfo_x86 *c)
 }
 
 const struct cpu_dev intel_cpu_dev = {
-	.c_vendor	= "Intel",
 	.c_early_init	= early_init_intel,
 	.c_init		= init_intel,
 };
diff --git a/xen/arch/x86/cpu/shanghai.c b/xen/arch/x86/cpu/shanghai.c
index 189e13e..08a81f0 100644
--- a/xen/arch/x86/cpu/shanghai.c
+++ b/xen/arch/x86/cpu/shanghai.c
@@ -16,6 +16,5 @@ static void init_shanghai(struct cpuinfo_x86 *c)
 }
 
 const struct cpu_dev shanghai_cpu_dev = {
-    .c_vendor   = "  Shang",
     .c_init     = init_shanghai,
 };
diff --git a/xen/include/asm-x86/x86-vendors.h b/xen/include/asm-x86/x86-vendors.h
index 774ceac..fca7396 100644
--- a/xen/include/asm-x86/x86-vendors.h
+++ b/xen/include/asm-x86/x86-vendors.h
@@ -30,6 +30,4 @@
 #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 c7a3bff..022757f 100644
--- a/xen/include/xen/lib/x86/cpuid.h
+++ b/xen/include/xen/lib/x86/cpuid.h
@@ -71,6 +71,12 @@ static inline void cpuid_count_leaf(
  */
 unsigned int x86_cpuid_lookup_vendor(uint32_t ebx, uint32_t ecx, uint32_t edx);
 
+/**
+ * Given Xen's internal vendor ID, return a string suitable for printing.
+ * Returns "Unknown" for any unrecognised ID.
+ */
+const char *x86_cpuid_vendor_to_str(unsigned int vendor);
+
 #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 311d19e..23619c7 100644
--- a/xen/lib/x86/cpuid.c
+++ b/xen/lib/x86/cpuid.c
@@ -34,6 +34,18 @@ unsigned int x86_cpuid_lookup_vendor(uint32_t ebx, uint32_t ecx, uint32_t edx)
     return X86_VENDOR_UNKNOWN;
 }
 
+const char *x86_cpuid_vendor_to_str(unsigned int vendor)
+{
+    switch ( vendor )
+    {
+    case X86_VENDOR_INTEL:    return "Intel";
+    case X86_VENDOR_AMD:      return "AMD";
+    case X86_VENDOR_CENTAUR:  return "Centaur";
+    case X86_VENDOR_SHANGHAI: return "Shanghai";
+    default:                  return "Unknown";
+    }
+}
+
 /* Recalculate the content in a CPUID policy which is derived from raw data. */
 static void recalculate_synth(struct cpuid_policy *p)
 {
-- 
2.1.4


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

  parent reply	other threads:[~2019-04-04 20:26 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-04 20:26 [PATCH 0/5] x86/cpu: Rework of X86_VENDOR_* constants Andrew Cooper
2019-04-04 20:26 ` [PATCH 1/5] x86/cpu: Drop cpu_devs[] and $VENDOR_init_cpu() hooks Andrew Cooper
2019-04-05  8:57   ` Jan Beulich
2019-04-05  9:27     ` Andrew Cooper
2019-04-04 20:26 ` Andrew Cooper [this message]
2019-04-05  8:59   ` [PATCH 2/5] x86/cpu: Introduce x86_cpuid_vendor_to_str() and drop cpu_dev.c_vendor[] Jan Beulich
2019-04-04 20:26 ` [PATCH 3/5] x86/cpu: Renumber X86_VENDOR_* to form a bitmap Andrew Cooper
2019-04-05  9:03   ` Jan Beulich
2019-04-05  9:28     ` Andrew Cooper
2019-04-04 20:26 ` [PATCH 4/5] x86/cpu: Create Hygon Dhyana architecture support file Andrew Cooper
2019-04-05  9:17   ` Jan Beulich
2019-04-05 15:30     ` Pu Wen
2019-04-05 16:10       ` Jan Beulich
2019-04-04 20:26 ` [PATCH 5/5] x86/msr: Fix handling of MSR_AMD_PATCHLEVEL/MSR_IA32_UCODE_REV Andrew Cooper
2019-04-05  9:20   ` Jan Beulich

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=1554409592-28572-3-git-send-email-andrew.cooper3@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --cc=davidwang@zhaoxin.com \
    --cc=puwen@hygon.cn \
    --cc=roger.pau@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).