Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp235724rwb; Tue, 25 Jul 2023 15:05:45 -0700 (PDT) X-Google-Smtp-Source: APBJJlEDoNAkldberd07nKH2TcCQ/ZEM05F+qxqMpoQF4OCDUnNXr/hoCnngfwOHA8E31GRmLrJb X-Received: by 2002:a05:6a00:2d09:b0:667:e17e:85c1 with SMTP id fa9-20020a056a002d0900b00667e17e85c1mr527992pfb.1.1690322745303; Tue, 25 Jul 2023 15:05:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690322745; cv=none; d=google.com; s=arc-20160816; b=v5I9sCin+LZDCpSG5mL+K/mmCg1NNmMbG1VNR/tdG/2IIBacAzKSxBhq+SyhBv5/Qm bFGYbDO+93uFwNopvUCpfgce/s7WA+izE7vHWphq9/QGUixu+ADL0Zg28IDyw/i2/GZl s9NF1tzO9tsNquIHc8DIgxTjmyeaCChwLDpLUViQ5paFZsm7Zn3pYzF3vrSqegMZnqKk g2ZBf+H4kmCibD485wDhcXmc2ezMQesfXVt/SFci3KnDTjW1H5OLq0pQ/kZHy4VJ+Mjv w76e2JfoSN7+TjJPOMElJOPIjL3u7ixFU88tqUXbjgY9P1nQOx9G0/4uJF+VNOMPWwn8 dhgg== 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=9K41uZJPRAaLfZAxcyXvJMZOSv31CqLM2GpGiII81Fk=; fh=1/sgOwi5AuU8IZymeFKs1Rjnf4QELnrK+C7GntqU2hA=; b=pwgxRrvgnM5rRH+77QBXsNY6hKV10SfoTgksO5WSDc9h1DFw4C7aY7kVFF/XO90iYs D9upk4aSPYEmMXC67KIW/bKAUq/OINVTj0PyORIozh5GZaNYyKwEu/6Jgh6He1wwR/E3 NCPhUnBxM3P9cRF37rGTjwKq0SiryPbMYp6vG6WhC/PIdJBqSoH4ZU20UEwXkl4oowma lGDVSDiGtdX++JG0wxaA+KwWfkzXR6QAYCOAOC4po+j0tB54mJhvQGQf7U0nncMvO8Kj jgoEvRi0RL7gu6NNVqmfgO/lTPLAHOjjQM0+HDVa4Dob9tl104VzqvaJC8aIlpgLFPBC Gntw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="JTk9de4/"; 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 o5-20020a056a001bc500b0066e4ceca385si11529518pfw.247.2023.07.25.15.05.33; Tue, 25 Jul 2023 15:05:45 -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="JTk9de4/"; 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 S229982AbjGYWET (ORCPT + 99 others); Tue, 25 Jul 2023 18:04:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232018AbjGYWDH (ORCPT ); Tue, 25 Jul 2023 18:03:07 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2D5B358D for ; Tue, 25 Jul 2023 15:02:38 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-cab7304dcccso6783396276.3 for ; Tue, 25 Jul 2023 15:02:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690322546; x=1690927346; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=9K41uZJPRAaLfZAxcyXvJMZOSv31CqLM2GpGiII81Fk=; b=JTk9de4/K946u8Ay850Ku0iIgatOSwKmZ/i3BV7a+jW4VknP2oV0EIgnk12pyHrwGh 2MSWY6/f7UiPYJyMQEKZa7TZf5eqN4nX8xSH5LpxDf4iGp7YF75id+IDZXdMTO3Yhx3B zRTNGbRsvYuRm3C4lVfXH8nJStrRylftYvpUkafZoYSVvlETwo+SpNkqhRNeDXIG7Se4 eK9p8lKew0Ad2XFhnXRW2gUCQI8aI/6ELrUH4z2LRvVaXIxIlNpjE5k8IG34DuIOpOd+ QBU0RdwVCFH5d9zOMZcV0hdMaDb7KYD8+EgVVTzqEPKZmLTJQWUIe1hNcoRjw/+0Klta ovPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690322546; x=1690927346; 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=9K41uZJPRAaLfZAxcyXvJMZOSv31CqLM2GpGiII81Fk=; b=TEqrHOQO8p986fiBHPF5D/FM5mLmWIlsxOY2iUrBDlX5SegqdSVHuaCSzzn2QDTgkW 8+rAz1kSRwt9/2m8x+hbT7WXu6mK9UNA0+XA55vaN+QLpF7csS0k0yOMW1wolLL+mK9B Pu7rH7lblidi1SoyTBpzRqDQwBoemaOWA9UByv89Acno7j5Gjg79DSvWD1R53HzzUgRJ yuWNCr5yfyninop0tJGdTeGKY+q5pCXKt3gJDPtq1WXAqvIPu9nc8c2M7dlT0GBmUrNo RFQbFhN0alZhLSE7jo41F28SjDi0gg8S47ynk3vIQO3IWfqKMVeeUywx009zP7/8mPuq lUpg== X-Gm-Message-State: ABy/qLauNUKI8qjq+/lSL1o6Fvq4i2fspXhQJAf+iSUVxb3fStbLrbJC V8IirBzFQd1vaTOMkQ3GnO7YcDCsK28M X-Received: from afranji.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:47f1]) (user=afranji job=sendgmr) by 2002:a05:6902:56a:b0:d0b:ca14:33fd with SMTP id a10-20020a056902056a00b00d0bca1433fdmr1962ybt.8.1690322546274; Tue, 25 Jul 2023 15:02:26 -0700 (PDT) Date: Tue, 25 Jul 2023 22:01:13 +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-21-afranji@google.com> Subject: [PATCH v4 20/28] KVM: selftests: TDX: Verify the behavior when host consumes a TD private memory 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 The test checks that host can only read fixed values when trying to access the guest's private memory. Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Change-Id: Ib30f58764c54122bf554639f0b8adf24b0438b5c --- .../selftests/kvm/x86_64/tdx_vm_tests.c | 85 +++++++++++++++++++ 1 file changed, 85 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 a6da9fda1c6b..36cc735fad30 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -1062,6 +1062,90 @@ void verify_td_cpuid_tdcall(void) printf("\t ... PASSED\n"); } +/* + * Shared variables between guest and host for host reading private mem test + */ +static uint64_t tdx_test_host_read_private_mem_addr; +#define TDX_HOST_READ_PRIVATE_MEM_PORT_TEST 0x53 + +void guest_host_read_priv_mem(void) +{ + uint64_t ret; + uint64_t placeholder = 0; + + /* Set value */ + *((uint32_t *) tdx_test_host_read_private_mem_addr) = 0xABCD; + + /* Exit so host can read value */ + ret = tdg_vp_vmcall_instruction_io( + TDX_HOST_READ_PRIVATE_MEM_PORT_TEST, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, &placeholder); + if (ret) + tdx_test_fatal(ret); + + /* Update guest_var's value and have host reread it. */ + *((uint32_t *) tdx_test_host_read_private_mem_addr) = 0xFEDC; + + tdx_test_success(); +} + +void verify_host_reading_private_mem(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + vm_vaddr_t test_page; + uint64_t *host_virt; + uint64_t first_host_read; + uint64_t second_host_read; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_host_read_priv_mem); + + test_page = vm_vaddr_alloc_page(vm); + TEST_ASSERT(test_page < BIT_ULL(32), + "Test address should fit in 32 bits so it can be sent to the guest"); + + host_virt = addr_gva2hva(vm, test_page); + TEST_ASSERT(host_virt != NULL, + "Guest address not found in guest memory regions\n"); + + tdx_test_host_read_private_mem_addr = test_page; + sync_global_to_guest(vm, tdx_test_host_read_private_mem_addr); + + td_finalize(vm); + + printf("Verifying host's behavior when reading TD private memory:\n"); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_HOST_READ_PRIVATE_MEM_PORT_TEST, + 4, TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + printf("\t ... Guest's variable contains 0xABCD\n"); + + /* Host reads guest's variable. */ + first_host_read = *host_virt; + printf("\t ... Host's read attempt value: %lu\n", first_host_read); + + /* Guest updates variable and host rereads it. */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + printf("\t ... Guest's variable updated to 0xFEDC\n"); + + second_host_read = *host_virt; + printf("\t ... Host's second read attempt value: %lu\n", + second_host_read); + + TEST_ASSERT(first_host_read == second_host_read, + "Host did not read a fixed pattern\n"); + + printf("\t ... Fixed pattern was returned to the host\n"); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -1084,6 +1168,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_mmio_reads); run_in_new_process(&verify_mmio_writes); run_in_new_process(&verify_td_cpuid_tdcall); + run_in_new_process(&verify_host_reading_private_mem); return 0; } -- 2.41.0.487.g6d72f3e995-goog