x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests
authorNikunj A Dadhania <nikunj@amd.com>
Mon, 6 Jan 2025 12:46:28 +0000 (18:16 +0530)
committerBorislav Petkov (AMD) <bp@alien8.de>
Tue, 7 Jan 2025 20:26:20 +0000 (21:26 +0100)
The hypervisor should not be intercepting RDTSC/RDTSCP when Secure TSC is
enabled. A #VC exception will be generated if the RDTSC/RDTSCP instructions
are being intercepted. If this should occur and Secure TSC is enabled,
guest execution should be terminated as the guest cannot rely on the TSC
value provided by the hypervisor.

Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
Tested-by: Peter Gonda <pgonda@google.com>
Link: https://lore.kernel.org/r/20250106124633.1418972-9-nikunj@amd.com
arch/x86/coco/sev/shared.c

index 71de5319408910b251930eb1fb3598b3fb605d93..4386f37bd31d54a636eb30961d4b7bb3c88280a0 100644 (file)
@@ -1140,6 +1140,16 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb,
        bool rdtscp = (exit_code == SVM_EXIT_RDTSCP);
        enum es_result ret;
 
+       /*
+        * The hypervisor should not be intercepting RDTSC/RDTSCP when Secure
+        * TSC is enabled. A #VC exception will be generated if the RDTSC/RDTSCP
+        * instructions are being intercepted. If this should occur and Secure
+        * TSC is enabled, guest execution should be terminated as the guest
+        * cannot rely on the TSC value provided by the hypervisor.
+        */
+       if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
+               return ES_VMM_ERROR;
+
        ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0);
        if (ret != ES_OK)
                return ret;