x86/microcode: Early load microcode

Started by Fenghua Yu on Nov 30, 2012 · 126 messages from 6 authors · Last reply Dec 20, 2012

Series

10 patches, 14 files

+915 −181

Subsystems (4)

Show 1 more

Maintainers (8)

Show 4 more
STALE5084d SUPERSEDED

Status: Superseded by v5 (posted 2012-12-21). [go to v5]

Revision v2 (5 revisions archived).

Revisions (5)
  1. v1
  2. v2 current [diff against previous]
  3. v3 [diff against previous]
  4. v4 [diff against previous]
  5. v5 [diff against previous]

page 1 of 2 · open the first message on its own page

  1. [PATCH v2 03/10] x86/microcode_core_early.c: Define interfaces for early loading ucode
    Revisions and related patches
    Revisions (5)
    1. v1
    2. v2 current [diff against previous]
    3. v3 [diff against previous]
    4. v4 [diff against previous]
    5. v5 [diff against previous]
  2. [PATCH v2 06/10] x86/head_32.S: Early update ucode in 32-bit
    Revisions and related patches
    Revisions (5)
    1. v1
    2. v2 current [diff against previous]
    3. v3 [diff against previous]
    4. v4 [diff against previous]
    5. v5 [diff against previous]
  3. [PATCH v2 04/10] x86/microcode_intel_lib.c: Early update ucode on Intel's CPU
    Revisions and related patches
    Revisions (5)
    1. v1
    2. v2 current [diff against previous]
    3. v3 [diff against previous]
    4. v4 [diff against previous]
    5. v5 [diff against previous]
  4. [PATCH v2 10/10] x86/Kconfig: Configurations to enable/disable the feature
    Revisions and related patches
    Revisions (5)
    1. v1
    2. v2 current [diff against previous]
    3. v3 [diff against previous]
    4. v4 [diff against previous]
    5. v5 [diff against previous]
  5. [PATCH v2 09/10] x86/mm/init.c: Copy ucode from initrd image to memory
    Revisions and related patches
    Revisions (5)
    1. v1
    2. v2 current [diff against previous]
    3. v3 [diff against previous]
    4. v4 [diff against previous]
    5. v5 [diff against previous]
  6. [PATCH v2 08/10] x86/smpboot.c: Early update ucode on AP
    Revisions and related patches
    Revisions (5)
    1. v1
    2. v2 current [diff against previous]
    3. v3 [diff against previous]
    4. v4 [diff against previous]
    5. v5 [diff against previous]
  7. [PATCH v2 07/10] x86/head64.c: Early update ucode in 64-bit
    Revisions and related patches
    Revisions (5)
    1. v1
    2. v2 current [diff against previous]
    3. v3 [diff against previous]
    4. v4 [diff against previous]
    5. v5 [diff against previous]
  8. [PATCH v2 05/10] x86/microcode_intel_early.c: Early update ucode on Intel's CPU
    Revisions and related patches
    Revisions (5)
    1. v1
    2. v2 current [diff against previous]
    3. v3 [diff against previous]
    4. v4 [diff against previous]
    5. v5 [diff against previous]
  9. [PATCH v2 02/10] x86/microcode_intel.h: Define functions and macros for early loading ucode
    Revisions and related patches
    Revisions (5)
    1. v1
    2. v2 current [diff against previous]
    3. v3 [diff against previous]
    4. v4 [diff against previous]
    5. v5 [diff against previous]
  10. [PATCH v2 01/10] Documentation/x86: Early load microcode
    Revisions and related patches
    Revisions (5)
    1. v1
    2. v2 current [diff against previous]
    3. v3 [diff against previous]
    4. v4 [diff against previous]
    5. v5 [diff against previous]
Message details and related content
From: Fenghua Yu <redacted>

The problem in current microcode loading method is that we load a microcode way,
way too late; ideally we should load it before turning paging on.  This may only
be practical on 32 bits since we can't get to 64-bit mode without paging on,
but we should still do it as early as at all possible.

Similarly, we should load the microcode update as early as possible during AP
bringup and when processors are brought back online after hotplug or S3/S4.

In order to do that, the microcode patch needs to be permanently present in
kernel memory.  Each individual patch is fairly small, so that is OK, but the
entire blob with support for each CPU is too big. Since only CPU's with same
model can be in the same platform, we store microcode with the same model as
BSP. Later on AP's can upload microcode from the saved microcodep patches.

Note, however, that Linux users have gotten used to being able to install a
microcode patch in the field without having a reboot; we support that model too.

v2: Detect vendor before loading microcode. Move some functions from
microcode_intel_early.c to microcode_intel_lib.c. Change some early loading
microcode dependencies in Kconfig. Reword doc.

Fenghua Yu (10):
  Documentation/x86: Early load microcode
  x86/microcode_intel.h: Define functions and macros for early load
    ucode
  x86/microcode_core_early.c: Define interfaces for early load ucode
  x86/microcode_intel_lib.c: Early update ucode on Intel's CPU
  x86/microcode_intel_early.c: Early update ucode on Intel's CPU
  x86/head_32.S: Early update ucode in 32-bit
  x86/head64.c: Early update ucode in 64-bit
  x86/smpboot.c: Early update ucode on AP
  x86/mm/init.c: Copy ucode from initrd image to memory
  x86/Kconfig: Configurations to enable/disable the feature

 Documentation/x86/early-microcode.txt   |   43 +++
 arch/x86/Kconfig                        |   18 ++
 arch/x86/include/asm/microcode.h        |   23 ++
 arch/x86/include/asm/microcode_intel.h  |  106 ++++++++

20 more lines

[PATCH v2 03/10] x86/microcode_core_early.c: Define interfaces for early loading ucode

Message details and related content
From: Fenghua Yu <redacted>

Define interfaces load_ucode_bsp() and load_ucode_ap() to load ucode on BSP and
AP in early boot time. These are generic interfaces. Internally they call
vendor specific implementations.

Signed-off-by: Fenghua Yu <redacted>
---
 arch/x86/include/asm/microcode.h       |   23 ++++++++++
 arch/x86/kernel/microcode_core_early.c |   70 ++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+), 0 deletions(-)
 create mode 100644 arch/x86/kernel/microcode_core_early.c
diff --git a/arch/x86/include/asm/microcode.h b/arch/x86/include/asm/microcode.h
index 43d921b..2e2ff3a 100644
--- a/arch/x86/include/asm/microcode.h
+++ b/arch/x86/include/asm/microcode.h
@@ -57,4 +57,27 @@ static inline struct microcode_ops * __init init_amd_microcode(void)
 static inline void __exit exit_amd_microcode(void) {}
 #endif
 
+struct mc_saved_data {
+	unsigned int mc_saved_count;
+	struct microcode_intel **mc_saved;
+	struct ucode_cpu_info *ucode_cpu_info;
+};
+#ifdef CONFIG_MICROCODE_EARLY
+#define MAX_UCODE_COUNT 128
+extern struct ucode_cpu_info ucode_cpu_info_early[NR_CPUS];
+extern struct microcode_intel __initdata *mc_saved_in_initrd[MAX_UCODE_COUNT];
+extern struct mc_saved_data mc_saved_data;
+extern void __init load_ucode_bsp(char *real_mode_data);
+extern __init void load_ucode_ap(void);
+extern void __init
+save_microcode_in_initrd(struct mc_saved_data *mc_saved_data,
+			 struct microcode_intel **mc_saved_in_initrd);
+#else
+static inline void __init load_ucode_bsp(char *real_mode_data) {}
+static inline __init void load_ucode_ap(void) {}
+static inline void __init
+save_microcode_in_initrd(struct mc_saved_data *mc_saved_data,
+			 struct microcode_intel **mc_saved_in_initrd) {}
+#endif
+
 #endif /* _ASM_X86_MICROCODE_H */
diff --git a/arch/x86/kernel/microcode_core_early.c b/arch/x86/kernel/microcode_core_early.c
new file mode 100644
index 0000000..1c6cc8f
--- /dev/null
+++ b/arch/x86/kernel/microcode_core_early.c
@@ -0,0 +1,70 @@
+/*
+ *	X86 CPU microcode early update for Linux
+ *
+ *	Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
+ *			   H Peter Anvin" <hpa@zytor.com>
+ *
+ *	This driver allows to early upgrade microcode on Intel processors
+ *	belonging to IA-32 family - PentiumPro, Pentium II,
+ *	Pentium III, Xeon, Pentium 4, etc.
+ *
+ *	Reference: Section 9.11 of Volume 3, IA-32 Intel Architecture
+ *	Software Developer's Manual.
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License
+ *	as published by the Free Software Foundation; either version
+ *	2 of the License, or (at your option) any later version.
+ */
+#include <linux/module.h>
+#include <linux/mm.h>
+#include <asm/microcode_intel.h>
+#include <asm/processor.h>
+
+struct ucode_cpu_info	ucode_cpu_info_early[NR_CPUS];
+EXPORT_SYMBOL_GPL(ucode_cpu_info_early);
+
+static inline int __init x86_vendor(void)
+{
+	unsigned int eax = 0x00000000;
+	char x86_vendor_id[16];
+	int i;
+	struct {
+		char x86_vendor_id[16];
+		__u8 x86_vendor;
+	} cpu_vendor_table[] = {
+		{ "GenuineIntel", X86_VENDOR_INTEL },
+		{ "AuthenticAMD", X86_VENDOR_AMD },
+	};
+
+	memset(x86_vendor_id, 0, ARRAY_SIZE(x86_vendor_id));
+	/* Get vendor name */
+	native_cpuid(&eax,
+		(unsigned int *)&x86_vendor_id[0],
+		(unsigned int *)&x86_vendor_id[8],
+		(unsigned int *)&x86_vendor_id[4]);
+
+	for (i = 0; i < ARRAY_SIZE(cpu_vendor_table); i++) {
+		if (!strcmp(x86_vendor_id, cpu_vendor_table[i].x86_vendor_id))
+			return cpu_vendor_table[i].x86_vendor;
+	}
+
+	return X86_VENDOR_UNKNOWN;
+}
+
+
+void __init load_ucode_bsp(char *real_mode_data)
+{
+	/*
+	 * boot_cpu_data is not setup yet in this early phase.
+	 * So we get vendor information directly through cpuid.
+	 */
+	if (x86_vendor() == X86_VENDOR_INTEL)
+		load_ucode_intel_bsp(real_mode_data);
+}
+
+void __cpuinit load_ucode_ap(void)
+{
+	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
+		load_ucode_intel_ap();
+}
-- 
1.7.2

[PATCH v2 06/10] x86/head_32.S: Early update ucode in 32-bit

Message details and related content
From: Fenghua Yu <redacted>

This updates ucode in 32-bit kernel. At this point, there is no paging and no
virtual address yet.

Signed-off-by: Fenghua Yu <redacted>
---
 arch/x86/kernel/head_32.S |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/arch/x86/kernel/head_32.S b/arch/x86/kernel/head_32.S
index 957a47a..b7b76a4 100644
--- a/arch/x86/kernel/head_32.S
+++ b/arch/x86/kernel/head_32.S
@@ -144,6 +144,12 @@ ENTRY(startup_32)
 	movl %eax, pa(olpc_ofw_pgd)
 #endif
 
+#ifdef CONFIG_MICROCODE_EARLY
+	/* Early load ucode on BSP. */
+	movl $pa(boot_params), %eax
+	call load_ucode_bsp
+#endif
+
 /*
  * Initialize page tables.  This creates a PDE and a set of page
  * tables, which are located immediately beyond __brk_base.  The variable
-- 
1.7.2

[PATCH v2 04/10] x86/microcode_intel_lib.c: Early update ucode on Intel's CPU

Message details and related content
From: Fenghua Yu <redacted>

Define interfaces microcode_sanity_check() and get_matching_microcode(). They
are called both in early boot time and in microcode Intel driver.

Signed-off-by: Fenghua Yu <redacted>
---
 arch/x86/kernel/microcode_intel_lib.c |  174 +++++++++++++++++++++++++++++++++
 1 files changed, 174 insertions(+), 0 deletions(-)
 create mode 100644 arch/x86/kernel/microcode_intel_lib.c
diff --git a/arch/x86/kernel/microcode_intel_lib.c b/arch/x86/kernel/microcode_intel_lib.c
new file mode 100644
index 0000000..ce69320
--- /dev/null
+++ b/arch/x86/kernel/microcode_intel_lib.c
@@ -0,0 +1,174 @@
+/*
+ *	Intel CPU Microcode Update Driver for Linux
+ *
+ *	Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
+ *			   H Peter Anvin" <hpa@zytor.com>
+ *
+ *	This driver allows to upgrade microcode on Intel processors
+ *	belonging to IA-32 family - PentiumPro, Pentium II,
+ *	Pentium III, Xeon, Pentium 4, etc.
+ *
+ *	Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
+ *	Software Developer's Manual
+ *	Order Number 253668 or free download from:
+ *
+ *	http://developer.intel.com/Assets/PDF/manual/253668.pdf
+ *
+ *	For more information, go to http://www.urbanmyth.org/microcode
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License
+ *	as published by the Free Software Foundation; either version
+ *	2 of the License, or (at your option) any later version.
+ *
+ */
+#include <linux/firmware.h>
+#include <linux/uaccess.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#include <asm/microcode_intel.h>
+#include <asm/processor.h>
+#include <asm/msr.h>
+
+static inline int
+update_match_cpu(unsigned int csig, unsigned int cpf,
+		 unsigned int sig, unsigned int pf)
+{
+	return (!sigmatch(sig, csig, pf, cpf)) ? 0 : 1;
+}
+
+int
+update_match_revision(struct microcode_header_intel *mc_header, int rev)
+{
+	return (mc_header->rev <= rev) ? 0 : 1;
+}
+
+int microcode_sanity_check(void *mc, int print_err)
+{
+	unsigned long total_size, data_size, ext_table_size;
+	struct microcode_header_intel *mc_header = mc;
+	struct extended_sigtable *ext_header = NULL;
+	int sum, orig_sum, ext_sigcount = 0, i;
+	struct extended_signature *ext_sig;
+
+	total_size = get_totalsize(mc_header);
+	data_size = get_datasize(mc_header);
+
+	if (data_size + MC_HEADER_SIZE > total_size) {
+		if (print_err)
+			pr_err("error! Bad data size in microcode data file\n");
+		return -EINVAL;
+	}
+
+	if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
+		if (print_err)
+			pr_err("error! Unknown microcode update format\n");
+		return -EINVAL;
+	}
+	ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
+	if (ext_table_size) {
+		if ((ext_table_size < EXT_HEADER_SIZE)
+		 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
+			if (print_err)
+				pr_err("error! Small exttable size in microcode data file\n");
+			return -EINVAL;
+		}
+		ext_header = mc + MC_HEADER_SIZE + data_size;
+		if (ext_table_size != exttable_size(ext_header)) {
+			if (print_err)
+				pr_err("error! Bad exttable size in microcode data file\n");
+			return -EFAULT;
+		}
+		ext_sigcount = ext_header->count;
+	}
+
+	/* check extended table checksum */
+	if (ext_table_size) {
+		int ext_table_sum = 0;
+		int *ext_tablep = (int *)ext_header;
+
+		i = ext_table_size / DWSIZE;
+		while (i--)
+			ext_table_sum += ext_tablep[i];
+		if (ext_table_sum) {
+			if (print_err)
+				pr_warn("aborting, bad extended signature table checksum\n");
+			return -EINVAL;
+		}
+	}
+
+	/* calculate the checksum */
+	orig_sum = 0;
+	i = (MC_HEADER_SIZE + data_size) / DWSIZE;
+	while (i--)
+		orig_sum += ((int *)mc)[i];
+	if (orig_sum) {
+		if (print_err)
+			pr_err("aborting, bad checksum\n");
+		return -EINVAL;
+	}
+	if (!ext_table_size)
+		return 0;
+	/* check extended signature checksum */
+	for (i = 0; i < ext_sigcount; i++) {
+		ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
+			  EXT_SIGNATURE_SIZE * i;
+		sum = orig_sum
+			- (mc_header->sig + mc_header->pf + mc_header->cksum)
+			+ (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
+		if (sum) {
+			if (print_err)
+				pr_err("aborting, bad checksum\n");
+			return -EINVAL;
+		}
+	}
+	return 0;
+}
+EXPORT_SYMBOL_GPL(microcode_sanity_check);
+
+/*
+ * return 0 - no update found
+ * return 1 - found update
+ */
+int get_matching_sig(unsigned int csig, int cpf, void *mc, int rev)
+{
+	struct microcode_header_intel *mc_header = mc;
+	struct extended_sigtable *ext_header;
+	unsigned long total_size = get_totalsize(mc_header);
+	int ext_sigcount, i;
+	struct extended_signature *ext_sig;
+
+	if (update_match_cpu(csig, cpf, mc_header->sig, mc_header->pf))
+		return 1;
+
+	/* Look for ext. headers: */
+	if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
+		return 0;
+
+	ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
+	ext_sigcount = ext_header->count;
+	ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
+
+	for (i = 0; i < ext_sigcount; i++) {
+		if (update_match_cpu(csig, cpf, ext_sig->sig, ext_sig->pf))
+			return 1;
+		ext_sig++;
+	}
+	return 0;
+}
+
+/*
+ * return 0 - no update found
+ * return 1 - found update
+ */
+int get_matching_microcode(unsigned int csig, int cpf, void *mc, int rev)
+{
+	struct microcode_header_intel *mc_header = mc;
+
+	if (!update_match_revision(mc_header, rev))
+		return 0;
+
+	return get_matching_sig(csig, cpf, mc, rev);
+}
+EXPORT_SYMBOL_GPL(get_matching_microcode);
-- 
1.7.2

[PATCH v2 10/10] x86/Kconfig: Configurations to enable/disable the feature

Message details and related content
From: Fenghua Yu <redacted>

MICROCODE_INTEL_LIB, MICROCODE_INTEL_EARLY, and MICROCODE_EARLY are three new
configurations to enable or disable the feature.

Signed-off-by: Fenghua Yu <redacted>
---
 arch/x86/Kconfig |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index ab62cd5..7c60a0f 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -1064,6 +1064,24 @@ config MICROCODE_OLD_INTERFACE
 	def_bool y
 	depends on MICROCODE
 
+config MICROCODE_INTEL_LIB
+	def_bool y
+	depends on MICROCODE_INTEL
+
+config MICROCODE_INTEL_EARLY
+	bool "Early load microcode"
+	depends on MICROCODE_INTEL && BLK_DEV_INITRD
+	default y
+	help
+	  This option provides functionality to read additional microcode data
+	  at the beginning of initrd image. The data tells kernel to load
+	  microcode to CPU's as early as possible. No functional change if no
+	  microcode data is glued to the initrd, therefore it's safe to say Y.
+
+config MICROCODE_EARLY
+	def_bool y
+	depends on MICROCODE_INTEL_EARLY
+
 config X86_MSR
 	tristate "/dev/cpu/*/msr - Model-specific register support"
 	---help---
-- 
1.7.2

[PATCH v2 09/10] x86/mm/init.c: Copy ucode from initrd image to memory

Message details and related content
From: Fenghua Yu <redacted>

Before initrd image is freed, copy valid ucode patches from initrd image
to kernel virtual memory. The saved ucode will be used to update AP in resume.

Signed-off-by: Fenghua Yu <redacted>
---
 arch/x86/mm/init.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c
index d7aea41..a294d4b 100644
--- a/arch/x86/mm/init.c
+++ b/arch/x86/mm/init.c
@@ -16,6 +16,7 @@
 #include <asm/tlb.h>
 #include <asm/proto.h>
 #include <asm/dma.h>		/* for MAX_DMA_PFN */
+#include <asm/microcode.h>
 
 unsigned long __initdata pgt_buf_start;
 unsigned long __meminitdata pgt_buf_end;
@@ -391,6 +392,15 @@ void free_initmem(void)
 #ifdef CONFIG_BLK_DEV_INITRD
 void __init free_initrd_mem(unsigned long start, unsigned long end)
 {
+#ifdef CONFIG_MICROCODE_EARLY
+	/*
+	 * Remember, initrd memory may contain microcode or other useful things.
+	 * Before we lose initrd mem, we need to find a place to hold them
+	 * now that normal virtual memory is enabled.
+	 */
+	save_microcode_in_initrd(&mc_saved_data, mc_saved_in_initrd);
+#endif
+
 	/*
 	 * end could be not aligned, and We can not align that,
 	 * decompresser could be confused by aligned initrd_end
-- 
1.7.2

[PATCH v2 08/10] x86/smpboot.c: Early update ucode on AP

Message details and related content
From: Fenghua Yu <redacted>

This updates ucode on AP. At this point, BSP should store some valid ucode
patches in memory if it finds the ucode patches in initrd. AP searches the
stored ucode and uploads the ucode.

Signed-off-by: Fenghua Yu <redacted>
---
 arch/x86/kernel/smpboot.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index 732bf5c..025690a 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -76,6 +76,7 @@
 #include <asm/i8259.h>
 
 #include <asm/realmode.h>
+#include <asm/microcode.h>
 
 /* State of each CPU */
 DEFINE_PER_CPU(int, cpu_state) = { 0 };
@@ -239,6 +240,12 @@ notrace static void __cpuinit start_secondary(void *unused)
 	 * most necessary things.
 	 */
 	cpu_init();
+	/*
+	 * Load microcode on this cpu if a valid microcode is available.
+	 * This is early microcode loading procedure.
+	 */
+	load_ucode_ap();
+
 	x86_cpuinit.early_percpu_clock_init();
 	preempt_disable();
 	smp_callin();
-- 
1.7.2

[PATCH v2 07/10] x86/head64.c: Early update ucode in 64-bit

Message details and related content
From: Fenghua Yu <redacted>

This updates ucode in 64-bit mode. Paging and virtual address are working now.

Signed-off-by: Fenghua Yu <redacted>
---
 arch/x86/kernel/head64.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c
index 037df57..a512f56 100644
--- a/arch/x86/kernel/head64.c
+++ b/arch/x86/kernel/head64.c
@@ -25,6 +25,7 @@
 #include <asm/kdebug.h>
 #include <asm/e820.h>
 #include <asm/bios_ebda.h>
+#include <asm/microcode.h>
 
 static void __init zap_identity_mappings(void)
 {
@@ -73,6 +74,11 @@ void __init x86_64_start_kernel(char * real_mode_data)
 	/* clear bss before set_intr_gate with early_idt_handler */
 	clear_bss();
 
+	/*
+	 * Load microcode early on BSP.
+	 */
+	load_ucode_bsp(real_mode_data);
+
 	/* Make NULL pointers segfault */
 	zap_identity_mappings();
 
-- 
1.7.2

[PATCH v2 05/10] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
From: Fenghua Yu <redacted>

Implementation of early update ucode on Intel's CPU.

load_ucode_intel_bsp() scans ucode in initrd image file which is a cpio format
ucode followed by ordinary initrd image file. The binary ucode file is stored
in kernel/x86/microcode/GenuineIntel/microcode.hex in the cpio data. All ucode
patches with the same model as BSP are saved in memory. A matching ucode patch
is updated on BSP.

load_ucode_intel_ap() reads saved ucoded patches and updates ucode on AP.

Signed-off-by: Fenghua Yu <redacted>
---
 arch/x86/kernel/microcode_intel_early.c |  438 +++++++++++++++++++++++++++++++
 1 files changed, 438 insertions(+), 0 deletions(-)
 create mode 100644 arch/x86/kernel/microcode_intel_early.c
diff --git a/arch/x86/kernel/microcode_intel_early.c b/arch/x86/kernel/microcode_intel_early.c
new file mode 100644
index 0000000..36b1df1
--- /dev/null
+++ b/arch/x86/kernel/microcode_intel_early.c
@@ -0,0 +1,438 @@
+/*
+ *	Intel CPU Microcode Update Driver for Linux
+ *
+ *	Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
+ *			   H Peter Anvin" <hpa@zytor.com>
+ *
+ *	This driver allows to early upgrade microcode on Intel processors
+ *	belonging to IA-32 family - PentiumPro, Pentium II,
+ *	Pentium III, Xeon, Pentium 4, etc.
+ *
+ *	Reference: Section 9.11 of Volume 3, IA-32 Intel Architecture
+ *	Software Developer's Manual.
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License
+ *	as published by the Free Software Foundation; either version
+ *	2 of the License, or (at your option) any later version.
+ */
+#include <linux/module.h>
+#include <linux/vmalloc.h>
+#include <linux/mm.h>
+#include <linux/earlycpio.h>
+#include <asm/msr.h>
+#include <asm/microcode_intel.h>
+#include <asm/processor.h>
+
+struct microcode_intel __initdata *mc_saved_in_initrd[MAX_UCODE_COUNT];
+struct mc_saved_data mc_saved_data;
+
+enum ucode_state
+generic_load_microcode_early(int cpu, struct microcode_intel **mc_saved_p,
+			     unsigned int mc_saved_count,
+			     struct ucode_cpu_info *uci)
+{
+	struct microcode_intel *ucode_ptr, *new_mc = NULL;
+	int new_rev = uci->cpu_sig.rev;
+	enum ucode_state state = UCODE_OK;
+	unsigned int mc_size;
+	struct microcode_header_intel *mc_header;
+	unsigned int csig = uci->cpu_sig.sig;
+	unsigned int cpf = uci->cpu_sig.pf;
+	int i;
+
+	for (i = 0; i < mc_saved_count; i++) {
+		ucode_ptr = mc_saved_p[i];
+		mc_header = (struct microcode_header_intel *)ucode_ptr;
+		mc_size = get_totalsize(mc_header);
+		if (get_matching_microcode(csig, cpf, ucode_ptr, new_rev)) {
+			new_rev = mc_header->rev;
+			new_mc  = ucode_ptr;
+		}
+	}
+
+	if (!new_mc) {
+		state = UCODE_NFOUND;
+		goto out;
+	}
+
+	uci->mc = (struct microcode_intel *)new_mc;
+out:
+	return state;
+}
+EXPORT_SYMBOL_GPL(generic_load_microcode_early);
+
+static enum ucode_state __init
+load_microcode(struct mc_saved_data *mc_saved_data, int cpu)
+{
+	struct ucode_cpu_info *uci = mc_saved_data->ucode_cpu_info + cpu;
+
+	return generic_load_microcode_early(cpu, mc_saved_data->mc_saved,
+					    mc_saved_data->mc_saved_count, uci);
+}
+
+static u8 get_x86_family(unsigned long sig)
+{
+	u8 x86;
+
+	x86 = (sig >> 8) & 0xf;
+
+	if (x86 == 0xf)
+		x86 += (sig >> 20) & 0xff;
+
+	return x86;
+}
+
+static u8 get_x86_model(unsigned long sig)
+{
+	u8 x86, x86_model;
+
+	x86 = get_x86_family(sig);
+	x86_model = (sig >> 4) & 0xf;
+
+	if (x86 == 0x6 || x86 == 0xf)
+		x86_model += ((sig >> 16) & 0xf) << 4;
+
+	return x86_model;
+}
+
+static enum ucode_state
+matching_model_microcode(struct microcode_header_intel *mc_header,
+			unsigned long sig)
+{
+	u8 x86, x86_model;
+	u8 x86_ucode, x86_model_ucode;
+
+	x86 = get_x86_family(sig);
+	x86_model = get_x86_model(sig);
+
+	x86_ucode = get_x86_family(mc_header->sig);
+	x86_model_ucode = get_x86_model(mc_header->sig);
+
+	if (x86 != x86_ucode || x86_model != x86_model_ucode)
+		return UCODE_ERROR;
+
+	return UCODE_OK;
+}
+
+static void
+save_microcode(struct mc_saved_data *mc_saved_data,
+	       struct microcode_intel **mc_saved_src,
+	       unsigned int mc_saved_count)
+{
+	int i;
+	struct microcode_intel **mc_saved_p;
+
+	if (!mc_saved_count)
+		return;
+
+	mc_saved_p = vmalloc(mc_saved_count*sizeof(struct microcode_intel *));
+	if (!mc_saved_p)
+		return;
+
+	for (i = 0; i < mc_saved_count; i++) {
+		struct microcode_intel *mc = mc_saved_src[i];
+		struct microcode_header_intel *mc_header = &mc->hdr;
+		unsigned long mc_size = get_totalsize(mc_header);
+		mc_saved_p[i] = vmalloc(mc_size);
+		if (mc_saved_src[i])
+			memcpy(mc_saved_p[i], mc, mc_size);
+	}
+
+	mc_saved_data->mc_saved = mc_saved_p;
+}
+
+/*
+ * Get microcode matching with BSP's model. Only CPU's with the same model as
+ * BSP can stay in the platform.
+ */
+enum ucode_state
+get_matching_model_microcode(int cpu, void *data, size_t size,
+			     struct mc_saved_data *mc_saved_data,
+			     struct microcode_intel **mc_saved_in_initrd,
+			     enum system_states system_state)
+{
+	u8 *ucode_ptr = data;
+	unsigned int leftover = size;
+	enum ucode_state state = UCODE_OK;
+	unsigned int mc_size;
+	struct microcode_header_intel *mc_header;
+	struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
+	size_t mc_saved_size;
+	size_t mem_size;
+	unsigned int mc_saved_count = mc_saved_data->mc_saved_count;
+	struct ucode_cpu_info *uci = mc_saved_data->ucode_cpu_info + cpu;
+	int found = 0;
+	int i;
+
+	if (mc_saved_count) {
+		mem_size = mc_saved_count * sizeof(struct microcode_intel *);
+		memcpy(mc_saved_tmp, mc_saved_data->mc_saved, mem_size);
+	}
+
+	while (leftover) {
+		mc_header = (struct microcode_header_intel *)ucode_ptr;
+
+		mc_size = get_totalsize(mc_header);
+		if (!mc_size || mc_size > leftover ||
+			microcode_sanity_check(ucode_ptr, 0) < 0)
+			break;
+
+		leftover -= mc_size;
+		if (matching_model_microcode(mc_header, uci->cpu_sig.sig) !=
+			 UCODE_OK) {
+			ucode_ptr += mc_size;
+			continue;
+		}
+
+		found = 0;
+		for (i = 0; i < mc_saved_count; i++) {
+			unsigned int sig, pf;
+			unsigned int new_rev;
+			struct microcode_header_intel *mc_saved_header =
+			     (struct microcode_header_intel *)mc_saved_tmp[i];
+			sig = mc_saved_header->sig;
+			pf = mc_saved_header->pf;
+			new_rev = mc_header->rev;
+
+			if (get_matching_sig(sig, pf, ucode_ptr, new_rev)) {
+				found = 1;
+				if (update_match_revision(mc_header, new_rev)) {
+					/*
+					 * Found an older ucode saved before.
+					 * Replace the older one with this newer
+					 * one.
+					 */
+					mc_saved_tmp[i] =
+					(struct microcode_intel *)ucode_ptr;
+					break;
+				}
+			}
+		}
+		if (i >= mc_saved_count && !found)
+			/*
+			 * This ucode is first time discovered in ucode file.
+			 * Save it to memory.
+			 */
+			mc_saved_tmp[mc_saved_count++] =
+					 (struct microcode_intel *)ucode_ptr;
+
+		ucode_ptr += mc_size;
+	}
+
+	if (leftover) {
+		state = UCODE_ERROR;
+		goto out;
+	}
+
+	if (mc_saved_count == 0) {
+		state = UCODE_NFOUND;
+		goto out;
+	}
+
+	if (system_state == SYSTEM_RUNNING) {
+		vfree(mc_saved_data->mc_saved);
+		save_microcode(mc_saved_data, mc_saved_tmp, mc_saved_count);
+	} else {
+		mc_saved_size = sizeof(struct microcode_intel *) *
+				mc_saved_count;
+		memcpy(mc_saved_in_initrd, mc_saved_tmp, mc_saved_size);
+		mc_saved_data->mc_saved = mc_saved_in_initrd;
+	}
+
+	mc_saved_data->mc_saved_count = mc_saved_count;
+out:
+	return state;
+}
+EXPORT_SYMBOL_GPL(get_matching_model_microcode);
+
+#define native_rdmsr(msr, val1, val2)		\
+do {						\
+	u64 __val = native_read_msr((msr));	\
+	(void)((val1) = (u32)__val);		\
+	(void)((val2) = (u32)(__val >> 32));	\
+} while (0)
+
+#define native_wrmsr(msr, low, high)		\
+	native_write_msr(msr, low, high);
+
+static int __cpuinit collect_cpu_info_early(struct ucode_cpu_info *uci)
+{
+	unsigned int val[2];
+	u8 x86, x86_model;
+	struct cpu_signature csig = {0, 0, 0};
+	unsigned int eax, ebx, ecx, edx;
+
+	memset(uci, 0, sizeof(*uci));
+
+	eax = 0x00000001;
+	ecx = 0;
+	native_cpuid(&eax, &ebx, &ecx, &edx);
+	csig.sig = eax;
+
+	x86 = get_x86_family(csig.sig);
+	x86_model = get_x86_model(csig.sig);
+
+	if ((x86_model >= 5) || (x86 > 6)) {
+		/* get processor flags from MSR 0x17 */
+		native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
+		csig.pf = 1 << ((val[1] >> 18) & 7);
+	}
+
+	/* get the current revision from MSR 0x8B */
+	native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
+
+	csig.rev = val[1];
+
+	uci->cpu_sig = csig;
+	uci->valid = 1;
+
+	return 0;
+}
+
+static __init enum ucode_state
+scan_microcode(unsigned long start, unsigned long end,
+		struct mc_saved_data *mc_saved_data,
+		struct microcode_intel **mc_saved_in_initrd)
+{
+	unsigned int size = end - start + 1;
+	struct cpio_data cd = { 0, 0 };
+	char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
+	long offset = 0;
+
+	cd = find_cpio_data(ucode_name, (void *)start, size, &offset);
+	if (!cd.data)
+		return UCODE_ERROR;
+
+	return get_matching_model_microcode(0, cd.data, cd.size, mc_saved_data,
+					 mc_saved_in_initrd, SYSTEM_BOOTING);
+}
+
+static int __init
+apply_microcode_early(struct mc_saved_data *mc_saved_data, int cpu)
+{
+	struct ucode_cpu_info *uci = mc_saved_data->ucode_cpu_info + cpu;
+	struct microcode_intel *mc_intel;
+	unsigned int val[2];
+
+	/* We should bind the task to the CPU */
+	mc_intel = uci->mc;
+	if (mc_intel == NULL)
+		return 0;
+
+	/* write microcode via MSR 0x79 */
+	native_wrmsr(MSR_IA32_UCODE_WRITE,
+	      (unsigned long) mc_intel->bits,
+	      (unsigned long) mc_intel->bits >> 16 >> 16);
+	native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
+
+	/* As documented in the SDM: Do a CPUID 1 here */
+	sync_core();
+
+	/* get the current revision from MSR 0x8B */
+	native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
+	if (val[1] != mc_intel->hdr.rev)
+		return -1;
+
+	uci->cpu_sig.rev = val[1];
+
+	return 0;
+}
+
+#ifdef CONFIG_X86_32
+static void __init map_mc_saved(struct mc_saved_data *mc_saved_data,
+				struct microcode_intel **mc_saved_in_initrd)
+{
+	int i;
+
+	if (mc_saved_data->mc_saved) {
+		for (i = 0; i < mc_saved_data->mc_saved_count; i++)
+			mc_saved_data->mc_saved[i] =
+					 __va(mc_saved_data->mc_saved[i]);
+
+		mc_saved_data->mc_saved = __va(mc_saved_data->mc_saved);
+	}
+
+	if (mc_saved_data->ucode_cpu_info->mc)
+		mc_saved_data->ucode_cpu_info->mc =
+				 __va(mc_saved_data->ucode_cpu_info->mc);
+	mc_saved_data->ucode_cpu_info = __va(mc_saved_data->ucode_cpu_info);
+}
+#else
+static inline void __init map_mc_saved(struct mc_saved_data *mc_saved_data,
+				struct microcode_intel **mc_saved_in_initrd)
+{
+}
+#endif
+
+void __init save_microcode_in_initrd(struct mc_saved_data *mc_saved_data,
+		 struct microcode_intel **mc_saved_in_initrd)
+{
+	unsigned int count = mc_saved_data->mc_saved_count;
+
+	save_microcode(mc_saved_data, mc_saved_in_initrd, count);
+}
+
+static void __init
+_load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data,
+		      struct microcode_intel **mc_saved_in_initrd,
+		      unsigned long initrd_start, unsigned long initrd_end)
+{
+	int cpu = 0;
+
+#ifdef CONFIG_X86_64
+	mc_saved_data->ucode_cpu_info = ucode_cpu_info_early;
+#else
+	mc_saved_data->ucode_cpu_info =
+			(struct ucode_cpu_info *)__pa(ucode_cpu_info_early);
+#endif
+	collect_cpu_info_early(mc_saved_data->ucode_cpu_info + cpu);
+	scan_microcode(initrd_start, initrd_end, mc_saved_data,
+		       mc_saved_in_initrd);
+	load_microcode(mc_saved_data, cpu);
+	apply_microcode_early(mc_saved_data, cpu);
+	map_mc_saved(mc_saved_data, mc_saved_in_initrd);
+}
+
+void __init
+load_ucode_intel_bsp(char *real_mode_data)
+{
+	u64 ramdisk_image, ramdisk_size, ramdisk_end;
+	unsigned long initrd_start, initrd_end;
+	struct boot_params *boot_params;
+
+	boot_params = (struct boot_params *)real_mode_data;
+	ramdisk_image = boot_params->hdr.ramdisk_image;
+	ramdisk_size  = boot_params->hdr.ramdisk_size;
+
+#ifdef CONFIG_X86_64
+	ramdisk_end  = PAGE_ALIGN(ramdisk_image + ramdisk_size);
+	initrd_start = ramdisk_image + PAGE_OFFSET;
+	initrd_end = initrd_start + ramdisk_size;
+	_load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd,
+			      initrd_start, initrd_end);
+#else
+	ramdisk_end  = ramdisk_image + ramdisk_size;
+	initrd_start = ramdisk_image;
+	initrd_end = initrd_start + ramdisk_size;
+	_load_ucode_intel_bsp((struct mc_saved_data *)__pa(&mc_saved_data),
+			(struct microcode_intel **)__pa(mc_saved_in_initrd),
+			initrd_start, initrd_end);
+#endif
+}
+
+void __cpuinit load_ucode_intel_ap(void)
+{
+	int cpu = smp_processor_id();
+
+	/*
+	 * If BSP doesn't find valid ucode and save it in memory, no need to
+	 * update ucode on this AP.
+	 */
+	if (!mc_saved_data.mc_saved)
+		return;
+
+	collect_cpu_info_early(mc_saved_data.ucode_cpu_info + cpu);
+	load_microcode(&mc_saved_data, cpu);
+	apply_microcode_early(&mc_saved_data, cpu);
+}
-- 
1.7.2

[PATCH v2 02/10] x86/microcode_intel.h: Define functions and macros for early loading ucode

Message details and related content
From: Fenghua Yu <redacted>

Define some functions and macros that will be used in early loading ucode. Some
of them are moved from microcode_intel.c driver in order to be called in early
boot phase before module can be called.

Signed-off-by: Fenghua Yu <redacted>
---
 arch/x86/include/asm/microcode_intel.h |  106 ++++++++++++++++++
 arch/x86/kernel/Makefile               |    3 +
 arch/x86/kernel/microcode_core.c       |    7 +-
 arch/x86/kernel/microcode_intel.c      |  185 ++------------------------------
 4 files changed, 120 insertions(+), 181 deletions(-)
 create mode 100644 arch/x86/include/asm/microcode_intel.h
diff --git a/arch/x86/include/asm/microcode_intel.h b/arch/x86/include/asm/microcode_intel.h
new file mode 100644
index 0000000..0544bf4
--- /dev/null
+++ b/arch/x86/include/asm/microcode_intel.h
@@ -0,0 +1,106 @@
+#ifndef _ASM_X86_MICROCODE_INTEL_H
+#define _ASM_X86_MICROCODE_INTEL_H
+
+#include <asm/microcode.h>
+
+struct microcode_header_intel {
+	unsigned int            hdrver;
+	unsigned int            rev;
+	unsigned int            date;
+	unsigned int            sig;
+	unsigned int            cksum;
+	unsigned int            ldrver;
+	unsigned int            pf;
+	unsigned int            datasize;
+	unsigned int            totalsize;
+	unsigned int            reserved[3];
+};
+
+struct microcode_intel {
+	struct microcode_header_intel hdr;
+	unsigned int            bits[0];
+};
+
+/* microcode format is extended from prescott processors */
+struct extended_signature {
+	unsigned int            sig;
+	unsigned int            pf;
+	unsigned int            cksum;
+};
+
+struct extended_sigtable {
+	unsigned int            count;
+	unsigned int            cksum;
+	unsigned int            reserved[3];
+	struct extended_signature sigs[0];
+};
+
+#define DEFAULT_UCODE_DATASIZE	(2000)
+#define MC_HEADER_SIZE		(sizeof(struct microcode_header_intel))
+#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
+#define EXT_HEADER_SIZE		(sizeof(struct extended_sigtable))
+#define EXT_SIGNATURE_SIZE	(sizeof(struct extended_signature))
+#define DWSIZE			(sizeof(u32))
+
+#define get_totalsize(mc) \
+	(((struct microcode_intel *)mc)->hdr.totalsize ? \
+	 ((struct microcode_intel *)mc)->hdr.totalsize : \
+	 DEFAULT_UCODE_TOTALSIZE)
+
+#define get_datasize(mc) \
+	(((struct microcode_intel *)mc)->hdr.datasize ? \
+	 ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
+
+#define sigmatch(s1, s2, p1, p2) \
+	(((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
+
+#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
+
+extern int
+get_matching_microcode(unsigned int csig, int cpf, void *mc, int rev);
+extern int microcode_sanity_check(void *mc, int print_err);
+extern int get_matching_sig(unsigned int csig, int cpf, void *mc, int rev);
+extern int
+update_match_revision(struct microcode_header_intel *mc_header, int rev);
+
+#ifdef CONFIG_MICROCODE_INTEL_EARLY
+extern enum ucode_state
+get_matching_model_microcode(int cpu, void *data, size_t size,
+			     struct mc_saved_data *mc_saved_data,
+			     struct microcode_intel **mc_saved_in_initrd,
+			     enum system_states system_state);
+extern enum ucode_state
+generic_load_microcode_early(int cpu, struct microcode_intel **mc_saved_p,
+			     unsigned int mc_saved_count,
+			     struct ucode_cpu_info *uci);
+extern void __init
+load_ucode_intel_bsp(char *real_mode_data);
+extern void __init load_ucode_intel_ap(void);
+#else
+static inline enum ucode_state
+get_matching_model_microcode(int cpu, void *data, size_t size,
+			     struct mc_saved_data *mc_saved_data,
+			     struct microcode_intel **mc_saved_in_initrd,
+			     enum system_states system_state)
+{
+	return UCODE_ERROR;
+}
+static inline enum ucode_state
+generic_load_microcode_early(int cpu, struct microcode_intel **mc_saved_p,
+			     unsigned int mc_saved_count,
+			     struct ucode_cpu_info *uci)
+{
+	return UCODE_ERROR;
+}
+static inline __init void
+load_ucode_intel_bsp(char *real_mode_data)
+{
+}
+static inline __init void
+load_ucode_intel_ap(struct ucode_cpu_info *uci,
+		    struct mc_saved_data *mc_saved_data)
+{
+}
+#endif
+
+#endif /* _ASM_X86_MICROCODE_INTEL_H */
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 9fd5eed..52b992a 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -87,6 +87,9 @@ obj-$(CONFIG_PARAVIRT_CLOCK)	+= pvclock.o
 
 obj-$(CONFIG_PCSPKR_PLATFORM)	+= pcspeaker.o
 
+obj-$(CONFIG_MICROCODE_EARLY)		+= microcode_core_early.o
+obj-$(CONFIG_MICROCODE_INTEL_EARLY)	+= microcode_intel_early.o
+obj-$(CONFIG_MICROCODE_INTEL_LIB)	+= microcode_intel_lib.o
 microcode-y				:= microcode_core.o
 microcode-$(CONFIG_MICROCODE_INTEL)	+= microcode_intel.o
 microcode-$(CONFIG_MICROCODE_AMD)	+= microcode_amd.o
diff --git a/arch/x86/kernel/microcode_core.c b/arch/x86/kernel/microcode_core.c
index 3a04b22..22db92b 100644
--- a/arch/x86/kernel/microcode_core.c
+++ b/arch/x86/kernel/microcode_core.c
@@ -364,10 +364,7 @@ static struct attribute_group mc_attr_group = {
 
 static void microcode_fini_cpu(int cpu)
 {
-	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
-
 	microcode_ops->microcode_fini_cpu(cpu);
-	uci->valid = 0;
 }
 
 static enum ucode_state microcode_resume_cpu(int cpu)
@@ -383,6 +380,10 @@ static enum ucode_state microcode_resume_cpu(int cpu)
 static enum ucode_state microcode_init_cpu(int cpu, bool refresh_fw)
 {
 	enum ucode_state ustate;
+	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+
+	if (uci && uci->valid)
+		return UCODE_OK;
 
 	if (collect_cpu_info(cpu))
 		return UCODE_ERROR;
diff --git a/arch/x86/kernel/microcode_intel.c b/arch/x86/kernel/microcode_intel.c
index 3544aed..f3ddf8e 100644
--- a/arch/x86/kernel/microcode_intel.c
+++ b/arch/x86/kernel/microcode_intel.c
@@ -79,7 +79,7 @@
 #include <linux/module.h>
 #include <linux/vmalloc.h>
 
-#include <asm/microcode.h>
+#include <asm/microcode_intel.h>
 #include <asm/processor.h>
 #include <asm/msr.h>
 
@@ -87,59 +87,6 @@ MODULE_DESCRIPTION("Microcode Update Driver");
 MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
 MODULE_LICENSE("GPL");
 
-struct microcode_header_intel {
-	unsigned int            hdrver;
-	unsigned int            rev;
-	unsigned int            date;
-	unsigned int            sig;
-	unsigned int            cksum;
-	unsigned int            ldrver;
-	unsigned int            pf;
-	unsigned int            datasize;
-	unsigned int            totalsize;
-	unsigned int            reserved[3];
-};
-
-struct microcode_intel {
-	struct microcode_header_intel hdr;
-	unsigned int            bits[0];
-};
-
-/* microcode format is extended from prescott processors */
-struct extended_signature {
-	unsigned int            sig;
-	unsigned int            pf;
-	unsigned int            cksum;
-};
-
-struct extended_sigtable {
-	unsigned int            count;
-	unsigned int            cksum;
-	unsigned int            reserved[3];
-	struct extended_signature sigs[0];
-};
-
-#define DEFAULT_UCODE_DATASIZE	(2000)
-#define MC_HEADER_SIZE		(sizeof(struct microcode_header_intel))
-#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
-#define EXT_HEADER_SIZE		(sizeof(struct extended_sigtable))
-#define EXT_SIGNATURE_SIZE	(sizeof(struct extended_signature))
-#define DWSIZE			(sizeof(u32))
-
-#define get_totalsize(mc) \
-	(((struct microcode_intel *)mc)->hdr.totalsize ? \
-	 ((struct microcode_intel *)mc)->hdr.totalsize : \
-	 DEFAULT_UCODE_TOTALSIZE)
-
-#define get_datasize(mc) \
-	(((struct microcode_intel *)mc)->hdr.datasize ? \
-	 ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
-
-#define sigmatch(s1, s2, p1, p2) \
-	(((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
-
-#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
-
 static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
 {
 	struct cpuinfo_x86 *c = &cpu_data(cpu_num);
@@ -162,128 +109,7 @@ static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
 	return 0;
 }
 
-static inline int update_match_cpu(struct cpu_signature *csig, int sig, int pf)
-{
-	return (!sigmatch(sig, csig->sig, pf, csig->pf)) ? 0 : 1;
-}
-
-static inline int
-update_match_revision(struct microcode_header_intel *mc_header, int rev)
-{
-	return (mc_header->rev <= rev) ? 0 : 1;
-}
-
-static int microcode_sanity_check(void *mc)
-{
-	unsigned long total_size, data_size, ext_table_size;
-	struct microcode_header_intel *mc_header = mc;
-	struct extended_sigtable *ext_header = NULL;
-	int sum, orig_sum, ext_sigcount = 0, i;
-	struct extended_signature *ext_sig;
-
-	total_size = get_totalsize(mc_header);
-	data_size = get_datasize(mc_header);
-
-	if (data_size + MC_HEADER_SIZE > total_size) {
-		pr_err("error! Bad data size in microcode data file\n");
-		return -EINVAL;
-	}
-
-	if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
-		pr_err("error! Unknown microcode update format\n");
-		return -EINVAL;
-	}
-	ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
-	if (ext_table_size) {
-		if ((ext_table_size < EXT_HEADER_SIZE)
-		 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
-			pr_err("error! Small exttable size in microcode data file\n");
-			return -EINVAL;
-		}
-		ext_header = mc + MC_HEADER_SIZE + data_size;
-		if (ext_table_size != exttable_size(ext_header)) {
-			pr_err("error! Bad exttable size in microcode data file\n");
-			return -EFAULT;
-		}
-		ext_sigcount = ext_header->count;
-	}
-
-	/* check extended table checksum */
-	if (ext_table_size) {
-		int ext_table_sum = 0;
-		int *ext_tablep = (int *)ext_header;
-
-		i = ext_table_size / DWSIZE;
-		while (i--)
-			ext_table_sum += ext_tablep[i];
-		if (ext_table_sum) {
-			pr_warning("aborting, bad extended signature table checksum\n");
-			return -EINVAL;
-		}
-	}
-
-	/* calculate the checksum */
-	orig_sum = 0;
-	i = (MC_HEADER_SIZE + data_size) / DWSIZE;
-	while (i--)
-		orig_sum += ((int *)mc)[i];
-	if (orig_sum) {
-		pr_err("aborting, bad checksum\n");
-		return -EINVAL;
-	}
-	if (!ext_table_size)
-		return 0;
-	/* check extended signature checksum */
-	for (i = 0; i < ext_sigcount; i++) {
-		ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
-			  EXT_SIGNATURE_SIZE * i;
-		sum = orig_sum
-			- (mc_header->sig + mc_header->pf + mc_header->cksum)
-			+ (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
-		if (sum) {
-			pr_err("aborting, bad checksum\n");
-			return -EINVAL;
-		}
-	}
-	return 0;
-}
-
-/*
- * return 0 - no update found
- * return 1 - found update
- */
-static int
-get_matching_microcode(struct cpu_signature *cpu_sig, void *mc, int rev)
-{
-	struct microcode_header_intel *mc_header = mc;
-	struct extended_sigtable *ext_header;
-	unsigned long total_size = get_totalsize(mc_header);
-	int ext_sigcount, i;
-	struct extended_signature *ext_sig;
-
-	if (!update_match_revision(mc_header, rev))
-		return 0;
-
-	if (update_match_cpu(cpu_sig, mc_header->sig, mc_header->pf))
-		return 1;
-
-	/* Look for ext. headers: */
-	if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
-		return 0;
-
-	ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
-	ext_sigcount = ext_header->count;
-	ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
-
-	for (i = 0; i < ext_sigcount; i++) {
-		if (update_match_cpu(cpu_sig, ext_sig->sig, ext_sig->pf))
-			return 1;
-		ext_sig++;
-	}
-	return 0;
-}
-
-static int apply_microcode(int cpu)
+int apply_microcode(int cpu)
 {
 	struct microcode_intel *mc_intel;
 	struct ucode_cpu_info *uci;
@@ -338,6 +164,7 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
 	unsigned int leftover = size;
 	enum ucode_state state = UCODE_OK;
 	unsigned int curr_mc_size = 0;
+	unsigned int csig, cpf;
 
 	while (leftover) {
 		struct microcode_header_intel mc_header;
@@ -362,11 +189,13 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
 		}
 
 		if (get_ucode_data(mc, ucode_ptr, mc_size) ||
-		    microcode_sanity_check(mc) < 0) {
+		    microcode_sanity_check(mc, 1) < 0) {
 			break;
 		}
 
-		if (get_matching_microcode(&uci->cpu_sig, mc, new_rev)) {
+		csig = uci->cpu_sig.sig;
+		cpf = uci->cpu_sig.pf;
+		if (get_matching_microcode(csig, cpf, mc, new_rev)) {
 			vfree(new_mc);
 			new_rev = mc_header.rev;
 			new_mc  = mc;
-- 
1.7.2

[PATCH v2 01/10] Documentation/x86: Early load microcode

Message details and related content
From: Fenghua Yu <redacted>

Documenation for early loading microcode methodology.

Signed-off-by: Fenghua Yu <redacted>
---
 Documentation/x86/early-microcode.txt |   43 +++++++++++++++++++++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/x86/early-microcode.txt
diff --git a/Documentation/x86/early-microcode.txt b/Documentation/x86/early-microcode.txt
new file mode 100644
index 0000000..9b3a6ab
--- /dev/null
+++ b/Documentation/x86/early-microcode.txt
@@ -0,0 +1,43 @@
+Early load microcode
+====================
+By Fenghua Yu <fenghua.yu@intel.com>
+
+Kernel can update microcode in early phase of boot time. Loading microcode early
+can fix CPU issues before they are observed during kernel boot time.
+
+Microcode is stored in an initrd file. The microcode is read from the initrd
+file and loaded to CPUs during boot time.
+
+The format of the combined initrd image is microcode in cpio format followed by
+the initrd image (maybe compressed). Kernel parses the combined initrd image
+during boot time. The microcode file in cpio name space is:
+kernel/x86/microcode/GenuineIntel.bin
+
+During BSP boot (before SMP starts), if the kernel finds the microcode file in
+the initrd file, it parses the microcode and saves matching microcode in memory.
+If matching microcode is found, it will be uploaded in BSP and later on in all
+APs.
+
+The cached microcode patch is applied when CPUs resume from a sleep state.
+
+There are two legacy user space interfaces to load microcode, either through
+/dev/cpu/microcode or through /sys/devices/system/cpu/microcode/reload file
+in sysfs.
+
+In addition to these two legacy methods, the early loading method described
+here is the third method with which microcode can be uploaded to a system's
+CPUs.
+
+The following example script shows how to generate a new combined initrd file in
+/boot/initrd-3.5.0.ucode.img with original microcode microcode.hex and
+original initrd image /boot/initrd-3.5.0.img.
+
+mkdir initrd
+cd initrd
+cp ../microcode.hex kernel/x86/microcode/GenuineIntel/microcode.hex
+find .|cpio -oc >../ucode.cpio
+cd ..
+cat ucode.cpio /boot/initrd-3.5.0.img >/boot/initrd-3.5.0.ucode.img
+
+The generated /boot/initrd-3.5.0.ucode.img can be used as initrd file to load
+microcode during early boot time.
-- 
1.7.2

Re: [PATCH v2 01/10] Documentation/x86: Early load microcode

Message details and related content
On 11/29/2012 05:47 PM, Fenghua Yu wrote:
quoted hunk
+
+mkdir initrd
+cd initrd
+cp ../microcode.hex kernel/x86/microcode/GenuineIntel/microcode.hex
+find .|cpio -oc >../ucode.cpio
+cd ..
+cat ucode.cpio /boot/initrd-3.5.0.img >/boot/initrd-3.5.0.ucode.img
+
+The generated /boot/initrd-3.5.0.ucode.img can be used as initrd file to load
+microcode during early boot time.
This is wrong; first of all, this refers to microcode.hex whereas the
patch refers to microcode.bin, and it misses the mandatory -H newc
option to cpio.

Could you update this bit, please?

	-hpa

RE: [PATCH v2 01/10] Documentation/x86: Early load microcode

Message details and related content
-----Original Message-----
From: H. Peter Anvin [mailto:hpa@zytor.com]
Sent: Friday, November 30, 2012 11:46 AM
To: Yu, Fenghua
Cc: Ingo Molnar; Thomas Gleixner; Mallick, Asit K; Tigran Aivazian;
Andreas Herrmann; Borislav Petkov; linux-kernel; x86
Subject: Re: [PATCH v2 01/10] Documentation/x86: Early load microcode

On 11/29/2012 05:47 PM, Fenghua Yu wrote:
quoted
+
+mkdir initrd
+cd initrd
+cp ../microcode.hex kernel/x86/microcode/GenuineIntel/microcode.hex
+find .|cpio -oc >../ucode.cpio
+cd ..
+cat ucode.cpio /boot/initrd-3.5.0.img >/boot/initrd-3.5.0.ucode.img
+
+The generated /boot/initrd-3.5.0.ucode.img can be used as initrd
file to load
quoted
+microcode during early boot time.
This is wrong; first of all, this refers to microcode.hex whereas the
patch refers to microcode.bin, and 
Will fix this sentence. 
it misses the mandatory -H newc
option to cpio.
I think there is no problem here. "-c" is "-H newc".

Quote from man cpio:
'-c' Identical to -H newc, use the new (SVR4) portable format.  If you wish the old
       portable (ASCII) archive format, use -H odc instead.
Could you update this bit, please?

	-hpa

[tip:x86/microcode] x86/microcode_intel.h: Define functions and macros for early loading ucode

Message details and related content
Commit-ID:  17f1087f1a80d2dfada790c31720eb6a57da2d1f
Gitweb:     http://git.kernel.org/tip/17f1087f1a80d2dfada790c31720eb6a57da2d1f
Author:     Fenghua Yu [off-list ref]
AuthorDate: Thu, 29 Nov 2012 17:47:40 -0800
Committer:  H. Peter Anvin [off-list ref]
CommitDate: Fri, 30 Nov 2012 15:18:14 -0800

x86/microcode_intel.h: Define functions and macros for early loading ucode

Define some functions and macros that will be used in early loading ucode. Some
of them are moved from microcode_intel.c driver in order to be called in early
boot phase before module can be called.

Signed-off-by: Fenghua Yu <redacted>
Link: http://lkml.kernel.org/r/1354240068-9821-3-git-send-email-fenghua.yu@intel.com
Signed-off-by: H. Peter Anvin <redacted>
---
 arch/x86/include/asm/microcode_intel.h | 106 +++++++++++++++++++
 arch/x86/kernel/Makefile               |   3 +
 arch/x86/kernel/microcode_core.c       |   7 +-
 arch/x86/kernel/microcode_intel.c      | 185 ++-------------------------------
 4 files changed, 120 insertions(+), 181 deletions(-)
diff --git a/arch/x86/include/asm/microcode_intel.h b/arch/x86/include/asm/microcode_intel.h
new file mode 100644
index 0000000..0544bf4
--- /dev/null
+++ b/arch/x86/include/asm/microcode_intel.h
@@ -0,0 +1,106 @@
+#ifndef _ASM_X86_MICROCODE_INTEL_H
+#define _ASM_X86_MICROCODE_INTEL_H
+
+#include <asm/microcode.h>
+
+struct microcode_header_intel {
+	unsigned int            hdrver;
+	unsigned int            rev;
+	unsigned int            date;
+	unsigned int            sig;
+	unsigned int            cksum;
+	unsigned int            ldrver;
+	unsigned int            pf;
+	unsigned int            datasize;
+	unsigned int            totalsize;
+	unsigned int            reserved[3];
+};
+
+struct microcode_intel {
+	struct microcode_header_intel hdr;
+	unsigned int            bits[0];
+};
+
+/* microcode format is extended from prescott processors */
+struct extended_signature {
+	unsigned int            sig;
+	unsigned int            pf;
+	unsigned int            cksum;
+};
+
+struct extended_sigtable {
+	unsigned int            count;
+	unsigned int            cksum;
+	unsigned int            reserved[3];
+	struct extended_signature sigs[0];
+};
+
+#define DEFAULT_UCODE_DATASIZE	(2000)
+#define MC_HEADER_SIZE		(sizeof(struct microcode_header_intel))
+#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
+#define EXT_HEADER_SIZE		(sizeof(struct extended_sigtable))
+#define EXT_SIGNATURE_SIZE	(sizeof(struct extended_signature))
+#define DWSIZE			(sizeof(u32))
+
+#define get_totalsize(mc) \
+	(((struct microcode_intel *)mc)->hdr.totalsize ? \
+	 ((struct microcode_intel *)mc)->hdr.totalsize : \
+	 DEFAULT_UCODE_TOTALSIZE)
+
+#define get_datasize(mc) \
+	(((struct microcode_intel *)mc)->hdr.datasize ? \
+	 ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
+
+#define sigmatch(s1, s2, p1, p2) \
+	(((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
+
+#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
+
+extern int
+get_matching_microcode(unsigned int csig, int cpf, void *mc, int rev);
+extern int microcode_sanity_check(void *mc, int print_err);
+extern int get_matching_sig(unsigned int csig, int cpf, void *mc, int rev);
+extern int
+update_match_revision(struct microcode_header_intel *mc_header, int rev);
+
+#ifdef CONFIG_MICROCODE_INTEL_EARLY
+extern enum ucode_state
+get_matching_model_microcode(int cpu, void *data, size_t size,
+			     struct mc_saved_data *mc_saved_data,
+			     struct microcode_intel **mc_saved_in_initrd,
+			     enum system_states system_state);
+extern enum ucode_state
+generic_load_microcode_early(int cpu, struct microcode_intel **mc_saved_p,
+			     unsigned int mc_saved_count,
+			     struct ucode_cpu_info *uci);
+extern void __init
+load_ucode_intel_bsp(char *real_mode_data);
+extern void __init load_ucode_intel_ap(void);
+#else
+static inline enum ucode_state
+get_matching_model_microcode(int cpu, void *data, size_t size,
+			     struct mc_saved_data *mc_saved_data,
+			     struct microcode_intel **mc_saved_in_initrd,
+			     enum system_states system_state)
+{
+	return UCODE_ERROR;
+}
+static inline enum ucode_state
+generic_load_microcode_early(int cpu, struct microcode_intel **mc_saved_p,
+			     unsigned int mc_saved_count,
+			     struct ucode_cpu_info *uci)
+{
+	return UCODE_ERROR;
+}
+static inline __init void
+load_ucode_intel_bsp(char *real_mode_data)
+{
+}
+static inline __init void
+load_ucode_intel_ap(struct ucode_cpu_info *uci,
+		    struct mc_saved_data *mc_saved_data)
+{
+}
+#endif
+
+#endif /* _ASM_X86_MICROCODE_INTEL_H */
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 91ce48f..74fa88b 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -88,6 +88,9 @@ obj-$(CONFIG_PARAVIRT_CLOCK)	+= pvclock.o
 
 obj-$(CONFIG_PCSPKR_PLATFORM)	+= pcspeaker.o
 
+obj-$(CONFIG_MICROCODE_EARLY)		+= microcode_core_early.o
+obj-$(CONFIG_MICROCODE_INTEL_EARLY)	+= microcode_intel_early.o
+obj-$(CONFIG_MICROCODE_INTEL_LIB)	+= microcode_intel_lib.o
 microcode-y				:= microcode_core.o
 microcode-$(CONFIG_MICROCODE_INTEL)	+= microcode_intel.o
 microcode-$(CONFIG_MICROCODE_AMD)	+= microcode_amd.o
diff --git a/arch/x86/kernel/microcode_core.c b/arch/x86/kernel/microcode_core.c
index 3a04b22..22db92b 100644
--- a/arch/x86/kernel/microcode_core.c
+++ b/arch/x86/kernel/microcode_core.c
@@ -364,10 +364,7 @@ static struct attribute_group mc_attr_group = {
 
 static void microcode_fini_cpu(int cpu)
 {
-	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
-
 	microcode_ops->microcode_fini_cpu(cpu);
-	uci->valid = 0;
 }
 
 static enum ucode_state microcode_resume_cpu(int cpu)
@@ -383,6 +380,10 @@ static enum ucode_state microcode_resume_cpu(int cpu)
 static enum ucode_state microcode_init_cpu(int cpu, bool refresh_fw)
 {
 	enum ucode_state ustate;
+	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+
+	if (uci && uci->valid)
+		return UCODE_OK;
 
 	if (collect_cpu_info(cpu))
 		return UCODE_ERROR;
diff --git a/arch/x86/kernel/microcode_intel.c b/arch/x86/kernel/microcode_intel.c
index 3544aed..f3ddf8e 100644
--- a/arch/x86/kernel/microcode_intel.c
+++ b/arch/x86/kernel/microcode_intel.c
@@ -79,7 +79,7 @@
 #include <linux/module.h>
 #include <linux/vmalloc.h>
 
-#include <asm/microcode.h>
+#include <asm/microcode_intel.h>
 #include <asm/processor.h>
 #include <asm/msr.h>
 
@@ -87,59 +87,6 @@ MODULE_DESCRIPTION("Microcode Update Driver");
 MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
 MODULE_LICENSE("GPL");
 
-struct microcode_header_intel {
-	unsigned int            hdrver;
-	unsigned int            rev;
-	unsigned int            date;
-	unsigned int            sig;
-	unsigned int            cksum;
-	unsigned int            ldrver;
-	unsigned int            pf;
-	unsigned int            datasize;
-	unsigned int            totalsize;
-	unsigned int            reserved[3];
-};
-
-struct microcode_intel {
-	struct microcode_header_intel hdr;
-	unsigned int            bits[0];
-};
-
-/* microcode format is extended from prescott processors */
-struct extended_signature {
-	unsigned int            sig;
-	unsigned int            pf;
-	unsigned int            cksum;
-};
-
-struct extended_sigtable {
-	unsigned int            count;
-	unsigned int            cksum;
-	unsigned int            reserved[3];
-	struct extended_signature sigs[0];
-};
-
-#define DEFAULT_UCODE_DATASIZE	(2000)
-#define MC_HEADER_SIZE		(sizeof(struct microcode_header_intel))
-#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
-#define EXT_HEADER_SIZE		(sizeof(struct extended_sigtable))
-#define EXT_SIGNATURE_SIZE	(sizeof(struct extended_signature))
-#define DWSIZE			(sizeof(u32))
-
-#define get_totalsize(mc) \
-	(((struct microcode_intel *)mc)->hdr.totalsize ? \
-	 ((struct microcode_intel *)mc)->hdr.totalsize : \
-	 DEFAULT_UCODE_TOTALSIZE)
-
-#define get_datasize(mc) \
-	(((struct microcode_intel *)mc)->hdr.datasize ? \
-	 ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
-
-#define sigmatch(s1, s2, p1, p2) \
-	(((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
-
-#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
-
 static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
 {
 	struct cpuinfo_x86 *c = &cpu_data(cpu_num);
@@ -162,128 +109,7 @@ static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
 	return 0;
 }
 
-static inline int update_match_cpu(struct cpu_signature *csig, int sig, int pf)
-{
-	return (!sigmatch(sig, csig->sig, pf, csig->pf)) ? 0 : 1;
-}
-
-static inline int
-update_match_revision(struct microcode_header_intel *mc_header, int rev)
-{
-	return (mc_header->rev <= rev) ? 0 : 1;
-}
-
-static int microcode_sanity_check(void *mc)
-{
-	unsigned long total_size, data_size, ext_table_size;
-	struct microcode_header_intel *mc_header = mc;
-	struct extended_sigtable *ext_header = NULL;
-	int sum, orig_sum, ext_sigcount = 0, i;
-	struct extended_signature *ext_sig;
-
-	total_size = get_totalsize(mc_header);
-	data_size = get_datasize(mc_header);
-
-	if (data_size + MC_HEADER_SIZE > total_size) {
-		pr_err("error! Bad data size in microcode data file\n");
-		return -EINVAL;
-	}
-
-	if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
-		pr_err("error! Unknown microcode update format\n");
-		return -EINVAL;
-	}
-	ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
-	if (ext_table_size) {
-		if ((ext_table_size < EXT_HEADER_SIZE)
-		 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
-			pr_err("error! Small exttable size in microcode data file\n");
-			return -EINVAL;
-		}
-		ext_header = mc + MC_HEADER_SIZE + data_size;
-		if (ext_table_size != exttable_size(ext_header)) {
-			pr_err("error! Bad exttable size in microcode data file\n");
-			return -EFAULT;
-		}
-		ext_sigcount = ext_header->count;
-	}
-
-	/* check extended table checksum */
-	if (ext_table_size) {
-		int ext_table_sum = 0;
-		int *ext_tablep = (int *)ext_header;
-
-		i = ext_table_size / DWSIZE;
-		while (i--)
-			ext_table_sum += ext_tablep[i];
-		if (ext_table_sum) {
-			pr_warning("aborting, bad extended signature table checksum\n");
-			return -EINVAL;
-		}
-	}
-
-	/* calculate the checksum */
-	orig_sum = 0;
-	i = (MC_HEADER_SIZE + data_size) / DWSIZE;
-	while (i--)
-		orig_sum += ((int *)mc)[i];
-	if (orig_sum) {
-		pr_err("aborting, bad checksum\n");
-		return -EINVAL;
-	}
-	if (!ext_table_size)
-		return 0;
-	/* check extended signature checksum */
-	for (i = 0; i < ext_sigcount; i++) {
-		ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
-			  EXT_SIGNATURE_SIZE * i;
-		sum = orig_sum
-			- (mc_header->sig + mc_header->pf + mc_header->cksum)
-			+ (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
-		if (sum) {
-			pr_err("aborting, bad checksum\n");
-			return -EINVAL;
-		}
-	}
-	return 0;
-}
-
-/*
- * return 0 - no update found
- * return 1 - found update
- */
-static int
-get_matching_microcode(struct cpu_signature *cpu_sig, void *mc, int rev)
-{
-	struct microcode_header_intel *mc_header = mc;
-	struct extended_sigtable *ext_header;
-	unsigned long total_size = get_totalsize(mc_header);
-	int ext_sigcount, i;
-	struct extended_signature *ext_sig;
-
-	if (!update_match_revision(mc_header, rev))
-		return 0;
-
-	if (update_match_cpu(cpu_sig, mc_header->sig, mc_header->pf))
-		return 1;
-
-	/* Look for ext. headers: */
-	if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
-		return 0;
-
-	ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
-	ext_sigcount = ext_header->count;
-	ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
-
-	for (i = 0; i < ext_sigcount; i++) {
-		if (update_match_cpu(cpu_sig, ext_sig->sig, ext_sig->pf))
-			return 1;
-		ext_sig++;
-	}
-	return 0;
-}
-
-static int apply_microcode(int cpu)
+int apply_microcode(int cpu)
 {
 	struct microcode_intel *mc_intel;
 	struct ucode_cpu_info *uci;
@@ -338,6 +164,7 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
 	unsigned int leftover = size;
 	enum ucode_state state = UCODE_OK;
 	unsigned int curr_mc_size = 0;
+	unsigned int csig, cpf;
 
 	while (leftover) {
 		struct microcode_header_intel mc_header;
@@ -362,11 +189,13 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
 		}
 
 		if (get_ucode_data(mc, ucode_ptr, mc_size) ||
-		    microcode_sanity_check(mc) < 0) {
+		    microcode_sanity_check(mc, 1) < 0) {
 			break;
 		}
 
-		if (get_matching_microcode(&uci->cpu_sig, mc, new_rev)) {
+		csig = uci->cpu_sig.sig;
+		cpf = uci->cpu_sig.pf;
+		if (get_matching_microcode(csig, cpf, mc, new_rev)) {
 			vfree(new_mc);
 			new_rev = mc_header.rev;
 			new_mc  = mc;

[tip:x86/microcode] x86/microcode_core_early.c: Define interfaces for early loading ucode

Message details and related content
Commit-ID:  d42bdf2139115faa4d5bdb0dc591d435a644fde4
Gitweb:     http://git.kernel.org/tip/d42bdf2139115faa4d5bdb0dc591d435a644fde4
Author:     Fenghua Yu [off-list ref]
AuthorDate: Thu, 29 Nov 2012 17:47:41 -0800
Committer:  H. Peter Anvin [off-list ref]
CommitDate: Fri, 30 Nov 2012 15:18:15 -0800

x86/microcode_core_early.c: Define interfaces for early loading ucode

Define interfaces load_ucode_bsp() and load_ucode_ap() to load ucode on BSP and
AP in early boot time. These are generic interfaces. Internally they call
vendor specific implementations.

Signed-off-by: Fenghua Yu <redacted>
Link: http://lkml.kernel.org/r/1354240068-9821-4-git-send-email-fenghua.yu@intel.com
Signed-off-by: H. Peter Anvin <redacted>
---
 arch/x86/include/asm/microcode.h       | 23 +++++++++++
 arch/x86/kernel/microcode_core_early.c | 70 ++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
diff --git a/arch/x86/include/asm/microcode.h b/arch/x86/include/asm/microcode.h
index 43d921b..2e2ff3a 100644
--- a/arch/x86/include/asm/microcode.h
+++ b/arch/x86/include/asm/microcode.h
@@ -57,4 +57,27 @@ static inline struct microcode_ops * __init init_amd_microcode(void)
 static inline void __exit exit_amd_microcode(void) {}
 #endif
 
+struct mc_saved_data {
+	unsigned int mc_saved_count;
+	struct microcode_intel **mc_saved;
+	struct ucode_cpu_info *ucode_cpu_info;
+};
+#ifdef CONFIG_MICROCODE_EARLY
+#define MAX_UCODE_COUNT 128
+extern struct ucode_cpu_info ucode_cpu_info_early[NR_CPUS];
+extern struct microcode_intel __initdata *mc_saved_in_initrd[MAX_UCODE_COUNT];
+extern struct mc_saved_data mc_saved_data;
+extern void __init load_ucode_bsp(char *real_mode_data);
+extern __init void load_ucode_ap(void);
+extern void __init
+save_microcode_in_initrd(struct mc_saved_data *mc_saved_data,
+			 struct microcode_intel **mc_saved_in_initrd);
+#else
+static inline void __init load_ucode_bsp(char *real_mode_data) {}
+static inline __init void load_ucode_ap(void) {}
+static inline void __init
+save_microcode_in_initrd(struct mc_saved_data *mc_saved_data,
+			 struct microcode_intel **mc_saved_in_initrd) {}
+#endif
+
 #endif /* _ASM_X86_MICROCODE_H */
diff --git a/arch/x86/kernel/microcode_core_early.c b/arch/x86/kernel/microcode_core_early.c
new file mode 100644
index 0000000..1c6cc8f
--- /dev/null
+++ b/arch/x86/kernel/microcode_core_early.c
@@ -0,0 +1,70 @@
+/*
+ *	X86 CPU microcode early update for Linux
+ *
+ *	Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
+ *			   H Peter Anvin" <hpa@zytor.com>
+ *
+ *	This driver allows to early upgrade microcode on Intel processors
+ *	belonging to IA-32 family - PentiumPro, Pentium II,
+ *	Pentium III, Xeon, Pentium 4, etc.
+ *
+ *	Reference: Section 9.11 of Volume 3, IA-32 Intel Architecture
+ *	Software Developer's Manual.
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License
+ *	as published by the Free Software Foundation; either version
+ *	2 of the License, or (at your option) any later version.
+ */
+#include <linux/module.h>
+#include <linux/mm.h>
+#include <asm/microcode_intel.h>
+#include <asm/processor.h>
+
+struct ucode_cpu_info	ucode_cpu_info_early[NR_CPUS];
+EXPORT_SYMBOL_GPL(ucode_cpu_info_early);
+
+static inline int __init x86_vendor(void)
+{
+	unsigned int eax = 0x00000000;
+	char x86_vendor_id[16];
+	int i;
+	struct {
+		char x86_vendor_id[16];
+		__u8 x86_vendor;
+	} cpu_vendor_table[] = {
+		{ "GenuineIntel", X86_VENDOR_INTEL },
+		{ "AuthenticAMD", X86_VENDOR_AMD },
+	};
+
+	memset(x86_vendor_id, 0, ARRAY_SIZE(x86_vendor_id));
+	/* Get vendor name */
+	native_cpuid(&eax,
+		(unsigned int *)&x86_vendor_id[0],
+		(unsigned int *)&x86_vendor_id[8],
+		(unsigned int *)&x86_vendor_id[4]);
+
+	for (i = 0; i < ARRAY_SIZE(cpu_vendor_table); i++) {
+		if (!strcmp(x86_vendor_id, cpu_vendor_table[i].x86_vendor_id))
+			return cpu_vendor_table[i].x86_vendor;
+	}
+
+	return X86_VENDOR_UNKNOWN;
+}
+
+
+void __init load_ucode_bsp(char *real_mode_data)
+{
+	/*
+	 * boot_cpu_data is not setup yet in this early phase.
+	 * So we get vendor information directly through cpuid.
+	 */
+	if (x86_vendor() == X86_VENDOR_INTEL)
+		load_ucode_intel_bsp(real_mode_data);
+}
+
+void __cpuinit load_ucode_ap(void)
+{
+	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
+		load_ucode_intel_ap();
+}

[tip:x86/microcode] x86/microcode_intel_lib.c: Early update ucode on Intel's CPU

Message details and related content
Commit-ID:  da7d824a00ec0f4d19e2b51653410bde0de40226
Gitweb:     http://git.kernel.org/tip/da7d824a00ec0f4d19e2b51653410bde0de40226
Author:     Fenghua Yu [off-list ref]
AuthorDate: Thu, 29 Nov 2012 17:47:42 -0800
Committer:  H. Peter Anvin [off-list ref]
CommitDate: Fri, 30 Nov 2012 15:18:15 -0800

x86/microcode_intel_lib.c: Early update ucode on Intel's CPU

Define interfaces microcode_sanity_check() and get_matching_microcode(). They
are called both in early boot time and in microcode Intel driver.

Signed-off-by: Fenghua Yu <redacted>
Link: http://lkml.kernel.org/r/1354240068-9821-5-git-send-email-fenghua.yu@intel.com
Signed-off-by: H. Peter Anvin <redacted>
---
 arch/x86/kernel/microcode_intel_lib.c | 174 ++++++++++++++++++++++++++++++++++
 1 file changed, 174 insertions(+)
diff --git a/arch/x86/kernel/microcode_intel_lib.c b/arch/x86/kernel/microcode_intel_lib.c
new file mode 100644
index 0000000..ce69320
--- /dev/null
+++ b/arch/x86/kernel/microcode_intel_lib.c
@@ -0,0 +1,174 @@
+/*
+ *	Intel CPU Microcode Update Driver for Linux
+ *
+ *	Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
+ *			   H Peter Anvin" <hpa@zytor.com>
+ *
+ *	This driver allows to upgrade microcode on Intel processors
+ *	belonging to IA-32 family - PentiumPro, Pentium II,
+ *	Pentium III, Xeon, Pentium 4, etc.
+ *
+ *	Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
+ *	Software Developer's Manual
+ *	Order Number 253668 or free download from:
+ *
+ *	http://developer.intel.com/Assets/PDF/manual/253668.pdf
+ *
+ *	For more information, go to http://www.urbanmyth.org/microcode
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License
+ *	as published by the Free Software Foundation; either version
+ *	2 of the License, or (at your option) any later version.
+ *
+ */
+#include <linux/firmware.h>
+#include <linux/uaccess.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#include <asm/microcode_intel.h>
+#include <asm/processor.h>
+#include <asm/msr.h>
+
+static inline int
+update_match_cpu(unsigned int csig, unsigned int cpf,
+		 unsigned int sig, unsigned int pf)
+{
+	return (!sigmatch(sig, csig, pf, cpf)) ? 0 : 1;
+}
+
+int
+update_match_revision(struct microcode_header_intel *mc_header, int rev)
+{
+	return (mc_header->rev <= rev) ? 0 : 1;
+}
+
+int microcode_sanity_check(void *mc, int print_err)
+{
+	unsigned long total_size, data_size, ext_table_size;
+	struct microcode_header_intel *mc_header = mc;
+	struct extended_sigtable *ext_header = NULL;
+	int sum, orig_sum, ext_sigcount = 0, i;
+	struct extended_signature *ext_sig;
+
+	total_size = get_totalsize(mc_header);
+	data_size = get_datasize(mc_header);
+
+	if (data_size + MC_HEADER_SIZE > total_size) {
+		if (print_err)
+			pr_err("error! Bad data size in microcode data file\n");
+		return -EINVAL;
+	}
+
+	if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
+		if (print_err)
+			pr_err("error! Unknown microcode update format\n");
+		return -EINVAL;
+	}
+	ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
+	if (ext_table_size) {
+		if ((ext_table_size < EXT_HEADER_SIZE)
+		 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
+			if (print_err)
+				pr_err("error! Small exttable size in microcode data file\n");
+			return -EINVAL;
+		}
+		ext_header = mc + MC_HEADER_SIZE + data_size;
+		if (ext_table_size != exttable_size(ext_header)) {
+			if (print_err)
+				pr_err("error! Bad exttable size in microcode data file\n");
+			return -EFAULT;
+		}
+		ext_sigcount = ext_header->count;
+	}
+
+	/* check extended table checksum */
+	if (ext_table_size) {
+		int ext_table_sum = 0;
+		int *ext_tablep = (int *)ext_header;
+
+		i = ext_table_size / DWSIZE;
+		while (i--)
+			ext_table_sum += ext_tablep[i];
+		if (ext_table_sum) {
+			if (print_err)
+				pr_warn("aborting, bad extended signature table checksum\n");
+			return -EINVAL;
+		}
+	}
+
+	/* calculate the checksum */
+	orig_sum = 0;
+	i = (MC_HEADER_SIZE + data_size) / DWSIZE;
+	while (i--)
+		orig_sum += ((int *)mc)[i];
+	if (orig_sum) {
+		if (print_err)
+			pr_err("aborting, bad checksum\n");
+		return -EINVAL;
+	}
+	if (!ext_table_size)
+		return 0;
+	/* check extended signature checksum */
+	for (i = 0; i < ext_sigcount; i++) {
+		ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
+			  EXT_SIGNATURE_SIZE * i;
+		sum = orig_sum
+			- (mc_header->sig + mc_header->pf + mc_header->cksum)
+			+ (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
+		if (sum) {
+			if (print_err)
+				pr_err("aborting, bad checksum\n");
+			return -EINVAL;
+		}
+	}
+	return 0;
+}
+EXPORT_SYMBOL_GPL(microcode_sanity_check);
+
+/*
+ * return 0 - no update found
+ * return 1 - found update
+ */
+int get_matching_sig(unsigned int csig, int cpf, void *mc, int rev)
+{
+	struct microcode_header_intel *mc_header = mc;
+	struct extended_sigtable *ext_header;
+	unsigned long total_size = get_totalsize(mc_header);
+	int ext_sigcount, i;
+	struct extended_signature *ext_sig;
+
+	if (update_match_cpu(csig, cpf, mc_header->sig, mc_header->pf))
+		return 1;
+
+	/* Look for ext. headers: */
+	if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
+		return 0;
+
+	ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
+	ext_sigcount = ext_header->count;
+	ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
+
+	for (i = 0; i < ext_sigcount; i++) {
+		if (update_match_cpu(csig, cpf, ext_sig->sig, ext_sig->pf))
+			return 1;
+		ext_sig++;
+	}
+	return 0;
+}
+
+/*
+ * return 0 - no update found
+ * return 1 - found update
+ */
+int get_matching_microcode(unsigned int csig, int cpf, void *mc, int rev)
+{
+	struct microcode_header_intel *mc_header = mc;
+
+	if (!update_match_revision(mc_header, rev))
+		return 0;
+
+	return get_matching_sig(csig, cpf, mc, rev);
+}
+EXPORT_SYMBOL_GPL(get_matching_microcode);

[tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
Commit-ID:  474355fe313391de2429ae225e0fb02f67ec6c31
Gitweb:     http://git.kernel.org/tip/474355fe313391de2429ae225e0fb02f67ec6c31
Author:     Fenghua Yu [off-list ref]
AuthorDate: Thu, 29 Nov 2012 17:47:43 -0800
Committer:  H. Peter Anvin [off-list ref]
CommitDate: Fri, 30 Nov 2012 15:18:16 -0800

x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Implementation of early update ucode on Intel's CPU.

load_ucode_intel_bsp() scans ucode in initrd image file which is a cpio format
ucode followed by ordinary initrd image file. The binary ucode file is stored
in kernel/x86/microcode/GenuineIntel/microcode.bin in the cpio data. All ucode
patches with the same model as BSP are saved in memory. A matching ucode patch
is updated on BSP.

load_ucode_intel_ap() reads saved ucoded patches and updates ucode on AP.

Signed-off-by: Fenghua Yu <redacted>
Link: http://lkml.kernel.org/r/1354240068-9821-6-git-send-email-fenghua.yu@intel.com
Signed-off-by: H. Peter Anvin <redacted>
---
 arch/x86/kernel/microcode_intel_early.c | 438 ++++++++++++++++++++++++++++++++
 1 file changed, 438 insertions(+)
diff --git a/arch/x86/kernel/microcode_intel_early.c b/arch/x86/kernel/microcode_intel_early.c
new file mode 100644
index 0000000..36b1df1
--- /dev/null
+++ b/arch/x86/kernel/microcode_intel_early.c
@@ -0,0 +1,438 @@
+/*
+ *	Intel CPU Microcode Update Driver for Linux
+ *
+ *	Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
+ *			   H Peter Anvin" <hpa@zytor.com>
+ *
+ *	This driver allows to early upgrade microcode on Intel processors
+ *	belonging to IA-32 family - PentiumPro, Pentium II,
+ *	Pentium III, Xeon, Pentium 4, etc.
+ *
+ *	Reference: Section 9.11 of Volume 3, IA-32 Intel Architecture
+ *	Software Developer's Manual.
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License
+ *	as published by the Free Software Foundation; either version
+ *	2 of the License, or (at your option) any later version.
+ */
+#include <linux/module.h>
+#include <linux/vmalloc.h>
+#include <linux/mm.h>
+#include <linux/earlycpio.h>
+#include <asm/msr.h>
+#include <asm/microcode_intel.h>
+#include <asm/processor.h>
+
+struct microcode_intel __initdata *mc_saved_in_initrd[MAX_UCODE_COUNT];
+struct mc_saved_data mc_saved_data;
+
+enum ucode_state
+generic_load_microcode_early(int cpu, struct microcode_intel **mc_saved_p,
+			     unsigned int mc_saved_count,
+			     struct ucode_cpu_info *uci)
+{
+	struct microcode_intel *ucode_ptr, *new_mc = NULL;
+	int new_rev = uci->cpu_sig.rev;
+	enum ucode_state state = UCODE_OK;
+	unsigned int mc_size;
+	struct microcode_header_intel *mc_header;
+	unsigned int csig = uci->cpu_sig.sig;
+	unsigned int cpf = uci->cpu_sig.pf;
+	int i;
+
+	for (i = 0; i < mc_saved_count; i++) {
+		ucode_ptr = mc_saved_p[i];
+		mc_header = (struct microcode_header_intel *)ucode_ptr;
+		mc_size = get_totalsize(mc_header);
+		if (get_matching_microcode(csig, cpf, ucode_ptr, new_rev)) {
+			new_rev = mc_header->rev;
+			new_mc  = ucode_ptr;
+		}
+	}
+
+	if (!new_mc) {
+		state = UCODE_NFOUND;
+		goto out;
+	}
+
+	uci->mc = (struct microcode_intel *)new_mc;
+out:
+	return state;
+}
+EXPORT_SYMBOL_GPL(generic_load_microcode_early);
+
+static enum ucode_state __init
+load_microcode(struct mc_saved_data *mc_saved_data, int cpu)
+{
+	struct ucode_cpu_info *uci = mc_saved_data->ucode_cpu_info + cpu;
+
+	return generic_load_microcode_early(cpu, mc_saved_data->mc_saved,
+					    mc_saved_data->mc_saved_count, uci);
+}
+
+static u8 get_x86_family(unsigned long sig)
+{
+	u8 x86;
+
+	x86 = (sig >> 8) & 0xf;
+
+	if (x86 == 0xf)
+		x86 += (sig >> 20) & 0xff;
+
+	return x86;
+}
+
+static u8 get_x86_model(unsigned long sig)
+{
+	u8 x86, x86_model;
+
+	x86 = get_x86_family(sig);
+	x86_model = (sig >> 4) & 0xf;
+
+	if (x86 == 0x6 || x86 == 0xf)
+		x86_model += ((sig >> 16) & 0xf) << 4;
+
+	return x86_model;
+}
+
+static enum ucode_state
+matching_model_microcode(struct microcode_header_intel *mc_header,
+			unsigned long sig)
+{
+	u8 x86, x86_model;
+	u8 x86_ucode, x86_model_ucode;
+
+	x86 = get_x86_family(sig);
+	x86_model = get_x86_model(sig);
+
+	x86_ucode = get_x86_family(mc_header->sig);
+	x86_model_ucode = get_x86_model(mc_header->sig);
+
+	if (x86 != x86_ucode || x86_model != x86_model_ucode)
+		return UCODE_ERROR;
+
+	return UCODE_OK;
+}
+
+static void
+save_microcode(struct mc_saved_data *mc_saved_data,
+	       struct microcode_intel **mc_saved_src,
+	       unsigned int mc_saved_count)
+{
+	int i;
+	struct microcode_intel **mc_saved_p;
+
+	if (!mc_saved_count)
+		return;
+
+	mc_saved_p = vmalloc(mc_saved_count*sizeof(struct microcode_intel *));
+	if (!mc_saved_p)
+		return;
+
+	for (i = 0; i < mc_saved_count; i++) {
+		struct microcode_intel *mc = mc_saved_src[i];
+		struct microcode_header_intel *mc_header = &mc->hdr;
+		unsigned long mc_size = get_totalsize(mc_header);
+		mc_saved_p[i] = vmalloc(mc_size);
+		if (mc_saved_src[i])
+			memcpy(mc_saved_p[i], mc, mc_size);
+	}
+
+	mc_saved_data->mc_saved = mc_saved_p;
+}
+
+/*
+ * Get microcode matching with BSP's model. Only CPU's with the same model as
+ * BSP can stay in the platform.
+ */
+enum ucode_state
+get_matching_model_microcode(int cpu, void *data, size_t size,
+			     struct mc_saved_data *mc_saved_data,
+			     struct microcode_intel **mc_saved_in_initrd,
+			     enum system_states system_state)
+{
+	u8 *ucode_ptr = data;
+	unsigned int leftover = size;
+	enum ucode_state state = UCODE_OK;
+	unsigned int mc_size;
+	struct microcode_header_intel *mc_header;
+	struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
+	size_t mc_saved_size;
+	size_t mem_size;
+	unsigned int mc_saved_count = mc_saved_data->mc_saved_count;
+	struct ucode_cpu_info *uci = mc_saved_data->ucode_cpu_info + cpu;
+	int found = 0;
+	int i;
+
+	if (mc_saved_count) {
+		mem_size = mc_saved_count * sizeof(struct microcode_intel *);
+		memcpy(mc_saved_tmp, mc_saved_data->mc_saved, mem_size);
+	}
+
+	while (leftover) {
+		mc_header = (struct microcode_header_intel *)ucode_ptr;
+
+		mc_size = get_totalsize(mc_header);
+		if (!mc_size || mc_size > leftover ||
+			microcode_sanity_check(ucode_ptr, 0) < 0)
+			break;
+
+		leftover -= mc_size;
+		if (matching_model_microcode(mc_header, uci->cpu_sig.sig) !=
+			 UCODE_OK) {
+			ucode_ptr += mc_size;
+			continue;
+		}
+
+		found = 0;
+		for (i = 0; i < mc_saved_count; i++) {
+			unsigned int sig, pf;
+			unsigned int new_rev;
+			struct microcode_header_intel *mc_saved_header =
+			     (struct microcode_header_intel *)mc_saved_tmp[i];
+			sig = mc_saved_header->sig;
+			pf = mc_saved_header->pf;
+			new_rev = mc_header->rev;
+
+			if (get_matching_sig(sig, pf, ucode_ptr, new_rev)) {
+				found = 1;
+				if (update_match_revision(mc_header, new_rev)) {
+					/*
+					 * Found an older ucode saved before.
+					 * Replace the older one with this newer
+					 * one.
+					 */
+					mc_saved_tmp[i] =
+					(struct microcode_intel *)ucode_ptr;
+					break;
+				}
+			}
+		}
+		if (i >= mc_saved_count && !found)
+			/*
+			 * This ucode is first time discovered in ucode file.
+			 * Save it to memory.
+			 */
+			mc_saved_tmp[mc_saved_count++] =
+					 (struct microcode_intel *)ucode_ptr;
+
+		ucode_ptr += mc_size;
+	}
+
+	if (leftover) {
+		state = UCODE_ERROR;
+		goto out;
+	}
+
+	if (mc_saved_count == 0) {
+		state = UCODE_NFOUND;
+		goto out;
+	}
+
+	if (system_state == SYSTEM_RUNNING) {
+		vfree(mc_saved_data->mc_saved);
+		save_microcode(mc_saved_data, mc_saved_tmp, mc_saved_count);
+	} else {
+		mc_saved_size = sizeof(struct microcode_intel *) *
+				mc_saved_count;
+		memcpy(mc_saved_in_initrd, mc_saved_tmp, mc_saved_size);
+		mc_saved_data->mc_saved = mc_saved_in_initrd;
+	}
+
+	mc_saved_data->mc_saved_count = mc_saved_count;
+out:
+	return state;
+}
+EXPORT_SYMBOL_GPL(get_matching_model_microcode);
+
+#define native_rdmsr(msr, val1, val2)		\
+do {						\
+	u64 __val = native_read_msr((msr));	\
+	(void)((val1) = (u32)__val);		\
+	(void)((val2) = (u32)(__val >> 32));	\
+} while (0)
+
+#define native_wrmsr(msr, low, high)		\
+	native_write_msr(msr, low, high);
+
+static int __cpuinit collect_cpu_info_early(struct ucode_cpu_info *uci)
+{
+	unsigned int val[2];
+	u8 x86, x86_model;
+	struct cpu_signature csig = {0, 0, 0};
+	unsigned int eax, ebx, ecx, edx;
+
+	memset(uci, 0, sizeof(*uci));
+
+	eax = 0x00000001;
+	ecx = 0;
+	native_cpuid(&eax, &ebx, &ecx, &edx);
+	csig.sig = eax;
+
+	x86 = get_x86_family(csig.sig);
+	x86_model = get_x86_model(csig.sig);
+
+	if ((x86_model >= 5) || (x86 > 6)) {
+		/* get processor flags from MSR 0x17 */
+		native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
+		csig.pf = 1 << ((val[1] >> 18) & 7);
+	}
+
+	/* get the current revision from MSR 0x8B */
+	native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
+
+	csig.rev = val[1];
+
+	uci->cpu_sig = csig;
+	uci->valid = 1;
+
+	return 0;
+}
+
+static __init enum ucode_state
+scan_microcode(unsigned long start, unsigned long end,
+		struct mc_saved_data *mc_saved_data,
+		struct microcode_intel **mc_saved_in_initrd)
+{
+	unsigned int size = end - start + 1;
+	struct cpio_data cd = { 0, 0 };
+	char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
+	long offset = 0;
+
+	cd = find_cpio_data(ucode_name, (void *)start, size, &offset);
+	if (!cd.data)
+		return UCODE_ERROR;
+
+	return get_matching_model_microcode(0, cd.data, cd.size, mc_saved_data,
+					 mc_saved_in_initrd, SYSTEM_BOOTING);
+}
+
+static int __init
+apply_microcode_early(struct mc_saved_data *mc_saved_data, int cpu)
+{
+	struct ucode_cpu_info *uci = mc_saved_data->ucode_cpu_info + cpu;
+	struct microcode_intel *mc_intel;
+	unsigned int val[2];
+
+	/* We should bind the task to the CPU */
+	mc_intel = uci->mc;
+	if (mc_intel == NULL)
+		return 0;
+
+	/* write microcode via MSR 0x79 */
+	native_wrmsr(MSR_IA32_UCODE_WRITE,
+	      (unsigned long) mc_intel->bits,
+	      (unsigned long) mc_intel->bits >> 16 >> 16);
+	native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
+
+	/* As documented in the SDM: Do a CPUID 1 here */
+	sync_core();
+
+	/* get the current revision from MSR 0x8B */
+	native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
+	if (val[1] != mc_intel->hdr.rev)
+		return -1;
+
+	uci->cpu_sig.rev = val[1];
+
+	return 0;
+}
+
+#ifdef CONFIG_X86_32
+static void __init map_mc_saved(struct mc_saved_data *mc_saved_data,
+				struct microcode_intel **mc_saved_in_initrd)
+{
+	int i;
+
+	if (mc_saved_data->mc_saved) {
+		for (i = 0; i < mc_saved_data->mc_saved_count; i++)
+			mc_saved_data->mc_saved[i] =
+					 __va(mc_saved_data->mc_saved[i]);
+
+		mc_saved_data->mc_saved = __va(mc_saved_data->mc_saved);
+	}
+
+	if (mc_saved_data->ucode_cpu_info->mc)
+		mc_saved_data->ucode_cpu_info->mc =
+				 __va(mc_saved_data->ucode_cpu_info->mc);
+	mc_saved_data->ucode_cpu_info = __va(mc_saved_data->ucode_cpu_info);
+}
+#else
+static inline void __init map_mc_saved(struct mc_saved_data *mc_saved_data,
+				struct microcode_intel **mc_saved_in_initrd)
+{
+}
+#endif
+
+void __init save_microcode_in_initrd(struct mc_saved_data *mc_saved_data,
+		 struct microcode_intel **mc_saved_in_initrd)
+{
+	unsigned int count = mc_saved_data->mc_saved_count;
+
+	save_microcode(mc_saved_data, mc_saved_in_initrd, count);
+}
+
+static void __init
+_load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data,
+		      struct microcode_intel **mc_saved_in_initrd,
+		      unsigned long initrd_start, unsigned long initrd_end)
+{
+	int cpu = 0;
+
+#ifdef CONFIG_X86_64
+	mc_saved_data->ucode_cpu_info = ucode_cpu_info_early;
+#else
+	mc_saved_data->ucode_cpu_info =
+			(struct ucode_cpu_info *)__pa(ucode_cpu_info_early);
+#endif
+	collect_cpu_info_early(mc_saved_data->ucode_cpu_info + cpu);
+	scan_microcode(initrd_start, initrd_end, mc_saved_data,
+		       mc_saved_in_initrd);
+	load_microcode(mc_saved_data, cpu);
+	apply_microcode_early(mc_saved_data, cpu);
+	map_mc_saved(mc_saved_data, mc_saved_in_initrd);
+}
+
+void __init
+load_ucode_intel_bsp(char *real_mode_data)
+{
+	u64 ramdisk_image, ramdisk_size, ramdisk_end;
+	unsigned long initrd_start, initrd_end;
+	struct boot_params *boot_params;
+
+	boot_params = (struct boot_params *)real_mode_data;
+	ramdisk_image = boot_params->hdr.ramdisk_image;
+	ramdisk_size  = boot_params->hdr.ramdisk_size;
+
+#ifdef CONFIG_X86_64
+	ramdisk_end  = PAGE_ALIGN(ramdisk_image + ramdisk_size);
+	initrd_start = ramdisk_image + PAGE_OFFSET;
+	initrd_end = initrd_start + ramdisk_size;
+	_load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd,
+			      initrd_start, initrd_end);
+#else
+	ramdisk_end  = ramdisk_image + ramdisk_size;
+	initrd_start = ramdisk_image;
+	initrd_end = initrd_start + ramdisk_size;
+	_load_ucode_intel_bsp((struct mc_saved_data *)__pa(&mc_saved_data),
+			(struct microcode_intel **)__pa(mc_saved_in_initrd),
+			initrd_start, initrd_end);
+#endif
+}
+
+void __cpuinit load_ucode_intel_ap(void)
+{
+	int cpu = smp_processor_id();
+
+	/*
+	 * If BSP doesn't find valid ucode and save it in memory, no need to
+	 * update ucode on this AP.
+	 */
+	if (!mc_saved_data.mc_saved)
+		return;
+
+	collect_cpu_info_early(mc_saved_data.ucode_cpu_info + cpu);
+	load_microcode(&mc_saved_data, cpu);
+	apply_microcode_early(&mc_saved_data, cpu);
+}

[tip:x86/microcode] x86/head_32.S: Early update ucode in 32-bit

Message details and related content
Commit-ID:  eadd948a43145b54b2da8ee4d83774ac2aa6ee02
Gitweb:     http://git.kernel.org/tip/eadd948a43145b54b2da8ee4d83774ac2aa6ee02
Author:     Fenghua Yu [off-list ref]
AuthorDate: Thu, 29 Nov 2012 17:47:44 -0800
Committer:  H. Peter Anvin [off-list ref]
CommitDate: Fri, 30 Nov 2012 15:18:16 -0800

x86/head_32.S: Early update ucode in 32-bit

This updates ucode in 32-bit kernel. At this point, there is no paging and no
virtual address yet.

Signed-off-by: Fenghua Yu <redacted>
Link: http://lkml.kernel.org/r/1354240068-9821-7-git-send-email-fenghua.yu@intel.com
Signed-off-by: H. Peter Anvin <redacted>
---
 arch/x86/kernel/head_32.S | 6 ++++++
 1 file changed, 6 insertions(+)
diff --git a/arch/x86/kernel/head_32.S b/arch/x86/kernel/head_32.S
index 957a47a..b7b76a4 100644
--- a/arch/x86/kernel/head_32.S
+++ b/arch/x86/kernel/head_32.S
@@ -144,6 +144,12 @@ ENTRY(startup_32)
 	movl %eax, pa(olpc_ofw_pgd)
 #endif
 
+#ifdef CONFIG_MICROCODE_EARLY
+	/* Early load ucode on BSP. */
+	movl $pa(boot_params), %eax
+	call load_ucode_bsp
+#endif
+
 /*
  * Initialize page tables.  This creates a PDE and a set of page
  * tables, which are located immediately beyond __brk_base.  The variable

[tip:x86/microcode] x86/head64.c: Early update ucode in 64-bit

Message details and related content
Commit-ID:  b8f3111a12c190ab8228718e766dd2707b6fc8e0
Gitweb:     http://git.kernel.org/tip/b8f3111a12c190ab8228718e766dd2707b6fc8e0
Author:     Fenghua Yu [off-list ref]
AuthorDate: Thu, 29 Nov 2012 17:47:45 -0800
Committer:  H. Peter Anvin [off-list ref]
CommitDate: Fri, 30 Nov 2012 15:18:16 -0800

x86/head64.c: Early update ucode in 64-bit

This updates ucode in 64-bit mode. Paging and virtual address are working now.

Signed-off-by: Fenghua Yu <redacted>
Link: http://lkml.kernel.org/r/1354240068-9821-8-git-send-email-fenghua.yu@intel.com
Signed-off-by: H. Peter Anvin <redacted>
---
 arch/x86/kernel/head64.c | 6 ++++++
 1 file changed, 6 insertions(+)
diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c
index 037df57..a512f56 100644
--- a/arch/x86/kernel/head64.c
+++ b/arch/x86/kernel/head64.c
@@ -25,6 +25,7 @@
 #include <asm/kdebug.h>
 #include <asm/e820.h>
 #include <asm/bios_ebda.h>
+#include <asm/microcode.h>
 
 static void __init zap_identity_mappings(void)
 {
@@ -73,6 +74,11 @@ void __init x86_64_start_kernel(char * real_mode_data)
 	/* clear bss before set_intr_gate with early_idt_handler */
 	clear_bss();
 
+	/*
+	 * Load microcode early on BSP.
+	 */
+	load_ucode_bsp(real_mode_data);
+
 	/* Make NULL pointers segfault */
 	zap_identity_mappings();
 

[tip:x86/microcode] x86/smpboot.c: Early update ucode on AP

Message details and related content
Commit-ID:  27f7c88afbdff536e65bbf1507ba63ce20eb0ba3
Gitweb:     http://git.kernel.org/tip/27f7c88afbdff536e65bbf1507ba63ce20eb0ba3
Author:     Fenghua Yu [off-list ref]
AuthorDate: Thu, 29 Nov 2012 17:47:46 -0800
Committer:  H. Peter Anvin [off-list ref]
CommitDate: Fri, 30 Nov 2012 15:18:16 -0800

x86/smpboot.c: Early update ucode on AP

This updates ucode on AP. At this point, BSP should store some valid ucode
patches in memory if it finds the ucode patches in initrd. AP searches the
stored ucode and uploads the ucode.

Signed-off-by: Fenghua Yu <redacted>
Link: http://lkml.kernel.org/r/1354240068-9821-9-git-send-email-fenghua.yu@intel.com
Signed-off-by: H. Peter Anvin <redacted>
---
 arch/x86/kernel/smpboot.c | 7 +++++++
 1 file changed, 7 insertions(+)
diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index c80a33b..f616692 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -76,6 +76,7 @@
 #include <asm/i8259.h>
 
 #include <asm/realmode.h>
+#include <asm/microcode.h>
 
 /* State of each CPU */
 DEFINE_PER_CPU(int, cpu_state) = { 0 };
@@ -239,6 +240,12 @@ notrace static void __cpuinit start_secondary(void *unused)
 	 * most necessary things.
 	 */
 	cpu_init();
+	/*
+	 * Load microcode on this cpu if a valid microcode is available.
+	 * This is early microcode loading procedure.
+	 */
+	load_ucode_ap();
+
 	x86_cpuinit.early_percpu_clock_init();
 	preempt_disable();
 	smp_callin();

[tip:x86/microcode] x86/mm/init.c: Copy ucode from initrd image to memory

Message details and related content
Commit-ID:  834137534b34b2ef0e1c3c00ad6a31e06b604e00
Gitweb:     http://git.kernel.org/tip/834137534b34b2ef0e1c3c00ad6a31e06b604e00
Author:     Fenghua Yu [off-list ref]
AuthorDate: Thu, 29 Nov 2012 17:47:47 -0800
Committer:  H. Peter Anvin [off-list ref]
CommitDate: Fri, 30 Nov 2012 15:18:17 -0800

x86/mm/init.c: Copy ucode from initrd image to memory

Before initrd image is freed, copy valid ucode patches from initrd image
to kernel virtual memory. The saved ucode will be used to update AP in resume.

Signed-off-by: Fenghua Yu <redacted>
Link: http://lkml.kernel.org/r/1354240068-9821-10-git-send-email-fenghua.yu@intel.com
Signed-off-by: H. Peter Anvin <redacted>
---
 arch/x86/mm/init.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c
index d7aea41..a294d4b 100644
--- a/arch/x86/mm/init.c
+++ b/arch/x86/mm/init.c
@@ -16,6 +16,7 @@
 #include <asm/tlb.h>
 #include <asm/proto.h>
 #include <asm/dma.h>		/* for MAX_DMA_PFN */
+#include <asm/microcode.h>
 
 unsigned long __initdata pgt_buf_start;
 unsigned long __meminitdata pgt_buf_end;
@@ -391,6 +392,15 @@ void free_initmem(void)
 #ifdef CONFIG_BLK_DEV_INITRD
 void __init free_initrd_mem(unsigned long start, unsigned long end)
 {
+#ifdef CONFIG_MICROCODE_EARLY
+	/*
+	 * Remember, initrd memory may contain microcode or other useful things.
+	 * Before we lose initrd mem, we need to find a place to hold them
+	 * now that normal virtual memory is enabled.
+	 */
+	save_microcode_in_initrd(&mc_saved_data, mc_saved_in_initrd);
+#endif
+
 	/*
 	 * end could be not aligned, and We can not align that,
 	 * decompresser could be confused by aligned initrd_end

[tip:x86/microcode] x86/Kconfig: Configurations to enable/ disable the feature

Message details and related content
Commit-ID:  16544f8d32aca4ec97b22db880f879ee164c3658
Gitweb:     http://git.kernel.org/tip/16544f8d32aca4ec97b22db880f879ee164c3658
Author:     Fenghua Yu [off-list ref]
AuthorDate: Thu, 29 Nov 2012 17:47:48 -0800
Committer:  H. Peter Anvin [off-list ref]
CommitDate: Fri, 30 Nov 2012 15:18:17 -0800

x86/Kconfig: Configurations to enable/disable the feature

MICROCODE_INTEL_LIB, MICROCODE_INTEL_EARLY, and MICROCODE_EARLY are three new
configurations to enable or disable the feature.

Signed-off-by: Fenghua Yu <redacted>
Link: http://lkml.kernel.org/r/1354240068-9821-11-git-send-email-fenghua.yu@intel.com
Signed-off-by: H. Peter Anvin <redacted>
---
 arch/x86/Kconfig | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 46c3bff..abb1153 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -1031,6 +1031,24 @@ config MICROCODE_OLD_INTERFACE
 	def_bool y
 	depends on MICROCODE
 
+config MICROCODE_INTEL_LIB
+	def_bool y
+	depends on MICROCODE_INTEL
+
+config MICROCODE_INTEL_EARLY
+	bool "Early load microcode"
+	depends on MICROCODE_INTEL && BLK_DEV_INITRD
+	default y
+	help
+	  This option provides functionality to read additional microcode data
+	  at the beginning of initrd image. The data tells kernel to load
+	  microcode to CPU's as early as possible. No functional change if no
+	  microcode data is glued to the initrd, therefore it's safe to say Y.
+
+config MICROCODE_EARLY
+	def_bool y
+	depends on MICROCODE_INTEL_EARLY
+
 config X86_MSR
 	tristate "/dev/cpu/*/msr - Model-specific register support"
 	---help---

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Fri, Nov 30, 2012 at 4:25 PM, tip-bot for Fenghua Yu
[off-list ref] wrote:
quoted hunk
Commit-ID:  474355fe313391de2429ae225e0fb02f67ec6c31
Gitweb:     http://git.kernel.org/tip/474355fe313391de2429ae225e0fb02f67ec6c31
Author:     Fenghua Yu [off-list ref]
AuthorDate: Thu, 29 Nov 2012 17:47:43 -0800
Committer:  H. Peter Anvin [off-list ref]
CommitDate: Fri, 30 Nov 2012 15:18:16 -0800

x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Implementation of early update ucode on Intel's CPU.

load_ucode_intel_bsp() scans ucode in initrd image file which is a cpio format
ucode followed by ordinary initrd image file. The binary ucode file is stored
in kernel/x86/microcode/GenuineIntel/microcode.bin in the cpio data. All ucode
patches with the same model as BSP are saved in memory. A matching ucode patch
is updated on BSP.

load_ucode_intel_ap() reads saved ucoded patches and updates ucode on AP.

Signed-off-by: Fenghua Yu <redacted>
Link: http://lkml.kernel.org/r/1354240068-9821-6-git-send-email-fenghua.yu@intel.com
Signed-off-by: H. Peter Anvin <redacted>
---
 arch/x86/kernel/microcode_intel_early.c | 438 ++++++++++++++++++++++++++++++++
 1 file changed, 438 insertions(+)
diff --git a/arch/x86/kernel/microcode_intel_early.c b/arch/x86/kernel/microcode_intel_early.c
new file mode 100644
index 0000000..36b1df1
--- /dev/null
+++ b/arch/x86/kernel/microcode_intel_early.c
@@ -0,0 +1,438 @@
+/*
+ *     Intel CPU Microcode Update Driver for Linux
+ *
+ *     Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
+ *                        H Peter Anvin" <hpa@zytor.com>
+ *
+ *     This driver allows to early upgrade microcode on Intel processors
+ *     belonging to IA-32 family - PentiumPro, Pentium II,

430 more quoted lines

looks like this patches do not consider that initrd could be relocated
even 64bit.

may need to copy the ucode.bin to BRK at first. that will make code
much simple, and later does not need to
copy them back in free_bootmem_initrd.

aka, this patchset is not ready for 3.8 even.

Thanks

Yinghai

RE: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
From: <redacted> [mailto:<redacted>] On Behalf Of
Yinghai Lu
On Fri, Nov 30, 2012 at 4:25 PM, tip-bot for Fenghua Yu
[off-list ref] wrote:
quoted
Commit-ID:  474355fe313391de2429ae225e0fb02f67ec6c31
Gitweb:
http://git.kernel.org/tip/474355fe313391de2429ae225e0fb02f67ec6c31
quoted
Author:     Fenghua Yu [off-list ref]
AuthorDate: Thu, 29 Nov 2012 17:47:43 -0800
Committer:  H. Peter Anvin [off-list ref]
CommitDate: Fri, 30 Nov 2012 15:18:16 -0800

x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Implementation of early update ucode on Intel's CPU.

load_ucode_intel_bsp() scans ucode in initrd image file which is a
cpio format
quoted
ucode followed by ordinary initrd image file. The binary ucode file
is stored
quoted
in kernel/x86/microcode/GenuineIntel/microcode.bin in the cpio data.
All ucode
quoted
patches with the same model as BSP are saved in memory. A matching
ucode patch
quoted
is updated on BSP.

load_ucode_intel_ap() reads saved ucoded patches and updates ucode on
AP.
quoted
looks like this patches do not consider that initrd could be relocated
even 64bit.

may need to copy the ucode.bin to BRK at first. that will make code
much simple, and later does not need to
copy them back in free_bootmem_initrd.

aka, this patchset is not ready for 3.8 even.
I will relocate saved microcode blob (mc_saved_in_initrd) after initrd is
relocated in updated patches. Thus, mc_saved_in_initrd always point to
right initrd during boot time.

Thanks.

-Fenghua

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Mon, Dec 3, 2012 at 4:18 PM, Yu, Fenghua [off-list ref] wrote:
quoted
From: <redacted> [mailto:<redacted>] On Behalf Of
Yinghai Lu

may need to copy the ucode.bin to BRK at first. that will make code
much simple, and later does not need to
copy them back in free_bootmem_initrd.

aka, this patchset is not ready for 3.8 even.
I will relocate saved microcode blob (mc_saved_in_initrd) after initrd is
relocated in updated patches. Thus, mc_saved_in_initrd always point to
right initrd during boot time.
No, you should not copy that several times.

just pre-allocate some kbytes in BRK, and copy to there one time.

Yinghai

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Mon, Dec 10, 2012 at 7:41 PM, H. Peter Anvin [off-list ref] wrote:
On 12/10/2012 06:39 PM, Yinghai Lu wrote:
quoted
No, you should not copy that several times.

just pre-allocate some kbytes in BRK, and copy to there one time.
He doesn't copy it several times.  He just saves an offset into the
initrd blob.
ucode is together with initrd blob, and code scan that blob, and save
the pointer about ucode,
then BSP use it, and APs use it
after that when initrd get freed,  that ucode is copied to somewhere...

and his patch missed initrd could be get relocated for 64bit and 32bit.
So AP would not find that saved ucode.

After i pointed that, he said he will update the pointer when relocate
the initrd for AP.

And my suggestion is: after scan and find the ucode, save it to BRK,
so don't need to adjust
pointer again, and don't need to copy the blob and update the pointer again.

Yinghai

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/10/2012 07:55 PM, Yinghai Lu wrote:
And my suggestion is: after scan and find the ucode, save it to BRK,
so don't need to adjust
pointer again, and don't need to copy the blob and update the pointer again.
That doesn't work if the microcode is replaced at runtime.  However, vmalloc doesn't work either since 32 bits needs any one blob to be physically contiguous.  I have suggested Fenghua replace it with a linked list of kmalloc areas, one for each blob.

	-hpa

-- 
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Mon, Dec 10, 2012 at 10:34 PM, H. Peter Anvin [off-list ref] wrote:
That doesn't work if the microcode is replaced at runtime.  However, vmalloc
doesn't work either since 32 bits needs any one blob to be physically
contiguous.  I have suggested Fenghua replace it with a linked list of
kmalloc areas, one for each blob.
you mean:
keep the all of version, and update code need to go over the list to
find latest before apply update?

BTW, do we really need to update microcode so early?

Yinghai

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Mon, Dec 10, 2012 at 11:07:38PM -0800, Yinghai Lu wrote:
BTW, do we really need to update microcode so early?
Yes we do. Normally ucode gets applied by the BIOS - this early approach
is for those cases where OEMs don't release new BIOS anymore but we
still need to apply a ucode patch as early as possible.

-- 
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Tue, Dec 11, 2012 at 6:57 AM, Borislav Petkov [off-list ref] wrote:
On Mon, Dec 10, 2012 at 11:07:38PM -0800, Yinghai Lu wrote:
quoted
BTW, do we really need to update microcode so early?
Yes we do. Normally ucode gets applied by the BIOS - this early approach
is for those cases where OEMs don't release new BIOS anymore but we
still need to apply a ucode patch as early as possible.
but old bios with old ucode still work with some kind of previous kernel?

Do we have case that system would not work with kernel if ucode is not
updated so early?

Yinghai

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/11/2012 08:46 AM, Yinghai Lu wrote:
On Tue, Dec 11, 2012 at 6:57 AM, Borislav Petkov [off-list ref] wrote:
quoted
On Mon, Dec 10, 2012 at 11:07:38PM -0800, Yinghai Lu wrote:
quoted
BTW, do we really need to update microcode so early?
Yes we do. Normally ucode gets applied by the BIOS - this early approach
is for those cases where OEMs don't release new BIOS anymore but we
still need to apply a ucode patch as early as possible.
but old bios with old ucode still work with some kind of previous kernel?

Do we have case that system would not work with kernel if ucode is not
updated so early?
It's not about "not working"... it is about "if the microcode isn't
loaded early we have to disable features."

	-hpa

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Tue, Dec 11, 2012 at 8:48 AM, H. Peter Anvin [off-list ref] wrote:
It's not about "not working"... it is about "if the microcode isn't
loaded early we have to disable features."
ok, then next question is how early it should be.

before early_cpu_init/early_identify_cpu

or just before check_bugs/identify_cpu

Yinghai

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Tue, Dec 11, 2012 at 09:00:55AM -0800, Yinghai Lu wrote:
ok, then next question is how early it should be.

before early_cpu_init/early_identify_cpu

or just before check_bugs/identify_cpu
Read the code. It's in x86_64_start_kernel on 64-bit.

-- 
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Tue, Dec 11, 2012 at 9:06 AM, Borislav Petkov [off-list ref] wrote:
On Tue, Dec 11, 2012 at 09:00:55AM -0800, Yinghai Lu wrote:
quoted
ok, then next question is how early it should be.

before early_cpu_init/early_identify_cpu

or just before check_bugs/identify_cpu
Read the code. It's in x86_64_start_kernel on 64-bit.
No, that is not right place. initrd could be loaded anywhere like way
high by bootloader.

to make code simple, we should have following sequence in setup_arch

early_ioremap_init()
early_update_microcode()...
early_cpu_init()

early_update_microcode could use early_ioremap to access initrd ramdisk area.

Yinghai

RE: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
-----Original Message-----
From: <redacted> [mailto:<redacted>] On Behalf Of
Yinghai Lu
Sent: Tuesday, December 11, 2012 9:16 AM
To: Borislav Petkov; Yinghai Lu; H. Peter Anvin; Yu, Fenghua;
mingo@kernel.org; linux-kernel@vger.kernel.org; tglx@linutronix.de;
hpa@linux.intel.com; linux-tip-commits@vger.kernel.org
Subject: Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early
update ucode on Intel's CPU

On Tue, Dec 11, 2012 at 9:06 AM, Borislav Petkov [off-list ref] wrote:
quoted
On Tue, Dec 11, 2012 at 09:00:55AM -0800, Yinghai Lu wrote:
quoted
ok, then next question is how early it should be.

before early_cpu_init/early_identify_cpu

or just before check_bugs/identify_cpu
Read the code. It's in x86_64_start_kernel on 64-bit.
No, that is not right place. initrd could be loaded anywhere like way
high by bootloader.

to make code simple, we should have following sequence in setup_arch

early_ioremap_init()
early_update_microcode()...
early_cpu_init()

early_update_microcode could use early_ioremap to access initrd ramdisk
area.
The problem is mainly from 32-bit. We need to load microcode before paging is enabled (I'm changing load_ucode_intel_ap to be called before paging as well). That makes the coding not simple. It's better to load ucode as early as possible in 64-bit as well. I don't want to have two totally different sets of early loading ucode for 64-bit and 32-bit. 

Thanks.

-Fenghua

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/11/2012 09:15 AM, Yinghai Lu wrote:
No, that is not right place. initrd could be loaded anywhere like way
high by bootloader.
Only *after* your changes... the current protocol doesn't allow that.

Anyway, we need to deal with it, see below.
to make code simple, we should have following sequence in setup_arch

early_ioremap_init()
early_update_microcode()...
early_cpu_init()

early_update_microcode could use early_ioremap to access initrd ramdisk area.
We need there to be as little machinery as possible, *especially* machinery involving paging, before the microcode gets loaded.  I would prefer if we could load the microcode before enabling paging at all, but that would mean running it in 32-bit mode which really isn't practical, so we have to deal with scaffolding here.

We really should cycle the paging off after this happens, but that really isn't possible until the trampoline gets set up later, since we may not have any other place to stand in low memory.

This complexity is the cost of allowing the kernel to load above the 4G mark.

Now, to be solution-focused...

I quite frankly don't see anything in early_remap_init() which can't be done at compile time.  All it does is set up some data structures which could just as well be statically generated, and then it would be available from the very beginning and thus usable for this purpose. Xen might need extra song & dance, but that can be done in Xen-specific code.

The Xen-induced (and since then expanded by others) 32-bit braindamage of having __FIXADDR_TOP be a variable is a huge problem, obviously, but it doesn't affect 64 bits which is what we're dealing with here.  There *is* a way to fix it on 32 bits without breaking Xen, which is to move the fixmap to the beginning of kernel virtual space instead of the end -- this boundary is known at compile time.

	-hpa

-- 
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/11/2012 09:15 AM, Yinghai Lu wrote:
On Tue, Dec 11, 2012 at 9:06 AM, Borislav Petkov [off-list ref] wrote:
quoted
On Tue, Dec 11, 2012 at 09:00:55AM -0800, Yinghai Lu wrote:
quoted
ok, then next question is how early it should be.

before early_cpu_init/early_identify_cpu

or just before check_bugs/identify_cpu
Read the code. It's in x86_64_start_kernel on 64-bit.
No, that is not right place. initrd could be loaded anywhere like way
high by bootloader.
The more I think about it, the more I think the right answer is the one we have pretty stated all along: if using the 64-bit entry point it is the responsibility of the boot loader to make sure the kernel, the setup data, and the initramfs are all mapped on entry.

	-hpa
-- 
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/11/2012 10:02 AM, H. Peter Anvin wrote:
The more I think about it, the more I think the right answer is the one
we have pretty stated all along: if using the 64-bit entry point it is
the responsibility of the boot loader to make sure the kernel, the setup
data, and the initramfs are all mapped on entry.
This, in turn, brings up another major problem with the 64-bit entry point: right now it assumes page tables set up the way the current kernels expect them, but the way the kernel expects pages to be laid out on the future is almost guaranteed to change.  We need to formalize the expectations of the page table layout at 64-bit entry, and check to see what the implications of that are.

In particular, we may need to build a set of scaffolding page tables in the brk when entered from the 64-bit entry point and switch to them a lot sooner.  We wouldn't have to do that when coming from the 16/32-bit entry points since we'd control the layout of those page tables.

Oh how fun...

	-hpa

-- 
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Tue, Dec 11, 2012 at 10:20 AM, H. Peter Anvin [off-list ref] wrote:
On 12/11/2012 10:02 AM, H. Peter Anvin wrote:
quoted

The more I think about it, the more I think the right answer is the one
we have pretty stated all along: if using the 64-bit entry point it is
the responsibility of the boot loader to make sure the kernel, the setup
data, and the initramfs are all mapped on entry.
arch/x86/kernel/head_64.S::startup_64

is setting up its own page table
This, in turn, brings up another major problem with the 64-bit entry point:
right now it assumes page tables set up the way the current kernels expect
them, but the way the kernel expects pages to be laid out on the future is
almost guaranteed to change.  We need to formalize the expectations of the
page table layout at 64-bit entry, and check to see what the implications of
that are.

In particular, we may need to build a set of scaffolding page tables in the
brk when entered from the 64-bit entry point and switch to them a lot
sooner.  We wouldn't have to do that when coming from the 16/32-bit entry
points since we'd control the layout of those page tables.

Oh how fun...
now in for-x86-boot:
http://git.kernel.org/?p=linux/kernel/git/yinghai/linux-yinghai.git;a=commit;h=8e4e093e6d140f1316953437fdde4e826f5cfd98

it adds extra mapping from the whole kernel when kernel is loaded above 1G.
from round_down(_text, 2M) to round_up(_end -1, 2M).

Do you mean we need to add extra mapping for realmode_data, cmdline,
ramdisk too? (not include setup_data, and it is accessed via
early_ioremap later).

but if the user memmap to exclude some page, we will still need to
relocate the ramdisk.

Yinghai

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/11/2012 10:42 AM, Yinghai Lu wrote:
now in for-x86-boot:
http://git.kernel.org/?p=linux/kernel/git/yinghai/linux-yinghai.git;a=commit;h=8e4e093e6d140f1316953437fdde4e826f5cfd98

it adds extra mapping from the whole kernel when kernel is loaded above 1G.
from round_down(_text, 2M) to round_up(_end -1, 2M).

Do you mean we need to add extra mapping for realmode_data, cmdline,
ramdisk too? (not include setup_data, and it is accessed via
early_ioremap later).
Yes, but...
but if the user memmap to exclude some page, we will still need to
relocate the ramdisk.
-ENOPARSE

I really need to look at this in more detail.  I'm starting to think
this is done completely backwards.

	-hpa

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Tue, Dec 11, 2012 at 10:46 AM, H. Peter Anvin [off-list ref] wrote:
On 12/11/2012 10:42 AM, Yinghai Lu wrote:
quoted
now in for-x86-boot:
http://git.kernel.org/?p=linux/kernel/git/yinghai/linux-yinghai.git;a=commit;h=8e4e093e6d140f1316953437fdde4e826f5cfd98

it adds extra mapping from the whole kernel when kernel is loaded above 1G.
from round_down(_text, 2M) to round_up(_end -1, 2M).

Do you mean we need to add extra mapping for realmode_data, cmdline,
ramdisk too? (not include setup_data, and it is accessed via
early_ioremap later).
Yes, but...
that will be bunch of asm code again, and need to parse the setup_header in that
asm to get range value for those regions...
quoted
but if the user memmap to exclude some page, we will still need to
relocate the ramdisk.
-ENOPARSE
.. I mean pointer for saved ucode will be updated then again.
and relocated_initrd wil still need to use ioremap because init_memory_mapping
will clean mapping the range range that is excluded by memmap=XX$YY
I really need to look at this in more detail.  I'm starting to think
this is done completely backwards.
really should not put asm code if it could be done in C in not complicated way.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/11/2012 11:18 AM, Yinghai Lu wrote:
that will be bunch of asm code again, and need to parse the setup_header in that
asm to get range value for those regions...
It's an index into an array, it's not "parsing".
quoted
quoted
but if the user memmap to exclude some page, we will still need to
relocate the ramdisk.
-ENOPARSE
.. I mean pointer for saved ucode will be updated then again.
and relocated_initrd wil still need to use ioremap because init_memory_mapping
will clean mapping the range range that is excluded by memmap=XX$YY
quoted
I really need to look at this in more detail.  I'm starting to think
this is done completely backwards.
really should not put asm code if it could be done in C in not complicated way.
Uhm... that's not what I'm talking about.

	-hpa

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Tue, Dec 11, 2012 at 9:38 AM, H. Peter Anvin [off-list ref] wrote:
On 12/11/2012 09:15 AM, Yinghai Lu wrote:
quoted

No, that is not right place. initrd could be loaded anywhere like way
high by bootloader.
Only *after* your changes... the current protocol doesn't allow that.
before for-x86-boot change,

bootloader put ramdisk just under 2g...

[    0.000000] memblock_reserve: [0x7d9dc000-0x7fffefff] RAMDISK

and arch/x86/kernel/head_64.S only set ident mapping for [0,1g)

so before init_memory_mapping, we need to use early_ioremap to access
ramdisk area.

also even after init_memory_mapping, we still need use early_ioremap to access
it because user could use memmap to skip it.

PS: this problem have nothing to do with mapping that is set by bootloader.
arch/x86/kernel/head_64.c will have it own mapping, and only cover [0,1g).

Yinghai
Anyway, we need to deal with it, see below.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/11/2012 03:53 PM, Yinghai Lu wrote:
On Tue, Dec 11, 2012 at 9:38 AM, H. Peter Anvin [off-list ref] wrote:
quoted
On 12/11/2012 09:15 AM, Yinghai Lu wrote:
quoted

No, that is not right place. initrd could be loaded anywhere like way
high by bootloader.
Only *after* your changes... the current protocol doesn't allow that.
before for-x86-boot change,

bootloader put ramdisk just under 2g...

[    0.000000] memblock_reserve: [0x7d9dc000-0x7fffefff] RAMDISK

and arch/x86/kernel/head_64.S only set ident mapping for [0,1g)

so before init_memory_mapping, we need to use early_ioremap to access
ramdisk area.

also even after init_memory_mapping, we still need use early_ioremap to access
it because user could use memmap to skip it.

PS: this problem have nothing to do with mapping that is set by bootloader.
arch/x86/kernel/head_64.c will have it own mapping, and only cover [0,1g).
Well, we could invoke it on the bootloader page tables, but as you say
it may not be a good idea... depending on how much memory we may be
talking about.  One solution -- which I have to admit is starting to
sound really good -- is to set up a #PF handler which cycles through a
set of page tables and creates a "virtual identity map"... it does have
the advantage of making the entire physical address space available
without any additional funnies.

	-hpa

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Tue, Dec 11, 2012 at 3:57 PM, H. Peter Anvin [off-list ref] wrote:
Well, we could invoke it on the bootloader page tables, but as you say
it may not be a good idea... depending on how much memory we may be
talking about.  One solution -- which I have to admit is starting to
sound really good -- is to set up a #PF handler which cycles through a
set of page tables and creates a "virtual identity map"... it does have
the advantage of making the entire physical address space available
without any additional funnies.
so that #PF handler will work before
arch/x86/kernel/setup.c::setup_arch/early_trap_init

early_strap_intit will install another handler there for #PF

for 64bit, moving early_ioremap_init ahead is very simple, like attach patch

but for 32 bit looks like it is not that easy.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/11/2012 04:27 PM, Yinghai Lu wrote:
On Tue, Dec 11, 2012 at 3:57 PM, H. Peter Anvin [off-list ref] wrote:
quoted
Well, we could invoke it on the bootloader page tables, but as you say
it may not be a good idea... depending on how much memory we may be
talking about.  One solution -- which I have to admit is starting to
sound really good -- is to set up a #PF handler which cycles through a
set of page tables and creates a "virtual identity map"... it does have
the advantage of making the entire physical address space available
without any additional funnies.
so that #PF handler will work before
arch/x86/kernel/setup.c::setup_arch/early_trap_init

early_strap_intit will install another handler there for #PF

for 64bit, moving early_ioremap_init ahead is very simple, like attach patch

but for 32 bit looks like it is not that easy.
For 32 bits, we don't need it, because we can just run this part in
linear mode.  It also doesn't help us on 32 bits since we are limited by
virtual address space anyway.

	-hpa

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/11/2012 04:27 PM, Yinghai Lu wrote:
On Tue, Dec 11, 2012 at 3:57 PM, H. Peter Anvin [off-list ref] wrote:
quoted
Well, we could invoke it on the bootloader page tables, but as you say
it may not be a good idea... depending on how much memory we may be
talking about.  One solution -- which I have to admit is starting to
sound really good -- is to set up a #PF handler which cycles through a
set of page tables and creates a "virtual identity map"... it does have
the advantage of making the entire physical address space available
without any additional funnies.
so that #PF handler will work before
arch/x86/kernel/setup.c::setup_arch/early_trap_init

early_strap_intit will install another handler there for #PF

for 64bit, moving early_ioremap_init ahead is very simple, like attach patch

but for 32 bit looks like it is not that easy.
Here is an incomplete patch for illustration purposes only what I mean with an early-mapping #PF handler.  This creates a set of transient page tables on demand which allows us to access memory as if it was all mapped, but using only O(1) storage.  The replacement policy is trivial: if we run out, we start over from scratch.

The "identity page tables" used during the transition to high virtual addresses are kind of magic; there is a bunch of extra aliases created, but the way it is done guarantees that the range we actually cares about is mapped correctly.  The aliases don't matter and get scrubbed shortly thereafter anyway.

This should, obviously, be used on native only -- in particular Xen should instead rely on the initial page tables provided by the domain builder, which should map all physical memory.

Once the proper memory-map-aware page tables are built, we should turn this off by swapping to the newly built real init_level4_pgt instead.

	-hpa

-- 
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Tue, Dec 11, 2012 at 4:37 PM, H. Peter Anvin [off-list ref] wrote:
On 12/11/2012 04:27 PM, Yinghai Lu wrote:
quoted
On Tue, Dec 11, 2012 at 3:57 PM, H. Peter Anvin [off-list ref] wrote:
quoted
Well, we could invoke it on the bootloader page tables, but as you say
it may not be a good idea... depending on how much memory we may be
talking about.  One solution -- which I have to admit is starting to
sound really good -- is to set up a #PF handler which cycles through a
set of page tables and creates a "virtual identity map"... it does have
the advantage of making the entire physical address space available
without any additional funnies.
so that #PF handler will work before
arch/x86/kernel/setup.c::setup_arch/early_trap_init

early_strap_intit will install another handler there for #PF

for 64bit, moving early_ioremap_init ahead is very simple, like attach patch

but for 32 bit looks like it is not that easy.
For 32 bits, we don't need it, because we can just run this part in
linear mode.  It also doesn't help us on 32 bits since we are limited by
virtual address space anyway.
please check draft version for early_memremap version for microcode...

1. make find_cpio take map/unmap function pointer, and use that to set
sliding window.
2. clean the end to size in some function to fix -1 offset
3. update_mc_saved to change back to __va for ap etc and after
initrd_relocation.

NOT EVEN COMPILING TEST.

should use this one on top of early_ioremap_head64.patch that i sent
it out this afternoon.

Thanks

Yinghai

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Tue, Dec 11, 2012 at 11:14 PM, Yinghai Lu [off-list ref] wrote:
please check draft version for early_memremap version for microcode...

1. make find_cpio take map/unmap function pointer, and use that to set
sliding window.
2. clean the end to size in some function to fix -1 offset
3. update_mc_saved to change back to __va for ap etc and after
initrd_relocation.

should use this one on top of early_ioremap_head64.patch that i sent
it out this afternoon.
this one pass the test on 64bit without microcode cpio with initrd...

Fenghua, can you check it on 64 config with more than 4g RAM ?

on top of tip/x86/microcode and early_ioremap_head64.patch

Thanks

Yinghai

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Tue, Dec 11, 2012 at 10:57:03PM -0800, H. Peter Anvin wrote:
quoted hunk
@@ -372,46 +394,21 @@ ENTRY(name)
 	i = i + 1 ;					\
 	.endr
 
-	.data
-	/*
-	 * This default setting generates an ident mapping at address 0x100000
-	 * and a mapping for the kernel that precisely maps virtual address
-	 * 0xffffffff80000000 to physical address 0x000000. (always using
-	 * 2Mbyte large pages provided by PAE mode)
-	 */
-NEXT_PAGE(init_level4_pgt)
-	.quad	level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
We completely lost level3_ident_pgt, causing:

arch/x86/built-in.o: In function `setup_real_mode':
/home/boris/kernel/linux-2.6/arch/x86/realmode/init.c:81: undefined reference to `level3_ident_pgt'
make: *** [vmlinux] Error 1
quoted hunk
-	.org	init_level4_pgt + L4_PAGE_OFFSET*8, 0
-	.quad	level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
-	.org	init_level4_pgt + L4_START_KERNEL*8, 0
-	/* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
+	__INITDATA
+NEXT_PAGE(early_level4_pgt)
+	.fill	511,8,0
 	.quad	level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE
 
-NEXT_PAGE(level3_ident_pgt)
-	.quad	level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
-	.fill	511,8,0
+NEXT_PAGE(early_dynamic_pgts)
+	.fill	512*EARLY_DYNAMIC_PAGE_TABLES,8,0
 
+	.data
 NEXT_PAGE(level3_kernel_pgt)
 	.fill	L3_START_KERNEL,8,0
 	/* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */
 	.quad	level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE
 	.quad	level2_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE
 
-NEXT_PAGE(level2_fixmap_pgt)
-	.fill	506,8,0
-	.quad	level1_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE
-	/* 8MB reserved for vsyscalls + a 2MB hole = 4 + 1 entries */
-	.fill	5,8,0
-
-NEXT_PAGE(level1_fixmap_pgt)
-	.fill	512,8,0
You still need that NEXT_PAGE(level1_fixmap_pgt) thing:

arch/x86/kernel/head_64.o: In function `level2_fixmap_pgt':
(.data+0x2fd0): undefined reference to `level1_fixmap_pgt'

-- 
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/12/2012 05:38 AM, Borislav Petkov wrote:
We completely lost level3_ident_pgt, causing:

arch/x86/built-in.o: In function `setup_real_mode':
/home/boris/kernel/linux-2.6/arch/x86/realmode/init.c:81: undefined reference to `level3_ident_pgt'
make: *** [vmlinux] Error 1
You still need that NEXT_PAGE(level1_fixmap_pgt) thing:

arch/x86/kernel/head_64.o: In function `level2_fixmap_pgt':
(.data+0x2fd0): undefined reference to `level1_fixmap_pgt'
Yes, I said it wasn't a complete patch.  There are bits missing, and some of them need restructuring.  The ident page table in the trampoline should be handled by mirroring the kernel ident page tables instead, for example -- right now that is completely broken if the kernel lives above the 512 GiB mark.

	-hpa

-- 
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
please check  early_memremap version for microcode...

1. make find_cpio take map/unmap function pointer, and use that to set
sliding window.
2. clean the end to size in some function to fix -1 offset
3. update_mc_saved to change back to __va for ap etc and after
initrd_relocation.

should use this one on top of early_ioremap_head64.patch that i sent
it out this afternoon.
v3...

should split it into to three or four.

Thanks

Yinghai

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
Here is a version that compiles.  It doesn't *boot* yet, because the switchover from dynamic mode to the real pagetables doesn't happen right and so we end up on an uninitialized set of page tables.

The new page table setup in tip:x86/mm2 should make that easier to achieve, however... I won't have time to test this out tonight, though.

	-hpa

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/12/2012 09:12 PM, H. Peter Anvin wrote:
Here is a version that compiles.  It doesn't *boot* yet, because the
switchover from dynamic mode to the real pagetables doesn't happen right
and so we end up on an uninitialized set of page tables.

The new page table setup in tip:x86/mm2 should make that easier to
achieve, however... I won't have time to test this out tonight, though.

     -hpa
Well, minus a simple brainfart now it actually gets into the page table setup.

	-hpa


-- 
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Wed, Dec 12, 2012 at 9:26 PM, H. Peter Anvin [off-list ref] wrote:
quoted
The new page table setup in tip:x86/mm2 should make that easier to
achieve, however... I won't have time to test this out tonight, though.

     -hpa

Well, minus a simple brainfart now it actually gets into the page table
setup.
of init_mem_mapping in setup_arch?

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/12/2012 11:01 PM, Yinghai Lu wrote:
On Wed, Dec 12, 2012 at 9:26 PM, H. Peter Anvin [off-list ref] wrote:
quoted
quoted
The new page table setup in tip:x86/mm2 should make that easier to
achieve, however... I won't have time to test this out tonight, though.

     -hpa

Well, minus a simple brainfart now it actually gets into the page table
setup.
of init_mem_mapping in setup_arch?
Probably, yes... it then gets confused which is not surprising since it
is expecting to do an incremental build instead of ground up.

	-hpa

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Wed, Dec 12, 2012 at 09:26:47PM -0800, H. Peter Anvin wrote:
On 12/12/2012 09:12 PM, H. Peter Anvin wrote:
quoted
Here is a version that compiles.  It doesn't *boot* yet, because the
switchover from dynamic mode to the real pagetables doesn't happen right
and so we end up on an uninitialized set of page tables.

The new page table setup in tip:x86/mm2 should make that easier to
achieve, however... I won't have time to test this out tonight, though.

    -hpa
Well, minus a simple brainfart now it actually gets into the page
table setup.
If by this you mean that you can only see

"Decompressing Linux...
Parsing ELF.."

and then the VM reboots (running it in KVM) then ok, I'm seeing this
here too.

Thanks.

-- 
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/13/2012 11:13 AM, Borislav Petkov wrote:
On Wed, Dec 12, 2012 at 09:26:47PM -0800, H. Peter Anvin wrote:
quoted
Well, minus a simple brainfart now it actually gets into the page
table setup.
If by this you mean that you can only see

"Decompressing Linux...
Parsing ELF.."

and then the VM reboots (running it in KVM) then ok, I'm seeing this
here too.
: tazenda 111 ; qemu-kvm -smp 2 -m 2048 -hda ~/qemu/fc10/qemu-fc10-64.img -serial stdio -kernel o.x86_64/arch/x86/boot/bzImage -append 'ro root=/dev/sda1 console=ttyS0 earlyprintk=serial,ttyS0 debug'
early console in setup code
early console in decompress_kernel

Decompressing Linux... Parsing ELF... done.
Booting the kernel.
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.7.0+ (hpa@tazenda.hos.anvin.org) (gcc version 4.7.2 20120921 (Red Hat 4.7.2-2) (GCC) ) #16 SMP Wed Dec 12 21:24:54 PST 2012
[    0.000000] Command line: ro root=/dev/sda1 console=ttyS0 earlyprintk=serial,ttyS0 debug
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000007fffdfff] usable
[    0.000000] BIOS-e820: [mem 0x000000007fffe000-0x000000007fffffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000feffc000-0x00000000feffffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved
[    0.000000] bootconsole [earlyser0] enabled
[    0.000000] NX (Execute Disable) protection: active
[    0.000000] DMI 2.4 present.
[    0.000000] DMI: Bochs Bochs, BIOS Bochs 01/01/2011
[    0.000000] e820: update [mem 0x00000000-0x0000ffff] usable ==> reserved
[    0.000000] e820: remove [mem 0x000a0000-0x000fffff] usable
[    0.000000] No AGP bridge found
[    0.000000] e820: last_pfn = 0x7fffe max_arch_pfn = 0x400000000
[    0.000000] MTRR default type: write-back
[    0.000000] MTRR fixed ranges enabled:
[    0.000000]   00000-9FFFF write-back
[    0.000000]   A0000-BFFFF uncachable
[    0.000000]   C0000-FFFFF write-protect
[    0.000000] MTRR variable ranges enabled:
[    0.000000]   0 base 00E0000000 mask FFE0000000 uncachable
[    0.000000]   1 disabled
[    0.000000]   2 disabled
[    0.000000]   3 disabled
[    0.000000]   4 disabled
[    0.000000]   5 disabled
[    0.000000]   6 disabled

24 more lines

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Thu, Dec 13, 2012 at 1:36 PM, H. Peter Anvin [off-list ref] wrote:
: tazenda 111 ; qemu-kvm -smp 2 -m 2048 -hda ~/qemu/fc10/qemu-fc10-64.img
-serial stdio -kernel o.x86_64/arch/x86/boot/bzImage -append 'ro
root=/dev/sda1 console=ttyS0 earlyprintk=serial,ttyS0 debug'
early console in setup code
early console in decompress_kernel

[    0.000000] init_memory_mapping: [mem 0x00000000-0x7fffdfff]
[    0.000000]  [mem 0x00000000-0x7fdfffff] page 2M
[    0.000000]  [mem 0x7fe00000-0x7fffdfff] page 4k
[    0.000000] Kernel panic - not syncing: Cannot find space for the kernel
page tables
[    0.000000] Pid: 0, comm: swapper Not tainted 3.7.0+ #16
[    0.000000] Call Trace:
[    0.000000]  [<ffffffff817f0d2e>] panic+0xb6/0x1b5
[    0.000000]  [<ffffffff817e3801>] init_memory_mapping+0x471/0x5a0
[    0.000000]  [<ffffffff81ecd37f>] setup_arch+0x65c/0xb71
[    0.000000]  [<ffffffff81ec998e>] start_kernel+0x8a/0x348
[    0.000000]  [<ffffffff81ec9452>] x86_64_start_reservations+0x132/0x136
[    0.000000]  [<ffffffff81ec94fe>] x86_64_start_kernel+0xa8/0xad
attached works on kvm local, but SMP does not work yet.

ON TOP of linus tree + tip:x86/mm2

I added mapping to kernel to init_level2_mapping with BRK before #PF
handler still works. (before early_trap_init)
copy entries into init_level2_page from early_level4_pgt....
split setup_real_mode to reserve and copy... copy need to after
init_mem_mapping for system with more than 512G ram.

my plan is using this one replace

      [PATCH v6 06/27] x86, 64bit: Set extra ident mapping for whole
kernel range

early console in setup code
Probing EDD (edd=off to disable)... ok
early console in decompress_kernel

Decompressing Linux... Parsing ELF... done.
Booting the kernel.
[    0.000000] BRK [0x03a9f000, 0x03a9ffff] PGTABLE
[    0.000000] BRK [0x03aa0000, 0x03aa0fff] PGTABLE
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.7.0-yh-07359-ge48ae89-dirty
(yhlu@linux-siqj.site) (gcc version 4.7.1 20120723 [gcc-4_7-branch
revision 189773] (SUSE Linux) ) #912 SMP Fri Dec 14 00:56:36 PST 2012
[    0.000000] Command line: BOOT_IMAGE=linux debug ignore_loglevel
initcall_debug pci=routeirq ramdisk_size=262144 root=/dev/ram0 rw
ip=dhcp console=uart8250,io,0x3f8,115200 initrd=initrd.img
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000dfffdfff] usable
[    0.000000] BIOS-e820: [mem 0x00000000dfffe000-0x00000000dfffffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000feffc000-0x00000000feffffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved

2678 more lines

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/14/2012 01:11 AM, Yinghai Lu wrote:
On Thu, Dec 13, 2012 at 1:36 PM, H. Peter Anvin [off-list ref] wrote:
quoted
: tazenda 111 ; qemu-kvm -smp 2 -m 2048 -hda ~/qemu/fc10/qemu-fc10-64.img
-serial stdio -kernel o.x86_64/arch/x86/boot/bzImage -append 'ro
root=/dev/sda1 console=ttyS0 earlyprintk=serial,ttyS0 debug'
early console in setup code
early console in decompress_kernel

[    0.000000] init_memory_mapping: [mem 0x00000000-0x7fffdfff]
[    0.000000]  [mem 0x00000000-0x7fdfffff] page 2M
[    0.000000]  [mem 0x7fe00000-0x7fffdfff] page 4k
[    0.000000] Kernel panic - not syncing: Cannot find space for the kernel
page tables
[    0.000000] Pid: 0, comm: swapper Not tainted 3.7.0+ #16
[    0.000000] Call Trace:
[    0.000000]  [<ffffffff817f0d2e>] panic+0xb6/0x1b5
[    0.000000]  [<ffffffff817e3801>] init_memory_mapping+0x471/0x5a0
[    0.000000]  [<ffffffff81ecd37f>] setup_arch+0x65c/0xb71
[    0.000000]  [<ffffffff81ec998e>] start_kernel+0x8a/0x348
[    0.000000]  [<ffffffff81ec9452>] x86_64_start_reservations+0x132/0x136
[    0.000000]  [<ffffffff81ec94fe>] x86_64_start_kernel+0xa8/0xad
attached works on kvm local, but SMP does not work yet.

ON TOP of linus tree + tip:x86/mm2

I added mapping to kernel to init_level2_mapping with BRK before #PF
handler still works. (before early_trap_init)
copy entries into init_level2_page from early_level4_pgt....
split setup_real_mode to reserve and copy... copy need to after
init_mem_mapping for system with more than 512G ram.

my plan is using this one replace

      [PATCH v6 06/27] x86, 64bit: Set extra ident mapping for whole
kernel range
That patch doesn't apply on top of the merge of x86/mm2 and
linus/master.  A trivial fixup is totally nonfunctional -- no
earlyprintk, just a null pointer death in setup_real_mode().

I suspect we don't need init_level4_pgt at all and should just plan to
get rid of it.  Is there any reason we can't just build the proper
kernel page table set in pagetable_init() and switch to it there?

	-hpa

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Fri, Dec 14, 2012 at 11:46 AM, H. Peter Anvin [off-list ref] wrote:
That patch doesn't apply on top of the merge of x86/mm2 and
linus/master.  A trivial fixup is totally nonfunctional -- no
earlyprintk, just a null pointer death in setup_real_mode().
just saw linus pulled efi fix that is touching real_mode/init.c.
and there is something wrong with it.

will check that...
I suspect we don't need init_level4_pgt at all and should just plan to
get rid of it.  Is there any reason we can't just build the proper
kernel page table set in pagetable_init() and switch to it there?
then how to pass the info to AP?

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Fri, Dec 14, 2012 at 12:04 PM, Yinghai Lu [off-list ref] wrote:
On Fri, Dec 14, 2012 at 11:46 AM, H. Peter Anvin [off-list ref] wrote:
quoted
I suspect we don't need init_level4_pgt at all and should just plan to
get rid of it.  Is there any reason we can't just build the proper
kernel page table set in pagetable_init() and switch to it there?
then how to pass the info to AP?
also we should merge early_level4_pgt with init_level4_pgt.

and #PE handler could just extend to use BRK ...

but need to make sure BRK get mapped at first, and BRK could cross the
1G, 512G boundary ...

that could make things less impact to all.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Fri, Dec 14, 2012 at 12:08 PM, Yinghai Lu [off-list ref] wrote:
On Fri, Dec 14, 2012 at 12:04 PM, Yinghai Lu [off-list ref] wrote:
quoted
On Fri, Dec 14, 2012 at 11:46 AM, H. Peter Anvin [off-list ref] wrote:
quoted
I suspect we don't need init_level4_pgt at all and should just plan to
get rid of it.  Is there any reason we can't just build the proper
kernel page table set in pagetable_init() and switch to it there?
then how to pass the info to AP?
also we should merge early_level4_pgt with init_level4_pgt.

and #PE handler could just extend to use BRK ...

but need to make sure BRK get mapped at first, and BRK could cross the
1G, 512G boundary ...

that could make things less impact to all.
your current switchover setup in arch/x86/kernel/head_64.S could
handle above 512g and cross boundary?

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
What info are you referring to?

Yinghai Lu [off-list ref] wrote:
On Fri, Dec 14, 2012 at 11:46 AM, H. Peter Anvin [off-list ref] wrote:
quoted
That patch doesn't apply on top of the merge of x86/mm2 and
linus/master.  A trivial fixup is totally nonfunctional -- no
earlyprintk, just a null pointer death in setup_real_mode().
just saw linus pulled efi fix that is touching real_mode/init.c.
and there is something wrong with it.

will check that...
quoted
I suspect we don't need init_level4_pgt at all and should just plan
to
quoted
get rid of it.  Is there any reason we can't just build the proper
kernel page table set in pagetable_init() and switch to it there?
then how to pass the info to AP?
-- 
Sent from my mobile phone. Please excuse brevity and lack of formatting.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/14/2012 12:14 PM, Yinghai Lu wrote:
On Fri, Dec 14, 2012 at 12:08 PM, Yinghai Lu [off-list ref] wrote:
quoted
On Fri, Dec 14, 2012 at 12:04 PM, Yinghai Lu [off-list ref] wrote:
quoted
On Fri, Dec 14, 2012 at 11:46 AM, H. Peter Anvin [off-list ref] wrote:
quoted
I suspect we don't need init_level4_pgt at all and should just plan to
get rid of it.  Is there any reason we can't just build the proper
kernel page table set in pagetable_init() and switch to it there?
then how to pass the info to AP?
also we should merge early_level4_pgt with init_level4_pgt.

and #PE handler could just extend to use BRK ...

but need to make sure BRK get mapped at first, and BRK could cross the
1G, 512G boundary ...

that could make things less impact to all.
your current switchover setup in arch/x86/kernel/head_64.S could
handle above 512g and cross boundary?
Yes, that's the purpose of the funny aliasing games I play there.

	-hpa

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/14/2012 12:17 PM, Yinghai Lu wrote:
On Fri, Dec 14, 2012 at 12:10 PM, H. Peter Anvin [off-list ref] wrote:
quoted
What info are you referring to?
pointer to init_level4_pgt page.
We point the trampoline at the proper page tables in swapper_pg_dir;
this means pushing realmode initialization (but not reservation!) after
pagetable_init(), but that should be fine I *believe*... the big
question is if we have something that wants to invoke EFI before then,
if so it could get tricky.

	-hpa

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Fri, Dec 14, 2012 at 12:04 PM, Yinghai Lu [off-list ref] wrote:
On Fri, Dec 14, 2012 at 11:46 AM, H. Peter Anvin [off-list ref] wrote:
quoted
That patch doesn't apply on top of the merge of x86/mm2 and
linus/master.  A trivial fixup is totally nonfunctional -- no
earlyprintk, just a null pointer death in setup_real_mode().
just saw linus pulled efi fix that is touching real_mode/init.c.
and there is something wrong with it.
one of change in efi pull make us can not call
kernel_pysical_mapping_init early...

please check attached...

also move init_mapping_kernel to setup.c instead to make lock init
warning go away.

again, it still has problem with SMP.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Fri, Dec 14, 2012 at 12:08 PM, Yinghai Lu [off-list ref] wrote:
On Fri, Dec 14, 2012 at 12:04 PM, Yinghai Lu [off-list ref] wrote:
quoted
On Fri, Dec 14, 2012 at 11:46 AM, H. Peter Anvin [off-list ref] wrote:
quoted
I suspect we don't need init_level4_pgt at all and should just plan to
get rid of it.  Is there any reason we can't just build the proper
kernel page table set in pagetable_init() and switch to it there?
then how to pass the info to AP?
also we should merge early_level4_pgt with init_level4_pgt.

and #PE handler could just extend to use BRK ...

but need to make sure BRK get mapped at first, and BRK could cross the
1G, 512G boundary ...

that could make things less impact to all.
I tailored your patch and made use 2M page increase to replace patch
ioremap function.

   [PATCH v6 12/27] x86: use io_remap to access real_mode_data

and it will extend init_level4_pgt to map extra range. that will limit
affect to even others.

please check if that is ok to you.

Thanks

Yinghai

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/14/2012 11:57 PM, Yinghai Lu wrote:
I tailored your patch and made use 2M page increase to replace patch
ioremap function.

   [PATCH v6 12/27] x86: use io_remap to access real_mode_data

and it will extend init_level4_pgt to map extra range. that will limit
affect to even others.

please check if that is ok to you.
What is the point of only managing 2M at a time?  Now you have to have
more conditionals and you don't get any more memory efficiency.

Filling arbitrarily into the brk is not acceptable... the brk is an O(1)
area and all brk allocations need to be reserved at compile time, so the
overflow handling is still necessary.

So no, this patch is not acceptable.

	-hpa

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On Sat, Dec 15, 2012 at 11:30 AM, H. Peter Anvin [off-list ref] wrote:
What is the point of only managing 2M at a time?  Now you have to have
more conditionals and you don't get any more memory efficiency.
We don't need to, because real_data is less than 2M, and ramdisk is about 16M.

Also if we set map too large, could have chance to cover mem hole near
1T for AMD HT system.
Filling arbitrarily into the brk is not acceptable... the brk is an O(1)
area and all brk allocations need to be reserved at compile time, so the
overflow handling is still necessary.
if run out of BRK, we will get panic, because early_make_pgtable will return -1.

and current BRK already have 64 slop space.

BTW, did you look at smp boot problem with early_level4_pgt version?

Yinghai

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
The mem hole at 1T should not be marked cachable in the MTRRs.

Yinghai Lu [off-list ref] wrote:
On Sat, Dec 15, 2012 at 11:30 AM, H. Peter Anvin [off-list ref]
wrote:
quoted
What is the point of only managing 2M at a time?  Now you have to
have
quoted
more conditionals and you don't get any more memory efficiency.
We don't need to, because real_data is less than 2M, and ramdisk is
about 16M.

Also if we set map too large, could have chance to cover mem hole near
1T for AMD HT system.
quoted
Filling arbitrarily into the brk is not acceptable... the brk is an
O(1)
quoted
area and all brk allocations need to be reserved at compile time, so
the
quoted
overflow handling is still necessary.
if run out of BRK, we will get panic, because early_make_pgtable will
return -1.

and current BRK already have 64 slop space.

BTW, did you look at smp boot problem with early_level4_pgt version?

Yinghai
-- 
Sent from my mobile phone. Please excuse brevity and lack of formatting.

Re: [tip:x86/microcode] x86/microcode_intel_early.c: Early update ucode on Intel's CPU

Message details and related content
On 12/15/2012 12:55 PM, Yinghai Lu wrote:
On Sat, Dec 15, 2012 at 11:30 AM, H. Peter Anvin [off-list ref] wrote:
quoted
What is the point of only managing 2M at a time?  Now you have to have
more conditionals and you don't get any more memory efficiency.
We don't need to, because real_data is less than 2M, and ramdisk is about 16M.
In other words, you make magic assumptions (some of which are very wrong in many real-life scenarios -- people can and do use gigabyte-plus initramfs).  That is exactly the wrong thing to do.  Furthermore it doesn't buy you anything, because you still have to allocate the PMDs.
Also if we set map too large, could have chance to cover mem hole near
1T for AMD HT system.
Again, should not be cachable in the MTRRs, and even so, is 1G aligned already.
quoted
Filling arbitrarily into the brk is not acceptable... the brk is an O(1)
area and all brk allocations need to be reserved at compile time, so the
overflow handling is still necessary.
if run out of BRK, we will get panic, because early_make_pgtable will return -1.
And you consider that panic an acceptable failure mode????
and current BRK already have 64 slop space.

BTW, did you look at smp boot problem with early_level4_pgt version?
No, I have been busy with non-Linux stuff today.

	-hpa

-- 
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help