Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1703711ybh; Tue, 14 Jul 2020 05:16:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwR9kZC9Nff7rQnq1q+uQaIoCTWO4fwApGRi/aw17dUaJaH8MYnKbmxPW6tBNmYLjbaTzll X-Received: by 2002:aa7:d4ca:: with SMTP id t10mr4218646edr.244.1594729001758; Tue, 14 Jul 2020 05:16:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594729001; cv=none; d=google.com; s=arc-20160816; b=ekHOcC+8WYiApCif9aA3xlMhVMaRft+XwT9pPMAwEIrZRUeb2B2z/V4f7kP6d1KmPc eBI5J054FK5d6yOykOC8IVlSs/1MWXySawaFD/uq8ykNrLFVUalDk1gOT3h0R2WLWH+l Z/sF2k7tkynGGBA8Jj387Zj5PeU2CDh9hiR7HUcrvmPng0AZvfrUd5cHcdgNjK++U5DG +1Ez3Jcakwp+OFNJHM7VRTuZZneNGuIawRqnpStj8PofmoCcrDkzj2z5ONazbW5/tGps RDQHkj7KpRF3gTBIijedBC7BeTQ5Z5ascf1kOA29wAJFpycgpUZMVAGF6muvNBxo4Ni1 lA8Q== 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=CDIN5cHdh3pcT6jgnd1l+WvkCDg0ecvDDAGY9HONPhw=; b=V0WlmEKbtYILkVJjmIQrNzjvzVr+/Q61c+QcMOA2X0mt1+u+dy1uwcePIuX0XYmCMf +7+L5mPz0xp5gljVcq5uv/NlUpb3amgnANv4hszhMOM69t+lX7vqSqMakOmxMZkvqAk7 QJfHh7AYMpcyDJdFKXxxfXa4V5IwZgtfKbLkDsGfx10wPNTBQJp3Ohp1VYRlMmhhLEwO ApPRlPWu2KJJkerTHFoMuZEbgU1TQbKzyZ4f0yIjD1obdeRBznrs+ZjrPBKF8JdZtx39 ODrcou/XIxpfTK7KcITKt7UgfTPHcxmCFAA2wLoxzXhNEREgQYg/S6Sbe0NSdN/+VciD Wfuw== 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 p26si11892326edy.181.2020.07.14.05.16.17; Tue, 14 Jul 2020 05:16:41 -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 S1728440AbgGNMOd (ORCPT + 99 others); Tue, 14 Jul 2020 08:14:33 -0400 Received: from 8bytes.org ([81.169.241.247]:53376 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728381AbgGNMK7 (ORCPT ); Tue, 14 Jul 2020 08:10:59 -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 D9C9DF8B; Tue, 14 Jul 2020 14:10:55 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: Joerg Roedel , Joerg Roedel , Tom Lendacky , hpa@zytor.com, Andy Lutomirski , Dave Hansen , Peter Zijlstra , Jiri Slaby , Dan Williams , 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 42/75] x86/sev-es: Setup per-cpu GHCBs for the runtime handler Date: Tue, 14 Jul 2020 14:08:44 +0200 Message-Id: <20200714120917.11253-43-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: Tom Lendacky The runtime handler needs a GHCB per CPU. Set them up and map them unencrypted. Signed-off-by: Tom Lendacky Signed-off-by: Joerg Roedel --- arch/x86/include/asm/mem_encrypt.h | 2 ++ arch/x86/kernel/sev-es.c | 56 +++++++++++++++++++++++++++++- arch/x86/kernel/traps.c | 3 ++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h index 6f61bb93366a..af3e58aa1603 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -48,6 +48,7 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size); void __init mem_encrypt_init(void); void __init mem_encrypt_free_decrypted_mem(void); +void __init sev_es_init_vc_handling(void); bool sme_active(void); bool sev_active(void); bool sev_es_active(void); @@ -71,6 +72,7 @@ static inline void __init sme_early_init(void) { } static inline void __init sme_encrypt_kernel(struct boot_params *bp) { } static inline void __init sme_enable(struct boot_params *bp) { } +static inline void sev_es_init_vc_handling(void) { } static inline bool sme_active(void) { return false; } static inline bool sev_active(void) { return false; } static inline bool sev_es_active(void) { return false; } diff --git a/arch/x86/kernel/sev-es.c b/arch/x86/kernel/sev-es.c index bb3e702a71eb..734afaee378d 100644 --- a/arch/x86/kernel/sev-es.c +++ b/arch/x86/kernel/sev-es.c @@ -8,8 +8,13 @@ */ #include /* For show_regs() */ -#include +#include +#include #include +#include +#include +#include +#include #include #include @@ -29,6 +34,13 @@ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE); */ static struct ghcb __initdata *boot_ghcb; +/* #VC handler runtime per-cpu data */ +struct sev_es_runtime_data { + struct ghcb ghcb_page; +}; + +static DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data); + /* Needed in vc_early_forward_exception */ void do_early_exception(struct pt_regs *regs, int trapnr); @@ -198,6 +210,48 @@ static bool __init sev_es_setup_ghcb(void) return true; } +static void __init sev_es_alloc_runtime_data(int cpu) +{ + struct sev_es_runtime_data *data; + + data = memblock_alloc(sizeof(*data), PAGE_SIZE); + if (!data) + panic("Can't allocate SEV-ES runtime data"); + + per_cpu(runtime_data, cpu) = data; +} + +static void __init sev_es_init_ghcb(int cpu) +{ + struct sev_es_runtime_data *data; + int err; + + data = per_cpu(runtime_data, cpu); + + err = early_set_memory_decrypted((unsigned long)&data->ghcb_page, + sizeof(data->ghcb_page)); + if (err) + panic("Can not map GHCBs unencrypted"); + + memset(&data->ghcb_page, 0, sizeof(data->ghcb_page)); +} + +void __init sev_es_init_vc_handling(void) +{ + int cpu; + + BUILD_BUG_ON((offsetof(struct sev_es_runtime_data, ghcb_page) % PAGE_SIZE) != 0); + + if (!sev_es_active()) + return; + + /* Initialize per-cpu GHCB pages */ + for_each_possible_cpu(cpu) { + sev_es_alloc_runtime_data(cpu); + sev_es_init_ghcb(cpu); + } +} + static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt) { int trapnr = ctxt->fi.vector; diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index b038695f36c5..907ac2b378a8 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -1081,6 +1081,9 @@ void __init trap_init(void) /* Init cpu_entry_area before IST entries are set up */ setup_cpu_entry_areas(); + /* Init GHCB memory pages when running as an SEV-ES guest */ + sev_es_init_vc_handling(); + idt_setup_traps(); /* -- 2.27.0