Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp229958lqc; Thu, 7 Mar 2024 16:01:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVUdkFAVEdPHPizogkPQZ5PBmY/0YhoGl4P2hsN2ZrjwZ/358erFEonezhP4N+TYC+Kgk25Vn5CEz561yPS2yTr27D9SVNcDDc1ldvdIA== X-Google-Smtp-Source: AGHT+IGbUCcszsrHzdgaX4dIMbBz+Q+q1QmgG9Rtf9jqCpiQdijyT2YwTsBlg6Xd/H/WmjZPVKR8 X-Received: by 2002:ac2:43ba:0:b0:513:25c6:e98e with SMTP id t26-20020ac243ba000000b0051325c6e98emr2158767lfl.1.1709856110125; Thu, 07 Mar 2024 16:01:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709856110; cv=pass; d=google.com; s=arc-20160816; b=p6U03lgim/t86kQO1x2r4qpX2yj/NWGldmq/pHTpVO4mN02CLiGwIkzcbS0fN08JR9 /g2tvZzz4tuWAjJKA1oio0uBE+ZLLjHNn4WfOFzq3e2Qp3PeRJI5nrHgLJQX+b2mMvOn smZy079Kz+qLhDkqHjs0r9f9h2uMuMxonIszHRdOFidK4E+ngcmekevBlWVloyulaDOc cN47JoO3qTiA9MebNRFeIqkC9q9ERrQzmNlme9Ko7GKXzaANsGVA3tJFEpstm1WAdBo5 JJMJJ1v6qlH/KRtXoWQC51j3g9f0glsSOdcaCK1LSKvU8yJJQ4svsWzCPGfE52p0Zo3P o3Fw== ARC-Message-Signature: i=2; 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=6RtHyaEuKpL9REXgkKi1O83Oy76Ia+vzTTF2nxsG3Eg=; fh=6RQVJhUadVlbCYQO8WbwFdcKs3+SddxIKFPo/gACb1Y=; b=EpULtwO3BUfa+E/ZPnC/R3LDWlt6OdDviD0ssGeY4At02JLBZ2cSQno58h7VPvJp97 bupVEwQv3jciRTACCQobUE6GoTYbrvMmKk7ompYtJBwd9I2vrDQRgem4I3xrkuNpcS4p vm2A2iF7wZd7uP1o0jUoAhO5xk4sp/vREg6iCfofBHrzF9kCBcxRbnwGTCvwGkEp6uoB A5L8MIjm/qwWBaeFR9ECtGmUg7CJhGJQ2Q+rcv5s74veS/K4GMJVKb6UK5Mybh99g9qx QOvh+mj2Z+gI85x33bouTHIot02TXuSJjqhRAxHTLPySmJF2SKh1t/ICgCJhaW6cKKAP 1pcw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b="S/v5Le/c"; arc=pass (i=1 dkim=pass dkdomain=broadcom.com dmarc=pass fromdomain=broadcom.com); spf=pass (google.com: domain of linux-kernel+bounces-96258-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96258-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id n21-20020a170906841500b00a4522c12526si4355835ejx.622.2024.03.07.16.01.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 16:01:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-96258-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b="S/v5Le/c"; arc=pass (i=1 dkim=pass dkdomain=broadcom.com dmarc=pass fromdomain=broadcom.com); spf=pass (google.com: domain of linux-kernel+bounces-96258-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96258-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 9BA361F26181 for ; Thu, 7 Mar 2024 21:32:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0851D143C73; Thu, 7 Mar 2024 21:30:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b="S/v5Le/c" Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 6134213D31D for ; Thu, 7 Mar 2024 21:30:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709847020; cv=none; b=d8zKRV4qvx8/PK55KoLM7HF+3e7viTlBjFa8gpt9fwNrFA3XkmEOI0ifOhV/m/O5+TFI3RN6sDdQ+DvOyanj1eCkGj8T5QrcESJYJ6bV+ArkeoU0zFdJAbwOHFwhJ9wuhMjAfZvJkRfdbHxXZ1GtjTH3748NXIk1TetWiSpq8Ac= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709847020; c=relaxed/simple; bh=uvXRQzVDVySpdmyTqAw3zeM7QrwQGclRjrSl2B8XVt8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=U7AI1UrtbPFlKqPX8pIH7SF90/FYmdqrZF0G2n2/i1BrTsyx1ZjDjCKJvWKMEAh7MieUpyRGnjgsPc0hnq+GCoWfT5Uct7WoTQIADuAAzrFgxOw/vm+BO5Md941hrQXsWRdNAvGtU57OElVt2dj0PeUAko3iwz5uK6mFqqpUno4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com; spf=fail smtp.mailfrom=broadcom.com; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b=S/v5Le/c; arc=none smtp.client-ip=209.85.210.178 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-pf1-f178.google.com with SMTP id d2e1a72fcca58-6e64cdf08e3so140698b3a.3 for ; Thu, 07 Mar 2024 13:30:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1709847015; x=1710451815; 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=6RtHyaEuKpL9REXgkKi1O83Oy76Ia+vzTTF2nxsG3Eg=; b=S/v5Le/cHh5R8A1+fOxYUBYFbXNCx+mbKvfgDE2JPp5nQiWMkyzxKJVNlEI0Jw/wqw jaPu+0qu3Fnv+w+MhjdLLqj15UUuuTVw0AUiZp4FlBhUEEMmd9gLOOrXnMjyKoJdGJVb krGHi5cIIOW9gJ/mn9D8JpqshWX37cUtYVLlQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709847015; x=1710451815; 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=6RtHyaEuKpL9REXgkKi1O83Oy76Ia+vzTTF2nxsG3Eg=; b=PtsIzEzAC+KpcOE6G6RMfVy87eW0FyCsGUpAvw82QXwDq9ygNVAQ7Z48tyOQV8bLnW px86Snaa1lw8NR3xWVLm62wGWs3MF9okatk8X2JthZMhniMM5eOpgC6KIP9+nlKuMqr7 bpdkoc/rldSDqkG7t7yk7KZ7tV/bdySgV+t4nMGkR5R7kAb6w9t/KJCYZyy786UfLWeU UC58380nkSqfauGr1ikBg1FSKoN8BZETYi4WFIdn2azW1FG0OoO43WuXUQuWujX7OTWG MliWWbfmBV0lsnaUB5M1VGfdU2lLRqzNDcoqJWEqibbYPLOPxEkvqIaz+KZ8agDunsA2 XsLg== X-Gm-Message-State: AOJu0Yzs/1qJOd07ZOJDiQ4o3H6XXXv2w0+kg8wwSbLd8PhMTtqSMzUw 30bSB4iTrWHdrstzUGfffruCQkoq8s3+I/UIX0sYQL2rQpn/yo90dt+6rySMxy29TZV5dYK6Q+J ziy12nbus9Mui5c4I9FOfZDidA2HhIVQWHX1VNxRdVTs87xtyjwchaNN1oUbagNThdrx0aJNcGg tEVvIRH0x2f3ymN1qvQspB3CmMNOx1JXPjhOM8YUDpOk3mZOExjCdi X-Received: by 2002:a05:6a20:3c88:b0:1a0:ceb1:8f65 with SMTP id b8-20020a056a203c8800b001a0ceb18f65mr9795630pzj.4.1709847015125; Thu, 07 Mar 2024 13:30:15 -0800 (PST) Received: from amakhalov-build-vm.eng.vmware.com ([64.186.27.43]) by smtp.gmail.com with ESMTPSA id y12-20020a17090abd0c00b00299db19a81asm1991021pjr.56.2024.03.07.13.30.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 13:30:14 -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 v7 7/7] x86/vmware: Add TDX hypercall support Date: Thu, 7 Mar 2024 13:29:49 -0800 Message-Id: <20240307212949.4166120-8-alexey.makhalov@broadcom.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20240307212949.4166120-1-alexey.makhalov@broadcom.com> References: <20240307212949.4166120-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 Acked-by: Kirill A. Shutemov Acked-by: Dave Hansen --- arch/x86/include/asm/vmware.h | 46 +++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/vmware.c | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/arch/x86/include/asm/vmware.h b/arch/x86/include/asm/vmware.h index 84a31f579a30..cc79c14d1ac2 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,28 @@ #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, + 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); + /* * 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 +109,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(cmd, in1, 0, 0, 0, + NULL, NULL, NULL, NULL, NULL); + asm_inline volatile (VMWARE_HYPERCALL : "=a" (out0) : [port] "i" (VMWARE_HYPERVISOR_PORT), @@ -105,6 +131,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(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 +154,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(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 +177,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(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 +203,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(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 +228,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(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..41309f03cb94 100644 --- a/arch/x86/kernel/cpu/vmware.c +++ b/arch/x86/kernel/cpu/vmware.c @@ -428,6 +428,58 @@ static bool __init vmware_legacy_x2apic_available(void) (eax & BIT(VCPU_LEGACY_X2APIC)); } +#ifdef CONFIG_INTEL_TDX_GUEST +/* + * TDCALL[TDG.VP.VMCALL] uses %rax (arg0) and %rcx (arg2). Therefore, + * we remap those registers to %r12 and %r13, respectively. + */ +unsigned long vmware_tdx_hypercall(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) +{ + struct tdx_module_args args; + + if (!hypervisor_is_type(X86_HYPER_VMWARE)) { + pr_warn_once("Incorrect usage\n"); + return ULONG_MAX; + } + + if (cmd & ~VMWARE_CMD_MASK) { + pr_warn_once("Out of range command %lx\n", cmd); + return ULONG_MAX; + } + + args.rbx = in1; + args.rdx = in3; + args.rsi = in4; + args.rdi = in5; + args.r10 = VMWARE_TDX_VENDOR_LEAF; + args.r11 = VMWARE_TDX_HCALL_FUNC; + args.r12 = VMWARE_HYPERVISOR_MAGIC; + args.r13 = cmd; + args.r15 = 0; /* CPL */ + + __tdx_hypercall(&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 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