From patchwork Tue Dec 12 20:46:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177569 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993820vqy; Tue, 12 Dec 2023 12:48:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IEaMkAob5lLXn/3MNNIpHSL8dTsvukD1HZJ/MH25+5HMLescrY4hQFe3w3ejHXVO04/UxN/ X-Received: by 2002:a05:6a20:3948:b0:18f:97c:4f4a with SMTP id r8-20020a056a20394800b0018f097c4f4amr4218367pzg.86.1702414131456; Tue, 12 Dec 2023 12:48:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414131; cv=none; d=google.com; s=arc-20160816; b=tKV7B66tGojPqSWOakRKaOOyO5vZdv/Cu1SGew1Zu3QEYfztpICi2fy5ala9L7J3ek OD9b9mYzzNQauP+tekYACfQm60Eat63JOQdJXE3LCYWtiL0i3hJExDN5dPSWiJTZqFRN MiDRrdcl9E96HBXCylWwZO6lR28hTPcUtlIvygZPFJynyFFJa86NlWqOzfZyYh3KsGdm VdbV996E2a71Iu7VkDDrMd/3k24eIchW0Nf9WqngGQxWcPpr3uJka1VtjOPW/F3kOK5h nMvjRNmizr8xcLvLbmkyTeOvswCZt5fmiNaHnabjOcj7GmqwjDuraywGA98KHG+2SagG gIXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=5MyTn48cv4nrVxvqRuS9eNhhEY7jSVIhffkw0y0uC7o=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=yrZZTlDXeY2Jy6A4A8aLWF7uCkoQj+5cL+0bWdZv5EImMusjxmgzzAK+nidB77rD98 WnHfi4aHr3rP7XYHc36TCxv295x5Wff6tlWnI9t/rt/pKQALmWm8pU/+zDEEVcp4KMuR Ew4y6TN9xKUwlSCVVfqWO7xIRQxObNdAl2snHZco5ZM0a6xmTD4cUuE4bYtd7TwcLf7q 0+hn4lSHvYQXkxhTnQqlyuXPq2OaNiM2JOZZuOiR3vm491OhnenmYoY9YFQQrbscFZ7g 2MWcX0gjdndKfh5F2IDwcYVsejm+nTou6llaOSW0gvs/CcRXBfC5FCFCFEfGbREOlRZZ jOdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Hijd/8DP"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id a3-20020a654183000000b005b9a1494637si8133426pgq.317.2023.12.12.12.48.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:48:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Hijd/8DP"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id CC0F380FFD80; Tue, 12 Dec 2023 12:48:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377373AbjLLUsV (ORCPT + 99 others); Tue, 12 Dec 2023 15:48:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235224AbjLLUry (ORCPT ); Tue, 12 Dec 2023 15:47:54 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E509410F0 for ; Tue, 12 Dec 2023 12:47:30 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1d33751a322so5237085ad.3 for ; Tue, 12 Dec 2023 12:47:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414048; x=1703018848; 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=5MyTn48cv4nrVxvqRuS9eNhhEY7jSVIhffkw0y0uC7o=; b=Hijd/8DPJdYHAfJvIEjgagibhTXaYX0KplIL+/L08ijNytvu42LzGUFb7L6vSD79I0 vWLRRx7Fhj5ndw13FvEyXloHA0lbtIjWHDBS9h0Stnx8KXuqXy0UPE3ov0gJbdvQiL5c bTXtcdLdQWMxzhKn/7789p5DETf79I2+wUzGu6XJ8rbzhaXoi7yrHairjFB7goPP96h1 C5lLPjVUaqXtkaRpefylDo3sjnEYDBTNB5KSVLxkopTNyP85me7q+lRfJg03eg4iOmTC zXGrx04GIgmKNyBffZV6/q0IM66sllCJmHNRvcCQzdXnkDiOyTh8UK4sDQAIG4XpsfJr Q60g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414048; x=1703018848; 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=5MyTn48cv4nrVxvqRuS9eNhhEY7jSVIhffkw0y0uC7o=; b=sK4Lfr/gotmdscPo0JUOTq1faDvIHzLWDobPsX5brlHwj4aI4Hl3t24AaeML9puNYP Il3Q/uS8nm4e7okDO56i6+Hp7I9fnyiEAu5t9QcWXDQvVhdz24XAg0aFNj7ldEArcgDX AFwFTMjXTxtMc9Dr7WBrO9bPIG16k8UFV+2BtdAzy3RZknhYkIKWgUNujmZVCDu9YgF4 zw0lGUAsUaP0miWbDy/X27sYv2mjCpYa00BLxrKzIFBiQNKQQEvUhVYWUkAfqWcS/rz6 UwKkRdUh1wT+zV+UHvf3uvuGRBLoiWUFlxzzvYepO9Wqa3f71w0MJROrlKq+Ao3sjC2p pA/w== X-Gm-Message-State: AOJu0Yz20RzAt9kdrIKalAWS9urw3EU/YUbegJ0+7ib2wF+LPYk7PS9a 6bWHaSA5ZKUstGy7Neg7Jas8aDiECg== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:b712:b0:1d3:46f4:7f1e with SMTP id d18-20020a170902b71200b001d346f47f1emr4989pls.1.1702414047966; Tue, 12 Dec 2023 12:47:27 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:31 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-17-sagis@google.com> Subject: [RFC PATCH v5 16/29] KVM: selftests: TDX: Add TDX HLT exit test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:48:49 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110600139200130 X-GMAIL-MSGID: 1785110600139200130 The test verifies that the guest runs TDVMCALL and the guest vCPU enters to the halted state. Signed-off-by: Erdem Aktas Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji --- .../selftests/kvm/include/x86_64/tdx/tdx.h | 2 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 10 +++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 78 +++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index 85ba6aab79a7..b18e39d20498 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -8,6 +8,7 @@ #define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000 #define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 +#define TDG_VP_VMCALL_INSTRUCTION_HLT 12 #define TDG_VP_VMCALL_INSTRUCTION_IO 30 #define TDG_VP_VMCALL_INSTRUCTION_RDMSR 31 #define TDG_VP_VMCALL_INSTRUCTION_WRMSR 32 @@ -20,5 +21,6 @@ uint64_t tdg_vp_vmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12, uint64_t *r13, uint64_t *r14); uint64_t tdg_vp_vmcall_instruction_rdmsr(uint64_t index, uint64_t *ret_value); uint64_t tdg_vp_vmcall_instruction_wrmsr(uint64_t index, uint64_t value); +uint64_t tdg_vp_vmcall_instruction_hlt(uint64_t interrupt_blocked_flag); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index 88ea6f2a6469..9485bafedc38 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -114,3 +114,13 @@ uint64_t tdg_vp_vmcall_instruction_wrmsr(uint64_t index, uint64_t value) return __tdx_hypercall(&args, 0); } + +uint64_t tdg_vp_vmcall_instruction_hlt(uint64_t interrupt_blocked_flag) +{ + struct tdx_hypercall_args args = { + .r11 = TDG_VP_VMCALL_INSTRUCTION_HLT, + .r12 = interrupt_blocked_flag, + }; + + return __tdx_hypercall(&args, 0); +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index 5db3701cc6d9..5fae4c6e5f95 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -721,6 +721,83 @@ void verify_guest_msr_writes(void) printf("\t ... PASSED\n"); } +/* + * Verifies HLT functionality. + */ +void guest_hlt(void) +{ + uint64_t ret; + uint64_t interrupt_blocked_flag; + + interrupt_blocked_flag = 0; + ret = tdg_vp_vmcall_instruction_hlt(interrupt_blocked_flag); + if (ret) + tdx_test_fatal(ret); + + tdx_test_success(); +} + +void _verify_guest_hlt(int signum); + +void wake_me(int interval) +{ + struct sigaction action; + + action.sa_handler = _verify_guest_hlt; + sigemptyset(&action.sa_mask); + action.sa_flags = 0; + + TEST_ASSERT(sigaction(SIGALRM, &action, NULL) == 0, + "Could not set the alarm handler!"); + + alarm(interval); +} + +void _verify_guest_hlt(int signum) +{ + struct kvm_vm *vm; + static struct kvm_vcpu *vcpu; + + /* + * This function will also be called by SIGALRM handler to check the + * vCPU MP State. If vm has been initialized, then we are in the signal + * handler. Check the MP state and let the guest run again. + */ + if (vcpu != NULL) { + struct kvm_mp_state mp_state; + + vcpu_mp_state_get(vcpu, &mp_state); + TEST_ASSERT_EQ(mp_state.mp_state, KVM_MP_STATE_HALTED); + + /* Let the guest to run and finish the test.*/ + mp_state.mp_state = KVM_MP_STATE_RUNNABLE; + vcpu_mp_state_set(vcpu, &mp_state); + return; + } + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_hlt); + td_finalize(vm); + + printf("Verifying HLT:\n"); + + printf("\t ... Running guest\n"); + + /* Wait 1 second for guest to execute HLT */ + wake_me(1); + td_vcpu_run(vcpu); + + TDX_TEST_ASSERT_SUCCESS(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + +void verify_guest_hlt(void) +{ + _verify_guest_hlt(0); +} int main(int argc, char **argv) { @@ -740,6 +817,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_guest_reads); run_in_new_process(&verify_guest_msr_writes); run_in_new_process(&verify_guest_msr_reads); + run_in_new_process(&verify_guest_hlt); return 0; }