Subject: [PATCH] x86, realmode: Separate real_mode reserve and setup

setup should be after init_mem_mapping(), otherwise will not have
ident mapping copied correctly when system have more than 512G RAM
from init_level4_pgt.

after that we could only backup pgd change to trampoline_pgd for
hot add memory only.

So we could call kernel_physical_memory_mapping early again.

Signed-off-by: Yinghai Lu <yinghai@kernel.org>

---
 arch/x86/include/asm/realmode.h |    3 ++-
 arch/x86/kernel/setup.c         |    4 +++-
 arch/x86/mm/init_64.c           |   11 +++++++----
 arch/x86/realmode/init.c        |   30 +++++++++++++++++++-----------
 4 files changed, 31 insertions(+), 17 deletions(-)

Index: linux-2.6/arch/x86/kernel/setup.c
===================================================================
--- linux-2.6.orig/arch/x86/kernel/setup.c
+++ linux-2.6/arch/x86/kernel/setup.c
@@ -913,10 +913,12 @@ void __init setup_arch(char **cmdline_p)
 	printk(KERN_DEBUG "initial memory mapped: [mem 0x00000000-%#010lx]\n",
 			(max_pfn_mapped<<PAGE_SHIFT) - 1);
 
-	setup_real_mode();
+	reserve_real_mode();
 
 	init_mem_mapping();
 
+	setup_real_mode();
+
 	memblock.current_limit = get_max_mapped();
 	dma_contiguous_reserve(0);
 
Index: linux-2.6/arch/x86/realmode/init.c
===================================================================
--- linux-2.6.orig/arch/x86/realmode/init.c
+++ linux-2.6/arch/x86/realmode/init.c
@@ -8,9 +8,26 @@
 struct real_mode_header *real_mode_header;
 u32 *trampoline_cr4_features;
 
-void __init setup_real_mode(void)
+void __init reserve_real_mode(void)
 {
 	phys_addr_t mem;
+	unsigned char *base;
+	size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
+
+	/* Has to be in very low memory so we can execute real-mode AP code. */
+	mem = memblock_find_in_range(0, 1<<20, size, PAGE_SIZE);
+	if (!mem)
+		panic("Cannot allocate trampoline\n");
+
+	base = __va(mem);
+	memblock_reserve(mem, size);
+	real_mode_header = (struct real_mode_header *) base;
+	printk(KERN_DEBUG "Base memory trampoline at [%p] %llx size %zu\n",
+	       base, (unsigned long long)mem, size);
+}
+
+void __init setup_real_mode(void)
+{
 	u16 real_mode_seg;
 	u32 *rel;
 	u32 count;
@@ -25,16 +42,7 @@ void __init setup_real_mode(void)
 	u64 efer;
 #endif
 
-	/* Has to be in very low memory so we can execute real-mode AP code. */
-	mem = memblock_find_in_range(0, 1<<20, size, PAGE_SIZE);
-	if (!mem)
-		panic("Cannot allocate trampoline\n");
-
-	base = __va(mem);
-	memblock_reserve(mem, size);
-	real_mode_header = (struct real_mode_header *) base;
-	printk(KERN_DEBUG "Base memory trampoline at [%p] %llx size %zu\n",
-	       base, (unsigned long long)mem, size);
+	base = (unsigned char *)real_mode_header;
 
 	memcpy(base, real_mode_blob, size);
 
Index: linux-2.6/arch/x86/include/asm/realmode.h
===================================================================
--- linux-2.6.orig/arch/x86/include/asm/realmode.h
+++ linux-2.6/arch/x86/include/asm/realmode.h
@@ -58,6 +58,7 @@ extern unsigned char boot_gdt[];
 extern unsigned char secondary_startup_64[];
 #endif
 
-extern void __init setup_real_mode(void);
+void reserve_real_mode(void);
+void setup_real_mode(void);
 
 #endif /* _ARCH_X86_REALMODE_H */
Index: linux-2.6/arch/x86/mm/init_64.c
===================================================================
--- linux-2.6.orig/arch/x86/mm/init_64.c
+++ linux-2.6/arch/x86/mm/init_64.c
@@ -133,11 +133,14 @@ void sync_global_pgds(unsigned long star
 			spin_unlock(pgt_lock);
 		}
 
-		pgd = __va(real_mode_header->trampoline_pgd);
-		pgd += pgd_index(address);
+		/* for hot add memory only */
+		if (after_bootmem) {
+			pgd = __va(real_mode_header->trampoline_pgd);
+			pgd += pgd_index(address);
 
-		if (pgd_none(*pgd))
-			set_pgd(pgd, *pgd_ref);
+			if (pgd_none(*pgd))
+				set_pgd(pgd, *pgd_ref);
+		}
 
 		spin_unlock(&pgd_lock);
 	}
