Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3776784pxv; Mon, 26 Jul 2021 11:44:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzSpJ0X05sC+KgWcaRi6M7Ms50Wo5fiBwjbimvZW4Epn66JO81nN7uTXK9ErIyenOXVmFU X-Received: by 2002:a92:d344:: with SMTP id a4mr13075849ilh.230.1627325068903; Mon, 26 Jul 2021 11:44:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627325068; cv=none; d=google.com; s=arc-20160816; b=pMIbADk53ftYc2fZfc5+yG67putZID7kzCvACsixjOQ0XXOuHa0Kncvas63omQ7L3L xeM5y7EZ15qnDR9YskWoIsv28zfGGNvTUk025z2hS+Nqz+ugSp2a+Y6h2b3E+UAkxLj4 PRuX7vEB/945vQ/pwlbULOWVQ4JdhVsFfiylK2vdx8SMIzTf0X27OxugnZK1ApTmGQ1X obsituS2WZTTkUExxT5Pzis5mr69go3r0jDwHAefVyVl10N1AHasUCL5vtxkDyrD/Bmw uc43IMu2DWhKY5rxLlILvid2xfTZhr0gQCiJbdMwKEug9XmGZK0TO9KJVS3WID2RAq/e W5sQ== 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:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=XF70JHPJp7a/I5hV/lLoxb2ss5wp1yJ7Rni1BB5YkuI=; b=i61Q1mVAH4LXOHeZpuWMjZ8/NvIhthDgjClgSC5CZ6hHDkZYLysklvhOgMH8n7SC7h 6Gv68rlLJSpD2XI73F+595e4elV854fkwxIqnogPjbpfLGW2XzqsZHG1xXGP/th2XJLx zCe3VvTqlne4JL2X8/NlGp0eDuUZ5bnHSymjVMvXHSJmQSs2kwku/MOU1l9C5V1KlPGq 1xIKV1fAVyE/5545jwo5efmBbnXTrOQIkLsp1Nx6hYNBv03N82kdp1K4eoIAZAjJSTe+ BJIcKfhFgq6V7ProWjdI+t//8gXHHohxqITwxh2MLmaCbUj5Lh6jpvcOA/TZJMpQPslP UbnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=fl9wff8X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 187si644917iow.86.2021.07.26.11.44.17; Mon, 26 Jul 2021 11:44:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=fl9wff8X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S232361AbhGZSCq (ORCPT + 99 others); Mon, 26 Jul 2021 14:02:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231362AbhGZSCp (ORCPT ); Mon, 26 Jul 2021 14:02:45 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A78DEC061760 for ; Mon, 26 Jul 2021 11:43:13 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id s1-20020a17090a8801b02901756faa6840so1127061pjn.2 for ; Mon, 26 Jul 2021 11:43:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=XF70JHPJp7a/I5hV/lLoxb2ss5wp1yJ7Rni1BB5YkuI=; b=fl9wff8XwRNg/uSrTucLMcQ1EIffjQmDBkhzRWf2F39kJ3PtcBzHVDnSwEu610JWOj vDDzIl3NVRKmI+SagJNZvWFrx/NHPAcvgAgjSlyUJkIhhMqqB7ZaQGR5wgLqRzPWUOtQ CrBfWsuq+iUQbgUeBRH+spfiBxwPuIDVyjEGqywpeXTR0XwqAiw5v5e0Hsr0O0TOt90I FNAOzr76IJgFACQ4/2onwCu3h4d0X8RfRG0Rzuyb8ec1PAhXI65vp4UyyasPAoRNMRNA tf2sihDm18LlG5c7S+LWiMW1tPoXbJUGuOimjiTaSQlND8lbwrm4peILK2uUGZVbMrig u91A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=XF70JHPJp7a/I5hV/lLoxb2ss5wp1yJ7Rni1BB5YkuI=; b=jzNGw79IyXpt5Z0RjgUZ7hXdCpcpOVA+HBDzRwlhwx9aqj4Jb7pODFdghyDLguG2hm BW8npS4FoSki3y2k34rD8Audgh2YyJSdCV/PhPqNpspVM6HKixgwYeoYY3Xyd9bIF2Vr n0/enT4UaGB1RBAAjo/21EscSBLYOjzBnch+7/52Bkjq7cai2Cims8qu9nupDfCabYXm +1OGlw6CS168pi3WXupnKbYjNBcOVF3jbYl+C4vUJv1hvoY96jn/SQ5/e8KNHvEcfbSM TQzWtRog/n52fkkIOaGOCLcHxfenACfMyKDMoZwXoqK4NpS/a8gwlp+ekEYvknqZN0s7 dp9Q== X-Gm-Message-State: AOAM533mpdPMR7kdDqfvgCv6Rw8GorjzEPuiXurEq5+07/LnB7GPgwQE YxFkax6d0rtVAptT98LSXU4V91Ss83xFdzWV X-Received: from nehir.kir.corp.google.com ([2620:15c:29:204:e222:115f:790c:cd0f]) (user=erdemaktas job=sendgmr) by 2002:a62:36c5:0:b029:32b:83fa:3a3e with SMTP id d188-20020a6236c50000b029032b83fa3a3emr19403121pfa.52.1627324993074; Mon, 26 Jul 2021 11:43:13 -0700 (PDT) Date: Mon, 26 Jul 2021 11:37:57 -0700 In-Reply-To: <20210726183816.1343022-1-erdemaktas@google.com> Message-Id: <20210726183816.1343022-5-erdemaktas@google.com> Mime-Version: 1.0 References: <20210726183816.1343022-1-erdemaktas@google.com> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog Subject: [RFC PATCH 4/4] KVM: selftest: Adding test case for TDX port IO From: Erdem Aktas To: linux-kselftest@vger.kernel.org Cc: erdemaktas@google.com, Sean Christopherson , Peter Gonda , Marc Orr , Sagi Shahar , Paolo Bonzini , Shuah Khan , Andrew Jones , Ben Gardon , Peter Xu , Emanuele Giuseppe Esposito , Eric Auger , Ricardo Koller , Zhenzhong Duan , Jim Mattson , Aaron Lewis , Vitaly Kuznetsov , Peter Shier , Oliver Upton , Axel Rasmussen , Yanan Wang , "Maciej S. Szmigiero" , David Matlack , Like Xu , open list , "open list:KERNEL VIRTUAL MACHINE (KVM)" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Verifies TDVMCALL READ and WRITE operations. Signed-off-by: Erdem Aktas Reviewed-by: Sean Christopherson Reviewed-by: Peter Gonda Reviewed-by: Marc Orr Reviewed-by: Sagi Shahar --- .../selftests/kvm/x86_64/tdx_vm_tests.c | 107 ++++++++++++++++++ 1 file changed, 107 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 da7ea67b1..7b0b4b378 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -88,6 +88,112 @@ void verify_td_lifecycle(void) kvm_vm_free(vm); 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; + + data_out = 0xAB; + tdvmcall_io(TDX_TEST_PORT, 1, 1, &data_out); + tdvmcall_io(TDX_TEST_PORT, 1, 0, &data_in); + delta = data_in - data_out - 1; + tdvmcall_io(TDX_TEST_PORT, 1, 1, &delta); +} + +void verify_td_ioexit(void) +{ + struct kvm_vm *vm; + struct kvm_run *run; + uint32_t port_data; + + printf("Verifying TD IO Exit:\n"); + /* Create a TD VM with no memory.*/ + vm = __vm_create(VM_MODE_DEFAULT, 0, O_RDWR, KVM_X86_TDX_VM); + + /* Allocate TD guest memory and initialize the TD.*/ + initialize_td(vm); + + /* Initialize the TD vcpu and copy the test code to the guest memory.*/ + 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); + + run = vcpu_state(vm, 0); + + /* Wait for guest to do a IO write */ + vcpu_run(vm, 0); + TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, + "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n", + run->exit_reason, + exit_reason_str(run->exit_reason)); + + TEST_ASSERT((run->exit_reason == KVM_EXIT_IO) + && (run->io.port == TDX_TEST_PORT) + && (run->io.size == 1) + && (run->io.direction == 1), + "Got an unexpected IO exit values: %u (%s) %d %d %d\n", + run->exit_reason, + exit_reason_str(run->exit_reason), + run->io.port, run->io.size, run->io.direction); + port_data = *(uint8_t *)((void *)run + 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(vm, 0); + TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, + "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n", + run->exit_reason, + exit_reason_str(run->exit_reason)); + + TEST_ASSERT(run->exit_reason == KVM_EXIT_IO && + run->io.port == TDX_TEST_PORT && + run->io.size == 1 && + run->io.direction == 0, + "Got an unexpected IO exit values: %u (%s) %d %d %d\n", + run->exit_reason, + exit_reason_str(run->exit_reason), + run->io.port, run->io.size, run->io.direction); + *(uint8_t *)((void *)run + run->io.data_offset) = port_data + 1; + + printf("\t ... IO READ: OK\n"); + /* + * Wait for the guest to do a IO write to the TDX_TEST_PORT with the + * value of 0. Any value other than 0 means, the guest has not able to + * read/write values correctly. + */ + vcpu_run(vm, 0); + TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, + "KVM_EXIT_IO is expected but got an exit_reason: %u (%s)\n", + run->exit_reason, + exit_reason_str(run->exit_reason)); + + TEST_ASSERT(run->exit_reason == KVM_EXIT_IO && + run->io.port == TDX_TEST_PORT && + run->io.size == 1 && + run->io.direction == 1 && + *(uint32_t *)((void *)run + run->io.data_offset) == 0, + "Got an unexpected IO exit values: %u (%s) %d %d %d %d\n", + run->exit_reason, + exit_reason_str(run->exit_reason), + run->io.port, run->io.size, run->io.direction, + *(uint32_t *)((void *)run + run->io.data_offset)); + + 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()) { @@ -97,6 +203,7 @@ int main(int argc, char **argv) } run_in_new_process(&verify_td_lifecycle); + run_in_new_process(&verify_td_ioexit); return 0; } -- 2.32.0.432.gabb21c7263-goog