All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH] x86/microcode: Use native_cpuid() in load_ucode_amd_bsp()
  2016-12-16 16:39 [PATCH] x86/microcode: Use native_cpuid() in load_ucode_amd_bsp() Boris Ostrovsky
@ 2016-12-16 16:37 ` Borislav Petkov
  2016-12-16 16:37 ` Borislav Petkov
  1 sibling, 0 replies; 13+ messages in thread
From: Borislav Petkov @ 2016-12-16 16:37 UTC (permalink / raw)
  To: Boris Ostrovsky; +Cc: x86, linux-kernel, jgross, xen-devel

On Fri, Dec 16, 2016 at 11:39:52AM -0500, Boris Ostrovsky wrote:
> When CONFIG_PARAVIRT is selected, cpuid() becomes a call. Since
> for 32-bit kernels load_ucode_amd_bsp() is executed before paging
> is enabled the call cannot be completed (as kernel virtual addresses
> are not reachable yet).
> 
> Use native_cpuid() instead which is an asm wrapper for CPUID instruction.
> 
> Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
> ---
>  arch/x86/kernel/cpu/microcode/amd.c |    6 +++++-
>  1 files changed, 5 insertions(+), 1 deletions(-)
> 
> diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
> index 6f353bd..e2d941f 100644
> --- a/arch/x86/kernel/cpu/microcode/amd.c
> +++ b/arch/x86/kernel/cpu/microcode/amd.c
> @@ -297,6 +297,7 @@ void __init load_ucode_amd_bsp(unsigned int family)
>  	struct cpio_data cp;
>  	const char *path;
>  	bool use_pa;
> +	u32 eax, ebx, ecx, edx;
>  
>  	if (IS_ENABLED(CONFIG_X86_32)) {
>  		uci	= (struct ucode_cpu_info *)__pa_nodebug(ucode_cpu_info);
> @@ -315,7 +316,10 @@ void __init load_ucode_amd_bsp(unsigned int family)
>  		return;
>  
>  	/* Get BSP's CPUID.EAX(1), needed in load_microcode_amd() */
> -	uci->cpu_sig.sig = cpuid_eax(1);
> +	eax = 0x00000001;
> +	ecx = 0;
> +	native_cpuid(&eax, &ebx, &ecx, &edx);
> +	uci->cpu_sig.sig = eax;
>  
>  	apply_microcode_early_amd(cp.data, cp.size, true);
>  }
> -- 

Applied, thanks.

-- 
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] x86/microcode: Use native_cpuid() in load_ucode_amd_bsp()
  2016-12-16 16:39 [PATCH] x86/microcode: Use native_cpuid() in load_ucode_amd_bsp() Boris Ostrovsky
  2016-12-16 16:37 ` Borislav Petkov
@ 2016-12-16 16:37 ` Borislav Petkov
  1 sibling, 0 replies; 13+ messages in thread
From: Borislav Petkov @ 2016-12-16 16:37 UTC (permalink / raw)
  To: Boris Ostrovsky; +Cc: jgross, xen-devel, x86, linux-kernel

On Fri, Dec 16, 2016 at 11:39:52AM -0500, Boris Ostrovsky wrote:
> When CONFIG_PARAVIRT is selected, cpuid() becomes a call. Since
> for 32-bit kernels load_ucode_amd_bsp() is executed before paging
> is enabled the call cannot be completed (as kernel virtual addresses
> are not reachable yet).
> 
> Use native_cpuid() instead which is an asm wrapper for CPUID instruction.
> 
> Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
> ---
>  arch/x86/kernel/cpu/microcode/amd.c |    6 +++++-
>  1 files changed, 5 insertions(+), 1 deletions(-)
> 
> diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
> index 6f353bd..e2d941f 100644
> --- a/arch/x86/kernel/cpu/microcode/amd.c
> +++ b/arch/x86/kernel/cpu/microcode/amd.c
> @@ -297,6 +297,7 @@ void __init load_ucode_amd_bsp(unsigned int family)
>  	struct cpio_data cp;
>  	const char *path;
>  	bool use_pa;
> +	u32 eax, ebx, ecx, edx;
>  
>  	if (IS_ENABLED(CONFIG_X86_32)) {
>  		uci	= (struct ucode_cpu_info *)__pa_nodebug(ucode_cpu_info);
> @@ -315,7 +316,10 @@ void __init load_ucode_amd_bsp(unsigned int family)
>  		return;
>  
>  	/* Get BSP's CPUID.EAX(1), needed in load_microcode_amd() */
> -	uci->cpu_sig.sig = cpuid_eax(1);
> +	eax = 0x00000001;
> +	ecx = 0;
> +	native_cpuid(&eax, &ebx, &ecx, &edx);
> +	uci->cpu_sig.sig = eax;
>  
>  	apply_microcode_early_amd(cp.data, cp.size, true);
>  }
> -- 

Applied, thanks.

-- 
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH] x86/microcode: Use native_cpuid() in load_ucode_amd_bsp()
@ 2016-12-16 16:39 Boris Ostrovsky
  2016-12-16 16:37 ` Borislav Petkov
  2016-12-16 16:37 ` Borislav Petkov
  0 siblings, 2 replies; 13+ messages in thread
From: Boris Ostrovsky @ 2016-12-16 16:39 UTC (permalink / raw)
  To: bp; +Cc: x86, linux-kernel, jgross, xen-devel, Boris Ostrovsky

When CONFIG_PARAVIRT is selected, cpuid() becomes a call. Since
for 32-bit kernels load_ucode_amd_bsp() is executed before paging
is enabled the call cannot be completed (as kernel virtual addresses
are not reachable yet).

Use native_cpuid() instead which is an asm wrapper for CPUID instruction.

Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
---
 arch/x86/kernel/cpu/microcode/amd.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 6f353bd..e2d941f 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -297,6 +297,7 @@ void __init load_ucode_amd_bsp(unsigned int family)
 	struct cpio_data cp;
 	const char *path;
 	bool use_pa;
+	u32 eax, ebx, ecx, edx;
 
 	if (IS_ENABLED(CONFIG_X86_32)) {
 		uci	= (struct ucode_cpu_info *)__pa_nodebug(ucode_cpu_info);
@@ -315,7 +316,10 @@ void __init load_ucode_amd_bsp(unsigned int family)
 		return;
 
 	/* Get BSP's CPUID.EAX(1), needed in load_microcode_amd() */
-	uci->cpu_sig.sig = cpuid_eax(1);
+	eax = 0x00000001;
+	ecx = 0;
+	native_cpuid(&eax, &ebx, &ecx, &edx);
+	uci->cpu_sig.sig = eax;
 
 	apply_microcode_early_amd(cp.data, cp.size, true);
 }
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 0/4] x86/microcode: Some 4.10 fixes
@ 2016-12-18 16:44 Borislav Petkov
  2016-12-18 16:44 ` [PATCH 1/4] x86/microcode/AMD: Make find_proper_container() sane again Borislav Petkov
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Borislav Petkov @ 2016-12-18 16:44 UTC (permalink / raw)
  To: X86 ML; +Cc: Boris Ostrovsky, Jürgen Gross, LKML

From: Borislav Petkov <bp@suse.de>

Hi,

here are 4 fixes for tip/urgent which address issues Boris and Jürgen
reported while testing latest upstream on xen.

As a result, let's just not load it at all when running on a hypervisor
and be done with the d*icking around.

Please apply,
thanks.

Boris Ostrovsky (1):
  x86/microcode/AMD: Use native_cpuid() in load_ucode_amd_bsp()

Borislav Petkov (3):
  x86/microcode/AMD: Make find_proper_container() sane again
  x86/microcode/AMD: Sanitize apply_microcode_early_amd()
  x86/microcode/AMD: Do not load when running on a hypervisor

 arch/x86/kernel/cpu/microcode/amd.c  | 56 +++++++++++++++++++++---------------
 arch/x86/kernel/cpu/microcode/core.c | 28 ++++++++++++------
 2 files changed, 52 insertions(+), 32 deletions(-)

-- 
2.11.0

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 1/4] x86/microcode/AMD: Make find_proper_container() sane again
  2016-12-18 16:44 [PATCH 0/4] x86/microcode: Some 4.10 fixes Borislav Petkov
@ 2016-12-18 16:44 ` Borislav Petkov
  2016-12-19  9:55   ` [tip:x86/urgent] " tip-bot for Borislav Petkov
  2016-12-18 16:44 ` [PATCH 2/4] x86/microcode/AMD: Sanitize apply_microcode_early_amd() Borislav Petkov
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 13+ messages in thread
From: Borislav Petkov @ 2016-12-18 16:44 UTC (permalink / raw)
  To: X86 ML; +Cc: Boris Ostrovsky, Jürgen Gross, LKML

From: Borislav Petkov <bp@suse.de>

Fixup signature and retvals, return the container struct through the
passed in pointer, not as a function return value.

Signed-off-by: Borislav Petkov <bp@suse.de>
---
 arch/x86/kernel/cpu/microcode/amd.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 6f353bdb3a25..31f4e3f94d46 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -116,10 +116,11 @@ static inline u16 find_equiv_id(struct equiv_cpu_entry *equiv_cpu_table,
 
 /*
  * This scans the ucode blob for the proper container as we can have multiple
- * containers glued together.
+ * containers glued together. Returns the equivalence ID from the equivalence
+ * table or 0 if none found.
  */
-static struct container
-find_proper_container(u8 *ucode, size_t size, u16 *ret_id)
+static u16
+find_proper_container(u8 *ucode, size_t size, struct container *ret_cont)
 {
 	struct container ret = { NULL, 0 };
 	u32 eax, ebx, ecx, edx;
@@ -138,7 +139,7 @@ find_proper_container(u8 *ucode, size_t size, u16 *ret_id)
 	if (header[0] != UCODE_MAGIC ||
 	    header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
 	    header[2] == 0)                            /* size */
-		return ret;
+		return eq_id;
 
 	eax = 0x00000001;
 	ecx = 0;
@@ -163,8 +164,9 @@ find_proper_container(u8 *ucode, size_t size, u16 *ret_id)
 			 * ucode update loop below
 			 */
 			left = ret.size - offset;
-			*ret_id = eq_id;
-			return ret;
+
+			*ret_cont = ret;
+			return eq_id;
 		}
 
 		/*
@@ -189,7 +191,7 @@ find_proper_container(u8 *ucode, size_t size, u16 *ret_id)
 		ucode     = data;
 	}
 
-	return ret;
+	return eq_id;
 }
 
 static int __apply_microcode_amd(struct microcode_amd *mc_amd)
@@ -237,7 +239,7 @@ apply_microcode_early_amd(void *ucode, size_t size, bool save_patch)
 	if (check_current_patch_level(&rev, true))
 		return (struct container){ NULL, 0 };
 
-	ret = find_proper_container(ucode, size, &eq_id);
+	eq_id = find_proper_container(ucode, size, &ret);
 	if (!eq_id)
 		return (struct container){ NULL, 0 };
 
@@ -443,7 +445,7 @@ int __init save_microcode_in_initrd_amd(unsigned int fam)
 				return -EINVAL;
 			}
 
-			cont = find_proper_container(cp.data, cp.size, &eq_id);
+			eq_id = find_proper_container(cp.data, cp.size, &cont);
 			if (!eq_id) {
 				cont.size = -1;
 				return -EINVAL;
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 2/4] x86/microcode/AMD: Sanitize apply_microcode_early_amd()
  2016-12-18 16:44 [PATCH 0/4] x86/microcode: Some 4.10 fixes Borislav Petkov
  2016-12-18 16:44 ` [PATCH 1/4] x86/microcode/AMD: Make find_proper_container() sane again Borislav Petkov
@ 2016-12-18 16:44 ` Borislav Petkov
  2016-12-19  9:55   ` [tip:x86/urgent] " tip-bot for Borislav Petkov
  2016-12-18 16:44 ` [PATCH 3/4] x86/microcode/AMD: Do not load when running on a hypervisor Borislav Petkov
  2016-12-18 16:44 ` [PATCH 4/4] x86/microcode/AMD: Use native_cpuid() in load_ucode_amd_bsp() Borislav Petkov
  3 siblings, 1 reply; 13+ messages in thread
From: Borislav Petkov @ 2016-12-18 16:44 UTC (permalink / raw)
  To: X86 ML; +Cc: Boris Ostrovsky, Jürgen Gross, LKML

From: Borislav Petkov <bp@suse.de>

Make it simply return bool to denote whether it found a container or not
and return the pointer to the container and its size in the handed-in
container pointer instead, as returning a struct was just silly.

Signed-off-by: Borislav Petkov <bp@suse.de>
---
 arch/x86/kernel/cpu/microcode/amd.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 31f4e3f94d46..dc80acfa922d 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -216,17 +216,18 @@ static int __apply_microcode_amd(struct microcode_amd *mc_amd)
  * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
  * load_microcode_amd() to save equivalent cpu table and microcode patches in
  * kernel heap memory.
+ *
+ * Returns true if container found (sets @ret_cont), false otherwise.
  */
-static struct container
-apply_microcode_early_amd(void *ucode, size_t size, bool save_patch)
+static bool apply_microcode_early_amd(void *ucode, size_t size, bool save_patch,
+				      struct container *ret_cont)
 {
-	struct container ret = { NULL, 0 };
 	u8 (*patch)[PATCH_MAX_SIZE];
+	u32 rev, *header, *new_rev;
+	struct container ret;
 	int offset, left;
-	u32 rev, *header;
-	u8  *data;
 	u16 eq_id = 0;
-	u32 *new_rev;
+	u8  *data;
 
 #ifdef CONFIG_X86_32
 	new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
@@ -237,11 +238,11 @@ apply_microcode_early_amd(void *ucode, size_t size, bool save_patch)
 #endif
 
 	if (check_current_patch_level(&rev, true))
-		return (struct container){ NULL, 0 };
+		return false;
 
 	eq_id = find_proper_container(ucode, size, &ret);
 	if (!eq_id)
-		return (struct container){ NULL, 0 };
+		return false;
 
 	this_equiv_id = eq_id;
 	header = (u32 *)ret.data;
@@ -275,7 +276,11 @@ apply_microcode_early_amd(void *ucode, size_t size, bool save_patch)
 		data   += offset;
 		left   -= offset;
 	}
-	return ret;
+
+	if (ret_cont)
+		*ret_cont = ret;
+
+	return true;
 }
 
 static bool get_builtin_microcode(struct cpio_data *cp, unsigned int family)
@@ -319,7 +324,7 @@ void __init load_ucode_amd_bsp(unsigned int family)
 	/* Get BSP's CPUID.EAX(1), needed in load_microcode_amd() */
 	uci->cpu_sig.sig = cpuid_eax(1);
 
-	apply_microcode_early_amd(cp.data, cp.size, true);
+	apply_microcode_early_amd(cp.data, cp.size, true, NULL);
 }
 
 #ifdef CONFIG_X86_32
@@ -351,7 +356,7 @@ void load_ucode_amd_ap(unsigned int family)
 	 * This would set amd_ucode_patch above so that the following APs can
 	 * use it directly instead of going down this path again.
 	 */
-	apply_microcode_early_amd(cp.data, cp.size, true);
+	apply_microcode_early_amd(cp.data, cp.size, true, NULL);
 }
 #else
 void load_ucode_amd_ap(unsigned int family)
@@ -389,8 +394,7 @@ void load_ucode_amd_ap(unsigned int family)
 			}
 		}
 
-		cont = apply_microcode_early_amd(cp.data, cp.size, false);
-		if (!(cont.data && cont.size)) {
+		if (!apply_microcode_early_amd(cp.data, cp.size, false, &cont)) {
 			cont.size = -1;
 			return;
 		}
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 3/4] x86/microcode/AMD: Do not load when running on a hypervisor
  2016-12-18 16:44 [PATCH 0/4] x86/microcode: Some 4.10 fixes Borislav Petkov
  2016-12-18 16:44 ` [PATCH 1/4] x86/microcode/AMD: Make find_proper_container() sane again Borislav Petkov
  2016-12-18 16:44 ` [PATCH 2/4] x86/microcode/AMD: Sanitize apply_microcode_early_amd() Borislav Petkov
@ 2016-12-18 16:44 ` Borislav Petkov
  2016-12-19  9:56   ` [tip:x86/urgent] " tip-bot for Borislav Petkov
  2016-12-18 16:44 ` [PATCH 4/4] x86/microcode/AMD: Use native_cpuid() in load_ucode_amd_bsp() Borislav Petkov
  3 siblings, 1 reply; 13+ messages in thread
From: Borislav Petkov @ 2016-12-18 16:44 UTC (permalink / raw)
  To: X86 ML; +Cc: Boris Ostrovsky, Jürgen Gross, LKML

From: Borislav Petkov <bp@suse.de>

Doing so is completely void of sense for multiple reasons so prevent
it. Set dis_ucode_ldr to true and thus disable the microcode loader by
default to address xen pv guests which execute the AP path but not the
BSP path.

By having it turned off by default, the APs won't run into the loader
either.

Also, check CPUID(1).ECX[31] which hypervisors set. Well almost, not the
xen pv one. That one gets the aforementioned "fix".

Also, improve the detection method by caching the final decision whether
to continue loading in dis_ucode_ldr and do it once on the BSP. The APs
then simply test that value.

Signed-off-by: Borislav Petkov <bp@suse.de>
Tested-by: Juergen Gross <jgross@suse.com>
Acked-by: Juergen Gross <jgross@suse.com>
Tested-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
---
 arch/x86/kernel/cpu/microcode/core.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c
index 6996413c78c3..c4bb2f7169f6 100644
--- a/arch/x86/kernel/cpu/microcode/core.c
+++ b/arch/x86/kernel/cpu/microcode/core.c
@@ -44,7 +44,7 @@
 #define DRIVER_VERSION	"2.2"
 
 static struct microcode_ops	*microcode_ops;
-static bool dis_ucode_ldr;
+static bool dis_ucode_ldr = true;
 
 LIST_HEAD(microcode_cache);
 
@@ -76,6 +76,7 @@ struct cpu_info_ctx {
 static bool __init check_loader_disabled_bsp(void)
 {
 	static const char *__dis_opt_str = "dis_ucode_ldr";
+	u32 a, b, c, d;
 
 #ifdef CONFIG_X86_32
 	const char *cmdline = (const char *)__pa_nodebug(boot_command_line);
@@ -88,8 +89,23 @@ static bool __init check_loader_disabled_bsp(void)
 	bool *res = &dis_ucode_ldr;
 #endif
 
-	if (cmdline_find_option_bool(cmdline, option))
-		*res = true;
+	if (!have_cpuid_p())
+		return *res;
+
+	a = 1;
+	c = 0;
+	native_cpuid(&a, &b, &c, &d);
+
+	/*
+	 * CPUID(1).ECX[31]: reserved for hypervisor use. This is still not
+	 * completely accurate as xen pv guests don't see that CPUID bit set but
+	 * that's good enough as they don't land on the BSP path anyway.
+	 */
+	if (c & BIT(31))
+		return *res;
+
+	if (cmdline_find_option_bool(cmdline, option) <= 0)
+		*res = false;
 
 	return *res;
 }
@@ -121,9 +137,6 @@ void __init load_ucode_bsp(void)
 	if (check_loader_disabled_bsp())
 		return;
 
-	if (!have_cpuid_p())
-		return;
-
 	vendor = x86_cpuid_vendor();
 	family = x86_cpuid_family();
 
@@ -157,9 +170,6 @@ void load_ucode_ap(void)
 	if (check_loader_disabled_ap())
 		return;
 
-	if (!have_cpuid_p())
-		return;
-
 	vendor = x86_cpuid_vendor();
 	family = x86_cpuid_family();
 
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 4/4] x86/microcode/AMD: Use native_cpuid() in load_ucode_amd_bsp()
  2016-12-18 16:44 [PATCH 0/4] x86/microcode: Some 4.10 fixes Borislav Petkov
                   ` (2 preceding siblings ...)
  2016-12-18 16:44 ` [PATCH 3/4] x86/microcode/AMD: Do not load when running on a hypervisor Borislav Petkov
@ 2016-12-18 16:44 ` Borislav Petkov
  2016-12-19  9:56   ` [tip:x86/urgent] " tip-bot for Boris Ostrovsky
  3 siblings, 1 reply; 13+ messages in thread
From: Borislav Petkov @ 2016-12-18 16:44 UTC (permalink / raw)
  To: X86 ML; +Cc: Boris Ostrovsky, Jürgen Gross, LKML

From: Boris Ostrovsky <boris.ostrovsky@oracle.com>

When CONFIG_PARAVIRT is selected, cpuid() becomes a call. Since
for 32-bit kernels load_ucode_amd_bsp() is executed before paging
is enabled the call cannot be completed (as kernel virtual addresses
are not reachable yet).

Use native_cpuid() instead which is an asm wrapper for the CPUID
instruction.

Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Jürgen Gross <jgross@suse.com>
Cc: x86-ml <x86@kernel.org>
Link: http://lkml.kernel.org/r/1481906392-3847-1-git-send-email-boris.ostrovsky@oracle.com
Signed-off-by: Borislav Petkov <bp@suse.de>
---
 arch/x86/kernel/cpu/microcode/amd.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index dc80acfa922d..6a31e2691f3a 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -301,6 +301,7 @@ static bool get_builtin_microcode(struct cpio_data *cp, unsigned int family)
 void __init load_ucode_amd_bsp(unsigned int family)
 {
 	struct ucode_cpu_info *uci;
+	u32 eax, ebx, ecx, edx;
 	struct cpio_data cp;
 	const char *path;
 	bool use_pa;
@@ -322,7 +323,10 @@ void __init load_ucode_amd_bsp(unsigned int family)
 		return;
 
 	/* Get BSP's CPUID.EAX(1), needed in load_microcode_amd() */
-	uci->cpu_sig.sig = cpuid_eax(1);
+	eax = 1;
+	ecx = 0;
+	native_cpuid(&eax, &ebx, &ecx, &edx);
+	uci->cpu_sig.sig = eax;
 
 	apply_microcode_early_amd(cp.data, cp.size, true, NULL);
 }
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [tip:x86/urgent] x86/microcode/AMD: Make find_proper_container() sane again
  2016-12-18 16:44 ` [PATCH 1/4] x86/microcode/AMD: Make find_proper_container() sane again Borislav Petkov
@ 2016-12-19  9:55   ` tip-bot for Borislav Petkov
  0 siblings, 0 replies; 13+ messages in thread
From: tip-bot for Borislav Petkov @ 2016-12-19  9:55 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: linux-kernel, jgross, mingo, bp, tglx, boris.ostrovsky, hpa

Commit-ID:  8feaa64a9a69652fdff87205f8a8cfe1bfd5b522
Gitweb:     http://git.kernel.org/tip/8feaa64a9a69652fdff87205f8a8cfe1bfd5b522
Author:     Borislav Petkov <bp@suse.de>
AuthorDate: Sun, 18 Dec 2016 17:44:11 +0100
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Mon, 19 Dec 2016 10:46:19 +0100

x86/microcode/AMD: Make find_proper_container() sane again

Fixup signature and retvals, return the container struct through the
passed in pointer, not as a function return value.

Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: Jürgen Gross <jgross@suse.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Link: http://lkml.kernel.org/r/20161218164414.9649-2-bp@alien8.de
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

---
 arch/x86/kernel/cpu/microcode/amd.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 6f353bd..31f4e3f 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -116,10 +116,11 @@ static inline u16 find_equiv_id(struct equiv_cpu_entry *equiv_cpu_table,
 
 /*
  * This scans the ucode blob for the proper container as we can have multiple
- * containers glued together.
+ * containers glued together. Returns the equivalence ID from the equivalence
+ * table or 0 if none found.
  */
-static struct container
-find_proper_container(u8 *ucode, size_t size, u16 *ret_id)
+static u16
+find_proper_container(u8 *ucode, size_t size, struct container *ret_cont)
 {
 	struct container ret = { NULL, 0 };
 	u32 eax, ebx, ecx, edx;
@@ -138,7 +139,7 @@ find_proper_container(u8 *ucode, size_t size, u16 *ret_id)
 	if (header[0] != UCODE_MAGIC ||
 	    header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
 	    header[2] == 0)                            /* size */
-		return ret;
+		return eq_id;
 
 	eax = 0x00000001;
 	ecx = 0;
@@ -163,8 +164,9 @@ find_proper_container(u8 *ucode, size_t size, u16 *ret_id)
 			 * ucode update loop below
 			 */
 			left = ret.size - offset;
-			*ret_id = eq_id;
-			return ret;
+
+			*ret_cont = ret;
+			return eq_id;
 		}
 
 		/*
@@ -189,7 +191,7 @@ find_proper_container(u8 *ucode, size_t size, u16 *ret_id)
 		ucode     = data;
 	}
 
-	return ret;
+	return eq_id;
 }
 
 static int __apply_microcode_amd(struct microcode_amd *mc_amd)
@@ -237,7 +239,7 @@ apply_microcode_early_amd(void *ucode, size_t size, bool save_patch)
 	if (check_current_patch_level(&rev, true))
 		return (struct container){ NULL, 0 };
 
-	ret = find_proper_container(ucode, size, &eq_id);
+	eq_id = find_proper_container(ucode, size, &ret);
 	if (!eq_id)
 		return (struct container){ NULL, 0 };
 
@@ -443,7 +445,7 @@ int __init save_microcode_in_initrd_amd(unsigned int fam)
 				return -EINVAL;
 			}
 
-			cont = find_proper_container(cp.data, cp.size, &eq_id);
+			eq_id = find_proper_container(cp.data, cp.size, &cont);
 			if (!eq_id) {
 				cont.size = -1;
 				return -EINVAL;

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [tip:x86/urgent] x86/microcode/AMD: Sanitize apply_microcode_early_amd()
  2016-12-18 16:44 ` [PATCH 2/4] x86/microcode/AMD: Sanitize apply_microcode_early_amd() Borislav Petkov
@ 2016-12-19  9:55   ` tip-bot for Borislav Petkov
  0 siblings, 0 replies; 13+ messages in thread
From: tip-bot for Borislav Petkov @ 2016-12-19  9:55 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: linux-kernel, mingo, tglx, jgross, boris.ostrovsky, hpa, bp

Commit-ID:  200d3553163f6065a0f1f142f92d1cf716d586c2
Gitweb:     http://git.kernel.org/tip/200d3553163f6065a0f1f142f92d1cf716d586c2
Author:     Borislav Petkov <bp@suse.de>
AuthorDate: Sun, 18 Dec 2016 17:44:12 +0100
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Mon, 19 Dec 2016 10:46:20 +0100

x86/microcode/AMD: Sanitize apply_microcode_early_amd()

Make it simply return bool to denote whether it found a container or not
and return the pointer to the container and its size in the handed-in
container pointer instead, as returning a struct was just silly.

Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: Jürgen Gross <jgross@suse.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Link: http://lkml.kernel.org/r/20161218164414.9649-3-bp@alien8.de
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

---
 arch/x86/kernel/cpu/microcode/amd.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 31f4e3f..dc80acf 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -216,17 +216,18 @@ static int __apply_microcode_amd(struct microcode_amd *mc_amd)
  * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
  * load_microcode_amd() to save equivalent cpu table and microcode patches in
  * kernel heap memory.
+ *
+ * Returns true if container found (sets @ret_cont), false otherwise.
  */
-static struct container
-apply_microcode_early_amd(void *ucode, size_t size, bool save_patch)
+static bool apply_microcode_early_amd(void *ucode, size_t size, bool save_patch,
+				      struct container *ret_cont)
 {
-	struct container ret = { NULL, 0 };
 	u8 (*patch)[PATCH_MAX_SIZE];
+	u32 rev, *header, *new_rev;
+	struct container ret;
 	int offset, left;
-	u32 rev, *header;
-	u8  *data;
 	u16 eq_id = 0;
-	u32 *new_rev;
+	u8  *data;
 
 #ifdef CONFIG_X86_32
 	new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
@@ -237,11 +238,11 @@ apply_microcode_early_amd(void *ucode, size_t size, bool save_patch)
 #endif
 
 	if (check_current_patch_level(&rev, true))
-		return (struct container){ NULL, 0 };
+		return false;
 
 	eq_id = find_proper_container(ucode, size, &ret);
 	if (!eq_id)
-		return (struct container){ NULL, 0 };
+		return false;
 
 	this_equiv_id = eq_id;
 	header = (u32 *)ret.data;
@@ -275,7 +276,11 @@ apply_microcode_early_amd(void *ucode, size_t size, bool save_patch)
 		data   += offset;
 		left   -= offset;
 	}
-	return ret;
+
+	if (ret_cont)
+		*ret_cont = ret;
+
+	return true;
 }
 
 static bool get_builtin_microcode(struct cpio_data *cp, unsigned int family)
@@ -319,7 +324,7 @@ void __init load_ucode_amd_bsp(unsigned int family)
 	/* Get BSP's CPUID.EAX(1), needed in load_microcode_amd() */
 	uci->cpu_sig.sig = cpuid_eax(1);
 
-	apply_microcode_early_amd(cp.data, cp.size, true);
+	apply_microcode_early_amd(cp.data, cp.size, true, NULL);
 }
 
 #ifdef CONFIG_X86_32
@@ -351,7 +356,7 @@ void load_ucode_amd_ap(unsigned int family)
 	 * This would set amd_ucode_patch above so that the following APs can
 	 * use it directly instead of going down this path again.
 	 */
-	apply_microcode_early_amd(cp.data, cp.size, true);
+	apply_microcode_early_amd(cp.data, cp.size, true, NULL);
 }
 #else
 void load_ucode_amd_ap(unsigned int family)
@@ -389,8 +394,7 @@ reget:
 			}
 		}
 
-		cont = apply_microcode_early_amd(cp.data, cp.size, false);
-		if (!(cont.data && cont.size)) {
+		if (!apply_microcode_early_amd(cp.data, cp.size, false, &cont)) {
 			cont.size = -1;
 			return;
 		}

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [tip:x86/urgent] x86/microcode/AMD: Do not load when running on a hypervisor
  2016-12-18 16:44 ` [PATCH 3/4] x86/microcode/AMD: Do not load when running on a hypervisor Borislav Petkov
@ 2016-12-19  9:56   ` tip-bot for Borislav Petkov
  0 siblings, 0 replies; 13+ messages in thread
From: tip-bot for Borislav Petkov @ 2016-12-19  9:56 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: jgross, bp, boris.ostrovsky, tglx, hpa, linux-kernel, mingo

Commit-ID:  a15a753539eca8ba243d576f02e7ca9c4b7d7042
Gitweb:     http://git.kernel.org/tip/a15a753539eca8ba243d576f02e7ca9c4b7d7042
Author:     Borislav Petkov <bp@suse.de>
AuthorDate: Sun, 18 Dec 2016 17:44:13 +0100
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Mon, 19 Dec 2016 10:46:20 +0100

x86/microcode/AMD: Do not load when running on a hypervisor

Doing so is completely void of sense for multiple reasons so prevent
it. Set dis_ucode_ldr to true and thus disable the microcode loader by
default to address xen pv guests which execute the AP path but not the
BSP path.

By having it turned off by default, the APs won't run into the loader
either.

Also, check CPUID(1).ECX[31] which hypervisors set. Well almost, not the
xen pv one. That one gets the aforementioned "fix".

Also, improve the detection method by caching the final decision whether
to continue loading in dis_ucode_ldr and do it once on the BSP. The APs
then simply test that value.

Signed-off-by: Borislav Petkov <bp@suse.de>
Tested-by: Juergen Gross <jgross@suse.com>
Tested-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Acked-by: Juergen Gross <jgross@suse.com>
Link: http://lkml.kernel.org/r/20161218164414.9649-4-bp@alien8.de
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

---
 arch/x86/kernel/cpu/microcode/core.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c
index 6996413..c4bb2f7 100644
--- a/arch/x86/kernel/cpu/microcode/core.c
+++ b/arch/x86/kernel/cpu/microcode/core.c
@@ -44,7 +44,7 @@
 #define DRIVER_VERSION	"2.2"
 
 static struct microcode_ops	*microcode_ops;
-static bool dis_ucode_ldr;
+static bool dis_ucode_ldr = true;
 
 LIST_HEAD(microcode_cache);
 
@@ -76,6 +76,7 @@ struct cpu_info_ctx {
 static bool __init check_loader_disabled_bsp(void)
 {
 	static const char *__dis_opt_str = "dis_ucode_ldr";
+	u32 a, b, c, d;
 
 #ifdef CONFIG_X86_32
 	const char *cmdline = (const char *)__pa_nodebug(boot_command_line);
@@ -88,8 +89,23 @@ static bool __init check_loader_disabled_bsp(void)
 	bool *res = &dis_ucode_ldr;
 #endif
 
-	if (cmdline_find_option_bool(cmdline, option))
-		*res = true;
+	if (!have_cpuid_p())
+		return *res;
+
+	a = 1;
+	c = 0;
+	native_cpuid(&a, &b, &c, &d);
+
+	/*
+	 * CPUID(1).ECX[31]: reserved for hypervisor use. This is still not
+	 * completely accurate as xen pv guests don't see that CPUID bit set but
+	 * that's good enough as they don't land on the BSP path anyway.
+	 */
+	if (c & BIT(31))
+		return *res;
+
+	if (cmdline_find_option_bool(cmdline, option) <= 0)
+		*res = false;
 
 	return *res;
 }
@@ -121,9 +137,6 @@ void __init load_ucode_bsp(void)
 	if (check_loader_disabled_bsp())
 		return;
 
-	if (!have_cpuid_p())
-		return;
-
 	vendor = x86_cpuid_vendor();
 	family = x86_cpuid_family();
 
@@ -157,9 +170,6 @@ void load_ucode_ap(void)
 	if (check_loader_disabled_ap())
 		return;
 
-	if (!have_cpuid_p())
-		return;
-
 	vendor = x86_cpuid_vendor();
 	family = x86_cpuid_family();
 

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [tip:x86/urgent] x86/microcode/AMD: Use native_cpuid() in load_ucode_amd_bsp()
  2016-12-18 16:44 ` [PATCH 4/4] x86/microcode/AMD: Use native_cpuid() in load_ucode_amd_bsp() Borislav Petkov
@ 2016-12-19  9:56   ` tip-bot for Boris Ostrovsky
  0 siblings, 0 replies; 13+ messages in thread
From: tip-bot for Boris Ostrovsky @ 2016-12-19  9:56 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: hpa, jgross, tglx, boris.ostrovsky, mingo, bp, linux-kernel

Commit-ID:  2b4c91569a40c4512ea1b413e0c817d179ce9868
Gitweb:     http://git.kernel.org/tip/2b4c91569a40c4512ea1b413e0c817d179ce9868
Author:     Boris Ostrovsky <boris.ostrovsky@oracle.com>
AuthorDate: Sun, 18 Dec 2016 17:44:14 +0100
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Mon, 19 Dec 2016 10:46:20 +0100

x86/microcode/AMD: Use native_cpuid() in load_ucode_amd_bsp()

When CONFIG_PARAVIRT is selected, cpuid() becomes a call. Since
for 32-bit kernels load_ucode_amd_bsp() is executed before paging
is enabled the call cannot be completed (as kernel virtual addresses
are not reachable yet).

Use native_cpuid() instead which is an asm wrapper for the CPUID
instruction.

Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: Jürgen Gross <jgross@suse.com>
Link: http://lkml.kernel.org/r/1481906392-3847-1-git-send-email-boris.ostrovsky@oracle.com
Link: http://lkml.kernel.org/r/20161218164414.9649-5-bp@alien8.de
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

---
 arch/x86/kernel/cpu/microcode/amd.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index dc80acf..6a31e26 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -301,6 +301,7 @@ static bool get_builtin_microcode(struct cpio_data *cp, unsigned int family)
 void __init load_ucode_amd_bsp(unsigned int family)
 {
 	struct ucode_cpu_info *uci;
+	u32 eax, ebx, ecx, edx;
 	struct cpio_data cp;
 	const char *path;
 	bool use_pa;
@@ -322,7 +323,10 @@ void __init load_ucode_amd_bsp(unsigned int family)
 		return;
 
 	/* Get BSP's CPUID.EAX(1), needed in load_microcode_amd() */
-	uci->cpu_sig.sig = cpuid_eax(1);
+	eax = 1;
+	ecx = 0;
+	native_cpuid(&eax, &ebx, &ecx, &edx);
+	uci->cpu_sig.sig = eax;
 
 	apply_microcode_early_amd(cp.data, cp.size, true, NULL);
 }

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH] x86/microcode: Use native_cpuid() in load_ucode_amd_bsp()
@ 2016-12-16 16:39 Boris Ostrovsky
  0 siblings, 0 replies; 13+ messages in thread
From: Boris Ostrovsky @ 2016-12-16 16:39 UTC (permalink / raw)
  To: bp; +Cc: jgross, xen-devel, Boris Ostrovsky, x86, linux-kernel

When CONFIG_PARAVIRT is selected, cpuid() becomes a call. Since
for 32-bit kernels load_ucode_amd_bsp() is executed before paging
is enabled the call cannot be completed (as kernel virtual addresses
are not reachable yet).

Use native_cpuid() instead which is an asm wrapper for CPUID instruction.

Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
---
 arch/x86/kernel/cpu/microcode/amd.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 6f353bd..e2d941f 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -297,6 +297,7 @@ void __init load_ucode_amd_bsp(unsigned int family)
 	struct cpio_data cp;
 	const char *path;
 	bool use_pa;
+	u32 eax, ebx, ecx, edx;
 
 	if (IS_ENABLED(CONFIG_X86_32)) {
 		uci	= (struct ucode_cpu_info *)__pa_nodebug(ucode_cpu_info);
@@ -315,7 +316,10 @@ void __init load_ucode_amd_bsp(unsigned int family)
 		return;
 
 	/* Get BSP's CPUID.EAX(1), needed in load_microcode_amd() */
-	uci->cpu_sig.sig = cpuid_eax(1);
+	eax = 0x00000001;
+	ecx = 0;
+	native_cpuid(&eax, &ebx, &ecx, &edx);
+	uci->cpu_sig.sig = eax;
 
 	apply_microcode_early_amd(cp.data, cp.size, true);
 }
-- 
1.7.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2016-12-19  9:57 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-16 16:39 [PATCH] x86/microcode: Use native_cpuid() in load_ucode_amd_bsp() Boris Ostrovsky
2016-12-16 16:37 ` Borislav Petkov
2016-12-16 16:37 ` Borislav Petkov
2016-12-16 16:39 Boris Ostrovsky
2016-12-18 16:44 [PATCH 0/4] x86/microcode: Some 4.10 fixes Borislav Petkov
2016-12-18 16:44 ` [PATCH 1/4] x86/microcode/AMD: Make find_proper_container() sane again Borislav Petkov
2016-12-19  9:55   ` [tip:x86/urgent] " tip-bot for Borislav Petkov
2016-12-18 16:44 ` [PATCH 2/4] x86/microcode/AMD: Sanitize apply_microcode_early_amd() Borislav Petkov
2016-12-19  9:55   ` [tip:x86/urgent] " tip-bot for Borislav Petkov
2016-12-18 16:44 ` [PATCH 3/4] x86/microcode/AMD: Do not load when running on a hypervisor Borislav Petkov
2016-12-19  9:56   ` [tip:x86/urgent] " tip-bot for Borislav Petkov
2016-12-18 16:44 ` [PATCH 4/4] x86/microcode/AMD: Use native_cpuid() in load_ucode_amd_bsp() Borislav Petkov
2016-12-19  9:56   ` [tip:x86/urgent] " tip-bot for Boris Ostrovsky

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.