xen-ucode: print information about currently loaded ucode
v3 cover xen-ucode: print information about currently loaded ucode
For v3 I've removed the usage of xenhypfs and gone back to adding a new platform op.
Sergey Dyasli (3):
tools/xenctrl: add xc_get_cpu_version()
x86/platform: introduce XENPF_get_ucode_version tools/xen-ucode: print information about currently loaded ucode
tools/include/xenctrl.h | 3 ++ tools/libs/ctrl/xc_misc.c | 41 +++++++++++++++ tools/misc/xen-ucode.c | 66 ++++++++++++++++++++++++ xen/arch/x86/platform_hypercall.c | 30 +++++++++++ xen/arch/x86/x86_64/platform_hypercall.c | 4 ++ xen/include/public/platform.h | 12 +++++ xen/include/xlat.lst | 1 + 7 files changed, 157 insertions(+)
Signature · 1 line
2.17.1
PATCH v3 1/3 tools/xenctrl: add xc_get_cpu_version()
As a wrapper for XENPF_get_cpu_version platform op.
--- tools/include/xenctrl.h | 1 + tools/libs/ctrl/xc_misc.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+)
tools/include/xenctrl.h +1 -0
@@ -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 +20 -0
@@ -226,6 +226,26 @@ 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; +DECLARE_PLATFORM_OP ; + +if ( !xch || !cpu_ver ) +return -1 ; + + platform_op.cmd = XENPF_get_cpu_version; + platform_op.u.pcpu_version.xen_cpuid = cpu_ver->xen_cpuid; + + ret =do_platform_op (xch, &platform_op); +if ( ret !=0 ) +return ret; + + *cpu_ver = platform_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
Re: [PATCH v3 1/3] tools/xenctrl: add xc_get_cpu_version()
Quoted 5 lines · Sergey Dyasli
tools/libs/ctrl/xc_misc.c +0
-0
tools/libs/ctrl/xc_misc.c int xc_microcode_update(xc_interface *xch, const void *buf, size_t len) · line 235
Quoted 10 lines
tools/libs/ctrl/xc_misc.c +7
-0@@ -226,6 +226,26 @@ 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; +DECLARE_PLATFORM_OP ; + +if ( !xch || !cpu_ver ) +return -1 ;
We don't check parameters like this anywhere else. It's library code, and the caller is required to DTRT.
Also, we're phasing out the use of the DECLARE macros. This wants to change to
struct xen_platform_op op = {
.cmd = XENPF_get_cpu_version, .u.pcpu_version.xen_cpuid = cpu_ver->xen_cpuid, };
Both can be fixed on commit, if you're happy.
~Andrew
PATCH v3 2/3 x86/platform: introduce XENPF_get_ucode_version
In reply to Sergey DyasliCurrently it's impossible to get CPU's microcode revision 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.
--- tools/include/xenctrl.h | 2 ++ tools/libs/ctrl/xc_misc.c | 21 +++++++++++++++++ xen/arch/x86/platform_hypercall.c | 30 ++++++++++++++++++++++++ xen/arch/x86/x86_64/platform_hypercall.c | 4 ++++ xen/include/public/platform.h | 12 ++++++++++ xen/include/xlat.lst | 1 + 6 files changed, 70 insertions(+)
tools/include/xenctrl.h +2 -0
@@ -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_version (xc_interface *xch, +struct xenpf_ucode_version *ucode_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 +21 -0
@@ -246,6 +246,27 @@ int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver)return 0 ; } +int xc_get_ucode_version (xc_interface *xch, +struct xenpf_ucode_version *ucode_ver) +{ +int ret; +DECLARE_PLATFORM_OP ; + +if ( !xch || !ucode_ver ) +return -1 ; + + platform_op.cmd = XENPF_get_ucode_version; + platform_op.u.ucode_version.xen_cpuid = ucode_ver->xen_cpuid; + + ret =do_platform_op (xch, &platform_op); +if ( ret !=0 ) +return ret; + + *ucode_ver = platform_op.u.ucode_version; + +return 0 ; +} +int xc_cputopoinfo (xc_interface *xch,unsigned *max_cpus,xc_cputopo_t *cputopo) {
xen/arch/x86/platform_hypercall.c +30 -0
@@ -640,6 +640,36 @@ 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) ) + { + ret = -ENOENT ; + } +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/arch/x86/x86_64/platform_hypercall.c +4 -0
@@ -28,6 +28,10 @@ CHECK_pf_pcpuinfo; CHECK_pf_pcpu_version;#undef xen_pf_pcpu_version +#define xen_pf_ucode_version xenpf_ucode_version +CHECK_pf_ucode_version; +#undef xen_pf_pucode_version +#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 +12 -0
@@ -614,6 +614,17 @@ 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. */ +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*);
@@ -645,6 +656,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
@@ -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
Re: [PATCH v3 2/3] x86/platform: introduce XENPF_get_ucode_version
Quoted 2 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.
It's not impossible (you can do modprobe msr; rdmsr 0x8b), but it is inconvenient for library code.
Quoted 19 lines
Add a new platform op in order to get the required data from Xen and provide a wrapper for libxenctrl.
Signed-off-by Sergey Dyasli <[email protected]>--- tools/include/xenctrl.h | 2 ++ tools/libs/ctrl/xc_misc.c | 21 +++++++++++++++++ xen/arch/x86/platform_hypercall.c | 30 ++++++++++++++++++++++++ xen/arch/x86/x86_64/platform_hypercall.c | 4 ++++ xen/include/public/platform.h | 12 ++++++++++ xen/include/xlat.lst | 1 + 6 files changed, 70 insertions(+)tools/include/xenctrl.h +0
-0
tools/include/xenctrl.h int xc_cputopoinfo(xc_interface *xch, unsigned *max_cpus, · line 1191
Quoted 5 lines
tools/include/xenctrl.h +2
-0@@ -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_version (xc_interface *xch, +struct xenpf_ucode_version *ucode_ver);
Throughout, we should use "revision" rather than "version" as that is the terminology used by both Intel and AMD.
Quoted 12 lines
tools/include/xenctrl.h +0
-0int 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 +1
-0@@ -246,6 +246,27 @@ int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver)return 0 ; } +int xc_get_ucode_version(xc_interface *xch,
tools/libs/ctrl/xc_misc.c int xc_get_cpu_version(xc_interface *xch, struct xenpf_pcpu_version *cpu_ver) · line 259
Quoted 10 lines
tools/libs/ctrl/xc_misc.c +10
-0+ struct xenpf_ucode_version *ucode_ver) +{ +int ret; +DECLARE_PLATFORM_OP ; + +if ( !xch || !ucode_ver ) +return -1 ; + + platform_op.cmd = XENPF_get_ucode_version; + platform_op.u.ucode_version.xen_cpuid = ucode_ver->xen_cpuid;
Same notes as per patch 1.
xen/include/public/platform.h DEFINE_XEN_GUEST_HANDLE(xenpf_symdata_t); · line 619
Quoted 11 lines
xen/include/public/platform.h +3
-0@@ -614,6 +614,17 @@ 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. */
We commonly call this just "cpu". As a platform op pertaining to microcode, it cannot plausibly be confused with vcpus.
xen/include/public/platform.h DEFINE_XEN_GUEST_HANDLE(xenpf_symdata_t); · line 620
Quoted 1 line
xen/include/public/platform.h +1
-0+uint32_t cpu_signature;/* OUT: CPU signature (CPUID.1.EAX). */
The cpu_ prefix can be dropped, as can ...
xen/include/public/platform.h DEFINE_XEN_GUEST_HANDLE(xenpf_symdata_t); · line 623
Quoted 3 lines
xen/include/public/platform.h +3
-0+uint32_t pf;/* OUT: Processor Flags. */ +/* Only applicable to Intel. */ +uint32_t ucode_revision;/* OUT: Microcode Revision. */
the ucode_ prefix here.
I'm tempted to say that I'm happy to fix all of this on commit as it's all mechanical changes.
~Andrew
Re: [PATCH v3 2/3] x86/platform: introduce XENPF_get_ucode_version
In reply to Sergey DyasliQuoted 10 lines · Sergey Dyasli
xen/arch/x86/platform_hypercall.c +4
-0@@ -640,6 +640,36 @@ ret_t do_platform_op( }break ; +case XENPF_get_ucode_version: + { +struct xenpf_ucode_version *ver = &op->u.ucode_version; +
xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 656
Quoted 10 lines
xen/arch/x86/platform_hypercall.c +10
-0+if ( !get_cpu_maps () ) + { + ret = -EBUSY ; +break ; + } + +if ( (ver->xen_cpuid >= nr_cpu_ids) || !cpu_online (ver->xen_cpuid) ) + { + ret = -ENOENT ; + }
Nit: Unnecessary braces.
xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 664
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; + }
While I don't mean to insist on making this work right in this patch, I'd like to re-raise my earlier comment regarding it also being of possible interest to know ucode revisions for parked CPUs (they are, after all, an active entity in the system). Parked CPUs have their per-CPU data retained, so getting at that data shouldn't be overly difficult (the main aspect being to tell parked from fully offline CPUs).
Jan
PATCH v3 3/3 tools/xen-ucode: print information about currently loaded ucode
In reply to Sergey DyasliAdd an option to xen-ucode tool to print the currently loaded ucode version and also print it during usage info. Print CPU signature and processor flags as well. The raw data comes from XENPF_get_cpu_version and XENPF_get_ucode_version platform ops.
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--- tools/misc/xen-ucode.c | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
tools/misc/xen-ucode.c +66 -0
@@ -12,6 +12,65 @@#include <fcntl.h> #include <xenctrl.h> +static const char intel_id[] ="GenuineIntel" ; +static const char amd_id[] ="AuthenticAMD" ; + +static void show_curr_cpu (FILE *f) +{ +int ret; +xc_interface *xch; +struct xenpf_pcpu_version cpu_ver = { .xen_cpuid =0 }; +struct xenpf_ucode_version ucode_ver = { .xen_cpuid =0 }; +bool intel = false, amd = false; + + xch =xc_interface_open (0 ,0 ,0 ); +if ( xch ==NULL ) +return ; + + ret =xc_get_cpu_version (xch, &cpu_ver); +if ( ret ) +return ; + + ret =xc_get_ucode_version (xch, &ucode_ver); +if ( ret ) +return ; + +if (memcmp (cpu_ver.vendor_id, intel_id, +sizeof (cpu_ver.vendor_id)) ==0 ) + intel = true; +else if (memcmp (cpu_ver.vendor_id, amd_id, +sizeof (cpu_ver.vendor_id)) ==0 ) + amd = true; + +/* +* 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 ( intel ) + { +fprintf (f,"Current CPU signature is: %02x-%02x-%02x (raw %#x)\n" , + cpu_ver.family, cpu_ver.model, cpu_ver.stepping, + ucode_ver.cpu_signature); + } +else if ( amd ) + { +fprintf (f,"Current CPU signature is: fam%xh (raw %#x)\n" , + cpu_ver.family, ucode_ver.cpu_signature); + } + +if ( intel || amd ) +fprintf (f,"Current CPU microcode revision is: %#x\n" , + ucode_ver.ucode_revision); + +if ( intel ) +fprintf (f,"Current CPU processor flags are: %#x\n" , ucode_ver.pf); + +xc_interface_close (xch); +} +int main (int argc,char *argv[]) {int fd, ret;
@@ -25,9 +84,16 @@ int main(int argc, char *argv[])fprintf (stderr,"xen-ucode: Xen microcode updating tool\n" "Usage: %s <microcode blob>\n" , argv[0 ]); +show_curr_cpu (stderr);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 )
Signature · 1 line
2.17.1
Re: [PATCH v3 3/3] tools/xen-ucode: print information about currently loaded ucode
Quoted 10 lines · Sergey Dyasli
Add an option to xen-ucode tool to print the currently loaded ucode version and also print it during usage info. Print CPU signature and processor flags as well. The raw data comes from
XENPF_get_cpu_versionandXENPF_get_ucode_versionplatform ops.Example output:
Intel: Current CPU signature is: 06-55-04 (raw 0x50654) Current CPU microcode revision is: 0x2006e05 Current CPU processor flags are: 0x1
It's platform flags, not processor flags. (And sadly, doesn't actually capture all the platform specific data these days either...)
Quoted 4 lines
AMD:
Current CPU signature is: fam19h (raw 0xa00f11)
Current CPU microcode revision is: 0xa0011a8This is unnecessarily verbose, and can fit into a single line.
CPU signature {XX-YY-ZZ / FamXXh} (raw X) {pf Y} revision Z.
Quoted 15 lines
Signed-off-by Sergey Dyasli <[email protected]>--- tools/misc/xen-ucode.c | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)tools/misc/xen-ucode.c +2
-0@@ -12,6 +12,65 @@#include <fcntl.h> #include <xenctrl.h> +static const char intel_id[] ="GenuineIntel" ; +static const char amd_id[] ="AuthenticAMD" ;
tools/misc/xen-ucode.c line 26
Quoted 10 lines
tools/misc/xen-ucode.c +10
-0+ +static void show_curr_cpu (FILE *f) +{ +int ret; +xc_interface *xch; +struct xenpf_pcpu_version cpu_ver = { .xen_cpuid =0 }; +struct xenpf_ucode_version ucode_ver = { .xen_cpuid =0 }; +bool intel = false, amd = false; + + xch =xc_interface_open (0 ,0 ,0 );
NULL, NULL, 0
but xch wants to be a global and opened once, rather than in each piece of sub-functionality.
tools/misc/xen-ucode.c line 36
Quoted 10 lines
tools/misc/xen-ucode.c +10
-0+if ( xch ==NULL ) +return ; + + ret =xc_get_cpu_version (xch, &cpu_ver); +if ( ret ) +return ; + + ret =xc_get_ucode_version (xch, &ucode_ver); +if ( ret ) +return ;
All 3 of these want to complain, rather than exiting silently.
See test-tsx/test-resource as examples. It's fine to use err(1, "message") to terminate fairly cleanly, and it renders errno to the user which is far more useful than printing nothing and exiting success.
tools/misc/xen-ucode.c line 43
Quoted 7 lines
tools/misc/xen-ucode.c +7
-0+ +if (memcmp (cpu_ver.vendor_id, intel_id, +sizeof (cpu_ver.vendor_id)) ==0 ) + intel = true; +else if (memcmp (cpu_ver.vendor_id, amd_id, +sizeof (cpu_ver.vendor_id)) ==0 ) + amd = true;
else some kind of error, again so we don't exit silently.
tools/misc/xen-ucode.c line 50
Quoted 7 lines
tools/misc/xen-ucode.c +7
-0+ +/* +* 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
I'm not sure if this is relevant any more, but for Fam < 0x15, everything is combined in microcode_amd.bin
In some copious free time (but not this patch), it might be nice to support xen-ucode --auto to try and pick the right firmware file out of the filesystem. One less thing for end users to get wrong.
Quoted 6 lines
tools/misc/xen-ucode.c +6
-0+*/ +if ( intel ) + { +fprintf (f,"Current CPU signature is: %02x-%02x-%02x (raw %#x)\n" , + cpu_ver.family, cpu_ver.model, cpu_ver.stepping, + ucode_ver.cpu_signature);
tools/misc/xen-ucode.c line 66
Quoted 10 lines
tools/misc/xen-ucode.c +10
-0+ } +else if ( amd ) + { +fprintf (f,"Current CPU signature is: fam%xh (raw %#x)\n" , + cpu_ver.family, ucode_ver.cpu_signature); + } + +if ( intel || amd ) +fprintf (f,"Current CPU microcode revision is: %#x\n" , + ucode_ver.ucode_revision);
For the two raw fields, and the revision field, it's important to use 0x08x.
They're all exactly 32bit quantities, and both vendors encode data in the higher bits, so visually it's very useful to know if you're looking at the top of penultimate nibble.
~Andrew