Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp4699269rwe; Tue, 30 Aug 2022 15:23:42 -0700 (PDT) X-Google-Smtp-Source: AA6agR5rlvEMgIEQ5lbtv1okF0fhu612spc32aWbiuZwUTijJmbYhkzx6A645SePxJexezfxqjwU X-Received: by 2002:a05:6402:3219:b0:448:2994:df99 with SMTP id g25-20020a056402321900b004482994df99mr13614001eda.326.1661898222359; Tue, 30 Aug 2022 15:23:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661898222; cv=none; d=google.com; s=arc-20160816; b=1CXTHpfmc1lIUzbwxc2MmPyC7zlnyLp+wgYrUyaWNAsYpZS6GXSAPATiVbR8SbeOAd DtqKxA+N7s/dfNEo36Njr3ye7Szcfhcc49dXo0GhJ6a7KYXwTeDvrCCiOzyChyka8k0M L69NICOPSTU/WAODex4ccZtnsRUhHba+/ej5vO2JU7JvGZSN8mL4lPK7v+Lhc+YXr9jW JVxXHFXetndkeYVP6skoTNDrcPdAlIzy98m25u4MKyPsOAY3wkYV/VHnl1i4+ACJo8y/ It4l4S+j22pEc4PSvfEy03Yu94D4bpTIbvFSfM3IirdmAGvweMRNIrHD5/k4hdPHigN5 dY7w== 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=4HErFRuZWnvNea+crcpmX3JHa78L1TL3m4q9Khg/Jh4=; b=ck16BDslVi3jzz9ez4TRspMRhIf/s30RtuDOp2XxrLkaaS+meEKlPv6BiJkshKKIUl l9I0Ql/qf9mqvKd/Eblua1ukeIgWdmrRt5XjDHTowZSa4x3CQMeClYcSv7rnTj4kI37z 1glsUU8SaZizk/BFKyez1W8mHASaDqpaE1hxlmD0APvWsnisAjJE0EG7n3aOL1tkahvk DA5oGf+laLIKdxGsaJb/MtGroqEaQL48tdsHoyexErtA3soTWGwYSntPJFV3XjTZ1gIe LOhfe4I3AOiRc+McfMpPpVzDi/RywhhcAIzflGRksXpG0xC/+0h1Nduxj/B6zNtrCweV 97EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=cDKvhJ6c; 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 sd5-20020a1709076e0500b00730d36cd631si10045045ejc.816.2022.08.30.15.23.14; Tue, 30 Aug 2022 15:23:42 -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=20210112 header.b=cDKvhJ6c; 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 S232154AbiH3WUi (ORCPT + 99 others); Tue, 30 Aug 2022 18:20:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231954AbiH3WUP (ORCPT ); Tue, 30 Aug 2022 18:20:15 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F31B2B27D for ; Tue, 30 Aug 2022 15:20:12 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id s13-20020a056a00194d00b005385093da2dso2905877pfk.13 for ; Tue, 30 Aug 2022 15:20:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=4HErFRuZWnvNea+crcpmX3JHa78L1TL3m4q9Khg/Jh4=; b=cDKvhJ6cjRi3/088MY6el/echJAjDXJyz87g+GLLGuxePVe7haWbP1mmqlJRdw8xAB ulPMlNP0Q84T5Whq/9d+LuwLsemViGiwbEZFwfvEv7jcTxYt6r549vumV8rRze0ajjo6 d66T4t3NqHaks1m7FwWmV+1aiUaKk+ZYCXeqXCBkDUzjO/DzHndVZKBaFBuDVEIbyKAT +pqk6gIdjXXXURpds3lOB/jPE9lqkusocO7BoFTelzCFxjeBoxSJSivWVYBTXlW3Xyqf eLc4OIWy3ajLwJAp+lyZdNq2em2r6Trqbg3vHRH1SsE4qlQRPmf522lj1lt+dPy3LkTG NL/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=4HErFRuZWnvNea+crcpmX3JHa78L1TL3m4q9Khg/Jh4=; b=ffzLAfPq/ATamHsikEzZ4690srp3LULyj5z/CfkRQbJ8yvKy1QuIu0bQlAc7SMBteC vfn6WNQwW062ng7vhKnrjwLR+KSR1MyJu2bkUyc0oszHX4B1jYckh1B50RGdd3G5Qpvq OcGkh3KvbmsNETnj+RwKSKg3jZkwKyiJKrjj7hE6L1AXZ4nOM2WoPGaxsFXOFH5mlWst 8qCyrWViP4vKoeEYCRwfJS69hQiVL+0JefTw6bxwNcIurQ8HJpvREEOIyE3+n4OHNZob g5kfRUz5ThHu5bN6YSf0kp8dGUveO+ZljVyuM0O/dgxDEGvS34A9uhNeyr8LnsXkpgaY JApw== X-Gm-Message-State: ACgBeo2UhXy5DIpV2/fWMgGQzxPw2+TALG/O8YSjN1bJBHBR0MP0/CHj iwviDQGhANcTQRcrV9YoV5iNvtioGQ== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a05:6a00:b8b:b0:536:71f7:4ce3 with SMTP id g11-20020a056a000b8b00b0053671f74ce3mr23113027pfj.74.1661898012331; Tue, 30 Aug 2022 15:20:12 -0700 (PDT) Date: Tue, 30 Aug 2022 22:19:47 +0000 In-Reply-To: <20220830222000.709028-1-sagis@google.com> Mime-Version: 1.0 References: <20220830222000.709028-1-sagis@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220830222000.709028-5-sagis@google.com> Subject: [RFC PATCH v2 04/17] KVM: selftest: TDX: Add report_fatal_error test From: Sagi Shahar To: linux-kselftest@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Isaku Yamahata , Sagi Shahar , Erdem Aktas , Ryan Afranji , Roger Wang , Shuah Khan , Andrew Jones , Marc Zyngier , Ben Gardon , Jim Mattson , David Matlack , Peter Xu , Oliver Upton , Ricardo Koller , Yang Zhong , Wei Wang , Xiaoyao Li , Peter Gonda , Marc Orr , Emanuele Giuseppe Esposito , Christian Borntraeger , Eric Auger , Yanan Wang , Aaron Lewis , Vitaly Kuznetsov , Peter Shier , Axel Rasmussen , Zhenzhong Duan , "Maciej S . Szmigiero" , Like Xu , linux-kernel@vger.kernel.org, kvm@vger.kernel.org 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 The test checks report_fatal_error functionality. Signed-off-by: Sagi Shahar --- tools/testing/selftests/kvm/lib/x86_64/tdx.h | 17 ++++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 55 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx.h b/tools/testing/selftests/kvm/lib/x86_64/tdx.h index d5de52657112..351ece3e80e2 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx.h +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx.h @@ -51,6 +51,7 @@ #define _PAGE_RW (1UL<<1) /* writeable */ #define _PAGE_PS (1UL<<7) /* page size bit*/ +#define TDX_REPORT_FATAL_ERROR 0x10003 #define TDX_INSTRUCTION_IO 30 #define TDX_SUCCESS_PORT 0x30 @@ -212,6 +213,22 @@ static inline void tdvmcall_success(void) tdvmcall_io(TDX_SUCCESS_PORT, /*size=*/4, TDX_IO_WRITE, &code); } +/* + * Report an error to user space. + * data_gpa may point to an optional shared guest memory holding the error string. + * Return value from tdvmcall is ignored since execution is not expected to + * continue beyond this point. + */ +static inline void tdvmcall_fatal(uint64_t error_code) +{ + struct kvm_regs regs; + + memset(®s, 0, sizeof(regs)); + regs.r11 = TDX_REPORT_FATAL_ERROR; + regs.r12 = error_code; + regs.rcx = 0x1C00; + tdcall(®s); +} #define TDX_FUNCTION_SIZE(name) ((uint64_t)&__stop_sec_ ## name -\ (uint64_t)&__start_sec_ ## name) \ 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 590e45aa7570..1db5400ca5ef 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -91,6 +91,60 @@ void verify_td_lifecycle(void) printf("\t ... PASSED\n"); } +/* + * Verifies TDX_REPORT_FATAL_ERROR functionality. + */ +TDX_GUEST_FUNCTION(guest_code_report_fatal_error) +{ + uint64_t err; + /* Note: err should follow the GHCI spec definition: + * bits 31:0 should be set to 0. + * bits 62:32 are used for TD-specific extended error code. + * bit 63 is used to mark additional information in shared memory. + */ + err = 0x0BAAAAAD00000000; + + if (err) + tdvmcall_fatal(err); + + tdvmcall_success(); +} + +void verify_report_fatal_error(void) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + + printf("Verifying report_fatal_error:\n"); + /* Create a TD VM with no memory.*/ + vm = vm_create_tdx(); + + /* Allocate TD guest memory and initialize the TD.*/ + initialize_td(vm); + + /* Initialize the TD vcpu and copy the test code to the guest memory.*/ + vcpu = vm_vcpu_add_tdx(vm, 0); + + /* Setup and initialize VM memory */ + prepare_source_image(vm, guest_code_report_fatal_error, + TDX_FUNCTION_SIZE(guest_code_report_fatal_error), + 0); + finalize_td_memory(vm); + + vcpu_run(vcpu); + ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT); + ASSERT_EQ(vcpu->run->system_event.ndata, 3); + ASSERT_EQ(vcpu->run->system_event.data[0], TDX_REPORT_FATAL_ERROR); + ASSERT_EQ(vcpu->run->system_event.data[1], 0x0BAAAAAD00000000); + ASSERT_EQ(vcpu->run->system_event.data[2], 0); + + vcpu_run(vcpu); + CHECK_GUEST_COMPLETION(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { if (!is_tdx_enabled()) { @@ -99,6 +153,7 @@ int main(int argc, char **argv) } run_in_new_process(&verify_td_lifecycle); + run_in_new_process(&verify_report_fatal_error); return 0; } -- 2.37.2.789.g6183377224-goog