Skip to contentSkip to conversation

x86/microcode: Add debugging glue

PATCH In review Borislav Petkov 4 participants 15 messages Aug 9, 2025 – Aug 18, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover x86/microcode: Add debugging glue

From: "Borislav Petkov (AMD)" <[email protected]>

Hi,

this is something I've been meaning to do for a long time: each time when doing despicable things to the loader, I get to add debugging code too and run it in a VM to see how those despicable things fare. But then I remove the debugging glue again when cleaning up the despicable things and turning them into proper patches.

So make this debugging code permanent but keep it out of reach from production use and have it build- and boot-disabled by default.

Thx.

Borislav Petkov (AMD) (2):
x86/microcode: Add microcode= cmdline parsing x86/microcode: Add microcode loader debugging functionality

 .../admin-guide/kernel-parameters.txt         | 15 +++-
 arch/x86/Kconfig                              | 12 +++
 arch/x86/kernel/cpu/microcode/amd.c           | 88 ++++++++++++++-----
 arch/x86/kernel/cpu/microcode/core.c          | 55 ++++++++++--
 arch/x86/kernel/cpu/microcode/internal.h      | 10 +++
 5 files changed, 149 insertions(+), 31 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 x86/microcode: Add microcode= cmdline parsing

From: "Borislav Petkov (AMD)" <[email protected]>

Add a "microcode=" command line argument after which all options can be passed in a comma-separated list.

---
 .../admin-guide/kernel-parameters.txt         |  8 +++--
 arch/x86/kernel/cpu/microcode/core.c          | 30 +++++++++++++++----
 2 files changed, 30 insertions(+), 8 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -3
index 747a55abf494..7c095177da85 100644
@@ -3767,10 +3767,12 @@
 
 	mga=		[HW,DRM]
 
-	microcode.force_minrev=	[X86]
-			Format: <bool>
+	microcode=      [X86] Control the behavior of the microcode loader.
+	                Available options, comma separated:
+
+			force_minrev
 			Enable or disable the microcode minimal revision
-			enforcement for the runtime microcode loader.
+ 			enforcement for the runtime microcode loader.
 
 	mini2440=	[ARM,HW,KNL]
 			Format:[0..2][b][c][t]
arch/x86/kernel/cpu/microcode/core.c +25 -5
index b92e09a87c69..351a51861f00 100644
@@ -43,10 +43,9 @@
 #include "internal.h"
 
 static struct microcode_ops *microcode_ops;
-static bool dis_ucode_ldr = false;
+static bool dis_ucode_ldr;
 
-bool force_minrev = IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV);
-module_param(force_minrev, bool, S_IRUSR | S_IWUSR);
+bool force_minrev = false;
 
 /*
  * Synchronization.
@@ -126,13 +125,34 @@ bool __init microcode_loader_disabled(void)
 	return dis_ucode_ldr;
 }
 
+static void early_parse_cmdline(void)
+{
+	char cmd_buf[64] = {};
+	char *s, *p = cmd_buf;
+
+	if (cmdline_find_option(boot_command_line, "microcode", cmd_buf, 64) > 0) {
+		while ((s = strsep(&p, ","))) {
+			if (IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV)) {
+				if (!strcmp("force_minrev", s))
+					force_minrev = true;
+			}
+
+			if (!strcmp(s, "dis_ucode_ldr"))
+				dis_ucode_ldr = true;
+		}
+	}
+
+	/* old, compat option */
+	if (cmdline_find_option_bool(boot_command_line, "dis_ucode_ldr") > 0)
+		dis_ucode_ldr = true;
+}
+
 void __init load_ucode_bsp(void)
 {
 	unsigned int cpuid_1_eax;
 	bool intel = true;
 
-	if (cmdline_find_option_bool(boot_command_line, "dis_ucode_ldr") > 0)
-		dis_ucode_ldr = true;
+	early_parse_cmdline();
 
 	if (microcode_loader_disabled())
 		return;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/2] x86/microcode: Add microcode= cmdline parsing

Quoted 16 lines · Borislav Petkov

From: "Borislav Petkov (AMD)" <[email protected]>

Add a "microcode=" command line argument after which all options can be passed in a comma-separated list.

---
 .../admin-guide/kernel-parameters.txt         |  8 +++--
 arch/x86/kernel/cpu/microcode/core.c          | 30 +++++++++++++++----
 2 files changed, 30 insertions(+), 8 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +0 -0
index 747a55abf494..7c095177da85 100644

Documentation/admin-guide/kernel-parameters.txt line 3772

Quoted 8 lines
Documentation/admin-guide/kernel-parameters.txt +3 -2
@@ -3767,10 +3767,12 @@
 
 	mga=		[HW,DRM]
 
-	microcode.force_minrev=	[X86]
-			Format: <bool>
+	microcode=      [X86] Control the behavior of the microcode loader.
+	                Available options, comma separated:
+

How about adding something like this to encourage using the new options:

@@ -1211,7 +1211,7 @@

-       dis_ucode_ldr   [X86] Disable the microcode loader.
+       dis_ucode_ldr   [X86] Same as microcode=disable_loader.

@@ -3770,6 +3770,9 @@

+ disable_loader
+ Disable the microcode loader.
+

Documentation/admin-guide/kernel-parameters.txt line 3775

Quoted 4 lines
Documentation/admin-guide/kernel-parameters.txt +2 -1
+			force_minrev
 			Enable or disable the microcode minimal revision
-			enforcement for the runtime microcode loader.
+ 			enforcement for the runtime microcode loader.

Unnecessary whitespace added before tab.

Quoted 7 lines
Documentation/admin-guide/kernel-parameters.txt +0 -0
 	mini2440=	[ARM,HW,KNL]
 			Format:[0..2][b][c][t]
arch/x86/kernel/cpu/microcode/core.c +0 -0
index b92e09a87c69..351a51861f00 100644

arch/x86/kernel/cpu/microcode/core.c line 49

Quoted 10 lines
arch/x86/kernel/cpu/microcode/core.c +2 -3
@@ -43,10 +43,9 @@
 #include "internal.h"
 
 static struct microcode_ops *microcode_ops;
-static bool dis_ucode_ldr = false;
+static bool dis_ucode_ldr;
 
-bool force_minrev = IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV);
-module_param(force_minrev, bool, S_IRUSR | S_IWUSR);
+bool force_minrev = false;
 

Setting this to "false" is redundant.

Quoted 9 lines
arch/x86/kernel/cpu/microcode/core.c +3 -0
 /*
  * Synchronization.
@@ -126,13 +125,34 @@ bool __init microcode_loader_disabled(void)
 	return dis_ucode_ldr;
 }
 
+static void early_parse_cmdline(void)
+{
+	char cmd_buf[64] = {};

arch/x86/kernel/cpu/microcode/core.c bool __init microcode_loader_disabled(void) · line 140

Quoted 10 lines
arch/x86/kernel/cpu/microcode/core.c +10 -0
+	char *s, *p = cmd_buf;
+
+	if (cmdline_find_option(boot_command_line, "microcode", cmd_buf, 64) > 0) {
+		while ((s = strsep(&p, ","))) {
+			if (IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV)) {
+				if (!strcmp("force_minrev", s))
+					force_minrev = true;
+			}
+
+			if (!strcmp(s, "dis_ucode_ldr"))

If you agree with the above suggestion,
s/dis_ucode_ldr/disable_loader

Quoted 20 lines
arch/x86/kernel/cpu/microcode/core.c +10 -2
+				dis_ucode_ldr = true;
+		}
+	}
+
+	/* old, compat option */
+	if (cmdline_find_option_bool(boot_command_line, "dis_ucode_ldr") > 0)
+		dis_ucode_ldr = true;
+}
+
 void __init load_ucode_bsp(void)
 {
 	unsigned int cpuid_1_eax;
 	bool intel = true;
 
-	if (cmdline_find_option_bool(boot_command_line, "dis_ucode_ldr") > 0)
-		dis_ucode_ldr = true;
+	early_parse_cmdline();
 
 	if (microcode_loader_disabled())
 		return;
maintainer
#3 lore raw

Re: [PATCH 1/2] x86/microcode: Add microcode= cmdline parsing

Review line 3772

Quoted 13 lines · Sohil Mehta

How about adding something like this to encourage using the new options:

(quoted diff) +3 -1
@@ -1211,7 +1211,7 @@
 
-       dis_ucode_ldr   [X86] Disable the microcode loader.
+       dis_ucode_ldr   [X86] Same as microcode=disable_loader.
 
 
@@ -3770,6 +3770,9 @@
 
+                       disable_loader
+                               Disable the microcode loader.

Because adding a new alias of a cmdline parameter doesn't belong in the same patch.

And if anything, that thing should be

microcode=off

to save me some silly typing.

Quoted 1 line

Unnecessary whitespace added before tab.

Pff.

Quoted 1 line

Setting this to "false" is redundant.

Fixed.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#4 lore raw

Re: [PATCH 1/2] x86/microcode: Add microcode= cmdline parsing

Quoted 6 lines · Borislav Petkov
Quoted 6 lines
(quoted diff) +1 -1
@@ -1211,7 +1211,7 @@
 
-       dis_ucode_ldr   [X86] Disable the microcode loader.
+       dis_ucode_ldr   [X86] Same as microcode=disable_loader.
 
 
Quoted 8 lines

Review line 3772

Quoted 4 lines
(quoted diff) +2 -0
@@ -3770,6 +3770,9 @@
 
+                       disable_loader
+                               Disable the microcode loader.

Because adding a new alias of a cmdline parameter doesn't belong in the same patch.

But, this patch introduces code to support microcode=dis_ucode_ldr which would then become ABI.

Quoted 4 lines

And if anything, that thing should be

microcode=off

Sure, a separate patch directly adding support for microcode=off works as well.

maintainer
#5 lore raw

Re: [PATCH 1/2] x86/microcode: Add microcode= cmdline parsing

Quoted 2 lines · Sohil Mehta

But, this patch introduces code to support microcode=dis_ucode_ldr which would then become ABI.

... and supports the old cmdline argument as well.

Quoted 2 lines

Sure, a separate patch directly adding support for microcode=off works as well.

I don't see the need for an alias atm.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#6 lore raw

Re: [PATCH 1/2] x86/microcode: Add microcode= cmdline parsing

In reply to Borislav Petkov

Documentation/admin-guide/kernel-parameters.txt line 3771

Quoted 5 lines · Borislav Petkov
Documentation/admin-guide/kernel-parameters.txt +2 -2
-	microcode.force_minrev=	[X86]
-			Format: <bool>
+	microcode=      [X86] Control the behavior of the microcode loader.
+	                Available options, comma separated:

It looks like microcode=dis_ucode_ldr is also supported. This could be added here:
ldr_ucode_ldr
Disable the microcode loader.

Documentation/admin-guide/kernel-parameters.txt line 3775

Quoted 4 lines
Documentation/admin-guide/kernel-parameters.txt +2 -1
+			force_minrev
  			Enable or disable the microcode minimal revision
-			enforcement for the runtime microcode loader.
+ 			enforcement for the runtime microcode loader.

I also noticed in arch/x86/Kconfig:

config MICROCODE_LATE_LOADING

	bool "Late microcode loading (DANGEROUS)"
	default n
	depends on MICROCODE && SMP
	help
	  ...
	  the kernel command line with "microcode.minrev=Y".

This outdated has been there already. Perhaps, it might be better to fix this typo with the new one while updating the option.

Quoted 3 lines
arch/x86/kernel/cpu/microcode/core.c +1 -2
-bool force_minrev = IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV);
-module_param(force_minrev, bool, S_IRUSR | S_IWUSR);
+bool force_minrev = false;

<snip>

arch/x86/kernel/cpu/microcode/core.c bool __init microcode_loader_disabled(void) · line 133

Quoted 6 lines
arch/x86/kernel/cpu/microcode/core.c +6 -0
+static void early_parse_cmdline(void)
+{
+	char cmd_buf[64] = {};
+	char *s, *p = cmd_buf;
+
+	if (cmdline_find_option(boot_command_line, "microcode", cmd_buf, 64) > 0) {

nit: s/64/sizeof(cmd_bug)/

arch/x86/kernel/cpu/microcode/core.c bool __init microcode_loader_disabled(void) · line 138

Quoted 5 lines
arch/x86/kernel/cpu/microcode/core.c +5 -0
+		while ((s = strsep(&p, ","))) {
+			if (IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV)) {
+				if (!strcmp("force_minrev", s))
+					force_minrev = true;
+			}

I think the behavior here differs from before:

Previously, the minrev requirement could be enforced by either
(a) Build with MICROCODE_LATE_FORCE_MINREV=y, or
(b) microcode.force_minrev with MICROCODE_LATE=y.

Now, this requires both. I don't know this is intentional, but it’s like asking for more from the user.

Thanks,
Chang

maintainer
#7 lore raw

Re: [PATCH 1/2] x86/microcode: Add microcode= cmdline parsing

Quoted 4 lines · Chang S. Bae

It looks like microcode=dis_ucode_ldr is also supported. This could be added here:
ldr_ucode_ldr
Disable the microcode loader.

Done.

Quoted 10 lines

I also noticed in arch/x86/Kconfig:

config MICROCODE_LATE_LOADING

	bool "Late microcode loading (DANGEROUS)"
	default n
	depends on MICROCODE && SMP
	help
	  ...
	  the kernel command line with "microcode.minrev=Y".
Quoted 2 lines

This outdated has been there already. Perhaps, it might be better to fix this typo with the new one while updating the option.

Done. Good catch.

Quoted 1 line

nit: s/64/sizeof(cmd_bug)/

Done.

Quoted 8 lines

I think the behavior here differs from before:

Previously, the minrev requirement could be enforced by either
(a) Build with MICROCODE_LATE_FORCE_MINREV=y, or
(b) microcode.force_minrev with MICROCODE_LATE=y.

Now, this requires both. I don't know this is intentional, but it’s like asking for more from the user.

Yeah, you're right. FORCE_MINREV is not a CONFIG item which enables force_minrev support.

Ok, here's a diff ontop with all the changes I've caught up until now:

---

Documentation/admin-guide/kernel-parameters.txt +6 -5
index fc002b1a9f57..e7badf2aba63 100644
@@ -3770,16 +3770,17 @@
 	microcode=      [X86] Control the behavior of the microcode loader.
 	                Available options, comma separated:
 
-			dbg - Format: <bool>
-			enable debugging mode when run in a guest
-
 			base_rev=X - with <X> with format: <u32>
 			Set the base microcode revision of each thread when in
 			debug mode.
 
-			force_minrev
+			dbg: enable debugging mode when run in a guest
+
+			dis_ucode_ldr: disable the microcode loader
+
+			force_minrev:
 			Enable or disable the microcode minimal revision
- 			enforcement for the runtime microcode loader.
+			enforcement for the runtime microcode loader.
 
 	mini2440=	[ARM,HW,KNL]
 			Format:[0..2][b][c][t]
arch/x86/Kconfig +2 -2
index 462bf03aeda5..77f72f075d89 100644
@@ -1340,7 +1340,7 @@ config MICROCODE_LATE_LOADING
 	  use this at your own risk. Late loading taints the kernel unless the
 	  microcode header indicates that it is safe for late loading via the
 	  minimal revision check. This minimal revision check can be enforced on
-	  the kernel command line with "microcode.minrev=Y".
+	  the kernel command line with "microcode=force_minrev".
 
 config MICROCODE_LATE_FORCE_MINREV
 	bool "Enforce late microcode loading minimal revision check"
@@ -1356,7 +1356,7 @@ config MICROCODE_LATE_FORCE_MINREV
 	  revision check fails.
 
 	  This minimal revision check can also be controlled via the
-	  "microcode.minrev" parameter on the kernel command line.
+	  "microcode=force_minrev" parameter on the kernel command line.
 
 	  If unsure say Y.
 
arch/x86/kernel/cpu/microcode/core.c +4 -6
index 3a4e210f6cf3..f045670a1fae 100644
@@ -45,7 +45,7 @@
 static struct microcode_ops *microcode_ops;
 static bool dis_ucode_ldr;
 
-bool force_minrev = false;
+bool force_minrev = IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV);
 
 /*
  * Those below should be behind CONFIG_MICROCODE_DBG ifdeffery but in
@@ -142,7 +142,7 @@ static void early_parse_cmdline(void)
 	char cmd_buf[64] = {};
 	char *s, *p = cmd_buf;
 
-	if (cmdline_find_option(boot_command_line, "microcode", cmd_buf, 64) > 0) {
+	if (cmdline_find_option(boot_command_line, "microcode", cmd_buf, sizeof(cmd_buf)) > 0) {
 		while ((s = strsep(&p, ","))) {
 			if (IS_ENABLED(CONFIG_MICROCODE_DBG)) {
 				if (!strcmp(s, "dbg"))
@@ -155,10 +155,8 @@ static void early_parse_cmdline(void)
 				}
 			}
 
-			if (IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV)) {
-				if (!strcmp("force_minrev", s))
-					force_minrev = true;
-			}
+			if (!strcmp("force_minrev", s))
+				force_minrev = true;
 
 			if (!strcmp(s, "dis_ucode_ldr"))
 				dis_ucode_ldr = true;
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Reviewed-by
#8 lore raw

Re: [PATCH 1/2] x86/microcode: Add microcode= cmdline parsing

Quoted 76 lines · Borislav Petkov
Documentation/admin-guide/kernel-parameters.txt +6 -5
index fc002b1a9f57..e7badf2aba63 100644
@@ -3770,16 +3770,17 @@
  	microcode=      [X86] Control the behavior of the microcode loader.
  	                Available options, comma separated:
 
-			dbg - Format: <bool>
-			enable debugging mode when run in a guest
-
  			base_rev=X - with <X> with format: <u32>
  			Set the base microcode revision of each thread when in
  			debug mode.
 
-			force_minrev
+			dbg: enable debugging mode when run in a guest
+
+			dis_ucode_ldr: disable the microcode loader
+
+			force_minrev:
  			Enable or disable the microcode minimal revision
- 			enforcement for the runtime microcode loader.
+			enforcement for the runtime microcode loader.
 
  	mini2440=	[ARM,HW,KNL]
  			Format:[0..2][b][c][t]
arch/x86/Kconfig +2 -2
index 462bf03aeda5..77f72f075d89 100644
@@ -1340,7 +1340,7 @@ config MICROCODE_LATE_LOADING
  	  use this at your own risk. Late loading taints the kernel unless the
  	  microcode header indicates that it is safe for late loading via the
  	  minimal revision check. This minimal revision check can be enforced on
-	  the kernel command line with "microcode.minrev=Y".
+	  the kernel command line with "microcode=force_minrev".
 
  config MICROCODE_LATE_FORCE_MINREV
  	bool "Enforce late microcode loading minimal revision check"
@@ -1356,7 +1356,7 @@ config MICROCODE_LATE_FORCE_MINREV
  	  revision check fails.
 
  	  This minimal revision check can also be controlled via the
-	  "microcode.minrev" parameter on the kernel command line.
+	  "microcode=force_minrev" parameter on the kernel command line.
 
  	  If unsure say Y.
 
arch/x86/kernel/cpu/microcode/core.c +2 -2
index 3a4e210f6cf3..f045670a1fae 100644
@@ -45,7 +45,7 @@
  static struct microcode_ops *microcode_ops;
  static bool dis_ucode_ldr;
 
-bool force_minrev = false;
+bool force_minrev = IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV);
 
  /*
   * Those below should be behind CONFIG_MICROCODE_DBG ifdeffery but in
@@ -142,7 +142,7 @@ static void early_parse_cmdline(void)
  	char cmd_buf[64] = {};
  	char *s, *p = cmd_buf;
 
-	if (cmdline_find_option(boot_command_line, "microcode", cmd_buf, 64) > 0) {
+	if (cmdline_find_option(boot_command_line, "microcode", cmd_buf, sizeof(cmd_buf)) > 0) {
  		while ((s = strsep(&p, ","))) {
  			if (IS_ENABLED(CONFIG_MICROCODE_DBG)) {
  				if (!strcmp(s, "dbg"))
@@ -155,10 +155,8 @@ static void early_parse_cmdline(void)
  				}
  			}

arch/x86/kernel/cpu/microcode/core.c static void early_parse_cmdline(void) · line 162

Quoted 10 lines
arch/x86/kernel/cpu/microcode/core.c +2 -4
 
-			if (IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV)) {
-				if (!strcmp("force_minrev", s))
-					force_minrev = true;
-			}
+			if (!strcmp("force_minrev", s))
+				force_minrev = true;
 
  			if (!strcmp(s, "dis_ucode_ldr"))
  				dis_ucode_ldr = true;

Thanks for consolidating those loader options -- this makes them easier to find and configure in one place.

The overall changes look good to me, so:

#10 lore raw

PATCH 2/2 x86/microcode: Add microcode loader debugging functionality

In reply to Borislav Petkov

From: "Borislav Petkov (AMD)" <[email protected]>

Instead of adding ad-hoc debugging glue to the microcode loader each time I need it, add debugging functionality which is not built by default and when built-in, off by default so that it can only be enabled explicitly on the command line.

Simulate all patch handling the loader does except the actual loading of the microcode patch into the hw.

---
 .../admin-guide/kernel-parameters.txt         |  7 ++
 arch/x86/Kconfig                              | 12 +++
 arch/x86/kernel/cpu/microcode/amd.c           | 88 ++++++++++++++-----
 arch/x86/kernel/cpu/microcode/core.c          | 25 +++++-
 arch/x86/kernel/cpu/microcode/internal.h      | 10 +++
 5 files changed, 119 insertions(+), 23 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +7 -0
index 7c095177da85..fc002b1a9f57 100644
@@ -3770,6 +3770,13 @@
 	microcode=      [X86] Control the behavior of the microcode loader.
 	                Available options, comma separated:
 
+			dbg - Format: <bool>
+			enable debugging mode when run in a guest
+
+			base_rev=X - with <X> with format: <u32>
+			Set the base microcode revision of each thread when in
+			debug mode.
+
 			force_minrev
 			Enable or disable the microcode minimal revision
  			enforcement for the runtime microcode loader.
arch/x86/Kconfig +12 -0
index 58d890fe2100..462bf03aeda5 100644
@@ -1360,6 +1360,18 @@ config MICROCODE_LATE_FORCE_MINREV
 
 	  If unsure say Y.
 
+config MICROCODE_DBG
+	bool "Enable microcode loader debugging"
+	default n
+	depends on MICROCODE
+	help
+	  Enable code which allows for debugging the microcode loader in
+	  a guest. Meaning the patch loading is simulated but everything else
+	  related to patch parsing and handling is done as on baremetal with
+	  the purpose of debugging solely the software side of things.
+
+	  You almost certainly want to say n here.
+
 config X86_MSR
 	tristate "/dev/cpu/*/msr - Model-specific register support"
 	help
arch/x86/kernel/cpu/microcode/amd.c +66 -22

184 lines not on this page · raw message

arch/x86/kernel/cpu/microcode/core.c +24 -1
index 351a51861f00..3a4e210f6cf3 100644
@@ -47,6 +47,18 @@ static bool dis_ucode_ldr;
 
 bool force_minrev = false;
 
+/*
+ * Those below should be behind CONFIG_MICROCODE_DBG ifdeffery but in
+ * order to not uglify the code with ifdeffery and use IS_ENABLED()
+ * instead, leave them in. When microcode debugging is not enabled,
+ * those are meaningless anyway.
+ */
+/* enable loader debugging */
+bool dbg;
+/* base microcode revision for debugging */
+u32 base_rev;
+u32 microcode_rev[NR_CPUS] = {};
+
 /*
  * Synchronization.
  *
@@ -118,7 +130,7 @@ bool __init microcode_loader_disabled(void)
 	 *    overwritten.
 	 */
 	if (!cpuid_feature() ||
-	    native_cpuid_ecx(1) & BIT(31) ||
+	    ((native_cpuid_ecx(1) & BIT(31)) && !dbg) ||
 	    amd_check_current_patch_level())
 		dis_ucode_ldr = true;
 
@@ -132,6 +144,17 @@ static void early_parse_cmdline(void)
 
 	if (cmdline_find_option(boot_command_line, "microcode", cmd_buf, 64) > 0) {
 		while ((s = strsep(&p, ","))) {
+			if (IS_ENABLED(CONFIG_MICROCODE_DBG)) {
+				if (!strcmp(s, "dbg"))
+					dbg = true;
+
+				if (strstr(s, "base_rev=")) {
+					/* advance to the option arg */
+					strsep(&s, "=");
+					if (kstrtouint(s, 16, &base_rev)) { ; }
+				}
+			}
+
 			if (IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV)) {
 				if (!strcmp("force_minrev", s))
 					force_minrev = true;
arch/x86/kernel/cpu/microcode/internal.h +10 -0
index 50a9702ae4e2..bca806dd1aac 100644
@@ -44,6 +44,10 @@ struct early_load_data {
 
 extern struct early_load_data early_data;
 extern struct ucode_cpu_info ucode_cpu_info[];
+extern u32 microcode_rev[NR_CPUS];
+extern u32 base_rev;
+extern bool dbg;
+
 struct cpio_data find_microcode_in_initrd(const char *path);
 
 #define MAX_UCODE_COUNT 128
@@ -122,4 +126,10 @@ static inline void reload_ucode_intel(void) { }
 static inline struct microcode_ops *init_intel_microcode(void) { return NULL; }
 #endif  /* !CONFIG_CPU_SUP_INTEL */
 
+#define ucode_dbg(fmt, ...)					\
+({								\
+	if (dbg)						\
+		pr_info(fmt, ##__VA_ARGS__);			\
+})
+
 #endif /* _X86_MICROCODE_INTERNAL_H */
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH 2/2] x86/microcode: Add microcode loader debugging functionality

Documentation/admin-guide/kernel-parameters.txt line 3773

Quoted 10 lines · Borislav Petkov
Documentation/admin-guide/kernel-parameters.txt +1 -0
index 7c095177da85..fc002b1a9f57 100644
@@ -3770,6 +3770,13 @@
 	microcode=      [X86] Control the behavior of the microcode loader.
 	                Available options, comma separated:
 
+			dbg - Format: <bool>

Since this is all new code, would it be better to use the expanded form of dbg everywhere?

s/dbg/debug/
s/MICROCODE_DBG/MICROCODE_DEBUG
s/ucode_dbg/ucode_debug

Also, I didn't understand the "Format: <bool>".

Isn't the usage similar to force_minrev? So microcode=debug should be enough, right?

Documentation/admin-guide/kernel-parameters.txt line 3782

Quoted 9 lines
Documentation/admin-guide/kernel-parameters.txt +6 -0
+			enable debugging mode when run in a guest
+
+			base_rev=X - with <X> with format: <u32>
+			Set the base microcode revision of each thread when in
+			debug mode.
+
 			force_minrev
 			Enable or disable the microcode minimal revision
  			enforcement for the runtime microcode loader.

Slight preference for a tabbed description:

debug
enable debugging mode when run in a guest

base_rev=X - with <X> with format: <u32>
Set the base microcode revision of each thread when in debug mode.

force_minrev
Enable or disable the microcode minimal revision enforcement for the runtime microcode loader.

arch/x86/Kconfig config MICROCODE_LATE_FORCE_MINREV · line 1365

Quoted 11 lines
arch/x86/Kconfig +3 -0
index 58d890fe2100..462bf03aeda5 100644
@@ -1360,6 +1360,18 @@ config MICROCODE_LATE_FORCE_MINREV
 
 	  If unsure say Y.
 
+config MICROCODE_DBG
+	bool "Enable microcode loader debugging"
+	default n

Isn't default n redundant? I am fine with keeping it to make it obvious.

arch/x86/Kconfig config MICROCODE_LATE_FORCE_MINREV · line 1366

Quoted 1 line
arch/x86/Kconfig +1 -0
+	depends on MICROCODE

Should we also include a dependency on DEBUG_KERNEL?

Quoted 8 lines
arch/x86/Kconfig +8 -0
+	help
+	  Enable code which allows for debugging the microcode loader in
+	  a guest. Meaning the patch loading is simulated but everything else
+	  related to patch parsing and handling is done as on baremetal with
+	  the purpose of debugging solely the software side of things.
+
+	  You almost certainly want to say n here.
+
maintainer
#12 lore raw

Re: [PATCH 2/2] x86/microcode: Add microcode loader debugging functionality

Quoted 6 lines · Sohil Mehta

Since this is all new code, would it be better to use the expanded form of dbg everywhere?

s/dbg/debug/
s/MICROCODE_DBG/MICROCODE_DEBUG
s/ucode_dbg/ucode_debug

No, because I don't want to type unnecessarily and "dbg" is very clear as it is.

Quoted 1 line

Also, I didn't understand the "Format: <bool>".

I think force_minrev was supposed to take a bool but then we changed it. And that remained here. Whacked.

Quoted 1 line

Isn't default n redundant? I am fine with keeping it to make it obvious.

Yes, we're making it obvious.

Quoted 1 line

Should we also include a dependency on DEBUG_KERNEL?

Because?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#13 lore raw

Re: [PATCH 2/2] x86/microcode: Add microcode loader debugging functionality

Quoted 7 lines · Borislav Petkov
Quoted 3 lines

s/dbg/debug/
s/MICROCODE_DBG/MICROCODE_DEBUG
s/ucode_dbg/ucode_debug

No, because I don't want to type unnecessarily and "dbg" is very clear as it is.

Yes, in this patch, dbg clearly means debug. But, are some contexts where dbg is used with other things which might take an extra second to process. For example,

	if (!cpuid_feature() ||
	    ((native_cpuid_ecx(1) & BIT(31)) && !dbg) ||
	    amd_check_current_patch_level())
		dis_ucode_ldr = true;

In the tradeoff between writing and reading, I am more inclined to make things easier to read. But obviously your preference supersedes mine :)

Quoted 5 lines
Quoted 2 lines

Should we also include a dependency on DEBUG_KERNEL?

Because?

To ensure people only use it for debugging purposes similar to X86_DEBUG_FPU or DEBUG_ENTRY.

maintainer
#14 lore raw

Re: [PATCH 2/2] x86/microcode: Add microcode loader debugging functionality

Quoted 2 lines · Sohil Mehta

To ensure people only use it for debugging purposes similar to X86_DEBUG_FPU or DEBUG_ENTRY.

No, because CONFIG_MICROCODE_DBG is a separate option and no one should enable it. Not even DEBUG_KERNEL builds.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette