Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2640789lqt; Mon, 22 Apr 2024 17:59:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXPU7pXCMnlYRGxnRSSmr3GRfsNq9mKQczubg3Bn37SyouUdKwqt867N89D41af4LlOMrjvVgSMIYCoAWAUHMGRrIhin6GnlQKJjYFquQ== X-Google-Smtp-Source: AGHT+IEqPa4qdAESt/orjZFin+4RDpqZsvAnBdeMB3Am8EeS9fNsq1Mu4F8P6td/E0FusHSaOvK9 X-Received: by 2002:a05:6a21:350f:b0:1ad:878:5006 with SMTP id zc15-20020a056a21350f00b001ad08785006mr6990103pzb.14.1713833961431; Mon, 22 Apr 2024 17:59:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713833961; cv=pass; d=google.com; s=arc-20160816; b=CaKLffA0XluzY36rgFwnMDzkANckukOGqhAQfDw+lqJ//dzotAbe3mtvAw9VWs/qZ6 Mojp1Wn5tgTlPnd9oHBHFNyAlk4EKbf6Mc6x+Tl49Nixkvb1NdgTRl3yIUFSkukq03ok 1hplSHZ+T6iVp78kXL7qHi/3GZzNi9IJdYavR56FCdzzamHnL//MljdAYikBuroM+4AS ChiN0diOIjxHO4rS184OITdqVSRGmzqi7NWt3jTdQBfIA7YYdKvs5HuWtRD4hbzyz1OL LHx3+tiI92NIspa+WEApCs0AwBryAYiDIDr8ye8kMXIIYhKpGP+osbnZ4iZbW9RspUaC cLYQ== 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=8514+b4qmh7LFcxQkUfD1+echxo4Pf00IaoPqebGK24=; fh=lZzfmW9RNLnz5mQiEzXwsK7sDaTj0vmHcnmM487ejgU=; b=KjF9tgYvLIh0+9HbhXEZ9FO+soaWgkkrI8RHggqH4SCpMv2IzOQI/Ns955PJoEjOnc opEcffbfp/jDsSTq/PBrICzhPtztccdPZPh4o4m4DqgoXNkNsErsbsyaXKNSp6KVKWcY Agc3GLKCEEDtRSAef6sZnOv72Ai+6E9I/Iiaxq5JSjjzXgxwnkmkPqd3KEL9I7TaGVsn oLZ5fB9guf8j2t9uBOis0HMPytSqeqXDOdNRiqCvKi+kLWeISmgLJ+iBsLNskUEaHbVp nxJr5cqtsx//P8NFkOjdHxZqVBLYQYM0PGW9sbs50HogLvdqw1gVY84ojfKa3jTZAIhS 10kw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b="Eb96s0t/"; arc=pass (i=1 dkim=pass dkdomain=broadcom.com dmarc=pass fromdomain=broadcom.com); spf=pass (google.com: domain of linux-kernel+bounces-154169-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154169-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. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id p30-20020a056a000a1e00b006e69f482eccsi8768367pfh.316.2024.04.22.17.59.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 17:59:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-154169-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b="Eb96s0t/"; arc=pass (i=1 dkim=pass dkdomain=broadcom.com dmarc=pass fromdomain=broadcom.com); spf=pass (google.com: domain of linux-kernel+bounces-154169-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154169-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 1A081B232B5 for ; Mon, 22 Apr 2024 23:06:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0B94E1A38D5; Mon, 22 Apr 2024 22:57:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b="Eb96s0t/" Received: from mail-oa1-f48.google.com (mail-oa1-f48.google.com [209.85.160.48]) (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 4CB151A0B03 for ; Mon, 22 Apr 2024 22:57:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713826648; cv=none; b=pFy87nc3t0Xz9oQHOI2MWZKfvLV9AM+pMRMVnW7fzgMDZkPa5rI6u45cY9CJbCHLVWBsEYaMcbbqrANMZwIc4cVZtPJvc9A8KH5aXPMj485ZPpZ/Eqi+A5nXovJ+qXaKwcB0sGe34gyBPbLxrHV3vEb1cVvamzjLrz+4f0Gt4+c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713826648; c=relaxed/simple; bh=3qvMJrIqIc2nlwUosocTCDA/Vwfsx5mP93+A7oI7OSE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=c+//3sKdH9yXv1uyiEWwG2TynKiBHnUHGD9yiheXRti8ZnWtl0WSBGILn8LTZeyqCTPgapvZkNYIaus+4/xmwo32KMhRKOcPQ68be71qSSMVkHSYMG+D5CnteI4RqYk0buzO6LsmZmAapvlu3aTTonWXL+eepKw98ZiYvOdv0vY= 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=Eb96s0t/; arc=none smtp.client-ip=209.85.160.48 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-f48.google.com with SMTP id 586e51a60fabf-22efc6b8dc5so3063985fac.0 for ; Mon, 22 Apr 2024 15:57:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1713826646; x=1714431446; 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=8514+b4qmh7LFcxQkUfD1+echxo4Pf00IaoPqebGK24=; b=Eb96s0t/DpZMlBQdZqFBD002UfIhytpq7l+JaGAHo8hBwf/DwXGZSuIakdasH1Hkh8 EmLXBbTxQ4aTBiNQ617l3ysC6wWtmwYSxCkrn2EVy7uNI0MnoZciQZbggcN3wMxmPdO6 +ktzX2snjSH0WsAARC8Jp1bWXl19PHY+9sWJQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713826646; x=1714431446; 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=8514+b4qmh7LFcxQkUfD1+echxo4Pf00IaoPqebGK24=; b=N7UgnY/ijaDbY6bdob1OgqRF8pitCKVRTB1E3RvcNpbZMCxG8RhW217Iljwi9tn/It ScXHMSYscHfseWY1N6jsXfSRUxzHEF1BskkrNqr3Ji5Gfjcy7g4N3Km5484bwLAUp3JI AD3fu0GQywE9Effz731Tet0pZKE9rnEpoyh6s7pBR3QAT5SGWqpqYXFpwRw+7MicKvtE 5nvfAKSqNvfbG8pN1py9yk+qBkljlnalRIQp5Fc1d9rwJHhxHyFqQxIeUQz5XJs/M+M8 XYgQa8IXiLm8EFoE6HleFruFGR5BWqzFFT4pG+SG/4YXL8XCztgMcg7PTSqeVc6M/bcy 6Qqg== X-Gm-Message-State: AOJu0YyBnuXLLN551zxPkqC5KnPoGKZAb++Urx4sNZlIXVx6YJPAi0SE SzYCoN5pXYSvyLMQ+IV9c4hTtsH5p1n5uhdQx0c43rJyntO+vgoyNSZQ8IkITDjVdB5omW0ouc1 idw+AmFTdQzT0xljXPNofeOKv2J4QZnM7dABuGZO416s+skLhyqlfbWav00/oGIwPH6fZQMYkVw 8oeIe7DBPKQdtQMPxTBHwp6jkj3Xiiyodr2U3ppj3zK9LEug3UH4MXxVw= X-Received: by 2002:a05:6870:2114:b0:22e:7ba2:ced4 with SMTP id f20-20020a056870211400b0022e7ba2ced4mr13791986oae.18.1713826645778; Mon, 22 Apr 2024 15:57:25 -0700 (PDT) Received: from amakhalov-build-vm.eng.vmware.com ([128.177.82.146]) by smtp.gmail.com with ESMTPSA id e131-20020a636989000000b005e43cce33f8sm8093597pgc.88.2024.04.22.15.57.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 15:57:25 -0700 (PDT) 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, timothym@vmware.com, akaher@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, Alexey Makhalov , Tim Merrifield , Nadav Amit Subject: [PATCH v8 7/7] x86/vmware: Add TDX hypercall support Date: Mon, 22 Apr 2024 15:56:56 -0700 Message-Id: <20240422225656.10309-8-alexey.makhalov@broadcom.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20240422225656.10309-1-alexey.makhalov@broadcom.com> References: <20240422225656.10309-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 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