Skip to contentSkip to conversation

tools/xen-ucode: print information about currently loaded ucode

PATCH Superseded Sergey Dyasli 2 participants 2 messages Jan 13, 2023 – Jan 16, 2023

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

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

Currently it's impossible to get CPU's microcode revision after late loading without looking into Xen logs which is not always convenient. Add an option to xen-ucode tool to print the currently loaded ucode version and also print it during usage info.

Add a new platform op in order to get the required data from Xen. Print CPU signature and processor flags as well.

Example output:

    Intel:
    Current CPU signature is: 06-55-04 (raw 0x50654)
    Current CPU microcode revision is: 0x2006e05
    Current CPU processor flags are: 0x1

    AMD:
    Current CPU signature is: fam19h (raw 0xa00f11)
    Current CPU microcode revision is: 0xa0011a8

---
CC: Wei Liu <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: "Roger Pau Monné" <[email protected]>

---
 tools/include/xenctrl.h           |  1 +
 tools/libs/ctrl/xc_misc.c         |  5 +++
 tools/misc/xen-ucode.c            | 68 +++++++++++++++++++++++++++++++
 xen/arch/x86/platform_hypercall.c | 32 +++++++++++++++
 xen/include/public/platform.h     | 14 +++++++
 xen/include/xlat.lst              |  1 +
 6 files changed, 121 insertions(+)
tools/include/xenctrl.h +1 -0
index 23037874d3..e9911da5ea 100644
@@ -1185,6 +1185,7 @@ typedef uint32_t xc_node_to_node_dist_t;
 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_platform_op(xc_interface *xch, struct xen_platform_op *op);
 int xc_microcode_update(xc_interface *xch, const void *buf, size_t len);
 int xc_numainfo(xc_interface *xch, unsigned *max_nodes,
                 xc_meminfo_t *meminfo, uint32_t *distance);
tools/libs/ctrl/xc_misc.c +5 -0
index 265f15ec2d..d03c240d14 100644
@@ -226,6 +226,11 @@ int xc_microcode_update(xc_interface *xch, const void *buf, size_t len)
     return ret;
 }
 
+int xc_platform_op(xc_interface *xch, struct xen_platform_op *op)
+{
+    return do_platform_op(xch, op);
+}
+
 int xc_cputopoinfo(xc_interface *xch, unsigned *max_cpus,
                    xc_cputopo_t *cputopo)
 {
tools/misc/xen-ucode.c +68 -0

85 lines not on this page · raw message

xen/arch/x86/platform_hypercall.c +32 -0
index 08ab2fea62..fcb7d81db1 100644
@@ -640,6 +640,38 @@ ret_t do_platform_op(
     }
     break;
 
+    case XENPF_get_ucode_version:
+    {
+        struct xenpf_ucode_version *ver = &op->u.ucode_version;
+
+        if ( !get_cpu_maps() )
+        {
+            ret = -EBUSY;
+            break;
+        }
+
+        if ( (ver->xen_cpuid >= nr_cpu_ids) || !cpu_online(ver->xen_cpuid) )
+        {
+            ver->cpu_signature = 0;
+            ver->pf = 0;
+            ver->ucode_revision = 0;
+        }
+        else
+        {
+            const struct cpu_signature *sig = &per_cpu(cpu_sig, ver->xen_cpuid);
+
+            ver->cpu_signature = sig->sig;
+            ver->pf = sig->pf;
+            ver->ucode_revision = sig->rev;
+        }
+
+        put_cpu_maps();
+
+        if ( __copy_field_to_guest(u_xenpf_op, op, u.ucode_version) )
+            ret = -EFAULT;
+    }
+    break;
+
     case XENPF_cpu_online:
     {
         int cpu = op->u.cpu_ol.cpuid;
xen/include/public/platform.h +14 -0
index 14784dfa77..1a24dc24c0 100644
@@ -610,6 +610,19 @@ 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_version 65
+struct xenpf_ucode_version {
+    uint32_t xen_cpuid;       /* IN:  CPU number to get the revision from.  */
+                              /*      Return data should be equal among all */
+                              /*      the CPUs.                             */
+    uint32_t cpu_signature;   /* OUT: CPU signature (CPUID.1.EAX).          */
+    uint32_t pf;              /* OUT: Processor Flags.                      */
+                              /*      Only applicable to Intel.             */
+    uint32_t ucode_revision;  /* OUT: Microcode Revision.                   */
+};
+typedef struct xenpf_ucode_version xenpf_ucode_version_t;
+DEFINE_XEN_GUEST_HANDLE(xenpf_ucode_version_t);
+
 /*
  * ` enum neg_errnoval
  * ` HYPERVISOR_platform_op(const struct xen_platform_op*);
@@ -641,6 +654,7 @@ struct xen_platform_op {
         xenpf_resource_op_t           resource_op;
         xenpf_symdata_t               symdata;
         xenpf_dom0_console_t          dom0_console;
+        xenpf_ucode_version_t         ucode_version;
         uint8_t                       pad[128];
     } u;
 };
xen/include/xlat.lst +1 -0
index d601a8a984..164f700eb6 100644
@@ -157,6 +157,7 @@
 ?	xenpf_pcpuinfo			platform.h
 ?	xenpf_pcpu_version		platform.h
 ?	xenpf_resource_entry		platform.h
+?	xenpf_ucode_version		platform.h
 ?	pmu_data			pmu.h
 ?	pmu_params			pmu.h
 !	sched_poll			sched.h
Signature · 1 line
2.17.1
#1 lore raw
Quoted 8 lines · Sergey Dyasli

Currently it's impossible to get CPU's microcode revision after late loading without looking into Xen logs which is not always convenient. Add an option to xen-ucode tool to print the currently loaded ucode version and also print it during usage info.

Add a new platform op in order to get the required data from Xen. Print CPU signature and processor flags as well.

Quoted 8 lines

Example output:

    Intel:
    Current CPU signature is: 06-55-04 (raw 0x50654)
    Current CPU microcode revision is: 0x2006e05
    Current CPU processor flags are: 0x1

    AMD:
    Current CPU signature is: fam19h (raw 0xa00f11)

So quite a bit less precise information than on Intel in the non-raw part. Is there a reason for this?

tools/libs/ctrl/xc_misc.c int xc_microcode_update(xc_interface *xch, const void *buf, size_t len) · line 232

Quoted 10 lines
tools/libs/ctrl/xc_misc.c +4 -0
@@ -226,6 +226,11 @@ int xc_microcode_update(xc_interface *xch, const void *buf, size_t len)
     return ret;
 }
 
+int xc_platform_op(xc_interface *xch, struct xen_platform_op *op)
+{
+    return do_platform_op(xch, op);
+}

Wouldn't it make sense to simply rename do_platform_op()?

tools/misc/xen-ucode.c line 16

Quoted 8 lines
tools/misc/xen-ucode.c +2 -0
@@ -12,6 +12,67 @@
 #include <fcntl.h>
 #include <xenctrl.h>
 
+static const char *intel_id = "GenuineIntel";
+static const char *amd_id   = "AuthenticAMD";

Do these need to be (non-const) pointers, rather than const char[]?

tools/misc/xen-ucode.c line 22

Quoted 5 lines
tools/misc/xen-ucode.c +5 -0
+void show_curr_cpu(FILE *f)
+{
+    int ret;
+    xc_interface *xch;
+    struct xen_platform_op op_cpu = {0}, op_ucode = {0};

Instead of the dummy initializers, can't you make ...

tools/misc/xen-ucode.c line 33

Quoted 11 lines
tools/misc/xen-ucode.c +11 -0
+    struct xenpf_pcpu_version *cpu_ver = &op_cpu.u.pcpu_version;
+    struct xenpf_ucode_version *ucode_ver = &op_ucode.u.ucode_version;
+    bool intel = false, amd = false;
+
+    xch = xc_interface_open(0, 0, 0);
+    if ( xch == NULL )
+        return;
+
+    op_cpu.cmd = XENPF_get_cpu_version;
+    op_cpu.interface_version = XENPF_INTERFACE_VERSION;
+    op_cpu.u.pcpu_version.xen_cpuid = 0;

... this and ...

tools/misc/xen-ucode.c line 41

Quoted 7 lines
tools/misc/xen-ucode.c +7 -0
+    ret = xc_platform_op(xch, &op_cpu);
+    if ( ret )
+        return;
+
+    op_ucode.cmd = XENPF_get_ucode_version;
+    op_ucode.interface_version = XENPF_INTERFACE_VERSION;
+    op_ucode.u.pcpu_version.xen_cpuid = 0;

... this the initializers?

Quoted 8 lines
tools/misc/xen-ucode.c +4 -0
@@ -20,11 +81,18 @@ int main(int argc, char *argv[])
     struct stat st;
     xc_interface *xch;
 
+    if ( argc >= 2 && !strcmp(argv[1], "show-cpu-info") )
+    {
+        show_curr_cpu(stdout);
+        return 0;

tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 97

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

Personally I'd find it mode logical if this remained first and you inserted your new fragment right afterwards. That way you also don't need to check argc twice.

Quoted 11 lines
xen/arch/x86/platform_hypercall.c +5 -0
@@ -640,6 +640,38 @@ ret_t do_platform_op(
     }
     break;
 
+    case XENPF_get_ucode_version:
+    {
+        struct xenpf_ucode_version *ver = &op->u.ucode_version;
+
+        if ( !get_cpu_maps() )

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

Quoted 10 lines
xen/arch/x86/platform_hypercall.c +10 -0
+        {
+            ret = -EBUSY;
+            break;
+        }
+
+        if ( (ver->xen_cpuid >= nr_cpu_ids) || !cpu_online(ver->xen_cpuid) )
+        {
+            ver->cpu_signature = 0;
+            ver->pf = 0;
+            ver->ucode_revision = 0;

Better return -ENOENT in this case?

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

Quoted 8 lines
xen/arch/x86/platform_hypercall.c +8 -0
+        }
+        else
+        {
+            const struct cpu_signature *sig = &per_cpu(cpu_sig, ver->xen_cpuid);
+
+            ver->cpu_signature = sig->sig;
+            ver->pf = sig->pf;
+            ver->ucode_revision = sig->rev;

Here you read what is actually present, which ...

xen/include/public/platform.h DEFINE_XEN_GUEST_HANDLE(xenpf_symdata_t); · line 617

Quoted 11 lines
xen/include/public/platform.h +5 -0
@@ -610,6 +610,19 @@ 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_version 65
+struct xenpf_ucode_version {
+    uint32_t xen_cpuid;       /* IN:  CPU number to get the revision from.  */
+                              /*      Return data should be equal among all */
+                              /*      the CPUs.                             */

... doesn't necessarily match the promise here. Perhaps weaken the "should", or clarify what the conditionsare for this to be the case? Also your addition to xen-ucode builds on this, which can easily end up misleading when it's not really the case.

xen/include/xlat.lst line 160

Quoted 7 lines
xen/include/xlat.lst +1 -0
@@ -157,6 +157,7 @@
 ?	xenpf_pcpuinfo			platform.h
 ?	xenpf_pcpu_version		platform.h
 ?	xenpf_resource_entry		platform.h
+?	xenpf_ucode_version		platform.h

You also want to invoke the resulting macro, so that the intended checking actually occurs.

Jan