Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp3844984pxy; Mon, 26 Apr 2021 11:05:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPmMEO0ZWFPCVKDcysYVL6AvMwxXLJHLTVas5bkC1qNL4EaanYGZHcOm6pszQV/Kyr9FIL X-Received: by 2002:a17:906:77c5:: with SMTP id m5mr18927330ejn.201.1619460323324; Mon, 26 Apr 2021 11:05:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619460323; cv=none; d=google.com; s=arc-20160816; b=ilYcOMVp4jstW5q4Y5HxstgZGMCtI/R5Oz2RWqzAOXFlkhlDCsbNZuHkX4ikSXdTib Y80Txe9vEjba//qPtJAN6xIb5d7c6LkuLNqTDnkBilb0azvwjygfyc0hdQfKiXtVAYn7 1VF/4aQruYj/x+Eg8JfV2jnxQ4phF4OiGsMSH/vsJV/hw0C5CJ4nuzw+NqN9vgpA0gh3 ow9a9zdU43kZ9vqNeQ76RFJaAu/y9glG70d3o+wQH3bCnC3qNKVQpLLc0a34kkarR5pV 7iQEnVfxu21plJK3CPy0Pb+vctmVscE/JiZpUMkl/oHero+dmFsgiRHTb2c236w3QW0b p1DQ== 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 :ironport-sdr:ironport-sdr; bh=0sjv0uEIAxaQxNH7JWh33Z/TRotBeE6itPU8kxg3MnE=; b=CRvI6lfkxi8At382VkUH3CFiring//Ul5hNcsA5tbMXBSEiQBNFVKbNKHGdObuSfBT jOxwf+qX4Yb4WIsThf46bsWV03wwGOUeML/ody/6WeMTkBxilu9o5FGf5Fs2r5ZSzrAS g0wY0CyP97FzkkH8KGR91Wi3eYJVcQKzlHFbxuGQF/Va+5mJ1+s+iCzgqXCsb5w+h+/V flMD86mM+HSGKhVnE/Akam6QRFmKtKhsotcMvHYeOX8iWPxYwQgI9h7matoSboIQhrjO 6X5pdKVsmTbgQPz4/01sP4LXbeszUndIUwD7g6s0poCOw6zJUdRi3BO2AfK2HurE637N hVuQ== 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=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v13si536720edl.24.2021.04.26.11.04.59; Mon, 26 Apr 2021 11:05: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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234809AbhDZSEN (ORCPT + 99 others); Mon, 26 Apr 2021 14:04:13 -0400 Received: from mga14.intel.com ([192.55.52.115]:31744 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234283AbhDZSDa (ORCPT ); Mon, 26 Apr 2021 14:03:30 -0400 IronPort-SDR: iiRhV4w60o5tc8rZTyFEdio768D3IJNZapGEoFy2mK6KLljlgx2M7fL9tg8LBCauDKGBWwMdCC iwmSz5ZjViWw== X-IronPort-AV: E=McAfee;i="6200,9189,9966"; a="195934042" X-IronPort-AV: E=Sophos;i="5.82,252,1613462400"; d="scan'208";a="195934042" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Apr 2021 11:02:47 -0700 IronPort-SDR: tBBgFdamlHP0+DALQaEyGOgfV75gRQBtU0xsbtMlLMVgyDwbOMCrw7i8cZxGRLo3Le4RdrIZl6 LI20kkCeXj0w== X-IronPort-AV: E=Sophos;i="5.82,252,1613462400"; d="scan'208";a="447353371" Received: from ssumanpx-mobl.amr.corp.intel.com (HELO skuppusw-mobl5.amr.corp.intel.com) ([10.254.34.197]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Apr 2021 11:02:46 -0700 From: Kuppuswamy Sathyanarayanan To: Peter Zijlstra , Andy Lutomirski , Dave Hansen , Dan Williams , Tony Luck Cc: Andi Kleen , Kirill Shutemov , Kuppuswamy Sathyanarayanan , Raj Ashok , Sean Christopherson , linux-kernel@vger.kernel.org, Isaku Yamahata , Kuppuswamy Sathyanarayanan Subject: [RFC v2 10/32] x86/tdx: Wire up KVM hypercalls Date: Mon, 26 Apr 2021 11:01:37 -0700 Message-Id: <04de6cd6549eee0b77fe3b0937c4b4b7f0e3ff2e.1619458733.git.sathyanarayanan.kuppuswamy@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Kirill A. Shutemov" KVM hypercalls have to be wrapped into vendor-specific TDVMCALLs. [Isaku: proposed KVM VENDOR string] Signed-off-by: Kirill A. Shutemov Signed-off-by: Isaku Yamahata Reviewed-by: Andi Kleen Signed-off-by: Kuppuswamy Sathyanarayanan --- arch/x86/include/asm/kvm_para.h | 21 +++++++++++++++ arch/x86/include/asm/tdx.h | 39 ++++++++++++++++++++++++++++ arch/x86/kernel/tdcall.S | 7 +++++ arch/x86/kernel/tdx-kvm.c | 45 +++++++++++++++++++++++++++++++++ arch/x86/kernel/tdx.c | 4 +++ 5 files changed, 116 insertions(+) create mode 100644 arch/x86/kernel/tdx-kvm.c diff --git a/arch/x86/include/asm/kvm_para.h b/arch/x86/include/asm/kvm_para.h index 338119852512..2fa85481520b 100644 --- a/arch/x86/include/asm/kvm_para.h +++ b/arch/x86/include/asm/kvm_para.h @@ -6,6 +6,7 @@ #include #include #include +#include extern void kvmclock_init(void); @@ -34,6 +35,10 @@ static inline bool kvm_check_and_clear_guest_paused(void) static inline long kvm_hypercall0(unsigned int nr) { long ret; + + if (is_tdx_guest()) + return tdx_kvm_hypercall0(nr); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr) @@ -44,6 +49,10 @@ static inline long kvm_hypercall0(unsigned int nr) static inline long kvm_hypercall1(unsigned int nr, unsigned long p1) { long ret; + + if (is_tdx_guest()) + return tdx_kvm_hypercall1(nr, p1); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1) @@ -55,6 +64,10 @@ static inline long kvm_hypercall2(unsigned int nr, unsigned long p1, unsigned long p2) { long ret; + + if (is_tdx_guest()) + return tdx_kvm_hypercall2(nr, p1, p2); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2) @@ -66,6 +79,10 @@ static inline long kvm_hypercall3(unsigned int nr, unsigned long p1, unsigned long p2, unsigned long p3) { long ret; + + if (is_tdx_guest()) + return tdx_kvm_hypercall3(nr, p1, p2, p3); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3) @@ -78,6 +95,10 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, unsigned long p4) { long ret; + + if (is_tdx_guest()) + return tdx_kvm_hypercall4(nr, p1, p2, p3, p4); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4) diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index 1ca55d8e9963..e0b3ed9e262c 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -56,6 +56,16 @@ u64 __tdcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, /* Helper function used to request services from VMM */ u64 __tdvmcall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15, struct tdvmcall_output *out); +u64 __tdvmcall_vendor_kvm(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15, + struct tdvmcall_output *out); + +long tdx_kvm_hypercall0(unsigned int nr); +long tdx_kvm_hypercall1(unsigned int nr, unsigned long p1); +long tdx_kvm_hypercall2(unsigned int nr, unsigned long p1, unsigned long p2); +long tdx_kvm_hypercall3(unsigned int nr, unsigned long p1, unsigned long p2, + unsigned long p3); +long tdx_kvm_hypercall4(unsigned int nr, unsigned long p1, unsigned long p2, + unsigned long p3, unsigned long p4); #else // !CONFIG_INTEL_TDX_GUEST @@ -66,6 +76,35 @@ static inline bool is_tdx_guest(void) static inline void tdx_early_init(void) { }; +static inline long tdx_kvm_hypercall0(unsigned int nr) +{ + return -ENODEV; +} + +static inline long tdx_kvm_hypercall1(unsigned int nr, unsigned long p1) +{ + return -ENODEV; +} + +static inline long tdx_kvm_hypercall2(unsigned int nr, unsigned long p1, + unsigned long p2) +{ + return -ENODEV; +} + +static inline long tdx_kvm_hypercall3(unsigned int nr, unsigned long p1, + unsigned long p2, unsigned long p3) +{ + return -ENODEV; +} + +static inline long tdx_kvm_hypercall4(unsigned int nr, unsigned long p1, + unsigned long p2, unsigned long p3, + unsigned long p4) +{ + return -ENODEV; +} + #endif /* CONFIG_INTEL_TDX_GUEST */ #endif /* _ASM_X86_TDX_H */ diff --git a/arch/x86/kernel/tdcall.S b/arch/x86/kernel/tdcall.S index 81af70c2acbd..964bfd7fc682 100644 --- a/arch/x86/kernel/tdcall.S +++ b/arch/x86/kernel/tdcall.S @@ -11,6 +11,7 @@ * refer to TDX GHCI specification). */ #define TDVMCALL_EXPOSE_REGS_MASK 0xfc00 +#define TDVMCALL_VENDOR_KVM 0x4d564b2e584454 /* "TDX.KVM" */ /* * TDX guests use the TDCALL instruction to make @@ -198,3 +199,9 @@ SYM_FUNC_START(__tdvmcall) call do_tdvmcall retq SYM_FUNC_END(__tdvmcall) + +SYM_FUNC_START(__tdvmcall_vendor_kvm) + movq $TDVMCALL_VENDOR_KVM, %r10 + call do_tdvmcall + retq +SYM_FUNC_END(__tdvmcall_vendor_kvm) diff --git a/arch/x86/kernel/tdx-kvm.c b/arch/x86/kernel/tdx-kvm.c new file mode 100644 index 000000000000..c4264e926712 --- /dev/null +++ b/arch/x86/kernel/tdx-kvm.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0 + +static long tdvmcall_vendor(unsigned int fn, unsigned long r12, + unsigned long r13, unsigned long r14, + unsigned long r15) +{ + return __tdvmcall_vendor_kvm(fn, r12, r13, r14, r15, NULL); +} + +/* Used by kvm_hypercall0() to trigger hypercall in TDX guest */ +long tdx_kvm_hypercall0(unsigned int nr) +{ + return tdvmcall_vendor(nr, 0, 0, 0, 0); +} +EXPORT_SYMBOL_GPL(tdx_kvm_hypercall0); + +/* Used by kvm_hypercall1() to trigger hypercall in TDX guest */ +long tdx_kvm_hypercall1(unsigned int nr, unsigned long p1) +{ + return tdvmcall_vendor(nr, p1, 0, 0, 0); +} +EXPORT_SYMBOL_GPL(tdx_kvm_hypercall1); + +/* Used by kvm_hypercall2() to trigger hypercall in TDX guest */ +long tdx_kvm_hypercall2(unsigned int nr, unsigned long p1, unsigned long p2) +{ + return tdvmcall_vendor(nr, p1, p2, 0, 0); +} +EXPORT_SYMBOL_GPL(tdx_kvm_hypercall2); + +/* Used by kvm_hypercall3() to trigger hypercall in TDX guest */ +long tdx_kvm_hypercall3(unsigned int nr, unsigned long p1, unsigned long p2, + unsigned long p3) +{ + return tdvmcall_vendor(nr, p1, p2, p3, 0); +} +EXPORT_SYMBOL_GPL(tdx_kvm_hypercall3); + +/* Used by kvm_hypercall4() to trigger hypercall in TDX guest */ +long tdx_kvm_hypercall4(unsigned int nr, unsigned long p1, unsigned long p2, + unsigned long p3, unsigned long p4) +{ + return tdvmcall_vendor(nr, p1, p2, p3, p4); +} +EXPORT_SYMBOL_GPL(tdx_kvm_hypercall4); diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c index 5169f72b6b3f..721c213d807d 100644 --- a/arch/x86/kernel/tdx.c +++ b/arch/x86/kernel/tdx.c @@ -8,6 +8,10 @@ #include +#ifdef CONFIG_KVM_GUEST +#include "tdx-kvm.c" +#endif + static struct { unsigned int gpa_width; unsigned long attributes; -- 2.25.1