Skip to contentSkip to conversation

x86/xen-ucode: Introduce --force option

PATCH Superseded Fouad Hilly 4 participants 20 messages Apr 16, 2024 – Apr 23, 2024

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 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: Update x86 low level version check of microcode
x86: Refactor microcode_update() hypercall with flags
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              |  3 +-
 tools/libs/ctrl/xc_misc.c            | 13 +++++-
 tools/misc/xen-ucode.c               | 62 ++++++++++++++++++++++------
 xen/arch/x86/cpu/microcode/amd.c     |  8 +---
 xen/arch/x86/cpu/microcode/core.c    | 14 +++++--
 xen/arch/x86/cpu/microcode/intel.c   | 11 ++---
 xen/arch/x86/include/asm/microcode.h |  3 +-
 xen/arch/x86/platform_hypercall.c    | 12 +++++-
 xen/include/public/platform.h        |  6 +++
 9 files changed, 97 insertions(+), 35 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH v2 1/5 x86: Update x86 low level version check of microcode

Update microcode version check at Intel and AMD Level by:
Preventing the low level code from sending errors if the microcode version provided is not a newer version. Other errors will be sent like before. When the provided microcode version is the same as the current one, code to point to microcode provided.
Microcode version check happens at higher and common level in core.c. Keep all the required code at low level that checks for signature and CPU compatibility

[v2]
Update message description to better describe the changes

---
 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
index 75fc84e445ce..4f805f662701 100644
@@ -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
index 060c529a6e5d..e65c02a57987 100644
@@ -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
#2 lore raw

Re: [PATCH v2 1/5] x86: Update x86 low level version check of microcode

Quoted 8 lines · Fouad Hilly

Update microcode version check at Intel and AMD Level by:
Preventing the low level code from sending errors if the microcode version provided is not a newer version. Other errors will be sent like before. When the provided microcode version is the same as the current one, code to point to microcode provided.
Microcode version check happens at higher and common level in core.c. Keep all the required code at low level that checks for signature and CPU compatibility

Quoted 5 lines

[v2]
Update message description to better describe the changes

---

As a general note, your v2/v3/etc changelog needs to go under this --- line.

~Andrew

#3 lore raw

Re: [PATCH v2 1/5] x86: Update x86 low level version check of microcode

Quoted 10 lines · Andrew Cooper
Quoted 8 lines · Fouad Hilly

Update microcode version check at Intel and AMD Level by:
Preventing the low level code from sending errors if the microcode version provided is not a newer version. Other errors will be sent like before. When the provided microcode version is the same as the current one, code to point to microcode provided.
Microcode version check happens at higher and common level in core.c. Keep all the required code at low level that checks for signature and CPU compatibility

Quoted 8 lines
Quoted 5 lines

[v2]
Update message description to better describe the changes

---

As a general note, your v2/v3/etc changelog needs to go under this --- line.

Noted.

Quoted 2 lines

~Andrew

Thanks,

Fouad

#4 lore raw

Re: [PATCH v2 1/5] x86: Update x86 low level version check of microcode

In reply to Fouad Hilly
Quoted 3 lines · Fouad Hilly

Update microcode version check at Intel and AMD Level by:
Preventing the low level code from sending errors if the microcode version provided is not a newer version.

And why is this change (a) wanted and (b) correct?

Quoted 3 lines

Other errors will be sent like before.
When the provided microcode version is the same as the current one, code to point to microcode provided.

I'm afraid I can't interpret this sentence.

Quoted 5 lines

Microcode version check happens at higher and common level in core.c. Keep all the required code at low level that checks for signature and CPU compatibility

[v2]
Update message description to better describe the changes

This belongs ...

Quoted 2 lines

---

... below the separator.

Quoted 20 lines
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 static int cf_check apply_microcode(const struct microcode_patch *patch) · line 298

Quoted 7 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;

Unlike the other two adjustments this one results in still permitting only same-or-newer. How does this fit with the AMD change above and the other Intel change ...

xen/arch/x86/cpu/microcode/intel.c static struct microcode_patch *cf_check cpu_request_microcode( · line 358

Quoted 13 lines
xen/arch/x86/cpu/microcode/intel.c +2 -6
@@ -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;

... here?

Jan

#5 lore raw

Re: [PATCH v2 1/5] x86: Update x86 low level version check of microcode

Quoted 7 lines · Jan Beulich
Quoted 3 lines · Fouad Hilly

Update microcode version check at Intel and AMD Level by:
Preventing the low level code from sending errors if the microcode version provided is not a newer version.

And why is this change (a) wanted and (b) correct?

I will improve the message description to cover more details and reasoning.

Quoted 6 lines
Quoted 4 lines

Other errors will be sent like before.
When the provided microcode version is the same as the current one, code to point to microcode provided.

I'm afraid I can't interpret this sentence.

"provided" is the firmware presented\provided to the code for firmware flashing. As above, I will provide more comprehensive description.

Quoted 37 lines
Quoted 6 lines

Microcode version check happens at higher and common level in core.c. Keep all the required code at low level that checks for signature and CPU compatibility

[v2]
Update message description to better describe the changes

This belongs ...

Quoted 2 lines

---

... below the separator.

Quoted 23 lines
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 +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;

Unlike the other two adjustments this one results in still permitting only same-or-newer. How does this fit with the AMD change above and the other Intel change ...

To be fixed in V3

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/cpu/microcode/intel.c +0 -0
@@ -354,12 +353,8 @@ static struct microcode_patch *cf_check cpu_request_microcode(
         if ( error )
             break;
 
Quoted 11 lines

xen/arch/x86/cpu/microcode/intel.c static struct microcode_patch *cf_check cpu_request_microcode( · line 358

Quoted 9 lines
xen/arch/x86/cpu/microcode/intel.c +2 -6
-        /*
-         * 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;

... here?

I assume this refers to the previous comment? Which will be fixed in V3

Quoted 2 lines

Jan

Thanks,

Fouad

#6 lore raw

PATCH v2 2/5 x86: Refactor microcode_update() hypercall with flags

In reply to Fouad Hilly

Refactor microcode_update() hypercall by adding flags field. Introduce XENPF_microcode_update2 hypercall to handle flags field. struct xenpf_microcode_update updated to have uint32_t flags at the end of the sturcture.

[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    | 14 +++++++++++---
 xen/arch/x86/include/asm/microcode.h |  3 ++-
 xen/arch/x86/platform_hypercall.c    | 12 +++++++++++-
 xen/include/public/platform.h        |  6 ++++++
 4 files changed, 30 insertions(+), 5 deletions(-)
xen/arch/x86/cpu/microcode/core.c +11 -3
index 1c9f66ea8a0f..99b651d8c3a1 100644
@@ -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;
 
+static unsigned int ucode_update_flags = 0;
+
 /* Protected by microcode_mutex */
 static struct microcode_patch *microcode_cache;
 
@@ -580,6 +584,7 @@ static long cf_check microcode_update_helper(void *data)
     struct ucode_buf *buffer = data;
     unsigned int cpu, updated;
     struct microcode_patch *patch;
+    bool ucode_force_flag = ucode_update_flags == XENPF_UCODE_FLAG_FORCE_SET;
 
     /* cpu_online_map must not change during update */
     if ( !get_cpu_maps() )
@@ -633,12 +638,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(&microcode_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,7 +713,8 @@ 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;
@@ -731,6 +737,8 @@ int microcode_update(XEN_GUEST_HANDLE(const_void) buf, unsigned long len)
     }
     buffer->len = len;
 
+    ucode_update_flags = flags;
+
     /*
      * Always queue microcode_update_helper() on CPU0.  Most of the logic
      * won't care, but the update of the Raw CPU policy wants to (re)run on
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 +11 -1
index 95467b88ab64..3b29ede8b316 100644
@@ -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 +6 -0
index 15777b541690..cc19b2956b46 100644
@@ -99,6 +99,9 @@ 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_SET     1
 };
 typedef struct xenpf_microcode_update xenpf_microcode_update_t;
 DEFINE_XEN_GUEST_HANDLE(xenpf_microcode_update_t);
@@ -624,6 +627,9 @@ 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
#7 lore raw

Re: [PATCH v2 2/5] x86: Refactor microcode_update() hypercall with flags

Quoted 5 lines · Fouad Hilly

Refactor microcode_update() hypercall by adding flags field. Introduce XENPF_microcode_update2 hypercall to handle flags field. struct xenpf_microcode_update updated to have uint32_t flags at the end of the sturcture.

Quoted 7 lines

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

Non-style comments on v1 have remained un-addressed. Specifically, to give an example, while 1 says you now highlight the interface change, the request was to explain why changing an existing structure is okay (hint: it likely isn't, as the structure size changes for compat [aka 32-bit] callers).

I'm not going to give the same comments again; I'll rather expect you to respond to them by either adjustments to the patch (or its description), or by verbal replies.

Jan

#8 lore raw

Re: [PATCH v2 2/5] x86: Refactor microcode_update() hypercall with flags

Quoted 15 lines · Jan Beulich
Quoted 5 lines · Fouad Hilly

Refactor microcode_update() hypercall by adding flags field. Introduce XENPF_microcode_update2 hypercall to handle flags field. struct xenpf_microcode_update updated to have uint32_t flags at the end of the sturcture.

Quoted 7 lines

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

Quoted 5 lines

Non-style comments on v1 have remained un-addressed. Specifically, to give an example, while 1 says you now highlight the interface change, the request was to explain why changing an existing structure is okay (hint: it likely isn't, as the structure size changes for compat [aka 32-bit] callers).

I see your point now, I will keep the stable ABI as is.

Quoted 4 lines

I'm not going to give the same comments again; I'll rather expect you to respond to them by either adjustments to the patch (or its description), or by verbal replies.

I will respond to your V1 comment on the previous email to keep things inlined

Quoted 2 lines

Jan

Thanks,

Fouad

#9 lore raw

PATCH v2 3/5 x86: Add usage() to print out usage message

In reply to Fouad Hilly

Refactor xen-ucode tool by adding usage() to handle usage\help messages As we add more command options this will keep help\usage messages in common block

[v2]
1- Improved message description.
2- Fixed formatting and indentation.
3- Error message to print to stderr.

---
 tools/misc/xen-ucode.c | 9 +++++++++
 1 file changed, 9 insertions(+)
tools/misc/xen-ucode.c +9 -0
index c6ae6498d659..0c0b2337b4ea 100644
@@ -17,6 +17,15 @@ 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] [options]\n"
+           "Options:\n"
+           "show-cou-info   show CPU information and exit\n",
+           name, name);
+}
+
 static void show_curr_cpu(FILE *f)
 {
     int ret;
Signature · 1 line
2.42.0
#10 lore raw

Re: [PATCH v2 3/5] x86: Add usage() to print out usage message

Quoted 7 lines · Fouad Hilly

Refactor xen-ucode tool by adding usage() to handle usage\help messages As we add more command options this will keep help\usage messages in common block

[v2]
1- Improved message description.
2- Fixed formatting and indentation.
3- Error message to print to stderr.

I can't spot any use of stderr in the change here.

Quoted 8 lines
tools/misc/xen-ucode.c +2 -0
@@ -17,6 +17,15 @@ static xc_interface *xch;
 static const char intel_id[] = "GenuineIntel";
 static const char   amd_id[] = "AuthenticAMD";
 
+static void usage(const char *name)
+{

tools/misc/xen-ucode.c static xc_interface *xch; · line 31

Quoted 10 lines
tools/misc/xen-ucode.c +7 -0
+    printf("%s: Xen microcode updating tool\n"
+           "Usage: %s [microcode file] [options]\n"
+           "Options:\n"
+           "show-cou-info   show CPU information and exit\n",
+           name, name);
+}
+
 static void show_curr_cpu(FILE *f)
 {
     int ret;

Without a caller this is going to cause a compiler warning (unused static function) and, with -Werror, a build failure.

Jan

#11 lore raw

Re: [PATCH v2 3/5] x86: Add usage() to print out usage message

In reply to Fouad Hilly
Quoted 18 lines · Fouad Hilly

Refactor xen-ucode tool by adding usage() to handle usage\help messages As we add more command options this will keep help\usage messages in common block

[v2]
1- Improved message description.
2- Fixed formatting and indentation.
3- Error message to print to stderr.

---
 tools/misc/xen-ucode.c | 9 +++++++++
 1 file changed, 9 insertions(+)
tools/misc/xen-ucode.c +0 -0
index c6ae6498d659..0c0b2337b4ea 100644

tools/misc/xen-ucode.c static xc_interface *xch; · line 25

Quoted 9 lines
tools/misc/xen-ucode.c +6 -0
@@ -17,6 +17,15 @@ 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] [options]\n"
+           "Options:\n"
+           "show-cou-info   show CPU information and exit\n",

Don't change the usage message just yet. It still is
"Usage: %s [<microcode file> | show-cpu-info]"

The current one mean we can run one of:
./xen-ucode ucode.bin
./xen-ucode show-cpu-info

The proposed help message in this patch mean we could have one of:
./xen-ucode ucode.bin
./xen-ucode show-cpu-info
./xen-ucode ucode.bin show-cpu-info
But that last one is an error.

Thanks,

Signature · 1 line
Anthony PERARD
#12 lore raw

PATCH v2 4/5 x86: Use getopt to handle command line args

In reply to Fouad Hilly

Use getopt_long() to handle command line arguments. Introduce ext_err for common exit with errors.

[v2]
1- Removed unnecessary NULL initialization.
2- Used static at the beginning of type struct declaration. 3- Corrected switch\case indentations.
4- Removed redundant checks.
5- Corrected label indentation.

---
 tools/misc/xen-ucode.c | 43 ++++++++++++++++++++++++++++++------------
 1 file changed, 31 insertions(+), 12 deletions(-)
tools/misc/xen-ucode.c +31 -12
index 0c0b2337b4ea..e3c1943e3633 100644
@@ -11,6 +11,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <xenctrl.h>
+#include <getopt.h>
 
 static xc_interface *xch;
 
@@ -22,7 +23,8 @@ static void usage(const char *name)
     printf("%s: Xen microcode updating tool\n"
            "Usage: %s [microcode file] [options]\n"
            "Options:\n"
-           "show-cou-info   show CPU information and exit\n",
+           "  -h, --help            display this help and exit\n"
+           "  -s, --show-cpu-info   show CPU information and exit\n",
            name, name);
 }
 
@@ -86,6 +88,13 @@ int main(int argc, char *argv[])
     char *filename, *buf;
     size_t len;
     struct stat st;
+    int opt;
+
+    static const 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 )
@@ -95,19 +104,22 @@ int main(int argc, char *argv[])
         exit(1);
     }
 
-    if ( argc < 2 )
-    {
-        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);
-    }
+    if ( argc != 2 )
+        goto ext_err;
 
-    if ( !strcmp(argv[1], "show-cpu-info") )
+    while ( (opt = getopt_long(argc, argv, "hs", options, NULL)) != -1 )
     {
-        show_curr_cpu(stdout);
-        return 0;
+        switch (opt)
+        {
+        case 'h':
+            usage(argv[0]);
+            exit(EXIT_SUCCESS);
+        case 's':
+            show_curr_cpu(stdout);
+            exit(EXIT_SUCCESS);
+        default:
+            goto ext_err;
+        }
     }
 
     filename = argv[1];
@@ -152,4 +164,11 @@ int main(int argc, char *argv[])
     close(fd);
 
     return 0;
+
+ ext_err:
+    fprintf(stderr,
+            "xen-ucode: Xen microcode updating tool\n"
+            "Usage: %s [microcode file] [options]\n", argv[0]);
+    show_curr_cpu(stderr);
+    exit(STDERR_FILENO);
 }
Signature · 1 line
2.42.0
#13 lore raw

Re: [PATCH v2 4/5] x86: Use getopt to handle command line args

Quoted 40 lines · Fouad Hilly
tools/misc/xen-ucode.c +10 -1
index 0c0b2337b4ea..e3c1943e3633 100644
@@ -11,6 +11,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <xenctrl.h>
+#include <getopt.h>
 
 static xc_interface *xch;
 
@@ -22,7 +23,8 @@ static void usage(const char *name)
     printf("%s: Xen microcode updating tool\n"
            "Usage: %s [microcode file] [options]\n"
            "Options:\n"
-           "show-cou-info   show CPU information and exit\n",
+           "  -h, --help            display this help and exit\n"
+           "  -s, --show-cpu-info   show CPU information and exit\n",
            name, name);
 }
 
@@ -86,6 +88,13 @@ int main(int argc, char *argv[])
     char *filename, *buf;
     size_t len;
     struct stat st;
+    int opt;
+
+    static const 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 )
@@ -95,19 +104,22 @@ int main(int argc, char *argv[])
         exit(1);
     }
 

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

Quoted 10 lines
tools/misc/xen-ucode.c +2 -8
-    if ( argc < 2 )
-    {
-        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);
-    }
+    if ( argc != 2 )
+        goto ext_err;

Why only two arguments allowed? And why check argc before calling getopt_long() ?

Quoted 10 lines
tools/misc/xen-ucode.c +5 -3
-    if ( !strcmp(argv[1], "show-cpu-info") )
+    while ( (opt = getopt_long(argc, argv, "hs", options, NULL)) != -1 )
     {
-        show_curr_cpu(stdout);
-        return 0;
+        switch (opt)
+        {
+        case 'h':
+            usage(argv[0]);

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

Quoted 10 lines
tools/misc/xen-ucode.c +7 -0
+            exit(EXIT_SUCCESS);
+        case 's':
+            show_curr_cpu(stdout);
+            exit(EXIT_SUCCESS);
+        default:
+            goto ext_err;
+        }
     }
 
     filename = argv[1];

So, after calling getopt_long(), the variable optind point to the next argument that getopt_long() didn't recognize as an argument. It would be a good time to start using it, and check that the are actually argument left on the command line, even if in the current patch the only possible outcome is that argv[1] has something that isn't an option.

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

Quoted 9 lines
tools/misc/xen-ucode.c +5 -0
@@ -152,4 +164,11 @@ int main(int argc, char *argv[])
     close(fd);
 
     return 0;
+
+ ext_err:
+    fprintf(stderr,
+            "xen-ucode: Xen microcode updating tool\n"
+            "Usage: %s [microcode file] [options]\n", argv[0]);

Isn't there a usage() function that we could use?

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

Quoted 1 line
tools/misc/xen-ucode.c +1 -0
+    show_curr_cpu(stderr);

Why call show_curr_cpu() on the error path?

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

Quoted 1 line
tools/misc/xen-ucode.c +1 -0
+    exit(STDERR_FILENO);

Still not an exit value.

Thanks,

Signature · 1 line
Anthony PERARD
#14 lore raw

Re: [PATCH v2 4/5] x86: Use getopt to handle command line args

Quoted 42 lines · Anthony PERARD
Quoted 40 lines · Fouad Hilly
tools/misc/xen-ucode.c +10 -1
index 0c0b2337b4ea..e3c1943e3633 100644
@@ -11,6 +11,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <xenctrl.h>
+#include <getopt.h>
 
 static xc_interface *xch;
 
@@ -22,7 +23,8 @@ static void usage(const char *name)
     printf("%s: Xen microcode updating tool\n"
            "Usage: %s [microcode file] [options]\n"
            "Options:\n"
-           "show-cou-info   show CPU information and exit\n",
+           "  -h, --help            display this help and exit\n"
+           "  -s, --show-cpu-info   show CPU information and exit\n",
            name, name);
 }
 
@@ -86,6 +88,13 @@ int main(int argc, char *argv[])
     char *filename, *buf;
     size_t len;
     struct stat st;
+    int opt;
+
+    static const 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 )
@@ -95,19 +104,22 @@ int main(int argc, char *argv[])
         exit(1);
     }
 
Quoted 13 lines

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

Quoted 10 lines
tools/misc/xen-ucode.c +2 -8
-    if ( argc < 2 )
-    {
-        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);
-    }
+    if ( argc != 2 )
+        goto ext_err;

Why only two arguments allowed? And why check argc before calling getopt_long() ?

At this stage of the patch series, xen-ucode expects either firmware file or a single argument, that is why argc should be 2.
If there is no argument or many arguments that are not supposed to be, we exit with error other than try to parse the arguments.

Quoted 32 lines
Quoted 12 lines
tools/misc/xen-ucode.c +5 -3
-    if ( !strcmp(argv[1], "show-cpu-info") )
+    while ( (opt = getopt_long(argc, argv, "hs", options, NULL)) != -1 )
     {
-        show_curr_cpu(stdout);
-        return 0;
+        switch (opt)
+        {
+        case 'h':
+            usage(argv[0]);

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

Quoted 10 lines
tools/misc/xen-ucode.c +7 -0
+            exit(EXIT_SUCCESS);
+        case 's':
+            show_curr_cpu(stdout);
+            exit(EXIT_SUCCESS);
+        default:
+            goto ext_err;
+        }
     }
 
     filename = argv[1];

So, after calling getopt_long(), the variable optind point to the next argument that getopt_long() didn't recognize as an argument. It would be a good time to start using it, and check that the are actually argument left on the command line, even if in the current patch the only possible outcome is that argv[1] has something that isn't an option.

Quoted 3 lines
tools/misc/xen-ucode.c +0 -0
@@ -152,4 +164,11 @@ int main(int argc, char *argv[])
     close(fd);
 
Quoted 8 lines

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

Quoted 6 lines
tools/misc/xen-ucode.c +5 -0
     return 0;
+
+ ext_err:
+    fprintf(stderr,
+            "xen-ucode: Xen microcode updating tool\n"
+            "Usage: %s [microcode file] [options]\n", argv[0]);

Isn't there a usage() function that we could use?

As there is an error, stderr should be used, which was a comment on V1.

Quoted 4 lines

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

Quoted 2 lines
tools/misc/xen-ucode.c +1 -0
+    show_curr_cpu(stderr);

Why call show_curr_cpu() on the error path?

Informative, but could be removed.

Quoted 9 lines

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

Quoted 2 lines
tools/misc/xen-ucode.c +1 -0
+    exit(STDERR_FILENO);

Still not an exit value.

Thanks,

--
Anthony PERARD

Thanks,

Fouad

#15 lore raw

Re: [PATCH v2 4/5] x86: Use getopt to handle command line args

Quoted 11 lines · Fouad Hilly
Quoted 6 lines · Anthony PERARD

Review

Quoted 2 lines · Fouad Hilly
(quoted diff) +2 -0
+    if ( argc != 2 )
+        goto ext_err;

Why only two arguments allowed? And why check argc before calling getopt_long() ?

At this stage of the patch series, xen-ucode expects either firmware file or a single argument, that is why argc should be 2.
If there is no argument or many arguments that are not supposed to be, we exit with error other than try to parse the arguments.

Right, but what's the point of introducing getopt_long() if we are not going to use it? The patch tries to make it nicer to introduce more options to the tool but then put an extra check that make this actually harder. This condition is even change in the next patch, that's one more reason that says that it's a wrong check.

You can let getopt_long() parse all the options, deal with them, then you can deal with the nonoptions after that, and check that's there's only one nonoption.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

+ ext_err:

tools/misc/xen-ucode.c +3 -0
+    fprintf(stderr,
+            "xen-ucode: Xen microcode updating tool\n"
+            "Usage: %s [microcode file] [options]\n", argv[0]);
Quoted 7 lines
Quoted 1 line

Isn't there a usage() function that we could use?

As there is an error, stderr should be used, which was a comment on V1.

Quoted 4 lines
Quoted 2 lines

+ show_curr_cpu(stderr);

Why call show_curr_cpu() on the error path?

Informative, but could be removed.

We are on the error path, it's looks like the usage message is printed before the cpu information, so if the error is due to wrong options then that information is lost. We should print why there's an error, not much else would be useful. Error messages should be as late as possible or they are getting lost in the scroll of the terminal. So yes, it's probably best to remove.

Thanks,

Signature · 1 line
Anthony PERARD
#16 lore raw

PATCH v2 5/5 x86: Add --force option to xen-ucode to override microcode version check

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.

[v2]
1- Changed data type from uint32_t to unsigned int.
2- Corrected line length.
3- Removed XENPF_UCODE_FLAG_FORCE_NOT_SET.
4- Corrected indentations.
5- Changed command line options to have the file name as first argument when applicable. 6- --force option doesn't require an argument anymore.
7- Used optint to access filename in argv.

---

---
 tools/include/xenctrl.h   |  3 ++-
 tools/libs/ctrl/xc_misc.c | 13 +++++++++++--
 tools/misc/xen-ucode.c    | 18 +++++++++++++-----
 3 files changed, 26 insertions(+), 8 deletions(-)
tools/include/xenctrl.h +2 -1
index 2ef8b4e05422..49d2f19c0d77 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 +11 -2
index 5ecdfa2c7934..fbc17cefa82e 100644
@@ -203,7 +203,8 @@ 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 = {};
@@ -215,7 +216,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 > 0 )
+    {
+        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 +13 -5
index e3c1943e3633..4178fd2221ea 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";
@@ -24,7 +26,8 @@ static void usage(const char *name)
            "Usage: %s [microcode file] [options]\n"
            "Options:\n"
            "  -h, --help            display this help and exit\n"
-           "  -s, --show-cpu-info   show CPU information and exit\n",
+           "  -s, --show-cpu-info   show CPU information and exit\n"
+           "  -f, --force           force to skip micorocde version check\n",
            name, name);
 }
 
@@ -89,10 +92,12 @@ int main(int argc, char *argv[])
     size_t len;
     struct stat st;
     int opt;
+    uint32_t ucode_flag = 0;
 
     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}
     };
 
@@ -104,10 +109,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)
         {
@@ -117,12 +122,15 @@ int main(int argc, char *argv[])
         case 's':
             show_curr_cpu(stdout);
             exit(EXIT_SUCCESS);
+        case 'f':
+            ucode_flag = XENPF_UCODE_FLAG_FORCE_SET;
+            break;
         default:
             goto ext_err;
         }
     }
 
-    filename = argv[1];
+    filename = argv[optind];
     fd = open(filename, O_RDONLY);
     if ( fd < 0 )
     {
@@ -146,7 +154,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
#17 lore raw

Re: [PATCH v2 5/5] x86: Add --force option to xen-ucode to override microcode version check

Quoted 13 lines · 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.

[v2]
1- Changed data type from uint32_t to unsigned int.
2- Corrected line length.
3- Removed XENPF_UCODE_FLAG_FORCE_NOT_SET.
4- Corrected indentations.
5- Changed command line options to have the file name as first argument when applicable. 6- --force option doesn't require an argument anymore.
7- Used optint to access filename in argv.

Quoted 4 lines

---

You might want to move that tag before the '---' separation otherwise it wont be part of the commit message. git am discard every thing after the '---' line. Also add the tag before your SOB.

tools/misc/xen-ucode.c static void usage(const char *name) · line 26

Quoted 12 lines
---
 tools/include/xenctrl.h   |  3 ++-
 tools/libs/ctrl/xc_misc.c | 13 +++++++++++--
 tools/misc/xen-ucode.c    | 18 +++++++++++++-----
 3 files changed, 26 insertions(+), 8 deletions(-)
tools/misc/xen-ucode.c +0 -0
index e3c1943e3633..4178fd2221ea 100644
@@ -24,7 +26,8 @@ static void usage(const char *name)
            "Usage: %s [microcode file] [options]\n"

Now, that usage line is wrong. The options needs to go before the file. That could be fix on the previous patch. With that usage line, we would want to run ./xen-ucode ucode.bin --force, but I don't think that would work.

tools/misc/xen-ucode.c static void usage(const char *name) · line 33

Quoted 8 lines
tools/misc/xen-ucode.c +2 -1
            "Options:\n"
            "  -h, --help            display this help and exit\n"
-           "  -s, --show-cpu-info   show CPU information and exit\n",
+           "  -s, --show-cpu-info   show CPU information and exit\n"
+           "  -f, --force           force to skip micorocde version check\n",
            name, name);
 }
 

Thanks,

Signature · 1 line
Anthony PERARD
#18 lore raw

Re: [PATCH v2 5/5] x86: Add --force option to xen-ucode to override microcode version check

Quoted 15 lines · Anthony PERARD
Quoted 13 lines · 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.

[v2]
1- Changed data type from uint32_t to unsigned int.
2- Corrected line length.
3- Removed XENPF_UCODE_FLAG_FORCE_NOT_SET.
4- Corrected indentations.
5- Changed command line options to have the file name as first argument when applicable. 6- --force option doesn't require an argument anymore.
7- Used optint to access filename in argv.

Quoted 8 lines
Quoted 4 lines

---

You might want to move that tag before the '---' separation otherwise it wont be part of the commit message. git am discard every thing after the '---' line. Also add the tag before your SOB.

Noted.

Quoted 7 lines
Quoted 7 lines
---
 tools/include/xenctrl.h   |  3 ++-
 tools/libs/ctrl/xc_misc.c | 13 +++++++++++--
 tools/misc/xen-ucode.c    | 18 +++++++++++++-----
 3 files changed, 26 insertions(+), 8 deletions(-)
Quoted 8 lines

tools/misc/xen-ucode.c static void usage(const char *name) · line 26

Quoted 6 lines
tools/misc/xen-ucode.c +0 -0
index e3c1943e3633..4178fd2221ea 100644
@@ -24,7 +26,8 @@ static void usage(const char *name)
            "Usage: %s [microcode file] [options]\n"

Now, that usage line is wrong. The options needs to go before the file.

I am not sure what you mean "wrong"? from parsing perspective, both scenarios can be successfully executed:
xen-ucode firmware-file --force
xen-ucode --force firmware-file
it becomes wrong if there is an argument expects the file as an input.

Quoted 13 lines

That could be fix on the previous patch. With that usage line, we would want to run ./xen-ucode ucode.bin --force, but I don't think that would work.

tools/misc/xen-ucode.c static void usage(const char *name) · line 33

Quoted 8 lines
tools/misc/xen-ucode.c +2 -1
            "Options:\n"
            "  -h, --help            display this help and exit\n"
-           "  -s, --show-cpu-info   show CPU information and exit\n",
+           "  -s, --show-cpu-info   show CPU information and exit\n"
+           "  -f, --force           force to skip micorocde version check\n",
            name, name);
 }
 
Quoted 4 lines

Thanks,

--
Anthony PERARD

Thanks,

Fouad

#19 lore raw

Re: [PATCH v2 5/5] x86: Add --force option to xen-ucode to override microcode version check

Quoted 10 lines · Fouad Hilly
Quoted 9 lines · Anthony PERARD

tools/misc/xen-ucode.c static void usage(const char *name) · line 26

Quoted 6 lines · Fouad Hilly
tools/misc/xen-ucode.c +0 -0
index e3c1943e3633..4178fd2221ea 100644
@@ -24,7 +26,8 @@ static void usage(const char *name)
            "Usage: %s [microcode file] [options]\n"

Now, that usage line is wrong. The options needs to go before the file.

Quoted 5 lines

I am not sure what you mean "wrong"? from parsing perspective, both scenarios can be successfully executed:
xen-ucode firmware-file --force
xen-ucode --force firmware-file
it becomes wrong if there is an argument expects the file as an input.

Oh, sorry, I didn't know that getopt() would look for options on all arguments, even when separated by nonoptions. I'm used to CLI programs that takes options first, then files, even if that might work with a different order.

Cheers,

Signature · 1 line
Anthony PERARD