From patchwork Tue Dec 12 20:46:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177570 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993833vqy; Tue, 12 Dec 2023 12:48:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IEsJhbf2InsxUTOwbGLO43RcNYMSr2NDz+dKjTyGSWKtwZ1BUlsrHSBM3Fcg0MW4DeVd9BA X-Received: by 2002:a05:6e02:15c5:b0:35d:59a2:bd2 with SMTP id q5-20020a056e0215c500b0035d59a20bd2mr6033065ilu.104.1702414133118; Tue, 12 Dec 2023 12:48:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414133; cv=none; d=google.com; s=arc-20160816; b=EHJgvzZHfuZ1lW25MpCSqJpUI5/IhWowWMWHLWpxN+X/b1Vtwp4UDXF4tRMDEmLj7f BTyahdChA/+WY6DXdEEDS8ogHLp971jjGJPvE+Dj4Qyu3Z53hDLgt2D0BxKR+l9KJ3Jx 29Be+jApiD5Pjtg8XcJKy+8/zPZE375/6OvOB8LypFkebjPEMFnNo2NB1jGXmZ/2yJXI 9pgXQRoQZPu22Ul39T5BnHLWC4t4ZXjEwSUTIMy8bGOSp81sjD+JE6rm5rvAYHp1gLRC SCOaK30iuNC/+CGs206+hvw/FH+oFVR9sNBoHlz1idksGxOSs3XsolC0fAJDyLYlEFHf HCjw== 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=mgr9hweDYRXBGfxKvpySsIK8PkryOfdG9AXrBZT3Fps=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=V9GlIbSHsFG8GzkS9Gg31ssiqWHbwrhuES6Kv6EQEvUMJiR5ZS5lnY2MRYmkMWkdKi esu6lXrK/REaZEqRP6+uq+2MpWpWnpc1q0//mAvpfBgzXyaF/T2XWK9jSQoMVOBRK7dx +RIZRwJbyR0tqCrkw7M8ORuANGmhvb6iH9E6KKgqTFQwk0d4ErQ96wKZGlRRoOIotiUQ gm//lHqanERWL6hhGsgD2MtKrrcYNqfejrt4/ffUfilc9erBTrBfr7bj1I+eZsA834/p gtSQVCICTiaNs7L/hkBLxL790n4oMrc+2RL+5FwPtMm3WqChcWt63/9Jl9Y/QiLaC1+m F0Zw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JY5LJD4K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id o2-20020a655202000000b005898d997f52si8410342pgp.66.2023.12.12.12.48.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:48:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JY5LJD4K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (Postfix) with ESMTP id BD2B28023778; Tue, 12 Dec 2023 12:48:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377422AbjLLUsj (ORCPT + 99 others); Tue, 12 Dec 2023 15:48:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235180AbjLLUsM (ORCPT ); Tue, 12 Dec 2023 15:48:12 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F296173C for ; Tue, 12 Dec 2023 12:47:34 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5e175c8b326so21388587b3.2 for ; Tue, 12 Dec 2023 12:47:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414051; x=1703018851; 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=mgr9hweDYRXBGfxKvpySsIK8PkryOfdG9AXrBZT3Fps=; b=JY5LJD4K90PQbgBzv1Xi3PdxBwRktSYWxAyB2m/rKQENg6cmOMn5286I2Aa8qD26Df Z6CTSwRuS6uuBZ2I4GH3wnIvyBoFdlIAFVCntwrIQ3HLaUSAuy1ot2kACYBQtAqf0XgV kFXABvX0nU9ytQLPphvQOANuBNukaDL07BNJ5BpEVwaVem6glx87kXqI/7Qorbo19ZQi bJPysTemt6ziby4h5aE0BC7hIydxCDjoUE9XPFEwru8GwPB6N8FOW8AvcmffAoTwMaQm i2apSmHCs9ekOA+vJ86RA8ce+AlV6pzaksgNDRUqf416mpeVi2+jd5rnFOrQ4RGbU6FU Yg1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414051; x=1703018851; 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=mgr9hweDYRXBGfxKvpySsIK8PkryOfdG9AXrBZT3Fps=; b=UUQYeaHlSfDxCUo+mzFWd28ihOSH1fpfy+lToS1B3Xd1QSQi5zatUeYSvPU47X11NW 8ii33kW/iWAknjnwPqt1rL9oFDm73cBGmmicjvrzw+5wDzI7r4TNRnSI7ryNgVYAqFrT 6XoXkaHzZ7m4zeRJB6U4doJDpSwX9hJ9yyzgAd1YvOaQmdH3OA2Wcj3nQ0yWnoKyRoLD g9QUYDNHie3qhqpB4bSN2+Q0rENiSF5ZIeANDv8rxZiM+lqCrd/7BjB3bx3N+XIvi2E6 AquzvbLfKmxSYpSXeUg3XcKPSi1bzWA6ekc2hb0/iwnFUfITJ2JvOEtGn81uBYgUgfOa jAYg== X-Gm-Message-State: AOJu0Yzhtc2gixuocIBTU3j23VAwwioQ1nKYeyrtL6bueqOOS9UkoC5v LM1zriFx5MIgv5PBXGyfR3HMMFTK+g== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a05:690c:3506:b0:5d9:447a:43e8 with SMTP id fq6-20020a05690c350600b005d9447a43e8mr67267ywb.5.1702414051607; Tue, 12 Dec 2023 12:47:31 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:33 -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-19-sagis@google.com> Subject: [RFC PATCH v5 18/29] KVM: selftests: TDX: Add TDX MMIO writes test 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 pete.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 (pete.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:48:48 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110601723046990 X-GMAIL-MSGID: 1785110601723046990 The test verifies MMIO writes of various sizes from the guest to the host. Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji --- .../selftests/kvm/include/x86_64/tdx/tdx.h | 2 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 14 +++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 85 +++++++++++++++++++ 3 files changed, 101 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index 13ce60df5684..502b670ea699 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -25,5 +25,7 @@ uint64_t tdg_vp_vmcall_instruction_wrmsr(uint64_t index, uint64_t value); uint64_t tdg_vp_vmcall_instruction_hlt(uint64_t interrupt_blocked_flag); uint64_t tdg_vp_vmcall_ve_request_mmio_read(uint64_t address, uint64_t size, uint64_t *data_out); +uint64_t tdg_vp_vmcall_ve_request_mmio_write(uint64_t address, uint64_t size, + uint64_t data_in); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index b19f07ebc0e7..f4afa09f7e3d 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -143,3 +143,17 @@ uint64_t tdg_vp_vmcall_ve_request_mmio_read(uint64_t address, uint64_t size, return ret; } + +uint64_t tdg_vp_vmcall_ve_request_mmio_write(uint64_t address, uint64_t size, + uint64_t data_in) +{ + struct tdx_hypercall_args args = { + .r11 = TDG_VP_VMCALL_VE_REQUEST_MMIO, + .r12 = size, + .r13 = TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE, + .r14 = address, + .r15 = data_in, + }; + + return __tdx_hypercall(&args, 0); +} 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 48902b69d13e..5e28ba828a92 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -885,6 +885,90 @@ void verify_mmio_reads(void) printf("\t ... PASSED\n"); } +void guest_mmio_writes(void) +{ + uint64_t ret; + + ret = tdg_vp_vmcall_ve_request_mmio_write(TDX_MMIO_TEST_ADDR, 1, 0x12); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_ve_request_mmio_write(TDX_MMIO_TEST_ADDR, 2, 0x1234); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_ve_request_mmio_write(TDX_MMIO_TEST_ADDR, 4, 0x12345678); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_ve_request_mmio_write(TDX_MMIO_TEST_ADDR, 8, 0x1234567890ABCDEF); + if (ret) + tdx_test_fatal(ret); + + // Write across page boundary. + ret = tdg_vp_vmcall_ve_request_mmio_write(PAGE_SIZE - 1, 8, 0); + if (ret) + tdx_test_fatal(ret); + + tdx_test_success(); +} + +/* + * Varifies guest MMIO writes. + */ +void verify_mmio_writes(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + uint8_t byte_1; + uint16_t byte_2; + uint32_t byte_4; + uint64_t byte_8; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_mmio_writes); + td_finalize(vm); + + printf("Verifying TD MMIO writes:\n"); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 1, TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE); + byte_1 = *(uint8_t *)(vcpu->run->mmio.data); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 2, TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE); + byte_2 = *(uint16_t *)(vcpu->run->mmio.data); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 4, TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE); + byte_4 = *(uint32_t *)(vcpu->run->mmio.data); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 8, TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE); + byte_8 = *(uint64_t *)(vcpu->run->mmio.data); + + TEST_ASSERT_EQ(byte_1, 0x12); + TEST_ASSERT_EQ(byte_2, 0x1234); + TEST_ASSERT_EQ(byte_4, 0x12345678); + TEST_ASSERT_EQ(byte_8, 0x1234567890ABCDEF); + + td_vcpu_run(vcpu); + TEST_ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT); + TEST_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); @@ -905,6 +989,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_guest_msr_reads); run_in_new_process(&verify_guest_hlt); run_in_new_process(&verify_mmio_reads); + run_in_new_process(&verify_mmio_writes); return 0; }