Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp246388rwb; Tue, 25 Jul 2023 15:17:20 -0700 (PDT) X-Google-Smtp-Source: APBJJlF0ErxvMKVWi1yFW9bP3+A8NTei0Ocye0D4/gJjNsPtoNM4M0eBMu6nxeKXhL+x+7zsNBUN X-Received: by 2002:a17:907:760a:b0:988:fb2f:274e with SMTP id jx10-20020a170907760a00b00988fb2f274emr141665ejc.27.1690323440345; Tue, 25 Jul 2023 15:17:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690323440; cv=none; d=google.com; s=arc-20160816; b=CbPuftRlt2BOIwJKWMIHZDEzexc4pW6sIF2h/DV8RhrEWNg7YtrZx0e8nZVmFX2A3o OM37Q+DgA76SB4lF+m7rVKHv8Wk/xJmTa2jLFueufsForA1tcWWbrL8o6/KUthAu6hRA 8c9+Oehzfc/4HkIl+bUYRHn7/aMPYPwGZZwJ2wijnY3WH7o5K93xPMsIGFiy7QXWkC6x 7MT0HwCx2DRbqQDqaplUuAmw7x0fJe3EHGZcm/IseGMBGjtM4LWVWv8YoSt2PFcYBvwu mBRh7Ma6bIH13ZuURxcIfbcRDV7tUGqwei7TdaMBT5JAaAES9X0Vhcp7xTEO9BLeknEh Krgw== 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=NtcWyuvzCv676hKj496tQHwOP6H0h/MN00b1AT520wM=; fh=1/sgOwi5AuU8IZymeFKs1Rjnf4QELnrK+C7GntqU2hA=; b=XgzV5ubpggFulms/KF/Y2W4ozLxxwZZ5ToDrs8QLPXUaL974/TLXbYBMw0LDFGhhPk X5N0UvO/k60u7jLK+8gDKonFMruBN9mQWNOk+a+6iDZb4d/jtkfUCca0DdkLCO/0o1Ut LiSU4KPe6vYjZdCYb2Ql8FNp6V7ztntBd2WiRY2ZqETtCs7k8wRwRE38Z+NcgYFY9V+N ySWFUnW2MUCtskc7Dj11VW46UxtYIIzSWHphKNp/Fe6SUql79l3W9XH5WF7ipu9Q1lCH S+G7EPx1BxTGsI1aQJGjW849S1V2cc/nAjzIeWKmB9XXx6e6tpjPeSY5OoezpyJF21r6 F2DQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Sh6NfQAq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h18-20020a1709063c1200b0098e48ad11e8si7991089ejg.902.2023.07.25.15.16.56; Tue, 25 Jul 2023 15:17:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Sh6NfQAq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229548AbjGYWEF (ORCPT + 99 others); Tue, 25 Jul 2023 18:04:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231960AbjGYWDE (ORCPT ); Tue, 25 Jul 2023 18:03:04 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57A4930ED for ; Tue, 25 Jul 2023 15:02:32 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5704991ea05so73325917b3.1 for ; Tue, 25 Jul 2023 15:02:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690322538; x=1690927338; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NtcWyuvzCv676hKj496tQHwOP6H0h/MN00b1AT520wM=; b=Sh6NfQAqvxkG5Are5kDLONdnFcJrwZpKh6nlYP7ihUcp0MyboQJIaMix7EZelt/tH5 YEaF/QFx1wf75q6z7BValjIDs/5hz4oVqItxo03qkxBtrJy3nlqOJI25e+gIp0EqJu81 4T/0eyD+p7zf0TF6k/I+gZQBJ1TA1vTJojtP/5Rh+R+1rAtiRJPI1xeaoQyfMtkafzZ2 RGjIVHWw49K5p5Jiek47RDAX2/SJTmFlfyIUbzY5JX/PRv+bwDwcBXgwAncJ9s5op5wB i7zlP3ffiwTtqHx/t3wy3XB+TthDG9uQ/3P5nACNJhbIkGpbDBint+Stx5MnxG9fgvpF doIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690322538; x=1690927338; 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=NtcWyuvzCv676hKj496tQHwOP6H0h/MN00b1AT520wM=; b=MB5tz3SsFXT2jNC9B5Ou9kWVh938Bl4wdVc0mpZa9+z8tjB7LeQ7GuA6J61w7vFMai x6w66/Yer4UsMaFQrKFXsZJy5NIwWS97uImz0zqlTjWpb4lJ5EhV+ZMpTcFbONBKDNEW Tc/Lf4yqI9ANcMy7Ni5W2nS4XevFfwrlzdMzKu/6+hq6S4Sx+VpdLCIPmbgP+UMYBM+K 9OCRF5EHIul+5x6H2Y56ubEQVpKCaH5V5j68+MAFTY1DFrnEEslqrJJ25joHZKA5bjPt RJW7Cve+V6rPLcEbxjhtw3udoi5bN/6NIjMWWthKGp8k118sRLBtCl/hjRfKPF2d9KRF txaQ== X-Gm-Message-State: ABy/qLbUHwPxpI9SGabGLz5yPneRCO0zm1YuMATjIxoxkPZ3PAY6Bwym umxZDMkEeT/eAZFUqtoozXrd2f/SJwiz X-Received: from afranji.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:47f1]) (user=afranji job=sendgmr) by 2002:a25:69c8:0:b0:d0c:e37:b749 with SMTP id e191-20020a2569c8000000b00d0c0e37b749mr1261ybc.10.1690322538496; Tue, 25 Jul 2023 15:02:18 -0700 (PDT) Date: Tue, 25 Jul 2023 22:01:09 +0000 In-Reply-To: <20230725220132.2310657-1-afranji@google.com> Mime-Version: 1.0 References: <20230725220132.2310657-1-afranji@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230725220132.2310657-17-afranji@google.com> Subject: [PATCH v4 16/28] KVM: selftests: TDX: Add TDX HLT exit test From: Ryan Afranji To: linux-kselftest@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, isaku.yamahata@intel.com, sagis@google.com, erdemaktas@google.com, afranji@google.com, runanwang@google.com, shuah@kernel.org, drjones@redhat.com, maz@kernel.org, bgardon@google.com, jmattson@google.com, dmatlack@google.com, peterx@redhat.com, oupton@google.com, ricarkol@google.com, yang.zhong@intel.com, wei.w.wang@intel.com, xiaoyao.li@intel.com, pgonda@google.com, eesposit@redhat.com, borntraeger@de.ibm.com, eric.auger@redhat.com, wangyanan55@huawei.com, aaronlewis@google.com, vkuznets@redhat.com, pshier@google.com, axelrasmussen@google.com, zhenzhong.duan@intel.com, maciej.szmigiero@oracle.com, like.xu@linux.intel.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, ackerleytng@google.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sagi Shahar 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 Change-Id: I253a2432bb29265bc3b3360f9254395761c5aadd 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 41dad6cf249b..f77caf262f84 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); + 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; } -- 2.41.0.487.g6d72f3e995-goog