From patchwork Tue Dec 12 20:46:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177566 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993597vqy; Tue, 12 Dec 2023 12:48:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IFvzEjdvzgIZmBNur1Ireqe2JfOikBEilad6GB5FNJGGYMbvSua/ds9BBQX9I1Fb96Y9YhN X-Received: by 2002:a05:6a21:78a8:b0:190:1258:3da3 with SMTP id bf40-20020a056a2178a800b0019012583da3mr10156452pzc.4.1702414103823; Tue, 12 Dec 2023 12:48:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414103; cv=none; d=google.com; s=arc-20160816; b=SqggnlHxCyjwSjUMMp4/53DgZeajzIibqgOGG9WbJ0+pR+54GzM0oPKr7oeykvkqie mE1J7JiB+FCNhCimj+dlyJtGZ3Y8/i1/aa/inqdmjzeW8is95J/KAKEYwxVSw5r+m7tB DYcZFDHSWvVREeVCt+FbBuNyHlaykM53+OjS7OooTN8ZZGXyaAgSW3v9TI/zwtWjTAPI TgekLWJ2EBeQxI0vMFKuTbXb+n4WO3bE9Eo9d1f2NDNW+lgGOqJHT7thyu5bDl9EMQ+2 k77spy+oWNAPj9wfYNVBA8/UqehO8RUauOo5oHo27i1x/0/updM6NqZIQvOPX/8InNbw yAOA== 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=IJkjWxWHyZzDnEqdZ8iaPH05VJWonQ7XdgN0T6HdUW8=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=ZxklAJRH3jt2uY7LX0kvLGuOmzFZyUcCirR5AvlEuCvxzCo9tImeQBLQSFHmci/vAO bTKRB9DFs8f8NjG7627oVduEbawYmRVTgV/zbdrXo27APU9zAfQoRsbA94kSilt2zY68 w/nYnAlJIj/VLKLXoLDocwEqyXgq3sG9JrCEhCSyFgeWDwcy2rRz1lzmu9RuXDjQRa1c 4FiV7fn7np8aSScMxhwLdR6NLl1SmpucQ2m9G0ztQyLWYR7RjEZCV74v8XsNwceQ3kP8 AG04dByLI4gLBqBrZVBs4hKdTuhh6dFLdrsgFbRkOmXNs6uBWNUj+DXSSmW4EmQPXHKC GOFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="psHW/NdD"; 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 h21-20020a63e155000000b005c69765acddsi8277591pgk.87.2023.12.12.12.48.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:48:23 -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="psHW/NdD"; 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 CD62380FE965; Tue, 12 Dec 2023 12:48:16 -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 S233319AbjLLUr7 (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377402AbjLLUr2 (ORCPT ); Tue, 12 Dec 2023 15:47:28 -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 7DF521AE for ; Tue, 12 Dec 2023 12:47:23 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1d053953954so30442665ad.2 for ; Tue, 12 Dec 2023 12:47:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414043; x=1703018843; 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=IJkjWxWHyZzDnEqdZ8iaPH05VJWonQ7XdgN0T6HdUW8=; b=psHW/NdDK8z8wrvk/9vGBuBjFq3hY6pzJj1w3OLzJ8Eqw3CBWyEI8/Qawy6LaPTuuZ T6PkJ6YLR2n7YrjFxjtVNJenrLXcGCOVkvwCc2mDnEGE/G3N5osbGZLYiLI/DNNHcXXm RgxSMkkBuq1v+1ZhEAG1V0qLwh6DOKIc11WAdnhDSyTefnXPRJncuvQq6k3Gqxj+kjiq tVcAJHcmONzksaCnQclZLiemJq5tYyLMWTMGoyBaAy30KTFrhVe23isQA96HwbwPet5a RkKBjmXkFJjlngiT3RnncGPR0qWfJZdNhb9YF1BbirZOsSKO9AWHfIxP6lhdRNCy8Gqi ntMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414043; x=1703018843; 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=IJkjWxWHyZzDnEqdZ8iaPH05VJWonQ7XdgN0T6HdUW8=; b=Eg8UOzIoZyHoX3VlRlr4b6fEp2KrF9I2C9clpZegXutBFpy1FhBHlCesEdowPpq4A7 tD42EqbKwqNaZvlfvqsdBqT5SC/CPxOcD8u0Uyaq9BYuL65jvT+UfmFreyyq7YWGHDPE qdz8RyA0KYFOFxvsp2e1TyKqXZYmYW4j9ZoioyoNusNMhTrBqWqM7/ZYB71wQZIHWXEW 1xy9wRfMdTdQmC0MLq4meHQb5q4xXbxOUEjekFYJOc8ra/RhPgkW2TbnCbF/REVnYpfo MQQnQzpIayXVnUvflVekbaGzK6FqnA3T2ITjkB1zgjTz1TNH6pPuYxZy6//FrSCwu+N3 dZPg== X-Gm-Message-State: AOJu0YxkdOrQinSE5gC+XdaV5uQQWtEg3NoZRwtwyrrhNqH9nYKQCahC 6SONG1UOpayBRcFYw8PyX2ZrCdnKSw== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:ec85:b0:1d0:537e:852d with SMTP id x5-20020a170902ec8500b001d0537e852dmr58319plg.13.1702414042901; Tue, 12 Dec 2023 12:47:22 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:28 -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-14-sagis@google.com> Subject: [RFC PATCH v5 13/29] KVM: selftests: TDX: Add TDX IO 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 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:48:16 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110571360077650 X-GMAIL-MSGID: 1785110571360077650 The test verifies IO 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 Reviewed-by: Binbin Wu --- .../selftests/kvm/include/x86_64/tdx/tdcall.h | 3 + .../selftests/kvm/x86_64/tdx_vm_tests.c | 91 +++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h index 78001bfec9c8..b5e94b7c48fa 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h @@ -10,6 +10,9 @@ #define TDG_VP_VMCALL_INSTRUCTION_IO_READ 0 #define TDG_VP_VMCALL_INSTRUCTION_IO_WRITE 1 +#define TDG_VP_VMCALL_SUCCESS 0x0000000000000000 +#define TDG_VP_VMCALL_INVALID_OPERAND 0x8000000000000000 + #define TDX_HCALL_HAS_OUTPUT BIT(0) #define TDX_HYPERCALL_STANDARD 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 569c8fb0a59f..a2b3e1aef151 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -339,6 +339,96 @@ void verify_get_td_vmcall_info(void) printf("\t ... PASSED\n"); } +#define TDX_IO_WRITES_TEST_PORT 0x51 + +/* + * Verifies IO functionality by writing values of different sizes + * to the host. + */ +void guest_io_writes(void) +{ + uint64_t byte_1 = 0xAB; + uint64_t byte_2 = 0xABCD; + uint64_t byte_4 = 0xFFABCDEF; + uint64_t ret; + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_WRITES_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &byte_1); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_WRITES_TEST_PORT, 2, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &byte_2); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_WRITES_TEST_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &byte_4); + if (ret) + tdx_test_fatal(ret); + + // Write an invalid number of bytes. + ret = tdg_vp_vmcall_instruction_io(TDX_IO_WRITES_TEST_PORT, 5, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &byte_4); + if (ret) + tdx_test_fatal(ret); + + tdx_test_success(); +} + +void verify_guest_writes(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + uint8_t byte_1; + uint16_t byte_2; + uint32_t byte_4; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_io_writes); + td_finalize(vm); + + printf("Verifying guest writes:\n"); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_WRITES_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + byte_1 = *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_WRITES_TEST_PORT, 2, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + byte_2 = *(uint16_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_WRITES_TEST_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + byte_4 = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + TEST_ASSERT_EQ(byte_1, 0xAB); + TEST_ASSERT_EQ(byte_2, 0xABCD); + TEST_ASSERT_EQ(byte_4, 0xFFABCDEF); + + 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); @@ -353,6 +443,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; }