Received: by 2002:a05:7412:d008:b0:f9:6acb:47ec with SMTP id bd8csp202830rdb; Tue, 19 Dec 2023 14:00:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IFXn+wEraGtO1VTFvxPztSKpGc4zv8VBL8r9ULQG+0zDRjFEkHL9+aupv+/+W2eTshJIIsr X-Received: by 2002:a05:620a:1726:b0:77e:fbba:646a with SMTP id az38-20020a05620a172600b0077efbba646amr2825042qkb.73.1703023253875; Tue, 19 Dec 2023 14:00:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703023253; cv=none; d=google.com; s=arc-20160816; b=jbQoPmzh92/ZR3COMcU0pY2++z3UkSde9vQUb454k2xoHjz6Z5BwepGFC3bgNvjjwZ jdpYeqEGGG1tBFaOSdh7oP6FnKthI2VxAfgakzANxi8OpuUT1ZF9yoZoKtND53rvjRCJ XmjlyLWnfonW8RgX1rENrW58fOFeCiDDAV4/wNt08MPuWOPsplX3EmNMd3uZ82aK5j4V QpCYVVmJebTkGjfkEUYMVRwZVQk9qR4FdKFAws8APMdck8rLFN0TkicddQ4aalLa43Qn bcm6c3dg1pYwzr/wi5WHqDbmYzQbtOldkSl9oQRrQdBIXxI2mxf5HvCSz5UFqcALNiyL xb2A== 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=nM0koGqiZNSpnJMYJB5q6p5i94HTaqJ4gpyX8NFfI6c=; fh=6RQVJhUadVlbCYQO8WbwFdcKs3+SddxIKFPo/gACb1Y=; b=t1Ij8yjHqYrRzKXXcz809aX1IhUiGcXXIGZEMhKvR6WV4SuFqphPFKED8jPKVx+/qS v6/7Vi5il2HYwRZjdNyzUXS+rTZdK1PpGWo0VwrZ8HBAslFO0HeapguEE2wOjQQvGYib bcwlSPp23p+SFCwJxDUOlQZXSUXm/ToEBR/ojNxXCrCBQ8QesF3G5K+AZ8HTD8mgAm1s MUN9LCWT7N2dDR0/5csI+K7oOLQICfC3vrmjW1CnkVNiSrlM0EOIZW1CbAXcCeQa8X/X wshg3Cta9JHUAF0eBDUyKzzz6lgwnUtddGtc6+fTAH1OHMhxd2taMpep7tPkHLCOqLw7 Qg9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=fRDX8hck; spf=pass (google.com: domain of linux-kernel+bounces-6000-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-6000-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 13-20020a05620a048d00b0077effeed954si1441275qkr.115.2023.12.19.14.00.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 14:00:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-6000-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=fRDX8hck; spf=pass (google.com: domain of linux-kernel+bounces-6000-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-6000-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 63D6B1C24E60 for ; Tue, 19 Dec 2023 22:00:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0009C41C73; Tue, 19 Dec 2023 21:58:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b="fRDX8hck" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 D4E3141763 for ; Tue, 19 Dec 2023 21:58:18 +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-pl1-f175.google.com with SMTP id d9443c01a7336-1d075392ff6so972505ad.1 for ; Tue, 19 Dec 2023 13:58:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1703023098; x=1703627898; 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=nM0koGqiZNSpnJMYJB5q6p5i94HTaqJ4gpyX8NFfI6c=; b=fRDX8hckiFwYV1zT2iJHGfNFvvok3NPoYrr3NtK5ZSOIlGIP8kl/EcLArXAB1T45wE pHFlqm4BGV1B53r9mjCozIF+J2w2RtgA5JRfX4MlQmt40ZTH5k/OoGMP6HqFGnAHc9dP chswhawTCKLU7+nxTSH85SrV6EddiSc/JUsjE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703023098; x=1703627898; 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=nM0koGqiZNSpnJMYJB5q6p5i94HTaqJ4gpyX8NFfI6c=; b=WEFYJ9i+5+dGoYBtnwGmyDdD/eKmt6Shz1BGevFtS67070bnH2I1htIr+MJ2DM+iy7 /UmysBmnxSxoDUUE3S0ODH1XAWwEgw4z9r7bDTxLn1OJ7RYD04ERaYsvLJkkkBMipxvk +aI80WE3OXBwNQKBFzDXlbcsKGSbT9zDa157I60XKCD2I8P6r1Ksrk/OwWKqUaJFln4R 7ldWozX1Ci6Izz4qt4c86lXzTLFOmuBedPilKwC6txkVv3p7MykW2j4513GFhTxkwoSo elNmEkiP66O1iCqZobUXGZEG0LHHp7Rrr0hQJuU1SlQRn//vN4cboLi5Sg/Zw157brg3 KZNg== X-Gm-Message-State: AOJu0Yxe2ZKwF73HfRmBnk9zV6gJZ1SyWvbEcGnn/fuz9/RmXaCQRlKW kK4/zivqx+L3yu/XaTQZH1/OAIWnQmlJxL+qPTsOHJ2zJDVLoE8ftZwarZmRr1OB/pvsZMgNW95 /t8qq2Cw94gY5vvf8ULlBi0WQgToekwcoGrvHvBxfN9wdotJ7qL1ZLuNK+Rp4ARWK8VPh+CYh+j SnUcJoD28xwrqZ+mu61O8= X-Received: by 2002:a17:903:238e:b0:1d3:e694:c20 with SMTP id v14-20020a170903238e00b001d3e6940c20mr1556985plh.43.1703023097808; Tue, 19 Dec 2023 13:58:17 -0800 (PST) Received: from amakhalov-build-vm.eng.vmware.com ([128.177.82.146]) by smtp.gmail.com with ESMTPSA id v1-20020a170902e8c100b001d3aa7604c5sm5356476plg.0.2023.12.19.13.58.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 13:58:17 -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 v3 6/6] x86/vmware: Add TDX hypercall support Date: Tue, 19 Dec 2023 13:57:51 -0800 Message-Id: <20231219215751.9445-7-alexey.makhalov@broadcom.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20231219215751.9445-1-alexey.makhalov@broadcom.com> References: <20231219215751.9445-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 | 83 +++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/vmware.c | 24 ++++++++++ 2 files changed, 107 insertions(+) diff --git a/arch/x86/include/asm/vmware.h b/arch/x86/include/asm/vmware.h index 719e41260ece..cad6f5b371a8 100644 --- a/arch/x86/include/asm/vmware.h +++ b/arch/x86/include/asm/vmware.h @@ -34,12 +34,65 @@ #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), while the use of + * rbp (arg6) is discouraged by the TDX specification. Therefore, we + * remap those registers to r12, r13 and r14, respectively. + */ +static inline +unsigned long vmware_tdx_hypercall_args(unsigned long cmd, unsigned long in1, + unsigned long in3, unsigned long in4, + unsigned long in5, unsigned long in6, + uint32_t *out1, uint32_t *out2, + uint32_t *out3, uint32_t *out4, + uint32_t *out5, uint32_t *out6) +{ + unsigned long ret; + + struct tdx_module_args args = { + .rbx = in1, + .rdx = in3, + .rsi = in4, + .rdi = in5, + .r14 = in6, + }; + + 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; + if (out6) + *out6 = args.r14; + + 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. @@ -67,6 +120,11 @@ 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, 0, + NULL, NULL, NULL, + NULL, NULL, NULL); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0) : [port] "i" (VMWARE_HYPERVISOR_PORT), @@ -85,6 +143,11 @@ 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, 0, + out1, out2, NULL, + NULL, NULL, NULL); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0), "=b" (*out1), "=c" (*out2) : [port] "i" (VMWARE_HYPERVISOR_PORT), @@ -104,6 +167,11 @@ 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, 0, + out1, out2, out3, + NULL, NULL, NULL); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0), "=b" (*out1), "=c" (*out2), "=d" (*out3) : [port] "i" (VMWARE_HYPERVISOR_PORT), @@ -123,6 +191,11 @@ 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, 0, + NULL, out2, NULL, + NULL, NULL, NULL); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0), "=c" (*out2) : [port] "i" (VMWARE_HYPERVISOR_PORT), @@ -145,6 +218,11 @@ 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, 0, + NULL, out2, out3, + out4, out5, NULL); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0), "=c" (*out2), "=d" (*out3), "=S" (*out4), "=D" (*out5) @@ -166,6 +244,11 @@ 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, 0, + out1, out2, out3, + NULL, 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..ef07ab7a07e1 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 0; + + if (cmd & ~VMWARE_CMD_MASK) { + pr_warn("Out of range command %x\n", cmd); + return 0; + } + + 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