Skip to contentSkip to conversation

x86/efi: Identity mapping pagetable

PATCH In review Matt Fleming 2 participants 5 messages Oct 5, 2012

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

v2 cover x86/efi: Identity mapping pagetable

From: Matt Fleming <[email protected]>

This series upgrades real_mode_header->trampoline_pgd to a proper kernel pagetable instead of just mapping the kernel text and module space. It also inserts the physical mappings for anything we ioremap(), so I/O regions are always accessible via their physical addresses whenever this pagetable is loaded, making it a true identity mapping.

These changes make it suitable for loading when calling virtual EFI runtime functions on x86-64. The main benefit of this change is to fix the ASUS firmware bug documented here,

	 https://lkml.org/lkml/2012/8/7/108

but having our own EFI pagetable makes sense anyway. The memory map code is easily the most complicatd part of the EFI infrastructure, and it's likely that there will be other funky stuff we have to do with our memory map as more and more machines ship with various implementations of EFI firmware.

Note that we only switch to the identity pagetable for x86-64. I'm unaware of any bugs like the above on 32-bit EFI platforms, but we can easily adopt the 64-bit scheme if one is discovered.

v2: Based on feedback from Jan Beulich delete the 32-bit changes in [PATCH 1/3], they were likely to create more problems than they would solve and the identity I/O mappings are unused on 32-bit anyway.

Matt Fleming (2):
x86, mm: Include the entire kernel memory map in trampoline_pgd x86, efi: 1:1 pagetable mapping for virtual EFI calls

Xiaoyan Zhang (1):
x86/kernel: remove tboot 1:1 page table creation code

 arch/x86/include/asm/efi.h     |  28 ++++++++---
 arch/x86/kernel/tboot.c        |  78 ++----------------------------
 arch/x86/mm/init_64.c          |   9 +++-
 arch/x86/mm/ioremap.c          | 105 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/platform/efi/efi_64.c |  15 ++++++
 arch/x86/realmode/init.c       |  17 ++++++-
 6 files changed, 169 insertions(+), 83 deletions(-)
Signature · 1 line
1.7.11.4
#1 lore raw

PATCH v2 1/3 x86, mm: Include the entire kernel memory map in trampoline_pgd

From: Matt Fleming <[email protected]>

There are various pieces of code in arch/x86 that require a page table with an identity mapping. Make trampoline_pgd a proper kernel page table, it currently only includes the kernel text and module space mapping.

One new feature of trampoline_pgd is that it now has mappings for the physical I/O device addresses, which are inserted at ioremap() time. Some broken implementations of EFI firmware require these mappings to always be around.

---

v2: Remove the 32-bit code for insert_identitity_mapping(). It's unlikely that we'll successfully be able to map physical I/O addresses because they'll probably be above PAGE_OFFSET, e.g. on machines with multi-gigabytes of RAM. Since there's no in-kernel user of these mappings on x86 (unlike on x86-64 where we work around the ASUS firmware bug) just delete the code.

Also, add a check in insert_identity_mapping() to ensure we don't try to insert any mappings above the x86-64 memory map guard hole.

 arch/x86/mm/init_64.c    |   9 +++-
 arch/x86/mm/ioremap.c    | 105 +++++++++++++++++++++++++++++++++++++++++++++++
 arch/x86/realmode/init.c |  17 +++++++-
 3 files changed, 128 insertions(+), 3 deletions(-)
arch/x86/mm/init_64.c +8 -1
index 2b6b4a3..fd4404f 100644
@@ -108,13 +108,13 @@ void sync_global_pgds(unsigned long start, unsigned long end)
 	for (address = start; address <= end; address += PGDIR_SIZE) {
 		const pgd_t *pgd_ref = pgd_offset_k(address);
 		struct page *page;
+		pgd_t *pgd;
 
 		if (pgd_none(*pgd_ref))
 			continue;
 
 		spin_lock(&pgd_lock);
 		list_for_each_entry(page, &pgd_list, lru) {
-			pgd_t *pgd;
 			spinlock_t *pgt_lock;
 
 			pgd = (pgd_t *)page_address(page) + pgd_index(address);
@@ -130,6 +130,13 @@ void sync_global_pgds(unsigned long start, unsigned long end)
 
 			spin_unlock(pgt_lock);
 		}
+
+		pgd = __va(real_mode_header->trampoline_pgd);
+		pgd += pgd_index(address);
+
+		if (pgd_none(*pgd))
+			set_pgd(pgd, *pgd_ref);
+
 		spin_unlock(&pgd_lock);
 	}
 }
arch/x86/mm/ioremap.c +105 -0

117 lines not on this page · raw message

arch/x86/realmode/init.c +15 -2
index cbca565..8e6ab61 100644
@@ -78,8 +78,21 @@ void __init setup_real_mode(void)
 	*trampoline_cr4_features = read_cr4();
 
 	trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);
-	trampoline_pgd[0] = __pa(level3_ident_pgt) + _KERNPG_TABLE;
-	trampoline_pgd[511] = __pa(level3_kernel_pgt) + _KERNPG_TABLE;
+
+	/*
+	 * Create an identity mapping for all of physical memory.
+	 */
+	for (i = 0; i <= pgd_index(max_pfn << PAGE_SHIFT); i++) {
+		int index = pgd_index(PAGE_OFFSET) + i;
+
+		trampoline_pgd[i] = (u64)pgd_val(swapper_pg_dir[index]);
+	}
+
+	/*
+	 * Copy the upper-half of the kernel pages tables.
+	 */
+	for (i = pgd_index(PAGE_OFFSET); i < PTRS_PER_PGD; i++)
+		trampoline_pgd[i] = (u64)pgd_val(swapper_pg_dir[i]);
 #endif
 }
 
Signature · 1 line
1.7.11.4
#2 lore raw

PATCH v2 2/3 x86, efi: 1:1 pagetable mapping for virtual EFI calls

In reply to Matt Fleming

From: Matt Fleming <[email protected]>

Some firmware still needs a 1:1 (virt->phys) mapping even after we've called SetVirtualAddressMap(). So install the mapping alongside our existing kernel mapping whenever we make EFI calls in virtual mode.

This bug was discovered on ASUS machines where the firmware implementation of GetTime() accesses the RTC device via physical addresses, even though that's bogus per the UEFI spec since we've informed the firmware via SetVirtualAddressMap() that the boottime memory map is no longer valid.

This bug seems to be present in a lot of consumer devices, so there's not a lot we can do about this spec violation apart from workaround it.

Cc JérômeCarretero <[email protected]>
Cc Vasco Dias <[email protected]>
---
 arch/x86/include/asm/efi.h     | 28 +++++++++++++++++++++-------
 arch/x86/platform/efi/efi_64.c | 15 +++++++++++++++
 2 files changed, 36 insertions(+), 7 deletions(-)
arch/x86/include/asm/efi.h +21 -7
index c9dcc18..ae3bf3b 100644
@@ -69,23 +69,37 @@ extern u64 efi_call6(void *fp, u64 arg1, u64 arg2, u64 arg3,
 	efi_call6((void *)(f), (u64)(a1), (u64)(a2), (u64)(a3),		\
 		  (u64)(a4), (u64)(a5), (u64)(a6))
 
+extern unsigned long efi_call_virt_prelog(void);
+extern void efi_call_virt_epilog(unsigned long);
+
+#define efi_callx(x, func, ...)					\
+	({							\
+		efi_status_t __status;				\
+		unsigned long __pgd;				\
+								\
+		__pgd = efi_call_virt_prelog();			\
+		__status = efi_call##x(func, __VA_ARGS__);	\
+		efi_call_virt_epilog(__pgd);			\
+		__status;					\
+	})
+
 #define efi_call_virt0(f)				\
-	efi_call0((void *)(efi.systab->runtime->f))
+	efi_callx(0, (void *)(efi.systab->runtime->f))
 #define efi_call_virt1(f, a1)					\
-	efi_call1((void *)(efi.systab->runtime->f), (u64)(a1))
+	efi_callx(1, (void *)(efi.systab->runtime->f), (u64)(a1))
 #define efi_call_virt2(f, a1, a2)					\
-	efi_call2((void *)(efi.systab->runtime->f), (u64)(a1), (u64)(a2))
+	efi_callx(2, (void *)(efi.systab->runtime->f), (u64)(a1), (u64)(a2))
 #define efi_call_virt3(f, a1, a2, a3)					\
-	efi_call3((void *)(efi.systab->runtime->f), (u64)(a1), (u64)(a2), \
+	efi_callx(3, (void *)(efi.systab->runtime->f), (u64)(a1), (u64)(a2), \
 		  (u64)(a3))
 #define efi_call_virt4(f, a1, a2, a3, a4)				\
-	efi_call4((void *)(efi.systab->runtime->f), (u64)(a1), (u64)(a2), \
+	efi_callx(4, (void *)(efi.systab->runtime->f), (u64)(a1), (u64)(a2), \
 		  (u64)(a3), (u64)(a4))
 #define efi_call_virt5(f, a1, a2, a3, a4, a5)				\
-	efi_call5((void *)(efi.systab->runtime->f), (u64)(a1), (u64)(a2), \
+	efi_callx(5, (void *)(efi.systab->runtime->f), (u64)(a1), (u64)(a2), \
 		  (u64)(a3), (u64)(a4), (u64)(a5))
 #define efi_call_virt6(f, a1, a2, a3, a4, a5, a6)			\
-	efi_call6((void *)(efi.systab->runtime->f), (u64)(a1), (u64)(a2), \
+	efi_callx(6, (void *)(efi.systab->runtime->f), (u64)(a1), (u64)(a2), \
 		  (u64)(a3), (u64)(a4), (u64)(a5), (u64)(a6))
 
 extern void __iomem *efi_ioremap(unsigned long addr, unsigned long size,
arch/x86/platform/efi/efi_64.c +15 -0
index ac3aa54..ddb0174 100644
@@ -58,6 +58,21 @@ static void __init early_code_mapping_set_exec(int executable)
 	}
 }
 
+unsigned long efi_call_virt_prelog(void)
+{
+	unsigned long saved;
+
+	saved = read_cr3();
+	write_cr3(real_mode_header->trampoline_pgd);
+
+	return saved;
+}
+
+void efi_call_virt_epilog(unsigned long saved)
+{
+	write_cr3(saved);
+}
+
 void __init efi_call_phys_prelog(void)
 {
 	unsigned long vaddress;
Signature · 1 line
1.7.11.4
#3 lore raw

PATCH v2 3/3 x86/kernel: remove tboot 1:1 page table creation code

In reply to Matt Fleming

From: Xiaoyan Zhang <[email protected]>

For TXT boot, while Linux kernel trys to shutdown/S3/S4/reboot, it need to jump back to tboot code and do TXT teardown work. Previously kernel zapped all mem page identity mapping (va=pa) after booting, so tboot code mem address was mapped again with identity mapping. Now kernel didn't zap the identity mapping page table, so tboot related code can remove the remapping code before trapping back now.

---
 arch/x86/kernel/tboot.c | 78 ++++---------------------------------------------
 1 file changed, 5 insertions(+), 73 deletions(-)
arch/x86/kernel/tboot.c +5 -73
index f84fe00..d4f460f 100644
@@ -103,71 +103,13 @@ void __init tboot_probe(void)
 	pr_debug("tboot_size: 0x%x\n", tboot->tboot_size);
 }
 
-static pgd_t *tboot_pg_dir;
-static struct mm_struct tboot_mm = {
-	.mm_rb          = RB_ROOT,
-	.pgd            = swapper_pg_dir,
-	.mm_users       = ATOMIC_INIT(2),
-	.mm_count       = ATOMIC_INIT(1),
-	.mmap_sem       = __RWSEM_INITIALIZER(init_mm.mmap_sem),
-	.page_table_lock =  __SPIN_LOCK_UNLOCKED(init_mm.page_table_lock),
-	.mmlist         = LIST_HEAD_INIT(init_mm.mmlist),
-};
-
 static inline void switch_to_tboot_pt(void)
 {
-	write_cr3(virt_to_phys(tboot_pg_dir));
-}
-
-static int map_tboot_page(unsigned long vaddr, unsigned long pfn,
-			  pgprot_t prot)
-{
-	pgd_t *pgd;
-	pud_t *pud;
-	pmd_t *pmd;
-	pte_t *pte;
-
-	pgd = pgd_offset(&tboot_mm, vaddr);
-	pud = pud_alloc(&tboot_mm, pgd, vaddr);
-	if (!pud)
-		return -1;
-	pmd = pmd_alloc(&tboot_mm, pud, vaddr);
-	if (!pmd)
-		return -1;
-	pte = pte_alloc_map(&tboot_mm, NULL, pmd, vaddr);
-	if (!pte)
-		return -1;
-	set_pte_at(&tboot_mm, vaddr, pte, pfn_pte(pfn, prot));
-	pte_unmap(pte);
-	return 0;
-}
-
-static int map_tboot_pages(unsigned long vaddr, unsigned long start_pfn,
-			   unsigned long nr)
-{
-	/* Reuse the original kernel mapping */
-	tboot_pg_dir = pgd_alloc(&tboot_mm);
-	if (!tboot_pg_dir)
-		return -1;
-
-	for (; nr > 0; nr--, vaddr += PAGE_SIZE, start_pfn++) {
-		if (map_tboot_page(vaddr, start_pfn, PAGE_KERNEL_EXEC))
-			return -1;
-	}
-
-	return 0;
-}
-
-static void tboot_create_trampoline(void)
-{
-	u32 map_base, map_size;
-
-	/* Create identity map for tboot shutdown code. */
-	map_base = PFN_DOWN(tboot->tboot_base);
-	map_size = PFN_UP(tboot->tboot_size);
-	if (map_tboot_pages(map_base << PAGE_SHIFT, map_base, map_size))
-		panic("tboot: Error mapping tboot pages (mfns) @ 0x%x, 0x%x\n",
-		      map_base, map_size);
+#ifdef CONFIG_X86_32
+	load_cr3(initial_page_table);
+#else
+	write_cr3(real_mode_header->trampoline_pgd);
+#endif
 }
 
 #ifdef CONFIG_ACPI_SLEEP
@@ -225,14 +167,6 @@ void tboot_shutdown(u32 shutdown_type)
 	if (!tboot_enabled())
 		return;
 
-	/*
-	 * if we're being called before the 1:1 mapping is set up then just
-	 * return and let the normal shutdown happen; this should only be
-	 * due to very early panic()
-	 */
-	if (!tboot_pg_dir)
-		return;
-
 	/* if this is S3 then set regions to MAC */
 	if (shutdown_type == TB_SHUTDOWN_S3)
 		if (tboot_setup_sleep())
@@ -343,8 +277,6 @@ static __init int tboot_late_init(void)
 	if (!tboot_enabled())
 		return 0;
 
-	tboot_create_trampoline();
-
 	atomic_set(&ap_wfs_count, 0);
 	register_hotcpu_notifier(&tboot_cpu_notifier);
 
Signature · 1 line
1.7.11.4
Acked-by on the v2 series
#4 lore raw
In reply to Matt Fleming
Quoted 26 lines
Quoted 1 line
Quoted 1 line · Matt Fleming

From: Matt Fleming <[email protected]>

This series upgrades real_mode_header->trampoline_pgd to a proper kernel pagetable instead of just mapping the kernel text and module space. It also inserts the physical mappings for anything we ioremap(), so I/O regions are always accessible via their physical addresses whenever this pagetable is loaded, making it a true identity mapping.

These changes make it suitable for loading when calling virtual EFI runtime functions on x86-64. The main benefit of this change is to fix the ASUS firmware bug documented here,

	 https://lkml.org/lkml/2012/8/7/108 

but having our own EFI pagetable makes sense anyway. The memory map code is easily the most complicatd part of the EFI infrastructure, and it's likely that there will be other funky stuff we have to do with our memory map as more and more machines ship with various implementations of EFI firmware.

Note that we only switch to the identity pagetable for x86-64. I'm unaware of any bugs like the above on 32-bit EFI platforms, but we can easily adopt the 64-bit scheme if one is discovered.

Quoted 7 lines

v2: Based on feedback from Jan Beulich delete the 32-bit changes in [PATCH 1/3], they were likely to create more problems than they would solve and the identity I/O mappings are unused on 32-bit anyway.

Matt Fleming (2):
x86, mm: Include the entire kernel memory map in trampoline_pgd x86, efi: 1:1 pagetable mapping for virtual EFI calls

(I know too little about the tboot code to meaningfully ack that one too.)

Quoted 13 lines

Xiaoyan Zhang (1):
x86/kernel: remove tboot 1:1 page table creation code

 arch/x86/include/asm/efi.h     |  28 ++++++++---
 arch/x86/kernel/tboot.c        |  78 ++----------------------------
 arch/x86/mm/init_64.c          |   9 +++-
 arch/x86/mm/ioremap.c          | 105 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/platform/efi/efi_64.c |  15 ++++++
 arch/x86/realmode/init.c       |  17 ++++++-
 6 files changed, 169 insertions(+), 83 deletions(-)
Signature · 1 line
1.7.11.4