x86/xen-ucode: Introduce --force option
v7 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.
This change is required to enable developers to load ucode that is the same version as the one already loaded or downgrade for testing.
Fouad Hilly (3):
x86/ucode: refactor xen-ucode to utilize getopt x86/ucode: Introduce --force option to xen-ucode x86/ucode: Utilize ucode_force and remove opt_ucode_allow_same
CHANGELOG.md | 1 + docs/misc/xen-command-line.pandoc | 7 +- tools/include/xenctrl.h | 3 +- tools/libs/ctrl/xc_misc.c | 12 ++-- tools/misc/xen-ucode.c | 66 ++++++++++++++++--- xen/arch/x86/cpu/microcode/amd.c | 8 ++- xen/arch/x86/cpu/microcode/core.c | 95 ++++++++++++++++------------ xen/arch/x86/cpu/microcode/intel.c | 9 ++- xen/arch/x86/cpu/microcode/private.h | 5 +- 9 files changed, 138 insertions(+), 68 deletions(-)
Signature · 1 line
2.42.0
PATCH v7 1/3 x86/ucode: refactor xen-ucode to utilize getopt
Use getopt_long() to handle command line arguments. Introduce ext_err for common errors exit.
Introducing usage() to handle usage\help messages in a common block.
---
[v7]
1- Fix Usage message format.
2- Remove generic error message from ext_err and add specific error messages for "unknown option" and missing "microcode file". 3- Update "microcode file" -> "microcode-file" to remove ambiguity.
[v6]
1- Update usage() printed message format: [microcode file] [options] -> [microcode file | options]
2- Add missing blanks in switch ( opt )
[v5]
1- Update message description.
2- re-arrange static and automatic variables.
3- Fix indentations.
4- reverted the deletion of show-cpu-info for backwards compatibility.
[v4]
1- Merge three patches into one.
2- usage() to print messages to the correct stream.
3- Update commit message and description.
--- tools/misc/xen-ucode.c | 54 +++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-)
tools/misc/xen-ucode.c +46 -8
@@ -11,6 +11,7 @@#include <sys/stat.h> #include <fcntl.h> #include <xenctrl.h> +#include <getopt.h> static xc_interface *xch;
@@ -71,12 +72,30 @@ static void show_curr_cpu(FILE *f) } } +static void usage (FILE *stream,const char *name) +{ +fprintf (stream, +"%s: Xen microcode updating tool\n" +"Usage: %s [options | microcode-file]\n" +"options:\n" +" -h, --help display this help\n" +" -s, --show-cpu-info show CPU information\n" , + name, name); +show_curr_cpu (stream); +} +int main (int argc,char *argv[]) { +static const struct option options[] = { + {"help" , no_argument,NULL ,'h' }, + {"show-cpu-info" , no_argument,NULL ,'s' }, + {NULL , no_argument,NULL ,0 } + };int fd, ret;char *filename, *buf;size_t len;struct stat st; +int opt; xch =xc_interface_open (NULL ,NULL ,0 );if ( xch ==NULL )
@@ -86,22 +105,37 @@ int main(int argc, char *argv[])exit (1 ); } -if ( argc <2 ) +while ( (opt =getopt_long (argc, argv,"hs" , options,NULL )) !=-1 ) { -fprintf (stderr, -"xen-ucode: Xen microcode updating tool\n" -"Usage: %s [<microcode file> | show-cpu-info]\n" , argv[0 ]); -show_curr_cpu (stderr); -exit (2 ); +switch ( opt ) + { +case 'h' : +usage (stdout, argv[0 ]); +exit (EXIT_SUCCESS ); + +case 's' : +show_curr_cpu (stdout); +exit (EXIT_SUCCESS ); + +default : +fprintf (stderr,"%s: unknown option\n" , argv[0 ]); + goto ext_err; + } } -if ( !strcmp (argv[1 ],"show-cpu-info" ) ) +if ( optind == argc ) { +fprintf (stderr,"%s: missing microcode file\n" , argv[0 ]); + goto ext_err; + } + +/* For backwards compatibility to the pre-getopt() cmdline handling */ +if ( !strcmp (argv[optind],"show-cpu-info" ) ) {show_curr_cpu (stdout);return 0 ; } - filename = argv[1 ]; + filename = argv[optind]; fd =open (filename,O_RDONLY );if ( fd <0 ) {
@@ -146,4 +180,8 @@ int main(int argc, char *argv[])close (fd);return 0 ; + + ext_err: +usage (stderr, argv[0 ]); +exit (EXIT_FAILURE ); }
Signature · 1 line
2.42.0
PATCH v7 2/3 x86/ucode: Introduce --force option to xen-ucode
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 or downgrade. xc_microcode_update() refactored to accept flags and utilize xenpf_microcode_update2.
---
[v7]
1- Fix usage() printout message.
[v6]
1- Fix usage() output for -f option to be explicitly wrapped for 80 character width [v5]
1- Update commit message.
2- Re-phrase --force option description.
[v4]
1- Add --force to xen-ucode options.
2- Update xc_microcode_update() to accept and handle flags.
--- tools/include/xenctrl.h | 3 ++- tools/libs/ctrl/xc_misc.c | 12 +++++++----- tools/misc/xen-ucode.c | 16 +++++++++++++--- 3 files changed, 22 insertions(+), 9 deletions(-)
tools/include/xenctrl.h +2 -1
@@ -1171,7 +1171,8 @@ 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,unsigned int 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 +7 -5
@@ -203,11 +203,12 @@ 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,unsigned int flags) {int ret;struct xen_platform_op platform_op = {}; - DECLARE_HYPERCALL_BUFFER(struct xenpf_microcode_update, uc); + DECLARE_HYPERCALL_BUFFER(struct xenpf_microcode_update2, uc); uc =xc_hypercall_buffer_alloc (xch, uc, len);if ( uc ==NULL )
@@ -215,9 +216,10 @@ int xc_microcode_update(xc_interface *xch, const void *buf, size_t len)memcpy (uc, buf, len); - platform_op.cmd = XENPF_microcode_update; - platform_op.u.microcode.length = len; -set_xen_guest_handle (platform_op.u.microcode.data, uc); + platform_op.cmd = XENPF_microcode_update2; + platform_op.u.microcode2.length = len; + platform_op.u.microcode2.flags = flags; +set_xen_guest_handle (platform_op.u.microcode2.data, uc); ret =do_platform_op (xch, &platform_op);
tools/misc/xen-ucode.c +13 -3
@@ -13,6 +13,8 @@#include <xenctrl.h> #include <getopt.h> +#include <xen/platform.h> +static xc_interface *xch;static const char intel_id[] ="GenuineIntel" ;
@@ -79,7 +81,10 @@ static void usage(FILE *stream, const char *name)"Usage: %s [options | microcode-file]\n" "options:\n" " -h, --help display this help\n" -" -s, --show-cpu-info show CPU information\n" , +" -s, --show-cpu-info show CPU information\n" +" -f, --force skip certain checks when applying\n" +" microcode; do not use unless you know\n" +" exactly what you are doing\n" , name, name);show_curr_cpu (stream); }
@@ -89,6 +94,7 @@ int main(int argc, char *argv[])static const struct option options[] = { {"help" , no_argument,NULL ,'h' }, {"show-cpu-info" , no_argument,NULL ,'s' }, + {"force" , no_argument,NULL ,'f' }, {NULL , no_argument,NULL ,0 } };int fd, ret;
@@ -96,6 +102,7 @@ int main(int argc, char *argv[])size_t len;struct stat st;int opt; +uint32_t ucode_flags =0 ; xch =xc_interface_open (NULL ,NULL ,0 );if ( xch ==NULL )
@@ -105,7 +112,7 @@ int main(int argc, char *argv[])exit (1 ); } -while ( (opt =getopt_long (argc, argv,"hs" , options,NULL )) !=-1 ) +while ( (opt =getopt_long (argc, argv,"hsf" , options,NULL )) !=-1 ) {switch ( opt ) {
@@ -117,6 +124,9 @@ int main(int argc, char *argv[])show_curr_cpu (stdout);exit (EXIT_SUCCESS ); +case 'f' : + ucode_flags =XENPF_UCODE_FORCE ; +break ;default :fprintf (stderr,"%s: unknown option\n" , argv[0 ]); goto ext_err;
@@ -160,7 +170,7 @@ int main(int argc, char *argv[]) } errno =0 ; - ret =xc_microcode_update (xch, buf, len); + ret =xc_microcode_update (xch, buf, len, ucode_flags);if ( ret ==-1 && errno ==EEXIST )printf ("Microcode already up to date\n" );else if ( ret )
Signature · 1 line
2.42.0
Re: [PATCH v7 2/3] x86/ucode: Introduce --force option to xen-ucode
tools/misc/xen-ucode.c static void usage(FILE *stream, const char *name) · line 88
Quoted 10 lines · Fouad Hilly
tools/misc/xen-ucode.c +4
-1@@ -79,7 +81,10 @@ static void usage(FILE *stream, const char *name)"Usage: %s [options | microcode-file]\n" "options:\n" " -h, --help display this help\n" -" -s, --show-cpu-info show CPU information\n" , +" -s, --show-cpu-info show CPU information\n" +" -f, --force skip certain checks when applying\n" +" microcode; do not use unless you know\n" +" exactly what you are doing\n" , name, name);
As indicated before: --force does no longer fit "[options | microcode-file]". The form coming closest is imo going to be "[options] [microcode-file]", with the file name simply taking no effect when specified together with one of the pre-existing two options.
Jan
Re: [PATCH v7 2/3] x86/ucode: Introduce --force option to xen-ucode
Quoted 14 lines · Jan Beulich
tools/misc/xen-ucode.c static void usage(FILE *stream, const char *name) · line 85
Quoted 7 lines · Fouad Hilly
tools/misc/xen-ucode.c +2
-1@@ -79,7 +81,10 @@ static void usage(FILE *stream, const char *name)"Usage: %s [options | microcode-file]\n" "options:\n" " -h, --help display this help\n" - " -s, --show-cpu-info show CPU information\n", +" -s, --show-cpu-info show CPU information\n" + " -f, --force skip certain checks whenapplying\n"
Review static void usage(FILE *stream, const char *name) · line 86
Quoted 1 line
(quoted diff) +1
-0+ " microcode; do not use unlessyou know\n"
tools/misc/xen-ucode.c static void usage(FILE *stream, const char *name) · line 88
Quoted 2 lines
tools/misc/xen-ucode.c +1
-0+" exactly what you are doing\n" , name, name);
Quoted 8 lines
As indicated before: --force does no longer fit "[options | microcode-file]".
The form coming closest is imo going to be "[options] [microcode-file]", with
the file name simply taking no effect when specified together with one of the
pre-existing two options.
My bad, I missed this change in v7.
I agree, "[options] [microcode-file]" seems to better fit with the introduction of --force.
If there are no further comments on the rest of the patch series, I can prep v8 with this change.
Quoted 3 lines
Jan
Thanks,
Fouad
PATCH v7 3/3 x86/ucode: Utilize ucode_force and remove opt_ucode_allow_same
In reply to Fouad HillyPass xen-ucode flags to do low level checks on microcode version and utilize it to allow for microcode downgrade or reapply the same version of the microcode.ucode_force is required to be passed to a low level Intel and AMD for version checks to be done.
While adding ucode_force, opt_ucode_allow_same was removed. Remove opt_ucode_allow_same from documentation.
update CHANGELOG.md for opt_ucode_allow_same removal.
---
[v7]
1- update commit message.
2- update CHANGELOG.md.
3- update microcode_free_patch().
[v6]
1- Removed struct microcode_nmi_patch_with_flags.
2- removed const from control_thread_fn() and primary_thread_fn().
3- Fixed "flags" checks.
[v5]
1- Update commit message.
2- Introduce structs microcode_patch_with_flags and microcode_nmi_patch_with_flags. 3- pass flags to Intel and AMD low level through apply_microcode().
[4]
1- As opt_ucode_allow_same is not required anymore, it has been removed while introducing ucode_force. 2- Apply the changes for both AMD and Intel.
3- Remove the mention of opt_ucode_allow_same from documentation.
--- CHANGELOG.md | 1 + docs/misc/xen-command-line.pandoc | 7 +- xen/arch/x86/cpu/microcode/amd.c | 8 ++- xen/arch/x86/cpu/microcode/core.c | 95 ++++++++++++++++------------ xen/arch/x86/cpu/microcode/intel.c | 9 ++- xen/arch/x86/cpu/microcode/private.h | 5 +- 6 files changed, 72 insertions(+), 53 deletions(-)
CHANGELOG.md +1 -0
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Removed - On x86: - Support for running on Xeon Phi processors. + - Remove ucode=allow-same option. ## [4.19.0](https://xenbits.xenproject.org/gitweb/?p=xen.git;a=shortlog;h=RELEASE-4.19.0) - 2024-07-29
docs/misc/xen-command-line.pandoc +1 -6
@@ -2650,7 +2650,7 @@ performance. Alternatively, selecting `tsx=1` will re-enable TSX at the users own risk. ### ucode -> `= List of [ <integer> | scan=<bool>, nmi=<bool>, allow-same=<bool> ]` +> `= List of [ <integer> | scan=<bool>, nmi=<bool> ]` Applicability: x86 Default: `nmi`
@@ -2682,11 +2682,6 @@ precedence over `scan`. stop_machine context. In NMI handler, even NMIs are blocked, which is considered safer. The default value is `true`. -'allow-same' alters the default acceptance policy for new microcode to permit -trying to reload the same version. Many CPUs will actually reload microcode -of the same version, and this allows for easy testing of the late microcode -loading path. - ### unrestricted_guest (Intel) > `= <boolean>`
xen/arch/x86/cpu/microcode/amd.c +6 -2
@@ -22,6 +22,8 @@#include "private.h" +#include "public/platform.h" + #define pr_debug(x...) ((void)0)struct equiv_cpu_entry {
@@ -214,13 +216,15 @@ static enum microcode_match_result cf_check compare_patch(return compare_header (new, old); } -static int cf_check apply_microcode(const struct microcode_patch *patch) +static int cf_check apply_microcode(const struct microcode_patch *patch, +unsigned int flags) {int hw_err;unsigned int cpu =smp_processor_id ();struct cpu_signature *sig = &per_cpu (cpu_sig, cpu);uint32_t rev, old_rev = sig->rev;enum microcode_match_result result =microcode_fits (patch); +bool ucode_force = flags &XENPF_UCODE_FORCE ;if ( result ==MIS_UCODE )return -EINVAL ;
@@ -229,7 +233,7 @@ static int cf_check apply_microcode(const struct microcode_patch *patch)* Allow application of the same revision to pick up SMT-specific changes * even if the revision of the other SMT thread is already up-to-date. */ -if ( result ==OLD_UCODE ) +if ( !ucode_force && (result ==SAME_UCODE || result ==OLD_UCODE ) )return -EEXIST ;if (check_final_patch_levels (sig) )
xen/arch/x86/cpu/microcode/core.c +56 -39
269 lines not on this page · raw message
xen/arch/x86/cpu/microcode/intel.c +6 -3
@@ -29,6 +29,8 @@#include "private.h" +#include "public/platform.h" + #define pr_debug(x...) ((void)0)struct microcode_patch {
@@ -284,21 +286,22 @@ static enum microcode_match_result cf_check compare_patch(return compare_revisions (old->rev, new->rev); } -static int cf_check apply_microcode(const struct microcode_patch *patch) +static int cf_check apply_microcode(const struct microcode_patch *patch, +unsigned int flags) {uint64_t msr_content;unsigned int cpu =smp_processor_id ();struct cpu_signature *sig = &this_cpu (cpu_sig);uint32_t rev, old_rev = sig->rev;enum microcode_match_result result; +bool ucode_force = flags &XENPF_UCODE_FORCE ; result =microcode_update_match (patch);if ( result ==MIS_UCODE )return -EINVAL ; -if ( result ==OLD_UCODE || - (result ==SAME_UCODE && !opt_ucode_allow_same) ) +if ( !ucode_force && (result ==SAME_UCODE || result ==OLD_UCODE ) )return -EEXIST ;wbinvd ();
xen/arch/x86/cpu/microcode/private.h +2 -3
@@ -3,8 +3,6 @@#include <asm/microcode.h> -extern bool opt_ucode_allow_same; -enum microcode_match_result {OLD_UCODE ,/* signature matched, but revision id is older */ SAME_UCODE ,/* signature matched, but revision id is the same */
@@ -50,7 +48,8 @@ struct microcode_ops {* Attempt to load the provided patch into the CPU. Returns an error if * anything didn't go as expected. */ -int (*apply_microcode)(const struct microcode_patch *patch); +int (*apply_microcode)(const struct microcode_patch *patch, +unsigned int flags);/* * Given two patches, are they both applicable to the current CPU, and is
Signature · 1 line
2.42.0