Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp199329ybz; Fri, 24 Apr 2020 14:24:27 -0700 (PDT) X-Google-Smtp-Source: APiQypLy0LjMkk/xLT9NTxcAVhJnDJPSysXoMCp6/B4AkAYEOnHA8KdinzkRHleiM7K+FXsVoJwc X-Received: by 2002:a17:906:adb:: with SMTP id z27mr9168471ejf.263.1587763467014; Fri, 24 Apr 2020 14:24:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587763467; cv=none; d=google.com; s=arc-20160816; b=POXKD82Msa+XG2uJnoLk/r9Ss0BrJ30f6fJOKb96FF2coF+Fc3nw50iyHh30VFTUPw 4IhR20EzlRheYopmHJI9XLq+zZR8ypHVUBB6R8gfKCA8U1PPKpiyJZZvOkqYa4wdbE1q HELIugLTOzMikjm5PcA7hy2QPGYol89RkGA5tBYlHn5KVAG5uTbIgjpVJyjKcYRf6wuj fWxTQxUbfp3XM5tkbNCkmHdwfTFOPqCruFtsVGypO3j+PEeCeWS35zgBG3573iISAkfX uiBxOZLHRGiDST82SvflVDXJjja71aNUL8GaEksy/kKZUruo/J23xHxz+o/Pka0Ejw/X PZ4Q== 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=fk6V3gkP9kYcVQ6yaujrMdTnSlXUSAWJFBQjrHH/wLY=; b=odpVGNz9nttaGgoZDMEOSILOBlBoJxnqpR6dPLoR8V8sUv/YoIyWudl6DZlS9PqXkM 7uq3ZXeXj+gesj+bi0lMJOgksKnyF21g0h0Rwjwsy/fuNdW+CPEgEDP0mGeNKRXb30d1 wi+i23M1YmzbC3velg0mbGHXKTD0MKURmsevofZZE7VnRUCeXZqCwwub8uKLOnDmJCIk n/h96/vvAC1rsDU17CwgYGESVDM34emmAtJBztV0xNGIA7pVYgdWWDG52rtFevZCrSmh xUeHK2qd8KCeXt/gR1TYabHxn54OsshLTeGZQKisl64ecNd6lE3nSpeB2r7ZjK5M78p+ NfVQ== 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=vmware.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r12si3704850edw.278.2020.04.24.14.24.03; Fri, 24 Apr 2020 14:24:27 -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=vmware.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726070AbgDXVSl (ORCPT + 99 others); Fri, 24 Apr 2020 17:18:41 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:9377 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726027AbgDXVSk (ORCPT ); Fri, 24 Apr 2020 17:18:40 -0400 X-Greylist: delayed 904 seconds by postgrey-1.27 at vger.kernel.org; Fri, 24 Apr 2020 17:18:40 EDT Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Fri, 24 Apr 2020 14:03:32 -0700 Received: from mstunes-sid.eng.vmware.com (mstunes-sid.eng.vmware.com [10.118.100.24]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id CBB63405C8; Fri, 24 Apr 2020 14:03:35 -0700 (PDT) From: Mike Stunes To: CC: , , , , , , , , , , , , , , , Mike Stunes Subject: [PATCH] Allow RDTSC and RDTSCP from userspace Date: Fri, 24 Apr 2020 14:03:16 -0700 Message-ID: <20200424210316.848878-1-mstunes@vmware.com> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200319091407.1481-56-joro@8bytes.org> References: <20200319091407.1481-56-joro@8bytes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII Received-SPF: None (EX13-EDG-OU-002.vmware.com: mstunes@vmware.com does not designate permitted sender hosts) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Joerg, I needed to allow RDTSC(P) from userspace and in early boot in order to get userspace started properly. Patch below. --- SEV-ES guests will need to execute rdtsc and rdtscp from userspace and during early boot. Move the rdtsc(p) #VC handler into common code and extend the #VC handlers. Signed-off-by: Mike Stunes --- arch/x86/boot/compressed/sev-es.c | 4 ++++ arch/x86/kernel/sev-es-shared.c | 23 +++++++++++++++++++++++ arch/x86/kernel/sev-es.c | 25 ++----------------------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/arch/x86/boot/compressed/sev-es.c b/arch/x86/boot/compressed/sev-es.c index 53c65fc09341..1d0290cc46c1 100644 --- a/arch/x86/boot/compressed/sev-es.c +++ b/arch/x86/boot/compressed/sev-es.c @@ -158,6 +158,10 @@ void boot_vc_handler(struct pt_regs *regs, unsigned long exit_code) case SVM_EXIT_CPUID: result = vc_handle_cpuid(boot_ghcb, &ctxt); break; + case SVM_EXIT_RDTSC: + case SVM_EXIT_RDTSCP: + result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code); + break; default: result = ES_UNSUPPORTED; break; diff --git a/arch/x86/kernel/sev-es-shared.c b/arch/x86/kernel/sev-es-shared.c index a632b8f041ec..373ced468659 100644 --- a/arch/x86/kernel/sev-es-shared.c +++ b/arch/x86/kernel/sev-es-shared.c @@ -442,3 +442,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 409a7a2aa630..82199527d012 100644 --- a/arch/x86/kernel/sev-es.c +++ b/arch/x86/kernel/sev-es.c @@ -815,29 +815,6 @@ static enum es_result vc_handle_wbinvd(struct ghcb *ghcb, return sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WBINVD, 0, 0); } -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; -} - static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt) { enum es_result ret; @@ -1001,6 +978,8 @@ static enum es_result vc_context_filter(struct pt_regs *regs, long exit_code) /* List of #VC exit-codes we support in user-space */ case SVM_EXIT_EXCP_BASE ... SVM_EXIT_LAST_EXCP: case SVM_EXIT_CPUID: + case SVM_EXIT_RDTSC: + case SVM_EXIT_RDTSCP: r = ES_OK; break; default: -- 2.26.1