Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1752291pxm; Thu, 24 Feb 2022 08:43:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJzFi84mJh0QN9CPeNEWcZLMX6x0j2QUWM6FqnR7UEFzQcKSolmMf6Q+5IBYagEk/GsPmKv5 X-Received: by 2002:a17:90b:3587:b0:1bc:87f6:73ae with SMTP id mm7-20020a17090b358700b001bc87f673aemr3640954pjb.14.1645720997908; Thu, 24 Feb 2022 08:43:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645720997; cv=none; d=google.com; s=arc-20160816; b=y4NME6JF+gXpWiCptCOOoni/kejjvq39s5xIURls8h5lXlBuJe9dxWjzkM9/sC5WrU xjL38VWXrI/AiT2u5qR823qXeDvlKZDrax+bpAY/o+iDs3chF7FfCWzZBnPrEwr14fEQ oXxC15iImcwXPGbomLLrLbRT/DKXau1nJs1miIJzgHIc+qbINivd/qDfBzyIYCxzL7mf YakzJI9RvxWh/0GWO/8qDhMnGVw175lzanpJCh2TYcDMQMcmw2YdFZaoJtCLJBdcMH6k 4wkhfwUqNUtKIE9nwfx2kRvfhfA7vUAvmlwswAkZ4ajVzRi2Zmlme9EFAfSzOh9MCYPV l25A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=mC+gofEXdsU+rFLkRyGEfPez2kFHTw81WCI/EbnrB+c=; b=TR5Pg1jSHUyY1MXFF0JaXgRtV9Gzl2o2zOqJ+apJufy5h8s47NlvUfjxN662oSexcM eOH2aGMfoWm2IT4idfLCzk3hz6ASrU3/4aSBB8o2AErY0YzsK3fN7h9Pbte8sGGHnkta jz9DFFp4W3vou1NFIuF1IuqXh/4MrpK38jWT2hHOHY+gx5H69nlERUjfzx9Rk/yd/5fd IkrDB15sHnamMRzmWO4a17pSxNyKAKkoWdI343fbPXkvd+By62MqFZ/N8S6Vps3xRSw5 E7UNg+R5O0F81uKk1SPIf0yryQ/JgeJrapTTPlDsex7eb0uiCZ28wRFfFc/ntGvqfWAj O9uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="ByzT/3p2"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id m20-20020a17090ade1400b001bc6d8da02asi5351094pjv.140.2022.02.24.08.43.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 08:43:17 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="ByzT/3p2"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EA21F2614B1; Thu, 24 Feb 2022 08:22:52 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236739AbiBXP5W (ORCPT + 99 others); Thu, 24 Feb 2022 10:57:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236680AbiBXP5F (ORCPT ); Thu, 24 Feb 2022 10:57:05 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D288B33C for ; Thu, 24 Feb 2022 07:56:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1645718191; x=1677254191; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iAKapfo2TJyc613nnXWyeTmHnGckX06XMzYOp7xRyi0=; b=ByzT/3p267MaiBGeuCWtUmOzuXZOAGutBuyMA7JfP6ibtqJdI17cw0Yt BTEykOrrYommW807hCAjHd9RCXazw2SFrBwCVKM3+CaycAJNjD9moju75 +XpP6w6K+9HNar4YxWef1ZYoy/zP3P8/2bLYFO4E4IvuxOKn/hE2G/j9P zH3WOWO0p2jVTmXcx/Z4g/M/S1xNMjmQwNHzB93A/Ki4aJ9tmyaGHvnNa SPUUxKtc6d7Dh8VbTJwDVJI9CRdZBju3vpmfEbhk/ckZnBZPZ5WfQtXuz vvtRBnck7UMD4CCr92CbHea2fx/SJAE+Zn4vbj2OJxlLdSvZgIG7j7URJ A==; X-IronPort-AV: E=McAfee;i="6200,9189,10268"; a="232889946" X-IronPort-AV: E=Sophos;i="5.90,134,1643702400"; d="scan'208";a="232889946" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Feb 2022 07:56:31 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,134,1643702400"; d="scan'208";a="543744455" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga007.fm.intel.com with ESMTP; 24 Feb 2022 07:56:25 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id A5F5D924; Thu, 24 Feb 2022 17:56:34 +0200 (EET) From: "Kirill A. Shutemov" To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@intel.com, luto@kernel.org, peterz@infradead.org Cc: sathyanarayanan.kuppuswamy@linux.intel.com, aarcange@redhat.com, ak@linux.intel.com, dan.j.williams@intel.com, david@redhat.com, hpa@zytor.com, jgross@suse.com, jmattson@google.com, joro@8bytes.org, jpoimboe@redhat.com, knsathya@kernel.org, pbonzini@redhat.com, sdeep@vmware.com, seanjc@google.com, tony.luck@intel.com, vkuznets@redhat.com, wanpengli@tencent.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, x86@kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv4 10/30] x86/tdx: Handle CPUID via #VE Date: Thu, 24 Feb 2022 18:56:10 +0300 Message-Id: <20220224155630.52734-11-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220224155630.52734-1-kirill.shutemov@linux.intel.com> References: <20220224155630.52734-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In TDX guests, most CPUID leaf/sub-leaf combinations are virtualized by the TDX module while some trigger #VE. Implement the #VE handling for EXIT_REASON_CPUID by handing it through the hypercall, which in turn lets the TDX module handle it by invoking the host VMM. More details on CPUID Virtualization can be found in the TDX module specification, the section titled "CPUID Virtualization". Co-developed-by: Kuppuswamy Sathyanarayanan Signed-off-by: Kuppuswamy Sathyanarayanan Reviewed-by: Andi Kleen Reviewed-by: Tony Luck Signed-off-by: Kirill A. Shutemov --- arch/x86/coco/tdx.c | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/arch/x86/coco/tdx.c b/arch/x86/coco/tdx.c index 89992593a209..fd78b81a951d 100644 --- a/arch/x86/coco/tdx.c +++ b/arch/x86/coco/tdx.c @@ -154,6 +154,36 @@ static bool write_msr(struct pt_regs *regs) return !__tdx_hypercall(&args, 0); } +static bool handle_cpuid(struct pt_regs *regs) +{ + struct tdx_hypercall_args args = { + .r10 = TDX_HYPERCALL_STANDARD, + .r11 = EXIT_REASON_CPUID, + .r12 = regs->ax, + .r13 = regs->cx, + }; + + /* + * Emulate the CPUID instruction via a hypercall. More info about + * ABI can be found in TDX Guest-Host-Communication Interface + * (GHCI), section titled "VP.VMCALL". + */ + if (__tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT)) + return false; + + /* + * As per TDX GHCI CPUID ABI, r12-r15 registers contain contents of + * EAX, EBX, ECX, EDX registers after the CPUID instruction execution. + * So copy the register contents back to pt_regs. + */ + regs->ax = args.r12; + regs->bx = args.r13; + regs->cx = args.r14; + regs->dx = args.r15; + + return true; +} + void tdx_get_ve_info(struct ve_info *ve) { struct tdx_module_output out; @@ -186,8 +216,13 @@ void tdx_get_ve_info(struct ve_info *ve) */ static bool virt_exception_user(struct pt_regs *regs, struct ve_info *ve) { - pr_warn("Unexpected #VE: %lld\n", ve->exit_reason); - return false; + switch (ve->exit_reason) { + case EXIT_REASON_CPUID: + return handle_cpuid(regs); + default: + pr_warn("Unexpected #VE: %lld\n", ve->exit_reason); + return false; + } } /* Handle the kernel #VE */ @@ -200,6 +235,8 @@ static bool virt_exception_kernel(struct pt_regs *regs, struct ve_info *ve) return read_msr(regs); case EXIT_REASON_MSR_WRITE: return write_msr(regs); + case EXIT_REASON_CPUID: + return handle_cpuid(regs); default: pr_warn("Unexpected #VE: %lld\n", ve->exit_reason); return false; -- 2.34.1