[PATCH v3 24/75] x86/boot/compressed/64: Unmap GHCB page before booting the kernel

Borislav Petkov
Wed, 13 May 2020 11:13:40 -0000

On Tue, Apr 28, 2020 at 05:16:34PM +0200, Joerg Roedel
wrote:
> @@ -302,9 +313,13 @@ void do_boot_page_fault(struct pt_regs *regs, unsigned
long error_code)
>  	 *	- User faults
>  	 *	- Reserved bits set
>  	 */
> -	if (error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD)) {
> +	if (ghcb_fault ||
> +	    error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD)) {
>  		/* Print some information for debugging */
> -		error_putstr("Unexpected page-fault:");
> +		if (ghcb_fault)
> +			error_putstr("Page-fault on GHCB page:");
> +		else
> +			error_putstr("Unexpected page-fault:");

You could carve out the info dumping into a separate function to
unclutter this if-statement (diff ontop):

diff --git a/arch/x86/boot/compressed/ident_map_64.c
b/arch/x86/boot/compressed/ident_map_64.c
index d3771d455249..c1979fc0f853 100644
--- a/arch/x86/boot/compressed/ident_map_64.c
+++ b/arch/x86/boot/compressed/ident_map_64.c
@@ -296,6 +296,22 @@ int set_page_non_present(unsigned long address)
 	return set_clr_page_flags(&mapping_info, address, 0, _PAGE_PRESENT);
 }

+static void do_pf_error(const char *msg, unsigned long error_code,
+			unsigned long address, unsigned long ip)
+{
+	error_putstr(msg);
+
+	error_putstr("\nError Code: ");
+	error_puthex(error_code);
+	error_putstr("\nCR2: 0x");
+	error_puthex(address);
+	error_putstr("\nRIP relative to _head: 0x");
+	error_puthex(ip - (unsigned long)_head);
+	error_putstr("\n");
+
+	error("Stopping.\n");
+}
+
 void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code)
 {
 	unsigned long address = native_read_cr2();
@@ -309,27 +325,15 @@ void do_boot_page_fault(struct pt_regs *regs, unsigned
long error_code)

 	/*
 	 * Check for unexpected error codes. Unexpected are:
+	 *	- Faults on the GHCB page due to unexpected #VCs
 	 *	- Faults on present pages
 	 *	- User faults
 	 *	- Reserved bits set
 	 */
-	if (ghcb_fault ||
-	    error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD)) {
-		/* Print some information for debugging */
-		if (ghcb_fault)
-			error_putstr("Page-fault on GHCB page:");
-		else
-			error_putstr("Unexpected page-fault:");
-		error_putstr("\nError Code: ");
-		error_puthex(error_code);
-		error_putstr("\nCR2: 0x");
-		error_puthex(address);
-		error_putstr("\nRIP relative to _head: 0x");
-		error_puthex(regs->ip - (unsigned long)_head);
-		error_putstr("\n");
-
-		error("Stopping.\n");
-	}
+	if (ghcb_fault)
+		do_pf_error("Page-fault on GHCB page:", error_code, address,
regs->ip);
+	else if (error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD))
+		do_pf_error("Unexpected page-fault:", error_code, address,
regs->ip);

 	/*
 	 * Error code is sane - now identity map the 2M region around

--
Regards/Gruss,
    Boris.