From patchwork Tue Dec 12 20:46:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177574 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7994085vqy; Tue, 12 Dec 2023 12:49:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IEzziuA6jGOznoxrpzI6ExUVVkw8eEIcogOjo6PqjCIgvLeTLzVU4oe0q1nq7/GtN1zawsI X-Received: by 2002:a17:90a:8d86:b0:286:b91c:7774 with SMTP id d6-20020a17090a8d8600b00286b91c7774mr3571329pjo.47.1702414168917; Tue, 12 Dec 2023 12:49:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414168; cv=none; d=google.com; s=arc-20160816; b=LVKUPP+mzoGjzUq5fjSWTn/5PVsPxmA5INpcUXqTF8ktS4TiXK2D7KhoRVa4M1KE6w qR9ZPfRNsNJk+FcxhjhvBRp3JsXdgkPBXbWlRD6Rf8DPCMRF10G/FYEdKCKgNTD7Cbt4 kMwZbWkRW+sozhWJKmXjWxlBVbWmg6YsG9I/UEOx1T/b4HpqB/qzwQlUz6Be8rzoqaFz Ag3iKUpFuxyY13Oam/wLsFlqa+HgA5VyQl/YSZ+RL3oxByyRG30ZkeLsN7DdibK7D5AO 75ufwWZLhvAIPYBDdn7e0BdTQlqe7cVVZ2pO55oy8QwkQZ6pL5Q1JihtyoVYOf3U8Vk3 de+g== 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=OBiOJZf7KtnyH3BNNYJPAadRM1tNvWRCeEycOvMN+pU=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=oyW5b1zXK1kKBxfkh4YT2NS+UOdCQj1Hs6qRs3R1LbO0lb8q04pJuAgWjH1H/R2hDr 4lSmtuNqxBMfCDtlzaOoD9fJINpDWH1+iSZe5dhU+mMKAxXPq5jwJqBuI8Jo/Ay8jLxE cEKGLWRW+E3l0tmwueoxsV81rnTfgrRpkLqmui6glFaDRU8skKWUzbmheSByahWGkN5E 003q1aQa6+p3V/tTR32vBR2wry2uuh1uL0Thf8CRI3IH58kZWLqNEfNRm/JxsoXagNhV X3/3wOz7/FnYesurGweIlRsQI0AOMrHoAJjY0COQlVs+LH3h/Z/Hwph9oxk/H3aT+tfD mhPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NnfZGkQ2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id lb15-20020a17090b4a4f00b00288657cdf5fsi9729175pjb.101.2023.12.12.12.49.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:49:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NnfZGkQ2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 87C2B80FFDBE; Tue, 12 Dec 2023 12:49:26 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377486AbjLLUtG (ORCPT + 99 others); Tue, 12 Dec 2023 15:49:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235218AbjLLUsd (ORCPT ); Tue, 12 Dec 2023 15:48:33 -0500 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 EAA9BD7F for ; Tue, 12 Dec 2023 12:47:37 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1d34b1ea914so696315ad.1 for ; Tue, 12 Dec 2023 12:47:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414055; x=1703018855; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OBiOJZf7KtnyH3BNNYJPAadRM1tNvWRCeEycOvMN+pU=; b=NnfZGkQ2apcvX/2Rd3wgDpv90mk+ufZQ8ErQE1WRuNh7O7FOHKDouuaO8Y0HEb13nC 8J7M1U2ShFpL3tkcbujZmF66Ydy/6+t2NxmnLxtc/GQE2AaewNFpqTQWZK2pBZLSK8Ex hcTeeyQ/XbJSUYO1UHpe4phyzaFKjz3vrCx/OJYjxSZ2/Rbkma+CQazO4a6Sf9WDxUHj 4trDNFvTQhAjwPZ4he6tN5JhCT/9DuvijaPfNmOIqe/wRs9tGdaaTgDZSg5gAFQ0SGpf UyOJwUbyNgaBlzc+ztHy/2O1RqYXHd6ij9cCKR5eYI3KlkiKGR79SG6uXEPR9OIz2E6n 85aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414055; x=1703018855; 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=OBiOJZf7KtnyH3BNNYJPAadRM1tNvWRCeEycOvMN+pU=; b=sd0SaFFMkPViUgM4/26gtRoYt2ydgbJpNWIrBxgWp/fona7/A1vgzjX4bpiaZHk+tX 40QLuFGJTFxOuNbGe3L2ip/nVgq2kaD1RCpU+OrSyFxzPrjqc/YAP7Lc+i0A9g4lvevD 8ZHZgSrC69ivb8NL21Yvt4Dfdaj2KW9bERaklk8+LMFrux87CZV6c/DsvSFn0x5zs5SF NKg6te/OxJIcsjtGNQuuV6AdK9RdbRvubM+DRXRDSv5r2jS18bMmQktlheSx9j4KFxB4 TEwP1umlgQlPUeVxb7oPNbyKPYvma9qAkEZzgJIsSgIckJVviy2yWkz1+VFKZDC3Kga+ yhAQ== X-Gm-Message-State: AOJu0Yw8BoVxW9bDXzL2rBtB7ggTi4T567zs4Mt9RDwTtc+BFZS9Pt2U xSKD/M3NyRX91SQaiCEH+aEvzb9i1w== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:cec5:b0:1d2:f388:6def with SMTP id d5-20020a170902cec500b001d2f3886defmr52709plg.10.1702414055001; Tue, 12 Dec 2023 12:47:35 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:35 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-21-sagis@google.com> Subject: [RFC PATCH v5 20/29] KVM: selftests: TDX: Verify the behavior when host consumes a TD private memory From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,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 morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:49:26 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110639479005345 X-GMAIL-MSGID: 1785110639479005345 From: Ryan Afranji 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 --- .../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 6935604d768b..c977223ff871 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; }