Skip to contentSkip to conversation

xen-ucode: print information about currently loaded ucode

PATCH Superseded Sergey Dyasli 3 participants 10 messages Apr 4, 2023 – Apr 6, 2023

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover xen-ucode: print information about currently loaded ucode

Posting v4 with addressed review comments. Changes from v3 are available in each patch.

Sergey Dyasli (3):
tools/xenctrl: add xc_get_cpu_version()
x86/platform: introduce XENPF_get_ucode_revision tools/xen-ucode: print information about currently loaded ucode

 tools/include/xenctrl.h                  |  3 +
 tools/libs/ctrl/xc_misc.c                | 35 ++++++++++
 tools/misc/xen-ucode.c                   | 83 ++++++++++++++++++++----
 xen/arch/x86/platform_hypercall.c        | 29 +++++++++
 xen/arch/x86/x86_64/platform_hypercall.c |  4 ++
 xen/include/public/platform.h            | 11 ++++
 xen/include/xlat.lst                     |  1 +
 7 files changed, 154 insertions(+), 12 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v4 1/3 tools/xenctrl: add xc_get_cpu_version()

As a wrapper for XENPF_get_cpu_version platform op.

---
v3 --> v4:
- Replaced DECLARE_PLATFORM_OP
- Removed NULL checks

---
 tools/include/xenctrl.h   |  1 +
 tools/libs/ctrl/xc_misc.c | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)
tools/include/xenctrl.h +1 -0
index 05967ecc92..34b3b25289 100644
@@ -1186,6 +1186,7 @@ int xc_physinfo(xc_interface *xch, xc_physinfo_t *info);
 int xc_cputopoinfo(xc_interface *xch, unsigned *max_cpus,
                    xc_cputopo_t *cputopo);
 int xc_microcode_update(xc_interface *xch, const void *buf, size_t len);
+int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver);
 int xc_numainfo(xc_interface *xch, unsigned *max_nodes,
                 xc_meminfo_t *meminfo, uint32_t *distance);
 int xc_pcitopoinfo(xc_interface *xch, unsigned num_devs,
tools/libs/ctrl/xc_misc.c +17 -0
index 265f15ec2d..90d50faa4f 100644
@@ -226,6 +226,23 @@ int xc_microcode_update(xc_interface *xch, const void *buf, size_t len)
     return ret;
 }
 
+int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver)
+{
+    int ret;
+    struct xen_platform_op op = {
+        .cmd = XENPF_get_cpu_version,
+        .u.pcpu_version.xen_cpuid = cpu_ver->xen_cpuid,
+    };
+
+    ret = do_platform_op(xch, &op);
+    if ( ret != 0 )
+        return ret;
+
+    *cpu_ver = op.u.pcpu_version;
+
+    return 0;
+}
+
 int xc_cputopoinfo(xc_interface *xch, unsigned *max_cpus,
                    xc_cputopo_t *cputopo)
 {
Signature · 1 line
2.17.1
#3 lore raw

PATCH v4 2/3 x86/platform: introduce XENPF_get_ucode_revision

In reply to Sergey Dyasli

Currently it's impossible to get CPU's microcode revision from Xen after late loading without looking into Xen logs which is not always convenient.

Add a new platform op in order to get the required data from Xen and provide a wrapper for libxenctrl.

---
v3 --> v4:
- clarified the commit message
- Renamed "ucode version" to "ucode revision"
- Removed DECLARE_PLATFORM_OP and NULL checks
- Added a TODO comment about parked CPUs
- Renamed struct xenpf_ucode_revision fields

---
 tools/include/xenctrl.h                  |  2 ++
 tools/libs/ctrl/xc_misc.c                | 18 +++++++++++++++
 xen/arch/x86/platform_hypercall.c        | 29 ++++++++++++++++++++++++
 xen/arch/x86/x86_64/platform_hypercall.c |  4 ++++
 xen/include/public/platform.h            | 11 +++++++++
 xen/include/xlat.lst                     |  1 +
 6 files changed, 65 insertions(+)
tools/include/xenctrl.h +2 -0
index 34b3b25289..1149f805ba 100644
@@ -1187,6 +1187,8 @@ int xc_cputopoinfo(xc_interface *xch, unsigned *max_cpus,
                    xc_cputopo_t *cputopo);
 int xc_microcode_update(xc_interface *xch, const void *buf, size_t len);
 int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver);
+int xc_get_ucode_revision(xc_interface *xch,
+                          struct xenpf_ucode_revision *ucode_rev);
 int xc_numainfo(xc_interface *xch, unsigned *max_nodes,
                 xc_meminfo_t *meminfo, uint32_t *distance);
 int xc_pcitopoinfo(xc_interface *xch, unsigned num_devs,
tools/libs/ctrl/xc_misc.c +18 -0
index 90d50faa4f..4159294b2e 100644
@@ -243,6 +243,24 @@ int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver)
     return 0;
 }
 
+int xc_get_ucode_revision(xc_interface *xch,
+                          struct xenpf_ucode_revision *ucode_rev)
+{
+    int ret;
+    struct xen_platform_op op = {
+        .cmd = XENPF_get_ucode_revision,
+        .u.ucode_revision.cpu = ucode_rev->cpu,
+    };
+
+    ret = do_platform_op(xch, &op);
+    if ( ret != 0 )
+        return ret;
+
+    *ucode_rev = op.u.ucode_revision;
+
+    return 0;
+}
+
 int xc_cputopoinfo(xc_interface *xch, unsigned *max_cpus,
                    xc_cputopo_t *cputopo)
 {
xen/arch/x86/platform_hypercall.c +29 -0
index a2d9526355..9ff2da8fc3 100644
@@ -640,6 +640,35 @@ ret_t do_platform_op(
     }
     break;
 
+    case XENPF_get_ucode_revision:
+    {
+        struct xenpf_ucode_revision *rev = &op->u.ucode_revision;
+
+        if ( !get_cpu_maps() )
+        {
+            ret = -EBUSY;
+            break;
+        }
+
+        /* TODO: make it possible to know ucode revisions for parked CPUs */
+        if ( (rev->cpu >= nr_cpu_ids) || !cpu_online(rev->cpu) )
+            ret = -ENOENT;
+        else
+        {
+            const struct cpu_signature *sig = &per_cpu(cpu_sig, rev->cpu);
+
+            rev->signature = sig->sig;
+            rev->pf = sig->pf;
+            rev->revision = sig->rev;
+        }
+
+        put_cpu_maps();
+
+        if ( __copy_field_to_guest(u_xenpf_op, op, u.ucode_revision) )
+            ret = -EFAULT;
+    }
+    break;
+
     case XENPF_cpu_online:
     {
         int cpu = op->u.cpu_ol.cpuid;
xen/arch/x86/x86_64/platform_hypercall.c +4 -0
index 5bf6b958d2..99440f4076 100644
@@ -28,6 +28,10 @@ CHECK_pf_pcpuinfo;
 CHECK_pf_pcpu_version;
 #undef xen_pf_pcpu_version
 
+#define xen_pf_ucode_revision xenpf_ucode_revision
+CHECK_pf_ucode_revision;
+#undef xen_pf_pucode_revision
+
 #define xen_pf_enter_acpi_sleep xenpf_enter_acpi_sleep
 CHECK_pf_enter_acpi_sleep;
 #undef xen_pf_enter_acpi_sleep
xen/include/public/platform.h +11 -0
index 60caa5ce7e..15777b5416 100644
@@ -614,6 +614,16 @@ DEFINE_XEN_GUEST_HANDLE(xenpf_symdata_t);
 typedef struct dom0_vga_console_info xenpf_dom0_console_t;
 DEFINE_XEN_GUEST_HANDLE(xenpf_dom0_console_t);
 
+#define XENPF_get_ucode_revision 65
+struct xenpf_ucode_revision {
+    uint32_t cpu;             /* IN:  CPU number to get the revision from.  */
+    uint32_t signature;       /* OUT: CPU signature (CPUID.1.EAX).          */
+    uint32_t pf;              /* OUT: Platform Flags (Intel only)           */
+    uint32_t revision;        /* OUT: Microcode Revision.                   */
+};
+typedef struct xenpf_ucode_revision xenpf_ucode_revision_t;
+DEFINE_XEN_GUEST_HANDLE(xenpf_ucode_revision_t);
+
 /*
  * ` enum neg_errnoval
  * ` HYPERVISOR_platform_op(const struct xen_platform_op*);
@@ -645,6 +655,7 @@ struct xen_platform_op {
         xenpf_resource_op_t           resource_op;
         xenpf_symdata_t               symdata;
         xenpf_dom0_console_t          dom0_console;
+        xenpf_ucode_revision_t        ucode_revision;
         uint8_t                       pad[128];
     } u;
 };
xen/include/xlat.lst +1 -0
index d601a8a984..9c41948514 100644
@@ -157,6 +157,7 @@
 ?	xenpf_pcpuinfo			platform.h
 ?	xenpf_pcpu_version		platform.h
 ?	xenpf_resource_entry		platform.h
+?	xenpf_ucode_revision		platform.h
 ?	pmu_data			pmu.h
 ?	pmu_params			pmu.h
 !	sched_poll			sched.h
Signature · 1 line
2.17.1
Reviewed-by on v4 2/3
#4 lore raw

Re: [PATCH v4 2/3] x86/platform: introduce XENPF_get_ucode_revision

Quoted 7 lines · Sergey Dyasli

Currently it's impossible to get CPU's microcode revision from Xen after late loading without looking into Xen logs which is not always convenient.

Add a new platform op in order to get the required data from Xen and provide a wrapper for libxenctrl.

with two remarks:

Quoted 8 lines
tools/libs/ctrl/xc_misc.c +2 -0
@@ -243,6 +243,24 @@ int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver)
     return 0;
 }
 
+int xc_get_ucode_revision(xc_interface *xch,
+                          struct xenpf_ucode_revision *ucode_rev)

tools/libs/ctrl/xc_misc.c int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver) · line 257

Quoted 10 lines
tools/libs/ctrl/xc_misc.c +10 -0
+{
+    int ret;
+    struct xen_platform_op op = {
+        .cmd = XENPF_get_ucode_revision,
+        .u.ucode_revision.cpu = ucode_rev->cpu,
+    };
+
+    ret = do_platform_op(xch, &op);
+    if ( ret != 0 )
+        return ret;

Is there anything wrong with omitting this if() and ...

tools/libs/ctrl/xc_misc.c int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver) · line 261

Quoted 3 lines
tools/libs/ctrl/xc_misc.c +3 -0
+    *ucode_rev = op.u.ucode_revision;
+
+    return 0;

... using "return ret" here?

Quoted 9 lines
xen/arch/x86/platform_hypercall.c +3 -0
@@ -640,6 +640,35 @@ ret_t do_platform_op(
     }
     break;
 
+    case XENPF_get_ucode_revision:
+    {
+        struct xenpf_ucode_revision *rev = &op->u.ucode_revision;

xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 655

Quoted 10 lines
xen/arch/x86/platform_hypercall.c +10 -0
+
+        if ( !get_cpu_maps() )
+        {
+            ret = -EBUSY;
+            break;
+        }
+
+        /* TODO: make it possible to know ucode revisions for parked CPUs */
+        if ( (rev->cpu >= nr_cpu_ids) || !cpu_online(rev->cpu) )
+            ret = -ENOENT;

While the cpu_online() check needs to be done under lock, it's kind of misleading for the caller to tell it to try again later when it has passed an out-of-range CPU number.

Jan

#5 lore raw

Re: [PATCH v4 2/3] x86/platform: introduce XENPF_get_ucode_revision

Quoted 21 lines · Jan Beulich
Quoted 8 lines · Sergey Dyasli
tools/libs/ctrl/xc_misc.c +2 -0
@@ -243,6 +243,24 @@ int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver)
     return 0;
 }
 
+int xc_get_ucode_revision(xc_interface *xch,
+                          struct xenpf_ucode_revision *ucode_rev)

tools/libs/ctrl/xc_misc.c int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver) · line 257

Quoted 10 lines
tools/libs/ctrl/xc_misc.c +10 -0
+{
+    int ret;
+    struct xen_platform_op op = {
+        .cmd = XENPF_get_ucode_revision,
+        .u.ucode_revision.cpu = ucode_rev->cpu,
+    };
+
+    ret = do_platform_op(xch, &op);
+    if ( ret != 0 )
+        return ret;

Is there anything wrong with omitting this if() and ...

Quoted 4 lines

tools/libs/ctrl/xc_misc.c int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver) · line 261

Quoted 3 lines
tools/libs/ctrl/xc_misc.c +3 -0
+    *ucode_rev = op.u.ucode_revision;
+
+    return 0;

... using "return ret" here?

Conceptually, yes.  *ucode_rev oughtn't to be written to on failure.

More importantly though, what Sergey wrote is consistent with the vast majority of libxc, and consistency is far more important than a marginal perf improvement which you won't be able to measure.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/platform_hypercall.c +0 -0
@@ -640,6 +640,35 @@ ret_t do_platform_op(
     }
     break;
 
Quoted 16 lines

xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 655

Quoted 13 lines
xen/arch/x86/platform_hypercall.c +13 -0
+    case XENPF_get_ucode_revision:
+    {
+        struct xenpf_ucode_revision *rev = &op->u.ucode_revision;
+
+        if ( !get_cpu_maps() )
+        {
+            ret = -EBUSY;
+            break;
+        }
+
+        /* TODO: make it possible to know ucode revisions for parked CPUs */
+        if ( (rev->cpu >= nr_cpu_ids) || !cpu_online(rev->cpu) )
+            ret = -ENOENT;

While the cpu_online() check needs to be done under lock, it's kind of misleading for the caller to tell it to try again later when it has passed an out-of-range CPU number.

Honestly, I think you over-estimate the likelihood of the cpu map being contended, and over-estimate by 100% the chances that an out-of-range CPU is going to be passed.

~Andrew

#6 lore raw

Re: [PATCH v4 2/3] x86/platform: introduce XENPF_get_ucode_revision

Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Jan Beulich
Quoted 8 lines · Sergey Dyasli
tools/libs/ctrl/xc_misc.c +2 -0
@@ -243,6 +243,24 @@ int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver)
     return 0;
 }
 
+int xc_get_ucode_revision(xc_interface *xch,
+                          struct xenpf_ucode_revision *ucode_rev)

tools/libs/ctrl/xc_misc.c int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver) · line 257

Quoted 10 lines
tools/libs/ctrl/xc_misc.c +10 -0
+{
+    int ret;
+    struct xen_platform_op op = {
+        .cmd = XENPF_get_ucode_revision,
+        .u.ucode_revision.cpu = ucode_rev->cpu,
+    };
+
+    ret = do_platform_op(xch, &op);
+    if ( ret != 0 )
+        return ret;

Is there anything wrong with omitting this if() and ...

Quoted 3 lines
tools/libs/ctrl/xc_misc.c +3 -0
+    *ucode_rev = op.u.ucode_revision;
+
+    return 0;
Quoted 7 lines
Quoted 1 line

... using "return ret" here?

Conceptually, yes.  *ucode_rev oughtn't to be written to on failure.

More importantly though, what Sergey wrote is consistent with the vast majority of libxc, and consistency is far more important than a marginal perf improvement which you won't be able to measure.

My remark was entirely unrelated to performance, and instead solely to (source) code size.

Jan

#7 lore raw

PATCH v4 3/3 tools/xen-ucode: print information about currently loaded ucode

In reply to Sergey Dyasli

Add an option to xen-ucode tool to print the currently loaded ucode revision and also print it during usage info. Print CPU signature and platform flags as well. The raw data comes from XENPF_get_cpu_version and XENPF_get_ucode_revision platform ops.

Example output:

    Intel:
    CPU signature 06-55-04 (raw 0x00050654) pf 0x1 revision 0x02006e05

    AMD:
    CPU signature fam19h (raw 0x00a00f11) revision 0x0a0011ce

---
v3 --> v4:
- changed the output to be 1-line long
- made xc_interface *xch global
- added error checking to xc calls
- added error for unsupported CPU vendor
- changed printf format to 0x%08x for raw signature and revision values

---
 tools/misc/xen-ucode.c | 83 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 71 insertions(+), 12 deletions(-)
tools/misc/xen-ucode.c +71 -12
index ad32face2b..bd0bfaaa00 100644
@@ -12,22 +12,89 @@
 #include <fcntl.h>
 #include <xenctrl.h>
 
+static xc_interface *xch;
+
+static const char intel_id[] = "GenuineIntel";
+static const char   amd_id[] = "AuthenticAMD";
+
+static void show_curr_cpu(FILE *f)
+{
+    int ret;
+    struct xenpf_pcpu_version cpu_ver = { .xen_cpuid = 0 };
+    struct xenpf_ucode_revision ucode_rev = { .cpu = 0 };
+
+    ret = xc_get_cpu_version(xch, &cpu_ver);
+    if ( ret )
+    {
+        fprintf(f, "Failed to get CPU information. (err: %s)\n",
+                strerror(errno));
+        exit(1);
+    }
+
+    ret = xc_get_ucode_revision(xch, &ucode_rev);
+    if ( ret )
+    {
+        fprintf(f, "Failed to get microcode information. (err: %s)\n",
+                strerror(errno));
+        exit(1);
+    }
+
+    /*
+     * Print signature in a form that allows to quickly identify which ucode
+     * blob to load, e.g.:
+     *
+     *      Intel:   /lib/firmware/intel-ucode/06-55-04
+     *      AMD:     /lib/firmware/amd-ucode/microcode_amd_fam19h.bin
+     */
+    if ( memcmp(cpu_ver.vendor_id, intel_id,
+                sizeof(cpu_ver.vendor_id)) == 0 )
+    {
+        fprintf(f, "CPU signature %02x-%02x-%02x (raw 0x%08x) pf %#x revision 0x%08x\n",
+                   cpu_ver.family, cpu_ver.model, cpu_ver.stepping,
+                   ucode_rev.signature, ucode_rev.pf, ucode_rev.revision);
+    }
+    else if ( memcmp(cpu_ver.vendor_id, amd_id,
+                     sizeof(cpu_ver.vendor_id)) == 0 )
+    {
+        fprintf(f, "CPU signature fam%xh (raw 0x%08x) revision 0x%08x\n",
+                   cpu_ver.family, ucode_rev.signature, ucode_rev.revision);
+    }
+    else
+    {
+        fprintf(f, "Unsupported CPU vendor: %s\n", cpu_ver.vendor_id);
+        exit(3);
+    }
+}
+
 int main(int argc, char *argv[])
 {
     int fd, ret;
     char *filename, *buf;
     size_t len;
     struct stat st;
-    xc_interface *xch;
+
+    xch = xc_interface_open(NULL, NULL, 0);
+    if ( xch == NULL )
+    {
+        fprintf(stderr, "Error opening xc interface. (err: %s)\n",
+                strerror(errno));
+        exit(1);
+    }
 
     if ( argc < 2 )
     {
-        fprintf(stderr,
-                "xen-ucode: Xen microcode updating tool\n"
-                "Usage: %s <microcode blob>\n", argv[0]);
+        fprintf(stderr, "xen-ucode: Xen microcode updating tool\n");
+        show_curr_cpu(stderr);
+        fprintf(stderr, "Usage: %s <microcode blob>\n", argv[0]);
         exit(2);
     }
 
+    if ( !strcmp(argv[1], "show-cpu-info") )
+    {
+        show_curr_cpu(stdout);
+        return 0;
+    }
+
     filename = argv[1];
     fd = open(filename, O_RDONLY);
     if ( fd < 0 )
@@ -52,14 +119,6 @@ int main(int argc, char *argv[])
         exit(1);
     }
 
-    xch = xc_interface_open(NULL, NULL, 0);
-    if ( xch == NULL )
-    {
-        fprintf(stderr, "Error opening xc interface. (err: %s)\n",
-                strerror(errno));
-        exit(1);
-    }
-
     ret = xc_microcode_update(xch, buf, len);
     if ( ret )
     {
Signature · 1 line
2.17.1
#8 lore raw

Re: [PATCH v4 3/3] tools/xen-ucode: print information about currently loaded ucode

Quoted 11 lines · Sergey Dyasli

Add an option to xen-ucode tool to print the currently loaded ucode revision and also print it during usage info. Print CPU signature and platform flags as well. The raw data comes from XENPF_get_cpu_version and XENPF_get_ucode_revision platform ops.

Example output:

    Intel:
    CPU signature 06-55-04 (raw 0x00050654) pf 0x1 revision 0x02006e05

    AMD:
    CPU signature fam19h (raw 0x00a00f11) revision 0x0a0011ce

Wouldn't it make sense to also report the model number here, even if ucode blob file names (currently) don't include it?

While for the revision always printing 8 digits is definitely helpful, I wonder whether the two top nibbles of the raw value wouldn't better be omitted. (I understand Andrew did ask for this, but it's unclear to me why he extended this request to the raw fields.)

Quoted 6 lines
tools/misc/xen-ucode.c +0 -0
@@ -12,22 +12,89 @@
 #include <fcntl.h>
 #include <xenctrl.h>
 

tools/misc/xen-ucode.c line 24

Quoted 10 lines
tools/misc/xen-ucode.c +10 -0
+static xc_interface *xch;
+
+static const char intel_id[] = "GenuineIntel";
+static const char   amd_id[] = "AuthenticAMD";
+
+static void show_curr_cpu(FILE *f)
+{
+    int ret;
+    struct xenpf_pcpu_version cpu_ver = { .xen_cpuid = 0 };
+    struct xenpf_ucode_revision ucode_rev = { .cpu = 0 };

As mentioned before - the current state of the system may be inconsistent, so I question the entire lack of a way to know of this by using this tool (even if via a specific command line option, defaulting to CPU0-only).

tools/misc/xen-ucode.c line 31

Quoted 6 lines
tools/misc/xen-ucode.c +6 -0
+    ret = xc_get_cpu_version(xch, &cpu_ver);
+    if ( ret )
+    {
+        fprintf(f, "Failed to get CPU information. (err: %s)\n",
+                strerror(errno));
+        exit(1);

Errors want to go to stderr, I would say (just like is already the case in main()).

Quoted 13 lines
tools/misc/xen-ucode.c +13 -0
+    }
+
+    ret = xc_get_ucode_revision(xch, &ucode_rev);
+    if ( ret )
+    {
+        fprintf(f, "Failed to get microcode information. (err: %s)\n",
+                strerror(errno));
+        exit(1);
+    }
+
+    /*
+     * Print signature in a form that allows to quickly identify which ucode
+     * blob to load, e.g.:

tools/misc/xen-ucode.c line 54

Quoted 10 lines
tools/misc/xen-ucode.c +10 -0
+     *
+     *      Intel:   /lib/firmware/intel-ucode/06-55-04
+     *      AMD:     /lib/firmware/amd-ucode/microcode_amd_fam19h.bin
+     */
+    if ( memcmp(cpu_ver.vendor_id, intel_id,
+                sizeof(cpu_ver.vendor_id)) == 0 )
+    {
+        fprintf(f, "CPU signature %02x-%02x-%02x (raw 0x%08x) pf %#x revision 0x%08x\n",
+                   cpu_ver.family, cpu_ver.model, cpu_ver.stepping,
+                   ucode_rev.signature, ucode_rev.pf, ucode_rev.revision);

Nit: Indentation (also below). I think you mean

        fprintf(f,
                "CPU signature %02x-%02x-%02x (raw 0x%08x) pf %#x revision 0x%08x\n",
                cpu_ver.family, cpu_ver.model, cpu_ver.stepping,
                ucode_rev.signature, ucode_rev.pf, ucode_rev.revision);
Quoted 27 lines
tools/misc/xen-ucode.c +20 -1
+    }
+    else if ( memcmp(cpu_ver.vendor_id, amd_id,
+                     sizeof(cpu_ver.vendor_id)) == 0 )
+    {
+        fprintf(f, "CPU signature fam%xh (raw 0x%08x) revision 0x%08x\n",
+                   cpu_ver.family, ucode_rev.signature, ucode_rev.revision);
+    }
+    else
+    {
+        fprintf(f, "Unsupported CPU vendor: %s\n", cpu_ver.vendor_id);
+        exit(3);
+    }
+}
+
 int main(int argc, char *argv[])
 {
     int fd, ret;
     char *filename, *buf;
     size_t len;
     struct stat st;
-    xc_interface *xch;
+
+    xch = xc_interface_open(NULL, NULL, 0);
+    if ( xch == NULL )
+    {
+        fprintf(stderr, "Error opening xc interface. (err: %s)\n",
+                strerror(errno));

tools/misc/xen-ucode.c line 87

Quoted 10 lines
tools/misc/xen-ucode.c +4 -3
+        exit(1);
+    }
 
     if ( argc < 2 )
     {
-        fprintf(stderr,
-                "xen-ucode: Xen microcode updating tool\n"
-                "Usage: %s <microcode blob>\n", argv[0]);
+        fprintf(stderr, "xen-ucode: Xen microcode updating tool\n");
+        show_curr_cpu(stderr);

I recall you had it this way before, but I don't see why this information needs to be part of argument error handling. Furthermore, considering the possibility of hitting an error path in show_curr_cpu(), I think ...

tools/misc/xen-ucode.c line 88

Quoted 1 line
tools/misc/xen-ucode.c +1 -0
+        fprintf(stderr, "Usage: %s <microcode blob>\n", argv[0]);

... this would need printing first, and perhaps ...

tools/misc/xen-ucode.c line 89

Quoted 1 line
tools/misc/xen-ucode.c +0 -0
         exit(2);

... the exit code also shouldn't be anything other than 2 in that event.

Jan

#9 lore raw

Re: [PATCH v4 3/3] tools/xen-ucode: print information about currently loaded ucode

Quoted 6 lines · Jan Beulich
Quoted 5 lines · Sergey Dyasli

Add an option to xen-ucode tool to print the currently loaded ucode revision and also print it during usage info. Print CPU signature and platform flags as well. The raw data comes from XENPF_get_cpu_version and XENPF_get_ucode_revision platform ops.

Quoted 8 lines
Quoted 6 lines

Example output:

    Intel:
    CPU signature 06-55-04 (raw 0x00050654) pf 0x1 revision 0x02006e05

    AMD:
    CPU signature fam19h (raw 0x00a00f11) revision 0x0a0011ce

Wouldn't it make sense to also report the model number here, even if ucode blob file names (currently) don't include it?

I have debated FF-MM-SS on AMD several times.

AMD do document all their CPUs consistently in hex, and I know the model tables alarmingly well now.  Furthermore, it's not a straight nibble shuffle from the raw value.

So yeah, it probably is worth including.  Given that we're not guessing the Linux path of the microcode, it can replace the fam19h without any loss of information.

Quoted 4 lines

While for the revision always printing 8 digits is definitely helpful, I wonder whether the two top nibbles of the raw value wouldn't better be omitted. (I understand Andrew did ask for this, but it's unclear to me why he extended this request to the raw fields.)

For precisely the same reason.  (With a question like this, you clearly don't work frequently with microcode crossing a wide range of CPUs...)

Quoted 6 lines
Quoted 6 lines
tools/misc/xen-ucode.c +0 -0
@@ -12,22 +12,89 @@
 #include <fcntl.h>
 #include <xenctrl.h>
 
Quoted 14 lines

tools/misc/xen-ucode.c line 24

Quoted 10 lines
tools/misc/xen-ucode.c +10 -0
+static xc_interface *xch;
+
+static const char intel_id[] = "GenuineIntel";
+static const char   amd_id[] = "AuthenticAMD";
+
+static void show_curr_cpu(FILE *f)
+{
+    int ret;
+    struct xenpf_pcpu_version cpu_ver = { .xen_cpuid = 0 };
+    struct xenpf_ucode_revision ucode_rev = { .cpu = 0 };

As mentioned before - the current state of the system may be inconsistent, so I question the entire lack of a way to know of this by using this tool (even if via a specific command line option, defaulting to CPU0-only).

It's a theoretical problem, not a practical one, and definitely not something worthy to block this patch with.

Software always has more up-to-date microcode than firmware these days, and it has probably been a decade since there was a system released which could usefully function with asymmetric microcode.

I know we seen it in the past, but only on truly ancient systems.

Quoted 6 lines

tools/misc/xen-ucode.c line 31

Quoted 6 lines
tools/misc/xen-ucode.c +6 -0
+    ret = xc_get_cpu_version(xch, &cpu_ver);
+    if ( ret )
+    {
+        fprintf(f, "Failed to get CPU information. (err: %s)\n",
+                strerror(errno));
+        exit(1);

Use err(), which removes all the boilerplate here.

Quoted 30 lines
Quoted 30 lines
tools/misc/xen-ucode.c +22 -1
+    }
+    else if ( memcmp(cpu_ver.vendor_id, amd_id,
+                     sizeof(cpu_ver.vendor_id)) == 0 )
+    {
+        fprintf(f, "CPU signature fam%xh (raw 0x%08x) revision 0x%08x\n",
+                   cpu_ver.family, ucode_rev.signature, ucode_rev.revision);
+    }
+    else
+    {
+        fprintf(f, "Unsupported CPU vendor: %s\n", cpu_ver.vendor_id);
+        exit(3);
+    }
+}
+
 int main(int argc, char *argv[])
 {
     int fd, ret;
     char *filename, *buf;
     size_t len;
     struct stat st;
-    xc_interface *xch;
+
+    xch = xc_interface_open(NULL, NULL, 0);
+    if ( xch == NULL )
+    {
+        fprintf(stderr, "Error opening xc interface. (err: %s)\n",
+                strerror(errno));
+        exit(1);
+    }
 
Quoted 9 lines

tools/misc/xen-ucode.c line 87

Quoted 7 lines
tools/misc/xen-ucode.c +2 -3
     if ( argc < 2 )
     {
-        fprintf(stderr,
-                "xen-ucode: Xen microcode updating tool\n"
-                "Usage: %s <microcode blob>\n", argv[0]);
+        fprintf(stderr, "xen-ucode: Xen microcode updating tool\n");
+        show_curr_cpu(stderr);

I recall you had it this way before, but I don't see why this information needs to be part of argument error handling.

Because it is specifically useful information to be given when running xen-ucode with no arguments.

But also because this patch predates Spectre/Meltdown going public, and is documented this way for XenServer.

That said, the information does need correcting because it's changed to be:

  "Usage: %s [show-cpu-info | <microcode file>]"

in this patch.  (and file is specifically more relevant than calling it a blob).

~Andrew