From patchwork Fri Aug 16 19:23:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pratik R. Sampat" X-Patchwork-Id: 13766794 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2077.outbound.protection.outlook.com [40.107.102.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 585F378C7D; Fri, 16 Aug 2024 19:23:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.102.77 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836215; cv=fail; b=u2NVyOG+zwCCGD56WP2PsscUEUVmUkn7OeTtTF+xDcBQQzsrai/fkYM8utWVA+POzbZYVI5K8QGPU4dWELEAf1+00CzLR7xXut4WC4biThS8NitTjHmnYOM78Juip+Kj3ohq3g0lcHl9fZbtA/49DAOJcwY9YdARxmqRDjBRTuI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836215; c=relaxed/simple; bh=JB9sUIYO6DnLGORuHJch66YMSGJ0Md6ImACNLwLkT3Q=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Go0YU/DCWpvId5Dk7F0szkVuDMZg5u2KPtw0zM+iULnd5u1LG2a6T7Bot++kmcnVjecrnvlanZE7CkGki8vL4tzpyZepePErWGUPd7/cl2FwWNoXRPEaKT2wh4dpNQ/wxuLFNdK0hMqejXfQTFxo/xp04FT/AJ46hNVYkyl9lzQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=EpDdcDIH; arc=fail smtp.client-ip=40.107.102.77 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="EpDdcDIH" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=mC7dC4gNEYTyvCyd+Esvcv1nl+/jJT1MCgc2NiVCWEysHjyT6Ltqjsakzr54tNwXx3wqglSBLQiyan6hzfnW2jbzuFGUq4aZ549PJNEo86HirnSSJ/66OxVY0eJhjxd/T3JyvKKVtZiXQkQQtZTTesAW3UlYtCXxHCVY1+ejby45EWDJBERAPewhA2u47jxy+VO7W8pHDkdFJnjVcv7zRJND+41w4YHGY0NfQC2okmZCTlokkni5uoLxcoCZGVtmWcYaksRB0k7nmL474kHAJ1W2V9ge6NplXzSi8f2LSz2pZ+tnS2xLViTUOWnEFE8trK1Ou0jeA2HJTCxRIhfwVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=xw6Yz72/tjVZeurtHL9dYw5golvC71teAzlJ+iE06wE=; b=CBLU37eCYKRkLGPFYPkHXqX5ffxf6aYjiNdm3G6vcOHdjF2PuHt7TiwioxeOKMcPEeWDX6lYxk5Uy5Xf9PqzzY4Yz31B7fXeyoBVTpdDmcU1qh/aXKKKMBT7V21MKhG4ZLALY8eyE2+uwaHpfFmi4kRI3fqGHVITPsqlP02TfTo7KG8MCM5dX7YyWxwlU6KpydntEJxGCWlR8dJ9GtR4vGdL1oXrgiduClw/rXXK2U6BsT4iGYz6Li7B4h21lD0dJnsIv/fyPRRCqmBpnlJXKCemrAT8TK8q/SZ4U+GlkLchSq/7aZfULtrCazUNmguiJjvw5LeKFsLnZzg+2L3c+A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xw6Yz72/tjVZeurtHL9dYw5golvC71teAzlJ+iE06wE=; b=EpDdcDIH9sjVPfDYptZ9KKuvgoLJf0H3otCWlurj/ITnC0kXOk1Ar0FSyCWxsPIAjCyG/PT7mTkCGRueFzOnqg9UwFv7rQNYDE7w7ZTsTVBkwRh3MGJ7DPViHyAlk/ztW5k7M8H4l/rbaGEsDoYIWfqHPCbh3TlTj0mgCynU5Mc= Received: from SJ0PR03CA0271.namprd03.prod.outlook.com (2603:10b6:a03:39e::6) by CH3PR12MB9025.namprd12.prod.outlook.com (2603:10b6:610:129::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.19; Fri, 16 Aug 2024 19:23:28 +0000 Received: from SJ1PEPF00001CE5.namprd03.prod.outlook.com (2603:10b6:a03:39e:cafe::85) by SJ0PR03CA0271.outlook.office365.com (2603:10b6:a03:39e::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.20 via Frontend Transport; Fri, 16 Aug 2024 19:23:28 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CE5.mail.protection.outlook.com (10.167.242.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7828.19 via Frontend Transport; Fri, 16 Aug 2024 19:23:27 +0000 Received: from AUSPRSAMPAT.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 16 Aug 2024 14:23:26 -0500 From: "Pratik R. Sampat" To: CC: , , , , , , , Subject: [PATCH v2 1/9] KVM: selftests: Decouple SEV ioctls from asserts Date: Fri, 16 Aug 2024 14:23:02 -0500 Message-ID: <20240816192310.117456-2-pratikrajesh.sampat@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> References: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CE5:EE_|CH3PR12MB9025:EE_ X-MS-Office365-Filtering-Correlation-Id: 6e8d343a-3eaf-4613-fc5a-08dcbe28e7c7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|376014|82310400026|1800799024; X-Microsoft-Antispam-Message-Info: TnFXCeb7688YoYs3h5lRwzvijQTYVZDVS1gzgWztI3uHTiqwG3843UyA1vI4z2XY8ASRO0BBrQOwYkAb2kgd0wnyH5B279QXt6znVKpPwbYbSoVn8wSYk2T8iFjXxE93UjKE920MDkLiquQlE6hKSdaY3B5GAoNeHiwNO/F/93gcc1MRaIIqxwFq0zvRCG5mNBG25ncmxNh+zk4xMEJ35NYNtusQ7MLjRWeduK0RyuooQv6Gp2fGpaYrcqyoTFx4nl42niFabGvYhtrzG3o6ifGCC+Ap8iOyMs02M5t/zWrb2cLIlnAOK3PYOBFCyxM9rsebNN4bjWgG19y69/XSx7TOKPhJr77wEKrp64IhxljczK3+i97WojkxdMRl8VOLxsFJ3mwTlP6AVJo5MQsA/n/26++BkfRpPOWZJbG5sgwvDIbLJODSN2s4a+G1JAeFKnl29UswLo6L3FBhVnVG2KQ1M8gR0GZFo+p7hmgp2bYxhq0vO7jARyGSJsyUMc1hgoYwjmhyB2dq3+ct/jHVVHcA69S0IOW47s9tV9G5BYC3TfYxONLQ+W06TRZkbz9F+/VfrxIsuLAa0wlEwqeOAFotYEG4vB7PCO54L4sBruHJseIinC13PZvzGVQKlQbZin2PmvhMzudmot9fLfBjTfjRMU3ls5nFXUe/uH+Y5My3tPNPAOA6gTitUJ18weBEgwhunck+IsX+6lIfaeDd6qfqWTtRfoYVgRYlLtEvNOPLIFjSpq0lODONy1JEMncVoYTOQ/JWNZy7tkkTNEXai+fEOMdYhMHQVRxtfQxHm5837X8vff0YLFVylYbVDZk/v70BcTBGh/ZPSHTpit2kkm5RhxX0L8sG1EYfT7MpIcIxr2QRRCrJ1a+v6OrTnVUvWwRkNvCfX+Iio5TgRG66I9M6+eBnPY9A3V1ciMcKsbUgl4SVPww/zPJX9TpvxRjDuSNp5sv5EpysM+Gw4L9+v1C8Ertu7J6e+maYlJOZwvTG8V5Erk83YAwUiV6cUK0md3/c0TonP+JysQf7TbEG0zZqWe2NFzVEljIf8/oYlBCg4s3KRsLHTNY89K2IhtWNOwlm0nc/+54qL72JJcQln4glxB2C1LgHdekgtd7uvGfISiKUOMUKXkkMA59yJYkvRfouSud9kL8UoZHx4lSaHG69blt8VzUuYPskCw/g5b+wEofHOw2t8Csu8eck+v7P6h0+H3B2wbnORH8Bnbal/pJLO1prJTG0yjLA4xP93auNCcJG02UmLRGZzuMxirc4a37uXXw1OsTontONZcjVJjftyBuRY1Gs7SGLRLVYlfIE7CXiaMdl4tRoH/DOEa2Cwpz2XzIk37L3iDxcu4JSNBFXGTDYUQN6nveq2zUD6gLS2luGWdFi/zXY6D8t73kkXFbMBh8YpfAVt4nLNxNV5sLhBTkX4/avKKIWLjOVxijLgUUNqaqiJS6U/BvLlLUC X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700013)(376014)(82310400026)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Aug 2024 19:23:27.4817 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6e8d343a-3eaf-4613-fc5a-08dcbe28e7c7 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CE5.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB9025 Add variants of sev, sev-es launch path that return the status of the ioctl call instead of asserting for success. This enables both positive and negative testing of the path. Cleanups are performed with no functional change intended. Signed-off-by: Pratik R. Sampat --- .../selftests/kvm/include/x86_64/sev.h | 22 +++++- tools/testing/selftests/kvm/lib/x86_64/sev.c | 78 +++++++++++++++---- 2 files changed, 80 insertions(+), 20 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/sev.h b/tools/testing/selftests/kvm/include/x86_64/sev.h index 82c11c81a956..3998152cc081 100644 --- a/tools/testing/selftests/kvm/include/x86_64/sev.h +++ b/tools/testing/selftests/kvm/include/x86_64/sev.h @@ -27,6 +27,12 @@ enum sev_guest_state { #define GHCB_MSR_TERM_REQ 0x100 +/* Variants of the SEV launch path that do not assert the ioctl status */ +int __sev_vm_launch_start(struct kvm_vm *vm, uint32_t policy); +int __sev_vm_launch_update(struct kvm_vm *vm, uint32_t policy); +int __sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement); +int __sev_vm_launch_finish(struct kvm_vm *vm); + void sev_vm_launch(struct kvm_vm *vm, uint32_t policy); void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement); void sev_vm_launch_finish(struct kvm_vm *vm); @@ -82,15 +88,23 @@ static inline void sev_register_encrypted_memory(struct kvm_vm *vm, vm_ioctl(vm, KVM_MEMORY_ENCRYPT_REG_REGION, &range); } -static inline void sev_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa, - uint64_t size) +static inline int __sev_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa, + uint64_t hva, uint64_t size) { struct kvm_sev_launch_update_data update_data = { - .uaddr = (unsigned long)addr_gpa2hva(vm, gpa), + .uaddr = hva, .len = size, }; - vm_sev_ioctl(vm, KVM_SEV_LAUNCH_UPDATE_DATA, &update_data); + return __vm_sev_ioctl(vm, KVM_SEV_LAUNCH_UPDATE_DATA, &update_data); +} + +static inline void sev_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa, + uint64_t hva, uint64_t size) +{ + int ret = __sev_launch_update_data(vm, gpa, hva, size); + + TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_SEV_LAUNCH_UPDATE_DATA, ret, vm); } #endif /* SELFTEST_KVM_SEV_H */ diff --git a/tools/testing/selftests/kvm/lib/x86_64/sev.c b/tools/testing/selftests/kvm/lib/x86_64/sev.c index e9535ee20b7f..290eb0bd495e 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/sev.c +++ b/tools/testing/selftests/kvm/lib/x86_64/sev.c @@ -14,15 +14,16 @@ * and find the first range, but that's correct because the condition * expression would cause us to quit the loop. */ -static void encrypt_region(struct kvm_vm *vm, struct userspace_mem_region *region) +static int encrypt_region(struct kvm_vm *vm, struct userspace_mem_region *region) { const struct sparsebit *protected_phy_pages = region->protected_phy_pages; const vm_paddr_t gpa_base = region->region.guest_phys_addr; const sparsebit_idx_t lowest_page_in_region = gpa_base >> vm->page_shift; sparsebit_idx_t i, j; + int ret; if (!sparsebit_any_set(protected_phy_pages)) - return; + return 0; sev_register_encrypted_memory(vm, region); @@ -30,8 +31,15 @@ static void encrypt_region(struct kvm_vm *vm, struct userspace_mem_region *regio const uint64_t size = (j - i + 1) * vm->page_size; const uint64_t offset = (i - lowest_page_in_region) * vm->page_size; - sev_launch_update_data(vm, gpa_base + offset, size); + ret = __sev_launch_update_data(vm, gpa_base + offset, + (uint64_t)addr_gpa2hva(vm, gpa_base + offset), + size); + if (ret) + return ret; + } + + return 0; } void sev_vm_init(struct kvm_vm *vm) @@ -60,38 +68,74 @@ void sev_es_vm_init(struct kvm_vm *vm) } } -void sev_vm_launch(struct kvm_vm *vm, uint32_t policy) +int __sev_vm_launch_start(struct kvm_vm *vm, uint32_t policy) { struct kvm_sev_launch_start launch_start = { .policy = policy, }; + + return __vm_sev_ioctl(vm, KVM_SEV_LAUNCH_START, &launch_start); +} + +int __sev_vm_launch_update(struct kvm_vm *vm, uint32_t policy) +{ struct userspace_mem_region *region; - struct kvm_sev_guest_status status; int ctr; - vm_sev_ioctl(vm, KVM_SEV_LAUNCH_START, &launch_start); - vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status); + hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) { + int ret = encrypt_region(vm, region); - TEST_ASSERT_EQ(status.policy, policy); - TEST_ASSERT_EQ(status.state, SEV_GUEST_STATE_LAUNCH_UPDATE); - - hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) - encrypt_region(vm, region); + if (ret) + return ret; + } if (policy & SEV_POLICY_ES) vm_sev_ioctl(vm, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL); vm->arch.is_pt_protected = true; + + return 0; } -void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement) +int __sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement) { struct kvm_sev_launch_measure launch_measure; - struct kvm_sev_guest_status guest_status; launch_measure.len = 256; launch_measure.uaddr = (__u64)measurement; - vm_sev_ioctl(vm, KVM_SEV_LAUNCH_MEASURE, &launch_measure); + + return __vm_sev_ioctl(vm, KVM_SEV_LAUNCH_MEASURE, &launch_measure); +} + +int __sev_vm_launch_finish(struct kvm_vm *vm) +{ + return __vm_sev_ioctl(vm, KVM_SEV_LAUNCH_FINISH, NULL); +} + +void sev_vm_launch(struct kvm_vm *vm, uint32_t policy) +{ + struct kvm_sev_guest_status status; + int ret; + + ret = __sev_vm_launch_start(vm, policy); + TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_SEV_LAUNCH_START, ret, vm); + + vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status); + + TEST_ASSERT_EQ(status.policy, policy); + TEST_ASSERT_EQ(status.state, SEV_GUEST_STATE_LAUNCH_UPDATE); + + ret = __sev_vm_launch_update(vm, policy); + TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_SEV_LAUNCH_UPDATE_DATA, ret, vm); +} + +void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement) +{ + struct kvm_sev_guest_status guest_status; + int ret; + + ret = __sev_vm_launch_measure(vm, measurement); + TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_SEV_LAUNCH_FINISH, ret, vm); vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &guest_status); TEST_ASSERT_EQ(guest_status.state, SEV_GUEST_STATE_LAUNCH_SECRET); @@ -100,13 +144,15 @@ void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement) void sev_vm_launch_finish(struct kvm_vm *vm) { struct kvm_sev_guest_status status; + int ret; vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status); TEST_ASSERT(status.state == SEV_GUEST_STATE_LAUNCH_UPDATE || status.state == SEV_GUEST_STATE_LAUNCH_SECRET, "Unexpected guest state: %d", status.state); - vm_sev_ioctl(vm, KVM_SEV_LAUNCH_FINISH, NULL); + ret = __sev_vm_launch_finish(vm); + TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_SEV_LAUNCH_MEASURE, ret, vm); vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status); TEST_ASSERT_EQ(status.state, SEV_GUEST_STATE_RUNNING); From patchwork Fri Aug 16 19:23:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pratik R. Sampat" X-Patchwork-Id: 13766795 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2082.outbound.protection.outlook.com [40.107.223.82]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEFD91C460F; Fri, 16 Aug 2024 19:23:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.82 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836228; cv=fail; b=Yfs6xnDi6USMsL5pamZrznrXeZEDYIV/N6GztOXYWOyLsnJUU/JVuJbhhoxUtRCHHOGZYQHCttuM9vbljCwUief4CCllUW5PByf88Fw4/F4p5LvY3cOo21UnY474CxvEt3fvtayYODdeHMkrdPkO0iPouG4ceGP0SLLm2Ta+SkY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836228; c=relaxed/simple; bh=5G0T+/JQ/tmth3im4O/XnGOO34YIKXOqxeI5kHS/eEg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jKsO1F5EOM1pLSuVZm7J6TvtjeMnzB+qFQkdofQw/KL4KKiy+XurF7aomv8j9XFNpp4xsDh+fQS4P4kyLwbSehyI9xonk37NDy/6iQ9x1VoYquV2RNr7mQAsEHB0WV66it/KSIopwjoRQBLN3d9MOqN6AfEybcImTMiliG5nqiI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=aHCMuvgG; arc=fail smtp.client-ip=40.107.223.82 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="aHCMuvgG" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=R/Y+PMUzc+84lv42mPKYG1BromtsPFIJC9TViKTQ8WOdbVrHXyKma6LIH3+yIJO8TG5pgFfmssxytda03oBA8/NB9FshgxsDaaNWPWyEHvTPwrw6apsURQaZZhT4YC5hA+NenK7EZN8z7zKmz1DLWk5t2YYSuRWKnOwYS4OUfjWWUikj8emTQfHbOh29C141/EEV20bIrkRJaf+Xl+q/D14I0aX+S73/pxAVc8Q38biI1OwAqzdlK23iZfG8Nih4QkejT4WIvZSUkC1xXn2p1gLbM5po+RVHtbQX+BC8pAeMvskPiK7kmxXMZ7z3TdtZxE/6T7V3rpVXdbyP/A1ulQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JvUU+MgTFdNdlmcaF8T7ucBMdN3CuqsOBgVk4jmPqDM=; b=T4P4ZCQqozATWG7g2cyQ9HGmqO/U9TOCI8BtBDLfU4/cowVn1MUkyrHtKVmDCRfx2r9sNj1mc6eKTfLab4kLCW50SF43BltkiWE5xy+MWmnXKCstZHfotYZ/l5TBj5/9qgDNjKvXxpEIWZXyC9CLyeuR2SVV0Z89Z77sjV8cV9yIX/QGFcUaeq+7XCbIyXskQ8rTs6VcJvVCaf+x8MH5sF5H3dAvbYdMq1X14n9nG8C55ZF4A2bCo8tdbwPSEyJF8kSsvaxpV1rDvtOVEBv1TNJOVmTjxx5asfabguvi7ZzQA5Xyz4+zDHiRNh4z+PENGHDhqPJKklgXS0NknYLxVA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JvUU+MgTFdNdlmcaF8T7ucBMdN3CuqsOBgVk4jmPqDM=; b=aHCMuvgGxJx6tqaLDiU7CN1yuxsaR4aqZxclgLggNCwmQ/+c4e8rUqWtuzmmJXweLs8G1ovns5sA+9ghdAg8TmadzYu6msx9XlsLwNvnCOQYkFpSyy94dJCupBkNlUE8uP62WmxJ6ABUvw+BOpX4f7N9GYYthe0phGsZr9nceN8= Received: from MW4PR04CA0071.namprd04.prod.outlook.com (2603:10b6:303:6b::16) by MN0PR12MB6126.namprd12.prod.outlook.com (2603:10b6:208:3c6::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7849.20; Fri, 16 Aug 2024 19:23:40 +0000 Received: from SJ1PEPF00001CE4.namprd03.prod.outlook.com (2603:10b6:303:6b:cafe::11) by MW4PR04CA0071.outlook.office365.com (2603:10b6:303:6b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7849.24 via Frontend Transport; Fri, 16 Aug 2024 19:23:39 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CE4.mail.protection.outlook.com (10.167.242.20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7828.19 via Frontend Transport; Fri, 16 Aug 2024 19:23:38 +0000 Received: from AUSPRSAMPAT.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 16 Aug 2024 14:23:37 -0500 From: "Pratik R. Sampat" To: CC: , , , , , , , Subject: [PATCH v2 2/9] KVM: selftests: Add a basic SNP smoke test Date: Fri, 16 Aug 2024 14:23:03 -0500 Message-ID: <20240816192310.117456-3-pratikrajesh.sampat@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> References: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CE4:EE_|MN0PR12MB6126:EE_ X-MS-Office365-Filtering-Correlation-Id: 4d87cb51-0fc9-41aa-9a61-08dcbe28eea1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|36860700013|82310400026; X-Microsoft-Antispam-Message-Info: yKDwqo0xkvciNOZR2rktPHxqDFgWMqhn8dDZox3n/8tIDDlAvbQ8PXO6NuVc0X5vBrWvEZWJn7DYR46ARhb971gfILUCFDRNsM50veiKsVyP1W4k30uxfkyOBrd4ePEfg3XryTfcdV38UOM6o7l+b/uPxEC3stMVeOlzU30Eb+WqsoII/f1nAD4+cB4wYSawwVcQl6Xfb/DDX7pCLIUU8VoqXpOWF2OXArq8HZmsp+gIzZgbiwJr1ppsSH4kt4j3yIY8c++GlNQm+iKyftfL7UnpMgi8H4eCcv9NAG+vDra/Vrk0EfoLQf3opLLhLQq+cT4s54Gd16C4YN0rXxN+MbEzsKSaWqHORdvlAi1WuPakY1sBSTKTqEA5510Qk1vPPLe9d+JBd/IrfcBmdzRcbYjUxPGoJHFutTc2YfyTKW3P/KTDxMnQgqKuCCCtdkoP74u6P14+itpPG7FgTwHxoI0Zbf7OODa2yl16g56R8AFz1bn5IPiIVUho4nivvq4NPffK88e6PQJcLipLieOVUAvKQas+i+Xxd7+Ilwj8VOkLkrZ30TLaVNypUxmCbk89405NnxXwh1RLNuKKyBk9Nxzstxg0h1EwNh1YIBdt79PjMYKCipe/aYZKu4zyQt6VcDsVxq0Tso1kbd5FhzDUB+q7DIvq2b1laEtYhnLlQI30pNri9nqT/uCJKKR6Ik67Uk3Uk4/FU8yM2EFJrS+PgsYAXFMRwj7kqnKUZO4/xxvjqtO4vPaeqczhx2zDhVRGmWGHK5OJhXC4TeiafPlJ9mt+lf2DajeJG3pz4pmjL0T5CgaXX2XAOWklP/HLgrc3kYDgzxO1ukWloU+H2ZMalY5Zp0wFbJHc4X7BLgbHPNCVyXaSWaU2YiiPmgfBG2rKsY7qCVd8ClpZ5nP+08LMO6W3sXzjdh7OtOdK4k0KDe6S7SecX6vTYOI0AUj1g7AzhkguNlLT7z71/1lKIEfreWDS0z2mImLNEiR1Em5Aea5vKs3N66joPCLa3cxKDKPJQkOM90rl2+4k2Shx8CY2s8LZjAQG1rskbIB3jvtxMRSX0ks5uML81NuCGpzP46gRe1Cz/bkJ6Pl35MfD+K9Iu8f+BlcS/mNQV5C65XegRfSJsnp88oJ/J1ulMw0ESBhor+vmXC/kTA5V+LQM8B5dZE72JAHBL4eq7nU9JBl+R/PwuES7GcA6AHvl6mbRwAbuI0604l21zE8f7Qc6L2hTuSIqeell4curXUKLO4nC1aSMX7YwrEigyJ9BsOsQUKttOguLx/SKP2hj+doNUMvbxLrVj4/P8VB+/oDPPm5u0xNeReHnKmRP6lH3FPT2WHWBzBsK6ia9O8YUpBF/BTc0wejHfjFhWw7w8FnTmf6lJxK6wbknJqBevY0GBeBcuDY13jj6vhI/kOozfqegkBYL5uOyda74Cg07MX0VaGIRc04Uynxe0dGX8PRqfL2V6IBD X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(376014)(36860700013)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Aug 2024 19:23:38.9742 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4d87cb51-0fc9-41aa-9a61-08dcbe28eea1 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CE4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6126 Extend sev_smoke_test to also run a minimal SEV-SNP smoke test that initializes and sets up private memory regions required to run a simple SEV-SNP guest. Similar to its SEV-ES smoke test counterpart, this also does not support GHCB and ucall yet and uses the GHCB MSR protocol to trigger an exit of the type KVM_EXIT_SYSTEM_EVENT. Also, decouple policy and type and require functions to provide both such that there is no assumption regarding the type using policy. Signed-off-by: Pratik R. Sampat --- .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/include/x86_64/sev.h | 54 +++++++- tools/testing/selftests/kvm/lib/kvm_util.c | 8 +- .../selftests/kvm/lib/x86_64/processor.c | 6 +- tools/testing/selftests/kvm/lib/x86_64/sev.c | 116 +++++++++++++++++- .../selftests/kvm/x86_64/sev_smoke_test.c | 67 ++++++++-- 6 files changed, 230 insertions(+), 22 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index a0c1440017bb..8acf06cc66cb 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -195,6 +195,7 @@ struct kvm_x86_cpu_feature { #define X86_FEATURE_VGIF KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16) #define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1) #define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3) +#define X86_FEATURE_SNP KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 4) /* * KVM defined paravirt features. diff --git a/tools/testing/selftests/kvm/include/x86_64/sev.h b/tools/testing/selftests/kvm/include/x86_64/sev.h index 3998152cc081..658c3cca208d 100644 --- a/tools/testing/selftests/kvm/include/x86_64/sev.h +++ b/tools/testing/selftests/kvm/include/x86_64/sev.h @@ -22,8 +22,21 @@ enum sev_guest_state { SEV_GUEST_STATE_RUNNING, }; +/* Minimum firmware version required for the SEV-SNP support */ +#define SNP_FW_REQ_VER_MAJOR 1 +#define SNP_FW_REQ_VER_MINOR 51 +#define SNP_POLICY_MINOR_BIT 0 +#define SNP_POLICY_MAJOR_BIT 8 + #define SEV_POLICY_NO_DBG (1UL << 0) #define SEV_POLICY_ES (1UL << 2) +#define SNP_POLICY_SMT (1ULL << 16) +#define SNP_POLICY_RSVD_MBO (1ULL << 17) +#define SNP_POLICY_DBG (1ULL << 19) +#define SNP_POLICY (SNP_POLICY_SMT | SNP_POLICY_RSVD_MBO) + +#define SNP_FW_VER_MAJOR(maj) ((uint8_t)(maj) << SNP_POLICY_MAJOR_BIT) +#define SNP_FW_VER_MINOR(min) ((uint8_t)(min) << SNP_POLICY_MINOR_BIT) #define GHCB_MSR_TERM_REQ 0x100 @@ -32,14 +45,22 @@ int __sev_vm_launch_start(struct kvm_vm *vm, uint32_t policy); int __sev_vm_launch_update(struct kvm_vm *vm, uint32_t policy); int __sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement); int __sev_vm_launch_finish(struct kvm_vm *vm); +int __snp_vm_launch_start(struct kvm_vm *vm, uint64_t policy, uint8_t flags); +int __snp_vm_launch_update(struct kvm_vm *vm, uint8_t page_type); +int __snp_vm_launch_finish(struct kvm_vm *vm, uint16_t flags); void sev_vm_launch(struct kvm_vm *vm, uint32_t policy); void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement); void sev_vm_launch_finish(struct kvm_vm *vm); +void snp_vm_launch_start(struct kvm_vm *vm, uint64_t policy); +void snp_vm_launch_update(struct kvm_vm *vm); +void snp_vm_launch_finish(struct kvm_vm *vm); + +bool is_kvm_snp_supported(void); struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t type, void *guest_code, struct kvm_vcpu **cpu); -void vm_sev_launch(struct kvm_vm *vm, uint32_t policy, uint8_t *measurement); +void vm_sev_launch(struct kvm_vm *vm, uint64_t policy, uint8_t *measurement); kvm_static_assert(SEV_RET_SUCCESS == 0); @@ -74,8 +95,18 @@ kvm_static_assert(SEV_RET_SUCCESS == 0); __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, vm); \ }) +/* Ensure policy is within bounds for SEV, SEV-ES */ +#define ASSERT_SEV_POLICY(type, policy) \ +({ \ + if (type == KVM_X86_SEV_VM || type == KVM_X86_SEV_ES_VM) { \ + TEST_ASSERT(policy < ((uint32_t)~0U), \ + "Policy beyond bounds for SEV"); \ + } \ +}) \ + void sev_vm_init(struct kvm_vm *vm); void sev_es_vm_init(struct kvm_vm *vm); +void snp_vm_init(struct kvm_vm *vm); static inline void sev_register_encrypted_memory(struct kvm_vm *vm, struct userspace_mem_region *region) @@ -99,6 +130,19 @@ static inline int __sev_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa, return __vm_sev_ioctl(vm, KVM_SEV_LAUNCH_UPDATE_DATA, &update_data); } +static inline int __snp_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa, + uint64_t hva, uint64_t size, uint8_t type) +{ + struct kvm_sev_snp_launch_update update_data = { + .uaddr = hva, + .gfn_start = gpa >> PAGE_SHIFT, + .len = size, + .type = type, + }; + + return __vm_sev_ioctl(vm, KVM_SEV_SNP_LAUNCH_UPDATE, &update_data); +} + static inline void sev_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa, uint64_t hva, uint64_t size) { @@ -107,4 +151,12 @@ static inline void sev_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa, TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_SEV_LAUNCH_UPDATE_DATA, ret, vm); } +static inline int snp_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa, + uint64_t hva, uint64_t size, uint8_t type) +{ + int ret = __snp_launch_update_data(vm, gpa, hva, size, type); + + TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_SEV_SNP_LAUNCH_UPDATE, ret, vm); +} + #endif /* SELFTEST_KVM_SEV_H */ diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 56b170b725b3..9cc4dfc72329 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -413,14 +413,18 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus, nr_extra_pages); struct userspace_mem_region *slot0; struct kvm_vm *vm; - int i; + int i, flags = 0; pr_debug("%s: mode='%s' type='%d', pages='%ld'\n", __func__, vm_guest_mode_string(shape.mode), shape.type, nr_pages); vm = ____vm_create(shape); - vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, 0); + if (shape.type == KVM_X86_SNP_VM) + flags |= KVM_MEM_GUEST_MEMFD; + + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, flags); + for (i = 0; i < NR_MEM_REGIONS; i++) vm->memslots[i] = 0; diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 153739f2e201..670fb09c43d0 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -629,7 +629,8 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm) sync_global_to_guest(vm, host_cpu_is_amd); sync_global_to_guest(vm, is_forced_emulation_enabled); - if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) { + if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM || + vm->type == KVM_X86_SNP_VM) { struct kvm_sev_init init = { 0 }; vm_sev_ioctl(vm, KVM_SEV_INIT2, &init); @@ -1138,7 +1139,8 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) void kvm_init_vm_address_properties(struct kvm_vm *vm) { - if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) { + if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM || + vm->type == KVM_X86_SNP_VM) { vm->arch.sev_fd = open_sev_dev_path_or_exit(); vm->arch.c_bit = BIT_ULL(this_cpu_property(X86_PROPERTY_SEV_C_BIT)); vm->gpa_tag_mask = vm->arch.c_bit; diff --git a/tools/testing/selftests/kvm/lib/x86_64/sev.c b/tools/testing/selftests/kvm/lib/x86_64/sev.c index 290eb0bd495e..75c1463b98d4 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/sev.c +++ b/tools/testing/selftests/kvm/lib/x86_64/sev.c @@ -14,7 +14,8 @@ * and find the first range, but that's correct because the condition * expression would cause us to quit the loop. */ -static int encrypt_region(struct kvm_vm *vm, struct userspace_mem_region *region) +static int encrypt_region(struct kvm_vm *vm, struct userspace_mem_region *region, + uint8_t page_type) { const struct sparsebit *protected_phy_pages = region->protected_phy_pages; const vm_paddr_t gpa_base = region->region.guest_phys_addr; @@ -25,12 +26,23 @@ static int encrypt_region(struct kvm_vm *vm, struct userspace_mem_region *region if (!sparsebit_any_set(protected_phy_pages)) return 0; - sev_register_encrypted_memory(vm, region); + if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) + sev_register_encrypted_memory(vm, region); sparsebit_for_each_set_range(protected_phy_pages, i, j) { const uint64_t size = (j - i + 1) * vm->page_size; const uint64_t offset = (i - lowest_page_in_region) * vm->page_size; + if (vm->type == KVM_X86_SNP_VM) { + vm_mem_set_private(vm, gpa_base + offset, size); + ret = __snp_launch_update_data(vm, gpa_base + offset, + (uint64_t)addr_gpa2hva(vm, gpa_base + offset), + size, page_type); + if (ret) + return ret; + continue; + } + ret = __sev_launch_update_data(vm, gpa_base + offset, (uint64_t)addr_gpa2hva(vm, gpa_base + offset), size); @@ -68,6 +80,14 @@ void sev_es_vm_init(struct kvm_vm *vm) } } +void snp_vm_init(struct kvm_vm *vm) +{ + struct kvm_sev_init init = { 0 }; + + assert(vm->type == KVM_X86_SNP_VM); + vm_sev_ioctl(vm, KVM_SEV_INIT2, &init); +} + int __sev_vm_launch_start(struct kvm_vm *vm, uint32_t policy) { struct kvm_sev_launch_start launch_start = { @@ -83,7 +103,7 @@ int __sev_vm_launch_update(struct kvm_vm *vm, uint32_t policy) int ctr; hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) { - int ret = encrypt_region(vm, region); + int ret = encrypt_region(vm, region, 0); if (ret) return ret; @@ -112,6 +132,41 @@ int __sev_vm_launch_finish(struct kvm_vm *vm) return __vm_sev_ioctl(vm, KVM_SEV_LAUNCH_FINISH, NULL); } +int __snp_vm_launch_start(struct kvm_vm *vm, uint64_t policy, uint8_t flags) +{ + struct kvm_sev_snp_launch_start launch_start = { + .policy = policy, + .flags = flags, + }; + + return __vm_sev_ioctl(vm, KVM_SEV_SNP_LAUNCH_START, &launch_start); +} + +int __snp_vm_launch_update(struct kvm_vm *vm, uint8_t page_type) +{ + struct userspace_mem_region *region; + int ctr, ret; + + hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) { + ret = encrypt_region(vm, region, page_type); + if (ret) + return ret; + } + + vm->arch.is_pt_protected = true; + + return 0; +} + +int __snp_vm_launch_finish(struct kvm_vm *vm, uint16_t flags) +{ + struct kvm_sev_snp_launch_finish launch_finish = { + .flags = flags, + }; + + return __vm_sev_ioctl(vm, KVM_SEV_SNP_LAUNCH_FINISH, &launch_finish); +} + void sev_vm_launch(struct kvm_vm *vm, uint32_t policy) { struct kvm_sev_guest_status status; @@ -158,6 +213,45 @@ void sev_vm_launch_finish(struct kvm_vm *vm) TEST_ASSERT_EQ(status.state, SEV_GUEST_STATE_RUNNING); } +void snp_vm_launch_start(struct kvm_vm *vm, uint64_t policy) +{ + int ret = __snp_vm_launch_start(vm, policy, 0); + + TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_SEV_SNP_LAUNCH_START, ret, vm); +} + +void snp_vm_launch_update(struct kvm_vm *vm) +{ + int ret = __snp_vm_launch_update(vm, KVM_SEV_SNP_PAGE_TYPE_NORMAL); + + TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_SEV_SNP_LAUNCH_UPDATE, ret, vm); +} + +void snp_vm_launch_finish(struct kvm_vm *vm) +{ + int ret = __snp_vm_launch_finish(vm, 0); + + TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_SEV_SNP_LAUNCH_FINISH, ret, vm); +} + +bool is_kvm_snp_supported(void) +{ + int sev_fd = open_sev_dev_path_or_exit(); + struct sev_user_data_status sev_status; + + struct sev_issue_cmd arg = { + .cmd = SEV_PLATFORM_STATUS, + .data = (unsigned long)&sev_status, + }; + + kvm_ioctl(sev_fd, SEV_ISSUE_CMD, &arg); + close(sev_fd); + + return sev_status.api_major > SNP_FW_REQ_VER_MAJOR || + (sev_status.api_major == SNP_FW_REQ_VER_MAJOR && + sev_status.api_minor >= SNP_FW_REQ_VER_MINOR); +} + struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t type, void *guest_code, struct kvm_vcpu **cpu) { @@ -174,8 +268,22 @@ struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t type, void *guest_code, return vm; } -void vm_sev_launch(struct kvm_vm *vm, uint32_t policy, uint8_t *measurement) +void vm_sev_launch(struct kvm_vm *vm, uint64_t policy, uint8_t *measurement) { + if (vm->type == KVM_X86_SNP_VM) { + vm_enable_cap(vm, KVM_CAP_EXIT_HYPERCALL, (1 << KVM_HC_MAP_GPA_RANGE)); + + snp_vm_launch_start(vm, policy); + + snp_vm_launch_update(vm); + + snp_vm_launch_finish(vm); + + return; + } + + ASSERT_SEV_POLICY(vm->type, policy); + sev_vm_launch(vm, policy); if (!measurement) diff --git a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c index 04f24d5f0987..0b65b59b9b40 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +++ b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c @@ -16,6 +16,27 @@ #define XFEATURE_MASK_X87_AVX (XFEATURE_MASK_FP | XFEATURE_MASK_SSE | XFEATURE_MASK_YMM) +static bool is_smt_active(void) +{ + FILE *f; + + f = fopen("/sys/devices/system/cpu/smt/active", "r"); + if (!f) + return false; + + return fgetc(f) - '0'; +} + +static void guest_snp_code(void) +{ + GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ENABLED); + GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ES_ENABLED); + GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_SNP_ENABLED); + + wrmsr(MSR_AMD64_SEV_ES_GHCB, GHCB_MSR_TERM_REQ); + __asm__ __volatile__("rep; vmmcall"); +} + static void guest_sev_es_code(void) { /* TODO: Check CPUID after GHCB-based hypercall support is added. */ @@ -61,7 +82,7 @@ static void compare_xsave(u8 *from_host, u8 *from_guest) abort(); } -static void test_sync_vmsa(uint32_t policy) +static void test_sync_vmsa(uint32_t type, uint64_t policy) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; @@ -77,7 +98,10 @@ static void test_sync_vmsa(uint32_t policy) .xcrs[0].value = XFEATURE_MASK_X87_AVX, }; - vm = vm_sev_create_with_one_vcpu(KVM_X86_SEV_ES_VM, guest_code_xsave, &vcpu); + TEST_ASSERT(type != KVM_X86_SEV_VM, + "sync_vmsa only supported for SEV-ES and SNP VM types"); + + vm = vm_sev_create_with_one_vcpu(type, guest_code_xsave, &vcpu); gva = vm_vaddr_alloc_shared(vm, PAGE_SIZE, KVM_UTIL_MIN_VADDR, MEM_REGION_TEST_DATA); hva = addr_gva2hva(vm, gva); @@ -99,7 +123,7 @@ static void test_sync_vmsa(uint32_t policy) : "ymm4", "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)"); vcpu_xsave_set(vcpu, &xsave); - vm_sev_launch(vm, SEV_POLICY_ES | policy, NULL); + vm_sev_launch(vm, policy, NULL); /* This page is shared, so make it decrypted. */ memset(hva, 0, 4096); @@ -118,14 +142,12 @@ static void test_sync_vmsa(uint32_t policy) kvm_vm_free(vm); } -static void test_sev(void *guest_code, uint64_t policy) +static void test_sev(void *guest_code, uint32_t type, uint64_t policy) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; struct ucall uc; - uint32_t type = policy & SEV_POLICY_ES ? KVM_X86_SEV_ES_VM : KVM_X86_SEV_VM; - vm = vm_sev_create_with_one_vcpu(type, guest_code, &vcpu); /* TODO: Validate the measurement is as expected. */ @@ -134,7 +156,7 @@ static void test_sev(void *guest_code, uint64_t policy) for (;;) { vcpu_run(vcpu); - if (policy & SEV_POLICY_ES) { + if (vm->type == KVM_X86_SEV_ES_VM || vm->type == KVM_X86_SNP_VM) { TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SYSTEM_EVENT, "Wanted SYSTEM_EVENT, got %s", exit_reason_str(vcpu->run->exit_reason)); @@ -188,19 +210,38 @@ int main(int argc, char *argv[]) { TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV)); - test_sev(guest_sev_code, SEV_POLICY_NO_DBG); - test_sev(guest_sev_code, 0); + test_sev(guest_sev_code, KVM_X86_SEV_VM, SEV_POLICY_NO_DBG); + test_sev(guest_sev_code, KVM_X86_SEV_VM, 0); if (kvm_cpu_has(X86_FEATURE_SEV_ES)) { - test_sev(guest_sev_es_code, SEV_POLICY_ES | SEV_POLICY_NO_DBG); - test_sev(guest_sev_es_code, SEV_POLICY_ES); + test_sev(guest_sev_es_code, KVM_X86_SEV_ES_VM, SEV_POLICY_ES | SEV_POLICY_NO_DBG); + test_sev(guest_sev_es_code, KVM_X86_SEV_ES_VM, SEV_POLICY_ES); test_sev_es_shutdown(); if (kvm_has_cap(KVM_CAP_XCRS) && (xgetbv(0) & XFEATURE_MASK_X87_AVX) == XFEATURE_MASK_X87_AVX) { - test_sync_vmsa(0); - test_sync_vmsa(SEV_POLICY_NO_DBG); + test_sync_vmsa(KVM_X86_SEV_ES_VM, SEV_POLICY_ES); + test_sync_vmsa(KVM_X86_SEV_ES_VM, SEV_POLICY_ES | SEV_POLICY_NO_DBG); + } + } + + if (kvm_cpu_has(X86_FEATURE_SNP) && is_kvm_snp_supported()) { + unsigned long snp_policy = SNP_POLICY; + + if (unlikely(!is_smt_active())) + snp_policy &= ~SNP_POLICY_SMT; + + test_sev(guest_snp_code, KVM_X86_SNP_VM, snp_policy); + /* Test minimum firmware level */ + test_sev(guest_snp_code, KVM_X86_SNP_VM, + snp_policy | + SNP_FW_VER_MAJOR(SNP_FW_REQ_VER_MAJOR) | + SNP_FW_VER_MINOR(SNP_FW_REQ_VER_MINOR)); + + if (kvm_has_cap(KVM_CAP_XCRS) && + (xgetbv(0) & XFEATURE_MASK_X87_AVX) == XFEATURE_MASK_X87_AVX) { + test_sync_vmsa(KVM_X86_SNP_VM, snp_policy); } } From patchwork Fri Aug 16 19:23:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pratik R. Sampat" X-Patchwork-Id: 13766796 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2070.outbound.protection.outlook.com [40.107.93.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6944B1C4602; Fri, 16 Aug 2024 19:23:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.70 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836239; cv=fail; b=btz45YIo3ubr9Hc+yagG0Uu2IpARuHQPVLVxSUtZ0UR/fOEhhNhq5Z9DajsUn5OTKn3p4G8lL7GAPUOLaUXLlAAkgFMSOpZcQtBaUCm5GeoXQKoOi60H5eul9gKtcpncZbAvY7egcrSYuhGZIWfAoi/Nb8gsQnBMfhwuq6M+hiw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836239; c=relaxed/simple; bh=AX+2EMoa/HLDbMgYoCjGl9+ZiPBGRwWGjjf19h1rcMw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BzzrWhAFn/iuIS1Mk8RtNxTZ1ljJpJsGWOb2EnNTaKrd8Dw8N9JT4uhj+MHTdsFlif9MbeKmDL/3gf3dHmhe4CC0MTZXWhcogz94AgbsIUzQ1MaZxEUg8aeEdqkqgPaoqbzkfHj0UXPK3r8SAVDt4oBjYuaFtUgSsFf6iSN/TW4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=yzPCPMb2; arc=fail smtp.client-ip=40.107.93.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="yzPCPMb2" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pb1YTI887VBw3j5DVbg/QJiO23psgqzV5u6EHYlSRRvXhAH0UFvTXi6jBjD+/Fkf0Kmht7jdVsOD9xIYfPUaVvQw1PiIA6XH2X0CxDpRyDm6jIPpeXKkEqnlX0yq+ebVUJLpodGfHvXPr/ePqs3/KBU8lF6w7KJEnhuzXt+nBYvSkKa45AX9zQiIvHoB7jbT5xRlpr810Og3WDFpRsXPWueA0x06Qg3lNxZiNnRFnJmdDQ7miNwdMSAzx0yVrTlLM1jss0T0/uUtrs7WwtIT39aPE15t9qTNJVWDBKYSF9IZUltGScWWSTgplNWhqwuLbSYe1Cv0ZRyGCjUqUp81PA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UdYFkl2xVGNKLXDOwrZh2ldVl6wuOpVobuY1t9dVTvQ=; b=FFQ7bP5M1lWV/lylFcK/uGyk6iPGqA1HaNrZHmQ/B1jrJIVXfkS77I3VRhyeWOYvp261x1No+j8cJFtTvLUP/e3swEsy26qgYTqWQ3W4PhNv3iq32gpwFQopYOhaftR7wDfkQh6hrPG5TMzk+YQ4ZWCQhkK32LNGt5hsfQIRzVVqaZtE8C9sx0mcrOlRFdevP0rhLJXjcU/8EjS7+fqu3gX9/nRqEs1xKZQMG1KDxrwC440cUm27l4KFK7PSRj8ejsaMbSbnHEKhxIYiUG55WDpZbq4F3b1qwRxZ/OmnflzEJWYwFbI+7AWDhtspOo86xhxp8kgr4mNe/KBZrCJD4w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UdYFkl2xVGNKLXDOwrZh2ldVl6wuOpVobuY1t9dVTvQ=; b=yzPCPMb29vCiUoMU9GgYriqHfmsiHqj0jfzedVhNmK+8r9rsV7tWruVDKkE71ZnQeD0fvOXgX+JqdCQ4SFLXOtG3ytWCl/5WBfTpU7mkm5IlUX2Sm1dpG33ayYxym9umZjx7wGYpDR0ebYi8GC8jnlEe3l42dhaNf8TsHCuMg6c= Received: from MW4PR03CA0014.namprd03.prod.outlook.com (2603:10b6:303:8f::19) by CH3PR12MB8878.namprd12.prod.outlook.com (2603:10b6:610:17e::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.20; Fri, 16 Aug 2024 19:23:54 +0000 Received: from SJ1PEPF00001CEB.namprd03.prod.outlook.com (2603:10b6:303:8f:cafe::61) by MW4PR03CA0014.outlook.office365.com (2603:10b6:303:8f::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.20 via Frontend Transport; Fri, 16 Aug 2024 19:23:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CEB.mail.protection.outlook.com (10.167.242.27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7828.19 via Frontend Transport; Fri, 16 Aug 2024 19:23:54 +0000 Received: from AUSPRSAMPAT.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 16 Aug 2024 14:23:53 -0500 From: "Pratik R. Sampat" To: CC: , , , , , , , Subject: [PATCH v2 3/9] KVM: selftests: Add SNP to shutdown testing Date: Fri, 16 Aug 2024 14:23:04 -0500 Message-ID: <20240816192310.117456-4-pratikrajesh.sampat@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> References: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CEB:EE_|CH3PR12MB8878:EE_ X-MS-Office365-Filtering-Correlation-Id: dfd64a3e-4a0f-42e1-9f29-08dcbe28f7d3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|376014|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: HNZBwdMsAR8Vb6Pmu87EjyUFiDeAPdwECj8BgP2KZke6NyeOKq4e9r7WceBvUd7abtOrb2I3LLsWBWjop4bSwWxrZJygRtnLlQNVxjQbATCB9x/eHQssmvaWImcbi8FWH4Tmp2A60eJxDGIwRLux/vgIxGexqXNFGzAJhtaIWMMS5q/JVT2fJ26p0Oi72hd4Oot7GUZNfc9iD5+VmxulRwBD7rK5vL3xygXutxjw2FHrKuLOBX8BQwPdMl45o87//IalKtB4K8DqeBTpPDHU+Wv70UUrdd0ZV6QwmTTweYaWMpOpmCnX6he1udNxok2XxC+06GK/MIXps8lR/umyapiY3dbgLwo1P7LEtBjTPpfoyUmpREU9AgK0sE6GkMutAzrgDv8PlAaYF6tIub+XXRQzT03uAR+5WjM8Kgu3ZQxfI3RoG/s8fvTXVJRVqoMloEoznMlBLvVbTl8CmwjtLo0dB5bd8lava2aL0uCG+rDObNpvh52wmqT9WUx3xPjAkDux+S/cTCi57+s9HHIjfsgs7MFlvMIrE2k5m2pGkwnquB5LeGJxWJ/iVxMhO1T4y0eom0SzIWWe1WwyeLi13OOQBNlijH0EUF8azSEED9wr7bsSQ7INiQerFjmpaGb5mTAPsmbffnckW40fUsn4iVaXv3pUQlH5dulGiCmhViXtx7X/wH55um3bpPuQQTefKvG1woX+uYn4CufbrClkBnUcF1Znz6DXjuzKlcXdQqTkNXjOyVFyR3KdJjpBQFvNhYrUS2V+0pxMQsmhXt7ry11elD0G3mk1nHSdEay23JsC96DY54GBrVx2cxr/bTBeG0FD2l2283Y23clvcSt4i5aay/4qJONTKBdi0wOtf+f7THnbso7I9UFmTdh7eOLPCpu2MgYmUlvda+mCIcSB2Telxki78vBBHQ2bwXSydytLKuZKP8VwwxqyHqAzbs4nRiDFIct1IJLiSjV0PA6zd+jXEMnKfUm6qfxQ0NqdLl1AQ83Bx+PAft2vxrLWD15R6eFBypX2nW9K+dktQonNPPVg73Je0RdfsnWya30/ywKX4MIka/p8YYCqDn1Eo5XhfPuuWAWZzF/cvlbkVIrc5mwOxrsSJEVFNiVjk45UMWI1421lGxeLeJ4LM9iEcn0NmrM8Hi9TjP/2gcyUgRZ4nxEOsfScBNSF1OUIHX3fPY6uhDPy+3W43QiU+tWpK2BsFb68ar4ijNqrRSg5zT8wiuXe2Gq3UMRTG+yQBAJFd5V+VgKYWGBgTtnVgKuMiIB0PpNP5bMdBO14tIn7Bj78Pps3YGIWgMgGoCoT94vkRpfN8oTBU3I7JZwMvTYSwlhXNKiwwsVVEP2tfZReW6WcfGcyUsNqEih7ffTKeCHUSK2E4Zx/VytgWfpSyWoPNgywgShf7X0Xg+243t2yE+8uPKhRyD0ST/ZkxJTlQBuiup4= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(376014)(36860700013)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Aug 2024 19:23:54.4171 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dfd64a3e-4a0f-42e1-9f29-08dcbe28f7d3 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CEB.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8878 Parameterize the shutdown test to include the SEV-SNP VM type Signed-off-by: Pratik R. Sampat --- tools/testing/selftests/kvm/x86_64/sev_smoke_test.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c index 0b65b59b9b40..c5a9fda49554 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +++ b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c @@ -187,16 +187,14 @@ static void guest_shutdown_code(void) __asm__ __volatile__("ud2"); } -static void test_sev_es_shutdown(void) +static void test_sev_shutdown(uint32_t type, uint64_t policy) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; - uint32_t type = KVM_X86_SEV_ES_VM; - vm = vm_sev_create_with_one_vcpu(type, guest_shutdown_code, &vcpu); - vm_sev_launch(vm, SEV_POLICY_ES, NULL); + vm_sev_launch(vm, policy, NULL); vcpu_run(vcpu); TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SHUTDOWN, @@ -217,7 +215,7 @@ int main(int argc, char *argv[]) test_sev(guest_sev_es_code, KVM_X86_SEV_ES_VM, SEV_POLICY_ES | SEV_POLICY_NO_DBG); test_sev(guest_sev_es_code, KVM_X86_SEV_ES_VM, SEV_POLICY_ES); - test_sev_es_shutdown(); + test_sev_shutdown(KVM_X86_SEV_ES_VM, SEV_POLICY_ES); if (kvm_has_cap(KVM_CAP_XCRS) && (xgetbv(0) & XFEATURE_MASK_X87_AVX) == XFEATURE_MASK_X87_AVX) { @@ -239,6 +237,8 @@ int main(int argc, char *argv[]) SNP_FW_VER_MAJOR(SNP_FW_REQ_VER_MAJOR) | SNP_FW_VER_MINOR(SNP_FW_REQ_VER_MINOR)); + test_sev_shutdown(KVM_X86_SNP_VM, snp_policy); + if (kvm_has_cap(KVM_CAP_XCRS) && (xgetbv(0) & XFEATURE_MASK_X87_AVX) == XFEATURE_MASK_X87_AVX) { test_sync_vmsa(KVM_X86_SNP_VM, snp_policy); From patchwork Fri Aug 16 19:23:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pratik R. Sampat" X-Patchwork-Id: 13766797 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2054.outbound.protection.outlook.com [40.107.237.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 336931C4609; Fri, 16 Aug 2024 19:24:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.54 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836251; cv=fail; b=XoiaS0VdbEDPLcVHEvJr6CdbQkcHNQUqzKpDLw5yocjeVObU108xlNh0tuUOZOkYY2xRmCklQ1mHwNBngv6IOSlOE7TcQfrDTTlt672eQcWV0UKCmsj/rzFhV5Jur2rfsMpK2Yuxeogk17LGN2jQ0YNkKsMUvloYoJa8X5Nc/5s= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836251; c=relaxed/simple; bh=rvNxInwGf6wUzTnOeTAMt7yYqLiCqMh268KyAcxkZtE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DH7THgewP9Xx2POkoKRIdU51Ec3EOB3Pxl4dXR3Agte5CiBUFbLBc/SzLbWsDxsUe7HICfLMkoCnAF4oUN5LvmgNxJpb/Ts3RWT/q8Obgt3/bCn3qdpRkCnxJ0QorWfgODKhBQHHm7QYmaPmk0LO24/Ymjlqw2Qw9suelWjpQ/M= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=rgw7i1pR; arc=fail smtp.client-ip=40.107.237.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="rgw7i1pR" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=GKYORayKdwoWm8w7y9jJjArWudASjVxv2Qqb3F9sWYhJ3dllNpFXj0a7ptSOxosS8QFWL3v8naqv4TM1mUcSD2Vw3Bcj7HCWsjPz1TeWonoy87DPFECWYQAT+HbI+xcQ0O1KlSnPLVL0ABXjgeyZ53BbWKRtPT6EEDPCq3LIuFt34gLlbMKl9N7KcV152ajFqiGCqarwkCgv4l234sr/QDY2ebvMZpz01SgCVR2n3eBfu/FDTROxWU0tTC1t8akqLZZfuJRt5lC7CyXdMcAfWL/nGIcylRs/BsplKMNDVPXKTDTi4CSx259jHO7uAks5V8MEQ1irt2og1DAMFGC41Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=VxeV025tvPb0Xme+rrM5uNQ4QVzViIugChkTE39mgWA=; b=XtgIIeur2nJqQQKt/I0j/JuCDMbxAl5ksM3QdNIeRUIdfhXVX3bQXcGehDilvI7HJODIyjb76Vk8kXi9fbwE9xSZnYyF2r5yDsfYpJnKpMWbpVgHDbhjoX1Vp/YVszI/NIQU9EdALJL6MJoUuWBpzEUEnArAhu6yzYpJ1Ht10eCCnAn0s2DyzLLpipTPUreU//CU3py1h0/KQ6qCiMAfFc5gXM2ht2Yg45DeQrP/1S9bUYVTFTQgWRhIY0H80GytDDMwRnuBQYBplkfEhvVsOB53q//JKxAaQmwFh/UfOKEYFWw/bD4+ty7TJ2Bgq43zFQ15k5zmo2pL4kS2n7GD8w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VxeV025tvPb0Xme+rrM5uNQ4QVzViIugChkTE39mgWA=; b=rgw7i1pRZnS/cdhTDT5PDksMr80LbqUGO9xGWDLt5vN1yfH6vpRxbLe4GxLJmydA6x1qTnvjaJKG3xlJO0iykBSFWWdp3eDBpx3Vi+FsvpvyAvACDOq1qmx1ikFmrp+KEMyOA4PQ/T7AWDhdIp2ppufH/HnuwVoNl6Mq4UyPlec= Received: from MW4PR04CA0061.namprd04.prod.outlook.com (2603:10b6:303:6b::6) by SJ0PR12MB6830.namprd12.prod.outlook.com (2603:10b6:a03:47c::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.20; Fri, 16 Aug 2024 19:24:05 +0000 Received: from SJ1PEPF00001CE4.namprd03.prod.outlook.com (2603:10b6:303:6b:cafe::42) by MW4PR04CA0061.outlook.office365.com (2603:10b6:303:6b::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7849.24 via Frontend Transport; Fri, 16 Aug 2024 19:24:05 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CE4.mail.protection.outlook.com (10.167.242.20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7828.19 via Frontend Transport; Fri, 16 Aug 2024 19:24:05 +0000 Received: from AUSPRSAMPAT.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 16 Aug 2024 14:24:04 -0500 From: "Pratik R. Sampat" To: CC: , , , , , , , Subject: [PATCH v2 4/9] KVM: selftests: SEV IOCTL test Date: Fri, 16 Aug 2024 14:23:05 -0500 Message-ID: <20240816192310.117456-5-pratikrajesh.sampat@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> References: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CE4:EE_|SJ0PR12MB6830:EE_ X-MS-Office365-Filtering-Correlation-Id: 89bbc030-6965-4c79-7950-08dcbe28fe49 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|36860700013|376014|82310400026; X-Microsoft-Antispam-Message-Info: 1cAFafD+GaomxnvbEgc66+CsZLayNv3hXZvTnhVVLwxsov3K8zEB6aByeU5+RH66tYDuZc7XCCiQTRc8dRpFPaZ3s2aaZydyFmFv5R/j6S263zc6YuDpqeM5ZWYCORheKg7nuPrZYRYjGYn4lpnrSw2dftmOLwWf/cmN3SZ9cSEOYqWtEbIXgDMZjV3iZ3UeT0q659X74UcMz3RtbINnOntnDbHqNiMDVjxD8o6KAtCwjROx1xGo9CBZ9qQje1LfBR4OS84KUdqvud2AI9MNxL3QDDUK4WTmV6GaTdC9UJAKsyqvurd7/2YPHkOBJe50sfVeWm4cCHvFrLIjn1YTZ58HM+1npHup8JPLDTUk5KNosSoPGm6JiMAprfCgKu0wY3bEKqWzFLqwQ4Nd4aFyFJumL6ut8zPtguaI5JRPmRvWnqygOifjcc96IAS99hzuiUqxmppwcySkCCMyIJkD8PgEmz+yp7K0P8fr/ZaSZHL6u466N7qfFjywM/ZU0T3f20F+QvkQx14neQXLqjT2jjPd4ZtNCMVpYxR9j38d7geS6R3bfMY198WanxrsS4ANDifwVa6nK4S3iQin0jg8BFu0st0pyE5Xtu8U43295pwllQk64JU4RRRGlRko7JAcWlqcf17zzuUhpQAr4o5XwWHlmJT9yC+C0pZGjyV2kSCM7Vf2Ba46hykzkz8QBWvaFDOe3qJBVQw8aXruVQqr9Luz2/lgYKmpe98HZT4q5/36PZyFEIVoP0W6/p4AYOu7OUC53ezwTGSO8tz0/vYGJ2K0n9tnlYOHiEsGqw4yW0nuBpTHdMV/eAJigP+SVmnNUMq+8TSsHf45mIqxfJ+e48dyyuOvuzX/EKWRXCF94bgvPrTHaxjdHUChyqss9v3gshernKEBya07IHGnNYB1i85zMslAd5YFkAKPRMjVtAOYSY41L1fxFJ993P6Mg9a7bOeVOsudeiwXWBNZH/AaKqYUHTQAFJVEZIoCs45EHG9Br2tRyK8db0ZEPHD9V3QPAgyRHoc5MXubJyCJGUSoElLerZaTL70+NkGvfmRyJioETQCu3kN/ryOp0CFO10NlJSUh9QQrnAhKExkGBMdOLsxAOtmIfbdeLBlTJ4qENVzxfEE5eDiAsNSCctKKighVEFm+PP8cKzsCFoRQ8tVmUyjIwL/n5Zx+JFAX980Imei3dhW9aIUD0vpqTxE2xe231Cmk3SykCo4JLFCqTOTFS0MDIwJoipVkMXSB9wqYWU1NH3889EnEkIynnritIms/MuohKHM8ZdPoSPM1flOyR08TgPSGHR/Tmu2OR4tRVHGjOXNZ9wvG52r626qqfrGl7s8gCg4TSvbVf/TuoLG5RgJUyxggp/sBXnXsxkcdzzCY9LdONbi7PcSeYOcp6KPsIbSjS5bnKZ6c1sAyuAYrstx2T4CAdk7y2NKloLNSaSF8j1gCf1qoAdJJXIEGr8nw X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(36860700013)(376014)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Aug 2024 19:24:05.2402 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 89bbc030-6965-4c79-7950-08dcbe28fe49 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CE4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6830 Introduce tests for sev and sev-es ioctl that exercises the boot path of launch, update and finish on an invalid policy. Signed-off-by: Pratik R. Sampat --- .../selftests/kvm/x86_64/sev_smoke_test.c | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c index c5a9fda49554..cca2e0467f3d 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +++ b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c @@ -142,12 +142,97 @@ static void test_sync_vmsa(uint32_t type, uint64_t policy) kvm_vm_free(vm); } +static void sev_guest_neg_status_assert(struct kvm_vm *vm, uint32_t type) +{ + struct kvm_sev_guest_status status; + int ret; + + ret = __vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status); + TEST_ASSERT(ret, "KVM_SEV_GUEST_STATUS should fail, invalid VM Type."); +} + +static void vm_sev_es_launch_neg(struct kvm_vm *vm, uint32_t type, uint64_t policy) +{ + int ret; + + /* Launch start with policy SEV_POLICY_NO_DBG (0x0) */ + ret = __sev_vm_launch_start(vm, 0); + TEST_ASSERT(ret, + "KVM_SEV_LAUNCH_START should fail due to type (%d) - policy(0x0) mismatch", + type); + + ret = __sev_vm_launch_update(vm, policy); + TEST_ASSERT(ret, + "KVM_SEV_LAUNCH_UPDATE should fail due to LAUNCH_START. type: %d policy: 0x%lx", + type, policy); + sev_guest_neg_status_assert(vm, type); + + ret = __sev_vm_launch_measure(vm, alloca(256)); + TEST_ASSERT(ret, + "KVM_SEV_LAUNCH_UPDATE should fail due to LAUNCH_START. type: %d policy: 0x%lx", + type, policy); + sev_guest_neg_status_assert(vm, type); + + ret = __sev_vm_launch_finish(vm); + TEST_ASSERT(ret, + "KVM_SEV_LAUNCH_UPDATE should fail due to LAUNCH_START. type: %d policy: 0x%lx", + type, policy); + sev_guest_neg_status_assert(vm, type); +} + +/* + * Test for SEV ioctl launch path + * + * VMs of the type SEV and SEV-ES are created, however they are launched with + * an empty policy to observe the effect on the control flow of launching a VM. + * + * SEV - Expected to pass through the path of launch start, update, measure, + * and finish. vcpu_run expected to fail with error KVM_EXIT_IO. + * + * SEV-ES - Expected to fail the launch start as vm created with type + * KVM_X86_DEFAULT_VM but policy passed to launch start is KVM_X86_SEV_ES_VM. + * Post this calls that pass the correct policy to update, measure, and finish + * are also expected to fail cascading. + */ +static void test_sev_launch(void *guest_code, uint32_t type, uint64_t policy) +{ + int exp_exit_reason; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + + vm = vm_sev_create_with_one_vcpu(type, guest_code, &vcpu); + + if (type == KVM_X86_SEV_VM) { + sev_vm_launch(vm, 0); + sev_vm_launch_measure(vm, alloca(256)); + sev_vm_launch_finish(vm); + } else { + vm_sev_es_launch_neg(vm, type, policy); + } + + vcpu_run(vcpu); + get_ucall(vcpu, &uc); + if (type == KVM_X86_SEV_VM) + exp_exit_reason = KVM_EXIT_IO; + else + exp_exit_reason = KVM_EXIT_FAIL_ENTRY; + + TEST_ASSERT(vcpu->run->exit_reason == exp_exit_reason, + "vcpu_run failed exit expected: %d, got: %d", + exp_exit_reason, vcpu->run->exit_reason); + + kvm_vm_free(vm); +} + static void test_sev(void *guest_code, uint32_t type, uint64_t policy) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; struct ucall uc; + test_sev_launch(guest_code, type, policy); + vm = vm_sev_create_with_one_vcpu(type, guest_code, &vcpu); /* TODO: Validate the measurement is as expected. */ From patchwork Fri Aug 16 19:23:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pratik R. Sampat" X-Patchwork-Id: 13766798 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 603251C3F32; Fri, 16 Aug 2024 19:24:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.40 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836263; cv=fail; b=WNsDefOi8gScQG9+Lx7eI4iPDuZAXp+Gqk/emb66KBa57Nxqm1OjJbztVkFyOYuolyN091jZyKpizSkVRQgKZjUJeqF6bV6vJtldnab9laXFOyLZ/Q5tH6kgx67EdCuTk17pJF2xG0GhSY/nth/bLmZRH+jY7yejjByS6Cy4OZI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836263; c=relaxed/simple; bh=3eq4PzKo9bfvQPwtj/TEUl8ygIhzHPr4BecB7o+jH80=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dXDmryOZIoK4O7JK7ZZOmNCCHjFGek88keR3qIxGhwYkD79BXKzE91Y/Ir99ARxmndYHgmgj7njwEa8Iw5QwkJ3rARv3cqJKchTJSYzQPikJOLHXeOz70qzf2TMqT2Ko6j3AMeNVpFFaGRW2cL/9fVnxCB3NAOTghbZw5/XPckc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=gKBBUkt2; arc=fail smtp.client-ip=40.107.236.40 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="gKBBUkt2" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=YAyuCPXSqkhu5MprohREJsd06cmCmaLsbR86gL7z5xqPBarQJjHPajZHU2BvNwps4e0uBcUa+Xf6eezU2yISK1XblUYf4jU69yRrvLqPoolu0cfypK+D9zA4XC2Kxxo8KrmQe+Ul+6iOJwDO5nupSlaWp5KXbl/3tOoEBRIYbrSn3IYbzkIByyibkvpDcOIP2/DtwzTBFlndYNVjPvybdAZll+DbGNrg7UmkdM85qlhOqWoj/s/3fFeIEyL1X35J5v2MKfLhRw94u7POME6XRjowyPkNz7VJoL4wHSm9bw8b3IgGpQTYuEJPfoX2lE1XtO32CW4lWO8v5qGqbk/Dew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=EA66KCYDdQe8DKKCgR0HxvBakgkpa82FaCXVGuVZnfE=; b=ME8HdJa6vHrDeoYtVT8bhtUGVPjG/V+sP92S9H7OGBxBDiOqyebXDlZfPt82Sesf4/iFKJ5jn9hgHkGb0K87Bg1riC7M8YojMuV7UKgudG9ZcovdtqY5XpGoG5Iw4jYjUNMvo+Ddx+e7EifyhyY+kSbjG/vP+YSe8RzYyMHppMQj+Kt9O3udxPFlA7bISuwYdaUvkGU8viGO9NgpRHfu+wHfIKOvNMOZa7QDvyEfi8YpUFQ//pJJO+R1J+xAm6LwBjJOlWMvcTOGm2dN0nCLJmyQQYbBkfrSXq3gDnhfsdTHX2MgKMe+J8y3Uui2JtQNG54OVmypP8uWY5nV5ddrGQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=EA66KCYDdQe8DKKCgR0HxvBakgkpa82FaCXVGuVZnfE=; b=gKBBUkt221yCHKfZM4l/jyS/BRFwtxU2xXZZj784Q+uF1Ll/ulRR/Epi7iIeiERwKEQtEF1HZp7uXhwhZi30YfAC99kA79n0Rza2LEVHjTkiZyVz6tBFMkZLHvghKPQFO+BBBLeQlhy+t/d4Rh7VXUUoc9s1KNXvnT+a5hZVt9U= Received: from SJ0PR05CA0176.namprd05.prod.outlook.com (2603:10b6:a03:339::31) by DM6PR12MB4171.namprd12.prod.outlook.com (2603:10b6:5:21f::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.19; Fri, 16 Aug 2024 19:24:16 +0000 Received: from SJ1PEPF00001CE9.namprd03.prod.outlook.com (2603:10b6:a03:339:cafe::7d) by SJ0PR05CA0176.outlook.office365.com (2603:10b6:a03:339::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.17 via Frontend Transport; Fri, 16 Aug 2024 19:24:16 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CE9.mail.protection.outlook.com (10.167.242.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7828.19 via Frontend Transport; Fri, 16 Aug 2024 19:24:16 +0000 Received: from AUSPRSAMPAT.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 16 Aug 2024 14:24:15 -0500 From: "Pratik R. Sampat" To: CC: , , , , , , , Subject: [PATCH v2 5/9] KVM: selftests: SNP IOCTL test Date: Fri, 16 Aug 2024 14:23:06 -0500 Message-ID: <20240816192310.117456-6-pratikrajesh.sampat@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> References: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CE9:EE_|DM6PR12MB4171:EE_ X-MS-Office365-Filtering-Correlation-Id: d2e6f349-0d50-4fc9-11b4-08dcbe2904c8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|82310400026|1800799024|36860700013; X-Microsoft-Antispam-Message-Info: xcU06uP83/q3vTJ6/z2gmUe4y/JwQouWIwv3qIwzYOG2GmnrGbzxpYTApLjenvaEwqmNBirtzMpMv1lO8CvJKh7cR24RxdkHaHlx6Et/Si3y7S/72osiaOHP1Z5RhVGeeApgVx9sN1QIae4deaY7DsnKWcyp24wl2VAKPSt39cUjwI3Hz1Y5AlyMVT0mCJ1Ajxu+M7WP69RIRnnqI4W4wtCG98kDZPVs/cggIZA+eRoEZ75AY4aFrWJI2A9ugh59DXncu4c7quPDfAMszLiKTqr4Q8tgAuQ47dwNVQ+pINC/nYqliiK73XxToUG5TzQvBRXTkLkpPvvu4IY0YIfbv9vNy8AH9DhuvzepmOiEX5IW0TRPtBV3QGNvklVf/nRTJWW0ZvmS/X9yItpJfYxCfaAT/3Ztta+Cq6xs6ZnhJWeN+RALgg6tuLaTzb8kLZACUTDnv6zCvo5TVI3qovzm5XZdLvAA4OM+vBnS4kUTo68j9OzXpMc79GZ5u3CDoGp3e1moVSfB4oUQLlgiL0MkigP5ssOO1u1HTQsWfYKnUyZ9pU95A9hPE8hg4fukeLz0s1oHt5vhTvMmFU4SdvzvxSR3VBQ3R+Rbo6N5rt/fu9gLxEmFVzt+3/WbeML7jPN/TdKyLTBFWBKDDfhMsgfnkK41HEIGCuVA7pvYHrm4WlOjps2ZhfGzFzbQMQjwOEL4swFd8YwklAzPaOy9cLfoT4NSPyPnHnnggN2M+/maeSSvMFV9cMeaVHWY13BmY4TCaUQEmRIPE3owhRazCoBmOkYCz7+mfKBYsLTg0tBD+P5bEdIhLLPrLzyZ274onvnk3mY1tc1c+l6qflib4ortM0nS2uKaCUAcfiemgifHw5FDQgoUwZ4AHH5gj7c5AcGYRZ6zqet+sJIz9xCnB6G1PSf3G+xV4lpF13SbvfgtwQ2xmh6pfxBNTF4cu84VionZR/h1QyqhXxwWLAg+Pakgxla67UUWPyQeSDFpa5ggdfFVSiVSs1lv+NhRGod1XYXeJ7o1daBuXwo769lVAx+qjt/h8BtV3lMBf7Q9C+QAL0d0jMh6OsRFgiP79Rt9Gb5MDrXnUfc4o2Jj96P60jnwq3qTgF0LAcR7zHwYlFGheBrfkM0EvlVVahjoJ4KkswMegQzcIvdH7ZYd4DPAz/Jr0juX5U0P/72p9SScMjXb9qjlTg0EfdkDAz4Gr3QZCM4fG8hs7yqsCKeGjw6nxyG/xXeMOg/so08gRlezZAY9wnz+TrSwvC9nxWkvf2RpETTOdPFncML3gPO5ewRfnXGTEqe6TiYdlJo09FS8MHnFYHjtJ51hJiComEGhTKQ7BxpfHukesVFDd7DQMjUdF4TjuXmTmLD8klxUZAYG4jrwSgXXcaH2TwKYu96mszYs5SVlC6pbUQfuhrhaPemUTITvczKTMp9IJ4wE9g0jtTxkps7dPPKfmEgbhRkNeXxGnuD4 X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(376014)(82310400026)(1800799024)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Aug 2024 19:24:16.1518 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d2e6f349-0d50-4fc9-11b4-08dcbe2904c8 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CE9.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4171 Introduce testing of SNP ioctl calls. Tests attributes such as flags, page types, and policies in various combinations along the SNP launch path. Signed-off-by: Pratik R. Sampat --- .../testing/selftests/kvm/include/kvm_util.h | 11 ++ .../selftests/kvm/x86_64/sev_smoke_test.c | 140 +++++++++++++++++- 2 files changed, 150 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 63c2aaae51f3..144730efbffa 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -888,6 +888,17 @@ static inline struct kvm_vm *vm_create(uint32_t nr_runnable_vcpus) return __vm_create(VM_SHAPE_DEFAULT, nr_runnable_vcpus, 0); } +static inline struct kvm_vm *vm_create_type(unsigned long type, + uint32_t nr_runnable_vcpus) +{ + const struct vm_shape shape = { + .mode = VM_MODE_DEFAULT, + .type = type, + }; + + return __vm_create(shape, nr_runnable_vcpus, 0); +} + struct kvm_vm *__vm_create_with_vcpus(struct vm_shape shape, uint32_t nr_vcpus, uint64_t extra_mem_pages, void *guest_code, struct kvm_vcpu *vcpus[]); diff --git a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c index cca2e0467f3d..3901a0cf44ee 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +++ b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c @@ -225,13 +225,151 @@ static void test_sev_launch(void *guest_code, uint32_t type, uint64_t policy) kvm_vm_free(vm); } +static int __test_snp_launch_start(uint32_t type, uint64_t policy, + uint8_t flags, bool assert) +{ + struct kvm_vm *vm; + int ret = 0; + + vm = vm_create_type(type, 1); + ret = __snp_vm_launch_start(vm, policy, flags); + if (assert) + TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_SEV_SNP_LAUNCH_START, ret, vm); + kvm_vm_free(vm); + + return ret; +} + +static void test_snp_launch_start(uint32_t type, uint64_t policy) +{ + uint8_t i; + int ret; + + /* Flags must be zero for success */ + __test_snp_launch_start(type, policy, 0, true); + + for (i = 1; i < 8; i++) { + ret = __test_snp_launch_start(type, policy, BIT(i), false); + TEST_ASSERT(ret && errno == EINVAL, + "KVM_SEV_SNP_LAUNCH_START should fail, invalid flag\n" + "(type:%d policy:0x%lx, flag:0x%lx)", + type, policy, BIT(i)); + } + + ret = __test_snp_launch_start(type, SNP_POLICY_SMT, 0, false); + TEST_ASSERT(ret && errno == EINVAL, + "KVM_SEV_SNP_LAUNCH_START should fail, SNP_POLICY_RSVD_MBO policy bit not set\n" + "(type:%d policy:0x%llx, flags:0x0)", + type, SNP_POLICY_SMT); + + ret = __test_snp_launch_start(type, SNP_POLICY_RSVD_MBO, 0, false); + if (unlikely(!is_smt_active())) { + TEST_ASSERT(!ret, + "KVM_SEV_SNP_LAUNCH_START should succeed, SNP_POLICY_SMT not required on non-SMT systems\n" + "(type:%d policy:0x%llx, flags:0x0)", + type, SNP_POLICY_RSVD_MBO); + } else { + TEST_ASSERT(ret && errno == EINVAL, + "KVM_SEV_SNP_LAUNCH_START should fail, SNP_POLICY_SMT is not set on a SMT system\n" + "(type:%d policy:0x%llx, flags:0x0)", + type, SNP_POLICY_RSVD_MBO); + } + + ret = __test_snp_launch_start(type, SNP_POLICY | + SNP_FW_VER_MAJOR(UINT8_MAX) | + SNP_FW_VER_MINOR(UINT8_MAX), 0, false); + TEST_ASSERT(ret && errno == EIO, + "KVM_SEV_SNP_LAUNCH_START should fail, invalid version\n" + "expected:%d.%d got:%d.%d (type:%d policy:0x%llx, flags:0x0)", + SNP_FW_REQ_VER_MAJOR, SNP_FW_REQ_VER_MINOR, + UINT8_MAX, UINT8_MAX, type, + SNP_POLICY | SNP_FW_VER_MAJOR(UINT8_MAX) | SNP_FW_VER_MINOR(UINT8_MAX)); +} + +static void test_snp_launch_update(uint32_t type, uint64_t policy) +{ + struct kvm_vm *vm; + int ret; + + for (int pgtype = 0; pgtype <= KVM_SEV_SNP_PAGE_TYPE_CPUID + 1; pgtype++) { + vm = vm_create_type(type, 1); + snp_vm_launch_start(vm, policy); + ret = __snp_vm_launch_update(vm, pgtype); + + switch (pgtype) { + case KVM_SEV_SNP_PAGE_TYPE_NORMAL: + case KVM_SEV_SNP_PAGE_TYPE_ZERO: + case KVM_SEV_SNP_PAGE_TYPE_UNMEASURED: + case KVM_SEV_SNP_PAGE_TYPE_SECRETS: + TEST_ASSERT(!ret, + "KVM_SEV_SNP_LAUNCH_UPDATE should succeed, invalid Page type %d", + pgtype); + break; + case KVM_SEV_SNP_PAGE_TYPE_CPUID: + /* + * Expect failure if performed on random pages of + * guest memory rather than properly formatted CPUID Page + */ + TEST_ASSERT(ret && errno == EIO, + "KVM_SEV_SNP_LAUNCH_UPDATE should fail,\n" + "CPUID page type only valid for CPUID pages"); + break; + default: + TEST_ASSERT(ret && errno == EINVAL, + "KVM_SEV_SNP_LAUNCH_UPDATE should fail, invalid Page type"); + } + + kvm_vm_free(vm); + } +} + +void test_snp_launch_finish(uint32_t type, uint64_t policy) +{ + struct kvm_vm *vm; + int ret; + + vm = vm_create_type(type, 1); + snp_vm_launch_start(vm, policy); + snp_vm_launch_update(vm); + /* Flags must be zero for success */ + snp_vm_launch_finish(vm); + kvm_vm_free(vm); + + for (int i = 1; i < 16; i++) { + vm = vm_create_type(type, 1); + snp_vm_launch_start(vm, policy); + snp_vm_launch_update(vm); + ret = __snp_vm_launch_finish(vm, BIT(i)); + TEST_ASSERT(ret && errno == EINVAL, + "KVM_SEV_SNP_LAUNCH_FINISH should fail, invalid flag\n" + "(type:%d policy:0x%lx, flag:0x%lx)", + type, policy, BIT(i)); + kvm_vm_free(vm); + } +} + +static void test_snp_ioctl(void *guest_code, uint32_t type, uint64_t policy) +{ + test_snp_launch_start(type, policy); + test_snp_launch_update(type, policy); + test_snp_launch_finish(type, policy); +} + +static void test_sev_ioctl(void *guest_code, uint32_t type, uint64_t policy) +{ + test_sev_launch(guest_code, type, policy); +} + static void test_sev(void *guest_code, uint32_t type, uint64_t policy) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; struct ucall uc; - test_sev_launch(guest_code, type, policy); + if (type == KVM_X86_SNP_VM) + test_snp_ioctl(guest_code, type, policy); + else + test_sev_ioctl(guest_code, type, policy); vm = vm_sev_create_with_one_vcpu(type, guest_code, &vcpu); From patchwork Fri Aug 16 19:23:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pratik R. Sampat" X-Patchwork-Id: 13766799 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2071.outbound.protection.outlook.com [40.107.243.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDCA01C3F32; Fri, 16 Aug 2024 19:24:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.71 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836276; cv=fail; b=ZbxxpHLCUssHkx67pa2nj4GtunqhyT1Vh1Y4Uznq8UFWRSMzVhjvx37CBxb+iJqnHqa7lXsX1vm2E03vayrhRA512vLil1ZX/Itn4a8TN2mBVBkNyOYq6JJ7FvXzlgaXM2tFYyJGMEpieVW7B3R8xKVQbfe4pmDKXc30S86ob1g= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836276; c=relaxed/simple; bh=ey6nI2l3D4+9GfClCS9p2cOk+n8CYkiWZD8oJZUQru8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cGL9UAZkyrjOTK2hjopQ2+40bdlh9yEAclGoURhSn8UiuNSNdbnpQnWfigS77ZfIwWa4nyfqgbCeAKRwCEOpjPbVssWrisL00Y9/a8MzZ8kLGhxwxkB8duPcKz32wXlUjfjqqLqSOF++WyK+hM1D5y00KqKw38dMehdB2sXjWdg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=nXiU418R; arc=fail smtp.client-ip=40.107.243.71 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="nXiU418R" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=CZoJjlE2R3tX9s3DkF1b4BcwwnTtiN/wXGAMMavjTEq9E9eI82F9ASKr4MumJGIi/BZybEJXy5v578HIhOLK/Et4GWVXf3zDHpmss5AJ9CjssVKewJXjkWqAigV2neqTd8xG3qBgRFhH66TqBsNa+ifrLInXlBGZPhOL4W1fSS8gXWNaq4SF9oYk0gwpIWRn08nfCqYHo7DJA+ZE0Qjq1D2uD76vJRYsxZiv3/3d9Es0rOuobtNmc/jTvZbVcSVuj4hXReWeKiAuSbgtJSz8MXtJxgv4tNLYH4Q3lH/EPlWJEdg8p8cESn0/wPvzZeY5nVzXcSnv/eqogbyXd8Omgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=LwjcYrvsvWYVwD4AlWGt3YT9WCWEQ+3QVrWeNkt3rRo=; b=s0uf0ytAIoLqLhpiBhcvpdn1N1Bw8+L22MtdzHagmGJLUeqSe+Ys2fGsLMIw6TlK0PVHKM7PJqR8jzSjHh5sSCjDR3oPRu1/KdkT0RVIAoJxlWJL/lwEs+SPfiNvVPtJV7owhT8kfJcYRfTrWPrU/q0SHZzXGiaU8rP055lmuhZKneLbc9ShE8DrYBZ8uLypcJOsLpF4mSj5FCaxkUfaxPaNs/DryS9wHQtlEnYeI8GMV/ja0n7uJjsVT0cEfPOdB+meFC5RKvrs3tJUIRV6XVGZcxm4hzC/iI5gQ+q24lALYF54AIFjiUbBraYYxNV+ij/Sv+LeDL+b7c4Ea9rOOg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LwjcYrvsvWYVwD4AlWGt3YT9WCWEQ+3QVrWeNkt3rRo=; b=nXiU418Rk0gqf+AoJTK/Lio28BjHoDKZc/EavUYTC+b9iWxHIZNRg78lqVBm5i2ZETmV9YMlkelCyAn6+ngPOEf/t4TGYMOnP0RIsDJ86K/xooM0EDlFrWySVoUceWYkNnkoKv+cKDt9xqV4JDeEjZoVfMOyL6mq7qTiLu6iQVU= Received: from SJ0PR13CA0136.namprd13.prod.outlook.com (2603:10b6:a03:2c6::21) by IA0PR12MB7750.namprd12.prod.outlook.com (2603:10b6:208:431::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.20; Fri, 16 Aug 2024 19:24:28 +0000 Received: from SJ1PEPF00001CEA.namprd03.prod.outlook.com (2603:10b6:a03:2c6:cafe::61) by SJ0PR13CA0136.outlook.office365.com (2603:10b6:a03:2c6::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.9 via Frontend Transport; Fri, 16 Aug 2024 19:24:27 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CEA.mail.protection.outlook.com (10.167.242.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7828.19 via Frontend Transport; Fri, 16 Aug 2024 19:24:27 +0000 Received: from AUSPRSAMPAT.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 16 Aug 2024 14:24:25 -0500 From: "Pratik R. Sampat" To: CC: , , , , , , , Subject: [PATCH v2 6/9] KVM: selftests: SEV-SNP test for KVM_SEV_INIT2 Date: Fri, 16 Aug 2024 14:23:07 -0500 Message-ID: <20240816192310.117456-7-pratikrajesh.sampat@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> References: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CEA:EE_|IA0PR12MB7750:EE_ X-MS-Office365-Filtering-Correlation-Id: 163ada2e-b03e-40e9-e198-08dcbe290b53 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|82310400026|376014; X-Microsoft-Antispam-Message-Info: MGbhRBcxh1ZsjU6tnCkeuo2qKK5Zt2htPF2jcO/jqwF+G1dovveH5GsmnKxUobvvk32CeQnePFaBFDXYIdWvfh7fmrGy2XOhO12YtKU/vSyFkhVrulaT5nPUSIOqiCZaUM62T7DC7/qWH4JQ8/qDNlLwd/icaOANMC6WL1QxyogvNi6p6i3OnQOx3182Ug4u5leMT9HUfi5FrAS1lDjbY0TdpySYAyLGywnw1/aHEUjexSB7WnGW7AAi4LFRRAU33CjiQftJwM13nnE+pZMtFx7Nrg/UtS71+caVlBOQkLzgC38b8ZL6aGB9vnzvKIhqjShi9T3b0Z7LFa7pE/bTAMUPLVddK+LQFwZXk7TYkFfDjftYjONNNzqY1J3il250x3iG9NOInvgtPMQtngKSHsMrg7CkqVTbMdscBoKUrHwv+BjkRrBzV4t9DU4lxXbG7r812+J33CHsJqvOo9DYLkSV7S8XdFpCk0VqQFEzyMFd2chnXmUa9BfJreaJREopC5ufRBE8uSKUdAqOY2bDj07b10m8a7gy0Xq1b+eTa8rbPtqroRMIMcuypBRuiFgd3fNEs1+c5llDPDlVeEptxaK0R9j7CSL2YLgHMh/KVUio2OfXHsoNG5qdHPP9wNBOdK2cpXJUuB5JyND2C+90tqW7SS4RiQW0sQgo+JJuhllfXcCDkqsyiH20Y7p1Al4x4Y52zoLeEqGHAsB88G+PZqXKN3L/US5GPfDl52RzE9EsWoR6kOYDmmrXMJte8uh4VsODbTSYOcqSUjU+ebP/idp+WVkAfHJYuQsCo1dhAIYblJcSN3kjrfNDwLDN1sVIGIDv3y7+rjOLNxSE6/IqkCGvrKF1qUSyUqoqv9h2uacpoOXi9ezAWZs5Xl9LuD//9i+KKQb3k5EqCPMH+NeFYk8RJeSDUi8N4HfpmILL5PVKCgEJpFqy83r7jUmtMqL6zgG4XG1+b+HIC704L5PqAMf4AtlSgqlCXtTAZwCBfoGXnAlpnNIg29B4zPOjhpbWw7yA/bnIJhioAq7Chu8/+MrB3kuWKsSpEXS/O+ijxvo1cbwesyuE7Q/oEz6LnrQLaovMJk6RH8qACq2UtkS/p14xIwARw8rQ2my4wkRw/3O84eFkL2wc5IQ64IXZry67gafP+LCKeBiPLd7P1XWTml/Q2uo+vtUx6GtiGxK7CUSrXYRcvqau4caw7aTSkAQ4ovmDNQ6z/4lpYvv8SI3YbCwo/vdIgqASLogoixjq54d71cGYbBicDYcLUD1gjAVYnQXsEiS0GsCjd+9iki/6FXjgoFXqZHHgyf6SWn3XQzs0CUSVgrW8DnSl/snmu7pixNbF4v7C9XYDXV7SXI5EplVbqldSY/ZwQdztWON9IvO5bXV5M6u1zX+F4HhS6tOFOlYKVjMwUStORxRtuB2ln3OamfAsC2Hj9Aw+e6Tfty8CIWajD8XUOD7NgHHbaanK X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700013)(1800799024)(82310400026)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Aug 2024 19:24:27.1344 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 163ada2e-b03e-40e9-e198-08dcbe290b53 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CEA.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7750 Add SEV-SNP VM type to exercise the KVM_SEV_INIT2 call. Also ensure that SNP case is skipped for scenarios where CPUID supports it but KVM does not so that a failure is not reported for such cases. Signed-off-by: Pratik R. Sampat --- .../testing/selftests/kvm/x86_64/sev_init2_tests.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c b/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c index 3fb967f40c6a..3f8fb2cc3431 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c +++ b/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c @@ -28,6 +28,7 @@ int kvm_fd; u64 supported_vmsa_features; bool have_sev_es; +bool have_snp; static int __sev_ioctl(int vm_fd, int cmd_id, void *data) { @@ -83,6 +84,9 @@ void test_vm_types(void) if (have_sev_es) test_init2(KVM_X86_SEV_ES_VM, &(struct kvm_sev_init){}); + if (have_snp) + test_init2(KVM_X86_SNP_VM, &(struct kvm_sev_init){}); + test_init2_invalid(0, &(struct kvm_sev_init){}, "VM type is KVM_X86_DEFAULT_VM"); if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)) @@ -138,15 +142,24 @@ int main(int argc, char *argv[]) "sev-es: KVM_CAP_VM_TYPES (%x) does not match cpuid (checking %x)", kvm_check_cap(KVM_CAP_VM_TYPES), 1 << KVM_X86_SEV_ES_VM); + have_snp = kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SNP_VM); + TEST_ASSERT(!have_snp || kvm_cpu_has(X86_FEATURE_SNP), + "sev-snp: KVM_CAP_VM_TYPES (%x) indicates SNP support (bit %d), but CPUID does not", + kvm_check_cap(KVM_CAP_VM_TYPES), KVM_X86_SNP_VM); + test_vm_types(); test_flags(KVM_X86_SEV_VM); if (have_sev_es) test_flags(KVM_X86_SEV_ES_VM); + if (have_snp) + test_flags(KVM_X86_SNP_VM); test_features(KVM_X86_SEV_VM, 0); if (have_sev_es) test_features(KVM_X86_SEV_ES_VM, supported_vmsa_features); + if (have_snp) + test_features(KVM_X86_SNP_VM, supported_vmsa_features); return 0; } From patchwork Fri Aug 16 19:23:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pratik R. Sampat" X-Patchwork-Id: 13766800 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2059.outbound.protection.outlook.com [40.107.244.59]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 302D31C3F21; Fri, 16 Aug 2024 19:24:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.59 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836283; cv=fail; b=jXx4aLh+gwMpw4Ss0EgqcRGfNRjLIIiMe8Onpr0Xmg5HZcgxjs/jpjec8Nm1BFRJxtB/uOHRqw4nWHD5jL0+I+S6QFT8SfEqhl8BHFOD/2QZhEX0kHeq6efFANc5Sj7dHAajhsyS+OrPUZQ3uvcFsvDwAfmmHpkoqutvGpU+kts= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836283; c=relaxed/simple; bh=VcJA07A4HZw6N56PINsN8bO6ADh5MLHwR4cYxitCTGE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ZfV7rF8eo6fBCGQh9DEy0KdVe2UeoaJKAvgmrhfg2/2QPyn9iQ00WCFwgJQts7d7A2rcwWlZmqWN2l1TJXQ5QRqpptkPMzk41NEUN7PIORR5v+xV2/0gm4P4F+hQHgrTCmkp8fT/J98sJ+jBBXK7MbNt1II/o2GtVGycVIrKaKU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=rLpeHxO8; arc=fail smtp.client-ip=40.107.244.59 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="rLpeHxO8" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=i5RnLwydsDiGEt7Im599vHgWAF9dyMFX5Wh30ncJRui/sUFi+/01JxtZ7vB8hLGiuRKdVdNoJ82kW4TbcAxSHF5kKh4F4fLAYJj21bHXXbv1XzkN1dlr5W1Ow/u1m8UqOfBlFN2pKqfEyPc119RBXbdPJMGZDFQyD80UCTPBk1x4s8VNwDMv61Qfc7bfF+0EmM1h+rQGcJvAg6PBW1dWg5ePbPNnmGY7mBsPDW1QRyHONQCsIKi4k3l0sgVR9ZwtliHzYNlfkj9Kzy0QSWqxyadKSK5dwO6IYReT/An8/9lSbvdwgsycwV28AkfGey1ou0NE5kQJE6psanGYWZx+yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=iVKXtwbxNMh0WSbHHwaj9KxTRjbV6c3LQ/UMYsh5wmQ=; b=L1U956ItSznwHsfgifDiGm4MsXDKUCVVSMaLmVs/BViJTauFM6ViTsgUqclsVfM3az01uGNjFScOhoHahpP6RjrUXWoZ9Y+FRgxMcnjjtK4e6n9tfwIoLmruRRLzgPijeLZ8oNzHjSoURLs1ZV7bi5QdEZsG6Ilo+e8g1AhpaBIi+jremXJMMDWWVm/64ZocPlnzSaIUBPc9Zyec5g39xmIFqax71nSkr42VgEkzNPdk6hZdRsccf74srBsP6OTwtJV86Gy8bNGLFODTmU87JAv4iR0BpOjZICdKZiDrtcQjw0+xrn5lDH+VeEd3q+4KsWeSF3/dvHm8vXgnPcPxig== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=iVKXtwbxNMh0WSbHHwaj9KxTRjbV6c3LQ/UMYsh5wmQ=; b=rLpeHxO8PQsyoGMXAEmV3BwzaIGpnPe/zmqVBXD71uKRPE1I9blkKyBGTmiUBXHNdFdxBGh20b9naHljesQEyJgsMpYP0+QL9mNZOgHbpi4ZnqEOufPdBjHtz0/uINuVMl0MiFnARZH6z3+iGA/dW25Qgi7Xr/eExJaTE2n0dh0= Received: from SJ0PR13CA0125.namprd13.prod.outlook.com (2603:10b6:a03:2c6::10) by SA1PR12MB7200.namprd12.prod.outlook.com (2603:10b6:806:2bb::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.19; Fri, 16 Aug 2024 19:24:38 +0000 Received: from SJ1PEPF00001CE5.namprd03.prod.outlook.com (2603:10b6:a03:2c6:cafe::74) by SJ0PR13CA0125.outlook.office365.com (2603:10b6:a03:2c6::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Fri, 16 Aug 2024 19:24:37 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CE5.mail.protection.outlook.com (10.167.242.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7828.19 via Frontend Transport; Fri, 16 Aug 2024 19:24:37 +0000 Received: from AUSPRSAMPAT.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 16 Aug 2024 14:24:36 -0500 From: "Pratik R. Sampat" To: CC: , , , , , , , Subject: [PATCH v2 7/9] KVM: selftests: Add interface to manually flag protected/encrypted ranges Date: Fri, 16 Aug 2024 14:23:08 -0500 Message-ID: <20240816192310.117456-8-pratikrajesh.sampat@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> References: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CE5:EE_|SA1PR12MB7200:EE_ X-MS-Office365-Filtering-Correlation-Id: 4d0a53bf-9e56-463f-ed58-08dcbe291185 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|376014|1800799024|36860700013; X-Microsoft-Antispam-Message-Info: REDx5gclNtELPd8nJB+2akWT3GDpgjxtf/eprlCyZy00iXlZ1YKUAFgB+yfDK4p+HvXlOUmMo+HbbBXoPuoAjCjgqtflh0XOQxeK3cE4OHnDdEFJS10tvVSTQindHhsXN2R46IJ5gEk2o0NY67JldJ4bB6daS05pEQBKOR3hdyeSTtE6HMWaO+/8rfw/h+QNcNAyzZQKkLKWlMopLbztn00Yg5StPiV+oyNjtGlUpOKN8O9RYFCsaBNL2zFcSt1p03JgsHa+i3WkAtIUJIuHQ00YRkCvbp5nSnYY/vOcwjG+N3HQbXdzMy+NJRWEzC7spWjQI2rKWZ7sCq1/Cr3fs+/0F9+oUC0gWmqSPw1G388opfkG6H+5BscS+Ih3tqVOOLB4qkRS0HKCZ0Ud3AExqZRvS0+u5BzO2Sp4pU69rcaCGGHfrKjzU0LTiJW/anrBLFwEW1bGVb0zHFBnqd31yihLKuh8Vnwiawxr0HUzMw5Zk0DQNTs6D5c+taT5DW0f9TwFXNZ8ppvkAgIlNDh80LfZnKgxSt93bWnzFUcQI1mTS30/JA14sQeeq+oZv7KVeUpJqqpiWroVvB86iPUprArhxLv9M4WMVJH2+3/ORajRVKzJ1v1A9uhvAe+iZuqIK84JhWaEkTxqlWY10iYtrSYRFJWmq1tre6N+oJpKalsI0kBFoOgzk4ESUjj9UEDVfNPA5mNDdnhkXhMAq4i50dKZakzxzVCJwWzG1lOog7jhhnflX2V2D03nmCF38UMKyUfz9XBs9FymI6RnL1eHxeyB0l1JJE2X6xzC2zlD/Iro9rzU4V6GUxLYxXEoQjuU02MEjlDWI7MW3XcNyeDOXAF5JcN5orR9nYu56P36zX3+G9l3Otb5hYAkP6tRP2fTfXG5khomVwXmNVv/Exe3KB1IOTymZbXC30FsRBFwrlEHxzwcU8hJ8o91LJUeDXFD9tb7oTIA9N+0dwvZRfqXM+y1QR+e3MwPrzj2vCa4a4DalQC60hW6d5RuJ5MwiGaLDr305zCq0CMuTBe3t1w+6P7RUyaPgZTXJMJbFzNyOBi1G+9lPsCfk7n3+SbI7/rs/eZCprWowb84rVNF+iJEl6FoI3U7TgAU70fJswgSM19m+htBInQux2lx8573IWJJ/PbD6hag97tLlItOKVsEevBNojB5W9MWO2Kdx2Q/f/AmoCFcca9qtojdjSS4hOOoleFOPhjULmA3EQW6IfM9wRfVTNHceVpGOUMtbX3KsYpQGUPCiOQPBQopYTfR6yO+p8l1gNeJSrezHispdpaabOklW/p2MSc8xKyZE8OcwiSzG7Ci/ksjkrIjJa9Wjivxp6CjWUkeW3KPtD1SJQqSpZ9tXImLvhRPhj4v9LDzgoLfzATpWLJ/WrCyO4krUpUfAK5LiUdTXRKCQoK5hdVpIvHKLYtmwmkThZEq28ZWF15+qURnb+62RsAVs8vrG40w X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(376014)(1800799024)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Aug 2024 19:24:37.5131 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4d0a53bf-9e56-463f-ed58-08dcbe291185 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CE5.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB7200 From: Michael Roth For SEV and SNP, currently __vm_phy_pages_alloc() handles setting the region->protected_phy_pages bitmap to mark that the region needs to be encrypted/measured into the initial guest state prior to finalizing/starting the guest. It also marks what GPAs need to be mapped as encrypted in the initial guest page table. This works when using virtual/physical allocators to manage memory, but if the test manages allocations/mapping directly then an alternative is needed to set region->protected_phy_pages directly. Add an interface to handle that. Signed-off-by: Michael Roth Signed-off-by: Pratik R. Sampat --- .../testing/selftests/kvm/include/kvm_util.h | 2 + tools/testing/selftests/kvm/lib/kvm_util.c | 45 +++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 144730efbffa..8017a75a5a61 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -394,6 +394,8 @@ static inline void vm_set_memory_attributes(struct kvm_vm *vm, uint64_t gpa, vm_ioctl(vm, KVM_SET_MEMORY_ATTRIBUTES, &attr); } +void vm_mem_set_protected(struct kvm_vm *vm, uint32_t memslot, + vm_paddr_t paddr, size_t num); static inline void vm_mem_set_private(struct kvm_vm *vm, uint64_t gpa, uint64_t size) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 9cc4dfc72329..fa8eb998f13c 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -2064,6 +2064,43 @@ const char *exit_reason_str(unsigned int exit_reason) return "Unknown"; } +/* + * Set what guest GFNs need to be encrypted prior to finalizing a CoCo VM. + * + * Input Args: + * vm - Virtual Machine + * memslot - Memory region to allocate page from + * paddr - Start of physical address to mark as encrypted + * num - number of pages + * + * Output Args: None + * + * Return: None + * + * Generally __vm_phy_pages_alloc() will handle this automatically, but + * for cases where the test handles managing the physical allocation and + * mapping directly this interface should be used to mark physical pages + * that are intended to be encrypted as part of the initial guest state. + * This will also affect whether virt_map()/virt_pg_map() will map the + * page as encrypted or not in the initial guest page table. + * + * If the initial guest state has already been finalized, then setting + * it as encrypted will essentially be a noop since nothing more can be + * encrypted into the initial guest state at that point. + */ +void vm_mem_set_protected(struct kvm_vm *vm, uint32_t memslot, + vm_paddr_t paddr, size_t num) +{ + struct userspace_mem_region *region; + sparsebit_idx_t pg, base; + + base = paddr >> vm->page_shift; + region = memslot2region(vm, memslot); + + for (pg = base; pg < base + num; ++pg) + sparsebit_set(region->protected_phy_pages, pg); +} + /* * Physical Contiguous Page Allocator * @@ -2121,11 +2158,11 @@ vm_paddr_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, abort(); } - for (pg = base; pg < base + num; ++pg) { + for (pg = base; pg < base + num; ++pg) sparsebit_clear(region->unused_phy_pages, pg); - if (protected) - sparsebit_set(region->protected_phy_pages, pg); - } + + if (protected) + vm_mem_set_protected(vm, memslot, base << vm->page_shift, num); return base * vm->page_size; } From patchwork Fri Aug 16 19:23:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pratik R. Sampat" X-Patchwork-Id: 13766801 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2083.outbound.protection.outlook.com [40.107.236.83]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DA9A1C3F21; Fri, 16 Aug 2024 19:24:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.83 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836298; cv=fail; b=aTKHO/XGVdG4MB69g1bIUK9kYNtdjvuTB2FRYqgovjrq6az0LkdLl6FoWsYiOFlW+sPoIfQeM9GtZRlmket7VWqp+4BfPddVDiHh5skBVzInbKEQczflf7KEDUz/9DoBhoXXiscv1j7/o9P3le+cmU+Z5HuT8YBEbYIUl+6eTLM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836298; c=relaxed/simple; bh=ti1A8m5XK1ndnnaLRd5Lnh8XHKMMPbhAtdjFlzbibUI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IiQKH8sEAlhg/8pZ53h81LHRooiuCtf15kVpu8VI1iK65hG7xiihv2R31ucgyjQvcKKhULhAfMEGz8gpfTk82VwpBrcFaNZiQKEQ19NbtFX+Oh1utzH7256PvCbKt8de2U9GeG4ykTij6jWh0G60zlrbF7kBGs/XjqkAll53ZIM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=yYBLprm7; arc=fail smtp.client-ip=40.107.236.83 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="yYBLprm7" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=efl+UXPlAdN1Yu3nY1bvsboYfUcP7/0TDLRg6lgpPN01Zo8tRqLoOjKdqW1YUle88NDS3sePOsNtafwGhVviSMXQLUHx4vEpkNKlsVH+0LN/Xya3Q2WrGXHJFPj+jnyWFQQcDzKJ2Qv6MQukZktY6jZuzgHfiimfmMi0oEWLf0BFca5FXxXuhrjD701Y3R6llZSB9U8I2cteCU8WBLWzfVtEs12w25nW11rejK0v/QtDEcIcxZw+eW2FQyfbrY6qTtJdBtwYiA6Av/+IdXKSlUB9HoLKPBhFp3TUej92rsTb8IRqe805Y4wkRm1VD4bclKnEgrBQ+NEELksoxgOe/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=HgoGBdqj8XR4W8IGhzWnhzzpWidDcZTs/2j9X8K9cKs=; b=Rdz6qUQ9qPNJD0AjpMTKozpc/Rk/5lBD5WvJnWfdL1j+CYVOz/UuQvg/vnU3euW5FJai7YiVkN5mQTmPFXahHZOjEkMbzNlMU8n7E8HYixi8z5n3VuLdJnuu0k6aS7hLt4pl5iZFggiPnUqT98NGCvoY3/oGPzJQKnQjTIxhEpJQAi6z1Rm59fy1yST4Q83eYuVitM3H354kya1KYpWsFmx6zyvcLFakXBBGf1UK48Qzi60jkcxNIWLxJKhtTFfX9Wug+U5Tob7vF9ogJnC4hiQaYgQj+iIiNm44NATDa5wxCAlGp6yOp2MB7sDup1kiCYf+Y0c2K97emtnuBk2i5Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HgoGBdqj8XR4W8IGhzWnhzzpWidDcZTs/2j9X8K9cKs=; b=yYBLprm79MZKnaw1Cod0lkKDjOvydAl/dvtdvH4yjjRAZTRjjAw8WsnigBcyMAveNryh9GNrXjKxji48Zb9tcuTmqT0692K3maKXEXwLrtk0qgaf1Uu7HVliTxDwIb2lEjUlhOjtwFdlKl38XA5SVZFdmhJna5y4pnMGMyj7dDs= Received: from SJ0PR05CA0163.namprd05.prod.outlook.com (2603:10b6:a03:339::18) by SN7PR12MB8004.namprd12.prod.outlook.com (2603:10b6:806:341::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7849.22; Fri, 16 Aug 2024 19:24:49 +0000 Received: from SJ1PEPF00001CE9.namprd03.prod.outlook.com (2603:10b6:a03:339:cafe::dd) by SJ0PR05CA0163.outlook.office365.com (2603:10b6:a03:339::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.16 via Frontend Transport; Fri, 16 Aug 2024 19:24:49 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CE9.mail.protection.outlook.com (10.167.242.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7828.19 via Frontend Transport; Fri, 16 Aug 2024 19:24:48 +0000 Received: from AUSPRSAMPAT.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 16 Aug 2024 14:24:47 -0500 From: "Pratik R. Sampat" To: CC: , , , , , , , Subject: [PATCH v2 8/9] KVM: selftests: Add a CoCo-specific test for KVM_PRE_FAULT_MEMORY Date: Fri, 16 Aug 2024 14:23:09 -0500 Message-ID: <20240816192310.117456-9-pratikrajesh.sampat@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> References: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CE9:EE_|SN7PR12MB8004:EE_ X-MS-Office365-Filtering-Correlation-Id: 7bc01973-af0d-4370-448f-08dcbe291859 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|82310400026|36860700013; X-Microsoft-Antispam-Message-Info: 5M/isi01rE1jtNTwyaxl6bBJ4ng19QUE7HoT3zj85oJE/zhzvGc5IWDrnVHip5MHz7/e8CUkzI3h3XtjGGppoLsuSxTtayaP0FiBmE6FaQheWeGy9t4uTciwc9XQK4NJyelh34B6A/GZDXeLI27JMxviBX7hFMLlkM89nluGsZ9QvLDFSt10IeG3poDFTajaM70hYiDeDb4RnmyUqAjvyGW7/6bn3gbzgYdIhLuiG+J2dhMHoPBISLBR4b2Th0LrMrEVwG4dlh5o6/n9JgMeAmfafaPfk59qQsaNtrQuJL+cY8pu6RX15AObS95Re0sQIjqXU6ktMNx7i/d9BWDfy5ZANmshsj6Ao2I3zoctB0lFsM4xyQXSNjlsoYTpnccUChK+mYXpQr4sPmJqhmIH5GwhRqwRUbhJ1tssKjykbQ0T34JsmuB08sV7DCnfTiR0WwpoG1z5jD2+bVOcoBKBUiacIGcj+FZcZkppAC3gX7pyTd5iJRTyJebeUo87vwUy9GUsfA4eCqtOd0xbA246698DbuLUJ5v6OnWxoHfcwQ2JW0cTWaB/Y7GhJBfk8Y0W1a7mROA/o6Xjtq661vejO413Ni47mgH9KuouZC4cWZYtloBzjV5/feVqbydGWzCsTKHu10NVCTpHQPEH/n77Yuk56LgpPJ5Dm94fTRRhDCKgyVt8Y19zrqembqAd9K/V/nATh6VYOd/8YJ5GqBmCnNn+qBh3WyhU1BmzrbeB9JX0mpLUfurq/xVthyp073NPCjvWs7I53oJQoj5PQ405hfxxNHKsHC3BbS5G6L0fvJXsj4qQRyFCedlFCU8Lz7KpNkalvlE5bRNuYRO+f+5gGTAbfHKGvLIXmsKhEwGoQdXuoVlT1MO6r8nmxJ9LHrW2U7rXCDpjA79LDprDp4a4wI6qiMAKKUnhMYHS4MCj0UCUdMuI6XOgtG3Mk6SwzX7w70PmaOqJ+elUf5R24ZbFgSPS+H/oikNPXdJ38/MNZAWAuUeh8opnIQ09xA7dm30xfkvAJdpYLwlxPXcnIjgJElW84FXW6OlmeyuzMUSCrCLmGWva7LI2XaUhU+k9jcgq4EY5BQ/kRvcoV0i63IDBvX/BV00YWhKY+Nt1q0AOUik0wnJWlOjbo0UCD+AJHjKPWbA4+NsW+cacWJqE10Kmqc8FGEF9OkuywF0dHILEZvPm4Ht1iChaWQ6MpMCR5kVIfhzX5Vb3B8f0yuiGSXprxDfr2SjXO32RoY4L7rpyPnhTWPYH4zUY0Hcs22Z5xSbh3fmewKINoJYOr0LWeUaIcReNGwZlacqbayrx8jKNBOnAZL25M+c7Wb7huhPVVTDLKDjp9i6WvuvrgbKHc9qmzaWJ9Z6bnXxq45/+UCkebwZc/n+WYDphvUblHpsQyD4ywWEUJ4w/2sQ5OYXL1Vj/myVs696k/U/SbCqUs6x8u5p02KFBi+dtzJWOwTzHeqtO X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(376014)(82310400026)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Aug 2024 19:24:48.9645 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7bc01973-af0d-4370-448f-08dcbe291859 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CE9.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB8004 From: Michael Roth SEV, SEV-ES, and SNP have a few corner cases where there is potential for KVM_PRE_FAULT_MEMORY to behave differently depending on when it is issued during initial guest setup. Exercising these various paths requires a bit more fine-grained control over when the KVM_PRE_FAULT_MEMORY requests are issued while setting up the guests. Since these CoCo-specific events are likely to be architecture-specific KST helpers, take the existing generic test in pre_fault_memory_test.c as a starting template, and then introduce an x86-specific version of it with expanded coverage for SEV, SEV-ES, and SNP. Since there's a reasonable chance that TDX could extend this for similar testing of TDX, give it a "coco-" prefix rather than an SEV-specific one. Signed-off-by: Michael Roth Co-developed-by: Pratik R. Sampat Signed-off-by: Pratik R. Sampat --- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/coco_pre_fault_memory_test.c | 314 ++++++++++++++++++ 2 files changed, 315 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/coco_pre_fault_memory_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 48d32c5aa3eb..65d19b277b06 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -129,6 +129,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/amx_test TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test TEST_GEN_PROGS_x86_64 += x86_64/triple_fault_event_test TEST_GEN_PROGS_x86_64 += x86_64/recalc_apic_map_test +TEST_GEN_PROGS_x86_64 += x86_64/coco_pre_fault_memory_test TEST_GEN_PROGS_x86_64 += access_tracking_perf_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test diff --git a/tools/testing/selftests/kvm/x86_64/coco_pre_fault_memory_test.c b/tools/testing/selftests/kvm/x86_64/coco_pre_fault_memory_test.c new file mode 100644 index 000000000000..e16fe185fb5a --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/coco_pre_fault_memory_test.c @@ -0,0 +1,314 @@ +// SPDX-License-Identifier: GPL-2.0 +#include + +#include +#include +#include +#include "sev.h" + +/* Arbitrarily chosen values */ +#define TEST_SIZE (SZ_2M + PAGE_SIZE) +#define TEST_NPAGES (TEST_SIZE / PAGE_SIZE) +#define TEST_SLOT 10 +#define TEST_GPA 0x100000000ul +#define TEST_GVA 0x100000000ul + +enum prefault_snp_test_type { + /* Skip pre-faulting tests. */ + NO_PREFAULT_TYPE = 0, + /* + * Issue KVM_PRE_FAULT_MEMORY for GFNs mapping non-private memory + * before finalizing the initial guest contents (e.g. via + * KVM_SEV_SNP_LAUNCH_FINISH for SNP guests). + * + * This should result in failure since KVM explicitly disallows + * KVM_PRE_FAULT_MEMORY from being issued prior to finalizing the + * initial guest contents. + */ + PREFAULT_SHARED_BEFORE_FINALIZING = 0, + /* + * Issue KVM_PRE_FAULT_MEMORY for GFNs mapping private memory + * before finalizing the initial guest contents (e.g. via + * KVM_SEV_SNP_LAUNCH_FINISH for SNP guests). + * + * This should result in failure since KVM explicitly disallows + * KVM_PRE_FAULT_MEMORY from being issued prior to finalizing the + * initial guest contents. + */ + PREFAULT_PRIVATE_BEFORE_FINALIZING, + /* + * Issue KVM_PRE_FAULT_MEMORY for GFNs mapping shared/private + * memory after finalizing the initial guest contents + * (e.g. via * KVM_SEV_SNP_LAUNCH_FINISH for SNP guests). + * + * This should succeed since pre-faulting is supported for both + * non-private/private memory once the guest contents are finalized. + */ + PREFAULT_PRIVATE_SHARED_AFTER_FINALIZING +}; + +static void guest_code_sev(void) +{ + int i; + + GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ENABLED); + + for (i = 0; i < TEST_NPAGES; i++) { + uint64_t *src = (uint64_t *)(TEST_GVA + i * PAGE_SIZE); + uint64_t val = *src; + + /* Validate the data stored in the pages */ + if ((i < TEST_NPAGES / 2 && val != i + 1) || + (i >= TEST_NPAGES / 2 && val != 0)) { + GUEST_FAIL("Inconsistent view of memory values in guest"); + } + } + + if (rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ES_ENABLED) { + wrmsr(MSR_AMD64_SEV_ES_GHCB, GHCB_MSR_TERM_REQ); + __asm__ __volatile__("rep; vmmcall"); + GUEST_FAIL("This should be unreachable."); + } + + GUEST_DONE(); +} + +static void __pre_fault_memory(struct kvm_vcpu *vcpu, u64 gpa, u64 size, + u64 left, bool expect_fail) +{ + struct kvm_pre_fault_memory range = { + .gpa = gpa, + .size = size, + .flags = 0, + }; + int ret, save_errno; + u64 prev; + + do { + prev = range.size; + ret = __vcpu_ioctl(vcpu, KVM_PRE_FAULT_MEMORY, &range); + save_errno = errno; + TEST_ASSERT((range.size < prev) ^ (ret < 0), + "%sexpecting range.size to change on %s", + ret < 0 ? "not " : "", + ret < 0 ? "failure" : "success"); + } while (ret >= 0 ? range.size : save_errno == EINTR); + + TEST_ASSERT(expect_fail ? !(range.size == left) : (range.size == left), + "[EXPECT %s] completed with %lld bytes left, expected %" PRId64, + expect_fail ? "FAIL" : "PASS", + range.size, left); + + if (left == 0) { + TEST_ASSERT(expect_fail ? ret : !ret, + "[EXPECT %s] KVM_PRE_FAULT_MEMORY", + expect_fail ? "FAIL" : "PASS"); + } else { + /* + * For shared memory, no memory slot causes RET_PF_EMULATE. It + * results in -ENOENT. + * + * For private memory, no memory slot is an error case returning + * -EFAULT, but it also possible the only the GPA ranges backed + * by a slot are marked as private, in which case the noslot + * range will also result in -ENOENT. + * + * So allow both errors for now, but in the future it would be + * good to distinguish between these cases to tighten up the + * error-checking. + */ + TEST_ASSERT(expect_fail ? !ret : + (ret && (save_errno == EFAULT || save_errno == ENOENT)), + "[EXPECT %s] KVM_PRE_FAULT_MEMORY", + expect_fail ? "FAIL" : "PASS"); + } +} + +static void pre_fault_memory(struct kvm_vcpu *vcpu, u64 gpa, + u64 size, u64 left) +{ + __pre_fault_memory(vcpu, gpa, size, left, false); +} + +static void pre_fault_memory_negative(struct kvm_vcpu *vcpu, u64 gpa, + u64 size, u64 left) +{ + __pre_fault_memory(vcpu, gpa, size, left, true); +} + +static void pre_fault_memory_snp(struct kvm_vcpu *vcpu, struct kvm_vm *vm, + bool private, enum prefault_snp_test_type p_type) +{ + if (p_type == PREFAULT_SHARED_BEFORE_FINALIZING) + pre_fault_memory_negative(vcpu, TEST_GPA, SZ_2M, 0); + + snp_vm_launch_start(vm, SNP_POLICY); + + if (p_type == PREFAULT_SHARED_BEFORE_FINALIZING) + pre_fault_memory_negative(vcpu, TEST_GPA, SZ_2M, 0); + + if (private) { + /* + * Make sure when pages are pre-faulted later after + * finalization they are treated the same as a private + * access by the guest so that the expected gmem + * backing pages are used. + */ + vm_mem_set_private(vm, TEST_GPA, TEST_SIZE); + if (p_type == PREFAULT_PRIVATE_BEFORE_FINALIZING) + pre_fault_memory_negative(vcpu, TEST_GPA, SZ_2M, 0); + } else { + if (p_type == PREFAULT_SHARED_BEFORE_FINALIZING) + pre_fault_memory_negative(vcpu, TEST_GPA, SZ_2M, 0); + } + + snp_vm_launch_update(vm); + + if (p_type == PREFAULT_SHARED_BEFORE_FINALIZING) + pre_fault_memory_negative(vcpu, TEST_GPA, SZ_2M, 0); + + snp_vm_launch_finish(vm); + + /* + * After finalization, pre-faulting either private or shared + * ranges should work regardless of whether the pages were + * encrypted as part of setting up initial guest state. + */ + if (p_type == PREFAULT_PRIVATE_SHARED_AFTER_FINALIZING) { + pre_fault_memory(vcpu, TEST_GPA, SZ_2M, 0); + pre_fault_memory(vcpu, TEST_GPA + SZ_2M, PAGE_SIZE * 2, PAGE_SIZE); + pre_fault_memory(vcpu, TEST_GPA + TEST_SIZE, PAGE_SIZE, PAGE_SIZE); + } +} + +static void pre_fault_memory_sev(unsigned long vm_type, struct kvm_vcpu *vcpu, + struct kvm_vm *vm) +{ + uint32_t policy = (vm_type == KVM_X86_SEV_ES_VM) ? SEV_POLICY_ES : 0; + + pre_fault_memory(vcpu, TEST_GPA, SZ_2M, 0); + pre_fault_memory(vcpu, TEST_GPA + SZ_2M, PAGE_SIZE * 2, PAGE_SIZE); + pre_fault_memory(vcpu, TEST_GPA + TEST_SIZE, PAGE_SIZE, PAGE_SIZE); + + sev_vm_launch(vm, policy); + + pre_fault_memory(vcpu, TEST_GPA, SZ_2M, 0); + pre_fault_memory(vcpu, TEST_GPA + SZ_2M, PAGE_SIZE * 2, PAGE_SIZE); + pre_fault_memory(vcpu, TEST_GPA + TEST_SIZE, PAGE_SIZE, PAGE_SIZE); + + sev_vm_launch_measure(vm, alloca(256)); + + pre_fault_memory(vcpu, TEST_GPA, SZ_2M, 0); + pre_fault_memory(vcpu, TEST_GPA + SZ_2M, PAGE_SIZE * 2, PAGE_SIZE); + pre_fault_memory(vcpu, TEST_GPA + TEST_SIZE, PAGE_SIZE, PAGE_SIZE); + + sev_vm_launch_finish(vm); + + pre_fault_memory(vcpu, TEST_GPA, SZ_2M, 0); + pre_fault_memory(vcpu, TEST_GPA + SZ_2M, PAGE_SIZE * 2, PAGE_SIZE); + pre_fault_memory(vcpu, TEST_GPA + TEST_SIZE, PAGE_SIZE, PAGE_SIZE); +} + +static void test_pre_fault_memory_sev(unsigned long vm_type, bool private, + enum prefault_snp_test_type p_type) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + int i; + + vm = vm_sev_create_with_one_vcpu(vm_type, guest_code_sev, &vcpu); + + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, + TEST_GPA, TEST_SLOT, TEST_NPAGES, + (vm_type == KVM_X86_SNP_VM) ? KVM_MEM_GUEST_MEMFD : 0); + + /* + * Make sure guest page table is in agreement with what pages will be + * initially encrypted by the ASP. + */ + if (private) + vm_mem_set_protected(vm, TEST_SLOT, TEST_GPA, TEST_NPAGES); + + virt_map(vm, TEST_GVA, TEST_GPA, TEST_NPAGES); + + /* + * Populate the pages to compare data read from the guest + * Populate the first half with data and second half as all zeros. + */ + for (i = 0; i < TEST_NPAGES; i++) { + uint64_t *hva = addr_gva2hva(vm, TEST_GVA + i * PAGE_SIZE); + + if (i < TEST_NPAGES / 2) + *hva = i + 1; + else + *hva = 0; + } + + if (vm_type == KVM_X86_SNP_VM) + pre_fault_memory_snp(vcpu, vm, private, p_type); + else + pre_fault_memory_sev(vm_type, vcpu, vm); + + vcpu_run(vcpu); + + if (vm->type == KVM_X86_SEV_ES_VM || vm->type == KVM_X86_SNP_VM) { + TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SYSTEM_EVENT, + "Wanted SYSTEM_EVENT, got %s", + exit_reason_str(vcpu->run->exit_reason)); + TEST_ASSERT_EQ(vcpu->run->system_event.type, KVM_SYSTEM_EVENT_SEV_TERM); + TEST_ASSERT_EQ(vcpu->run->system_event.ndata, 1); + TEST_ASSERT_EQ(vcpu->run->system_event.data[0], GHCB_MSR_TERM_REQ); + goto out; + } + + switch (get_ucall(vcpu, &uc)) { + case UCALL_DONE: + break; + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + default: + TEST_FAIL("Unexpected exit: %s", + exit_reason_str(vcpu->run->exit_reason)); + } + +out: + kvm_vm_free(vm); +} + +static void test_pre_fault_memory(unsigned long vm_type, bool private) +{ + int pt; + + if (vm_type && !(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type))) { + pr_info("Skipping tests for vm_type 0x%lx\n", vm_type); + return; + } + + switch (vm_type) { + case KVM_X86_SEV_VM: + case KVM_X86_SEV_ES_VM: + test_pre_fault_memory_sev(vm_type, private, NO_PREFAULT_TYPE); + break; + case KVM_X86_SNP_VM: + for (pt = 0; pt <= PREFAULT_PRIVATE_SHARED_AFTER_FINALIZING; pt++) + test_pre_fault_memory_sev(vm_type, private, pt); + break; + default: + abort(); + } +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(kvm_check_cap(KVM_CAP_PRE_FAULT_MEMORY)); + + test_pre_fault_memory(KVM_X86_SEV_VM, false); + test_pre_fault_memory(KVM_X86_SEV_VM, true); + test_pre_fault_memory(KVM_X86_SEV_ES_VM, false); + test_pre_fault_memory(KVM_X86_SEV_ES_VM, true); + test_pre_fault_memory(KVM_X86_SNP_VM, false); + test_pre_fault_memory(KVM_X86_SNP_VM, true); + + return 0; +} From patchwork Fri Aug 16 19:23:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pratik R. Sampat" X-Patchwork-Id: 13766802 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2044.outbound.protection.outlook.com [40.107.220.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFB251C57B6; Fri, 16 Aug 2024 19:25:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.44 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836305; cv=fail; b=S1ya3dYOU8ICJ58RL0sXJejq0K0IVZCr+EhtGQg9fKyVy65o8hUK99hpe4cflQwjQnQlYqXLEaUhBTrE7zMrISYQEAdPYuZBjqW4RhGHpvb2z3dzeNxnA4fKnJGc7yi5i8Cj4JLiEphGEvv6me5bRQw6qfjtybPiwTm736RpoyI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723836305; c=relaxed/simple; bh=IrSQwlkoFJxFwTfiEycarB9pqWv6fk3NHxoTLlcpF0U=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DgzCF7Qep/INn9JkSM22KMm3KeOoy6mpmnm9KiuGUMlZwOGqwd5pIPQZvNAFz8A6KF99h1pQ6nXgFkYBFvitEjCzh66+DNg6oa0qtzjh4UD+/L3+hR74aoH6kteBjEL3eYOpSe1/Eq+gDkP0gVhGK75/j49U8JN0oOLkqvxgQbE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=cGlsRLnL; arc=fail smtp.client-ip=40.107.220.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="cGlsRLnL" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=j1VmvrZf+L5WBrSBZjX57xZv0z7Va4W1ib4Js/9yuMxlg7BAw7Q+daoirw1JjusCwHoZRk9VxuFj9YMDS2DxwOXND0AFfoIpgwufz5H+7zkNC/kldH4VX6Io6G+5NEwOLeLc9KveIE35EsFgdBMFg6SEzbICh0kv6EaI/wLhQ+6l13QOxjtUQqjQHaaK6LexvZoxETrDFRxn8WpD+dl2sLSZubnYrj7/zjMaNY5kp71lIQeJh05i+95LVA38evg4xow4qQUkz+Zvl2HIKECBYGWBuB5kxP1hX354IqcIn+33hT1uPUH7LMxv7Jd0Pmb5/JVO62BZvC0cYcbnSzCXhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Sm8ioKdDNp+5Kz3rLUSSVW9umY7+7/6wi45XMxUlDIc=; b=QKTYX7cX3vB0lu5jiKZWUwxhHzOsvLHzwg9gTkbJBrTvxw21Y+3vJAxFTb0gXHvYk0YKWd1Kx4eJwpsDjytbl5NTT+0t9ABfmLCbzdo4CERwX+P44gPn3g1RDNxL0423W2N5jExYAEi00T4qQd0QrTXb/yV1idg4u6fWjncVm6ewBiPkRR1CrK1AHC9d3nTdniLuqHT4oVA6LwIkkP2MLs+jVq3uyIy2UWArtSTqRGPifVCqGo5dYWtUZ9TSkdkZcVrw0HJN/evDAuljcj3deKjVYaE6VrFOAeIRfH2N4HKfHbVJUiW2i/p7sk3Wi6DL7sglReconQOYCLbQpFOOCQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Sm8ioKdDNp+5Kz3rLUSSVW9umY7+7/6wi45XMxUlDIc=; b=cGlsRLnLfDFat5tf6s7W7JpNTIMKecvJ7I3OCUUHOIeUwfim4dgfhpES4x9UQSN6KI85Dkpqj5lWY/bOtND+g69lpg+cxSq5JBVmRNfXStIjvPEMb/Ej0dJEIRQiWrT5c46EQ1hHVv5Lknmi4nihudLSUueL60eAiDsQtSxDrgE= Received: from SJ0PR13CA0161.namprd13.prod.outlook.com (2603:10b6:a03:2c7::16) by SA0PR12MB4446.namprd12.prod.outlook.com (2603:10b6:806:71::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.20; Fri, 16 Aug 2024 19:25:00 +0000 Received: from SJ1PEPF00001CE8.namprd03.prod.outlook.com (2603:10b6:a03:2c7:cafe::51) by SJ0PR13CA0161.outlook.office365.com (2603:10b6:a03:2c7::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Fri, 16 Aug 2024 19:25:00 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CE8.mail.protection.outlook.com (10.167.242.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7828.19 via Frontend Transport; Fri, 16 Aug 2024 19:24:59 +0000 Received: from AUSPRSAMPAT.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 16 Aug 2024 14:24:58 -0500 From: "Pratik R. Sampat" To: CC: , , , , , , , Subject: [PATCH v2 9/9] KVM: selftests: Interleave fallocate for KVM_PRE_FAULT_MEMORY Date: Fri, 16 Aug 2024 14:23:10 -0500 Message-ID: <20240816192310.117456-10-pratikrajesh.sampat@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> References: <20240816192310.117456-1-pratikrajesh.sampat@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CE8:EE_|SA0PR12MB4446:EE_ X-MS-Office365-Filtering-Correlation-Id: 99b8f14e-a9b9-431c-b4c0-08dcbe291ed2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|36860700013|376014|82310400026; X-Microsoft-Antispam-Message-Info: hGHpITF4gcc1o/Tn3PdsAyDPHyzZWN4xA9IDdciWQ/HYHY4D+rPElNVIf+mILG0GW/EsfRhf6sxfPIfhj962Fc9yxH36KHUCJ+WbaHYxMS2U49SzJZea7umEHWEb9oOgfYff99Lpzn8CCg6RLl2Pw5hcdTKJIzBdO/ie1RIopoRvmm7aREmphNJkANhwQLgDNHs3FzFpQfaan+9NBhvoARXz3c/s93QmLwwnCtNwVQuWBtfvT1XuD8Q5d+1uLBynwCD6MmTpXJEGQLK4BEOH8ActerW+4e8bVswfZxNCkoH68Rdly5ce+li21oLUQtBCv6FpwIL8k9ZGJwp25mSIoAwYmXemPjrdNCcCOJ4iRMlJKn2cwVO+Y7TmRPXDxMh+9Q+7vlMYXJFJ9gAyHcXr11QGcpVFfysv14/t+s8RJmQMD0TrSvekw9qgb0WGltfGKrB1/sp8c9Ova/hKMoaAb6U4vKhHPunaJQI8Pbv1gq8v/xYWly9L7ZpCRt9xQyikZJFiqV82/VdJoMJFjboPkB8oCCs5ZC0J6ZqubqEV9BsHf/TWuCZStiw5JaLEVsPhESg0H9GrFwhanaLM1ZD2hlMstpvZbmHozNZnytsJ0EVfMBG7HGcJxxQJaDN51X4Wxcjht3B0yLv0nbHTunDiNUOab3DUXnRQngXDpA9Hzbvaz6GXrDomhr3HNdulE1sK/A4HHhcd8I5LXB7wXu+qpgMrtI/sNDbUUjjT8ubnUZYAXxo7OBd+Y4Dng05c6K8sYGF4ZRkx8tlw0IW58qS72+kl5cJxNrOpEc2YmrWtIw6fvKZ3yki6EIBeoadNmOV++AxFZsjF1RXLgyzN97OCqVMaudvh9xUN4yrLChtNoDmcFZaGv+YWQOnbDFWLLvoc1QaFW+AR6lqY4RtsVi+kW1jlsnpGcOTN4X/YAvcZQrwXa2n5H7pxsVmETpccO1aqHFwd18T+JqvBxMjL1Mj4Sa5VRh1kve4QXjNkdnvRGFsk+kEUKf9NM1yor5Z9He7qB8dJWkKKbrb1fhp2zgW+LQGf06orGzjkRutK9v2dIOX15slRK/KJeAS+yM5Y79deP8qFiK0WaJWF0eDUrdYZirdObDMILluxQiprt+g0NDjvvggm6vg4mUQYWikLFpAIR+TfMgSTLsH200zBbv2XYd9GpByoD45bQZh1BgV9B+22iW/v0+roV9xEtWkcH+4KAi7gB6sBsTQyulUdZmoGmhRkkxDb1tGxhVUrAg9gHl6cJw8hXMdZYHK1NkuADugLWAfroi3gJ/5lmxlOMchQFvpA0iQ9JWoIo7HDtPcUWTpPPgVEQ0U0C7jZ42l9XrcJxjX42rLQV5E0EPyd8B/QrPIOQ2qU8TKUf2cfqZYMKU/skvalYvp9ibjd4dq3zD6P5IOo1/OJms8Waofq8Sm4c+taNkLFkipSf49uXWxX4wYGIf6ODv+nTjrrmrrHOGYk X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(36860700013)(376014)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Aug 2024 19:24:59.8267 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 99b8f14e-a9b9-431c-b4c0-08dcbe291ed2 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CE8.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4446 fallocate triggers gmem_prepare(), and KVM_PRE_FAULT_MEMORY can cause guest page faults at unexpected points. Therefore, introduce several test cases to interleave fallocate, hole punching through various points of the SNP launch lifecycle, and observe both positive and negative vcpcu_run exit statuses. Signed-off-by: Pratik R. Sampat --- .../kvm/x86_64/coco_pre_fault_memory_test.c | 121 +++++++++++++++++- 1 file changed, 114 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/coco_pre_fault_memory_test.c b/tools/testing/selftests/kvm/x86_64/coco_pre_fault_memory_test.c index e16fe185fb5a..6ad07e2f25b4 100644 --- a/tools/testing/selftests/kvm/x86_64/coco_pre_fault_memory_test.c +++ b/tools/testing/selftests/kvm/x86_64/coco_pre_fault_memory_test.c @@ -47,6 +47,31 @@ enum prefault_snp_test_type { PREFAULT_PRIVATE_SHARED_AFTER_FINALIZING }; +enum falloc_snp_test_type { + /* Skip alloc tests. */ + NO_ALLOC_TYPE = 0, + /* + * Allocate and/or deallocate a region of guest memfd before + * memory regions are updated to be protected and encrypted + * + * This should succeed since allocation and deallocation is + * supported before the memory is finalized. + */ + ALLOC_BEFORE_UPDATE, + ALLOC_AFTER_UPDATE, + DEALLOC_BEFORE_UPDATE, + ALLOC_DEALLOC_BEFORE_UPDATE, + /* + * Allocate and/or deallocate a region of guest memfd after + * memory regions are updated to be protected and encrypted + * + * This should fail since dealloc will nuke the pages that + * contain the initial code that the guest will run. + */ + DEALLOC_AFTER_UPDATE, + ALLOC_DEALLOC_AFTER_UPDATE +}; + static void guest_code_sev(void) { int i; @@ -73,6 +98,29 @@ static void guest_code_sev(void) GUEST_DONE(); } +static void __falloc_region(struct kvm_vm *vm, bool punch_hole) +{ + int ctr, ret, flags = FALLOC_FL_KEEP_SIZE; + struct userspace_mem_region *region; + + hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) { + if (punch_hole) + flags |= FALLOC_FL_PUNCH_HOLE; + ret = fallocate(region->region.guest_memfd, flags, 0, PAGE_SIZE * TEST_NPAGES); + TEST_ASSERT(!ret, "fallocate should succeed."); + } +} + +static void gmemfd_alloc(struct kvm_vm *vm) +{ + __falloc_region(vm, false); +} + +static void gmemfd_dealloc(struct kvm_vm *vm) +{ + __falloc_region(vm, true); +} + static void __pre_fault_memory(struct kvm_vcpu *vcpu, u64 gpa, u64 size, u64 left, bool expect_fail) { @@ -137,13 +185,34 @@ static void pre_fault_memory_negative(struct kvm_vcpu *vcpu, u64 gpa, } static void pre_fault_memory_snp(struct kvm_vcpu *vcpu, struct kvm_vm *vm, - bool private, enum prefault_snp_test_type p_type) + bool private, enum prefault_snp_test_type p_type, + enum falloc_snp_test_type f_type) { + if (f_type == ALLOC_BEFORE_UPDATE || + f_type == ALLOC_DEALLOC_BEFORE_UPDATE) { + gmemfd_alloc(vm); + } + + if (f_type == DEALLOC_BEFORE_UPDATE || + f_type == ALLOC_DEALLOC_BEFORE_UPDATE) { + gmemfd_dealloc(vm); + } + if (p_type == PREFAULT_SHARED_BEFORE_FINALIZING) pre_fault_memory_negative(vcpu, TEST_GPA, SZ_2M, 0); snp_vm_launch_start(vm, SNP_POLICY); + if (f_type == ALLOC_BEFORE_UPDATE || + f_type == ALLOC_DEALLOC_BEFORE_UPDATE) { + gmemfd_alloc(vm); + } + + if (f_type == DEALLOC_BEFORE_UPDATE || + f_type == ALLOC_DEALLOC_BEFORE_UPDATE) { + gmemfd_dealloc(vm); + } + if (p_type == PREFAULT_SHARED_BEFORE_FINALIZING) pre_fault_memory_negative(vcpu, TEST_GPA, SZ_2M, 0); @@ -164,11 +233,36 @@ static void pre_fault_memory_snp(struct kvm_vcpu *vcpu, struct kvm_vm *vm, snp_vm_launch_update(vm); + if (f_type == ALLOC_AFTER_UPDATE || + f_type == ALLOC_DEALLOC_AFTER_UPDATE) { + gmemfd_alloc(vm); + } + + /* + * Hole-punch after SNP LAUNCH UPDATE is not expected to fail + * immediately, rather its affects are observed on vcpu_run() + * as the pages that contain the initial code is nuked. + */ + if (f_type == DEALLOC_AFTER_UPDATE || + f_type == ALLOC_DEALLOC_AFTER_UPDATE) { + gmemfd_dealloc(vm); + } + if (p_type == PREFAULT_SHARED_BEFORE_FINALIZING) pre_fault_memory_negative(vcpu, TEST_GPA, SZ_2M, 0); snp_vm_launch_finish(vm); + if (f_type == ALLOC_AFTER_UPDATE || + f_type == ALLOC_DEALLOC_AFTER_UPDATE) { + gmemfd_alloc(vm); + } + + if (f_type == DEALLOC_AFTER_UPDATE || + f_type == ALLOC_DEALLOC_AFTER_UPDATE) { + gmemfd_dealloc(vm); + } + /* * After finalization, pre-faulting either private or shared * ranges should work regardless of whether the pages were @@ -210,7 +304,8 @@ static void pre_fault_memory_sev(unsigned long vm_type, struct kvm_vcpu *vcpu, } static void test_pre_fault_memory_sev(unsigned long vm_type, bool private, - enum prefault_snp_test_type p_type) + enum prefault_snp_test_type p_type, + enum falloc_snp_test_type f_type) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; @@ -246,12 +341,22 @@ static void test_pre_fault_memory_sev(unsigned long vm_type, bool private, } if (vm_type == KVM_X86_SNP_VM) - pre_fault_memory_snp(vcpu, vm, private, p_type); + pre_fault_memory_snp(vcpu, vm, private, p_type, f_type); else pre_fault_memory_sev(vm_type, vcpu, vm); vcpu_run(vcpu); + /* Expect SHUTDOWN when we falloc using PUNCH_HOLE after SNP_UPDATE */ + if (vm->type == KVM_X86_SNP_VM && + (f_type == DEALLOC_AFTER_UPDATE || + f_type == ALLOC_DEALLOC_AFTER_UPDATE)) { + TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SHUTDOWN, + "Wanted SYSTEM_EVENT, got %s", + exit_reason_str(vcpu->run->exit_reason)); + goto out; + } + if (vm->type == KVM_X86_SEV_ES_VM || vm->type == KVM_X86_SNP_VM) { TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SYSTEM_EVENT, "Wanted SYSTEM_EVENT, got %s", @@ -278,7 +383,7 @@ static void test_pre_fault_memory_sev(unsigned long vm_type, bool private, static void test_pre_fault_memory(unsigned long vm_type, bool private) { - int pt; + int pt, ft; if (vm_type && !(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type))) { pr_info("Skipping tests for vm_type 0x%lx\n", vm_type); @@ -288,11 +393,13 @@ static void test_pre_fault_memory(unsigned long vm_type, bool private) switch (vm_type) { case KVM_X86_SEV_VM: case KVM_X86_SEV_ES_VM: - test_pre_fault_memory_sev(vm_type, private, NO_PREFAULT_TYPE); + test_pre_fault_memory_sev(vm_type, private, NO_PREFAULT_TYPE, NO_ALLOC_TYPE); break; case KVM_X86_SNP_VM: - for (pt = 0; pt <= PREFAULT_PRIVATE_SHARED_AFTER_FINALIZING; pt++) - test_pre_fault_memory_sev(vm_type, private, pt); + for (pt = 0; pt <= PREFAULT_PRIVATE_SHARED_AFTER_FINALIZING; pt++) { + for (ft = 0; ft <= ALLOC_DEALLOC_AFTER_UPDATE; ft++) + test_pre_fault_memory_sev(vm_type, private, pt, ft); + } break; default: abort();