Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp4723421rwe; Tue, 30 Aug 2022 16:02:15 -0700 (PDT) X-Google-Smtp-Source: AA6agR5YRyAqlDzkudoQblfxZno5N7i0FaqD0lVW8R2Ee194IckXmd1FXcvr+c5vEFJANdYZ3oKX X-Received: by 2002:a17:907:3e07:b0:741:7db9:eb74 with SMTP id hp7-20020a1709073e0700b007417db9eb74mr9128511ejc.83.1661900535566; Tue, 30 Aug 2022 16:02:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661900535; cv=none; d=google.com; s=arc-20160816; b=wpsgvFOdYxvC8YZFtmKu4/o6iU9j/L2hx2X4e4o08lULtUE6JZV4YmVe0oVw6EQ6Bs 0h8ICbeNBMzvi9PTw5yJP26Qb26ZmL1RD1L136gj2frtupT4jdWu5yCGf6UdFtUw1jCI 5Uy7qs25lvvx9hP0zaQ0NzkUoix4HqW1LfqdcwLx/unvQKSxQDYVrhh98hS7egSizVx/ 250w36bs0MJn/x9Y+FBNYWT8Hq2UZvq14QISWs2NEMx6ATRinBQbn3+ZT5fNPSt5/e5M AFaeIs4Rtj0KUZiv1lhA6X0HBN0FMAaK3zEiKHvNTgOENHpAI3U2G+jZ+almHNU/AVVm NNbw== 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=xND6YprCPZaQfpHo5dST5YbROtuGVFjyk8gKa/6IPBk=; b=yYLOrAReiQ5drCvlUNeKEsAg70Pz4GgseZVYdPbvHYWJ7VDy1ABF7fAmge+LCAIB/m v9L3NhZlGAUUdftMG9/dt+MoxjdEUQd8n4K599li+usdtpw7N9cE/jl0Ka/YT3LGkT+T H6PGWh3nETPJ312we25lHgsKh+JJkGhxgA2fwFfCGpTdKugVirHm577+U6cB0s9AuA3C ojzBX3BUaUavTIQhTyB38S4JTS4oU2TNjtuoyQhoV20BU4zHnflrG0AD4QvQ61/FDEkM T0yI+BNofJbi++R7i4nKukNJDPRay8oEC6V15DX0tXaBC4eJ1szmxJwcI3vDHln9CsPX 2UdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ihbMRkP6; 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 r9-20020a50c009000000b00447d6f244c6si8894808edb.248.2022.08.30.16.01.49; Tue, 30 Aug 2022 16:02:15 -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=ihbMRkP6; 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 S232266AbiH3WUx (ORCPT + 99 others); Tue, 30 Aug 2022 18:20:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232037AbiH3WUX (ORCPT ); Tue, 30 Aug 2022 18:20:23 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E7AD45F57 for ; Tue, 30 Aug 2022 15:20:18 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id oo12-20020a17090b1c8c00b001faa0b549caso11850493pjb.0 for ; Tue, 30 Aug 2022 15:20:18 -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=xND6YprCPZaQfpHo5dST5YbROtuGVFjyk8gKa/6IPBk=; b=ihbMRkP6LsisdZGdDSPBzStuAZmKk3JB59/cYAJtuNTJ2cOdmKtcPsX+9Y9WE4rfpv vgYOKUHpBCwMSlmOerTzfPv4fmUL5At8WQduTf4NtNmYGZpLCyywSQT2Jp+kN6E8uNFV EepkBJ87b4P1IjxdGwbI8BmSnztidDFGgOjsb3LHMBcm9aLfFXt4aSLsE1EilcD7ap5L FTag25ZySHcLIEbgXeHAbmEH2+sO7GmRffsS1/3sNPa7ZllFLqwWJLu8R+ruByRZ4B/V sI3Iieu3p0jj3Y7Rzw4Shshnx2WugrYTDfqwQXnZ6EnaEFapcLFdgAqTEPg6sxK/DduH qkxQ== 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=xND6YprCPZaQfpHo5dST5YbROtuGVFjyk8gKa/6IPBk=; b=JIxaz1U3/VIcW7KxKFtSsaXos8h79CjMb96xPrusGxYznyU/xU5s1TfdKczv/AcjSM NxytM13OeX1wpOA3DL94+0zjWm8JpJcGyOXRXfIn47IqNwBGuJ70mH7XP8ofj6db0eqh gTl6FD4LFsDpPpaGwhZRMN3NBi+e0W6jAPvsLFv1FvxIZdoJyqwRkGnIJiA0xQa5gbV0 GIIpQ4WBcTbYTMj3lTnL1lGaGa945ygQblJ0oXe0L4IuzQwgdQwiilmzePqdVKzy8W5u Sd7418BnaJvTwjWVJzjk+g5507dp2aDiBB8Cbd87uc9oh2Zcnh0NE/w3xTwJV5ugqHd3 m1/g== X-Gm-Message-State: ACgBeo0wVr2Lo5zr1UIWr0pVPJQsfOqNQdHSj0qXF7zkwLz7vtcwBUBA atmEVJ4qPW6tCd/151+c+nuCD59lWw== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:90a:bb96:b0:1fd:8068:cfd4 with SMTP id v22-20020a17090abb9600b001fd8068cfd4mr186096pjr.114.1661898017506; Tue, 30 Aug 2022 15:20:17 -0700 (PDT) Date: Tue, 30 Aug 2022 22:19:50 +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-8-sagis@google.com> Subject: [RFC PATCH v2 07/17] KVM: selftest: TDX: Add basic get_td_vmcall_info 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=ham 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 calls get_td_vmcall_info from the guest and verifies the expected returned values. Signed-off-by: Sagi Shahar --- tools/testing/selftests/kvm/lib/x86_64/tdx.h | 43 +++++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 107 ++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx.h b/tools/testing/selftests/kvm/lib/x86_64/tdx.h index a28d15417d3e..39b000118e26 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_GET_TD_VM_CALL_INFO 0x10000 #define TDX_REPORT_FATAL_ERROR 0x10003 #define TDX_INSTRUCTION_IO 30 @@ -232,6 +233,28 @@ static inline void tdvmcall_fatal(uint64_t error_code) tdcall(®s); } +/* + * Get td vmcall info. + * Used to help request the host VMM enumerate which TDG.VP.VMCALLs are supported. + * Returns return in r10 code and leaf-specific output in r11-r14. + */ +static inline uint64_t tdvmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12, + uint64_t *r13, uint64_t *r14) +{ + struct kvm_regs regs; + + memset(®s, 0, sizeof(regs)); + regs.r11 = TDX_GET_TD_VM_CALL_INFO; + regs.r12 = 0; + regs.rcx = 0x1C00; + tdcall(®s); + *r11 = regs.r11; + *r12 = regs.r12; + *r13 = regs.r13; + *r14 = regs.r14; + return regs.r10; +} + /* * Reports a 32 bit value from the guest to user space using a TDVM IO call. * Data is reported on port TDX_DATA_REPORT_PORT. @@ -244,6 +267,26 @@ static inline uint64_t tdvm_report_to_user_space(uint32_t data) return tdvmcall_io(TDX_DATA_REPORT_PORT, /*size=*/4, TDX_IO_WRITE, &data_64); } +/* + * Reports a 64 bit value from the guest to user space using a TDVM IO call. + * Data is reported on port TDX_DATA_REPORT_PORT. + * Data is sent to host in 2 calls. LSB is sent (and needs to be read) first. + */ +static inline uint64_t tdvm_report_64bit_to_user_space(uint64_t data) +{ + uint64_t err; + uint64_t data_lo = data & 0xFFFFFFFF; + uint64_t data_hi = (data >> 32) & 0xFFFFFFFF; + + err = tdvmcall_io(TDX_DATA_REPORT_PORT, /*size=*/4, TDX_IO_WRITE, + &data_lo); + if (err) + return err; + + return tdvmcall_io(TDX_DATA_REPORT_PORT, /*size=*/4, TDX_IO_WRITE, + &data_hi); +} + #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 3f51f936ea5a..cf8260db1f5b 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -53,6 +53,25 @@ (VCPU)->run->system_event.data[1]); \ } while (0) +static uint64_t read_64bit_from_guest(struct kvm_vcpu *vcpu, uint64_t port) +{ + uint32_t lo, hi; + uint64_t res; + + CHECK_IO(vcpu, port, 4, TDX_IO_WRITE); + lo = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + vcpu_run(vcpu); + + CHECK_IO(vcpu, port, 4, TDX_IO_WRITE); + hi = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + res = hi; + res = (res << 32) | lo; + return res; +} + + /* * There might be multiple tests we are running and if one test fails, it will * prevent the subsequent tests to run due to how tests are failing with @@ -383,6 +402,93 @@ void verify_td_cpuid(void) printf("\t ... PASSED\n"); } +/* + * Verifies get_td_vmcall_info functionality. + */ +TDX_GUEST_FUNCTION(guest_code_get_td_vmcall_info) +{ + uint64_t err; + uint64_t r11, r12, r13, r14; + + err = tdvmcall_get_td_vmcall_info(&r11, &r12, &r13, &r14); + if (err) + tdvmcall_fatal(err); + + err = tdvm_report_64bit_to_user_space(r11); + if (err) + tdvmcall_fatal(err); + + err = tdvm_report_64bit_to_user_space(r12); + if (err) + tdvmcall_fatal(err); + + err = tdvm_report_64bit_to_user_space(r13); + if (err) + tdvmcall_fatal(err); + + err = tdvm_report_64bit_to_user_space(r14); + if (err) + tdvmcall_fatal(err); + + tdvmcall_success(); +} + +void verify_get_td_vmcall_info(void) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + uint64_t r11, r12, r13, r14; + + printf("Verifying TD get vmcall info:\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_get_td_vmcall_info, + TDX_FUNCTION_SIZE(guest_code_get_td_vmcall_info), + 0); + finalize_td_memory(vm); + + /* Wait for guest to report r11 value */ + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + r11 = read_64bit_from_guest(vcpu, TDX_DATA_REPORT_PORT); + + /* Wait for guest to report r12 value */ + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + r12 = read_64bit_from_guest(vcpu, TDX_DATA_REPORT_PORT); + + /* Wait for guest to report r13 value */ + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + r13 = read_64bit_from_guest(vcpu, TDX_DATA_REPORT_PORT); + + /* Wait for guest to report r14 value */ + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + r14 = read_64bit_from_guest(vcpu, TDX_DATA_REPORT_PORT); + + ASSERT_EQ(r11, 0); + ASSERT_EQ(r12, 0); + ASSERT_EQ(r13, 0); + ASSERT_EQ(r14, 0); + + /* Wait for guest to complete execution */ + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + CHECK_GUEST_COMPLETION(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { if (!is_tdx_enabled()) { @@ -394,6 +500,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_report_fatal_error); run_in_new_process(&verify_td_ioexit); run_in_new_process(&verify_td_cpuid); + run_in_new_process(&verify_get_td_vmcall_info); return 0; } -- 2.37.2.789.g6183377224-goog