x86/xen-ucode: Introduce --force option
cover x86/xen-ucode: Introduce --force option
Refactor and introduce --force option to xen-ucode, which skips microcode version check when updating x86 CPU micocode. A new hypercall introduced with flags field to facilitate the new option and allow for future flags as needed.
Fouad Hilly (5):
x86: Remove x86 low level version check of microcode
x86: Refactor microcode_update() hypercall with flags field
x86: Add usage() to print out usage message
x86: Use getopt to handle command line args
x86: Add --force option to xen-ucode to override microcode version
check
tools/include/xenctrl.h | 2 +- tools/libs/ctrl/xc_misc.c | 12 ++++- tools/misc/xen-ucode.c | 69 +++++++++++++++++++++++----- xen/arch/x86/cpu/microcode/amd.c | 8 +--- xen/arch/x86/cpu/microcode/core.c | 12 +++-- xen/arch/x86/cpu/microcode/intel.c | 11 ++--- xen/arch/x86/include/asm/microcode.h | 2 +- xen/arch/x86/platform_hypercall.c | 12 ++++- xen/include/public/platform.h | 8 ++++ 9 files changed, 102 insertions(+), 34 deletions(-)
Signature · 1 line
2.42.0
PATCH 1/5 x86: Remove x86 low level version check of microcode
Remove microcode version check at Intel and AMD Level. Microcode version check will be at higher and common level.
--- xen/arch/x86/cpu/microcode/amd.c | 8 ++------ xen/arch/x86/cpu/microcode/intel.c | 11 +++-------- 2 files changed, 5 insertions(+), 14 deletions(-)
xen/arch/x86/cpu/microcode/amd.c +2 -6
@@ -383,12 +383,8 @@ static struct microcode_patch *cf_check cpu_request_microcode( goto skip; } -/* -* If the new ucode covers current CPU, compare ucodes and store the -* one with higher revision. -*/ -if ( (microcode_fits (mc->patch) !=MIS_UCODE ) && - (!saved || (compare_header (mc->patch, saved) ==NEW_UCODE )) ) +/* If the provided ucode covers current CPU, then store its revision. */ +if ( (microcode_fits (mc->patch) !=MIS_UCODE ) && !saved ) { saved = mc->patch; saved_size = mc->len;
xen/arch/x86/cpu/microcode/intel.c +3 -8
@@ -294,8 +294,7 @@ static int cf_check apply_microcode(const struct microcode_patch *patch) result =microcode_update_match (patch); -if ( result !=NEW_UCODE && - !(opt_ucode_allow_same && result ==SAME_UCODE ) ) +if ( result !=NEW_UCODE && result !=SAME_UCODE )return -EINVAL ;wbinvd ();
@@ -354,12 +353,8 @@ static struct microcode_patch *cf_check cpu_request_microcode(if ( error )break ; -/* -* If the new update covers current CPU, compare updates and store the -* one with higher revision. -*/ -if ( (microcode_update_match (mc) !=MIS_UCODE ) && - (!saved ||compare_revisions (saved->rev, mc->rev) ==NEW_UCODE ) ) +/* If the provided ucode covers current CPU, then store its revision. */ +if ( (microcode_update_match (mc) !=MIS_UCODE ) && !saved ) saved = mc; buf += blob_size;
Signature · 1 line
2.42.0
Re: [PATCH 1/5] x86: Remove x86 low level version check of microcode
Quoted 2 lines · Fouad Hilly
Remove microcode version check at Intel and AMD Level. Microcode version check will be at higher and common level.
"will be" reads as if you're removing logic here, to introduce some replacement later. If so, that's going to be a transient regression, which needs avoiding. Indeed ...
Quoted 7 lines
xen/arch/x86/cpu/microcode/amd.c +0
-1@@ -383,12 +383,8 @@ static struct microcode_patch *cf_check cpu_request_microcode( goto skip; } -/*
xen/arch/x86/cpu/microcode/amd.c static struct microcode_patch *cf_check cpu_request_microcode( · line 390
Quoted 10 lines
xen/arch/x86/cpu/microcode/amd.c +2
-5-* If the new ucode covers current CPU, compare ucodes and store the -* one with higher revision. -*/ -if ( (microcode_fits (mc->patch) !=MIS_UCODE ) && - (!saved || (compare_header (mc->patch, saved) ==NEW_UCODE )) ) +/* If the provided ucode covers current CPU, then store its revision. */ +if ( (microcode_fits (mc->patch) !=MIS_UCODE ) && !saved ) { saved = mc->patch; saved_size = mc->len;
... this looks like a logic change to me, with there not being anything similar in common code afaics. Am I overlooking anything?
xen/arch/x86/cpu/microcode/intel.c static int cf_check apply_microcode(const struct microcode_patch *patch) · line 298
Quoted 10 lines
xen/arch/x86/cpu/microcode/intel.c +1
-2@@ -294,8 +294,7 @@ static int cf_check apply_microcode(const struct microcode_patch *patch) result =microcode_update_match (patch); -if ( result !=NEW_UCODE && - !(opt_ucode_allow_same && result ==SAME_UCODE ) ) +if ( result !=NEW_UCODE && result !=SAME_UCODE )return -EINVAL ;
I'm afraid I can't relate this adjustment with title and description of the patch.
Jan
Re: [PATCH 1/5] x86: Remove x86 low level version check of microcode
Quoted 9 lines · Jan Beulich
Quoted 2 lines · Fouad Hilly
Remove microcode version check at Intel and AMD Level. Microcode version check will be at higher and common level.
"will be" reads as if you're removing logic here, to introduce some replacement later. If so, that's going to be a transient regression, which needs avoiding. Indeed ...
Higher level at core.c already does version checks, by removing the check from low level, higher level "will be" the place. I will update the description.
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/cpu/microcode/amd.c +0
-0@@ -383,12 +383,8 @@ static struct microcode_patch *cf_check cpu_request_microcode( goto skip; }
Quoted 15 lines
xen/arch/x86/cpu/microcode/amd.c static struct microcode_patch *cf_check cpu_request_microcode( · line 390
Quoted 11 lines
xen/arch/x86/cpu/microcode/amd.c +2
-6-/* -* If the new ucode covers current CPU, compare ucodes and store the -* one with higher revision. -*/ -if ( (microcode_fits (mc->patch) !=MIS_UCODE ) && - (!saved || (compare_header (mc->patch, saved) ==NEW_UCODE )) ) +/* If the provided ucode covers current CPU, then store its revision. */ +if ( (microcode_fits (mc->patch) !=MIS_UCODE ) && !saved ) { saved = mc->patch; saved_size = mc->len;... this looks like a logic change to me, with there not being anything similar in common code afaics. Am I overlooking anything?
The code still checks if it is the current CPU; however, I removed the check for "NEW_CODE" as a prerequisite for storing the firmware revision.
If there is any error at this stage (CPU specific) an error will be propagated to a higher level and dealt with.
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/cpu/microcode/intel.c +0
-0@@ -294,8 +294,7 @@ static int cf_check apply_microcode(const struct microcode_patch *patch) result =microcode_update_match (patch);
Quoted 8 lines
xen/arch/x86/cpu/microcode/intel.c static int cf_check apply_microcode(const struct microcode_patch *patch) · line 298
Quoted 4 lines
xen/arch/x86/cpu/microcode/intel.c +1
-2-if ( result !=NEW_UCODE && - !(opt_ucode_allow_same && result ==SAME_UCODE ) ) +if ( result !=NEW_UCODE && result !=SAME_UCODE )return -EINVAL ;I'm afraid I can't relate this adjustment with title and description of the patch.
I will update the patch description
Quoted 1 line
Jan
Thanks,
Fouad
PATCH 2/5 x86: Refactor microcode_update() hypercall with flags field
In reply to Fouad HillyRefactor microcode_update() hypercall by adding flags field. Introduce XENPF_microcode_update2 hypercall to handle flags field.
--- xen/arch/x86/cpu/microcode/core.c | 12 +++++++++--- xen/arch/x86/include/asm/microcode.h | 2 +- xen/arch/x86/platform_hypercall.c | 12 +++++++++++- xen/include/public/platform.h | 8 ++++++++ 4 files changed, 29 insertions(+), 5 deletions(-)
xen/arch/x86/cpu/microcode/core.c +9 -3
@@ -40,6 +40,8 @@#include <asm/processor.h> #include <asm/setup.h> +#include <public/platform.h> +#include "private.h" /*
@@ -100,6 +102,8 @@ static bool ucode_in_nmi = true;bool __read_mostly opt_ucode_allow_same; +bool ucode_force_flag; +/* Protected by microcode_mutex */ static struct microcode_patch *microcode_cache;
@@ -633,12 +637,12 @@ static long cf_check microcode_update_helper(void *data) microcode_cache);if ( result !=NEW_UCODE && - !(opt_ucode_allow_same && result ==SAME_UCODE ) ) + !((opt_ucode_allow_same || ucode_force_flag) && result ==SAME_UCODE ) ) {spin_unlock (µcode_mutex);printk (XENLOG_WARNING "microcode: couldn't find any newer%s revision in the provided blob!\n" , - opt_ucode_allow_same ?" (or the same)" :"" ); + (opt_ucode_allow_same || ucode_force_flag) ?" (or the same)" :"" );microcode_free_patch (patch); ret = -ENOENT ;
@@ -708,11 +712,13 @@ static long cf_check microcode_update_helper(void *data)return ret; } -int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len) +int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len,unsigned int flags) {int ret;struct ucode_buf *buffer; + ucode_force_flag = (flags ==XENPF_UCODE_FLAG_FORCE_SET )?1 :0 ; +if ( len != (uint32_t )len )return -E2BIG ;
xen/arch/x86/include/asm/microcode.h +1 -1
@@ -22,7 +22,7 @@ struct cpu_signature { DECLARE_PER_CPU(struct cpu_signature, cpu_sig);void microcode_set_module (unsigned int idx); -int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len); +int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len,unsigned int flags);int early_microcode_init (unsigned long *module_map,const struct multiboot_info *mbi); int microcode_init_cache(unsigned long *module_map,
xen/arch/x86/platform_hypercall.c +11 -1
@@ -311,7 +311,17 @@ ret_t do_platform_op(guest_from_compat_handle (data, op->u.microcode.data); - ret =microcode_update (data, op->u.microcode.length); + ret =microcode_update (data, op->u.microcode.length,0 ); +break ; + } + +case XENPF_microcode_update2: + { +XEN_GUEST_HANDLE (const_void) data; + +guest_from_compat_handle (data, op->u.microcode.data); + + ret =microcode_update (data, op->u.microcode.length, op->u.microcode.flags);break ; }
xen/include/public/platform.h +8 -0
@@ -99,6 +99,10 @@ struct xenpf_microcode_update {/* IN variables. */ XEN_GUEST_HANDLE (const_void) data;/* Pointer to microcode data */ uint32_t length;/* Length of microcode data. */ +uint32_t flags;/* Flags to be passed with ucode. */ +/* Force to skip microcode version check when set */ +#define XENPF_UCODE_FLAG_FORCE_NOT_SET 0 +#define XENPF_UCODE_FLAG_FORCE_SET 1 };typedef struct xenpf_microcode_update xenpf_microcode_update_t ;DEFINE_XEN_GUEST_HANDLE (xenpf_microcode_update_t);
@@ -624,6 +628,10 @@ struct xenpf_ucode_revision {typedef struct xenpf_ucode_revision xenpf_ucode_revision_t ;DEFINE_XEN_GUEST_HANDLE (xenpf_ucode_revision_t); +/* Hypercall to microcode_update with flags */ +#define XENPF_microcode_update2 66 + +/* * ` enum neg_errnoval * ` HYPERVISOR_platform_op(const struct xen_platform_op*);
Signature · 1 line
2.42.0
Re: [PATCH 2/5] x86: Refactor microcode_update() hypercall with flags field
xen/arch/x86/cpu/microcode/core.c static long cf_check microcode_update_helper(void *data) · line 720
Quoted 11 lines · Fouad Hilly
xen/arch/x86/cpu/microcode/core.c +2
-1@@ -708,11 +712,13 @@ static long cf_check microcode_update_helper(void *data)return ret; } -int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len) +int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len,unsigned int flags) {int ret;struct ucode_buf *buffer; + ucode_force_flag = (flags ==XENPF_UCODE_FLAG_FORCE_SET )?1 :0 ;
No need for ?: when the lhs has type bool.
But - do we really need to resort to parameter passing via static variables here? If it's unavoidable, its setting needs to move inside a locked region (with that region covering everything up to all consumption of the value).
Further, to avoid the same issue again when another flag wants adding, you want to check that all other bits in the flags field are clear.
xen/arch/x86/include/asm/microcode.h struct cpu_signature { · line 25
Quoted 8 lines
xen/arch/x86/include/asm/microcode.h +1
-1@@ -22,7 +22,7 @@ struct cpu_signature { DECLARE_PER_CPU(struct cpu_signature, cpu_sig);void microcode_set_module (unsigned int idx); -int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len); +int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len,unsigned int flags);
Nit: Too long line.
xen/include/public/platform.h struct xenpf_microcode_update { · line 106
Quoted 11 lines
xen/include/public/platform.h +4
-0@@ -99,6 +99,10 @@ struct xenpf_microcode_update {/* IN variables. */ XEN_GUEST_HANDLE (const_void) data;/* Pointer to microcode data */ uint32_t length;/* Length of microcode data. */ +uint32_t flags;/* Flags to be passed with ucode. */ +/* Force to skip microcode version check when set */ +#define XENPF_UCODE_FLAG_FORCE_NOT_SET 0 +#define XENPF_UCODE_FLAG_FORCE_SET 1 };
The safety of this growing of an existing stable ABI struct wants at least briefly mentioning in the description.
xen/include/public/platform.h struct xenpf_ucode_revision { · line 634
Quoted 8 lines
xen/include/public/platform.h +4
-0@@ -624,6 +628,10 @@ struct xenpf_ucode_revision {typedef struct xenpf_ucode_revision xenpf_ucode_revision_t ;DEFINE_XEN_GUEST_HANDLE (xenpf_ucode_revision_t); +/* Hypercall to microcode_update with flags */ +#define XENPF_microcode_update2 66 + +
No double blank lines please.
Jan
Re: [PATCH 2/5] x86: Refactor microcode_update() hypercall with flags field
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Fouad Hilly
xen/arch/x86/cpu/microcode/core.c +1
-1@@ -708,11 +712,13 @@ static long cf_check microcode_update_helper(void *data)return ret; } -int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len) +int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len,unsigned int flags) {int ret;struct ucode_buf *buffer;
Quoted 7 lines
xen/arch/x86/cpu/microcode/core.c static long cf_check microcode_update_helper(void *data) · line 720
Quoted 1 line
xen/arch/x86/cpu/microcode/core.c +1
-0+ ucode_force_flag = (flags ==XENPF_UCODE_FLAG_FORCE_SET )?1 :0 ;No need for ?: when the lhs has type bool.
But - do we really need to resort to parameter passing via static variables here? If it's unavoidable, its setting needs to move inside a locked region (with that region covering everything up to all consumption of the value).
There are many function calls and checks of the firmware between microcode_update() and the actual update, which makes static variable the viable option.
In V2 I broke it down between the actual update_flags (static) and force_flag (local to firmware update function), I understand that might not be enough, I will look into further improvement for microcode_update flags in V3.
Quoted 3 lines
Further, to avoid the same issue again when another flag wants adding, you want to check that all other bits in the flags field are clear.
The above check is checking all bits in the flags field. Are you referring to flag per bit where multiple flags can be set simultaneously?
Quoted 31 lines
xen/arch/x86/include/asm/microcode.h struct cpu_signature { · line 25
Quoted 9 lines
xen/arch/x86/include/asm/microcode.h +1
-1@@ -22,7 +22,7 @@ struct cpu_signature { DECLARE_PER_CPU(struct cpu_signature, cpu_sig);void microcode_set_module (unsigned int idx); -int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len); +int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len,unsigned int flags);Nit: Too long line.
xen/include/public/platform.h struct xenpf_microcode_update { · line 106
Quoted 11 lines
xen/include/public/platform.h +4
-0@@ -99,6 +99,10 @@ struct xenpf_microcode_update {/* IN variables. */ XEN_GUEST_HANDLE (const_void) data;/* Pointer to microcode data */ uint32_t length;/* Length of microcode data. */ +uint32_t flags;/* Flags to be passed with ucode. */ +/* Force to skip microcode version check when set */ +#define XENPF_UCODE_FLAG_FORCE_NOT_SET 0 +#define XENPF_UCODE_FLAG_FORCE_SET 1 };The safety of this growing of an existing stable ABI struct wants at least briefly mentioning in the description.
Quoted 4 lines
xen/include/public/platform.h +0
-0@@ -624,6 +628,10 @@ struct xenpf_ucode_revision {typedef struct xenpf_ucode_revision xenpf_ucode_revision_t ;DEFINE_XEN_GUEST_HANDLE (xenpf_ucode_revision_t);
Quoted 8 lines
xen/include/public/platform.h struct xenpf_ucode_revision { · line 634
Quoted 4 lines
xen/include/public/platform.h +4
-0+/* Hypercall to microcode_update with flags */ +#define XENPF_microcode_update2 66 + +No double blank lines please.
Jan
Thanks,
Fouad
Re: [PATCH 2/5] x86: Refactor microcode_update() hypercall with flags field
Quoted 27 lines · Fouad Hilly
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Fouad Hilly
xen/arch/x86/cpu/microcode/core.c +1
-1@@ -708,11 +712,13 @@ static long cf_check microcode_update_helper(void *data)return ret; } -int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len) +int microcode_update (XEN_GUEST_HANDLE (const_void) buf,unsigned long len,unsigned int flags) {int ret;struct ucode_buf *buffer;Quoted 7 lines
xen/arch/x86/cpu/microcode/core.c static long cf_check microcode_update_helper(void *data) · line 720
Quoted 1 line
xen/arch/x86/cpu/microcode/core.c +1
-0+ ucode_force_flag = (flags ==XENPF_UCODE_FLAG_FORCE_SET )?1 :0 ;No need for ?: when the lhs has type bool.
But - do we really need to resort to parameter passing via static variables here? If it's unavoidable, its setting needs to move inside a locked region (with that region covering everything up to all consumption of the value).
There are many function calls and checks of the firmware between
microcode_update()and the actual update, which makes static variable the viable option.
In V2 I broke it down between the actualupdate_flags(static) andforce_flag(local to firmware update function), I understand that might not be enough, I will look into further improvement formicrocode_updateflags in V3.Quoted 1 line
Quoted 5 lines
Quoted 2 lines
Further, to avoid the same issue again when another flag wants adding, you want to check that all other bits in the flags field are clear.
The above check is checking all bits in the flags field. Are you referring to flag per bit where multiple flags can be set simultaneously?
No. What you do is treat a flags value of, say, 2 the same as a flags value of 0. That's wrong when considering that the value 2 may gain a meaning going forward. At this point you want to refuse flags values other than 0 or 1.
Jan
PATCH 3/5 x86: Add usage() to print out usage message
In reply to Fouad HillyRefactor xen-ucode tool by adding usage() to handle usage\help messages
--- tools/misc/xen-ucode.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
tools/misc/xen-ucode.c +9 -3
@@ -17,6 +17,14 @@ static xc_interface *xch;static const char intel_id[] ="GenuineIntel" ;static const char amd_id[] ="AuthenticAMD" ; +static void usage (const char *name) +{ +printf ("%s: Xen microcode updating tool\n" +"Usage: %s [<microcode file> | show-cpu-info]\n" +"\n" + , name, name); +} +static void show_curr_cpu (FILE *f) {int ret;
@@ -88,9 +96,7 @@ int main(int argc, char *argv[])if ( argc <2 ) { -fprintf (stderr, -"xen-ucode: Xen microcode updating tool\n" -"Usage: %s [<microcode file> | show-cpu-info]\n" , argv[0 ]); +usage (argv[0 ]);show_curr_cpu (stderr);exit (2 ); }
Signature · 1 line
2.42.0
Re: [PATCH 3/5] x86: Add usage() to print out usage message
Quoted 1 line · Fouad Hilly
Refactor xen-ucode tool by adding
usage()to handle usage\help messages
Would be nice if you could also say why you want this.
tools/misc/xen-ucode.c static xc_interface *xch; · line 26
Quoted 13 lines
tools/misc/xen-ucode.c +7
-0@@ -17,6 +17,14 @@ static xc_interface *xch;static const char intel_id[] ="GenuineIntel" ;static const char amd_id[] ="AuthenticAMD" ; +static void usage (const char *name) +{ +printf ("%s: Xen microcode updating tool\n" +"Usage: %s [<microcode file> | show-cpu-info]\n" +"\n" + , name, name); +}
Besides formatting issues you also add an extra \n to the format string, you convert the previously hard-coded xen-ucode: (which may be okay, but wants clarifying on why it is done), ...
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 102
Quoted 11 lines
tools/misc/xen-ucode.c +1
-3@@ -88,9 +96,7 @@ int main(int argc, char *argv[])if ( argc <2 ) { -fprintf (stderr, -"xen-ucode: Xen microcode updating tool\n" -"Usage: %s [<microcode file> | show-cpu-info]\n" , argv[0 ]); +usage (argv[0 ]);show_curr_cpu (stderr);exit (2 ); }
... and you no longer print to stderr. This being an error path, the message ought to continue to go there; only a possible -h / --help option would want it to go to stdout.
Jan
PATCH 4/5 x86: Use getopt to handle command line args
In reply to Fouad HillyUse getopt_long() to handle command line arguments. Introduce ext_err for common exit with errors.
--- tools/misc/xen-ucode.c | 49 +++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 10 deletions(-)
tools/misc/xen-ucode.c +39 -10
@@ -11,6 +11,7 @@#include <sys/stat.h> #include <fcntl.h> #include <xenctrl.h> +#include <getopt.h> static xc_interface *xch;
@@ -20,7 +21,10 @@ static const char amd_id[] = "AuthenticAMD";static void usage (const char *name) {printf ("%s: Xen microcode updating tool\n" -"Usage: %s [<microcode file> | show-cpu-info]\n" +"Usage: %s [<microcode file> | --show-cpu-info]\n" +"\n" +" -h, --help display this help and exit\n" +" -s, --show-cpu-info show CPU information and exit\n" "\n" , name, name); }
@@ -82,9 +86,16 @@ static void show_curr_cpu(FILE *f)int main (int argc,char *argv[]) {int fd, ret; -char *filename, *buf; +char *filename =NULL , *buf;size_t len;struct stat st; +int opt; + +const static struct option options[] = { + {"help" , no_argument,NULL ,'h' }, + {"show-cpu-info" , no_argument,NULL ,'s' }, + {NULL , no_argument,NULL ,0 } + }; xch =xc_interface_open (NULL ,NULL ,0 );if ( xch ==NULL )
@@ -94,20 +105,33 @@ int main(int argc, char *argv[])exit (1 ); } -if ( argc <2 ) +if ( argc !=2 ) + goto ext_err; + +while ( (opt =getopt_long (argc, argv,"hs:" , options,NULL )) !=-1 ) { -usage (argv[0 ]); -show_curr_cpu (stderr); -exit (2 ); +switch (opt) + { +case 'h' : +usage (argv[0 ]); +exit (EXIT_SUCCESS ); +case 's' : +if ( argc >2 ) + goto ext_err; +show_curr_cpu (stdout); +exit (EXIT_SUCCESS ); +default : + goto ext_err; + } } -if ( !strcmp (argv[1 ],"show-cpu-info" ) ) + filename = argv[1 ]; +if ( filename ==NULL ) { -show_curr_cpu (stdout); -return 0 ; +printf ("File name error\n" ); + goto ext_err; } - filename = argv[1 ]; fd =open (filename,O_RDONLY );if ( fd <0 ) {
@@ -149,4 +173,9 @@ int main(int argc, char *argv[])close (fd);return 0 ; + +ext_err: +usage (argv[0 ]); +show_curr_cpu (stderr); +exit (STDERR_FILENO ); }
Signature · 1 line
2.42.0
Re: [PATCH 4/5] x86: Use getopt to handle command line args
tools/misc/xen-ucode.c static void show_curr_cpu(FILE *f) · line 89
Quoted 6 lines · Fouad Hilly
tools/misc/xen-ucode.c +1
-1@@ -82,9 +86,16 @@ static void show_curr_cpu(FILE *f)int main (int argc,char *argv[]) {int fd, ret; -char *filename, *buf; +char *filename =NULL , *buf;
Why?
tools/misc/xen-ucode.c static void show_curr_cpu(FILE *f) · line 94
Quoted 5 lines
tools/misc/xen-ucode.c +3
-0size_t len;struct stat st; +int opt; + +const static struct option options[] = {
Nit: Canonically "static" comes first and all type information is kept together.
Quoted 18 lines
tools/misc/xen-ucode.c +8
-2+ {"help", no_argument, NULL, 'h'}, + {"show-cpu-info", no_argument, NULL, 's'}, + {NULL, no_argument, NULL, 0} + }; xch =xc_interface_open (NULL ,NULL ,0 );if ( xch ==NULL )@@ -94,20 +105,33 @@ int main(int argc, char *argv[])exit (1 ); } -if ( argc <2 ) +if ( argc !=2 ) + goto ext_err; + +while ( (opt =getopt_long (argc, argv,"hs:" , options,NULL )) !=-1 ) { -usage (argv[0 ]);
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 120
Quoted 10 lines
tools/misc/xen-ucode.c +8
-2-show_curr_cpu (stderr); -exit (2 ); +switch (opt) + { +case 'h' : +usage (argv[0 ]); +exit (EXIT_SUCCESS ); +case 's' : +if ( argc >2 ) + goto ext_err;
This looks redundant with the earlier check that you adjust above.
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 125
Quoted 5 lines
tools/misc/xen-ucode.c +5
-0+show_curr_cpu (stdout); +exit (EXIT_SUCCESS ); +default : + goto ext_err; + }
Nit: Case labels want indenting the same as the immediately enclosing braces.
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 129
Quoted 5 lines
tools/misc/xen-ucode.c +2
-1} -if ( !strcmp (argv[1 ],"show-cpu-info" ) ) + filename = argv[1 ]; +if ( filename ==NULL )
Can this really happen when argc == 2?
Quoted 12 lines
tools/misc/xen-ucode.c +2
-3{ -show_curr_cpu (stdout); -return 0 ; +printf ("File name error\n" ); + goto ext_err; } - filename = argv[1 ]; fd =open (filename,O_RDONLY );if ( fd <0 ) {
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 177
Quoted 5 lines
tools/misc/xen-ucode.c +2
-0@@ -149,4 +173,9 @@ int main(int argc, char *argv[])close (fd);return 0 ; + +ext_err:
Labels indented by at least on blank please.
Jan
Re: [PATCH 4/5] x86: Use getopt to handle command line args
In reply to Fouad HillyQuoted 13 lines · Fouad Hilly
tools/misc/xen-ucode.c +1
-0@@ -11,6 +11,7 @@#include <sys/stat.h> #include <fcntl.h> #include <xenctrl.h> +#include <getopt.h> static xc_interface *xch;
tools/misc/xen-ucode.c static const char amd_id[] = "AuthenticAMD"; · line 24
Quoted 5 lines
tools/misc/xen-ucode.c +1
-1@@ -20,7 +21,10 @@ static const char amd_id[] = "AuthenticAMD";static void usage (const char *name) {printf ("%s: Xen microcode updating tool\n" - "Usage: %s [<microcode file> | show-cpu-info]\n" + "Usage: %s [<microcode file> | --show-cpu-info]\n"
This look like a change worth mentioning to users, can we add something in the CHANGELOG to say "show-cpu-info" is no longer an option and users/admin should use "--show-cpu-info" instead?
Quoted 25 lines
tools/misc/xen-ucode.c +11
-1+"\n" +" -h, --help display this help and exit\n" +" -s, --show-cpu-info show CPU information and exit\n" "\n" , name, name); }@@ -82,9 +86,16 @@ static void show_curr_cpu(FILE *f)int main (int argc,char *argv[]) {int fd, ret; -char *filename, *buf; +char *filename =NULL , *buf;size_t len;struct stat st; +int opt; + +const static struct option options[] = { + {"help" , no_argument,NULL ,'h' }, + {"show-cpu-info" , no_argument,NULL ,'s' }, + {NULL , no_argument,NULL ,0 } + }; xch =xc_interface_open (NULL ,NULL ,0 );if ( xch ==NULL )
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 108
Quoted 5 lines
tools/misc/xen-ucode.c +1
-1@@ -94,20 +105,33 @@ int main(int argc, char *argv[])exit (1 ); } -if ( argc <2 ) +if ( argc !=2 )
This is overly restrictive, and doesn't need to be, especially when this patch introduces the use of getopt_long().
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 111
Quoted 3 lines
tools/misc/xen-ucode.c +3
-0+ goto ext_err; + +while ( (opt =getopt_long (argc, argv,"hs:" , options,NULL )) !=-1 )
-s requires an argument but --show-cpu-info, looks there's an extra ':' in the optstring, it should read "hs", not "hs:".
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 119
Quoted 11 lines
tools/misc/xen-ucode.c +7
-3{ -usage (argv[0 ]); -show_curr_cpu (stderr); -exit (2 ); +switch (opt) + { +case 'h' : +usage (argv[0 ]); +exit (EXIT_SUCCESS ); +case 's' : +if ( argc >2 )
Why is -s only allowed alone? What if want to include some other option like "--json" to print the cpu-info in a different format?
I think one way to deal with this would be to record the fact that we want to display the cpu information, and after the getopt_long() loop, check that they are no more arguments. (Check out optind in the man page)
Quoted 26 lines
tools/misc/xen-ucode.c +10
-4+ goto ext_err; +show_curr_cpu (stdout); +exit (EXIT_SUCCESS ); +default : + goto ext_err; + } } -if ( !strcmp (argv[1 ],"show-cpu-info" ) ) + filename = argv[1 ]; +if ( filename ==NULL ) { -show_curr_cpu (stdout); -return 0 ; +printf ("File name error\n" ); + goto ext_err; } - filename = argv[1 ];fd = open(filename, O_RDONLY); if ( fd < 0 ) {
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 179
Quoted 7 lines
tools/misc/xen-ucode.c +4
-0@@ -149,4 +173,9 @@ int main(int argc, char *argv[])close (fd);return 0 ; + +ext_err: +usage (argv[0 ]); +show_curr_cpu (stderr);
Why is show_curr_cpu() called on an error path?
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 180
Quoted 1 line
tools/misc/xen-ucode.c +1
-0+exit (STDERR_FILENO );
STDERR_FILENO isn't an exit code, it's a file descriptor.
Thanks,
Signature · 1 line
Anthony PERARD
PATCH 5/5 x86: Add --force option to xen-ucode to override microcode version check
In reply to Fouad HillyIntroduce --force option to xen-ucode to force skipping microcode version check, which allows the user to update x86 microcode even if both versions are the same.
---
--- tools/include/xenctrl.h | 2 +- tools/libs/ctrl/xc_misc.c | 12 ++++++++++-- tools/misc/xen-ucode.c | 20 +++++++++++++++----- 3 files changed, 26 insertions(+), 8 deletions(-)
tools/include/xenctrl.h +1 -1
@@ -1171,7 +1171,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_microcode_update (xc_interface *xch,const void *buf,size_t len); +int xc_microcode_update (xc_interface *xch,const void *buf,size_t len,uint32_t flags);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);
tools/libs/ctrl/xc_misc.c +10 -2
@@ -203,7 +203,7 @@ int xc_physinfo(xc_interface *xch,return 0 ; } -int xc_microcode_update (xc_interface *xch,const void *buf,size_t len) +int xc_microcode_update (xc_interface *xch,const void *buf,size_t len,uint32_t flags) {int ret;struct xen_platform_op platform_op = {};
@@ -215,7 +215,15 @@ int xc_microcode_update(xc_interface *xch, const void *buf, size_t len)memcpy (uc, buf, len); - platform_op.cmd = XENPF_microcode_update; +if ( flags >XENPF_UCODE_FLAG_FORCE_NOT_SET ) + { + platform_op.cmd = XENPF_microcode_update2; + platform_op.u.microcode.flags = flags; + } +else + { + platform_op.cmd = XENPF_microcode_update; + } platform_op.u.microcode.length = len;set_xen_guest_handle (platform_op.u.microcode.data, uc);
tools/misc/xen-ucode.c +15 -5
@@ -13,6 +13,8 @@#include <xenctrl.h> #include <getopt.h> +#include <xen/platform.h> +static xc_interface *xch;static const char intel_id[] ="GenuineIntel" ;
@@ -21,10 +23,11 @@ static const char amd_id[] = "AuthenticAMD";static void usage (const char *name) {printf ("%s: Xen microcode updating tool\n" -"Usage: %s [<microcode file> | --show-cpu-info]\n" +"Usage: %s [[--force] <microcode file> | --show-cpu-info]\n" "\n" " -h, --help display this help and exit\n" " -s, --show-cpu-info show CPU information and exit\n" +" -f, --force force to skip micorocde version check\n" "\n" , name, name); }
@@ -89,11 +92,13 @@ int main(int argc, char *argv[])char *filename =NULL , *buf;size_t len;struct stat st; +uint32_t ucode_flag =XENPF_UCODE_FLAG_FORCE_NOT_SET ;int opt;const static struct option options[] = { {"help" , no_argument,NULL ,'h' }, {"show-cpu-info" , no_argument,NULL ,'s' }, + {"force" , required_argument,NULL ,'f' }, {NULL , no_argument,NULL ,0 } };
@@ -105,10 +110,10 @@ int main(int argc, char *argv[])exit (1 ); } -if ( argc !=2 ) +if ( argc <2 || argc >3 ) goto ext_err; -while ( (opt =getopt_long (argc, argv,"hs:" , options,NULL )) !=-1 ) +while ( (opt =getopt_long (argc, argv,"hsf:" , options,NULL )) !=-1 ) {switch (opt) {
@@ -120,12 +125,17 @@ int main(int argc, char *argv[]) goto ext_err;show_curr_cpu (stdout);exit (EXIT_SUCCESS ); +case 'f' : + ucode_flag =XENPF_UCODE_FLAG_FORCE_SET ; + filename = optarg; +break ;default : goto ext_err; } } - filename = argv[1 ]; +if ( argc ==2 ) + filename = argv[1 ];if ( filename ==NULL ) {printf ("File name error\n" );
@@ -155,7 +165,7 @@ int main(int argc, char *argv[])exit (1 ); } - ret =xc_microcode_update (xch, buf, len); + ret =xc_microcode_update (xch, buf, len, ucode_flag);if ( ret ) { fprintf(stderr, "Failed to update microcode. (err: %s)\n",
Signature · 1 line
2.42.0
Re: [PATCH 5/5] x86: Add --force option to xen-ucode to override microcode version check
tools/libs/ctrl/xc_misc.c int xc_physinfo(xc_interface *xch, · line 206
Quoted 8 lines · Fouad Hilly
tools/libs/ctrl/xc_misc.c +1
-1@@ -203,7 +203,7 @@ int xc_physinfo(xc_interface *xch,return 0 ; } -int xc_microcode_update (xc_interface *xch,const void *buf,size_t len) +int xc_microcode_update (xc_interface *xch,const void *buf,size_t len,uint32_t flags)
I don't think uint32_t needs using here; unsigned int will be just fine. The line also looks a little long.
tools/libs/ctrl/xc_misc.c int xc_microcode_update(xc_interface *xch, const void *buf, size_t len) · line 218
Quoted 6 lines
tools/libs/ctrl/xc_misc.c +1
-1@@ -215,7 +215,15 @@ int xc_microcode_update(xc_interface *xch, const void *buf, size_t len)memcpy (uc, buf, len); - platform_op.cmd = XENPF_microcode_update; +if ( flags >XENPF_UCODE_FLAG_FORCE_NOT_SET )
I was wondering about the purpose of XENPF_UCODE_FLAG_FORCE_NOT_SET. I don't think this constant is needed: Just like with any flags, simply using 0 when none are set is fine both at the producing and consuming sides.
tools/libs/ctrl/xc_misc.c int main(int argc, char *argv[]) · line 129
Quoted 6 lines
tools/libs/ctrl/xc_misc.c +2
-0@@ -120,12 +125,17 @@ int main(int argc, char *argv[]) goto ext_err;show_curr_cpu (stdout);exit (EXIT_SUCCESS ); +case 'f' : + ucode_flag =XENPF_UCODE_FLAG_FORCE_SET ;
Nit: Bad indentation.
Jan
Re: [PATCH 5/5] x86: Add --force option to xen-ucode to override microcode version check
In reply to Fouad Hillytools/misc/xen-ucode.c static const char amd_id[] = "AuthenticAMD"; · line 26
Quoted 10 lines · Fouad Hilly
tools/misc/xen-ucode.c +1
-1@@ -21,10 +23,11 @@ static const char amd_id[] = "AuthenticAMD";static void usage (const char *name) {printf ("%s: Xen microcode updating tool\n" - "Usage: %s [<microcode file> | --show-cpu-info]\n" + "Usage: %s [[--force] <microcode file> | --show-cpu-info]\n"
How about "Usage: %s [OPTIONS..] [MICROCODE FILE]" ?
tools/misc/xen-ucode.c static const char amd_id[] = "AuthenticAMD"; · line 30
Quoted 4 lines
tools/misc/xen-ucode.c +1
-0"\n" " -h, --help display this help and exit\n" " -s, --show-cpu-info show CPU information and exit\n" +" -f, --force force to skip micorocde version check\n"
typo: microcode ;-)
Quoted 4 lines
tools/misc/xen-ucode.c +0
-0"\n" , name, name); }
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 101
Quoted 10 lines
tools/misc/xen-ucode.c +2
-0@@ -89,11 +92,13 @@ int main(int argc, char *argv[])char *filename =NULL , *buf;size_t len;struct stat st; +uint32_t ucode_flag =XENPF_UCODE_FLAG_FORCE_NOT_SET ;int opt;const static struct option options[] = { {"help" , no_argument,NULL ,'h' }, {"show-cpu-info" , no_argument,NULL ,'s' }, + {"force" , required_argument,NULL ,'f' },
This is weird, could you do without the argument?
It is weird because sometime "microcode file" is an argument of "--force", sometime it is part of the rests of the options.
Quoted 22 lines
tools/misc/xen-ucode.c +4
-2{NULL, no_argument, NULL, 0} };@@ -105,10 +110,10 @@ int main(int argc, char *argv[])exit (1 ); } -if ( argc !=2 ) +if ( argc <2 || argc >3 ) goto ext_err; -while ( (opt =getopt_long (argc, argv,"hs:" , options,NULL )) !=-1 ) +while ( (opt =getopt_long (argc, argv,"hsf:" , options,NULL )) !=-1 ) {switch (opt) {@@ -120,12 +125,17 @@ int main(int argc, char *argv[]) goto ext_err;show_curr_cpu (stdout);exit (EXIT_SUCCESS ); +case 'f' : + ucode_flag =XENPF_UCODE_FLAG_FORCE_SET ;
tools/misc/xen-ucode.c int main(int argc, char *argv[]) · line 138
Quoted 10 lines
tools/misc/xen-ucode.c +4
-1+ filename = optarg; +break ;default : goto ext_err; } } - filename = argv[1 ]; +if ( argc ==2 ) + filename = argv[1 ];
Sometime we take filename from argv[1], sometime we don't? The logic is going to be very confusing, takeout of context, only set filename from a single place please.
Thanks,
Signature · 1 line
Anthony PERARD
Quoted 4 lines · Fouad Hilly
Refactor and introduce --force option to xen-ucode, which skips microcode version check when updating x86 CPU micocode. A new hypercall introduced with flags field to facilitate the new option and allow for future flags as needed.
It's worth giving a bit of background for reviewers.
Basically, my "ucode=allow-same" for testing purposes has proved insufficient, so we're wanting to replace it with a "user knows what they're doing and gets to keep the pieces" option instead.
Upcoming is the Intel microcode-revision extension, which has different implications for Xen than it does for Linux, by virtue of our proto-support to re-scan CPUID and cope with new things appearing.
Also, while not a consideration for this issue, it subsequently turns out that there is a microcode bug on Gemini Lake CPUs for which downgrading the microcode is a viable and practical fix.
In all cases, having an ability to say "trust me, I know what I'm doing, really try and give this blob to the CPU" would help in some corner cases.
~Andrew