Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp4270085ybz; Tue, 28 Apr 2020 08:29:55 -0700 (PDT) X-Google-Smtp-Source: APiQypK1lvpDT3wtcPmuFqzLV3adR44Ew4yB6xF2R2uHeGIKMptpdsbaWV/B2MOhVp8fAxlXlDL+ X-Received: by 2002:a17:907:2049:: with SMTP id pg9mr25772501ejb.248.1588087795695; Tue, 28 Apr 2020 08:29:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588087795; cv=none; d=google.com; s=arc-20160816; b=Fx1Uwi3bF0I+tSZUpPyeW0WMN+FUD0sNbI7dA0T6I6oUKV16hepn80yzAobUT2rijY WzfEDc/hqUOxxetcH6YzhtKVFP+xFa/KZyFbI7PuWnu8XZpGQx2zF78RLM5ZSXogoto3 jrbrYOnGv3hT78nxWj4bmJGWYVh3EIq94u3+QDZ7TewIjqVBKKJk4gUqSmlch6uvr3+Y ZhihpwsrWQJ7Ia5mOSj9QhFlubpaT3vzIQHKrxuRN56FCwPLIyEnnQxsc6F8rgUUfPft h/BRjhafLPoh4KRw43LCv+VjlNhu3BDgJCiVz9hEK5QOf6nZlTDpqjmbTMEa7npaUM3K F4kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=/DhhhKwvmG0B7bEBzNr2CdWSy70vxQPdZOerTkDt/ZY=; b=nUlVEjwQZ3mkh5HIuzyHlpsxGajhDLPQVTiMxRTU6FxWMesBw2CXzWZOZnYNvPki7o m+fx9hkvpoQ+d+3ebDfGOSlC3CNZ9mNu5BVBIuV4Jn12WMYrEIWTKVTQfGmGn27UpTPu cnX53n6eTkRh5g5QmmF2/o/VFPl42mAV6yUG1WMYqc4klgk4xqAfVU4UngqiMn2AU4rJ xCZtn/Xmsz5WWwP16D/1XrIShmM+j95cqxyz5TtrDU7H/GOdd/USY2CN/Mf9pBBG/Qxr a2fV3h5IcU6399fIrQVb+5zHL/7URUXozdoY27aRWYwU6eQGnoPFD5jfLRnQdGcvf3ej gp4A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i14si1842254edt.120.2020.04.28.08.28.44; Tue, 28 Apr 2020 08:29:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728741AbgD1PYG (ORCPT + 99 others); Tue, 28 Apr 2020 11:24:06 -0400 Received: from 8bytes.org ([81.169.241.247]:37630 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728204AbgD1PR6 (ORCPT ); Tue, 28 Apr 2020 11:17:58 -0400 Received: by theia.8bytes.org (Postfix, from userid 1000) id E238FE96; Tue, 28 Apr 2020 17:17:44 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: hpa@zytor.com, Andy Lutomirski , Dave Hansen , Peter Zijlstra , Thomas Hellstrom , Jiri Slaby , Dan Williams , Tom Lendacky , Juergen Gross , Kees Cook , David Rientjes , Cfir Cohen , Erdem Aktas , Masami Hiramatsu , Mike Stunes , Joerg Roedel , Joerg Roedel , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v3 15/75] x86/boot/compressed/64: Add page-fault handler Date: Tue, 28 Apr 2020 17:16:25 +0200 Message-Id: <20200428151725.31091-16-joro@8bytes.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200428151725.31091-1-joro@8bytes.org> References: <20200428151725.31091-1-joro@8bytes.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel Install a page-fault handler to add an identity mapping to addresses not yet mapped. Also do some checking whether the error code is sane. This makes non SEV-ES machines use the exception handling infrastructure in the pre-decompressions boot code too, making it less likely to break in the future. Signed-off-by: Joerg Roedel --- arch/x86/boot/compressed/ident_map_64.c | 33 ++++++++++++++++++++++ arch/x86/boot/compressed/idt_64.c | 2 ++ arch/x86/boot/compressed/idt_handlers_64.S | 2 ++ arch/x86/boot/compressed/misc.h | 6 ++++ 4 files changed, 43 insertions(+) diff --git a/arch/x86/boot/compressed/ident_map_64.c b/arch/x86/boot/compressed/ident_map_64.c index 3a2115582920..33bdf923cbab 100644 --- a/arch/x86/boot/compressed/ident_map_64.c +++ b/arch/x86/boot/compressed/ident_map_64.c @@ -19,11 +19,13 @@ /* No PAGE_TABLE_ISOLATION support needed either: */ #undef CONFIG_PAGE_TABLE_ISOLATION +#include "error.h" #include "misc.h" /* These actually do the work of building the kernel identity maps. */ #include #include +#include /* Use the static base for this part of the boot process */ #undef __PAGE_OFFSET #define __PAGE_OFFSET __PAGE_OFFSET_BASE @@ -163,3 +165,34 @@ void finalize_identity_maps(void) { write_cr3(top_level_pgt); } + +void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code) +{ + unsigned long address = native_read_cr2(); + + /* + * Check for unexpected error codes. Unexpected are: + * - Faults on present pages + * - User faults + * - Reserved bits set + */ + if (error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD)) { + /* Print some information for debugging */ + 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"); + } + + /* + * Error code is sane - now identity map the 2M region around + * the faulting address. + */ + add_identity_map(address & PMD_MASK, PMD_SIZE); +} diff --git a/arch/x86/boot/compressed/idt_64.c b/arch/x86/boot/compressed/idt_64.c index 46ecea671b90..99cc78062684 100644 --- a/arch/x86/boot/compressed/idt_64.c +++ b/arch/x86/boot/compressed/idt_64.c @@ -39,5 +39,7 @@ void load_stage2_idt(void) { boot_idt_desc.address = (unsigned long)boot_idt; + set_idt_entry(X86_TRAP_PF, boot_page_fault); + load_boot_idt(&boot_idt_desc); } diff --git a/arch/x86/boot/compressed/idt_handlers_64.S b/arch/x86/boot/compressed/idt_handlers_64.S index f86ea872d860..eda50cbdafa0 100644 --- a/arch/x86/boot/compressed/idt_handlers_64.S +++ b/arch/x86/boot/compressed/idt_handlers_64.S @@ -67,3 +67,5 @@ SYM_FUNC_END(\name) .text .code64 + +EXCEPTION_HANDLER boot_page_fault do_boot_page_fault error_code=1 diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 3a030a878d53..345c90fbc500 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -37,6 +37,9 @@ #define memptr unsigned #endif +/* boot/compressed/vmlinux start and end markers */ +extern char _head[], _end[]; + /* misc.c */ extern memptr free_mem_ptr; extern memptr free_mem_end_ptr; @@ -146,4 +149,7 @@ extern pteval_t __default_kernel_pte_mask; extern gate_desc boot_idt[BOOT_IDT_ENTRIES]; extern struct desc_ptr boot_idt_desc; +/* IDT Entry Points */ +void boot_page_fault(void); + #endif /* BOOT_COMPRESSED_MISC_H */ -- 2.17.1