From nobody Wed Dec 17 12:17:19 2025 Received: from mail-io1-f73.google.com (mail-io1-f73.google.com [209.85.166.73]) (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 DE95A3570D4 for ; Tue, 28 Oct 2025 21:21:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761686469; cv=none; b=CW6WVQK7yxnsH7gybGrWnRXdfBzJbdeoI+LYHARd+QEirJYWU0C9hNCKHcE32QOk5o8gQs9Y9/JYZ9CodBYnw5YXNtGJgwcUj84zjn7fwcUsoRxL8NVV5zQxiCueogioFPVJF3O2Jao+Bfr6/bf0LBd4r3rFdshlwJ4VrS/46WQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761686469; c=relaxed/simple; bh=h+BtiPzVy2gWivSMFIqWnCmHMSFsal+CXe23zn8LbkQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bs6g6mNwCAcejUYMNBK41ySIUWMff+al58lqZobHXZqh4hKw2kGU5u2WL03kMkbC9IjAXUl73Sz8V3/JbWSjFvngG17c5k0QUe+2TtCWPdEg1capPJNeJz/qtdhf+OY6Zc54hh2krmwYLR6c1bDOkwfktfPFp4B8Gu6GccExcZ8= 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=fdgVSBV+; arc=none smtp.client-ip=209.85.166.73 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="fdgVSBV+" Received: by mail-io1-f73.google.com with SMTP id ca18e2360f4ac-940f9efd090so2079327039f.3 for ; Tue, 28 Oct 2025 14:21:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1761686466; x=1762291266; 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=PnRis7314sxjOj0Rh6H340e0+BpA55H88Cr2h5cu4AY=; b=fdgVSBV+sJ+MOWLn4uBqBX9j7MuqNRnEuoi5NEstlqWtJJtx+oQHLiUNOEXr9h+kge vwAJGs1e9CYE9Ew/GNKwb5onQsxuM4Nmnwu86OivT4iIR3MskigPm6Ya5plxRPCyNaZP H3p0EmcTM1SNPSTBJj5hU6uxRV4ZY3WlZM1Gb1AMV6saBZlqfq9Uk3xhecuXkTAfKOi9 1PvEpcsH6vrEpEAIo9srUq4F+rEhOzttM/irMvxBqkr5FDiYKSlzVfXyPSP+HwSulo2+ uDaXFIrVZIp1d1BJoW6N73Nh6l//KnDd/39GQfC1Wyx4K99NLaYKUBt5jNNnym+KiNYV ewvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761686466; x=1762291266; 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=PnRis7314sxjOj0Rh6H340e0+BpA55H88Cr2h5cu4AY=; b=CSwfyW6fUZqDlFetez63X11pLJOHkE3626iCLMbIQ43Qeg2qjG9Boe1eV+JtNSHgNW Edo/3IKojBYUcIdr6YiFnREE95MAnMc0KTR2Xcq5d8967PfVQRTlt90dbi4Ha/GtrLkB FDPEmvnPzAxG7PRmdcVcdvv2MT3B7oRrpXy86MopCuznUOt/w/FxQXzrVRZDNMAHowOp bBSngJ0mmswM/rfOuPy+QP35nv5UH/x36CCo6wTkQlEyshKkmZT/iFIRmOhPPyyUlVe+ v1uFnCSfpmM8qg90otHjU7WpwX73WvKa6QWehqEPnDplT2zliXCY53RpVnlYs6X3i4Be ViMw== X-Gm-Message-State: AOJu0YwhKbjoQO7I797bx0z5aS8AyuRBYXtg31Fb4C/XVXUSuv0zgkqK tiVGkhcZAyFDng/hqOfxmj8RC49tJTixpqCuwy9KnFxQJMhvYagDktfJe2Rbqx5EvL+U56mf2Pq c5w== X-Google-Smtp-Source: AGHT+IFFotzwJGtcAawKS1Bi4VJy5fzH8cXmMVxBeTy7ud7wRUCgQ0kdS8CGAINF03YwqDm76lS1B0wIdA== X-Received: from iobby9.prod.google.com ([2002:a05:6602:3a09:b0:93e:ac34:407d]) (user=sagis job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6602:134a:b0:940:df70:3a48 with SMTP id ca18e2360f4ac-945c9841230mr147217139f.10.1761686465936; Tue, 28 Oct 2025 14:21:05 -0700 (PDT) Date: Tue, 28 Oct 2025 21:20:37 +0000 In-Reply-To: <20251028212052.200523-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: <20251028212052.200523-1-sagis@google.com> X-Mailer: git-send-email 2.51.1.851.g4ebd6896fd-goog Message-ID: <20251028212052.200523-12-sagis@google.com> Subject: [PATCH v12 11/23] KVM: selftests: Set up TDX boot parameters region 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" Allocate memory for TDX boot parameters and define the utility functions necessary to fill this memory with the boot parameters. Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Signed-off-by: Sagi Shahar --------------------------------------------- Changes from v10: * Removed code for setting up X86_CR4_OSXMMEXCPT bit. At least for now it is not needed and the test pass without it. --- .../selftests/kvm/include/x86/tdx/tdx_util.h | 4 ++ .../selftests/kvm/lib/x86/tdx/tdx_util.c | 72 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h b/tools= /testing/selftests/kvm/include/x86/tdx/tdx_util.h index ec05bcd59145..dafdc7e46abe 100644 --- a/tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +++ b/tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h @@ -12,5 +12,9 @@ static inline bool is_tdx_vm(struct kvm_vm *vm) } =20 void vm_tdx_setup_boot_code_region(struct kvm_vm *vm); +void vm_tdx_setup_boot_parameters_region(struct kvm_vm *vm, uint32_t nr_ru= nnable_vcpus); +void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm); +void vm_tdx_load_vcpu_boot_parameters(struct kvm_vm *vm, struct kvm_vcpu *= vcpu); +void vm_tdx_set_vcpu_entry_point(struct kvm_vcpu *vcpu, void *guest_code); =20 #endif // SELFTESTS_TDX_TDX_UTIL_H diff --git a/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c b/tools/tes= ting/selftests/kvm/lib/x86/tdx/tdx_util.c index a1cf12de9d56..f3b69923e928 100644 --- a/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +++ b/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c @@ -5,10 +5,12 @@ #include "kvm_util.h" #include "processor.h" #include "tdx/td_boot.h" +#include "tdx/td_boot_asm.h" #include "tdx/tdx_util.h" =20 /* Arbitrarily selected to avoid overlaps with anything else */ #define TD_BOOT_CODE_SLOT 20 +#define TD_BOOT_PARAMETERS_SLOT 21 =20 #define X86_RESET_VECTOR 0xfffffff0ul #define X86_RESET_VECTOR_SIZE 16 @@ -52,3 +54,73 @@ void vm_tdx_setup_boot_code_region(struct kvm_vm *vm) hva[1] =3D 256 - 2 - TD_BOOT_CODE_SIZE; hva[2] =3D 0xcc; } + +void vm_tdx_setup_boot_parameters_region(struct kvm_vm *vm, uint32_t nr_ru= nnable_vcpus) +{ + size_t boot_params_size =3D + sizeof(struct td_boot_parameters) + + nr_runnable_vcpus * sizeof(struct td_per_vcpu_parameters); + int npages =3D DIV_ROUND_UP(boot_params_size, PAGE_SIZE); + vm_paddr_t gpa; + + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, + TD_BOOT_PARAMETERS_GPA, + TD_BOOT_PARAMETERS_SLOT, npages, + KVM_MEM_GUEST_MEMFD); + gpa =3D vm_phy_pages_alloc(vm, npages, TD_BOOT_PARAMETERS_GPA, TD_BOOT_PA= RAMETERS_SLOT); + TEST_ASSERT(gpa =3D=3D TD_BOOT_PARAMETERS_GPA, "Failed vm_phy_pages_alloc= \n"); + + virt_map(vm, TD_BOOT_PARAMETERS_GPA, TD_BOOT_PARAMETERS_GPA, npages); +} + +void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm) +{ + struct td_boot_parameters *params =3D + addr_gpa2hva(vm, TD_BOOT_PARAMETERS_GPA); + uint32_t cr4; + + TEST_ASSERT_EQ(vm->mode, VM_MODE_PXXV48_4K); + + cr4 =3D kvm_get_default_cr4(); + + /* TDX spec 11.6.2: CR4 bit MCE is fixed to 1 */ + cr4 |=3D X86_CR4_MCE; + + /* TDX spec 11.6.2: CR4 bit VMXE and SMXE are fixed to 0 */ + cr4 &=3D ~(X86_CR4_VMXE | X86_CR4_SMXE); + + /* Set parameters! */ + params->cr0 =3D kvm_get_default_cr0(); + params->cr3 =3D vm->pgd; + params->cr4 =3D cr4; + params->idtr.base =3D vm->arch.idt; + params->idtr.limit =3D kvm_get_default_idt_limit(); + params->gdtr.base =3D vm->arch.gdt; + params->gdtr.limit =3D kvm_get_default_gdt_limit(); + + TEST_ASSERT(params->cr0 !=3D 0, "cr0 should not be 0"); + TEST_ASSERT(params->cr3 !=3D 0, "cr3 should not be 0"); + TEST_ASSERT(params->cr4 !=3D 0, "cr4 should not be 0"); + TEST_ASSERT(params->gdtr.base !=3D 0, "gdt base address should not be 0"); + TEST_ASSERT(params->idtr.base !=3D 0, "idt base address should not be 0"); +} + +void vm_tdx_load_vcpu_boot_parameters(struct kvm_vm *vm, struct kvm_vcpu *= vcpu) +{ + struct td_boot_parameters *params =3D + addr_gpa2hva(vm, TD_BOOT_PARAMETERS_GPA); + struct td_per_vcpu_parameters *vcpu_params =3D + ¶ms->per_vcpu[vcpu->id]; + + vcpu_params->esp_gva =3D kvm_allocate_vcpu_stack(vm); +} + +void vm_tdx_set_vcpu_entry_point(struct kvm_vcpu *vcpu, void *guest_code) +{ + struct td_boot_parameters *params =3D + addr_gpa2hva(vcpu->vm, TD_BOOT_PARAMETERS_GPA); + struct td_per_vcpu_parameters *vcpu_params =3D + ¶ms->per_vcpu[vcpu->id]; + + vcpu_params->guest_code =3D (uint64_t)guest_code; +} --=20 2.51.1.851.g4ebd6896fd-goog