From patchwork Tue Oct 22 05:48:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Manali Shukla X-Patchwork-Id: 13845139 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2043.outbound.protection.outlook.com [40.107.100.43]) (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 0523A13174B; Tue, 22 Oct 2024 05:48:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.100.43 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729576112; cv=fail; b=jWuF6odBKqHydMzmYipSGqA7Q6NPA/AmB/ba4AiFxKmXadXCMe79pZcTt12YYtf+YIGppI8khQ+vaZNjNGmxi2XDb0KL8r/7NE6FkPCddelT2GOAZv5P8AmX0fIAzawF1xLiBD0FvKZjwOSFLkf3334tRiUfMOgfP8MLeCCOnVc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729576112; c=relaxed/simple; bh=ztIQf+TAHhanXzDGvDH54mQcE/F2PCrjFil1/MixUL0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ge1eHity+liBn3u3/n7yC8gulFM8YDtf5kw9xpvSHahrEeMPe4PsZqmrZ5+xVEAdyzN6fUrKFXoJyidk08iaTq0/4dfWhfYdjoWjEpL9nua31jk+UcrvZDrmfXJZBGmVI6kdBbCIqrejE9JA9J4bCDpGy1QQFsI3RHZj0+2nBpQ= 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=mmcWh7wv; arc=fail smtp.client-ip=40.107.100.43 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="mmcWh7wv" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=XBbIzxYuyJra2My4ZOprt/oxv7FwGgXM/QudfQHoAlrUbXVm1slCKcjHX651nq3G3N37v2OoP9O5Mlo0R9I3BcW8grixcX1oXwgl4UcjtDuxjcf/PJMG5i1lm4w2CzjilACfvqXmWVu5m0b6NlbbCQtYvGqyA2MdTgOcgH2e7CgsH0jJL1O61mh79XdlnVmQnpvLVQuEgBJIjD3QNb97l88POZLFWjYgcaSowF/6TNjTLcctZg5WWEQEUqtlY9kX+p78imElS2rMJXYncoc6yVbDVdlHIDLXskN9gtcCxDWm+yJEskcvN9GGnDNVyX/1wZ4gCJ+TmcF8Vau/gMJF7A== 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=2z1p6M6PWjpN3aDYdo3GObOAnwFxmCQ7iykDKvDk68Y=; b=rjDkKXEwdYUPxfXDVBn2Os8bIb4t4YivGuvwu3aJzKdEzm+hQr02Qe+N9V3lgGCWOzgMsB+klfpQMCWy7SoEtTTkk5PXFYJuXPOmnIuvqTALolXUtxb+i/TxzMaZArXHtQHQrqPypK2fgD9rvBtdALGzpJ07GBrxtL38lF3L8sC+jJo6KWHi1D4bTuL6miy+5q7W4MUSBU0UHg3D8MIBotqqWoC/PJ/txp5lZCnjN1jW42tTEXJPnbcvMh/TOmZJMq/OFbA5fq8KrcwV/CwxZHkoWyEkxHhpSoOEqMdymwrvrYFyNZ6UTjO+1GGAdNvvdWgVghiFMFdlWWW7W8vG+w== 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=2z1p6M6PWjpN3aDYdo3GObOAnwFxmCQ7iykDKvDk68Y=; b=mmcWh7wvZ/dmxgj/HKblplgqhSw2B5u0XpmdS+7NVTzPIzd/0FO0UN9qLanm1zoMhu3eg2tIIAaE6SjWg/lEV7apaHqgbigJNEaTCKRV99mi6c8A0MgGksSDDgGlzUHXwmMAxZi0QXG6Vo2a3KsjtPUIwKiQN5TPRoVZ4mviCkU= Received: from BYAPR04CA0021.namprd04.prod.outlook.com (2603:10b6:a03:40::34) by SJ2PR12MB7990.namprd12.prod.outlook.com (2603:10b6:a03:4c3::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 05:48:27 +0000 Received: from CO1PEPF000075ED.namprd03.prod.outlook.com (2603:10b6:a03:40:cafe::a0) by BYAPR04CA0021.outlook.office365.com (2603:10b6:a03:40::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.16 via Frontend Transport; Tue, 22 Oct 2024 05:48:26 +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 CO1PEPF000075ED.mail.protection.outlook.com (10.167.249.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8093.14 via Frontend Transport; Tue, 22 Oct 2024 05:48:26 +0000 Received: from chalupa-d178host.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; Tue, 22 Oct 2024 00:48:25 -0500 From: Manali Shukla To: , CC: , , , , , , , , Subject: [PATCH v4 1/4] x86/cpufeatures: Add CPUID feature bit for Idle HLT intercept Date: Tue, 22 Oct 2024 05:48:07 +0000 Message-ID: <20241022054810.23369-2-manali.shukla@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241022054810.23369-1-manali.shukla@amd.com> References: <20241022054810.23369-1-manali.shukla@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000075ED:EE_|SJ2PR12MB7990:EE_ X-MS-Office365-Filtering-Correlation-Id: b04e3f91-c30b-4a52-fa0e-08dcf25d265d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|36860700013|82310400026|1800799024; X-Microsoft-Antispam-Message-Info: =?utf-8?q?fExT0XQaQ8KtsBSpXbW5uDqFBCxIbsN?= =?utf-8?q?PEcc6aIs7bnz1K56L7ju+fnkxogUfS5nMFjpAJW2oC3AIkDT3ueo0pXFsebDdgq6K?= =?utf-8?q?/3oWYEHmETtlFQR2QBsqRJhlp6VPE/CiRmWrzE0nXNSCwPB8P7Scvtlt3m1yf4GXy?= =?utf-8?q?HnNrdnah/N2xgzUGrqS/WN2Y381CTvsWcwnyMaWYXV/Pe7CUeBFMg5n+064G2Y+/g?= =?utf-8?q?innUMmqhVSwNExAaLev/RjpZNBbTjfcSSzpEIykXt0/pBywvhBPIgzAVu3toNyF/f?= =?utf-8?q?K4jIfTZ+WYkGNEBv0zVKDVW8lDgQlXj/bm+Wnk3F4TMRQUR2Nx4DtwYhqmlK1qGEO?= =?utf-8?q?Wb5I/qVUgU7+PKoUKkUPll5v7yR17sMBFeUgop7tQaAHxSL4cnfJ482TrYzQTjxhB?= =?utf-8?q?ZkZbkV6WYZw651vTaBnSfg3UzXM2tNW+gTKw0KMioNBxqyDt2nI+Lkq6De4BVeV/Y?= =?utf-8?q?VRqK+VLnV+hBb+FwslSIVpCllFiIWqTvNb9VrfMueYqW/WJg+vQHqFQk9fXmabiPs?= =?utf-8?q?Zzyr/ZpxD7osRWBneHrGJJeq3XvorkLiLwvu37pwXTP1cArmJlUbmkJwbwmEc8H8p?= =?utf-8?q?MWrMTWkfiAZalm406cV1blaW2wGqKGOfCXQKuGPT4YME9wwpYD4B6djXaTv8SoXPE?= =?utf-8?q?VP/OqsOQytgMvJuKkoGPxLURed27M1IxJJo/oRYZEu2bnUWqlRptwqp/jRcCvT8dv?= =?utf-8?q?+u61V//DZZYDfpLxrcHEFNehTdNBEpZcVSjG0AGK2ngkmnQETAkS7x6N57hitwGqb?= =?utf-8?q?GXaK6EcTUh22EfRaV1M9AFoA2pHxq02jKAft4UP7Dtslqu67NySWIg9XmMvTYYcid?= =?utf-8?q?sM9IIOpyTd/WhXsFh43ahJEW94hnE0Ju4tSqnxvg0vW+OIPsh39cMVvOO3KeEhASh?= =?utf-8?q?XNtV1VO+/sYYmAahlWhvPRGX7OzJCQGCjg3QkVXMQiMlCyvZrtwbW4kgq9mKslXvB?= =?utf-8?q?g8dXmCpgXvNM5Pwad+cY8OAV57UwmCNXZvrFPOQ3abKJLWoI0+d1YYNH5MdbIJYp1?= =?utf-8?q?RanG8PaLjFAsrn38zBwzA45Wte7ceCMisEn5fIeb7eRVx0rLX2OFfzjm1StBGK45c?= =?utf-8?q?ovvJ/TB6zfNWMf3/4YShte06UsYbO/bzKWK1s3p/2OJkPS1H8hG/6ni6xxq9uo+bx?= =?utf-8?q?J0jQFUMuDWWH5rP9yWLh7gdf6rAAkfZjNV57O06N8Bd8clorWX9bvbtVb31gSIE7S?= =?utf-8?q?3o5ZDxHM5ALDZsKGwOzLybWqn9FNTKtzETzp36LQCX6KgdYQHhueBPUbxbIk4AHrn?= =?utf-8?q?o5rU1cOY5vvxfkcJKof/yupEcp1iZ1StT3/0w0HvQ02vTK4Nw7G+lSweW8cKsIED5?= =?utf-8?q?ma7JoAQWx1E/7IUbK8bcsaPdaaKfmXqH7A=3D=3D?= 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)(36860700013)(82310400026)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 05:48:26.7622 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b04e3f91-c30b-4a52-fa0e-08dcf25d265d 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: CO1PEPF000075ED.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB7990 From: Manali Shukla The Idle HLT Intercept feature allows for the HLT instruction execution by a vCPU to be intercepted by the hypervisor only if there are no pending events (V_INTR and V_NMI) for the vCPU. When the vCPU is expected to service the pending events (V_INTR and V_NMI), the Idle HLT intercept won’t trigger. The feature allows the hypervisor to determine if the vCPU is idle and reduces wasteful VMEXITs. In addition to the aforementioned use case, the Idle HLT intercept feature is also used for enlightened guests who aim to securely manage events without the hypervisor’s awareness. If a HLT occurs while a virtual event is pending and the hypervisor is unaware of this pending event (as could be the case with enlightened guests), the absence of the Idle HLT intercept feature could result in a vCPU being suspended indefinitely. Presence of Idle HLT intercept feature for guests is indicated via CPUID function 0x8000000A_EDX[30]. Signed-off-by: Manali Shukla Acked-by: Borislav Petkov (AMD) --- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+) base-commit: c8d430db8eec7d4fd13a6bea27b7086a54eda6da prerequisite-patch-id: ca912571db5c004f77b70843b8dd35517ff1267f prerequisite-patch-id: 164ea3b4346f9e04bc69819278d20f5e1b5df5ed prerequisite-patch-id: 90d870f426ebc2cec43c0dd89b701ee998385455 prerequisite-patch-id: 45812b799c517a4521782a1fdbcda881237e1eda diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index dd4682857c12..7461a49e1045 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -382,6 +382,7 @@ #define X86_FEATURE_V_SPEC_CTRL (15*32+20) /* "v_spec_ctrl" Virtual SPEC_CTRL */ #define X86_FEATURE_VNMI (15*32+25) /* "vnmi" Virtual NMI */ #define X86_FEATURE_SVME_ADDR_CHK (15*32+28) /* SVME addr check */ +#define X86_FEATURE_IDLE_HLT (15*32+30) /* "" IDLE HLT intercept */ /* Intel-defined CPU features, CPUID level 0x00000007:0 (ECX), word 16 */ #define X86_FEATURE_AVX512VBMI (16*32+ 1) /* "avx512vbmi" AVX512 Vector Bit Manipulation instructions*/ From patchwork Tue Oct 22 05:48:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Manali Shukla X-Patchwork-Id: 13845140 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2044.outbound.protection.outlook.com [40.107.223.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 8F4DF13AA4C; Tue, 22 Oct 2024 05:48:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.44 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729576114; cv=fail; b=mMUN1gnYP3OBgFgLBwcWTH14F8GH04joUL4SlcdZMXjbb7xDguE1pVbXq7O9J+Z3RgIwa4o6HPonEhmj+LNVaADLnHsiK27jz9K3MZdGWTz4kiO1k1PxVTND/9pvkx8vm701/QI0TYpfOgWMzA83bWeYEyMq5rZFJZQ0/W2dq14= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729576114; c=relaxed/simple; bh=8G2UBTnNk6rmQuWOIrhpwjZAqb+Gcj/3d6etHKN0NEA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=meXH1kdfuvTV6oxZAAJTuo6CUv9qM9dy3IStP4sABOQ0L5aYrlzQhtOV/R9koMmRLllFxZV4hS1tvFXIntp79H6zp8ythdbjdLpoJ8VbmvDkEg2rqchwi8ohfl5AvRdaHlZD1/FS9UdgZpql/s5ugz8TGmMkI1yYQFwrm/DHrKU= 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=20Tin1hZ; arc=fail smtp.client-ip=40.107.223.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="20Tin1hZ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=H5Ywa4lq5UEMHcRwArUXILyEx75I8rxudIEZ++ng6QSGasDDAFkef6fkMk8ETaS0o47Gk4riz71kzn6O8U2CDw9YHQSSlHzJSYau4Q/y+etRZuTsoeHFZuX4mz57AbPK2imcbbIKSFaX63svGz2Qv8iN/qITMZVOoWhuxACakO2l+cmZEYKii8kln/3sgbO1SX4RQoEbbMly6YDoWgPEM/QclG2PDLbTi37Bl1kwuHPXtZwj6rBOPdg62pnji8eigiHUrLd0qFiDnncX2B7m8dFrq3ZR6gFjEonph/U2gRok59A/BC7kuHMR5IfunSakT18OSr7NfMe6xdbnsM0UNg== 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=EbmSpe7yBEKtmmMtQlzdmhivP1zDgXZjr9lx5yr7P0c=; b=pihTKs9xobLQu687gOUF5qBES6Wh//KKRViNxCzhyeKqbRkHSpRU0xg0hVem5jT0dG6S4aMqy26PvyT7KyKzkxiQyHr+2hN/RTcErl5sfUS1Nc9MwiWSccaq4bIbbfj6y5KDhgqMwIJwk3g0gJ9/Xoy6aIFifjo4nONZBTtwBix9xPRZO/I7o5+sK/WsVcyeC7jH6Kcp8VST2MYgkkc7o3R2dOOrQpaDZlbAMja6CkdvFnmz4c4+1KOLSzluqlotkFqqZjklsvxx0ZXHksEA8FCOc3yVEaddX9nSVYaTemvZANtbHqi5oUi1p9UceNeDUzdC4Qi9a20DMe8I+cFgYw== 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=EbmSpe7yBEKtmmMtQlzdmhivP1zDgXZjr9lx5yr7P0c=; b=20Tin1hZATkaE9w0wYlS614RjAowerkzHKZewnT2vLj20dqzVurKx5+C08pPRvF1MXsfIJzzgMgYYDNCfP+1YalCyY30eIS4sOCxrIfWgS2VQl7Dhtic6GwfDa+3esuMTLxMULCVTERQccYaLAIbnD1s02EGsjVrU+c/woe4jyI= Received: from BYAPR04CA0011.namprd04.prod.outlook.com (2603:10b6:a03:40::24) by SJ2PR12MB8848.namprd12.prod.outlook.com (2603:10b6:a03:537::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.18; Tue, 22 Oct 2024 05:48:28 +0000 Received: from CO1PEPF000075ED.namprd03.prod.outlook.com (2603:10b6:a03:40:cafe::ee) by BYAPR04CA0011.outlook.office365.com (2603:10b6:a03:40::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.29 via Frontend Transport; Tue, 22 Oct 2024 05:48: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 CO1PEPF000075ED.mail.protection.outlook.com (10.167.249.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8093.14 via Frontend Transport; Tue, 22 Oct 2024 05:48:28 +0000 Received: from chalupa-d178host.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; Tue, 22 Oct 2024 00:48:26 -0500 From: Manali Shukla To: , CC: , , , , , , , , Subject: [PATCH v4 2/4] KVM: SVM: Add Idle HLT intercept support Date: Tue, 22 Oct 2024 05:48:08 +0000 Message-ID: <20241022054810.23369-3-manali.shukla@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241022054810.23369-1-manali.shukla@amd.com> References: <20241022054810.23369-1-manali.shukla@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000075ED:EE_|SJ2PR12MB8848:EE_ X-MS-Office365-Filtering-Correlation-Id: a12727cd-bbdf-4fbe-29bd-08dcf25d2751 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|376014|82310400026; X-Microsoft-Antispam-Message-Info: =?utf-8?q?WvkP1NcffFcvNHSVWNbdWt/RvxcYPpR?= =?utf-8?q?N4Z+pAvEdjfIdCRhBDrnZCzTXhNtt9mQNHVQngvVida5Nn36y/Z4fQq9sRtFjg2QU?= =?utf-8?q?SRUNCKlHsfJ6n/7IzblQjAtnog5MkZ6DCrP0rgKaJm0s7K4y1xpa7e5P4fy7e0t9r?= =?utf-8?q?+yMIIBPT0Y8xFvG+w5LjcJD4GF1EEv011R5YWSOkGJg94Saz3bfzTVX11KOAihhiM?= =?utf-8?q?1AUaMJrIO2RF2L0qTLOatEOBGYEmBOuZ99qkw+P5eEJ0E3C6GeBnW0/bGv9/QA+LH?= =?utf-8?q?aTQ2kE1j3CmwjaMSB1rDOSjFqOteF36ioj8NwLNnDNbZ52iAJ68wgoq9HNTujtuYm?= =?utf-8?q?q7dfxEhOD3m4t7iz5fTG8aPdt2eGUOkOhLaQlGiKSAq1OkIBzQp1Q/RsDaFeo3tT6?= =?utf-8?q?xOFyfiLaHt+b/BA84mlBNQ6RxsBngIEU2atYtaXGNNh0oLRd+cbMaYlY2vTMRbfWC?= =?utf-8?q?+6HOOJfTW3F1oYNGfFleSrHr8MZtyG56QHNmXNibN9GScPW/wV6cZ27YUSe4laHw7?= =?utf-8?q?cULsMYjlMOCmQCeoPkLN7Rx4+0q2/t61y6BZ+k9MbC8cxCYNDtONEeCgaaJJIWSJ4?= =?utf-8?q?wm4yzxv48WejKKzt+D0NVZBx++1trdJ1E/Q8io80DpuSVqu/GpmNW0wxPThZ6EmPO?= =?utf-8?q?BtD0dJwxtwqCpeRCT5mtaSIfLhvBtziIznk04aAbUR+9xvDzgGPdDzhZ9i1SYOXHy?= =?utf-8?q?hzoRYT/h+dWkINzhAwcC9hsUbTAuFiDTWLsQNbHpMjzlN46efYpuSNwg92cBctboU?= =?utf-8?q?AR4sh530OioOZ9Z1bbd+e+SquSYKmONDOyM41q9VqabpweVCBBaTAxt/WmZfjxG5T?= =?utf-8?q?2HTlOulxX8pDPvrvhJSLnsIWo6sJLgTKpGJbYKr24kCYSRmVKO3I5zNOeN+9jJqAg?= =?utf-8?q?7QF7aE13xcc8uOeFsUoqJhlTch21S0qL8QgWxYFocqrFfEf1DQFkAJVS4gZG/71xY?= =?utf-8?q?JNHP18tsMayiQQDmSX5SKP2v6mCatPTLoR9NmHmnTx0oahEwIvrL3zSBnyoEKydYe?= =?utf-8?q?twGIDBhUu4N/P1sVdVEDdgmpnXrcNAGAMGrBKpNokmiAPvVO+FPmX9WbL56mAghFo?= =?utf-8?q?u/Xcm+jRAzJvsDrh8inkoIZogF4lP0uQyE0Xvi3fuJHksHQkwN7o9VZfLFJb4wNHX?= =?utf-8?q?AnjGOFiNWclKvg0oNgbD/V3au0HpCRlsOKEsQ3h7BWmx/Fr5qdf1cLnkfl19oFkKH?= =?utf-8?q?D+otYkziILBuZrDXLWkc0O9CmOL9th7cNZbF/VE/JaKlAWlESOmWJTFjiTvM0GTSh?= =?utf-8?q?RfcGppogHDOtO0rTnVXB7Jv64M7mjnLUYYpZakES7zKnDK13Nz7QVV23+WbIcSSkq?= =?utf-8?q?n2Bv/w4CSLr4nxihIyUg/zy4LdUL/i4/+A=3D=3D?= 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)(376014)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 05:48:28.3559 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a12727cd-bbdf-4fbe-29bd-08dcf25d2751 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: CO1PEPF000075ED.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8848 From: Manali Shukla The hypervisor can intercept the HLT instruction by setting the HLT-Intercept Bit in VMCB, causing a VMEXIT. This can be wasteful if there are pending V_INTR and V_NMI events, as the hypervisor must then initiate a VMRUN to handle them. If the HLT-Intercept Bit is cleared and the vCPU executes HLT while there are pending V_INTR and V_NMI events, the hypervisor won’t detect them, potentially causing indefinite suspension of the vCPU. This poses a problem for enlightened guests who wish to securely handle the events. For Secure AVIC scenarios, if a guest does a HLT while an interrupt is pending (in IRR), the hypervisor does not have a way to figure out whether the guest needs to be re-entered, as it cannot read the guest backing page. The Idle HLT intercept feature allows the hypervisor to intercept HLT execution only if there are no pending V_INTR and V_NMI events. There are two use cases for the Idle HLT intercept feature: - Secure VMs that wish to handle pending events securely without exiting to the hypervisor on HLT (Secure AVIC). - Optimization for all the VMs to avoid a wasteful VMEXIT during HLT when there are pending events. On discovering the Idle HLT Intercept, the KVM hypervisor, Sets the Idle HLT Intercept bit (bit (6), offset 0x14h) in the VMCB. When the Idle HLT Intercept bit is set, HLT Intercept bit (bit (0), offset 0xFh) should be cleared. Before entering the HLT state, the HLT instruction performs checks in following order: - The HLT intercept check, if set, it unconditionally triggers SVM_EXIT_HLT (0x78). - The Idle HLT intercept check, if set and there are no pending V_INTR or V_NMI events, triggers SVM_EXIT_IDLE_HLT (0xA6). Details about the Idle HLT intercept feature can be found in AMD APM [1]. [1]: AMD64 Architecture Programmer's Manual Pub. 24593, April 2024, Vol 2, 15.9 Instruction Intercepts (Table 15-7: IDLE_HLT). https://bugzilla.kernel.org/attachment.cgi?id=306250 Signed-off-by: Manali Shukla --- arch/x86/include/asm/svm.h | 1 + arch/x86/include/uapi/asm/svm.h | 2 ++ arch/x86/kvm/svm/svm.c | 11 ++++++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 2b59b9951c90..992050cb83d0 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -116,6 +116,7 @@ enum { INTERCEPT_INVPCID, INTERCEPT_MCOMMIT, INTERCEPT_TLBSYNC, + INTERCEPT_IDLE_HLT = 166, }; diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index 1814b413fd57..ec1321248dac 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -95,6 +95,7 @@ #define SVM_EXIT_CR14_WRITE_TRAP 0x09e #define SVM_EXIT_CR15_WRITE_TRAP 0x09f #define SVM_EXIT_INVPCID 0x0a2 +#define SVM_EXIT_IDLE_HLT 0x0a6 #define SVM_EXIT_NPF 0x400 #define SVM_EXIT_AVIC_INCOMPLETE_IPI 0x401 #define SVM_EXIT_AVIC_UNACCELERATED_ACCESS 0x402 @@ -224,6 +225,7 @@ { SVM_EXIT_CR4_WRITE_TRAP, "write_cr4_trap" }, \ { SVM_EXIT_CR8_WRITE_TRAP, "write_cr8_trap" }, \ { SVM_EXIT_INVPCID, "invpcid" }, \ + { SVM_EXIT_IDLE_HLT, "idle-halt" }, \ { SVM_EXIT_NPF, "npf" }, \ { SVM_EXIT_AVIC_INCOMPLETE_IPI, "avic_incomplete_ipi" }, \ { SVM_EXIT_AVIC_UNACCELERATED_ACCESS, "avic_unaccelerated_access" }, \ diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 9df3e1e5ae81..e86b79e975d3 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1298,8 +1298,12 @@ static void init_vmcb(struct kvm_vcpu *vcpu) svm_set_intercept(svm, INTERCEPT_MWAIT); } - if (!kvm_hlt_in_guest(vcpu->kvm)) - svm_set_intercept(svm, INTERCEPT_HLT); + if (!kvm_hlt_in_guest(vcpu->kvm)) { + if (cpu_feature_enabled(X86_FEATURE_IDLE_HLT)) + svm_set_intercept(svm, INTERCEPT_IDLE_HLT); + else + svm_set_intercept(svm, INTERCEPT_HLT); + } control->iopm_base_pa = iopm_base; control->msrpm_base_pa = __sme_set(__pa(svm->msrpm)); @@ -3353,6 +3357,7 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap, [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap, [SVM_EXIT_INVPCID] = invpcid_interception, + [SVM_EXIT_IDLE_HLT] = kvm_emulate_halt, [SVM_EXIT_NPF] = npf_interception, [SVM_EXIT_RSM] = rsm_interception, [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, @@ -3515,7 +3520,7 @@ int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code) return interrupt_window_interception(vcpu); else if (exit_code == SVM_EXIT_INTR) return intr_interception(vcpu); - else if (exit_code == SVM_EXIT_HLT) + else if (exit_code == SVM_EXIT_HLT || exit_code == SVM_EXIT_IDLE_HLT) return kvm_emulate_halt(vcpu); else if (exit_code == SVM_EXIT_NPF) return npf_interception(vcpu); From patchwork Tue Oct 22 05:48:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manali Shukla X-Patchwork-Id: 13845141 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2040.outbound.protection.outlook.com [40.107.101.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 EE78C13CA8A; Tue, 22 Oct 2024 05:48:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.101.40 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729576115; cv=fail; b=Ez5JUVIJ4iyfuRbki3meVrx9HTLBmfO6Yl+/na7cXZtLKudhoZ4lXyICMMt3840qdFKe6+X+GOuNYLp0OaAgNLNdd8t71NDj09NRKPzJiXN+unvHph4I7yZCOaKiMwaBbwZY9LTssLr9pMADXk2HiYRgxA9AUmAEN8G3+Zyw3go= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729576115; c=relaxed/simple; bh=PpEsdxTF0E2GMoW+CVNBUjw6a7ejvVYM8LFpd35YGTI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=liS/fHJDGkLBYOB34UZYufKJw5Im8isNyB3AkYg3c8ObdpMEkLWgzqqc0EomIsFx9nY7M4OTh8Vw1s7ZgrDl9ROvGZGP1OdcjhWzs7E1+Oal0ED1QWA0eMjzn52jTSNpJ8k2FEyewNqnLRMvjBcf2100jh1zheVJhsyEQrfKlSY= 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=C9L/S1Kc; arc=fail smtp.client-ip=40.107.101.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="C9L/S1Kc" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=uPnA5iq4Ac7ckrgLLpVUFSwbdj2rPUi78FL1bDyk65CkfsoZiiY/NRqfypEvLY8exiy6SJ27EtcoFHuYhUWJFecMYMREU8lFS03Xy+MJhlcgBmMJWCE+Tkov+vouq3Cx953AZ00b2o3722yhqSRyvQ5GJlbSXBkqgLJATABXAGUh4nVHtxn4i7M96qiTMZ3YZjEiEl7Db+z+adMiYOqrabNymFhsMNT1/e/gHSmIEnz7HNAqUoFYPXdNhb4gUIqUG7M4orLmfNel08lyTA5lC/8oyxUXWh+NrzFbSpziGWcLVPjRWqjH1vBreOMOc16v+xZwpFU8LyDrcjwNH8/liQ== 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=HM8DD6gt15aAnodLpHKwLeUrsrPhLQeKb2QZa/iw3mM=; b=CuqSAPzBbfcKks/e60HYGiMlw2XvdtWe/ZIjkzS7elHaekS61wcvO/uWgAceSdaRIHquOLSQF90ynKmTTNtb/TfAVNwe2b8eU5fdHvcFA5qlRjbnSCkVUZV6gpnJvwcDazPaqCI04CWsRpfXNEfIq+1TK3fBoVxpkHVxLJnZnPjYhWPMDmhDbe0yXVrK2+iDel1hjG5s+vS2o6xU6K/K9gyZ1yNKVwJoJ/K9kXVUQq5qlphoeLE2popfXQydOdhHWksAXT/g/UbcGyb4sRuAy+eeW+VarBzOCyAIg4dRoQTEZCncDzotS5RVkBrc2+8pNH/ZcSpT01gVn4Y/lAmTpQ== 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=HM8DD6gt15aAnodLpHKwLeUrsrPhLQeKb2QZa/iw3mM=; b=C9L/S1Kc2rmOy+ATtO8rwf4jp4H/cd/gR0UkaeHeOiQH7j2mxBytiqbBiVpOA6NhJlgw20E4TdHO3EOhovVq73JX7r0mD14blQGPid7ALBjSXaFOdWA8qnXXXHXzuMfgVXS2G8Ezt+p9bupOCpdB4TMUt/rb3rMU6D9l1i9Vbuc= Received: from BYAPR04CA0016.namprd04.prod.outlook.com (2603:10b6:a03:40::29) by MN0PR12MB6343.namprd12.prod.outlook.com (2603:10b6:208:3c0::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.27; Tue, 22 Oct 2024 05:48:29 +0000 Received: from CO1PEPF000075ED.namprd03.prod.outlook.com (2603:10b6:a03:40:cafe::8c) by BYAPR04CA0016.outlook.office365.com (2603:10b6:a03:40::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.29 via Frontend Transport; Tue, 22 Oct 2024 05:48:29 +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 CO1PEPF000075ED.mail.protection.outlook.com (10.167.249.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8093.14 via Frontend Transport; Tue, 22 Oct 2024 05:48:28 +0000 Received: from chalupa-d178host.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; Tue, 22 Oct 2024 00:48:27 -0500 From: Manali Shukla To: , CC: , , , , , , , , Subject: [PATCH v4 3/4] KVM: nSVM: implement the nested idle halt intercept Date: Tue, 22 Oct 2024 05:48:09 +0000 Message-ID: <20241022054810.23369-4-manali.shukla@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241022054810.23369-1-manali.shukla@amd.com> References: <20241022054810.23369-1-manali.shukla@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000075ED:EE_|MN0PR12MB6343:EE_ X-MS-Office365-Filtering-Correlation-Id: b195b657-b185-4e5b-ea0f-08dcf25d27b2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|36860700013|82310400026|1800799024; X-Microsoft-Antispam-Message-Info: v6Fcf/nkdkRRch4/EzOoT7aTvk4w5A90Cf6C1Tq7ZvTN9ZH1DRocmaHH//ydEkuQGYAdV2tl0dovi/imQOe53jgq1LtDEYAESejPRpOAimqv5hQOP0AqAGpLOAZvD5SSGrnFtubX/BtSyvTjmiVv3PM9SUG90Ysu4Y3uMDoxjruw98sGV0MLbeXLe4OnsQy2oYY8iLyzgkpAn7ArZn3pgs1ZqAdYRQouAyoyxuYgDUaA3UOcJa+3TX0WCpCUpva/QEwzNWTJbRYGhnjDKpWFUjsK5yyeiXD2xE4OCQGHABFncXgstQVoNjNFfBYeojHzKhngRG5bVFOPWQLFpHJYs9QwkQqo3yuIyhenbUZ4J5KTh2NpRGZo0xR4WlpROZHs5N6ptvde44K5XRZkn0oM6HAkq40VTVCyc0IFeqpXCAO2M89JjCkYzKH6tVlC0sCLIjyQWAvGMfPSQZy4k8LYg3fFr9oFTVtHWvcaZmntGsD8RLy2RJTbsuXMtMKcyIgvish5byLdGC4CnyYUKnfplI8Gh2JCAZTfwviZgxxUonNNf62mAvVihRP3+qCiI79nA+3Nx1KuBbCGyIumStBXYAzd8m+fUNFIVuU7JgIO7wKteviczEUX60rR+xh+Y9y/oNt+B01O4hkseBvuar841R9x1Aqgn0NzlF92bRj1EPEwOwmJr9YJHxBM+zRE5pz9oGGW9WD1TIy3iGpNgx8m1JDDfexjom/EO7o0W+lI6B9Iw/ZsLby7xfeGd0+W52JzYQqQHHo/mEc/kpX+MdQIU9e2E8QGUkn3Qla5/DMiVhOYytksTPV0x4t3Ou3h7QIblLgPWulE0ukS0kRZo6s2aPHr2tz6Eck29nYOX/xDVihs3eQTxsRXCsoxCscvPDq7xrzImkFLLFkBB/KlT0MG3OLppsVuiZuiWY87mIuaw1ZvDuYlgOmSLhCtLZEgbbLt8+wKXhLf2CxdIPowJDUlVjBD8D5OwWEd5IiI6ubPlaIJ4z02IoEpB3gBBZ2X2NKjaRdH0uFUvqbf0t3MeiSyzQpnrsjSDCsAYmO4XtoEj1HXsgmWfRxVttJwjXZqSPeNzSR7wEOMb8pW3uVjddAlxJO7xDYJOUdHDhLzKSUHeXNxvCoqsKgmqJacCJ7FGuLDN/q6rmo7Od9ENQxTIk79FimaZgfMgi/SYgsASuT2A8/sr2b6rEP8RiO4de+qm6+DriK03zlg53eP2H/cMfxCBQVsN6olVJ1+BiBPPO6k+Tq9C4yhgVlrQineGtJDalKQ2/KuXnrkBJJjDnP4ME5RorjsvlZ9TjLD7j1ynr/XbhWF/Z0bbJfpkToqToq77lslYlSzIk1GtyBfJCVHXENRzOjQgxV+sItcWzs8+1lEVZHE0JQAh/6VStc4ktQHCFgFImQ9XcMB/0eyVbk7gFPK8g== 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)(36860700013)(82310400026)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 05:48:28.9809 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b195b657-b185-4e5b-ea0f-08dcf25d27b2 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: CO1PEPF000075ED.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6343 Enable the idle HLT intercept for the L2 guest when it is available on the platform and the L2 guest can utilize the Idle HLT intercept feature. This is achieved by clearing the HLT intercept for the L1 hypervisor in the recalc_interrupts() function. The Idle HLT intercept requires that the HLT intercept is cleared and the Idle HLT intercept is set to properly enable the feature. The nested idle halt intercept was tested by booting L1,L2 (all Linux) and checking there are only idle-hlt vmexits happened. Signed-off-by: Manali Shukla --- arch/x86/kvm/governed_features.h | 1 + arch/x86/kvm/svm/nested.c | 8 ++++++++ arch/x86/kvm/svm/svm.c | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/arch/x86/kvm/governed_features.h b/arch/x86/kvm/governed_features.h index ad463b1ed4e4..0154012721cb 100644 --- a/arch/x86/kvm/governed_features.h +++ b/arch/x86/kvm/governed_features.h @@ -17,6 +17,7 @@ KVM_GOVERNED_X86_FEATURE(PFTHRESHOLD) KVM_GOVERNED_X86_FEATURE(VGIF) KVM_GOVERNED_X86_FEATURE(VNMI) KVM_GOVERNED_X86_FEATURE(LAM) +KVM_GOVERNED_X86_FEATURE(IDLE_HLT) #undef KVM_GOVERNED_X86_FEATURE #undef KVM_GOVERNED_FEATURE diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index d5314cb7dff4..feb241110f1a 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -178,6 +178,14 @@ void recalc_intercepts(struct vcpu_svm *svm) } else { WARN_ON(!(c->virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK)); } + + /* + * Clear the HLT intercept for L2 guest when the Idle HLT intercept feature + * is enabled on the platform and the guest can use the Idle HLT intercept + * feature. + */ + if (guest_can_use(&svm->vcpu, X86_FEATURE_IDLE_HLT)) + vmcb_clr_intercept(c, INTERCEPT_HLT); } /* diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index e86b79e975d3..38d546788fc6 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4425,6 +4425,7 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) kvm_governed_feature_check_and_set(vcpu, X86_FEATURE_PFTHRESHOLD); kvm_governed_feature_check_and_set(vcpu, X86_FEATURE_VGIF); kvm_governed_feature_check_and_set(vcpu, X86_FEATURE_VNMI); + kvm_governed_feature_check_and_set(vcpu, X86_FEATURE_IDLE_HLT); svm_recalc_instruction_intercepts(vcpu, svm); @@ -5228,6 +5229,9 @@ static __init void svm_set_cpu_caps(void) if (vnmi) kvm_cpu_cap_set(X86_FEATURE_VNMI); + if (cpu_feature_enabled(X86_FEATURE_IDLE_HLT)) + kvm_cpu_cap_set(X86_FEATURE_IDLE_HLT); + /* Nested VM can receive #VMEXIT instead of triggering #GP */ kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK); } From patchwork Tue Oct 22 05:48:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manali Shukla X-Patchwork-Id: 13845142 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2085.outbound.protection.outlook.com [40.107.220.85]) (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 D76E913D8A3; Tue, 22 Oct 2024 05:48:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.85 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729576117; cv=fail; b=U0fNBGpWkzlCNXG9pvOvTVKUWwGK3Wh0YaxGrCDYat0quDIN51jBWVxvhXxU/OhSdv0S2g0098rQHxB/RCDf5OTzH8yn4Tr7HqWO4JTK0kkWnrU+k9KAgla6sMNv//b6UOO4rWVHh2MWxahm7Y+/YRPZcI0daE+kyk8AXMzat80= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729576117; c=relaxed/simple; bh=KQ8Mg8m6P94QNtSTdZclvzfNwVhh/10TsjnisGgslvo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TkMqje3h/z3aCSKt34zVDGEXX8O3smogCKgcaWZU4tf0XKRtPqfykEbVYVI7GlBqqV3nm3PcqwQ+6uMpbTzIEeG0M9b3Nn1++lb7Ou9JSlWl2VTv0y9os3kHRvr2UAkpUXgQ4onatNSKN0F/nchWmx+/Sbv8i1OrUlCJ93oY35k= 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=NupElujd; arc=fail smtp.client-ip=40.107.220.85 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="NupElujd" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=QP2uEeABsdrCm6aeHakteja3wnF8xxZR/1V5lfEpAOWJMOg/wAVb66VfKGgoBFtoZPQ5MAFw3yPHJ5Wb+iBKhAtBNBvQKjIPGeIfWZPo85ky8RxctxpfVyEnDAcaTmQMlz2v/vY6bmBr1wIPyxyrt5vms+K+Kwksmgxkt296L92YtqxLuUp5yTrWhjWXvu2wMjT6AKaTL2IUUOg+QC1v0/iWFF7ivwdzRG4JGN1ZAdjb1JDzsCKCeo/XX5cdk6d/uCaQnRTBGEWoLePgzkWus9BR/7rjfUYCBAydwMrKtfgoyQWoX0F7HO93QKoPcqNt6lD8hZTWIEoGRYoqxaRIOw== 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=i1sWCVnMPzhuQjhvBehanKowo8g+FB0kzW25tpSpa0g=; b=S2OqesI1HzIVzPaRvDnqGdQMWPx8Ar54CuhOPozfTzIIJ/ef9czeKYwUQysmWt7jExPJfZk3ZAV8GedL5DxgA3DyQaewbWyW8KwOX3VcSSsRA7UE85/kuc4OwSmNEQGyBLUqBiwqeN+jbw+uNb5WlOKqgkyXhCeFnyylKPf2+Ok3KvcukYiQPXxh1SlO0o2/3H9wwbQ1TeBLUx4a0Hsg/B+dq67Q2QtIxfCq1Vx8/+oVaRBJdvt1OpUo5nUJZ4LPipQfTgW6F/2LzfIrkngYKwiheCNFWVdoRDcWrHrzrYJgrJLjYEfYn3hppHBb8GeObmyYIN8UBWbQuLRhFz8LUw== 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=i1sWCVnMPzhuQjhvBehanKowo8g+FB0kzW25tpSpa0g=; b=NupElujd2rdb5+mAp9MyGS3A8EpVBLF5Iapd6uHzQOn2EkbZ6N7PWT2W1Yudr5bpDjzzx5bmftqbr5qM1msuwdUToAd6mNqRbyrhlM4Rjp9HEdAh36jnkRuX+1XpaE8yrjTjZcfz+oA4a7QGBKy/MNZpKHRN+ROEkWhl3ak+Sp0= Received: from BYAPR04CA0028.namprd04.prod.outlook.com (2603:10b6:a03:40::41) by PH7PR12MB6978.namprd12.prod.outlook.com (2603:10b6:510:1b8::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.29; Tue, 22 Oct 2024 05:48:30 +0000 Received: from CO1PEPF000075ED.namprd03.prod.outlook.com (2603:10b6:a03:40:cafe::ec) by BYAPR04CA0028.outlook.office365.com (2603:10b6:a03:40::41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.29 via Frontend Transport; Tue, 22 Oct 2024 05:48:30 +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 CO1PEPF000075ED.mail.protection.outlook.com (10.167.249.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8093.14 via Frontend Transport; Tue, 22 Oct 2024 05:48:30 +0000 Received: from chalupa-d178host.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; Tue, 22 Oct 2024 00:48:28 -0500 From: Manali Shukla To: , CC: , , , , , , , , Subject: [PATCH v4 4/4] KVM: selftests: KVM: SVM: Add Idle HLT intercept test Date: Tue, 22 Oct 2024 05:48:10 +0000 Message-ID: <20241022054810.23369-5-manali.shukla@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241022054810.23369-1-manali.shukla@amd.com> References: <20241022054810.23369-1-manali.shukla@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000075ED:EE_|PH7PR12MB6978:EE_ X-MS-Office365-Filtering-Correlation-Id: ed08898a-c85e-43f4-5860-08dcf25d2863 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|82310400026|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: BvGYQZ4mSTIKyvpqa8c15e6SzOqermbI6jkwlM5cqXVJYxM5C1LcDzUxFTGRKg5jScbFa8C/2Kl1oavakDf5jvxedu6oUsTMyhy6kTkJnOc52gb8VDKkrVQqkHa7mHknIqi+x0iXjJWaZ7+BuBUL1dlLPTdekG0ybLt/eKzn6Sr77BzhpgfNsGdbhkHQ3giQsftenPjE/cgUiRPEmkMNC4k0gxMFQEQlMpn4JAHlL1QfVomvJT1G7BhDXDBATI9LwQ0GYsr9CGBGB5HIeoktFGQbDwHItv1364NgR8kSiaQGgdkH/tHetwx8y5XF25fyIST1NPwa/PYIkpi5Df8RBSjR251ADXQp6AKRY+i8WjeCk6br7oWv2DI6QfTMW3lmO7aBXrk8Ha8zcskVfoCxM77I7Jq9PJ0x+EgPit6iZS1UAq/Vi4fgMvGWPR84IfIn59tXavVQQVX+4uoh1ngN/pxz7KAT5jqwdj3+eknzT2oZ37x6ZGktzqASGSdGDg6drXpaNgVyUkOeUZE7QVEUXT0p798Am9MPXnAEx+Y/7vSr9Jkyq3NxyZ9LZfgcPWcAy+4hJ0SgrvXMaO/HteYr97qymWTfF5A5NSlZuLpp76hyjCfjKwWzprky4sVKjIyEvbeidieIeXyjtkoC0zAVrBpahEN4x8DGEIUaoMZZhBY2hswBOzKluKaaCGuMEJyDayAccn/NayLzG8Xh4v0M9SyKo7nZLYmYI37YsxttI8E0lKPsw80brFrHFmxewqxBKkYKxiCs9Ahe2FNa+0c4OToCagsOe5ol8+uQKbA/CT0w5yJLD3A6sSij5JmDibxC0ivHVQdfw1oD/Rw+UCubWZKuNnosYPwNjyRCdHxBTVzcgXnRgkJvL9zKaPp0fGQQ52N2c/kf5hoVshD9Zu97wjJsimLXGlsh4iNimwHZ3zQnuj5P2sstxu+H/pLFXwzdxKMGADsPhXtZ+wT3fmIEkcVLFu4Iyx6F7rkxojy0HEiEhWUTSPDT+rGOSqeetb6L+Q/VFpI0dchkJQNIK3dr7knCPrM5I0T4h/V6ok+rYi92ry/Tp3R9wMw6CUFfXZ/rEakrDTquFYyrIwqWmBqYKcY63QK2s7P45Ynn+/grAasGuj0fEnvWsZ0K2+6B9Lq6jszMbB6lPoj04FZvEp11ExNpcZm8epncLYvOjfxo7SZ7imaH/1LPnZW+WmEayMMW8fbKB/MAD+JdsyzNytCrEs2GvD/O6QPRKtZATM2HB+4x+RYkmkbzr39EbuXfvDs6x2rfEiMEXB2RHdmLl7/EuJjvoe0Qpd7ujqJ5vxFcw1mWvHeI6PFUW2CsgwfdG/rN9/72q5ibs46fwXcUjO55t2jy1UuzZ8CI3/aDdTJrkTp7oU6Mjl6qZwbAg6nzKDY8UlXVKrT19b7pbywPyJBimA== 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)(36860700013)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 05:48:30.1528 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ed08898a-c85e-43f4-5860-08dcf25d2863 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: CO1PEPF000075ED.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6978 From: Manali Shukla Execution of the HLT instruction results in VMEXIT. Hypervisor observes pending V_INTR and V_NMI events just after VMEXIT generated by HLT for the vCPU and causes VM entry to service the pending events. The Idle HLT intercept feature avoids the wasteful VMEXIT during halt if there are pending V_INTR and V_NMI events for the vCPU. The selftest for Idle HLT intercept instruments above-mentioned scenario. Signed-off-by: Manali Shukla --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/x86_64/svm_idle_hlt_test.c | 89 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/svm_idle_hlt_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 960cf6a77198..25e4bc9473d5 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -94,6 +94,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test TEST_GEN_PROGS_x86_64 += x86_64/smm_test TEST_GEN_PROGS_x86_64 += x86_64/state_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_preemption_timer_test +TEST_GEN_PROGS_x86_64 += x86_64/svm_idle_hlt_test TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test TEST_GEN_PROGS_x86_64 += x86_64/svm_int_ctl_test TEST_GEN_PROGS_x86_64 += x86_64/svm_nested_shutdown_test diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 8e36de85b68f..41815d6ae500 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -197,6 +197,7 @@ struct kvm_x86_cpu_feature { #define X86_FEATURE_PAUSEFILTER KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 10) #define X86_FEATURE_PFTHRESHOLD KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 12) #define X86_FEATURE_VGIF KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16) +#define X86_FEATURE_IDLE_HLT KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 30) #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) diff --git a/tools/testing/selftests/kvm/x86_64/svm_idle_hlt_test.c b/tools/testing/selftests/kvm/x86_64/svm_idle_hlt_test.c new file mode 100644 index 000000000000..fe2ea96695e4 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/svm_idle_hlt_test.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 Advanced Micro Devices, Inc. + * + */ +#include +#include +#include +#include "svm_util.h" +#include "apic.h" + +#define VINTR_VECTOR 0x30 +#define NUM_ITERATIONS 1000 + +static bool irq_received; + +/* + * The guest code instruments the scenario where there is a V_INTR pending + * event available while hlt instruction is executed. The HLT VM Exit doesn't + * occur in above-mentioned scenario if Idle HLT intercept feature is enabled. + */ + +static void guest_code(void) +{ + uint32_t icr_val; + int i; + + xapic_enable(); + + icr_val = (APIC_DEST_SELF | APIC_INT_ASSERT | VINTR_VECTOR); + + for (i = 0; i < NUM_ITERATIONS; i++) { + cli(); + xapic_write_reg(APIC_ICR, icr_val); + safe_halt(); + GUEST_ASSERT(READ_ONCE(irq_received)); + WRITE_ONCE(irq_received, false); + } + GUEST_DONE(); +} + +static void guest_vintr_handler(struct ex_regs *regs) +{ + WRITE_ONCE(irq_received, true); + xapic_write_reg(APIC_EOI, 0x00); +} + +int main(int argc, char *argv[]) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + struct ucall uc; + uint64_t halt_exits, vintr_exits; + + /* Check the extension for binary stats */ + TEST_REQUIRE(this_cpu_has(X86_FEATURE_IDLE_HLT)); + TEST_REQUIRE(kvm_has_cap(KVM_CAP_BINARY_STATS_FD)); + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + + vm_install_exception_handler(vm, VINTR_VECTOR, guest_vintr_handler); + virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA); + + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + + halt_exits = vcpu_get_stat(vcpu, halt_exits); + vintr_exits = vcpu_get_stat(vcpu, irq_window_exits); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + /* NOT REACHED */ + case UCALL_DONE: + break; + + default: + TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd); + } + + TEST_ASSERT_EQ(halt_exits, 0); + pr_debug("Guest executed VINTR followed by halts: %d times.\n" + "The guest exited due to halt: %ld times and number\n" + "of vintr exits: %ld.\n", + NUM_ITERATIONS, halt_exits, vintr_exits); + + kvm_vm_free(vm); + return 0; +}