Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1703828ybh; Tue, 14 Jul 2020 05:16:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxaej7pYWIoNpL6QSNq1iDQGnJzM1KxPu/1t0lQKkF6brxM/pb0WVRHEY+VSOpida46zmEL X-Received: by 2002:a17:906:aac9:: with SMTP id kt9mr4021581ejb.488.1594729010800; Tue, 14 Jul 2020 05:16:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594729010; cv=none; d=google.com; s=arc-20160816; b=ukE6qt+W3rHoWzIoyNnAE+5IAyKa/buxU/w+LStT//J+m+Lgni6x8bexrzXHL2ymUc 86cpUEoGYnnfwY20MmeY3+sGK5Tn9cnSG7rMlQ2frrxzIt+tRhQgi5HXCjP4dyd27F41 Cy0xOqLx/su6ElfURQAvlf6qurE9ABq0N9fuduRJKqyvHjwr2wpaexnetz51htcSpb11 KF6h/KUUCVATplfjKjIo4OoTIvgFi0g0Pw8KmrgUppRw6i+kOoeIBVR23DKwhriVRWYJ kcC6e6xuWdrv1K1q37Eft4ilncYeW52TioPLLBKWZLGrSQkipEb8hGnqNlNp+ufFdEkL KVgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=/A4FZ1CB6NmxT1atTKkY5t8Qs9gQq9bdNub09yokLEM=; b=pVLtkYlW0zpMUDvZtRSEOV4XBElcW9e/Tecwmn9SykrtvVbQbN0fdE21Yvu0ZldUW3 7HfAZ9Xp+pgf8C+2B4rYrXXlgk48RGS9s7QsLN6aBCR/SkStW12LKSc4eVDkmGVrrgg2 v/3ISTdK5E1FirhZVVMEfYBHnj8Yye/pjXNqN41FswXXLo5zzpra42iKhDH8XcO1lhL4 rZbQNr6hhRnMP+XVrhclYiL+Q2f/gFc+J3q/bLKcXePgsfoJnX+B4cLDHi0OfnSwmXmB flv4KIOrNROpm41MlE8BL4RHdDphz9mABkaSes50A1hoXoE46Cckm/nXsMWwQkiP/X7Y BjJQ== 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 u25si11562732ejr.632.2020.07.14.05.16.25; Tue, 14 Jul 2020 05:16:50 -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 S1728684AbgGNMO5 (ORCPT + 99 others); Tue, 14 Jul 2020 08:14:57 -0400 Received: from 8bytes.org ([81.169.241.247]:52918 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728352AbgGNMK6 (ORCPT ); Tue, 14 Jul 2020 08:10:58 -0400 Received: from cap.home.8bytes.org (p5b006776.dip0.t-ipconnect.de [91.0.103.118]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by theia.8bytes.org (Postfix) with ESMTPSA id B4CF6F89; Tue, 14 Jul 2020 14:10:54 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: Joerg Roedel , Joerg Roedel , hpa@zytor.com, Andy Lutomirski , Dave Hansen , Peter Zijlstra , Jiri Slaby , Dan Williams , Tom Lendacky , Juergen Gross , Kees Cook , David Rientjes , Cfir Cohen , Erdem Aktas , Masami Hiramatsu , Mike Stunes , Sean Christopherson , Martin Radev , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v4 40/75] x86/sev-es: Setup early #VC handler Date: Tue, 14 Jul 2020 14:08:42 +0200 Message-Id: <20200714120917.11253-41-joro@8bytes.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714120917.11253-1-joro@8bytes.org> References: <20200714120917.11253-1-joro@8bytes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel Setup an early handler for #VC exceptions. There is no GHCB mapped yet, so just re-use the vc_no_ghcb_handler. It can only handle CPUID exit-codes, but that should be enough to get the kernel through verify_cpu() and __startup_64() until it runs on virtual addresses. Signed-off-by: Joerg Roedel --- arch/x86/include/asm/processor.h | 1 + arch/x86/include/asm/sev-es.h | 3 +++ arch/x86/kernel/head64.c | 31 +++++++++++++++++++++++++ arch/x86/kernel/head_64.S | 39 ++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index 03b7c4ca425a..e6a029b76b3d 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -756,6 +756,7 @@ extern int sysenter_setup(void); /* Defined in head.S */ extern struct desc_ptr early_gdt_descr; +extern struct desc_ptr early_idt_descr; extern void switch_to_new_gdt(int); extern void load_direct_gdt(int); diff --git a/arch/x86/include/asm/sev-es.h b/arch/x86/include/asm/sev-es.h index 7c0807b84546..ec0e112a742b 100644 --- a/arch/x86/include/asm/sev-es.h +++ b/arch/x86/include/asm/sev-es.h @@ -73,4 +73,7 @@ static inline u64 lower_bits(u64 val, unsigned int bits) return (val & mask); } +/* Early IDT entry points for #VC handler */ +extern void vc_no_ghcb(void); + #endif diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index 32080e34ba92..9ea8a0437b66 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -38,6 +38,7 @@ #include #include #include +#include /* * Manage page tables very early on. @@ -537,3 +538,33 @@ void __head early_idt_setup_early_handler(unsigned long descr_addr, unsigned lon native_write_idt_entry(idt, i, &desc); } } + +#ifdef CONFIG_AMD_MEM_ENCRYPT +static void __head set_early_idt_handler(gate_desc *idt, int n, void *handler) +{ + struct idt_data data; + gate_desc desc; + + init_idt_data(&data, n, handler); + idt_init_desc(&desc, &data); + native_write_idt_entry(idt, n, &desc); +} +#endif + +void __head early_idt_setup(struct desc_ptr *descr, unsigned long physbase) +{ + gate_desc *idt = (gate_desc *)descr->address; + void __maybe_unused *handler; + + idt = fixup_pointer(idt, physbase); + +#ifdef CONFIG_AMD_MEM_ENCRYPT + /* VMM Communication Exception */ + handler = fixup_pointer(vc_no_ghcb, physbase); + set_early_idt_handler(idt, X86_TRAP_VC, handler); +#endif + + /* Initialize IDT descriptor and load IDT */ + early_idt_descr.address = (unsigned long)idt; + native_load_idt(&early_idt_descr); +} diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 86473d49619d..3bd4c36d1d36 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -93,6 +93,15 @@ SYM_CODE_START_NOALIGN(startup_64) .Lon_kernel_cs: UNWIND_HINT_EMPTY + /* Setup IDT - Needed for SEV-ES */ + pushq %rsi + /* early_idt_setup - first parameter */ + leaq idt_descr(%rip), %rdi + /* early_idt_setup - second parameter */ + leaq _text(%rip), %rsi + call early_idt_setup + popq %rsi + /* Sanitize CPU configuration */ call verify_cpu @@ -382,6 +391,33 @@ SYM_CODE_START_LOCAL(early_idt_handler_common) jmp restore_regs_and_return_to_kernel SYM_CODE_END(early_idt_handler_common) +#ifdef CONFIG_AMD_MEM_ENCRYPT +/* + * VC Exception handler used during very early boot. The + * early_idt_handler_array can't be used because it returns via the + * paravirtualized INTERRUPT_RETURN and pv-ops don't work that early. + */ +SYM_CODE_START_NOALIGN(vc_no_ghcb) + UNWIND_HINT_IRET_REGS offset=8 + + /* Build pt_regs */ + PUSH_AND_CLEAR_REGS + + /* Call C handler */ + movq %rsp, %rdi + movq ORIG_RAX(%rsp), %rsi + call do_vc_no_ghcb + + /* Unwind pt_regs */ + POP_REGS + + /* Remove Error Code */ + addq $8, %rsp + + /* Pure iret required here - don't use INTERRUPT_RETURN */ + iretq +SYM_CODE_END(vc_no_ghcb) +#endif #define SYM_DATA_START_PAGE_ALIGNED(name) \ SYM_START(name, SYM_L_GLOBAL, .balign PAGE_SIZE) @@ -523,6 +559,9 @@ SYM_DATA_END(level1_fixmap_pgt) SYM_DATA(early_gdt_descr, .word GDT_ENTRIES*8-1) SYM_DATA_LOCAL(early_gdt_descr_base, .quad INIT_PER_CPU_VAR(gdt_page)) +SYM_DATA(early_idt_descr, .word NUM_EXCEPTION_VECTORS * 16) +SYM_DATA_LOCAL(early_idt_descr_base, .quad 0) + .align 16 /* This must match the first entry in level2_kernel_pgt */ SYM_DATA(phys_base, .quad 0x0) -- 2.27.0