From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (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 C6045388392 for ; Wed, 22 Jul 2026 23:15:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784762103; cv=none; b=XLBN9RLBZ4cG2XFGGGZc8m/5XoNWkmQfRBrlYvaCW5q7EOGY5CO/pl3U+t+66YmQV++VVbx9tEPUKEU9tLUm5nP6yEscHuRKpCYp9pVg3/bqZc4LBxxy9jliTiMuFtRT+/hOEWNKSvtlHAVW4zMr7MN6zx4FCPrEZymyLypDB7I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784762103; c=relaxed/simple; bh=Mt5Lh8+8kiaK6W3VjPfbBEwiyc60WE+BOpoJvPyUsrE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ItzUIVyJZmZmmK/3ezjhFfy2kVqwjSqEfMKSB+rMP0JCZYXeh0YBYWF2z2rS/78uc6TWzCTZDs2CdgfXcsEVTM8wbF5/6JJ6WVBqzFaPaFJKOlbLEQSFWRDZ7wCyth/ZAN+b28dNjXSJZo/v5N0zZ+Zuwbmg8dmcAg7fVPhunDk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bpbxXw0o; arc=none smtp.client-ip=209.85.214.197 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--wyihan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bpbxXw0o" Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2ccb6823efcso470705ad.0 for ; Wed, 22 Jul 2026 16:15:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784762101; x=1785366901; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=npQwrp8dxwBrq6gTxtnOVCzjkW4oB/ew6/3QNAsGhNI=; b=bpbxXw0oGaA+tiR+zn/p47h23T/ZbVWRcFyRxVUZK67zQJj+X6w2B6SIHZnti6u5fb sBu6LqGzCz2F+KcAGIrPLKBCTC0pSCuFaewHBtXAGrfAOowlo2h1rxZ8eDSCUkaxKDD7 /o8VEOEtmKCY+KSHNDjxBHOyAeQ+1LJiOHlFsn8C0g+WkYAKxQEb9G+diR1fvnpfUDrz 9TZOm4d2eyydefDjTNtBN8LPr8DBWU8/rxDdCa1jcVswJoZVWGLi8VP2O3l5eEKmxX/6 iRSJ5yqHoX2zSU1jyf34O/u8t+BbHknUIpDbmkr89RCP5jZ2yZgL3UoLyiSbNoFGXdYa WH+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784762101; x=1785366901; h=content-type: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:content-type; bh=npQwrp8dxwBrq6gTxtnOVCzjkW4oB/ew6/3QNAsGhNI=; b=MC/fg0oq4K2lq0FGENNZcCyRPlxFqaA2lToiSRkWJZjdV83WyFf9CuZoQ+N7X3i8HX ehA5q6fIPKdFjmq1UNbPv5rFzLNYZJCesbL9TWUskTI3o9322ojoMwIC5zSKXcj1CJd1 m1W+n6zjo3wYNPfTm5dzRVKJqvrAkodV/4kktkCV3dmYOetYFnVJXae5UtCkCM7UJ7Rv gCtGRuimcX6qAsMa3EYARrD2YdLHDPmljQeMyg41kD8NiEHekxOEr/boBJ/WhbTlT00g GCknvlZd+/e+JoBGjAQ4s5akbstwP1ovAFJP74piFmg8IMJLRe8XUabGzFXL0yLgjii4 YWJw== X-Forwarded-Encrypted: i=1; AHgh+RqHcV/3lfcKJtlxoua33obnu879uq+VTAilXTgglIZtsyQqfSr4jvB1oKfhZI4xiyJ98H9jpNzu7lWLH48=@vger.kernel.org X-Gm-Message-State: AOJu0YxpCM8vJiUzsrTDBmiUqWappKpwuzqlHenqYQIlOCSsixHtQBx7 KHVVuNtMSFTe/0uD1CH2UpE81savAvC8drbEeRIyoxVPjpiCzMqqiTJow7SnHxy51kDF7ykNKPM JQGq02g== X-Received: from plld11.prod.google.com ([2002:a17:902:728b:b0:2c8:7ed:acf4]) (user=wyihan job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:dac7:b0:2c7:ebfb:618f with SMTP id d9443c01a7336-2cfa6b73a76mr9771635ad.14.1784762078730; Wed, 22 Jul 2026 16:14:38 -0700 (PDT) Date: Wed, 22 Jul 2026 23:13:26 +0000 In-Reply-To: <20260722-tdx-selftests-v14-0-15ad654a50db@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260722-tdx-selftests-v14-0-15ad654a50db@google.com> X-Developer-Key: i=wyihan@google.com; a=ed25519; pk=cRi0fKzS5BMxlHyHY2pJv3w/1zcgfYKr6EYGYppdMYc= X-Developer-Signature: v=1; a=ed25519-sha256; t=1784762054; l=3287; i=wyihan@google.com; s=20260319; h=from:subject:message-id; bh=7diizuo3ArwKIH9Jsi5ICyi9zc8nAkbP5tj3yujaUxE=; b=r/IDcKTeYPBvxZo+9sWeP0LiVNW/DOX6AaP68XXLFLG4gDOoC2whw6k/TQoOk3j3a00HlfP8h 0OI5SVbn2efDnVqhQHLtWLQ/eyVISA6/dT0Nlv7T+BBa01oHO7mNOiZ X-Mailer: b4 0.14.3 Message-ID: <20260722-tdx-selftests-v14-21-15ad654a50db@google.com> Subject: [PATCH v14 21/22] KVM: selftests: Add ucall support for TDX From: Lisa Wang To: Andrew Jones , Ackerley Tng , Binbin Wu , Chao Gao , Chenyi Qiang , Dave Hansen , Erdem Aktas , Ira Weiny , Isaku Yamahata , Kiryl Shutsemau , linux-kselftest@vger.kernel.org, Paolo Bonzini , "Pratik R. Sampat" , Reinette Chatre , Rick Edgecombe , Roger Wang , Ryan Afranji , Sagi Shahar , Sean Christopherson , Shuah Khan , Xiaoyao Li , Oliver Upton Cc: Jeremiah McReynolds , kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, x86@kernel.org, Lisa Wang Content-Type: text/plain; charset="utf-8" From: Ackerley Tng Implement TDX ucall using TDCALL-based MMIO to pass the ucall address from the VM to the host. In standard KVM selftests, ucall uses a PIO instruction as a trigger to exit to the host, which then retrieves the ucall address by reading the guest's RDI register. This approach is incompatible with TDX because the host cannot access guest registers. Furthermore, PIO exits only expose 4 bytes of immediate data, which is insufficient for a 8-byte ucall address. By using TDCALL-based MMIO, the VM can share the full 8-byte address in a single exit without refactoring the common ucall framework and other non-x86 architectures. Signed-off-by: Ackerley Tng Co-developed-by: Sagi Shahar Signed-off-by: Sagi Shahar Co-developed-by: Lisa Wang Signed-off-by: Lisa Wang --- tools/testing/selftests/kvm/include/x86/ucall.h | 6 ----- tools/testing/selftests/kvm/lib/x86/ucall.c | 33 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86/ucall.h b/tools/testing/selftests/kvm/include/x86/ucall.h index 0e4950041e3e..7e54ec2c1a45 100644 --- a/tools/testing/selftests/kvm/include/x86/ucall.h +++ b/tools/testing/selftests/kvm/include/x86/ucall.h @@ -2,12 +2,6 @@ #ifndef SELFTEST_KVM_UCALL_H #define SELFTEST_KVM_UCALL_H -#include "kvm_util.h" - #define UCALL_EXIT_REASON KVM_EXIT_IO -static inline void ucall_arch_init(struct kvm_vm *vm, gpa_t mmio_gpa) -{ -} - #endif diff --git a/tools/testing/selftests/kvm/lib/x86/ucall.c b/tools/testing/selftests/kvm/lib/x86/ucall.c index e7dd5791959b..7a954b2d0bdd 100644 --- a/tools/testing/selftests/kvm/lib/x86/ucall.c +++ b/tools/testing/selftests/kvm/lib/x86/ucall.c @@ -5,11 +5,35 @@ * Copyright (C) 2018, Red Hat, Inc. */ #include "kvm_util.h" +#include "tdx/tdx.h" +#include "tdx/tdx_util.h" #define UCALL_PIO_PORT ((u16)0x1000) +/* HPET address is guaranteed to be unused for ucall MMIO */ +#define UCALL_MMIO_GPA 0xfed00000 + +static u8 vm_type; +static gpa_t ucall_mmio_gpa; + +void ucall_arch_init(struct kvm_vm *vm, gpa_t mmio_gpa) +{ + vm_type = vm->type; + sync_global_to_guest(vm, vm_type); + + if (is_tdx_vm(vm)) { + ucall_mmio_gpa = UCALL_MMIO_GPA | vm->arch.s_bit; + sync_global_to_guest(vm, ucall_mmio_gpa); + } +} + void ucall_arch_do_ucall(gva_t uc) { + if (vm_type == KVM_X86_TDX_VM) { + tdx_mmio_write(ucall_mmio_gpa, sizeof(gva_t), 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,6 +70,15 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) { struct kvm_run *run = vcpu->run; + if (vm_type == KVM_X86_TDX_VM) { + if (run->exit_reason == KVM_EXIT_MMIO && + run->mmio.phys_addr == UCALL_MMIO_GPA && + run->mmio.len == sizeof(gva_t) && + run->mmio.is_write) + return (void *)(*((gva_t *)run->mmio.data)); + return NULL; + } + if (run->exit_reason == KVM_EXIT_IO && run->io.port == UCALL_PIO_PORT) { struct kvm_regs regs; -- 2.55.0.229.g6434b31f56-goog