Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp262848rwb; Tue, 25 Jul 2023 15:37:54 -0700 (PDT) X-Google-Smtp-Source: APBJJlHNj8+K/DLfH530zNrsUdkD0IbDTyz2DbKzCg44RF0GKQ7Q7J9dWB1kGiQ7XNoIeQhi2Mph X-Received: by 2002:a17:902:f54d:b0:1b8:30d8:bc49 with SMTP id h13-20020a170902f54d00b001b830d8bc49mr563468plf.45.1690324674036; Tue, 25 Jul 2023 15:37:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690324674; cv=none; d=google.com; s=arc-20160816; b=BYxxlp6EsNdxCqe3jXRbIq+458cjoBFKfT5J7GUOa4ZY0eZqtGg+vviyaxExvQRTad AmIkLcF7sOF5mEBQWC3LuCuR1Ql05s2tyjmgq1yGF9jvJnx3rQ9TNCTLDIza63ukuI4x kvswc4I3OxqVm+npTTJ2AMMob/6d96vtQ45ITg0DPDWE9l16s4A1WsRfn6kGDZHrCzFz dkduoED8V/PIRMBsWUGGbuuUVgaP+m+cfqRhWn8L7SwUjl0hu83zJRy9HQwaY7YhKO3b +NPlumjwmKq6UCkiZEzH2xawz2S/baBvnNeFuEQ3c2gGSOiV99STUs8zcStdJRX2jkpU OPcQ== 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=uYb2UntJCk/zn4c108HA9yrg8j3dAHpcDzElziN7WIY=; fh=1/sgOwi5AuU8IZymeFKs1Rjnf4QELnrK+C7GntqU2hA=; b=tWWTMSKSLkMG3Lj+6Sytl8FNozlIJU+7MZBkZ9QmEH6G/Lna6ThUwfuV4G7LACbaeA MhKVw29KKN/mgMHm3qlgsbheRm//l7BpxeFqHzMCmeMrze/zyN2PGJzsbJ5oDuIaSq76 BCyvHeyH3TchVnAUw7kuN4qb+ALWFECKY9At2syqzEdKblwoNChcJdhwN8vO89W5NZ5p hoX2jSaFr5tdyeMV7Kb3sQg4MDUgc25H1lmdo+rkQRjgIDrnEIhfsRDuzg/oEOObhdz6 kClEEX0QfYo+EqRsUSVJLrQhQt09bhKfNb08ZUEkICX7YTWlPGnFg2fP2UK3+4mcAMRL +f6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=xfBM6s26; 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 bd8-20020a170902830800b001b7fa1a9a36si11307813plb.67.2023.07.25.15.37.41; Tue, 25 Jul 2023 15:37:54 -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=xfBM6s26; 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 S231953AbjGYWDv (ORCPT + 99 others); Tue, 25 Jul 2023 18:03:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231911AbjGYWDC (ORCPT ); Tue, 25 Jul 2023 18:03:02 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB84730DD for ; Tue, 25 Jul 2023 15:02:29 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-563ab574cb5so1114930a12.1 for ; Tue, 25 Jul 2023 15:02:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690322534; x=1690927334; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=uYb2UntJCk/zn4c108HA9yrg8j3dAHpcDzElziN7WIY=; b=xfBM6s26jIZR4tKHpN5cd5tGGN3dCvhjj6B+IBCaPz8I7dc7Wrl2gcTdnsmi5cwETW qW+huhKAXm6iJbBQI9IdKm+q01Ajlv5Uv6cOsi2+juIL8bTmUlYqmfLdkRhGwGcwdjh9 Koz1CrduWFY6PCwFQUVcQfkcclozEzBClHSC4HHvIcjMzFYGYHzv6sLApCC6jc6JELJi 47M9z0zeEpPwp+7L8Hw8pkPC55y+YtN43Qxv6CUYKhLKHIbHqKQozX1nFMQtrK/U9Suq +qvq3c61LKNxLONkIXcYdEcey0KelBhjgwng+T/nmwcGbtOrZwo7I1ZYu5wiU6gQkcv1 9uug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690322534; x=1690927334; 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=uYb2UntJCk/zn4c108HA9yrg8j3dAHpcDzElziN7WIY=; b=FxgnyZadur3fYpV0JJ1JTP1mwBXNNxiVEpo8/JWB+2HCLrb2+bXv+doK4aE6xQk5Iv Z411fwExzf9n81YuRm9swJGcfmD3JMM5qQUgg4CrCtbo+XxE9/qdNE1msQ5mmveBF8eQ YSrzHj+wF2ZL1O8mkcy0eUc4olWGmU/V7Kvgnl1lYluZuajNry3uZM4j/N8taDPnU8o3 I1t0zVVRDWEkdRv8Zm2G1YHQ7woSgq+hRDtgQj2lr5g+jk1P1kqqOmQ6kfkMWOk82oBs C4ZEufd8IqRJGi7kqi7DhYXRPrVgOYMBVnD1bVkseUaTyk9zkxkKQPmGK8FVC6xJbk9F GYqQ== X-Gm-Message-State: ABy/qLa0DanT8fdIcfxbxUhSh/9iQkdRp/nEiC5enu+Fk0Wts34I24gg O9aMgjU8rabFHkmhJgX4FS3cHj4zHYgy X-Received: from afranji.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:47f1]) (user=afranji job=sendgmr) by 2002:a63:8c54:0:b0:55b:5c9b:671e with SMTP id q20-20020a638c54000000b0055b5c9b671emr2339pgn.5.1690322534554; Tue, 25 Jul 2023 15:02:14 -0700 (PDT) Date: Tue, 25 Jul 2023 22:01:07 +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-15-afranji@google.com> Subject: [PATCH v4 14/28] KVM: selftests: TDX: Add TDX IO reads 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=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 From: Sagi Shahar The test verifies IO reads of various sizes from the host to the guest. Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Change-Id: If5b43a7b084823707b5b4a546c16800d5c61daaf Signed-off-by: Ryan Afranji --- .../selftests/kvm/x86_64/tdx_vm_tests.c | 87 +++++++++++++++++++ 1 file changed, 87 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 ca0136930775..9ac0b793f8c5 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -429,6 +429,92 @@ void verify_guest_writes(void) printf("\t ... PASSED\n"); } +#define TDX_IO_READS_TEST_PORT 0x52 + +/* + * Verifies IO functionality by reading values of different sizes + * from the host. + */ +void guest_io_reads(void) +{ + uint64_t data; + uint64_t ret; + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_READS_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_READ, + &data); + if (ret) + tdx_test_fatal(ret); + if (data != 0xAB) + tdx_test_fatal(1); + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_READS_TEST_PORT, 2, + TDG_VP_VMCALL_INSTRUCTION_IO_READ, + &data); + if (ret) + tdx_test_fatal(ret); + if (data != 0xABCD) + tdx_test_fatal(2); + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_READS_TEST_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_READ, + &data); + if (ret) + tdx_test_fatal(ret); + if (data != 0xFFABCDEF) + tdx_test_fatal(4); + + // Read an invalid number of bytes. + ret = tdg_vp_vmcall_instruction_io(TDX_IO_READS_TEST_PORT, 5, + TDG_VP_VMCALL_INSTRUCTION_IO_READ, + &data); + if (ret) + tdx_test_fatal(ret); + + tdx_test_success(); +} + +void verify_guest_reads(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_io_reads); + td_finalize(vm); + + printf("Verifying guest reads:\n"); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_READS_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_READ); + *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset) = 0xAB; + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_READS_TEST_PORT, 2, + TDG_VP_VMCALL_INSTRUCTION_IO_READ); + *(uint16_t *)((void *)vcpu->run + vcpu->run->io.data_offset) = 0xABCD; + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_READS_TEST_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_READ); + *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset) = 0xFFABCDEF; + + td_vcpu_run(vcpu); + ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT); + ASSERT_EQ(vcpu->run->system_event.data[1], TDG_VP_VMCALL_INVALID_OPERAND); + + td_vcpu_run(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -444,6 +530,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.41.0.487.g6d72f3e995-goog