Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp4141376rdb; Thu, 28 Dec 2023 11:28:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IFxEJD281hTXdzNerg4i3X/mRha6zwqlIXWUkHPa/ULpSr1iHqBRMtN0jimKKfgIVCImf+B X-Received: by 2002:a05:6358:e485:b0:175:17c5:e0e6 with SMTP id by5-20020a056358e48500b0017517c5e0e6mr697837rwb.61.1703791707740; Thu, 28 Dec 2023 11:28:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703791707; cv=none; d=google.com; s=arc-20160816; b=uLGyfbcN3YBopnf8kzwebYN28c2z0x8/9q6rkuZ8mRaUHQKlbd5oFVMxR5l2ztphSy AEWlZFBPCM2jr13l+HFmOXpA6fUlFC124CgRM7L8onfy49A1cVc4VT3VrDuonWgrjJDj WT/tiViX65Ssa8FqCww3etrZoESXL/K0DJWh8ZdAgYE5+I7XNO76XDxdqo5oljZ06+HJ EMd86BsS1+3VnvaZJURlF2NB2pz7S9/3a49YrsjV054qkhRHNiuD14VEqdHxqPeVC0UG z/r5taP0CIVAlq5pUnyiWw0Rk0sjx19Q2kF4RqdhPIOQxbdbbhiugzb2/LviQ1GuIRYF EeyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=X6NHqoeKipmnRRrBtGGekIe1Ago4hx2yTydoB5DSv04=; fh=6RQVJhUadVlbCYQO8WbwFdcKs3+SddxIKFPo/gACb1Y=; b=XKA8q12Ty2+8/aP0Zslu74wIGhzFYwgBt0scO9GsI4Jjhw7Rd9ez+zMS7fdFhUoFmW 5kEKzW0RKotWPARYe40uQTnk+Ii75pPGXPtcRPCrwcK3uQojhoBj8MPLsXYyi22niTvf qQu3VZOO/XJVDSxr7MeP5hCR/nkr14BLQulubYTboIOcj1/7NYqaCtybS4MvZ6ks8UI4 agacV5LhWRMcVDtSZ7X6hXsH4UPM0Bo7lJ1nqz0cT7FmW503P2EQkinJy1uPFi/cUVdm F1kWJbEfgyhe1AZG29k4Ab4BtoRZGoi7Erg8WwO1keYBMZ3fIB92bZy/7XydqRkI4Mmq 5h6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=eEMZi1EL; spf=pass (google.com: domain of linux-kernel+bounces-12837-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12837-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id s40-20020a056a0017a800b006d9b4294509si7992729pfg.200.2023.12.28.11.28.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Dec 2023 11:28:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-12837-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=eEMZi1EL; spf=pass (google.com: domain of linux-kernel+bounces-12837-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12837-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 528B8B23F0A for ; Thu, 28 Dec 2023 19:27:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 16790125BD; Thu, 28 Dec 2023 19:24:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b="eEMZi1EL" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-oa1-f52.google.com (mail-oa1-f52.google.com [209.85.160.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD71E11CAA for ; Thu, 28 Dec 2023 19:24:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=broadcom.com Received: by mail-oa1-f52.google.com with SMTP id 586e51a60fabf-203fed05a31so4673917fac.0 for ; Thu, 28 Dec 2023 11:24:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1703791492; x=1704396292; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=X6NHqoeKipmnRRrBtGGekIe1Ago4hx2yTydoB5DSv04=; b=eEMZi1ELj49Ln0yTBgt7llYl1X3QSw+RwCpX890q3AKiaGOXJkDQlq3l5ai3xahFIU vO4360huoav1roNykaT7oVwQbdUDlZ3k9oKi6ooM/WB6JTwDlWaSQbBVwDG271JyfyJ1 sIMiU8dJ2XaBe5qBHo6VXRCiCmag2J+uoYrBg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703791492; x=1704396292; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=X6NHqoeKipmnRRrBtGGekIe1Ago4hx2yTydoB5DSv04=; b=gywOT/KJ68Ne0DQunVYiiyOlX/0LnToOt2FHVC1Czm+Gb56OgggE84BFtK5BLdF0Bu ub/LfXu4IMhfwAt+eSTtUzCBHTdAguYoEoO1qSkxCgRH+Pg7coKXISqitf9WeUCoBBdZ ZoZ5EX3CHVGJJ06iNMq7o7VWwuwbv5Z1SG7gumxeLdXDAu5SUd+Qfnp8fFwD+RjdMalC sfmBp01GpHpzjxeteqaY2YTONN9/61/bmDPUgwHfWwMh+chzYhDerz1K0VIGNenJKLbp fZLfKgNAHv0CfDXlFzBmmtv+X9Ua6ciOv8cSixPz3HrmYTXEepcRIgsQ7LLTwCk4+6FD dDPw== X-Gm-Message-State: AOJu0YybXyC/Vfr2WvGLW8lt/dEurmBl6lRqEUCUgBJydVvX2sIodsm5 h/N8c8xif7NJA8mscD1kBCqU+Zf+pikijruEyafzqj4KcX6SBHZFNubknSpUSDzvKKwY1owcHIP qYgcF9s48dQ/+WCHXQgqR9B95TZ1E8Q3f3OYHYEHoMjkqUNNA1r1ytYtY7iUtME7BfBSVQso3CZ H8bTlx9yApU8jP8Y67/aPLavk= X-Received: by 2002:a05:6870:9106:b0:204:792:8a55 with SMTP id o6-20020a056870910600b0020407928a55mr12289655oae.37.1703791491770; Thu, 28 Dec 2023 11:24:51 -0800 (PST) Received: from amakhalov-build-vm.eng.vmware.com ([128.177.82.146]) by smtp.gmail.com with ESMTPSA id k16-20020aa79990000000b006d9aa04574csm9522987pfh.52.2023.12.28.11.24.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Dec 2023 11:24:51 -0800 (PST) From: Alexey Makhalov To: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, bp@alien8.de, hpa@zytor.com, dave.hansen@linux.intel.com, mingo@redhat.com, tglx@linutronix.de Cc: x86@kernel.org, netdev@vger.kernel.org, richardcochran@gmail.com, linux-input@vger.kernel.org, dmitry.torokhov@gmail.com, zackr@vmware.com, linux-graphics-maintainer@vmware.com, pv-drivers@vmware.com, namit@vmware.com, timothym@vmware.com, akaher@vmware.com, jsipek@vmware.com, dri-devel@lists.freedesktop.org, daniel@ffwll.ch, airlied@gmail.com, tzimmermann@suse.de, mripard@kernel.org, maarten.lankhorst@linux.intel.com, horms@kernel.org, kirill.shutemov@linux.intel.com Subject: [PATCH v4 6/6] x86/vmware: Add TDX hypercall support Date: Thu, 28 Dec 2023 11:24:21 -0800 Message-Id: <20231228192421.29894-7-alexey.makhalov@broadcom.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20231228192421.29894-1-alexey.makhalov@broadcom.com> References: <20231228192421.29894-1-alexey.makhalov@broadcom.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Alexey Makhalov VMware hypercalls use I/O port, VMCALL or VMMCALL instructions. Add __tdx_hypercall path to support TDX guests. No change in high bandwidth hypercalls, as only low bandwidth ones are supported for TDX guests. Co-developed-by: Tim Merrifield Signed-off-by: Tim Merrifield Signed-off-by: Alexey Makhalov Reviewed-by: Nadav Amit --- arch/x86/include/asm/vmware.h | 79 +++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/vmware.c | 24 +++++++++++ 2 files changed, 103 insertions(+) diff --git a/arch/x86/include/asm/vmware.h b/arch/x86/include/asm/vmware.h index 84a31f579a30..3bd593c6591d 100644 --- a/arch/x86/include/asm/vmware.h +++ b/arch/x86/include/asm/vmware.h @@ -18,6 +18,12 @@ * arg2 - Hypercall command * arg3 bits [15:0] - Port number, LB and direction flags * + * - Low bandwidth TDX hypercalls (x86_64 only) are similar to LB + * hypercalls. They also have up to 6 input and 6 output on registers + * arguments, with different argument to register mapping: + * %r12 (arg0), %rbx (arg1), %r13 (arg2), %rdx (arg3), + * %rsi (arg4), %rdi (arg5). + * * - High bandwidth (HB) hypercalls are I/O port based only. They have * up to 7 input and 7 output arguments passed and returned using * registers: %eax (arg0), %ebx (arg1), %ecx (arg2), %edx (arg3), @@ -54,12 +60,61 @@ #define VMWARE_CMD_GETHZ 45 #define VMWARE_CMD_GETVCPU_INFO 68 #define VMWARE_CMD_STEALCLOCK 91 +/* + * Hypercall command mask: + * bits [6:0] command, range [0, 127] + * bits [19:16] sub-command, range [0, 15] + */ +#define VMWARE_CMD_MASK 0xf007fU #define CPUID_VMWARE_FEATURES_ECX_VMMCALL BIT(0) #define CPUID_VMWARE_FEATURES_ECX_VMCALL BIT(1) extern u8 vmware_hypercall_mode; +#define VMWARE_TDX_VENDOR_LEAF 0x1af7e4909ULL +#define VMWARE_TDX_HCALL_FUNC 1 + +extern unsigned long vmware_tdx_hypercall(unsigned long cmd, + struct tdx_module_args *args); + +/* + * TDCALL[TDG.VP.VMCALL] uses %rax (arg0) and %rcx (arg2). Therefore, + * we remap those registers to %r12 and %r13, respectively. + */ +static inline +unsigned long vmware_tdx_hypercall_args(unsigned long cmd, unsigned long in1, + unsigned long in3, unsigned long in4, + unsigned long in5, + uint32_t *out1, uint32_t *out2, + uint32_t *out3, uint32_t *out4, + uint32_t *out5) +{ + unsigned long ret; + + struct tdx_module_args args = { + .rbx = in1, + .rdx = in3, + .rsi = in4, + .rdi = in5, + }; + + ret = vmware_tdx_hypercall(cmd, &args); + + if (out1) + *out1 = args.rbx; + if (out2) + *out2 = args.r13; + if (out3) + *out3 = args.rdx; + if (out4) + *out4 = args.rsi; + if (out5) + *out5 = args.rdi; + + return ret; +} + /* * The low bandwidth call. The low word of %edx is presumed to have OUT bit * set. The high word of %edx may contain input data from the caller. @@ -87,6 +142,10 @@ unsigned long vmware_hypercall1(unsigned long cmd, unsigned long in1) { unsigned long out0; + if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) + return vmware_tdx_hypercall_args(cmd, in1, 0, 0, 0, + NULL, NULL, NULL, NULL, NULL); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0) : [port] "i" (VMWARE_HYPERVISOR_PORT), @@ -105,6 +164,10 @@ unsigned long vmware_hypercall3(unsigned long cmd, unsigned long in1, { unsigned long out0; + if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) + return vmware_tdx_hypercall_args(cmd, in1, 0, 0, 0, + out1, out2, NULL, NULL, NULL); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0), "=b" (*out1), "=c" (*out2) : [port] "i" (VMWARE_HYPERVISOR_PORT), @@ -124,6 +187,10 @@ unsigned long vmware_hypercall4(unsigned long cmd, unsigned long in1, { unsigned long out0; + if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) + return vmware_tdx_hypercall_args(cmd, in1, 0, 0, 0, + out1, out2, out3, NULL, NULL); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0), "=b" (*out1), "=c" (*out2), "=d" (*out3) : [port] "i" (VMWARE_HYPERVISOR_PORT), @@ -143,6 +210,10 @@ unsigned long vmware_hypercall5(unsigned long cmd, unsigned long in1, { unsigned long out0; + if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) + return vmware_tdx_hypercall_args(cmd, in1, in3, in4, in5, + NULL, out2, NULL, NULL, NULL); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0), "=c" (*out2) : [port] "i" (VMWARE_HYPERVISOR_PORT), @@ -165,6 +236,10 @@ unsigned long vmware_hypercall6(unsigned long cmd, unsigned long in1, { unsigned long out0; + if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) + return vmware_tdx_hypercall_args(cmd, in1, in3, 0, 0, + NULL, out2, out3, out4, out5); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0), "=c" (*out2), "=d" (*out3), "=S" (*out4), "=D" (*out5) @@ -186,6 +261,10 @@ unsigned long vmware_hypercall7(unsigned long cmd, unsigned long in1, { unsigned long out0; + if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) + return vmware_tdx_hypercall_args(cmd, in1, in3, in4, in5, + out1, out2, out3, NULL, NULL); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0), "=b" (*out1), "=c" (*out2), "=d" (*out3) : [port] "i" (VMWARE_HYPERVISOR_PORT), diff --git a/arch/x86/kernel/cpu/vmware.c b/arch/x86/kernel/cpu/vmware.c index 3aa1adaed18f..a88dcd25aefe 100644 --- a/arch/x86/kernel/cpu/vmware.c +++ b/arch/x86/kernel/cpu/vmware.c @@ -428,6 +428,30 @@ static bool __init vmware_legacy_x2apic_available(void) (eax & BIT(VCPU_LEGACY_X2APIC)); } +#ifdef CONFIG_INTEL_TDX_GUEST +unsigned long vmware_tdx_hypercall(unsigned long cmd, + struct tdx_module_args *args) +{ + if (!hypervisor_is_type(X86_HYPER_VMWARE)) + return ULONG_MAX; + + if (cmd & ~VMWARE_CMD_MASK) { + pr_warn_once("Out of range command %lx\n", cmd); + return ULONG_MAX; + } + + args->r10 = VMWARE_TDX_VENDOR_LEAF; + args->r11 = VMWARE_TDX_HCALL_FUNC; + args->r12 = VMWARE_HYPERVISOR_MAGIC; + args->r13 = cmd; + + __tdx_hypercall(args); + + return args->r12; +} +EXPORT_SYMBOL_GPL(vmware_tdx_hypercall); +#endif + #ifdef CONFIG_AMD_MEM_ENCRYPT static void vmware_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs) -- 2.39.0