Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp4722204rwe; Tue, 30 Aug 2022 16:01:03 -0700 (PDT) X-Google-Smtp-Source: AA6agR5M+Q4F7StRz4ybsx7jNcaIIWIYpG4irqETppieEA92AOC20iOLifO2Bww2RwFib9q9llun X-Received: by 2002:a05:6402:3591:b0:448:a15e:3ca0 with SMTP id y17-20020a056402359100b00448a15e3ca0mr7208248edc.195.1661900463156; Tue, 30 Aug 2022 16:01:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661900463; cv=none; d=google.com; s=arc-20160816; b=jwmWRKKzowuDB+bLxxS+R87LrJpKVlWlg3tuZSLHku/IJ8nYjyl6F5tuCYsM5P/G1D 2X7kkEY9zlym1ejwfn4ZqGEYoO+8yTlEBpLk4FAtC9pi910dd8mh6pjQVNQow/yGeOON sn0gEOPxYiwtqhF0m19+iYbk+V46QKAEXCCaNbrBqE35XJNtodae+uWDArWXv9onrajg Uh1OFugNPHA5OHnAPG1MnKUu4rNyIceXNvHFwPc48Gh7JF/0Qxtxx3ZgYf6EssO0H8Np 9AVF0lZT1Rk9TzxFXWyAdvSHqrv60DSh/OQSdFhTMpM8APGGNY+E5JdNYPdlBDLKwSay 8Q9A== 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=t1EJEc3ehuP8VgtxlwMAMy4e8zFNWLY0+qkN4qZlgjo=; b=iJQmTfqePHBEH7yvMHWy+xG+FzrHOOAhY+B2qbYBuEfpCtG4SpRAF/qMJVSYaz/Ds2 Klk5PDCQ8x2J0kRdlfiElIFoHICX0ZZXNPLEaWL1KGtUo1QGOpXU6ShR8ojcs7oig1sq DHyiz7I7yGCXFTMIStbbcd086dO8pr/0XUtnF3ks7rIKF4JJQYBKXEUmUg3bWI5QzBrS mdt6s8U5Vf42BABYH3wF6gmBwvWEHuefamIvOBEAopvr9yMdeHUG0DOHhGWuS6uUelJv jMCcCmUSo3LcpWDHaL5ZgsQSh//9miuNQ0V7QrL04cv0XP0rnv/Tg03e9IhiwIfc9vQL AWig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=OBDHMiVs; 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 sg14-20020a170907a40e00b00730f3cb968fsi9313880ejc.990.2022.08.30.16.00.37; Tue, 30 Aug 2022 16:01:03 -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=OBDHMiVs; 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 S232251AbiH3WUp (ORCPT + 99 others); Tue, 30 Aug 2022 18:20:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231965AbiH3WUR (ORCPT ); Tue, 30 Aug 2022 18:20:17 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A4712E9DE for ; Tue, 30 Aug 2022 15:20:15 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id n1-20020a170902d2c100b0017520dd4a0eso1724605plc.8 for ; Tue, 30 Aug 2022 15:20:15 -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=t1EJEc3ehuP8VgtxlwMAMy4e8zFNWLY0+qkN4qZlgjo=; b=OBDHMiVsduqJn82HBATnhpkGshEMFhpVZd04lCP+8vCRmGXewLCmigJbYMcHR2G1Wg +1Jp0anvPji+d03Qe1im6ULPefCWNZxTRGuYy1fmOcQDpL+aa60iN43yHrmxPsTe47Lz n0ZFXEzIqvp6UzBWc088o439BDnM4TCo/tLCOgVz8zadA67XwGMERiEPcIyK+BRZpsLi AxREiplpSLOHm7UHaBzpEo0sw0HIKQ3Gsv1eW/xGP6Tf9RDyUF55ezGrN/oO204ZnPif E2kdK9vpGN5AC9zHWegVB8elrEBJDwd6bK7V8/y9BJ0ywKUFP2pPlVZ3S8T9TdzVsk5M Ol8A== 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=t1EJEc3ehuP8VgtxlwMAMy4e8zFNWLY0+qkN4qZlgjo=; b=34PHLdqf72TzlqUH4a/HzF3rQZK7rGs6QPsSBcVJXmUOv7tEw0bpFynLZ4XsQMUbeB arl46+aXVwY3g+xTh6W6LVpX7b1tn/w3QRlqFjSINqV727O9mX0vTPWEULjv8occKwoi VpZEg+rOYICiB7Okjdg+JIWFl2c7kaB0v/3TbiczdOFQIs9fUToBE0YTMFZjRueJOd0r LV/anZKy9eKjPpra5fjA8R7NudQin1MFL+sWn1yoCcNnO1kD4JnACwbimZ6iuTnVO0y/ iJZTTETyRr2W+HXyzllimhjcAmRAnpqSC9hDRT2W/1lFXeaqX2fDMUKN0f8OtSS8Kn9r 6VAA== X-Gm-Message-State: ACgBeo3zTFi2g0hHKUWkPdsPNI056IqKJ8rrT23BpapnjLuSkIGZxHLp d7p3nU3wMgaUECKAYsehCz5IbHmn2w== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a05:6a00:1a89:b0:536:5dca:a673 with SMTP id e9-20020a056a001a8900b005365dcaa673mr23237123pfv.71.1661898014056; Tue, 30 Aug 2022 15:20:14 -0700 (PDT) Date: Tue, 30 Aug 2022 22:19:48 +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-6-sagis@google.com> Subject: [RFC PATCH v2 05/17] KVM: selftest: Adding test case for TDX port IO 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 From: Erdem Aktas Verifies TDVMCALL READ and WRITE operations. Signed-off-by: Erdem Aktas Signed-off-by: Sagi Shahar --- tools/testing/selftests/kvm/lib/x86_64/tdx.h | 1 + .../selftests/kvm/x86_64/tdx_vm_tests.c | 108 ++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx.h b/tools/testing/selftests/kvm/lib/x86_64/tdx.h index 351ece3e80e2..c78dba1af14f 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx.h +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx.h @@ -55,6 +55,7 @@ #define TDX_INSTRUCTION_IO 30 #define TDX_SUCCESS_PORT 0x30 +#define TDX_TEST_PORT 0x31 #define TDX_IO_READ 0 #define TDX_IO_WRITE 1 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 1db5400ca5ef..a93629cfd13f 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -27,6 +27,32 @@ (VCPU)->run->exit_reason, exit_reason_str((VCPU)->run->exit_reason), \ (VCPU)->run->io.port, (VCPU)->run->io.size, (VCPU)->run->io.direction)) +#define CHECK_IO(VCPU, PORT, SIZE, DIR) \ + do { \ + TEST_ASSERT((VCPU)->run->exit_reason == KVM_EXIT_IO, \ + "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n", \ + (VCPU)->run->exit_reason, \ + exit_reason_str((VCPU)->run->exit_reason)); \ + \ + TEST_ASSERT(((VCPU)->run->exit_reason == KVM_EXIT_IO) && \ + ((VCPU)->run->io.port == (PORT)) && \ + ((VCPU)->run->io.size == (SIZE)) && \ + ((VCPU)->run->io.direction == (DIR)), \ + "Got an unexpected IO exit values: %u (%s) %d %d %d\n", \ + (VCPU)->run->exit_reason, \ + exit_reason_str((VCPU)->run->exit_reason), \ + (VCPU)->run->io.port, (VCPU)->run->io.size, \ + (VCPU)->run->io.direction); \ + } while (0) + +#define CHECK_GUEST_FAILURE(VCPU) \ + do { \ + if ((VCPU)->run->exit_reason == KVM_EXIT_SYSTEM_EVENT) \ + TEST_FAIL("Guest reported error. error code: %lld (0x%llx)\n", \ + (VCPU)->run->system_event.data[1], \ + (VCPU)->run->system_event.data[1]); \ + } while (0) + /* * There might be multiple tests we are running and if one test fails, it will * prevent the subsequent tests to run due to how tests are failing with @@ -145,6 +171,87 @@ void verify_report_fatal_error(void) printf("\t ... PASSED\n"); } +/* + * Verifies IO functionality by writing a |value| to a predefined port. + * Verifies that the read value is |value| + 1 from the same port. + * If all the tests are passed then write a value to port TDX_TEST_PORT + */ +TDX_GUEST_FUNCTION(guest_io_exit) +{ + uint64_t data_out, data_in, delta; + uint64_t ret; + + data_out = 0xAB; + + ret = tdvmcall_io(TDX_TEST_PORT, 1, TDX_IO_WRITE, &data_out); + if (ret) + tdvmcall_fatal(ret); + + ret = tdvmcall_io(TDX_TEST_PORT, 1, TDX_IO_READ, &data_in); + if (ret) + tdvmcall_fatal(ret); + + delta = data_in - data_out; + if (delta != 1) + tdvmcall_fatal(ret); + + tdvmcall_success(); +} + +void verify_td_ioexit(void) +{ + struct kvm_vcpu *vcpu; + uint32_t port_data; + struct kvm_vm *vm; + + printf("Verifying TD IO Exit:\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_exit, + TDX_FUNCTION_SIZE(guest_io_exit), 0); + finalize_td_memory(vm); + + /* Wait for guest to do a IO write */ + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + CHECK_IO(vcpu, TDX_TEST_PORT, 1, TDX_IO_WRITE); + port_data = *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + printf("\t ... IO WRITE: OK\n"); + + /* + * Wait for the guest to do a IO read. Provide the previos written data + * + 1 back to the guest + */ + 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) = port_data + 1; + + printf("\t ... IO READ: OK\n"); + + /* + * Wait for the guest to complete execution successfully. The read + * value is checked within the guest. + */ + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + CHECK_GUEST_COMPLETION(vcpu); + + printf("\t ... IO verify read/write values: OK\n"); + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + + int main(int argc, char **argv) { if (!is_tdx_enabled()) { @@ -154,6 +261,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_td_lifecycle); run_in_new_process(&verify_report_fatal_error); + run_in_new_process(&verify_td_ioexit); return 0; } -- 2.37.2.789.g6183377224-goog