From nobody Sun Sep 14 14:53:51 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 8567A2BDC20 for ; Thu, 4 Sep 2025 06:55:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756968936; cv=none; b=IGkJg0ieFQDipw8sDE3aQ1cIG7DP0lgIy8jKvkorKipA2HEPzz+JZZ0ACScW0hWAIQVPjbs4ZU8TH0d6bXq8hC5RZsnsB6mANJWVxEzksrPGQNIqCgugHEF/zD/dN+xBiGesm4TIrtyNMUAX4bjcZezKOM4Ca/gdC1p9ojQe3oI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756968936; c=relaxed/simple; bh=82imUpUGSrASE3Ews8yZjglaw3mYQKnEEacvVHA3ZMU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VupNnaESsmA+t0rP/oAexTUxYMnZ13JarcqspCLoQeCGv3Jqsl0okOup1ErPBVqIhG6m3mTYl/mNsBHWjx2WEyVGNlPwQM2P0TJYVG3yjCwDTTPIMaeep6YLOsq/Ds3sXE5D/coAJb54cVb/41ga6Gl+UWdWefas6xu/5b3ymqo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sagis.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=MHtgfCZR; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sagis.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MHtgfCZR" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-329dbf4476cso553499a91.1 for ; Wed, 03 Sep 2025 23:55:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1756968934; x=1757573734; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=gEbqVx1XUrmfHL/bfo75/bOQleA+IfiyOTZneZaFJoQ=; b=MHtgfCZRlzzy+B1FnCuKz64C5x++VmiPA7BvlutupPDVQBi+P9SgS6K+HLsBkwrNJX IL0G9dSoSofxv3OI+ebB/A2xcLUj8nbROUmoM7+g6fPxJr9BKniBy4/62oDFhDnOz0EY iRlg9tG//Q+V1+zcLb2qmY2Je44Tse7ojTRfUqKVZZZrUwXTW1Bt6xNWcDo6LeR6Oevu Y9+DixkzAJCvtur0DjGjXq4xqTmGP1BCCcQ5MCYjQ4ogW4RXO0WZ7FX0AkIB9JePFqW9 uqtvP8bBzF5nwvGd0MjDQ8F/tuaoRUDsjydrimq8rhcE7nZ7TErQ6d/NF2TFuJTmiTSx JtNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756968934; x=1757573734; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gEbqVx1XUrmfHL/bfo75/bOQleA+IfiyOTZneZaFJoQ=; b=lQPC50rMak7gIOXnNJ5ygrNLVEAJZ+9bLUdu3FnDiPkkuUunj7ZuvQuWiO1IGD5VYb msiuV/IemrZBZNH43HIPnNgf+H1+zfcKCzc7Mzy83guj99mItOOi/RR2ANmKag2/W18Z ppAKYheOO0TnVNtrBEReNaL0j9punZk3CXivD+QoS/NzC0KpMO6QCDfyzpLFKiQRjMzA q+4Tsuh9CihI7b2epuKe2tsC/gStXBo6h83QLFNfaIzLOAoRb2xGVi95mxPGTe+OofuO mDDfR6KS9R0pj6AmQ3fb1cOkBTOHyke2snJlNe9K4SuPAyAbpI2BH7UIraXvejQy+658 Hjbg== X-Gm-Message-State: AOJu0Yx8Kdu+bObAqixCfZTKOx6nsHpTE6UNQXnlIeP5AhhVyasIhGMH gytF6D73vAlRR6lRdQ+1/93RWbeV1w6u8EWMxAKguIJTUuR4Aad2ItqhWeTGHYam4hV2GdCB/Je qMQ== X-Google-Smtp-Source: AGHT+IHxg5XX+QGRvB3eVe6BPmD4a7wWjsiBO7i2k5ga/0nig7c6kMIoh8dnmBhJcA/htRSJV7twgBolAg== X-Received: from pjn16.prod.google.com ([2002:a17:90b:5710:b0:32b:58d1:a610]) (user=sagis job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:384c:b0:329:e708:c88e with SMTP id 98e67ed59e1d1-329e708c9fdmr12000946a91.20.1756968933985; Wed, 03 Sep 2025 23:55:33 -0700 (PDT) Date: Wed, 3 Sep 2025 23:54:50 -0700 In-Reply-To: <20250904065453.639610-1-sagis@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250904065453.639610-1-sagis@google.com> X-Mailer: git-send-email 2.51.0.338.gd7d06c2dae-goog Message-ID: <20250904065453.639610-21-sagis@google.com> Subject: [PATCH v10 20/21] KVM: selftests: Add ucall support for TDX From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Paolo Bonzini , Shuah Khan , Sean Christopherson , Ackerley Tng , Ryan Afranji , Andrew Jones , Isaku Yamahata , Erdem Aktas , Rick Edgecombe , Sagi Shahar , Roger Wang , Binbin Wu , Oliver Upton , "Pratik R. Sampat" , Reinette Chatre , Ira Weiny , Chao Gao , Chenyi Qiang Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ackerley Tng ucalls for non-Coco VMs work by having the guest write to the rdi register, then perform an io instruction to exit to the host. The host then reads rdi using kvm_get_regs(). CPU registers can't be read using kvm_get_regs() for TDX, so TDX guests use MMIO to pass the struct ucall's hva to the host. MMIO was chosen because it is one of the simplest (hence unlikely to fail) mechanisms that support passing 8 bytes from guest to host. Signed-off-by: Ackerley Tng Co-developed-by: Sagi Shahar Signed-off-by: Sagi Shahar --- .../testing/selftests/kvm/include/x86/ucall.h | 4 +- tools/testing/selftests/kvm/lib/x86/ucall.c | 45 ++++++++++++++++--- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86/ucall.h b/tools/testin= g/selftests/kvm/include/x86/ucall.h index d3825dcc3cd9..0494a4a21557 100644 --- a/tools/testing/selftests/kvm/include/x86/ucall.h +++ b/tools/testing/selftests/kvm/include/x86/ucall.h @@ -6,8 +6,6 @@ =20 #define UCALL_EXIT_REASON KVM_EXIT_IO =20 -static inline void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa) -{ -} +void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa); =20 #endif diff --git a/tools/testing/selftests/kvm/lib/x86/ucall.c b/tools/testing/se= lftests/kvm/lib/x86/ucall.c index 1265cecc7dd1..0ad24baaa3c4 100644 --- a/tools/testing/selftests/kvm/lib/x86/ucall.c +++ b/tools/testing/selftests/kvm/lib/x86/ucall.c @@ -5,11 +5,34 @@ * Copyright (C) 2018, Red Hat, Inc. */ #include "kvm_util.h" +#include "tdx/tdx.h" =20 #define UCALL_PIO_PORT ((uint16_t)0x1000) =20 +static uint8_t vm_type; +static vm_paddr_t host_ucall_mmio_gpa; +static vm_paddr_t ucall_mmio_gpa; + +void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa) +{ + vm_type =3D vm->type; + sync_global_to_guest(vm, vm_type); + + host_ucall_mmio_gpa =3D ucall_mmio_gpa =3D mmio_gpa; + + if (vm_type =3D=3D KVM_X86_TDX_VM) + ucall_mmio_gpa |=3D vm->arch.s_bit; + + sync_global_to_guest(vm, ucall_mmio_gpa); +} + void ucall_arch_do_ucall(vm_vaddr_t uc) { + if (vm_type =3D=3D KVM_X86_TDX_VM) { + tdg_vp_vmcall_ve_request_mmio_write(ucall_mmio_gpa, 8, uc); + return; + } + /* * FIXME: Revert this hack (the entire commit that added it) once nVMX * preserves L2 GPRs across a nested VM-Exit. If a ucall from L2, e.g. @@ -46,11 +69,23 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) { struct kvm_run *run =3D vcpu->run; =20 - if (run->exit_reason =3D=3D KVM_EXIT_IO && run->io.port =3D=3D UCALL_PIO_= PORT) { - struct kvm_regs regs; + switch (vm_type) { + case KVM_X86_TDX_VM: + if (vcpu->run->exit_reason =3D=3D KVM_EXIT_MMIO && + vcpu->run->mmio.phys_addr =3D=3D host_ucall_mmio_gpa && + vcpu->run->mmio.len =3D=3D 8 && vcpu->run->mmio.is_write) { + uint64_t data =3D *(uint64_t *)vcpu->run->mmio.data; + + return (void *)data; + } + return NULL; + default: + if (run->exit_reason =3D=3D KVM_EXIT_IO && run->io.port =3D=3D UCALL_PIO= _PORT) { + struct kvm_regs regs; =20 - vcpu_regs_get(vcpu, ®s); - return (void *)regs.rdi; + vcpu_regs_get(vcpu, ®s); + return (void *)regs.rdi; + } + return NULL; } - return NULL; } --=20 2.51.0.338.gd7d06c2dae-goog