Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp4699279rwe; Tue, 30 Aug 2022 15:23:43 -0700 (PDT) X-Google-Smtp-Source: AA6agR5d9uQur7lTMuYsWJeYs3xzvSQ7HgGk0bEnAIDcIQKRk1rrZtidpDu+SzaNi9zV6E4hOUTd X-Received: by 2002:a65:6a83:0:b0:42b:344a:3546 with SMTP id q3-20020a656a83000000b0042b344a3546mr19916347pgu.612.1661898223193; Tue, 30 Aug 2022 15:23:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661898223; cv=none; d=google.com; s=arc-20160816; b=YY5GYawq5xl/lSc8Ivs/QRgNqR8k6lK7+SnG6pT0ATpQ8RAnQd+eqCFmSHkIXvmqsQ NlzZ7k5BADcVbxKlr8jXDYMqzid1rv6KP/okwekU+TeBnQR6pHf0nRUivvAGstcD2ifX ImkhD0L/M9t7ycS/eD9vZgtYY0MApw36fbB4KG5PqustVk858T5baUm9ycbyOXOcnBbA VnfoJlBN6uSWv0NbLBl2L/WloX0m5fUOMHEa/qBeWUOgwrusCPh2Q6hFIteGFYMSlN85 YdyY3DRvuK7ct4iFAbbVdXsIktNn33czp16EYai8ddPeNP/cXuiWJfm/SbZHJ0OX5fhz 0bTQ== 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=ZKZyychf2ckUAiVIKhV5N5UZMfwgcYI+ar1kxk3E/L4=; b=zkDXms2dpmdYuEean8vqez9ZddNXWuMaDtdYA5bmk42EitT1FWUo3O4qFwjelIHf/T ZtMbof/6hcRzWFu8M0k8QyuaCH1Uermvw85XyDBlhy3o1GI63TtN6iBws7eT6fs1NnD/ dK7+plC3no2YfH9MsGHehOYCcsjX5l4kXC5Cq4a/rG9jKvK65oZLWfeByb28b+egs0mi /Ms6cNEp86bYCgr2xjQpzLPQGSBoEkn2UzeMtnkHrVR9bceG/sYkk5OHMmXUF7Pyon1q rTVY0L0RAZ4tp1V219nWnaEdVgLdPVxo7Sf+zdq1NxGzf9SBcFPE650hipRwFHOT2No2 IFig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=EApviBTv; 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 u2-20020a63d342000000b0042b9117ba6asi3130174pgi.746.2022.08.30.15.23.31; Tue, 30 Aug 2022 15:23:43 -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=EApviBTv; 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 S230450AbiH3WVK (ORCPT + 99 others); Tue, 30 Aug 2022 18:21:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232137AbiH3WUg (ORCPT ); Tue, 30 Aug 2022 18:20:36 -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 6A4635244B for ; Tue, 30 Aug 2022 15:20:20 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id w1-20020a63d741000000b0042c254a4ccdso3375762pgi.15 for ; Tue, 30 Aug 2022 15:20:20 -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=ZKZyychf2ckUAiVIKhV5N5UZMfwgcYI+ar1kxk3E/L4=; b=EApviBTvewlHWbvbESqFcFdkFYfHnAVWK/NKIk8LewMSDM8v9kNk/x3ajoUqcjYrIi flTf9vFfA3VRfld7AapqlhyC6ac7BhSmpLyRq5FRDcH07mjcgdM5tFQy6e+nmXHbknDX 97ykUwvGQ6BDAtBFkJX3cgf87ZBhPYHXYV1cIeEH6gQWDkvbfq37IvDTgNX3bNgG1vFa myR9YHb1sOhKoPNyNSkvToAFa1ijO+sTHKeIEluMZI0XR6UgJEG72KpAwUwBd/FPJPPQ HEmwn51NHQPeWtEHTXH+M+HVZEcxdnywlVv/L1lBhetPph+3AWUopPOTUULyIDolyFNn ILCg== 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=ZKZyychf2ckUAiVIKhV5N5UZMfwgcYI+ar1kxk3E/L4=; b=MOj5sGcX50XjZbiAgWKtLeNAG53nl2KBsk4Csm/WAL1WghFnPqtNhKO2faOSz9lhnk KKThRM+xYn26Sb+LZIBEaEnm5z3y0YJKmj4+XJzykyt4fT/ADoxudw5z0ZU6ZzEizqLb A/3aKwzyzU9tgZokVS3ddK/vOZ5bMYw+spv7W9aIIQ/Isu5HFgWjf45nlV+xyKbfeD7O wSCMCEZwsSmld9tiy0mplJi12RNJwA7fTtEa3npDkeosZoynlEUC7iqCJRghrjRfcuw4 pf/wvq5FWoPBJLMEbc/bPuXfQ/wo2y8UJgXWmw1my6b0YF0yRzTXyGxf5luFCFeKgNH3 X1eA== X-Gm-Message-State: ACgBeo3IFZs4ec1Nhr+b6gbjkRLqXE506YQ12wOW4VmeHzuI1ltAfCag zplzK9WVxlDyx3pRSdc1IDlzoTttgg== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:90a:e558:b0:1fb:c4b7:1a24 with SMTP id ei24-20020a17090ae55800b001fbc4b71a24mr995pjb.1.1661898018970; Tue, 30 Aug 2022 15:20:18 -0700 (PDT) Date: Tue, 30 Aug 2022 22:19:51 +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-9-sagis@google.com> Subject: [RFC PATCH v2 08/17] KVM: selftest: TDX: Add TDX IO writes test 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 The test verifies IO writes of various sizes from the guest to the host. Google-Bug-Id: 235407183 Signed-off-by: Sagi Shahar --- tools/testing/selftests/kvm/lib/x86_64/tdx.h | 3 + .../selftests/kvm/x86_64/tdx_vm_tests.c | 85 +++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx.h b/tools/testing/selftests/kvm/lib/x86_64/tdx.h index 39b000118e26..f1f44c2ad40e 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx.h +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx.h @@ -51,6 +51,9 @@ #define _PAGE_RW (1UL<<1) /* writeable */ #define _PAGE_PS (1UL<<7) /* page size bit*/ +#define TDX_VMCALL_SUCCESS 0x0000000000000000 +#define TDX_VMCALL_INVALID_OPERAND 0x8000000000000000 + #define TDX_GET_TD_VM_CALL_INFO 0x10000 #define TDX_REPORT_FATAL_ERROR 0x10003 #define TDX_INSTRUCTION_IO 30 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 cf8260db1f5b..ee60f77fe38e 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -489,6 +489,90 @@ void verify_get_td_vmcall_info(void) printf("\t ... PASSED\n"); } +/* + * Verifies IO functionality by writing values of different sizes + * to the host. + */ +TDX_GUEST_FUNCTION(guest_io_writes) +{ + uint64_t byte_1 = 0xAB; + uint64_t byte_2 = 0xABCD; + uint64_t byte_4 = 0xFFABCDEF; + uint64_t ret; + + ret = tdvmcall_io(TDX_TEST_PORT, 1, TDX_IO_WRITE, &byte_1); + if (ret) + tdvmcall_fatal(ret); + + ret = tdvmcall_io(TDX_TEST_PORT, 2, TDX_IO_WRITE, &byte_2); + if (ret) + tdvmcall_fatal(ret); + + ret = tdvmcall_io(TDX_TEST_PORT, 4, TDX_IO_WRITE, &byte_4); + if (ret) + tdvmcall_fatal(ret); + + // Write an invalid number of bytes. + ret = tdvmcall_io(TDX_TEST_PORT, 5, TDX_IO_WRITE, &byte_4); + if (ret) + tdvmcall_fatal(ret); + + tdvmcall_success(); +} + +void verify_guest_writes(void) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + uint8_t byte_1; + uint16_t byte_2; + uint32_t byte_4; + + printf("Verifying guest writes:\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_writes, + TDX_FUNCTION_SIZE(guest_io_writes), 0); + finalize_td_memory(vm); + + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + CHECK_IO(vcpu, TDX_TEST_PORT, 1, TDX_IO_WRITE); + byte_1 = *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + CHECK_IO(vcpu, TDX_TEST_PORT, 2, TDX_IO_WRITE); + byte_2 = *(uint16_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + vcpu_run(vcpu); + CHECK_GUEST_FAILURE(vcpu); + CHECK_IO(vcpu, TDX_TEST_PORT, 4, TDX_IO_WRITE); + byte_4 = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + ASSERT_EQ(byte_1, 0xAB); + ASSERT_EQ(byte_2, 0xABCD); + ASSERT_EQ(byte_4, 0xFFABCDEF); + + vcpu_run(vcpu); + ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT); + ASSERT_EQ(vcpu->run->system_event.data[1], TDX_VMCALL_INVALID_OPERAND); + + vcpu_run(vcpu); + CHECK_GUEST_COMPLETION(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { if (!is_tdx_enabled()) { @@ -501,6 +585,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_td_ioexit); run_in_new_process(&verify_td_cpuid); run_in_new_process(&verify_get_td_vmcall_info); + run_in_new_process(&verify_guest_writes); return 0; } -- 2.37.2.789.g6183377224-goog