Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1701046ybh; Tue, 14 Jul 2020 05:12:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxj1FZVnaelAiGO37T++V0tS3Dl8n9qNHpDddbWHxYjtefKmVAmBGJNJ6ThyqPp9Ab2CVvs X-Received: by 2002:a17:906:d106:: with SMTP id b6mr4365695ejz.125.1594728743887; Tue, 14 Jul 2020 05:12:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594728743; cv=none; d=google.com; s=arc-20160816; b=T4Fg4e2WCH21fIhmPYoW2rSneNA+7YvTC5ht8GLDFbtsDTZ0+hM4X9wmZ0CrJ72mBg XBMK0bZePcxAZnD875/X8odLoir/TXTJobmfuSyH3gNUpadfNdipS43XUSMmAm2DwP+V HHv7teFokHOCURLfn3Vb0GcBl9C1vyiHdYlTRPiXaXzdkbjRlcIzJTCNZZazjy9SJ78S 8SnohqhnlcB/OC/V3d4U+v6d/DORgLwE/QmOJzWKCgKZ6U3ikKmmFDO5UIwCjv6k6Fqk qOHNnD78Xfs0TjYiKyUCGUsXccm2Z7QBjpVu1KrtydNKYiziBQuFoxoBdZmOKKkp4oDv CPmQ== 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=Nq7HCHZxjOn5Z0Nusnx4OY7sS6t9RmXeAVJNft4GPCA=; b=O0VJlV+jih/LWqZKMa8YeiQS5PCi8eJQNx/5rQW87rOPWq2US1LR6dwWZVMbOvom96 TrE0L4v+4eiYOgmyamdP1HADYstJssuGU03zLIixLHoWubiU/jIyobfxUcfeLpNgkSsB XMBlVlWLF7evTqJ2nAs0bDmZUucqQk/QVgwtuSzUM7ExZxswAbaGb0RtYdVJKhFiQgXU 065SpU68h5Wrx0TqPDkStGVr8Z+iCEdxFf9YGCsM9lj1ydLqGPTzkwcA/+lKQuxZcmKZ F5z83OHsvibFpIj0bETUGECE2u49UM+XixtW52e2L0XeCqrPE3wRElpwzhICizfnqA05 0Ybw== 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 dt2si15019643ejc.596.2020.07.14.05.11.59; Tue, 14 Jul 2020 05:12:23 -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 S1728535AbgGNMLP (ORCPT + 99 others); Tue, 14 Jul 2020 08:11:15 -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 S1728494AbgGNMLK (ORCPT ); Tue, 14 Jul 2020 08:11:10 -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 0199BFC6; Tue, 14 Jul 2020 14:11:03 +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 56/75] x86/sev-es: Handle RDTSC(P) Events Date: Tue, 14 Jul 2020 14:08:58 +0200 Message-Id: <20200714120917.11253-57-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 Implement a handler for #VC exceptions caused by RDTSC and RDTSCP instructions. Also make it available in the pre-decompression stage because the KASLR code used RDTSC/RDTSCP to gather entropy and some hypervisors intercept these instructions. Signed-off-by: Tom Lendacky [ jroedel@suse.de: - Adapt to #VC handling infrastructure - Make it available early ] Co-developed-by: Joerg Roedel Signed-off-by: Joerg Roedel --- arch/x86/boot/compressed/sev-es.c | 4 ++++ arch/x86/kernel/sev-es-shared.c | 23 +++++++++++++++++++++++ arch/x86/kernel/sev-es.c | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/arch/x86/boot/compressed/sev-es.c b/arch/x86/boot/compressed/sev-es.c index 851d7af29d79..1ce144e0ddc3 100644 --- a/arch/x86/boot/compressed/sev-es.c +++ b/arch/x86/boot/compressed/sev-es.c @@ -181,6 +181,10 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) goto finish; switch (exit_code) { + case SVM_EXIT_RDTSC: + case SVM_EXIT_RDTSCP: + result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code); + break; case SVM_EXIT_IOIO: result = vc_handle_ioio(boot_ghcb, &ctxt); break; diff --git a/arch/x86/kernel/sev-es-shared.c b/arch/x86/kernel/sev-es-shared.c index 59884926fae5..608f76d0d088 100644 --- a/arch/x86/kernel/sev-es-shared.c +++ b/arch/x86/kernel/sev-es-shared.c @@ -467,3 +467,26 @@ static enum es_result vc_handle_cpuid(struct ghcb *ghcb, return ES_OK; } + +static enum es_result vc_handle_rdtsc(struct ghcb *ghcb, + struct es_em_ctxt *ctxt, + unsigned long exit_code) +{ + bool rdtscp = (exit_code == SVM_EXIT_RDTSCP); + enum es_result ret; + + ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0); + if (ret != ES_OK) + return ret; + + if (!(ghcb_is_valid_rax(ghcb) && ghcb_is_valid_rdx(ghcb) && + (!rdtscp || ghcb_is_valid_rcx(ghcb)))) + return ES_VMM_ERROR; + + ctxt->regs->ax = ghcb->save.rax; + ctxt->regs->dx = ghcb->save.rdx; + if (rdtscp) + ctxt->regs->cx = ghcb->save.rcx; + + return ES_OK; +} diff --git a/arch/x86/kernel/sev-es.c b/arch/x86/kernel/sev-es.c index aa117004fc2e..0e69d498f008 100644 --- a/arch/x86/kernel/sev-es.c +++ b/arch/x86/kernel/sev-es.c @@ -872,6 +872,10 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt, case SVM_EXIT_WRITE_DR7: result = vc_handle_dr7_write(ghcb, ctxt); break; + case SVM_EXIT_RDTSC: + case SVM_EXIT_RDTSCP: + result = vc_handle_rdtsc(ghcb, ctxt, exit_code); + break; case SVM_EXIT_CPUID: result = vc_handle_cpuid(ghcb, ctxt); break; -- 2.27.0