Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp4699081rwe; Tue, 30 Aug 2022 15:23:23 -0700 (PDT) X-Google-Smtp-Source: AA6agR5sIJQ5nEbyAHK1RB0wBXh9tcnGa6GfXtdS+gJvQM6LOSgJjugrDxT8om4CMSTN62BNcka5 X-Received: by 2002:a05:6a00:244a:b0:52b:e9a8:cb14 with SMTP id d10-20020a056a00244a00b0052be9a8cb14mr23383954pfj.32.1661898203619; Tue, 30 Aug 2022 15:23:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661898203; cv=none; d=google.com; s=arc-20160816; b=VSHwcuV6lId4XpbR3EFta+8t/509fAI3N2yWEZQD0zGNCTdixVeLHjwFcPfGWUKpFr dt+coki58GZZgzowg//xx+D6EoTiN9LDcICvVN2UP7fOMFXbgGVImgNTzN8T5DaU7BlU LGNqDz/88T7BZv1+O31HtSCgtXjjEDcRjQ8+UimBVmPiRFl0enciS9MxSa0UzM+U3puq Ej667Wy7UHlRUx2UKPiWNOqc5QOylhaK96x37HIq3bVtFx9mqowN9AZAXLsWyngvHdxO cfZhT1c9rUMh5lNjlGaWbqSHZiDX91rMmOtkSGkXztqD2PeYBE1OhFhtoCWGFgyer0t6 wSGg== 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=4AF1UYLjwIVXmrQJvzJrIG8LzqFDnrR9FyDZEzZ+cxA=; b=fMIAI1EgVaKtcDwfT1CusCqoIrh0CYzXgQdTzeJoONG97Rt+wWc89lOesLJeg04aV2 ADjOKqGfLrvPvTyPfjKk6+ALxirl+oVeZJ6//tjhltXg6Y4bnLTAFNQE7PBidcJO30Y7 lD5+l8CTmgUWewzsnh7D+o3Om4gk+6819i+dz5vQUxrSrsd35YoqDtPbXUw6lDn4ju+d Ejz4AgLU3ygZa9gtbJME7eeZWCVwTVJ5o7I/FDI+lKNuPia9Y+GCqMppxzyZ2d4rZwVE knD1B9SuuItHv8j+wpTYsS86enxJoo9ha7KF6Tpqw3WrDh1IjJQgO3df7s4A7FjjvSjq RWrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=BLGMtymf; 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 u12-20020a63600c000000b0041c30d5db34si1412453pgb.640.2022.08.30.15.23.09; Tue, 30 Aug 2022 15:23:23 -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=BLGMtymf; 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 S232327AbiH3WVO (ORCPT + 99 others); Tue, 30 Aug 2022 18:21:14 -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 S232011AbiH3WUi (ORCPT ); Tue, 30 Aug 2022 18:20:38 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E413A58DD5 for ; Tue, 30 Aug 2022 15:20:21 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id s8-20020a170902ea0800b00172e456031eso8664382plg.3 for ; Tue, 30 Aug 2022 15:20:21 -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=4AF1UYLjwIVXmrQJvzJrIG8LzqFDnrR9FyDZEzZ+cxA=; b=BLGMtymfosqoL+BOv1LwQs5tr4NSlaP/SpxDqt3yNRrcO6o4JXX+XCPHk2vXRbtGA+ sMpkWToV2Anu6hegtbXOwvigD8G9kJSB0KajxojvM+GhZDD/5INbIw8CJobpfr+2cDKe +eXhnrrUzrFIsAfYEAHjATunPBBR9lHbGOUe/lmcLCoP+U8QRAxD77rtkBrTXA8UhVnl PkI2bPurJZ36XQdElpU130uGvHicr76h4lVL/z+JjQupAG7njNahc89+zt5Gq8Ovx/kF EE69PgkbCgl6MCCJWFJM1wewVd4IJqa4H31cm2SXygjTyYZGeZd+ttIZJFymsyQGYwoI trJw== 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=4AF1UYLjwIVXmrQJvzJrIG8LzqFDnrR9FyDZEzZ+cxA=; b=eAPcPD4W9RPDdsZ9mnjJlxPGYjRQ3Yxwn1kD73dT/Lzk0x2IUX70BWePC7/BzOkIXE J32PejoXPiLddA8xZiOC+lJMYDCb2Ulee6Ok2QafzS3TwZpDQdbJoCnxDIO0hoHN1zV3 Z7+m5qU89c+YHzpbLQlWhO/50/ByLDf9+TwKj+cMb688UnoY/+/iJLVrMf8RkqMV02vo zS5E2zmW6lvtQ+CIqYkHbXObCgfLXfXevaZIHlsgvnkPMvlKIEV26iKhKOKDnNzIbqAF 6+EY0X0wVEcALOc2MbeV40zZ6DQwlDgBIMiZQQgGEcGPYiXiL42Ylf3HhWhO2jOqRUcl b7TA== X-Gm-Message-State: ACgBeo2I+pI7Z74E1WKXnZ7hXLJ3M+9DZScK5N2P0x88WMbCQ3FFOWOy BesShjKNj0Vrdy6NI/H9Hrm+mTyeSg== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:e5d2:b0:175:fe0:223b with SMTP id u18-20020a170902e5d200b001750fe0223bmr6245168plf.92.1661898020660; Tue, 30 Aug 2022 15:20:20 -0700 (PDT) Date: Tue, 30 Aug 2022 22:19:52 +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-10-sagis@google.com> Subject: [RFC PATCH v2 09/17] KVM: selftest: TDX: Add TDX IO reads 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 verifies IO reads of various sizes from the host to the guest. Signed-off-by: Sagi Shahar --- .../selftests/kvm/x86_64/tdx_vm_tests.c | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) 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 ee60f77fe38e..85b5ab99424e 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -1,5 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only +#include "asm/kvm.h" +#include #include #include #include @@ -573,6 +575,87 @@ void verify_guest_writes(void) printf("\t ... PASSED\n"); } +/* + * Verifies IO functionality by reading values of different sizes + * from the host. + */ +TDX_GUEST_FUNCTION(guest_io_reads) +{ + uint64_t data; + uint64_t ret; + + ret = tdvmcall_io(TDX_TEST_PORT, 1, TDX_IO_READ, &data); + if (ret) + tdvmcall_fatal(ret); + if (data != 0xAB) + tdvmcall_fatal(1); + + ret = tdvmcall_io(TDX_TEST_PORT, 2, TDX_IO_READ, &data); + if (ret) + tdvmcall_fatal(ret); + if (data != 0xABCD) + tdvmcall_fatal(2); + + ret = tdvmcall_io(TDX_TEST_PORT, 4, TDX_IO_READ, &data); + if (ret) + tdvmcall_fatal(ret); + if (data != 0xFFABCDEF) + tdvmcall_fatal(4); + + // Read an invalid number of bytes. + ret = tdvmcall_io(TDX_TEST_PORT, 5, TDX_IO_READ, &data); + if (ret) + tdvmcall_fatal(ret); + + tdvmcall_success(); +} + +void verify_guest_reads(void) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + + printf("Verifying guest reads:\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_io_reads, + TDX_FUNCTION_SIZE(guest_io_reads), 0); + finalize_td_memory(vm); + + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + CHECK_IO(vcpu, TDX_TEST_PORT, 1, TDX_IO_READ); + *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset) = 0xAB; + + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + CHECK_IO(vcpu, TDX_TEST_PORT, 2, TDX_IO_READ); + *(uint16_t *)((void *)vcpu->run + vcpu->run->io.data_offset) = 0xABCD; + + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + CHECK_IO(vcpu, TDX_TEST_PORT, 4, TDX_IO_READ); + *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset) = 0xFFABCDEF; + + vcpu_run(vcpu); + ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT); + ASSERT_EQ(vcpu->run->system_event.data[1], TDX_VMCALL_INVALID_OPERAND); + + 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()) { @@ -586,6 +669,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_td_cpuid); run_in_new_process(&verify_get_td_vmcall_info); run_in_new_process(&verify_guest_writes); + run_in_new_process(&verify_guest_reads); return 0; } -- 2.37.2.789.g6183377224-goog