Skip to contentSkip to conversation

x86/xen-ucode: Introduce --force option

PATCH Superseded Fouad Hilly 3 participants 13 messages Jul 12, 2024 – Aug 19, 2024

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 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 (4):
x86/ucode: Introduce XENPF_microcode_update2 with flags parameter 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

 docs/misc/xen-command-line.pandoc    |   7 +-
 tools/include/xenctrl.h              |   3 +-
 tools/libs/ctrl/xc_misc.c            |  12 ++--
 tools/misc/xen-ucode.c               |  63 +++++++++++++---
 xen/arch/x86/cpu/microcode/amd.c     |   8 ++-
 xen/arch/x86/cpu/microcode/core.c    | 104 +++++++++++++++++----------
 xen/arch/x86/cpu/microcode/intel.c   |   9 ++-
 xen/arch/x86/cpu/microcode/private.h |   5 +-
 xen/arch/x86/include/asm/microcode.h |   3 +-
 xen/arch/x86/platform_hypercall.c    |  13 +++-
 xen/include/public/platform.h        |  14 ++++
 11 files changed, 172 insertions(+), 69 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH v5 1/4 x86/ucode: Introduce XENPF_microcode_update2 with flags parameter

Refactor microcode_update() by adding flags field. struct xenpf_microcode_update2 added with uint32_t flags field. Introduce XENPF_microcode_update2 hypercall with flags field.

---
[v5]
1- Update commit message to include the full name of XENPF_microcode_update2
[v4]
1- Commit message and description updated.
2- Changing the order of the patches.
[v3]
1- Updated Commit message description.
2- Revereted changes to a stable ABI and introduced a new struct.
3- ucode_force_flag updated from static to a local variable.
4- microcode_update() updated to reject unsupported flags yet.
[v2]
1- Update message description to highlight interface change.
2- Removed extra empty lines.
3- removed unnecessary define.
4- Corrected long lines.
5- Removed ternary operator.
6- Introduced static ucode_update_flags, which will be used later to determine local ucode_force_flag.

---
 xen/arch/x86/cpu/microcode/core.c    | 11 ++++++++---
 xen/arch/x86/include/asm/microcode.h |  3 ++-
 xen/arch/x86/platform_hypercall.c    | 13 ++++++++++++-
 xen/include/public/platform.h        | 14 ++++++++++++++
 4 files changed, 36 insertions(+), 5 deletions(-)
xen/arch/x86/cpu/microcode/core.c +8 -3
index e90055772acf..8a9e744489b9 100644
@@ -40,6 +40,8 @@
 #include <asm/processor.h>
 #include <asm/setup.h>
 
+#include <public/platform.h>
+
 #include "private.h"
 
 /*
@@ -570,6 +572,7 @@ static int cf_check do_microcode_update(void *patch)
 }
 
 struct ucode_buf {
+    unsigned int flags;
     unsigned int len;
     char buffer[];
 };
@@ -708,13 +711,14 @@ 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;
 
-    if ( len != (uint32_t)len )
-        return -E2BIG;
+    if ( flags & ~XENPF_UCODE_FORCE )
+        return -EINVAL;
 
     if ( !ucode_ops.apply_microcode )
         return -EINVAL;
@@ -730,6 +734,7 @@ int microcode_update(XEN_GUEST_HANDLE(const_void) buf, unsigned long len)
         return -EFAULT;
     }
     buffer->len = len;
+    buffer->flags = flags;
 
     /*
      * Always queue microcode_update_helper() on CPU0.  Most of the logic
xen/arch/x86/include/asm/microcode.h +2 -1
index 8f59b20b0289..57c08205d475 100644
@@ -22,7 +22,8 @@ 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 +12 -1
index 95467b88ab64..7e3278109300 100644
@@ -311,7 +311,18 @@ 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.microcode2.data);
+
+        ret = microcode_update(data, op->u.microcode2.length,
+                               op->u.microcode2.flags);
         break;
     }
 
xen/include/public/platform.h +14 -0
index 15777b541690..2725b8d1044f 100644
@@ -624,6 +624,19 @@ 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
+struct xenpf_microcode_update2 {
+    /* IN variables. */
+    uint32_t flags;                   /* Flags to be passed with ucode. */
+/* Force to skip microcode version check */
+#define XENPF_UCODE_FORCE           1
+    uint32_t length;                  /* Length of microcode data. */
+    XEN_GUEST_HANDLE(const_void) data;/* Pointer to microcode data */
+};
+typedef struct xenpf_microcode_update2 xenpf_microcode_update2_t;
+DEFINE_XEN_GUEST_HANDLE(xenpf_microcode_update2_t);
+
 /*
  * ` enum neg_errnoval
  * ` HYPERVISOR_platform_op(const struct xen_platform_op*);
@@ -656,6 +669,7 @@ struct xen_platform_op {
         xenpf_symdata_t               symdata;
         xenpf_dom0_console_t          dom0_console;
         xenpf_ucode_revision_t        ucode_revision;
+        xenpf_microcode_update2_t     microcode2;
         uint8_t                       pad[128];
     } u;
 };
Signature · 1 line
2.42.0
#2 lore raw

PATCH v5 2/4 x86/ucode: refactor xen-ucode to utilize getopt

In reply to Fouad Hilly

Use getopt_long() to handle command line arguments. Introduce ext_err for common exit with errors. Introducing usage() to handle usage\help messages in a common block. show_curr_cpu is printed to stdout only.

---
[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 | 52 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 44 insertions(+), 8 deletions(-)
tools/misc/xen-ucode.c +44 -8
index 390969db3d1c..8de82e5b8a10 100644
@@ -11,6 +11,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <xenctrl.h>
+#include <getopt.h>
 
 static xc_interface *xch;
 
@@ -71,12 +72,29 @@ static void show_curr_cpu(FILE *f)
     }
 }
 
+static void usage(FILE *stream, const char *name)
+{
+    fprintf(stream,
+            "%s: Xen microcode updating tool\n"
+            "options:\n"
+            "  -h, --help            display this help\n"
+            "  -s, --show-cpu-info   show CPU information\n"
+            "Usage: %s [microcode file] [options]\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 +104,34 @@ 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:
+            goto ext_err;
+        }
     }
 
-    if ( !strcmp(argv[1], "show-cpu-info") )
+    if ( optind == argc )
+        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 +176,10 @@ int main(int argc, char *argv[])
     close(fd);
 
     return 0;
+
+ ext_err:
+    fprintf(stderr,
+            "%s: unable to process command line arguments\n", argv[0]);
+    usage(stderr, argv[0]);
+    exit(EXIT_FAILURE);
 }
Signature · 1 line
2.42.0
#3 lore raw

Re: [PATCH v5 2/4] x86/ucode: refactor xen-ucode to utilize getopt

Quoted 12 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;
 
@@ -71,12 +72,29 @@ static void show_curr_cpu(FILE *f)
     }

tools/misc/xen-ucode.c static void show_curr_cpu(FILE *f) · line 82

Quoted 10 lines
tools/misc/xen-ucode.c +8 -0
 }
 
+static void usage(FILE *stream, const char *name)
+{
+    fprintf(stream,
+            "%s: Xen microcode updating tool\n"
+            "options:\n"
+            "  -h, --help            display this help\n"
+            "  -s, --show-cpu-info   show CPU information\n"
+            "Usage: %s [microcode file] [options]\n", name, name);

Isn't it more like [microcode file | options] at this point? Even when --force support is added, neither of the two options here go together with a microcode file.

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

Quoted 13 lines
tools/misc/xen-ucode.c +2 -6
@@ -86,22 +104,34 @@ 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)

Nit (style): Missing blanks inside the parentheses.

Jan

#4 lore raw

Re: [PATCH v5 2/4] x86/ucode: refactor xen-ucode to utilize getopt

Quoted 16 lines · Jan Beulich
Quoted 14 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;
 
@@ -71,12 +72,29 @@ static void show_curr_cpu(FILE *f)
     }
 }
 
Quoted 12 lines

tools/misc/xen-ucode.c static void show_curr_cpu(FILE *f) · line 82

Quoted 8 lines
tools/misc/xen-ucode.c +8 -0
+static void usage(FILE *stream, const char *name)
+{
+    fprintf(stream,
+            "%s: Xen microcode updating tool\n"
+            "options:\n"
+            "  -h, --help            display this help\n"
+            "  -s, --show-cpu-info   show CPU information\n"
+            "Usage: %s [microcode file] [options]\n", name, name);

Isn't it more like [microcode file | options] at this point? Even when --force support is added, neither of the two options here go together with a microcode file.

Yes, I will fix it in v6

Quoted 5 lines
Quoted 5 lines
tools/misc/xen-ucode.c +0 -0
@@ -86,22 +104,34 @@ int main(int argc, char *argv[])
         exit(1);
     }
 
Quoted 11 lines

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

Quoted 9 lines
tools/misc/xen-ucode.c +2 -6
-    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)

Nit (style): Missing blanks inside the parentheses.

Will be fixed in v6

Quoted 2 lines

Jan

Thanks,

Fouad

#5 lore raw

Re: [PATCH v5 2/4] x86/ucode: refactor xen-ucode to utilize getopt

In reply to Fouad Hilly
Quoted 6 lines · Fouad Hilly
tools/misc/xen-ucode.c +0 -0
index 390969db3d1c..8de82e5b8a10 100644
@@ -71,12 +72,29 @@ static void show_curr_cpu(FILE *f)
     }

tools/misc/xen-ucode.c static void show_curr_cpu(FILE *f) · line 82

Quoted 10 lines
tools/misc/xen-ucode.c +8 -0
 }
 
+static void usage(FILE *stream, const char *name)
+{
+    fprintf(stream,
+            "%s: Xen microcode updating tool\n"
+            "options:\n"
+            "  -h, --help            display this help\n"
+            "  -s, --show-cpu-info   show CPU information\n"
+            "Usage: %s [microcode file] [options]\n", name, name);

FYI, I disagree with Andy about the order of this message. First is "Usage:" which explain where the option (dash-prefixed) can go, and which are the mandatory arguments, sometime having all the single-letter option in this line as well. Then there's an explanation of what the options are. I've check bash, cat, xl, gcc.

I wonder which CLI program would print the minimum amount of information on how to run the program as the last line of the help message.

Quoted 44 lines
tools/misc/xen-ucode.c +20 -8
@@ -86,22 +104,34 @@ 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:
+            goto ext_err;
+        }
     }
 
-    if ( !strcmp(argv[1], "show-cpu-info") )
+    if ( optind == argc )
+        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 )
     {

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

Quoted 7 lines
tools/misc/xen-ucode.c +4 -0
@@ -146,4 +176,10 @@ int main(int argc, char *argv[])
     close(fd);
 
     return 0;
+
+ ext_err:
+    fprintf(stderr,
+            "%s: unable to process command line arguments\n", argv[0]);

A nice to have would be to have a better error message to point out what's wrong with the arguments. For that you could print the error message before "goto ext_err". One would be "unknown option" for the first goto, and "missing microcode file" for the second goto, that is instead of printing this more generic error message.

Cheers,

Signature · 5 lines
Anthony Perard | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#6 lore raw

Re: [PATCH v5 2/4] x86/ucode: refactor xen-ucode to utilize getopt

Quoted 24 lines · Anthony PERARD
Quoted 6 lines · Fouad Hilly
tools/misc/xen-ucode.c +0 -0
index 390969db3d1c..8de82e5b8a10 100644
@@ -71,12 +72,29 @@ static void show_curr_cpu(FILE *f)
     }

tools/misc/xen-ucode.c static void show_curr_cpu(FILE *f) · line 82

Quoted 10 lines
tools/misc/xen-ucode.c +8 -0
 }
 
+static void usage(FILE *stream, const char *name)
+{
+    fprintf(stream,
+            "%s: Xen microcode updating tool\n"
+            "options:\n"
+            "  -h, --help            display this help\n"
+            "  -s, --show-cpu-info   show CPU information\n"
+            "Usage: %s [microcode file] [options]\n", name, name);

FYI, I disagree with Andy about the order of this message. First is "Usage:" which explain where the option (dash-prefixed) can go, and which are the mandatory arguments, sometime having all the single-letter option in this line as well. Then there's an explanation of what the options are. I've check bash, cat, xl, gcc.

Quoted 3 lines

I wonder which CLI program would print the minimum amount of information on how to run the program as the last line of the help message.

My Bad, I misinterpreted Andy's comment, will fix in v7:
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);
}
Quoted 49 lines

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

Quoted 11 lines
tools/misc/xen-ucode.c +1 -4
@@ -86,22 +104,34 @@ 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]);

Quoted 12 lines
tools/misc/xen-ucode.c +10 -2
-        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);
+

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

Quoted 10 lines
tools/misc/xen-ucode.c +7 -1
+        default:
+            goto ext_err;
+        }
     }
 
-    if ( !strcmp(argv[1], "show-cpu-info") )
+    if ( optind == argc )
+        goto ext_err;
+
+    /* For backwards compatibility to the pre-getopt() cmdline handling

*/

Quoted 14 lines
tools/misc/xen-ucode.c +2 -1
+    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 +176,10 @@ int main(int argc, char *argv[])
     close(fd);
 
Quoted 12 lines

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

Quoted 5 lines
tools/misc/xen-ucode.c +4 -0
     return 0;
+
+ ext_err:
+    fprintf(stderr,
+            "%s: unable to process command line arguments\n", argv[0]);

A nice to have would be to have a better error message to point out what's wrong with the arguments. For that you could print the error message before "goto ext_err". One would be "unknown option" for the first goto, and "missing microcode file" for the second goto, that is instead of printing this more generic error message.

Sure, I will have specific error messages instead of generic one in v7

Quoted 10 lines

Cheers,

--

Anthony Perard | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

Thanks,

Fouad

#7 lore raw

PATCH v5 3/4 x86/ucode: Introduce --force option to xen-ucode

In reply to Fouad Hilly

Introduce --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.

---
[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    | 13 +++++++++++--
 3 files changed, 20 insertions(+), 8 deletions(-)
tools/include/xenctrl.h +2 -1
index 9ceca0cffc2f..2c4608c09ab0 100644
@@ -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
index 50282fd60dcc..6a60216bda03 100644
@@ -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 +11 -2
index 8de82e5b8a10..a0eb3f205341 100644
@@ -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,6 +81,8 @@ static void usage(FILE *stream, const char *name)
             "options:\n"
             "  -h, --help            display this help\n"
             "  -s, --show-cpu-info   show CPU information\n"
+            "  -f, --force           skip certain checks; do not use unless you"
+            "know exactly what you are doing\n"
             "Usage: %s [microcode file] [options]\n", name, name);
     show_curr_cpu(stream);
 }
@@ -88,6 +92,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;
@@ -95,6 +100,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 )
@@ -104,7 +110,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)
         {
@@ -116,6 +122,9 @@ int main(int argc, char *argv[])
             show_curr_cpu(stdout);
             exit(EXIT_SUCCESS);
 
+        case 'f':
+            ucode_flags = XENPF_UCODE_FORCE;
+            break;
         default:
             goto ext_err;
         }
@@ -156,7 +165,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
#8 lore raw

Re: [PATCH v5 3/4] x86/ucode: Introduce --force option to xen-ucode

tools/misc/xen-ucode.c static void usage(FILE *stream, const char *name) · line 85

Quoted 6 lines · Fouad Hilly
tools/misc/xen-ucode.c +2 -0
@@ -79,6 +81,8 @@ static void usage(FILE *stream, const char *name)
             "options:\n"
             "  -h, --help            display this help\n"
             "  -s, --show-cpu-info   show CPU information\n"
+            "  -f, --force           skip certain checks; do not use unless you"
+            "know exactly what you are doing\n"

Would this output line perhaps better be wrapped explicitly, to avoid odd wrapping effects on terminals not wider than 80 chars? In any event there's a blank missing at the present source wrapping point.

Jan

#9 lore raw

Re: [PATCH v5 3/4] x86/ucode: Introduce --force option to xen-ucode

Quoted 12 lines · Jan Beulich

tools/misc/xen-ucode.c static void usage(FILE *stream, const char *name) · line 85

Quoted 6 lines · Fouad Hilly
tools/misc/xen-ucode.c +2 -0
@@ -79,6 +81,8 @@ static void usage(FILE *stream, const char *name)
             "options:\n"
             "  -h, --help            display this help\n"
             "  -s, --show-cpu-info   show CPU information\n"
+            "  -f, --force           skip certain checks; do not use unless you"
+            "know exactly what you are doing\n"

Would this output line perhaps better be wrapped explicitly, to avoid odd wrapping effects on terminals not wider than 80 chars? In any event there's a blank missing at the present source wrapping point.

Sure, I will wrap it in v6

Quoted 2 lines

Jan

Thanks,

Fouad

#10 lore raw

PATCH v5 4/4 x86/ucode: Utilize ucode_force and remove opt_ucode_allow_same

In reply to Fouad Hilly

Pass xen-ucode flags to do low level checks on micorocde 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.

---
[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.

---
 docs/misc/xen-command-line.pandoc    |  7 +--
 xen/arch/x86/cpu/microcode/amd.c     |  8 ++-
 xen/arch/x86/cpu/microcode/core.c    | 93 +++++++++++++++++-----------
 xen/arch/x86/cpu/microcode/intel.c   |  9 ++-
 xen/arch/x86/cpu/microcode/private.h |  5 +-
 5 files changed, 73 insertions(+), 49 deletions(-)
docs/misc/xen-command-line.pandoc +1 -6
index 98a45211556b..2a8d47bbc664 100644
@@ -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
index f76a563c8b84..8ec19441cfcd 100644
@@ -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 +58 -35

256 lines not on this page · raw message

xen/arch/x86/cpu/microcode/intel.c +6 -3
index f505aa1b7888..cb6e906481a4 100644
@@ -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
index da556fe5060a..017889e1b58d 100644
@@ -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
#11 lore raw

Re: [PATCH v5 4/4] x86/ucode: Utilize ucode_force and remove opt_ucode_allow_same

Quoted 5 lines · Fouad Hilly
xen/arch/x86/cpu/microcode/core.c +0 -0
@@ -90,6 +90,16 @@ struct ucode_mod_blob {
     size_t size;
 };

xen/arch/x86/cpu/microcode/core.c struct ucode_mod_blob { · line 101

Quoted 10 lines
xen/arch/x86/cpu/microcode/core.c +9 -0
 
+struct microcode_patch_with_flags {
+    unsigned int flags;
+    struct microcode_patch *patch;
+};
+
+struct microcode_nmi_patch_with_flags {
+    unsigned int flags;
+    const struct microcode_patch *patch;
+};

Why two different structures? I have to admit that I can't spot where the difference (const or not) would matter?

Also for an internal struct I don't think you need the microcode_ prefixes.

Quoted 4 lines
xen/arch/x86/cpu/microcode/core.c +0 -0
@@ -284,21 +286,22 @@ static enum microcode_match_result cf_check compare_patch(
     return compare_revisions(old->rev, new->rev);
 }
 

xen/arch/x86/cpu/microcode/core.c static enum microcode_match_result cf_check compare_patch( · line 297

Quoted 10 lines
xen/arch/x86/cpu/microcode/core.c +3 -1
-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;

Why == ? The term "flags" usually stands for there being multiple boolean indicators in a single value. That would demand use of & here.

Jan

#12 lore raw

Re: [PATCH v5 4/4] x86/ucode: Utilize ucode_force and remove opt_ucode_allow_same

Quoted 8 lines · Jan Beulich
Quoted 6 lines · Fouad Hilly
xen/arch/x86/cpu/microcode/core.c +0 -0
@@ -90,6 +90,16 @@ struct ucode_mod_blob {
     size_t size;
 };
 
Quoted 12 lines

xen/arch/x86/cpu/microcode/core.c struct ucode_mod_blob { · line 101

Quoted 9 lines
xen/arch/x86/cpu/microcode/core.c +9 -0
+struct microcode_patch_with_flags {
+    unsigned int flags;
+    struct microcode_patch *patch;
+};
+
+struct microcode_nmi_patch_with_flags {
+    unsigned int flags;
+    const struct microcode_patch *patch;
+};

Why two different structures? I have to admit that I can't spot where the difference (const or not) would matter?

I will remove the struct with const and update:
static int control_thread_fn(*const struct microcode_patch *patch*,
unsigned int flags)

Quoted 2 lines

Also for an internal struct I don't think you need the microcode_

prefixes.
I will remove microcode_prefixes.

Quoted 2 lines

xen/arch/x86/cpu/microcode/core.c static enum microcode_match_result cf_check

Quoted 2 lines
xen/arch/x86/cpu/microcode/core.c +0 -0
@@ -284,21 +286,22 @@ static enum microcode_match_result cf_check

compare_patch(

Quoted 4 lines

xen/arch/x86/cpu/microcode/core.c static enum microcode_match_result cf_check · line 287

Quoted 4 lines
xen/arch/x86/cpu/microcode/core.c +0 -1
     return compare_revisions(old->rev, new->rev);
 }
 
-static int cf_check apply_microcode(const struct microcode_patch

*patch)

Quoted 1 line

xen/arch/x86/cpu/microcode/core.c static enum microcode_match_result cf_check · line 289

Quoted 1 line
xen/arch/x86/cpu/microcode/core.c +1 -0
+static int cf_check apply_microcode(const struct microcode_patch

*patch,

Quoted 11 lines

xen/arch/x86/cpu/microcode/core.c static enum microcode_match_result cf_check · line 297

Quoted 8 lines
xen/arch/x86/cpu/microcode/core.c +2 -0
+                                    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;

Why == ? The term "flags" usually stands for there being multiple boolean indicators in a single value. That would demand use of & here.

Will be fixed in v6

Quoted 2 lines

Jan

Thanks,

Fouad